paxCompiler/help/demo_call_routine_ex.htm
Dalibor Marković 9d0de424e8
Init
Signed-off-by: Dalibor Marković <dalibor31@gmail.com>
2024-07-06 22:28:12 +02:00

80 lines
3.4 KiB
HTML

<html>
<head>
<link rel=stylesheet type="text/css" href="styles.css">
</head>
<body>
<H2>
paxCompiler for Delphi. Using TPaxInvoke component to call a script-defined function.
</H2>
<hr>
<blockquote>
<pre>
<font color="blue"><b>procedure</b></font> Print(<font color="blue"><b>const</b></font> S: <font color="blue"><b>String</b></font>);
<font color="blue"><b>begin</b></font>
ShowMessage(S);
<font color="blue"><b>end</b></font>;
<font color="blue"><b>var</b></font>
H_MyFunc: Integer;
I: Integer;
P: Pointer;
<font color="blue"><b>begin</b></font>
{$O-}
PaxCompiler1.Reset;
PaxCompiler1.RegisterLanguage(PaxPascalLanguage1);
PaxCompiler1.RegisterHeader(0, <font color="Red">'procedure Print(const S: String);'</font>, @Print);
PaxCompiler1.AddModule(<font color="Red">'1'</font>, PaxPascalLanguage1.LanguageName);
PaxCompiler1.AddCode(<font color="Red">'1'</font>, <font color="Red">'uses SysUtils;'</font>);
PaxCompiler1.AddCode(<font color="Red">'1'</font>, <font color="Red">'function MyFunc(U, V: Integer): Currency; cdecl;'</font>);
PaxCompiler1.AddCode(<font color="Red">'1'</font>, <font color="Red">'begin'</font>);
PaxCompiler1.AddCode(<font color="Red">'1'</font>, <font color="Red">' try'</font>);
PaxCompiler1.AddCode(<font color="Red">'1'</font>, <font color="Red">' result := U / V;'</font>);
PaxCompiler1.AddCode(<font color="Red">'1'</font>, <font color="Red">' except'</font>);
PaxCompiler1.AddCode(<font color="Red">'1'</font>, <font color="Red">' on E: Exception do'</font>);
PaxCompiler1.AddCode(<font color="Red">'1'</font>, <font color="Red">' begin'</font>);
PaxCompiler1.AddCode(<font color="Red">'1'</font>, <font color="Red">' print(E.Message);'</font>);
PaxCompiler1.AddCode(<font color="Red">'1'</font>, <font color="Red">' result := 7;'</font>);
PaxCompiler1.AddCode(<font color="Red">'1'</font>, <font color="Red">' end;'</font>);
PaxCompiler1.AddCode(<font color="Red">'1'</font>, <font color="Red">' end;'</font>);
PaxCompiler1.AddCode(<font color="Red">'1'</font>, <font color="Red">'end;'</font>);
PaxCompiler1.AddCode(<font color="Red">'1'</font>, <font color="Red">'begin'</font>);
PaxCompiler1.AddCode(<font color="Red">'1'</font>, <font color="Red">'end.'</font>);
<font color="blue"><b>if</b></font> PaxCompiler1.Compile(PaxProgram1) <font color="blue"><b>then</b></font>
<font color="blue"><b>begin</b></font>
H_MyFunc := PaxCompiler1.GetHandle(0, <font color="Red">'MyFunc'</font>, true);
P := PaxProgram1.GetAddress(H_MyFunc); // get address of script-defined function
PaxInvoke1.Address := P;
PaxInvoke1.This := <font color="blue"><b>nil</b></font>; // 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^)));
<font color="blue"><b>end</b></font>
<font color="blue"><b>else</b></font>
<font color="blue"><b>for</b></font> I:=0 <font color="blue"><b>to</b></font> PaxCompiler1.ErrorCount <font color="blue"><b>do</b></font>
ShowMessage(PaxCompiler1.ErrorMessage[I]);
<font color="blue"><b>end</b></font>;
</pre>
</blockquote>
<p>
<HR>
<font size = 1 color ="gray">
Copyright &copy; 2006-2009
VIRT Laboratory. All rights reserved.
</font>
</body>
</html>