When exposing a host class and calling a procedure from it I get this exception:

First chance exception at $7513C41F. Exception class ECompileError with message 'There is no accessible member with name "GetUnitCount"'. Process Project23.exe (12832)

How to fix this? I am using the latest version from 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.
有帮助吗?

解决方案

Your member function does indeed exist, but it's not getting picked up by ExposeClassToUnit, because it doesn't read functions.

From the comment just above the implementation of 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. 

So it only picks up properties, not functions, and only ones whose access level is published.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top