regexphost.lpr 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. {
  2. This file is part of the Free Component Library
  3. Webassembly RegExp API - Demo program
  4. Copyright (c) 2024 by Michael Van Canneyt [email protected]
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. program regexphost;
  12. {$mode objfpc}
  13. {$modeswitch externalclass}
  14. uses
  15. BrowserConsole, BrowserApp, WASIHostApp, JS, Classes, SysUtils, Web, wasm.pas2js.regexp;
  16. type
  17. THostConfig = class external name 'Object' (TJSObject)
  18. wasmFilename : String;
  19. logRegExpAPI : Boolean;
  20. logWasiAPI : Boolean;
  21. end;
  22. var
  23. HostConfig : THostConfig; external name 'hostConfig';
  24. Type
  25. { TMyApplication }
  26. TMyApplication = class(TWASIHostApplication)
  27. cbLog:TJSHTMLInputElement;
  28. FRegexp : TWasmRegExpAPI;
  29. private
  30. procedure HandleLogClick(Event: TJSEvent);
  31. protected
  32. procedure DoRun; override;
  33. public
  34. Constructor Create(aOwner : TComponent); override;
  35. end;
  36. procedure TMyApplication.DoRun;
  37. var
  38. wasm : String;
  39. begin
  40. Terminate;
  41. if Assigned(HostConfig) and isString(HostConfig.wasmFilename) then
  42. Wasm:=HostConfig.wasmFilename
  43. else
  44. begin
  45. Wasm:=ParamStr(1);
  46. if Wasm='' then
  47. Wasm:='wasmregexpdemo.wasm';
  48. end;
  49. StartWebAssembly(wasm);
  50. end;
  51. procedure TMyApplication.HandleLogClick(Event : TJSEvent);
  52. begin
  53. FRegexp.LogAPICalls:=cbLog.Checked;
  54. end;
  55. constructor TMyApplication.Create(aOwner: TComponent);
  56. begin
  57. inherited Create(aOwner);
  58. FRegexp:=TWasmRegExpAPI.Create(WasiEnvironment);
  59. if isDefined(hostConfig) and Assigned(hostConfig) then
  60. begin
  61. WasiEnvironment.LogAPI:=HostConfig.logWasiAPi;
  62. FRegexp.LogAPICalls:=HostConfig.logRegExpAPI;
  63. end;
  64. cbLog:=TJSHTMLInputElement(GetHTMLElement('cbLog'));
  65. cbLog.Checked:=FRegexp.LogAPICalls;
  66. cbLog.addEventListener('click',@HandleLogClick);
  67. end;
  68. var
  69. Application : TMyApplication;
  70. begin
  71. Application:=TMyApplication.Create(nil);
  72. Application.Initialize;
  73. Application.Run;
  74. end.