Skip to content

Commit

Permalink
fix code for lua5.3
Browse files Browse the repository at this point in the history
  • Loading branch information
pangweiwei committed Mar 27, 2015
1 parent 0645730 commit 75f2ece
Showing 1 changed file with 51 additions and 38 deletions.
89 changes: 51 additions & 38 deletions Assets/Slua/slua_src/slua.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* This file is based off of MonoLuaInterface's wrapping: https://github.com/stevedonovan/MonoLuaInterface
*/

Expand All @@ -12,34 +12,34 @@
#ifndef _WIN32
#define __stdcall
#endif
typedef int (__stdcall *lua_stdcallCFunction) (lua_State *L);
typedef int(__stdcall *lua_stdcallCFunction) (lua_State *L);

static int stdcall_closure(lua_State *L)
static int stdcall_closure(lua_State *L)
{
lua_stdcallCFunction fn = (lua_stdcallCFunction)lua_touserdata(L, lua_upvalueindex(1));
int r = fn(L);
return r;
lua_stdcallCFunction fn = (lua_stdcallCFunction)lua_touserdata(L, lua_upvalueindex(1));
int r = fn(L);
return r;
}


LUA_API void lua_pushstdcallcfunction(lua_State *L, lua_stdcallCFunction fn)
{
lua_pushlightuserdata(L, fn);
lua_pushcclosure(L, stdcall_closure, 1);
lua_pushlightuserdata(L, fn);
lua_pushcclosure(L, stdcall_closure, 1);
}


LUA_API void luaS_newuserdata(lua_State *L, int val)
{
int* pointer=(int*)lua_newuserdata(L,sizeof(int));
*pointer=val;
int* pointer = (int*)lua_newuserdata(L, sizeof(int));
*pointer = val;
}

LUA_API int luaS_rawnetobj(lua_State *L,int index)
LUA_API int luaS_rawnetobj(lua_State *L, int index)
{
int *udata=lua_touserdata(L,index);
if(udata!=NULL) return *udata;
return -1;
int *udata = lua_touserdata(L, index);
if (udata != NULL) return *udata;
return -1;
}

// The MIT License (MIT)
Expand Down Expand Up @@ -93,18 +93,18 @@ static int s_closure(lua_State *L)
}


