mirror of
https://github.com/ChaiScript/ChaiScript.git
synced 2026-06-15 00:16:17 +08:00
* Fix #693: Add Emscripten/embind bindings for get_state/set_state The Emscripten wrapper exported only the eval family, leaving JS consumers with no way to snapshot or restore the singleton ChaiScript engine. The playground in chaiscript.github.io needs that to reset between runs without reloading the WASM module. Added handle-based wrappers that hide ChaiScript::State behind an int registry so JS callers don't have to manage embind object lifetimes, exported them as saveState/restoreState/releaseState, and added a native regression test that exercises capture, restore, and release through the same wrapper functions the WASM binding uses. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * Address review: snapshot top-level locals alongside engine state ChaiScript::State captures globals, functions, and types but not the top-level scripting locals created by `var x = ...`. The previous restoreState therefore left such variables behind, breaking the playground reset use case and tripping the new test's assertion in Debug builds (where assert is enabled). Pair get_state with get_locals in the snapshot so a restore brings back a clean baseline. Requested by @lefticus in PR #699 review. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * Address review: make ChaiScript engine an opaque handle, drop singleton Replace the static ChaiScript singleton in the Emscripten wrapper with a handle-based registry symmetric to the existing State registry. JS callers now create an engine with chaiscript_create(), pass the resulting handle to the eval/state helpers, and release it with chaiscript_destroy(). Multiple independent engines are now possible, and a state snapshot can be restored onto any engine. Updated the playground HTML and the three native regression tests to exercise the new API. Requested by @lefticus in PR #699 review. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> --------- Co-authored-by: leftibot <leftibot@users.noreply.github.com> Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
73 lines
2.2 KiB
C++
73 lines
2.2 KiB
C++
// Test that validates the eval patterns used by the Emscripten wrapper.
|
|
// Based on work by Rob Loach (https://github.com/RobLoach/ChaiScript.js)
|
|
|
|
#ifndef CHAISCRIPT_NO_THREADS
|
|
#define CHAISCRIPT_NO_THREADS
|
|
#endif
|
|
|
|
#ifndef CHAISCRIPT_NO_DYNLOAD
|
|
#define CHAISCRIPT_NO_DYNLOAD
|
|
#endif
|
|
|
|
#include "../emscripten/chaiscript_eval.hpp"
|
|
#include <cassert>
|
|
#include <chaiscript/chaiscript.hpp>
|
|
#include <cmath>
|
|
#include <string>
|
|
|
|
int main() {
|
|
const int chai = chaiscript_create();
|
|
assert(chai > 0 && "create must return a positive handle");
|
|
|
|
// Test eval (void return) - same as Emscripten eval()
|
|
chaiscript_eval(chai, "var x = 42");
|
|
|
|
// Test evalString - same as Emscripten evalString()
|
|
[[maybe_unused]] const std::string s = chaiscript_eval_string(chai, "to_string(x)");
|
|
assert(s == "42");
|
|
|
|
// Test evalInt - same as Emscripten evalInt()
|
|
[[maybe_unused]] const int i = chaiscript_eval_int(chai, "1 + 2");
|
|
assert(i == 3);
|
|
|
|
// Test evalBool - same as Emscripten evalBool()
|
|
[[maybe_unused]] bool b = chaiscript_eval_bool(chai, "true");
|
|
assert(b == true);
|
|
|
|
b = chaiscript_eval_bool(chai, "false");
|
|
assert(b == false);
|
|
|
|
// Test evalFloat - same as Emscripten evalFloat()
|
|
[[maybe_unused]] const float f = chaiscript_eval_float(chai, "1.5f");
|
|
assert(std::abs(f - 1.5f) < 0.001f);
|
|
|
|
// Test evalDouble - same as Emscripten evalDouble()
|
|
[[maybe_unused]] const double d = chaiscript_eval_double(chai, "3.14");
|
|
assert(std::abs(d - 3.14) < 0.001);
|
|
|
|
// Test a more complex expression
|
|
chaiscript_eval(chai, "def square(n) { return n * n; }");
|
|
[[maybe_unused]] const int sq = chaiscript_eval_int(chai, "square(7)");
|
|
assert(sq == 49);
|
|
|
|
// A second engine is fully independent of the first.
|
|
const int chai2 = chaiscript_create();
|
|
assert(chai2 != chai && "create must mint a fresh handle each call");
|
|
[[maybe_unused]] bool caught = false;
|
|
try {
|
|
chaiscript_eval(chai2, "x");
|
|
} catch (const chaiscript::exception::eval_error &) {
|
|
caught = true;
|
|
}
|
|
assert(caught && "engines must not share globals");
|
|
|
|
chaiscript_destroy(chai2);
|
|
chaiscript_destroy(chai);
|
|
|
|
// Destroying an unknown handle is a no-op.
|
|
chaiscript_destroy(chai);
|
|
chaiscript_destroy(99999);
|
|
|
|
return 0;
|
|
}
|