diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1994-11-07 16:27:39 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1994-11-07 16:27:39 -0200 |
| commit | aa7b1fcec4f4a8147150fcb726146800c3c6af7e (patch) | |
| tree | 2c48bf687ad3400fe0a97f5bd568b10ef9105a0b | |
| parent | d95a8b312166752e2211678b33514edb1d68a0a6 (diff) | |
| download | lua-aa7b1fcec4f4a8147150fcb726146800c3c6af7e.tar.gz lua-aa7b1fcec4f4a8147150fcb726146800c3c6af7e.tar.bz2 lua-aa7b1fcec4f4a8147150fcb726146800c3c6af7e.zip | |
new API
| -rw-r--r-- | lua.h | 10 | ||||
| -rw-r--r-- | opcode.c | 85 |
2 files changed, 74 insertions, 21 deletions
| @@ -2,7 +2,7 @@ | |||
| 2 | ** LUA - Linguagem para Usuarios de Aplicacao | 2 | ** LUA - Linguagem para Usuarios de Aplicacao |
| 3 | ** Grupo de Tecnologia em Computacao Grafica | 3 | ** Grupo de Tecnologia em Computacao Grafica |
| 4 | ** TeCGraf - PUC-Rio | 4 | ** TeCGraf - PUC-Rio |
| 5 | ** $Id: lua.h,v 3.2 1994/11/04 10:47:49 roberto Exp roberto $ | 5 | ** $Id: lua.h,v 3.3 1994/11/07 16:34:44 roberto Exp $ |
| 6 | */ | 6 | */ |
| 7 | 7 | ||
| 8 | 8 | ||
| @@ -29,6 +29,8 @@ typedef enum | |||
| 29 | typedef void (*lua_CFunction) (void); | 29 | typedef void (*lua_CFunction) (void); |
| 30 | typedef unsigned int lua_Object; | 30 | typedef unsigned int lua_Object; |
| 31 | 31 | ||
| 32 | lua_Object lua_setfallback (char *name, lua_CFunction fallback); | ||
| 33 | |||
| 32 | void lua_error (char *s); | 34 | void lua_error (char *s); |
| 33 | int lua_dofile (char *filename); | 35 | int lua_dofile (char *filename); |
| 34 | int lua_dostring (char *string); | 36 | int lua_dostring (char *string); |
| @@ -54,7 +56,7 @@ lua_Object lua_getglobal (char *name); | |||
| 54 | int lua_storeglobal (char *name); | 56 | int lua_storeglobal (char *name); |
| 55 | 57 | ||
| 56 | int lua_storesubscript (void); | 58 | int lua_storesubscript (void); |
| 57 | lua_Object lua_getIndex (void); | 59 | lua_Object lua_getsubscript (void); |
| 58 | 60 | ||
| 59 | int lua_type (lua_Object object); | 61 | int lua_type (lua_Object object); |
| 60 | 62 | ||
| @@ -65,8 +67,8 @@ int lua_type (lua_Object object); | |||
| 65 | 67 | ||
| 66 | #define lua_call(f) lua_callfunction(lua_getglobal(f)) | 68 | #define lua_call(f) lua_callfunction(lua_getglobal(f)) |
| 67 | 69 | ||
| 68 | #define lua_getindexed(o,n) (lua_pushobject(o), lua_pushnumber(n), lua_getIndex()) | 70 | #define lua_getindexed(o,n) (lua_pushobject(o), lua_pushnumber(n), lua_getsubscript()) |
| 69 | #define lua_getfield(o,f) (lua_pushobject(o), lua_pushstring(f), lua_getIndex()) | 71 | #define lua_getfield(o,f) (lua_pushobject(o), lua_pushstring(f), lua_getsubscript()) |
| 70 | 72 | ||
| 71 | #define lua_isnil(_) (lua_type(_)==LUA_T_NIL) | 73 | #define lua_isnil(_) (lua_type(_)==LUA_T_NIL) |
| 72 | #define lua_isnumber(_) (lua_type(_)==LUA_T_NUMBER) | 74 | #define lua_isnumber(_) (lua_type(_)==LUA_T_NUMBER) |
| @@ -3,7 +3,7 @@ | |||
| 3 | ** TecCGraf - PUC-Rio | 3 | ** TecCGraf - PUC-Rio |
| 4 | */ | 4 | */ |
| 5 | 5 | ||
| 6 | char *rcs_opcode="$Id: opcode.c,v 3.3 1994/11/07 15:20:56 roberto Exp $"; | 6 | char *rcs_opcode="$Id: opcode.c,v 3.4 1994/11/07 16:34:44 roberto Exp roberto $"; |
| 7 | 7 | ||
| 8 | #include <stdio.h> | 8 | #include <stdio.h> |
| 9 | #include <stdlib.h> | 9 | #include <stdlib.h> |
| @@ -99,7 +99,7 @@ void luaI_setfallback (void) | |||
| 99 | { | 99 | { |
| 100 | if (strcmp(fallBacks[i].kind, name) == 0) | 100 | if (strcmp(fallBacks[i].kind, name) == 0) |
| 101 | { | 101 | { |
| 102 | lua_pushobject(Ref(&fallBacks[i].function)); | 102 | luaI_pushobject(&fallBacks[i].function); |
| 103 | fallBacks[i].function = *Address(func); | 103 | fallBacks[i].function = *Address(func); |
| 104 | return; | 104 | return; |
| 105 | } | 105 | } |
| @@ -245,6 +245,12 @@ static void adjust_top (Object *newtop) | |||
| 245 | } | 245 | } |
| 246 | 246 | ||
| 247 | 247 | ||
| 248 | static void adjustC (int nParams) | ||
| 249 | { | ||
| 250 | adjust_top(stack+CBase+nParams); | ||
| 251 | } | ||
| 252 | |||
| 253 | |||
| 248 | /* | 254 | /* |
| 249 | ** Call a C function. CBase will point to the top of the stack, | 255 | ** Call a C function. CBase will point to the top of the stack, |
| 250 | ** and CnResults is the number of parameters. Returns an index | 256 | ** and CnResults is the number of parameters. Returns an index |
| @@ -297,7 +303,7 @@ static void do_call (Object *func, int base, int nResults, int whereRes) | |||
| 297 | 303 | ||
| 298 | /* | 304 | /* |
| 299 | ** Function to index a table. Receives the table at top-2 and the index | 305 | ** Function to index a table. Receives the table at top-2 and the index |
| 300 | ** at top-1. Remove them from stack and push the result. | 306 | ** at top-1. |
| 301 | */ | 307 | */ |
| 302 | static void pushsubscript (void) | 308 | static void pushsubscript (void) |
| 303 | { | 309 | { |
| @@ -433,6 +439,51 @@ int lua_dostring (char *string) | |||
| 433 | 439 | ||
| 434 | 440 | ||
| 435 | /* | 441 | /* |
| 442 | ** API: set a function as a fallback | ||
| 443 | */ | ||
| 444 | lua_Object lua_setfallback (char *name, lua_CFunction fallback) | ||
| 445 | { | ||
| 446 | static Object func = {LUA_T_CFUNCTION, luaI_setfallback}; | ||
| 447 | adjustC(0); | ||
| 448 | lua_pushstring(name); | ||
| 449 | lua_pushcfunction(fallback); | ||
| 450 | do_protectedrun(&func, 1); | ||
| 451 | return (Ref(top-1)); | ||
| 452 | } | ||
| 453 | |||
| 454 | |||
| 455 | /* | ||
| 456 | ** API: receives on the stack the table and the index. | ||
| 457 | ** returns the value. | ||
| 458 | */ | ||
| 459 | lua_Object lua_getsubscript (void) | ||
| 460 | { | ||
| 461 | static Byte code[2] = {PUSHINDEXED, RETCODE0}; | ||
| 462 | int status; | ||
| 463 | Object func; | ||
| 464 | tag(&func) = LUA_T_FUNCTION; bvalue(&func) = code; | ||
| 465 | adjustC(2); | ||
| 466 | status = do_protectedrun(&func, 1); | ||
| 467 | if (status == 0) | ||
| 468 | return (Ref(top-1)); | ||
| 469 | else | ||
| 470 | return 0; | ||
| 471 | } | ||
| 472 | |||
| 473 | /* | ||
| 474 | ** API: receives on the stack the table, the index, and the new value. | ||
| 475 | */ | ||
| 476 | int lua_storesubscript (void) | ||
| 477 | { | ||
| 478 | static Byte code[2] = {STOREINDEXED, RETCODE0}; | ||
| 479 | Object func; | ||
| 480 | tag(&func) = LUA_T_FUNCTION; bvalue(&func) = code; | ||
| 481 | adjustC(3); | ||
| 482 | return(do_protectedrun(&func, 0)); | ||
| 483 | } | ||
| 484 | |||
| 485 | |||
| 486 | /* | ||
| 436 | ** Get a parameter, returning the object handle or 0 on error. | 487 | ** Get a parameter, returning the object handle or 0 on error. |
| 437 | ** 'number' must be 1 to get the first parameter. | 488 | ** 'number' must be 1 to get the first parameter. |
| 438 | */ | 489 | */ |
| @@ -501,12 +552,24 @@ lua_Object lua_getglobal (char *name) | |||
| 501 | { | 552 | { |
| 502 | int n = lua_findsymbol(name); | 553 | int n = lua_findsymbol(name); |
| 503 | if (n < 0) return 0; | 554 | if (n < 0) return 0; |
| 504 | *(top-1) = s_object(n); | 555 | *(top++) = s_object(n); |
| 505 | top++; | ||
| 506 | return Ref(top-1); | 556 | return Ref(top-1); |
| 507 | } | 557 | } |
| 508 | 558 | ||
| 509 | /* | 559 | /* |
| 560 | ** Store top of the stack at a global variable array field. | ||
| 561 | ** Return 1 on error, 0 on success. | ||
| 562 | */ | ||
| 563 | int lua_storeglobal (char *name) | ||
| 564 | { | ||
| 565 | int n = lua_findsymbol (name); | ||
| 566 | if (n < 0) return 1; | ||
| 567 | adjustC(1); | ||
| 568 | s_object(n) = *(--top); | ||
| 569 | return 0; | ||
| 570 | } | ||
| 571 | |||
| 572 | /* | ||
| 510 | ** Push a nil object | 573 | ** Push a nil object |
| 511 | */ | 574 | */ |
| 512 | int lua_pushnil (void) | 575 | int lua_pushnil (void) |
| @@ -576,18 +639,6 @@ void luaI_pushobject (Object *o) | |||
| 576 | *top++ = *o; | 639 | *top++ = *o; |
| 577 | } | 640 | } |
| 578 | 641 | ||
| 579 | /* | ||
| 580 | ** Store top of the stack at a global variable array field. | ||
| 581 | ** Return 1 on error, 0 on success. | ||
| 582 | */ | ||
| 583 | int lua_storeglobal (char *name) | ||
| 584 | { | ||
| 585 | int n = lua_findsymbol (name); | ||
| 586 | if (n < 0) return 1; | ||
| 587 | s_object(n) = *(--top); | ||
| 588 | return 0; | ||
| 589 | } | ||
| 590 | |||
| 591 | int lua_type (lua_Object o) | 642 | int lua_type (lua_Object o) |
| 592 | { | 643 | { |
| 593 | if (o == 0) | 644 | if (o == 0) |
