module.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Module search function
  2. // Copyright (C) 2015 Legimet
  3. //
  4. // This file is part of Duktape-nspire.
  5. //
  6. // Duktape-nspire is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU Lesser General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // Duktape-nspire is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Lesser General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Lesser General Public License
  17. // along with Duktape-nspire. If not, see <http://www.gnu.org/licenses/>.
  18. #include "duktape.h"
  19. #include "module.h"
  20. #include "misc.h"
  21. #include <stdbool.h>
  22. #include <stdio.h>
  23. #include <limits.h>
  24. #include <sys/types.h>
  25. #include <sys/stat.h>
  26. #include <unistd.h>
  27. duk_ret_t cb_resolve_module(duk_context *ctx) {
  28. /*
  29. * Entry stack: [ requested_id parent_id ]
  30. */
  31. static const char * start[] = {"/", "./", "../"};
  32. static const char * ext[] = {".tns", ".js.tns", ".json.tns",
  33. "/index.js.tns", "/index.json.tns"};
  34. const char *requested_id = duk_get_string(ctx, 0);
  35. const char *parent_id = duk_get_string(ctx, 1); /* calling module */
  36. char resolved_id[PATH_MAX];
  37. // Check if it is a built-in C module
  38. for (unsigned i = 0; i < c_module_count; ++i) {
  39. if (!strcmp(c_module_list[i].name, requested_id)) {
  40. strcpy(resolved_id, requested_id);
  41. goto success;
  42. }
  43. }
  44. // External modules
  45. bool valid = false;
  46. for (unsigned i = 0; i < sizeof(start) / sizeof(start[0]); ++i) {
  47. if (!strncmp(requested_id, start[i], strlen(start[i]))) {
  48. valid = true;
  49. break;
  50. }
  51. }
  52. if (!valid) goto failure;
  53. char path[PATH_MAX];
  54. char *cur;
  55. struct stat stbuf;
  56. if (requested_id[0] == '/') { // Absolute path
  57. path[0] = '\0';
  58. cur = path;
  59. } else { // Relative path
  60. strcpy(path, parent_id);
  61. cur = strrchr(path, '/'); // Get rid of basename
  62. if (cur) ++cur;
  63. else cur = path;
  64. }
  65. size_t remaining = PATH_MAX - (cur - path);
  66. size_t copied;
  67. if ((copied = strlcpy(cur, requested_id, remaining)) >= remaining) goto failure;
  68. remaining -= copied;
  69. cur += copied;
  70. // Check each of the extensions in order
  71. for (unsigned i = 0; i < sizeof(ext) / sizeof(ext[0]); ++i) {
  72. if (strlcpy(cur, ext[i], remaining) >= remaining)
  73. goto failure;
  74. if (!stat(path, &stbuf) && S_ISREG(stbuf.st_mode)) {
  75. if (realpath(path, resolved_id))
  76. goto success;
  77. }
  78. }
  79. failure:
  80. return duk_generic_error(ctx, "Could not find module '%s'", requested_id);
  81. success:
  82. duk_push_string(ctx, resolved_id);
  83. return 1; /*nrets*/
  84. }
  85. duk_ret_t cb_load_module(duk_context *ctx) {
  86. /*
  87. * Entry stack: [ resolved_id exports module ]
  88. */
  89. const char *id = duk_get_string(ctx, 0); // Get the resolved id
  90. // C modules have ids that don't start with /
  91. if (id[0] != '/') {
  92. for (unsigned i = 0; i < c_module_count; i++) {
  93. if (!strcmp(c_module_list[i].name, id)) {
  94. // Call init function, enumerate properties of returned object,
  95. // and put them in exports
  96. c_module_list[i].init_func(ctx);
  97. duk_enum(ctx, -1, 0);
  98. while (duk_next(ctx, -1, 1)) {
  99. duk_put_prop(ctx, 1);
  100. }
  101. duk_pop_2(ctx);
  102. return 0;
  103. }
  104. }
  105. // We shouldn't reach here!
  106. return duk_error(ctx, DUK_ERR_ERROR, "Could not find module '%s'", id);
  107. } else { // Otherwise, it's in an external file
  108. push_file_contents(ctx, id);
  109. size_t len = strlen(id);
  110. size_t jsonext_len = strlen(".json.tns");
  111. if (len >= jsonext_len && !strcmp(".json.tns", id + len - jsonext_len)) {
  112. // Decode JSON file and place in exports
  113. duk_json_decode(ctx, -1);
  114. duk_enum(ctx, -1, 0);
  115. while (duk_next(ctx, -1, 1)) {
  116. duk_put_prop(ctx, 1);
  117. }
  118. duk_pop_2(ctx);
  119. return 0;
  120. } else { // A regular JS file
  121. return 1;
  122. }
  123. }
  124. }