Browse Source

* Generic webassembly JOB loader

Michael Van Canneyt 1 year ago
parent
commit
8ea30c73c1

+ 19 - 0
demo/wasienv/job/host/README.md

@@ -0,0 +1,19 @@
+This directory contains a generic pas2js program to load a webassembly
+module using JOB. 
+
+By default it loads demo.wasm, but by creating a file host-config.js with
+the following contents:
+```javascript
+var 
+  wasmFilename = "wasmdemo.wasm";
+```
+you can change the loaded file without needing to recompile the application.
+
+You can also specify the webassembly module to load in the hash part of the
+URL:
+
+```
+http://localhost:8080/index.html#wasmdemo.wasm
+```
+will achieve the same as the config file.
+

+ 2 - 0
demo/wasienv/job/host/host-config-sample.js

@@ -0,0 +1,2 @@
+var 
+  wasmFilename = "wasmdemo.wasm";

+ 26 - 0
demo/wasienv/job/host/index.html

@@ -0,0 +1,26 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <title>Wasm Javascript Object Bindings - Test bed</title>
+    <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate"> <!-- Prevents caching -->
+    <meta http-equiv="Pragma" content="no-cache"> <!-- Legacy HTTP 1.0 backward compatibility -->
+    <meta http-equiv="Expires" content="0"> <!-- Proxies -->
+    <!-- In this file you can specify easily which .wasm file to load, 
+         no need to recompile the pas2js loader-->
+    <script src="host-config.js"></script>
+    <script src="wasmjobhost.js"></script>
+</head>
+<body>
+    <div>
+      <h3 >Webassembly JOB bindings test bed</h3>
+      <p>Console output:</p>
+      <div id="pasjsconsole">
+      </div>
+    </div>
+  <script>
+    rtl.showUncaughtExceptions=true;
+    rtl.run();
+  </script>
+</body>
+</html>

+ 104 - 0
demo/wasienv/job/host/wasmjobhost.lpi

@@ -0,0 +1,104 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<CONFIG>
+  <ProjectOptions>
+    <Version Value="12"/>
+    <General>
+      <Flags>
+        <MainUnitHasCreateFormStatements Value="False"/>
+        <MainUnitHasTitleStatement Value="False"/>
+        <MainUnitHasScaledStatement Value="False"/>
+        <Runnable Value="False"/>
+      </Flags>
+      <SessionStorage Value="InProjectDir"/>
+      <Title Value="BrowserDomTest1"/>
+      <UseAppBundle Value="False"/>
+      <ResourceType Value="res"/>
+    </General>
+    <CustomData Count="4">
+      <Item0 Name="MaintainHTML" Value="1"/>
+      <Item1 Name="Pas2JSProject" Value="1"/>
+      <Item2 Name="PasJSLocation" Value="BrowserTixeoDom"/>
+      <Item3 Name="PasJSWebBrowserProject" Value="1"/>
+    </CustomData>
+    <BuildModes>
+      <Item Name="Default" Default="True"/>
+    </BuildModes>
+    <PublishOptions>
+      <Version Value="2"/>
+      <UseFileFilters Value="True"/>
+    </PublishOptions>
+    <RunParams>
+      <FormatVersion Value="2"/>
+    </RunParams>
+    <Units>
+      <Unit>
+        <Filename Value="wasmjobhost.lpr"/>
+        <IsPartOfProject Value="True"/>
+      </Unit>
+      <Unit>
+        <Filename Value="index.html"/>
+        <IsPartOfProject Value="True"/>
+        <CustomData Count="1">
+          <Item0 Name="PasJSIsProjectHTMLFile" Value="1"/>
+        </CustomData>
+      </Unit>
+      <Unit>
+        <Filename Value="../../../packages/job/job_browser.pp"/>
+        <IsPartOfProject Value="True"/>
+        <UnitName Value="JOB_Browser"/>
+      </Unit>
+      <Unit>
+        <Filename Value="../../../packages/job/job_shared.pp"/>
+        <IsPartOfProject Value="True"/>
+        <UnitName Value="JOB_Shared"/>
+      </Unit>
+    </Units>
+  </ProjectOptions>
+  <CompilerOptions>
+    <Version Value="11"/>
+    <Target FileExt=".js">
+      <Filename Value="wasmjobhost"/>
+    </Target>
+    <SearchPaths>
+      <IncludeFiles Value="$(ProjOutDir)"/>
+      <OtherUnitFiles Value="../../../packages/job"/>
+      <UnitOutputDirectory Value="js"/>
+    </SearchPaths>
+    <Parsing>
+      <SyntaxOptions>
+        <AllowLabel Value="False"/>
+        <UseAnsiStrings Value="False"/>
+        <CPPInline Value="False"/>
+      </SyntaxOptions>
+    </Parsing>
+    <CodeGeneration>
+      <TargetOS Value="browser"/>
+    </CodeGeneration>
+    <Linking>
+      <Debugging>
+        <GenerateDebugInfo Value="False"/>
+        <UseLineInfoUnit Value="False"/>
+      </Debugging>
+    </Linking>
+    <Other>
+      <CustomOptions Value="-Jeutf-8
+-Jirtl.js
+-Jc
+-Jminclude"/>
+      <CompilerPath Value="$(pas2js)"/>
+    </Other>
+  </CompilerOptions>
+  <Debugging>
+    <Exceptions>
+      <Item>
+        <Name Value="EAbort"/>
+      </Item>
+      <Item>
+        <Name Value="ECodetoolError"/>
+      </Item>
+      <Item>
+        <Name Value="EFOpenError"/>
+      </Item>
+    </Exceptions>
+  </Debugging>
+</CONFIG>

