MCPcopy Create free account
hub / github.com/ceifa/wasmoon / constructor

Method constructor

src/type-extensions/proxy.ts:22–125  ·  view source on GitHub ↗
(thread: Global)

Source from the content-addressed store, hash-verified

20 private readonly gcPointer: number
21
22 public constructor(thread: Global) {
23 super(thread, 'js_proxy')
24
25 this.gcPointer = thread.lua.module.addFunction((functionStateAddress: LuaState) => {
26 // Throws a lua error which does a jump if it does not match.
27 const userDataPointer = thread.lua.luaL_checkudata(functionStateAddress, 1, this.name)
28 const referencePointer = thread.lua.module.getValue(userDataPointer, '*')
29 thread.lua.unref(referencePointer)
30
31 return LuaReturn.Ok
32 }, 'ii')
33
34 if (thread.lua.luaL_newmetatable(thread.address, this.name)) {
35 const metatableIndex = thread.lua.lua_gettop(thread.address)
36
37 // Mark it as uneditable
38 thread.lua.lua_pushstring(thread.address, 'protected metatable')
39 thread.lua.lua_setfield(thread.address, metatableIndex, '__metatable')
40
41 // Add the gc function
42 thread.lua.lua_pushcclosure(thread.address, this.gcPointer, 0)
43 thread.lua.lua_setfield(thread.address, metatableIndex, '__gc')
44
45 thread.pushValue((self: any, key: unknown) => {
46 switch (typeof key) {
47 case 'number':
48 // Map from Lua's 1 based indexing to JS's 0.
49 // This is especially important here because ipairs just calls
50 // __index with 1, 2, 3, 4 etc until there's a null.
51 key = key - 1
52 // Fallthrough
53 case 'string':
54 break
55 default:
56 throw new Error('Only strings or numbers can index js objects')
57 }
58
59 const value = self[key as string | number]
60 if (typeof value === 'function') {
61 return decorateFunction(value as (...args: any[]) => any, { self })
62 }
63
64 return value
65 })
66 thread.lua.lua_setfield(thread.address, metatableIndex, '__index')
67
68 thread.pushValue((self: any, key: unknown, value: any) => {
69 switch (typeof key) {
70 case 'number':
71 // Map from Lua's 1 based indexing to JS's 0.
72 key = key - 1
73 // Fallthrough
74 case 'string':
75 break
76 default:
77 throw new Error('Only strings or numbers can index js objects')
78 }
79 self[key as string | number] = value

Callers

nothing calls this directly

Calls 5

decorateFunctionFunction · 0.90
unrefMethod · 0.80
lua_popMethod · 0.80
getValueMethod · 0.45
pushValueMethod · 0.45

Tested by

no test coverage detected