DWScript: Migrating from IScriptObj to IInfo or TProgramInfo

If an IScriptObj link is IScriptObj , how do I get to the corresponding IInfo or TProgramInfo ?


I have a script object that wraps a Delphi object.

To control the lifetime of a script object, the Delphi object stores a reference to the script object. The script object is declared by the TdwsUnit component. It is pretty standard and looks something like this:

Delphi

 type TDelphiObject = class private FScriptObject: IScriptObj; public procedure DoSomething; property ScriptObject: IScriptObj read FScriptObject write FScriptObject; end; 

Script

 type TScriptObject = class protected procedure DoSomething; virtual; public constructor Create; end; 

The implementation of the Delphi object and the configuration of the Delphi / script links occurs in the implementation of the Delphi script object constructor. Also pretty standard:

Delphi

 // Implements TScriptObject.Create procedure TMyForm.dwsUnitClassesTScriptObjectConstructorsCreateEval(Info: TProgramInfo; var ExtObject: TObject); var DelphiObject: TDelphiObject; DelphiObjectInfo: IInfo; begin // Create the Delphi-side object DelphiObject := TDelphiObject.Create; // Get the script object "self" value DelphiObjectInfo := Info.Vars['self']; // Store the ScriptObject reference DelphiObject.ScriptObject := DelphiObjectInfo.ScriptObj; // Return the instance reference to the script ExtObject := DelphiObject; end; 

Ideally, I would keep the IInfo link, and IScriptObj , since IInfo does everything I need later, but from experience it seems that the IInfo object IInfo valid only for the duration of the method call.

In any case, the problem occurs later when TDelphiObject.DoSomething is called on the Delphi side. TDelphiObject.DoSomething intended to call the appropriate virtual method for the script object:

Delphi

 procedure TDelphiObject.DoSomething; var Info: IInfo; DoSomethingInfo: IInfo; begin // I have a IScriptObj but I need a IInfo... Info := { what happens here? }; // Call the virtual DoSomething method DoSomethingInfo := Info.Method['DoSomething']; DoSomethingInfo.Call([]); end; 

I tried many different methods to get a useful IInfo or TProgramInfo from a saved IScriptObj , but everything failed. So what is the right way to do this?

+7
delphi dwscript
source share
1 answer

The problem turned out to be that I suggested that I needed an IInfo interface to encapsulate an instance of an object, but apparently DWScript did not work. I need to create a temporary link / pointer to an instance, and then create IInfo instead.

Here's how to do it:

 procedure TDelphiObject.DoSomething; var ProgramExecution: TdwsProgramExecution; ProgramInfo: TProgramInfo; Data: TData; DataContext: IDataContext; Info: IInfo; DoSomethingInfo: IInfo; begin (* ** Create an IInfo that lets me access the object represented by the IScriptObj pointer. *) // FProgramExecution is the IdwsProgramExecution reference that is returned by // TdwsMainProgram.CreateNewExecution and BeginNewExecution. I have stored this // elsewhere. ProgramExecution := TdwsProgramExecution(FProgramExecution); ProgramInfo := ProgramExecution.AcquireProgramInfo(nil); try // Create a temporary reference object SetLength(Data, 1); Data[0] := FScriptObject; ProgramInfo.Execution.DataContext_Create(Data, 0, DataContext); // Wrap the reference Info := TInfoClassObj.Create(ProgramInfo, FScriptObject.ClassSym, DataContext); // Call the virtual DoSomething method DoSomethingInfo := Info.Method['DoSomething']; DoSomethingInfo.Call([]); finally ProgramExecution.ReleaseProgramInfo(ProgramInfo); end; end; 

That means object oriented callbacks from Delphi to script. Without this, you can call the global script functions from Delphi.

FWIW, the following two lines from the above:

 ProgramInfo.Execution.DataContext_Create(Data, 0, DataContext); Info := TInfoClassObj.Create(ProgramInfo, FScriptObject.ClassSym, DataContext); 

can be replaced by calling CreateInfoOnSymbol (declared in dwsInfo):

 CreateInfoOnSymbol(Info, ProgramInfo, FScriptObject.ClassSym, Data, 0); 
+1
source share

All Articles