wasi: job: added InvokeJSTypeOf

This commit is contained in:
mattias 2022-06-01 16:10:00 +02:00
parent 471d1ee1e4
commit 6ff83dd8b5
4 changed files with 101 additions and 67 deletions

View File

@ -39,8 +39,14 @@ type
{ TApplication } { TApplication }
function TWasmApp.OnPlaygroundClick(Event: IJSEventListenerEvent): boolean; function TWasmApp.OnPlaygroundClick(Event: IJSEventListenerEvent): boolean;
var
w: TJOBResult;
begin begin
writeln('TWasmApp.OnPlaygroundClick '); writeln('TWasmApp.OnPlaygroundClick ');
w:=Event.InvokeJSTypeOf('targetElement',[]);
writeln('TWasmApp.OnPlaygroundClick typeof=',w);
Result:=true; Result:=true;
end; end;

View File

@ -233,11 +233,16 @@ begin
JSResult:=NewObj(TJSFunction(fn),Args) JSResult:=NewObj(TJSFunction(fn),Args)
end; end;
end; end;
JOBInvokeGet: JOBInvokeGet,JOBInvokeGetTypeOf:
begin begin
if ArgsP>0 then if ArgsP>0 then
exit(JOBResult_WrongArgs); exit(JOBResult_WrongArgs);
JSResult:=Obj[PropName]; JSResult:=Obj[PropName];
if Invoke=JOBInvokeGetTypeOf then
begin
Result:=GetJOBResult(jsTypeOf(JSResult));
exit;
end;
end; end;
JOBInvokeSet: JOBInvokeSet:
begin begin

View File

@ -90,6 +90,7 @@ const
JOBInvokeCall = 0; // call function JOBInvokeCall = 0; // call function
JOBInvokeGet = 1; // read property JOBInvokeGet = 1; // read property
JOBInvokeGetTypeOf = 4; // read property and typeof
JOBInvokeSet = 2; // set property JOBInvokeSet = 2; // set property
JOBInvokeNew = 3; // new operator JOBInvokeNew = 3; // new operator

View File

