* Add share

This commit is contained in:
Michael Van Canneyt 2025-02-12 09:19:46 +01:00
parent ad2ee34f56
commit 2c0404c1e9
2 changed files with 57 additions and 2 deletions

View File

@ -113,6 +113,7 @@ Type
Function ImportName: String; override; Function ImportName: String; override;
function FindObject(ObjId: TJOBObjectID): TJSObject; virtual; function FindObject(ObjId: TJOBObjectID): TJSObject; virtual;
function FindGlobalObject(const aName: string): TJOBObjectID; virtual; // 0=not found function FindGlobalObject(const aName: string): TJOBObjectID; virtual; // 0=not found
function RegisterLocalObjectAt(Obj: TJSObject; aObjectID : TJOBObjectID) : Boolean; virtual;
function RegisterLocalObject(Obj: TJSObject): TJOBObjectID; virtual; function RegisterLocalObject(Obj: TJSObject): TJOBObjectID; virtual;
Function RegisterGlobalObject(Obj: JSValue; const aName: string): TJOBObjectID; virtual; Function RegisterGlobalObject(Obj: JSValue; const aName: string): TJOBObjectID; virtual;
Procedure RegisterObjectFactory(const aName : string; aFunc : TObjectFactory); overload; Procedure RegisterObjectFactory(const aName : string; aFunc : TObjectFactory); overload;
@ -125,6 +126,19 @@ Type
property OnCallBackPasError : TCallbackErrorPasEventHandler read FOnCallBackPasError Write FOnCallBackPasError; property OnCallBackPasError : TCallbackErrorPasEventHandler read FOnCallBackPasError Write FOnCallBackPasError;
end; end;
{ TJSThreadAwareObjectBridge }
TShareObjectEvent = procedure (aObjectID: TJOBObjectID; aThreadID: Longint; aObj : TJSObject) of object;
TJSThreadAwareObjectBridge = class(TJSObjectBridge)
private
FOnShareObject: TShareObjectEvent;
Protected
function ShareObject(aObjectID: TJOBObjectID; aThreadID: Longint): TJOBResult; virtual;
public
procedure FillImportObject(aObject: TJSObject); override;
Property OnShareObject : TShareObjectEvent Read FOnShareObject Write FOnShareObject;
end;
Implementation Implementation
@ -317,6 +331,18 @@ begin
end; end;
end; end;
function TJSObjectBridge.RegisterLocalObjectAt(Obj: TJSObject; aObjectID: TJOBObjectID): Boolean;
var
lExisting : TJSObject;
begin
lExisting:=TJSObject(FLocalObjects[aObjectID]);
Result:=Not assigned(lExisting);
if Result then
FLocalObjects[aObjectID]:=Obj
else
Result:=(Obj=lExisting); // It's OK if it is the same object
end;
function TJSObjectBridge.RegisterLocalObject(Obj: TJSObject): TJOBObjectID; function TJSObjectBridge.RegisterLocalObject(Obj: TJSObject): TJOBObjectID;
var var
NewId: JSValue; NewId: JSValue;
@ -1247,4 +1273,30 @@ begin
Console.debug(FGlobalObjects); Console.debug(FGlobalObjects);
end; end;
{ TJSThreadAwareObjectBridge }
function TJSThreadAwareObjectBridge.ShareObject(aObjectID : TJOBObjectID; aThreadID : Longint) : TJOBResult;
var
Obj : TJSObject;
begin
Result:=JOBResult_None;
Obj:=FindObject(aObjectID);
if not Assigned(Obj) then
exit(JOBResult_UnknownObjId);
if not Assigned(OnShareObject) then
exit(JOBResult_UnImplemented);
OnShareObject(aObjectID,aThreadId,Obj);
Result:=JOBResult_Success;
end;
procedure TJSThreadAwareObjectBridge.FillImportObject(aObject: TJSObject);
begin
inherited FillImportObject(aObject);
aObject[JOBFn_ShareObject]:=@ShareObject;
end;
end. end.

View File

@ -32,8 +32,9 @@ const
JOBResult_BigInt = 12; JOBResult_BigInt = 12;
JOBResult_Symbol = 13; JOBResult_Symbol = 13;
JOBResult_ArrayOfString = 14; JOBResult_ArrayOfString = 14;
JOBResult_UnImplemented = 15;
JOBResultLast = JOBResult_ArrayOfString; JOBResultLast = JOBResult_UnImplemented;
JOBResult_Names: array[0..JOBResultLast] of string = ( JOBResult_Names: array[0..JOBResultLast] of string = (
'None', 'None',
@ -50,7 +51,8 @@ const
'Object', 'Object',
'BigInt', 'BigInt',
'Symbol', 'Symbol',
'ArrayOfString' 'ArrayOfString',
'UnImplemented'
); );
JOBExportName = 'job'; JOBExportName = 'job';
@ -70,6 +72,7 @@ const
JOBFn_SetMemFromArray = 'set_mem_from_object'; JOBFn_SetMemFromArray = 'set_mem_from_object';
JOBFn_SetArrayFromMem = 'set_object_from_mem'; JOBFn_SetArrayFromMem = 'set_object_from_mem';
JOBFn_DebugObject = 'debug_object'; JOBFn_DebugObject = 'debug_object';
JOBFn_ShareObject = 'share_object';
JOBArgUndefined = 0; JOBArgUndefined = 0;
JOBArgLongint = 1; JOBArgLongint = 1;