static SQInteger default_delegate_tointeger(HSQUIRRELVM v) { SQObjectPtr &o=stack_get(v,1); switch(type(o)) { case OT_STRING: { SQObjectPtr res; if(str2num(_stringval(o),res)) { v->Push(SQObjectPtr(tointeger(res))); break; } } return sq_throwerror(v, _SC("cannot convert the string")); break; case OT_INTEGER: case OT_FLOAT: v->Push(SQObjectPtr(tointeger(o))); break; case OT_BOOL: v->Push(SQObjectPtr(_integer(o)?(SQInteger)1:(SQInteger)0)); break; default: v->Push(_null_); break; } return 1; }
/* cyrussasl.setssf(conn, min_ssf, max_ssf) * * conn: the conn pointer from cyrussasl.server_new(). * min_ssf, max_ssf: set the minimum and maximum security strength factor * required for this AuthN. * * Throws Lua errors if it fails (as it should not typically fail). * Does not return any value. */ static int cyrussasl_setssf(lua_State *l) { sasl_security_properties_t secprops; int err; int min_ssf, max_ssf; struct _sasl_ctx *ctx = NULL; int numargs = lua_gettop(l); if (numargs != 3) { lua_pushstring(l, "usage: cyrussasl.setssf(conn, min_ssf, max_ssf)"); lua_error(l); return 0; } ctx = get_context(l, 1); min_ssf = tointeger(l, 2); max_ssf = tointeger(l, 3); memset(&secprops, 0L, sizeof(secprops)); secprops.min_ssf = min_ssf; secprops.max_ssf = max_ssf; err = sasl_setprop(ctx->conn, SASL_SEC_PROPS, &secprops); if ( err != SASL_OK ) { lua_pushstring(l, "setssf failed"); lua_error(l); return 0; } return 0; }
static SQInteger array_remove(HSQUIRRELVM v) { SQObject &o = stack_get(v, 1); SQObject &idx = stack_get(v, 2); if(!sq_isnumeric(idx)) return sq_throwerror(v, _SC("wrong type")); SQObjectPtr val; if(_array(o)->Get(tointeger(idx), val)) { _array(o)->Remove(tointeger(idx)); v->Push(val); return 1; } return sq_throwerror(v, _SC("idx out of range")); }
static SQInteger base_array(HSQUIRRELVM v) { SQArray *a; SQObject &size = stack_get(v,2); if(sq_gettop(v) > 2) { a = SQArray::Create(_ss(v),0); a->Resize(tointeger(size),stack_get(v,3)); } else { a = SQArray::Create(_ss(v),tointeger(size)); } v->Push(a); return 1; }
/* ** Main operation for equality of Lua values; return 't1 == t2'. ** L == NULL means raw equality (no metamethods) */ int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) { const TValue *tm; if (ttype(t1) != ttype(t2)) { /* not the same variant? */ #ifndef _KERNEL if (ttnov(t1) != ttnov(t2) || ttnov(t1) != LUA_TNUMBER) return 0; /* only numbers can be equal with different variants */ else { /* two numbers with different variants */ lua_Integer i1, i2; /* compare them as integers */ return (tointeger(t1, &i1) && tointeger(t2, &i2) && i1 == i2); } #else /* _KERNEL */ return 0; /* numbers have only the integer variant */ #endif /* _KERNEL */ } /* values have same type and same variant */ switch (ttype(t1)) { case LUA_TNIL: return 1; case LUA_TNUMINT: return (ivalue(t1) == ivalue(t2)); #ifndef _KERNEL case LUA_TNUMFLT: return luai_numeq(fltvalue(t1), fltvalue(t2)); #endif /* _KERNEL */ case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2); /* true must be 1 !! */ case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2); case LUA_TLCF: return fvalue(t1) == fvalue(t2); case LUA_TSHRSTR: return eqshrstr(tsvalue(t1), tsvalue(t2)); case LUA_TLNGSTR: return luaS_eqlngstr(tsvalue(t1), tsvalue(t2)); case LUA_TUSERDATA: { if (uvalue(t1) == uvalue(t2)) return 1; else if (L == NULL) return 0; tm = fasttm(L, uvalue(t1)->metatable, TM_EQ); if (tm == NULL) tm = fasttm(L, uvalue(t2)->metatable, TM_EQ); break; /* will try TM */ } case LUA_TTABLE: { if (hvalue(t1) == hvalue(t2)) return 1; else if (L == NULL) return 0; tm = fasttm(L, hvalue(t1)->metatable, TM_EQ); if (tm == NULL) tm = fasttm(L, hvalue(t2)->metatable, TM_EQ); break; /* will try TM */ } default: return gcvalue(t1) == gcvalue(t2); } if (tm == NULL) /* no TM? */ return 0; /* objects are different */ luaT_callTM(L, tm, t1, t2, L->top, 1); /* call TM */ return !l_isfalse(L->top); }
void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2, TValue *res) { switch (op) { case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR: case LUA_OPSHL: case LUA_OPSHR: case LUA_OPBNOT: { /* operate only on integers */ lua_Integer i1; lua_Integer i2; if (tointeger(p1, &i1) && tointeger(p2, &i2)) { setivalue(res, intarith(L, op, i1, i2)); return; } else break; /* go to the end */ } #ifndef _KERNEL case LUA_OPDIV: case LUA_OPPOW: { /* operate only on floats */ lua_Number n1; lua_Number n2; if (tonumber(p1, &n1) && tonumber(p2, &n2)) { setfltvalue(res, numarith(L, op, n1, n2)); return; } else break; /* go to the end */ } #endif default: { /* other operations */ #ifndef _KERNEL lua_Number n1; lua_Number n2; if (ttisinteger(p1) && ttisinteger(p2)) { setivalue(res, intarith(L, op, ivalue(p1), ivalue(p2))); return; } else if (tonumber(p1, &n1) && tonumber(p2, &n2)) { setfltvalue(res, numarith(L, op, n1, n2)); return; } #else /* _KERNEL */ lua_Integer i1; lua_Integer i2; if (tointeger(p1, &i1) && tointeger(p2, &i2)) { setivalue(res, intarith(L, op, i1, i2)); return; } #endif else break; /* go to the end */ } } /* could not perform raw operation; try metamethod */ lua_assert(L != NULL); /* should not fail when folding (compile time) */ luaT_trybinTM(L, p1, p2, res, cast(TMS, (op - LUA_OPADD) + TM_ADD)); }
static SQInteger number_delegate_tochar(HSQUIRRELVM v) { SQObject &o=stack_get(v,1); SQChar c = (SQChar)tointeger(o); v->Push(SQString::Create(_ss(v),(const SQChar *)&c,1)); return 1; }
SQInteger sq_objtointeger(const HSQOBJECT *o) { if(sq_isnumeric(*o)) { return tointeger(*o); } return 0; }
/* cyrussasl.setprop(conn, propnum, val) * * conn: the conn pointer from cyrussasl.server_new(). * propnum: an integer corresponding to the property to set * val: a lua string object * * Throws Lua errors if it fails (as it should not typically fail). * Does not return any value. */ static int cyrussasl_sasl_setprop(lua_State *l) { int err; int proptype; const void *proparg; struct _sasl_ctx *ctx = NULL; int numargs = lua_gettop(l); if (numargs != 3) { lua_pushstring(l, "usage: cyrussasl.setprop(conn, propnum, propval)"); lua_error(l); return 0; } ctx = get_context(l, 1); proptype = tointeger(l, 2); proparg = tolstring(l, 3, NULL); err = sasl_setprop(ctx->conn, proptype, &proparg); if ( err != SASL_OK ) { const char *ret = get_context_message(ctx); if (ret) lua_pushstring(l, ret); else lua_pushstring(l, "sasl_setprop failed"); lua_error(l); return 0; } return 0; }
l_noret luaG_tointerror (lua_State *L, const TValue *p1, const TValue *p2) { lua_Integer temp; if (!tointeger(p1, &temp)) p2 = p1; luaG_runerror(L, "attempt to convert an out of range float%s to an integer", varinfo(L, p2)); }
static SQInteger array_insert(HSQUIRRELVM v) { SQObject &o=stack_get(v,1); SQObject &idx=stack_get(v,2); SQObject &val=stack_get(v,3); if(!_array(o)->Insert(tointeger(idx),val)) return sq_throwerror(v,_SC("index out of range")); return 0; }
/* ** return false if folding can raise an error */ static int validop (int op, TValue *v1, TValue *v2) { lua_Number a, b; lua_Integer i; cast_void(a); cast_void(b); /* macro may not use its arguments */ if (luai_numinvalidop(op, (cast_void(tonumber(v1, &a)), a), (cast_void(tonumber(v2, &b)), b))) return 0; switch (op) { case LUA_OPIDIV: /* division by 0 and conversion errors */ return (tointeger(v1, &i) && tointeger(v2, &i) && i != 0); case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR: case LUA_OPSHL: case LUA_OPSHR: case LUA_OPBNOT: /* conversion errors */ return (tointeger(v1, &i) && tointeger(v2, &i)); case LUA_OPMOD: /* integer module by 0 */ return !(ttisinteger(v1) && ttisinteger(v2) && ivalue(v2) == 0); default: return 1; /* everything else is valid */ } }
LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *pisnum) { lua_Integer res; const TValue *o = index2addr(L, idx); int isnum = tointeger(o, &res); if (!isnum) res = 0; /* call to 'tointeger' may change 'n' even if it fails */ if (pisnum) *pisnum = isnum; return res; }
static SQInteger get_slice_params(HSQUIRRELVM v,SQInteger &sidx,SQInteger &eidx,SQObjectPtr &o) { SQInteger top = sq_gettop(v); sidx=0; eidx=0; o=stack_get(v,1); SQObjectPtr &start=stack_get(v,2); if(type(start)!=OT_NULL && sq_isnumeric(start)){ sidx=tointeger(start); } if(top>2){ SQObjectPtr &end=stack_get(v,3); if(sq_isnumeric(end)){ eidx=tointeger(end); } } else { eidx = sq_getsize(v,1); } return 1; }
static SQInteger array_resize(HSQUIRRELVM v) { SQObject &o = stack_get(v, 1); SQObject &nsize = stack_get(v, 2); SQObjectPtr fill; if(sq_isnumeric(nsize)) { if(sq_gettop(v) > 2) fill = stack_get(v, 3); _array(o)->Resize(tointeger(nsize),fill); return 0; } return sq_throwerror(v, _SC("size must be a number")); }
static SQInteger base_array(HSQUIRRELVM v) { SQArray *a; SQInteger nInitialSize = tointeger(stack_get(v,2)); SQInteger ret = 1; if (nInitialSize < 0) { v->Raise_Error(_SC("can't create/resize array with/to size %d"), nInitialSize); nInitialSize = 0; ret = -1; } if(sq_gettop(v) > 2) { a = SQArray::Create(_ss(v),0); a->Resize(nInitialSize,stack_get(v,3)); } else { a = SQArray::Create(_ss(v),nInitialSize); } v->Push(a); return ret; }
/* ** Error when both values are convertible to numbers, but not to integers */ l_noret luaG_tointerror (lua_State *L, const TValue *p1, const TValue *p2) { lua_Integer temp; if (!tointeger(p1, &temp)) p2 = p1; luaG_runerror(L, "number%s has no integer representation", varinfo(L, p2)); }
static int cyrussasl_getprop(lua_State *l) { struct _sasl_ctx *ctx = NULL; unsigned *maxbufsize; const char *strdata; sasl_ssf_t *ssf; int propnum; int ret; int numargs = lua_gettop(l); if (numargs != 2) { lua_pushstring(l, "usage: user = cyrussasl.get_prop(conn, property)"); lua_error(l); return 0; } ctx = get_context(l, 1); propnum = tointeger(l, 2); switch (propnum) { /* strings */ case SASL_USERNAME: case SASL_DEFUSERREALM: case SASL_SERVICE: case SASL_SERVERFQDN: case SASL_AUTHSOURCE: case SASL_MECHNAME: case SASL_PLUGERR: case SASL_IPLOCALPORT: case SASL_IPREMOTEPORT: ret = sasl_getprop(ctx->conn, propnum, (const void **)&strdata); // return strdata lua_pushstring(l, strdata); lua_pushnumber(l, ret); return 2; case SASL_SSF: // sasl_ssf_t* ret = sasl_getprop(ctx->conn, propnum, (const void **)&ssf); // return *ssf lua_pushnumber(l, *ssf); lua_pushnumber(l, ret); return 2; case SASL_MAXOUTBUF: // unsigned ret = sasl_getprop(ctx->conn, propnum, (const void **)&maxbufsize); // return *maxbufsize lua_pushnumber(l, *maxbufsize); lua_pushnumber(l, ret); return 2; /* I'm not sure how SASL_GETOPTCTX would be useful to a Lua * caller, so I'm not including it for the moment. If you're * reading this and have a good use case, drop me a line and we'll * figure out how to integrate it. */ default: lua_pushstring(l, "Unsupported property passed to cyrussasl.getprop()"); lua_error(l); return 0; } /* NOTREACHED */ }