Skip to content

Commit 39a4a70

Browse files
marafpavelsavara
andauthored
[wasm] Revert back to eval in wasm InvokeJS with modularization support . (#61212)
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 (successful, null, undefined, global scope separation) Co-authored-by: Pavel Savara <pavelsavara@microsoft.com>
1 parent c7be76d commit 39a4a70

File tree

2 files changed

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

2 files changed

+34
-9
lines changed

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,5 +871,33 @@ 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+
}
888+
889+
[Fact]
890+
public static void InvokeJSUndefinedExpression()
891+
{
892+
var result = Runtime.InvokeJS(@"undefined");
893+
Assert.Null(result);
894+
}
895+
896+
[Fact]
897+
public static void InvokeJSNotInGlobalScope()
898+
{
899+
var result = Runtime.InvokeJS(@"var test_local_variable_name = 5; globalThis.test_local_variable_name");
900+
Assert.Null(result);
901+
}
874902
}
875903
}

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -541,21 +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+
const 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;
556-
if (typeof res !== "string")
557-
return wrap_error(is_exception, `Return type of InvokeJS is string. Can't marshal response of type ${typeof res}.`);
558-
return js_string_to_mono_string(res);
554+
555+
return js_string_to_mono_string(res.toString());
559556
} catch (ex) {
560557
return wrap_error(is_exception, ex);
561558
}

0 commit comments

Comments
 (0)