Răsfoiți Sursa

wasi: job: write property object via separate wasi call

mattias 3 ani în urmă
părinte
comite
00c7d0ff4b
3 a modificat fișierele cu 47 adăugiri și 23 ștergeri
  1. 13 4
      demo/wasienv/dom/job_browser.pp
  2. 1 0
      demo/wasienv/dom/job_shared.pp
  3. 33 19
      demo/wasienv/dom/job_wasm.pas

+ 13 - 4
demo/wasienv/dom/job_browser.pp

@@ -25,7 +25,8 @@ Type
   Protected
   Protected
     function FindObject(ObjId: TJOBObjectID): TJSObject; virtual;
     function FindObject(ObjId: TJOBObjectID): TJSObject; virtual;
     function Invoke_JSResult(ObjId: TJOBObjectID; NameP, NameLen, Invoke, ArgsP: NativeInt; out JSResult: JSValue): TJOBResult; virtual;
     function Invoke_JSResult(ObjId: TJOBObjectID; NameP, NameLen, Invoke, ArgsP: NativeInt; out JSResult: JSValue): TJOBResult; virtual;
-    function Invoke_NoResult(ObjId: TJOBObjectID; NameP, NameLen, Invoke, ArgsP: NativeInt): TJOBResult; virtual;
+    function Invoke_NoResult(ObjId: TJOBObjectID; NameP, NameLen, ArgsP: NativeInt): TJOBResult; virtual;
+    function Set_JSProperty(ObjId: TJOBObjectID; NameP, NameLen, ArgsP: NativeInt): TJOBResult; virtual;
     function Invoke_BooleanResult(ObjId: TJOBObjectID; NameP, NameLen, Invoke, ArgsP, ResultP: NativeInt): TJOBResult; virtual;
     function Invoke_BooleanResult(ObjId: TJOBObjectID; NameP, NameLen, Invoke, ArgsP, ResultP: NativeInt): TJOBResult; virtual;
     function Invoke_DoubleResult(ObjId: TJOBObjectID; NameP, NameLen, Invoke, ArgsP, ResultP: NativeInt): TJOBResult; virtual;
     function Invoke_DoubleResult(ObjId: TJOBObjectID; NameP, NameLen, Invoke, ArgsP, ResultP: NativeInt): TJOBResult; virtual;
     function Invoke_StringResult(ObjId: TJOBObjectID; NameP, NameLen, Invoke, ArgsP, ResultP: NativeInt): TJOBResult; virtual;
     function Invoke_StringResult(ObjId: TJOBObjectID; NameP, NameLen, Invoke, ArgsP, ResultP: NativeInt): TJOBResult; virtual;
@@ -74,6 +75,7 @@ end;
 procedure TJOBBridge.FillImportObject(aObject: TJSObject);
 procedure TJOBBridge.FillImportObject(aObject: TJSObject);
 begin
 begin
   aObject[JOBFn_InvokeNoResult]:=@Invoke_NoResult;
   aObject[JOBFn_InvokeNoResult]:=@Invoke_NoResult;
+  aObject[JOBFn_SetProperty]:=@Set_JSProperty;
   aObject[JOBFn_InvokeBooleanResult]:=@Invoke_BooleanResult;
   aObject[JOBFn_InvokeBooleanResult]:=@Invoke_BooleanResult;
   aObject[JOBFn_InvokeDoubleResult]:=@Invoke_DoubleResult;
   aObject[JOBFn_InvokeDoubleResult]:=@Invoke_DoubleResult;
   aObject[JOBFn_InvokeStringResult]:=@Invoke_StringResult;
   aObject[JOBFn_InvokeStringResult]:=@Invoke_StringResult;
@@ -153,12 +155,19 @@ begin
 end;
 end;
 
 
 function TJOBBridge.Invoke_NoResult(ObjId: TJOBObjectID; NameP, NameLen,
 function TJOBBridge.Invoke_NoResult(ObjId: TJOBObjectID; NameP, NameLen,
-  Invoke, ArgsP: NativeInt): TJOBResult;
+  ArgsP: NativeInt): TJOBResult;
 var
 var
   JSResult: JSValue;
   JSResult: JSValue;
 begin
 begin
