aboutsummaryrefslogtreecommitdiff
path: root/targets/os/zephyr/src/main-zephyr.c
blob: db81a3c2a567b5eb96267cfc54ec8e32fbe1d9f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/* Copyright JS Foundation and other contributors, http://js.foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <zephyr.h>

#include "jerryscript-port.h"
#include "jerryscript.h"

#include "getline-zephyr.h"
#include "jerryscript-ext/handler.h"
#include <sys/printk.h>

static jerry_value_t print_function;

/**
 * Register a JavaScript function in the global object.
 */
static void
register_js_function (const char *name_p, /**< name of the function */
                      jerry_external_handler_t handler_p) /**< function callback */
{
  jerry_value_t result_val = jerryx_handler_register_global (name_p, handler_p);

  if (jerry_value_is_exception (result_val))
  {
    jerry_port_log (JERRY_LOG_LEVEL_WARNING, "Warning: failed to register '%s' method.", name_p);
  }

  jerry_value_free (result_val);
} /* register_js_function */

static int
shell_cmd_handler (char *source_buffer)
{
  jerry_value_t ret_val;

  ret_val = jerry_eval ((jerry_char_t *) source_buffer, strlen (source_buffer), JERRY_PARSE_NO_OPTS);

  if (jerry_value_is_exception (ret_val))
  {
    /* User-friendly error messages require at least "cp" JerryScript
       profile. Include a message prefix in case "cp_minimal" profile
       is used. */
    printf ("Error executing statement: ");
    /* Clear error flag, otherwise print call below won't produce any
       output. */
    ret_val = jerry_exception_value (ret_val, true);
  }

  if (!jerry_value_is_exception (print_function))
  {
    jerry_value_t ret_val_print = jerry_call (print_function, jerry_undefined (), &ret_val, 1);
    jerry_value_free (ret_val_print);
  }

  jerry_value_free (ret_val);

  return 0;
} /* shell_cmd_handler */

void
main (void)
{
  union
  {
    double d;
    unsigned u;
  } now = { .d = jerry_port_get_current_time () };
  srand (now.u);
  uint32_t zephyr_ver = sys_kernel_version_get ();
  printf ("JerryScript build: " __DATE__ " " __TIME__ "\n");
  printf ("JerryScript API %d.%d.%d\n", JERRY_API_MAJOR_VERSION, JERRY_API_MINOR_VERSION, JERRY_API_PATCH_VERSION);
  printf ("Zephyr version %d.%d.%d\n",
          (int) SYS_KERNEL_VER_MAJOR (zephyr_ver),
          (int) SYS_KERNEL_VER_MINOR (zephyr_ver),
          (int) SYS_KERNEL_VER_PATCHLEVEL (zephyr_ver));

  zephyr_getline_init ();
  jerry_init (JERRY_INIT_EMPTY);
  register_js_function ("print", jerryx_handler_print);
  jerry_value_t global_obj_val = jerry_current_realm ();

  jerry_value_t print_func_name_val = jerry_string_sz ("print");
  print_function = jerry_object_get (global_obj_val, print_func_name_val);
  jerry_value_free (print_func_name_val);
  jerry_value_free (global_obj_val);
  if (jerry_value_is_exception (print_function))
  {
    printf ("Error: could not look up print function, expression results won't be printed\n");
  }

  while (1)
  {
    char *s;
    printf ("js> ");
    fflush (stdout);
    s = zephyr_getline ();
    if (*s)
    {
      shell_cmd_handler (s);
    }
  }

  /* As we never retturn from REPL above, don't call jerry_cleanup() here. */
} /* main */