LUA_API void luaS_pushcclosure(lua_State *L, lua_CFunction func,int n) {
LUA_API void luaS_pushcclosure(lua_State *L, lua_CFunction func, int n) {
if (func != NULL) {
lua_pushlightuserdata(L, func);
lua_pushcclosure(L, s_closure, n + 1);
}
}

LUA_API const char* luaS_tolstring32(lua_State *L, int index, int* len) {
size_t l;
const char* ret = lua_tolstring(L,index, &l);
*len = (int)l;
return ret;
size_t l;
const char* ret = lua_tolstring(L, index, &l);
*len = (int)l;
return ret;
}

static lua_stdcallCFunction panicf = NULL;
Expand All @@ -120,17 +120,17 @@ LUA_API void luaS_atpanic(lua_State *L, lua_stdcallCFunction f) {
}

#if LUA_VERSION_NUM>=503
static int k (lua_State *L, int status, lua_KContext ctx) {
static int k(lua_State *L, int status, lua_KContext ctx) {
return status;
}

LUA_API int luaS_yield(lua_State *L,int nrets) {
int ret = lua_yieldk(L,nrets,0,k);
LUA_API int luaS_yield(lua_State *L, int nrets) {
int ret = lua_yieldk(L, nrets, 0, k);
return ret;
}

LUA_API int luaS_pcall(lua_State *L,int nargs,int nresults,int err) {
return k(L,lua_pcallk(L,nargs,nresults,err,0,k),0);
LUA_API int luaS_pcall(lua_State *L, int nargs, int nresults, int err) {
return k(L, lua_pcallk(L, nargs, nresults, err, 0, k), 0);
}
#endif

Expand All @@ -144,20 +144,25 @@ static void getmetatable(lua_State *L, const char* key) {
snprintf(ns, 256, "UnityEngine.%s.Instance", key);
#endif

lua_getfield(L,LUA_REGISTRYINDEX, ns);
lua_getfield(L, LUA_REGISTRYINDEX, ns);
}

static void setmetatable(lua_State *L, int p, int what) {

int ref;

#if LUA_VERSION_NUM>=503
lua_pushglobaltable(L);
lua_rawgeti(L,-1,what);
lua_remove(L, -2);
#else
lua_rawgeti(L, LUA_GLOBALSINDEX, what);
#endif
if (!lua_isnil(L, -1)) {
ref = (int)lua_tointeger(L, -1);
lua_pop(L, 1);
if (ref != LUA_REFNIL)
{
lua_getref(L, ref);
lua_rawgeti(L, LUA_REGISTRYINDEX, ref);
}
}
else {
Expand All @@ -180,8 +185,16 @@ static void setmetatable(lua_State *L, int p, int what) {

lua_pushvalue(L, -1);
ref = luaL_ref(L, LUA_REGISTRYINDEX);

#if LUA_VERSION_NUM>=503
lua_pushglobaltable(L);
lua_pushinteger(L, ref);
lua_rawseti(L, -2, what);
lua_pop(L,1);
#else
lua_pushinteger(L, ref);
lua_rawseti(L, LUA_GLOBALSINDEX, what);
#endif
}

lua_setmetatable(L, p);
Expand All @@ -206,13 +219,13 @@ LUA_API int luaS_checkluatype(lua_State *L, int p, const char *t) {
lua_settop(L, top);
return 0;
}
b=lua_tostring(L, -1);
b = lua_tostring(L, -1);
lua_settop(L, top);
return strcmp(t, b)==0;
return strcmp(t, b) == 0;
}


LUA_API void luaS_checkVector4(lua_State *L, int p, float* x, float *y, float *z,float *w) {
LUA_API void luaS_checkVector4(lua_State *L, int p, float* x, float *y, float *z, float *w) {
luaL_checktype(L, p, LUA_TTABLE);
lua_getfield(L, p, "x");
*x = (float)lua_tonumber(L, -1);
Expand All @@ -225,7 +238,7 @@ LUA_API void luaS_checkVector4(lua_State *L, int p, float* x, float *y, float *z
lua_pop(L, 4);
}

LUA_API void luaS_pushVector4(lua_State *L, float x, float y, float z,float w) {
LUA_API void luaS_pushVector4(lua_State *L, float x, float y, float z, float w) {
lua_newtable(L);
lua_pushnumber(L, x);
lua_setfield(L, -2, "x");
Expand Down Expand Up @@ -295,7 +308,7 @@ LUA_API void luaS_checkQuaternion(lua_State *L, int p, float* x, float *y, float
lua_pop(L, 4);
}

LUA_API void luaS_pushQuaternion(lua_State *L, float x, float y, float z,float w) {
LUA_API void luaS_pushQuaternion(lua_State *L, float x, float y, float z, float w) {
lua_newtable(L);
lua_pushnumber(L, x);
lua_setfield(L, -2, "x");
Expand All @@ -320,21 +333,21 @@ static void setelement(lua_State* L, int p, float v, const char* key) {
}


LUA_API void luaS_setData(lua_State *L,int p, float x, float y, float z, float w) {
LUA_API void luaS_setData(lua_State *L, int p, float x, float y, float z, float w) {
setelement(L, p, x, "x");
setelement(L, p, y, "y");
setelement(L, p, z, "z");
setelement(L, p, w, "w");
}

static void cacheud(lua_State *l, int index, int cref) {
lua_getref(l, cref);
lua_rawgeti(l,LUA_REGISTRYINDEX,cref);
lua_pushvalue(l, -2);
lua_rawseti(l, -2, index);
lua_pop(l, 1);
}

LUA_API void luaS_pushobject(lua_State *l,int index,const char* t,int gco,int cref) {
LUA_API void luaS_pushobject(lua_State *l, int index, const char* t, int gco, int cref) {
luaS_newuserdata(l, index);
if (gco) cacheud(l, index, cref);

Expand All @@ -349,8 +362,8 @@ LUA_API void luaS_pushobject(lua_State *l,int index,const char* t,int gco,int cr
lua_setmetatable(l, -2);
}

LUA_API int luaS_getcacheud(lua_State *l,int index,int cref) {
lua_getref(l, cref);
LUA_API int luaS_getcacheud(lua_State *l, int index, int cref) {
lua_rawgeti(l, LUA_REGISTRYINDEX, cref);
lua_rawgeti(l, -1, index);
if (!lua_isnil(l, -1))
{
Expand Down

0 comments on commit 75f2ece

Please sign in to comment.