Refactor the JS wasm loader

This commit is contained in:
Frank A. Krueger 2018-03-09 16:44:10 -08:00
parent 7bab361a63
commit 406a08c9cc
No known key found for this signature in database
GPG Key ID: 0471C67474FFE664
3 changed files with 87 additions and 65 deletions

View File

@ -13,7 +13,7 @@ Expand that into this directory.
## Build ## Build
```bash ```bash
csc /nostdlib /target:library /r:managed/mscorlib.dll /r:managed/Ooui.dll /out:managed/Ooui.Sample.dll ooui-sample.cs csc /nostdlib /target:library /r:managed/mscorlib.dll /r:managed/System.Runtime.dll /r:managed/Ooui.dll /out:managed/Ooui.Sample.dll ooui-sample.cs
``` ```

View File

@ -1,12 +1,17 @@
using System; using System;
using Ooui; using Ooui;
public class Math { public class Program
public static string Add (string a, string b) { {
public static string Main (string a0, string a1)
{
try { try {
Console.WriteLine ("ENTER"); var l = new Label { Text = "Hello" };
var e = new Div (); var b = new Button ("Click Me");
return e.Id; var e = new Div (new Div (l), b);
UI.SetGlobalElement ("main", e);
return e.ToString ();
} }
catch (Exception e) { catch (Exception e) {
Console.WriteLine (e); Console.WriteLine (e);

View File

@ -1,14 +1,16 @@
<!doctype html> <!DOCTYPE html>
<html lang="en-us"> <html>
<head> <head>
</head> <title>Ooui Wasm</title>
<body> <meta name="viewport" content="width=device-width, initial-scale=1" />
C# output: <link rel="stylesheet" href="https://ajax.aspnetcdn.com/ajax/bootstrap/3.3.7/css/bootstrap.min.css" />
<br> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<textarea rows="10" cols="100" id="output"></textarea> </head>
<br> <body>
<button type="button" onclick="App.onClick()" id="button" disabled="true">Run</button> <div id="ooui-body" class="container-fluid">
<br> <p id="loading"><i class="fa fa-refresh fa-spin" style="font-size:14px"></i>&nbsp;&nbsp;Loading...</p>
<textarea rows="10" cols="100" id="output" hidden="true"></textarea>
</div>
<script type='text/javascript'> <script type='text/javascript'>
var mainAsmName = "Ooui.Sample"; var mainAsmName = "Ooui.Sample";
@ -18,36 +20,36 @@
var bclLoadTime = null; var bclLoadTime = null;
var runtimeLoadTime = null; var runtimeLoadTime = null;
var Module = { var Module = {
onRuntimeInitialized: function () { onRuntimeInitialized: function () {
var assemblies = [ var assemblies = [
"mscorlib.dll", "mscorlib.dll",
"Microsoft.CSharp.dll",
"Newtonsoft.Json.dll",
"System.dll", "System.dll",
"System.Core.dll",
"System.Runtime.dll",
"System.Xml.XDocument.dll",
"System.Threading.Tasks.dll",
"System.Diagnostics.Debug.dll",
"System.ComponentModel.TypeConverter.dll",
"System.Dynamic.Runtime.dll",
"System.Runtime.Serialization.Primitives.dll",
"System.Collections.dll", "System.Collections.dll",
"System.ComponentModel.TypeConverter.dll",
"System.Core.dll",
"System.Diagnostics.Debug.dll",
"System.Dynamic.Runtime.dll",
"System.Globalization.dll",
"System.IO.dll",
"System.Linq.dll",
"System.Linq.Expressions.dll",
"System.ObjectModel.dll", "System.ObjectModel.dll",
"System.Text.RegularExpressions.dll", "System.Text.RegularExpressions.dll",
"System.Text.Encoding.dll", "System.Text.Encoding.dll",
"System.Text.Encoding.Extensions.dll", "System.Text.Encoding.Extensions.dll",
"System.Xml.ReaderWriter.dll",
"System.Xml.dll",
"System.IO.dll",
"System.Threading.dll", "System.Threading.dll",
"System.Globalization.dll", "System.Threading.Tasks.dll",
"System.Runtime.Extensions.dll",
"System.Reflection.dll", "System.Reflection.dll",
"System.Reflection.Extensions.dll", "System.Reflection.Extensions.dll",
"Microsoft.CSharp.dll", "System.Runtime.dll",
"System.Linq.dll", "System.Runtime.Extensions.dll",
"System.Linq.Expressions.dll", "System.Runtime.Serialization.Primitives.dll",
"Newtonsoft.Json.dll", "System.Xml.dll",
"System.Xml.ReaderWriter.dll",
"System.Xml.XDocument.dll",
"Ooui.dll", "Ooui.dll",
mainAsmName + ".dll" mainAsmName + ".dll"
]; ];
@ -91,20 +93,9 @@
this.find_method = Module.cwrap ('mono_wasm_assembly_find_method', 'number', ['number', 'string', 'number']); this.find_method = Module.cwrap ('mono_wasm_assembly_find_method', 'number', ['number', 'string', 'number']);
this.invoke_method = Module.cwrap ('mono_wasm_invoke_method', 'number', ['number', 'number', 'number']); this.invoke_method = Module.cwrap ('mono_wasm_invoke_method', 'number', ['number', 'number', 'number']);
this.mono_string_get_utf8 = Module.cwrap ('mono_wasm_string_get_utf8', 'number', ['number']); this.mono_string_get_utf8 = Module.cwrap ('mono_wasm_string_get_utf8', 'number', ['number']);
this.mono_string = Module.cwrap ('mono_wasm_string_from_js', 'number', ['string']) this.mono_string = Module.cwrap ('mono_wasm_string_from_js', 'number', ['string']);
this.load_runtime ("managed", 1); this.load_runtime ("managed", 1);
this.main_module = this.assembly_load (mainAsmName)
if (!this.main_module)
throw "Could not find Main Module sample.dll";
this.math_class = this.find_class (this.main_module, "", "Math")
if (!this.math_class)
throw "Could not find Math class in main module";
this.add_method = this.find_method (this.math_class, "Add", -1)
if (!this.add_method)
throw "Could not find add method";
runtimeLoadTime = Date.now (); runtimeLoadTime = Date.now ();
@ -144,33 +135,59 @@
return res; return res;
}, },
};
perform_add: function (a, b) { var App = {
init: function () {
this.loading = document.getElementById ("loading");
this.output = document.getElementById ("output");
this.findMethods ();
var res = this.runApp ("1", "2");
this.output.value = res;
this.output.hidden = false;
this.loading.hidden = true;
},
runApp: function (a, b) {
try { try {
var res = this.call_method (this.add_method, null, [this.mono_string (a), this.mono_string (b)]); MonoRuntime.call_method (this.ooui_method, null, [MonoRuntime.mono_string (a), MonoRuntime.mono_string (b)]);
return this.conv_string (res); var res = MonoRuntime.call_method (this.add_method, null, [MonoRuntime.mono_string (a), MonoRuntime.mono_string (b)]);
return MonoRuntime.conv_string (res);
} catch (e) { } catch (e) {
return e.msg; return e.msg;
} }
}, },
}; findMethods: function () {
this.ooui_module = MonoRuntime.assembly_load ("Ooui")
if (!this.ooui_module)
throw "Could not find Ooui.dll";
var App = { this.ooui_class = MonoRuntime.find_class (this.ooui_module, "Ooui", "UI")
onClick: function () { if (!this.ooui_class)
this.output.value = "..."; throw "Could not find UI class in Ooui module";
var res = MonoRuntime.perform_add("1", "2");
this.output.value = res;
},
init: function () { this.ooui_method = MonoRuntime.find_method (this.ooui_class, "StartWebAssemblySession", -1)
this.output = document.getElementById ("output"); if (!this.ooui_method)
this.button = document.getElementById ("button"); throw "Could not find StartWebAssemblySession method";
this.button.disabled = false; this.main_module = MonoRuntime.assembly_load (mainAsmName)
if (!this.main_module)
throw "Could not find Main Module " + mainAsmName + ".dll";
this.math_class = MonoRuntime.find_class (this.main_module, "", "Program")
if (!this.math_class)
throw "Could not find Program class in main module";
this.add_method = MonoRuntime.find_method (this.math_class, "Main", -1)
if (!this.add_method)
throw "Could not find Main method";
}, },
}; };
</script> </script>
<script async type="text/javascript" src="mono.js"></script> <script async type="text/javascript" src="mono.js"></script>
</body> </body>
</html> </html>