* 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 FindObject(ObjId: TJOBObjectID): TJSObject; virtual;
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 RegisterGlobalObject(Obj: JSValue; const aName: string): TJOBObjectID; virtual;
Procedure RegisterObjectFactory(const aName : string; aFunc : TObjectFactory); overload;
@ -125,6 +126,19 @@ Type
property OnCallBackPasError : TCallbackErrorPasEventHandler read FOnCallBackPasError Write FOnCallBackPasError;
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
@ -317,6 +331,18 @@ begin
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;
var
NewId: JSValue;
@ -1247,4 +1273,30 @@ begin
Console.debug(FGlobalObjects);
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.

View File

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