| 1 |
package wazevoapi |
| 2 |
|
| 3 |
// ExitCode is an exit code of an execution of a function. |
| 4 |
type ExitCode uint32 |
| 5 |
|
| 6 |
const ( |
| 7 |
ExitCodeOK ExitCode = iota |
| 8 |
ExitCodeGrowStack |
| 9 |
ExitCodeGrowMemory |
| 10 |
ExitCodeUnreachable |
| 11 |
ExitCodeMemoryOutOfBounds |
| 12 |
// ExitCodeCallGoModuleFunction is an exit code for a call to an api.GoModuleFunction. |
| 13 |
ExitCodeCallGoModuleFunction |
| 14 |
// ExitCodeCallGoFunction is an exit code for a call to an api.GoFunction. |
| 15 |
ExitCodeCallGoFunction |
| 16 |
ExitCodeTableOutOfBounds |
| 17 |
ExitCodeIndirectCallNullPointer |
| 18 |
ExitCodeIndirectCallTypeMismatch |
| 19 |
ExitCodeIntegerDivisionByZero |
| 20 |
ExitCodeIntegerOverflow |
| 21 |
ExitCodeInvalidConversionToInteger |
| 22 |
ExitCodeCheckModuleExitCode |
| 23 |
ExitCodeCallListenerBefore |
| 24 |
ExitCodeCallListenerAfter |
| 25 |
ExitCodeCallGoModuleFunctionWithListener |
| 26 |
ExitCodeCallGoFunctionWithListener |
| 27 |
ExitCodeTableGrow |
| 28 |
ExitCodeRefFunc |
| 29 |
ExitCodeMemoryWait32 |
| 30 |
ExitCodeMemoryWait64 |
| 31 |
ExitCodeMemoryNotify |
| 32 |
ExitCodeUnalignedAtomic |
| 33 |
// ExitCodeThrowAlloc is the first phase of wasm throw: Go allocates the |
| 34 |
// Exception heap object (with Params sized to the tag's param count) and |
| 35 |
// writes its Params data pointer to execCtx.exceptionParamsPtr. |
| 36 |
// Compiled code then stores params directly into the Exception.Params slice, |
| 37 |
// followed by ExitCodeThrow to search for a matching handler. |
| 38 |
ExitCodeThrowAlloc |
| 39 |
// ExitCodeThrow is the shared throw/throw_ref exit code. |
| 40 |
// The exnref is passed on the stack. The handler searches for a |
| 41 |
// matching catch clause and restores the stack checkpoint. |
| 42 |
ExitCodeThrow |
| 43 |
// ExitCodeNullReference is an exit code for a null reference trap (throw_ref with null exnref). |
| 44 |
ExitCodeNullReference |
| 45 |
// ExitCodeTryTableEnter is an exit code for entering a try_table block. |
| 46 |
// The catch clause info is encoded in the upper bits. The dispatch loop |
| 47 |
// saves the current SP/FP/returnAddress as a try handler checkpoint. |
| 48 |
ExitCodeTryTableEnter |
| 49 |
// ExitCodeTryTableLeave is an exit code for leaving a try_table block. |
| 50 |
// The dispatch loop pops the most recent try handler. |
| 51 |
ExitCodeTryTableLeave |
| 52 |
exitCodeMax |
| 53 |
) |
| 54 |
|
| 55 |
const ExitCodeMask = 0xff |
| 56 |
|
| 57 |
// String implements fmt.Stringer. |
| 58 |
func (e ExitCode) String() string { |
| 59 |
switch e { |
| 60 |
case ExitCodeOK: |
| 61 |
return "ok" |
| 62 |
case ExitCodeGrowStack: |
| 63 |
return "grow_stack" |
| 64 |
case ExitCodeCallGoModuleFunction: |
| 65 |
return "call_go_module_function" |
| 66 |
case ExitCodeCallGoFunction: |
| 67 |
return "call_go_function" |
| 68 |
case ExitCodeUnreachable: |
| 69 |
return "unreachable" |
| 70 |
case ExitCodeMemoryOutOfBounds: |
| 71 |
return "memory_out_of_bounds" |
| 72 |
case ExitCodeUnalignedAtomic: |
| 73 |
return "unaligned_atomic" |
| 74 |
case ExitCodeTableOutOfBounds: |
| 75 |
return "table_out_of_bounds" |
| 76 |
case ExitCodeIndirectCallNullPointer: |
| 77 |
return "indirect_call_null_pointer" |
| 78 |
case ExitCodeIndirectCallTypeMismatch: |
| 79 |
return "indirect_call_type_mismatch" |
| 80 |
case ExitCodeIntegerDivisionByZero: |
| 81 |
return "integer_division_by_zero" |
| 82 |
case ExitCodeIntegerOverflow: |
| 83 |
return "integer_overflow" |
| 84 |
case ExitCodeInvalidConversionToInteger: |
| 85 |
return "invalid_conversion_to_integer" |
| 86 |
case ExitCodeCheckModuleExitCode: |
| 87 |
return "check_module_exit_code" |
| 88 |
case ExitCodeCallListenerBefore: |
| 89 |
return "call_listener_before" |
| 90 |
case ExitCodeCallListenerAfter: |
| 91 |
return "call_listener_after" |
| 92 |
case ExitCodeCallGoModuleFunctionWithListener: |
| 93 |
return "call_go_module_function_with_listener" |
| 94 |
case ExitCodeCallGoFunctionWithListener: |
| 95 |
return "call_go_function_with_listener" |
| 96 |
case ExitCodeGrowMemory: |
| 97 |
return "grow_memory" |
| 98 |
case ExitCodeTableGrow: |
| 99 |
return "table_grow" |
| 100 |
case ExitCodeRefFunc: |
| 101 |
return "ref_func" |
| 102 |
case ExitCodeMemoryWait32: |
| 103 |
return "memory_wait32" |
| 104 |
case ExitCodeMemoryWait64: |
| 105 |
return "memory_wait64" |
| 106 |
case ExitCodeMemoryNotify: |
| 107 |
return "memory_notify" |
| 108 |
case ExitCodeThrowAlloc: |
| 109 |
return "throw_alloc" |
| 110 |
case ExitCodeThrow: |
| 111 |
return "throw" |
| 112 |
case ExitCodeNullReference: |
| 113 |
return "null_reference" |
| 114 |
case ExitCodeTryTableEnter: |
| 115 |
return "try_table_enter" |
| 116 |
case ExitCodeTryTableLeave: |
| 117 |
return "try_table_leave" |
| 118 |
} |
| 119 |
panic("TODO") |
| 120 |
} |
| 121 |
|
| 122 |
func ExitCodeCallGoModuleFunctionWithIndex(index int, withListener bool) ExitCode { |
| 123 |
if withListener { |
| 124 |
return ExitCodeCallGoModuleFunctionWithListener | ExitCode(index<<8) |
| 125 |
} |
| 126 |
return ExitCodeCallGoModuleFunction | ExitCode(index<<8) |
| 127 |
} |
| 128 |
|
| 129 |
func ExitCodeCallGoFunctionWithIndex(index int, withListener bool) ExitCode { |
| 130 |
if withListener { |
| 131 |
return ExitCodeCallGoFunctionWithListener | ExitCode(index<<8) |
| 132 |
} |
| 133 |
return ExitCodeCallGoFunction | ExitCode(index<<8) |
| 134 |
} |
| 135 |
|
| 136 |
func GoFunctionIndexFromExitCode(exitCode ExitCode) int { |
| 137 |
return int(exitCode >> 8) |
| 138 |
} |
| 139 |
|
| 140 |
// TryTableIDFromExitCode extracts the try-table ID from an ExitCodeTryTableEnter |
| 141 |
// exit code. Uses the same encoding as GoFunctionIndexFromExitCode (upper 24 bits). |
| 142 |
func TryTableIDFromExitCode(exitCode ExitCode) int { |
| 143 |
return GoFunctionIndexFromExitCode(exitCode) |
| 144 |
} |
| 145 |
|
| 146 |
// CatchClauseInstance is a runtime catch clause with resolved tag index. |
| 147 |
type CatchClauseInstance struct { |
| 148 |
Kind byte // wasm.CatchKindCatch, etc. |
| 149 |
TagIndex uint32 // module-local tag index |
| 150 |
} |