Răsfoiți Sursa

wasi: job: read/write property object

mattias 3 ani în urmă
părinte
comite
83b37657ff

+ 2 - 2
demo/wasienv/dom/BrowserDomTest1.lpr

@@ -12,13 +12,13 @@ Type
 
   TBird = class
   public
-    Name: string;
     constructor Create(const aName: string); reintroduce;
     procedure Proc;
     function ArgsToStr(Args: TJSFunctionArguments): string;
   published
     Size: integer;
-    Caption: string;
+    Name: string;
+    Child: TBird;
     function GetBoolean: boolean;
     function GetDouble: double;
     function GetString: string;

+ 41 - 7
demo/wasienv/dom/WasiDomTest1.lpr

@@ -13,21 +13,47 @@ type
 
   TBird = class(TJSObject)
   private
+    function GetName: string;
+    function GetChild: TBird;
     function GetSize: integer;
+    procedure SetName(const AValue: string);
+    procedure SetChild(const AValue: TBird);
     procedure SetSize(const AValue: integer);
   public
     function GetDouble: double;
     function GetInteger: integer;
+    property Name: string read GetName write SetName;
     property Size: integer read GetSize write SetSize;
+    property Child: TBird read GetChild write SetChild;
   end;
 
 { TBird }
 
+function TBird.GetName: string;
+begin
+  Result:=ReadJSPropertyUtf8String('Name');
+end;
+
+function TBird.GetChild: TBird;
+begin
+  Result:=ReadJSPropertyObject('Child',TBird) as TBird;
+end;
+
 function TBird.GetSize: integer;
 begin
   Result:=ReadJSPropertyLongInt('Size');
 end;
 
+procedure TBird.SetName(const AValue: string);
+begin
+  WriteJSPropertyUtf8String('Name',AValue);
+end;
+
+procedure TBird.SetChild(const AValue: TBird);
+begin
+  WriteJSPropertyObject('Child',AValue);
+end;
+
 procedure TBird.SetSize(const AValue: integer);
 begin
   WriteJSPropertyLongInt('Size',AValue);
@@ -47,22 +73,30 @@ var
   obj: TJSObject;
   d: Double;
   u: UnicodeString;
-  Freddy: TBird;
+  Freddy, Alice, aBird: TBird;
   i: Integer;
 begin
   obj:=TJSObject.CreateFromID(WasiObjIdBird);
+  obj.WriteJSPropertyUnicodeString('Caption','Root');
   writeln('AAA1 ');
   u:='äbc';
 
   //obj.InvokeJSNoResult('Proc',[]);
   //d:=obj.InvokeJSDoubleResult('GetDouble',[u,12345678901]);
+  writeln('Create Freddy...');
   Freddy:=obj.InvokeJSObjResult('CreateChick',['Freddy'],TBird) as TBird;
-  writeln('AAA3 ');
-  Freddy.Size:=81;
-  writeln('AAA4 ');
-  i:=Freddy.Size;
-  writeln('AAA5 ',i);
+  writeln('AAA5 ',Freddy.Name);
+  writeln('Create Alice...');
+  Alice:=obj.InvokeJSObjResult('CreateChick',['Alice'],TBird) as TBird;
+  writeln('Freddy.Child:=Alice...');
+  Freddy.Child:=Alice;
+  aBird:=Freddy.Child;
+  writeln('Freddy.Child=',aBird.Name);
+
+
+  writeln('Freeing Freddy...');
   Freddy.Free;
-  writeln('AAA6 ');
+  writeln('Freeing Alice...');
+  Alice.Free;
 end.
 

+ 17 - 2
demo/wasienv/dom/job_browser.pp

@@ -214,6 +214,7 @@ begin
     exit(GetJOBResult(JSResult));
   Result:=JOBResult_String;
   FStringResult:=String(JSResult);
+  //writeln('TJOBBridge.Invoke_StringResult FStringResult="',FStringResult,'"');
 
   // set result length
   getModuleMemoryDataView().setInt32(ResultP, length(FStringResult), env.IsLittleEndian);
@@ -273,7 +274,7 @@ begin
   if l=0 then exit;
   View:=getModuleMemoryDataView();
   for i:=0 to l-1 do
-    View.setUint16(ResultP+2*i,ord(FStringResult[i]),env.IsLittleEndian);
+    View.setUint16(ResultP+2*i,ord(FStringResult[i+1]),env.IsLittleEndian);
   FStringResult:='';
 end;
 
@@ -292,6 +293,8 @@ var
   Len, Ptr: LongWord;
   aBytes: TJSUint8Array;
   aWords: TJSUint16Array;
+  ObjID: LongInt;
+  Obj: TJSObject;
 begin
   p:=ArgsP;
   Cnt:=View.getUInt8(p);
