sqratimport.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. //
  2. // SqratImport: Supports importing of squirrel modules
  3. //
  4. //
  5. // Copyright (c) 2009 Brandon Jones
  6. //
  7. // This software is provided 'as-is', without any express or implied
  8. // warranty. In no event will the authors be held liable for any damages
  9. // arising from the use of this software.
  10. //
  11. // Permission is granted to anyone to use this software for any purpose,
  12. // including commercial applications, and to alter it and redistribute it
  13. // freely, subject to the following restrictions:
  14. //
  15. // 1. The origin of this software must not be misrepresented; you must not
  16. // claim that you wrote the original software. If you use this software
  17. // in a product, an acknowledgment in the product documentation would be
  18. // appreciated but is not required.
  19. //
  20. // 2. Altered source versions must be plainly marked as such, and must not be
  21. // misrepresented as being the original software.
  22. //
  23. // 3. This notice may not be removed or altered from any source
  24. // distribution.
  25. //
  26. #include "sqratimport.h"
  27. #include "sqmodule.h"
  28. //#include "sqratlib/sqratBase.h"
  29. #include <sqstdio.h>
  30. #include <string>
  31. #if defined(_WIN32)
  32. #include <windows.h>
  33. #elif defined(__unix)
  34. #include <dlfcn.h>
  35. #endif
  36. typedef SQRESULT (*SQMODULELOAD)(HSQUIRRELVM v, HSQAPI sq);
  37. static HSQAPI sqapi = NULL;
  38. // Create and populate the HSQAPI structure with function pointers
  39. // If new functions are added to the Squirrel API, they should be added here too
  40. static HSQAPI sqrat_newapi() {
  41. HSQAPI sq = (HSQAPI)sq_malloc(sizeof(sq_api));
  42. /*vm*/
  43. sq->open = sq_open;
  44. sq->newthread = sq_newthread;
  45. sq->seterrorhandler = sq_seterrorhandler;
  46. sq->close = sq_close;
  47. sq->setforeignptr = sq_setforeignptr;
  48. sq->getforeignptr = sq_getforeignptr;
  49. sq->setprintfunc = sq_setprintfunc;
  50. sq->getprintfunc = sq_getprintfunc;
  51. sq->suspendvm = sq_suspendvm;
  52. sq->wakeupvm = sq_wakeupvm;
  53. sq->getvmstate = sq_getvmstate;
  54. /*compiler*/
  55. sq->compile = sq_compile;
  56. sq->compilebuffer = sq_compilebuffer;
  57. sq->enabledebuginfo = sq_enabledebuginfo;
  58. sq->notifyallexceptions = sq_notifyallexceptions;
  59. sq->setcompilererrorhandler = sq_setcompilererrorhandler;
  60. /*stack operations*/
  61. sq->push = sq_push;
  62. sq->pop = sq_pop;
  63. sq->poptop = sq_poptop;
  64. sq->remove = sq_remove;
  65. sq->gettop = sq_gettop;
  66. sq->settop = sq_settop;
  67. sq->reservestack = sq_reservestack;
  68. sq->cmp = sq_cmp;
  69. sq->move = sq_move;
  70. /*object creation handling*/
  71. sq->newuserdata = sq_newuserdata;
  72. sq->newtable = sq_newtable;
  73. sq->newarray = sq_newarray;
  74. sq->newclosure = sq_newclosure;
  75. sq->setparamscheck = sq_setparamscheck;
  76. sq->bindenv = sq_bindenv;
  77. sq->pushstring = sq_pushstring;
  78. sq->pushfloat = sq_pushfloat;
  79. sq->pushinteger = sq_pushinteger;
  80. sq->pushbool = sq_pushbool;
  81. sq->pushuserpointer = sq_pushuserpointer;
  82. sq->pushnull = sq_pushnull;
  83. sq->gettype = sq_gettype;
  84. sq->getsize = sq_getsize;
  85. sq->getbase = sq_getbase;
  86. sq->instanceof = sq_instanceof;
  87. sq->tostring = sq_tostring;
  88. sq->tobool = sq_tobool;
  89. sq->getstring = sq_getstring;
  90. sq->getinteger = sq_getinteger;
  91. sq->getthread = sq_getthread;
  92. sq->getbool = sq_getbool;
  93. sq->getuserpointer = sq_getuserpointer;
  94. sq->getuserdata = sq_getuserdata;
  95. sq->settypetag = sq_settypetag;
  96. sq->gettypetag = sq_gettypetag;
  97. sq->setreleasehook = sq_setreleasehook;
  98. sq->getscratchpad = sq_getscratchpad;
  99. sq->getclosureinfo = sq_getclosureinfo;
  100. sq->setnativeclosurename = sq_setnativeclosurename;
  101. sq->setinstanceup = sq_setinstanceup;
  102. sq->getinstanceup = sq_getinstanceup;
  103. sq->setclassudsize = sq_setclassudsize;
  104. sq->newclass = sq_newclass;
  105. sq->createinstance = sq_createinstance;
  106. sq->setattributes = sq_setattributes;
  107. sq->getattributes = sq_getattributes;
  108. sq->getclass = sq_getclass;
  109. sq->weakref = sq_weakref;
  110. sq->getdefaultdelegate = sq_getdefaultdelegate;
  111. /*object manipulation*/
  112. sq->pushroottable = sq_pushroottable;
  113. sq->pushregistrytable = sq_pushregistrytable;
  114. sq->pushconsttable = sq_pushconsttable;
  115. sq->setroottable = sq_setroottable;
  116. sq->setconsttable = sq_setconsttable;
  117. sq->newslot = sq_newslot;
  118. sq->deleteslot = sq_deleteslot;
  119. sq->set = sq_set;
  120. sq->get = sq_get;
  121. sq->rawset = sq_rawset;
  122. sq->rawget = sq_rawget;
  123. sq->rawdeleteslot = sq_rawdeleteslot;
  124. sq->arrayappend = sq_arrayappend;
  125. sq->arraypop = sq_arraypop;
  126. sq->arrayresize = sq_arrayresize;
  127. sq->arrayreverse = sq_arrayreverse;
  128. sq->arrayremove = sq_arrayremove;
  129. sq->arrayinsert = sq_arrayinsert;
  130. sq->setdelegate = sq_setdelegate;
  131. sq->getdelegate = sq_getdelegate;
  132. sq->clone = sq_clone;
  133. sq->setfreevariable = sq_setfreevariable;
  134. sq->next = sq_next;
  135. sq->getweakrefval = sq_getweakrefval;
  136. sq->clear = sq_clear;
  137. /*calls*/
  138. sq->call = sq_call;
  139. sq->resume = sq_resume;
  140. sq->getlocal = sq_getlocal;
  141. sq->getfreevariable = sq_getfreevariable;
  142. sq->throwerror = sq_throwerror;
  143. sq->reseterror = sq_reseterror;
  144. sq->getlasterror = sq_getlasterror;
  145. /*raw object handling*/
  146. sq->getstackobj = sq_getstackobj;
  147. sq->pushobject = sq_pushobject;
  148. sq->addref = sq_addref;
  149. sq->release = sq_release;
  150. sq->resetobject = sq_resetobject;
  151. sq->objtostring = sq_objtostring;
  152. sq->objtobool = sq_objtobool;
  153. sq->objtointeger = sq_objtointeger;
  154. sq->objtofloat = sq_objtofloat;
  155. sq->getobjtypetag = sq_getobjtypetag;
  156. /*GC*/
  157. sq->collectgarbage = sq_collectgarbage;
  158. /*serialization*/
  159. sq->writeclosure = sq_writeclosure;
  160. sq->readclosure = sq_readclosure;
  161. /*mem allocation*/
  162. sq->malloc = sq_malloc;
  163. sq->realloc = sq_realloc;
  164. sq->free = sq_free;
  165. /*debug*/
  166. sq->stackinfos = sq_stackinfos;
  167. sq->setdebughook = sq_setdebughook;
  168. return sq;
  169. }
  170. static void sqrat_deleteapi(HSQAPI sq) {
  171. sq_free(sq, sizeof(sq_api));
  172. }
  173. static SQRESULT sqrat_importscript(HSQUIRRELVM v, const SQChar* moduleName) {
  174. if(SQ_FAILED(sqstd_loadfile(v, moduleName, true))) {
  175. std::basic_string<SQChar> filename(moduleName);
  176. filename += _SC(".nut");
  177. if(SQ_FAILED(sqstd_loadfile(v, filename.c_str(), true))) {
  178. return SQ_ERROR;
  179. }
  180. }
  181. sq_push(v, -2);
  182. sq_call(v, 1, false, true);
  183. return SQ_OK;
  184. }
  185. static SQRESULT sqrat_importbin(HSQUIRRELVM v, const SQChar* moduleName) {
  186. SQMODULELOAD modLoad = 0;
  187. #if defined(_WIN32)
  188. HMODULE mod;
  189. mod = GetModuleHandle(moduleName);
  190. if(mod == NULL) {
  191. mod = LoadLibrary(moduleName);
  192. if(mod == NULL) {
  193. return SQ_ERROR;
  194. }
  195. }
  196. modLoad = (SQMODULELOAD)GetProcAddress(mod, "sqmodule_load");
  197. if(modLoad == NULL) {
  198. FreeLibrary(mod);
  199. return SQ_ERROR;
  200. }
  201. #elif defined(__unix)
  202. /* adding .so to moduleName? */
  203. void *mod = dlopen(moduleName, RTLD_NOW | RTLD_LOCAL
  204. #ifndef ANDROID_BUILD
  205. | RTLD_NOLOAD //RTLD_NOLOAD flag is not specified in POSIX.1-2001..so not the best solution :(
  206. #endif
  207. );
  208. if (mod == NULL) {
  209. mod = dlopen(moduleName, RTLD_NOW | RTLD_LOCAL);
  210. if (mod == NULL)
  211. return SQ_ERROR;
  212. }
  213. modLoad = (SQMODULELOAD) dlsym(mod, "sqmodule_load");
  214. if (modLoad == NULL) {
  215. dlclose(mod);
  216. return SQ_ERROR;
  217. }
  218. #endif
  219. if(sqapi == NULL) {
  220. sqapi = sqrat_newapi(); // Caching this for multiple imports is probably a very good idea
  221. }
  222. SQRESULT res = modLoad(v, sqapi);
  223. return res;
  224. }
  225. SQRESULT sqrat_import(HSQUIRRELVM v) {
  226. const SQChar* moduleName;
  227. HSQOBJECT table;
  228. SQRESULT res = SQ_OK;
  229. //SQInteger top = sq_gettop(v);
  230. sq_getstring(v, -2, &moduleName);
  231. sq_getstackobj(v, -1, &table);
  232. sq_addref(v, &table);
  233. sq_settop(v, 0); // Clear Stack
  234. sq_pushobject(v, table); // Push the target table onto the stack
  235. if(SQ_FAILED(sqrat_importscript(v, moduleName))) {
  236. res = sqrat_importbin(v, moduleName);
  237. }
  238. sq_settop(v, 0); // Clean up the stack (just in case the module load leaves it messy)
  239. sq_pushobject(v, table); // return the target table
  240. sq_release(v, &table);
  241. return res;
  242. }
  243. static SQInteger sqratbase_import(HSQUIRRELVM v) {
  244. SQInteger args = sq_gettop(v);
  245. switch(args) {
  246. case 2:
  247. sq_pushroottable(v);
  248. break;
  249. case 3:
  250. // should already have the desired table pushed onto the stack
  251. if(sq_gettype(v, 3) != OT_TABLE) return sq_throwerror(v, _SC("table expected as second parameter"));
  252. break;
  253. default:
  254. // Error, unexpected number of arguments
  255. return sq_throwerror(v, _SC("unexpected number of arguments"));
  256. break;
  257. }
  258. sqrat_import(v);
  259. return 1;
  260. }
  261. SQRESULT sqrat_register_importlib(HSQUIRRELVM v) {
  262. sq_pushroottable(v);
  263. sq_pushstring(v, _SC("import"), -1);
  264. sq_newclosure(v, &sqratbase_import, 0);
  265. sq_newslot(v, -3, 0);
  266. sq_pop(v, 1); // pop root table
  267. return SQ_OK;
  268. }