remobjects / pascalscript

pascalscript
Other
447 stars 178 forks source link

Event assign to script object fail with error erCouldNotCallProc #212

Open ccy opened 4 years ago

ccy commented 4 years ago

Execute a simple script

program sample5;

{$APPTYPE CONSOLE}

uses
  Winapi.Windows,
  System.Math,
  uPSCompiler,
  uPSRuntime,
  uPSC_Classes,
  uPSC_std,
  uPSR_Classes,
  uPSR_std
  ;

function ScriptOnUses(Sender: TPSPascalCompiler; const Name: AnsiString): Boolean;
begin
  if Name = 'SYSTEM' then
  begin
    SIRegister_Std(Sender);
    SIRegister_Classes(Sender, False);
    Result := True;
  end else
    Result := False;
end;

procedure ExecuteScript(const Script: string);
var
  Compiler: TPSPascalCompiler;
  Exec: TPSExec;
  Data: AnsiString;
  CI: TPSRuntimeClassImporter;
begin
  Compiler := TPSPascalCompiler.Create; // create an instance of the compiler.
  Compiler.OnUses := ScriptOnUses; // assign the OnUses event.
  if not Compiler.Compile(Script) then  // Compile the Pascal script into bytecode.
  begin
    Compiler.Free;
    Exit;
  end;

  Compiler.GetOutput(Data); // Save the output of the compiler in the string Data.
  Compiler.Free; // After compiling the script, there is no need for the compiler anymore.

  CI := TPSRuntimeClassImporter.Create;
  { Create an instance of the runtime class importer.}

  RIRegister_Std(CI);  // uPSR_std.pas unit.
  RIRegister_Classes(CI, True);

  Exec := TPSExec.Create;  // Create an instance of the executer.

  RegisterClassLibraryRuntime(Exec, CI);

  // Assign the runtime class importer to the executer.
  if not  Exec.LoadData(Data) then // Load the data from the Data string.
  begin
    Exec.Free;
    Exit;
  end;

  if not Exec.RunScript then // Run the script.
    WriteLn('Error');
  Exec.Free; // Free the executer.
  CI.Free;  // Free the runtime class importer.
end;

begin
  ExecuteScript('var o: TStringList; begin o := TStringList.Create; o.OnChange := nil; end.');
end.

will raise error: erCouldNotCallProc

This happen in commit 86a057c8686143ce0443be6995d6515c5c9b705c: InvokeCall added, instead of all different callers (x86, x64, power