## paxCompiler for Delphi. Using TPaxInvoke component to call a script-defined function.

>     procedure Print(const S: String);
>     begin
>       ShowMessage(S);
>     end;

>     var
>       H_MyFunc: Integer;
>       I: Integer;
>       P: Pointer;
>     begin
>     {$O-}
>       PaxCompiler1.Reset;
>       PaxCompiler1.RegisterLanguage(PaxPascalLanguage1);
>       PaxCompiler1.RegisterHeader(0, 'procedure Print(const S: String);', @Print);

>       PaxCompiler1.AddModule('1', PaxPascalLanguage1.LanguageName);
>       PaxCompiler1.AddCode('1', 'uses SysUtils;');
>       PaxCompiler1.AddCode('1', 'function MyFunc(U, V: Integer): Currency; cdecl;');
>       PaxCompiler1.AddCode('1', 'begin');
>       PaxCompiler1.AddCode('1', '  try');
>       PaxCompiler1.AddCode('1', '    result := U / V;');
>       PaxCompiler1.AddCode('1', '  except');
>       PaxCompiler1.AddCode('1', '    on E: Exception do');
>       PaxCompiler1.AddCode('1', '    begin');
>       PaxCompiler1.AddCode('1', '      print(E.Message);');
>       PaxCompiler1.AddCode('1', '      result := 7;');
>       PaxCompiler1.AddCode('1', '    end;');
>       PaxCompiler1.AddCode('1', '  end;');
>       PaxCompiler1.AddCode('1', 'end;');
>       PaxCompiler1.AddCode('1', 'begin');
>       PaxCompiler1.AddCode('1', 'end.');

>       if PaxCompiler1.Compile(PaxProgram1) then
>       begin
>         H_MyFunc := PaxCompiler1.GetHandle(0, 'MyFunc', true);

>         P := PaxProgram1.GetAddress(H_MyFunc); // get address of script-defined function

>         PaxInvoke1.Address := P;
>         PaxInvoke1.This := nil; // this is not a method, but global function.
>         PaxInvoke1.ClearArguments;
>         PaxInvoke1.AddArgAsInteger(8)
>         PaxInvoke1.AddArgAsInteger(2);
>         PaxInvoke1.SetResultAsCurrency;
>         PaxInvoke1.CallConv := _ccCDECL;

>         PaxProgram1.SetEntryPoint(PaxInvoke1);
>         PaxProgram1.Run;

>         ShowMessage(CurrToStr(Currency(PaxInvoke1.GetResultPtr^)));
>       end
>       else
>         for I:=0 to PaxCompiler1.ErrorCount do
>           ShowMessage(PaxCompiler1.ErrorMessage[I]);
>     end;

Copyright © 2006-2008 VIRT Laboratory. All rights reserved.