diff options
author | Mike Pall <mike> | 2017-04-07 12:48:37 +0200 |
---|---|---|
committer | Mike Pall <mike> | 2017-04-07 12:48:37 +0200 |
commit | de26f76e2ec1989713a0353366de64adfc6ee111 (patch) | |
tree | 00ecbd6146c4e5fc86666b6b9198e55924689f4d /src | |
parent | 2b8de8cfc6ae483d6ec16b7609bab64e37d6fc02 (diff) | |
download | luajit-de26f76e2ec1989713a0353366de64adfc6ee111.tar.gz luajit-de26f76e2ec1989713a0353366de64adfc6ee111.tar.bz2 luajit-de26f76e2ec1989713a0353366de64adfc6ee111.zip |
From Lua 5.2: Add lua_tonumberx() and lua_tointegerx().
Contributed by François Perrad.
Diffstat (limited to 'src')
-rw-r--r-- | src/lj_api.c | 47 | ||||
-rw-r--r-- | src/lua.h | 2 |
2 files changed, 48 insertions, 1 deletions
diff --git a/src/lj_api.c b/src/lj_api.c index fe8880eb..d17a5754 100644 --- a/src/lj_api.c +++ b/src/lj_api.c | |||
@@ -342,6 +342,22 @@ LUA_API lua_Number lua_tonumber(lua_State *L, int idx) | |||
342 | return 0; | 342 | return 0; |
343 | } | 343 | } |
344 | 344 | ||
345 | LUA_API lua_Number lua_tonumberx(lua_State *L, int idx, int *ok) | ||
346 | { | ||
347 | cTValue *o = index2adr(L, idx); | ||
348 | TValue tmp; | ||
349 | if (LJ_LIKELY(tvisnumber(o))) { | ||
350 | if (ok) *ok = 1; | ||
351 | return numberVnum(o); | ||
352 | } else if (tvisstr(o) && lj_strscan_num(strV(o), &tmp)) { | ||
353 | if (ok) *ok = 1; | ||
354 | return numV(&tmp); | ||
355 | } else { | ||
356 | if (ok) *ok = 0; | ||
357 | return 0; | ||
358 | } | ||
359 | } | ||
360 | |||
345 | LUALIB_API lua_Number luaL_checknumber(lua_State *L, int idx) | 361 | LUALIB_API lua_Number luaL_checknumber(lua_State *L, int idx) |
346 | { | 362 | { |
347 | cTValue *o = index2adr(L, idx); | 363 | cTValue *o = index2adr(L, idx); |
@@ -379,9 +395,38 @@ LUA_API lua_Integer lua_tointeger(lua_State *L, int idx) | |||
379 | if (!(tvisstr(o) && lj_strscan_number(strV(o), &tmp))) | 395 | if (!(tvisstr(o) && lj_strscan_number(strV(o), &tmp))) |
380 | return 0; | 396 | return 0; |
381 | if (tvisint(&tmp)) | 397 | if (tvisint(&tmp)) |
382 | return (lua_Integer)intV(&tmp); | 398 | return intV(&tmp); |
399 | n = numV(&tmp); | ||
400 | } | ||
401 | #if LJ_64 | ||
402 | return (lua_Integer)n; | ||
403 | #else | ||
404 | return lj_num2int(n); | ||
405 | #endif | ||
406 | } | ||
407 | |||
408 | LUA_API lua_Integer lua_tointegerx(lua_State *L, int idx, int *ok) | ||
409 | { | ||
410 | cTValue *o = index2adr(L, idx); | ||
411 | TValue tmp; | ||
412 | lua_Number n; | ||
413 | if (LJ_LIKELY(tvisint(o))) { | ||
414 | if (ok) *ok = 1; | ||
415 | return intV(o); | ||
416 | } else if (LJ_LIKELY(tvisnum(o))) { | ||
417 | n = numV(o); | ||
418 | } else { | ||
419 | if (!(tvisstr(o) && lj_strscan_number(strV(o), &tmp))) { | ||
420 | if (ok) *ok = 0; | ||
421 | return 0; | ||
422 | } | ||
423 | if (tvisint(&tmp)) { | ||
424 | if (ok) *ok = 1; | ||
425 | return intV(&tmp); | ||
426 | } | ||
383 | n = numV(&tmp); | 427 | n = numV(&tmp); |
384 | } | 428 | } |
429 | if (ok) *ok = 1; | ||
385 | #if LJ_64 | 430 | #if LJ_64 |
386 | return (lua_Integer)n; | 431 | return (lua_Integer)n; |
387 | #else | 432 | #else |
@@ -350,6 +350,8 @@ LUA_API int lua_loadx (lua_State *L, lua_Reader reader, void *dt, | |||
350 | const char *chunkname, const char *mode); | 350 | const char *chunkname, const char *mode); |
351 | LUA_API const lua_Number *lua_version (lua_State *L); | 351 | LUA_API const lua_Number *lua_version (lua_State *L); |
352 | LUA_API void lua_copy (lua_State *L, int fromidx, int toidx); | 352 | LUA_API void lua_copy (lua_State *L, int fromidx, int toidx); |
353 | LUA_API lua_Number lua_tonumberx (lua_State *L, int idx, int *isnum); | ||
354 | LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *isnum); | ||
353 | 355 | ||
354 | /* From Lua 5.3. */ | 356 | /* From Lua 5.3. */ |
355 | LUA_API int lua_isyieldable (lua_State *L); | 357 | LUA_API int lua_isyieldable (lua_State *L); |