@tilua-types/sunc 7.1.0 → 8.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lowering.mjs CHANGED
@@ -1,9 +1,28 @@
1
1
  // @ts-check
2
2
  /**
3
- * What tilua lowers for an executor: the functions whose Luau answer is
4
- * several values. tilua has one value where Luau has several — an array — so
5
- * each of these is called through runtime/sunc.luau, which gathers them. A
6
- * global read as a value gets the same function, so the two never disagree.
3
+ * What tilua lowers for an executor: its namespaces.
4
+ *
5
+ * Every function of the library is in a namespace — `env.getgenv()`,
6
+ * `fs.read(path)` — and no executor has those namespaces as they are
7
+ * written. runtime/sunc.luau builds each from whatever the executor has, under
8
+ * whichever spelling it has, and the compiler reads the namespace from there:
9
+ *
10
+ * fs.read("a.txt") -> tilua_sunc.fs.read("a.txt")
11
+ * const files = fs -> local files = tilua_sunc.fs
12
+ *
13
+ * The runtime's functions also answer several Luau values as tilua has them
14
+ * (`executor.identify()` is one array), and `http.request(types => ...)` builds
15
+ * a body that holds what a Luau table cannot.
16
+ *
17
+ * And the executor's own globals are out of a script's reach. Each name the
18
+ * runtime reads one by — `readfile`, `getrawmetatable`, `syn` — is `nil`
19
+ * where a script writes it (and reported, nothing declaring it), as if the
20
+ * script began with `local readfile = nil`. The executor's environment itself
21
+ * is not touched: other scripts share it, and `env.getgenv().readfile` is
22
+ * still there for whoever means it.
23
+ *
24
+ * `debug` is Lua's own global, which the library only adds to: it is left as
25
+ * it is.
7
26
  */
8
27
  import { readFileSync } from "node:fs"
9
28
  import { dirname, join } from "node:path"
@@ -14,22 +33,37 @@ import { fileURLToPath } from "node:url"
14
33
  const here = dirname(fileURLToPath(import.meta.url))
15
34
  const sunc = readFileSync(join(here, "runtime", "sunc.luau"), "utf8")
16
35
 
