duk_module_duktape.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /*
  2. * Duktape 1.x compatible module loading framework
  3. */
  4. #include "duktape.h"
  5. #include "duk_module_duktape.h"
  6. #if 0 /* Enable manually */
  7. #define DUK__ASSERT(x) do { \
  8. if (!(x)) { \
  9. fprintf(stderr, "ASSERTION FAILED at %s:%d: " #x "\n", __FILE__, __LINE__); \
  10. fflush(stderr); \
  11. } \
  12. } while (0)
  13. #define DUK__ASSERT_TOP(ctx,val) do { \
  14. DUK__ASSERT(duk_get_top((ctx)) == (val)); \
  15. } while (0)
  16. #else
  17. #define DUK__ASSERT(x) do { (void) (x); } while (0)
  18. #define DUK__ASSERT_TOP(ctx,val) do { (void) ctx; (void) (val); } while (0)
  19. #endif
  20. static void duk__resolve_module_id(duk_context *ctx, const char *req_id, const char *mod_id) {
  21. duk_uint8_t buf[DUK_COMMONJS_MODULE_ID_LIMIT];
  22. duk_uint8_t *p;
  23. duk_uint8_t *q;
  24. duk_uint8_t *q_last; /* last component */
  25. duk_int_t int_rc;
  26. DUK__ASSERT(req_id != NULL);
  27. /* mod_id may be NULL */
  28. /*
  29. * A few notes on the algorithm:
  30. *
  31. * - Terms are not allowed to begin with a period unless the term
  32. * is either '.' or '..'. This simplifies implementation (and
  33. * is within CommonJS modules specification).
  34. *
  35. * - There are few output bound checks here. This is on purpose:
  36. * the resolution input is length checked and the output is never
  37. * longer than the input. The resolved output is written directly
  38. * over the input because it's never longer than the input at any
  39. * point in the algorithm.
  40. *
  41. * - Non-ASCII characters are processed as individual bytes and
  42. * need no special treatment. However, U+0000 terminates the
  43. * algorithm; this is not an issue because U+0000 is not a
  44. * desirable term character anyway.
  45. */
  46. /*
  47. * Set up the resolution input which is the requested ID directly
  48. * (if absolute or no current module path) or with current module
  49. * ID prepended (if relative and current module path exists).
  50. *
  51. * Suppose current module is 'foo/bar' and relative path is './quux'.
  52. * The 'bar' component must be replaced so the initial input here is
  53. * 'foo/bar/.././quux'.
  54. */
  55. if (mod_id != NULL && req_id[0] == '.') {
  56. int_rc = snprintf((char *) buf, sizeof(buf), "%s/../%s", mod_id, req_id);
  57. } else {
  58. int_rc = snprintf((char *) buf, sizeof(buf), "%s", req_id);
  59. }
  60. if (int_rc >= (duk_int_t) sizeof(buf) || int_rc < 0) {
  61. /* Potentially truncated, NUL not guaranteed in any case.
  62. * The (int_rc < 0) case should not occur in practice.
  63. */
  64. goto resolve_error;
  65. }
  66. DUK__ASSERT(strlen((const char *) buf) < sizeof(buf)); /* at most sizeof(buf) - 1 */
  67. /*
  68. * Resolution loop. At the top of the loop we're expecting a valid
  69. * term: '.', '..', or a non-empty identifier not starting with a period.
  70. */
  71. p = buf;
  72. q = buf;
  73. for (;;) {
  74. duk_uint_fast8_t c;
  75. /* Here 'p' always points to the start of a term.
  76. *
  77. * We can also unconditionally reset q_last here: if this is
  78. * the last (non-empty) term q_last will have the right value
  79. * on loop exit.
  80. */
  81. DUK__ASSERT(p >= q); /* output is never longer than input during resolution */
  82. q_last = q;
  83. c = *p++;
  84. if (c == 0) {
  85. goto resolve_error;
  86. } else if (c == '.') {
  87. c = *p++;
  88. if (c == '/') {
  89. /* Term was '.' and is eaten entirely (including dup slashes). */
  90. goto eat_dup_slashes;
  91. }
  92. if (c == '.' && *p == '/') {
  93. /* Term was '..', backtrack resolved name by one component.
  94. * q[-1] = previous slash (or beyond start of buffer)
  95. * q[-2] = last char of previous component (or beyond start of buffer)
  96. */
  97. p++; /* eat (first) input slash */
  98. DUK__ASSERT(q >= buf);
  99. if (q == buf) {
  100. goto resolve_error;
  101. }
  102. DUK__ASSERT(*(q - 1) == '/');
  103. q--; /* Backtrack to last output slash (dups already eliminated). */
  104. for (;;) {
  105. /* Backtrack to previous slash or start of buffer. */
  106. DUK__ASSERT(q >= buf);
  107. if (q == buf) {
  108. break;
  109. }
  110. if (*(q - 1) == '/') {
  111. break;
  112. }
  113. q--;
  114. }
  115. goto eat_dup_slashes;
  116. }
  117. goto resolve_error;
  118. } else if (c == '/') {
  119. /* e.g. require('/foo'), empty terms not allowed */
  120. goto resolve_error;
  121. } else {
  122. for (;;) {
  123. /* Copy term name until end or '/'. */
  124. *q++ = c;
  125. c = *p++;
  126. if (c == 0) {
  127. /* This was the last term, and q_last was
  128. * updated to match this term at loop top.
  129. */
  130. goto loop_done;
  131. } else if (c == '/') {
  132. *q++ = '/';
  133. break;
  134. } else {
  135. /* write on next loop */
  136. }
  137. }
  138. }
  139. eat_dup_slashes:
  140. for (;;) {
  141. /* eat dup slashes */
  142. c = *p;
  143. if (c != '/') {
  144. break;
  145. }
  146. p++;
  147. }
  148. }
  149. loop_done:
  150. /* Output #1: resolved absolute name. */
  151. DUK__ASSERT(q >= buf);
  152. duk_push_lstring(ctx, (const char *) buf, (size_t) (q - buf));
  153. /* Output #2: last component name. */
  154. DUK__ASSERT(q >= q_last);
  155. DUK__ASSERT(q_last >= buf);
  156. duk_push_lstring(ctx, (const char *) q_last, (size_t) (q - q_last));
  157. return;
  158. resolve_error:
  159. duk_error(ctx, DUK_ERR_TYPE_ERROR, "cannot resolve module id: %s", (const char *) req_id);
  160. }
  161. /* Stack indices for better readability. */
  162. #define DUK__IDX_REQUESTED_ID 0 /* module id requested */
  163. #define DUK__IDX_REQUIRE 1 /* current require() function */
  164. #define DUK__IDX_REQUIRE_ID 2 /* the base ID of the current require() function, resolution base */
  165. #define DUK__IDX_RESOLVED_ID 3 /* resolved, normalized absolute module ID */
  166. #define DUK__IDX_LASTCOMP 4 /* last component name in resolved path */
  167. #define DUK__IDX_DUKTAPE 5 /* Duktape object */
  168. #define DUK__IDX_MODLOADED 6 /* Duktape.modLoaded[] module cache */
  169. #define DUK__IDX_UNDEFINED 7 /* 'undefined', artifact of lookup */
  170. #define DUK__IDX_FRESH_REQUIRE 8 /* new require() function for module, updated resolution base */
  171. #define DUK__IDX_EXPORTS 9 /* default exports table */
  172. #define DUK__IDX_MODULE 10 /* module object containing module.exports, etc */
  173. static duk_ret_t duk__require(duk_context *ctx) {
  174. const char *str_req_id; /* requested identifier */
  175. const char *str_mod_id; /* require.id of current module */
  176. duk_int_t pcall_rc;
  177. /* NOTE: we try to minimize code size by avoiding unnecessary pops,
  178. * so the stack looks a bit cluttered in this function. DUK__ASSERT_TOP()
  179. * assertions are used to ensure stack configuration is correct at each
  180. * step.
  181. */
  182. /*
  183. * Resolve module identifier into canonical absolute form.
  184. */
  185. str_req_id = duk_require_string(ctx, DUK__IDX_REQUESTED_ID);
  186. duk_push_current_function(ctx);
  187. duk_get_prop_string(ctx, -1, "id");
  188. str_mod_id = duk_get_string(ctx, DUK__IDX_REQUIRE_ID); /* ignore non-strings */
  189. duk__resolve_module_id(ctx, str_req_id, str_mod_id);
  190. str_req_id = NULL;
  191. str_mod_id = NULL;
  192. /* [ requested_id require require.id resolved_id last_comp ] */
  193. DUK__ASSERT_TOP(ctx, DUK__IDX_LASTCOMP + 1);
  194. /*
  195. * Cached module check.
  196. *
  197. * If module has been loaded or its loading has already begun without
  198. * finishing, return the same cached value (module.exports). The
  199. * value is registered when module load starts so that circular
  200. * references can be supported to some extent.
  201. */
  202. duk_push_global_stash(ctx);
  203. duk_get_prop_string(ctx, -1, "\xff" "module:Duktape");
  204. duk_remove(ctx, -2); /* Lookup stashed, original 'Duktape' object. */
  205. duk_get_prop_string(ctx, DUK__IDX_DUKTAPE, "modLoaded"); /* Duktape.modLoaded */
  206. duk_require_type_mask(ctx, DUK__IDX_MODLOADED, DUK_TYPE_MASK_OBJECT);
  207. DUK__ASSERT_TOP(ctx, DUK__IDX_MODLOADED + 1);
  208. duk_dup(ctx, DUK__IDX_RESOLVED_ID);
  209. if (duk_get_prop(ctx, DUK__IDX_MODLOADED)) {
  210. /* [ requested_id require require.id resolved_id last_comp Duktape Duktape.modLoaded Duktape.modLoaded[id] ] */
  211. duk_get_prop_string(ctx, -1, "exports"); /* return module.exports */
  212. return 1;
  213. }
  214. DUK__ASSERT_TOP(ctx, DUK__IDX_UNDEFINED + 1);
  215. /* [ requested_id require require.id resolved_id last_comp Duktape Duktape.modLoaded undefined ] */
  216. /*
  217. * Module not loaded (and loading not started previously).
  218. *
  219. * Create a new require() function with 'id' set to resolved ID
  220. * of module being loaded. Also create 'exports' and 'module'
  221. * tables but don't register exports to the loaded table yet.
  222. * We don't want to do that unless the user module search callbacks
  223. * succeeds in finding the module.
  224. */
  225. /* Fresh require: require.id is left configurable (but not writable)
  226. * so that is not easy to accidentally tweak it, but it can still be
  227. * done with Object.defineProperty().
  228. *
  229. * XXX: require.id could also be just made non-configurable, as there
  230. * is no practical reason to touch it (at least from Ecmascript code).
  231. */
  232. duk_push_c_function(ctx, duk__require, 1 /*nargs*/);
  233. duk_push_string(ctx, "name");
  234. duk_push_string(ctx, "require");
  235. duk_def_prop(ctx, DUK__IDX_FRESH_REQUIRE, DUK_DEFPROP_HAVE_VALUE); /* not writable, not enumerable, not configurable */
  236. duk_push_string(ctx, "id");
  237. duk_dup(ctx, DUK__IDX_RESOLVED_ID);
  238. duk_def_prop(ctx, DUK__IDX_FRESH_REQUIRE, DUK_DEFPROP_HAVE_VALUE | DUK_DEFPROP_SET_CONFIGURABLE); /* a fresh require() with require.id = resolved target module id */
  239. /* Module table:
  240. * - module.exports: initial exports table (may be replaced by user)
  241. * - module.id is non-writable and non-configurable, as the CommonJS
  242. * spec suggests this if possible
  243. * - module.filename: not set, defaults to resolved ID if not explicitly
  244. * set by modSearch() (note capitalization, not .fileName, matches Node.js)
  245. * - module.name: not set, defaults to last component of resolved ID if
  246. * not explicitly set by modSearch()
  247. */
  248. duk_push_object(ctx); /* exports */
  249. duk_push_object(ctx); /* module */
  250. duk_push_string(ctx, "exports");
  251. duk_dup(ctx, DUK__IDX_EXPORTS);
  252. duk_def_prop(ctx, DUK__IDX_MODULE, DUK_DEFPROP_HAVE_VALUE | DUK_DEFPROP_SET_WRITABLE | DUK_DEFPROP_SET_CONFIGURABLE); /* module.exports = exports */
  253. duk_push_string(ctx, "id");
  254. duk_dup(ctx, DUK__IDX_RESOLVED_ID); /* resolved id: require(id) must return this same module */
  255. duk_def_prop(ctx, DUK__IDX_MODULE, DUK_DEFPROP_HAVE_VALUE); /* module.id = resolved_id; not writable, not enumerable, not configurable */
  256. duk_compact(ctx, DUK__IDX_MODULE); /* module table remains registered to modLoaded, minimize its size */
  257. DUK__ASSERT_TOP(ctx, DUK__IDX_MODULE + 1);
  258. /* [ requested_id require require.id resolved_id last_comp Duktape Duktape.modLoaded undefined fresh_require exports module ] */
  259. /* Register the module table early to modLoaded[] so that we can
  260. * support circular references even in modSearch(). If an error
  261. * is thrown, we'll delete the reference.
  262. */
  263. duk_dup(ctx, DUK__IDX_RESOLVED_ID);
  264. duk_dup(ctx, DUK__IDX_MODULE);
  265. duk_put_prop(ctx, DUK__IDX_MODLOADED); /* Duktape.modLoaded[resolved_id] = module */
  266. /*
  267. * Call user provided module search function and build the wrapped
  268. * module source code (if necessary). The module search function
  269. * can be used to implement pure Ecmacsript, pure C, and mixed
  270. * Ecmascript/C modules.
  271. *
  272. * The module search function can operate on the exports table directly
  273. * (e.g. DLL code can register values to it). It can also return a
  274. * string which is interpreted as module source code (if a non-string
  275. * is returned the module is assumed to be a pure C one). If a module
  276. * cannot be found, an error must be thrown by the user callback.
  277. *
  278. * Because Duktape.modLoaded[] already contains the module being
  279. * loaded, circular references for C modules should also work
  280. * (although expected to be quite rare).
  281. */
  282. duk_push_string(ctx, "(function(require,exports,module){");
  283. /* Duktape.modSearch(resolved_id, fresh_require, exports, module). */
  284. duk_get_prop_string(ctx, DUK__IDX_DUKTAPE, "modSearch"); /* Duktape.modSearch */
  285. duk_dup(ctx, DUK__IDX_RESOLVED_ID);
  286. duk_dup(ctx, DUK__IDX_FRESH_REQUIRE);
  287. duk_dup(ctx, DUK__IDX_EXPORTS);
  288. duk_dup(ctx, DUK__IDX_MODULE); /* [ ... Duktape.modSearch resolved_id last_comp fresh_require exports module ] */
  289. pcall_rc = duk_pcall(ctx, 4 /*nargs*/); /* -> [ ... source ] */
  290. DUK__ASSERT_TOP(ctx, DUK__IDX_MODULE + 3);
  291. if (pcall_rc != DUK_EXEC_SUCCESS) {
  292. /* Delete entry in Duktape.modLoaded[] and rethrow. */
  293. goto delete_rethrow;
  294. }
  295. /* If user callback did not return source code, module loading
  296. * is finished (user callback initialized exports table directly).
  297. */
  298. if (!duk_is_string(ctx, -1)) {
  299. /* User callback did not return source code, so module loading
  300. * is finished: just update modLoaded with final module.exports
  301. * and we're done.
  302. */
  303. goto return_exports;
  304. }
  305. /* Finish the wrapped module source. Force module.filename as the
  306. * function .fileName so it gets set for functions defined within a
  307. * module. This also ensures loggers created within the module get
  308. * the module ID (or overridden filename) as their default logger name.
  309. * (Note capitalization: .filename matches Node.js while .fileName is
  310. * used elsewhere in Duktape.)
  311. */
  312. duk_push_string(ctx, "})");
  313. duk_concat(ctx, 3);
  314. if (!duk_get_prop_string(ctx, DUK__IDX_MODULE, "filename")) {
  315. /* module.filename for .fileName, default to resolved ID if
  316. * not present.
  317. */
  318. duk_pop(ctx);
  319. duk_dup(ctx, DUK__IDX_RESOLVED_ID);
  320. }
  321. pcall_rc = duk_pcompile(ctx, DUK_COMPILE_EVAL);
  322. if (pcall_rc != DUK_EXEC_SUCCESS) {
  323. goto delete_rethrow;
  324. }
  325. pcall_rc = duk_pcall(ctx, 0); /* -> eval'd function wrapper (not called yet) */
  326. if (pcall_rc != DUK_EXEC_SUCCESS) {
  327. goto delete_rethrow;
  328. }
  329. /* Module has now evaluated to a wrapped module function. Force its
  330. * .name to match module.name (defaults to last component of resolved
  331. * ID) so that it is shown in stack traces too. Note that we must not
  332. * introduce an actual name binding into the function scope (which is
  333. * usually the case with a named function) because it would affect the
  334. * scope seen by the module and shadow accesses to globals of the same name.
  335. * This is now done by compiling the function as anonymous and then forcing
  336. * its .name without setting a "has name binding" flag.
  337. */
  338. duk_push_string(ctx, "name");
  339. if (!duk_get_prop_string(ctx, DUK__IDX_MODULE, "name")) {
  340. /* module.name for .name, default to last component if
  341. * not present.
  342. */
  343. duk_pop(ctx);
  344. duk_dup(ctx, DUK__IDX_LASTCOMP);
  345. }
  346. duk_def_prop(ctx, -3, DUK_DEFPROP_HAVE_VALUE | DUK_DEFPROP_FORCE);
  347. /*
  348. * Call the wrapped module function.
  349. *
  350. * Use a protected call so that we can update Duktape.modLoaded[resolved_id]
  351. * even if the module throws an error.
  352. */
  353. /* [ requested_id require require.id resolved_id last_comp Duktape Duktape.modLoaded undefined fresh_require exports module mod_func ] */
  354. DUK__ASSERT_TOP(ctx, DUK__IDX_MODULE + 2);
  355. duk_dup(ctx, DUK__IDX_EXPORTS); /* exports (this binding) */
  356. duk_dup(ctx, DUK__IDX_FRESH_REQUIRE); /* fresh require (argument) */
  357. duk_get_prop_string(ctx, DUK__IDX_MODULE, "exports"); /* relookup exports from module.exports in case it was changed by modSearch */
  358. duk_dup(ctx, DUK__IDX_MODULE); /* module (argument) */
  359. DUK__ASSERT_TOP(ctx, DUK__IDX_MODULE + 6);
  360. /* [ requested_id require require.id resolved_id last_comp Duktape Duktape.modLoaded undefined fresh_require exports module mod_func exports fresh_require exports module ] */
  361. pcall_rc = duk_pcall_method(ctx, 3 /*nargs*/);
  362. if (pcall_rc != DUK_EXEC_SUCCESS) {
  363. /* Module loading failed. Node.js will forget the module
  364. * registration so that another require() will try to load
  365. * the module again. Mimic that behavior.
  366. */
  367. goto delete_rethrow;
  368. }
  369. /* [ requested_id require require.id resolved_id last_comp Duktape Duktape.modLoaded undefined fresh_require exports module result(ignored) ] */
  370. DUK__ASSERT_TOP(ctx, DUK__IDX_MODULE + 2);
  371. /* fall through */
  372. return_exports:
  373. duk_get_prop_string(ctx, DUK__IDX_MODULE, "exports");
  374. duk_compact(ctx, -1); /* compact the exports table */
  375. return 1; /* return module.exports */
  376. delete_rethrow:
  377. duk_dup(ctx, DUK__IDX_RESOLVED_ID);
  378. duk_del_prop(ctx, DUK__IDX_MODLOADED); /* delete Duktape.modLoaded[resolved_id] */
  379. duk_throw(ctx); /* rethrow original error */
  380. return 0; /* not reachable */
  381. }
  382. void duk_module_duktape_init(duk_context *ctx) {
  383. /* Stash 'Duktape' in case it's modified. */
  384. duk_push_global_stash(ctx);
  385. duk_get_global_string(ctx, "Duktape");
  386. duk_put_prop_string(ctx, -2, "\xff" "module:Duktape");
  387. duk_pop(ctx);
  388. /* Register `require` as a global function. */
  389. duk_eval_string(ctx,
  390. "(function(req){"
  391. "var D=Object.defineProperty;"
  392. "D(req,'name',{value:'require'});"
  393. "D(this,'require',{value:req,writable:true,configurable:true});"
  394. "D(Duktape,'modLoaded',{value:Object.create(null),writable:true,configurable:true});"
  395. "})");
  396. duk_push_c_function(ctx, duk__require, 1 /*nargs*/);
  397. duk_call(ctx, 1);
  398. duk_pop(ctx);
  399. }
  400. #undef DUK__ASSERT
  401. #undef DUK__ASSERT_TOP
  402. #undef DUK__IDX_REQUESTED_ID
  403. #undef DUK__IDX_REQUIRE
  404. #undef DUK__IDX_REQUIRE_ID
  405. #undef DUK__IDX_RESOLVED_ID
  406. #undef DUK__IDX_LASTCOMP
  407. #undef DUK__IDX_DUKTAPE
  408. #undef DUK__IDX_MODLOADED
  409. #undef DUK__IDX_UNDEFINED
  410. #undef DUK__IDX_FRESH_REQUIRE
  411. #undef DUK__IDX_EXPORTS
  412. #undef DUK__IDX_MODULE