Browse Source

wasmjob: callback variant arg and result

mattias 3 years ago
parent
commit
be5baeec71
1 changed files with 100 additions and 4 deletions
  1. 100 4
      demo/wasienv/dom/job_js.pas

+ 100 - 4
demo/wasienv/dom/job_js.pas

@@ -137,6 +137,7 @@ type
     function GetString: UnicodeString;
     function GetString: UnicodeString;
     function GetObject(aResultClass: TJSObjectClass): TJSObject;
     function GetObject(aResultClass: TJSObjectClass): TJSObject;
     function GetValue: TJOB_JSValue;
     function GetValue: TJOB_JSValue;
+    function GetVariant: Variant;
     function GetLongInt: longint;
     function GetLongInt: longint;
     function GetMaxInt: int64;
     function GetMaxInt: int64;
 
 
@@ -146,10 +147,11 @@ type
     function AllocDouble(const d: double): PByte;
     function AllocDouble(const d: double): PByte;
     function AllocString(const s: UnicodeString): PByte;
     function AllocString(const s: UnicodeString): PByte;
     function AllocNil: PByte;
     function AllocNil: PByte;
-    function AllocIntf(Intf: IJSObject): PByte;
+    function AllocIntf(const Intf: IJSObject): PByte;
     function AllocObject(Obj: TJSObject): PByte;
     function AllocObject(Obj: TJSObject): PByte;
     function AllocObjId(ObjId: TJOBObjectID): PByte;
     function AllocObjId(ObjId: TJOBObjectID): PByte;
-    function AllocJSValue(Value: TJOB_JSValue): PByte;
+    function AllocJSValue(const Value: TJOB_JSValue): PByte;
+    function AllocVariant(const Value: Variant): PByte;
   end;
   end;
 
 
   TJOBCallback = function(const aMethod: TMethod; var H: TJOBCallbackHelper): PByte;
   TJOBCallback = function(const aMethod: TMethod; var H: TJOBCallbackHelper): PByte;
@@ -1926,6 +1928,67 @@ begin
   inc(Index);
   inc(Index);
 end;
 end;
 
 
+function TJOBCallbackHelper.GetVariant: Variant;
+var
+  ObjId, Len: LongWord;
+  Obj: TJSObject;
+  S: UnicodeString;
+begin
+  if (Index=Count) or (p^=JOBArgUndefined) then
+  begin
+    Result:=Variants.Unassigned;
+    exit;
+  end;
+  case p^ of
+  JOBArgTrue:
+    begin
+      Result:=true;
+      inc(p);
+    end;
+  JOBArgFalse:
+    begin
+      Result:=false;
+      inc(p);
+    end;
+  JOBArgDouble:
+    begin
+      inc(p);
+      Result:=PDouble(p)^;
+      inc(p,8);
+    end;
+  JOBArgUnicodeString:
+    begin
+      inc(p);
+      Len:=PLongWord(p)^;
+      inc(p,4);
+      S:='';
+      if Len>0 then
+      begin
+        SetLength(S,Len);
+        Move(p^,S[1],2*Len);
+        inc(p,2*Len);
+      end;
+      Result:=S;
+    end;
+  JOBArgNil:
+    begin
+      Result:=Variants.Null;
+      inc(p);
+    end;
+  JOBArgObject:
+    begin
+      inc(p);
+      ObjId:=PLongWord(p)^;
+      inc(p,4);
+      Obj:=TJSObject.JOBCreateFromID(ObjId);
+      Result:=Obj as IJSObject;
+    end;
+  else
+    raise EJSArgParse.Create(JOBArgNames[p^]);
+  end;
+  inc(Index);
+end;
+
 function TJOBCallbackHelper.GetLongInt: longint;
 function TJOBCallbackHelper.GetLongInt: longint;
 var
 var
   d: Double;
   d: Double;
@@ -1995,7 +2058,7 @@ begin
   Result^:=JOBArgNil;
   Result^:=JOBArgNil;
 end;
 end;
 
 
-function TJOBCallbackHelper.AllocIntf(Intf: IJSObject): PByte;
+function TJOBCallbackHelper.AllocIntf(const Intf: IJSObject): PByte;
 begin
 begin
   if Intf=nil then
   if Intf=nil then
     Result:=AllocNil
     Result:=AllocNil
@@ -2018,7 +2081,7 @@ begin
   PJOBObjectID(Result+1)^:=ObjId;
   PJOBObjectID(Result+1)^:=ObjId;
 end;
 end;
 
 
-function TJOBCallbackHelper.AllocJSValue(Value: TJOB_JSValue): PByte;
+function TJOBCallbackHelper.AllocJSValue(const Value: TJOB_JSValue): PByte;
 begin
 begin
   if Value=nil then
   if Value=nil then
     exit(AllocUndefined);
     exit(AllocUndefined);
@@ -2033,6 +2096,39 @@ begin
   end;
   end;
 end;
 end;
 
 
+function TJOBCallbackHelper.AllocVariant(const Value: Variant): PByte;
+var
+  t: tvartype;
+  Intf: IJSObject;
+begin
+  t:=VarType(Value);
+  case t of
+  varEmpty:
+    Result:=AllocUndefined;
+  varNull:
+    Result:=AllocNil;
+  varSmallInt,varInteger,varByte,varWord,varShortInt:
+    Result:=AllocLongint(Value);
+  varLongWord,varCurrency,varInt64,varQWord,varSingle,varDouble,varDate:
+    Result:=AllocDouble(Value);
+  varOleStr,varString:
+    Result:=AllocString(Value);
+  varBoolean:
+    Result:=AllocBool(Value);
+  varUnknown:
+    begin
+    if tvardata(Value).vunknown=nil then
+      Result:=AllocNil
+    else if VarSupports(Value,IJSObject,Intf) then
+      Result:=AllocIntf(Intf)
+    else
+      raise EJSInvoke.Create('TJOBCallbackHelper.AllocVariant: [20220822103744] unsupported variant: '+IntToStr(t));
+    end
+  else
+    raise EJSInvoke.Create('TJOBCallbackHelper.AllocVariant: [20220822103751] unsupported variant: '+IntToStr(t));
+  end;
+end;
+
 { TJOB_JSValue }
 { TJOB_JSValue }
 
 
 constructor TJOB_JSValue.Create(aKind: TJOB_JSValueKind);
 constructor TJOB_JSValue.Create(aKind: TJOB_JSValueKind);