-  // invoke
-  Result:=Invoke_JSResult(ObjId,NameP,NameLen,Invoke,ArgsP,JSResult);
+  Result:=Invoke_JSResult(ObjId,NameP,NameLen,JOBInvokeCall,ArgsP,JSResult);
+end;
+
+function TJOBBridge.Set_JSProperty(ObjId: TJOBObjectID; NameP, NameLen,
+  ArgsP: NativeInt): TJOBResult;
+var
+  JSResult: JSValue;
+begin
+  Result:=Invoke_JSResult(ObjId,NameP,NameLen,JOBInvokeSetter,ArgsP,JSResult);
 end;
 end;
 
 
 function TJOBBridge.Invoke_BooleanResult(ObjId: TJOBObjectID; NameP, NameLen,
 function TJOBBridge.Invoke_BooleanResult(ObjId: TJOBObjectID; NameP, NameLen,

+ 1 - 0
demo/wasienv/dom/job_shared.pp

@@ -52,6 +52,7 @@ const
 
 
   JOBExportName = 'job';
   JOBExportName = 'job';
   JOBFn_InvokeNoResult = 'invoke_noresult';
   JOBFn_InvokeNoResult = 'invoke_noresult';
+  JOBFn_SetProperty = 'setproperty';
   JOBFn_InvokeBooleanResult = 'invoke_boolresult';
   JOBFn_InvokeBooleanResult = 'invoke_boolresult';
   JOBFn_InvokeDoubleResult = 'invoke_doubleresult';
   JOBFn_InvokeDoubleResult = 'invoke_doubleresult';
   JOBFn_InvokeStringResult = 'invoke_stringresult';
   JOBFn_InvokeStringResult = 'invoke_stringresult';

+ 33 - 19
demo/wasienv/dom/job_wasm.pas

@@ -42,10 +42,6 @@ Type
     jigCall,  // call function
     jigCall,  // call function
     jigGetter // read property
     jigGetter // read property
     );
     );
-  TJOBInvokeSetType = (
-    jisCall,  // call function
-    jisSetter // write property
-    );
 
 
   TJSObject = class;
   TJSObject = class;
   TJSObjectClass = class of TJSObject;
   TJSObjectClass = class of TJSObject;
@@ -62,11 +58,12 @@ Type
     procedure InvokeJS_RaiseResultMismatch(const aName: string; Expected, Actual: TJOBResult); virtual;
     procedure InvokeJS_RaiseResultMismatch(const aName: string; Expected, Actual: TJOBResult); virtual;
     procedure InvokeJS_RaiseResultMismatchStr(const aName: string; const Expected, Actual: string); virtual;
     procedure InvokeJS_RaiseResultMismatchStr(const aName: string; const Expected, Actual: string); virtual;
     function CreateInvokeJSArgs(const Args: array of const): PByte; virtual;
     function CreateInvokeJSArgs(const Args: array of const): PByte; virtual;
+    procedure SetJSProperty(const aName: string; Const Args: Array of const); virtual;
   public
   public
     constructor CreateFromID(aID: TJOBObjectID); virtual;
     constructor CreateFromID(aID: TJOBObjectID); virtual;
     destructor Destroy; override;
     destructor Destroy; override;
     property ObjectID: TJOBObjectID read FObjectID;
     property ObjectID: TJOBObjectID read FObjectID;
-    procedure InvokeJSNoResult(const aName: string; Const Args: Array of const; Invoke: TJOBInvokeSetType = jisCall); virtual;
+    procedure InvokeJSNoResult(const aName: string; Const Args: Array of const); virtual;
     function InvokeJSBooleanResult(const aName: string; Const Args: Array of const; Invoke: TJOBInvokeGetType = jigCall): Boolean; virtual;
     function InvokeJSBooleanResult(const aName: string; Const Args: Array of const; Invoke: TJOBInvokeGetType = jigCall): Boolean; virtual;
     function InvokeJSDoubleResult(const aName: string; Const Args: Array of const; Invoke: TJOBInvokeGetType = jigCall): Double; virtual;
     function InvokeJSDoubleResult(const aName: string; Const Args: Array of const; Invoke: TJOBInvokeGetType = jigCall): Double; virtual;
     function InvokeJSUnicodeStringResult(const aName: string; Const Args: Array of const; Invoke: TJOBInvokeGetType = jigCall): UnicodeString; virtual;
     function InvokeJSUnicodeStringResult(const aName: string; Const Args: Array of const; Invoke: TJOBInvokeGetType = jigCall): UnicodeString; virtual;
@@ -96,10 +93,16 @@ function __job_invoke_noresult(
   ObjID: TJOBObjectID;
   ObjID: TJOBObjectID;
   NameP: PChar;
   NameP: PChar;
   NameLen: longint;
   NameLen: longint;
-  Invoke: longint;
   ArgP: PByte
   ArgP: PByte
 ): TJOBResult; external JOBExportName name JOBFn_InvokeNoResult;
 ): TJOBResult; external JOBExportName name JOBFn_InvokeNoResult;
 
 
+function __job_setproperty(
+  ObjID: TJOBObjectID;
+  NameP: PChar;
+  NameLen: longint;
+  ArgP: PByte
+): TJOBResult; external JOBExportName name JOBFn_SetProperty;
+
 function __job_invoke_boolresult(
 function __job_invoke_boolresult(
   ObjID: TJOBObjectID;
   ObjID: TJOBObjectID;
   NameP: PChar;
   NameP: PChar;
@@ -154,10 +157,6 @@ const
     JOBInvokeCall,
     JOBInvokeCall,
     JOBInvokeGetter
     JOBInvokeGetter
     );
     );
