Skip to content

Commit

Permalink
Merge branch 'v2.1' into v2.1-agentzh
Browse files Browse the repository at this point in the history
  • Loading branch information
agentzh committed Jul 18, 2016
2 parents e6c3659 + 22e8e07 commit 455f067
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 67 deletions.
1 change: 1 addition & 0 deletions doc/extensions.html
Expand Up @@ -349,6 +349,7 @@ <h2 id="lua53">Extensions from Lua 5.3</h2>
LuaJIT supports some extensions from Lua&nbsp;5.3:
<ul>
<li>Unicode escape <tt>'\u{XX...}'</tt> embeds the UTF-8 encoding in string literals.</li>
<li>The argument table <tt>arg</tt> can be read (and modified) by <tt>LUA_INIT</tt> and <tt>-e</tt> chunks.</li>
</ul>

<h2 id="exceptions">C++ Exception Interoperability</h2>
Expand Down
8 changes: 4 additions & 4 deletions src/host/genminilua.lua
Expand Up @@ -157,11 +157,11 @@ local function merge_includes(src)
if includes[name] then return "" end
includes[name] = true
local fp = assert(io.open(LUA_SOURCE..name, "r"))
local src = fp:read("*a")
local inc = fp:read("*a")
assert(fp:close())
src = gsub(src, "#ifndef%s+%w+_h\n#define%s+%w+_h\n", "")
src = gsub(src, "#endif%s*$", "")
return merge_includes(src)
inc = gsub(inc, "#ifndef%s+%w+_h\n#define%s+%w+_h\n", "")
inc = gsub(inc, "#endif%s*$", "")
return merge_includes(inc)
end)
end

