jerry_main.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /* Copyright JS Foundation and other contributors, http://js.foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <stdlib.h>
  18. #include "jerry-api.h"
  19. #include "jerry-port.h"
  20. #include <rtthread.h>
  21. #include <finsh.h>
  22. /**
  23. * Maximum command line arguments number.
  24. */
  25. #define JERRY_MAX_COMMAND_LINE_ARGS (16)
  26. /**
  27. * Standalone Jerry exit codes.
  28. */
  29. #define JERRY_STANDALONE_EXIT_CODE_OK (0)
  30. #define JERRY_STANDALONE_EXIT_CODE_FAIL (1)
  31. /**
  32. * Read source files.
  33. *
  34. * @return concatenated source files
  35. */
  36. static char* read_sources(const char *script_file_names[], int files_count, size_t *out_source_size_p)
  37. {
  38. int i;
  39. char* source_buffer = NULL;
  40. char *source_buffer_tail = NULL;
  41. size_t total_length = 0;
  42. FILE *file = NULL;
  43. for (i = 0; i < files_count; i++)
  44. {
  45. const char *script_file_name = script_file_names[i];
  46. file = fopen(script_file_name, "r");
  47. if (file == NULL) {
  48. jerry_port_log(JERRY_LOG_LEVEL_ERROR, "Failed to fopen [%s]\n",
  49. script_file_name);
  50. return NULL;
  51. }
  52. int fseek_status = fseek(file, 0, SEEK_END);
  53. if (fseek_status != 0)
  54. {
  55. jerry_port_log(JERRY_LOG_LEVEL_ERROR,
  56. "Failed to fseek fseek_status(%d)\n", fseek_status);
  57. fclose(file);
  58. return NULL;
  59. }
  60. long script_len = ftell(file);
  61. if (script_len < 0)
  62. {
  63. jerry_port_log(JERRY_LOG_LEVEL_ERROR,
  64. "Failed to ftell script_len(%ld)\n", script_len);
  65. fclose(file);
  66. break;
  67. }
  68. total_length += (size_t) script_len;
  69. fclose(file);
  70. file = NULL;
  71. }
  72. if (total_length <= 0)
  73. {
  74. jerry_port_log(JERRY_LOG_LEVEL_ERROR, "There's nothing to read\n");
  75. return NULL;
  76. }
  77. source_buffer = (char*) rt_malloc(total_length);
  78. if (source_buffer == NULL)
  79. {
  80. jerry_port_log(JERRY_LOG_LEVEL_ERROR, "Out of memory error\n");
  81. return NULL;
  82. }
  83. memset(source_buffer, 0, sizeof(char) * total_length);
  84. source_buffer_tail = source_buffer;
  85. for (i = 0; i < files_count; i++)
  86. {
  87. const char *script_file_name = script_file_names[i];
  88. file = fopen(script_file_name, "r");
  89. if (file == NULL)
  90. {
  91. jerry_port_log(JERRY_LOG_LEVEL_ERROR, "Failed to fopen [%s]\n",
  92. script_file_name);
  93. break;
  94. }
  95. int fseek_status = fseek(file, 0, SEEK_END);
  96. if (fseek_status != 0)
  97. {
  98. jerry_port_log(JERRY_LOG_LEVEL_ERROR,
  99. "Failed to fseek fseek_status(%d)\n", fseek_status);
  100. break;
  101. }
  102. long script_len = ftell(file);
  103. if (script_len < 0)
  104. {
  105. jerry_port_log(JERRY_LOG_LEVEL_ERROR,
  106. "Failed to ftell script_len(%ld)\n", script_len);
  107. break;
  108. }
  109. rewind(file);
  110. const size_t current_source_size = (size_t) script_len;
  111. size_t bytes_read = fread(source_buffer_tail, 1, current_source_size,
  112. file);
  113. if (bytes_read < current_source_size)
  114. {
  115. jerry_port_log(JERRY_LOG_LEVEL_ERROR,
  116. "Failed to fread bytes_read(%d)\n", bytes_read);
  117. break;
  118. }
  119. fclose(file);
  120. file = NULL;
  121. source_buffer_tail += current_source_size;
  122. }
  123. if (file != NULL)
  124. {
  125. fclose(file);
  126. }
  127. if (i < files_count)
  128. {
  129. jerry_port_log(JERRY_LOG_LEVEL_ERROR, "Failed to read script N%d\n",
  130. i + 1);
  131. rt_free(source_buffer);
  132. return NULL;
  133. }
  134. *out_source_size_p = (size_t) total_length;
  135. return source_buffer;
  136. } /* read_sources */
  137. /**
  138. * JerryScript log level
  139. */
  140. static jerry_log_level_t jerry_log_level = JERRY_LOG_LEVEL_ERROR;
  141. /**
  142. * Main program.
  143. *
  144. * @return 0 if success, error code otherwise
  145. */
  146. int jerry_main(int argc, char *argv[])
  147. {
  148. if (argc > JERRY_MAX_COMMAND_LINE_ARGS)
  149. {
  150. jerry_port_log(JERRY_LOG_LEVEL_ERROR,
  151. "Too many command line arguments. Current maximum is %d\n",
  152. JERRY_MAX_COMMAND_LINE_ARGS);
  153. return JERRY_STANDALONE_EXIT_CODE_FAIL;
  154. }
  155. const char *file_names[JERRY_MAX_COMMAND_LINE_ARGS];
  156. int i;
  157. int files_counter = 0;
  158. jerry_init_flag_t flags = JERRY_INIT_EMPTY;
  159. for (i = 1; i < argc; i++)
  160. {
  161. if (!strcmp("--mem-stats", argv[i]))
  162. {
  163. flags |= JERRY_INIT_MEM_STATS;
  164. }
  165. else if (!strcmp("--mem-stats-separate", argv[i]))
  166. {
  167. flags |= JERRY_INIT_MEM_STATS_SEPARATE;
  168. }
  169. else if (!strcmp("--show-opcodes", argv[i]))
  170. {
  171. flags |= JERRY_INIT_SHOW_OPCODES | JERRY_INIT_SHOW_REGEXP_OPCODES;
  172. }
  173. else if (!strcmp("--log-level", argv[i]))
  174. {
  175. if (++i < argc && strlen(argv[i]) == 1 && argv[i][0] >= '0'
  176. && argv[i][0] <= '3')
  177. {
  178. jerry_log_level = argv[i][0] - '0';
  179. }
  180. else
  181. {
  182. jerry_port_log(JERRY_LOG_LEVEL_ERROR,
  183. "Error: wrong format or invalid argument\n");
  184. return JERRY_STANDALONE_EXIT_CODE_FAIL;
  185. }
  186. }
  187. else
  188. {
  189. file_names[files_counter++] = argv[i];
  190. }
  191. }
  192. if (files_counter == 0)
  193. {
  194. jerry_port_console("No input files, running a hello world demo:\n");
  195. char *source_p =
  196. "var a = 3.5; print('Hello world ' + (a + 1.5) + ' times from JerryScript')";
  197. jerry_run_simple((jerry_char_t *) source_p, strlen(source_p), flags);
  198. return 0;
  199. }
  200. size_t source_size;
  201. char *source_p = read_sources(file_names, files_counter, &source_size);
  202. if (source_p == NULL)
  203. {
  204. jerry_port_log(JERRY_LOG_LEVEL_ERROR,
  205. "JERRY_STANDALONE_EXIT_CODE_FAIL\n");
  206. return JERRY_STANDALONE_EXIT_CODE_FAIL;
  207. }
  208. bool success = jerry_run_simple((jerry_char_t *) source_p, source_size, flags);
  209. rt_free(source_p);
  210. if (!success)
  211. {
  212. return JERRY_STANDALONE_EXIT_CODE_FAIL;
  213. }
  214. return JERRY_STANDALONE_EXIT_CODE_OK;
  215. } /* main */
  216. MSH_CMD_EXPORT(jerry_main, jerryScript Demo);
  217. /* JerryScript CMD Line Interpreter */
  218. #define DECLARE_HANDLER(NAME) \
  219. static jerry_value_t \
  220. NAME ## _handler (const jerry_value_t func_value, \
  221. const jerry_value_t this_value, \
  222. const jerry_value_t args[], \
  223. const jerry_length_t args_cnt )
  224. #define REGISTER_HANDLER(NAME) \
  225. register_native_function ( # NAME, NAME ## _handler)
  226. DECLARE_HANDLER(assert)
  227. {
  228. if (args_cnt == 1 && jerry_value_is_boolean(args[0]) && jerry_get_boolean_value(args[0]))
  229. {
  230. jerry_port_log(JERRY_LOG_LEVEL_DEBUG, ">> Jerry assert true\r\n");
  231. return jerry_create_boolean(true);
  232. }
  233. jerry_port_log(JERRY_LOG_LEVEL_ERROR, "ERROR: Script assertion failed\n");
  234. return jerry_create_boolean(false);
  235. }
  236. static bool register_native_function(const char* name, jerry_external_handler_t handler)
  237. {
  238. jerry_value_t global_object_val = jerry_get_global_object();
  239. jerry_value_t reg_function = jerry_create_external_function(handler);
  240. bool is_ok = true;
  241. if (!(jerry_value_is_function(reg_function) && jerry_value_is_constructor(reg_function)))
  242. {
  243. is_ok = false;
  244. jerry_port_log(JERRY_LOG_LEVEL_ERROR,
  245. "Error: create_external_function failed !!!\r\n");
  246. jerry_release_value(global_object_val);
  247. jerry_release_value(reg_function);
  248. return is_ok;
  249. }
  250. if (jerry_value_has_error_flag(reg_function))
  251. {
  252. is_ok = false;
  253. jerry_port_log(JERRY_LOG_LEVEL_ERROR,
  254. "Error: create_external_function has error flag! \n\r");
  255. jerry_release_value(global_object_val);
  256. jerry_release_value(reg_function);
  257. return is_ok;
  258. }
  259. jerry_value_t jerry_name = jerry_create_string((jerry_char_t *) name);
  260. jerry_value_t set_result = jerry_set_property(global_object_val, jerry_name, reg_function);
  261. if (jerry_value_has_error_flag(set_result))
  262. {
  263. is_ok = false;
  264. jerry_port_log(JERRY_LOG_LEVEL_ERROR,
  265. "Error: register_native_function failed: [%s]\r\n", name);
  266. }
  267. jerry_release_value(jerry_name);
  268. jerry_release_value(global_object_val);
  269. jerry_release_value(reg_function);
  270. jerry_release_value(set_result);
  271. return is_ok;
  272. }
  273. void js_register_functions(void)
  274. {
  275. REGISTER_HANDLER(assert);
  276. }
  277. static void _jerry_thread(void* parameter)
  278. {
  279. char *source_p;
  280. size_t source_size;
  281. jerry_init(JERRY_INIT_EMPTY);
  282. js_register_functions();
  283. while (1)
  284. {
  285. #if 0
  286. jerry_value_t ret_val = jerry_eval((jerry_char_t *) source_p, source_size, false);
  287. if (jerry_value_has_error_flag(ret_val))
  288. {
  289. jerry_port_log(JERRY_LOG_LEVEL_ERROR, "Error: jerry_eval failed!\r\n");
  290. }
  291. jerry_release_value(ret_val);
  292. #else
  293. jerry_port_log(JERRY_LOG_LEVEL_ERROR, "Error: Jerryscript CMD Line Interpreter Not Implementation\r\n");
  294. rt_thread_delay(RT_TICK_PER_SECOND);
  295. #endif
  296. }
  297. }
  298. int js_run(int argc, char *argv[])
  299. {
  300. rt_thread_t thread;
  301. thread = rt_thread_create("jsth", _jerry_thread, RT_NULL, 2048, RT_THREAD_PRIORITY_MAX / 3, 20);
  302. if (thread != RT_NULL)
  303. {
  304. rt_thread_startup(thread);
  305. return 0;
  306. }
  307. else
  308. {
  309. rt_kprintf("JerryScript Thread Create Failure\n");
  310. return -1;
  311. }
  312. }
  313. // MSH_CMD_EXPORT(js_run, JerryScript Cmd Interpreter);