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

Class Thread

src/thread.ts:27–416  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

25const INSTRUCTION_HOOK_COUNT = 1000
26
27export default class Thread {
28 public readonly address: LuaState
29 public readonly lua: LuaWasm
30 protected readonly typeExtensions: OrderedExtension[]
31 private closed = false
32 private hookFunctionPointer: number | undefined
33 private timeout?: number
34 private readonly parent?: Thread
35
36 public constructor(lua: LuaWasm, typeExtensions: OrderedExtension[], address: number, parent?: Thread) {
37 this.lua = lua
38 this.typeExtensions = typeExtensions
39 this.address = address
40 this.parent = parent
41 }
42
43 public newThread(): Thread {
44 const address = this.lua.lua_newthread(this.address)
45 if (!address) {
46 throw new Error('lua_newthread returned a null pointer')
47 }
48 return new Thread(this.lua, this.typeExtensions, address, this.parent || this)
49 }
50
51 public resetThread(): void {
52 this.assertOk(this.lua.lua_resetthread(this.address))
53 }
54
55 public loadString(luaCode: string, name?: string): void {
56 const size = this.lua.module.lengthBytesUTF8(luaCode)
57 const pointerSize = size + 1
58 const bufferPointer = this.lua.module._malloc(pointerSize)
59 try {
60 this.lua.module.stringToUTF8(luaCode, bufferPointer, pointerSize)
61 this.assertOk(this.lua.luaL_loadbufferx(this.address, bufferPointer, size, name ?? bufferPointer, null))
62 } finally {
63 this.lua.module._free(bufferPointer)
64 }
65 }
66
67 public loadFile(filename: string): void {
68 this.assertOk(this.lua.luaL_loadfilex(this.address, filename, null))
69 }
70
71 public resume(argCount = 0): LuaResumeResult {
72 const dataPointer = this.lua.module._malloc(PointerSize)
73 try {
74 this.lua.module.setValue(dataPointer, 0, 'i32')
75 const luaResult = this.lua.lua_resume(this.address, null, argCount, dataPointer)
76 return {
77 result: luaResult,
78 resultCount: this.lua.module.getValue(dataPointer, 'i32'),
79 }
80 } finally {
81 this.lua.module._free(dataPointer)
82 }
83 }
84

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected