diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-06-06 17:54:40 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-06-06 17:54:40 -0300 |
| commit | 5fdcfeb353d726a9bcd6d4db5dd0b62b9b8e4b02 (patch) | |
| tree | a339cd8041ea76d8071e6afcfe9ad360803a566c /opcode.c | |
| parent | d68d8287aa48270e9cb7a7c2c80563cb0039feee (diff) | |
| download | lua-5fdcfeb353d726a9bcd6d4db5dd0b62b9b8e4b02.tar.gz lua-5fdcfeb353d726a9bcd6d4db5dd0b62b9b8e4b02.tar.bz2 lua-5fdcfeb353d726a9bcd6d4db5dd0b62b9b8e4b02.zip | |
new lua_Objects are created below the C2lua stack, so most API functions
don't need to adjust stack.
Diffstat (limited to 'opcode.c')
| -rw-r--r-- | opcode.c | 105 |
1 files changed, 53 insertions, 52 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 4.4 1997/04/24 22:59:57 roberto Exp roberto $"; | 6 | char *rcs_opcode="$Id: opcode.c,v 4.5 1997/05/26 14:23:55 roberto Exp roberto $"; |
| 7 | 7 | ||
| 8 | #include <setjmp.h> | 8 | #include <setjmp.h> |
| 9 | #include <stdio.h> | 9 | #include <stdio.h> |
| @@ -193,6 +193,13 @@ static void adjust_top (StkId newtop) | |||
| 193 | #define adjustC(nParams) adjust_top(CLS_current.base+nParams) | 193 | #define adjustC(nParams) adjust_top(CLS_current.base+nParams) |
| 194 | 194 | ||
| 195 | 195 | ||
| 196 | static void checkCparams (int nParams) | ||
| 197 | { | ||
| 198 | if (top-stack < CLS_current.base+nParams) | ||
| 199 | lua_error("API error - wrong number of arguments in C2lua stack"); | ||
| 200 | } | ||
| 201 | |||
| 202 | |||
| 196 | /* | 203 | /* |
| 197 | ** Open a hole below "nelems" from the top. | 204 | ** Open a hole below "nelems" from the top. |
| 198 | */ | 205 | */ |
| @@ -205,6 +212,23 @@ static void open_stack (int nelems) | |||
| 205 | } | 212 | } |
| 206 | 213 | ||
| 207 | 214 | ||
| 215 | static lua_Object put_luaObject (TObject *o) | ||
| 216 | { | ||
| 217 | open_stack((top-stack)-CLS_current.base); | ||
| 218 | stack[CLS_current.base++] = *o; | ||
| 219 | return CLS_current.base; /* this is +1 real position (see Ref) */ | ||
| 220 | } | ||
| 221 | |||
| 222 | |||
| 223 | static lua_Object put_luaObjectonTop (void) | ||
| 224 | { | ||
| 225 | open_stack((top-stack)-CLS_current.base); | ||
| 226 | stack[CLS_current.base++] = *(--top); | ||
| 227 | return CLS_current.base; /* this is +1 real position (see Ref) */ | ||
| 228 | } | ||
| 229 | |||
| 230 | |||
| 231 | |||
| 208 | /* | 232 | /* |
| 209 | ** call Line hook | 233 | ** call Line hook |
| 210 | */ | 234 | */ |
| @@ -352,7 +376,7 @@ static void pushsubscript (void) | |||
| 352 | 376 | ||
| 353 | lua_Object lua_rawgettable (void) | 377 | lua_Object lua_rawgettable (void) |
| 354 | { | 378 | { |
| 355 | adjustC(2); | 379 | checkCparams(2); |
| 356 | if (ttype(top-2) != LUA_T_ARRAY) | 380 | if (ttype(top-2) != LUA_T_ARRAY) |
| 357 | lua_error("indexed expression not a table in raw gettable"); | 381 | lua_error("indexed expression not a table in raw gettable"); |
| 358 | else { | 382 | else { |
| @@ -363,8 +387,7 @@ lua_Object lua_rawgettable (void) | |||
| 363 | else | 387 | else |
| 364 | ttype(top-1) = LUA_T_NIL; | 388 | ttype(top-1) = LUA_T_NIL; |
| 365 | } | 389 | } |
| 366 | CLS_current.base++; /* incorporate object in the stack */ | 390 | return put_luaObjectonTop(); |
| 367 | return (Ref(top-1)); | ||
| 368 | } | 391 | } |
| 369 | 392 | ||
| 370 | 393 | ||
| @@ -697,10 +720,9 @@ void lua_seterrormethod (lua_CFunction method) | |||
| 697 | */ | 720 | */ |
| 698 | lua_Object lua_gettable (void) | 721 | lua_Object lua_gettable (void) |
| 699 | { | 722 | { |
| 700 | adjustC(2); | 723 | checkCparams(2); |
| 701 | pushsubscript(); | 724 | pushsubscript(); |
| 702 | CLS_current.base++; /* incorporate object in the stack */ | 725 | return put_luaObjectonTop(); |
| 703 | return (Ref(top-1)); | ||
| 704 | } | 726 | } |
| 705 | 727 | ||
| 706 | 728 | ||
| @@ -732,7 +754,7 @@ void lua_endblock (void) | |||
| 732 | 754 | ||
| 733 | void lua_settag (int tag) | 755 | void lua_settag (int tag) |
| 734 | { | 756 | { |
| 735 | adjustC(1); | 757 | checkCparams(1); |
| 736 | luaI_settag(tag, --top); | 758 | luaI_settag(tag, --top); |
| 737 | } | 759 | } |
| 738 | 760 | ||
| @@ -741,13 +763,13 @@ void lua_settag (int tag) | |||
| 741 | */ | 763 | */ |
| 742 | void lua_settable (void) | 764 | void lua_settable (void) |
| 743 | { | 765 | { |
| 744 | adjustC(3); | 766 | checkCparams(3); |
| 745 | storesubscript(top-3, 1); | 767 | storesubscript(top-3, 1); |
| 746 | } | 768 | } |
| 747 | 769 | ||
| 748 | void lua_rawsettable (void) | 770 | void lua_rawsettable (void) |
| 749 | { | 771 | { |
| 750 | adjustC(3); | 772 | checkCparams(3); |
| 751 | storesubscript(top-3, 0); | 773 | storesubscript(top-3, 0); |
| 752 | } | 774 | } |
| 753 | 775 | ||
| @@ -756,12 +778,10 @@ void lua_rawsettable (void) | |||
| 756 | */ | 778 | */ |
| 757 | lua_Object lua_createtable (void) | 779 | lua_Object lua_createtable (void) |
| 758 | { | 780 | { |
| 759 | adjustC(0); | 781 | TObject o; |
| 760 | avalue(top) = lua_createarray(0); | 782 | avalue(&o) = lua_createarray(0); |
| 761 | ttype(top) = LUA_T_ARRAY; | 783 | ttype(&o) = LUA_T_ARRAY; |
| 762 | incr_top; | 784 | return put_luaObject(&o); |
| 763 | CLS_current.base++; /* incorporate object in the stack */ | ||
| 764 | return Ref(top-1); | ||
| 765 | } | 785 | } |
| 766 | 786 | ||
| 767 | /* | 787 | /* |
| @@ -770,10 +790,10 @@ lua_Object lua_createtable (void) | |||
| 770 | */ | 790 | */ |
| 771 | lua_Object lua_lua2C (int number) | 791 | lua_Object lua_lua2C (int number) |
| 772 | { | 792 | { |
| 773 | if (number <= 0 || number > CLS_current.num) return LUA_NOOBJECT; | 793 | if (number <= 0 || number > CLS_current.num) return LUA_NOOBJECT; |
| 774 | /* Ref(stack+(CLS_current.base-CLS_current.num+number-1)) == | 794 | /* Ref(stack+(CLS_current.base-CLS_current.num+number-1)) == |
| 775 | stack+(CLS_current.base-CLS_current.num+number-1)-stack+1 == */ | 795 | stack+(CLS_current.base-CLS_current.num+number-1)-stack+1 == */ |
| 776 | return CLS_current.base-CLS_current.num+number; | 796 | return CLS_current.base-CLS_current.num+number; |
| 777 | } | 797 | } |
| 778 | 798 | ||
| 779 | int lua_isnil (lua_Object o) | 799 | int lua_isnil (lua_Object o) |
| @@ -874,25 +894,13 @@ lua_Object lua_getref (int ref) | |||
| 874 | TObject *o = luaI_getref(ref); | 894 | TObject *o = luaI_getref(ref); |
| 875 | if (o == NULL) | 895 | if (o == NULL) |
| 876 | return LUA_NOOBJECT; | 896 | return LUA_NOOBJECT; |
| 877 | adjustC(0); | 897 | return put_luaObject(o); |
| 878 | luaI_pushobject(o); | ||
| 879 | CLS_current.base++; /* incorporate object in the stack */ | ||
| 880 | return Ref(top-1); | ||
| 881 | } | ||
| 882 | |||
| 883 | |||
| 884 | void lua_pushref (int ref) | ||
| 885 | { | ||
| 886 | TObject *o = luaI_getref(ref); | ||
| 887 | if (o == NULL) | ||
| 888 | lua_error("access to invalid reference (possibly garbage collected)"); | ||
| 889 | luaI_pushobject(o); | ||
| 890 | } | 898 | } |
| 891 | 899 | ||
| 892 | 900 | ||
| 893 | int lua_ref (int lock) | 901 | int lua_ref (int lock) |
| 894 | { | 902 | { |
| 895 | adjustC(1); | 903 | checkCparams(1); |
| 896 | return luaI_ref(--top, lock); | 904 | return luaI_ref(--top, lock); |
| 897 | } | 905 | } |
| 898 | 906 | ||
| @@ -903,20 +911,14 @@ int lua_ref (int lock) | |||
| 903 | */ | 911 | */ |
| 904 | lua_Object lua_getglobal (char *name) | 912 | lua_Object lua_getglobal (char *name) |
| 905 | { | 913 | { |
| 906 | adjustC(0); | 914 | getglobal(luaI_findsymbolbyname(name)); |
| 907 | getglobal(luaI_findsymbolbyname(name)); | 915 | return put_luaObjectonTop(); |
| 908 | CLS_current.base++; /* incorporate object in the stack */ | ||
| 909 | return Ref(top-1); | ||
| 910 | } | 916 | } |
| 911 | 917 | ||
| 912 | 918 | ||
| 913 | lua_Object lua_rawgetglobal (char *name) | 919 | lua_Object lua_rawgetglobal (char *name) |
| 914 | { | 920 | { |
| 915 | adjustC(0); | 921 | return put_luaObject(&lua_table[luaI_findsymbolbyname(name)].object); |
| 916 | *top = lua_table[luaI_findsymbolbyname(name)].object; | ||
| 917 | incr_top; | ||
| 918 | CLS_current.base++; /* incorporate object in the stack */ | ||
| 919 | return Ref(top-1); | ||
| 920 | } | 922 | } |
| 921 | 923 | ||
| 922 | 924 | ||
| @@ -944,15 +946,15 @@ static void setglobal (Word n) | |||
| 944 | 946 | ||
| 945 | void lua_setglobal (char *name) | 947 | void lua_setglobal (char *name) |
| 946 | { | 948 | { |
| 947 | adjustC(1); | 949 | checkCparams(1); |
| 948 | setglobal(luaI_findsymbolbyname(name)); | 950 | setglobal(luaI_findsymbolbyname(name)); |
| 949 | } | 951 | } |
| 950 | 952 | ||
| 951 | void lua_rawsetglobal (char *name) | 953 | void lua_rawsetglobal (char *name) |
| 952 | { | 954 | { |
| 953 | Word n = luaI_findsymbolbyname(name); | 955 | Word n = luaI_findsymbolbyname(name); |
| 954 | adjustC(1); | 956 | checkCparams(1); |
| 955 | s_object(n) = *(--top); | 957 | s_object(n) = *(--top); |
| 956 | } | 958 | } |
| 957 | 959 | ||
| 958 | /* | 960 | /* |
| @@ -960,8 +962,8 @@ void lua_rawsetglobal (char *name) | |||
| 960 | */ | 962 | */ |
| 961 | void lua_pushnil (void) | 963 | void lua_pushnil (void) |
| 962 | { | 964 | { |
| 963 | ttype(top) = LUA_T_NIL; | 965 | ttype(top) = LUA_T_NIL; |
| 964 | incr_top; | 966 | incr_top; |
| 965 | } | 967 | } |
| 966 | 968 | ||
| 967 | /* | 969 | /* |
| @@ -987,8 +989,7 @@ void lua_pushstring (char *s) | |||
| 987 | } | 989 | } |
| 988 | incr_top; | 990 | incr_top; |
| 989 | } | 991 | } |
| 990 | /*>>>>>>>>>#undef lua_pushliteral | 992 | |
| 991 | void lua_pushliteral(char *s) { lua_pushstring(s); }*/ | ||
| 992 | 993 | ||
| 993 | /* | 994 | /* |
| 994 | ** Push an object (ttype=cfunction) to stack. | 995 | ** Push an object (ttype=cfunction) to stack. |
| @@ -1035,7 +1036,7 @@ void luaI_pushobject (TObject *o) | |||
| 1035 | void lua_pushobject (lua_Object o) | 1036 | void lua_pushobject (lua_Object o) |
| 1036 | { | 1037 | { |
| 1037 | if (o == LUA_NOOBJECT) | 1038 | if (o == LUA_NOOBJECT) |
| 1038 | lua_error("attempt to push a NOOBJECT"); | 1039 | lua_error("API error - attempt to push a NOOBJECT"); |
| 1039 | *top = *Address(o); | 1040 | *top = *Address(o); |
| 1040 | if (ttype(top) == LUA_T_MARK) ttype(top) = LUA_T_FUNCTION; | 1041 | if (ttype(top) == LUA_T_MARK) ttype(top) = LUA_T_FUNCTION; |
| 1041 | else if (ttype(top) == LUA_T_CMARK) ttype(top) = LUA_T_CFUNCTION; | 1042 | else if (ttype(top) == LUA_T_CMARK) ttype(top) = LUA_T_CFUNCTION; |
