Skip to content

Commit

Permalink
[WASM] Converted mono-config.js to mono-config.json (dotnet#53606)
Browse files Browse the repository at this point in the history
* converted mono-config.js to mono-config.json

* Fixed test

* fixed handling of case where Module isn't defined

* Fixed remaining failing tests

* Replaced alerts with console.log to fix Helix test

* replaced all vars with consts

* use fetch

* clean syntax

* prevent timeouts when the mono-config file fails to load

* Moved config file loading to preInit

* Fixed tests

* Adjusted file linking

* removed the unnecessary js_support.js

* cleaned up function

* updated samples

* removed lingering pre-js flag

* Fixed trimming tests

* addressed PR comments

* removed useless function
  • Loading branch information
Daniel-Genkin committed Jun 21, 2021
1 parent f891033 commit a3f0e2b
Show file tree
Hide file tree
Showing 19 changed files with 218 additions and 109 deletions.
1 change: 0 additions & 1 deletion src/mono/sample/mbr/browser/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ <h3 id="header">Wasm Browser Sample</h3>
},
};
</script>
<script type="text/javascript" src="mono-config.js"></script>
<script type="text/javascript" src="runtime.js"></script>

<script defer src="dotnet.js"></script>
Expand Down
20 changes: 16 additions & 4 deletions src/mono/sample/mbr/browser/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,29 @@
// The .NET Foundation licenses this file to you under the MIT license.

var Module = {
config: null,

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

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

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

MONO.mono_load_runtime_and_bcl_args (config);
MONO.mono_load_runtime_and_bcl_args (Module.config);
},
};
1 change: 0 additions & 1 deletion src/mono/sample/wasm/browser-bench/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ <h3 id="header">Wasm Browser Sample - Simple Benchmark</h3>
}
};
</script>
<script type="text/javascript" src="mono-config.js"></script>
<script type="text/javascript" src="runtime.js"></script>

<script defer src="dotnet.js"></script>
Expand Down
27 changes: 23 additions & 4 deletions src/mono/sample/wasm/browser-bench/runtime.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,42 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

