diff --git a/emscripten/chaiscript.html b/emscripten/chaiscript.html
index 29e32252..ae7cd1a8 100644
--- a/emscripten/chaiscript.html
+++ b/emscripten/chaiscript.html
@@ -185,6 +185,8 @@ print("10! = " + to_string(factorial(10)))
outputEl.scrollTop = outputEl.scrollHeight;
}
+ var chaiHandle = 0;
+
var Module = {
print: function(text) {
appendOutput(text);
@@ -193,6 +195,7 @@ print("10! = " + to_string(factorial(10)))
appendOutput(text, 'output-error');
},
onRuntimeInitialized: function() {
+ chaiHandle = Module.create();
statusEl.textContent = 'Ready';
statusEl.className = 'ready';
btnRun.disabled = false;
@@ -205,7 +208,7 @@ print("10! = " + to_string(factorial(10)))
appendOutput('> Running...', 'output-line');
try {
- Module.eval(code);
+ Module.eval(chaiHandle, code);
} catch (e) {
appendOutput('Error: ' + e.message, 'output-error');
}
diff --git a/emscripten/chaiscript_em.cpp b/emscripten/chaiscript_em.cpp
index f6026689..7a48c593 100644
--- a/emscripten/chaiscript_em.cpp
+++ b/emscripten/chaiscript_em.cpp
@@ -13,6 +13,8 @@
#include
EMSCRIPTEN_BINDINGS(chaiscript) {
+ emscripten::function("create", &chaiscript_create);
+ emscripten::function("destroy", &chaiscript_destroy);
emscripten::function("eval", &chaiscript_eval);
emscripten::function("evalString", &chaiscript_eval_string);
emscripten::function("evalBool", &chaiscript_eval_bool);
diff --git a/emscripten/chaiscript_eval.hpp b/emscripten/chaiscript_eval.hpp
index 02aca25c..ab16706e 100644
--- a/emscripten/chaiscript_eval.hpp
+++ b/emscripten/chaiscript_eval.hpp
@@ -7,21 +7,26 @@
// Shared eval helper functions for the ChaiScript Emscripten wrapper.
// These functions provide typed evaluation of ChaiScript expressions,
// used by both the Emscripten/WebAssembly build and native tests.
+//
+// The interface is opaque and handle-based: JS callers create one or more
+// engines via chaiscript_create(), pass the resulting handle to the eval and
+// state helpers, and release the engine with chaiscript_destroy() when done.
+// State snapshots are themselves opaque handles produced by
+// chaiscript_save_state() and consumed by chaiscript_restore_state() /
+// chaiscript_release_state(). Hiding ChaiScript and ChaiScript::State behind
+// integer handles keeps embind from having to manage their lifetimes and
+// avoids forcing a static singleton on the C++ side.
#ifndef CHAISCRIPT_EMSCRIPTEN_EVAL_HPP_
#define CHAISCRIPT_EMSCRIPTEN_EVAL_HPP_
#include
#include