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 <stdlib.h>
17#include <stdio.h>
18
19#include "jerry_extapi.h"
20#include "jerry_run.h"
21
22#include "jerryscript.h"
23#include "jerryscript-port.h"
24
25static const char* fn_sys_loop_name = "sysloop";
26
27void js_entry ()
28{
29  union { double d; unsigned u; } now = { .d = jerry_port_get_current_time () };
30  srand (now.u);
31
32  jerry_init (JERRY_INIT_EMPTY);
33  js_register_functions ();
34}
35
36int js_eval (const char *source_p, const size_t source_size)
37{
38  jerry_value_t res = jerry_eval ((jerry_char_t *) source_p,
39                                  source_size,
40                                  JERRY_PARSE_NO_OPTS);
41  if (jerry_value_is_error (res)) {
42    jerry_release_value (res);
43    return -1;
44  }
45
46  jerry_release_value (res);
47
48  return 0;
49}
50
51int js_loop (uint32_t ticknow)
52{
53  jerry_value_t global_obj_val = jerry_get_global_object ();
54  jerry_value_t prop_name_val = jerry_create_string ((const jerry_char_t *) fn_sys_loop_name);
55  jerry_value_t sysloop_func = jerry_get_property (global_obj_val, prop_name_val);
56  jerry_release_value (prop_name_val);
57
58  if (jerry_value_is_error (sysloop_func)) {
59    printf ("Error: '%s' not defined!!!\r\n", fn_sys_loop_name);
60    jerry_release_value (sysloop_func);
61    jerry_release_value (global_obj_val);
62    return -1;
63  }
64
65  if (!jerry_value_is_function (sysloop_func)) {
66    printf ("Error: '%s' is not a function!!!\r\n", fn_sys_loop_name);
67    jerry_release_value (sysloop_func);
68    jerry_release_value (global_obj_val);
69    return -2;
70  }
71
72  jerry_value_t val_args[] = { jerry_create_number (ticknow) };
73  uint16_t val_argv = sizeof (val_args) / sizeof (jerry_value_t);
74
75  jerry_value_t res = jerry_call_function (sysloop_func,
76                                           global_obj_val,
77                                           val_args,
78                                           val_argv);
79
80  for (uint16_t i = 0; i < val_argv; i++) {
81    jerry_release_value (val_args[i]);
82  }
83
84  jerry_release_value (sysloop_func);
85  jerry_release_value (global_obj_val);
86
87  if (jerry_value_is_error (res)) {
88    jerry_release_value (res);
89    return -3;
90  }
91
92  jerry_release_value (res);
93
94  return 0;
95}
96
97void js_exit (void)
98{
99  jerry_cleanup ();
100}
101