aboutsummaryrefslogtreecommitdiff
path: root/opcode.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1997-06-09 14:28:14 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1997-06-09 14:28:14 -0300
commitdd22ea4da550c3a158e0f11b928c349336184f85 (patch)
treeb73e2ae0498c4efa30dee04f710fb1a349f164ed /opcode.c
parent5fdcfeb353d726a9bcd6d4db5dd0b62b9b8e4b02 (diff)
downloadlua-dd22ea4da550c3a158e0f11b928c349336184f85.tar.gz
lua-dd22ea4da550c3a158e0f11b928c349336184f85.tar.bz2
lua-dd22ea4da550c3a158e0f11b928c349336184f85.zip
new implementation for udata (again they are just void *);
new implementation for the API: most operations now do not disturb structures lua2C and C2lua.
Diffstat (limited to 'opcode.c')
-rw-r--r--opcode.c95
1 files changed, 36 insertions, 59 deletions
diff --git a/opcode.c b/opcode.c
index 8f56b8de..b51598ea 100644
--- a/opcode.c
+++ b/opcode.c
@@ -3,7 +3,7 @@
3** TecCGraf - PUC-Rio 3** TecCGraf - PUC-Rio
4*/ 4*/
5 5
6char *rcs_opcode="$Id: opcode.c,v 4.5 1997/05/26 14:23:55 roberto Exp roberto $"; 6char *rcs_opcode="$Id: opcode.c,v 4.6 1997/06/06 20:54:40 roberto Exp roberto $";
7 7
8#include <setjmp.h> 8#include <setjmp.h>
9#include <stdio.h> 9#include <stdio.h>
@@ -52,13 +52,13 @@ static TObject *top = &initial_stack;
52#define incr_top if (++top >= stackLimit) growstack() 52#define incr_top if (++top >= stackLimit) growstack()
53 53
54struct C_Lua_Stack { 54struct C_Lua_Stack {
55 StkId base; /* when Lua calls C or C calls Lua, points to */ 55 StkId base; /* when Lua calls C or C calls Lua, points to */
56 /* the first slot after the last parameter. */ 56 /* the first slot after the last parameter. */
57 int num; /* when Lua calls C, has the number of parameters; */ 57 StkId lua2C; /* points to first element of "array" lua2C */
58 /* when C calls Lua, has the number of results. */ 58 int num; /* size of "array" lua2C */
59}; 59};
60 60
61static struct C_Lua_Stack CLS_current = {0, 0}; 61static struct C_Lua_Stack CLS_current = {0, 0, 0};
62 62
63static jmp_buf *errorJmp = NULL; /* current error recover point */ 63static jmp_buf *errorJmp = NULL; /* current error recover point */
64 64
@@ -228,6 +228,13 @@ static lua_Object put_luaObjectonTop (void)
228} 228}
229 229
230 230
231lua_Object lua_pop (void)
232{
233 checkCparams(1);
234 return put_luaObjectonTop();
235}
236
237
231 238
232/* 239/*
233** call Line hook 240** call Line hook
@@ -235,7 +242,7 @@ static lua_Object put_luaObjectonTop (void)
235static void lineHook (int line) 242static void lineHook (int line)
236{ 243{
237 struct C_Lua_Stack oldCLS = CLS_current; 244 struct C_Lua_Stack oldCLS = CLS_current;
238 StkId old_top = CLS_current.base = top-stack; 245 StkId old_top = CLS_current.lua2C = CLS_current.base = top-stack;
239 CLS_current.num = 0; 246 CLS_current.num = 0;
240 (*lua_linehook)(line); 247 (*lua_linehook)(line);
241 top = stack+old_top; 248 top = stack+old_top;
@@ -250,7 +257,7 @@ static void lineHook (int line)
250static void callHook (StkId base, lua_Type type, int isreturn) 257static void callHook (StkId base, lua_Type type, int isreturn)
251{ 258{
252 struct C_Lua_Stack oldCLS = CLS_current; 259 struct C_Lua_Stack oldCLS = CLS_current;
253 StkId old_top = CLS_current.base = top-stack; 260 StkId old_top = CLS_current.lua2C = CLS_current.base = top-stack;
254 CLS_current.num = 0; 261 CLS_current.num = 0;
255 if (isreturn) 262 if (isreturn)
256 (*lua_callhook)(LUA_NOOBJECT, "(return)", 0); 263 (*lua_callhook)(LUA_NOOBJECT, "(return)", 0);
@@ -278,6 +285,7 @@ static StkId callC (lua_CFunction func, StkId base)
278 StkId firstResult; 285 StkId firstResult;
279 CLS_current.num = (top-stack) - base; 286 CLS_current.num = (top-stack) - base;
280 /* incorporate parameters on the stack */ 287 /* incorporate parameters on the stack */
288 CLS_current.lua2C = base;
281 CLS_current.base = base+CLS_current.num; /* == top-stack */ 289 CLS_current.base = base+CLS_current.num; /* == top-stack */
282 if (lua_callhook) 290 if (lua_callhook)
283 callHook(base, LUA_T_CMARK, 0); 291 callHook(base, LUA_T_CMARK, 0);
@@ -518,7 +526,7 @@ int lua_setlocal (lua_Function func, int local_number)
518{ 526{
519 TObject *f = Address(func); 527 TObject *f = Address(func);
520 char *name = luaI_getlocalname(f->value.tf, local_number, lua_currentline(func)); 528 char *name = luaI_getlocalname(f->value.tf, local_number, lua_currentline(func));
521 adjustC(1); 529 checkCparams(1);
522 --top; 530 --top;
523 if (name) 531 if (name)
524 { 532 {
@@ -537,9 +545,11 @@ int lua_setlocal (lua_Function func, int local_number)
537*/ 545*/
538static void do_callinc (int nResults) 546static void do_callinc (int nResults)
539{ 547{
540 do_call(CLS_current.base+1, nResults); 548 StkId base = CLS_current.base;
541 CLS_current.num = (top-stack) - CLS_current.base; /* number of results */ 549 do_call(base+1, nResults);
542 CLS_current.base += CLS_current.num; /* incorporate results on the stack */ 550 CLS_current.lua2C = base; /* position of the new results */
551 CLS_current.num = (top-stack) - base; /* number of results */
552 CLS_current.base = base + CLS_current.num; /* incorporate results on stack */
543} 553}
544 554
545static void do_unprotectedrun (lua_CFunction f, int nParams, int nResults) 555static void do_unprotectedrun (lua_CFunction f, int nParams, int nResults)
@@ -628,15 +638,6 @@ int lua_callfunction (lua_Object function)
628} 638}
629 639
630 640
631int lua_call (char *funcname)
632{
633 Word n = luaI_findsymbolbyname(funcname);
634 open_stack((top-stack)-CLS_current.base);
635 stack[CLS_current.base] = s_object(n);
636 return do_protectedrun(MULT_RET);
637}
638
639
640/* 641/*
641** Open file, generate opcode and execute global statement. Return 0 on 642** Open file, generate opcode and execute global statement. Return 0 on
642** success or non 0 on error. 643** success or non 0 on error.
@@ -791,9 +792,9 @@ lua_Object lua_createtable (void)
791lua_Object lua_lua2C (int number) 792lua_Object lua_lua2C (int number)
792{ 793{
793 if (number <= 0 || number > CLS_current.num) return LUA_NOOBJECT; 794 if (number <= 0 || number > CLS_current.num) return LUA_NOOBJECT;
794 /* Ref(stack+(CLS_current.base-CLS_current.num+number-1)) == 795 /* Ref(stack+(CLS_current.lua2C+number-1)) ==
795 stack+(CLS_current.base-CLS_current.num+number-1)-stack+1 == */ 796 stack+(CLS_current.lua2C+number-1)-stack+1 == */
796 return CLS_current.base-CLS_current.num+number; 797 return CLS_current.lua2C+number;
797} 798}
798 799
799int lua_isnil (lua_Object o) 800int lua_isnil (lua_Object o)
@@ -850,33 +851,20 @@ real lua_getnumber (lua_Object object)
850*/ 851*/
851char *lua_getstring (lua_Object object) 852char *lua_getstring (lua_Object object)
852{ 853{
853 if (object == LUA_NOOBJECT) return NULL; 854 if (object == LUA_NOOBJECT || tostring (Address(object)))
854 if (tostring (Address(object))) return NULL;
855 else return (svalue(Address(object)));
856}
857
858void *lua_getbindata (lua_Object object)
859{
860 if (object == LUA_NOOBJECT || ttype(Address(object)) != LUA_T_USERDATA)
861 return NULL; 855 return NULL;
862 else return svalue(Address(object)); 856 else return (svalue(Address(object)));
863}
864
865void *lua_getuserdata (lua_Object object)
866{
867 void *add = lua_getbindata(object);
868 if (add == NULL) return NULL;
869 else return *(void **)add;
870} 857}
871 858
872 859
873int lua_getbindatasize (lua_Object object) 860void *lua_getuserdata (lua_Object object)
874{ 861{
875 if (object == LUA_NOOBJECT || ttype(Address(object)) != LUA_T_USERDATA) 862 if (object == LUA_NOOBJECT || ttype(Address(object)) != LUA_T_USERDATA)
876 return 0; 863 return NULL;
877 else return (Address(object))->value.ts->size; 864 else return tsvalue(Address(object))->u.v;
878} 865}
879 866
867
880/* 868/*
881** Given an object handle, return its cfuntion pointer. On error, return NULL. 869** Given an object handle, return its cfuntion pointer. On error, return NULL.
882*/ 870*/
@@ -1000,25 +988,14 @@ void lua_pushcfunction (lua_CFunction fn)
1000 incr_top; 988 incr_top;
1001} 989}
1002 990
1003void lua_pushbindata (void *buff, int size, int tag)
1004{
1005 if (buff == NULL)
1006 ttype(top) = LUA_T_NIL;
1007 else {
1008 if (tag < 0)
1009 luaI_realtag(tag);
1010 tsvalue(top) = luaI_createuserdata(buff, size, tag);
1011 ttype(top) = LUA_T_USERDATA;
1012 }
1013 incr_top;
1014}
1015 991
1016/* 992
1017** Push an object (ttype=userdata) to stack.
1018*/
1019void lua_pushusertag (void *u, int tag) 993void lua_pushusertag (void *u, int tag)
1020{ 994{
1021 lua_pushbindata(&u, sizeof(void *), tag); 995 if (tag < 0) luaI_realtag(tag); /* error if tag is not valid */
996 tsvalue(top) = luaI_createudata(u, tag);
997 ttype(top) = LUA_T_USERDATA;
998 incr_top;
1022} 999}
1023 1000
1024/* 1001/*