about summary refs log tree commit diff stats
path: root/js/scripting-lang/scripting-harness/core/environment.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/scripting-lang/scripting-harness/core/environment.js')
-rw-r--r--js/scripting-lang/scripting-harness/core/environment.js68
1 files changed, 68 insertions, 0 deletions
diff --git a/js/scripting-lang/scripting-harness/core/environment.js b/js/scripting-lang/scripting-harness/core/environment.js
new file mode 100644
index 0000000..750ef90
--- /dev/null
+++ b/js/scripting-lang/scripting-harness/core/environment.js
@@ -0,0 +1,68 @@
+/**
+ * ScriptEnvironment - Manages script interaction with the harness
+ * 
+ * @description Provides the interface between scripts and the harness, handling
+ * state access via ..listen and command emission via ..emit. This class maintains
+ * the current state and collects commands emitted by the script during execution.
+ * 
+ * Features:
+ * - State access for ..listen operations
+ * - Command collection for ..emit operations
+ * - Pure table data extraction from metadata wrapper
+ * - Command batching for atomic processing
+ */
+
+class ScriptEnvironment {
+    constructor(currentState) {
+        this.currentState = currentState;
+        this.commands = [];
+    }
+
+    /**
+     * Get current state for ..listen operations
+     * 
+     * @returns {Object} Pure table data (without metadata wrapper)
+     */
+    getCurrentState() {
+        // Return pure table data, removing metadata wrapper if present
+        return this.currentState.data || this.currentState;
+    }
+
+    /**
+     * Emit value for ..emit operations
+     * 
+     * @param {*} value - Value to emit (any table-compatible data)
+     * @returns {*} The emitted value (for script continuation)
+     */
+    emitValue(value) {
+        this.commands.push({ type: 'emit', value });
+        return value; // Return value for script continuation
+    }
+
+    /**
+     * Get all collected commands
+     * 
+     * @returns {Array} Array of command objects
+     */
+    getCommands() {
+        return this.commands;
+    }
+
+    /**
+     * Clear commands (useful for resetting between executions)
+     */
+    clearCommands() {
+        this.commands = [];
+    }
+
+    /**
+     * Update current state
+     * 
+     * @param {Object} newState - New state with metadata wrapper
+     */
+    updateState(newState) {
+        this.currentState = newState;
+    }
+}
+
+export { ScriptEnvironment }; 
\ No newline at end of file