From 2c0404c1e9b8c641f95bf50f91945781a1554577 Mon Sep 17 00:00:00 2001 From: Michael Van Canneyt Date: Wed, 12 Feb 2025 09:19:46 +0100 Subject: [PATCH] * Add share --- packages/job/src/job_browser.pp | 52 +++++++++++++++++++++++++++++++++ packages/job/src/job_shared.pp | 7 +++-- 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/packages/job/src/job_browser.pp b/packages/job/src/job_browser.pp index 267a3c5..d31f128 100644 --- a/packages/job/src/job_browser.pp +++ b/packages/job/src/job_browser.pp @@ -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. diff --git a/packages/job/src/job_shared.pp b/packages/job/src/job_shared.pp index a0c7ab6..3f6aa17 100644 --- a/packages/job/src/job_shared.pp +++ b/packages/job/src/job_shared.pp @@ -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;