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

Method assertOk

src/thread.ts:375–411  ·  view source on GitHub ↗
(result: LuaReturn)

Source from the content-addressed store, hash-verified

373 }
374
375 public assertOk(result: LuaReturn): void {
376 if (result !== LuaReturn.Ok && result !== LuaReturn.Yield) {
377 const resultString = LuaReturn[result]
378 // This is the default message if there's nothing on the stack.
379 const error = new Error(`Lua Error(${resultString}/${result})`)
380 if (this.getTop() > 0) {
381 if (result === LuaReturn.ErrorMem) {
382 // If there's no memory just do a normal to string.
383 error.message = this.lua.lua_tolstring(this.address, -1, null)
384 } else {
385 const luaError = this.getValue(-1)
386 if (luaError instanceof Error) {
387 error.stack = luaError.stack
388 }
389
390 // Calls __tostring if it exists and pushes onto the stack.
391 error.message = this.indexToString(-1)
392 }
393 }
394
395 // Also attempt to get a traceback
396 if (result !== LuaReturn.ErrorMem) {
397 try {
398 this.lua.luaL_traceback(this.address, this.address, null, 1)
399 const traceback = this.lua.lua_tolstring(this.address, -1, null)
400 if (traceback.trim() !== 'stack traceback:') {
401 error.message = `${error.message}\n${traceback}`
402 }
403 this.pop(1) // pop stack trace.
404 } catch (err) {
405 console.warn('Failed to generate stack trace', err)
406 }
407 }
408
409 throw error
410 }
411 }
412
413 private getValueDecorations(value: any): Decoration {
414 return value instanceof Decoration ? value : new Decoration(value, {})

Callers 6

resetThreadMethod · 0.95
loadStringMethod · 0.95
loadFileMethod · 0.95
runMethod · 0.95
runSyncMethod · 0.95
jsFuncMethod · 0.80

Calls 4

getTopMethod · 0.95
getValueMethod · 0.95
indexToStringMethod · 0.95
popMethod · 0.95

Tested by

no test coverage detected