17
- /** The globals the runtime answers for. */
18
- const GATHERED = new Set(["gethiddenproperty", "loadfile", "identifyexecutor", "getexecutorname", "crypt.encrypt"])
36
+ /** The namespaces the runtime builds. */
37
+ const NAMESPACES = new Set([...sunc.matchAll(/^__NAME__\.(\w+) = \{/gm)].map(m => m[1]))
19
38
 
20
- /** @type {(runtime: string, name: string) => string} */
21
- const member = (runtime, name) => `${runtime}.${name.replace(".", "_")}`
39
+ /** The executor's globals the runtime reads: every name handed to `pick` (or
40
+ * to `field`, a table it looks in), taken from the Luau so the two cannot
41
+ * drift apart. */
42
+ const EXECUTOR_GLOBALS = (() => {
43
+ const names = new Set()
44
+ for (const start of sunc.matchAll(/(?<!function )\bpick\(/g)) {
45
+ let depth = 0
46
+ let end = start.index + start[0].length - 1
47
+ for (; end < sunc.length; end++) {
48
+ if (sunc[end] === "(") depth++
49
+ else if (sunc[end] === ")" && --depth === 0) break
50
+ }
51
+ const args = sunc.slice(start.index + start[0].length, end).replace(/"[^"]*"/g, "")
52
+ for (const [name] of args.matchAll(/\b[A-Za-z_]\w*\b/g)) {
53
+ if (name !== "field" && name !== "nil") names.add(name)
54
+ }
55
+ }
56
+ return names
57
+ })()
22
58
 
23
59
  /** @type {LoweringPlugin} */
24
60
  const plugin = {
25
61
  runtime: { sunc },
26
62
 
27
- globalCall({ name, use }) {
28
- return GATHERED.has(name) ? { callee: member(use("sunc"), name) } : undefined
29
- },
30
-
31
63
  globalValue({ name, use }) {
32
- return GATHERED.has(name) ? member(use("sunc"), name) : undefined
64
+ if (NAMESPACES.has(name)) return `${use("sunc")}.${name}`
65
+ if (EXECUTOR_GLOBALS.has(name)) return "nil"
66
+ return undefined
33
67
  },
34
68
  }
35
69
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tilua-types/sunc",
3
- "version": "7.1.0",
3
+ "version": "8.0.0",
4
4
  "description": "The script-executor environment as tilua types: sUNC and UNC names (getgenv, writefile, hookfunction, Drawing, ...)",
5
5
  "license": "ISC",
6
6
  "files": [
@@ -14,7 +14,7 @@
14
14
  "lowering": "lowering.mjs"
15
15
  },
16
16
  "dependencies": {
17
- "@tilua-types/roblox": "^7.1.0"
17
+ "@tilua-types/roblox": "^8.0.0"
18
18
  },
19
19
  "keywords": [
20
20
  "tilua",
package/runtime/sunc.luau CHANGED
@@ -1,26 +1,278 @@
1
- local __NAME__ = {}
2
-
3
- -- The executor functions whose Luau answer is several values, as tilua has
4
- -- them: one array. The compiler calls these instead of the globals they stand
5
- -- for, and reading one of those globals as a value gives the function here.
6
-
7
- __NAME__.gethiddenproperty = function(object, property)
8
- return { gethiddenproperty(object, property) }
9
- end
10
-
11
- __NAME__.loadfile = function(path, chunkname)
12
- local f, message = loadfile(path, chunkname)
13
- return { f, message }
14
- end
15
-
16
- __NAME__.identifyexecutor = function()
17
- return { identifyexecutor() }
18
- end
19
-
20
- __NAME__.getexecutorname = function()
21
- return { getexecutorname() }
22
- end
23
-
24
- __NAME__.crypt_encrypt = function(...)
25
- return { crypt.encrypt(...) }
26
- end
1
+ local __NAME__ = {}
2
+
3
+ -- The namespaces of index.d.tilua — `env`, `file`, `closure`, ... — built from
4
+ -- whatever the executor has. The compiler reads each namespace from here: none
5
+ -- of them is the executor's own global, and most executors have none of them.
6
+ --
7
+ -- Each function is looked up once, as the script starts, under every spelling
8
+ -- executors use for it; the first one there is the one called. With none of
9
+ -- them, calling it says so, rather than "attempt to call a nil value".
10
+
11
+ local function missing(name)
12
+ return function()
13
+ error(name .. " is not available in this executor", 2)
14
+ end
15
+ end
16
+
17
+ local function pick(name, ...)
18
+ for i = 1, select("#", ...) do
19
+ local found = select(i, ...)
20
+ if found ~= nil then
21
+ return found
22
+ end
23
+ end
24
+ return missing(name)
25
+ end
26
+
27
+ -- `t[key]`, where `t` may not be a table at all — `syn.request` in an
28
+ -- executor with no `syn`.
29
+ local function field(t, key)
30
+ if type(t) == "table" then
31
+ return t[key]
32
+ end
33
+ return nil
34
+ end
35
+
36
+ -- A function whose Luau answer is several values, answering them as tilua
37
+ -- has them: one array.
38
+ local function gathered(f)
39
+ return function(...)
40
+ return { f(...) }
41
+ end
42
+ end
43
+
44
+ -- ============================================================
45
+ -- Namespaces
46
+ -- ============================================================
47
+
48
+ __NAME__.env = {
49
+ getgenv = pick("env.getgenv", getgenv),
50
+ getrenv = pick("env.getrenv", getrenv),
51
+ getreg = pick("env.getreg", getreg),
52
+ getgc = pick("env.getgc", getgc),
53
+ filtergc = pick("env.filtergc", filtergc),
54
+ }
55
+
56
+ __NAME__.closure = {
57
+ hook = pick("closure.hook", hookfunction, replaceclosure),
58
+ restore = pick("closure.restore", restorefunction),
59
+ clone = pick("closure.clone", clonefunction),
60
+ newcclosure = pick("closure.newcclosure", newcclosure),
61
+ iscclosure = pick("closure.iscclosure", iscclosure),
62
+ islclosure = pick("closure.islclosure", islclosure),
63
+ isexecutorclosure = pick("closure.isexecutorclosure", isexecutorclosure, isourclosure, checkclosure),
64
+ checkcaller = pick("closure.checkcaller", checkcaller),
65
+ hash = pick("closure.hash", getfunctionhash),
66
+ bytecode = pick("closure.bytecode", getfunctionbytecode),
67
+ }
68
+
69
+ __NAME__.metatable = {
70
+ getraw = pick("metatable.getraw", getrawmetatable),
71
+ setraw = pick("metatable.setraw", setrawmetatable),
72
+ isreadonly = pick("metatable.isreadonly", isreadonly),
73
+ setreadonly = pick("metatable.setreadonly", setreadonly),
74
+ hook = pick("metatable.hook", hookmetamethod),
75
+ getnamecallmethod = pick("metatable.getnamecallmethod", getnamecallmethod),
76
+ }
77
+
78
+ __NAME__.scripts = {
79
+ calling = pick("scripts.calling", getcallingscript),
80
+ all = pick("scripts.all", getscripts),
81
+ running = pick("scripts.running", getrunningscripts),
82
+ loadedmodules = pick("scripts.loadedmodules", getloadedmodules),
83
+ fromthread = pick("scripts.fromthread", getscriptfromthread),
84
+ env = pick("scripts.env", getsenv),
85
+ bytecode = pick("scripts.bytecode", getscriptbytecode, dumpstring),
86
+ closure = pick("scripts.closure", getscriptclosure, getscriptfunction),
87
+ hash = pick("scripts.hash", getscripthash),
88
+ decompile = pick("scripts.decompile", decompile),
89
+ }
90
+
91
+ __NAME__.identity = {
92
+ get = pick("identity.get", getthreadidentity, getidentity, getthreadcontext),
93
+ set = pick("identity.set", setthreadidentity, setidentity, setthreadcontext),
94
+ }
95
+
96
+ local gethidden = pick("instance.gethidden", gethiddenproperty)
97
+
98
+ __NAME__.instance = {
99
+ cloneref = pick("instance.cloneref", cloneref),
100
+ compare = pick("instance.compare", compareinstances),
101
+ all = pick("instance.all", getinstances),
102
+ nilparented = pick("instance.nilparented", getnilinstances),
103
+ gethui = pick("instance.gethui", gethui),
104
+ gethidden = gathered(gethidden),
105
+ sethidden = pick("instance.sethidden", sethiddenproperty),
106
+ isscriptable = pick("instance.isscriptable", isscriptable),
107
+ setscriptable = pick("instance.setscriptable", setscriptable),
108
+ getcallback = pick("instance.getcallback", getcallbackvalue),
109
+ fireclickdetector = pick("instance.fireclickdetector", fireclickdetector),
110
+ fireproximityprompt = pick("instance.fireproximityprompt", fireproximityprompt),
111
+ firetouch = pick("instance.firetouch", firetouchinterest),
112
+ getobjects = pick("instance.getobjects", getobjects),
113
+ isnetworkowner = pick("instance.isnetworkowner", isnetworkowner),
114
+ save = pick("instance.save", saveinstance),
115
+ }
116
+
117
+ __NAME__.cache = {
118
+ invalidate = pick("cache.invalidate", field(cache, "invalidate")),
119
+ replace = pick("cache.replace", field(cache, "replace")),
120
+ iscached = pick("cache.iscached", field(cache, "iscached")),
121
+ }
122
+
123
+ __NAME__.signal = {
124
+ connections = pick("signal.connections", getconnections),
125
+ fire = pick("signal.fire", firesignal),
126
+ replicate = pick("signal.replicate", replicatesignal),
127
+ }
128
+
129
+ local loadfileOf = pick("fs.loadfile", loadfile)
130
+
131
+ __NAME__.fs = {
132
+ read = pick("fs.read", readfile),
133
+ write = pick("fs.write", writefile),
134
+ append = pick("fs.append", appendfile),
135
+ delete = pick("fs.delete", delfile),
136
+ isfile = pick("fs.isfile", isfile),
137
+ list = pick("fs.list", listfiles),
138
+ makefolder = pick("fs.makefolder", makefolder),
139
+ deletefolder = pick("fs.deletefolder", delfolder),
140
+ isfolder = pick("fs.isfolder", isfolder),
141
+ dofile = pick("fs.dofile", dofile),
142
+ loadfile = function(path, chunkname)
143
+ local f, message = loadfileOf(path, chunkname)
144
+ return { f, message }
145
+ end,
146
+ getcustomasset = pick("fs.getcustomasset", getcustomasset),
147
+ }
148
+
149
+ __NAME__.websocket = {
150
+ connect = pick("websocket.connect", field(WebSocket, "connect"), field(WebSocket, "Connect")),
151
+ }
152
+
153
+ __NAME__.encoding = {
154
+ base64encode = pick("encoding.base64encode", base64encode, field(crypt, "base64encode"), field(crypt, "base64_encode"),
155
+ field(field(crypt, "base64"), "encode"), field(base64, "encode"), base64_encode),
156
+ base64decode = pick("encoding.base64decode", base64decode, field(crypt, "base64decode"), field(crypt, "base64_decode"),
157
+ field(field(crypt, "base64"), "decode"), field(base64, "decode"), base64_decode),
158
+ lz4compress = pick("encoding.lz4compress", lz4compress),
159
+ lz4decompress = pick("encoding.lz4decompress", lz4decompress),
160
+ }
161
+
162
+ __NAME__.crypt = {
163
+ encrypt = gathered(pick("crypt.encrypt", field(crypt, "encrypt"))),
164
+ decrypt = pick("crypt.decrypt", field(crypt, "decrypt")),
165
+ hash = pick("crypt.hash", field(crypt, "hash")),
166
+ generatebytes = pick("crypt.generatebytes", field(crypt, "generatebytes")),
167
+ generatekey = pick("crypt.generatekey", field(crypt, "generatekey")),
168
+ }
169
+
170
+ __NAME__.drawing = {
171
+ new = pick("drawing.new", field(Drawing, "new")),
172
+ fonts = field(Drawing, "Fonts"),
173
+ clear = pick("drawing.clear", cleardrawcache),
174
+ getproperty = pick("drawing.getproperty", getrenderproperty),
175
+ setproperty = pick("drawing.setproperty", setrenderproperty),
176
+ isdrawing = pick("drawing.isdrawing", isrenderobj),
177
+ }
178
+
179
+ __NAME__.rconsole = {
180
+ create = pick("rconsole.create", rconsolecreate, consolecreate),
181
+ destroy = pick("rconsole.destroy", rconsoledestroy, consoledestroy),
182
+ clear = pick("rconsole.clear", rconsoleclear, consoleclear),
183
+ print = pick("rconsole.print", rconsoleprint, consoleprint),
184
+ settitle = pick("rconsole.settitle", rconsolesettitle, rconsolename, consolesettitle),
185
+ input = pick("rconsole.input", rconsoleinput, consoleinput),
186
+ }
187
+
188
+ __NAME__.input = {
189
+ isactive = pick("input.isactive", isrbxactive, isgameactive),
190
+ mouse1click = pick("input.mouse1click", mouse1click),
191
+ mouse1press = pick("input.mouse1press", mouse1press),
192
+ mouse1release = pick("input.mouse1release", mouse1release),
193
+ mouse2click = pick("input.mouse2click", mouse2click),
194
+ mouse2press = pick("input.mouse2press", mouse2press),
195
+ mouse2release = pick("input.mouse2release", mouse2release),
196
+ mousemoveabs = pick("input.mousemoveabs", mousemoveabs),
197
+ mousemoverel = pick("input.mousemoverel", mousemoverel),
198
+ mousescroll = pick("input.mousescroll", mousescroll),
199
+ keypress = pick("input.keypress", keypress),
200
+ keyrelease = pick("input.keyrelease", keyrelease),
201
+ keyclick = pick("input.keyclick", keyclick),
202
+ }
203
+
204
+ __NAME__.executor = {
205
+ identify = gathered(pick("executor.identify", identifyexecutor, getexecutorname)),
206
+ hwid = pick("executor.hwid", gethwid),
207
+ setfpscap = pick("executor.setfpscap", setfpscap),
208
+ getfpscap = pick("executor.getfpscap", getfpscap),
209
+ messagebox = pick("executor.messagebox", messagebox),
210
+ }
211
+
212
+ __NAME__.clipboard = {
213
+ set = pick("clipboard.set", setclipboard, toclipboard),
214
+ setrbx = pick("clipboard.setrbx", setrbxclipboard),
215
+ }
216
+
217
+ __NAME__.teleport = {
218
+ queue = pick("teleport.queue", queue_on_teleport, queueonteleport, queueteleport, field(syn, "queue_on_teleport")),
219
+ }
220
+
221
+ __NAME__.actor = {
222
+ all = pick("actor.all", getactors),
223
+ run = pick("actor.run", run_on_actor),
224
+ isparallel = pick("actor.isparallel", isparallel),
225
+ }
226
+
227
+ -- ============================================================
228
+ -- http.request(types => ...)
229
+ -- ============================================================
230
+ --
231
+ -- What a request body can hold that a Luau table cannot: a key whose value
232
+ -- is nil. A body built by a function may hold `types.null` and
233
+ -- `types.undefined` for it, and a body that is a table is sent as JSON —
234
+ -- the language's (`__JSON__`), which writes them as what they say, and
235
+ -- `[]` and `{}` as what they are.
236
+
237
+ local send = pick("http.request", request, http_request, field(http, "request"), field(syn, "request"), field(fluxus, "request"))
238
+
239
+ local types = { null = __JSON__.null, undefined = __JSON__.undefined }
240
+
241
+ local function hasHeader(headers, name)
242
+ for key in pairs(headers) do
243
+ if type(key) == "string" and key:lower() == name then
244
+ return true
245
+ end
246
+ end
247
+ return false
248
+ end
249
+
250
+ -- The request a function built, as the executor's `request` takes it.
251
+ local function built(options)
252
+ if options.Body == nil or type(options.Body) == "string" then
253
+ return options
254
+ end
255
+ local copy = {}
256
+ for key, value in pairs(options) do
257
+ copy[key] = value
258
+ end
259
+ copy.Body = __JSON__.encode(options.Body)
260
+ local headers = {}
261
+ for key, value in pairs(options.Headers or {}) do
262
+ headers[key] = value
263
+ end
264
+ if not hasHeader(headers, "content-type") then
265
+ headers["Content-Type"] = "application/json"
266
+ end
267
+ copy.Headers = headers
268
+ return copy
269
+ end
270
+
271
+ __NAME__.http = {
272
+ request = function(options)
273
+ if type(options) == "function" then
274
+ return send(built(options(types)))
275
+ end
276
+ return send(options)
277
+ end,
278
+ }