+ 72 - 0
demo/wasienv/job/host/wasmjobhost.lpr

@@ -0,0 +1,72 @@
+program wasmjobhost;
+
+{$mode objfpc}
+
+uses
+  BrowserConsole, JS, Types, Classes, SysUtils, Web, WasiEnv, WasiHostApp, JOB_Browser, JOB_Shared;
+
+var
+  wasmFilename : string; external name 'wasmFilename';
+
+
+Type
+
+  { TMyApplication }
+
+  TMyApplication = class(TBrowserWASIHostApplication)
+  Private
+    FBridge : TJSObjectBridge;
+  Public
+    constructor Create(aOwner : TComponent); override;
+    procedure DoRun; override;
+  end;
+
+
+constructor TMyApplication.Create(aOwner: TComponent);
+begin
+  inherited Create(aOwner);
+  FBridge:=TJSObjectBridge.Create(WasiEnvironment);
+  RunEntryFunction:='_initialize';
+end;
+
+procedure TMyApplication.DoRun;
+
+  function DoError(aValue: JSValue): JSValue;
+  begin
+    if isObject(aValue) then
+      if TJSObject(aValue) is TJSError then
+        Writeln('Failed to start webassembly: ',TJSError(aValue).message)
+      else if TObject(aValue) is Exception then
+        Writeln('Failed to start webassembly: ',Exception(aValue).message);
+  end;
+
+var
+  wasmmodule : string;
+
+begin
+  // Your code here
+  Terminate;
+  if isString(wasmFilename) then
+    WasmModule:=wasmFilename
+  else
+    begin
+    WasmModule:=ParamStr(1);
+    if WasmModule='' then
+      WasmModule:='demo.wasm';
+    end;
+  try
+    StartWebAssembly(wasmmodule,true).catch(@DoError);
+  except
+    on E : exception do
+  end;
+end;
+
+var
+  Application : TMyApplication;
+begin
+  ConsoleStyle:=DefaultCRTConsoleStyle;
+  HookConsole;
+  Application:=TMyApplication.Create(nil);
+  Application.Initialize;
+  Application.Run;
+end.