From 83eabf083b695a296556b35b30be052afd76ee0c Mon Sep 17 00:00:00 2001 From: mitchell <70453897+667e-11@users.noreply.github.com> Date: Thu, 15 Dec 2011 11:16:50 -0500 Subject: Add support for compiling with LuaJIT. --- core/compat.lua | 19 +++++++++++++++++++ core/init.lua | 1 + doc/manual/12_Compiling.md | 18 ++++++++++++++++++ src/Makefile | 41 +++++++++++++++++++++++++++++++++++------ src/lua.sym | 9 +++++++++ src/textadept.c | 12 +++++++++++- 6 files changed, 93 insertions(+), 7 deletions(-) create mode 100644 core/compat.lua diff --git a/core/compat.lua b/core/compat.lua new file mode 100644 index 00000000..245233b3 --- /dev/null +++ b/core/compat.lua @@ -0,0 +1,19 @@ +-- Copyright 2007-2011 Mitchell mitchellcaladbolg.net. See LICENSE. + +-- When using LuaJIT try to retain backwards compatibility (Lua 5.1). +-- LuaJIT is compiled with LUAJIT_ENABLE_LUA52COMPAT for some Lua 5.2 features. + +-- In Lua 5.1, `xpcall` did not accept function arguments. +local xpcall51 = xpcall +function xpcall(f, error, ...) + local args = {...} + return xpcall51(function() f(unpack(args)) end, error) +end + +-- In Lua 5.1, `load` did not take mode and environment parameters. +local load51 = load +function load(ld, source, mode, env) + local f, err = load51(ld, source) + if f and env then return setfenv(f, env) end + return f, err +end diff --git a/core/init.lua b/core/init.lua index 18077169..cd1c9546 100644 --- a/core/init.lua +++ b/core/init.lua @@ -5,6 +5,7 @@ _RELEASE = "Textadept 4.3" package.path = _HOME..'/core/?.lua;'..package.path os.setlocale('C', 'collate') +if jit then require 'compat' end _SCINTILLA = require 'iface' args = require 'args' locale = require 'locale' diff --git a/doc/manual/12_Compiling.md b/doc/manual/12_Compiling.md index cf1e83c5..3a8b5043 100644 --- a/doc/manual/12_Compiling.md +++ b/doc/manual/12_Compiling.md @@ -79,3 +79,21 @@ lines (put `//` at the start of the line): #define CharacterRange Sci_CharacterRange #define TextRange Sci_TextRange #define TextToFind Sci_TextToFind + +#### Compiling with LuaJIT + +[LuaJIT](http://luajit.org) is a Just-In-Time Compiler for Lua and can boost the +speed of Lua programs. I have noticed that syntax highlighting can be up to 2 +times faster with LuaJIT than with vanilla Lua. This difference is largely +unnoticable on modern computers and usually only discernable when initially +loading large files. Other than syntax highlighting, LuaJIT offers no real +benefit performance-wise to justify it being Textadept's default runtime. +LuaJIT's [ffi library](http://luajit.org/ext_ffi.html), however, appears to be +useful for interfacing with external, non-Lua, libraries. + +You can compile Textadept with LuaJIT by running `make LUAJIT=1` for non-Windows +systems and `make WIN32=1 LUAJIT=1` for Windows systems. + +Please note that a `lua51.dll` is produced for Windows platforms because +limitations on external Lua library loading do not allow statically linking +LuaJIT to Textadept. Static linking occurs on all other platforms. diff --git a/src/Makefile b/src/Makefile index 333fb838..2870818a 100644 --- a/src/Makefile +++ b/src/Makefile @@ -19,6 +19,8 @@ TEXTADEPT_RC = EXPORTLUASYMS = -rdynamic -Wl,--retain-symbols-file -Wl,lua.sym WINDRES = LDL = -ldl +LUAJIT_LIB = libluajit.a +LUAJIT_MAKE = # Win32 (WIN32=1) ifdef WIN32 @@ -34,6 +36,8 @@ TEXTADEPT_RC = textadept_rc.o EXPORTLUASYMS = -Wl,--retain-symbols-file -Wl,lua.sym WINDRES = i486-mingw32-windres LDL = +LUAJIT_LIB = lua51.dll +LUAJIT_MAKE = HOST_CC="gcc -m32" CROSS=i486-mingw32- TARGET_SYS=Windows endif # Mac OSX (OSX=1) @@ -52,14 +56,28 @@ TEXTADEPT_RC = EXPORTLUASYMS = -rdynamic WINDRES = LDL = +LUAJIT_LIB = libluajit.a +LUAJIT_MAKE = endif +# No debugging unless DEBUG=1. ifndef DEBUG DEBUG_FLAG = -DNDEBUG else DEBUG_FLAG = -DDEBUG -DTRACE -g endif -INCLUDEDIRS = -Iscintilla/include -Ilua/src -Igcocoadialog + +# Do not compile with LuaJIT unless LUAJIT=1. +ifndef LUAJIT +LUADIR = lua +LUAJIT_FLAG = +LUAJIT_LIB = +else +LUADIR = luajit +LUAJIT_FLAG = -DLUAJIT +endif + +INCLUDEDIRS = -Iscintilla/include -I$(LUADIR)/src -Igcocoadialog ifdef GTK3 GTKVERSION = gtk+-3.0 @@ -69,8 +87,8 @@ endif # Textadept -CFLAGS = -std=c99 $(DEBUG_FLAG) -O $(PLAT_FLAGS) $(INCLUDEDIRS) -W -Wall \ - -Wno-sign-compare -Wno-unused +CFLAGS = -std=c99 $(DEBUG_FLAG) $(LUAJIT_FLAG) -O $(PLAT_FLAGS) $(INCLUDEDIRS) \ + -W -Wall -Wno-sign-compare -Wno-unused GTKFLAGS = $(shell PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) $(PKG_CONFIG) \ --cflags $(GTKVERSION)) GTKLIBS = $(shell PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) $(PKG_CONFIG) \ @@ -89,6 +107,13 @@ LUA_OBJS = lapi.o lcode.o lctype.o ldebug.o ldo.o ldump.o lfunc.o lgc.o \ lauxlib.o lbaselib.o lbitlib.o lcorolib.o ldblib.o liolib.o lmathlib.o \ loadlib.o loslib.o ltablib.o lstrlib.o \ lpeg.o lfs.o +LUA_SRCS = lua/src/*.c lua/src/lib/*.c +LUAJIT_OBJS = +ifdef LUAJIT +LUA_OBJS = lpeg.o lfs.o +LUA_SRCS = lua/src/lib/lpeg.c lua/src/lib/lfs.c +LUAJIT_OBJS = $(LUAJIT_LIB) +endif GCOCOADIALOG = gcocoadialog.o # Scintilla @@ -122,20 +147,23 @@ $(SCINTILLA_LEXER): LexLPeg.cxx $(CPP) $(SCI_CXXFLAGS) $(GTKFLAGS) $(LUA_CFLAGS) -DLPEG_LEXER -DNO_SCITE -c $^ $(TEXTADEPT_OBJS): *.c $(CC) $(CFLAGS) $(GTKFLAGS) -c $^ -$(LUA_OBJS): lua/src/*.c +$(LUA_OBJS): $(LUA_SRCS) $(CC) $(LUA_CFLAGS) $(INCLUDEDIRS) -c $^ +$(LUAJIT_OBJS): + cd luajit && make $(LUAJIT_MAKE) + cp luajit/src/$(LUAJIT_LIB) . $(GCOCOADIALOG): gcocoadialog/gcocoadialog.c $(CC) $(GTKFLAGS) $(INCLUDEDIRS) -c $^ $(TEXTADEPT):\ $(SCINTILLA_OBJS) $(SCINTILLA_MARSHALLER) $(SCINTILLA_LEXER) \ - $(TEXTADEPT_OBJS) $(LUA_OBJS) $(GCOCOADIALOG) \ + $(TEXTADEPT_OBJS) $(LUA_OBJS) $(LUAJIT_OBJS) $(GCOCOADIALOG) \ $(TEXTADEPT_RC) $(CPP) $(EXPORTLUASYMS) -o $@ $^ $(GTKLIBS) $(LDL) mv $(TEXTADEPT) ../ $(TEXTADEPT_RC): textadept.rc $(WINDRES) $^ $@ clean: - rm ../$(TEXTADEPT) *.o + rm -f ../$(TEXTADEPT) *.o *.a *.dll # Package (only for Linux x86_64) # Pass 'VERSION=[release version]' to 'make'. @@ -177,6 +205,7 @@ release: ../$(TEXTADEPT) ../$(TEXTADEPT32) ../$(TEXTADEPTWIN32) \ # Win32 cp -r $(RELEASEDIR32) $(RELEASEDIRWIN32) cp ../$(TEXTADEPTWIN32) $(RELEASEDIRWIN32) + if [ -f lua51.dll ]; then cp lua51.dll $(RELEASEDIRWIN32); fi cp win32gtk/bin/*.dll $(RELEASEDIRWIN32) cp -r win32gtk/{etc,lib,share} $(RELEASEDIRWIN32) rm -r $(RELEASEDIRWIN32)/lib/{*.a,glib-2.0,gtk-2.0/include,pkgconfig} diff --git a/src/lua.sym b/src/lua.sym index a2ce7548..5398a9c2 100644 --- a/src/lua.sym +++ b/src/lua.sym @@ -1,3 +1,4 @@ +# This file has Lua 5.2 symbols and Lua 5.1 symbols (for LuaJIT). luaL_addlstring luaL_addstring luaL_addvalue @@ -39,6 +40,8 @@ luaL_prepbuffersize luaL_pushresult luaL_pushresultsize luaL_ref +luaL_register +luaL_typerror luaL_requiref luaL_setfuncs luaL_setmetatable @@ -55,13 +58,16 @@ lua_callk lua_checkstack lua_close lua_concat +lua_cpcall lua_copy lua_createtable lua_dump +lua_equal lua_error lua_gc lua_getallocf lua_getctx +lua_getfenv lua_getfield lua_gethook lua_gethookcount @@ -81,11 +87,13 @@ lua_isnumber lua_isstring lua_isuserdata lua_len +lua_lessthan lua_load lua_newstate lua_newthread lua_newuserdata lua_next +lua_objlen lua_pcall lua_pcallk lua_pushboolean @@ -110,6 +118,7 @@ lua_remove lua_replace lua_resume lua_setallocf +lua_setfenv lua_setfield lua_sethook lua_setlevel diff --git a/src/textadept.c b/src/textadept.c index f828e8ae..e6b3cb10 100644 --- a/src/textadept.c +++ b/src/textadept.c @@ -731,8 +731,18 @@ static gboolean c_keypress(GtkWidget*_, GdkEventKey *event, gpointer __) { /******************************** Lua Interface *******************************/ /******************************************************************************/ +#if LUAJIT +#define LUA_RIDX_GLOBALS LUA_GLOBALSINDEX +#define LUA_OK 0 +#define lua_rawlen lua_objlen +#define LUA_OPEQ 0 +#define lua_compare(l, a, b, _) lua_equal(l, a, b) #define lL_openlib(l, n, f) \ - (luaL_requiref(l, n, f, 1), lua_pop(l, 1)) + (lua_pushcfunction(l, f), lua_pushstring(l, n), lua_call(l, 1, 0)) +#else +#define lL_openlib(l, n, f) (luaL_requiref(l, n, f, 1), lua_pop(l, 1)) +#endif + #define l_setcfunction(l, n, k, f) \ (lua_pushcfunction(l, f), lua_setfield(l, (n > 0) ? n : n - 1, k)) #define l_setmetatable(l, n, k, i, ni) { \ -- cgit v1.2.3