var Module = {
config: null,

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

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

Module.config.loaded_cb = function () {
try {
App.init ();
} catch (error) {
test_exit(1);
throw (error);
}
};
config.fetch_file_cb = function (asset) {
Module.config.fetch_file_cb = function (asset) {
return fetch (asset, { credentials: 'same-origin' });
}

if (Module.config.enable_profiler)
{
Module.config.aot_profiler_options = {
write_at:"Sample.Test::StopProfile",
send_to: "System.Runtime.InteropServices.JavaScript.Runtime::DumpAotProfileData"
}
}

try
{
MONO.mono_load_runtime_and_bcl_args (config);
MONO.mono_load_runtime_and_bcl_args (Module.config);
} catch (error) {
test_exit(1);
throw(error);
Expand Down
59 changes: 0 additions & 59 deletions src/mono/sample/wasm/browser-profile/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,67 +10,8 @@
<body>
<h3 id="header">Wasm Browser Sample</h3>
Result from Sample.Test.TestMeaning: <span id="out"></span>
<script type='text/javascript'>
var is_testing = false;
var onLoad = function() {
var url = new URL(decodeURI(window.location));
let args = url.searchParams.getAll('arg');
is_testing = args !== undefined && (args.find(arg => arg == '--testing') !== undefined);
};

var test_exit = function(exit_code)
{
if (!is_testing) {
console.log(`test_exit: ${exit_code}`);
return;
}

/* Set result in a tests_done element, to be read by xharness */
var tests_done_elem = document.createElement("label");
tests_done_elem.id = "tests_done";
tests_done_elem.innerHTML = exit_code.toString();
document.body.appendChild(tests_done_elem);

console.log(`WASM EXIT ${exit_code}`);
};

var App = {
init: function () {
var ret = BINDING.call_static_method("[Wasm.BrowserProfile.Sample] Sample.Test:TestMeaning", []);
document.getElementById("out").innerHTML = ret;
console.log ("ready");

if (is_testing)
{
console.debug(`ret: ${ret}`);
let exit_code = ret == 42 ? 0 : 1;
test_exit(exit_code);
}

if (config.enable_profiler) {
BINDING.call_static_method("[Wasm.BrowserProfile.Sample] Sample.Test:StopProfile", []);
saveProfile();
}
},
};

function saveProfile() {
var a = document.createElement('a');
var blob = new Blob([Module.aot_profile_data]);
a.href = URL.createObjectURL(blob);
a.download = "data.aotprofile";
// Append anchor to body.
document.body.appendChild(a);
a.click();

// Remove anchor from body
document.body.removeChild(a);
}

</script>
<script type="text/javascript" src="mono-config.js"></script>
<script type="text/javascript" src="runtime.js"></script>

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

</body>
Expand Down
86 changes: 76 additions & 10 deletions src/mono/sample/wasm/browser-profile/runtime.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,99 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
var Module = {
var Module = {
is_testing: false,
config: null,

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

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

Module.config.loaded_cb = function () {
try {
App.init ();
Module.init();
} catch (error) {
test_exit(1);
Module.test_exit(1);
throw (error);
}
};
config.fetch_file_cb = function (asset) {
Module.config.fetch_file_cb = function (asset) {
return fetch (asset, { credentials: 'same-origin' });
}

if (config.enable_profiler)
if (Module.config.enable_profiler)
{
config.aot_profiler_options = {
Module.config.aot_profiler_options = {
write_at:"Sample.Test::StopProfile",
send_to: "System.Runtime.InteropServices.JavaScript.Runtime::DumpAotProfileData"
}
}

try
{
MONO.mono_load_runtime_and_bcl_args (config);
MONO.mono_load_runtime_and_bcl_args (Module.config);
} catch (error) {
test_exit(1);
Module.test_exit(1);
throw(error);
}
},

init: function () {
console.log("not ready yet")
var ret = BINDING.call_static_method("[Wasm.BrowserProfile.Sample] Sample.Test:TestMeaning", []);
document.getElementById("out").innerHTML = ret;
console.log ("ready");

if (Module.is_testing)
{
console.debug(`ret: ${ret}`);
let exit_code = ret == 42 ? 0 : 1;
Module.test_exit(exit_code);
}

if (Module.config.enable_profiler) {
BINDING.call_static_method("[Wasm.BrowserProfile.Sample] Sample.Test:StopProfile", []);
Module.saveProfile();
}
},

onLoad: function() {
var url = new URL(decodeURI(window.location));
let args = url.searchParams.getAll('arg');
Module.is_testing = args !== undefined && (args.find(arg => arg == '--testing') !== undefined);
},

test_exit: function(exit_code) {
if (!Module.is_testing) {
console.log(`test_exit: ${exit_code}`);
return;
}

/* Set result in a tests_done element, to be read by xharness */
var tests_done_elem = document.createElement("label");
tests_done_elem.id = "tests_done";
tests_done_elem.innerHTML = exit_code.toString();
document.body.appendChild(tests_done_elem);

console.log(`WASM EXIT ${exit_code}`);
},

saveProfile: function () {
var a = document.createElement('a');
var blob = new Blob([Module.aot_profile_data]);
a.href = URL.createObjectURL(blob);
a.download = "data.aotprofile";
// Append anchor to body.
document.body.appendChild(a);
a.click();

// Remove anchor from body
document.body.removeChild(a);
}
};
};
1 change: 0 additions & 1 deletion src/mono/sample/wasm/browser/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ <h3 id="header">Wasm Browser Sample</h3>
},
};
</script>
<script type="text/javascript" src="mono-config.js"></script>
<script type="text/javascript" src="runtime.js"></script>

<script defer src="dotnet.js"></script>
Expand Down
19 changes: 16 additions & 3 deletions src/mono/sample/wasm/browser/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,35 @@
// The .NET Foundation licenses this file to you under the MIT license.

var Module = {

config: null,

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

// Called when the runtime is initialized and wasm is ready
onRuntimeInitialized: function () {
config.loaded_cb = function () {
if (!Module.config || Module.config.error) {
console.log("No config found");
return;
}

Module.config.loaded_cb = function () {
try {
App.init ();
} catch (error) {
test_exit(1);
throw (error);
}
};
config.fetch_file_cb = function (asset) {
Module.config.fetch_file_cb = function (asset) {
return fetch (asset, { credentials: 'same-origin' });
}

try
{
MONO.mono_load_runtime_and_bcl_args (config);
MONO.mono_load_runtime_and_bcl_args (Module.config);
} catch (error) {
test_exit(1);
throw(error);
Expand Down
2 changes: 1 addition & 1 deletion src/mono/wasm/build/WasmApp.targets
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
- @(WasmFilesToIncludeInFileSystem) - Files to include in the vfs
- @(WasmNativeAsset) - Native files to be added to `NativeAssets` in the bundle.
- @(WasmExtraConfig) - json elements to add to `mono-config.js`
- @(WasmExtraConfig) - json elements to add to `mono-config.json`
Eg. <WasmExtraConfig Include="enable_profiler" Value="true" />
- Value attribute can have a number, bool, quoted string, or json string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@
return App.int_add (a, b);
}
</script>
<script type="text/javascript" src="mono-config.js"></script>
<script type="text/javascript" src="runtime-debugger.js"></script>
<script type="text/javascript" src="other.js"></script>
<script async type="text/javascript" src="dotnet.js"></script>
Expand Down
18 changes: 15 additions & 3 deletions src/mono/wasm/debugger/tests/debugger-test/runtime-debugger.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,20 @@
// The .NET Foundation licenses this file to you under the MIT license.

var Module = {
onRuntimeInitialized: function () {
config.loaded_cb = function () {
config: null,

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

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

Module.config.loaded_cb = function () {
App.init ();
};
// For custom logging patch the functions below
Expand All @@ -15,6 +27,6 @@ var Module = {
MONO.mono_wasm_setenv ("MONO_LOG_LEVEL", "debug");
MONO.mono_wasm_setenv ("MONO_LOG_MASK", "all");
*/
MONO.mono_load_runtime_and_bcl_args (config)
MONO.mono_load_runtime_and_bcl_args (Module.config)
},
};
Loading

0 comments on commit a3f0e2b

Please sign in to comment.