Skip to content

Commit c952f06

Browse files
marafpavelsavara
andcommitted
Revert back to eval in wasm InvokeJS with modularization support .
Wrap code to evaluate in a function with MONO, BINDING, INTERNAL and module as local variables. Added tests for running js expressions. Co-authored-by: Pavel Savara <pavelsavara@microsoft.com>
1 parent d216c7c commit c952f06

File tree

2 files changed

+20
-6
lines changed
  • src
    • libraries/System.Private.Runtime.InteropServices.JavaScript/tests/System/Runtime/InteropServices/JavaScript
    • mono/wasm/runtime

2 files changed

+20
-6
lines changed

src/libraries/System.Private.Runtime.InteropServices.JavaScript/tests/System/Runtime/InteropServices/JavaScript/MarshalTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,5 +871,19 @@ public static void InternedStringReturnValuesWork()
871871
Assert.Equal("s: 1 length: 1", HelperMarshal._stringResource);
872872
Assert.Equal("1", HelperMarshal._stringResource2);
873873
}
874+
875+
[Fact]
876+
public static void InvokeJSExpression()
877+
{
878+
var result = Runtime.InvokeJS(@"1 + 2");
879+
Assert.Equal("3", result);
880+
}
881+
882+
[Fact]
883+
public static void InvokeJSNullExpression()
884+
{
885+
var result = Runtime.InvokeJS(@"null");
886+
Assert.Null(result);
887+
}
874888
}
875889
}

src/mono/wasm/runtime/method-calls.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -541,18 +541,18 @@ export function mono_wasm_invoke_js(code: MonoString, is_exception: Int32Ptr): M
541541
if (code === MonoStringNull)
542542
return MonoStringNull;
543543

544-
const js_code = conv_string(code);
544+
const js_code = conv_string(code)!;
545545

546546
try {
547-
const closure = {
548-
Module, MONO, BINDING, INTERNAL
547+
const closedEval = function (Module: EmscriptenModule, MONO: any, BINDING: any, INTERNAL: any, code: string) {
548+
return eval(code);
549549
};
550-
const fn_body_template = `const {Module, MONO, BINDING, INTERNAL} = __closure; const __fn = function(){ ${js_code} }; return __fn.call(__closure);`;
551-
const fn_defn = new Function("__closure", fn_body_template);
552-
const res = fn_defn(closure);
550+
let res = closedEval(Module, MONO, BINDING, INTERNAL, js_code);
553551
Module.setValue(is_exception, 0, "i32");
554552
if (typeof res === "undefined" || res === null)
555553
return MonoStringNull;
554+
555+
res = res.toString();
556556
if (typeof res !== "string")
557557
return wrap_error(is_exception, `Return type of InvokeJS is string. Can't marshal response of type ${typeof res}.`);
558558
return js_string_to_mono_string(res);

0 commit comments

Comments
 (0)