static int Solver_addDIMACSSeqence(struct lua_State *state) { LGL* lgl = get_lgl(state); int n = lua_gettop (state); int success; for (int i = 2; i <= n; i++) { int type = lua_type (state, i); if (type == LUA_TTABLE) { // Iterate over the entries of the table lua_len(state, i); lua_Integer len = lua_tointeger(state, -1); lua_pop(state, 1); for (int j = 1; j <= len; j++) { lua_geti(state, i, j); lua_Integer c = lua_tointegerx (state, -1, &success); if (!success) { lua_pushliteral(state, "Illegal argument in DIMACS sequence"); lua_error(state); } lgladd (lgl, c); lua_pop(state, 1); } } else { lua_Integer c = lua_tointegerx (state, i, &success); if (!success) { lua_pushliteral(state, "Illegal argument in DIMACS sequence"); lua_error(state); } lgladd (lgl, c); } } return 0; }
void aiscript::calcCall_threaded(GameTable* gameStat) { CodeConv::tostringstream o; o << _T("AIの副露判定に入ります。プレイヤー [") << (int)gameStat->CurrentPlayer.Passive << _T("]"); info(o.str().c_str()); gameStat->statOfPassive().DeclarationFlag.Chi = chiiNone; // リセット gameStat->statOfPassive().DeclarationFlag.Pon = gameStat->statOfPassive().DeclarationFlag.Kan = gameStat->statOfPassive().DeclarationFlag.Ron = false; if (callFunc(gameStat, gameStat->CurrentPlayer.Passive, fncname_call[gameStat->KangFlag.chankanFlag], (gameStat->KangFlag.chankanFlag == 0))) { return; } else { /* 実行完了 */ int flag = 0; MeldCallID meldtype = (MeldCallID)lua_tointegerx(status[gameStat->CurrentPlayer.Passive].state, -2, &flag); if (!flag) { warn(_T("1番目の返り値が数値ではありません。無視します。")); } else { switch (meldtype) { case meldNone: break; case meldRon: gameStat->statOfPassive().DeclarationFlag.Ron = true; break; case meldKan: gameStat->statOfPassive().DeclarationFlag.Kan = true; break; case meldPon: gameStat->statOfPassive().DeclarationFlag.Pon = true; break; case meldChiiLower: gameStat->statOfPassive().DeclarationFlag.Chi = chiiLower; break; case meldChiiMiddle: gameStat->statOfPassive().DeclarationFlag.Chi = chiiMiddle; break; case meldChiiUpper: gameStat->statOfPassive().DeclarationFlag.Chi = chiiUpper; break; default: warn(_T("1番目の返り値が正しくありません。無視します。")); break; } } lua_pop(status[gameStat->CurrentPlayer.Passive].state, 1); return; } }
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"); }
LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int narg) { int isnum; lua_Integer d = lua_tointegerx(L, narg, &isnum); if (!isnum) tag_error(L, narg, LUA_TNUMBER); return d; }
static int LuaWrapper_SetShaderParameter(lua_State *L) { int num_param = lua_gettop(L); if (num_param != 3) { printf("SetShaderParameter : Invalid input parameters.\n"); return 0; } size_t str_len; int isNum; ShaderHandle hShader = (ShaderHandle)lua_tointegerx(L, 1, &isNum); const char* paramName = lua_tolstring(L, 2, &str_len); if (lua_istable(L, 3)) { float values[4]; int elemCnt = GetFloatArrayFromTable(L, 3, (float*)values, 4); KRT_SetShaderParameter(hShader, paramName, (void*)values, sizeof(float)*elemCnt); } else if (lua_isnumber(L, 3)) { float paramValue = (float)lua_tonumber(L, 3); KRT_SetShaderParameter(hShader, paramName, (void*)¶mValue, sizeof(float)); } else if (lua_isstring(L, 3)) { const char* paramValue = lua_tolstring(L, 3, &str_len); KRT_SetShaderParameter(hShader, paramName, (void*)paramValue, (int)strlen(paramValue)); } return 0; }
ptrdiff_t luaL_checkinteger (LuaThread *L, int narg) { THREAD_CHECK(L); int isnum; ptrdiff_t d = lua_tointegerx(L, narg, &isnum); if (!isnum) tag_error(L, narg, LUA_TNUMBER); return d; }
LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int arg) { int isnum; lua_Integer d = lua_tointegerx(L, arg, &isnum); if (!isnum) { interror(L, arg); } return d; }
static optional<T> get( lua_State* L, int index, Handler&& handler) { int isnum = 0; lua_Integer value = lua_tointegerx(L, index, &isnum); if (isnum == 0) { handler(L, index, type::number, type_of(L, index)); return nullopt; } return static_cast<T>(value); }
static int math_floor (lua_State *L) { int valid; lua_Integer n = lua_tointegerx(L, 1, &valid); if (valid) lua_pushinteger(L, n); /* floor computed by Lua */ else lua_pushnumber(L, l_mathop(floor)(luaL_checknumber(L, 1))); return 1; }
static int test_tointeger (lua_State *L) { int isnum = 0; lua_Integer n = lua_tointegerx(L, 1, &isnum); if (!isnum) lua_pushnil(L); else lua_pushinteger(L, n); return 1; }
lua_Integer luaX_checkinteger (lua_State *L, int narg, const char *argname) { int isnum = false; lua_Integer d = lua_tointegerx(L, narg, &isnum); if (!isnum) { tag_error(L, narg, argname, LUA_TNUMBER); } return d; }
LUALIB_API int luaL_len (lua_State *L, int idx) { int l; int isnum; lua_len(L, idx); l = (int)lua_tointegerx(L, -1, &isnum); if (!isnum) luaL_error(L, "object length is not a number"); lua_pop(L, 1); /* remove object */ return l; }
int luaL_len (lua_State *L, int i) { int res = 0, isnum = 0; luaL_checkstack(L, 1, "not enough stack slots"); lua_len(L, i); res = (int)lua_tointegerx(L, -1, &isnum); lua_pop(L, 1); if (!isnum) luaL_error(L, "object length is not a number"); return res; }
/* ** Get the .n field and make sure that it is a valid length. ** Return -1 if not. */ static lua_Integer get_n (lua_State *L, int n) { lua_Integer len = 0; int valid = 0; lua_getfield(L, n, "n"); len = lua_tointegerx(L, -1, &valid); lua_pop(L, 1); if (!valid || len < 0) len = -1; return len; }
long long LuaState::ToInteger(int index, bool* succeeded) const { int success; long long result = lua_tointegerx(m_state, index, &success); if (succeeded) *succeeded = (success != 0); return result; }
static int math_toint(lua_State *L) { int valid; lua_Integer n = lua_tointegerx(L, 1, &valid); if (valid) lua_pushinteger(L, n); else { luaL_checkany(L, 1); lua_pushnil(L); /* value is not convertible to integer */ } return 1; }
int ringbuffer_luawrite(jack_ringbuffer_t *rbuf, lua_State *L, int arg) /* bool, errmsg = write(..., tag, data) * expects * tag (integer) at index 'arg' of the stack, and * data (string) at index 'arg+1' (optional) * * if there is not enough space available, it returns 'false, "no space"'; * data may be an empty string ("") or nil, in which case it defaults * to the empty string (i.e., the message has only the header). */ { jack_ringbuffer_data_t vec[2]; hdr_t hdr; int isnum; size_t space, cnt; size_t len; const char *data; hdr.tag = (uint32_t)lua_tointegerx(L, arg, &isnum); if(!isnum) luaL_error(L, "invalid tag"); data = luaL_optlstring(L, arg + 1, NULL, &len); if(!data) hdr.len = 0; else hdr.len = len; /*@@*/ space = jack_ringbuffer_write_space(rbuf); if((sizeof(hdr) + hdr.len) > space) { lua_pushboolean(L, 0); return 1; } /* write header first (this automatically advances) */ cnt = jack_ringbuffer_write(rbuf, (const char *)&hdr, sizeof(hdr)); if(cnt != sizeof(hdr)) return luaL_error(L, UNEXPECTED_ERROR); if(hdr.len) { /* write data */ jack_ringbuffer_get_write_vector(rbuf, vec); if((vec[0].len+vec[1].len) < hdr.len) return luaL_error(L, UNEXPECTED_ERROR); if(vec[0].len >= hdr.len) memcpy(vec[0].buf, data, hdr.len); else { memcpy(vec[0].buf, data, vec[0].len); memcpy(vec[1].buf, data + vec[0].len, hdr.len - vec[0].len); } jack_ringbuffer_write_advance(rbuf, hdr.len); } lua_pushboolean(L, 1); return 1; }
COMPAT53_API lua_Integer luaL_len (lua_State *L, int i) { lua_Integer res = 0; int isnum = 0; luaL_checkstack(L, 1, "not enough stack slots"); lua_len(L, i); res = lua_tointegerx(L, -1, &isnum); lua_pop(L, 1); if (!isnum) luaL_error(L, "object length is not an integer"); return res; }
static int math_ifloor (lua_State *L) { int valid; lua_Integer n = lua_tointegerx(L, 1, &valid); if (valid) lua_pushinteger(L, n); /* floor computed by Lua */ else { luaL_checktype(L, 1, LUA_TNUMBER); /* argument must be a number */ lua_pushnil(L); /* number is not convertible to integer */ } return 1; }
int luaL_len (LuaThread *L, int idx) { THREAD_CHECK(L); int l; int isnum; lua_len(L, idx); l = (int)lua_tointegerx(L, -1, &isnum); if (!isnum) luaL_error(L, "object length is not a number"); L->stack_.pop(); /* remove object */ return l; }
static int getfield (lua_State *L, const char *key, int d) { int res, isnum; lua_getfield(L, -1, key); res = (int)lua_tointegerx(L, -1, &isnum); if (!isnum) { if (d < 0) return luaL_error(L, "field " LUA_QS " missing in date table", key); res = d; } lua_pop(L, 1); return res; }
LUA_INLINE int luaL_len(lua_State* L, int i) { luaL_checkstack(L, 1, "not enough stack slots"); lua_len(L, i); int is_num = 0; int res = int(lua_tointegerx(L, -1, &is_num)); lua_pop(L, 1); if (!is_num) { luaL_error(L, "object length is not a number"); } return res; }
void aiscript::calcDiscard_threaded(DiscardTileNum& answer, const GameTable* gameStat) { CodeConv::tostringstream o; o << _T("AIの打牌処理に入ります。プレイヤー [") << (int)gameStat->CurrentPlayer.Active << _T("]"); info(o.str().c_str()); if (callFunc(gameStat, gameStat->CurrentPlayer.Active, fncname_discard, true)) { answer = DiscardThrough; return; } else { /* 実行完了 */ int flag; answer.type = (DiscardTileNum::discardType)lua_tointegerx(status[gameStat->CurrentPlayer.Active].state, -2, &flag); if (!flag) { warn(_T("1番目の返り値が数値ではありません。通常の打牌とみなします。")); answer.type = DiscardTileNum::Normal; // fallback } else if ((answer.type < DiscardTileNum::Normal) || (answer.type > DiscardTileNum::Disconnect)) { warn(_T("1番目の返り値が正しくありません。通常の打牌とみなします。")); answer.type = DiscardTileNum::Normal; // fallback } if ((answer.type == DiscardTileNum::Agari) || (answer.type == DiscardTileNum::Kyuushu) || (answer.type == DiscardTileNum::Disconnect)) { // 番号指定が不要な場合 answer.id = NumOfTilesInHand - 1; // 2番めの返り値は無視 } else { int i = lua_tointegerx(status[gameStat->CurrentPlayer.Active].state, -1, &flag); if (!flag) { warn(_T("2番目の返り値が数値ではありません。ツモ切りとみなします。")); answer.id = NumOfTilesInHand - 1; // fallback } else if ((i >= 1)&&(i <= NumOfTilesInHand)) { answer.id = i - 1; // オリジンを1にする仕様…… } else if ((i <= -1)&&(i >= -((int)NumOfTilesInHand))) { // マイナスを指定した場合の処理 answer.id = NumOfTilesInHand + i; } else { warn(_T("2番目の返り値が範囲外です。ツモ切りとみなします。")); answer.id = NumOfTilesInHand - 1; // fallback } } lua_pop(status[gameStat->CurrentPlayer.Active].state, 2); return; } }
static int getfield (LuaThread *L, const char *key, int d) { THREAD_CHECK(L); int res, isnum; lua_getfield(L, -1, key); res = (int)lua_tointegerx(L, -1, &isnum); if (!isnum) { if (d < 0) return luaL_error(L, "field " LUA_QS " missing in date table", key); res = d; } L->stack_.pop(); return res; }
static int LuaWrapper_SetNodeSurfaceShader(lua_State *L) { int num_param = lua_gettop(L); if (num_param != 2) { printf("SetNodeSurfaceShader : Invalid input parameters.\n"); return 0; } size_t str_len; const char* nodeName = lua_tolstring(L, 1, &str_len); int isNum; ShaderHandle hShader = (ShaderHandle)lua_tointegerx(L, 2, &isNum); KRT_SetNodeSurfaceShader(nodeName, hShader); return 0; }
static int getfield (lua_State *L, const char *key, int d, int delta) { int isnum; int t = lua_getfield(L, -1, key); /* get field and its type */ lua_Integer res = lua_tointegerx(L, -1, &isnum); if (!isnum) { /* field is not an integer? */ if (t != LUA_TNIL) /* some other value? */ return luaL_error(L, "field '%s' is not an integer", key); else if (d < 0) /* absent field; no default? */ return luaL_error(L, "field '%s' missing in date table", key); res = d; } else { if (!(-L_MAXDATEFIELD <= res && res <= L_MAXDATEFIELD)) return luaL_error(L, "field '%s' is out-of-bound", key); res -= delta; } lua_pop(L, 1); return (int)res; }
bool read_int(int &i, std::string const &str) { if (luaL_dostring(state_, ("_read_value_ = " + str).c_str())) { RAIN_ERROR("Read int <" + str + "> occurs error: " + lua_tostring(state_, -1)); lua_pop(state_, 1); return false; } int cvt = 0; lua_getglobal(state_, "_read_value_"); i = (int)lua_tointegerx(state_, -1, &cvt); if (!cvt) { RAIN_ERROR("Field <" + str + "> is not an int."); lua_pop(state_, 1); return false; } lua_pop(state_, 1); return true; }
static int luaC_array__call(lua_State *L) { Array *A = lunum_checkarray1(L, 1); /* slicing done here to split concerns between indexing and slicing */ if (lua_type(L, 2) == LUA_TTABLE || lua_type(L, 2) == LUA_TSTRING) { /* make slice */ lua_getglobal(L, "lunum"); lua_getfield(L, -1, "__build_slice"); lua_insert(L, 1); lua_settop(L, 3); lua_call(L, 2, 1); return 1; } /* index */ const int nind = lua_gettop(L) - 1; if (nind != A->ndims) { return luaL_error(L, "wrong number of indices (%d) for array of dimension %d", nind, A->ndims); return 0; } int isnum; size_t m = 0; for (int d=0; d < nind; ++d) { const size_t i = lua_tointegerx(L, d+2, &isnum); if (i >= A->shape[d]) { return luaL_error(L, "array indexed out of bounds (%d) on dimension %d of size %d", i, d, A->shape[d]); } else if (!isnum) { return luaL_error(L, "non-integer index encountered"); } m = m * A->shape[d] + i; } _push_value(L, A->dtype, (char*)A->data + m*array_sizeof(A->dtype)); return 1; }
static ERL_NIF_TERM gencall(ErlNifEnv *env, lua_State *L, const ERL_NIF_TERM arg_func, const ERL_NIF_TERM arg_fmt, const ERL_NIF_TERM arg_list) { char buff_str[STACK_STRING_BUFF]; char buff_fmt[STACK_STRING_BUFF]; char buff_fun[STACK_STRING_BUFF/2]; unsigned input_len=0; unsigned output_len=0; if(enif_get_string(env, arg_func, buff_fun, STACK_STRING_BUFF/2, ERL_NIF_LATIN1)<=0){ return enif_make_badarg(env); } if(enif_get_string(env, arg_fmt, buff_fmt, STACK_STRING_BUFF, ERL_NIF_LATIN1)<=0){ return enif_make_badarg(env); } if(!enif_is_list(env, arg_list)){ return enif_make_badarg(env); } input_len = strchr(buff_fmt, ':') - buff_fmt; output_len = strlen(buff_fmt) - input_len -1; // printf("input args %d output args %d fun %s\n", input_len, output_len, buff_fun); ERL_NIF_TERM head,tail,list; list=arg_list; int i=0, status = 0, ret; ERL_NIF_TERM return_list = enif_make_list(env, 0); lua_getglobal(L, buff_fun); const char *error; while(buff_fmt[i]!='\0') { // printf("i:%d %c\n", i, buff_fmt[i]); if(status==0 && buff_fmt[i]!=':') { ret = enif_get_list_cell(env, list, &head, &tail); if(!ret) { error = FMT_AND_LIST_NO_MATCH; goto error; } list=tail; } switch(buff_fmt[i]) { case ':' : status=1; if(lua_pcall(L, input_len, output_len,0) != LUA_OK) { error = lua_tostring(L, -1); lua_pop(L,1); return enif_make_tuple2(env, atom_error, enif_make_string(env, error, ERL_NIF_LATIN1)); } //output_len = - 1; break; case 'i': if( status == 0) { int input_int; ret = enif_get_int(env, head, &input_int); // printf("input %d\n", input_int); if(!ret) { error = FMT_AND_LIST_NO_MATCH; goto error; } lua_pushinteger(L, input_int); } else if ( status==1 ){ int isnum; int n = lua_tointegerx(L, -1, &isnum); if(!isnum){ error = FMT_AND_LIST_NO_MATCH; goto error; } // printf("output %d %d\n", output_len, n); return_list = enif_make_list_cell(env, enif_make_int(env, n), return_list); lua_pop(L,1); output_len--; } break; case 's': if( status == 0) { ret = enif_get_string(env, head, buff_str, STACK_STRING_BUFF, ERL_NIF_LATIN1); if(ret<=0) { error = FMT_AND_LIST_NO_MATCH; goto error; } lua_pushstring(L, buff_str); } else if ( status==1 ) { const char *s = lua_tostring(L, -1); if (s==NULL) { error = FMT_AND_RET_NO_MATCH; goto error; } // printf("output %d %s\n", output_len, s); return_list = enif_make_list_cell(env, enif_make_string(env, s, ERL_NIF_LATIN1), return_list); lua_pop(L,1); output_len--; } break; /* case 'd': */ /* break; */ /* case 'b': */ /* break; */ default: error = FMT_WRONG; goto error; break; } i++; } return enif_make_tuple2(env, atom_ok, return_list); error: // printf("in error \n"); // @fix clean the heap var. // before call, pop the call if(status ==0 ) { lua_pop(L, 1); } else if(output_len>0) { lua_pop(L, output_len); } return make_error_tuple(env, error); }
inline lua_Integer toX<lua_Integer>(lua_State* L, int index, int* is) { LUNA_INDEX_ASSERT(L, index); return static_cast<lua_Integer>(lua_tointegerx(L, index, is)); }