diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-02-26 14:38:41 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-02-26 14:38:41 -0300 |
| commit | 131d66efd2dc26b05bcb2bdaf67f5175f3eda1aa (patch) | |
| tree | e912e35e04883c24e89917ee3d94b8fa574f6294 /inout.c | |
| parent | bbf1b3060a1aa4e5ec3235a560d3d054457e957d (diff) | |
| download | lua-131d66efd2dc26b05bcb2bdaf67f5175f3eda1aa.tar.gz lua-131d66efd2dc26b05bcb2bdaf67f5175f3eda1aa.tar.bz2 lua-131d66efd2dc26b05bcb2bdaf67f5175f3eda1aa.zip | |
first step in implementing internal methods.
Diffstat (limited to 'inout.c')
| -rw-r--r-- | inout.c | 205 |
1 files changed, 141 insertions, 64 deletions
| @@ -5,7 +5,7 @@ | |||
| 5 | ** Also provides some predefined lua functions. | 5 | ** Also provides some predefined lua functions. |
| 6 | */ | 6 | */ |
| 7 | 7 | ||
| 8 | char *rcs_inout="$Id: inout.c,v 2.42 1996/09/24 21:46:44 roberto Exp roberto $"; | 8 | char *rcs_inout="$Id: inout.c,v 2.43 1996/09/25 12:57:22 roberto Exp roberto $"; |
| 9 | 9 | ||
| 10 | #include <stdio.h> | 10 | #include <stdio.h> |
| 11 | #include <string.h> | 11 | #include <string.h> |
| @@ -16,7 +16,9 @@ char *rcs_inout="$Id: inout.c,v 2.42 1996/09/24 21:46:44 roberto Exp roberto $"; | |||
| 16 | #include "table.h" | 16 | #include "table.h" |
| 17 | #include "tree.h" | 17 | #include "tree.h" |
| 18 | #include "lua.h" | 18 | #include "lua.h" |
| 19 | #include "hash.h" | ||
| 19 | #include "mem.h" | 20 | #include "mem.h" |
| 21 | #include "fallback.h" | ||
| 20 | 22 | ||
| 21 | 23 | ||
| 22 | /* Exported variables */ | 24 | /* Exported variables */ |
| @@ -109,6 +111,21 @@ static void check_arg (int cond, char *func) | |||
| 109 | } | 111 | } |
| 110 | } | 112 | } |
| 111 | 113 | ||
| 114 | static char *check_string (int numArg, char *funcname) | ||
| 115 | { | ||
| 116 | lua_Object o = lua_getparam(numArg); | ||
| 117 | check_arg(lua_isstring(o), funcname); | ||
| 118 | return lua_getstring(o); | ||
| 119 | } | ||
| 120 | |||
| 121 | static int check_number (int numArg, char *funcname) | ||
| 122 | { | ||
| 123 | lua_Object o = lua_getparam(numArg); | ||
| 124 | check_arg(lua_isnumber(o), funcname); | ||
| 125 | return (int)lua_getnumber(o); | ||
| 126 | } | ||
| 127 | |||
| 128 | |||
| 112 | 129 | ||
| 113 | static int passresults (void) | 130 | static int passresults (void) |
| 114 | { | 131 | { |
| @@ -122,10 +139,9 @@ static int passresults (void) | |||
| 122 | /* | 139 | /* |
| 123 | ** Internal function: do a string | 140 | ** Internal function: do a string |
| 124 | */ | 141 | */ |
| 125 | void lua_internaldostring (void) | 142 | static void lua_internaldostring (void) |
| 126 | { | 143 | { |
| 127 | lua_Object obj = lua_getparam (1); | 144 | if (lua_dostring(check_string(1, "dostring")) == 0) |
| 128 | if (lua_isstring(obj) && lua_dostring(lua_getstring(obj)) == 0) | ||
| 129 | if (passresults() == 0) | 145 | if (passresults() == 0) |
| 130 | lua_pushuserdata(NULL); /* at least one result to signal no errors */ | 146 | lua_pushuserdata(NULL); /* at least one result to signal no errors */ |
| 131 | } | 147 | } |
| @@ -133,7 +149,7 @@ void lua_internaldostring (void) | |||
| 133 | /* | 149 | /* |
| 134 | ** Internal function: do a file | 150 | ** Internal function: do a file |
| 135 | */ | 151 | */ |
| 136 | void lua_internaldofile (void) | 152 | static void lua_internaldofile (void) |
| 137 | { | 153 | { |
| 138 | lua_Object obj = lua_getparam (1); | 154 | lua_Object obj = lua_getparam (1); |
| 139 | char *fname = NULL; | 155 | char *fname = NULL; |
| @@ -150,36 +166,24 @@ void lua_internaldofile (void) | |||
| 150 | 166 | ||
| 151 | static char *tostring (lua_Object obj) | 167 | static char *tostring (lua_Object obj) |
| 152 | { | 168 | { |
| 153 | char *buff = luaI_buffer(20); | ||
| 154 | if (lua_isstring(obj)) /* get strings and numbers */ | 169 | if (lua_isstring(obj)) /* get strings and numbers */ |
| 155 | return lua_getstring(obj); | 170 | return lua_getstring(obj); |
| 156 | else switch(lua_type(obj)) | 171 | else if (lua_istable(obj)) |
| 157 | { | 172 | return "<table>"; |
| 158 | case LUA_T_FUNCTION: | 173 | else if (lua_isfunction(obj)) |
| 159 | sprintf(buff, "function: %p", (luaI_Address(obj))->value.tf); | 174 | return "<function>"; |
| 160 | break; | 175 | else if (lua_isnil(obj)) |
| 161 | case LUA_T_CFUNCTION: | 176 | return "nil"; |
| 162 | sprintf(buff, "cfunction: %p", lua_getcfunction(obj)); | 177 | else /* if (lua_isuserdata(obj)) */ |
| 163 | break; | 178 | return "<userdata>"; |
| 164 | case LUA_T_ARRAY: | ||
| 165 | sprintf(buff, "table: %p", avalue(luaI_Address(obj))); | ||
| 166 | break; | ||
| 167 | case LUA_T_NIL: | ||
| 168 | sprintf(buff, "nil"); | ||
| 169 | break; | ||
| 170 | default: | ||
| 171 | sprintf(buff, "userdata: %p", lua_getuserdata(obj)); | ||
| 172 | break; | ||
| 173 | } | ||
| 174 | return buff; | ||
| 175 | } | 179 | } |
| 176 | 180 | ||
| 177 | void luaI_tostring (void) | 181 | static void luaI_tostring (void) |
| 178 | { | 182 | { |
| 179 | lua_pushstring(tostring(lua_getparam(1))); | 183 | lua_pushstring(tostring(lua_getparam(1))); |
| 180 | } | 184 | } |
| 181 | 185 | ||
| 182 | void luaI_print (void) | 186 | static void luaI_print (void) |
| 183 | { | 187 | { |
| 184 | int i = 1; | 188 | int i = 1; |
| 185 | lua_Object obj; | 189 | lua_Object obj; |
| @@ -190,42 +194,35 @@ void luaI_print (void) | |||
| 190 | /* | 194 | /* |
| 191 | ** Internal function: return an object type. | 195 | ** Internal function: return an object type. |
| 192 | */ | 196 | */ |
| 193 | void luaI_type (void) | 197 | static void luaI_type (void) |
| 194 | { | 198 | { |
| 195 | lua_Object o = lua_getparam(1); | 199 | lua_Object o = lua_getparam(1); |
| 196 | int t; | 200 | int t = lua_tag(o); |
| 197 | if (o == LUA_NOOBJECT) | 201 | char *s; |
| 202 | if (t == LUA_T_NUMBER) | ||
| 203 | s = "number"; | ||
| 204 | else if (lua_isstring(o)) | ||
| 205 | s = "string"; | ||
| 206 | else if (lua_istable(o)) | ||
| 207 | s = "table"; | ||
| 208 | else if (lua_isnil(o)) | ||
| 209 | s = "nil"; | ||
| 210 | else if (lua_isfunction(o)) | ||
| 211 | s = "function"; | ||
| 212 | else if (lua_isuserdata(o)) | ||
| 213 | s = "userdata"; | ||
| 214 | else { | ||
| 198 | lua_error("no parameter to function 'type'"); | 215 | lua_error("no parameter to function 'type'"); |
| 199 | t = lua_type(o); | 216 | return; /* to avoid warnings */ |
| 200 | switch (t) | ||
| 201 | { | ||
| 202 | case LUA_T_NIL : | ||
| 203 | lua_pushliteral("nil"); | ||
| 204 | break; | ||
| 205 | case LUA_T_NUMBER : | ||
| 206 | lua_pushliteral("number"); | ||
| 207 | break; | ||
| 208 | case LUA_T_STRING : | ||
| 209 | lua_pushliteral("string"); | ||
| 210 | break; | ||
| 211 | case LUA_T_ARRAY : | ||
| 212 | lua_pushliteral("table"); | ||
| 213 | break; | ||
| 214 | case LUA_T_FUNCTION : | ||
| 215 | case LUA_T_CFUNCTION : | ||
| 216 | lua_pushliteral("function"); | ||
| 217 | break; | ||
| 218 | default : | ||
| 219 | lua_pushliteral("userdata"); | ||
| 220 | break; | ||
| 221 | } | 217 | } |
| 218 | lua_pushliteral(s); | ||
| 222 | lua_pushnumber(t); | 219 | lua_pushnumber(t); |
| 223 | } | 220 | } |
| 224 | 221 | ||
| 225 | /* | 222 | /* |
| 226 | ** Internal function: convert an object to a number | 223 | ** Internal function: convert an object to a number |
| 227 | */ | 224 | */ |
| 228 | void lua_obj2number (void) | 225 | static void lua_obj2number (void) |
| 229 | { | 226 | { |
| 230 | lua_Object o = lua_getparam(1); | 227 | lua_Object o = lua_getparam(1); |
| 231 | if (lua_isnumber(o)) | 228 | if (lua_isnumber(o)) |
| @@ -233,39 +230,36 @@ void lua_obj2number (void) | |||
| 233 | } | 230 | } |
| 234 | 231 | ||
| 235 | 232 | ||
| 236 | void luaI_error (void) | 233 | static void luaI_error (void) |
| 237 | { | 234 | { |
| 238 | char *s = lua_getstring(lua_getparam(1)); | 235 | char *s = lua_getstring(lua_getparam(1)); |
| 239 | if (s == NULL) s = "(no message)"; | 236 | if (s == NULL) s = "(no message)"; |
| 240 | lua_error(s); | 237 | lua_error(s); |
| 241 | } | 238 | } |
| 242 | 239 | ||
| 243 | void luaI_assert (void) | 240 | static void luaI_assert (void) |
| 244 | { | 241 | { |
| 245 | lua_Object p = lua_getparam(1); | 242 | lua_Object p = lua_getparam(1); |
| 246 | if (p == LUA_NOOBJECT || lua_isnil(p)) | 243 | if (p == LUA_NOOBJECT || lua_isnil(p)) |
| 247 | lua_error("assertion failed!"); | 244 | lua_error("assertion failed!"); |
| 248 | } | 245 | } |
| 249 | 246 | ||
| 250 | void luaI_setglobal (void) | 247 | static void luaI_setglobal (void) |
| 251 | { | 248 | { |
| 252 | lua_Object name = lua_getparam(1); | ||
| 253 | lua_Object value = lua_getparam(2); | 249 | lua_Object value = lua_getparam(2); |
| 254 | check_arg(lua_isstring(name), "setglobal"); | 250 | check_arg(value != LUA_NOOBJECT, "setglobal"); |
| 255 | lua_pushobject(value); | 251 | lua_pushobject(value); |
| 256 | lua_storeglobal(lua_getstring(name)); | 252 | lua_storeglobal(check_string(1, "setglobal")); |
| 257 | lua_pushobject(value); /* return given value */ | 253 | lua_pushobject(value); /* return given value */ |
| 258 | } | 254 | } |
| 259 | 255 | ||
| 260 | void luaI_getglobal (void) | 256 | static void luaI_getglobal (void) |
| 261 | { | 257 | { |
| 262 | lua_Object name = lua_getparam(1); | 258 | lua_pushobject(lua_getglobal(check_string(1, "getglobal"))); |
| 263 | check_arg(lua_isstring(name), "getglobal"); | ||
| 264 | lua_pushobject(lua_getglobal(lua_getstring(name))); | ||
| 265 | } | 259 | } |
| 266 | 260 | ||
| 267 | #define MAXPARAMS 256 | 261 | #define MAXPARAMS 256 |
| 268 | void luaI_call (void) | 262 | static void luaI_call (void) |
| 269 | { | 263 | { |
| 270 | lua_Object f = lua_getparam(1); | 264 | lua_Object f = lua_getparam(1); |
| 271 | lua_Object arg = lua_getparam(2); | 265 | lua_Object arg = lua_getparam(2); |
| @@ -298,3 +292,86 @@ void luaI_call (void) | |||
| 298 | else | 292 | else |
| 299 | passresults(); | 293 | passresults(); |
| 300 | } | 294 | } |
| 295 | |||
| 296 | static void luaIl_settag (void) | ||
| 297 | { | ||
| 298 | lua_Object o = lua_getparam(1); | ||
| 299 | check_arg(o != LUA_NOOBJECT, "settag"); | ||
| 300 | lua_pushobject(o); | ||
| 301 | lua_settag(check_number(2, "settag")); | ||
| 302 | } | ||
| 303 | |||
| 304 | static void luaIl_newtag (void) | ||
| 305 | { | ||
| 306 | lua_pushnumber(lua_newtag(check_string(1, "newtag"))); | ||
| 307 | } | ||
| 308 | |||
| 309 | static void basicindex (void) | ||
| 310 | { | ||
| 311 | lua_Object t = lua_getparam(1); | ||
| 312 | lua_Object i = lua_getparam(2); | ||
| 313 | check_arg(t != LUA_NOOBJECT && i != LUA_NOOBJECT, "basicindex"); | ||
| 314 | lua_pushobject(t); | ||
| 315 | lua_pushobject(i); | ||
| 316 | lua_pushobject(lua_basicindex()); | ||
| 317 | } | ||
| 318 | |||
| 319 | static void basicstoreindex (void) | ||
| 320 | { | ||
| 321 | lua_Object t = lua_getparam(1); | ||
| 322 | lua_Object i = lua_getparam(2); | ||
| 323 | lua_Object v = lua_getparam(3); | ||
| 324 | check_arg(t != LUA_NOOBJECT && i != LUA_NOOBJECT && v != LUA_NOOBJECT, | ||
| 325 | "basicindex"); | ||
| 326 | lua_pushobject(t); | ||
| 327 | lua_pushobject(i); | ||
| 328 | lua_pushobject(v); | ||
| 329 | lua_basicstoreindex(); | ||
| 330 | } | ||
| 331 | |||
| 332 | |||
| 333 | |||
| 334 | /* | ||
| 335 | ** Internal functions | ||
| 336 | */ | ||
| 337 | static struct { | ||
| 338 | char *name; | ||
| 339 | lua_CFunction func; | ||
| 340 | } int_funcs[] = { | ||
| 341 | {"assert", luaI_assert}, | ||
| 342 | {"call", luaI_call}, | ||
| 343 | {"basicindex", basicindex}, | ||
| 344 | {"basicstoreindex", basicstoreindex}, | ||
| 345 | {"settag", luaIl_settag}, | ||
| 346 | {"dofile", lua_internaldofile}, | ||
| 347 | {"dostring", lua_internaldostring}, | ||
| 348 | {"error", luaI_error}, | ||
| 349 | {"getglobal", luaI_getglobal}, | ||
| 350 | {"next", lua_next}, | ||
| 351 | {"nextvar", luaI_nextvar}, | ||
| 352 | {"newtag", luaIl_newtag}, | ||
| 353 | {"print", luaI_print}, | ||
| 354 | {"setfallback", luaI_setfallback}, | ||
| 355 | {"setintmethod", luaI_setintmethod}, | ||
| 356 | {"setglobal", luaI_setglobal}, | ||
| 357 | {"tonumber", lua_obj2number}, | ||
| 358 | {"tostring", luaI_tostring}, | ||
| 359 | {"type", luaI_type} | ||
| 360 | }; | ||
| 361 | |||
| 362 | #define INTFUNCSIZE (sizeof(int_funcs)/sizeof(int_funcs[0])) | ||
| 363 | |||
| 364 | |||
| 365 | void luaI_predefine (void) | ||
| 366 | { | ||
| 367 | int i; | ||
| 368 | Word n; | ||
| 369 | for (i=0; i<INTFUNCSIZE; i++) { | ||
| 370 | n = luaI_findsymbolbyname(int_funcs[i].name); | ||
| 371 | s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = int_funcs[i].func; | ||
| 372 | } | ||
| 373 | n = luaI_findsymbolbyname("_VERSION_"); | ||
| 374 | s_tag(n) = LUA_T_STRING; s_tsvalue(n) = lua_createstring(LUA_VERSION); | ||
| 375 | } | ||
| 376 | |||
| 377 | |||
