Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wasm] Revert back to eval in wasm InvokeJS with modularization support . #61212

Merged
merged 3 commits into from
Nov 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -871,5 +871,33 @@ public static void InternedStringReturnValuesWork()
Assert.Equal("s: 1 length: 1", HelperMarshal._stringResource);
Assert.Equal("1", HelperMarshal._stringResource2);
}

[Fact]
public static void InvokeJSExpression()
{
var result = Runtime.InvokeJS(@"1 + 2");
Assert.Equal("3", result);
}

[Fact]
public static void InvokeJSNullExpression()
{
var result = Runtime.InvokeJS(@"null");
Assert.Null(result);
}

[Fact]
public static void InvokeJSUndefinedExpression()
{
var result = Runtime.InvokeJS(@"undefined");
Assert.Null(result);
}

[Fact]
public static void InvokeJSNotInGlobalScope()
{
var result = Runtime.InvokeJS(@"var test_local_variable_name = 5; globalThis.test_local_variable_name");
Assert.Null(result);
}
}
}
15 changes: 6 additions & 9 deletions src/mono/wasm/runtime/method-calls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -541,21 +541,18 @@ export function mono_wasm_invoke_js(code: MonoString, is_exception: Int32Ptr): M
if (code === MonoStringNull)
return MonoStringNull;

const js_code = conv_string(code);
const js_code = conv_string(code)!;

try {
const closure = {
Module, MONO, BINDING, INTERNAL
const closedEval = function (Module: EmscriptenModule, MONO: any, BINDING: any, INTERNAL: any, code: string) {
return eval(code);
};
const fn_body_template = `const {Module, MONO, BINDING, INTERNAL} = __closure; const __fn = function(){ ${js_code} }; return __fn.call(__closure);`;
const fn_defn = new Function("__closure", fn_body_template);
const res = fn_defn(closure);
const res = closedEval(Module, MONO, BINDING, INTERNAL, js_code);
Module.setValue(is_exception, 0, "i32");
if (typeof res === "undefined" || res === null)
return MonoStringNull;
if (typeof res !== "string")
return wrap_error(is_exception, `Return type of InvokeJS is string. Can't marshal response of type ${typeof res}.`);
return js_string_to_mono_string(res);

return js_string_to_mono_string(res.toString());
} catch (ex) {
return wrap_error(is_exception, ex);
}
Expand Down