static SQInteger _file_constructor(HSQUIRRELVM v) { const SQChar *filename,*mode; bool owns = true; SQFile *f; SQFILE newf; if(sq_gettype(v,2) == OT_STRING && sq_gettype(v,3) == OT_STRING) { sq_getstring(v, 2, &filename); sq_getstring(v, 3, &mode); newf = sqstd_fopen(filename, mode); if(!newf) return sq_throwerror(v, _SC("cannot open file")); } else if(sq_gettype(v,2) == OT_USERPOINTER) { owns = !(sq_gettype(v,3) == OT_NULL); sq_getuserpointer(v,2,&newf); } else { return sq_throwerror(v,_SC("wrong parameter")); } f = new (sq_malloc(sizeof(SQFile)))SQFile(newf,owns); if(SQ_FAILED(sq_setinstanceup(v,1,f))) { f->~SQFile(); sq_free(f,sizeof(SQFile)); return sq_throwerror(v, _SC("cannot create blob with negative size")); } sq_setreleasehook(v,1,_file_releasehook); return 0; }
Tokenizer* Tokenizer_new(const SQChar* str, size_t str_size) { Tokenizer *tok = (Tokenizer*)sq_malloc(sizeof(Tokenizer)); memset(tok, 0, sizeof(Tokenizer)); tok->s_size = str_size; tok->s = str; return tok; }
SQFuncState *SQFuncState::PushChildState(SQSharedState *ss) { SQFuncState *child = (SQFuncState *)sq_malloc(sizeof(SQFuncState)); new (child) SQFuncState(ss,this,_errfunc,_errtarget); _childstates.push_back(child); return child; }
SQBlob::SQBlob(SQInteger size, SQInteger allocated) { _size = size; _allocated = allocated > size ? allocated : size; _buf = (unsigned char *)sq_malloc(_allocated); memset(_buf, 0, _allocated); _ptr = 0; _owns = true; }
static const SQChar* Tokenizer_set(Tokenizer* tok, const SQChar* s, size_t size) { if(!size||!s) return 0; free(tok->m_token); tok->m_token = (SQChar*)sq_malloc(size+1); scstrncpy(tok->m_token,s, size); tok->m_token[size] = 0; tok->m_token_size = tok->m_token_capacity = size; //Tokenizer_print(tok); return tok->m_token; }
int Xml_registerCode(HSQUIRRELVM v) { SQ_FUNC_VARS_NO_TOP(v); SQ_GET_STRING(v, 2, decoded); SQ_GET_STRING(v, 3, encoded); size_t i; for(i=0; i<sv_code_size; i+=2) if(scstrcmp(sv_code[i],decoded)==0) return sq_throwerror(v,_SC"SQXml ERROR: code already exists.")); if(sv_code_size+2>sv_code_capacity) { size_t old_capacity = sv_code_capacity; sv_code_capacity*=2; sv_code = (SQChar**)sq_realloc(sv_code, old_capacity*sizeof(SQChar*), sv_code_capacity*sizeof(SQChar*)); } sv_code[sv_code_size]=(SQChar*)sq_malloc((scstrlen(decoded)+1)*sizeof(SQChar*)); strcpy(sv_code[sv_code_size++], decoded); sv_code[sv_code_size]=(SQChar*)sq_malloc((scstrlen(decoded)+1)*sizeof(SQChar*)); strcpy(sv_code[sv_code_size++],encoded); return 0; }
static SQRESULT sq_libclang_constructor(HSQUIRRELVM v) { //SQ_FUNC_VARS_NO_TOP(v); if(!load_dynamicLib(dynamicLibName)) return sq_throwerror(v, _SC("Failed to load libclang !")); MyLibClang *self = (MyLibClang *)sq_malloc(sizeof(MyLibClang)); memset(self, 0, sizeof(MyLibClang)); self->v = v; self->index = dlclang_createIndex(0, 0); sq_setinstanceup(v, 1, self); sq_setreleasehook(v,1, sq_libclang_releasehook); return 1; }
int Xml_load (HSQUIRRELVM v) { SQ_FUNC_VARS_NO_TOP(v); SQ_GET_STRING(v, 2, filename); FILE * file=fopen(filename,"r"); if(!file) return sq_throwerror(v, _SC("SQXml ERROR: \"%s\" file error or file not found!"),filename); fseek (file , 0 , SEEK_END); size_t sz = ftell (file); rewind (file); char* buffer = (char*)sq_malloc(sz+1); sz = fread (buffer,1,sz,file); fclose(file); buffer[sz]=0; sq_pushuserpointer(v,buffer); sq_replace(v,2); return Xml_eval(v); };
bool SQBlob::Resize(SQInteger n) { if(!_owns) return false; if(n != _allocated) { unsigned char *newbuf = (unsigned char *)sq_malloc(n); memset(newbuf,0,n); if(_size > n) memcpy(newbuf,_buf,n); else memcpy(newbuf,_buf,_size); sq_free(_buf,_allocated); _buf=newbuf; _allocated = n; if(_size > _allocated) _size = _allocated; if(_ptr > _size) _ptr = _size; } return true; }
static SQRESULT sq_mysql_prepare(HSQUIRRELVM v){ SQ_FUNC_VARS_NO_TOP(v); GET_mysql_INSTANCE(); SQ_GET_STRING(v, 2, szSQL); MYSQL_STMT *stmt = dlmysql_stmt_init(self); if (!stmt) return sq_throwerror(v, _SC(" mysql_stmt_init(), out of memory\n")); if (dlmysql_stmt_prepare(stmt, szSQL, szSQL_size)) { SQRESULT res = sq_throwerror(v, dlmysql_stmt_error(stmt)); dlmysql_stmt_close(stmt); return res; } unsigned long expected_params = dlmysql_stmt_param_count(stmt); size_t bind_params_info_size = sizeof(MYSQL_BIND) * expected_params; size_t bind_params_data_size = sizeof(double) * expected_params; size_t bind_data_buffer_size = bind_params_info_size + bind_params_data_size; size_t st_mysq_stmt_size = sizeof(SQ_MysqlStatement) + bind_data_buffer_size; SQ_MysqlStatement *sq_stmt = (SQ_MysqlStatement*)sq_malloc(st_mysq_stmt_size); sq_stmt->stmt = stmt; sq_stmt->bind_data_buffer_size = bind_data_buffer_size; sq_stmt->param_count = expected_params; sq_stmt->bind_params_info = (MYSQL_BIND*)sq_stmt->bind_data_buffer; sq_stmt->bind_params_data = ((char*)sq_stmt->bind_params_info) + bind_params_info_size; sq_pushroottable(v); sq_pushstring(v, MySQL_Statement_TAG, -1); if(sq_get(v, -2) == SQ_OK){ if(sq_createinstance(v, -1) == SQ_OK){ sq_setinstanceup(v, -1, sq_stmt); sq_setreleasehook(v, -1, sq_mysql_statement_releasehook); return 1; } } return SQ_ERROR; }
SQRESULT sqext_register_sqxml(HSQUIRRELVM v) { sq_pushstring(v,_SC("SQXml"),-1); sq_newclass(v,SQFalse); sq_insert_reg_funcs(v, xml_methods); sq_newslot(v,-3,SQTrue); // register default codes: if(!sv_code) { sv_code=(SQChar**)sq_malloc(sv_code_capacity*sizeof(SQChar*)); sv_code[sv_code_size++]=_SC("&"); sv_code[sv_code_size++]=_SC("&"); sv_code[sv_code_size++]=_SC("<"); sv_code[sv_code_size++]=_SC("<"); sv_code[sv_code_size++]=_SC(">"); sv_code[sv_code_size++]=_SC(">"); sv_code[sv_code_size++]=_SC("\""); sv_code[sv_code_size++]=_SC("""); sv_code[sv_code_size++]=_SC("'"); sv_code[sv_code_size++]=_SC("'"); } return 1; }
static SQRESULT sq_libclang_parseTranslationUnit(HSQUIRRELVM v){ SQ_FUNC_VARS(v); GET_libclang_INSTANCE(); if(sq_gettype(v, 2) != OT_CLOSURE) return sq_throwerror(v, _SC("invalid fisrt parameter expected closure")); SQ_GET_STRING(v, 3, fname); release_visitor_cb(self); sq_getstackobj(v, 2, &self->visitor_cb); sq_addref(v, &self->visitor_cb); const char *cl_argsDefault[] = {"-I."}; const char **cl_args = cl_argsDefault; int cl_argNum = 1; int rc = 0; const int cl_arg_start = 4; bool has_extra_params = _top_ >= cl_arg_start; if(has_extra_params) { //create cl_args with extra parameters cl_argNum = _top_ - (cl_arg_start -1); cl_args = (const char **)sq_malloc(sizeof(char*) * cl_argNum); for(int i=cl_arg_start; i <= _top_; ++i) { const SQChar *p; if(sq_gettype(v, i) == OT_STRING) { rc = sq_getstring(v, i, &p); } else { rc = sq_throwerror(v, _SC("not a string parameter at %d"), i); goto cleanup; } cl_args[i-cl_arg_start] = p; } } CXTranslationUnit TU; CXCursor rootCursor; TU = dlclang_parseTranslationUnit(self->index, fname, cl_args, cl_argNum, 0, 0, CXTranslationUnit_Incomplete); if (TU == NULL) { rc = sq_throwerror(v, _SC("clang_parseTranslationUnit for %s failed\n"), fname); goto cleanup; } rootCursor = dlclang_getTranslationUnitCursor(TU); dlclang_visitChildren(rootCursor, cursorVisitor, self); dlclang_disposeTranslationUnit(TU); cleanup: if(has_extra_params) { sq_free(cl_args, sizeof(char*) * cl_argNum); } return rc; }
// Create and populate the HSQAPI structure with function pointers // If new functions are added to the Squirrel API, they should be added here too static HSQAPI sqrat_newapi() { HSQAPI sq = (HSQAPI)sq_malloc(sizeof(sq_api)); /*vm*/ sq->open = sq_open; sq->newthread = sq_newthread; sq->seterrorhandler = sq_seterrorhandler; sq->close = sq_close; sq->setforeignptr = sq_setforeignptr; sq->getforeignptr = sq_getforeignptr; sq->setprintfunc = sq_setprintfunc; sq->getprintfunc = sq_getprintfunc; sq->suspendvm = sq_suspendvm; sq->wakeupvm = sq_wakeupvm; sq->getvmstate = sq_getvmstate; /*compiler*/ sq->compile = sq_compile; sq->compilebuffer = sq_compilebuffer; sq->enabledebuginfo = sq_enabledebuginfo; sq->notifyallexceptions = sq_notifyallexceptions; sq->setcompilererrorhandler = sq_setcompilererrorhandler; /*stack operations*/ sq->push = sq_push; sq->pop = sq_pop; sq->poptop = sq_poptop; sq->remove = sq_remove; sq->gettop = sq_gettop; sq->settop = sq_settop; sq->reservestack = sq_reservestack; sq->cmp = sq_cmp; sq->move = sq_move; /*object creation handling*/ sq->newuserdata = sq_newuserdata; sq->newtable = sq_newtable; sq->newarray = sq_newarray; sq->newclosure = sq_newclosure; sq->setparamscheck = sq_setparamscheck; sq->bindenv = sq_bindenv; sq->pushstring = sq_pushstring; sq->pushfloat = sq_pushfloat; sq->pushinteger = sq_pushinteger; sq->pushbool = sq_pushbool; sq->pushuserpointer = sq_pushuserpointer; sq->pushnull = sq_pushnull; sq->gettype = sq_gettype; sq->getsize = sq_getsize; sq->getbase = sq_getbase; sq->instanceof = sq_instanceof; sq->tostring = sq_tostring; sq->tobool = sq_tobool; sq->getstring = sq_getstring; sq->getinteger = sq_getinteger; sq->getthread = sq_getthread; sq->getbool = sq_getbool; sq->getuserpointer = sq_getuserpointer; sq->getuserdata = sq_getuserdata; sq->settypetag = sq_settypetag; sq->gettypetag = sq_gettypetag; sq->setreleasehook = sq_setreleasehook; sq->getscratchpad = sq_getscratchpad; sq->getclosureinfo = sq_getclosureinfo; sq->setnativeclosurename = sq_setnativeclosurename; sq->setinstanceup = sq_setinstanceup; sq->getinstanceup = sq_getinstanceup; sq->setclassudsize = sq_setclassudsize; sq->newclass = sq_newclass; sq->createinstance = sq_createinstance; sq->setattributes = sq_setattributes; sq->getattributes = sq_getattributes; sq->getclass = sq_getclass; sq->weakref = sq_weakref; sq->getdefaultdelegate = sq_getdefaultdelegate; /*object manipulation*/ sq->pushroottable = sq_pushroottable; sq->pushregistrytable = sq_pushregistrytable; sq->pushconsttable = sq_pushconsttable; sq->setroottable = sq_setroottable; sq->setconsttable = sq_setconsttable; sq->newslot = sq_newslot; sq->deleteslot = sq_deleteslot; sq->set = sq_set; sq->get = sq_get; sq->rawset = sq_rawset; sq->rawget = sq_rawget; sq->rawdeleteslot = sq_rawdeleteslot; sq->arrayappend = sq_arrayappend; sq->arraypop = sq_arraypop; sq->arrayresize = sq_arrayresize; sq->arrayreverse = sq_arrayreverse; sq->arrayremove = sq_arrayremove; sq->arrayinsert = sq_arrayinsert; sq->setdelegate = sq_setdelegate; sq->getdelegate = sq_getdelegate; sq->clone = sq_clone; sq->setfreevariable = sq_setfreevariable; sq->next = sq_next; sq->getweakrefval = sq_getweakrefval; sq->clear = sq_clear; /*calls*/ sq->call = sq_call; sq->resume = sq_resume; sq->getlocal = sq_getlocal; sq->getfreevariable = sq_getfreevariable; sq->throwerror = sq_throwerror; sq->reseterror = sq_reseterror; sq->getlasterror = sq_getlasterror; /*raw object handling*/ sq->getstackobj = sq_getstackobj; sq->pushobject = sq_pushobject; sq->addref = sq_addref; sq->release = sq_release; sq->resetobject = sq_resetobject; sq->objtostring = sq_objtostring; sq->objtobool = sq_objtobool; sq->objtointeger = sq_objtointeger; sq->objtofloat = sq_objtofloat; sq->getobjtypetag = sq_getobjtypetag; /*GC*/ sq->collectgarbage = sq_collectgarbage; /*serialization*/ sq->writeclosure = sq_writeclosure; sq->readclosure = sq_readclosure; /*mem allocation*/ sq->malloc = sq_malloc; sq->realloc = sq_realloc; sq->free = sq_free; /*debug*/ sq->stackinfos = sq_stackinfos; sq->setdebughook = sq_setdebughook; return sq; }
int Xml_eval(HSQUIRRELVM v) { SQ_FUNC_VARS_NO_TOP(v); SQChar* str = 0; size_t str_size=0; if(sq_gettype(v,2) == OT_USERPOINTER) sq_getuserpointer(v, 2, &str); else { SQ_GET_STRING(v, 2, sTmp); str = (SQChar*)sq_malloc(sTmp_size+(sizeof(SQChar))); memcpy(str, sTmp, sTmp_size); str[sTmp_size]=0; str_size = sTmp_size; } Tokenizer* tok = Tokenizer_new(str, str_size ? str_size : scstrlen(str)); sq_settop(v,0); const SQChar* token=0; int firstStatement = 1; while((token=Tokenizer_next(tok))!=0) if(token[0]==OPN) { // new tag found if(sq_gettop(v)) { int newIndex=sq_size(v,-1)+1; sq_pushinteger(v,newIndex); sq_newtable(v); sq_set(v, -3); sq_pushinteger(v,newIndex); sq_get(v,-2); } else { if (firstStatement) { sq_newtable(v); firstStatement = 0; } else return lua_gettop(L); } // set metatable: sq_newtable(v); sq_pushliteral(v, _SC("__index")); sq_getglobal(v, "xml"); lua_settable(v, -3); sq_pushliteral(v, _SC("__tostring")); // set __tostring metamethod lua_getglobal(L, "xml"); lua_pushliteral(L,"str"); lua_gettable(v, -2); sq_remove(v, -2); sq_set(v, -3); lua_setmetatable(L, -2); // parse tag and content: sq_pushinteger(v,0); // use index 0 for storing the tag sq_pushstring(v, Tokenizer_next(tok), -1); sq_set(v, -3); while(((token = Tokenizer_next(tok))!=0)&&(token[0]!=CLS)&&(token[0]!=ESC)) { // parse tag header size_t sepPos=find(token, "=", 0); if(token[sepPos]) { // regular attribute const SQChar* aVal =token+sepPos+2; sq_pushstring(v, token, sepPos); size_t lenVal = strlen(aVal)-1; if(!lenVal) Xml_pushDecode(v, _SC(""), 0); else Xml_pushDecode(v, aVal, lenVal); sq_set(v, -3); } } if(!token||(token[0]==ESC)) { if(sq_gettop(v)>1) sq_settop(v,-2); // this tag has no content, only attributes else break; } } else if(token[0]==ESC) { // previous tag is over if(sq_gettop(v)>1) sq_settop(v,-2); // pop current table else break; } else { // read elements sq_pushinteger(v,sq_size(v,-1)+1); Xml_pushDecode(v, token, 0); sq_rawset(v, -3); } Tokenizer_delete(tok); sq_free(str); return sq_gettop(v); }
void *sqlite3MallocZero(size_t size) { void *p = sq_malloc(size); memset(p, 0, size); return p; }