@ -115,15 +115,14 @@ type
function AsString: string; override; function AsString: string; override;
end; end;
TJOBInvokeGetType = ( TJOBInvokeType = (
jigCall, // call function jiCall, // call function
jigGetter, // read property jiGet, // read property
jigNew // new operator jiGetTypeOf, // read property and do typeof
); jiSet, // write property
TJOBInvokeSetType = ( jiNew // new operator
jisCall, // call function
jisSetter // write property
); );
TJOBInvokeTypes = set of TJOBInvokeType;
TJSObject = class; TJSObject = class;
TJSObjectClass = class of TJSObject; TJSObjectClass = class of TJSObject;
@ -135,14 +134,15 @@ type
function GetJSObjectID: TJOBObjectID; function GetJSObjectID: TJOBObjectID;
function GetJSObjectCastSrc: IJSObject; function GetJSObjectCastSrc: IJSObject;
function GetPascalClassName: string; function GetPascalClassName: string;
procedure InvokeJSNoResult(const aName: string; Const Args: Array of const; Invoke: TJOBInvokeSetType = jisCall); virtual; procedure InvokeJSNoResult(const aName: string; Const Args: Array of const; Invoke: TJOBInvokeType = jiCall); 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: TJOBInvokeType = jiCall): 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: TJOBInvokeType = jiCall): 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: TJOBInvokeType = jiCall): UnicodeString; virtual;
function InvokeJSObjectResult(const aName: string; Const Args: Array of const; aResultClass: TJSObjectClass; Invoke: TJOBInvokeGetType = jigCall): TJSObject; virtual; function InvokeJSObjectResult(const aName: string; Const Args: Array of const; aResultClass: TJSObjectClass; Invoke: TJOBInvokeType = jiCall): TJSObject; virtual;
function InvokeJSValueResult(const aName: string; Const Args: Array of const; Invoke: TJOBInvokeGetType = jigCall): TJOB_JSValue; virtual; function InvokeJSValueResult(const aName: string; Const Args: Array of const; Invoke: TJOBInvokeType = jiCall): TJOB_JSValue; virtual;
function InvokeJSUtf8StringResult(const aName: string; Const args: Array of const; Invoke: TJOBInvokeGetType = jigCall): String; virtual; function InvokeJSUtf8StringResult(const aName: string; Const args: Array of const; Invoke: TJOBInvokeType = jiCall): String; virtual;
function InvokeJSLongIntResult(const aName: string; Const args: Array of const; Invoke: TJOBInvokeGetType = jigCall): LongInt; virtual; function InvokeJSLongIntResult(const aName: string; Const args: Array of const; Invoke: TJOBInvokeType = jiCall): LongInt; virtual;
function InvokeJSTypeOf(const aName: string; Const Args: Array of const): TJOBResult; virtual;
function ReadJSPropertyBoolean(const aName: string): boolean; virtual; function ReadJSPropertyBoolean(const aName: string): boolean; virtual;
function ReadJSPropertyDouble(const aName: string): double; virtual; function ReadJSPropertyDouble(const aName: string): double; virtual;
function ReadJSPropertyUnicodeString(const aName: string): UnicodeString; virtual; function ReadJSPropertyUnicodeString(const aName: string): UnicodeString; virtual;
@ -167,6 +167,13 @@ type
FCastSrc: IJSObject; FCastSrc: IJSObject;
protected protected
type type
TJOBInvokeNoResultFunc = function(
ObjID: TJOBObjectID;
NameP: PChar;
NameLen: longint;
Invoke: longint;
ArgP: PByte
): TJOBResult;
TJOBInvokeOneResultFunc = function( TJOBInvokeOneResultFunc = function(
ObjID: TJOBObjectID; ObjID: TJOBObjectID;
NameP: PChar; NameP: PChar;
@ -179,8 +186,10 @@ type
function GetJSObjectCastSrc: IJSObject; function GetJSObjectCastSrc: IJSObject;
function GetPascalClassName: string; function GetPascalClassName: string;
function FetchString(Len: NativeInt): UnicodeString; function FetchString(Len: NativeInt): UnicodeString;
function InvokeJSNoResultFunc(const aName: string; Const Args: Array of const;
const InvokeFunc: TJOBInvokeNoResultFunc; Invoke: TJOBInvokeType): TJOBResult;
function InvokeJSOneResult(const aName: string; Const Args: Array of const; function InvokeJSOneResult(const aName: string; Const Args: Array of const;
const InvokeFunc: TJOBInvokeOneResultFunc; ResultP: PByte; Invoke: TJOBInvokeGetType): TJOBResult; const InvokeFunc: TJOBInvokeOneResultFunc; ResultP: PByte; Invoke: TJOBInvokeType): TJOBResult;
procedure InvokeJS_Raise(const aName, Msg: string); virtual; procedure InvokeJS_Raise(const aName, Msg: string); virtual;
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;
@ -192,14 +201,15 @@ type
property ObjectID: TJOBObjectID read FObjectID; property ObjectID: TJOBObjectID read FObjectID;
property CastSrc: IJSObject read FCastSrc; // nil means it is the owner, otherwise it is a typecast property CastSrc: IJSObject read FCastSrc; // nil means it is the owner, otherwise it is a typecast
// call a function // call a function
procedure InvokeJSNoResult(const aName: string; Const Args: Array of const; Invoke: TJOBInvokeSetType = jisCall); virtual; procedure InvokeJSNoResult(const aName: string; Const Args: Array of const; Invoke: TJOBInvokeType = jiCall); 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: TJOBInvokeType = jiCall): 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: TJOBInvokeType = jiCall): 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: TJOBInvokeType = jiCall): UnicodeString; virtual;
function InvokeJSObjectResult(const aName: string; Const Args: Array of const; aResultClass: TJSObjectClass; Invoke: TJOBInvokeGetType = jigCall): TJSObject; virtual; function InvokeJSObjectResult(const aName: string; Const Args: Array of const; aResultClass: TJSObjectClass; Invoke: TJOBInvokeType = jiCall): TJSObject; virtual;
function InvokeJSValueResult(const aName: string; Const Args: Array of const; Invoke: TJOBInvokeGetType = jigCall): TJOB_JSValue; virtual; function InvokeJSValueResult(const aName: string; Const Args: Array of const; Invoke: TJOBInvokeType = jiCall): TJOB_JSValue; virtual;
function InvokeJSUtf8StringResult(const aName: string; Const args: Array of const; Invoke: TJOBInvokeGetType = jigCall): String; virtual; function InvokeJSUtf8StringResult(const aName: string; Const args: Array of const; Invoke: TJOBInvokeType = jiCall): String; virtual;
function InvokeJSLongIntResult(const aName: string; Const args: Array of const; Invoke: TJOBInvokeGetType = jigCall): LongInt; virtual; function InvokeJSLongIntResult(const aName: string; Const args: Array of const; Invoke: TJOBInvokeType = jiCall): LongInt; virtual;
function InvokeJSTypeOf(const aName: string; Const Args: Array of const): TJOBResult; virtual;
// read a property // read a property
function ReadJSPropertyBoolean(const aName: string): boolean; virtual; function ReadJSPropertyBoolean(const aName: string): boolean; virtual;
function ReadJSPropertyDouble(const aName: string): double; virtual; function ReadJSPropertyDouble(const aName: string): double; virtual;
@ -319,15 +329,13 @@ function JOBCallback(const Func: TJOBCallback; Data, Code: Pointer; Args: PByte)
implementation implementation
const const
InvokeGetToInt: array[TJOBInvokeGetType] of integer = ( InvokeGetToInt: array[TJOBInvokeType] of integer = (
JOBInvokeCall, JOBInvokeCall,
JOBInvokeGet, JOBInvokeGet,
JOBInvokeGetTypeOf,
JOBInvokeSet,
JOBInvokeNew JOBInvokeNew
); );
InvokeSetToInt: array[TJOBInvokeSetType] of integer = (
JOBInvokeCall,
JOBInvokeSet
);
{$IFDEF VerboseJOB} {$IFDEF VerboseJOB}
function GetVarRecName(vt: word): string; function GetVarRecName(vt: word): string;
@ -771,9 +779,28 @@ begin
__job_getstringresult(PByte(Result)); __job_getstringresult(PByte(Result));
end; end;
function TJSObject.InvokeJSNoResultFunc(const aName: string;
const Args: array of const; const InvokeFunc: TJOBInvokeNoResultFunc;
Invoke: TJOBInvokeType): TJOBResult;
var
InvokeArgs: PByte;
begin
if length(Args)=0 then
Result:=InvokeFunc(ObjectID,PChar(aName),length(aName),InvokeGetToInt[Invoke],nil)
else begin
InvokeArgs:=CreateInvokeJSArgs(Args);
try
Result:=InvokeFunc(ObjectID,PChar(aName),length(aName),InvokeGetToInt[Invoke],InvokeArgs);
finally
if InvokeArgs<>nil then
FreeMem(InvokeArgs);
end;
end;
end;
function TJSObject.InvokeJSOneResult(const aName: string; function TJSObject.InvokeJSOneResult(const aName: string;
const Args: array of const; const InvokeFunc: TJOBInvokeOneResultFunc; const Args: array of const; const InvokeFunc: TJOBInvokeOneResultFunc;
ResultP: PByte; Invoke: TJOBInvokeGetType): TJOBResult; ResultP: PByte; Invoke: TJOBInvokeType): TJOBResult;
var var
InvokeArgs: PByte; InvokeArgs: PByte;
begin begin
@ -1229,28 +1256,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; Invoke: TJOBInvokeType);
var var
aError: TJOBResult; aError: TJOBResult;
InvokeArgs: PByte;
begin begin
if length(Args)=0 then aError:=InvokeJSNoResultFunc(aName,Args,@__job_invoke_noresult,Invoke);
aError:=__job_invoke_noresult(ObjectID,PChar(aName),length(aName),InvokeSetToInt[Invoke],nil)
else begin
InvokeArgs:=CreateInvokeJSArgs(Args);
try
aError:=__job_invoke_noresult(ObjectID,PChar(aName),length(aName),InvokeSetToInt[Invoke],InvokeArgs);
finally
if InvokeArgs<>nil then
FreeMem(InvokeArgs);
end;
end;
if aError<>JOBResult_Success then if aError<>JOBResult_Success then
InvokeJS_RaiseResultMismatch(aName,JOBResult_Success,aError); InvokeJS_RaiseResultMismatch(aName,JOBResult_Success,aError);
end; end;
function TJSObject.InvokeJSBooleanResult(const aName: string; function TJSObject.InvokeJSBooleanResult(const aName: string;
const Args: array of const; Invoke: TJOBInvokeGetType): Boolean; const Args: array of const; Invoke: TJOBInvokeType): Boolean;
var var
aError: TJOBResult; aError: TJOBResult;
b: bytebool; b: bytebool;
@ -1266,7 +1282,7 @@ begin
end; end;
function TJSObject.InvokeJSDoubleResult(const aName: string; function TJSObject.InvokeJSDoubleResult(const aName: string;
const Args: array of const; Invoke: TJOBInvokeGetType): Double; const Args: array of const; Invoke: TJOBInvokeType): Double;
var var
aError: TJOBResult; aError: TJOBResult;
begin begin
@ -1280,7 +1296,7 @@ begin
end; end;
function TJSObject.InvokeJSUnicodeStringResult(const aName: string; function TJSObject.InvokeJSUnicodeStringResult(const aName: string;
const Args: array of const; Invoke: TJOBInvokeGetType): UnicodeString; const Args: array of const; Invoke: TJOBInvokeType): UnicodeString;
var var
ResultLen: NativeInt; ResultLen: NativeInt;
aError: TJOBResult; aError: TJOBResult;
@ -1299,7 +1315,7 @@ end;
function TJSObject.InvokeJSObjectResult(const aName: string; function TJSObject.InvokeJSObjectResult(const aName: string;
const Args: array of const; aResultClass: TJSObjectClass; const Args: array of const; aResultClass: TJSObjectClass;
Invoke: TJOBInvokeGetType): TJSObject; Invoke: TJOBInvokeType): TJSObject;
var var
aError: TJOBResult; aError: TJOBResult;
NewObjId: TJOBObjectID; NewObjId: TJOBObjectID;
@ -1316,7 +1332,7 @@ begin
end; end;
function TJSObject.InvokeJSValueResult(const aName: string; function TJSObject.InvokeJSValueResult(const aName: string;
const Args: array of const; Invoke: TJOBInvokeGetType): TJOB_JSValue; const Args: array of const; Invoke: TJOBInvokeType): TJOB_JSValue;
var var
Buf: array[0..7] of byte; Buf: array[0..7] of byte;
p: PByte; p: PByte;
@ -1350,13 +1366,13 @@ begin
end; end;
function TJSObject.InvokeJSUtf8StringResult(const aName: string; function TJSObject.InvokeJSUtf8StringResult(const aName: string;
const args: array of const; Invoke: TJOBInvokeGetType): String; const args: array of const; Invoke: TJOBInvokeType): String;
begin begin
Result:=String(InvokeJSUnicodeStringResult(aName,Args,Invoke)); Result:=String(InvokeJSUnicodeStringResult(aName,Args,Invoke));
end; end;
function TJSObject.InvokeJSLongIntResult(const aName: string; function TJSObject.InvokeJSLongIntResult(const aName: string;
const args: array of const; Invoke: TJOBInvokeGetType): LongInt; const args: array of const; Invoke: TJOBInvokeType): LongInt;
var var
d: Double; d: Double;
begin begin
@ -1367,86 +1383,92 @@ begin
Result:=Trunc(d); Result:=Trunc(d);
end; end;
function TJSObject.InvokeJSTypeOf(const aName: string;
const Args: array of const): TJOBResult;
begin
Result:=InvokeJSNoResultFunc(aName,Args,@__job_invoke_noresult,jiGetTypeOf);
end;
function TJSObject.ReadJSPropertyBoolean(const aName: string): boolean; function TJSObject.ReadJSPropertyBoolean(const aName: string): boolean;
begin begin
Result:=InvokeJSBooleanResult(aName,[],jigGetter); Result:=InvokeJSBooleanResult(aName,[],jiGet);
end; end;
function TJSObject.ReadJSPropertyDouble(const aName: string): double; function TJSObject.ReadJSPropertyDouble(const aName: string): double;
begin begin
Result:=InvokeJSDoubleResult(aName,[],jigGetter); Result:=InvokeJSDoubleResult(aName,[],jiGet);
end; end;
function TJSObject.ReadJSPropertyUnicodeString(const aName: string function TJSObject.ReadJSPropertyUnicodeString(const aName: string
): UnicodeString; ): UnicodeString;
begin begin
Result:=InvokeJSUnicodeStringResult(aName,[],jigGetter); Result:=InvokeJSUnicodeStringResult(aName,[],jiGet);
end; end;
function TJSObject.ReadJSPropertyObject(const aName: string; function TJSObject.ReadJSPropertyObject(const aName: string;
aResultClass: TJSObjectClass): TJSObject; aResultClass: TJSObjectClass): TJSObject;
begin begin
Result:=InvokeJSObjectResult(aName,[],aResultClass,jigGetter); Result:=InvokeJSObjectResult(aName,[],aResultClass,jiGet);
end; end;
function TJSObject.ReadJSPropertyUtf8String(const aName: string): string; function TJSObject.ReadJSPropertyUtf8String(const aName: string): string;
begin begin
Result:=InvokeJSUtf8StringResult(aName,[],jigGetter); Result:=InvokeJSUtf8StringResult(aName,[],jiGet);
end; end;
function TJSObject.ReadJSPropertyLongInt(const aName: string): LongInt; function TJSObject.ReadJSPropertyLongInt(const aName: string): LongInt;
begin begin
Result:=InvokeJSLongIntResult(aName,[],jigGetter); Result:=InvokeJSLongIntResult(aName,[],jiGet);
end; end;
function TJSObject.ReadJSPropertyValue(const aName: string): TJOB_JSValue; function TJSObject.ReadJSPropertyValue(const aName: string): TJOB_JSValue;
begin begin
Result:=InvokeJSValueResult(aName,[],jigGetter); Result:=InvokeJSValueResult(aName,[],jiGet);
end; end;
procedure TJSObject.WriteJSPropertyBoolean(const aName: string; Value: Boolean); procedure TJSObject.WriteJSPropertyBoolean(const aName: string; Value: Boolean);
begin begin
InvokeJSNoResult(aName,[Value],jisSetter); InvokeJSNoResult(aName,[Value],jiSet);
end; end;
procedure TJSObject.WriteJSPropertyDouble(const aName: string; Value: Double); procedure TJSObject.WriteJSPropertyDouble(const aName: string; Value: Double);
begin begin
InvokeJSNoResult(aName,[Value],jisSetter); InvokeJSNoResult(aName,[Value],jiSet);
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); InvokeJSNoResult(aName,[Value],jiSet);
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); InvokeJSNoResult(aName,[Value],jiSet);
end; end;
procedure TJSObject.WriteJSPropertyObject(const aName: string; Value: TJSObject procedure TJSObject.WriteJSPropertyObject(const aName: string; Value: TJSObject
); );
begin begin
InvokeJSNoResult(aName,[Value],jisSetter); InvokeJSNoResult(aName,[Value],jiSet);
end; end;
procedure TJSObject.WriteJSPropertyLongInt(const aName: string; Value: LongInt); procedure TJSObject.WriteJSPropertyLongInt(const aName: string; Value: LongInt);
begin begin
InvokeJSNoResult(aName,[Value],jisSetter); InvokeJSNoResult(aName,[Value],jiSet);
end; end;
procedure TJSObject.WriteJSPropertyValue(const aName: string; procedure TJSObject.WriteJSPropertyValue(const aName: string;
Value: TJOB_JSValue); Value: TJOB_JSValue);
begin begin
InvokeJSNoResult(aName,[Value],jisSetter); InvokeJSNoResult(aName,[Value],jiSet);
end; end;
function TJSObject.NewJSObject(const Args: array of const; function TJSObject.NewJSObject(const Args: array of const;
aResultClass: TJSObjectClass): TJSObject; aResultClass: TJSObjectClass): TJSObject;
begin begin
Result:=InvokeJSObjectResult('',Args,aResultClass,jigNew); Result:=InvokeJSObjectResult('',Args,aResultClass,jiNew);
end; end;
initialization initialization