You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/emulator/vmcalls.md
+25-2
Original file line number
Diff line number
Diff line change
@@ -222,7 +222,30 @@ The helper function `machine.instruction_limit_reached()` will tell you if the i
222
222
223
223
It is possible to interrupt a running machine to perform another task. This can be done using the `machine.preempt()` function. A machine can also interrupt itself without any issues. Preemption stores and restores all registers, making it slightly expensive, but guarantees the ability to preempt from any location.
224
224
225
-
High-quality scripting solutions will use pre-emption when re-entrancy is detected:
This example uses optional to determine if a call failed. Otherwise, it returns an (optional) integer which is the return value from the called function. `MAX_CALL_INSTR` is the number of instruction to execute before it's considered a timeout. `address` is the address of the function we want to call, which you can get with `machine.address_of("my_function")`.
243
+
244
+
We make `handle_exception()` opaque so that it hides the implementation, and inside we re-throw the exception to handle it properly.
245
+
246
+
### Alternative 2:
247
+
248
+
High-quality scripting solutions will use pre-emption only when re-entrancy is detected:
226
249
227
250
```cpp
228
251
template <typename... Args>
@@ -247,4 +270,4 @@ inline std::optional<Script::sgaddr_t> Script::call(gaddr_t address, Args&&... a
247
270
return std::nullopt;
248
271
}
249
272
```
250
-
From `script.call(...)` as implemented in the [gamedev example](https://github.com/libriscv/libriscv/blob/master/examples/gamedev/script.hpp). `ScriptDepthMeter` measures the current call depth in order to avoid recursive calls back into the script, while also using `preempt()` on the second call.
273
+
From `script.call(...)` as implemented in the [gamedev example](https://github.com/libriscv/libriscv/blob/master/examples/gamedev/script.hpp). `ScriptDepthMeter` measures the current call depth in order to avoid too many recursive calls back into the script, while also using the faster `vmcall()` on the first call.
0 commit comments