Merge branch 'master' into v2.1
[luajit-2.0.git] / src / lib_base.c
blob5d1b88a9ad07144e9fdf97ceb04f41a52297774c
1 /*
2 ** Base and coroutine library.
3 ** Copyright (C) 2005-2025 Mike Pall. See Copyright Notice in luajit.h
4 **
5 ** Major portions taken verbatim or adapted from the Lua interpreter.
6 ** Copyright (C) 1994-2011 Lua.org, PUC-Rio. See Copyright Notice in lua.h
7 */
9 #include <stdio.h>
11 #define lib_base_c
12 #define LUA_LIB
14 #include "lua.h"
15 #include "lauxlib.h"
16 #include "lualib.h"
18 #include "lj_obj.h"
19 #include "lj_gc.h"
20 #include "lj_err.h"
21 #include "lj_debug.h"
22 #include "lj_buf.h"
23 #include "lj_str.h"
24 #include "lj_tab.h"
25 #include "lj_meta.h"
26 #include "lj_state.h"
27 #include "lj_frame.h"
28 #if LJ_HASFFI
29 #include "lj_ctype.h"
30 #include "lj_cconv.h"
31 #endif
32 #include "lj_bc.h"
33 #include "lj_ff.h"
34 #include "lj_dispatch.h"
35 #include "lj_char.h"
36 #include "lj_strscan.h"
37 #include "lj_strfmt.h"
38 #include "lj_lib.h"
40 /* -- Base library: checks ------------------------------------------------ */
42 #define LJLIB_MODULE_base
44 LJLIB_ASM(assert) LJLIB_REC(.)
46 lj_lib_checkany(L, 1);
47 if (L->top == L->base+1)
48 lj_err_caller(L, LJ_ERR_ASSERT);
49 else if (tvisstr(L->base+1) || tvisnumber(L->base+1))
50 lj_err_callermsg(L, strdata(lj_lib_checkstr(L, 2)));
51 else
52 lj_err_run(L);
53 return FFH_UNREACHABLE;
56 /* ORDER LJ_T */
57 LJLIB_PUSH("nil")
58 LJLIB_PUSH("boolean")
59 LJLIB_PUSH(top-1) /* boolean */
60 LJLIB_PUSH("userdata")
61 LJLIB_PUSH("string")
62 LJLIB_PUSH("upval")
63 LJLIB_PUSH("thread")
64 LJLIB_PUSH("proto")
65 LJLIB_PUSH("function")
66 LJLIB_PUSH("trace")
67 LJLIB_PUSH("cdata")
68 LJLIB_PUSH("table")
69 LJLIB_PUSH(top-9) /* userdata */
70 LJLIB_PUSH("number")
71 LJLIB_ASM_(type) LJLIB_REC(.)
72 /* Recycle the lj_lib_checkany(L, 1) from assert. */
74 /* -- Base library: iterators --------------------------------------------- */
76 /* This solves a circular dependency problem -- change FF_next_N as needed. */
77 LJ_STATIC_ASSERT((int)FF_next == FF_next_N);
79 LJLIB_ASM(next) LJLIB_REC(.)
81 lj_lib_checktab(L, 1);
82 lj_err_msg(L, LJ_ERR_NEXTIDX);
83 return FFH_UNREACHABLE;
86 #if LJ_52 || LJ_HASFFI
87 static int ffh_pairs(lua_State *L, MMS mm)
89 TValue *o = lj_lib_checkany(L, 1);
90 cTValue *mo = lj_meta_lookup(L, o, mm);
91 if ((LJ_52 || tviscdata(o)) && !tvisnil(mo)) {
92 L->top = o+1; /* Only keep one argument. */
93 copyTV(L, L->base-1-LJ_FR2, mo); /* Replace callable. */
94 return FFH_TAILCALL;
95 } else {
96 if (!tvistab(o)) lj_err_argt(L, 1, LUA_TTABLE);
97 if (LJ_FR2) { copyTV(L, o-1, o); o--; }
98 setfuncV(L, o-1, funcV(lj_lib_upvalue(L, 1)));
99 if (mm == MM_pairs) setnilV(o+1); else setintV(o+1, 0);
100 return FFH_RES(3);
103 #else
104 #define ffh_pairs(L, mm) (lj_lib_checktab(L, 1), FFH_UNREACHABLE)
105 #endif
107 LJLIB_PUSH(lastcl)
108 LJLIB_ASM(pairs) LJLIB_REC(xpairs 0)
110 return ffh_pairs(L, MM_pairs);
113 LJLIB_NOREGUV LJLIB_ASM(ipairs_aux) LJLIB_REC(.)
115 lj_lib_checktab(L, 1);
116 lj_lib_checkint(L, 2);
117 return FFH_UNREACHABLE;
120 LJLIB_PUSH(lastcl)
121 LJLIB_ASM(ipairs) LJLIB_REC(xpairs 1)
123 return ffh_pairs(L, MM_ipairs);
126 /* -- Base library: getters and setters ----------------------------------- */
128 LJLIB_ASM_(getmetatable) LJLIB_REC(.)
129 /* Recycle the lj_lib_checkany(L, 1) from assert. */
131 LJLIB_ASM(setmetatable) LJLIB_REC(.)
133 GCtab *t = lj_lib_checktab(L, 1);
134 GCtab *mt = lj_lib_checktabornil(L, 2);
135 if (!tvisnil(lj_meta_lookup(L, L->base, MM_metatable)))
136 lj_err_caller(L, LJ_ERR_PROTMT);
137 setgcref(t->metatable, obj2gco(mt));
138 if (mt) { lj_gc_objbarriert(L, t, mt); }
139 settabV(L, L->base-1-LJ_FR2, t);
140 return FFH_RES(1);
143 LJLIB_CF(getfenv) LJLIB_REC(.)
145 GCfunc *fn;
146 cTValue *o = L->base;
147 if (!(o < L->top && tvisfunc(o))) {
148 int level = lj_lib_optint(L, 1, 1);
149 if (level < 0)
150 lj_err_arg(L, 1, LJ_ERR_INVLVL);
151 o = lj_debug_frame(L, level, &level);
152 if (o == NULL)
153 lj_err_arg(L, 1, LJ_ERR_INVLVL);
154 if (LJ_FR2) o--;
156 fn = &gcval(o)->fn;
157 settabV(L, L->top++, isluafunc(fn) ? tabref(fn->l.env) : tabref(L->env));
158 return 1;
161 LJLIB_CF(setfenv)
163 GCfunc *fn;
164 GCtab *t = lj_lib_checktab(L, 2);
165 cTValue *o = L->base;
166 if (!(o < L->top && tvisfunc(o))) {
167 int level = lj_lib_checkint(L, 1);
168 if (level == 0) {
169 /* NOBARRIER: A thread (i.e. L) is never black. */
170 setgcref(L->env, obj2gco(t));
171 return 0;
173 if (level < 0)
174 lj_err_arg(L, 1, LJ_ERR_INVLVL);
175 o = lj_debug_frame(L, level, &level);
176 if (o == NULL)
177 lj_err_arg(L, 1, LJ_ERR_INVLVL);
178 if (LJ_FR2) o--;
180 fn = &gcval(o)->fn;
181 if (!isluafunc(fn))
182 lj_err_caller(L, LJ_ERR_SETFENV);
183 setgcref(fn->l.env, obj2gco(t));
184 lj_gc_objbarrier(L, obj2gco(fn), t);
185 setfuncV(L, L->top++, fn);
186 return 1;
189 LJLIB_ASM(rawget) LJLIB_REC(.)
191 lj_lib_checktab(L, 1);
192 lj_lib_checkany(L, 2);
193 return FFH_UNREACHABLE;
196 LJLIB_CF(rawset) LJLIB_REC(.)
198 lj_lib_checktab(L, 1);
199 lj_lib_checkany(L, 2);
200 L->top = 1+lj_lib_checkany(L, 3);
201 lua_rawset(L, 1);
202 return 1;
205 LJLIB_CF(rawequal) LJLIB_REC(.)
207 cTValue *o1 = lj_lib_checkany(L, 1);
208 cTValue *o2 = lj_lib_checkany(L, 2);
209 setboolV(L->top-1, lj_obj_equal(o1, o2));
210 return 1;
213 #if LJ_52
214 LJLIB_CF(rawlen) LJLIB_REC(.)
216 cTValue *o = L->base;
217 int32_t len;
218 if (L->top > o && tvisstr(o))
219 len = (int32_t)strV(o)->len;
220 else
221 len = (int32_t)lj_tab_len(lj_lib_checktab(L, 1));
222 setintV(L->top-1, len);
223 return 1;
225 #endif
227 LJLIB_CF(unpack)
229 GCtab *t = lj_lib_checktab(L, 1);
230 int32_t n, i = lj_lib_optint(L, 2, 1);
231 int32_t e = (L->base+3-1 < L->top && !tvisnil(L->base+3-1)) ?
232 lj_lib_checkint(L, 3) : (int32_t)lj_tab_len(t);
233 uint32_t nu;
234 if (i > e) return 0;
235 nu = (uint32_t)e - (uint32_t)i;
236 n = (int32_t)(nu+1);
237 if (nu >= LUAI_MAXCSTACK || !lua_checkstack(L, n))
238 lj_err_caller(L, LJ_ERR_UNPACK);
239 do {
240 cTValue *tv = lj_tab_getint(t, i);
241 if (tv) {
242 copyTV(L, L->top++, tv);
243 } else {
244 setnilV(L->top++);
246 } while (i++ < e);
247 return n;
250 LJLIB_CF(select) LJLIB_REC(.)
252 int32_t n = (int32_t)(L->top - L->base);
253 if (n >= 1 && tvisstr(L->base) && *strVdata(L->base) == '#') {
254 setintV(L->top-1, n-1);
255 return 1;
256 } else {
257 int32_t i = lj_lib_checkint(L, 1);
258 if (i < 0) i = n + i; else if (i > n) i = n;
259 if (i < 1)
260 lj_err_arg(L, 1, LJ_ERR_IDXRNG);
261 return n - i;
265 /* -- Base library: conversions ------------------------------------------- */
267 LJLIB_ASM(tonumber) LJLIB_REC(.)
269 int32_t base = lj_lib_optint(L, 2, 10);
270 if (base == 10) {
271 TValue *o = lj_lib_checkany(L, 1);
272 if (lj_strscan_numberobj(o)) {
273 copyTV(L, L->base-1-LJ_FR2, o);
274 return FFH_RES(1);
276 #if LJ_HASFFI
277 if (tviscdata(o)) {
278 CTState *cts = ctype_cts(L);
279 CType *ct = lj_ctype_rawref(cts, cdataV(o)->ctypeid);
280 if (ctype_isenum(ct->info)) ct = ctype_child(cts, ct);
281 if (ctype_isnum(ct->info) || ctype_iscomplex(ct->info)) {
282 if (LJ_DUALNUM && ctype_isinteger_or_bool(ct->info) &&
283 ct->size <= 4 && !(ct->size == 4 && (ct->info & CTF_UNSIGNED))) {
284 int32_t i;
285 lj_cconv_ct_tv(cts, ctype_get(cts, CTID_INT32), (uint8_t *)&i, o, 0);
286 setintV(L->base-1-LJ_FR2, i);
287 return FFH_RES(1);
289 lj_cconv_ct_tv(cts, ctype_get(cts, CTID_DOUBLE),
290 (uint8_t *)&(L->base-1-LJ_FR2)->n, o, 0);
291 return FFH_RES(1);
294 #endif
295 } else {
296 const char *p = strdata(lj_lib_checkstr(L, 1));
297 char *ep;
298 unsigned int neg = 0;
299 unsigned long ul;
300 if (base < 2 || base > 36)
301 lj_err_arg(L, 2, LJ_ERR_BASERNG);
302 while (lj_char_isspace((unsigned char)(*p))) p++;
303 if (*p == '-') { p++; neg = 1; } else if (*p == '+') { p++; }
304 if (lj_char_isalnum((unsigned char)(*p))) {
305 ul = strtoul(p, &ep, base);
306 if (p != ep) {
307 while (lj_char_isspace((unsigned char)(*ep))) ep++;
308 if (*ep == '\0') {
309 if (LJ_DUALNUM && LJ_LIKELY(ul < 0x80000000u+neg)) {
310 if (neg) ul = ~ul+1u;
311 setintV(L->base-1-LJ_FR2, (int32_t)ul);
312 } else {
313 lua_Number n = (lua_Number)ul;
314 if (neg) n = -n;
315 setnumV(L->base-1-LJ_FR2, n);
317 return FFH_RES(1);
322 setnilV(L->base-1-LJ_FR2);
323 return FFH_RES(1);
326 LJLIB_ASM(tostring) LJLIB_REC(.)
328 TValue *o = lj_lib_checkany(L, 1);
329 cTValue *mo;
330 L->top = o+1; /* Only keep one argument. */
331 if (!tvisnil(mo = lj_meta_lookup(L, o, MM_tostring))) {
332 copyTV(L, L->base-1-LJ_FR2, mo); /* Replace callable. */
333 return FFH_TAILCALL;
335 lj_gc_check(L);
336 setstrV(L, L->base-1-LJ_FR2, lj_strfmt_obj(L, L->base));
337 return FFH_RES(1);
340 /* -- Base library: throw and catch errors -------------------------------- */
342 LJLIB_CF(error)
344 int32_t level = lj_lib_optint(L, 2, 1);
345 lua_settop(L, 1);
346 if (lua_isstring(L, 1) && level > 0) {
347 luaL_where(L, level);
348 lua_pushvalue(L, 1);
349 lua_concat(L, 2);
351 return lua_error(L);
354 LJLIB_ASM(pcall) LJLIB_REC(.)
356 lj_lib_checkany(L, 1);
357 lj_lib_checkfunc(L, 2); /* For xpcall only. */
358 return FFH_UNREACHABLE;
360 LJLIB_ASM_(xpcall) LJLIB_REC(.)
362 /* -- Base library: load Lua code ----------------------------------------- */
364 static int load_aux(lua_State *L, int status, int envarg)
366 if (status == LUA_OK) {
368 ** Set environment table for top-level function.
369 ** Don't do this for non-native bytecode, which returns a prototype.
371 if (tvistab(L->base+envarg-1) && tvisfunc(L->top-1)) {
372 GCfunc *fn = funcV(L->top-1);
373 GCtab *t = tabV(L->base+envarg-1);
374 setgcref(fn->c.env, obj2gco(t));
375 lj_gc_objbarrier(L, fn, t);
377 return 1;
378 } else {
379 setnilV(L->top-2);
380 return 2;
384 LJLIB_CF(loadfile)
386 GCstr *fname = lj_lib_optstr(L, 1);
387 GCstr *mode = lj_lib_optstr(L, 2);
388 int status;
389 lua_settop(L, 3); /* Ensure env arg exists. */
390 status = luaL_loadfilex(L, fname ? strdata(fname) : NULL,
391 mode ? strdata(mode) : NULL);
392 return load_aux(L, status, 3);
395 static const char *reader_func(lua_State *L, void *ud, size_t *size)
397 UNUSED(ud);
398 luaL_checkstack(L, 2, "too many nested functions");
399 copyTV(L, L->top++, L->base);
400 lua_call(L, 0, 1); /* Call user-supplied function. */
401 L->top--;
402 if (tvisnil(L->top)) {
403 *size = 0;
404 return NULL;
405 } else if (tvisstr(L->top) || tvisnumber(L->top)) {
406 copyTV(L, L->base+4, L->top); /* Anchor string in reserved stack slot. */
407 return lua_tolstring(L, 5, size);
408 } else {
409 lj_err_caller(L, LJ_ERR_RDRSTR);
410 return NULL;
414 LJLIB_CF(load)
416 GCstr *name = lj_lib_optstr(L, 2);
417 GCstr *mode = lj_lib_optstr(L, 3);
418 int status;
419 if (L->base < L->top &&
420 (tvisstr(L->base) || tvisnumber(L->base) || tvisbuf(L->base))) {
421 const char *s;
422 MSize len;
423 if (tvisbuf(L->base)) {
424 SBufExt *sbx = bufV(L->base);
425 s = sbx->r;
426 len = sbufxlen(sbx);
427 if (!name) name = &G(L)->strempty; /* Buffers are not NUL-terminated. */
428 } else {
429 GCstr *str = lj_lib_checkstr(L, 1);
430 s = strdata(str);
431 len = str->len;
433 lua_settop(L, 4); /* Ensure env arg exists. */
434 status = luaL_loadbufferx(L, s, len, name ? strdata(name) : s,
435 mode ? strdata(mode) : NULL);
436 } else {
437 lj_lib_checkfunc(L, 1);
438 lua_settop(L, 5); /* Reserve a slot for the string from the reader. */
439 status = lua_loadx(L, reader_func, NULL, name ? strdata(name) : "=(load)",
440 mode ? strdata(mode) : NULL);
442 return load_aux(L, status, 4);
445 LJLIB_CF(loadstring)
447 return lj_cf_load(L);
450 LJLIB_CF(dofile)
452 GCstr *fname = lj_lib_optstr(L, 1);
453 setnilV(L->top);
454 L->top = L->base+1;
455 if (luaL_loadfile(L, fname ? strdata(fname) : NULL) != LUA_OK)
456 lua_error(L);
457 lua_call(L, 0, LUA_MULTRET);
458 return (int)(L->top - L->base) - 1;
461 /* -- Base library: GC control -------------------------------------------- */
463 LJLIB_CF(gcinfo)
465 setintV(L->top++, (int32_t)(G(L)->gc.total >> 10));
466 return 1;
469 LJLIB_CF(collectgarbage)
471 int opt = lj_lib_checkopt(L, 1, LUA_GCCOLLECT, /* ORDER LUA_GC* */
472 "\4stop\7restart\7collect\5count\1\377\4step\10setpause\12setstepmul\1\377\11isrunning");
473 int32_t data = lj_lib_optint(L, 2, 0);
474 if (opt == LUA_GCCOUNT) {
475 setnumV(L->top, (lua_Number)G(L)->gc.total/1024.0);
476 } else {
477 int res = lua_gc(L, opt, data);
478 if (opt == LUA_GCSTEP || opt == LUA_GCISRUNNING)
479 setboolV(L->top, res);
480 else
481 setintV(L->top, res);
483 L->top++;
484 return 1;
487 /* -- Base library: miscellaneous functions ------------------------------- */
489 LJLIB_PUSH(top-2) /* Upvalue holds weak table. */
490 LJLIB_CF(newproxy)
492 lua_settop(L, 1);
493 lua_newuserdata(L, 0);
494 if (lua_toboolean(L, 1) == 0) { /* newproxy(): without metatable. */
495 return 1;
496 } else if (lua_isboolean(L, 1)) { /* newproxy(true): with metatable. */
497 lua_newtable(L);
498 lua_pushvalue(L, -1);
499 lua_pushboolean(L, 1);
500 lua_rawset(L, lua_upvalueindex(1)); /* Remember mt in weak table. */
501 } else { /* newproxy(proxy): inherit metatable. */
502 int validproxy = 0;
503 if (lua_getmetatable(L, 1)) {
504 lua_rawget(L, lua_upvalueindex(1));
505 validproxy = lua_toboolean(L, -1);
506 lua_pop(L, 1);
508 if (!validproxy)
509 lj_err_arg(L, 1, LJ_ERR_NOPROXY);
510 lua_getmetatable(L, 1);
512 lua_setmetatable(L, 2);
513 return 1;
516 LJLIB_PUSH("tostring")
517 LJLIB_CF(print)
519 ptrdiff_t i, nargs = L->top - L->base;
520 cTValue *tv = lj_tab_getstr(tabref(L->env), strV(lj_lib_upvalue(L, 1)));
521 int shortcut;
522 if (tv && !tvisnil(tv)) {
523 copyTV(L, L->top++, tv);
524 } else {
525 setstrV(L, L->top++, strV(lj_lib_upvalue(L, 1)));
526 lua_gettable(L, LUA_GLOBALSINDEX);
527 tv = L->top-1;
529 shortcut = (tvisfunc(tv) && funcV(tv)->c.ffid == FF_tostring) &&
530 !gcrefu(basemt_it(G(L), LJ_TNUMX));
531 for (i = 0; i < nargs; i++) {
532 cTValue *o = &L->base[i];
533 const char *str;
534 size_t size;
535 MSize len;
536 if (shortcut && (str = lj_strfmt_wstrnum(L, o, &len)) != NULL) {
537 size = len;
538 } else {
539 copyTV(L, L->top+1, o);
540 copyTV(L, L->top, L->top-1);
541 L->top += 2;
542 lua_call(L, 1, 1);
543 str = lua_tolstring(L, -1, &size);
544 if (!str)
545 lj_err_caller(L, LJ_ERR_PRTOSTR);
546 L->top--;
548 if (i)
549 putchar('\t');
550 fwrite(str, 1, size, stdout);
552 putchar('\n');
553 return 0;
556 LJLIB_PUSH(top-3)
557 LJLIB_SET(_VERSION)
559 #include "lj_libdef.h"
561 /* -- Coroutine library --------------------------------------------------- */
563 #define LJLIB_MODULE_coroutine
565 LJLIB_CF(coroutine_status)
567 const char *s;
568 lua_State *co;
569 if (!(L->top > L->base && tvisthread(L->base)))
570 lj_err_arg(L, 1, LJ_ERR_NOCORO);
571 co = threadV(L->base);
572 if (co == L) s = "running";
573 else if (co->status == LUA_YIELD) s = "suspended";
574 else if (co->status != LUA_OK) s = "dead";
575 else if (co->base > tvref(co->stack)+1+LJ_FR2) s = "normal";
576 else if (co->top == co->base) s = "dead";
577 else s = "suspended";
578 lua_pushstring(L, s);
579 return 1;
582 LJLIB_CF(coroutine_running)
584 #if LJ_52
585 int ismain = lua_pushthread(L);
586 setboolV(L->top++, ismain);
587 return 2;
588 #else
589 if (lua_pushthread(L))
590 setnilV(L->top++);
591 return 1;
592 #endif
595 LJLIB_CF(coroutine_isyieldable)
597 setboolV(L->top++, cframe_canyield(L->cframe));
598 return 1;
601 LJLIB_CF(coroutine_create)
603 lua_State *L1;
604 if (!(L->base < L->top && tvisfunc(L->base)))
605 lj_err_argt(L, 1, LUA_TFUNCTION);
606 L1 = lua_newthread(L);
607 setfuncV(L, L1->top++, funcV(L->base));
608 return 1;
611 LJLIB_ASM(coroutine_yield)
613 lj_err_caller(L, LJ_ERR_CYIELD);
614 return FFH_UNREACHABLE;
617 static int ffh_resume(lua_State *L, lua_State *co, int wrap)
619 if (co->cframe != NULL || co->status > LUA_YIELD ||
620 (co->status == LUA_OK && co->top == co->base)) {
621 ErrMsg em = co->cframe ? LJ_ERR_CORUN : LJ_ERR_CODEAD;
622 if (wrap) lj_err_caller(L, em);
623 setboolV(L->base-1-LJ_FR2, 0);
624 setstrV(L, L->base-LJ_FR2, lj_err_str(L, em));
625 return FFH_RES(2);
627 if (lj_state_cpgrowstack(co, (MSize)(L->top - L->base)) != LUA_OK) {
628 cTValue *msg = --co->top;
629 lj_err_callermsg(L, strVdata(msg));
631 return FFH_RETRY;
634 LJLIB_ASM(coroutine_resume)
636 if (!(L->top > L->base && tvisthread(L->base)))
637 lj_err_arg(L, 1, LJ_ERR_NOCORO);
638 return ffh_resume(L, threadV(L->base), 0);
641 LJLIB_NOREG LJLIB_ASM(coroutine_wrap_aux)
643 return ffh_resume(L, threadV(lj_lib_upvalue(L, 1)), 1);
646 /* Inline declarations. */
647 LJ_ASMF void lj_ff_coroutine_wrap_aux(void);
648 #if !(LJ_TARGET_MIPS && defined(ljamalg_c))
649 LJ_FUNCA_NORET void LJ_FASTCALL lj_ffh_coroutine_wrap_err(lua_State *L,
650 lua_State *co);
651 #endif
653 /* Error handler, called from assembler VM. */
654 void LJ_FASTCALL lj_ffh_coroutine_wrap_err(lua_State *L, lua_State *co)
656 co->top--; copyTV(L, L->top, co->top); L->top++;
657 if (tvisstr(L->top-1))
658 lj_err_callermsg(L, strVdata(L->top-1));
659 else
660 lj_err_run(L);
663 /* Forward declaration. */
664 static void setpc_wrap_aux(lua_State *L, GCfunc *fn);
666 LJLIB_CF(coroutine_wrap)
668 GCfunc *fn;
669 lj_cf_coroutine_create(L);
670 fn = lj_lib_pushcc(L, lj_ffh_coroutine_wrap_aux, FF_coroutine_wrap_aux, 1);
671 setpc_wrap_aux(L, fn);
672 return 1;
675 #include "lj_libdef.h"
677 /* Fix the PC of wrap_aux. Really ugly workaround. */
678 static void setpc_wrap_aux(lua_State *L, GCfunc *fn)
680 setmref(fn->c.pc, &L2GG(L)->bcff[lj_lib_init_coroutine[1]+2]);
683 /* ------------------------------------------------------------------------ */
685 static void newproxy_weaktable(lua_State *L)
687 /* NOBARRIER: The table is new (marked white). */
688 GCtab *t = lj_tab_new(L, 0, 1);
689 settabV(L, L->top++, t);
690 setgcref(t->metatable, obj2gco(t));
691 setstrV(L, lj_tab_setstr(L, t, lj_str_newlit(L, "__mode")),
692 lj_str_newlit(L, "kv"));
693 t->nomm = (uint8_t)(~(1u<<MM_mode));
696 LUALIB_API int luaopen_base(lua_State *L)
698 /* NOBARRIER: Table and value are the same. */
699 GCtab *env = tabref(L->env);
700 settabV(L, lj_tab_setstr(L, env, lj_str_newlit(L, "_G")), env);
701 lua_pushliteral(L, LUA_VERSION); /* top-3. */
702 newproxy_weaktable(L); /* top-2. */
703 LJ_LIB_REG(L, "_G", base);
704 LJ_LIB_REG(L, LUA_COLIBNAME, coroutine);
705 return 2;