diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib_base.c | 67 |
1 files changed, 41 insertions, 26 deletions
diff --git a/src/lib_base.c b/src/lib_base.c index d5f5b0a6..1d736b24 100644 --- a/src/lib_base.c +++ b/src/lib_base.c | |||
| @@ -341,28 +341,31 @@ LJLIB_ASM_(xpcall) LJLIB_REC(.) | |||
| 341 | 341 | ||
| 342 | /* -- Base library: load Lua code ----------------------------------------- */ | 342 | /* -- Base library: load Lua code ----------------------------------------- */ |
| 343 | 343 | ||
| 344 | static int load_aux(lua_State *L, int status) | 344 | static int load_aux(lua_State *L, int status, int envarg) |
| 345 | { | 345 | { |
| 346 | if (status == 0) | 346 | if (status == 0) { |
| 347 | if (tvistab(L->base+envarg-1)) { | ||
| 348 | GCfunc *fn = funcV(L->top-1); | ||
| 349 | GCtab *t = tabV(L->base+envarg-1); | ||
| 350 | setgcref(fn->c.env, obj2gco(t)); | ||
| 351 | lj_gc_objbarrier(L, fn, t); | ||
| 352 | } | ||
| 347 | return 1; | 353 | return 1; |
| 348 | copyTV(L, L->top, L->top-1); | 354 | } else { |
| 349 | setnilV(L->top-1); | 355 | setnilV(L->top-2); |
| 350 | L->top++; | 356 | return 2; |
| 351 | return 2; | 357 | } |
| 352 | } | ||
| 353 | |||
| 354 | LJLIB_CF(loadstring) | ||
| 355 | { | ||
| 356 | GCstr *s = lj_lib_checkstr(L, 1); | ||
| 357 | GCstr *name = lj_lib_optstr(L, 2); | ||
| 358 | return load_aux(L, | ||
| 359 | luaL_loadbuffer(L, strdata(s), s->len, strdata(name ? name : s))); | ||
| 360 | } | 358 | } |
| 361 | 359 | ||
| 362 | LJLIB_CF(loadfile) | 360 | LJLIB_CF(loadfile) |
| 363 | { | 361 | { |
| 364 | GCstr *fname = lj_lib_optstr(L, 1); | 362 | GCstr *fname = lj_lib_optstr(L, 1); |
| 365 | return load_aux(L, luaL_loadfile(L, fname ? strdata(fname) : NULL)); | 363 | GCstr *mode = lj_lib_optstr(L, 2); |
| 364 | int status; | ||
| 365 | lua_settop(L, 3); /* Ensure env arg exists. */ | ||
| 366 | status = luaL_loadfilex(L, fname ? strdata(fname) : NULL, | ||
| 367 | mode ? strdata(mode) : NULL); | ||
| 368 | return load_aux(L, status, 3); | ||
| 366 | } | 369 | } |
| 367 | 370 | ||
| 368 | static const char *reader_func(lua_State *L, void *ud, size_t *size) | 371 | static const char *reader_func(lua_State *L, void *ud, size_t *size) |
| @@ -376,8 +379,8 @@ static const char *reader_func(lua_State *L, void *ud, size_t *size) | |||
| 376 | *size = 0; | 379 | *size = 0; |
| 377 | return NULL; | 380 | return NULL; |
| 378 | } else if (tvisstr(L->top) || tvisnumber(L->top)) { | 381 | } else if (tvisstr(L->top) || tvisnumber(L->top)) { |
| 379 | copyTV(L, L->base+2, L->top); /* Anchor string in reserved stack slot. */ | 382 | copyTV(L, L->base+4, L->top); /* Anchor string in reserved stack slot. */ |
| 380 | return lua_tolstring(L, 3, size); | 383 | return lua_tolstring(L, 5, size); |
| 381 | } else { | 384 | } else { |
| 382 | lj_err_caller(L, LJ_ERR_RDRSTR); | 385 | lj_err_caller(L, LJ_ERR_RDRSTR); |
| 383 | return NULL; | 386 | return NULL; |
| @@ -386,14 +389,26 @@ static const char *reader_func(lua_State *L, void *ud, size_t *size) | |||
| 386 | 389 | ||
| 387 | LJLIB_CF(load) | 390 | LJLIB_CF(load) |
| 388 | { | 391 | { |
| 389 | GCstr *name; | 392 | GCstr *name = lj_lib_optstr(L, 2); |
| 390 | if (L->base < L->top && (tvisstr(L->base) || tvisnumber(L->base))) | 393 | GCstr *mode = lj_lib_optstr(L, 3); |
| 391 | return lj_cf_loadstring(L); | 394 | int status; |
| 392 | lj_lib_checkfunc(L, 1); | 395 | if (L->base < L->top && (tvisstr(L->base) || tvisnumber(L->base))) { |
| 393 | name = lj_lib_optstr(L, 2); | 396 | GCstr *s = lj_lib_checkstr(L, 1); |
| 394 | lua_settop(L, 3); /* Reserve a slot for the string from the reader. */ | 397 | lua_settop(L, 4); /* Ensure env arg exists. */ |
| 395 | return load_aux(L, | 398 | status = luaL_loadbufferx(L, strdata(s), s->len, strdata(name ? name : s), |
| 396 | lua_load(L, reader_func, NULL, name ? strdata(name) : "=(load)")); | 399 | mode ? strdata(mode) : NULL); |
| 400 | } else { | ||
| 401 | lj_lib_checkfunc(L, 1); | ||
| 402 | lua_settop(L, 5); /* Reserve a slot for the string from the reader. */ | ||
| 403 | status = lua_loadx(L, reader_func, NULL, name ? strdata(name) : "=(load)", | ||
| 404 | mode ? strdata(mode) : NULL); | ||
| 405 | } | ||
| 406 | return load_aux(L, status, 4); | ||
| 407 | } | ||
| 408 | |||
| 409 | LJLIB_CF(loadstring) | ||
| 410 | { | ||
| 411 | return lj_cf_load(L); | ||
| 397 | } | 412 | } |
| 398 | 413 | ||
| 399 | LJLIB_CF(dofile) | 414 | LJLIB_CF(dofile) |
