diff options
Diffstat (limited to 'src/lj_api.c')
-rw-r--r-- | src/lj_api.c | 136 |
1 files changed, 134 insertions, 2 deletions
diff --git a/src/lj_api.c b/src/lj_api.c index ea4eaf66..db48e3a6 100644 --- a/src/lj_api.c +++ b/src/lj_api.c | |||
@@ -91,6 +91,12 @@ LUA_API int lua_checkstack(lua_State *L, int size) | |||
91 | return 1; | 91 | return 1; |
92 | } | 92 | } |
93 | 93 | ||
94 | LUALIB_API void luaL_checkstack(lua_State *L, int size, const char *msg) | ||
95 | { | ||
96 | if (!lua_checkstack(L, size)) | ||
97 | lj_err_callerv(L, LJ_ERR_STKOVM, msg); | ||
98 | } | ||
99 | |||
94 | LUA_API void lua_xmove(lua_State *from, lua_State *to, int n) | 100 | LUA_API void lua_xmove(lua_State *from, lua_State *to, int n) |
95 | { | 101 | { |
96 | TValue *f, *t; | 102 | TValue *f, *t; |
@@ -193,6 +199,18 @@ LUA_API int lua_type(lua_State *L, int idx) | |||
193 | } | 199 | } |
194 | } | 200 | } |
195 | 201 | ||
202 | LUALIB_API void luaL_checktype(lua_State *L, int idx, int tt) | ||
203 | { | ||
204 | if (lua_type(L, idx) != tt) | ||
205 | lj_err_argt(L, idx, tt); | ||
206 | } | ||
207 | |||
208 | LUALIB_API void luaL_checkany(lua_State *L, int idx) | ||
209 | { | ||
210 | if (index2adr(L, idx) == niltv(L)) | ||
211 | lj_err_arg(L, idx, LJ_ERR_NOVAL); | ||
212 | } | ||
213 | |||
196 | LUA_API const char *lua_typename(lua_State *L, int t) | 214 | LUA_API const char *lua_typename(lua_State *L, int t) |
197 | { | 215 | { |
198 | UNUSED(L); | 216 | UNUSED(L); |
@@ -202,7 +220,7 @@ LUA_API const char *lua_typename(lua_State *L, int t) | |||
202 | LUA_API int lua_iscfunction(lua_State *L, int idx) | 220 | LUA_API int lua_iscfunction(lua_State *L, int idx) |
203 | { | 221 | { |
204 | cTValue *o = index2adr(L, idx); | 222 | cTValue *o = index2adr(L, idx); |
205 | return !isluafunc(funcV(o)); | 223 | return tvisfunc(o) && !isluafunc(funcV(o)); |
206 | } | 224 | } |
207 | 225 | ||
208 | LUA_API int lua_isnumber(lua_State *L, int idx) | 226 | LUA_API int lua_isnumber(lua_State *L, int idx) |
@@ -295,6 +313,30 @@ LUA_API lua_Number lua_tonumber(lua_State *L, int idx) | |||
295 | return 0; | 313 | return 0; |
296 | } | 314 | } |
297 | 315 | ||
316 | LUALIB_API lua_Number luaL_checknumber(lua_State *L, int idx) | ||
317 | { | ||
318 | cTValue *o = index2adr(L, idx); | ||
319 | TValue tmp; | ||
320 | if (tvisnum(o)) | ||
321 | return numV(o); | ||
322 | else if (!(tvisstr(o) && lj_str_numconv(strVdata(o), &tmp))) | ||
323 | lj_err_argt(L, idx, LUA_TNUMBER); | ||
324 | return numV(&tmp); | ||
325 | } | ||
326 | |||
327 | LUALIB_API lua_Number luaL_optnumber(lua_State *L, int idx, lua_Number def) | ||
328 | { | ||
329 | cTValue *o = index2adr(L, idx); | ||
330 | TValue tmp; | ||
331 | if (tvisnum(o)) | ||
332 | return numV(o); | ||
333 | else if (tvisnil(o)) | ||
334 | return def; | ||
335 | else if (!(tvisstr(o) && lj_str_numconv(strVdata(o), &tmp))) | ||
336 | lj_err_argt(L, idx, LUA_TNUMBER); | ||
337 | return numV(&tmp); | ||
338 | } | ||
339 | |||
298 | LUA_API lua_Integer lua_tointeger(lua_State *L, int idx) | 340 | LUA_API lua_Integer lua_tointeger(lua_State *L, int idx) |
299 | { | 341 | { |
300 | cTValue *o = index2adr(L, idx); | 342 | cTValue *o = index2adr(L, idx); |
@@ -313,6 +355,44 @@ LUA_API lua_Integer lua_tointeger(lua_State *L, int idx) | |||
313 | #endif | 355 | #endif |
314 | } | 356 | } |
315 | 357 | ||
358 | LUALIB_API lua_Integer luaL_checkinteger(lua_State *L, int idx) | ||
359 | { | ||
360 | cTValue *o = index2adr(L, idx); | ||
361 | TValue tmp; | ||
362 | lua_Number n; | ||
363 | if (LJ_LIKELY(tvisnum(o))) | ||
364 | n = numV(o); | ||
365 | else if (tvisstr(o) && lj_str_numconv(strVdata(o), &tmp)) | ||
366 | n = numV(&tmp); | ||
367 | else | ||
368 | lj_err_argt(L, idx, LUA_TNUMBER); | ||
369 | #if LJ_64 | ||
370 | return (lua_Integer)n; | ||
371 | #else | ||
372 | return lj_num2int(n); | ||
373 | #endif | ||
374 | } | ||
375 | |||
376 | LUALIB_API lua_Integer luaL_optinteger(lua_State *L, int idx, lua_Integer def) | ||
377 | { | ||
378 | cTValue *o = index2adr(L, idx); | ||
379 | TValue tmp; | ||
380 | lua_Number n; | ||
381 | if (LJ_LIKELY(tvisnum(o))) | ||
382 | n = numV(o); | ||
383 | else if (tvisnil(o)) | ||
384 | return def; | ||
385 | else if (tvisstr(o) && lj_str_numconv(strVdata(o), &tmp)) | ||
386 | n = numV(&tmp); | ||
387 | else | ||
388 | lj_err_argt(L, idx, LUA_TNUMBER); | ||
389 | #if LJ_64 | ||
390 | return (lua_Integer)n; | ||
391 | #else | ||
392 | return lj_num2int(n); | ||
393 | #endif | ||
394 | } | ||
395 | |||
316 | LUA_API int lua_toboolean(lua_State *L, int idx) | 396 | LUA_API int lua_toboolean(lua_State *L, int idx) |
317 | { | 397 | { |
318 | cTValue *o = index2adr(L, idx); | 398 | cTValue *o = index2adr(L, idx); |
@@ -337,6 +417,57 @@ LUA_API const char *lua_tolstring(lua_State *L, int idx, size_t *len) | |||
337 | return strdata(s); | 417 | return strdata(s); |
338 | } | 418 | } |
339 | 419 | ||
420 | LUALIB_API const char *luaL_checklstring(lua_State *L, int idx, size_t *len) | ||
421 | { | ||
422 | TValue *o = index2adr(L, idx); | ||
423 | GCstr *s; | ||
424 | if (LJ_LIKELY(tvisstr(o))) { | ||
425 | s = strV(o); | ||
426 | } else if (tvisnum(o)) { | ||
427 | lj_gc_check(L); | ||
428 | o = index2adr(L, idx); /* GC may move the stack. */ | ||
429 | s = lj_str_fromnum(L, &o->n); | ||
430 | } else { | ||
431 | lj_err_argt(L, idx, LUA_TSTRING); | ||
432 | } | ||
433 | if (len != NULL) *len = s->len; | ||
434 | return strdata(s); | ||
435 | } | ||
436 | |||
437 | LUALIB_API const char *luaL_optlstring(lua_State *L, int idx, | ||
438 | const char *def, size_t *len) | ||
439 | { | ||
440 | TValue *o = index2adr(L, idx); | ||
441 | GCstr *s; | ||
442 | if (LJ_LIKELY(tvisstr(o))) { | ||
443 | s = strV(o); | ||
444 | } else if (tvisnil(o)) { | ||
445 | if (len != NULL) *len = def ? strlen(def) : 0; | ||
446 | return def; | ||
447 | } else if (tvisnum(o)) { | ||
448 | lj_gc_check(L); | ||
449 | o = index2adr(L, idx); /* GC may move the stack. */ | ||
450 | s = lj_str_fromnum(L, &o->n); | ||
451 | } else { | ||
452 | lj_err_argt(L, idx, LUA_TSTRING); | ||
453 | } | ||
454 | if (len != NULL) *len = s->len; | ||
455 | return strdata(s); | ||
456 | } | ||
457 | |||
458 | LUALIB_API int luaL_checkoption(lua_State *L, int idx, const char *def, | ||
459 | const char *const lst[]) | ||
460 | { | ||
461 | ptrdiff_t i; | ||
462 | const char *s = lua_tolstring(L, idx, NULL); | ||
463 | if (s == NULL && (s = def) == NULL) | ||
464 | lj_err_argt(L, idx, LUA_TSTRING); | ||
465 | for (i = 0; lst[i]; i++) | ||
466 | if (strcmp(lst[i], s) == 0) | ||
467 | return (int)i; | ||
468 | lj_err_argv(L, idx, LJ_ERR_INVOPTM, s); | ||
469 | } | ||
470 | |||
340 | LUA_API size_t lua_objlen(lua_State *L, int idx) | 471 | LUA_API size_t lua_objlen(lua_State *L, int idx) |
341 | { | 472 | { |
342 | TValue *o = index2adr(L, idx); | 473 | TValue *o = index2adr(L, idx); |
@@ -355,7 +486,8 @@ LUA_API size_t lua_objlen(lua_State *L, int idx) | |||
355 | LUA_API lua_CFunction lua_tocfunction(lua_State *L, int idx) | 486 | LUA_API lua_CFunction lua_tocfunction(lua_State *L, int idx) |
356 | { | 487 | { |
357 | cTValue *o = index2adr(L, idx); | 488 | cTValue *o = index2adr(L, idx); |
358 | return funcV(o)->c.gate == lj_gate_c ? funcV(o)->c.f : NULL; | 489 | ASMFunction gate = funcV(o)->c.gate; |
490 | return (gate == lj_gate_c || gate == lj_gate_cwrap) ? funcV(o)->c.f : NULL; | ||
359 | } | 491 | } |
360 | 492 | ||
361 | LUA_API void *lua_touserdata(lua_State *L, int idx) | 493 | LUA_API void *lua_touserdata(lua_State *L, int idx) |