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
16#include <stdio.h>
17#include <stdint.h>
18#include <string.h>
19
20/* Infra */
21#include "infra/bsp.h"
22#include "infra/reboot.h"
23#include "infra/log.h"
24#include "infra/time.h"
25#include "infra/system_events.h"
26#include "infra/tcmd/handler.h"
27
28#include "cfw/cfw.h"
29/* Watchdog helper */
30#include "infra/wdt_helper.h"
31
32#include "jerryscript.h"
33#include "jerryscript-port.h"
34#include "string.h"
35
36#include "zephyr.h"
37#include "microkernel/task.h"
38#include "os/os.h"
39#include "misc/printk.h"
40
41static T_QUEUE queue;
42
43jerry_value_t print_function;
44
45void jerry_resolve_error (jerry_value_t ret_value)
46{
47  if (jerry_value_is_error (ret_value))
48  {
49    ret_value = jerry_get_value_from_error (ret_value, true);
50    jerry_value_t err_str_val = jerry_value_to_string (ret_value);
51    jerry_size_t err_str_size = jerry_get_utf8_string_size (err_str_val);
52    jerry_char_t *err_str_buf = (jerry_char_t *) balloc (err_str_size, NULL);
53    jerry_size_t sz = jerry_string_to_utf8_char_buffer (err_str_val, err_str_buf, err_str_size);
54    err_str_buf[sz] = 0;
55    printk ("Script Error: unhandled exception: %s\n", err_str_buf);
56    bfree(err_str_buf);
57    jerry_release_value (err_str_val);
58  }
59}
60
61void help ()
62{
63  printk ("Usage:\n");
64  printk ("js e 'JavaScript Command'\n");
65  printk ("eg. js e print ('Hello World');\n");
66}
67
68void eval_jerry_script (int argc, char *argv[], struct tcmd_handler_ctx *ctx)
69{
70  if (argc < 3)
71  {
72    TCMD_RSP_ERROR (ctx, NULL);
73    help ();
74    return;
75  }
76  else
77  {
78    OS_ERR_TYPE err;
79    size_t str_total_length = 0;
80    size_t *str_lens = (size_t *) balloc ((argc - 2) * sizeof(size_t), &err);
81    if (str_lens == NULL || err != E_OS_OK)
82    {
83      printk ("%s: allocate memory failed!", __func__);
84      TCMD_RSP_ERROR (ctx, NULL);
85      return;
86    }
87    for (int i = 2; i < argc; ++i)
88    {
89      str_lens[i - 2] = strlen (argv[i]);
90      str_total_length += str_lens[i - 2] + 1;
91    }
92    err = E_OS_OK;
93    char *buffer = (char *) balloc (str_total_length, &err);
94    if (buffer == NULL || err != E_OS_OK)
95    {
96      printk ("%s: allocate memory failed!", __func__);
97      TCMD_RSP_ERROR (ctx, NULL);
98      return;
99    }
100
101    char *p = buffer;
102    for (int i = 2; i < argc; ++i)
103    {
104      for (int j =0; j < str_lens[i - 2]; ++j)
105      {
106        *p = argv[i][j];
107        ++p;
108      }
109      *p = ' ';
110      ++p;
111    }
112    *p = '\0';
113
114    jerry_value_t eval_ret = jerry_eval (buffer, str_total_length - 1, JERRY_PARSE_NO_OPTS);
115
116    if (jerry_value_is_error (eval_ret))
117    {
118      jerry_resolve_error (eval_ret);
119      TCMD_RSP_ERROR (ctx, NULL);
120    }
121    else
122    {
123      jerry_value_t args[] = {eval_ret};
124      jerry_value_t ret_val_print = jerry_call_function (print_function,
125                                                         jerry_create_undefined (),
126                                                         args,
127                                                         1);
128      jerry_release_value (ret_val_print);
129      TCMD_RSP_FINAL (ctx, NULL);
130    }
131    jerry_release_value (eval_ret);
132    bfree (buffer);
133    bfree (str_lens);
134  }
135}
136
137void jerry_start ()
138{
139  union { double d; unsigned u; } now = { .d = jerry_port_get_current_time () };
140  srand (now.u);
141  jerry_init (JERRY_INIT_EMPTY);
142  jerry_value_t global_obj_val = jerry_get_global_object ();
143  jerry_value_t print_func_name_val = jerry_create_string ((jerry_char_t *) "print");
144  print_function = jerry_get_property (global_obj_val, print_func_name_val);
145  jerry_release_value (print_func_name_val);
146  jerry_release_value (global_obj_val);
147}
148
149/* Application main entry point */
150void main_task (void *param)
151{
152  /* Init BSP (also init BSP on ARC core) */
153  queue = bsp_init ();
154  /* start Quark watchdog */
155  wdt_start (WDT_MAX_TIMEOUT_MS);
156  /* Init the CFW */
157  cfw_init (queue);
158  jerry_start ();
159  /* Loop to process message queue */
160  while (1)
161  {
162    OS_ERR_TYPE err = E_OS_OK;
163    /* Process message with a given timeout */
164    queue_process_message_wait (queue, 5000, &err);
165    /* Acknowledge the system watchdog to prevent panic and reset */
166    wdt_keepalive ();
167  }
168}
169
170DECLARE_TEST_COMMAND (js, e, eval_jerry_script);
171