当我调用DWScript的ExposeClassToUnit时,为什么方法调用不起作用?

时间:2013-10-04 21:24:37

标签: delphi dwscript

当暴露主机类并从中调用过程时,我得到了这个异常:

  

第一次机会异常,价格为7513C41F。异常类ECompileError,消息'没有可访问的成员,名称为“GetUnitCount”'。处理Project23.exe(12832)

如何解决这个问题?我使用的是SVN的最新版本。

program DWScript_Test;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  dwsComp,
  dwsCompiler,
  dwsExprs,
  dwsSymbols;


type
  TMyClass = class
    function GetUnitCount: Integer; // It does indeed exist..
  end;

type
  TTest = class
    DWS: TDelphiWebScript;
    dwsUnit: TdwsUnit;
    fStats: TMyClass;
    prog: IdwsProgram;
    exec: IdwsProgramExecution;
    procedure ExposeInstancesAfterInitTable(Sender: TObject);
    procedure Execute(aText: string);
  end;

function TMyClass.GetUnitCount: Integer;
begin
  Result := 4;
end;

procedure TTest.ExposeInstancesAfterInitTable(Sender: TObject);
begin
 dwsUnit.ExposeInstanceToUnit('fStats', 'TMyClass', fStats);
end;

procedure TTest.Execute(aText: string);
begin
  DWS := TDelphiWebScript.Create(nil);

  dwsUnit := TdwsUnit.Create(nil);
  dwsUnit.UnitName := 'Test';
  try
    fStats := TMyClass.Create;
    dwsUnit.Script := DWS;
    dwsUnit.ExposeClassToUnit(TMyClass, TObject);
    dwsUnit.OnAfterInitUnitTable := ExposeInstancesAfterInitTable;
    prog := DWS.Compile(aText);

    if prog.Msgs.Count = 0 then
    begin
      exec := prog.Execute;
      Writeln(exec.Result.ToString);
    end
    else
      Writeln(prog.Msgs.AsInfo);
  finally
    dwsUnit.Free;
    DWS.Free;
  end;
  Readln;
end;

begin
  TTest.Create.Execute('PrintLn(IntToStr(fStats.GetUnitCount));');
end.

1 个答案:

答案 0 :(得分:0)

您的成员函数确实存在,但它不会被ExposeClassToUnit选中,因为它不会读取函数。

来自TdwsUnit.ExposeClassToUnit

的实施正上方的评论
AClass is the class to expose to the unit. All published properties of standard
simple datatypes that are supported in DWS will be exposed that were introduced
between AAncestor and AClass. 

所以它只选择属性,而不是功能,只有那些访问级别为已发布的人。