Skip to content

Commit

Permalink
Support Lua 5.2
Browse files Browse the repository at this point in the history
  • Loading branch information
jmaslak committed Apr 16, 2016
1 parent f8f3f22 commit 5f8ab2a
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 6 deletions.
26 changes: 26 additions & 0 deletions Lua.xs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <stdio.h>
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
Expand All @@ -14,9 +15,17 @@
#include <lualib.h>
#include <lauxlib.h>

/* Support Lua 5.2 */
#if LUA_VERSION_NUM >= 502
#define lua_open() luaL_newstate()
#define lua_strlen(L,i) lua_rawlen(L, (i))
#endif

SV *UNDEF, *LuaNil, NIL;
AV *INLINE_RETURN;

int close_attempt (lua_State *);

void push_ary (lua_State *, AV*);
void push_hash (lua_State *, HV*);
void push_val (lua_State *, SV*);
Expand Down Expand Up @@ -153,12 +162,29 @@ push_func (lua_State *L, CV *cv) {
* along with the appropriate metatable onto the Lua stack */
void
push_io (lua_State *L, PerlIO *pio) {
#if LUA_VERSION_NUM < 502
FILE **fp = (FILE**)lua_newuserdata(L, sizeof(FILE*));
*fp = PerlIO_exportFILE(pio, NULL);
luaL_getmetatable(L, "FILE*");
lua_setmetatable(L, -2);
#else
// Lua 5.2+
// We need a close function or Lua thinks the file handle is closed
luaL_Stream *p = (luaL_Stream *)lua_newuserdata(L, sizeof(luaL_Stream));
p->f = PerlIO_exportFILE(pio, NULL);
p->closef = &close_attempt;
luaL_setmetatable(L, LUA_FILEHANDLE);
#endif
}

/* Called when Lua >= 5.2 closes a Perl filehandle - We don't currently
* do anything with this. So we end up leaking :stdio layers.
*/
int close_attempt(lua_State *L) {
return 0;
}


/* push a generic reference into the Lua stack:
* calls one of push_(ary|hash|func|io) */
void
Expand Down
2 changes: 2 additions & 0 deletions Makefile.PL
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ use Config;
my @candidates = (
'lua-config',
'pkg-config lua',
'pkg-config lua5.2',
'pkg-config lua-5.2',
'pkg-config lua5.1',
'pkg-config lua-5.1',
);
Expand Down
6 changes: 3 additions & 3 deletions check.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
#include <lualib.h>
#include <lauxlib.h>

#if LUA_VERSION_NUM != 501
# error "Lua 5.1 required"
#if (LUA_VERSION_NUM < 501) || (LUA_VERSION_NUM > 502)
# error "Lua 5.1 or 5.2 required"
#endif

int main (void) {
void *p = (void *) lua_getfenv;
void *p = (void *) lua_getfield;
return 0;
}
3 changes: 2 additions & 1 deletion t/1_basic.t
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ end
function take_any (...)
local a = ''
for i = 1, table.getn(arg), 1 do
local arg={...}
for i = 1, #arg, 1 do
a = a..arg[i]
end
return a
Expand Down
5 changes: 3 additions & 2 deletions t/4_filehandle.t
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ is_deeply([<$fh>], ["foo\n", "bar\n", "baz\n"]);
__END__
__Lua__
function write_file (fh, ...)
for i = 1, table.getn(arg), 1 do
fh:write(arg[i], "\n")
local arg={...}
for i,v in ipairs(arg) do
fh:write(v, "\n")
end
end

Expand Down

0 comments on commit 5f8ab2a

Please sign in to comment.