-  InvokeSetToInt: array[TJOBInvokeSetType] of integer = (
-    JOBInvokeCall,
-    JOBInvokeSetter
-    );
 
 
 {$IFDEF VerboseJOB}
 {$IFDEF VerboseJOB}
 function GetVarRecName(vt: word): string;
 function GetVarRecName(vt: word): string;
@@ -506,6 +505,21 @@ begin
   {$ENDIF}
   {$ENDIF}
 end;
 end;
 
 
+procedure TJSObject.SetJSProperty(const aName: string;
+  const Args: array of const);
+var
+  InvokeArgs: PByte;
+begin
+  if length(Args)<>1 then
+    InvokeJS_Raise(aName,'wrong arg count');
+  InvokeArgs:=CreateInvokeJSArgs(Args);
+  try
+    __job_setproperty(ObjectID,PChar(aName),length(aName),InvokeArgs);
+  finally
+    FreeMem(InvokeArgs);
+  end;
+end;
+
 constructor TJSObject.CreateFromID(aID: TJOBObjectID);
 constructor TJSObject.CreateFromID(aID: TJOBObjectID);
 begin
 begin
   FObjectID:=aID;
   FObjectID:=aID;
@@ -519,17 +533,17 @@ begin
 end;
 end;
 
 
 procedure TJSObject.InvokeJSNoResult(const aName: string;
 procedure TJSObject.InvokeJSNoResult(const aName: string;
-  const Args: array of const; Invoke: TJOBInvokeSetType);
+  const Args: array of const);
 var
 var
   aError: TJOBResult;
   aError: TJOBResult;
   InvokeArgs: PByte;
   InvokeArgs: PByte;
 begin
 begin
   if length(Args)=0 then
   if length(Args)=0 then
-    aError:=__job_invoke_noresult(ObjectID,PChar(aName),length(aName),InvokeSetToInt[Invoke],nil)
+    aError:=__job_invoke_noresult(ObjectID,PChar(aName),length(aName),nil)
   else begin
   else begin
     InvokeArgs:=CreateInvokeJSArgs(Args);
     InvokeArgs:=CreateInvokeJSArgs(Args);
     try
     try
-      aError:=__job_invoke_noresult(ObjectID,PChar(aName),length(aName),InvokeSetToInt[Invoke],InvokeArgs);
+      aError:=__job_invoke_noresult(ObjectID,PChar(aName),length(aName),InvokeArgs);
     finally
     finally
       if InvokeArgs<>nil then
       if InvokeArgs<>nil then
         FreeMem(InvokeArgs);
         FreeMem(InvokeArgs);
@@ -658,35 +672,35 @@ end;
 
 
 procedure TJSObject.WriteJSPropertyBoolean(const aName: string; Value: Boolean);
 procedure TJSObject.WriteJSPropertyBoolean(const aName: string; Value: Boolean);
 begin
 begin
-  InvokeJSNoResult(aName,[Value],jisSetter);
+  SetJSProperty(aName,[Value]);
 end;
 end;
 
 
 procedure TJSObject.WriteJSPropertyDouble(const aName: string; Value: Double);
 procedure TJSObject.WriteJSPropertyDouble(const aName: string; Value: Double);
 begin
 begin
-  InvokeJSNoResult(aName,[Value],jisSetter);
+  SetJSProperty(aName,[Value]);
 end;
 end;
 
 
 procedure TJSObject.WriteJSPropertyUnicodeString(const aName: string;
 procedure TJSObject.WriteJSPropertyUnicodeString(const aName: string;
   const Value: UnicodeString);
   const Value: UnicodeString);
 begin
 begin
-  InvokeJSNoResult(aName,[Value],jisSetter);
+  SetJSProperty(aName,[Value]);
 end;
 end;
 
 
 procedure TJSObject.WriteJSPropertyUtf8String(const aName: string;
 procedure TJSObject.WriteJSPropertyUtf8String(const aName: string;
   const Value: String);
   const Value: String);
 begin
 begin
-  InvokeJSNoResult(aName,[Value],jisSetter);
+  SetJSProperty(aName,[Value]);
 end;
 end;
 
 
 procedure TJSObject.WriteJSPropertyObject(const aName: string; Value: TJSObject
 procedure TJSObject.WriteJSPropertyObject(const aName: string; Value: TJSObject
   );
   );
 begin
 begin
-  InvokeJSNoResult(aName,[Value],jisSetter);
+  SetJSProperty(aName,[Value]);
 end;
 end;
 
 
 procedure TJSObject.WriteJSPropertyLongInt(const aName: string; Value: LongInt);
 procedure TJSObject.WriteJSPropertyLongInt(const aName: string; Value: LongInt);
 begin
 begin
-  InvokeJSNoResult(aName,[Value],jisSetter);
+  SetJSProperty(aName,[Value]);
 end;
 end;
 
 
 initialization
 initialization