diff Samples/WebAssembly/RtViewer/RtViewerWasmApp.js @ 1393:27e0a00bd3e8

RtViewer SingleFrameViewer OK : wasm SDL single viewport other viewports ongoing
author Benjamin Golinvaux <bgo@osimis.io>
date Wed, 29 Apr 2020 15:54:18 +0200
parents 24bcff8ea58f
children 3e644f6fadd4
line wrap: on
line diff
--- a/Samples/WebAssembly/RtViewer/RtViewerWasmApp.js	Tue Apr 28 13:52:21 2020 +0200
+++ b/Samples/WebAssembly/RtViewer/RtViewerWasmApp.js	Wed Apr 29 15:54:18 2020 +0200
@@ -1,11 +1,33 @@
-// Check support for WebAssembly
-if (!('WebAssembly' in window)) {
-  alert('Sorry, your browser does not support WebAssembly :(');
-} else {
+
+// This object wraps the functions exposed by the wasm module
+
+const WasmModuleWrapper = function() {
+  this._InitializeViewport = undefined;
+};
+
+WasmModuleWrapper.prototype.Setup = function(Module) {
+  this._SetArgument = Module.cwrap('SetArgument', null, [ 'string', 'string' ]);
+  this._Initialize = Module.cwrap('Initialize', null, [ 'string' ]);
+};
 
-  // Wait for the module to be loaded (the event "WebAssemblyLoaded"
-  // must be emitted by the "main" function)
-  window.addEventListener('WebAssemblyLoaded', function() {
+WasmModuleWrapper.prototype.SetArgument = function(key, value) {
+  this._SetArgument(key, value);
+};
+
+WasmModuleWrapper.prototype.Initialize = function(canvasId) {
+  this._Initialize(canvasId);
+};
+
+var wasmModuleWrapper = new WasmModuleWrapper();
+
+$(document).ready(function() {
+
+  window.addEventListener('WasmModuleInitialized', function() {
+
+    // bind the C++ global functions
+    wasmModuleWrapper.Setup(Module);
+
+    console.warn('Native C++ module initialized');
 
     // Loop over the GET arguments
     var parameters = window.location.search.substr(1);
@@ -14,15 +36,50 @@
       for (var i = 0; i < tokens.length; i++) {
         var arg = tokens[i].split('=');
         if (arg.length == 2) {
-
           // Send each GET argument to WebAssembly
-          Module.ccall('SetArgument', null, [ 'string', 'string' ],
-                       [ arg[0], decodeURIComponent(arg[1]) ]);
+          wasmModuleWrapper.SetArgument(arg[0], decodeURIComponent(arg[1]));
         }
       }
     }
+    wasmModuleWrapper.Initialize();
+  });
 
-    // Inform the WebAssembly module that it can start
-    Module.ccall('Initialize', null, null, null);
-  });
-}
+  window.addEventListener('StoneException', function() {
+    alert('Exception caught in C++ code');
+  });    
+
+  var scriptSource;
+
+  if ('WebAssembly' in window) {
+    console.warn('Loading WebAssembly');
+    scriptSource = 'RtViewerWasm.js';
+  } else {
+    console.error('Your browser does not support WebAssembly!');
+  }
+
+  // Option 1: Loading script using plain HTML
+  
+  /*
+    var script = document.createElement('script');
+    script.src = scriptSource;
+    script.type = 'text/javascript';
+    document.body.appendChild(script);
+  */
+
+  // Option 2: Loading script using AJAX (gives the opportunity to
+  // report explicit errors)
+  
+  axios.get(scriptSource)
+    .then(function (response) {
+      var script = document.createElement('script');
+      script.innerHTML = response.data;
+      script.type = 'text/javascript';
+      document.body.appendChild(script);
+    })
+    .catch(function (error) {
+      alert('Cannot load the WebAssembly framework');
+    });
+});
+
+// http://localhost:9979/rtviewer/index.html?loglevel=trace&ctseries=CTSERIES&rtdose=RTDOSE&rtstruct=RTSTRUCT
+