Ok, I see. That's a bit more complicated than the other way but it's possible. I am using the Lua52 headers which can be found on: http://lua-users.org/wiki/LuaInFreePascal
I'll post a simple example where two integers are added and the sum is returned to the Pascal program, it shouldn't be that hard to adapt it to a string/boolean function.
First the Lua script:
Code:
-- add two numbers
function add (x,y)
return (x+y)
end
That's fairly simple. Now the Pascal program (console only). After compiling one has to specify the Lua script as a parameter on the console.
Code:
program callLua;
{$mode objfpc}{$H+}
uses sysutils,lua52;
var L : plua_state;
script:string;
sum:integer;
function luaadd (x,y:integer):integer ;
var sum:integer;
begin
//* the function name */
lua_getglobal(L, 'add');
//* the first argument */
lua_pushinteger(L, x);
//* the second argument */
lua_pushinteger(L, y);
//* call the function with 2 arguments, return 1 result */
lua_call(L, 2, 1);
//* get the result */
sum := (lua_tointeger(L, -1));
lua_pop(L, 1);
result:= sum;
end;
begin
if (ParamCount > 0) then
begin
script := pChar(ParamStr(1));
L:=luaL_newstate();
luaL_openlibs(L);
if (luaL_dofile(L,pChar( script)))<>0 then
begin
//show if an error occured
writeln(lua_tostring(L, -1));
end;
sum:=luaadd(12,3 );
lua_close(L);
writeln (sum);
end;
end.
The result should be 15 of course.
Bookmarks