Registering a type method to a Lua VM?

Started by NoNameHere, February 07, 2019, 12:50:53

Previous topic - Next topic

NoNameHere

I was wondering if it's possible to register a BlitzMax type method instead of a function to a Lua virtual machine.

Here is the code I'm trying:

Framework BRL.StandardIO
Import PUB.Lua

Local DataStruct:SomeDataStructure = New SomeDataStructure
DataStruct.Data = "Some super important data"

Local Lua:LuaContext = New LuaContext

Lua.Init(DataStruct)
Lua.Execute("test.lua")


Type SomeDataStructure
Field Data:String
End Type


Type LuaContext
Field LuaVM:Byte Ptr
Field DataStructure:SomeDataStructure

Method Init(DataStructArg:SomeDataStructure)
DataStructure = DataStructArg

LuaVM = luaL_newstate()
luaL_openlibs(LuaVM)

LuaAddFunction("LuaPrintSomeData", LuaPrintSomeData)
End Method

Method Execute(ScriptFilename:String)
luaL_loadfile(LuaVM, ScriptFilename)

' Debug code, provides stack traces in case of crash
lua_getfield(LuaVM, LUA_GLOBALSINDEX, "debug")
lua_getfield(LuaVM, -1, "traceback")
lua_remove(LuaVM, -2)

If lua_pcall(LuaVM, 1, -1, -1) <> 0
Print "Lua error: " + lua_tostring(LuaVM, -1)
lua_pop(LuaVM, 1)
End If
End Method

Method LuaAddFunction(FunctionName:String, FunctionPtr:Byte Ptr)
lua_register(LuaVM, FunctionName, FunctionPtr)
End Method

Method LuaPrintSomeData(LuaState:Byte Ptr)
Print DataStructure.Data
Return 0
End Method
End Type


On current BlitzMaxNG v0.99 it gives me the following error:

C:/.../.bmx/test.bmx.console.release.win32.x64.c: In function '__m_test_LuaContext_Init_TSomeDataStructure':
C:/.../.bmx/test.bmx.console.release.win32.x64.c:84:105: error: '_m_test_LuaContext_LuaPrintSomeData_pb' undeclared (first use in this function); did you mean '__m_test_LuaContext_LuaPrintSomeData_pb'?
  ((struct _m_test_LuaContext_obj*)o)->clas->m_LuaAddFunction_Spb((struct _m_test_LuaContext_obj*)o,&_s2,_m_test_LuaContext_LuaPrintSomeData_pb);
                                                                                                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                                                                                                         __m_test_LuaContext_LuaPrintSomeData_pb


Checking the .c file, function beginning with the double underscore is indeed declared. I don't know much about the inner workings of BlitzMaxNG, but could that single-underscore difference be an error in the compiler?
Or doing that (Registering a type method to Lua) is not allowed?

Derron

Brl.maxlua might help.

Or check my lua stuff:
Https://github.com/GWRon/Dig
It shows how to expose methods and functions from BlitzMax to Lua.


Bye
Ron