6 char *rcs_opcode
="$Id: opcode.c,v 4.15 1997/06/26 21:40:57 roberto Exp $";
24 #define tonumber(o) ((ttype(o) != LUA_T_NUMBER) && (lua_tonumber(o) != 0))
25 #define tostring(o) ((ttype(o) != LUA_T_STRING) && (lua_tostring(o) != 0))
28 #define STACK_SIZE 128
31 #define STACK_LIMIT 6000
34 typedef int StkId
; /* index to stack elements */
36 static TObject initial_stack
;
38 static TObject
*stackLimit
= &initial_stack
+1;
39 static TObject
*stack
= &initial_stack
;
40 static TObject
*top
= &initial_stack
;
43 /* macros to convert from lua_Object to (TObject *) and back */
45 #define Address(lo) ((lo)+stack-1)
46 #define Ref(st) ((st)-stack+1)
49 /* macro to increment stack top. There must be always an empty slot in
52 #define incr_top if (++top >= stackLimit) growstack()
55 StkId base
; /* when Lua calls C or C calls Lua, points to */
56 /* the first slot after the last parameter. */
57 StkId lua2C
; /* points to first element of "array" lua2C */
58 int num
; /* size of "array" lua2C */
61 static struct C_Lua_Stack CLS_current
= {0, 0, 0};
63 static jmp_buf *errorJmp
= NULL
; /* current error recover point */
67 lua_LHFunction lua_linehook
= NULL
;
68 lua_CHFunction lua_callhook
= NULL
;
71 static StkId
lua_execute (Byte
*pc
, StkId base
);
72 static void do_call (StkId base
, int nResults
);
76 TObject
*luaI_Address (lua_Object o
)
85 static void lua_initstack (void)
87 Long maxstack
= STACK_SIZE
;
88 stack
= newvector(maxstack
, TObject
);
89 stackLimit
= stack
+maxstack
;
91 *(top
++) = initial_stack
;
96 ** Check stack overflow and, if necessary, realloc vector
98 #define lua_checkstack(nt) if ((nt) >= stackLimit) growstack()
100 static void growstack (void)
102 if (stack
== &initial_stack
)
106 static int limit
= STACK_LIMIT
;
108 Long stacksize
= stackLimit
- stack
;
109 stacksize
= growvector(&stack
, stacksize
, TObject
, stackEM
, limit
+100);
110 stackLimit
= stack
+stacksize
;
112 if (stacksize
>= limit
)
122 ** Concatenate two given strings. Return the new string pointer.
124 static char *lua_strconc (char *l
, char *r
)
126 size_t nl
= strlen(l
);
127 char *buffer
= luaI_buffer(nl
+strlen(r
)+1);
129 strcpy(buffer
+nl
, r
);
135 ** Convert, if possible, to a number object.
136 ** Return 0 if success, not 0 if error.
138 static int lua_tonumber (TObject
*obj
)
142 if (ttype(obj
) != LUA_T_STRING
)
144 else if (sscanf(svalue(obj
), "%f %c",&t
, &c
) == 1)
147 ttype(obj
) = LUA_T_NUMBER
;
156 ** Convert, if possible, to a string ttype
157 ** Return 0 in success or not 0 on error.
159 static int lua_tostring (TObject
*obj
)
161 if (ttype(obj
) != LUA_T_NUMBER
)
165 real f
= nvalue(obj
);
167 if ((real
)(-MAX_INT
) <= f
&& f
<= (real
)MAX_INT
&& (real
)(i
=(int)f
) == f
)
168 sprintf (s
, "%d", i
);
170 sprintf (s
, "%g", nvalue(obj
));
171 tsvalue(obj
) = lua_createstring(s
);
172 ttype(obj
) = LUA_T_STRING
;
179 ** Adjust stack. Set top to the given value, pushing NILs if needed.
181 static void adjust_top_aux (StkId newtop
)
184 lua_checkstack(stack
+newtop
);
185 nt
= stack
+newtop
; /* warning: previous call may change stack */
186 while (top
< nt
) ttype(top
++) = LUA_T_NIL
;
190 #define adjust_top(newtop) { if (newtop <= top-stack) \
191 top = stack+newtop; \
192 else adjust_top_aux(newtop); }
194 #define adjustC(nParams) adjust_top(CLS_current.base+nParams)
197 static void checkCparams (int nParams
)
199 if (top
-stack
< CLS_current
.base
+nParams
)
200 lua_error("API error - wrong number of arguments in C2lua stack");
205 ** Open a hole below "nelems" from the top.
207 static void open_stack (int nelems
)
210 for (i
=0; i
<nelems
; i
++)
211 *(top
-i
) = *(top
-i
-1);
216 static lua_Object
put_luaObject (TObject
*o
)
218 open_stack((top
-stack
)-CLS_current
.base
);
219 stack
[CLS_current
.base
++] = *o
;
220 return CLS_current
.base
; /* this is +1 real position (see Ref) */
224 static lua_Object
put_luaObjectonTop (void)
226 open_stack((top
-stack
)-CLS_current
.base
);
227 stack
[CLS_current
.base
++] = *(--top
);
228 return CLS_current
.base
; /* this is +1 real position (see Ref) */
232 lua_Object
lua_pop (void)
235 return put_luaObjectonTop();
243 static void lineHook (int line
)
245 struct C_Lua_Stack oldCLS
= CLS_current
;
246 StkId old_top
= CLS_current
.lua2C
= CLS_current
.base
= top
-stack
;
248 (*lua_linehook
)(line
);
250 CLS_current
= oldCLS
;
256 ** The function being called is in [stack+base-1]
258 static void callHook (StkId base
, lua_Type type
, int isreturn
)
260 struct C_Lua_Stack oldCLS
= CLS_current
;
261 StkId old_top
= CLS_current
.lua2C
= CLS_current
.base
= top
-stack
;
264 (*lua_callhook
)(LUA_NOOBJECT
, "(return)", 0);
267 TObject
*f
= stack
+base
-1;
268 if (type
== LUA_T_MARK
)
269 (*lua_callhook
)(Ref(f
), f
->value
.tf
->fileName
, f
->value
.tf
->lineDefined
);
271 (*lua_callhook
)(Ref(f
), "(C)", -1);
274 CLS_current
= oldCLS
;
279 ** Call a C function. CLS_current.base will point to the top of the stack,
280 ** and CLS_current.num is the number of parameters. Returns an index
281 ** to the first result from C.
283 static StkId
callC (lua_CFunction func
, StkId base
)
285 struct C_Lua_Stack oldCLS
= CLS_current
;
287 CLS_current
.num
= (top
-stack
) - base
;
288 /* incorporate parameters on the stack */
289 CLS_current
.lua2C
= base
;
290 CLS_current
.base
= base
+CLS_current
.num
; /* == top-stack */
292 callHook(base
, LUA_T_CMARK
, 0);
294 if (lua_callhook
) /* func may have changed lua_callhook */
295 callHook(base
, LUA_T_CMARK
, 1);
296 firstResult
= CLS_current
.base
;
297 CLS_current
= oldCLS
;
301 static void callIM (TObject
*f
, int nParams
, int nResults
)
304 *(top
-nParams
-1) = *f
;
305 do_call((top
-stack
)-nParams
, nResults
);
310 ** Call a function (C or Lua). The parameters must be on the stack,
311 ** between [stack+base,top). The function to be called is at stack+base-1.
312 ** When returns, the results are on the stack, between [stack+base-1,top).
313 ** The number of results is nResults, unless nResults=MULT_RET.
315 static void do_call (StkId base
, int nResults
)
318 TObject
*func
= stack
+base
-1;
320 if (ttype(func
) == LUA_T_CFUNCTION
) {
321 ttype(func
) = LUA_T_CMARK
;
322 firstResult
= callC(fvalue(func
), base
);
324 else if (ttype(func
) == LUA_T_FUNCTION
) {
325 ttype(func
) = LUA_T_MARK
;
326 firstResult
= lua_execute(func
->value
.tf
->code
, base
);
328 else { /* func is not a function */
329 /* Check the tag method for invalid functions */
330 TObject
*im
= luaI_getimbyObj(func
, IM_FUNCTION
);
331 if (ttype(im
) == LUA_T_NIL
)
332 lua_error("call expression not a function");
333 open_stack((top
-stack
)-(base
-1));
335 do_call(base
, nResults
);
338 /* adjust the number of results */
339 if (nResults
!= MULT_RET
)
340 adjust_top(firstResult
+nResults
);
341 /* move results to base-1 (to erase parameters and function) */
343 nResults
= top
- (stack
+firstResult
); /* actual number of results */
344 for (i
=0; i
<nResults
; i
++)
345 *(stack
+base
+i
) = *(stack
+firstResult
+i
);
346 top
-= firstResult
-base
;
351 ** Function to index a table. Receives the table at top-2 and the index
354 static void pushsubscript (void)
357 if (ttype(top
-2) != LUA_T_ARRAY
) /* not a table, get "gettable" method */
358 im
= luaI_getimbyObj(top
-2, IM_GETTABLE
);
359 else { /* object is a table... */
360 int tg
= (top
-2)->value
.a
->htag
;
361 im
= luaI_getim(tg
, IM_GETTABLE
);
362 if (ttype(im
) == LUA_T_NIL
) { /* and does not have a "gettable" method */
363 TObject
*h
= lua_hashget(avalue(top
-2), top
-1);
364 if (h
!= NULL
&& ttype(h
) != LUA_T_NIL
) {
368 else if (ttype(im
=luaI_getim(tg
, IM_INDEX
)) != LUA_T_NIL
)
372 ttype(top
-1) = LUA_T_NIL
;
376 /* else it has a "gettable" method, go through to next command */
378 /* object is not a table, or it has a "gettable" method */
379 if (ttype(im
) != LUA_T_NIL
)
382 lua_error("indexed expression not a table");
386 lua_Object
lua_rawgettable (void)
389 if (ttype(top
-2) != LUA_T_ARRAY
)
390 lua_error("indexed expression not a table in raw gettable");
392 TObject
*h
= lua_hashget(avalue(top
-2), top
-1);
397 ttype(top
-1) = LUA_T_NIL
;
399 return put_luaObjectonTop();
404 ** Function to store indexed based on values at the top
405 ** mode = 0: raw store (without internal methods)
406 ** mode = 1: normal store (with internal methods)
407 ** mode = 2: "deep stack" store (with internal methods)
409 static void storesubscript (TObject
*t
, int mode
)
411 TObject
*im
= (mode
== 0) ? NULL
: luaI_getimbyObj(t
, IM_SETTABLE
);
412 if (ttype(t
) == LUA_T_ARRAY
&& (im
== NULL
|| ttype(im
) == LUA_T_NIL
)) {
413 TObject
*h
= lua_hashdefine(avalue(t
), t
+1);
415 top
-= (mode
== 2) ? 1 : 3;
417 else { /* object is not a table, and/or has a specific "settable" method */
418 if (im
&& ttype(im
) != LUA_T_NIL
) {
420 lua_checkstack(top
+2);
429 lua_error("indexed expression not a table");
434 static void getglobal (Word n
)
436 TObject
*value
= &lua_table
[n
].object
;
437 TObject
*im
= luaI_getimbyObj(value
, IM_GETGLOBAL
);
438 if (ttype(im
) == LUA_T_NIL
) { /* default behavior */
443 ttype(top
) = LUA_T_STRING
;
444 tsvalue(top
) = lua_table
[n
].varname
;
453 ** Traverse all objects on stack
455 void lua_travstack (int (*fn
)(TObject
*))
458 for (i
= (top
-1)-stack
; i
>=0; i
--)
464 ** Error messages and debug functions
467 static void lua_message (char *s
)
469 TObject
*im
= luaI_geterrorim();
470 if (ttype(im
) != LUA_T_NIL
) {
477 ** Reports an error, and jumps up to the available recover label
479 void lua_error (char *s
)
481 if (s
) lua_message(s
);
483 longjmp(*errorJmp
, 1);
486 fprintf (stderr
, "lua: exit(1). Unable to recover\n");
492 lua_Function
lua_stackedfunction (int level
)
495 for (i
= (top
-1)-stack
; i
>=0; i
--)
496 if (stack
[i
].ttype
== LUA_T_MARK
|| stack
[i
].ttype
== LUA_T_CMARK
)
503 int lua_currentline (lua_Function func
)
505 TObject
*f
= Address(func
);
506 return (f
+1 < top
&& (f
+1)->ttype
== LUA_T_LINE
) ? (f
+1)->value
.i
: -1;
510 lua_Object
lua_getlocal (lua_Function func
, int local_number
, char **name
)
512 TObject
*f
= luaI_Address(func
);
513 *name
= luaI_getlocalname(f
->value
.tf
, local_number
, lua_currentline(func
));
516 /* if "*name", there must be a LUA_T_LINE */
517 /* therefore, f+2 points to function base */
518 return Ref((f
+2)+(local_number
-1));
524 int lua_setlocal (lua_Function func
, int local_number
)
526 TObject
*f
= Address(func
);
527 char *name
= luaI_getlocalname(f
->value
.tf
, local_number
, lua_currentline(func
));
532 /* if "name", there must be a LUA_T_LINE */
533 /* therefore, f+2 points to function base */
534 *((f
+2)+(local_number
-1)) = *top
;
542 ** Call the function at CLS_current.base, and incorporate results on
543 ** the Lua2C structure.
545 static void do_callinc (int nResults
)
547 StkId base
= CLS_current
.base
;
548 do_call(base
+1, nResults
);
549 CLS_current
.lua2C
= base
; /* position of the new results */
550 CLS_current
.num
= (top
-stack
) - base
; /* number of results */
551 CLS_current
.base
= base
+ CLS_current
.num
; /* incorporate results on stack */
555 static void do_unprotectedrun (lua_CFunction f
, int nParams
, int nResults
)
557 StkId base
= (top
-stack
)-nParams
;
559 stack
[base
].ttype
= LUA_T_CFUNCTION
;
560 stack
[base
].value
.f
= f
;
561 do_call(base
+1, nResults
);
566 ** Execute a protected call. Assumes that function is at CLS_current.base and
567 ** parameters are on top of it. Leave nResults on the stack.
569 static int do_protectedrun (int nResults
)
573 struct C_Lua_Stack oldCLS
= CLS_current
;
574 jmp_buf *oldErr
= errorJmp
;
575 errorJmp
= &myErrorJmp
;
576 if (setjmp(myErrorJmp
) == 0) {
577 do_callinc(nResults
);
580 else { /* an error occurred: restore CLS_current and top */
581 CLS_current
= oldCLS
;
582 top
= stack
+CLS_current
.base
;
589 int luaI_dorun (TFunc
*tf
)
592 adjustC(1); /* one slot for the pseudo-function */
593 stack
[CLS_current
.base
].ttype
= LUA_T_FUNCTION
;
594 stack
[CLS_current
.base
].value
.tf
= tf
;
595 status
= do_protectedrun(MULT_RET
);
600 int lua_domain (void)
605 jmp_buf *oldErr
= errorJmp
;
606 errorJmp
= &myErrorJmp
;
608 if (setjmp(myErrorJmp
) == 0) {
613 adjustC(0); /* erase extra slot */
617 status
= luaI_dorun(&tf
);
624 ** Execute the given lua function. Return 0 on success or 1 on error.
626 int lua_callfunction (lua_Object function
)
628 if (function
== LUA_NOOBJECT
)
632 open_stack((top
-stack
)-CLS_current
.base
);
633 stack
[CLS_current
.base
] = *Address(function
);
634 return do_protectedrun (MULT_RET
);
639 lua_Object
lua_gettagmethod (int tag
, char *event
)
642 lua_pushstring(event
);
643 do_unprotectedrun(luaI_gettagmethod
, 2, 1);
644 return put_luaObjectonTop();
647 lua_Object
lua_settagmethod (int tag
, char *event
)
651 newmethod
= *(--top
);
653 lua_pushstring(event
);
654 *top
= newmethod
; incr_top
;
655 do_unprotectedrun(luaI_settagmethod
, 3, 1);
656 return put_luaObjectonTop();
659 lua_Object
lua_seterrormethod (void)
662 do_unprotectedrun(luaI_seterrormethod
, 1, 1);
663 return put_luaObjectonTop();
668 ** API: receives on the stack the table and the index.
669 ** returns the value.
671 lua_Object
lua_gettable (void)
675 return put_luaObjectonTop();
679 #define MAX_C_BLOCKS 10
681 static int numCblocks
= 0;
682 static struct C_Lua_Stack Cblocks
[MAX_C_BLOCKS
];
685 ** API: starts a new block
687 void lua_beginblock (void)
689 if (numCblocks
>= MAX_C_BLOCKS
)
690 lua_error("`lua_beginblock': too many nested blocks");
691 Cblocks
[numCblocks
] = CLS_current
;
698 void lua_endblock (void)
701 CLS_current
= Cblocks
[numCblocks
];
705 void lua_settag (int tag
)
708 luaI_settag(tag
, --top
);
712 ** API: receives on the stack the table, the index, and the new value.
714 void lua_settable (void)
717 storesubscript(top
-3, 1);
720 void lua_rawsettable (void)
723 storesubscript(top
-3, 0);
727 ** API: creates a new table
729 lua_Object
lua_createtable (void)
732 avalue(&o
) = lua_createarray(0);
733 ttype(&o
) = LUA_T_ARRAY
;
734 return put_luaObject(&o
);
738 ** Get a parameter, returning the object handle or LUA_NOOBJECT on error.
739 ** 'number' must be 1 to get the first parameter.
741 lua_Object
lua_lua2C (int number
)
743 if (number
<= 0 || number
> CLS_current
.num
) return LUA_NOOBJECT
;
744 /* Ref(stack+(CLS_current.lua2C+number-1)) ==
745 stack+(CLS_current.lua2C+number-1)-stack+1 == */
746 return CLS_current
.lua2C
+number
;
749 int lua_isnil (lua_Object o
)
751 return (o
!= LUA_NOOBJECT
) && (ttype(Address(o
)) == LUA_T_NIL
);
754 int lua_istable (lua_Object o
)
756 return (o
!= LUA_NOOBJECT
) && (ttype(Address(o
)) == LUA_T_ARRAY
);
759 int lua_isuserdata (lua_Object o
)
761 return (o
!= LUA_NOOBJECT
) && (ttype(Address(o
)) == LUA_T_USERDATA
);
764 int lua_iscfunction (lua_Object o
)
767 return (t
== LUA_T_CMARK
) || (t
== LUA_T_CFUNCTION
);
770 int lua_isnumber (lua_Object o
)
772 return (o
!= LUA_NOOBJECT
) && (tonumber(Address(o
)) == 0);
775 int lua_isstring (lua_Object o
)
778 return (t
== LUA_T_STRING
) || (t
== LUA_T_NUMBER
);
781 int lua_isfunction (lua_Object o
)
784 return (t
== LUA_T_FUNCTION
) || (t
== LUA_T_CFUNCTION
) ||
785 (t
== LUA_T_MARK
) || (t
== LUA_T_CMARK
);
789 ** Given an object handle, return its number value. On error, return 0.0.
791 real
lua_getnumber (lua_Object object
)
793 if (object
== LUA_NOOBJECT
) return 0.0;
794 if (tonumber (Address(object
))) return 0.0;
795 else return (nvalue(Address(object
)));
799 ** Given an object handle, return its string pointer. On error, return NULL.
801 char *lua_getstring (lua_Object object
)
803 if (object
== LUA_NOOBJECT
|| tostring (Address(object
)))
805 else return (svalue(Address(object
)));
809 void *lua_getuserdata (lua_Object object
)
811 if (object
== LUA_NOOBJECT
|| ttype(Address(object
)) != LUA_T_USERDATA
)
813 else return tsvalue(Address(object
))->u
.v
;
818 ** Given an object handle, return its cfuntion pointer. On error, return NULL.
820 lua_CFunction
lua_getcfunction (lua_Object object
)
822 if (object
== LUA_NOOBJECT
|| ((ttype(Address(object
)) != LUA_T_CFUNCTION
) &&
823 (ttype(Address(object
)) != LUA_T_CMARK
)))
825 else return (fvalue(Address(object
)));
829 lua_Object
lua_getref (int ref
)
831 TObject
*o
= luaI_getref(ref
);
834 return put_luaObject(o
);
838 int lua_ref (int lock
)
841 return luaI_ref(--top
, lock
);
847 ** Get a global object.
849 lua_Object
lua_getglobal (char *name
)
851 getglobal(luaI_findsymbolbyname(name
));
852 return put_luaObjectonTop();
856 lua_Object
lua_rawgetglobal (char *name
)
858 return put_luaObject(&lua_table
[luaI_findsymbolbyname(name
)].object
);
863 ** Store top of the stack at a global variable array field.
865 static void setglobal (Word n
)
867 TObject
*oldvalue
= &lua_table
[n
].object
;
868 TObject
*im
= luaI_getimbyObj(oldvalue
, IM_SETGLOBAL
);
869 if (ttype(im
) == LUA_T_NIL
) /* default behavior */
870 s_object(n
) = *(--top
);
872 TObject newvalue
= *(top
-1);
873 ttype(top
-1) = LUA_T_STRING
;
874 tsvalue(top
-1) = lua_table
[n
].varname
;
884 void lua_setglobal (char *name
)
887 setglobal(luaI_findsymbolbyname(name
));
890 void lua_rawsetglobal (char *name
)
892 Word n
= luaI_findsymbolbyname(name
);
894 s_object(n
) = *(--top
);
900 void lua_pushnil (void)
902 ttype(top
) = LUA_T_NIL
;
907 ** Push an object (ttype=number) to stack.
909 void lua_pushnumber (real n
)
911 ttype(top
) = LUA_T_NUMBER
; nvalue(top
) = n
;
916 ** Push an object (ttype=string) to stack.
918 void lua_pushstring (char *s
)
921 ttype(top
) = LUA_T_NIL
;
924 tsvalue(top
) = lua_createstring(s
);
925 ttype(top
) = LUA_T_STRING
;
932 ** Push an object (ttype=cfunction) to stack.
934 void lua_pushcfunction (lua_CFunction fn
)
936 ttype(top
) = LUA_T_CFUNCTION
; fvalue(top
) = fn
;
942 void lua_pushusertag (void *u
, int tag
)
944 if (tag
< 0 && tag
!= LUA_ANYTAG
)
945 luaI_realtag(tag
); /* error if tag is not valid */
946 tsvalue(top
) = luaI_createudata(u
, tag
);
947 ttype(top
) = LUA_T_USERDATA
;
952 ** Push an object on the stack.
954 void luaI_pushobject (TObject
*o
)
961 ** Push a lua_Object on stack.
963 void lua_pushobject (lua_Object o
)
965 if (o
== LUA_NOOBJECT
)
966 lua_error("API error - attempt to push a NOOBJECT");
968 if (ttype(top
) == LUA_T_MARK
) ttype(top
) = LUA_T_FUNCTION
;
969 else if (ttype(top
) == LUA_T_CMARK
) ttype(top
) = LUA_T_CFUNCTION
;
973 int lua_tag (lua_Object lo
)
975 if (lo
== LUA_NOOBJECT
) return LUA_T_NIL
;
977 TObject
*o
= Address(lo
);
978 lua_Type t
= ttype(o
);
979 if (t
== LUA_T_USERDATA
)
980 return o
->value
.ts
->tag
;
981 else if (t
== LUA_T_ARRAY
)
982 return o
->value
.a
->htag
;
988 void luaI_gcIM (TObject
*o
)
990 TObject
*im
= luaI_getimbyObj(o
, IM_GC
);
991 if (ttype(im
) != LUA_T_NIL
) {
999 static void call_binTM (IMS event
, char *msg
)
1001 TObject
*im
= luaI_getimbyObj(top
-2, event
); /* try first operand */
1002 if (ttype(im
) == LUA_T_NIL
) {
1003 im
= luaI_getimbyObj(top
-1, event
); /* try second operand */
1004 if (ttype(im
) == LUA_T_NIL
) {
1005 im
= luaI_getim(0, event
); /* try a 'global' i.m. */
1006 if (ttype(im
) == LUA_T_NIL
)
1010 lua_pushstring(luaI_eventname
[event
]);
1015 static void call_arith (IMS event
)
1017 call_binTM(event
, "unexpected type at arithmetic operation");
1021 static void comparison (lua_Type ttype_less
, lua_Type ttype_equal
,
1022 lua_Type ttype_great
, IMS op
)
1027 if (ttype(l
) == LUA_T_NUMBER
&& ttype(r
) == LUA_T_NUMBER
)
1028 result
= (nvalue(l
) < nvalue(r
)) ? -1 : (nvalue(l
) == nvalue(r
)) ? 0 : 1;
1029 else if (ttype(l
) == LUA_T_STRING
&& ttype(r
) == LUA_T_STRING
)
1030 result
= strcmp(svalue(l
), svalue(r
));
1032 call_binTM(op
, "unexpected type at comparison");
1037 ttype(top
-1) = (result
< 0) ? ttype_less
:
1038 (result
== 0) ? ttype_equal
: ttype_great
;
1042 static void adjust_varargs (StkId first_extra_arg
)
1045 TObject
*firstelem
= stack
+first_extra_arg
;
1046 int nvararg
= top
-firstelem
;
1048 if (nvararg
< 0) nvararg
= 0;
1049 avalue(&arg
) = lua_createarray(nvararg
+1); /* +1 for field 'n' */
1050 ttype(&arg
) = LUA_T_ARRAY
;
1051 for (i
=0; i
<nvararg
; i
++) {
1053 ttype(&index
) = LUA_T_NUMBER
;
1054 nvalue(&index
) = i
+1;
1055 *(lua_hashdefine(avalue(&arg
), &index
)) = *(firstelem
+i
);
1057 /* store counter in field "n" */ {
1058 TObject index
, extra
;
1059 ttype(&index
) = LUA_T_STRING
;
1060 tsvalue(&index
) = lua_createstring("n");
1061 ttype(&extra
) = LUA_T_NUMBER
;
1062 nvalue(&extra
) = nvararg
;
1063 *(lua_hashdefine(avalue(&arg
), &index
)) = extra
;
1065 adjust_top(first_extra_arg
);
1066 *top
= arg
; incr_top
;
1072 ** Execute the given opcode, until a RET. Parameters are between
1073 ** [stack+base,top). Returns n such that the the results are between
1076 static StkId
lua_execute (Byte
*pc
, StkId base
)
1079 callHook (base
, LUA_T_MARK
, 0);
1083 switch (opcode
= (OpCode
)*pc
++)
1085 case PUSHNIL
: ttype(top
) = LUA_T_NIL
; incr_top
; break;
1087 case PUSH0
: case PUSH1
: case PUSH2
:
1088 ttype(top
) = LUA_T_NUMBER
;
1089 nvalue(top
) = opcode
-PUSH0
;
1094 ttype(top
) = LUA_T_NUMBER
; nvalue(top
) = *pc
++; incr_top
; break;
1100 ttype(top
) = LUA_T_NUMBER
; nvalue(top
) = w
;
1109 ttype(top
) = LUA_T_NUMBER
; nvalue(top
) = num
;
1118 ttype(top
) = LUA_T_STRING
; tsvalue(top
) = lua_constant
[w
];
1127 luaI_insertfunction(f
); /* may take part in GC */
1128 top
->ttype
= LUA_T_FUNCTION
;
1134 case PUSHLOCAL0
: case PUSHLOCAL1
: case PUSHLOCAL2
:
1135 case PUSHLOCAL3
: case PUSHLOCAL4
: case PUSHLOCAL5
:
1136 case PUSHLOCAL6
: case PUSHLOCAL7
: case PUSHLOCAL8
:
1138 *top
= *((stack
+base
) + (int)(opcode
-PUSHLOCAL0
)); incr_top
; break;
1140 case PUSHLOCAL
: *top
= *((stack
+base
) + (*pc
++)); incr_top
; break;
1156 TObject receiver
= *(top
-1);
1159 ttype(top
) = LUA_T_STRING
; tsvalue(top
) = lua_constant
[w
];
1167 case STORELOCAL0
: case STORELOCAL1
: case STORELOCAL2
:
1168 case STORELOCAL3
: case STORELOCAL4
: case STORELOCAL5
:
1169 case STORELOCAL6
: case STORELOCAL7
: case STORELOCAL8
:
1171 *((stack
+base
) + (int)(opcode
-STORELOCAL0
)) = *(--top
);
1174 case STORELOCAL
: *((stack
+base
) + (*pc
++)) = *(--top
); break;
1185 storesubscript(top
-3, 1);
1188 case STOREINDEXED
: {
1190 storesubscript(top
-3-n
, 2);
1199 if (opcode
== STORELIST0
) m
= 0;
1200 else m
= *(pc
++) * FIELDS_PER_FLUSH
;
1205 ttype(top
) = LUA_T_NUMBER
; nvalue(top
) = n
+m
;
1206 *(lua_hashdefine (avalue(arr
), top
)) = *(top
-1);
1213 case STORERECORD
: /* opcode obsolete: supersed by STOREMAP */
1216 TObject
*arr
= top
-n
-1;
1221 ttype(top
) = LUA_T_STRING
; tsvalue(top
) = lua_constant
[w
];
1222 *(lua_hashdefine (avalue(arr
), top
)) = *(top
-1);
1231 TObject
*arr
= top
-(2*n
)-1;
1233 *(lua_hashdefine (avalue(arr
), top
-2)) = *(top
-1);
1244 StkId newtop
= base
+ *(pc
++);
1250 adjust_varargs(base
+ *(pc
++));
1257 avalue(top
) = lua_createarray(size
);
1258 ttype(top
) = LUA_T_ARRAY
;
1265 int res
= lua_equalObj(top
-2, top
-1);
1267 ttype(top
-1) = res
? LUA_T_NUMBER
: LUA_T_NIL
;
1273 comparison(LUA_T_NUMBER
, LUA_T_NIL
, LUA_T_NIL
, IM_LT
);
1277 comparison(LUA_T_NUMBER
, LUA_T_NUMBER
, LUA_T_NIL
, IM_LE
);
1281 comparison(LUA_T_NIL
, LUA_T_NIL
, LUA_T_NUMBER
, IM_GT
);
1285 comparison(LUA_T_NIL
, LUA_T_NUMBER
, LUA_T_NUMBER
, IM_GE
);
1292 if (tonumber(r
) || tonumber(l
))
1296 nvalue(l
) += nvalue(r
);
1306 if (tonumber(r
) || tonumber(l
))
1310 nvalue(l
) -= nvalue(r
);
1320 if (tonumber(r
) || tonumber(l
))
1324 nvalue(l
) *= nvalue(r
);
1334 if (tonumber(r
) || tonumber(l
))
1338 nvalue(l
) /= nvalue(r
);
1351 if (tostring(l
) || tostring(r
))
1352 call_binTM(IM_CONCAT
, "unexpected type for concatenation");
1354 tsvalue(l
) = lua_createstring(lua_strconc(svalue(l
),svalue(r
)));
1361 if (tonumber(top
-1))
1363 ttype(top
) = LUA_T_NIL
;
1368 nvalue(top
-1) = - nvalue(top
-1);
1372 ttype(top
-1) = (ttype(top
-1) == LUA_T_NIL
) ? LUA_T_NUMBER
: LUA_T_NIL
;
1380 if (ttype(top
-1) != LUA_T_NIL
) pc
+= w
;
1388 if (ttype(top
-1) == LUA_T_NIL
) pc
+= w
;
1413 if (ttype(top
) == LUA_T_NIL
) pc
+= w
;
1422 if (ttype(top
) == LUA_T_NIL
) pc
-= w
;
1426 case POP
: --top
; break;
1430 int nParams
= *(pc
++);
1431 int nResults
= *(pc
++);
1432 StkId newBase
= (top
-stack
)-nParams
;
1433 do_call(newBase
, nResults
);
1440 callHook (base
, LUA_T_MARK
, 1);
1441 return (base
+ ((opcode
==RETCODE0
) ? 0 : *pc
));
1447 if ((stack
+base
-1)->ttype
!= LUA_T_LINE
)
1449 /* open space for LINE value */
1450 open_stack((top
-stack
)-base
);
1452 (stack
+base
-1)->ttype
= LUA_T_LINE
;
1454 (stack
+base
-1)->value
.i
= line
;
1461 lua_error ("internal error - opcode doesn't match");
1469 ** API: set a function as a fallback
1471 lua_Object
lua_setfallback (char *name
, lua_CFunction fallback
)
1473 lua_pushstring(name
);
1474 lua_pushcfunction(fallback
);
1475 do_unprotectedrun(luaI_setfallback
, 2, 1);
1476 return put_luaObjectonTop();