ChaiScript/unittests/emscripten_exception_test.cpp
leftibot 45cbec0e41
Fix #693: Add Emscripten/embind bindings for get_state / set_state (needed for playground state reset) (#699)
* 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>
2026-05-02 11:04:52 -06:00

78 lines
2.3 KiB
C++

// Test that validates exception propagation through the Emscripten eval wrapper.
// Without proper exception support flags (-fwasm-exceptions) in the WASM build,
// C++ exceptions would cause an abort instead of being catchable.
#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 <iostream>
#include <stdexcept>
#include <string>
int main() {
// Verify that ChaiScript evaluation errors propagate as exceptions
// through the eval wrapper functions. In WASM builds without exception
// support, these would abort instead of throwing.
const int chai = chaiscript_create();
[[maybe_unused]] bool caught = false;
// Test 1: eval with undefined variable should throw
caught = false;
try {
chaiscript_eval(chai, "this_variable_does_not_exist");
} catch (const chaiscript::exception::eval_error &) {
caught = true;
}
assert(caught && "eval of undefined variable must throw eval_error");
// Test 2: evalString with a type mismatch should throw
caught = false;
try {
chaiscript_eval_string(chai, "1 + 2");
} catch (const chaiscript::exception::bad_boxed_cast &) {
caught = true;
}
assert(caught && "evalString with non-string result must throw bad_boxed_cast");
// Test 3: evalInt with invalid syntax should throw
caught = false;
try {
chaiscript_eval_int(chai, "def {}");
} catch (const chaiscript::exception::eval_error &) {
caught = true;
}
assert(caught && "evalInt with syntax error must throw eval_error");
// Test 4: eval with throw statement should propagate exception
caught = false;
try {
chaiscript_eval(chai, "throw(\"user exception\")");
} catch (const chaiscript::Boxed_Value &) {
caught = true;
} catch (...) {
caught = true;
}
assert(caught && "ChaiScript throw must propagate as an exception");
// Test 5: Verify normal operation still works after caught exceptions
chaiscript_eval(chai, "var post_exception_test = 100");
[[maybe_unused]] const int result = chaiscript_eval_int(chai, "post_exception_test");
assert(result == 100 && "normal eval must work after caught exceptions");
chaiscript_destroy(chai);
std::cout << "All emscripten exception tests passed.\n";
return 0;
}