lua-users home
lua-l archive

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


Re-sending this as it got rejected due to size.

Attached (actually download it from http://www.eonclash.com/LUA/Library.zip) is an example of using libraries from within Lua and my pLua library (available at http://eonclash.com/ViewProduct.php?ProductID=26).  No difference from the library side of things, just the test harness is using my code.  This is all in Lazarus but will compile in Delphi as well.  Hope it helps, but in short here are the fixes to the code you provided:

library prandom_lib;

{$mode objfpc}{$H+}

uses
  Classes,
  lua;

{$R *.res}

function prandom(l: plua_state):integer; cdecl;
var
n: integer;
begin
  randomize;
  n := random(100);
  lua_pushnumber(l, n);
  prandom := 1;
end;

function luaopen_prandom_lib(l: plua_state):integer; cdecl;
begin
  lua_register(l, 'random', @prandom);
end;

exports
  luaopen_prandom_lib;

begin
end.

Lua Usage:
local lib_File = "C:/lazarus/projects/pLua/LazarusDemos/Library/bin/prandom_lib.dll"
local prandom_init = package.loadlib(lib_File, "luaopen_prandom_lib")
prandom_init()
print(random())

 - Jeremy

PS: You will have to rename the .exe_ file to .exe to run it as GMail doesn't let me send zip's with exe's in them.

On Tue, Mar 1, 2011 at 7:02 AM, luciano de souza <luchyanus@gmail.com> wrote:
Well, regarding the steps to compile a regular program, with your
hints, this code seemed to be more logical.

Now, I am following rigourously the same steps.


library test;
uses
lua;

function prandom(l: plua_state):integer; cdecl;
var
n: integer;
begin
randomize;
n := random(100);
lua_tonumber(l, n);
prandom := 1;
end;

function luaopen_test(l: plua_state):integer; cdecl;
begin
l := lua_open;
lua_register(l, 'random', prandom);
lua_pcall(l, 0, 0, 0);
lua_close(l);
end;

exports
luaopen_test;

begin

end.

Now, I am not in front of the computer where I have Lua, but I think
is this. Thank you for the help!

2011/3/1, Daurnimator <quae@daurnimator.com>:
> On 1 March 2011 23:37, luciano de souza <luchyanus@gmail.com> wrote:
>> function prandom(l: plua_state):integer;
>> var
>> n: integer;
>> begin
>> randomize;
>> n := random(100);
>> lua_tonumber(l, n);
>> prandom:=1;
>> end;
>>
>
> You probably wanted pushnumber; not tonumber.
>
> Daurn.
>
>


--
Luciano de Souza