Skip to content

Commit

Permalink
Revert back to eval in wasm InvokeJS with modularization support .
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
maraf and pavelsavara committed Nov 4, 2021
1 parent d216c7c commit c952f06
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -871,5 +871,19 @@ 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);
}
}
}
12 changes: 6 additions & 6 deletions src/mono/wasm/runtime/method-calls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -541,18 +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);
let res = closedEval(Module, MONO, BINDING, INTERNAL, js_code);
Module.setValue(is_exception, 0, "i32");
if (typeof res === "undefined" || res === null)
return MonoStringNull;

res = res.toString();
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);
Expand Down

0 comments on commit c952f06

Please sign in to comment.