Skip to content

[wasm] encapsulate emscripten js runtime #60504

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

Closed
wants to merge 1 commit into from
Closed
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 @@ -221,6 +221,8 @@
<PlatformManifestFileEntry Include="runtime.iffe.js" IsNative="true" />
<PlatformManifestFileEntry Include="dotnet.d.ts" IsNative="true" />
<PlatformManifestFileEntry Include="library-dotnet.js" IsNative="true" />
<PlatformManifestFileEntry Include="modularize-dotnet.pre.js" IsNative="true" />
<PlatformManifestFileEntry Include="modularize-dotnet.post.js" IsNative="true" />
<PlatformManifestFileEntry Include="pal_random.js" IsNative="true" />
<PlatformManifestFileEntry Include="corebindings.c" IsNative="true" />
<PlatformManifestFileEntry Include="driver.c" IsNative="true" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public static void StopProfile()
{
}

// Called by the AOT profiler to save profile data into INTERNAL.aot_profile_data
// Called by the AOT profiler to save profile data into Module.INTERNAL.aot_profile_data
[MethodImplAttribute(MethodImplOptions.NoInlining)]
public static unsafe void DumpAotProfileData(ref byte buf, int len, string extraArg)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ public static void BindStaticMethod()
{
HelperMarshal._intValue = 0;
Runtime.InvokeJS(@$"
var invoke_int = INTERNAL.mono_bind_static_method (""{HelperMarshal.INTEROP_CLASS}InvokeInt"");
var invoke_int = Module.INTERNAL.mono_bind_static_method (""{HelperMarshal.INTEROP_CLASS}InvokeInt"");
invoke_int (200);
");

Expand All @@ -295,7 +295,7 @@ public static void BindIntPtrStaticMethod()
{
HelperMarshal._intPtrValue = IntPtr.Zero;
Runtime.InvokeJS(@$"
var invoke_int_ptr = INTERNAL.mono_bind_static_method (""{HelperMarshal.INTEROP_CLASS}InvokeIntPtr"");
var invoke_int_ptr = Module.INTERNAL.mono_bind_static_method (""{HelperMarshal.INTEROP_CLASS}InvokeIntPtr"");
invoke_int_ptr (42);
");
Assert.Equal(42, (int)HelperMarshal._intPtrValue);
Expand All @@ -306,7 +306,7 @@ public static void MarshalIntPtrToJS()
{
HelperMarshal._marshaledIntPtrValue = IntPtr.Zero;
Runtime.InvokeJS(@$"
var invokeMarshalIntPtr = INTERNAL.mono_bind_static_method (""{HelperMarshal.INTEROP_CLASS}InvokeMarshalIntPtr"");
var invokeMarshalIntPtr = Module.INTERNAL.mono_bind_static_method (""{HelperMarshal.INTEROP_CLASS}InvokeMarshalIntPtr"");
var r = invokeMarshalIntPtr ();

if (r != 42) throw `Invalid int_ptr value`;
Expand All @@ -319,7 +319,7 @@ public static void InvokeStaticMethod()
{
HelperMarshal._intValue = 0;
Runtime.InvokeJS(@$"
INTERNAL.call_static_method (""{HelperMarshal.INTEROP_CLASS}InvokeInt"", [ 300 ]);
Module.INTERNAL.call_static_method (""{HelperMarshal.INTEROP_CLASS}InvokeInt"", [ 300 ]);
");

Assert.Equal(300, HelperMarshal._intValue);
Expand All @@ -330,7 +330,7 @@ public static void ResolveMethod()
{
HelperMarshal._intValue = 0;
Runtime.InvokeJS(@$"
var invoke_int = INTERNAL.mono_method_resolve (""{HelperMarshal.INTEROP_CLASS}InvokeInt"");
var invoke_int = Module.INTERNAL.mono_method_resolve (""{HelperMarshal.INTEROP_CLASS}InvokeInt"");
App.call_test_method (""InvokeInt"", [ invoke_int ]);
");

Expand Down Expand Up @@ -629,7 +629,7 @@ public static void BoundStaticMethodMissingArgs()

HelperMarshal._intValue = 1;
Runtime.InvokeJS(@$"
var invoke_int = INTERNAL.mono_bind_static_method (""{HelperMarshal.INTEROP_CLASS}InvokeInt"");
var invoke_int = Module.INTERNAL.mono_bind_static_method (""{HelperMarshal.INTEROP_CLASS}InvokeInt"");
invoke_int ();
");
Assert.Equal(0, HelperMarshal._intValue);
Expand All @@ -640,7 +640,7 @@ public static void BoundStaticMethodExtraArgs()
{
HelperMarshal._intValue = 0;
Runtime.InvokeJS(@$"
var invoke_int = INTERNAL.mono_bind_static_method (""{HelperMarshal.INTEROP_CLASS}InvokeInt"");
var invoke_int = Module.INTERNAL.mono_bind_static_method (""{HelperMarshal.INTEROP_CLASS}InvokeInt"");
invoke_int (200, 400);
");
Assert.Equal(200, HelperMarshal._intValue);
Expand All @@ -654,13 +654,13 @@ public static void BoundStaticMethodArgumentTypeCoercion()

HelperMarshal._intValue = 0;
Runtime.InvokeJS(@$"
var invoke_int = INTERNAL.mono_bind_static_method (""{HelperMarshal.INTEROP_CLASS}InvokeInt"");
var invoke_int = Module.INTERNAL.mono_bind_static_method (""{HelperMarshal.INTEROP_CLASS}InvokeInt"");
invoke_int (""200"");
");
Assert.Equal(200, HelperMarshal._intValue);

Runtime.InvokeJS(@$"
var invoke_int = INTERNAL.mono_bind_static_method (""{HelperMarshal.INTEROP_CLASS}InvokeInt"");
var invoke_int = Module.INTERNAL.mono_bind_static_method (""{HelperMarshal.INTEROP_CLASS}InvokeInt"");
invoke_int (400.5);
");
Assert.Equal(400, HelperMarshal._intValue);
Expand All @@ -671,14 +671,14 @@ public static void BoundStaticMethodUnpleasantArgumentTypeCoercion()
{
HelperMarshal._intValue = 100;
Runtime.InvokeJS(@$"
var invoke_int = INTERNAL.mono_bind_static_method (""{HelperMarshal.INTEROP_CLASS}InvokeInt"");
var invoke_int = Module.INTERNAL.mono_bind_static_method (""{HelperMarshal.INTEROP_CLASS}InvokeInt"");
invoke_int (""hello"");
");
Assert.Equal(0, HelperMarshal._intValue);

// In this case at the very least, the leading "7" is not turned into the number 7
Runtime.InvokeJS(@$"
var invoke_int = INTERNAL.mono_bind_static_method (""{HelperMarshal.INTEROP_CLASS}InvokeInt"");
var invoke_int = Module.INTERNAL.mono_bind_static_method (""{HelperMarshal.INTEROP_CLASS}InvokeInt"");
invoke_int (""7apples"");
");
Assert.Equal(0, HelperMarshal._intValue);
Expand All @@ -689,7 +689,7 @@ public static void PassUintArgument()
{
HelperMarshal._uintValue = 0;
Runtime.InvokeJS(@$"
var invoke_uint = INTERNAL.mono_bind_static_method (""{HelperMarshal.INTEROP_CLASS}InvokeUInt"");
var invoke_uint = Module.INTERNAL.mono_bind_static_method (""{HelperMarshal.INTEROP_CLASS}InvokeUInt"");
invoke_uint (0xFFFFFFFE);
");

Expand All @@ -702,9 +702,9 @@ public static void ReturnUintEnum ()
HelperMarshal._uintValue = 0;
HelperMarshal._enumValue = TestEnum.BigValue;
Runtime.InvokeJS(@$"
var get_value = INTERNAL.mono_bind_static_method (""{HelperMarshal.INTEROP_CLASS}GetEnumValue"");
var get_value = Module.INTERNAL.mono_bind_static_method (""{HelperMarshal.INTEROP_CLASS}GetEnumValue"");
var e = get_value ();
var invoke_uint = INTERNAL.mono_bind_static_method (""{HelperMarshal.INTEROP_CLASS}InvokeUInt"");
var invoke_uint = Module.INTERNAL.mono_bind_static_method (""{HelperMarshal.INTEROP_CLASS}InvokeUInt"");
invoke_uint (e);
");
Assert.Equal((uint)TestEnum.BigValue, HelperMarshal._uintValue);
Expand All @@ -715,7 +715,7 @@ public static void PassUintEnumByValue ()
{
HelperMarshal._enumValue = TestEnum.Zero;
Runtime.InvokeJS(@$"
var set_enum = INTERNAL.mono_bind_static_method (""{HelperMarshal.INTEROP_CLASS}SetEnumValue"", ""j"");
var set_enum = Module.INTERNAL.mono_bind_static_method (""{HelperMarshal.INTEROP_CLASS}SetEnumValue"", ""j"");
set_enum (0xFFFFFFFE);
");
Assert.Equal(TestEnum.BigValue, HelperMarshal._enumValue);
Expand All @@ -728,7 +728,7 @@ public static void PassUintEnumByValueMasqueradingAsInt ()
// HACK: We're explicitly telling the bindings layer to pass an int here, not an enum
// Because we know the enum is : uint, this is compatible, so it works.
Runtime.InvokeJS(@$"
var set_enum = INTERNAL.mono_bind_static_method (""{HelperMarshal.INTEROP_CLASS}SetEnumValue"", ""i"");
var set_enum = Module.INTERNAL.mono_bind_static_method (""{HelperMarshal.INTEROP_CLASS}SetEnumValue"", ""i"");
set_enum (0xFFFFFFFE);
");
Assert.Equal(TestEnum.BigValue, HelperMarshal._enumValue);
Expand All @@ -740,7 +740,7 @@ public static void PassUintEnumByNameIsNotImplemented ()
HelperMarshal._enumValue = TestEnum.Zero;
var exc = Assert.Throws<JSException>( () =>
Runtime.InvokeJS(@$"
var set_enum = INTERNAL.mono_bind_static_method (""{HelperMarshal.INTEROP_CLASS}SetEnumValue"", ""j"");
var set_enum = Module.INTERNAL.mono_bind_static_method (""{HelperMarshal.INTEROP_CLASS}SetEnumValue"", ""j"");
set_enum (""BigValue"");
")
);
Expand All @@ -752,7 +752,7 @@ public static void CannotUnboxUint64 ()
{
var exc = Assert.Throws<JSException>( () =>
Runtime.InvokeJS(@$"
var get_u64 = INTERNAL.mono_bind_static_method (""{HelperMarshal.INTEROP_CLASS}GetUInt64"", """");
var get_u64 = Module.INTERNAL.mono_bind_static_method (""{HelperMarshal.INTEROP_CLASS}GetUInt64"", """");
var u64 = get_u64();
")
);
Expand Down Expand Up @@ -806,7 +806,7 @@ public static void ManuallyInternString()
{
HelperMarshal._stringResource = HelperMarshal._stringResource2 = null;
Runtime.InvokeJS(@"
var sym = INTERNAL.mono_intern_string(""interned string 3"");
var sym = Module.INTERNAL.mono_intern_string(""interned string 3"");
App.call_test_method (""InvokeString"", [ sym ], ""s"");
App.call_test_method (""InvokeString2"", [ sym ], ""s"");
");
Expand All @@ -823,7 +823,7 @@ public static void LargeStringsAreNotAutomaticallyLocatedInInternTable()
var s = ""long interned string"";
for (var i = 0; i < 1024; i++)
s += String(i % 10);
var sym = INTERNAL.mono_intern_string(s);
var sym = Module.INTERNAL.mono_intern_string(s);
App.call_test_method (""InvokeString"", [ sym ], ""S"");
App.call_test_method (""InvokeString2"", [ sym ], ""s"");
");
Expand All @@ -837,7 +837,7 @@ public static void CanInternVeryManyStrings()
HelperMarshal._stringResource = null;
Runtime.InvokeJS(@"
for (var i = 0; i < 10240; i++)
INTERNAL.mono_intern_string('s' + i);
Module.INTERNAL.mono_intern_string('s' + i);
App.call_test_method (""InvokeString"", [ 's5000' ], ""S"");
");
Assert.Equal("s5000", HelperMarshal._stringResource);
Expand All @@ -864,8 +864,8 @@ public static void InternedStringReturnValuesWork()
HelperMarshal._stringResource = HelperMarshal._stringResource2 = null;
var fqn = "[System.Private.Runtime.InteropServices.JavaScript.Tests]System.Runtime.InteropServices.JavaScript.Tests.HelperMarshal:StoreArgumentAndReturnLiteral";
Runtime.InvokeJS(
$"var a = INTERNAL.mono_bind_static_method('{fqn}')('test');\r\n" +
$"var b = INTERNAL.mono_bind_static_method('{fqn}')(a);\r\n" +
$"var a = Module.INTERNAL.mono_bind_static_method('{fqn}')('test');\r\n" +
$"var b = Module.INTERNAL.mono_bind_static_method('{fqn}')(a);\r\n" +
"App.call_test_method ('InvokeString2', [ b ]);"
);
Assert.Equal("s: 1 length: 1", HelperMarshal._stringResource);
Expand Down
65 changes: 34 additions & 31 deletions src/mono/sample/mbr/browser/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,40 @@
<!-- Licensed to the .NET Foundation under one or more agreements. -->
<!-- The .NET Foundation licenses this file to you under the MIT license. -->
<html>
<head>
<title>TESTS</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h3 id="header">Wasm Browser Sample</h3>
Result from Sample.Test.TestMeaning: <span id="out"></span>
<div>
Click here (upto 2 times): <button id="update">Update</button>
</div>
<script type='text/javascript'>
var App = {
init: function () {
var outElement = document.getElementById("out");
document.getElementById("update").addEventListener("click", function () {
INTERNAL.call_static_method("[WasmDelta] Sample.Test:Update", []);
console.log ("applied update");
var ret = INTERNAL.call_static_method("[WasmDelta] Sample.Test:TestMeaning", []);
outElement.innerHTML = ret;
})
var ret = INTERNAL.call_static_method("[WasmDelta] Sample.Test:TestMeaning", []);
outElement.innerHTML = ret;
console.log ("ready");
},
};
</script>
<script type="text/javascript" src="runtime.js"></script>

<script defer src="dotnet.js"></script>
<head>
<title>TESTS</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>

<body>
<h3 id="header">Wasm Browser Sample</h3>
Result from Sample.Test.TestMeaning: <span id="out"></span>
<div>
Click here (upto 2 times): <button id="update">Update</button>
</div>
<script type='text/javascript'>
var App = {
init: function () {
var outElement = document.getElementById("out");
document.getElementById("update").addEventListener("click", function () {
Module.INTERNAL.call_static_method("[WasmDelta] Sample.Test:Update", []);
console.log("applied update");
var ret = Module.INTERNAL.call_static_method("[WasmDelta] Sample.Test:TestMeaning", []);
outElement.innerHTML = ret;
})
var ret = Module.INTERNAL.call_static_method("[WasmDelta] Sample.Test:TestMeaning", []);
outElement.innerHTML = ret;
console.log("ready");
},
};
</script>
<script type="text/javascript" src="runtime.js"></script>

</body>
</html>
<script defer src="dotnet.js"></script>


</body>

</html>
13 changes: 7 additions & 6 deletions src/mono/sample/mbr/browser/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,30 @@

"use strict";
var Module = {
no_global_exports: true,
config: null,

preInit: async function () {
await MONO.mono_wasm_load_config("./mono-config.json"); // sets MONO.config implicitly
await Module.MONO.mono_wasm_load_config("./mono-config.json"); // sets Module.MONO.config implicitly
},

// Called when the runtime is initialized and wasm is ready
onRuntimeInitialized: function () {
if (!MONO.config || MONO.config.error) {
if (!Module.MONO.config || Module.MONO.config.error) {
console.log("An error occured while loading the config file");
return;
}

MONO.config.loaded_cb = function () {
Module.MONO.config.loaded_cb = function () {
App.init();
};
MONO.config.environment_variables = {
Module.MONO.config.environment_variables = {
"DOTNET_MODIFIABLE_ASSEMBLIES": "debug"
};
MONO.config.fetch_file_cb = function (asset) {
Module.MONO.config.fetch_file_cb = function (asset) {
return fetch(asset, { credentials: 'same-origin' });
}

MONO.mono_load_runtime_and_bcl_args(MONO.config);
Module.MONO.mono_load_runtime_and_bcl_args(Module.MONO.config);
},
};
Loading