Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Some cleanup of propsal 1
  • Loading branch information
Daniel-Genkin committed Jun 8, 2021
commit 49dffa86c8018ac66883bddaf97e6cc73b91897c
18 changes: 17 additions & 1 deletion src/mono/sample/wasm/browser2/declarations/interop.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

// This file defines various types related with the interaction with Dotnet interop
// This file defines various types related with the interaction with Dotnet interop

// TODO find better types than the anys, however it will do for now

export interface BINDING { // Should BINDING be renamed?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, and it should belong to DotNet namespace.
Also all the methods here should be renamed to camelCase as usual in JS.

Also I would like to understand if this is public contract or just internal API for Mono team.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. I also didn't yet add the library_mono.js exported functions nor those from dotnet_support.js. Perhaps I can ask this during the team meeting today.

call_static_method: (method: string, params: any[]) => void,
mono_bindings_init: (binding: string) => void,
mono_bind_method: (method: string, this_arg: number, args_marshal: string?, friendly_name: string) => any,
mono_method_invoke: (method: number, this_arg: number, args_marshal: string, args: any) => any,
mono_method_get_call_signature: (method: string, mono_obj: any[]) => any,
mono_method_resolve: (fqn: string) => any,
mono_bind_static_method: (fqn: string, signature: string?) => any,
mono_call_static_method: (fqn: string, args: any, signature: string) => any,
mono_bind_assembly_entry_point: (assembly: any, signature: string?) => any,
mono_call_assembly_entry_point: (assembly: any, args: any, signature: string?) => any,
mono_intern_string: (string: string) => any,
}
2 changes: 1 addition & 1 deletion src/mono/sample/wasm/browser2/declarations/runtime.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

// This file defines various types related with the WASM API

export namespace DotNet {
declare namespace DotNet {
type onConfigLoaded = (config: MonoConfig) => MonoConfig;
type onRuntimeInitialized = () => void;
type onMonoRuntimeInitialized = (error: Error) => void;
Expand Down
7 changes: 1 addition & 6 deletions src/mono/sample/wasm/browser2/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@
<h3 id="header">Wasm Browser Sample</h3>
Result from Sample.Test.TestMeaning: <span id="out"></span>

<!-- Loads the User code -->
<script type="text/javascript" src="runtime.js"></script>

<!-- Loads our code (defer just delays the loading until after the page is loaded) -->
<script defer src="dotnet.js"></script>

<script type="text/javascript" src="main.js"></script>
</body>
</html>
20 changes: 12 additions & 8 deletions src/mono/sample/wasm/browser2/main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

import DotNet from "./dotnet.js"; // User can import DotNet so that there can even potentially be multiple instances in 1 app

// Called when the mono-config.js (soon to be mono-config.json via PR#53606) is loaded
function onConfigLoaded (config) {
// we can do some modification to config at runtime here
Expand Down Expand Up @@ -35,32 +37,34 @@ DotNet.onMonoRuntimeInitialized = onMonoRuntimeInitialized; // required as it ac


// FROM HTML SCRIPT TAG (all js code should be in js files) ////////////////////////////////////////////////////////////////////////////
var is_testing = false;
var onLoad = function() {
var url = new URL(decodeURI(window.location));
// Only modification was to use newer syntax.

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

var test_exit = function(exit_code) {
function test_exit (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");
let 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 = {
const App = {
init: function () {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to make clear this is user space, rename it to customInit or sampleInit

Copy link
Owner Author

@Daniel-Genkin Daniel-Genkin Jun 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this App object? I kept it to stay similar to the old browser sample. However, would it be more clear to remove this object and just have a main or init function?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in C# you mean? yes
in JS that is kind of preMain :-)

var ret = BINDING.call_static_method("[Wasm.Browser.Sample] Sample.Test:TestMeaning", []);
const ret = BINDING.call_static_method("[Wasm.Browser.Sample] Sample.Test:TestMeaning", []);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BINDING should become part of the DotNet import or separate import in DotNet.Binding.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To not pollute global namespace

document.getElementById("out").innerHTML = ret;

if (is_testing)
Expand All @@ -70,4 +74,4 @@ var App = {
test_exit(exit_code);
}
},
};
};
8 changes: 4 additions & 4 deletions src/mono/wasm/runtime/js_support.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

let DotNet = {}; // DotNet can be thought of as the exported API

function configLoaded(config) {
// In some cases there may be no DotNet object (such as in tests) so we no-op during the callback
// Note that DotNet is the new 'Module' object
const onConfigLoadedCallback = typeof DotNet !== "undefined" ? DotNet['onConfigLoaded'] : (_) => {};
const onMonoRuntimeInitializedCallback = typeof DotNet !== "undefined" ? DotNet['onMonoRuntimeInitialized'] : (_) => {};
const onConfigLoadedCallback = DotNet['onConfigLoaded'];
const onMonoRuntimeInitializedCallback = DotNet['onMonoRuntimeInitialized'];
// Note: onRuntimeInitialized is called by emsdk in another location

config.loaded_cb = function () {
Expand Down