DWScript:从IScriptObj到IInfo或TProgramInfo

时间:2014-12-16 14:59:30

标签: delphi dwscript

给定IScriptObj引用如何获得相应的IInfoTProgramInfo


我有一个包装Delphi对象的脚本对象。

为了管理脚本对象的生命周期,Delphi对象存储对脚本对象的引用。 Script对象使用TdwsUnit组件声明。这是非常标准的,就像这样:

的Delphi

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

脚本

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

Delphi对象的实例化和Delphi /脚本链接的设置发生在脚本对象构造函数的Delphi实现中。也非常标准:

的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;

理想情况下,我会保存IInfo引用而不是IScriptObj,因为IInfo执行我以后需要的所有内容,但从经验来看,IInfo对象似乎只是在方法调用的持续时间内有效。

无论如何,问题发生在Delphi方面调用TDelphiObject.DoSomething之后。 TDelphiObject.DoSomething用于在脚本对象上调用相应的虚方法:

的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;

我尝试了很多不同的技术来从存储的IInfo中获取可用的TProgramInfoIScriptObj但是每件事都失败了。那么这样做的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

问题结果是我认为我需要一个IInfo接口来封装对象实例,但显然DWScript不能这样工作。我需要的是创建一个指向实例的临时引用/指针,然后在其上创建一个IInfo

以下是如何完成的:

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;

这样做可以实现从Delphi到脚本的面向对象的回调。没有它,只能从Delphi调用全局脚本函数。

FWIW,以上两行来自上述:

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

可以替换为对CreateInfoOnSymbol的调用(在dwsInfo中声明):

CreateInfoOnSymbol(Info, ProgramInfo, FScriptObject.ClassSym, Data, 0);