Expand Down
4 changes: 2 additions & 2 deletions src/jit/bcsave.lua
Expand Up @@ -125,12 +125,12 @@ extern "C"
#ifdef _WIN32
__declspec(dllexport)
#endif
const char %s%s[] = {
const unsigned char %s%s[] = {
]], LJBC_PREFIX, ctx.modname))
else
fp:write(string.format([[
#define %s%s_SIZE %d
static const char %s%s[] = {
static const unsigned char %s%s[] = {
]], LJBC_PREFIX, ctx.modname, #s, LJBC_PREFIX, ctx.modname))
end
local t, n, m = {}, 0, 0
Expand Down
2 changes: 1 addition & 1 deletion src/jit/dis_arm.lua
Expand Up @@ -12,7 +12,7 @@

local type = type
local sub, byte, format = string.sub, string.byte, string.format
local match, gmatch, gsub = string.match, string.gmatch, string.gsub
local match, gmatch = string.match, string.gmatch
local concat = table.concat
local bit = require("bit")
local band, bor, ror, tohex = bit.band, bit.bor, bit.ror, bit.tohex
Expand Down
4 changes: 2 additions & 2 deletions src/jit/dis_mips.lua
Expand Up @@ -11,8 +11,8 @@
------------------------------------------------------------------------------

local type = type
local sub, byte, format = string.sub, string.byte, string.format
local match, gmatch, gsub = string.match, string.gmatch, string.gsub
local byte, format = string.byte, string.format
local match, gmatch = string.match, string.gmatch
local concat = table.concat
local bit = require("bit")
local band, bor, tohex = bit.band, bit.bor, bit.tohex
Expand Down
2 changes: 1 addition & 1 deletion src/jit/dis_ppc.lua
Expand Up @@ -13,7 +13,7 @@
------------------------------------------------------------------------------

local type = type
local sub, byte, format = string.sub, string.byte, string.format
local byte, format = string.byte, string.format
local match, gmatch, gsub = string.match, string.gmatch, string.gsub
local concat = table.concat
local bit = require("bit")
Expand Down
2 changes: 1 addition & 1 deletion src/jit/dis_x86.lua
Expand Up @@ -818,7 +818,7 @@ map_act = {
m = b%32; b = (b-m)/32
local nb = b%2; b = (b-nb)/2
if nb == 0 then ctx.rexb = true end
local nx = b%2; b = (b-nx)/2
local nx = b%2
if nx == 0 then ctx.rexx = true end
b = byte(ctx.code, pos, pos)
if not b then return incomplete(ctx) end
Expand Down
6 changes: 3 additions & 3 deletions src/jit/dump.lua
Expand Up @@ -63,9 +63,9 @@ local traceinfo, traceir, tracek = jutil.traceinfo, jutil.traceir, jutil.tracek
local tracemc, tracesnap = jutil.tracemc, jutil.tracesnap
local traceexitstub, ircalladdr = jutil.traceexitstub, jutil.ircalladdr
local bit = require("bit")
local band, shl, shr, tohex = bit.band, bit.lshift, bit.rshift, bit.tohex
local band, shr, tohex = bit.band, bit.rshift, bit.tohex
local sub, gsub, format = string.sub, string.gsub, string.format
local byte, char, rep = string.byte, string.char, string.rep
local byte, rep = string.byte, string.rep
local type, tostring = type, tostring
local stdout, stderr = io.stdout, io.stderr

Expand Down Expand Up @@ -213,7 +213,7 @@ local colortype_ansi = {
"\027[35m%s\027[m",
}

local function colorize_text(s, t)
local function colorize_text(s)
return s
end

Expand Down
2 changes: 1 addition & 1 deletion src/jit/p.lua
Expand Up @@ -120,7 +120,7 @@ end
-- Show top N list.
local function prof_top(count1, count2, samples, indent)
local t, n = {}, 0
for k, v in pairs(count1) do
for k in pairs(count1) do
n = n + 1
t[n] = k
end
Expand Down
3 changes: 2 additions & 1 deletion src/lj_crecord.c
Expand Up @@ -1249,7 +1249,7 @@ void LJ_FASTCALL recff_cdata_call(jit_State *J, RecordFFData *rd)

static TRef crec_arith_int64(jit_State *J, TRef *sp, CType **s, MMS mm)
{
if (ctype_isnum(s[0]->info) && ctype_isnum(s[1]->info)) {
if (sp[0] && sp[1] && ctype_isnum(s[0]->info) && ctype_isnum(s[1]->info)) {
IRType dt;
CTypeID id;
TRef tr;
Expand Down Expand Up @@ -1307,6 +1307,7 @@ static TRef crec_arith_ptr(jit_State *J, TRef *sp, CType **s, MMS mm)
{
CTState *cts = ctype_ctsG(J2G(J));
CType *ctp = s[0];
if (!(sp[0] && sp[1])) return 0;
if (ctype_isptr(ctp->info) || ctype_isrefarray(ctp->info)) {
if ((mm == MM_sub || mm == MM_eq || mm == MM_lt || mm == MM_le) &&
(ctype_isptr(s[1]->info) || ctype_isrefarray(s[1]->info))) {
Expand Down
14 changes: 7 additions & 7 deletions src/lj_ctype.h
Expand Up @@ -42,18 +42,18 @@ LJ_STATIC_ASSERT(((int)CT_STRUCT & (int)CT_ARRAY) == CT_STRUCT);
** ---------- info ------------
** |type flags... A cid | size | sib | next | name |
** +----------------------------+--------+-------+-------+-------+--
** |NUM BFvcUL.. A | size | | type | |
** |STRUCT ..vcU..V A | size | field | name? | name? |
** |PTR ..vcR... A cid | size | | type | |
** |ARRAY VCvc...V A cid | size | | type | |
** |VOID ..vc.... A | size | | type | |
** |NUM BFcvUL.. A | size | | type | |
** |STRUCT ..cvU..V A | size | field | name? | name? |
** |PTR ..cvR... A cid | size | | type | |
** |ARRAY VCcv...V A cid | size | | type | |
** |VOID ..cv.... A | size | | type | |
** |ENUM A cid | size | const | name? | name? |
** |FUNC ....VS.. cc cid | nargs | field | name? | name? |
** |TYPEDEF cid | | | name | name |
** |ATTRIB attrnum cid | attr | sib? | type? | |
** |FIELD cid | offset | field | | name? |
** |BITFIELD B.vcU csz bsz pos | offset | field | | name? |
** |CONSTVAL c cid | value | const | name | name |
** |BITFIELD B.cvU csz bsz pos | offset | field | | name? |
** |CONSTVAL c cid | value | const | name | name |
** |EXTERN cid | | sib? | name | name |
** |KW tok | size | | name | name |
** +----------------------------+--------+-------+-------+-------+--
Expand Down
103 changes: 59 additions & 44 deletions src/luajit.c
Expand Up @@ -152,22 +152,15 @@ static void print_jit_status(lua_State *L)
putc('\n', stdout);
}

static int getargs(lua_State *L, char **argv, int n)
static void createargtable(lua_State *L, char **argv, int argc, int argf)
{
int narg;
int i;
int argc = 0;
while (argv[argc]) argc++; /* count total number of arguments */
narg = argc - (n + 1); /* number of arguments to the script */
luaL_checkstack(L, narg + 3, "too many arguments to script");
for (i = n+1; i < argc; i++)
lua_pushstring(L, argv[i]);
lua_createtable(L, narg, n + 1);
lua_createtable(L, argc - argf, argf);
for (i = 0; i < argc; i++) {
lua_pushstring(L, argv[i]);
lua_rawseti(L, -2, i - n);
lua_rawseti(L, -2, i - argf);
}
return narg;
lua_setglobal(L, "arg");
}

static int dofile(lua_State *L, const char *name)
Expand Down Expand Up @@ -273,21 +266,30 @@ static void dotty(lua_State *L)
progname = oldprogname;
}

static int handle_script(lua_State *L, char **argv, int n)
static int handle_script(lua_State *L, char **argx)
{
int status;
const char *fname;
int narg = getargs(L, argv, n); /* collect arguments */
lua_setglobal(L, "arg");
fname = argv[n];
if (strcmp(fname, "-") == 0 && strcmp(argv[n-1], "--") != 0)
const char *fname = argx[0];
if (strcmp(fname, "-") == 0 && strcmp(argx[-1], "--") != 0)
fname = NULL; /* stdin */
status = luaL_loadfile(L, fname);
lua_insert(L, -(narg+1));
if (status == 0)
if (status == 0) {
/* Fetch args from arg table. LUA_INIT or -e might have changed them. */
int narg = 0;
lua_getglobal(L, "arg");
if (lua_istable(L, -1)) {
do {
narg++;
lua_rawgeti(L, -narg, narg);
} while (!lua_isnil(L, -1));
lua_pop(L, 1);
lua_remove(L, -narg);
narg--;
} else {
lua_pop(L, 1);
}
status = docall(L, narg, 0);
else
lua_pop(L, narg);
}
return report(L, status);
}

Expand Down Expand Up @@ -384,7 +386,8 @@ static int dobytecode(lua_State *L, char **argv)
}
for (argv++; *argv != NULL; narg++, argv++)
lua_pushstring(L, *argv);
return report(L, lua_pcall(L, narg, 0, 0));
report(L, lua_pcall(L, narg, 0, 0));
return 1;
}

/* check that argument has no extra characters at the end */
Expand All @@ -405,7 +408,7 @@ static int collectargs(char **argv, int *flags)
switch (argv[i][1]) { /* Check option. */
case '-':
notail(argv[i]);
return (argv[i+1] != NULL ? i+1 : 0);
return i+1;
case '\0':
return i;
case 'i':
Expand All @@ -430,23 +433,23 @@ static int collectargs(char **argv, int *flags)
case 'b': /* LuaJIT extension */
if (*flags) return -1;
*flags |= FLAGS_EXEC;
return 0;
return i+1;
case 'E':
*flags |= FLAGS_NOENV;
break;
default: return -1; /* invalid option */
}
}
return 0;
return i;
}

static int runargs(lua_State *L, char **argv, int n)
static int runargs(lua_State *L, char **argv, int argn)
{
int i;
for (i = 1; i < n; i++) {
for (i = 1; i < argn; i++) {
if (argv[i] == NULL) continue;
lua_assert(argv[i][0] == '-');
switch (argv[i][1]) { /* option */
switch (argv[i][1]) {
case 'e': {
const char *chunk = argv[i] + 2;
if (*chunk == '\0') chunk = argv[++i];
Expand All @@ -460,22 +463,22 @@ static int runargs(lua_State *L, char **argv, int n)
if (*filename == '\0') filename = argv[++i];
lua_assert(filename != NULL);
if (dolibrary(L, filename))
return 1; /* stop if file fails */
return 1;
break;
}
case 'j': { /* LuaJIT extension */
case 'j': { /* LuaJIT extension. */
const char *cmd = argv[i] + 2;
if (*cmd == '\0') cmd = argv[++i];
lua_assert(cmd != NULL);
if (dojitcmd(L, cmd))
return 1;
break;
}
case 'O': /* LuaJIT extension */
case 'O': /* LuaJIT extension. */
if (dojitopt(L, argv[i] + 2))
return 1;
break;
case 'b': /* LuaJIT extension */
case 'b': /* LuaJIT extension. */
return dobytecode(L, argv+i);
default: break;
}
Expand Down Expand Up @@ -508,45 +511,57 @@ static int pmain(lua_State *L)
{
struct Smain *s = &smain;
char **argv = s->argv;
int script;
int argn;
int flags = 0;
globalL = L;
if (argv[0] && argv[0][0]) progname = argv[0];
LUAJIT_VERSION_SYM(); /* linker-enforced version check */
script = collectargs(argv, &flags);
if (script < 0) { /* invalid args? */

LUAJIT_VERSION_SYM(); /* Linker-enforced version check. */

argn = collectargs(argv, &flags);
if (argn < 0) { /* Invalid args? */
print_usage();
s->status = 1;
return 0;
}

if ((flags & FLAGS_NOENV)) {
lua_pushboolean(L, 1);
lua_setfield(L, LUA_REGISTRYINDEX, "LUA_NOENV");
}
lua_gc(L, LUA_GCSTOP, 0); /* stop collector during initialization */
luaL_openlibs(L); /* open libraries */

/* Stop collector during library initialization. */
lua_gc(L, LUA_GCSTOP, 0);
luaL_openlibs(L);
lua_gc(L, LUA_GCRESTART, -1);

createargtable(L, argv, s->argc, argn);

if (!(flags & FLAGS_NOENV)) {
s->status = handle_luainit(L);
if (s->status != 0) return 0;
}

if ((flags & FLAGS_VERSION)) print_version();
s->status = runargs(L, argv, (script > 0) ? script : s->argc);

s->status = runargs(L, argv, argn);
if (s->status != 0) return 0;
if (script) {
s->status = handle_script(L, argv, script);

if (s->argc > argn) {
s->status = handle_script(L, argv + argn);
if (s->status != 0) return 0;
}

if ((flags & FLAGS_INTERACTIVE)) {
print_jit_status(L);
dotty(L);
} else if (script == 0 && !(flags & (FLAGS_EXEC|FLAGS_VERSION))) {
} else if (s->argc == argn && !(flags & (FLAGS_EXEC|FLAGS_VERSION))) {
if (lua_stdin_is_tty()) {
print_version();
print_jit_status(L);
dotty(L);
} else {
dofile(L, NULL); /* executes stdin as a file */
dofile(L, NULL); /* Executes stdin as a file. */
}
}
return 0;
Expand All @@ -555,7 +570,7 @@ static int pmain(lua_State *L)
int main(int argc, char **argv)
{
int status;
lua_State *L = lua_open(); /* create state */
lua_State *L = lua_open();
if (L == NULL) {
l_message(argv[0], "cannot create state: not enough memory");
return EXIT_FAILURE;
Expand Down

0 comments on commit 455f067

Please sign in to comment.