Browse Source

wasmjob: test callback boolean and integer

mattias 3 years ago
parent
commit
6e8bc2507d
2 changed files with 108 additions and 5 deletions
  1. 7 0
      demo/wasienv/dom/BrowserDomTest1.lpr
  2. 101 5
      demo/wasienv/dom/WasiDomTest1.lpr

+ 7 - 0
demo/wasienv/dom/BrowserDomTest1.lpr

@@ -6,6 +6,7 @@ uses
   BrowserConsole, JS, Classes, SysUtils, Web, WasiEnv, WasiHostApp, JOB_Browser, JOB_Shared;
   BrowserConsole, JS, Classes, SysUtils, Web, WasiEnv, WasiHostApp, JOB_Browser, JOB_Shared;
 
 
 Type
 Type
+  TBirdCallback = function(const v: JSValue): JSValue;
 
 
   { TBird }
   { TBird }
 
 
@@ -26,6 +27,7 @@ Type
     function GetString: string;
     function GetString: string;
     function GetBird: TBird;
     function GetBird: TBird;
     function Echo(const a: JSValue): JSValue;
     function Echo(const a: JSValue): JSValue;
+    function EchoCall(const a: JSValue; const CB: TBirdCallback): JSValue;
     function CreateBird(const aName: string): TBird;
     function CreateBird(const aName: string): TBird;
     procedure IncSize;
     procedure IncSize;
   end;
   end;
@@ -94,6 +96,11 @@ begin
   Result:=a;
   Result:=a;
 end;
 end;
 
 
+function TBird.EchoCall(const a: JSValue; const CB: TBirdCallback): JSValue;
+begin
+  Result:=CB(a);
+end;
+
 function TBird.GetInteger: integer;
 function TBird.GetInteger: integer;
 begin
 begin
   writeln('TBird.GetInteger [',Name,'] ',ArgsToStr(JSArguments));
   writeln('TBird.GetInteger [',Name,'] ',ArgsToStr(JSArguments));

+ 101 - 5
demo/wasienv/dom/WasiDomTest1.lpr

@@ -18,6 +18,10 @@ type
 
 
   TJSBird = class;
   TJSBird = class;
 
 
+  TBirdCallBoolean = function(const v: Boolean): Boolean of object;
+  TBirdCallInteger = function(const v: integer): integer of object;
+  TBirdCallVariant = function(const v: variant): variant of object;
+
   { IJSBird }
   { IJSBird }
 
 
   IJSBird = interface(IJSObject)
   IJSBird = interface(IJSObject)
@@ -32,6 +36,8 @@ type
     function GetUtf8String: String;
     function GetUtf8String: String;
     function GetBird: IJSBird;
     function GetBird: IJSBird;
     function Echo(const v: Variant): Variant;
     function Echo(const v: Variant): Variant;
+    function EchoBoolean(const v: Boolean; const Call: TBirdCallBoolean): Boolean;
+    function EchoInteger(const v: integer; const Call: TBirdCallInteger): integer;
     // properties
     // properties
     function GetCaption: UnicodeString;
     function GetCaption: UnicodeString;
     function GetEnabled: boolean;
     function GetEnabled: boolean;
@@ -69,6 +75,8 @@ type
     function GetUtf8String: String;
     function GetUtf8String: String;
     function GetBird: IJSBird;
     function GetBird: IJSBird;
     function Echo(const v: Variant): Variant;
     function Echo(const v: Variant): Variant;
+    function EchoBoolean(const v: Boolean; const Call: TBirdCallBoolean): Boolean;
+    function EchoInteger(const v: integer; const Call: TBirdCallInteger): integer;
     // properties
     // properties
     function GetCaption: UnicodeString;
     function GetCaption: UnicodeString;
     function GetEnabled: boolean;
     function GetEnabled: boolean;
@@ -95,6 +103,8 @@ type
   TWasmApp = class
   TWasmApp = class
   private
   private
     function OnPlaygroundClick(Event: IJSEvent): boolean;
     function OnPlaygroundClick(Event: IJSEvent): boolean;
+    function OnBirdCallBoolean(const v: boolean): boolean;
+    function OnBirdCallInteger(const v: integer): integer;
   public
   public
     Prefix: string;
     Prefix: string;
     Bird: IJSBird;
     Bird: IJSBird;
@@ -128,11 +138,12 @@ type
     procedure TestFuncResultObject;
     procedure TestFuncResultObject;
     procedure TestFuncResultVariant;
     procedure TestFuncResultVariant;
     procedure TestFuncResultVariantNumber;
     procedure TestFuncResultVariantNumber;
-    procedure TestFuncResultVariantStrings;
+    procedure TestFuncResultVariantString;
     procedure TestFuncResultVariantObject;
     procedure TestFuncResultVariantObject;
 
 
-    // function args
-    // todo procedure TestFuncArgMethod;
+    // callbacks
+    procedure TestFuncArgMethod_Boolean;
+    procedure TestFuncArgMethod_Integer;
 
 
     // dictionaries
     // dictionaries
 
 
@@ -142,6 +153,21 @@ type
     // todo: TestFuncResultUnicodeStringArray
     // todo: TestFuncResultUnicodeStringArray
   end;
   end;
 
 
