main.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Main 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 <stdio.h>
  21. #include <stdlib.h>
  22. #include <stdbool.h>
  23. #include <libndls.h>
  24. duk_context *ctx;
  25. // Push stack of Error object at -1, remove the Error object
  26. duk_ret_t get_error_stack(duk_context *ctx) {
  27. if (duk_is_error(ctx, -1) && duk_has_prop_string(ctx, -1, "stack")) {
  28. duk_get_prop_string(ctx, -1, "stack");
  29. duk_remove(ctx, -2);
  30. }
  31. return 1;
  32. }
  33. // Print stack of Error, remove the Error object
  34. void print_pop_error(void) {
  35. duk_safe_call(ctx, get_error_stack, 1, 1);
  36. fprintf(stderr, "%s\n", duk_safe_to_string(ctx, -1));
  37. duk_pop(ctx);
  38. }
  39. // Run JS file
  40. int handle_file(char *path) {
  41. if (duk_pcompile_file(ctx, 0, path)) {
  42. return 1;
  43. }
  44. duk_push_global_object(ctx);
  45. if (duk_pcall_method(ctx, 0)) {
  46. return 1;
  47. }
  48. return 0;
  49. }
  50. // REPL
  51. int handle_repl(void) {
  52. char input[256];
  53. puts("Duktape-nspire " VERSION "\nBuilt on " BUILD_DATE ", using Duktape " DUK_GIT_DESCRIBE);
  54. while (true) {
  55. printf("> ");
  56. fflush(stdout);
  57. if (!fgets(input, 256, stdin)) {
  58. if (ferror(stdin)) {
  59. fprintf(stderr, "unable to read input\n");
  60. return 1;
  61. } else {
  62. return 0;
  63. }
  64. }
  65. if (!strcmp(input, ".exit\n")) {
  66. return 0;
  67. }
  68. if (duk_pcompile_string(ctx, 0, input)) {
  69. print_pop_error();
  70. continue;
  71. }
  72. duk_push_global_object(ctx);
  73. if (duk_pcall_method(ctx, 0)) {
  74. print_pop_error();
  75. continue;
  76. }
  77. puts(duk_safe_to_string(ctx, -1));
  78. }
  79. return 0;
  80. }
  81. // Cleanup at end
  82. void cleanup(void) {
  83. duk_destroy_heap(ctx);
  84. }
  85. int main(int argc, char **argv) {
  86. enable_relative_paths(argv); // Enable relative paths
  87. // Register .js file extension
  88. if (argc > 0) {
  89. char *ptr = strrchr(argv[0], '/');
  90. if (ptr++) {
  91. char *dot = strrchr(ptr, '.');
  92. if (dot && dot != ptr && !strcmp(dot, ".tns")) {
  93. *dot = '\0';
  94. cfg_register_fileext("js", ptr);
  95. *dot = '.';
  96. }
  97. }
  98. }
  99. // Initialize Duktape heap and register cleanup with atexit
  100. ctx = duk_create_heap_default();
  101. if (atexit(cleanup)) {
  102. cleanup();
  103. return EXIT_FAILURE;
  104. }
  105. // Add modSearch function for module loading support
  106. duk_push_global_object(ctx);
  107. duk_get_prop_string(ctx, -1, "Duktape");
  108. duk_push_c_function(ctx, module_search, 4);
  109. duk_put_prop_string(ctx, -2, "modSearch");
  110. duk_pop_2(ctx);
  111. if (argc <= 1) {
  112. if (handle_repl()) {
  113. wait_key_pressed();
  114. return EXIT_FAILURE;
  115. }
  116. } else {
  117. for (int i = 1; i < argc; i++) {
  118. duk_push_string(ctx, argv[i]);
  119. if (handle_file(argv[i])) {
  120. print_pop_error();
  121. wait_key_pressed();
  122. return EXIT_FAILURE;
  123. }
  124. }
  125. }
  126. return 0;
  127. }