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
..
chaiscript_em.cpp Fix #693: Add Emscripten/embind bindings for get_state / set_state (needed for playground state reset) (#699) 2026-05-02 11:04:52 -06:00
chaiscript_eval.hpp Fix #693: Add Emscripten/embind bindings for get_state / set_state (needed for playground state reset) (#699) 2026-05-02 11:04:52 -06:00
chaiscript.html Fix #693: Add Emscripten/embind bindings for get_state / set_state (needed for playground state reset) (#699) 2026-05-02 11:04:52 -06:00
CMakeLists.txt Fix #678: Add WASM exception support to Emscripten build (#689) 2026-04-13 22:40:26 -06:00