The best way to control Claude Code, Codex and more from any computer or phone
| 1 | /* |
| 2 | * Copyright 2018 WebAssembly Community Group participants |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef WASM_RT_IMPL_H_ |
| 18 | #define WASM_RT_IMPL_H_ |
| 19 | |
| 20 | #include "wasm-rt.h" |
| 21 | |
| 22 | #ifdef _WIN32 |
| 23 | #include <windows.h> |
| 24 | #endif |
| 25 | |
| 26 | #ifdef __cplusplus |
| 27 | extern "C" { |
| 28 | #endif |
| 29 | |
| 30 | #ifndef WASM_RT_TRAP_HANDLER |
| 31 | /** A setjmp buffer used for handling traps. */ |
| 32 | extern WASM_RT_THREAD_LOCAL wasm_rt_jmp_buf g_wasm_rt_jmp_buf; |
| 33 | #endif |
| 34 | |
| 35 | #if WASM_RT_STACK_DEPTH_COUNT |
| 36 | /** Saved call stack depth that will be restored in case a trap occurs. */ |
| 37 | extern WASM_RT_THREAD_LOCAL uint32_t wasm_rt_saved_call_stack_depth; |
| 38 | #define WASM_RT_SAVE_STACK_DEPTH() \ |
| 39 | wasm_rt_saved_call_stack_depth = wasm_rt_call_stack_depth |
| 40 | #else |
| 41 | #define WASM_RT_SAVE_STACK_DEPTH() (void)0 |
| 42 | #endif |
| 43 | |
| 44 | /** |
| 45 | * Convenience macro to use before calling a wasm function. On first execution |
| 46 | * it will return `WASM_RT_TRAP_NONE` (i.e. 0). If the function traps, it will |
| 47 | * jump back and return the trap that occurred. |
| 48 | * |
| 49 | * ``` |
| 50 | * wasm_rt_trap_t code = wasm_rt_impl_try(); |
| 51 | * if (code != 0) { |
| 52 | * printf("A trap occurred with code: %d\n", code); |
| 53 | * ... |
| 54 | * } |
| 55 | * |
| 56 | * // Call the potentially-trapping function. |
| 57 | * my_wasm_func(); |
| 58 | * ``` |
| 59 | */ |
| 60 | #define wasm_rt_impl_try() \ |
| 61 | (WASM_RT_SAVE_STACK_DEPTH(), wasm_rt_set_unwind_target(&g_wasm_rt_jmp_buf), \ |
| 62 | WASM_RT_SETJMP(g_wasm_rt_jmp_buf)) |
| 63 | |
| 64 | #ifdef __cplusplus |
| 65 | } |
| 66 | #endif |
| 67 | |
| 68 | #endif /* WASM_RT_IMPL_H_ */ |