// // Printer // void Printer::PrintTable(std::string *result, Sqrat::Table table) { result->append("{"); int i = 0; Sqrat::Table::iterator it; while (table.Next(it)) { if (i > 0) { result->append(","); } result->append("[\""); Sqrat::Object o(it.getKey(), table.GetVM()); result->append(o.Cast<std::string>()); result->append("\"]="); Sqrat::Object obj(it.getValue(), table.GetVM()); PrintValue(result, obj); i++; } result->append("}"); }
void JsonPrinter::PrintTable(Json::Value &value, Sqrat::Table table) { Sqrat::Table::iterator it; while(table.Next(it)) { Sqrat::Object o(it.getKey(), table.GetVM()); std::string key = o.Cast<std::string>(); Sqrat::Object obj(it.getValue(), table.GetVM()); value[key] = PrintValue(obj); } }
//////////////////////////////////////////////////////////// // Initializes and registers the SFML vector3 library in the given VM //////////////////////////////////////////////////////////// void RegisterSfmlVector3Lib(HSQUIRRELVM v, Sqrat::Table& namespaceTable) { Sqrat::Class<sf::Vector3i> sfVector3i(v, _SC("sf.Vector3i")); sfVector3i.SquirrelFunc(_SC("constructor"), &sfVector3i_constructor); sfVector3i.GlobalFunc(_SC("equals"), &sfVector3i_equals); sfVector3i.Var(_SC("x"), &sf::Vector3i::x); sfVector3i.Var(_SC("y"), &sf::Vector3i::y); sfVector3i.Var(_SC("z"), &sf::Vector3i::z); sfVector3i.GlobalFunc(_SC("_add"), &sfVector3i_add); sfVector3i.GlobalFunc(_SC("_sub"), &sfVector3i_sub); sfVector3i.GlobalFunc(_SC("_mul"), &sfVector3i_mul); sfVector3i.GlobalFunc(_SC("_div"), &sfVector3i_div); sfVector3i.GlobalFunc(_SC("_unm"), &sfVector3i_unm); Sqrat::Class<sf::Vector3f> sfVector3f(v, _SC("sf.Vector3f")); sfVector3f.SquirrelFunc(_SC("constructor"), &sfVector3f_constructor); sfVector3f.GlobalFunc(_SC("equals"), &sfVector3f_equals); sfVector3f.Var(_SC("x"), &sf::Vector3f::x); sfVector3f.Var(_SC("y"), &sf::Vector3f::y); sfVector3f.Var(_SC("z"), &sf::Vector3f::z); sfVector3f.GlobalFunc(_SC("_add"), &sfVector3f_add); sfVector3f.GlobalFunc(_SC("_sub"), &sfVector3f_sub); sfVector3f.GlobalFunc(_SC("_mul"), &sfVector3f_mul); sfVector3f.GlobalFunc(_SC("_div"), &sfVector3f_div); sfVector3f.GlobalFunc(_SC("_unm"), &sfVector3f_unm); namespaceTable.Bind(_SC("Vector3i"), sfVector3i); namespaceTable.Bind(_SC("Vector3f"), sfVector3f); }
bool GameWorld::copyParameterTable(const Sqrat::Table &in, Sqrat::Table &out) { sq::Object::iterator it; while (in.Next(it)) { sq::Object value = in.GetSlot(it.getName()); switch (value.GetType()) { case OT_NULL: { out.SetValue(it.getName(), nullptr); } break; case OT_BOOL: { out.SetValue(it.getName(), value.Cast<bool>()); } break; case OT_INTEGER: { out.SetValue(it.getName(), value.Cast<int>()); } break; case OT_FLOAT: { out.SetValue(it.getName(), value.Cast<float>()); } break; case OT_STRING: { out.SetValue(it.getName(), value.Cast<std::string>()); } break; default: std::cout<<"Attemt to pass unsafe parameter to game world"; } } return true; }
bool SqMod::construct(CtxMod* pctx) { _pctx = pctx; _header_sent=false; if(_penv) { _penv->acquire(); Sqrat::Class<SqMod> module; module.Func(_SC("write"), &SqMod::swrite); Sqrat::RootTable().SetInstance(_SC("_this"), this); // // _context // Sqrat::Table context; //Sqrat::Class<CtxMod> context; context.SetValue(_SC("urldoc"), pctx->_url_doc) .SetValue(_SC("urldir"), pctx->_url_path) .SetValue(_SC("home"), pctx->_dir_home) .SetValue(_SC("index"), pctx->_doc_index) .SetValue(_SC("ip"), pctx->_cli_ip); Sqrat::RootTable().Bind(_SC("_context"), context); // // _REQ table with _ARGS, GET, COOKIE and so on // Sqrat::Table headers; const KeyValx* ps = _pctx->_hdrs; while((ps)->key) { if(ps->val && *ps->val) { headers.SetValue(_SC(ps->key), _SC(ps->val)); } ++ps; } Sqrat::RootTable().Bind(_SC("_request"), headers); Sqrat::Table ckooks; const KeyValx* psc = _pctx->_cookies; while((psc)->key) { if(psc->val && *psc->val) { ckooks.SetValue(_SC(psc->key), _SC(psc->val)); } ++psc; } Sqrat::RootTable().Bind(_SC("_cookies"), ckooks); Sqrat::Table get; const KeyValx* psv = _pctx->_get; while((psv)->key) { if(psv->val && *psv->val) { get.SetValue(_SC(psv->key), _SC(psv->val)); } ++psv; } Sqrat::RootTable().Bind(_SC("_get"), get); //Sqrat::RootTable().SetInstance(_SC("_ctx"), pctx); Sqrat::Table post; if(pctx->_tempfile) { // parse the post /* Content-Type: application/x-www-form-urlencoded Content-Length: 53 realname=ssssssss&email=ddddddddddd&comments=wwwwwwww */ const char* astype = _getHdr("CONTENT-TYPE")->val; int64_t len = atoll(_getHdr("CONTENT-LENGTH")->val); if(!str_cmp(astype,"application/x-www-form-urlencoded") && len < 16384) { char* buff = new char[len + 1]; if(buff) { int bytes = fread(buff, 1, len, pctx->_tempfile); char* pwalk = buff; assert(bytes == len); buff[bytes] = 0; do { const char* key = str_up2chr(&pwalk, '='); const char* val = str_up2chr(&pwalk, '&'); if(!valid_ptr(key) || !valid_ptr(val)) break; post.SetValue(_SC(key), _SC(val)); } while(1); delete []buff; } } } Sqrat::RootTable().Bind(_SC("_post"), post); return true; } return false; }