Forum

> > Off Topic > Attempt to set a metatable in Lua
Forums overviewOff Topic overviewLog in to reply

English Attempt to set a metatable in Lua

2 replies
To the start Previous 1 Next To the start

old Attempt to set a metatable in Lua

Starkkz
Moderator Off Offline

Quote
I've been trying to create a multi-thread module in BlitzMax which would allow me to access objects from different Lua states, for that I have to set a metatable to _G but I don't know how to do it nor anyone who could help me so I'm posting the script here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
Function lua_mt_index(L:Byte Ptr)
	Local gObject:Object = lua_tolightobject(L, 1)
	Local gField:String = lua_tostring(L, 2)
	Print "Index: "+gField
EndFunction

Function lua_mt_newindex(L:Byte Ptr)
	Local gObject:Object = lua_tolightobject(L, 1)
	Local gField:String = lua_tostring(L, 2)
	Print "New Index: "+gField
EndFunction

Function lua_CreateThread(L:Byte Ptr)
	Local Code:String = luaL_checkstring(L, 1)
	If Len(Code) > 0 Then
		Local State:Byte Ptr = luaL_newstate()
		luaL_openlibs State
		lua_createtable State, 0, 0
		
		lua_pushcfunction State, lua_mt_index
		lua_setfield State, -2, "__index"
		
		lua_pushcfunction State, lua_mt_newindex
		lua_setfield State, -2, "__newindex"
		
		lua_getglobal State, "_G"
		lua_setmetatable State, -2
		luaL_dostring State, Code
		
		Print "Error: "+lua_tostring(State, -1)
		
		'Local Script:TLuaThreadScript = New TLuaThreadScript.Create(State, Code)
	EndIf
EndFunction

Does anyone know how to set a metatable to _G using the C functions?

Note: I know this one is not working on different threads yet but I need to make it load the script and run the functions first.
To the start Previous 1 Next To the start
Log in to replyOff Topic overviewForums overview