diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1994-11-07 14:34:44 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1994-11-07 14:34:44 -0200 |
commit | d95a8b312166752e2211678b33514edb1d68a0a6 (patch) | |
tree | 87a7180705c1c5e6db71831fb8cb49ce8980f4d0 /opcode.c | |
parent | 9ffba7a3dbdfa68595cd8cec26bd99689ce5fd08 (diff) | |
download | lua-d95a8b312166752e2211678b33514edb1d68a0a6.tar.gz lua-d95a8b312166752e2211678b33514edb1d68a0a6.tar.bz2 lua-d95a8b312166752e2211678b33514edb1d68a0a6.zip |
new API: lua_Object now is an integer
Diffstat (limited to 'opcode.c')
-rw-r--r-- | opcode.c | 145 |
1 files changed, 64 insertions, 81 deletions
@@ -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.2 1994/11/04 10:47:49 roberto Exp roberto $"; | 6 | char *rcs_opcode="$Id: opcode.c,v 3.3 1994/11/07 15:20:56 roberto Exp $"; |
7 | 7 | ||
8 | #include <stdio.h> | 8 | #include <stdio.h> |
9 | #include <stdlib.h> | 9 | #include <stdlib.h> |
@@ -32,6 +32,12 @@ static Object *stack = NULL; | |||
32 | static Object *top = NULL; | 32 | static Object *top = NULL; |
33 | 33 | ||
34 | 34 | ||
35 | /* macros to convert from lua_Object to (Object *) and back */ | ||
36 | |||
37 | #define Address(lo) ((lo)+stack-1) | ||
38 | #define Ref(st) ((st)-stack+1) | ||
39 | |||
40 | |||
35 | static int CBase = 0; /* when Lua calls C or C calls Lua, points to the */ | 41 | static int CBase = 0; /* when Lua calls C or C calls Lua, points to the */ |
36 | /* first slot after the last parameter. */ | 42 | /* first slot after the last parameter. */ |
37 | static int CnResults = 0; /* when Lua calls C, has the number of parameters; */ | 43 | static int CnResults = 0; /* when Lua calls C, has the number of parameters; */ |
@@ -44,6 +50,12 @@ static int lua_execute (Byte *pc, int base); | |||
44 | static void do_call (Object *func, int base, int nResults, int whereRes); | 50 | static void do_call (Object *func, int base, int nResults, int whereRes); |
45 | 51 | ||
46 | 52 | ||
53 | Object *luaI_Address (lua_Object o) | ||
54 | { | ||
55 | return Address(o); | ||
56 | } | ||
57 | |||
58 | |||
47 | /* | 59 | /* |
48 | ** Fallbacks | 60 | ** Fallbacks |
49 | */ | 61 | */ |
@@ -87,8 +99,8 @@ void luaI_setfallback (void) | |||
87 | { | 99 | { |
88 | if (strcmp(fallBacks[i].kind, name) == 0) | 100 | if (strcmp(fallBacks[i].kind, name) == 0) |
89 | { | 101 | { |
90 | lua_pushobject(&fallBacks[i].function); | 102 | lua_pushobject(Ref(&fallBacks[i].function)); |
91 | fallBacks[i].function = *func; | 103 | fallBacks[i].function = *Address(func); |
92 | return; | 104 | return; |
93 | } | 105 | } |
94 | } | 106 | } |
@@ -96,6 +108,7 @@ void luaI_setfallback (void) | |||
96 | lua_pushnil(); | 108 | lua_pushnil(); |
97 | } | 109 | } |
98 | 110 | ||
111 | |||
99 | /* | 112 | /* |
100 | ** Error messages | 113 | ** Error messages |
101 | */ | 114 | */ |
@@ -373,12 +386,12 @@ static int do_protectedrun (Object *function, int nResults) | |||
373 | /* | 386 | /* |
374 | ** Execute the given lua function. Return 0 on success or 1 on error. | 387 | ** Execute the given lua function. Return 0 on success or 1 on error. |
375 | */ | 388 | */ |
376 | int lua_callfunction (Object *function) | 389 | int lua_callfunction (lua_Object function) |
377 | { | 390 | { |
378 | if (function == NULL) | 391 | if (function == NULL) |
379 | return 1; | 392 | return 1; |
380 | else | 393 | else |
381 | return do_protectedrun (function, MULT_RET); | 394 | return do_protectedrun (Address(function), MULT_RET); |
382 | } | 395 | } |
383 | 396 | ||
384 | 397 | ||
@@ -420,73 +433,77 @@ int lua_dostring (char *string) | |||
420 | 433 | ||
421 | 434 | ||
422 | /* | 435 | /* |
423 | ** Get a parameter, returning the object handle or NULL on error. | 436 | ** Get a parameter, returning the object handle or 0 on error. |
424 | ** 'number' must be 1 to get the first parameter. | 437 | ** 'number' must be 1 to get the first parameter. |
425 | */ | 438 | */ |
426 | Object *lua_getparam (int number) | 439 | lua_Object lua_getparam (int number) |
427 | { | 440 | { |
428 | if (number <= 0 || number > CnResults) return NULL; | 441 | if (number <= 0 || number > CnResults) return 0; |
429 | return (stack+(CBase-CnResults+number-1)); | 442 | /* Ref(stack+(CBase-CnResults+number-1)) == |
443 | stack+(CBase-CnResults+number-1)-stack+1 == */ | ||
444 | return CBase-CnResults+number; | ||
430 | } | 445 | } |
431 | 446 | ||
432 | /* | 447 | /* |
433 | ** Given an object handle, return its number value. On error, return 0.0. | 448 | ** Given an object handle, return its number value. On error, return 0.0. |
434 | */ | 449 | */ |
435 | real lua_getnumber (Object *object) | 450 | real lua_getnumber (lua_Object object) |
436 | { | 451 | { |
437 | if (object == NULL || tag(object) == LUA_T_NIL) return 0.0; | 452 | if (object == 0 || tag(Address(object)) == LUA_T_NIL) return 0.0; |
438 | if (tonumber (object)) return 0.0; | 453 | if (tonumber (Address(object))) return 0.0; |
439 | else return (nvalue(object)); | 454 | else return (nvalue(Address(object))); |
440 | } | 455 | } |
441 | 456 | ||
442 | /* | 457 | /* |
443 | ** Given an object handle, return its string pointer. On error, return NULL. | 458 | ** Given an object handle, return its string pointer. On error, return NULL. |
444 | */ | 459 | */ |
445 | char *lua_getstring (Object *object) | 460 | char *lua_getstring (lua_Object object) |
446 | { | 461 | { |
447 | if (object == NULL || tag(object) == LUA_T_NIL) return NULL; | 462 | if (object == 0 || tag(Address(object)) == LUA_T_NIL) return NULL; |
448 | if (tostring (object)) return NULL; | 463 | if (tostring (Address(object))) return NULL; |
449 | else return (svalue(object)); | 464 | else return (svalue(Address(object))); |
450 | } | 465 | } |
451 | 466 | ||
452 | /* | 467 | /* |
453 | ** Given an object handle, return a copy of its string. On error, return NULL. | 468 | ** Given an object handle, return a copy of its string. On error, return NULL. |
454 | */ | 469 | */ |
455 | char *lua_copystring (Object *object) | 470 | char *lua_copystring (lua_Object object) |
456 | { | 471 | { |
457 | if (object == NULL || tag(object) == LUA_T_NIL) return NULL; | 472 | if (object == 0 || tag(Address(object)) == LUA_T_NIL) return NULL; |
458 | if (tostring (object)) return NULL; | 473 | if (tostring (Address(object))) return NULL; |
459 | else return (strdup(svalue(object))); | 474 | else return (strdup(svalue(Address(object)))); |
460 | } | 475 | } |
461 | 476 | ||
462 | /* | 477 | /* |
463 | ** Given an object handle, return its cfuntion pointer. On error, return NULL. | 478 | ** Given an object handle, return its cfuntion pointer. On error, return NULL. |
464 | */ | 479 | */ |
465 | lua_CFunction lua_getcfunction (Object *object) | 480 | lua_CFunction lua_getcfunction (lua_Object object) |
466 | { | 481 | { |
467 | if (object == NULL) return NULL; | 482 | if (object == 0) return NULL; |
468 | if (tag(object) != LUA_T_CFUNCTION) return NULL; | 483 | if (tag(Address(object)) != LUA_T_CFUNCTION) return NULL; |
469 | else return (fvalue(object)); | 484 | else return (fvalue(Address(object))); |
470 | } | 485 | } |
471 | 486 | ||
472 | /* | 487 | /* |
473 | ** Given an object handle, return its user data. On error, return NULL. | 488 | ** Given an object handle, return its user data. On error, return NULL. |
474 | */ | 489 | */ |
475 | void *lua_getuserdata (Object *object) | 490 | void *lua_getuserdata (lua_Object object) |
476 | { | 491 | { |
477 | if (object == NULL) return NULL; | 492 | if (object == 0) return NULL; |
478 | if (tag(object) != LUA_T_USERDATA) return NULL; | 493 | if (tag(Address(object)) != LUA_T_USERDATA) return NULL; |
479 | else return (uvalue(object)); | 494 | else return (uvalue(Address(object))); |
480 | } | 495 | } |
481 | 496 | ||
482 | /* | 497 | /* |
483 | ** Get a global object. Return the object handle or NULL on error. | 498 | ** Get a global object. Return the object handle or NULL on error. |
484 | */ | 499 | */ |
485 | Object *lua_getglobal (char *name) | 500 | lua_Object lua_getglobal (char *name) |
486 | { | 501 | { |
487 | int n = lua_findsymbol(name); | 502 | int n = lua_findsymbol(name); |
488 | if (n < 0) return NULL; | 503 | if (n < 0) return 0; |
489 | return &s_object(n); | 504 | *(top-1) = s_object(n); |
505 | top++; | ||
506 | return Ref(top-1); | ||
490 | } | 507 | } |
491 | 508 | ||
492 | /* | 509 | /* |
@@ -541,16 +558,25 @@ int lua_pushuserdata (void *u) | |||
541 | } | 558 | } |
542 | 559 | ||
543 | /* | 560 | /* |
544 | ** Push an object to stack. | 561 | ** Push a lua_Object to stack. |
545 | */ | 562 | */ |
546 | int lua_pushobject (Object *o) | 563 | int lua_pushobject (lua_Object o) |
547 | { | 564 | { |
548 | lua_checkstack(top-stack+1); | 565 | lua_checkstack(top-stack+1); |
549 | *top++ = *o; | 566 | *top++ = *Address(o); |
550 | return 0; | 567 | return 0; |
551 | } | 568 | } |
552 | 569 | ||
553 | /* | 570 | /* |
571 | ** Push an object on the stack. | ||
572 | */ | ||
573 | void luaI_pushobject (Object *o) | ||
574 | { | ||
575 | lua_checkstack(top-stack+1); | ||
576 | *top++ = *o; | ||
577 | } | ||
578 | |||
579 | /* | ||
554 | ** Store top of the stack at a global variable array field. | 580 | ** Store top of the stack at a global variable array field. |
555 | ** Return 1 on error, 0 on success. | 581 | ** Return 1 on error, 0 on success. |
556 | */ | 582 | */ |
@@ -558,60 +584,16 @@ int lua_storeglobal (char *name) | |||
558 | { | 584 | { |
559 | int n = lua_findsymbol (name); | 585 | int n = lua_findsymbol (name); |
560 | if (n < 0) return 1; | 586 | if (n < 0) return 1; |
561 | if (top-stack <= CBase) return 1; | ||
562 | s_object(n) = *(--top); | 587 | s_object(n) = *(--top); |
563 | return 0; | 588 | return 0; |
564 | } | 589 | } |
565 | 590 | ||
566 | |||
567 | /* | ||
568 | ** Store top of the stack at an array field. Return 1 on error, 0 on success. | ||
569 | */ | ||
570 | int lua_storefield (lua_Object object, char *field) | ||
571 | { | ||
572 | if (tag(object) != LUA_T_ARRAY) | ||
573 | return 1; | ||
574 | else | ||
575 | { | ||
576 | Object ref, *h; | ||
577 | tag(&ref) = LUA_T_STRING; | ||
578 | svalue(&ref) = lua_createstring(field); | ||
579 | h = lua_hashdefine(avalue(object), &ref); | ||
580 | if (h == NULL) return 1; | ||
581 | if (tag(top-1) == LUA_T_MARK) return 1; | ||
582 | *h = *(--top); | ||
583 | } | ||
584 | return 0; | ||
585 | } | ||
586 | |||
587 | |||
588 | /* | ||
589 | ** Store top of the stack at an array index. Return 1 on error, 0 on success. | ||
590 | */ | ||
591 | int lua_storeindexed (lua_Object object, float index) | ||
592 | { | ||
593 | if (tag(object) != LUA_T_ARRAY) | ||
594 | return 1; | ||
595 | else | ||
596 | { | ||
597 | Object ref, *h; | ||
598 | tag(&ref) = LUA_T_NUMBER; | ||
599 | nvalue(&ref) = index; | ||
600 | h = lua_hashdefine(avalue(object), &ref); | ||
601 | if (h == NULL) return 1; | ||
602 | if (tag(top-1) == LUA_T_MARK) return 1; | ||
603 | *h = *(--top); | ||
604 | } | ||
605 | return 0; | ||
606 | } | ||
607 | |||
608 | |||
609 | int lua_type (lua_Object o) | 591 | int lua_type (lua_Object o) |
610 | { | 592 | { |
611 | if (o == NULL) | 593 | if (o == 0) |
612 | return LUA_T_NIL; | 594 | return LUA_T_NIL; |
613 | else | 595 | else |
614 | return tag(o); | 596 | return tag(Address(o)); |
615 | } | 597 | } |
616 | 598 | ||
617 | 599 | ||
@@ -1063,3 +1045,4 @@ static int lua_execute (Byte *pc, int base) | |||
1063 | } | 1045 | } |
1064 | } | 1046 | } |
1065 | 1047 | ||
1048 | |||