aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Pall <mike>2013-10-09 17:01:22 +0200
committerMike Pall <mike>2013-10-09 17:02:01 +0200
commitc8cfca05578932567d2c65d59262f398e8acaed2 (patch)
tree5699e7f2df3a5166eee667b0971775cea6c0ab4b
parent47df3ae5136521da96767e6daed4cdd241de2fa6 (diff)
downloadluajit-c8cfca05578932567d2c65d59262f398e8acaed2.tar.gz
luajit-c8cfca05578932567d2c65d59262f398e8acaed2.tar.bz2
luajit-c8cfca05578932567d2c65d59262f398e8acaed2.zip
Add table.new().
-rw-r--r--doc/extensions.html9
-rw-r--r--src/Makefile.dep2
-rw-r--r--src/lib_table.c20
-rw-r--r--src/lj_api.c4
-rw-r--r--src/lj_asm.c3
-rw-r--r--src/lj_ffrecord.c8
-rw-r--r--src/lj_ir.h1
-rw-r--r--src/lj_ircall.h2
-rw-r--r--src/lj_opt_fold.c5
-rw-r--r--src/lj_tab.c6
-rw-r--r--src/lj_tab.h1
11 files changed, 55 insertions, 6 deletions
diff --git a/doc/extensions.html b/doc/extensions.html
index 33bcfb28..48f4220c 100644
--- a/doc/extensions.html
+++ b/doc/extensions.html
@@ -208,6 +208,15 @@ minor releases (2.0 &rarr; 2.1) or between any beta release. Foreign
208bytecode (e.g. from Lua 5.1) is incompatible and cannot be loaded. 208bytecode (e.g. from Lua 5.1) is incompatible and cannot be loaded.
209</p> 209</p>
210 210
211<h3 id="table_new"><tt>table.new(narray, nhash)</tt> allocates a pre-sized table</h3>
212<p>
213An extra library function <tt>table.new()</tt> can be made available via
214<tt>require("table.new")</tt>. This creates a pre-sized table, just like
215the C API equivalent <tt>lua_createtable()</tt>. This is useful for big
216tables if the final table size is known and automatic table resizing is
217too expensive.
218</p>
219
211<h3 id="math_random">Enhanced PRNG for <tt>math.random()</tt></h3> 220<h3 id="math_random">Enhanced PRNG for <tt>math.random()</tt></h3>
212<p> 221<p>
213LuaJIT uses a Tausworthe PRNG with period 2^223 to implement 222LuaJIT uses a Tausworthe PRNG with period 2^223 to implement
diff --git a/src/Makefile.dep b/src/Makefile.dep
index a285df2c..5b35ddf5 100644
--- a/src/Makefile.dep
+++ b/src/Makefile.dep
@@ -40,7 +40,7 @@ lib_string.o: lib_string.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h \
40 lj_char.h lj_strfmt.h lj_lib.h lj_libdef.h 40 lj_char.h lj_strfmt.h lj_lib.h lj_libdef.h
41lib_table.o: lib_table.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h \ 41lib_table.o: lib_table.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h \
42 lj_def.h lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_buf.h lj_str.h \ 42 lj_def.h lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_buf.h lj_str.h \
43 lj_tab.h lj_lib.h lj_libdef.h 43 lj_tab.h lj_ff.h lj_ffdef.h lj_lib.h lj_libdef.h
44lj_alloc.o: lj_alloc.c lj_def.h lua.h luaconf.h lj_arch.h lj_alloc.h 44lj_alloc.o: lj_alloc.c lj_def.h lua.h luaconf.h lj_arch.h lj_alloc.h
45lj_api.o: lj_api.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \ 45lj_api.o: lj_api.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \
46 lj_err.h lj_errmsg.h lj_debug.h lj_str.h lj_tab.h lj_func.h lj_udata.h \ 46 lj_err.h lj_errmsg.h lj_debug.h lj_str.h lj_tab.h lj_func.h lj_udata.h \
diff --git a/src/lib_table.c b/src/lib_table.c
index d7df8399..85ca660e 100644
--- a/src/lib_table.c
+++ b/src/lib_table.c
@@ -18,6 +18,7 @@
18#include "lj_err.h" 18#include "lj_err.h"
19#include "lj_buf.h" 19#include "lj_buf.h"
20#include "lj_tab.h" 20#include "lj_tab.h"
21#include "lj_ff.h"
21#include "lj_lib.h" 22#include "lj_lib.h"
22 23
23/* ------------------------------------------------------------------------ */ 24/* ------------------------------------------------------------------------ */
@@ -264,6 +265,24 @@ LJLIB_CF(table_pack)
264} 265}
265#endif 266#endif
266 267
268LJLIB_NOREG LJLIB_CF(table_new) LJLIB_REC(.)
269{
270 int32_t a = lj_lib_checkint(L, 1);
271 int32_t h = lj_lib_checkint(L, 2);
272 lua_createtable(L, a, h);
273 return 1;
274}
275
276static int luaopen_table_new(lua_State *L)
277{
278 GCfunc *fn = lj_lib_pushcc(L, lj_cf_table_new, FF_table_new, 0);
279 GCtab *t = tabref(curr_func(L)->c.env); /* Reference to "table". */
280 setfuncV(L, lj_tab_setstr(L, t, lj_str_newlit(L, "new")), fn);
281 lj_gc_anybarriert(L, t);
282 setfuncV(L, L->top++, fn);
283 return 1;
284}
285
267/* ------------------------------------------------------------------------ */ 286/* ------------------------------------------------------------------------ */
268 287
269#include "lj_libdef.h" 288#include "lj_libdef.h"
@@ -275,6 +294,7 @@ LUALIB_API int luaopen_table(lua_State *L)
275 lua_getglobal(L, "unpack"); 294 lua_getglobal(L, "unpack");
276 lua_setfield(L, -2, "unpack"); 295 lua_setfield(L, -2, "unpack");
277#endif 296#endif
297 lj_lib_prereg(L, LUA_TABLIBNAME ".new", luaopen_table_new, tabV(L->top-1));
278 return 1; 298 return 1;
279} 299}
280 300
diff --git a/src/lj_api.c b/src/lj_api.c
index 8aaafdc5..05fb41a7 100644
--- a/src/lj_api.c
+++ b/src/lj_api.c
@@ -640,10 +640,8 @@ LUA_API void lua_pushlightuserdata(lua_State *L, void *p)
640 640
641LUA_API void lua_createtable(lua_State *L, int narray, int nrec) 641LUA_API void lua_createtable(lua_State *L, int narray, int nrec)
642{ 642{
643 GCtab *t;
644 lj_gc_check(L); 643 lj_gc_check(L);
645 t = lj_tab_new(L, (uint32_t)(narray > 0 ? narray+1 : 0), hsize2hbits(nrec)); 644 settabV(L, L->top, lj_tab_new_ah(L, narray, nrec));
646 settabV(L, L->top, t);
647 incr_top(L); 645 incr_top(L);
648} 646}
649 647
diff --git a/src/lj_asm.c b/src/lj_asm.c
index 2bf273e1..0d6875a6 100644
--- a/src/lj_asm.c
+++ b/src/lj_asm.c
@@ -1659,6 +1659,9 @@ static void asm_ir(ASMState *as, IRIns *ir)
1659 case IR_STRTO: asm_strto(as, ir); break; 1659 case IR_STRTO: asm_strto(as, ir); break;
1660 1660
1661 /* Calls. */ 1661 /* Calls. */
1662 case IR_CALLA:
1663 as->gcsteps++;
1664 /* fallthrough */
1662 case IR_CALLN: case IR_CALLL: case IR_CALLS: asm_call(as, ir); break; 1665 case IR_CALLN: case IR_CALLL: case IR_CALLS: asm_call(as, ir); break;
1663 case IR_CALLXS: asm_callx(as, ir); break; 1666 case IR_CALLXS: asm_callx(as, ir); break;
1664 case IR_CARG: break; 1667 case IR_CARG: break;
diff --git a/src/lj_ffrecord.c b/src/lj_ffrecord.c
index 918cc9cf..29916e71 100644
--- a/src/lj_ffrecord.c
+++ b/src/lj_ffrecord.c
@@ -1003,6 +1003,14 @@ static void LJ_FASTCALL recff_table_concat(jit_State *J, RecordFFData *rd)
1003 UNUSED(rd); 1003 UNUSED(rd);
1004} 1004}
1005 1005
1006static void LJ_FASTCALL recff_table_new(jit_State *J, RecordFFData *rd)
1007{
1008 TRef tra = lj_opt_narrow_toint(J, J->base[0]);
1009 TRef trh = lj_opt_narrow_toint(J, J->base[1]);
1010 J->base[0] = lj_ir_call(J, IRCALL_lj_tab_new_ah, tra, trh);
1011 UNUSED(rd);
1012}
1013
1006/* -- I/O library fast functions ------------------------------------------ */ 1014/* -- I/O library fast functions ------------------------------------------ */
1007 1015
1008/* Get FILE* for I/O function. Any I/O error aborts recording, so there's 1016/* Get FILE* for I/O function. Any I/O error aborts recording, so there's
diff --git a/src/lj_ir.h b/src/lj_ir.h
index 54bbbdda..30878b91 100644
--- a/src/lj_ir.h
+++ b/src/lj_ir.h
@@ -139,6 +139,7 @@
139 \ 139 \
140 /* Calls. */ \ 140 /* Calls. */ \
141 _(CALLN, N , ref, lit) \ 141 _(CALLN, N , ref, lit) \
142 _(CALLA, A , ref, lit) \
142 _(CALLL, L , ref, lit) \ 143 _(CALLL, L , ref, lit) \
143 _(CALLS, S , ref, lit) \ 144 _(CALLS, S , ref, lit) \
144 _(CALLXS, S , ref, ref) \ 145 _(CALLXS, S , ref, ref) \
diff --git a/src/lj_ircall.h b/src/lj_ircall.h
index 9e1fb367..53eb4638 100644
--- a/src/lj_ircall.h
+++ b/src/lj_ircall.h
@@ -25,6 +25,7 @@ typedef struct CCallInfo {
25#define CCI_OP(ci) ((ci)->flags >> CCI_OPSHIFT) /* Get op. */ 25#define CCI_OP(ci) ((ci)->flags >> CCI_OPSHIFT) /* Get op. */
26 26
27#define CCI_CALL_N (IR_CALLN << CCI_OPSHIFT) 27#define CCI_CALL_N (IR_CALLN << CCI_OPSHIFT)
28#define CCI_CALL_A (IR_CALLA << CCI_OPSHIFT)
28#define CCI_CALL_L (IR_CALLL << CCI_OPSHIFT) 29#define CCI_CALL_L (IR_CALLL << CCI_OPSHIFT)
29#define CCI_CALL_S (IR_CALLS << CCI_OPSHIFT) 30#define CCI_CALL_S (IR_CALLS << CCI_OPSHIFT)
30#define CCI_CALL_FN (CCI_CALL_N|CCI_CC_FASTCALL) 31#define CCI_CALL_FN (CCI_CALL_N|CCI_CC_FASTCALL)
@@ -140,6 +141,7 @@ typedef struct CCallInfo {
140 _(ANY, lj_buf_putstr_rep, 3, L, P32, 0) \ 141 _(ANY, lj_buf_putstr_rep, 3, L, P32, 0) \
141 _(ANY, lj_buf_puttab, 5, L, P32, 0) \ 142 _(ANY, lj_buf_puttab, 5, L, P32, 0) \
142 _(ANY, lj_buf_tostr, 1, FL, STR, 0) \ 143 _(ANY, lj_buf_tostr, 1, FL, STR, 0) \
144 _(ANY, lj_tab_new_ah, 3, A, TAB, CCI_L) \
143 _(ANY, lj_tab_new1, 2, FS, TAB, CCI_L) \ 145 _(ANY, lj_tab_new1, 2, FS, TAB, CCI_L) \
144 _(ANY, lj_tab_dup, 2, FS, TAB, CCI_L) \ 146 _(ANY, lj_tab_dup, 2, FS, TAB, CCI_L) \
145 _(ANY, lj_tab_newkey, 3, S, P32, CCI_L) \ 147 _(ANY, lj_tab_newkey, 3, S, P32, CCI_L) \
diff --git a/src/lj_opt_fold.c b/src/lj_opt_fold.c
index 9c00f7e7..2ca04a69 100644
--- a/src/lj_opt_fold.c
+++ b/src/lj_opt_fold.c
@@ -165,7 +165,7 @@ typedef IRRef (LJ_FASTCALL *FoldFunc)(jit_State *J);
165 (J->chain[IR_SNEW] || J->chain[IR_XSNEW] || \ 165 (J->chain[IR_SNEW] || J->chain[IR_XSNEW] || \
166 J->chain[IR_TNEW] || J->chain[IR_TDUP] || \ 166 J->chain[IR_TNEW] || J->chain[IR_TDUP] || \
167 J->chain[IR_CNEW] || J->chain[IR_CNEWI] || \ 167 J->chain[IR_CNEW] || J->chain[IR_CNEWI] || \
168 J->chain[IR_BUFSTR] || J->chain[IR_TOSTR])) 168 J->chain[IR_BUFSTR] || J->chain[IR_TOSTR] || J->chain[IR_CALLA]))
169 169
170/* -- Constant folding for FP numbers ------------------------------------- */ 170/* -- Constant folding for FP numbers ------------------------------------- */
171 171
@@ -2319,8 +2319,9 @@ LJFOLD(XSTORE any any)
2319LJFOLDX(lj_opt_dse_xstore) 2319LJFOLDX(lj_opt_dse_xstore)
2320 2320
2321LJFOLD(NEWREF any any) /* Treated like a store. */ 2321LJFOLD(NEWREF any any) /* Treated like a store. */
2322LJFOLD(CALLS any any) 2322LJFOLD(CALLA any any)
2323LJFOLD(CALLL any any) /* Safeguard fallback. */ 2323LJFOLD(CALLL any any) /* Safeguard fallback. */
2324LJFOLD(CALLS any any)
2324LJFOLD(CALLXS any any) 2325LJFOLD(CALLXS any any)
2325LJFOLD(XBAR) 2326LJFOLD(XBAR)
2326LJFOLD(RETF any any) /* Modifies BASE. */ 2327LJFOLD(RETF any any) /* Modifies BASE. */
diff --git a/src/lj_tab.c b/src/lj_tab.c
index ccad1f68..496904ee 100644
--- a/src/lj_tab.c
+++ b/src/lj_tab.c
@@ -149,6 +149,12 @@ GCtab *lj_tab_new(lua_State *L, uint32_t asize, uint32_t hbits)
149 return t; 149 return t;
150} 150}
151 151
152/* The API of this function conforms to lua_createtable(). */
153GCtab *lj_tab_new_ah(lua_State *L, int32_t a, int32_t h)
154{
155 return lj_tab_new(L, (uint32_t)(a > 0 ? a+1 : 0), hsize2hbits(h));
156}
157
152#if LJ_HASJIT 158#if LJ_HASJIT
153GCtab * LJ_FASTCALL lj_tab_new1(lua_State *L, uint32_t ahsize) 159GCtab * LJ_FASTCALL lj_tab_new1(lua_State *L, uint32_t ahsize)
154{ 160{
diff --git a/src/lj_tab.h b/src/lj_tab.h
index d361137c..c57bdd82 100644
--- a/src/lj_tab.h
+++ b/src/lj_tab.h
@@ -34,6 +34,7 @@ static LJ_AINLINE uint32_t hashrot(uint32_t lo, uint32_t hi)
34#define hsize2hbits(s) ((s) ? ((s)==1 ? 1 : 1+lj_fls((uint32_t)((s)-1))) : 0) 34#define hsize2hbits(s) ((s) ? ((s)==1 ? 1 : 1+lj_fls((uint32_t)((s)-1))) : 0)
35 35
36LJ_FUNCA GCtab *lj_tab_new(lua_State *L, uint32_t asize, uint32_t hbits); 36LJ_FUNCA GCtab *lj_tab_new(lua_State *L, uint32_t asize, uint32_t hbits);
37LJ_FUNC GCtab *lj_tab_new_ah(lua_State *L, int32_t a, int32_t h);
37#if LJ_HASJIT 38#if LJ_HASJIT
38LJ_FUNC GCtab * LJ_FASTCALL lj_tab_new1(lua_State *L, uint32_t ahsize); 39LJ_FUNC GCtab * LJ_FASTCALL lj_tab_new1(lua_State *L, uint32_t ahsize);
39#endif 40#endif