sqratimport.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 SQRESULT sqrat_importscript(HSQUIRRELVM v, const SQChar* moduleName) {
  171. std::basic_string<SQChar> filename(moduleName);
  172. filename += _SC(".nut");
  173. if(SQ_FAILED(sqstd_loadfile(v, moduleName, true))) {
  174. if(SQ_FAILED(sqstd_loadfile(v, filename.c_str(), true))) {
  175. return SQ_ERROR;
  176. }
  177. }
  178. sq_push(v, -2);
  179. sq_call(v, 1, false, true);
  180. return SQ_OK;
  181. }
  182. static SQRESULT sqrat_importbin(HSQUIRRELVM v, const SQChar* moduleName) {
  183. #ifdef SQUNICODE
  184. #warning sqrat_importbin() Not Implemented
  185. return SQ_ERROR;
  186. #else
  187. SQMODULELOAD modLoad = 0;
  188. #if defined(_WIN32)
  189. HMODULE mod;
  190. mod = GetModuleHandle(moduleName);
  191. if(mod == NULL) {
  192. mod = LoadLibrary(moduleName);
  193. if(mod == NULL) {
  194. return SQ_ERROR;
  195. }
  196. }
  197. modLoad = (SQMODULELOAD)GetProcAddress(mod, "sqmodule_load");
  198. if(modLoad == NULL) {
  199. FreeLibrary(mod);
  200. return SQ_ERROR;
  201. }
  202. #elif defined(__unix)
  203. /* adding .so to moduleName? */
  204. void *mod = dlopen(moduleName, RTLD_NOW | RTLD_LOCAL | RTLD_NOLOAD); //RTLD_NOLOAD flag is not specified in POSIX.1-2001..so not the best solution :(
  205. if (mod == NULL) {
  206. mod = dlopen(moduleName, RTLD_NOW | RTLD_LOCAL);
  207. if (mod == NULL)
  208. return SQ_ERROR;
  209. }
  210. modLoad = (SQMODULELOAD) dlsym(mod, "sqmodule_load");
  211. if (modLoad == NULL) {
  212. dlclose(mod);
  213. return SQ_ERROR;
  214. }
  215. #endif
  216. if(sqapi == NULL) {
  217. sqapi = sqrat_newapi(); // Caching this for multiple imports is probably a very good idea
  218. }
  219. SQRESULT res = modLoad(v, sqapi);
  220. return res;
  221. #endif
  222. }
  223. SQRESULT sqrat_import(HSQUIRRELVM v) {
  224. const SQChar* moduleName;
  225. HSQOBJECT table;
  226. SQRESULT res = SQ_OK;
  227. sq_getstring(v, -2, &moduleName);
  228. sq_getstackobj(v, -1, &table);
  229. sq_addref(v, &table);
  230. sq_settop(v, 0); // Clear Stack
  231. sq_pushobject(v, table); // Push the target table onto the stack
  232. if(SQ_FAILED(sqrat_importscript(v, moduleName))) {
  233. res = sqrat_importbin(v, moduleName);
  234. }
  235. sq_settop(v, 0); // Clean up the stack (just in case the module load leaves it messy)
  236. sq_pushobject(v, table); // return the target table
  237. sq_release(v, &table);
  238. return res;
  239. }
  240. static SQInteger sqratbase_import(HSQUIRRELVM v) {
  241. SQInteger args = sq_gettop(v);
  242. switch(args) {
  243. case 2:
  244. sq_pushroottable(v);
  245. break;
  246. case 3:
  247. // should already have the desired table pushed onto the stack
  248. break;
  249. default:
  250. // Error, unexpected number of arguments
  251. break;
  252. }
  253. sqrat_import(v);
  254. return 1;
  255. }
  256. SQRESULT sqrat_register_importlib(HSQUIRRELVM v) {
  257. sq_pushroottable(v);
  258. sq_pushstring(v, _SC("import"), -1);
  259. sq_newclosure(v, &sqratbase_import, 0);
  260. sq_newslot(v, -3, 0);
  261. sq_pop(v, 1); // pop sqrat table
  262. return SQ_OK;
  263. }