LuaJIT module

BlitzMax Forums/BlitzMax Programming/LuaJIT module

Zeke(Posted 2010) [#1]
Download LuaJIT 1.1.8 and LuaJIT 2.0.1 (latest stable) version:

Zeke_LuaJIT 1 & 2

There are 2 lua examples, mandelbrot and sieve. (LuaJIT 2 includes also some FFI tests)

My results(updated):
-Mandelbrot:
pub.lua - 57 seconds
luaJIT - 7 seconds
LuaJIT-2 time: 2secs

-Sieve:
pub.lua - 13.218 seconds
luaJIT -4.933 seconds
LuaJIT-2 time: 0.5secs

EDIT: Updated LuaJIT 2.0.1 stable version


plash(Posted 2010) [#2]
I can't get pub.lua to work on Linux (complains about dlsym, dlopen, etc.), but here are the times for luaJIT:
Mandelbrot: 10 seconds
Sieve: 6.06 seconds


kenshin(Posted 2010) [#3]
Works fine here.

Mandelbrot
pub - 26 secs
jit - 3 secs

Sieve
pub - 5.969
jit - 2.157

Thanks for sharing. It's a huge speed improvement.


ImaginaryHuman(Posted 2010) [#4]
Does it compile to exe or some kind of bytecode type of thing? Cross platform?


plash(Posted 2010) [#5]
Does it compile to exe or some kind of bytecode type of thing?
As far as I know it's all in memory.

Cross platform?
Yes.

I noticed the wrapper is using version 1.1.5 of luaJIT.. might want to try 2.0 (I also noticed some compilation warnings under Linux).


GW(Posted 2010) [#6]
Sweet!


ImaginaryHuman(Posted 2010) [#7]
What I should've asked is does it compile to machine-code opcodes which can be directly executed by the CPU or is there some interpretation still?


plash(Posted 2010) [#8]
What I should've asked is does it compile to machine-code opcodes which can be directly executed by the CPU or is there some interpretation still?
It has its own VM (see here for more info).

EDIT: Also, according to that page, LuaJIT was completely rewritten and optimized in version 2.0, so it may be even faster than the benchmarks we're getting now.


Tommo(Posted 2010) [#9]
It's X86 so far, there is a X64 version in development.
I've been using LJ1 for a while. It's about 4x fast as normal lua.
Coroutine support of LJ1 under windows is not very good, but this seems have been improved in LJ2.


JoshK(Posted 2010) [#10]
I am very surprised. I was able to just change pub.lua to your module, compile my engine, run my editor, and everything seems to work fine. No speed tests yet, but this is very interesting.


JoshK(Posted 2010) [#11]
Is there a way to toggle JIT/interpretation when a new state is created, instead of recompiling the app?


Zeke(Posted 2010) [#12]
Hi, there is a new LuaJIT 2 module (latest 2.0.0-beta3).. NOTE, that this is only for testing purposes only(windows only).I didin't get this work with source code. so i made .dll and this module use that. but this works and ITS MUCH faster than LuaJIT 1.1.5.
if you want to test: **EDIT LINK REMOVED** use first post link.

AND you need to copy 'lua51.dll' to your project directory (or copy to C:\windows/system32)

Leadwerks you can toggle JIT compiler on/off.. using 'jit.on()' or 'jit.off()' in lua script

some Results:
Sieve:
LuaJIT-2 time: 0.5secs
(same code but added 'jit.off()' in the first line of 'sieve.lua') time: 4.33 sec


GW(Posted 2010) [#13]
awesome! can't wait to try it out.


Zeke(Posted 2010) [#14]
wow. i got LuaJIT 2.0 beta 4 working without .dll... and speed is still same as in my first post.. i will give you guide how i made (and replaced old pub.lua) luajit2 module to working.. but maybe tomorrow.. because im now too drunk.. HAHA :D
but here is my test..
**EDIT LINK REMOVED** use first post link.


mic_pringle(Posted 2010) [#15]
.. i will give you guide how i made (and replaced old pub.lua) luajit2 module to working.. but maybe tomorrow..

Please do as I would really like to get this up and running on a Mac, and a guide to how you achieved it on Windows would go a long way to help out.

Thanks

-Mic


EOF(Posted 2010) [#16]
Sounds very interesting. I don't have MinGW or any such thing installed here and I can never get it and Max to live in harmony anyhow. Are you planning to release a pre-compiled module version? I would love to plaay around with this one

I want to learn the basics of LUA + Max but don't know where to begin
Some initial questions:

1) Who syntax-checks the Lua script? You (via BlitzMax) or the Lua module? E.g,
function MoveMyShip(x,y,speed=1.0       <-- missing end bracket


2) Can you register commands which have optional parameters? (thus allowing the script optional ways of using one command?) E.g,
function MoveMyShip(x,y,speed=1.0)



Oddball(Posted 2010) [#17]
1) You have to tell it to, but Lua can debug the code. You can get it to print out errors to the output window or a debug file or wherever you want.

2) Yes Lua is very forgiving with the number of params. passed to functions. You can even pass too many, the extra are ignored, and have multiple return values.

Disclaimer: I'm fairly new to Lua myself so my advice may not be the best.


N(Posted 2010) [#18]
I'll ignore 1 because that's an odd question to ask.

On the second question, you can have optional parameters, but they'll just be nil if you don't provide a value. You cannot have default values for arguments like in your example, it won't work.

For more information, read the manual. Really, it's a very good manual, I don't know why some people don't read it.


Zeke(Posted 2010) [#19]
Ok, here is this latest luajit 'hack': **EDIT LINK REMOVED** use first post link.

backup pub.lua, delete original pub.lua, copy my mod to pub.lua, copy libluajit.a to Blitzmax/lib folder, build pub.lua module.. test..

and here is guide how i made this:
download latest luajit sources. and extract to LuaJIT-2.0.0 folder
use: 'mingw32-make' to build sources.
use: 'ar rcs libluajit.a *.o' in src folder to get libluajit.a
copy libluajit.a to blitzmax\lib
lua.bmx changes:
delete ALL Imports
add:
Import "-lluajit"
Import "-lmingw32"
Import "-lgcc"
then build pub.lua module: 'bmk makemods -a pub.lua' (and -h for threaded)
and build other modules that use pub.lua (brl.maxlua) (yea. you get errors, because in lua_object.c, include point to original lua folder, so change that to '#include <pub.mod/lua.mod/LuaJIT-2.0.0/src/lua.h>', then build)

^^ i said this i 'hack' solution.. so if some c/c++ guru can make this easier, feel to free tell us. :D


JoshK(Posted 2010) [#20]
Lua is good at catching compile errors. If the return value of dofile() or pcall() returns non-zero, I think lua_tostring(-1) will give you the error message.

For actual crashes, you can use try/catch and actually get the debug info from Lua after your program has crashed using the Lua function debug.trace(). I posted an example of this recently.

With Lugi, all default parameters are 0/nil. This is because BlitzMax doesn't support default values in reflection. LuaBind for C++ does support default values. You could do it with BlitzMax, but you would have to parse source code files instead of using reflection, which is more error-prone. I have used all the code interfaces for BlitzMax and Lua, and I would not use anything except Noel's Lugi module.

wow. i got LuaJIT 2.0 beta 4 working without .dll... and speed is still same as in my first post.. i will give you guide how i made (and replaced old pub.lua) luajit2 module to working.. but maybe tomorrow.. because im now too drunk

That's funny, you were drunk when you emailed me the other night, too. Well, if it gets the code done...


JoshK(Posted 2010) [#21]
Please do not name your modules to replace the official pub or brl modules. It is not acceptable to make my users replace their modules, and it will be easily overwritten by an update.


Zeke(Posted 2010) [#22]
Yes, i know that, but that was only for testing.. But good news here.

here is a new zip, this contains BOTH LuaJIT modules (LuaJIT 1.1.6 and LuaJIT 2.0.0-beta4) AND they are now in zeke.mod folder.

http://www.byrathon.com/luajit_mods.zip

also good news is that you dont need to copy libluajit.a to Bmax\lib folder anymore,

so when there is new LuaJIT update, you just need to do:

1. download lastest LuaJIT sources from http://luajit.org/ and extract to somewhere
2. Delete all files+folders from 'zeke.mod/luajit2.mod/LuaJIT-2.0.0'
3. Copy #1 extracted files+folders to ^^ folder
4. Open console, go to ^^ LuaJIT-2.0.0/src folder and type: 'mingw32-make' (this will build luajit)
5. then type to console 'ar rcs libluajit.a *.o' (this will build libluajit.a to src folder)
6. build luajit2 module.


EOF(Posted 2010) [#23]
This is working great Zeke and I am having fun learning Lua

One neat way of experimenting/learning Lua is to "Lock Build" a file which launches a demo script. The demo script can be loaded into the BlitzMax IDE for editing (use extension .lua to prevent the IDE from changing commands)
Hitting F5 then launches the locked main executable, which in turn runs your script

For example, this is my "Locked" Blitzmax file
LuaDemo1 Run.bmx



Some random Lua script which I have loaded into the Max IDE for playing around with ...

Demo1.lua



theHand(Posted 2010) [#24]
Thank you sir! I am very happy with the results. Nicely wrapped!
The Mandelbrot was ~39 seconds with pub.lua, and ~5 seconds with your wrapped version of LuaJIT.
Assembly sure is fast, isn't it... :D (I think it might just be specific ways of optimizing for x86 chips)

I'm gonna stick with LuaJIT 1.1.6 so that I get less headaches.


JoshK(Posted 2010) [#25]
Seems to work great. Thanks again.


EOF(Posted 2010) [#26]
Fellow Lua coders,

Is there a way to change Lua global variables from within BlitzMax?
For example, the script here has global width and height variables
I register SetSize() and have a Max lSetSize() function to handle the values passed
Then, I want to change the width and height parameters in Lua

LuaScript.lua
global width
global height

SetSize(128,64)
print("Width:" .. width .. " and Height:" .. height)



BlitzMax code snippet
Function lSetSize:Int(L:Byte Ptr)
	local w:Int = lua_tonumber( L, 1 )
	local w:Int = lua_tonumber( L, 2 )

	' manipulation of w and h done

	' now, how do you set the Lua's global width and height
	' to the new values in w and h ?


	lua_???(lua_state,width)
	lua_???(lua_state,height)

	Return 2  ' what should be returned? the number of params?
End Function



N(Posted 2010) [#27]
Did you read the manual? (I'm not really fond of giving answers to simple questions, so there's your hint that you should read the manual.)


EOF(Posted 2010) [#28]
Yes:
http://www.lua.org/manual/5.1/manual.html


N(Posted 2010) [#29]
That's not the manual, that's a book. This is the manual: http://www.lua.org/manual/5.1/manual.html Notice that it says 'manual' in the title.


EOF(Posted 2010) [#30]
The lua_getglobal() is about as close as I can guess to being the appropriate function
I don't know how to change the value so that the script also sees the change though.


JoshK(Posted 2010) [#31]
Here's how I usually do things:
		Local size:Int=StackSize()
		lua_pushinteger(L,i)
		lua_setglobal(L,name)
		SetStackSize(size)


No worries about the stack size, just restore it to the original size when you are done.

Some helper functions:
	Method StackSize:Int()
		Return lua_gettop(L)
	EndMethod

	Method SetStackSize(size:Int) {hidden}
		Local currentsize:Int=StackSize()
		If size<currentsize
			lua_pop(L,currentsize-size)
		EndIf
	EndMethod



EOF(Posted 2010) [#32]
Great help Josh. The running script now reports the changed values


JoshK(Posted 2010) [#33]
Zeke, would it be possible to recompile LuaJit with the following change:

At line 99 of luaconf.h change this:
#define LUA_IDSIZE 60

to this:
#define LUA_IDSIZE 1024

This will fix it so error messages won't cut off the name of the file an error occurs in. Thanks.


Zeke(Posted 2010) [#34]
Changed LUA_IDSIZE to 1024 in both modules. download link is in first post.


EOF(Posted 2010) [#35]
Zeke,
Did you manage to take a look at this one I posted in your zeke.mod thread?

-----------------------------------------------------------------------
Can you implement the luaL_error() function ?
If not, are there other ways to push an error into lua?
-----------------------------------------------------------------------


N(Posted 2010) [#36]
Push a string and call lua_error. luaL_error is a convenience function, does roughly the same thing as running snprintf, pushing the result, and calling lua_error. There's also no good way to port it without it being different from the C API, since BlitzMax doesn't support variadic functions.


EOF(Posted 2010) [#37]
That works fine. Thanks Nilium
Do you know how to get the offending line number causing the error?
What I have is a registered function. When the Blitz function is called a check is made to ensure the correct number of parameters are passed. If not I now push the error out

Example:

Function MoveObject(L:Byte Ptr)
	Local numParams:Int = lua_gettop(L)
	If numParams<3
		lua_pushstring(L,"MoveObject - Expecting 3 parameters")
		lua_error(L)
	EndIf
End Function



(This function could be used anywhere in the Lua script)


Zeke(Posted 2010) [#38]
sorry Jim i forgot to post.. but just what Noel said that no support for variadic functions..

and about your test. using debug.traceback you can get line number where error occurs:
Function MoveObject(L:Byte Ptr)
	Local numParams:Int = lua_gettop(L)
	If numParams < 3
		lua_getfield(l , LUA_GLOBALSINDEX , "debug")
		lua_getfield(l , - 1 , "traceback")
		lua_remove(l , - 2)
		Print "Error: Expecting 3 parameters"
		If lua_pcall(l , 0 , 1 , 0) <> 0 Then lua_pushstring(l , lua_tostring(L,-1) )
		lua_error(L)
	EndIf
End Function



N(Posted 2010) [#39]
Do you know how to get the offending line number causing the error?
You should use the lua_Debug structure and lua_getinfo. Bear in mind that line info is not always available. You'll probably have to write a quick bit of C to allocate and release a lua_Debug structure, and you'll have to access its contents via pointer fiddling (or you could write auxiliary functions to get the data for you).

I don't recommend using Zeke's way for two reasons: 1) it's a lot more work than is needed and 2) the state of the global table may not be consistent with the state of the VM itself (meaning it's probably not the safest thing to do). Edit: Whether or not #2 can happen or not, I'm not sure, but I'd rather just pull the data straight from the VM.


EOF(Posted 2010) [#40]
Thanks both. I have lots to learn


JoshK(Posted 2010) [#41]
Thanks for the fix, Zeke!

This is my method for error handling:
	Method DoString:Int(source:String)
		Local result:Int
		Local dummy:Object
		
		Try
			result=luaL_dostring(L,source)
		Catch dummy:Object
			HandleException()
		EndTry
		If result=0
			Return True
		Else
			Return False
		EndIf
	EndMethod
	
	Method HandleException() {hidden}
		Local debugsuccess:Int
		Local err:String="No Lua debugger found."
		
		lua_getglobal(L,"debug")
		If lua_istable(L,-1)
			lua_getfield(L,-1,"traceback")
			If lua_isfunction(L,-1)
				If lua_pcall(L,0,1,0)=0
					If lua_isstring(L,-1)
						err=lua_tostring(L,-1)
					EndIf
				EndIf
			EndIf
		EndIf
		Print err
		If Not silentexception
			Notify err,1
		EndIf
		End
	EndMethod



JoshK(Posted 2010) [#42]
I tried using your new luajit2 module with ID_SIZE changed to 1024, but I still get the error messages cut off. Are you sure you recompiled that one?

Luajit1 seems to work correctly.


Zeke(Posted 2010) [#43]
hmm.. i tested both. and that worked fine, and i used this code to test:
Framework zeke.luajit2 'pub.lua
Import brl.standardio

Global l:Byte Ptr = luaL_newstate()

Local file$ = "c:\Lua_mega_long_filename_changed idsize from 60 to 1024 testing script file blaah blaah.lua"

Print file.length

If luaL_dofile(l , file)
	Print lua_tostring(l , - 1)
	End
EndIf

but now i also changed luajit2.bmx(and luajit1.bmx) debug type to the correct fields (1024 bytes reserved to short_src msg). and added a new function luaJIT_setmode()(only in luaJIT2). download from 1st post link.


JoshK(Posted 2010) [#44]
This example illustrates the problem:
Framework zeke.luajit2 'pub.lua
Import brl.standardio

Global l:Byte Ptr = luaL_newstate()

Local cs:Byte Ptr

Local file$ = "c:\Lua_mega_long_filename_changed idsize from 60 to 1024 testing script file blaah blaah.lua"

Local source:String="a.f=8"

cs=source.ToCString()

luaL_loadbuffer(l,cs,source.length,file)

If lua_pcall(l , 0,0,0)
	Print lua_tostring(l , - 1)
	End
EndIf



Zeke(Posted 2010) [#45]
[string "c:\Lua_mega_long_filename_changed idsize from 60 to 1024 testing script file blaah blaah.lua"]:1: attempt to index global 'a' (a nil value)

this is what i get.
make sure that you are downloading a NEW version.. and not that "cached" version.. <= im not sure how to delete old "cached files.. but...
i hope you can get this woking.


JoshK(Posted 2010) [#46]
I just downloaded from the link at the top of the thread and ran my example program. Here is the output:
Building test
Compiling:test.bmx
flat assembler  version 1.68  (1572863 kilobytes memory)
3 passes, 1288 bytes.
Linking:test.exe
Executing:test.exe
[string "c:\Lua_mega_long_filename_changed idsize from..."]:1: attempt to index global 'a' (a nil value)

Process complete


Using LuaJIT1 it works:
Building test
Compiling:test.bmx
flat assembler  version 1.68  (1572863 kilobytes memory)
3 passes, 1288 bytes.
Linking:test.exe
Executing:test.exe
[string "c:\Lua_mega_long_filename_changed idsize from 60 to 1024 testing script file blaah blaah.lua"]:1: attempt to index global 'a' (a nil value)

Process complete


So I think you forgot to recompile LuaJIT2. ;)


Zeke(Posted 2010) [#47]
**EDIT** removed old text. and here is the fix.

HMM.. do you have that 'libluajit.a' file in bmax/lib folder?? if it exists then maybe bmax first check that lib file and use it instead luajit2/src/libluajit.a file. (delete that bmax\lib\luaji.a)<= i think this will fix this problem.

Yes. i just tested. and i put old libluajit.a file to that folder and then i get that "old" error also.. but when you delete that file it should run fine. (My fault, sorry)


JoshK(Posted 2010) [#48]
It works, thank you again. :)


Zeke(Posted 2011) [#49]
updated modules.


mpmxyz(Posted 2011) [#50]
I can't download the modules but get 404 errors instead. :-/
Fix it! :-)


Zeke(Posted 2011) [#51]
sorry. fixed.


Zeke(Posted 2011) [#52]
Updated to LuaJIT 2.0 beta 9


GW(Posted 2011) [#53]
Yay!


GW(Posted 2012) [#54]
Has anyone been able to compile any of the examples?
All of my attempts fail with:
C:/Dev/BlitzMax/lib/libgcc.a(unwind-sjlj.o): undefined reference to `__mingwthr_key_dtor'
C:/Dev/BlitzMax/lib/libgcc.a(unwind-sjlj.o): undefined reference to `_CRT_MT'
Build Error: Failed to link C:/Dev/BlitzMax/mod/zeke.mod/luajit2.mod/Benchmark/Sieve test.exe

I've tried the latest Bmax/mingw 1.46 and the older 1.39/mingw. Same error for both.

Last edited 2012


Zeke(Posted 2012) [#55]
its working fine here. are you using original bmk or bruceys bmk(ng)?
http://www.blitzbasic.com/Community/posts.php?topic=84925#960166 there was same errors what you got.


GW(Posted 2012) [#56]
Original bmk.
My version of bmax 1.46 is on a new computer (win 7). I just installed bmax, the 'nuwen' mingw package and setup the env variables.
I can build the luajit2 module with bmk and no errors. Is there something else I need in addition to a vanilla install of bmax?
btw, I can build and run a lot of Brucey modules without a problem.
Any help is appreciated


Zeke(Posted 2012) [#57]
hmm.. 1.46 is Mac Lion only, but i think windows side is not changed(1.45) my mingw is not nuwen, but downloaded from the original mingw download page.. http://sourceforge.net/projects/mingw/files/Installer/mingw-get-inst/

and im runninw win 7 32-bit

and just tested, that i renamed my current luajit2 module to luajit22 and then downloaded this module again.. and extracted luajit2 and opened sieve test.bmx and hit build and run. and it works.

(i have rebuilded all modules with latest mingw build and using original bmk)

hmm.. GO to DOS console, and then to Blitzmax/mod/zeke.mod/luajit2.mod/luajit-2.0/ directory
then type mingw32-make<enter> and it start to compile luajit code...

after that rebuild luajit2 module and try examples...

Last edited 2012


Zeke(Posted 2012) [#58]
hmm.. yeah.. i think that all you need is to download module and "re-compile" luajit source using that ("mingw32-make") <in win32>
and then rebuild luajit2 module.. and then it should work..


GW(Posted 2012) [#59]
Thanks for the response. I downloaded the mod and rebuilt the object files with mingw, bmk'd the mod, but the error persists. Unless someone else independently gets the same error, I can only assume this error is local to me. Perhaps I'll try the rebuilt DLL in order to use the FFI in the meantime.
Thanks for looking into the problem, and let me know if any other possible solutions come to mind.
thanks!


AnniXa(Posted 2012) [#60]
Hey, that link is not working anymore :(

anyone got a working link?


Zeke(Posted 2012) [#61]
sorry. webmaster upgraded host, so there is some downtime..


Armitage 1982(Posted 2012) [#62]
The gain in performances are incredible !
I'm always very tempted to include script abilities in my games but always fear a major slow down when I see disastrous implementation like TorqueScript or some other.

But for some slow paced script this can be very interesting.
Hope for an uptime soon or later :)


Zeke(Posted 2012) [#63]
Link fixed. and also updated to LuaJIT 1.1.8 and LuaJIT 2.0.0 beta 10

Last edited 2012


Armitage 1982(Posted 2012) [#64]
Mandelbrot
pub.lua - 33s
LuaJIT - 3s
LuaJIT 2.0.0-b10 - 1s

Sieve
pub.lua - 5.619s
LuaJIT - 1.807s
LuaJIT 2.0.0-b10 - 0.147s


I think the numbers talk for themself :)
Thanks for this update!

I wonder how fast it is compared to BriskVM 2. BVM2 is no more updated but maybe because it's a complete product.


LT(Posted 2012) [#65]
I'm confused about a couple of things and was hoping someone could shed some light.

1. Where is the additional speed coming from? If LuaJIT is not running pre-compiled scripts, doesn't that mean it still has to interpret and then run them - just like maxlua?

2. If I'm not importing pub.lua explicitly, how do I replace it with zeke.luajit? I'm a little confused as to what the "standard modules" are and where they get included.


Yasha(Posted 2012) [#66]
Where is the additional speed coming from?


JIT = Just In Time, referring to when the program is compiled. As contrasted with Ahead Of Time, used by your Max code. The Lua is compiled into x86 instructions as and when they're needed when the code is about to run, directly in memory.


LT(Posted 2012) [#67]
Thanks for the quick reply! I am familiar with the term. I just don't see how that differs from what regular Lua is doing.


Yasha(Posted 2012) [#68]
Regular Lua compiles to LuaVM instructions, which are interpreted in software. It's a very clean and efficient VM design and instruction set, but even the best such are still an order of magnitude slower than running on the metal.


LT(Posted 2012) [#69]
Ah, so it's efficiency. I don't suppose there is a way to compile into byte code and execute later?


JoshK(Posted 2012) [#70]
Yes, there actually is. See Luac, I think.


LT(Posted 2012) [#71]
Thanks, I will definitely look into that!

One other question - has anyone out there found a way to get compiled Lua to use StandardIOStream? I have it implemented in my engine and would like to redirect Lua Print commands to my console, but strangely, it seems to ignore the usual redirect StandardIOStream = ??.

Just wondering if anyone else had encountered this problem?


JoshK(Posted 2012) [#72]
I always found print() a little unpredictable. You can just set print() to nil and add your own print function.


LT(Posted 2012) [#73]
Since I don't know how to get a variable list of arguments inside BMax, I made a Lua print() replacement that calls a Blitz version. It works, but it's not as elegant as I'd hoped.

Thanks for your response, though.


LT(Posted 2012) [#74]
Josh, I was trying out your method for Lua error handling and noticed that it only seems to work in debug mode. Was that your result, as well?

What if you want to make a stand-alone app that compiles and runs Lua scripts, but doesn't end on Lua errors?


JoshK(Posted 2012) [#75]
Josh, I was trying out your method for Lua error handling and noticed that it only seems to work in debug mode. Was that your result, as well?

What if you want to make a stand-alone app that compiles and runs Lua scripts, but doesn't end on Lua errors?

I've run across other undocumented behavior where Lua does not call debug hooks unless the DEBUG macro is defined. So it would not surprise me if what you describe is true.

I just discovered today, I think LuaJIT never calls the Lua HOOK_LINE function, but that's a very advanced feature.

Last edited 2012


LT(Posted 2012) [#76]
What you meant wasn't immediately obvious to me, but I seem to have the problem fixed. I know this is a luajit thread, but I was still using maxlua because of an unknown module conflict that I had yet to track down. Maxlua, if I'm not mistaken, doesn't define the DEBUG macro when Blitz is not in debug mode. Luajit does, so it was a matter of finding the conflict that was preventing me from using it.

Now that I'm actually using luajit - problem solved! Anyway, thanks for the advice. :)

EDIT: One caveat is that my print function replacement no longer works. It seems the hidden 'arg' table is not available in luajit.

Last edited 2012


LT(Posted 2012) [#77]
..

Last edited 2012


Pineapple(Posted 2012) [#78]
When attempting to compile my game using zeke.LuaJIT2 instead of pub.lua I get the build error:

C:/BlitzMax/lib/libgcc.a(unwind-sjlj.o): undefined reference to `__mingwthr_key_dtor'
C:/BlitzMax/lib/libgcc.a(unwind-sjlj.o): undefined reference to `_CRT_MT'

what?


edit: I tried recompiling jit and then rebuilding the module, as GW did. I got this build error:



and the problem persists

edit2: jit1 works without consequence, but it would certainly be nice to also have the option of jit2

Last edited 2012


LT(Posted 2012) [#79]
Running a for loop in Lua 100000 times and calling a single 'do nothing' function takes 100 times longer than doing the same thing in Blitz.

Is that the result I should expect? Seems slower than I thought.. :(


BladeRunner(Posted 2012) [#80]
As Lua is interpreted you can expect it to be lots slower than compiled code. Still Lua is one of the fastest interpreted scripting languages in existance. Did you try LuaJIT?


Yasha(Posted 2012) [#81]
Is that the result I should expect? Seems slower than I thought.. :(


That is actually pretty slow. Are you doing something like recompiling each time? Most professional-quality interpreters - which Lua is - are significantly faster than that (the number should be more like 3-10 depending on how complex the code is).


Zeke(Posted 2012) [#82]

Results:
[bbcode]
'pub.lua:
lua time = 15ms
bmx time = 0ms

'luajit1:
lua time = 2ms
bmx time = 1ms

'luajit2:
lua time = 0ms
bmx time = 0ms[/bbcode]


Yasha(Posted 2012) [#83]
You might want to crank up the number of iterations to at least a hundred million to get useful test results. Those times are too short to be statistically meaningful (for purposes other than "this is actually pretty fast"), both because the error margin for timing operations that way is a good deal higher than 15ms, and because too much detail is being truncated.

Doing that, I get:

'pub.lua
lua time= 12265ms
bmx time= 635ms

'luajit1
lua time= 3641ms
bmx time= 505ms

'luajit2
lua time= 110ms
bmx time= 506ms


Now, that suddenly got interesting.


LT(Posted 2012) [#84]
My test was admittedly non-scientific. I should be more specific about what I'm doing. By the way, I'm not comparing pub to luajit1 to luajit2 (but I realize that's how this thread started).

I am using luajit2 and a slightly modified luGI to add luascript capability to my 3d engine. My test was to call a function over and over, which is apparently slow in the LuGI lookup, then do the same in Blitz. I did another where I assigned the function to a local first and that sped things up quite a bit (x5). I am still a bit surprised by the initial result, though.

@Yasha - Recompiling each time? Perhaps, but I'm not sure how to avoid that. I didn't think we had any control over when luajit2 decides to do it. I was looking into luac, but I haven't been able to find the right one (with byte code in the same format).

Thanks for all the responses!

Last edited 2012

Last edited 2012


LT(Posted 2012) [#85]
@Zeke - Which version of Luac should be compatible with luajit2, please?


Zeke(Posted 2012) [#86]
use luajit.exe to generate byte code.

[bbcode]luajit -b source.lua dest.out[/bbcode]

Also you need to copy LuaJIT-2.0/lib folder to LuaJIT-2.0/src/ and rename lib to jit.


LT(Posted 2012) [#87]
Thanks, it works! Is all of that distributable? In a commercial project? Guess it would have to be called through the shell.


Zeke(Posted 2012) [#88]
LuaJIT 2 updated to beta 11


GW(Posted 2012) [#89]
Thanks!


Zeke(Posted 2012) [#90]
Hiya.. LuaJIT 2.0.0 Stable version released. links (first post) updated.


Zeke(Posted 2013) [#91]
And LuaJIT 2.0.1 version. (Hotfix #1 included).


PriorBlue(Posted 2013) [#92]
@Zeke - Thank you for the frequent updates, it makes my programs faster every time (I use a lot of Lua Script :D)

But I found a weird memory leak with "on the fly" Blitzmax Types since luajit 2 and I have no Idea how to solve it.

Sample Code:
[bbcode]
Framework Brl.MaxLua
Import Brl.StandardIO

SuperStrict

Type TObject
Method createObject2:TObject2()
Return New TObject2
End Method
End Type

Type TObject2
Method printCount:String(cnt:Int)
Print "cnt: " + cnt
End Method
End Type

Local obj:TObject = New TObject
LuaRegisterObject obj, "Object"

'works with luajit 1 & 2 (Workaround)
Local source$="cnt = 0 object2 = {} function printCount() object2[cnt] = Object.createObject2() object2[cnt].printCount(cnt) cnt = cnt + 1 end"

'works only with luajit 1 (luajit 2 has a memory leak after ~150 Runs)
'Local source$="cnt = 0 function printCount() object2 = Object.createObject2() object2.printCount(cnt) cnt = cnt + 1 end"

Local class:TLuaClass=TLuaClass.Create(source)
Local instance:TLuaObject=TLuaObject.Create(class,Null)

For Local i:Int = 0 To 99999
instance.Invoke("printCount", Null)
Next
[/bbcode]

Explanation:

When I create a "Blitzmax Type" with lua and overwrite it with an other, my code gets a memory leak and crash after x runs, so I can't use the same variable over and over again.
I know there are workarounds like an permanent increased array of Types, but I hope there is a better solution.

PS: a "Threaded Build" is working a long time for me, before the memory leak appears.


Wiebo(Posted 2013) [#93]
I am trying to use the make command on the luajit2 src but I get the following error:

HOSTLINK host/minilua
host/minilua.o: file not recognized: File format not recognized
collect2: error: ld returned 1 exit status
make: *** [host/minilua] Error 1

make on the luajit1 module works just fine. I am using Ubuntu 13.04 btw. Does anyone know how to overcome this?


PhotonTom(Posted 2014) [#94]
Sorry to revive this old thread but if you are looking for a working link then please see: http://www.blitzmax.com/Community/posts.php?topic=100728


Starkkz(Posted 2014) [#95]
The link is broken and the version of PhotonTom's link is the 2.0.0 beta 4, has anyone built a newer version with FFI? (FFI was released with beta 6)


Starkkz(Posted 2014) [#96]
I've compiled LuaJIT 2.0.3 on Windows and then I placed it on the mod folder.

Import "-Lmod/zeke.mod/luajit2.mod/LuaJIT-2.0.3/src"
Import "-lluajit"


It works when I build it, but when I try to build a program that uses this module I get this.
Building execute
Compiling:execute.bmx
flat assembler  version 1.69.14  (1009141 kilobytes memory)
3 passes, 1635 bytes.
Linking:execute.exe
E:/Programas/BlitzMax/bin/ld.exe: cannot find -lluajit
Build Error: Failed to link D:/Escritorio/LuaJIT/execute.exe
Process complete


Does anyone know how to solve it?


Starkkz(Posted 2014) [#97]
Well, before anyone else breaks his head trying to figure out how to get the newest LuaJIT working on BlitzMax. I had to place the LuaJIT-2.0.3 folder on BlitzMax/mod/zeke.mod/luajit2.mod/, and change the imports to these:
Import "LuaJIT-2.0.3/src/lib_aux.c"
Import "LuaJIT-2.0.3/src/lib_base.c"
Import "LuaJIT-2.0.3/src/lib_bit.c"
Import "LuaJIT-2.0.3/src/lib_debug.c"
Import "LuaJIT-2.0.3/src/lib_ffi.c"
Import "LuaJIT-2.0.3/src/lib_init.c"
Import "LuaJIT-2.0.3/src/lib_io.c"
Import "LuaJIT-2.0.3/src/lib_jit.c"
Import "LuaJIT-2.0.3/src/lib_math.c"
Import "LuaJIT-2.0.3/src/lib_os.c"
Import "LuaJIT-2.0.3/src/lib_package.c"
Import "LuaJIT-2.0.3/src/lib_string.c"
Import "LuaJIT-2.0.3/src/lib_table.c"
Import "LuaJIT-2.0.3/src/lj_alloc.c"
Import "LuaJIT-2.0.3/src/lj_api.c"
Import "LuaJIT-2.0.3/src/lj_asm.c"
Import "LuaJIT-2.0.3/src/lj_bc.c"
Import "LuaJIT-2.0.3/src/lj_bcread.c"
Import "LuaJIT-2.0.3/src/lj_bcwrite.c"
Import "LuaJIT-2.0.3/src/lj_carith.c"
Import "LuaJIT-2.0.3/src/lj_ccall.c"
Import "LuaJIT-2.0.3/src/lj_ccallback.c"
Import "LuaJIT-2.0.3/src/lj_cconv.c"
Import "LuaJIT-2.0.3/src/lj_cdata.c"
Import "LuaJIT-2.0.3/src/lj_char.c"
Import "LuaJIT-2.0.3/src/lj_clib.c"
Import "LuaJIT-2.0.3/src/lj_cparse.c"
Import "LuaJIT-2.0.3/src/lj_crecord.c"
Import "LuaJIT-2.0.3/src/lj_ctype.c"
Import "LuaJIT-2.0.3/src/lj_debug.c"
Import "LuaJIT-2.0.3/src/lj_dispatch.c"
Import "LuaJIT-2.0.3/src/lj_err.c"
Import "LuaJIT-2.0.3/src/lj_ffrecord.c"
Import "LuaJIT-2.0.3/src/lj_func.c"
Import "LuaJIT-2.0.3/src/lj_gc.c"
Import "LuaJIT-2.0.3/src/lj_gdbjit.c"
Import "LuaJIT-2.0.3/src/lj_ir.c"
Import "LuaJIT-2.0.3/src/lj_lex.c"
Import "LuaJIT-2.0.3/src/lj_lib.c"
Import "LuaJIT-2.0.3/src/lj_load.c"
Import "LuaJIT-2.0.3/src/lj_mcode.c"
Import "LuaJIT-2.0.3/src/lj_meta.c"
Import "LuaJIT-2.0.3/src/lj_obj.c"
Import "LuaJIT-2.0.3/src/lj_opt_dce.c"
Import "LuaJIT-2.0.3/src/lj_opt_fold.c"
Import "LuaJIT-2.0.3/src/lj_opt_loop.c"
Import "LuaJIT-2.0.3/src/lj_opt_mem.c"
Import "LuaJIT-2.0.3/src/lj_opt_narrow.c"
Import "LuaJIT-2.0.3/src/lj_opt_sink.c"
Import "LuaJIT-2.0.3/src/lj_opt_split.c"
Import "LuaJIT-2.0.3/src/lj_parse.c"
Import "LuaJIT-2.0.3/src/lj_record.c"
Import "LuaJIT-2.0.3/src/lj_snap.c"
Import "LuaJIT-2.0.3/src/lj_state.c"
Import "LuaJIT-2.0.3/src/lj_str.c"
Import "LuaJIT-2.0.3/src/lj_strscan.c"
Import "LuaJIT-2.0.3/src/lj_tab.c"
Import "LuaJIT-2.0.3/src/lj_trace.c"
Import "LuaJIT-2.0.3/src/lj_udata.c"
Import "LuaJIT-2.0.3/src/lj_vm.o"
Import "LuaJIT-2.0.3/src/lj_vmevent.c"
Import "LuaJIT-2.0.3/src/lj_vmmath.c"
Import "LuaJIT-2.0.3/src/ljamalg.c"
Import "LuaJIT-2.0.3/src/luajit.c"

It seems to work fine.


Yasha(Posted 2014) [#98]
The simplest and most cross-platform way to handle it is to just replace the import lines with one import "LuaJIT-2.0.3/src/libluajit.a", having run Make as per the LuaJIT documentation (which should mostly be "make CC=gcc -m32"). That way you don't need to import .o or .c files from Max, which is simpler and leaves less up to Max's compiler. Importing .c files from Max means that BlitzMax calls GCC itself rather than using prebuilt binaries, which can be simpler, or it can potentially be messy.


It works when I build it, but when I try to build a program that uses this module I get this.


The sneaky problem here is that it didn't actually work when you built it. When you just build modules (i.e. .o files), the compiler doesn't care if it can't find symbols because it assumes they'll be provided at program link time. Therefore if Import directives are wrong, it won't always notice.


Ferret(Posted 2014) [#99]
I'm new to Lua and have some questions.

How do i expose mi own BlitzMax functions, types and variables to Lua?
Is it possible to extend BlitzMax types in Lua?


PhotonTom(Posted 2014) [#100]
I recommend you use https://github.com/nilium/lugi.mod
This allows you to use your own blitzmax types in lua and before you call a lua function in blitzmax you can also send blitzmax objects/variables to your lua function.
This code although badly commented should help:
lua.bmx
Framework BRL.StandardIO
Import BRL.FileSystem
Import BRL.Blitz
Import BRL.System
Import LuGI.Core
Include "glue.bmx"

'Import LuGI.Generator
'GenerateGlueCode("glue.bmx")
'End

Global testing:TDemo

Type TDemo {expose}

	Field name:String 
	Field oldname:String
	Field mylist:TList

	Method SayHello$( name$ )
		Self.name = name:String 
		Return "Hello "+name+"! Peace be with you..."
	End Method
	
	Method addtolist(text:String)
	
		If mylist=Null Then 
			mylist=CreateList()
		EndIf 
		ListAddLast(mylist,text)
	
	End Method
	
	Method GetSelf:TDemo()
		Return Self
	End Method

End Type

Local source:String = openSource("test.lua")

Local state:Byte Ptr = luaL_newstate()
InitLuGI(state)

luaL_openlibs(state)

'Load our code/compile
Local Result:Int = luaL_loadstring(state,source)
If (Result <> 0) Then
  	' ERROR!!!
	Print "error!"
	lua_close(state) ' just to be complete
 	End
End If

'Call this after loading in lua code, not sure exactly why but you must do it 
lua_pcall(state,1,-1,-1)

'load a type
demo2:Tdemo = New TDemo
demo2.oldname = "oh dear"

'put function onto stack
lua_getfield(state, LUA_GLOBALSINDEX, "test")
'put object onto stack
lua_pushbmaxobject( state, demo2 )
'call function that returns a blitzmax object and takes demo2 object as input
lua_call(state, 1, 1) ' 2nd variable is number of inputs to function, 3rd variable is number of outputs returned to lua stack

'get returned object from stack
obj:Object = lua_tobmaxobject( state, 1 )
demo:TDemo = TDemo(obj)

'clean up stack by removing our returned object from stack
lua_pop(state,1)

Print demo.name
Print demo.oldname

lua_close(state)

Print "list"
For text:String = EachIn demo.mylist
	Print text
Next

Function openSource:String(File:String)
	Local line:String
	Local source:String
	Local luasource:TStream = ReadFile(File)
	Repeat
		line = ReadLine(luasource) 
		source = source + line +"~n~r"
		If Eof(luasource) Then Exit 	
	Forever
	Return source
End Function  


text.lua
function test(demo)
	print( demo:SayHello( 'Fredborg' ) )
	demo:addtolist("hello")
	demo:addtolist("hello2")
	return demo
end


Note that to generate the glue.bmx file you must uncomment the following lines
'Import LuGI.Generator
'GenerateGlueCode("glue.bmx")
'End

and comment the following line
Include "glue.bmx"


For more info on how to use lugi see here https://github.com/nilium/lugi.mod/wiki


Ferret(Posted 2014) [#101]
Thx, its always nice to have a working example.


Starkkz(Posted 2014) [#102]
@Yasha: would've done it, the problem is that Zeke's LuaJIT version is outdated and it doesn't have FFI. I got the latest one from LuaJIT's website, it doesn't have libluajit.a, I don't know how to compile that. It's working on windows, but now that I'm trying to compile my program for Linux I found out that for some reason it fails to compile lj_vm.o.

Can anyone help me on that, please?


GW(Posted 2014) [#103]
Zekes module absolutely has ffi. I've used it for years and the module comes with examples using the ffi.


Starkkz(Posted 2014) [#104]
@GW: Are you sure? I tried to get FFI working but it just told that the module was missing or something like that. But anyways, with that version I'm getting about 15GHz of speed, I get 20GHz with the newer one. I just need to make it run on Linux, for that it might be necessary to compile a new libluajit.a which is what I don't know how to do.


Starkkz(Posted 2014) [#105]
Alright I've managed to compile LuaJIT until a point where the command
ar rcs libluajit.a *.o

prints
ar: *.o: Invalid argument

How should I get libluajit.a if this command does not work?

Edit: I had to compile one by one, each one of the .o files. I got libluajit.a with a size of 540kbs (making sure to tell because I could've missed some file). Compiling Zeke's LuaJIT works until I run my program with the module, after that I'm getting this error:
E:/Programas/BlitzMax/lib/libluajit.a(lib_os.o):lib_os.c:(.text+0x12e): undefined reference to `_difftime64'

What should I do?

Edit2: Finally I got a working libluajit.a for Windows, but for some reason this method does not work on Linux because the BlitzMax linux does not recognize the .a file, I don't know what to do now because it doesn't import .o files.


Schlachtwerk(Posted 2015) [#106]
The Webseite is Offline, no luajit Download :(


GW(Posted 2015) [#107]
I put up a copy of the latest version of the zeke.luajit mod that I could find. http://www.gutterbox.com/download/zeke.mod.zip


Yasha(Posted 2015) [#108]
How should I get libluajit.a if this command does not work?


Why aren't you using LuaJIT's build process?

As above, you should not be compiling these files by hand. Nobody can guess where this stuff is going wrong if you have a non-standard build, because the chances are all sorts of parameters have been set incorrectly as a result of compiling separately. Just use the makefile and libluajit.a will appear in the src folder.


Starkkz(Posted 2015) [#109]
I had to do magic in order to build it, but at least it works for Linux and Windows (I haven't compiled it on MacOS yet).

http://www.mediafire.com/download/9anu685vphaqyw5/luajit.zip
I added some other modules I needed as LuaSocket, Extension Proposal, Lua File System, LuaLanes and MD5.

If you ever decide to use one of these, you may call the functions this way.
luaopen_socket_core(L) ' LuaSocket
luaopen_mime_core(L) ' Mime is included in LuaSocket
luaopen_md5_core(L) ' MD5
luaL_requiref(L, "ex", luaopen_ex, 0) ' Extension Proposal
luaL_requiref(L, "lfs", luaopen_lfs, 0) ' Lua File System
luaL_requiref(L, "lanes.core", luaopen_lanes_core, 0) ' Lanes core (it requires lanes.lua)


@Yasha: I did not understand a single word.


Yasha(Posted 2015) [#110]
@Yasha: I did not understand a single word.


To build LuaJIT, you open a command prompt and type `make`. That's literally all, on every OS. There should be no need to make it more complicated than that.

I'm therefore wondering what obstacle you are encountering that's making it impossible to use the normal method?


Starkkz(Posted 2015) [#111]
@Yasha: the "make" method just generates .o files, I've got to make the libluajit.a by myself. MinGW's ar does not work for this, I had to use BlitzMax's ar. When I load libluajit.a from BlitzMax on Windows it misses some clock functions, basically loading every .c file and lj_vm.o works better for me. Actually it's the only way that works for me and I prefer not to change it rather than breaking something.


Yasha(Posted 2015) [#112]
Make would be rather useless if all it could do was generate .o files...

OK so apparently you need to use the secondary makefile for Windows targets. It does generate libraries; if it doesn't, something is broken on your system. You might need to change BUILDMODE from mixed to static, can't tell from here.


I still recommend that you do change it, because your build is already broken, that much is clear; and hand-crafting this stuff is hardly a future-proof option. This issue is even called out in the documentation:

If you're a package maintainer for a distribution, please make use of these features and avoid patching, subverting, autotoolizing or messing up the build system in unspeakable ways.

There should be absolutely no need to patch luaconf.h or any of the Makefiles. And please do not hand-pick files for your packages — simply use whatever make install creates. There's a reason for all of the files and directories it creates.



Hummelpups(Posted 2015) [#113]
anyone that uses this module on mac?

i dont know the exact error yet because im at work

there seems to be something missing for ld.exe?
Import "-lmingw32"
Import "-lgcc"

i will post the rror in a few hours and edit that