Skip to content

Commit

Permalink
issue #119
Browse files Browse the repository at this point in the history
  • Loading branch information
Dibyendu Majumdar committed Dec 30, 2016
1 parent 1cecb6d commit 0f089b6
Show file tree
Hide file tree
Showing 30 changed files with 272 additions and 198 deletions.
10 changes: 9 additions & 1 deletion include/lauxlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,18 @@



/* extra error code for 'luaL_load' */
/* extra error code for 'luaL_loadfilex' */
#define LUA_ERRFILE (LUA_ERRERR+1)


/* key, in the registry, for table of loaded modules */
#define LUA_LOADED_TABLE "_LOADED"


/* key, in the registry, for table of preloaded loaders */
#define LUA_PRELOAD_TABLE "_PRELOAD"


typedef struct luaL_Reg {
const char *name;
lua_CFunction func;
Expand Down
7 changes: 4 additions & 3 deletions include/lstate.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
**
** 'allgc': all objects not marked for finalization;
** 'finobj': all objects marked for finalization;
** 'tobefnz': all objects ready to be finalized;
** 'tobefnz': all objects ready to be finalized;
** 'fixedgc': all objects that are not to be collected (currently
** only small strings, such as reserved words).
Expand All @@ -34,7 +34,7 @@ struct lua_longjmp; /* defined in ldo.c */


/*
** Atomic type (relative to signals) to better ensure that 'lua_sethook'
** Atomic type (relative to signals) to better ensure that 'lua_sethook'
** is thread safe
*/
#if !defined(l_signalT)
Expand Down Expand Up @@ -66,7 +66,7 @@ typedef struct stringtable {
** Information about a call.
** When a thread yields, 'func' is adjusted to pretend that the
** top function has only the yielded values in its stack; in that
** case, the actual 'func' value is saved in field 'extra'.
** case, the actual 'func' value is saved in field 'extra'.
** When a function calls another with a continuation, 'extra' keeps
** the function index so that, in case of errors, the continuation
** function can be called with the correct top.
Expand Down Expand Up @@ -106,6 +106,7 @@ typedef struct CallInfo {
#define CIST_TAIL (1<<5) /* call was tail called */
#define CIST_HOOKYIELD (1<<6) /* last hook called yielded */
#define CIST_LEQ (1<<7) /* using __lt for __le */
#define CIST_FIN (1<<8) /* call is running a finalizer */

#define isLua(ci) ((ci)->callstatus & CIST_LUA)
#define isJITed(ci) ((ci)->jitstatus)
Expand Down
13 changes: 11 additions & 2 deletions include/ltable.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@

#include "lobject.h"


#define gnode(t,i) (&(t)->node[i])
#define gval(n) (&(n)->i_val)
#define gnext(n) ((n)->i_key.nk.next)


/* 'const' to avoid wrong writings that can mess up field 'next' */
/* 'const' to avoid wrong writings that can mess up field 'next' */
#define gkey(n) cast(const TValue*, (&(n)->i_key.tvk))

/*
Expand All @@ -26,6 +27,14 @@
#define invalidateTMcache(t) ((t)->flags = 0)


/* true when 't' is using 'dummynode' as its hash part */
#define isdummy(t) ((t)->lastfree == NULL)


/* allocated size for hash nodes */
#define allocsizenode(t) (isdummy(t) ? 0 : sizenode(t))


/* returns the key, given the value of a table entry */
#define keyfromval(v) \
(gkey(cast(Node *, cast(char *, (v)) - offsetof(Node, i_val))))
Expand Down Expand Up @@ -178,7 +187,7 @@ LUAI_FUNC void raviH_get_integer_array_rawdata(lua_State *L, Table *t, lua_Integ

#if defined(LUA_DEBUG)
LUAI_FUNC Node *luaH_mainposition (const Table *t, const TValue *key);
LUAI_FUNC int luaH_isdummy (Node *n);
LUAI_FUNC int luaH_isdummy (const Table *t);
#endif


Expand Down
6 changes: 3 additions & 3 deletions include/lua.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
#define LUA_VERSION_MAJOR "5"
#define LUA_VERSION_MINOR "3"
#define LUA_VERSION_NUM 503
#define LUA_VERSION_RELEASE "3"
#define LUA_VERSION_RELEASE "4"

#define LUA_VERSION "Ravi " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR
#define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE
#define LUA_COPYRIGHT LUA_RELEASE "\nCopyright (C) 1994-2016 Lua.org, PUC-Rio\nPortions Copyright (C) 2015-2016 Dibyendu Majumdar"
#define LUA_COPYRIGHT LUA_RELEASE "\nCopyright (C) 1994-2017 Lua.org, PUC-Rio\nPortions Copyright (C) 2015-2016 Dibyendu Majumdar"
#define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo, W. Celes, Dibyendu Majumdar"


Expand Down Expand Up @@ -550,7 +550,7 @@ LUA_API void ravi_set_debuglevel(int level);


/******************************************************************************
* Copyright (C) 1994-2016 Lua.org, PUC-Rio.
* Copyright (C) 1994-2017 Lua.org, PUC-Rio.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
Expand Down
16 changes: 10 additions & 6 deletions include/luaconf.h
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@

/*
@@ LUA_NUMBER is the floating-point type used by Lua.
@@ LUAI_UACNUMBER is the result of an 'usual argument conversion'
@@ LUAI_UACNUMBER is the result of a 'default argument promotion'
@@ over a floating number.
@@ l_mathlim(x) corrects limit name 'x' to the proper float type
** by prefixing it with one of FLT/DBL/LDBL.
Expand All @@ -433,7 +433,8 @@

#define l_floor(x) (l_mathop(floor)(x))

#define lua_number2str(s,sz,n) l_sprintf((s), sz, LUA_NUMBER_FMT, (n))
#define lua_number2str(s,sz,n) \
l_sprintf((s), sz, LUA_NUMBER_FMT, (LUAI_UACNUMBER)(n))

/*
@@ lua_numbertointeger converts a float number to an integer, or
Expand Down Expand Up @@ -510,7 +511,7 @@
**
@@ LUA_UNSIGNED is the unsigned version of LUA_INTEGER.
**
@@ LUAI_UACINT is the result of an 'usual argument conversion'
@@ LUAI_UACINT is the result of a 'default argument promotion'
@@ over a lUA_INTEGER.
@@ LUA_INTEGER_FRMLEN is the length modifier for reading/writing integers.
@@ LUA_INTEGER_FMT is the format for writing integers.
Expand All @@ -523,10 +524,12 @@
/* The following definitions are good for most cases here */

#define LUA_INTEGER_FMT "%" LUA_INTEGER_FRMLEN "d"
#define lua_integer2str(s,sz,n) l_sprintf((s), sz, LUA_INTEGER_FMT, (n))

#define LUAI_UACINT LUA_INTEGER

#define lua_integer2str(s,sz,n) \
l_sprintf((s), sz, LUA_INTEGER_FMT, (LUAI_UACINT)(n))

/*
** use LUAI_UACINT here to avoid problems with promotions (which
** can turn a comparison between unsigneds into a signed comparison)
Expand Down Expand Up @@ -618,13 +621,14 @@


/*
@@ lua_number2strx converts a float to an hexadecimal numeric string.
@@ lua_number2strx converts a float to an hexadecimal numeric string.
** In C99, 'sprintf' (with format specifiers '%a'/'%A') does that.
** Otherwise, you can leave 'lua_number2strx' undefined and Lua will
** provide its own implementation.
*/
#if !defined(LUA_USE_C89)
#define lua_number2strx(L,b,sz,f,n) ((void)L, l_sprintf(b,sz,f,n))
#define lua_number2strx(L,b,sz,f,n) \
((void)L, l_sprintf(b,sz,f,(LUAI_UACNUMBER)(n)))
#endif


Expand Down
2 changes: 1 addition & 1 deletion lua-tests/events.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ assert(getmetatable(nil) == nil)
a={}; setmetatable(a, {__metatable = "xuxu",
__tostring=function(x) return x.name end})
assert(getmetatable(a) == "xuxu")
assert(tostring(a) == nil)
--assert(tostring(a) == nil)
-- cannot change a protected metatable
assert(pcall(setmetatable, a, {}) == false)
a.name = "gororoba"
Expand Down
2 changes: 1 addition & 1 deletion ravi-tests/ravi_tests1.ravi
Original file line number Diff line number Diff line change
Expand Up @@ -1197,7 +1197,7 @@ function event_test()
setmetatable(a, {__metatable = "xuxu",
__tostring=function(x: table) return x.name end})
assert(getmetatable(a) == "xuxu")
assert(tostring(a) == nil)
--assert(tostring(a) == nil)
-- cannot change a protected metatable
assert(pcall(setmetatable, a, {}) == false)
a.name = "gororoba"
Expand Down
12 changes: 8 additions & 4 deletions src/lapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

/*
** Portions Copyright (C) 2015-2016 Dibyendu Majumdar
** Portions Copyright (C) 2015-2017 Dibyendu Majumdar
*/


Expand Down Expand Up @@ -999,7 +999,8 @@ LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
auxsetstr(L, index2addr(L, idx), k);
}

LUA_API void lua_seti(lua_State *L, int idx, lua_Integer n) {

LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) {
StkId t;
const TValue *slot;
lua_lock(L);
Expand Down Expand Up @@ -1045,7 +1046,8 @@ LUA_API void lua_seti(lua_State *L, int idx, lua_Integer n) {
lua_unlock(L);
}

LUA_API void lua_rawset(lua_State *L, int idx) {

LUA_API void lua_rawset (lua_State *L, int idx) {
StkId o;
TValue *slot;
Table *t;
Expand Down Expand Up @@ -1099,7 +1101,8 @@ LUA_API void lua_rawset(lua_State *L, int idx) {
lua_unlock(L);
}

LUA_API void lua_rawseti(lua_State *L, int idx, lua_Integer n) {

LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) {
StkId o;
Table *t;
lua_lock(L);
Expand Down Expand Up @@ -1138,6 +1141,7 @@ LUA_API void lua_rawseti(lua_State *L, int idx, lua_Integer n) {
lua_unlock(L);
}


LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) {
StkId o;
Table *t;
Expand Down
44 changes: 26 additions & 18 deletions src/lauxlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,11 @@ static int findfield (lua_State *L, int objidx, int level) {

/*
** Search for a name for a function in all loaded modules
** (registry._LOADED).
*/
static int pushglobalfuncname (lua_State *L, lua_Debug *ar) {
int top = lua_gettop(L);
lua_getinfo(L, "f", ar); /* push function */
lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
lua_getfield(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE);
if (findfield(L, top + 1, 2)) {
const char *name = lua_tostring(L, -1);
if (strncmp(name, "_G.", 3) == 0) { /* name start with '_G.'? */
Expand Down Expand Up @@ -809,13 +808,17 @@ LUALIB_API lua_Integer luaL_len (lua_State *L, int idx) {


LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) {
if (!luaL_callmeta(L, idx, "__tostring")) { /* no metafield? */
if (luaL_callmeta(L, idx, "__tostring")) { /* metafield? */
if (!lua_isstring(L, -1))
luaL_error(L, "'__tostring' must return a string");
}
else {
switch (lua_type(L, idx)) {
case LUA_TNUMBER: {
if (lua_isinteger(L, idx))
lua_pushfstring(L, "%I", lua_tointeger(L, idx));
lua_pushfstring(L, "%I", (LUAI_UACINT)lua_tointeger(L, idx));
else
lua_pushfstring(L, "%f", lua_tonumber(L, idx));
lua_pushfstring(L, "%f", (LUAI_UACNUMBER)lua_tonumber(L, idx));
break;
}
case LUA_TSTRING:
Expand All @@ -827,10 +830,15 @@ LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) {
case LUA_TNIL:
lua_pushliteral(L, "nil");
break;
default:
lua_pushfstring(L, "%s: %p", luaL_typename(L, idx),
lua_topointer(L, idx));
default: {
int tt = luaL_getmetafield(L, idx, "__name"); /* try name */
const char *kind = (tt == LUA_TSTRING) ? lua_tostring(L, -1) :
luaL_typename(L, idx);
lua_pushfstring(L, "%s: %p", kind, lua_topointer(L, idx));
if (tt != LUA_TNIL)
lua_remove(L, -2); /* remove '__name' */
break;
}
}
}
return lua_tolstring(L, -1, len);
Expand Down Expand Up @@ -911,23 +919,23 @@ static int libsize (const luaL_Reg *l) {

/*
** Find or create a module table with a given name. The function
** first looks at the _LOADED table and, if that fails, try a
** first looks at the LOADED table and, if that fails, try a
** global variable with that name. In any case, leaves on the stack
** the module table.
*/
LUALIB_API void luaL_pushmodule (lua_State *L, const char *modname,
int sizehint) {
luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 1); /* get _LOADED table */
if (lua_getfield(L, -1, modname) != LUA_TTABLE) { /* no _LOADED[modname]? */
luaL_findtable(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE, 1);
if (lua_getfield(L, -1, modname) != LUA_TTABLE) { /* no LOADED[modname]? */
lua_pop(L, 1); /* remove previous result */
/* try global variable (and create one if it does not exist) */
lua_pushglobaltable(L);
if (luaL_findtable(L, 0, modname, sizehint) != NULL)
luaL_error(L, "name conflict for module '%s'", modname);
lua_pushvalue(L, -1);
lua_setfield(L, -3, modname); /* _LOADED[modname] = new table */
lua_setfield(L, -3, modname); /* LOADED[modname] = new table */
}
lua_remove(L, -2); /* remove _LOADED table */
lua_remove(L, -2); /* remove LOADED table */
}


Expand Down Expand Up @@ -991,17 +999,17 @@ LUALIB_API int luaL_getsubtable (lua_State *L, int idx, const char *fname) {
*/
LUALIB_API void luaL_requiref (lua_State *L, const char *modname,
lua_CFunction openf, int glb) {
luaL_getsubtable(L, LUA_REGISTRYINDEX, "_LOADED");
lua_getfield(L, -1, modname); /* _LOADED[modname] */
luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE);
lua_getfield(L, -1, modname); /* LOADED[modname] */
if (!lua_toboolean(L, -1)) { /* package not already loaded? */
lua_pop(L, 1); /* remove field */
lua_pushcfunction(L, openf);
lua_pushstring(L, modname); /* argument to open function */
lua_call(L, 1, 1); /* call 'openf' to open module */
lua_pushvalue(L, -1); /* make copy of module (call result) */
lua_setfield(L, -3, modname); /* _LOADED[modname] = module */
lua_setfield(L, -3, modname); /* LOADED[modname] = module */
}
lua_remove(L, -2); /* remove _LOADED table */
lua_remove(L, -2); /* remove LOADED table */
if (glb) {
lua_pushvalue(L, -1); /* copy of module */
lua_setglobal(L, modname); /* _G[modname] = module */
Expand Down Expand Up @@ -1059,7 +1067,7 @@ LUALIB_API void luaL_checkversion_ (lua_State *L, lua_Number ver, size_t sz) {
luaL_error(L, "multiple Lua VMs detected");
else if (*v != ver)
luaL_error(L, "version mismatch: app. needs %f, Lua core provides %f",
ver, *v);
(LUAI_UACNUMBER)ver, (LUAI_UACNUMBER)*v);
}

/* The normal Lua metatable functions in C use string
Expand Down
Loading

0 comments on commit 0f089b6

Please sign in to comment.