static int _llfunc_vec2_set(lua_State *L) { vec2* v = (vec2*)userdata_get_or_die(L, 1); int isnum = 0; float f = (float)lua_tonumberx(L, 2, &isnum); if (isnum) v->x = f; f = (float)lua_tonumberx(L, 3, &isnum); if (isnum) v->y = f; return 0; }
static int /*ALUA*/ _analyze_generated_pair_list( lua_State *state ) { struct feature_pair fpair; int isnum, lua_status; lua_getglobal( _L, opt_coroutine ); assert( ! lua_isnil( _L, -1 ) /* because it was checked early */ ); do { lua_pushnumber( _L, _matrix.rows ); lua_status = lua_resume( _L, NULL, 1 ); if( lua_status == LUA_YIELD ) { fpair.r.offset = lua_tonumberx( _L, -1, &isnum ); fpair.l.offset = lua_tonumberx( _L, -2, &isnum ); lua_pop( _L, 2 ); if( fetch_by_offset( &_matrix, &fpair ) ) { warnx( "one or both of %s-generated row indices (%d,%d) not in [0,%d)\n", opt_coroutine, fpair.l.offset, fpair.r.offset, _matrix.rows ); if( opt_warnings_are_fatal ) break; else continue; // no reason we -can't- continue } else _analyze( &fpair ); } else if( lua_status == LUA_OK ) break; else { // some sort of error occurred. fputs( lua_tostring( _L, -1 ), stderr ); } if( _sigint_received ) { time_t now = time(NULL); fprintf( stderr, "analysis loop interrupted @ %s", ctime(&now) ); break; } } while( lua_status == LUA_YIELD ); return 0; }
bool luaW_tolocation(lua_State *L, int index, map_location& loc) { if (!lua_checkstack(L, LUA_MINSTACK)) { return false; } if (lua_isnoneornil(L, index)) { // Need this special check because luaW_tovconfig returns true in this case return false; } vconfig dummy_vcfg = vconfig::unconstructed_vconfig(); index = lua_absindex(L, index); if (lua_istable(L, index) || luaW_tounit(L, index) || luaW_tovconfig(L, index, dummy_vcfg)) { map_location result; int x_was_num = 0, y_was_num = 0; lua_getfield(L, index, "x"); result.set_wml_x(lua_tonumberx(L, -1, &x_was_num)); lua_getfield(L, index, "y"); result.set_wml_y(lua_tonumberx(L, -1, &y_was_num)); lua_pop(L, 2); if (!x_was_num || !y_was_num) { // If we get here and it was userdata, checking numeric indices won't help // (It won't help if it was a config either, but there's no easy way to check that.) if (lua_isuserdata(L, index)) { return false; } lua_rawgeti(L, index, 1); result.set_wml_x(lua_tonumberx(L, -1, &x_was_num)); lua_rawgeti(L, index, 2); result.set_wml_y(lua_tonumberx(L, -1, &y_was_num)); lua_pop(L, 2); } if (x_was_num && y_was_num) { loc = result; return true; } } else if (lua_isnumber(L, index) && lua_isnumber(L, index + 1)) { // If it's a number, then we consume two elements on the stack // Since we have no way of notifying the caller that we have // done this, we remove the first number from the stack. loc.set_wml_x(lua_tonumber(L, index)); lua_remove(L, index); loc.set_wml_y(lua_tonumber(L, index)); return true; } return false; }
LUA_INLINE lua_Unsigned lua_tounsignedx(lua_State* L, int i, int* is_num) { lua_Unsigned result; lua_Number n = lua_tonumberx(L, i, is_num); lua_number2unsigned(result, n); return result; }
LUALIB_API lua_Number luaL_checknumber (lua_State *L, int narg) { int isnum; lua_Number d = lua_tonumberx(L, narg, &isnum); if (!isnum) tag_error(L, narg, LUA_TNUMBER); return d; }
uint32_t coerce_to_uint32_t(lua_State *L, int arg) { { // userdata const uint32_t * ud = static_cast<uint32_t *>(luaL_testudata(L, arg, "uint32_t")); if (ud != nullptr) { return *ud; } } { // integer // if this assert fails, you will need to add an upper bounds // check that ensures the value isn't greater then UINT32_MAX static_assert(sizeof(lua_Number) == sizeof(uint32_t), "32 bit integers are only supported"); int success; const lua_Integer v = lua_tointegerx(L, arg, &success); if (success && v >= 0) { return static_cast<uint32_t>(v); } } { // float int success; const lua_Number v = lua_tonumberx(L, arg, &success); if (success && v >= 0 && v <= UINT32_MAX) { return static_cast<uint32_t>(v); } } // failure return luaL_argerror(L, arg, "Unable to coerce to uint32_t"); }
double func(lua_State *L, double x, double y) { int isnum; double z; lua_getglobal(L, "f"); // function to be called lua_pushnumber(L, x); // push 1st arg lua_pushnumber(L, y); // push 2nd arg // do the call if (lua_pcall(L, 2, 1, 0) != LUA_OK) { printf("run function faled"); return -1; } // get result z = lua_tonumberx(L, -1, &isnum); if (!isnum) { printf("function f nust return a number"); return -1; } lua_pop(L, 1); // pop returned value return z; }
static int test_tonumber (lua_State *L) { int isnum = 0; lua_Number n = lua_tonumberx(L, 1, &isnum); if (!isnum) lua_pushnil(L); else lua_pushnumber(L, n); return 1; }
static optional<T> get( lua_State* L, int index, Handler&& handler) { int isnum = 0; lua_Number value = lua_tonumberx(L, index, &isnum); if (isnum == 0) { handler(L, index, type::number, type_of(L, index)); return nullopt; } return static_cast<T>(value); }
lua_Number luaX_checknumber (lua_State *L, int narg, const char *argname) { int isnum = false; lua_Number d = lua_tonumberx(L, narg, &isnum); if (!isnum) { tag_error(L, narg, argname, LUA_TNUMBER); } return d; }
double LuaState::ToNumber(int index, bool* succeeded) const { int success; double result = lua_tonumberx(m_state, index, &success); if (succeeded) *succeeded = (success != 0); return result; }
static int _llfunc_mat4_set(lua_State *L) { mat4* m = (mat4*)userdata_get_or_die(L, 1); float* p = m->p; for (int i = 0; i < 16; i++) { int isnum = 0; float f = (float)lua_tonumberx(L, 2 + i, &isnum); if (isnum) p[i] = f; } return 0; }
Type ReadFPValueFromLua( lua_State *L, int Index ) { CheckType( L, Index, LUA_TNUMBER ); int isnum; auto Val = lua_tonumberx( L, Index, &isnum ); if( !isnum ) { auto Str = lua_tostring( L, Index ); SCRIPT_PARSING_ERROR( L, "Failed to convert parameter ", Str, " to floating point" ); } return static_cast<Type>(Val); }
// Convert a Lua value into a JsonNode structure. static JsonNode *LuaValueToJson(lua_State *L) { assert(L); JsonNode *json = NULL; int top = lua_gettop(L); int type = lua_type(L, top); switch (type) { case LUA_TTABLE: // Check if the current value is a reference cycle if (IsRefCycle(L, JSON_REF_TABLE_IDX, top)) { luaL_error(L, "reference cycle detected in table"); return NULL; } // Add a reference to our tracking table AddTableRef(L, JSON_REF_TABLE_IDX, top); // Convert the table to JSON json = LuaTableToJsonPrivate(L, top); // Remove the table reference again after we unwind RemoveTableRef(L, JSON_REF_TABLE_IDX, top); break; case LUA_TSTRING: json = json_mkstring(luaL_tolstring(L, top, NULL)); lua_pop(L, 1); break; case LUA_TNUMBER: { int isnum; lua_Number num = lua_tonumberx(L, top, &isnum); if (isnum) { json = json_mknumber(num); } break; } case LUA_TBOOLEAN: json = json_mkbool((bool)lua_toboolean(L, top)); break; case LUA_TNIL: json = json_mknull(); break; default: break; } return json; }
Type ReadIntValueFromLua( lua_State *L, int Index ) { CheckType( L, Index, LUA_TNUMBER ); int isnum; auto Val = lua_tonumberx( L, Index, &isnum ); if( !isnum ) { auto Str = lua_tostring( L, Index ); SCRIPT_PARSING_ERROR( L, "Failed to convert parameter ", Str, " to int" ); } if( static_cast<Type>(Val) != Val ) { SCRIPT_PARSING_ERROR( L, "Parameter value (", Val, ") is not integer. Truncating to int" ); } return static_cast<Type>(Val); }
bool read_double(double &d, std::string const &str) { if (luaL_dostring(state_, ("_read_value_ = " + str).c_str())) { RAIN_ERROR("Read double <" + str + "> occurs error: " + lua_tostring(state_, -1)); lua_pop(state_, 1); return false; } int cvt = 0; lua_getglobal(state_, "_read_value_"); d = lua_tonumberx(state_, -1, &cvt); if (!cvt) { RAIN_ERROR("Field <" + str + "> is not a double."); lua_pop(state_, 1); return false; } lua_pop(state_, 1); return true; }
static int luaCV_tonumber (lua_State *L) { if(luaL_callmeta(L, 1, "toNumber")) { return 1; } else if (lua_isnoneornil(L, 2)) { /* standard conversion */ int isnum; lua_Number n = lua_tonumberx(L, 1, &isnum); if (isnum) { lua_pushnumber(L, n); return 1; } /* else not a number; must be something */ luaL_checkany(L, 1); } else { size_t l; const char *s = luaL_checklstring(L, 1, &l); const char *e = s + l; /* end point for 's' */ int base = luaL_checkint(L, 2); int neg = 0; luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range"); s += strspn(s, SPACECHARS); /* skip initial spaces */ if (*s == '-') { s++; neg = 1; } /* handle signal */ else if (*s == '+') s++; if (isalnum((unsigned char)*s)) { lua_Number n = 0; do { int digit = (isdigit((unsigned char)*s)) ? *s - '0' : toupper((unsigned char)*s) - 'A' + 10; if (digit >= base) break; /* invalid numeral; force a fail */ n = n * (lua_Number)base + (lua_Number)digit; s++; } while (isalnum((unsigned char)*s)); s += strspn(s, SPACECHARS); /* skip trailing spaces */ if (s == e) { /* no invalid trailing characters? */ lua_pushnumber(L, (neg) ? -n : n); return 1; } /* else not a number */ } /* else not a number */ } lua_pushnil(L); /* not a number */ return 1; }
double f (lua_State *L, double x) { int isnum; double z; lua_getglobal(L, "f"); lua_pushnumber(L, x); if(lua_pcall(L, 1, 1, 0) != LUA_OK) { printf("error running function 'f': %s", lua_tostring(L, -1)); } z = lua_tonumberx(L, -1, &isnum); if(!isnum) { printf("function 'f' must return a number"); } lua_pop(L, 1); return z; }
//============================================================================== Error LuaBinder::checkNumberInternal( lua_State* l, I32 stackIdx, lua_Number& number) { Error err = ErrorCode::NONE; lua_Number lnum; int isnum; lnum = lua_tonumberx(l, stackIdx, &isnum); if(isnum) { number = lnum; } else { err = ErrorCode::USER_DATA; lua_pushfstring( l, "Number expected. Got %s", luaL_typename(l, stackIdx)); } return err; }
void call_va(lua_State * L, const char *func, const char *sig, ...) { va_list vl; int narg, nres; /* number of arguments and results */ va_start(vl, sig); lua_getglobal(L, func); /* push function */ for (narg = 0; *sig; narg++) { /* repeat for each argument */ /* check stack space */ luaL_checkstack(L, 1, "too many arguments"); switch (*sig++) { case 'd': /* double argument */ lua_pushnumber(L, va_arg(vl, double)); break; case 'i': /* int argument */ lua_pushinteger(L, va_arg(vl, int)); break; case 's': /* string argument */ lua_pushstring(L, va_arg(vl, char *)); break; case '>': /* end of arguments */ goto endargs; default: error(L, "invalid option (%c)", *(sig - 1)); } } endargs: nres = strlen(sig); /* number of expected results */ if (lua_pcall(L, narg, nres, 0) != 0) /* do the call */ error(L, "error calling '%s': %s", func, lua_tostring(L, -1)); nres = -nres; /* stack index of first result */ while (*sig) { /* repeat for each result */ switch (*sig++) { case 'd':{ /* double result */ int isnum; double n = lua_tonumberx(L, nres, &isnum); if (!isnum) error(L, "wrong result type"); *va_arg(vl, double *) = n; break; } case 'i':{ /* int result */ int isnum; int n = lua_tointegerx(L, nres, &isnum); if (!isnum) error(L, "wrong result type"); *va_arg(vl, int *) = n; break; } case 's':{ /* string result */ const char *s = lua_tostring(L, nres); if (s == NULL) error(L, "wrong result type"); *va_arg(vl, const char **) = s; break; } default: error(L, "invalid option (%c)", *(sig - 1)); } nres++; } va_end(vl); }
static int _array_number_binary_op(lua_State *L, ArrayBinaryOperation op, Bool array_first) { const Array *A = array_first ? lunum_checkarray1(L, 1) : lunum_checkarray1(L, 2); ArrayType T = A->dtype; int num_pos = array_first ? 2 : 1; union { Bool b; char c; short s; int i; long l; size_t t; float f; double d; Complex z; lua_Integer li; lua_Number ln; } num; /* to force integer conversion if possible */ int isnum; if (lua_isboolean(L, num_pos)) { num.i = lua_toboolean(L, num_pos); /* number can't have a higher type, upgrade to type T */ switch (T) { case ARRAY_TYPE_BOOL : num.b = (Bool)num.i; break; case ARRAY_TYPE_CHAR : num.c = (char)num.i; break; case ARRAY_TYPE_SHORT : num.s = (short)num.i; break; case ARRAY_TYPE_INT : num.i = (int)num.i; break; case ARRAY_TYPE_LONG : num.l = (long)num.i; break; case ARRAY_TYPE_SIZE_T : num.t = (size_t)num.i; break; case ARRAY_TYPE_FLOAT : num.f = (float)num.i; break; case ARRAY_TYPE_DOUBLE : num.d = (double)num.i; break; case ARRAY_TYPE_COMPLEX : num.z = (Complex)num.i; break; } } else if (num.li = lua_tointegerx(L, num_pos, &isnum), isnum) { /* already assigned above */ if (T >= ARRAY_TYPE_LONG) { /* A has higher type */ } else { /* number has higher type */ T = ARRAY_TYPE_LONG; } /* upgrade to type T */ switch (T) { case ARRAY_TYPE_BOOL : num.b = (Bool)num.li; break; case ARRAY_TYPE_CHAR : num.c = (char)num.li; break; case ARRAY_TYPE_SHORT : num.s = (short)num.li; break; case ARRAY_TYPE_INT : num.i = (int)num.li; break; case ARRAY_TYPE_LONG : num.l = (long)num.li; break; case ARRAY_TYPE_SIZE_T : num.t = (size_t)num.li; break; case ARRAY_TYPE_FLOAT : num.f = (float)num.li; break; case ARRAY_TYPE_DOUBLE : num.d = (double)num.li; break; case ARRAY_TYPE_COMPLEX : num.z = (Complex)num.li; break; } } else if (num.ln = lua_tonumberx(L, num_pos, &isnum), isnum) { /* already assigned above */ if (T >= ARRAY_TYPE_DOUBLE) { /* A has higher type */ } else { /* number has higher type */ T = ARRAY_TYPE_DOUBLE; } /* upgrade to type T */ switch (T) { case ARRAY_TYPE_BOOL : num.b = (Bool)num.ln; break; case ARRAY_TYPE_CHAR : num.c = (char)num.ln; break; case ARRAY_TYPE_SHORT : num.s = (short)num.ln; break; case ARRAY_TYPE_INT : num.i = (int)num.ln; break; case ARRAY_TYPE_LONG : num.l = (long)num.ln; break; case ARRAY_TYPE_SIZE_T : num.t = (size_t)num.ln; break; case ARRAY_TYPE_FLOAT : num.f = (float)num.ln; break; case ARRAY_TYPE_DOUBLE : num.d = (double)num.ln; break; case ARRAY_TYPE_COMPLEX : num.z = (Complex)num.ln; break; } } else if (lunum_hasmetatable(L, num_pos, "complex")) { /* number complex */ num.z = *((Complex*) lua_touserdata(L, num_pos)); T = ARRAY_TYPE_COMPLEX; } else { return luaL_error(L, "Invalid argument in Array binary op"); } Array C = array_new_zeros(A->size, T); array_resize_t(&C, A->shape, A->ndims); lunum_pusharray1(L, &C); array_number_binary_op(L, A, (void *)&num, &C, op, array_first); return 1; }
inline lua_Number toX<lua_Number>(lua_State* L, int index, int* is) { LUNA_INDEX_ASSERT(L, index); return static_cast<lua_Number>(lua_tonumberx(L, index, is)); }
static int run_scan_float_indeed(lua_State * L, boolean exponent) { saved_tex_scanner texstate; int ok; boolean negative = false; double d; declare_buffer; save_tex_scanner(texstate); /* we collapse as in scan_dimen */ while(1) { do { get_x_token(); } while (cur_cmd == spacer_cmd); if (cur_tok == minus_token) { negative = !negative; } else if (cur_tok != plus_token) { break; } } if (negative) { add_to_buffer('-'); } /* we accept [.,]digits */ if (cur_tok == point_token || cur_tok == comma_token) { add_to_buffer('.'); while (1) { get_x_token(); if ((cur_tok >= zero_token) && (cur_tok <= nine_token)) { add_to_buffer(cur_chr); } else if (exponent) { goto EXPONENT; } else { back_input(); goto DONE; } } } else { back_input(); while (1) { get_x_token(); if ((cur_tok >= zero_token) && (cur_tok <= nine_token)) { add_to_buffer(cur_chr); } else { if (cur_tok == point_token || cur_tok == comma_token) { add_to_buffer('.'); while (1) { get_x_token(); if ((cur_tok >= zero_token) && (cur_tok <= nine_token)) { add_to_buffer(cur_chr); } else { back_input(); break; } } } else if (exponent) { goto EXPONENT; } else { back_input(); goto DONE; } } } } EXPONENT: if ((cur_chr == 'E') || (cur_chr == 'e')) { add_to_buffer(cur_chr); get_x_token(); if ((cur_tok == minus_token) || (cur_tok == plus_token)) { add_to_buffer(cur_chr); } else if ((cur_tok >= zero_token) && (cur_tok <= nine_token)) { add_to_buffer(cur_chr); } while (1) { get_x_token(); if ((cur_tok >= zero_token) && (cur_tok <= nine_token)) { add_to_buffer(cur_chr); } else { break; } } } back_input(); DONE: push_buffer; d = lua_tonumberx(L,1,&ok); if (ok) { lua_pushnumber(L,d); } else { lua_pushnil(L); } unsave_tex_scanner(texstate); return 1; }
static inline bool get(lua_State * const L, int idx, float & x) { int isnum; x = static_cast<float>(lua_tonumberx(L, idx, &isnum)); return (isnum != 0); }
// вызываем функцию на lua // указываем тип входящих аргументов через d(oble),i(nteger),s(tring) // символ > отделяет аргументы входащих от результата //void call_va(lua_State *L, const char *func, const char *sig, ...) { //int cbsdlua_funccmd(const char *func, const char *sig, ...) { int cbsdlua_funccmd(int argc, char **argv) { //const char *func, const char *sig, ...) { char *func=argv[1]; char *sig=argv[2]; int narg, nres, npos; lua_getglobal(L,func); // out2fmt_flush("func: %s\n", func); //push args for (narg=0; *sig; narg++ ) { npos=3 + narg; // 3 -$0, func, sig luaL_checkstack(L, 1, "too many arguments"); switch (*sig++) { case 'd': /* double */ // lua_pushnumber(L, va_arg(vl,double)); break; case 'i': /* integer */ lua_pushinteger(L, atoi(argv[npos])); break; case 's': /* string */ lua_pushstring(L, argv[npos]); break; case '>': /* end of argument */ goto endargs; default: cbsdlua_error(L,"invalid option (%c)", *(sig - 1 )); } } endargs: nres=strlen(sig); if (lua_pcall(L, narg, nres, 0)!= 0) cbsdlua_error(L, "error calling '%s': %s",func, lua_tostring(L,-1)); //get result nres = -nres; while (*sig) { switch (*sig++) { case 'd': { int isnum; double n = lua_tonumberx(L, nres, &isnum); if (!isnum) cbsdlua_error(L,"wrong result type"); // *va_arg(vl, double *) = n; break; } case 'i': { int isnum; int n = lua_tointegerx(L, nres, &isnum); if (!isnum) cbsdlua_error(L,"wrong result type"); // *va_arg(vl, int *) = n; // out2fmt_flush("LUA Return for %s: %d\n", argv[npos], n); char str[100]; fmtstr(str, sizeof(str), "%d", n); setvarsafe(argv[npos],str, 0); break; } case 's': { const char *s = lua_tostring(L, nres); if (s == NULL) cbsdlua_error(L,"wrong result type"); // *va_arg(vl, const char **) = s; // out2fmt_flush("LUA Return for %s: %s\n", argv[npos], s); setvarsafe(argv[npos],s, 0); break; } default: cbsdlua_error(L,"invalid option (%c)", *(sig - 1 )); } nres++; } return 0; }
LUA_API lua_Integer lua_tointegerx(lua_State *L, int idx, int *isnum) { lua_Number n = lua_tonumberx(L, idx, isnum); return (lua_Integer)n; }
// Call any kind of Lua function, where func is the name of lua function // args is the input arguments // res is the output of lua function // the others arguments should be put according the args and res void np_lua_func(lua_State *L, const char *func, const char *args, const char *res, ...) { va_list v1; int nargs, nres; va_start(v1, res); /* push lua function */ lua_getglobal(L, func); for (nargs = 0; *args; nargs++) { luaL_checkstack(L, 1, "too many arguments"); switch (*args++) { case 'd': lua_pushnumber(L, va_arg(v1, double)); break; case 'i': lua_pushinteger(L, va_arg(v1, int)); break; case 'b': lua_pushboolean(L, va_arg(v1, int)); break; case 's': lua_pushstring(L, va_arg(v1, char*)); break; default: luaL_error(L, "invalid input option (%c)", *(args -1)); } } /* Number of expected results */ nres = strlen(res); if (lua_pcall(L, nargs, nres, 0) != 0) luaL_error(L, "error calling '%s': %s", func, lua_tostring(L, -1)); nres = -nres; /* stack index of first result */ while (*res) { switch (*res++) { case 'd': { int isnum; double n = lua_tonumberx(L, nres, &isnum); if (&isnum == NULL) luaL_error(L, "Wrong result type"); *va_arg(v1, double *) = n; break; } case 'i': { int isint; int n = lua_tointegerx(L, nres, &isint); if (&isint == NULL) luaL_error(L, "Wrong result type"); *va_arg(v1, int *) = n; break; } case 'b': { int isbool; isbool = lua_isboolean(L, nres); if (isbool == 0) luaL_error(L, "Wrong result type"); int n = lua_toboolean(L, nres); *va_arg(v1, int *) = n; break; } case 's': { const char *s = lua_tostring(L, nres); if (s == NULL) luaL_error(L, "Wrong result type"); *va_arg(v1, const char **) = s; break; } default: luaL_error(L, "invalid return option (%c)", *(res -1)); } nres++; } va_end(v1); }
LUA_API lua_Unsigned lua_tounsignedx(lua_State *L, int idx, int *isnum) { lua_Number n = lua_tonumberx(L, idx, isnum); return (lua_Unsigned)n; }