webhost.lpr 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. program webhost;
  2. {$mode objfpc}
  3. uses
  4. BrowserConsole, JS, Types, Classes, SysUtils, Web, WasiEnv, WasiHostApp, JOB_Browser, JOB_Shared;
  5. var
  6. wasmFilename : string; external name 'wasmFilename';
  7. Type
  8. { TMyApplication }
  9. TMyApplication = class(TBrowserWASIHostApplication)
  10. Private
  11. FJOB : TJSObjectBridge;
  12. function GetWasmModuleName: String;
  13. Public
  14. constructor Create(aOwner : TComponent); override;
  15. procedure DoRun; override;
  16. end;
  17. { TMyApplication }
  18. constructor TMyApplication.Create(aOwner: TComponent);
  19. begin
  20. inherited Create(aOwner);
  21. FJOB:=TJSObjectBridge.Create(WasiEnvironment);
  22. RunEntryFunction:='_initialize';
  23. end;
  24. function TMyApplication.GetWasmModuleName : String;
  25. { Determine webassembly module to run
  26. 1. from external variable wasmFilename
  27. 2. from first part of hash: #moduleName/
  28. 3. from query varable wasmmodule: ?wasmmodule=x
  29. 4. Hardcoded 'demo.wasm';
  30. }
  31. begin
  32. Result:='';
  33. if IsString(wasmFilename) then
  34. Result:=wasmFilename;
  35. if (Result='') then
  36. Result:=ParamStr(1);
  37. if (Result='') then
  38. Result:=GetEnvironmentVar('wasmmodule');
  39. if Result='' then
  40. Result:='demo.wasm';
  41. end;
  42. procedure TMyApplication.DoRun;
  43. var
  44. WasmModule : String;
  45. begin
  46. Terminate;
  47. WasmModule:=GetWasmModuleName;
  48. Writeln('Loading & starting webassembly module :' ,WasmModule);
  49. StartWebAssembly(WasmModule,true);
  50. end;
  51. var
  52. Application : TMyApplication;
  53. begin
  54. ConsoleStyle:=DefaultCRTConsoleStyle;
  55. HookConsole;
  56. Application:=TMyApplication.Create(nil);
  57. Application.Initialize;
  58. Application.Run;
  59. end.