summaryrefslogtreecommitdiff
path: root/src/lj_api.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lj_api.c')
-rw-r--r--src/lj_api.c103
1 files changed, 61 insertions, 42 deletions
diff --git a/src/lj_api.c b/src/lj_api.c
index 92699078..f33748ce 100644
--- a/src/lj_api.c
+++ b/src/lj_api.c
@@ -115,7 +115,7 @@ LUA_API void lua_xmove(lua_State *from, lua_State *to, int n)
115 115
116LUA_API int lua_gettop(lua_State *L) 116LUA_API int lua_gettop(lua_State *L)
117{ 117{
118 return cast_int(L->top - L->base); 118 return (int)(L->top - L->base);
119} 119}
120 120
121LUA_API void lua_settop(lua_State *L, int idx) 121LUA_API void lua_settop(lua_State *L, int idx)
@@ -186,7 +186,7 @@ LUA_API void lua_pushvalue(lua_State *L, int idx)
186LUA_API int lua_type(lua_State *L, int idx) 186LUA_API int lua_type(lua_State *L, int idx)
187{ 187{
188 cTValue *o = index2adr(L, idx); 188 cTValue *o = index2adr(L, idx);
189 if (tvisnum(o)) { 189 if (tvisnumber(o)) {
190 return LUA_TNUMBER; 190 return LUA_TNUMBER;
191#if LJ_64 191#if LJ_64
192 } else if (tvislightud(o)) { 192 } else if (tvislightud(o)) {
@@ -234,13 +234,13 @@ LUA_API int lua_isnumber(lua_State *L, int idx)
234{ 234{
235 cTValue *o = index2adr(L, idx); 235 cTValue *o = index2adr(L, idx);
236 TValue tmp; 236 TValue tmp;
237 return (tvisnum(o) || (tvisstr(o) && lj_str_tonum(strV(o), &tmp))); 237 return (tvisnumber(o) || (tvisstr(o) && lj_str_tonumber(strV(o), &tmp)));
238} 238}
239 239
240LUA_API int lua_isstring(lua_State *L, int idx) 240LUA_API int lua_isstring(lua_State *L, int idx)
241{ 241{
242 cTValue *o = index2adr(L, idx); 242 cTValue *o = index2adr(L, idx);
243 return (tvisstr(o) || tvisnum(o)); 243 return (tvisstr(o) || tvisnumber(o));
244} 244}
245 245
246LUA_API int lua_isuserdata(lua_State *L, int idx) 246LUA_API int lua_isuserdata(lua_State *L, int idx)
@@ -260,8 +260,10 @@ LUA_API int lua_equal(lua_State *L, int idx1, int idx2)
260{ 260{
261 cTValue *o1 = index2adr(L, idx1); 261 cTValue *o1 = index2adr(L, idx1);
262 cTValue *o2 = index2adr(L, idx2); 262 cTValue *o2 = index2adr(L, idx2);
263 if (tvisnum(o1) && tvisnum(o2)) { 263 if (tvisint(o1) && tvisint(o2)) {
264 return numV(o1) == numV(o2); 264 return intV(o1) == intV(o2);
265 } else if (tvisnumber(o1) && tvisnumber(o2)) {
266 return numberVnum(o1) == numberVnum(o2);
265 } else if (itype(o1) != itype(o2)) { 267 } else if (itype(o1) != itype(o2)) {
266 return 0; 268 return 0;
267 } else if (tvispri(o1)) { 269 } else if (tvispri(o1)) {
@@ -293,8 +295,10 @@ LUA_API int lua_lessthan(lua_State *L, int idx1, int idx2)
293 cTValue *o2 = index2adr(L, idx2); 295 cTValue *o2 = index2adr(L, idx2);
294 if (o1 == niltv(L) || o2 == niltv(L)) { 296 if (o1 == niltv(L) || o2 == niltv(L)) {
295 return 0; 297 return 0;
296 } else if (tvisnum(o1) && tvisnum(o2)) { 298 } else if (tvisint(o1) && tvisint(o2)) {
297 return numV(o1) < numV(o2); 299 return intV(o1) < intV(o2);
300 } else if (tvisnumber(o1) && tvisnumber(o2)) {
301 return numberVnum(o1) < numberVnum(o2);
298 } else { 302 } else {
299 TValue *base = lj_meta_comp(L, o1, o2, 0); 303 TValue *base = lj_meta_comp(L, o1, o2, 0);
300 if ((uintptr_t)base <= 1) { 304 if ((uintptr_t)base <= 1) {
@@ -312,8 +316,8 @@ LUA_API lua_Number lua_tonumber(lua_State *L, int idx)
312{ 316{
313 cTValue *o = index2adr(L, idx); 317 cTValue *o = index2adr(L, idx);
314 TValue tmp; 318 TValue tmp;
315 if (LJ_LIKELY(tvisnum(o))) 319 if (LJ_LIKELY(tvisnumber(o)))
316 return numV(o); 320 return numberVnum(o);
317 else if (tvisstr(o) && lj_str_tonum(strV(o), &tmp)) 321 else if (tvisstr(o) && lj_str_tonum(strV(o), &tmp))
318 return numV(&tmp); 322 return numV(&tmp);
319 else 323 else
@@ -324,8 +328,8 @@ LUALIB_API lua_Number luaL_checknumber(lua_State *L, int idx)
324{ 328{
325 cTValue *o = index2adr(L, idx); 329 cTValue *o = index2adr(L, idx);
326 TValue tmp; 330 TValue tmp;
327 if (tvisnum(o)) 331 if (LJ_LIKELY(tvisnumber(o)))
328 return numV(o); 332 return numberVnum(o);
329 else if (!(tvisstr(o) && lj_str_tonum(strV(o), &tmp))) 333 else if (!(tvisstr(o) && lj_str_tonum(strV(o), &tmp)))
330 lj_err_argt(L, idx, LUA_TNUMBER); 334 lj_err_argt(L, idx, LUA_TNUMBER);
331 return numV(&tmp); 335 return numV(&tmp);
@@ -335,8 +339,8 @@ LUALIB_API lua_Number luaL_optnumber(lua_State *L, int idx, lua_Number def)
335{ 339{
336 cTValue *o = index2adr(L, idx); 340 cTValue *o = index2adr(L, idx);
337 TValue tmp; 341 TValue tmp;
338 if (tvisnum(o)) 342 if (LJ_LIKELY(tvisnumber(o)))
339 return numV(o); 343 return numberVnum(o);
340 else if (tvisnil(o)) 344 else if (tvisnil(o))
341 return def; 345 return def;
342 else if (!(tvisstr(o) && lj_str_tonum(strV(o), &tmp))) 346 else if (!(tvisstr(o) && lj_str_tonum(strV(o), &tmp)))
@@ -349,12 +353,17 @@ LUA_API lua_Integer lua_tointeger(lua_State *L, int idx)
349 cTValue *o = index2adr(L, idx); 353 cTValue *o = index2adr(L, idx);
350 TValue tmp; 354 TValue tmp;
351 lua_Number n; 355 lua_Number n;
352 if (LJ_LIKELY(tvisnum(o))) 356 if (LJ_LIKELY(tvisint(o))) {
357 return intV(o);
358 } else if (LJ_LIKELY(tvisnum(o))) {
353 n = numV(o); 359 n = numV(o);
354 else if (tvisstr(o) && lj_str_tonum(strV(o), &tmp)) 360 } else {
361 if (!(tvisstr(o) && lj_str_tonumber(strV(o), &tmp)))
362 return 0;
363 if (tvisint(&tmp))
364 return (lua_Integer)intV(&tmp);
355 n = numV(&tmp); 365 n = numV(&tmp);
356 else 366 }
357 return 0;
358#if LJ_64 367#if LJ_64
359 return (lua_Integer)n; 368 return (lua_Integer)n;
360#else 369#else
@@ -367,12 +376,17 @@ LUALIB_API lua_Integer luaL_checkinteger(lua_State *L, int idx)
367 cTValue *o = index2adr(L, idx); 376 cTValue *o = index2adr(L, idx);
368 TValue tmp; 377 TValue tmp;
369 lua_Number n; 378 lua_Number n;
370 if (LJ_LIKELY(tvisnum(o))) 379 if (LJ_LIKELY(tvisint(o))) {
380 return intV(o);
381 } else if (LJ_LIKELY(tvisnum(o))) {
371 n = numV(o); 382 n = numV(o);
372 else if (tvisstr(o) && lj_str_tonum(strV(o), &tmp)) 383 } else {
384 if (!(tvisstr(o) && lj_str_tonumber(strV(o), &tmp)))
385 lj_err_argt(L, idx, LUA_TNUMBER);
386 if (tvisint(&tmp))
387 return (lua_Integer)intV(&tmp);
373 n = numV(&tmp); 388 n = numV(&tmp);
374 else 389 }
375 lj_err_argt(L, idx, LUA_TNUMBER);
376#if LJ_64 390#if LJ_64
377 return (lua_Integer)n; 391 return (lua_Integer)n;
378#else 392#else
@@ -385,14 +399,19 @@ LUALIB_API lua_Integer luaL_optinteger(lua_State *L, int idx, lua_Integer def)
385 cTValue *o = index2adr(L, idx); 399 cTValue *o = index2adr(L, idx);
386 TValue tmp; 400 TValue tmp;
387 lua_Number n; 401 lua_Number n;
388 if (LJ_LIKELY(tvisnum(o))) 402 if (LJ_LIKELY(tvisint(o))) {
403 return intV(o);
404 } else if (LJ_LIKELY(tvisnum(o))) {
389 n = numV(o); 405 n = numV(o);
390 else if (tvisnil(o)) 406 } else if (tvisnil(o)) {
391 return def; 407 return def;
392 else if (tvisstr(o) && lj_str_tonum(strV(o), &tmp)) 408 } else {
409 if (!(tvisstr(o) && lj_str_tonumber(strV(o), &tmp)))
410 lj_err_argt(L, idx, LUA_TNUMBER);
411 if (tvisint(&tmp))
412 return (lua_Integer)intV(&tmp);
393 n = numV(&tmp); 413 n = numV(&tmp);
394 else 414 }
395 lj_err_argt(L, idx, LUA_TNUMBER);
396#if LJ_64 415#if LJ_64
397 return (lua_Integer)n; 416 return (lua_Integer)n;
398#else 417#else
@@ -412,10 +431,10 @@ LUA_API const char *lua_tolstring(lua_State *L, int idx, size_t *len)
412 GCstr *s; 431 GCstr *s;
413 if (LJ_LIKELY(tvisstr(o))) { 432 if (LJ_LIKELY(tvisstr(o))) {
414 s = strV(o); 433 s = strV(o);
415 } else if (tvisnum(o)) { 434 } else if (tvisnumber(o)) {
416 lj_gc_check(L); 435 lj_gc_check(L);
417 o = index2adr(L, idx); /* GC may move the stack. */ 436 o = index2adr(L, idx); /* GC may move the stack. */
418 s = lj_str_fromnum(L, &o->n); 437 s = lj_str_fromnumber(L, o);
419 } else { 438 } else {
420 if (len != NULL) *len = 0; 439 if (len != NULL) *len = 0;
421 return NULL; 440 return NULL;
@@ -430,10 +449,10 @@ LUALIB_API const char *luaL_checklstring(lua_State *L, int idx, size_t *len)
430 GCstr *s; 449 GCstr *s;
431 if (LJ_LIKELY(tvisstr(o))) { 450 if (LJ_LIKELY(tvisstr(o))) {
432 s = strV(o); 451 s = strV(o);
433 } else if (tvisnum(o)) { 452 } else if (tvisnumber(o)) {
434 lj_gc_check(L); 453 lj_gc_check(L);
435 o = index2adr(L, idx); /* GC may move the stack. */ 454 o = index2adr(L, idx); /* GC may move the stack. */
436 s = lj_str_fromnum(L, &o->n); 455 s = lj_str_fromnumber(L, o);
437 } else { 456 } else {
438 lj_err_argt(L, idx, LUA_TSTRING); 457 lj_err_argt(L, idx, LUA_TSTRING);
439 } 458 }
@@ -451,10 +470,10 @@ LUALIB_API const char *luaL_optlstring(lua_State *L, int idx,
451 } else if (tvisnil(o)) { 470 } else if (tvisnil(o)) {
452 if (len != NULL) *len = def ? strlen(def) : 0; 471 if (len != NULL) *len = def ? strlen(def) : 0;
453 return def; 472 return def;
454 } else if (tvisnum(o)) { 473 } else if (tvisnumber(o)) {
455 lj_gc_check(L); 474 lj_gc_check(L);
456 o = index2adr(L, idx); /* GC may move the stack. */ 475 o = index2adr(L, idx); /* GC may move the stack. */
457 s = lj_str_fromnum(L, &o->n); 476 s = lj_str_fromnumber(L, o);
458 } else { 477 } else {
459 lj_err_argt(L, idx, LUA_TSTRING); 478 lj_err_argt(L, idx, LUA_TSTRING);
460 } 479 }
@@ -484,8 +503,8 @@ LUA_API size_t lua_objlen(lua_State *L, int idx)
484 return cast(size_t, lj_tab_len(tabV(o))); 503 return cast(size_t, lj_tab_len(tabV(o)));
485 else if (tvisudata(o)) 504 else if (tvisudata(o))
486 return udataV(o)->len; 505 return udataV(o)->len;
487 else if (tvisnum(o)) 506 else if (tvisnumber(o))
488 return lj_str_fromnum(L, &o->n)->len; 507 return lj_str_fromnumber(L, o)->len;
489 else 508 else
490 return 0; 509 return 0;
491} 510}
@@ -551,7 +570,7 @@ LUA_API void lua_pushnumber(lua_State *L, lua_Number n)
551 570
552LUA_API void lua_pushinteger(lua_State *L, lua_Integer n) 571LUA_API void lua_pushinteger(lua_State *L, lua_Integer n)
553{ 572{
554 setnumV(L->top, cast_num(n)); 573 setintptrV(L->top, n);
555 incr_top(L); 574 incr_top(L);
556} 575}
557 576
@@ -687,7 +706,7 @@ LUA_API void lua_concat(lua_State *L, int n)
687 L->top -= n; 706 L->top -= n;
688 break; 707 break;
689 } 708 }
690 n -= cast_int(L->top - top); 709 n -= (int)(L->top - top);
691 L->top = top+2; 710 L->top = top+2;
692 lj_vm_call(L, top, 1+1); 711 lj_vm_call(L, top, 1+1);
693 L->top--; 712 L->top--;
@@ -1085,7 +1104,7 @@ LUA_API int lua_yield(lua_State *L, int nresults)
1085 setcont(top+1, lj_cont_hook); 1104 setcont(top+1, lj_cont_hook);
1086 setframe_pc(top+1, cframe_pc(cf)-1); 1105 setframe_pc(top+1, cframe_pc(cf)-1);
1087 setframe_gc(top+2, obj2gco(L)); 1106 setframe_gc(top+2, obj2gco(L));
1088 top[2].fr.tp.ftsz = cast_int((char *)(top+3)-(char *)L->base)+FRAME_CONT; 1107 top[2].fr.tp.ftsz = (int)((char *)(top+3)-(char *)L->base)+FRAME_CONT;
1089 L->top = L->base = top+3; 1108 L->top = L->base = top+3;
1090 } 1109 }
1091 L->cframe = NULL; 1110 L->cframe = NULL;
@@ -1160,10 +1179,10 @@ LUA_API int lua_gc(lua_State *L, int what, int data)
1160 lj_gc_fullgc(L); 1179 lj_gc_fullgc(L);
1161 break; 1180 break;
1162 case LUA_GCCOUNT: 1181 case LUA_GCCOUNT:
1163 res = cast_int(g->gc.total >> 10); 1182 res = (int)(g->gc.total >> 10);
1164 break; 1183 break;
1165 case LUA_GCCOUNTB: 1184 case LUA_GCCOUNTB:
1166 res = cast_int(g->gc.total & 0x3ff); 1185 res = (int)(g->gc.total & 0x3ff);
1167 break; 1186 break;
1168 case LUA_GCSTEP: { 1187 case LUA_GCSTEP: {
1169 MSize a = (MSize)data << 10; 1188 MSize a = (MSize)data << 10;
@@ -1176,11 +1195,11 @@ LUA_API int lua_gc(lua_State *L, int what, int data)
1176 break; 1195 break;
1177 } 1196 }
1178 case LUA_GCSETPAUSE: 1197 case LUA_GCSETPAUSE:
1179 res = cast_int(g->gc.pause); 1198 res = (int)(g->gc.pause);
1180 g->gc.pause = (MSize)data; 1199 g->gc.pause = (MSize)data;
1181 break; 1200 break;
1182 case LUA_GCSETSTEPMUL: 1201 case LUA_GCSETSTEPMUL:
1183 res = cast_int(g->gc.stepmul); 1202 res = (int)(g->gc.stepmul);
1184 g->gc.stepmul = (MSize)data; 1203 g->gc.stepmul = (MSize)data;
1185 break; 1204 break;
1186 default: 1205 default: