mirror of
https://gitlab.com/freepascal.org/fpc/pas2js.git
synced 2025-08-20 10:49:05 +02:00
* Let the StartWebAssembly return a promise, allow to initialize library
This commit is contained in:
parent
6e630afa75
commit
c85aad66f4
@ -307,7 +307,10 @@ type
|
|||||||
// Standard FPC exports.
|
// Standard FPC exports.
|
||||||
TWASIExports = Class External name 'Object' (TJSModulesExports)
|
TWASIExports = Class External name 'Object' (TJSModulesExports)
|
||||||
Public
|
Public
|
||||||
|
// Program
|
||||||
Procedure start; external name '_start';
|
Procedure start; external name '_start';
|
||||||
|
// Library
|
||||||
|
Procedure initialize; external name '_initialize';
|
||||||
function AllocMem(aSize : Integer) : Integer; external name 'wasiAlloc';
|
function AllocMem(aSize : Integer) : Integer; external name 'wasiAlloc';
|
||||||
function freeMem(aLocation : Integer) : Integer; external name 'wasiFree';
|
function freeMem(aLocation : Integer) : Integer; external name 'wasiFree';
|
||||||
end;
|
end;
|
||||||
@ -497,6 +500,8 @@ type
|
|||||||
FReadLineCount : Integer;
|
FReadLineCount : Integer;
|
||||||
FRunEntryFunction: String;
|
FRunEntryFunction: String;
|
||||||
FTableDescriptor : TJSWebAssemblyTableDescriptor;
|
FTableDescriptor : TJSWebAssemblyTableDescriptor;
|
||||||
|
function GetIsLibrary: Boolean;
|
||||||
|
function GetIsProgram: Boolean;
|
||||||
function GetStartDescriptorReady: Boolean;
|
function GetStartDescriptorReady: Boolean;
|
||||||
function GetUseSharedMemory: Boolean;
|
function GetUseSharedMemory: Boolean;
|
||||||
procedure SetPredefinedConsoleInput(AValue: TStrings);
|
procedure SetPredefinedConsoleInput(AValue: TStrings);
|
||||||
@ -539,7 +544,7 @@ type
|
|||||||
// Load and start webassembly. If DoRun is true, then Webassembly entry point is called.
|
// Load and start webassembly. If DoRun is true, then Webassembly entry point is called.
|
||||||
// If aBeforeStart is specified, then it is called prior to calling run, and can disable running.
|
// If aBeforeStart is specified, then it is called prior to calling run, and can disable running.
|
||||||
// If aAfterStart is specified, then it is called after calling run. It is not called if running was disabled.
|
// If aAfterStart is specified, then it is called after calling run. It is not called if running was disabled.
|
||||||
Procedure StartWebAssembly(aPath: string; DoRun: Boolean; aBeforeStart: TBeforeStartCallback; aAfterStart: TAfterStartCallback);
|
function StartWebAssembly(aPath: string; DoRun: Boolean; aBeforeStart: TBeforeStartCallback; aAfterStart: TAfterStartCallback) : TJSPromise;
|
||||||
// Run the prepared descriptor
|
// Run the prepared descriptor
|
||||||
Procedure RunPreparedDescriptor;
|
Procedure RunPreparedDescriptor;
|
||||||
// Initial memory descriptor
|
// Initial memory descriptor
|
||||||
@ -554,7 +559,10 @@ type
|
|||||||
Property StartDescriptorReady : Boolean Read GetStartDescriptorReady;
|
Property StartDescriptorReady : Boolean Read GetStartDescriptorReady;
|
||||||
// Default console input
|
// Default console input
|
||||||
Property PredefinedConsoleInput : TStrings Read FPredefinedConsoleInput Write SetPredefinedConsoleInput;
|
Property PredefinedConsoleInput : TStrings Read FPredefinedConsoleInput Write SetPredefinedConsoleInput;
|
||||||
|
// Is it a library ?
|
||||||
|
Property IsLibrary : Boolean Read GetIsLibrary;
|
||||||
|
// Is it a program ?
|
||||||
|
Property IsProgram : Boolean Read GetIsProgram;
|
||||||
// Name of function to run. If empty, the FPC default _start is used.
|
// Name of function to run. If empty, the FPC default _start is used.
|
||||||
Property RunEntryFunction : String Read FRunEntryFunction Write FRunEntryFunction;
|
Property RunEntryFunction : String Read FRunEntryFunction Write FRunEntryFunction;
|
||||||
// Called after webassembly start was run. Not called if webassembly was not run.
|
// Called after webassembly start was run. Not called if webassembly was not run.
|
||||||
@ -622,6 +630,16 @@ begin
|
|||||||
Result:=Assigned(Memory) and Assigned(Module);
|
Result:=Assigned(Memory) and Assigned(Module);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
function TWASIHost.GetIsLibrary: Boolean;
|
||||||
|
begin
|
||||||
|
Result:=Assigned(FExported.functions['_initialize']);
|
||||||
|
end;
|
||||||
|
|
||||||
|
function TWASIHost.GetIsProgram: Boolean;
|
||||||
|
begin
|
||||||
|
Result:=Assigned(FExported.functions['_start']);
|
||||||
|
end;
|
||||||
|
|
||||||
procedure TWASIHost.SetUseSharedMemory(AValue: Boolean);
|
procedure TWASIHost.SetUseSharedMemory(AValue: Boolean);
|
||||||
begin
|
begin
|
||||||
FMemoryDescriptor.shared:=aValue;
|
FMemoryDescriptor.shared:=aValue;
|
||||||
@ -707,6 +725,21 @@ begin
|
|||||||
WriteOutput(aOutput);
|
WriteOutput(aOutput);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
function ValueToMessage(Res : JSValue) : string;
|
||||||
|
|
||||||
|
begin
|
||||||
|
if isObject(Res) then
|
||||||
|
begin
|
||||||
|
Result:=TObject(Res).ClassName;
|
||||||
|
if TObject(Res) is Exception then
|
||||||
|
Result:=Result+': '+Exception(Res).Message
|
||||||
|
end;
|
||||||
|
if (JsTypeOf(Res)='object') and (TJSObject(Res).hasOwnProperty('message')) then
|
||||||
|
Result:=String(TJSObject(Res)['message'])
|
||||||
|
else
|
||||||
|
Result:=TJSJSON.Stringify(Res);
|
||||||
|
end;
|
||||||
|
|
||||||
function TWASIHost.CreateWebAssembly(aPath: string; aImportObject: TJSObject
|
function TWASIHost.CreateWebAssembly(aPath: string; aImportObject: TJSObject
|
||||||
): TJSPromise;
|
): TJSPromise;
|
||||||
|
|
||||||
@ -718,8 +751,12 @@ function TWASIHost.CreateWebAssembly(aPath: string; aImportObject: TJSObject
|
|||||||
|
|
||||||
Function InstantiateFail(Res : JSValue) : JSValue;
|
Function InstantiateFail(Res : JSValue) : JSValue;
|
||||||
|
|
||||||
|
var
|
||||||
|
S : String;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
Result:=False;
|
Result:=False;
|
||||||
|
console.Log('Instantiating of WebAssembly from '+aPath+' failed '+ValueToMessage(Res));
|
||||||
DoInstantiateFail(res);
|
DoInstantiateFail(res);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
@ -739,6 +776,7 @@ function TWASIHost.CreateWebAssembly(aPath: string; aImportObject: TJSObject
|
|||||||
function DoFail(res : jsValue) : JSValue;
|
function DoFail(res : jsValue) : JSValue;
|
||||||
begin
|
begin
|
||||||
Result:=False;
|
Result:=False;
|
||||||
|
console.Log('Loading of WebAssembly from '+aPath+' failed '+ValueToMessage(Res));
|
||||||
DoLoadFail(res);
|
DoLoadFail(res);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
@ -802,7 +840,7 @@ begin
|
|||||||
Result:=RunWebAssemblyInstance(aBeforeStart,aAfterStart,Nil);
|
Result:=RunWebAssemblyInstance(aBeforeStart,aAfterStart,Nil);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TWASIHost.StartWebAssembly(aPath: string; DoRun: Boolean; aBeforeStart: TBeforeStartCallback; aAfterStart: TAfterStartCallback);
|
function TWASIHost.StartWebAssembly(aPath: string; DoRun: Boolean; aBeforeStart: TBeforeStartCallback; aAfterStart: TAfterStartCallback) : TJSPromise;
|
||||||
|
|
||||||
Var
|
Var
|
||||||
WASD : TWebAssemblyStartDescriptor;
|
WASD : TWebAssemblyStartDescriptor;
|
||||||
@ -813,7 +851,6 @@ Var
|
|||||||
InstResult : TJSInstantiateResult absolute aValue;
|
InstResult : TJSInstantiateResult absolute aValue;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
Result:=True;
|
|
||||||
if not (jsTypeOf(aValue)='object') then
|
if not (jsTypeOf(aValue)='object') then
|
||||||
Raise EWasiError.Create('Did not get a instantiated webassembly');
|
Raise EWasiError.Create('Did not get a instantiated webassembly');
|
||||||
WASD.Instance:=InstResult.Instance;
|
WASD.Instance:=InstResult.Instance;
|
||||||
@ -822,13 +859,17 @@ Var
|
|||||||
WASD.CallRun:=Procedure(aExports : TWASIExports)
|
WASD.CallRun:=Procedure(aExports : TWASIExports)
|
||||||
begin
|
begin
|
||||||
if FRunEntryFunction='' then
|
if FRunEntryFunction='' then
|
||||||
aExports.Start
|
if Assigned(aExports['_initialize']) then
|
||||||
|
aExports.initialize
|
||||||
|
else
|
||||||
|
aExports.Start
|
||||||
else
|
else
|
||||||
TProcedure(aExports[RunEntryFunction])();
|
TProcedure(aExports[RunEntryFunction])();
|
||||||
end;
|
end;
|
||||||
PrepareWebAssemblyInstance(WASD);
|
PrepareWebAssemblyInstance(WASD);
|
||||||
if DoRun then
|
if DoRun then
|
||||||
RunWebAssemblyInstance(aBeforeStart,aAfterStart,Nil);
|
RunWebAssemblyInstance(aBeforeStart,aAfterStart,Nil);
|
||||||
|
Result:=TJSPromise.resolve(WASD);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function DoFail(aValue: JSValue): JSValue;
|
function DoFail(aValue: JSValue): JSValue;
|
||||||
@ -844,7 +885,7 @@ begin
|
|||||||
// Clear current descriptor.
|
// Clear current descriptor.
|
||||||
FPreparedStartDescriptor:=Default(TWebAssemblyStartDescriptor);
|
FPreparedStartDescriptor:=Default(TWebAssemblyStartDescriptor);
|
||||||
WASD:=InitStartDescriptor(GetMemory,GetTable,Nil);
|
WASD:=InitStartDescriptor(GetMemory,GetTable,Nil);
|
||||||
CreateWebAssembly(aPath,WASD.Imports)._then(@initEnv,@DoFail).catch(@DoFail);
|
Result:=CreateWebAssembly(aPath,WASD.Imports)._then(@initEnv,@DoFail).catch(@DoFail);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TWASIHost.RunPreparedDescriptor;
|
procedure TWASIHost.RunPreparedDescriptor;
|
||||||
|
@ -5,7 +5,7 @@ unit wasihostapp;
|
|||||||
interface
|
interface
|
||||||
|
|
||||||
uses
|
uses
|
||||||
Classes, SysUtils, browserapp, webassembly, wasienv;
|
Classes, SysUtils, browserapp, js, webassembly, wasienv;
|
||||||
|
|
||||||
Type
|
Type
|
||||||
|
|
||||||
@ -41,7 +41,7 @@ Type
|
|||||||
// Load and start webassembly. If DoRun is true, then Webassembly entry point is called.
|
// Load and start webassembly. If DoRun is true, then Webassembly entry point is called.
|
||||||
// If aBeforeStart is specified, then it is called prior to calling run, and can disable running.
|
// If aBeforeStart is specified, then it is called prior to calling run, and can disable running.
|
||||||
// If aAfterStart is specified, then it is called after calling run. It is not called is running was disabled.
|
// If aAfterStart is specified, then it is called after calling run. It is not called is running was disabled.
|
||||||
Procedure StartWebAssembly(aPath: string; DoRun : Boolean = True; aBeforeStart : TBeforeStartCallback = Nil; aAfterStart : TAfterStartCallback = Nil);
|
function StartWebAssembly(aPath: string; DoRun : Boolean = True; aBeforeStart : TBeforeStartCallback = Nil; aAfterStart : TAfterStartCallback = Nil) : TJSPromise;
|
||||||
// Initial memory descriptor
|
// Initial memory descriptor
|
||||||
Property MemoryDescriptor : TJSWebAssemblyMemoryDescriptor Read GetMemoryDescriptor Write SetMemoryDescriptor;
|
Property MemoryDescriptor : TJSWebAssemblyMemoryDescriptor Read GetMemoryDescriptor Write SetMemoryDescriptor;
|
||||||
// Import/export table descriptor
|
// Import/export table descriptor
|
||||||
@ -179,11 +179,11 @@ begin
|
|||||||
inherited Destroy;
|
inherited Destroy;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TBrowserWASIHostApplication.StartWebAssembly(aPath: string; DoRun: Boolean;
|
function TBrowserWASIHostApplication.StartWebAssembly(aPath: string; DoRun: Boolean;
|
||||||
aBeforeStart: TBeforeStartCallback = nil; aAfterStart: TAfterStartCallback = nil);
|
aBeforeStart: TBeforeStartCallback = nil; aAfterStart: TAfterStartCallback = nil) : TJSPromise;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
FHost.StartWebAssembly(aPath,DoRun,aBeforeStart,aAfterStart);
|
Result:=FHost.StartWebAssembly(aPath,DoRun,aBeforeStart,aAfterStart);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
end.
|
end.
|
||||||
|
Loading…
Reference in New Issue
Block a user