+function JOBCallTBirdCallBoolean(const aMethod: TMethod; var H: TJOBCallbackHelper): PByte;
+var
+  v: Boolean;
+begin
+  v:=H.GetBoolean;
+  Result:=H.AllocBool(TBirdCallBoolean(aMethod)(v));
+end;
+
+function JOBCallTBirdCallInteger(const aMethod: TMethod; var H: TJOBCallbackHelper): PByte;
+var
+  v: LongInt;
+begin
+  v:=H.GetLongInt;
+  Result:=H.AllocLongint(TBirdCallInteger(aMethod)(v));
+end;
 
 
 { TApplication }
 { TApplication }
 
 
@@ -157,6 +183,16 @@ begin
   Result:=true;
   Result:=true;
 end;
 end;
 
 
+function TWasmApp.OnBirdCallBoolean(const v: boolean): boolean;
+begin
+  Result:=v;
+end;
+
+function TWasmApp.OnBirdCallInteger(const v: integer): integer;
+begin
+  Result:=v;
+end;
+
 procedure TWasmApp.Run;
 procedure TWasmApp.Run;
 var
 var
   JSElem: IJSElement;
   JSElem: IJSElement;
@@ -179,9 +215,12 @@ begin
   TestFuncResultObject;
   TestFuncResultObject;
   TestFuncResultVariant;
   TestFuncResultVariant;
   TestFuncResultVariantNumber;
   TestFuncResultVariantNumber;
-  TestFuncResultVariantStrings;
+  TestFuncResultVariantString;
   TestFuncResultVariantObject;
   TestFuncResultVariantObject;
 
 
+  TestFuncArgMethod_Boolean;
+  TestFuncArgMethod_Integer;
+
   exit;
   exit;
 
 
   JSElem:=JSDocument.getElementById('playground');
   JSElem:=JSDocument.getElementById('playground');
@@ -473,7 +512,7 @@ begin
 
 
 end;
 end;
 
 
-procedure TWasmApp.TestFuncResultVariantStrings;
+procedure TWasmApp.TestFuncResultVariantString;
 var
 var
   Value: Variant;
   Value: Variant;
   us: UnicodeString;
   us: UnicodeString;
@@ -592,6 +631,37 @@ begin
   AssertEqual('Bird.Echo(Lisa)',Lisa,Bart);
   AssertEqual('Bird.Echo(Lisa)',Lisa,Bart);
 end;
 end;
 
 
+procedure TWasmApp.TestFuncArgMethod_Boolean;
+var
+  v: Boolean;
+begin
+  Prefix:='TWasmApp.TestFuncArgMethod_Boolean';
+  Bird.Name:='TestFuncArgMethod_Boolean';
+
+  v:=Bird.EchoBoolean(true,@OnBirdCallBoolean);
+  AssertEqual('Bird.EchoBoolean(true,...)',true,v);
+
+  v:=Bird.EchoBoolean(false,@OnBirdCallBoolean);
+  AssertEqual('Bird.EchoBoolean(false,...)',false,v);
+end;
+
+procedure TWasmApp.TestFuncArgMethod_Integer;
+var
+  v: Integer;
+begin
+  Prefix:='TWasmApp.TestFuncArgMethod_Integer';
+  Bird.Name:='TestFuncArgMethod_Integer';
+
+  v:=Bird.EchoInteger(13,@OnBirdCallInteger);
+  AssertEqual('Bird.EchoInteger(13,...)',13,v);
+
+  v:=Bird.EchoInteger(low(longint),@OnBirdCallInteger);
+  AssertEqual('Bird.EchoInteger(low(longint),...)',low(longint),v);
+
+  v:=Bird.EchoInteger(high(longint),@OnBirdCallInteger);
+  AssertEqual('Bird.EchoInteger(high(longint),...)',high(longint),v);
+end;
+
 procedure TWasmApp.Fail(const Msg: string);
 procedure TWasmApp.Fail(const Msg: string);
 begin
 begin
   writeln('TWasmApp.Fail ',Prefix+': '+Msg);
   writeln('TWasmApp.Fail ',Prefix+': '+Msg);
@@ -708,6 +778,32 @@ begin
   Result:=InvokeJSVariantResult('Echo',[v]);
   Result:=InvokeJSVariantResult('Echo',[v]);
 end;
 end;
 
 
+function TJSBird.EchoBoolean(const v: Boolean; const Call: TBirdCallBoolean
+  ): Boolean;
+var
+  m: TJOB_Method;
+begin
+  m:=TJOB_Method.Create(TMethod(Call),@JOBCallTBirdCallBoolean);
+  try
+    Result:=InvokeJSBooleanResult('EchoCall',[v,m]);
+  finally
+    m.Free;
+  end;
+end;
+
+function TJSBird.EchoInteger(const v: integer; const Call: TBirdCallInteger
+  ): integer;
+var
+  m: TJOB_Method;
+begin
+  m:=TJOB_Method.Create(TMethod(Call),@JOBCallTBirdCallInteger);
+  try
+    Result:=InvokeJSLongIntResult('EchoCall',[v,m]);
+  finally
+    m.Free;
+  end;
+end;
+
 function TJSBird.GetCaption: UnicodeString;
 function TJSBird.GetCaption: UnicodeString;
 begin
 begin
   Result:=ReadJSPropertyUnicodeString('Caption');
   Result:=ReadJSPropertyUnicodeString('Caption');