@@ -328,6 +331,7 @@ begin
         inc(p,4);
         aBytes:=TJSUint8Array.New(View.buffer, Ptr,Len);
         Result[i]:=TypedArrayToString(aBytes);
+        //writeln('TJOBBridge.GetInvokeArguments UTF8String="',Result[i],'"');
       end;
     JOBArgUnicodeString:
       begin
@@ -338,11 +342,22 @@ begin
         aWords:=TJSUint16Array.New(View.buffer, Ptr,Len);
         Result[i]:=TypedArrayToString(aWords);
       end;
+    JOBArgNil:
+      Result[i]:=nil;
     JOBArgPointer:
       begin
         Result[i]:=View.getUint32(p,env.IsLittleEndian);
         inc(p,4);
-      end
+      end;
+    JOBArgObject:
+      begin
+        ObjID:=View.getInt32(p,env.IsLittleEndian);
+        inc(p,4);
+        Obj:=FindObject(ObjID);
+        if Obj=nil then
+          raise Exception.Create('invalid JSObject'+IntToStr(ObjID));
+        Result[i]:=Obj;
+      end;
     else
       raise Exception.Create('unknown arg type '+IntToStr(aType));
     end;

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

@@ -68,7 +68,9 @@ const
   JOBArgChar = 5; // followed by a word
   JOBArgUTF8String = 6; // followed by length and pointer
   JOBArgUnicodeString = 7; // followed by length and pointer
-  JOBArgPointer = 8;
+  JOBArgNil = 8;
+  JOBArgPointer = 9;
+  JOBArgObject = 10; // followed by ObjectID
 
   JOBInvokeCall = 0;
   JOBInvokeGetter = 1;

+ 31 - 3
demo/wasienv/dom/job_wasm.pas

@@ -85,7 +85,7 @@ Type
     procedure WriteJSPropertyDouble(const aName: string; Value: Double); virtual;
     procedure WriteJSPropertyUnicodeString(const aName: string; const Value: UnicodeString); virtual;
     procedure WriteJSPropertyUtf8String(const aName: string; const Value: String); virtual;
-    // ToDo: procedure WriteJSPropertyObject(const aName: string; AnObjectID: TJOBObjectID); virtual;
+    procedure WriteJSPropertyObject(const aName: string; Value: TJSObject); virtual;
     procedure WriteJSPropertyLongInt(const aName: string; Value: LongInt); virtual;
   end;
 
@@ -259,6 +259,7 @@ var
   ws: WideString;
   us: UnicodeString;
   d: Double;
+  Obj: TObject;
 begin
   Result:=nil;
   if length(Args)>255 then
@@ -289,7 +290,13 @@ begin
       end;
     vtObject        :
       begin
-        RaiseNotSupported('object');
+        Obj:=Args[i].VObject;
+        if Obj=nil then
+          inc(Len,1)
+        else if Obj is TJSObject then
+          inc(Len,1+sizeof(TJOBObjectID))
+        else
+          RaiseNotSupported('object');
       end;
     vtClass         : RaiseNotSupported('class');
     vtPWideChar     : RaiseNotSupported('pwidechar');
@@ -398,7 +405,20 @@ begin
         PPointer(p)^:=h;
         inc(p,sizeof(Pointer));
       end;
-    vtObject        : ;
+    vtObject        :
+      begin
+        Obj:=Args[i].VObject;
+        if Obj=nil then
+        begin
+          p^:=JOBArgNil;
+          inc(p);
+        end else begin
+          p^:=JOBArgObject;
+          inc(p);
+          PNativeInt(p)^:=TJSObject(Args[i].VObject).ObjectID;
+          inc(p,sizeof(NativeInt));
+        end;
+      end;
     vtClass         : ;
     vtPWideChar     : ;
     vtAnsiString    :
@@ -558,12 +578,14 @@ begin
   try
     // try to allocate the memory
     SetLength(Result,ResultLen);
+    //writeln('TJSObject.InvokeJSUnicodeStringResult ResultLen=',ResultLen);
     aError:=JOBResult_Success;
   finally
     if aError<>JOBResult_Success then
       __job_releasestringresult();
   end;
   __job_getstringresult(PByte(Result));
+  //writeln('TJSObject.InvokeJSUnicodeStringResult Result="',Result,'"');
 end;
 
 function TJSObject.InvokeJSObjResult(const aName: string;
@@ -656,6 +678,12 @@ begin
   InvokeJSNoResult(aName,[Value],jisSetter);
 end;
 
+procedure TJSObject.WriteJSPropertyObject(const aName: string; Value: TJSObject
+  );
+begin
+  InvokeJSNoResult(aName,[Value],jisSetter);
+end;
+
 procedure TJSObject.WriteJSPropertyLongInt(const aName: string; Value: LongInt);
 begin
   InvokeJSNoResult(aName,[Value],jisSetter);