luaL_openlib

BlitzMax Forums/BlitzMax Programming/luaL_openlib

Starkkz(Posted 2014) [#1]
Hello, I'm trying to take this function from the Lua library into BlitzMax. This is how I wrote it in the code.
Rem
bbdoc: open a lua library
EndRem
  Function luaL_openlib(lua_state:Byte Ptr, libname$z, L:Byte Ptr, nup:Int)

However when I call this function from my program, it crashes with EXCEPTION_ACCESS_VIOLATION.
luaL_openlib(GameScript, "lanes.core", luaopen_lanes_core, 0)

Did I write anything wrong?


Derron(Posted 2014) [#2]
How is "GameScript" defined? Is it even defined?

Same for "luaopen_lanes_core".

I for myself leave the ", 0" out, as it defaults to 0 already.


bye
Ron


Starkkz(Posted 2014) [#3]
luaL_openlib is a C function, I defined it via extern.
GameScript is a Lua state (byte ptr)

GameScript = luaL_newstate()
luaL_openlibs(GameScript)
luaopen_socket_core(GameScript)
luaopen_mime_core(GameScript)
luaL_requiref(GameScript, "ex", luaopen_ex, 0)
luaL_requiref(GameScript, "lfs", luaopen_lfs, 0)
luaL_requiref(GameScript, "lanes.core", luaopen_lanes_core, 0)

I expected that luaL_openlib would work, but I replaced it with luaL_requiref (from Lua compat). It seems to work perfectly, and yet they have the same arguments. I can't really tell the difference between them.

luaopen_lanes_core should be fine
Rem
bbdoc: open lua lanes
EndRem
  Function luaopen_lanes_core(lua_state:Byte Ptr)



Derron(Posted 2014) [#4]
I knew the command...i just asked what the content if the used variables was...because i use the command in my framework and it successfully loads the libs on request (only using the ones which would get load by default with maxlua).

are you able to load other libs?

Bye
Ron


Derron(Posted 2014) [#5]
What I do to open the libs I want:

'register libraries to lua
'available libs:
'"base" = luaopen_base       "debug" = luaopen_debug
'"io" = luaopen_io           "math" = luaopen_math
'"os" = luaopen_os           "package" = luaopen_package
'"string"= luaopen_string    "table" = luaopen_table
Function RegisterLibraries:int(lua_state:Byte Ptr, libnames:string[])
	if not libnames then libnames = ["all"]
	
	For local lib:string = eachin libnames
		Select lib.toLower()
			'registers all libs
			case "all"      LuaL_openlibs(lua_state)
							return True
			'register single libs
			case "base"     lua_register(lua_state, lib, luaopen_base)
			case "debug"    lua_register(lua_state, lib, luaopen_debug)
			case "io"       lua_register(lua_state, lib, luaopen_io)
			case "math"     lua_register(lua_state, lib, luaopen_math)
			case "os"       lua_register(lua_state, lib, luaopen_os)
			case "package"  lua_register(lua_state, lib, luaopen_package)
			case "string"   lua_register(lua_state, lib, luaopen_string)
			case "table"    lua_register(lua_state, lib, luaopen_table)
		End Select
	Next
	Return True
End Function


"luaopen_base|_debug|_..." are functions defined as extern in pub.mod/lua.mod - so above does just work when using "predefined" libs.


This is an example how to connect some "c"-code with lua:
https://gist.github.com/randrews/939029

So at the end you "extern" the registration function.

pub.mod/lua.mod/lua-5.1.4/src/lbase.c does it this way
Line 447:
static const luaL_Reg base_funcs[] = {
//function mapping
}

Line 648:
LUALIB_API int luaopen_base (lua_State *L) {
  base_open(L);
  luaL_register(L, LUA_COLIBNAME, co_funcs);
  return 2;
}


that "base_open(L)" calls multiple things... some of them are not needed for you. Think the important lines are the following:
  /* set global _G */
  lua_pushvalue(L, LUA_GLOBALSINDEX);
  lua_setglobal(L, "_G");
  /* open lib into global table */
  luaL_register(L, "_G", base_funcs);




So for what that many lines? The difference between "requireF" and "openlibs" is, that with requiref you directly connect "luastate, name, functioncolletion" while openlibs calls a somewhere defined function which handles registering the functionpointers on their own and does some things more (setting up some global vars, some auxilary helpers etc).


bye
Ron