aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1998-12-30 15:26:49 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1998-12-30 15:26:49 -0200
commit59f8e6fb7710e46539fb9bf63131b45012bf86ef (patch)
tree2077a5d755afd0346994a53918c881cca8357668
parent05d89b5c05bdf3be8f6fd3c6d195672f1e10d13e (diff)
downloadlua-59f8e6fb7710e46539fb9bf63131b45012bf86ef.tar.gz
lua-59f8e6fb7710e46539fb9bf63131b45012bf86ef.tar.bz2
lua-59f8e6fb7710e46539fb9bf63131b45012bf86ef.zip
details ("settable")
-rw-r--r--lapi.c12
-rw-r--r--lvm.c76
-rw-r--r--lvm.h5
3 files changed, 50 insertions, 43 deletions
diff --git a/lapi.c b/lapi.c
index 65cd28b4..d5c16369 100644
--- a/lapi.c
+++ b/lapi.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lapi.c,v 1.28 1998/09/07 18:59:59 roberto Exp roberto $ 2** $Id: lapi.c,v 1.29 1998/12/03 15:45:15 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*/
@@ -179,17 +179,15 @@ lua_Object lua_rawgettable (void)
179} 179}
180 180
181 181
182void lua_settable (void) 182void lua_settable (void) {
183{
184 checkCparams(3); 183 checkCparams(3);
185 luaV_settable(L->stack.top-3, 1); 184 luaV_settable(L->stack.top-3, 0);
186} 185}
187 186
188 187
189void lua_rawsettable (void) 188void lua_rawsettable (void) {
190{
191 checkCparams(3); 189 checkCparams(3);
192 luaV_settable(L->stack.top-3, 0); 190 luaV_rawsettable(L->stack.top-3);
193} 191}
194 192
195 193
diff --git a/lvm.c b/lvm.c
index 85e570b4..df78f909 100644
--- a/lvm.c
+++ b/lvm.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lvm.c,v 1.34 1998/12/27 20:25:20 roberto Exp roberto $ 2** $Id: lvm.c,v 1.35 1998/12/30 13:16:50 roberto Exp roberto $
3** Lua virtual machine 3** Lua virtual machine
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -95,10 +95,8 @@ int luaV_tostring (TObject *obj) {
95 95
96void luaV_setn (Hash *t, int val) { 96void luaV_setn (Hash *t, int val) {
97 TObject index, value; 97 TObject index, value;
98 ttype(&index) = LUA_T_STRING; 98 ttype(&index) = LUA_T_STRING; tsvalue(&index) = luaS_new("n");
99 tsvalue(&index) = luaS_new("n"); 99 ttype(&value) = LUA_T_NUMBER; nvalue(&value) = val;
100 ttype(&value) = LUA_T_NUMBER;
101 nvalue(&value) = val;
102 *(luaH_set(t, &index)) = value; 100 *(luaH_set(t, &index)) = value;
103} 101}
104 102
@@ -121,8 +119,7 @@ void luaV_closure (int nelems)
121** Function to index a table. 119** Function to index a table.
122** Receives the table at top-2 and the index at top-1. 120** Receives the table at top-2 and the index at top-1.
123*/ 121*/
124void luaV_gettable (void) 122void luaV_gettable (void) {
125{
126 struct Stack *S = &L->stack; 123 struct Stack *S = &L->stack;
127 TObject *im; 124 TObject *im;
128 if (ttype(S->top-2) != LUA_T_ARRAY) /* not a table, get "gettable" method */ 125 if (ttype(S->top-2) != LUA_T_ARRAY) /* not a table, get "gettable" method */
@@ -147,40 +144,51 @@ void luaV_gettable (void)
147 /* else it has a "gettable" method, go through to next command */ 144 /* else it has a "gettable" method, go through to next command */
148 } 145 }
149 /* object is not a table, or it has a "gettable" method */ 146 /* object is not a table, or it has a "gettable" method */
150 if (ttype(im) != LUA_T_NIL) 147 if (ttype(im) == LUA_T_NIL)
151 luaD_callTM(im, 2, 1);
152 else
153 lua_error("indexed expression not a table"); 148 lua_error("indexed expression not a table");
149 luaD_callTM(im, 2, 1);
154} 150}
155 151
156 152
157/* 153/*
158** Function to store indexed based on values at the stack.top 154** Function to store indexed based on values at the stack.top
159** mode = 0: raw store (without tag methods) 155** deep = 1: "deep L->stack.stack" store (with tag methods)
160** mode = 1: normal store (with tag methods)
161** mode = 2: "deep L->stack.stack" store (with tag methods)
162*/ 156*/
163void luaV_settable (TObject *t, int mode) 157void luaV_settable (TObject *t, int deep) {
164{
165 struct Stack *S = &L->stack; 158 struct Stack *S = &L->stack;
166 TObject *im = (mode == 0) ? NULL : luaT_getimbyObj(t, IM_SETTABLE); 159 TObject *im;
167 if (ttype(t) == LUA_T_ARRAY && (im == NULL || ttype(im) == LUA_T_NIL)) { 160 if (ttype(t) != LUA_T_ARRAY) /* not a table, get "settable" method */
168 TObject *h = luaH_set(avalue(t), t+1); 161 im = luaT_getimbyObj(t, IM_SETTABLE);
169 *h = *(S->top-1); 162 else { /* object is a table... */
170 S->top -= (mode == 2) ? 1 : 3; 163 im = luaT_getim(avalue(t)->htag, IM_SETTABLE);
171 } 164 if (ttype(im) == LUA_T_NIL) { /* and does not have a "settable" method */
172 else { /* object is not a table, and/or has a specific "settable" method */ 165 *(luaH_set(avalue(t), t+1)) = *(S->top-1);
173 if (im && ttype(im) != LUA_T_NIL) { 166 /* if deep, pop only value; otherwise, pop table, index and value */
174 if (mode == 2) { 167 S->top -= (deep) ? 1 : 3;
175 *(S->top+1) = *(L->stack.top-1); 168 return;
176 *(S->top) = *(t+1);
177 *(S->top-1) = *t;
178 S->top += 2; /* WARNING: caller must assure stack space */
179 }
180 luaD_callTM(im, 3, 0);
181 } 169 }
182 else 170 /* else it has a "settable" method, go through to next command */
183 lua_error("indexed expression not a table"); 171 }
172 /* object is not a table, or it has a "settable" method */
173 if (ttype(im) == LUA_T_NIL)
174 lua_error("indexed expression not a table");
175 if (deep) { /* table and index were not on top; copy them */
176 *(S->top+1) = *(L->stack.top-1);
177 *(S->top) = *(t+1);
178 *(S->top-1) = *t;
179 S->top += 2; /* WARNING: caller must assure stack space */
180 }
181 luaD_callTM(im, 3, 0);
182}
183
184
185void luaV_rawsettable (TObject *t) {
186 if (ttype(t) != LUA_T_ARRAY)
187 lua_error("indexed expression not a table");
188 else {
189 struct Stack *S = &L->stack;
190 *(luaH_set(avalue(t), t+1)) = *(S->top-1);
191 S->top -= 3;
184 } 192 }
185} 193}
186 194
@@ -462,11 +470,11 @@ StkId luaV_execute (Closure *cl, TProtoFunc *tf, StkId base)
462 break; 470 break;
463 471
464 case SETTABLE0: 472 case SETTABLE0:
465 luaV_settable(S->top-3, 1); 473 luaV_settable(S->top-3, 0);
466 break; 474 break;
467 475
468 case SETTABLE: 476 case SETTABLE:
469 luaV_settable(S->top-3-(*pc++), 2); 477 luaV_settable(S->top-3-(*pc++), 1);
470 break; 478 break;
471 479
472 case SETLISTW: 480 case SETLISTW:
diff --git a/lvm.h b/lvm.h
index 7c9b5a8c..ee962945 100644
--- a/lvm.h
+++ b/lvm.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lvm.h,v 1.5 1998/07/12 16:16:43 roberto Exp roberto $ 2** $Id: lvm.h,v 1.6 1998/12/30 13:16:50 roberto Exp roberto $
3** Lua virtual machine 3** Lua virtual machine
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -22,7 +22,8 @@ int luaV_tonumber (TObject *obj);
22int luaV_tostring (TObject *obj); 22int luaV_tostring (TObject *obj);
23void luaV_setn (Hash *t, int val); 23void luaV_setn (Hash *t, int val);
24void luaV_gettable (void); 24void luaV_gettable (void);
25void luaV_settable (TObject *t, int mode); 25void luaV_settable (TObject *t, int deep);
26void luaV_rawsettable (TObject *t);
26void luaV_getglobal (TaggedString *ts); 27void luaV_getglobal (TaggedString *ts);
27void luaV_setglobal (TaggedString *ts); 28void luaV_setglobal (TaggedString *ts);
28StkId luaV_execute (Closure *cl, TProtoFunc *tf, StkId base); 29StkId luaV_execute (Closure *cl, TProtoFunc *tf, StkId base);