jerry_module.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <rtthread.h>
  5. #include <finsh.h>
  6. #include "jerry_util.h"
  7. #include "jerry_module.h"
  8. #include <jerryscript-ext/module.h>
  9. #include <ecma-globals.h>
  10. #ifdef RT_USING_MODULE
  11. #include <dlfcn.h>
  12. #endif
  13. #ifndef PATH_MAX
  14. #define PATH_MAX 256
  15. #endif
  16. char *getcwd(char *buf, size_t size);
  17. typedef jerry_value_t (*module_init_func_t)(void);
  18. #ifdef RT_USING_DFS
  19. char *js_module_dirname(char *path)
  20. {
  21. size_t i;
  22. char *s = NULL;
  23. if (!path || !*path) return NULL;
  24. s = rt_strdup(path);
  25. if (!s) return NULL;
  26. i = strlen(s)-1;
  27. for (; s[i]=='/'; i--) if (!i)
  28. {
  29. s[0] = '/';
  30. goto __exit;
  31. }
  32. for (; s[i]!='/'; i--) if (!i)
  33. {
  34. s[0] = '.';
  35. goto __exit;
  36. }
  37. for (; s[i]=='/'; i--) if (!i)
  38. {
  39. s[0] = '/';
  40. goto __exit;
  41. }
  42. __exit:
  43. s[i+1] = 0;
  44. return s;
  45. }
  46. char *js_module_normalize_path(const char *directory, const char *filename)
  47. {
  48. char *fullpath;
  49. char *dst0, *dst, *src;
  50. char *cwd = NULL;
  51. /* check parameters */
  52. if (filename == NULL) return NULL;
  53. if (directory == NULL && filename[0] != '/')
  54. {
  55. cwd = (char*) malloc (PATH_MAX);
  56. if (cwd == NULL) return NULL;
  57. /* get current working directory */
  58. getcwd(cwd, PATH_MAX);
  59. directory = cwd;
  60. }
  61. if (filename[0] != '/') /* it's a absolute path, use it directly */
  62. {
  63. fullpath = malloc(strlen(directory) + strlen(filename) + 2);
  64. if (fullpath == NULL)
  65. {
  66. free(cwd);
  67. return NULL;
  68. }
  69. /* join path and file name */
  70. snprintf(fullpath, strlen(directory) + strlen(filename) + 2,
  71. "%s/%s", directory, filename);
  72. }
  73. else
  74. {
  75. fullpath = rt_strdup(filename); /* copy string */
  76. if (fullpath == NULL)
  77. return NULL;
  78. }
  79. src = fullpath;
  80. dst = fullpath;
  81. dst0 = dst;
  82. while (1)
  83. {
  84. char c = *src;
  85. if (c == '.')
  86. {
  87. if (!src[1]) src ++; /* '.' and ends */
  88. else if (src[1] == '/')
  89. {
  90. /* './' case */
  91. src += 2;
  92. while ((*src == '/') && (*src != '\0'))
  93. src ++;
  94. continue;
  95. }
  96. else if (src[1] == '.')
  97. {
  98. if (!src[2])
  99. {
  100. /* '..' and ends case */
  101. src += 2;
  102. goto up_one;
  103. }
  104. else if (src[2] == '/')
  105. {
  106. /* '../' case */
  107. src += 3;
  108. while ((*src == '/') && (*src != '\0'))
  109. src ++;
  110. goto up_one;
  111. }
  112. }
  113. }
  114. /* copy up the next '/' and erase all '/' */
  115. while ((c = *src++) != '\0' && c != '/')
  116. *dst ++ = c;
  117. if (c == '/')
  118. {
  119. *dst ++ = '/';
  120. while (c == '/')
  121. c = *src++;
  122. src --;
  123. }
  124. else if (!c)
  125. break;
  126. continue;
  127. up_one:
  128. dst --;
  129. if (dst < dst0)
  130. {
  131. free(cwd);
  132. free(fullpath);
  133. return NULL;
  134. }
  135. while (dst0 < dst && dst[-1] != '/')
  136. dst --;
  137. }
  138. *dst = '\0';
  139. /* remove '/' in the end of path if exist */
  140. dst --;
  141. if ((dst != fullpath) && (*dst == '/'))
  142. *dst = '\0';
  143. /* final check fullpath is not empty, for the special path of lwext "/.." */
  144. if ('\0' == fullpath[0])
  145. {
  146. fullpath[0] = '/';
  147. fullpath[1] = '\0';
  148. }
  149. free(cwd);
  150. return fullpath;
  151. }
  152. /* load module from file system */
  153. static bool load_module_from_filesystem(const jerry_value_t module_name, jerry_value_t *result)
  154. {
  155. bool ret = false;
  156. char *str = NULL;
  157. char *module = js_value_to_string(module_name);
  158. char *dirname = NULL;
  159. jerry_value_t dirname_value = ECMA_VALUE_UNDEFINED;
  160. jerry_value_t filename_value = ECMA_VALUE_UNDEFINED;
  161. jerry_value_t global_obj = ECMA_VALUE_UNDEFINED;
  162. char *full_path = NULL;
  163. char *full_dir = NULL;
  164. global_obj = jerry_get_global_object();
  165. dirname_value = js_get_property(global_obj, "__dirname");
  166. if (jerry_value_is_string(dirname_value))
  167. {
  168. dirname = js_value_to_string(dirname_value);
  169. }
  170. else
  171. {
  172. dirname = NULL;
  173. }
  174. if (module[0] != '/') /* is a relative path */
  175. {
  176. full_path = js_module_normalize_path(dirname, module);
  177. }
  178. else
  179. {
  180. full_path = js_module_normalize_path(NULL, module);
  181. }
  182. free(dirname);
  183. uint32_t len = js_read_file(full_path, &str);
  184. if (len == 0) goto __exit;
  185. filename_value = js_get_property(global_obj, "__filename");
  186. /* set new __filename and __dirname */
  187. full_dir = js_module_dirname(full_path);
  188. js_set_string_property(global_obj, "__dirname", full_dir);
  189. js_set_string_property(global_obj, "__filename", full_path);
  190. (*result) = jerry_eval((jerry_char_t *)str, len, false);
  191. if (jerry_value_is_error(*result))
  192. printf("failed to evaluate JS\n");
  193. else
  194. ret = true;
  195. /* restore __filename and __dirname */
  196. js_set_property(global_obj, "__dirname", dirname_value);
  197. js_set_property(global_obj, "__filename", filename_value);
  198. __exit:
  199. if (full_dir) free(full_dir);
  200. if (full_path) free(full_path);
  201. jerry_release_value(global_obj);
  202. jerry_release_value(dirname_value);
  203. jerry_release_value(filename_value);
  204. free(module);
  205. free(str);
  206. return ret;
  207. }
  208. #endif
  209. /* load builtin module */
  210. static bool load_module_from_builtin(const jerry_value_t module_name,
  211. jerry_value_t *result)
  212. {
  213. bool ret = false;
  214. module_init_func_t module_init;
  215. char *module = js_value_to_string(module_name);
  216. #ifdef HOST_BUILD
  217. {
  218. extern jerry_value_t js_module_rtthread_init(void);
  219. if (strcmp(module, "os") == 0)
  220. {
  221. module_init = js_module_rtthread_init;
  222. *result = module_init();
  223. ret = true;
  224. }
  225. }
  226. #elif defined(RT_USING_FINSH)
  227. int len = strlen(module) + 7;
  228. char module_fullname[len];
  229. snprintf(module_fullname, len, "__jsm_%s", module);
  230. module_fullname[len - 1] = '\0';
  231. /* find syscall in shell symbol section */
  232. struct finsh_syscall* syscall;
  233. for (syscall = _syscall_table_begin; syscall < _syscall_table_end; FINSH_NEXT_SYSCALL(syscall))
  234. {
  235. if (strcmp(syscall->name, module_fullname) == 0)
  236. break;
  237. }
  238. if (syscall < _syscall_table_end)
  239. {
  240. module_init = (module_init_func_t)syscall->func;
  241. *result = module_init();
  242. ret = true;
  243. }
  244. #endif
  245. free(module);
  246. return ret;
  247. }
  248. #ifdef RT_USING_DFS
  249. static jerryx_module_resolver_t load_filesystem_resolver =
  250. {
  251. NULL,
  252. load_module_from_filesystem
  253. };
  254. #endif
  255. static jerryx_module_resolver_t load_builtin_resolver =
  256. {
  257. NULL,
  258. load_module_from_builtin
  259. };
  260. static const jerryx_module_resolver_t *resolvers[] =
  261. {
  262. &load_builtin_resolver,
  263. #ifdef RT_USING_DFS
  264. &load_filesystem_resolver
  265. #endif
  266. };
  267. #ifdef RT_USING_MODULE
  268. typedef jerry_value_t (*FFI_INIT)(void);
  269. static void js_module_info_free(void *native)
  270. {
  271. dlclose(native);
  272. }
  273. static const jerry_object_native_info_t js_module_info =
  274. {
  275. .free_cb = js_module_info_free
  276. };
  277. #endif
  278. DECLARE_HANDLER(require)
  279. {
  280. jerry_value_t result;
  281. char *module = NULL;
  282. if (args_cnt == 0)
  283. {
  284. printf("No module name supplied\n");
  285. return jerry_create_undefined();
  286. }
  287. if (jerry_value_is_string(args[0]) == 0)
  288. {
  289. printf("No module name supplied as string\n");
  290. return jerry_create_undefined();
  291. }
  292. module = js_value_to_string(args[0]);
  293. /* Try each of the resolvers to see if we can find the requested module */
  294. #ifdef RT_USING_MODULE
  295. if (strstr(module, ".so"))
  296. {
  297. void* handle;
  298. FFI_INIT ffi_init;
  299. jerry_value_t info;
  300. do
  301. {
  302. handle = dlopen(module, RTLD_LAZY);
  303. if(!handle)
  304. {
  305. printf("dlopen %s failed!\n", module);
  306. result = jerry_create_undefined();
  307. break;
  308. }
  309. ffi_init = (FFI_INIT)dlsym(handle,"ffi_init");
  310. if(!ffi_init)
  311. {
  312. printf("ffi_init not found!\n");
  313. result = jerry_create_undefined();
  314. dlclose(handle);
  315. break;
  316. }
  317. result = ffi_init();
  318. info = jerry_create_object();
  319. jerry_set_object_native_pointer(info, handle, &js_module_info);
  320. js_set_property(result, "info", info);
  321. jerry_release_value(info);
  322. }
  323. while(0);
  324. }
  325. else
  326. #endif
  327. {
  328. /* make new module.exports */
  329. jerry_value_t global_obj = jerry_get_global_object();
  330. jerry_value_t modules_obj = ECMA_VALUE_UNDEFINED;
  331. jerry_value_t exports_obj = ECMA_VALUE_UNDEFINED;
  332. modules_obj = js_get_property(global_obj, "module");
  333. exports_obj = js_get_property(modules_obj, "exports");
  334. jerry_value_t module_exports_obj = jerry_create_object();
  335. js_set_property(modules_obj, "exports", module_exports_obj);
  336. jerry_release_value(module_exports_obj);
  337. result = jerryx_module_resolve(args[0], resolvers, sizeof(resolvers)/sizeof(resolvers[0]));
  338. if (jerry_value_is_error(result))
  339. {
  340. printf("Couldn't load module %s\n", module);
  341. jerry_release_value(result);
  342. /* create result with error */
  343. result = jerry_create_error(JERRY_ERROR_TYPE,
  344. (const jerry_char_t *) "Module not found");
  345. }
  346. /* restore the parent module.exports */
  347. js_set_property(modules_obj, "exports", exports_obj);
  348. jerry_release_value(global_obj);
  349. jerry_release_value(modules_obj);
  350. jerry_release_value(exports_obj);
  351. }
  352. free(module);
  353. return result;
  354. }
  355. int js_module_init(void)
  356. {
  357. jerry_value_t global_obj = jerry_get_global_object();
  358. jerry_value_t modules_obj = jerry_create_object();
  359. jerry_value_t exports_obj = jerry_create_object();
  360. js_set_property(modules_obj, "exports", exports_obj);
  361. js_set_property(global_obj, "module", modules_obj);
  362. REGISTER_HANDLER(require);
  363. jerry_release_value(global_obj);
  364. jerry_release_value(modules_obj);
  365. jerry_release_value(exports_obj);
  366. return 0;
  367. }