November 24, 2020, 06:31:25 AM

Author Topic: [bmx] Lua - add blitzmax function to lua object method by skn3 [ 1+ years ago ]  (Read 736 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Lua - add blitzmax function to lua object method
Author : skn3
Posted : 1+ years ago

Description : With these functions you can quickly add "methods" to a lua object.

Example:
Code: [Select]
Function UpperCase:Int(vm:Byte ptr)
lua_pushstring(vm,lua_tostring(vm,1).ToUpper())
Return 1
End Function

lua_pushstring(vm,"some value")
lua_addMethod(vm,-1,"ToUpper",UpperCase)


This code would add a "method" ToUpper to the string we pushed to the stack. Obviously this example is pretty useless but if you want to create an interface to a blitz object you could do some more advanced things.

<a href="../Community/postsb692-2.html?topic=87716" target="_blank">http://www.blitzbasic.com/Community/posts.php?topic=87716[/url] (code for pushing a blitz object to the stack)

Here is a snippet of code to add a stream object onto the stack with methods. It is assuming the functions in the thread above are present...
Code: [Select]
lua_register(vm,"WriteStream",LuaWriteStream)

Function LuaWritetream:Int(vm:Byte ptr)
Local path:String = lua_tostring(vm,1)
lua_pushbmaxobject(vm,WriteStream(path))
lua_addMethods(vm,-1,["Close","WriteLine"],[LuaCloseStream,LuaWriteLine])
Return 1
End Function

Function LuaCloseStream:Int(vm:Byte ptr)
Local stream:TStream = lua_tobmaxobject(vm,1)
stream.close()
Return 0
End Function

Function LuaWriteLine:Int(vm:Byte ptr)
Local stream:TStream = lua_tobmaxobject(vm,1)
stream.WriteLine(lua_tostring(vm,2))
Return 0
End Function


You could then in lua do:
Code: [Select]
stream = WriteStream("blah.txt")
stream:WriteLine("hello world")
stream:Close()


Code :
Code: BlitzMax
  1. Function lua_getMetaIndex(vm:Byte ptr,index:Int)
  2.         ' --- add a meta table and index to the object on the stack ---
  3.         'get the meta table
  4.         If lua_getmetatable(vm,index) = 0
  5.                 'add new meta table to index if there wasn't one
  6.                 lua_newtable(vm)
  7.                 If index < 0
  8.                         lua_setmetatable(vm,index-1)
  9.                 Else
  10.                         lua_setmetatable(vm,index+1)
  11.                 EndIf
  12.                 lua_getmetatable(vm,-1)
  13.         EndIf
  14.        
  15.         'get __index
  16.         lua_getfield(vm,-1,"__index")
  17.        
  18.         'create __index if there was none
  19.         If lua_istable(vm,-1) = False
  20.                 lua_remove(vm,-1)
  21.                 lua_newtable(vm)
  22.                 lua_setfield(vm,-2,"__index")
  23.                 lua_getfield(vm,-1,"__index")
  24.         EndIf
  25.        
  26.         'remove the meta table from stack
  27.         lua_remove(vm,-2)
  28. End Function
  29.  
  30. Function lua_addMethod(vm:Byte ptr,index:Int,name:String,func:Int(vm:Byte ptr))
  31.         ' --- add a method to the object ---
  32.         'force object to have meta table and __index table
  33.         lua_getMetaIndex(vm,index)
  34.        
  35.         'add teh method
  36.         lua_pushcclosure(vm,func,0)
  37.         lua_setfield(vm,-2,name)
  38.                
  39.         'remove __index from stack
  40.         lua_remove(vm,-1)
  41. End Function
  42.  
  43. Function lua_addMethods(vm:Byte ptr,index:Int,name:String[],func:Int(vm:Byte ptr)[])
  44.         ' --- add a method to the object ---
  45.         'validate correct info passed in
  46.         Assert name<>Null And func <> Null And name.Length = func.Length,"invalid methods"
  47.        
  48.         'force object to have meta table and __index table
  49.         lua_getMetaIndex(vm,index)
  50.        
  51.         'add teh method
  52.         For Local funcIndex:Int = 0 Until name.Length
  53.                 lua_pushcclosure(vm,func[funcIndex],0)
  54.                 lua_setfield(vm,-2,name[funcIndex])
  55.         Next
  56.                
  57.         'remove __index from stack
  58.         lua_remove(vm,-1)
  59. End Function


Comments :


LT(Posted 1+ years ago)

 Had to make changes to get this compiling.  Both instances of...Local stream:TStream = lua_tobmaxobject(vm,1)...should probably be...Local stream:TStream = TStream( lua_tobmaxobject(vm,1) )Also, LuaWritetream should be LuaWriteStream.  Thanks for your contribution, though.  :)


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal