main-riotos.c 2.9 KB
Newer Older
1
/* Copyright JS Foundation and other contributors, http://js.foundation
2 3 4 5 6 7 8 9 10 11 12 13 14 15
 *
 * 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.
 */

16
#include <stdio.h>
17
#include <stdlib.h>
18 19
#include <string.h>
#include "shell.h"
20
#include "jerryscript.h"
21
#include "jerryscript-ext/handler.h"
22
#include "jerryscript-port.h"
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38

/**
 * Standalone Jerry exit codes
 */
#define JERRY_STANDALONE_EXIT_CODE_OK   (0)
#define JERRY_STANDALONE_EXIT_CODE_FAIL (1)

/**
 * 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
  {
    printf ("Warning: failed to register '%s' method.", name_p);
  }

  jerry_release_value (result_val);
} /* register_js_function */
46 47 48 49 50 51 52 53 54

/**
 * Jerryscript simple test
 */
int test_jerry (int argc, char **argv)
{
  /* Suppress compiler errors */
  (void) argc;
  (void) argv;
55 56 57

  jerry_value_t ret_value = jerry_create_undefined ();

58
  const jerry_char_t script[] = "print ('Hello, World!');";
59 60
  printf ("This test run the following script code: [%s]\n\n", script);

61 62 63 64 65 66 67
  /* Initialize engine */
  jerry_init (JERRY_INIT_EMPTY);

  /* Register the print function in the global object. */
  register_js_function ("print", jerryx_handler_print);

  /* Setup Global scope code */
68
  ret_value = jerry_parse (NULL, 0, script, sizeof (script) - 1, JERRY_PARSE_NO_OPTS);
69

70
  if (!jerry_value_is_error (ret_value))
71 72 73 74 75 76 77
  {
    /* Execute the parsed source code in the Global scope */
    ret_value = jerry_run (ret_value);
  }

  int ret_code = JERRY_STANDALONE_EXIT_CODE_OK;

78
  if (jerry_value_is_error (ret_value))
79 80 81 82 83 84 85 86 87 88 89 90
  {
    printf ("Script Error!");

    ret_code = JERRY_STANDALONE_EXIT_CODE_FAIL;
  }

  jerry_release_value (ret_value);

  /* Cleanup engine */
  jerry_cleanup ();

  return ret_code;
91 92 93 94 95 96 97 98 99 100

} /* test_jerry */

const shell_command_t shell_commands[] = {
  { "test", "Jerryscript Hello World test", test_jerry },
  { NULL, NULL, NULL }
};

int main (void)
{
101
  srand ((unsigned) jerry_port_get_current_time ());
102 103 104 105 106 107 108 109 110
  printf ("You are running RIOT on a(n) %s board.\n", RIOT_BOARD);
  printf ("This board features a(n) %s MCU.\n", RIOT_MCU);

  /* start the shell */
  char line_buf[SHELL_DEFAULT_BUFSIZE];
  shell_run (shell_commands, line_buf, SHELL_DEFAULT_BUFSIZE);

  return 0;
}