diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2010-03-08 13:55:52 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2010-03-08 13:55:52 -0300 |
commit | 1124cb12474b4398384bdefbf12a0769521e17bb (patch) | |
tree | 3467e74b2628626204cc094e154a3e437a79f32e | |
parent | c2951478741efaddd34f468fe33a40a786371a3a (diff) | |
download | lua-1124cb12474b4398384bdefbf12a0769521e17bb.tar.gz lua-1124cb12474b4398384bdefbf12a0769521e17bb.tar.bz2 lua-1124cb12474b4398384bdefbf12a0769521e17bb.zip |
first step towards _ENV: all chunks have an puvalues _ENV with the
global table
-rw-r--r-- | lapi.c | 8 | ||||
-rw-r--r-- | llex.h | 3 | ||||
-rw-r--r-- | lparser.c | 35 |
3 files changed, 33 insertions, 13 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lapi.c,v 2.112 2010/01/21 16:49:21 roberto Exp roberto $ | 2 | ** $Id: lapi.c,v 2.113 2010/02/09 11:55:37 roberto Exp roberto $ |
3 | ** Lua API | 3 | ** Lua API |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -882,6 +882,12 @@ LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data, | |||
882 | if (!chunkname) chunkname = "?"; | 882 | if (!chunkname) chunkname = "?"; |
883 | luaZ_init(L, &z, reader, data); | 883 | luaZ_init(L, &z, reader, data); |
884 | status = luaD_protectedparser(L, &z, chunkname); | 884 | status = luaD_protectedparser(L, &z, chunkname); |
885 | if (status == LUA_OK) { | ||
886 | Closure *f = clvalue(L->top - 1); | ||
887 | lua_assert(!f->c.isC); | ||
888 | if (f->l.nupvalues == 1) | ||
889 | sethvalue(L, f->l.upvals[0]->v, G(L)->l_gt); | ||
890 | } | ||
885 | lua_unlock(L); | 891 | lua_unlock(L); |
886 | return status; | 892 | return status; |
887 | } | 893 | } |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: llex.h,v 1.61 2007/10/25 16:45:47 roberto Exp roberto $ | 2 | ** $Id: llex.h,v 1.62 2009/10/11 20:02:19 roberto Exp roberto $ |
3 | ** Lexical Analyzer | 3 | ** Lexical Analyzer |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -60,6 +60,7 @@ typedef struct LexState { | |||
60 | Mbuffer *buff; /* buffer for tokens */ | 60 | Mbuffer *buff; /* buffer for tokens */ |
61 | struct Varlist *varl; /* list of all active local variables */ | 61 | struct Varlist *varl; /* list of all active local variables */ |
62 | TString *source; /* current source name */ | 62 | TString *source; /* current source name */ |
63 | TString *envn; /* name of environment variable */ | ||
63 | char decpoint; /* locale decimal point */ | 64 | char decpoint; /* locale decimal point */ |
64 | } LexState; | 65 | } LexState; |
65 | 66 | ||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lparser.c,v 2.76 2010/02/26 20:40:29 roberto Exp roberto $ | 2 | ** $Id: lparser.c,v 2.77 2010/03/04 18:12:57 roberto Exp roberto $ |
3 | ** Lua Parser | 3 | ** Lua Parser |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -224,10 +224,10 @@ static int newupvalue (FuncState *fs, TString *name, expdesc *v) { | |||
224 | luaM_growvector(fs->L, f->upvalues, fs->nups, f->sizeupvalues, | 224 | luaM_growvector(fs->L, f->upvalues, fs->nups, f->sizeupvalues, |
225 | Upvaldesc, UCHAR_MAX, "upvalues"); | 225 | Upvaldesc, UCHAR_MAX, "upvalues"); |
226 | while (oldsize < f->sizeupvalues) f->upvalues[oldsize++].name = NULL; | 226 | while (oldsize < f->sizeupvalues) f->upvalues[oldsize++].name = NULL; |
227 | f->upvalues[fs->nups].name = name; | ||
228 | luaC_objbarrier(fs->L, f, name); | ||
229 | f->upvalues[fs->nups].instack = (v->k == VLOCAL); | 227 | f->upvalues[fs->nups].instack = (v->k == VLOCAL); |
230 | f->upvalues[fs->nups].idx = cast_byte(v->u.s.info); | 228 | f->upvalues[fs->nups].idx = cast_byte(v->u.s.info); |
229 | f->upvalues[fs->nups].name = name; | ||
230 | luaC_objbarrier(fs->L, f, name); | ||
231 | return fs->nups++; | 231 | return fs->nups++; |
232 | } | 232 | } |
233 | 233 | ||
@@ -430,26 +430,39 @@ static void close_func (LexState *ls) { | |||
430 | } | 430 | } |
431 | 431 | ||
432 | 432 | ||
433 | /* | ||
434 | ** opens the main function, which is a regular vararg function with an | ||
435 | ** upvalue named '_ENV' | ||
436 | */ | ||
437 | static void open_mainfunc (lua_State *L, LexState *ls, FuncState *fs) { | ||
438 | expdesc v; | ||
439 | open_func(ls, fs); | ||
440 | fs->f->is_vararg = 1; /* main function is always vararg */ | ||
441 | ls->envn = luaS_new(L, "_ENV"); /* create '_ENV' string */ | ||
442 | setsvalue2s(L, L->top++, ls->envn); /* anchor it */ | ||
443 | init_exp(&v, VLOCAL, 0); | ||
444 | newupvalue(fs, ls->envn, &v); /* create '_ENV' upvalue */ | ||
445 | L->top--; /* now string is anchored as an upvalue name */ | ||
446 | } | ||
447 | |||
448 | |||
433 | Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, Varlist *varl, | 449 | Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, Varlist *varl, |
434 | const char *name) { | 450 | const char *name) { |
435 | struct LexState lexstate; | 451 | LexState lexstate; |
436 | struct FuncState funcstate; | 452 | FuncState funcstate; |
437 | TString *tname = luaS_new(L, name); | 453 | TString *tname = luaS_new(L, name); |
438 | setsvalue2s(L, L->top, tname); /* push name to protect it */ | 454 | setsvalue2s(L, L->top, tname); /* push name to protect it */ |
439 | incr_top(L); | 455 | incr_top(L); |
440 | lexstate.buff = buff; | 456 | lexstate.buff = buff; |
441 | lexstate.varl = varl; | 457 | lexstate.varl = varl; |
442 | luaX_setinput(L, &lexstate, z, tname); | 458 | luaX_setinput(L, &lexstate, z, tname); |
443 | open_func(&lexstate, &funcstate); | 459 | open_mainfunc(L, &lexstate, &funcstate); |
444 | funcstate.f->is_vararg = 1; /* main function is always vararg */ | ||
445 | luaX_next(&lexstate); /* read first token */ | 460 | luaX_next(&lexstate); /* read first token */ |
446 | chunk(&lexstate); | 461 | chunk(&lexstate); /* read main chunk */ |
447 | check(&lexstate, TK_EOS); | 462 | check(&lexstate, TK_EOS); |
448 | close_func(&lexstate); | 463 | close_func(&lexstate); |
449 | L->top--; /* pop name */ | 464 | L->top--; /* pop name */ |
450 | lua_assert(funcstate.prev == NULL); | 465 | lua_assert(!funcstate.prev && funcstate.nups == 1 && !lexstate.fs); |
451 | lua_assert(funcstate.nups == 0); | ||
452 | lua_assert(lexstate.fs == NULL); | ||
453 | return funcstate.f; | 466 | return funcstate.f; |
454 | } | 467 | } |
455 | 468 | ||