diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-10-24 15:17:24 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-10-24 15:17:24 -0200 |
| commit | e78cf96c971234ea25e35a9672ef00ea389d843f (patch) | |
| tree | 57dc00fb747c26730acb13a087afb9bc7dcd3216 /ldo.c | |
| parent | 0cb38439560fc2b912d41d3116d2d74c030d13af (diff) | |
| download | lua-e78cf96c971234ea25e35a9672ef00ea389d843f.tar.gz lua-e78cf96c971234ea25e35a9672ef00ea389d843f.tar.bz2 lua-e78cf96c971234ea25e35a9672ef00ea389d843f.zip | |
first version of Cclosures.
Diffstat (limited to '')
| -rw-r--r-- | ldo.c | 95 |
1 files changed, 51 insertions, 44 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: ldo.c,v 1.3 1997/10/16 10:59:34 roberto Exp roberto $ | 2 | ** $Id: ldo.c,v 1.4 1997/10/23 16:26:37 roberto Exp roberto $ |
| 3 | ** Stack and Call structure of Lua | 3 | ** Stack and Call structure of Lua |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -11,6 +11,7 @@ | |||
| 11 | 11 | ||
| 12 | #include "lbuiltin.h" | 12 | #include "lbuiltin.h" |
| 13 | #include "ldo.h" | 13 | #include "ldo.h" |
| 14 | #include "lfunc.h" | ||
| 14 | #include "lgc.h" | 15 | #include "lgc.h" |
| 15 | #include "lmem.h" | 16 | #include "lmem.h" |
| 16 | #include "lobject.h" | 17 | #include "lobject.h" |
| @@ -31,7 +32,7 @@ | |||
| 31 | 32 | ||
| 32 | static TObject initial_stack; | 33 | static TObject initial_stack; |
| 33 | 34 | ||
| 34 | struct Stack luaD_stack = {&initial_stack+1, &initial_stack, &initial_stack}; | 35 | struct Stack luaD_stack = {&initial_stack, &initial_stack, &initial_stack}; |
| 35 | 36 | ||
| 36 | 37 | ||
| 37 | struct C_Lua_Stack luaD_Cstack = {0, 0, 0}; | 38 | struct C_Lua_Stack luaD_Cstack = {0, 0, 0}; |
| @@ -40,6 +41,40 @@ static jmp_buf *errorJmp = NULL; /* current error recover point */ | |||
| 40 | 41 | ||
| 41 | 42 | ||
| 42 | 43 | ||
| 44 | /* | ||
| 45 | ** Error messages | ||
| 46 | */ | ||
| 47 | |||
| 48 | static void auxerrorim (char *form) | ||
| 49 | { | ||
| 50 | lua_Object s = lua_getparam(1); | ||
| 51 | if (lua_isstring(s)) | ||
| 52 | fprintf(stderr, form, lua_getstring(s)); | ||
| 53 | } | ||
| 54 | |||
| 55 | |||
| 56 | static void femergencyerror (void) | ||
| 57 | { | ||
| 58 | auxerrorim("THERE WAS AN ERROR INSIDE AN ERROR METHOD:\n%s\n"); | ||
| 59 | } | ||
| 60 | |||
| 61 | |||
| 62 | static void stderrorim (void) | ||
| 63 | { | ||
| 64 | auxerrorim("lua: %s\n"); | ||
| 65 | } | ||
| 66 | |||
| 67 | |||
| 68 | TObject luaD_errorim; | ||
| 69 | static TObject emergencyerror; | ||
| 70 | |||
| 71 | |||
| 72 | static void initCfunc (TObject *o, lua_CFunction f) | ||
| 73 | { | ||
| 74 | ttype(o) = LUA_T_CPROTO; | ||
| 75 | fvalue(o) = f; | ||
| 76 | luaF_simpleclosure(o); | ||
| 77 | } | ||
| 43 | 78 | ||
| 44 | 79 | ||
| 45 | #define STACK_EXTRA 32 | 80 | #define STACK_EXTRA 32 |
| @@ -50,8 +85,10 @@ static void initstack (int n) | |||
| 50 | luaD_stack.stack = luaM_newvector(maxstack, TObject); | 85 | luaD_stack.stack = luaM_newvector(maxstack, TObject); |
| 51 | luaD_stack.last = luaD_stack.stack+(maxstack-1); | 86 | luaD_stack.last = luaD_stack.stack+(maxstack-1); |
| 52 | luaD_stack.top = luaD_stack.stack; | 87 | luaD_stack.top = luaD_stack.stack; |
| 53 | *(luaD_stack.top++) = initial_stack; | 88 | *luaD_stack.stack = initial_stack; |
| 54 | luaB_predefine(); | 89 | luaB_predefine(); |
| 90 | initCfunc(&luaD_errorim, stderrorim); | ||
| 91 | initCfunc(&emergencyerror, femergencyerror); | ||
| 55 | } | 92 | } |
| 56 | 93 | ||
| 57 | 94 | ||
| @@ -79,7 +116,6 @@ void luaD_checkstack (int n) | |||
| 79 | } | 116 | } |
| 80 | 117 | ||
| 81 | 118 | ||
| 82 | |||
| 83 | /* | 119 | /* |
| 84 | ** Adjust stack. Set top to the given value, pushing NILs if needed. | 120 | ** Adjust stack. Set top to the given value, pushing NILs if needed. |
| 85 | */ | 121 | */ |
| @@ -128,9 +164,9 @@ void luaD_callHook (StkId base, lua_Type type, int isreturn) | |||
| 128 | (*lua_callhook)(LUA_NOOBJECT, "(return)", 0); | 164 | (*lua_callhook)(LUA_NOOBJECT, "(return)", 0); |
| 129 | else { | 165 | else { |
| 130 | TObject *f = luaD_stack.stack+base-1; | 166 | TObject *f = luaD_stack.stack+base-1; |
| 131 | if (type == LUA_T_MARK) | 167 | if (type == LUA_T_PROTO) |
| 132 | (*lua_callhook)(Ref(f), f->value.tf->fileName->str, | 168 | (*lua_callhook)(Ref(f), tfvalue(protovalue(f))->fileName->str, |
| 133 | f->value.tf->lineDefined); | 169 | tfvalue(protovalue(f))->lineDefined); |
| 134 | else | 170 | else |
| 135 | (*lua_callhook)(Ref(f), "(C)", -1); | 171 | (*lua_callhook)(Ref(f), "(C)", -1); |
| 136 | } | 172 | } |
| @@ -153,10 +189,10 @@ static StkId callC (lua_CFunction func, StkId base) | |||
| 153 | luaD_Cstack.lua2C = base; | 189 | luaD_Cstack.lua2C = base; |
| 154 | luaD_Cstack.base = base+luaD_Cstack.num; /* == top-stack */ | 190 | luaD_Cstack.base = base+luaD_Cstack.num; /* == top-stack */ |
| 155 | if (lua_callhook) | 191 | if (lua_callhook) |
| 156 | luaD_callHook(base, LUA_T_CMARK, 0); | 192 | luaD_callHook(base, LUA_T_CPROTO, 0); |
| 157 | (*func)(); | 193 | (*func)(); |
| 158 | if (lua_callhook) /* func may have changed lua_callhook */ | 194 | if (lua_callhook) /* func may have changed lua_callhook */ |
| 159 | luaD_callHook(base, LUA_T_CMARK, 1); | 195 | luaD_callHook(base, LUA_T_CPROTO, 1); |
| 160 | firstResult = luaD_Cstack.base; | 196 | firstResult = luaD_Cstack.base; |
| 161 | luaD_Cstack = oldCLS; | 197 | luaD_Cstack = oldCLS; |
| 162 | return firstResult; | 198 | return firstResult; |
| @@ -182,13 +218,11 @@ void luaD_call (StkId base, int nResults) | |||
| 182 | StkId firstResult; | 218 | StkId firstResult; |
| 183 | TObject *func = luaD_stack.stack+base-1; | 219 | TObject *func = luaD_stack.stack+base-1; |
| 184 | int i; | 220 | int i; |
| 185 | if (ttype(func) == LUA_T_CFUNCTION) { | 221 | if (ttype(func) == LUA_T_FUNCTION) { |
| 186 | ttype(func) = LUA_T_CMARK; | 222 | TObject *proto = protovalue(func); |
| 187 | firstResult = callC(fvalue(func), base); | ||
| 188 | } | ||
| 189 | else if (ttype(func) == LUA_T_FUNCTION) { | ||
| 190 | ttype(func) = LUA_T_MARK; | 223 | ttype(func) = LUA_T_MARK; |
| 191 | firstResult = luaV_execute(func->value.cl, base); | 224 | firstResult = (ttype(proto) == LUA_T_CPROTO) ? callC(fvalue(proto), base) |
| 225 | : luaV_execute(func->value.cl, base); | ||
| 192 | } | 226 | } |
| 193 | else { /* func is not a function */ | 227 | else { /* func is not a function */ |
| 194 | /* Check the tag method for invalid functions */ | 228 | /* Check the tag method for invalid functions */ |
| @@ -222,39 +256,12 @@ void luaD_travstack (int (*fn)(TObject *)) | |||
| 222 | } | 256 | } |
| 223 | 257 | ||
| 224 | 258 | ||
| 225 | /* | ||
| 226 | ** Error messages | ||
| 227 | */ | ||
| 228 | |||
| 229 | static void auxerrorim (char *form) | ||
| 230 | { | ||
| 231 | lua_Object s = lua_getparam(1); | ||
| 232 | if (lua_isstring(s)) | ||
| 233 | fprintf(stderr, form, lua_getstring(s)); | ||
| 234 | } | ||
| 235 | |||
| 236 | |||
| 237 | static void emergencyerrorf (void) | ||
| 238 | { | ||
| 239 | auxerrorim("THERE WAS AN ERROR INSIDE AN ERROR METHOD:\n%s\n"); | ||
| 240 | } | ||
| 241 | |||
| 242 | |||
| 243 | static void stderrorim (void) | ||
| 244 | { | ||
| 245 | auxerrorim("lua: %s\n"); | ||
| 246 | } | ||
| 247 | |||
| 248 | |||
| 249 | TObject luaD_errorim = {LUA_T_CFUNCTION, {stderrorim}}; | ||
| 250 | |||
| 251 | 259 | ||
| 252 | static void message (char *s) | 260 | static void message (char *s) |
| 253 | { | 261 | { |
| 254 | TObject im = luaD_errorim; | 262 | TObject im = luaD_errorim; |
| 255 | if (ttype(&im) != LUA_T_NIL) { | 263 | if (ttype(&im) != LUA_T_NIL) { |
| 256 | luaD_errorim.ttype = LUA_T_CFUNCTION; | 264 | luaD_errorim = emergencyerror; |
| 257 | luaD_errorim.value.f = emergencyerrorf; | ||
| 258 | lua_pushstring(s); | 265 | lua_pushstring(s); |
| 259 | luaD_callTM(&im, 1, 0); | 266 | luaD_callTM(&im, 1, 0); |
| 260 | luaD_errorim = im; | 267 | luaD_errorim = im; |
| @@ -291,7 +298,7 @@ static void do_callinc (int nResults) | |||
| 291 | 298 | ||
| 292 | /* | 299 | /* |
| 293 | ** Execute a protected call. Assumes that function is at luaD_Cstack.base and | 300 | ** Execute a protected call. Assumes that function is at luaD_Cstack.base and |
| 294 | ** parameters are on luaD_stack.top of it. Leave nResults on the luaD_stack.stack. | 301 | ** parameters are on top of it. Leave nResults on the stack. |
| 295 | */ | 302 | */ |
| 296 | int luaD_protectedrun (int nResults) | 303 | int luaD_protectedrun (int nResults) |
| 297 | { | 304 | { |
