|
Hi listers, I am trying to write a DLL with code callable for Lua. I can understand this example, but it's not enough to do what I want.
program h9k; {$mode objfpc}{$H+} uses Classes, SysUtils, lua;
var L : PLua_State;
function lua_readln(L : PLua_State) : integer; cdecl; // Standard Lua method type var i, n : Integer; s : AnsiString; begin n := lua_gettop(L); // Find out how many arguements the method was called with for i := 1 to n do // Loop through all of the arguements write(lua_tostring(L, i)); // Convert to string and write to stdout (console window) readln(s); // Call readln, and get back a string of input lua_pushstring(L, PChar(s)); // push what we read onto the lua stack as a return value lua_readln := 1; // State that we returned one value end;
begin L := lua_open; // Create a new lua instance luaL_loadfile(L, PChar(ParamStr(1))) // Then load the file luaL_loadbuffer(L, DefaultCode, Length(DefaultCode), DefaultName); // Else load the default script luaL_openlibs(L); // Load ALL standard libraries (base, table, io, os, string, math, and package) luaL_loadfile(L, PChar(ParamStr(1))) // Then load the file lua_register(L, 'readln', @lua_readln); // Register a custom method lua_pcall(L, 0, 0, 0); // Execute the script lua_close(L); // Close the lua instance end.
Suppose I desire to store lua_readln in a DLL and later to use in a Lua script as it occurs with, for example, with Iuplua.dll.
library h9k; {$mode objfpc}{$H+} uses Classes, SysUtils, lua; var L : PLua_State;
function lua_readln(L : PLua_State) : integer; cdecl; // Standard Lua method type var i, n : Integer; s : AnsiString; begin n := lua_gettop(L); // Find out how many arguements the method was called with for i := 1 to n do // Loop through all of the arguements write(lua_tostring(L, i)); // Convert to string and write to stdout (console window) readln(s); // Call readln, and get back a string of input lua_pushstring(L, PChar(s)); // push what we read onto the lua stack as a return value lua_readln := 1; // State that we returned one value end;
begin {code} end.
The problem is what to place as a initialization code. I don't need to use loadfile neither pcall because I don't want to call a Lua script. But I think I need to use lua_register because I need to say to Lua wich name will be exported. Whats wrong? the result DLL can't be loaded with "require".
Luciano de Souza |