| 16 | private readonly gcPointer: number |
| 17 | |
| 18 | public constructor(thread: Global) { |
| 19 | super(thread, 'js_userdata') |
| 20 | |
| 21 | this.gcPointer = thread.lua.module.addFunction((functionStateAddress: LuaState) => { |
| 22 | // Throws a lua error which does a jump if it does not match. |
| 23 | const userDataPointer = thread.lua.luaL_checkudata(functionStateAddress, 1, this.name) |
| 24 | const referencePointer = thread.lua.module.getValue(userDataPointer, '*') |
| 25 | thread.lua.unref(referencePointer) |
| 26 | |
| 27 | return LuaReturn.Ok |
| 28 | }, 'ii') |
| 29 | |
| 30 | if (thread.lua.luaL_newmetatable(thread.address, this.name)) { |
| 31 | const metatableIndex = thread.lua.lua_gettop(thread.address) |
| 32 | |
| 33 | // Mark it as uneditable |
| 34 | thread.lua.lua_pushstring(thread.address, 'protected metatable') |
| 35 | thread.lua.lua_setfield(thread.address, metatableIndex, '__metatable') |
| 36 | |
| 37 | // Add the gc function |
| 38 | thread.lua.lua_pushcclosure(thread.address, this.gcPointer, 0) |
| 39 | thread.lua.lua_setfield(thread.address, metatableIndex, '__gc') |
| 40 | } |
| 41 | |
| 42 | // Pop the metatable from the stack. |
| 43 | thread.lua.lua_pop(thread.address, 1) |
| 44 | } |
| 45 | |
| 46 | public isType(_thread: Thread, _index: number, type: LuaType, name?: string): boolean { |
| 47 | return type === LuaType.Userdata && name === this.name |