main-zephyr.c 3.5 KB
Newer Older
1
/* Copyright JS Foundation and other contributors, http://js.foundation
2 3 4 5 6
 *
 * 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
 *
A
Akos Kiss 已提交
7
 *     http://www.apache.org/licenses/LICENSE-2.0
8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * 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 <string.h>
#include <stdlib.h>

#include <zephyr.h>
#include <misc/printk.h>
22
#include "getline-zephyr.h"
23

24
#include "jerryscript.h"
25 26
#include "jerryscript-port.h"
#include "jerryscript-ext/handler.h"
27

28
static jerry_value_t print_function;
29

30 31 32 33 34 35 36 37 38
/**
 * 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 ((const jerry_char_t *) name_p, handler_p);

39
  if (jerry_value_is_error (result_val))
40 41 42 43 44 45 46
  {
    jerry_port_log (JERRY_LOG_LEVEL_WARNING, "Warning: failed to register '%s' method.", name_p);
  }

  jerry_release_value (result_val);
} /* register_js_function */

47
static int shell_cmd_handler (char *source_buffer)
48
{
49
  jerry_value_t ret_val;
50

51
  ret_val = jerry_eval ((jerry_char_t *) source_buffer,
52
    strlen (source_buffer),
53
    JERRY_PARSE_NO_OPTS);
54

55
  if (jerry_value_is_error (ret_val))
56
  {
57 58 59 60 61 62
    /* 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. */
63
    ret_val = jerry_get_value_from_error (ret_val, true);
64 65
  }

66
  if (!jerry_value_is_error (print_function))
67 68 69 70 71 72
  {
    jerry_value_t ret_val_print = jerry_call_function (print_function,
      jerry_create_undefined (),
      &ret_val,
      1);
    jerry_release_value (ret_val_print);
73 74
  }

75 76
  jerry_release_value (ret_val);

77 78 79 80 81
  return 0;
} /* shell_cmd_handler */

void main (void)
{
82
  srand ((unsigned) jerry_port_get_current_time ());
83 84
  uint32_t zephyr_ver = sys_kernel_version_get ();
  printf ("JerryScript build: " __DATE__ " " __TIME__ "\n");
85
  printf ("JerryScript API %d.%d.%d\n", JERRY_API_MAJOR_VERSION, JERRY_API_MINOR_VERSION, JERRY_API_PATCH_VERSION);
86 87 88 89 90
  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 ();
91
  jerry_init (JERRY_INIT_EMPTY);
92
  register_js_function ("print", jerryx_handler_print);
93 94 95 96 97 98
  jerry_value_t global_obj_val = jerry_get_global_object ();

  jerry_value_t print_func_name_val = jerry_create_string ((jerry_char_t *) "print");
  print_function = jerry_get_property (global_obj_val, print_func_name_val);
  jerry_release_value (print_func_name_val);
  jerry_release_value (global_obj_val);
99
  if (jerry_value_is_error (print_function))
100 101 102 103
  {
    printf ("Error: could not look up print function, expression results won't be printed\n");
  }

104 105 106 107 108 109 110 111 112 113 114
  while (1)
  {
    char *s;
    printf("js> ");
    fflush(stdout);
    s = zephyr_getline ();
    if (*s)
    {
      shell_cmd_handler (s);
    }
  }
115

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