Register a C Function array for LUA

Just starting out? Need help? Post your questions and find answers here.
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Re: Register a C Function array for LUA

Post 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
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Register a C Function array for LUA

Post 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)
Dave L
New User
New User
Posts: 6
Joined: Sat Aug 14, 2010 9:27 am

Re: Register a C Function array for LUA

Post 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 ?
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Register a C Function array for LUA

Post 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
erion
Enthusiast
Enthusiast
Posts: 128
Joined: Sun Jan 24, 2010 11:12 pm

Re: Register a C Function array for LUA

Post 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
To see a world in a grain of sand,
And a heaven in a wild flower,
Hold infinity in the palm of your hand,
And eternity in an hour.

- W. B.

Visit my site, also for PureBasic goodies http://erion.tdrealms.com
cas
Enthusiast
Enthusiast
Posts: 597
Joined: Mon Nov 03, 2008 9:56 pm

Re: Register a C Function array for LUA

Post 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")
erion
Enthusiast
Enthusiast
Posts: 128
Joined: Sun Jan 24, 2010 11:12 pm

Re: Register a C Function array for LUA

Post 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:

Code: Select all

luaL_register(L,libname.s,rl)
Any ideas?

Erion
To see a world in a grain of sand,
And a heaven in a wild flower,
Hold infinity in the palm of your hand,
And eternity in an hour.

- W. B.

Visit my site, also for PureBasic goodies http://erion.tdrealms.com
Post Reply