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