// test-js-interop.baba - Test JavaScript interop functionality // Test 1: Basic Math.abs call io.out "=== Test 1: Math.abs ===" ; absResult : io.callJS "Math.abs" [-42]; io.out "Math.abs(-42) result:"; io.out absResult; // Test 2: JSON parsing io.out "=== Test 2: JSON Parsing ===" ; jsonStr : "{\"name\": \"Alice\", \"age\": 30, \"active\": true}"; parseResult : io.callJS "JSON.parse" [jsonStr]; io.out "JSON.parse result:"; io.out parseResult; // Test 3: Property access io.out "=== Test 3: Property Access ===" ; nameResult : when parseResult is Ok obj then io.getProperty obj "name" Err msg then Err msg; io.out "Name property:"; io.out nameResult; // Test 4: Array conversion io.out "=== Test 4: Array Conversion ===" ; babaList : [1, 2, 3, 4, 5]; jsArray : io.listToJSArray babaList; stringifyResult : io.callJS "JSON.stringify" [jsArray]; io.out "List to JS array to JSON:"; io.out stringifyResult; // Test 5: Table to Object conversion io.out "=== Test 5: Table to Object ===" ; babaTable : {x: 100, y: 200, label: "point"}; jsObject : io.tableToObject babaTable; tableJsonResult : io.callJS "JSON.stringify" [jsObject]; io.out "Table to JS object to JSON:"; io.out tableJsonResult; // Test 6: Round-trip conversion io.out "=== Test 6: Round-trip Conversion ===" ; originalData : { users: ["Alice", "Bob", "Charlie"], count: 3, active: true }; // Convert to JS object jsObj : io.tableToObject originalData; // Convert to JSON string jsonResult : io.callJS "JSON.stringify" [jsObj]; // Parse back to JS object parseBackResult : when jsonResult is Ok jsonStr then io.callJS "JSON.parse" [jsonStr] Err msg then Err msg; // Convert back to Baba Yaga table finalResult : when parseBackResult is Ok jsObj then io.objectToTable jsObj Err msg then Err msg; io.out "Round-trip result:"; io.out finalResult; // Test 7: Error handling io.out "=== Test 7: Error Handling ===" ; errorResult : io.callJS "nonExistentFunction" [42]; io.out "Error result (should be Err):"; io.out errorResult; // Test 8: Property existence check io.out "=== Test 8: Property Existence ===" ; testObj : io.callJS "JSON.parse" ["{\"existing\": true}"]; hasExisting : when testObj is Ok obj then io.hasProperty obj "existing" Err _ then false; hasMissing : when testObj is Ok obj then io.hasProperty obj "missing" Err _ then false; io.out "Has 'existing' property:"; io.out hasExisting; io.out "Has 'missing' property:"; io.out hasMissing; io.out "=== All Tests Complete ===";