Browse Source

wasmjob: simplified example

mattias 3 years ago
parent
commit
88e26c886f
1 changed files with 11 additions and 86 deletions
  1. 11 86
      demo/wasienv/button/BrowserButton1.lpr

+ 11 - 86
demo/wasienv/button/BrowserButton1.lpr

@@ -4,116 +4,41 @@ program BrowserButton1;
 
 
 uses
 uses
   BrowserConsole, BrowserApp, JS, Classes, SysUtils, Web, WebAssembly, Types,
   BrowserConsole, BrowserApp, JS, Classes, SysUtils, Web, WebAssembly, Types,
-  WasiEnv, JOB_Shared, JOB_Browser;
+  WasiEnv, WasiHostApp, JOB_Shared, JOB_Browser;
 
 
 Type
 Type
 
 
   { TMyApplication }
   { TMyApplication }
 
 
-  TMyApplication = class(TBrowserApplication)
-  // todo: most of this code should be moved a TBrowserJOBApplication
+  TMyApplication = class(TBrowserWASIHostApplication)
   Private
   Private
-    FWasiEnv: TPas2JSWASIEnvironment;
-    FMemory : TJSWebAssemblyMemory; // Memory of webassembly
-    FTable : TJSWebAssemblyTable; // Table of exported functions
     FWADomBridge : TJSObjectBridge;
     FWADomBridge : TJSObjectBridge;
-    function CreateWebAssembly(Path: string; ImportObject: TJSObject
-      ): TJSPromise;
-    procedure DoWrite(Sender: TObject; const aOutput: String);
-    function InitEnv(aValue: JSValue): JSValue;
-    procedure InitWebAssembly;
+    function OnBeforeStart(Sender: TObject;
+      aDescriptor: TWebAssemblyStartDescriptor): Boolean;
   Public
   Public
     Constructor Create(aOwner : TComponent); override;
     Constructor Create(aOwner : TComponent); override;
-    Destructor Destroy; override;
     procedure DoRun; override;
     procedure DoRun; override;
   end;
   end;
 
 
-function TMyApplication.InitEnv(aValue: JSValue): JSValue;
-Var
-  Module : TJSInstantiateResult absolute aValue;
-  Exps : TWASIExports;
-  InitFunc: TProc;
+function TMyApplication.OnBeforeStart(Sender: TObject;
+  aDescriptor: TWebAssemblyStartDescriptor): Boolean;
 begin
 begin
-  Result:=True;
-  FWasiEnv.Instance:=Module.Instance;
-  Exps := TWASIExports(TJSObject(Module.Instance.exports_));
-  //writeln('TMyApplication.InitEnv wasm exports=',TJSObject.keys(Exps));
-  FWADomBridge.WasiExports:=Exps;
-
-  // init the library
-  InitFunc:=TProc(Exps.functions['_initialize']);
-  writeln('TMyApplication.InitEnv ');
-  InitFunc();
-end;
-
-{ TMyApplication }
-
-procedure TMyApplication.DoWrite(Sender: TObject; const aOutput: String);
-begin
-  Writeln(aOutput);
+  FWADomBridge.WasiExports:=aDescriptor.Exported;
+  Result:=true;
 end;
 end;
 
 
 constructor TMyApplication.Create(aOwner: TComponent);
 constructor TMyApplication.Create(aOwner: TComponent);
 begin
 begin
   inherited Create(aOwner);
   inherited Create(aOwner);
-  FWasiEnv:=TPas2JSWASIEnvironment.Create;
-  FWasiEnv.OnStdErrorWrite:=@DoWrite;
-  FWasiEnv.OnStdOutputWrite:=@DoWrite;
-  FWADomBridge:=TJSObjectBridge.Create(FWasiEnv);
-end;
-
-function TMyApplication.CreateWebAssembly(Path: string; ImportObject: TJSObject): TJSPromise;
-begin
-  Result:=window.fetch(Path)._then(Function (res : jsValue) : JSValue
-    begin
-      Result:=TJSResponse(Res).arrayBuffer._then(Function (res2 : jsValue) : JSValue
-        begin
-          Result:=TJSWebAssembly.instantiate(TJSArrayBuffer(res2),ImportObject);
-        end,Nil)
-    end,Nil
-  );
-end;
-
-procedure TMyApplication.InitWebAssembly;
-
-Var
-  mDesc: TJSWebAssemblyMemoryDescriptor;
-  tDesc: TJSWebAssemblyTableDescriptor;
-  ImportObj : TJSObject;
-
-begin
-  //  Setup memory
-  mDesc.initial:=256;
-  mDesc.maximum:=256;
-  FMemory:=TJSWebAssemblyMemory.New(mDesc);
-  // Setup table
-  tDesc.initial:=0;
-  tDesc.maximum:=0;
-  tDesc.element:='anyfunc';
-  FTable:=TJSWebAssemblyTable.New(tDesc);
-  // Setup ImportObject
-  ImportObj:=new([
-    'js', new([
-      'mem', FMemory,
-      'tbl', FTable
-    ])
- ]);
-  FWasiEnv.AddImports(ImportObj);
-  CreateWebAssembly('WasiButton1.wasm',ImportObj)._then(@InitEnv);
-end;
-
-destructor TMyApplication.Destroy;
-begin
-  FreeAndNil(FWasiEnv);
-  inherited Destroy;
+  FWADomBridge:=TJSObjectBridge.Create(WasiEnvironment);
+  RunEntryFunction:='_initialize';
 end;
 end;
 
 
 procedure TMyApplication.DoRun;
 procedure TMyApplication.DoRun;
 
 
 begin
 begin
   // Your code here
   // Your code here
-  Terminate;
-  InitWebAssembly;
+  StartWebAssembly('WasiButton1.wasm',true,@OnBeforeStart);
 end;
 end;
 
 
 var
 var