Page 2 of 2
Re: Register a C Function array for LUA
Posted: Sat Aug 14, 2010 3:25 pm
by SFSxOI
Dave L wrote:Hi Erion,
PureBasic is the challenge here, because you need to pass a nullpointer as second argument for luaL_register(). I don't know how to do this. Maybe this is impossible by design ?
Code: Select all
*nul_pointA.i = #Null
Debug *nul_pointA
;so.....
lua_register(L, "testfunc", *nul_pointA)
;Or
lua_register(L, "testfunc", #Null)
????
maybe
Re: Register a C Function array for LUA
Posted: Sat Aug 14, 2010 4:08 pm
by Demivec
Dave L wrote:Hi Erion,
PureBasic is the challenge here, because you need to pass a nullpointer as second argument for luaL_register(). I don't know how to do this. Maybe this is impossible by design ?
Or ...
Code: Select all
*nul_pointA = #Null
lua_register(L, "testfunc", @*nul_pointA)
Re: Register a C Function array for LUA
Posted: Sat Aug 14, 2010 6:01 pm
by Dave L
It depends on the function prototype: passing #null when a long integer is expected is easy.
The question that must be answered is: how do you pass #null when the function expects string (pointer to char in C) ?
I think Erin wants to replace luaL_register(ls,"EGP",EGPFuncs()) with luaL_register(ls,???,EGPFuncs()).
The correct value for the question mark is definitely 0 (or #null if you prefer this).
The only problem is the syntax error you get: "Bad parameter type: a string is expected".
And this is due to the function prototype: luaL_register(L,libname.s,rl).
Probably a workaround would be to change the function prototype to integer, but that is bad coding practice.
Question remains: how can you pass a pointer that doesn't point to a memory location (the null pointer = value 0) to a typed parameter (in this case type string) ? If possible at all ?
Re: Register a C Function array for LUA
Posted: Sat Aug 14, 2010 7:13 pm
by Demivec
Dave L wrote:Question remains: how can you pass a pointer that doesn't point to a memory location (the null pointer = value 0) to a typed parameter (in this case type string) ? If possible at all ?
@Dave L:
I think you are confusing the interpretation of 'null pointer'. It isn't a non-existent pointer, it is a pointer whose value is #Null. A pointer is referenced by it's address, not it's value.
The C language makes a distinction in it's syntax for this,
pointer is a pointer's value,
*pointer is the address of a pointer's value.
PureBasic uses the syntax of
*pointer to represent the address of a pointer's value,
*pointer\type to represent a pointer's value, and
@*pointer to represent a pointer's address.
Code: Select all
*nul_pointA = #Null ;this is the string pointer, it points to the address of the first character in the string
@*nul_pointA ;this value is a null pointer, it points to the address of a pointer whose value is #Null.
The null string can also be represented by a single #Null. Which would extend the above code to this:
Code: Select all
*nul_pointA = @"" ;Now the pointer points to the address of a null string
@*nul_pointA ;address of pointer
Re: Register a C Function array for LUA
Posted: Sat Aug 14, 2010 7:34 pm
by erion
Hi Dave,
I'm glad my code was useful for you.
What I mean is, if you pass an array to luaL_register(), it's faster than having a loop and calling lua_register in each loop code execution.
I believe C is somewhat faster than PB, but of course a few millisecs are not that important since I'm not planning to code anything which need high precision, e.g. a sound driver, etc.
I'm just trying to avoid a pretty long startup time, till the PB interface loads the interpreter and makes every custom function visible.
One day I'll get around this, finally sit down and finish my project and then we'll see if this has a serious effect on the startup time.
Anyways, PB gave me way more than I could wish for, so I'm not complaining at all

Also, LUA is really wonderful and I'm glad we finaly reached a working solution.
Thanks everyone
Erion
Re: Register a C Function array for LUA
Posted: Sun Aug 15, 2010 12:02 am
by cas
erion wrote:What I mean is, if you pass an array to luaL_register(), it's faster than having a loop and calling lua_register in each loop code execution.
I believe C is somewhat faster than PB, but of course a few millisecs are not that important since I'm not planning to code anything which need high precision, e.g. a sound driver, etc.
What few millisecs? It executes 1000 times lua_register() here in 0ms

with debugger disabled & Lua 5.1.4
Code: Select all
EnableExplicit
Procedure test()
EndProcedure
Define L=lua_open()
Define k
Define procname.s
Define time_start=ElapsedMilliseconds()
For k=0 To 1000
procname.s="test"+Str(k)
lua_register(L,@procname.s,@test())
Next
Define time_end=ElapsedMilliseconds()
MessageRequester("time",Str(time_end-time_start)+"ms")
Re: Register a C Function array for LUA
Posted: Fri Mar 09, 2012 10:50 am
by erion
It seems the latest PB (4.6) is unable to pass a proper array pointer. This code worked nicely in pb 4.5, now I get a memory access error.
Code: Select all
IncludeFile ("lua.pbi")
EnableExplicit
;LUA State
Global ls
; The sample procedure
ProcedureC testfunc(L)
MessageRequester("title","success!")
ProcedureReturn 0
EndProcedure
; Open the Interpreter
ls=lua_open()
;Load all the LUA Libs
luaL_openlibs(ls)
;Register the procedures for the interpreter
Global Dim EGPFuncs.luaL_Reg(1)
EGPFuncs(0)\name = "testfunc"
EGPFuncs(0)\func = @testfunc()
EGPFuncs(1)\name = #NULL$
EGPFuncs(1)\func = #Null
luaL_register(ls, "EGP", EGPFuncs())
;Close the interpreter
lua_close(ls)
The import:
Any ideas?
Erion