lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Yes, with the examples, I could load my library with loadlib. Really
wonderful, but I have a doubt else. Usually, I call "require" to load
modules. I have never done it with loadlib.

I tried to find something in the documentation. the function "require"
requires a loader in package.loaded. With package.loadlib, I presume a
loader is not placed automatically in this table.

If I understood, "require" calls a function passing the name of the
module. The return of this function is placed in package.loaded.

Perhaps, the function is like that:

function test_loader(name
local _G{name} = package.loadlib(name .. '.dll', 'luaopen_' .. name)
return _G{name}
end
table.insert(package.loaders, test_loader)

require('test')
print(random())

Well, this is my attempt. I really didn't understand how to call a C
library with "require".


2011/3/1, luciano de souza <luchyanus@gmail.com>:
> Hi all,
>
> I want to compile a Pascal DLL to use directly in Lua. As there is a
> Lua binding for Pascal, the task is possible. However, I didn't
> understand how to do it.
>
> If we talk about a regular program, not a DLL, the steps to use a
> Pascal function in Lua would be:
> 1. to declare the unit Lua;
> 2. to create a stack with lua_open;
> 3. to open the standard libraries of Lua with lua_openlibs;
> 4. to create a Pascal function and push the results on the stack;
> 5. To register the function to be recognized by Lua environment, using
> lua_register;
> 6. to process the stack in the protected mode with lua_pcall;
> 7. To close the stack with lua_close.
>
> These steps seemed to be not entirely valid for DLLs. If I understood,
> all DLLs directly processable for Lua possesses a initial function
> called lua_open[name of the module}. If I have a module named "test",
> its initial function will be called "lua_opentest".
>
> Following these ideas, I tried to create a DLL, but I could not
> understand some aspects.
> 1. This is the way to create a Pascal DLL for Lua?
> 2. Where should I place the lua_open and lua_close functions?
>
> library test;
> uses
> lua;
>
> function prandom(l: plua_state):integer;
> var
> n: integer;
> begin
> randomize;
> n := random(100);
> lua_tonumber(l, n);
> prandom:=1;
> end;
>
> function lua_opentest(l: plua_state):integer;
> begin
> lua_register(l, 'random', prandom);
> lua_opentest := 1;
> end;
>
> exports
> prandom;
>
> begin
> end.
>
> This DLL creates a prandom function generating numbers between 0 and
> 100. In Lua environment, it was named simply "random", so I could do:
>
> x = random()
>
>
> --
> Luciano de Souza
>


-- 
Luciano de Souza