Skip to content

Commit bd3bd97

Browse files
committed
Implementing common external function handlers in jerry-ext
Add `extfunc` module to `jerry-ext` to contain implementation of commonly used external function handlers: `assert`, `gc`, and `print`. JerryScript-DCO-1.0-Signed-off-by: Akos Kiss akiss@inf.u-szeged.hu
1 parent fb432a8 commit bd3bd97

File tree

5 files changed

+266
-0
lines changed

5 files changed

+266
-0
lines changed

jerry-ext/extfunc/extfunc-assert.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 "jerryscript-ext/extfunc.h"
17+
#include "jerryscript-port.h"
18+
19+
/**
20+
* Add assert for scripts. The routine calls jerry_port_fatal on assertion failure.
21+
*
22+
* @return true - if only one argument was passed and the argument is a boolean true.
23+
*/
24+
jerry_value_t
25+
jerryx_extfunc_assert (const jerry_value_t func_obj_val __attribute__((unused)), /**< function object */
26+
const jerry_value_t this_p __attribute__((unused)), /**< this arg */
27+
const jerry_value_t args_p[], /**< function arguments */
28+
const jerry_length_t args_cnt) /**< number of function arguments */
29+
{
30+
if (args_cnt == 1
31+
&& jerry_value_is_boolean (args_p[0])
32+
&& jerry_get_boolean_value (args_p[0]))
33+
{
34+
return jerry_create_boolean (true);
35+
}
36+
else
37+
{
38+
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Script Error: assertion failed\n");
39+
jerry_port_fatal (ERR_FAILED_INTERNAL_ASSERTION);
40+
}
41+
} /* jerryx_extfunc_assert */

jerry-ext/extfunc/extfunc-gc.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 "jerryscript-ext/extfunc.h"
17+
18+
/**
19+
* Expose garbage collector to scripts.
20+
*
21+
* @return undefined.
22+
*/
23+
jerry_value_t
24+
jerryx_extfunc_gc (const jerry_value_t func_obj_val __attribute__((unused)), /**< function object */
25+
const jerry_value_t this_p __attribute__((unused)), /**< this arg */
26+
const jerry_value_t args_p[] __attribute__((unused)), /**< function arguments */
27+
const jerry_length_t args_cnt __attribute__((unused))) /**< number of function arguments */
28+
{
29+
jerry_gc ();
30+
return jerry_create_undefined ();
31+
} /* jerryx_extfunc_gc */

jerry-ext/extfunc/extfunc-print.c

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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 "jerryscript-ext/extfunc.h"
17+
18+
19+
/**
20+
* Provide a 'print' implementation for scripts.
21+
*
22+
* The routine converts all of its arguments to strings and outputs them
23+
* char-by-char using jerryx_port_extfunc_print_char.
24+
*
25+
* The NUL character is output as "\u0000", other characters are output
26+
* bytewise.
27+
*
28+
* @return undefined - if all arguments could be converted to strings,
29+
* error - otherwise.
30+
*/
31+
jerry_value_t
32+
jerryx_extfunc_print (const jerry_value_t func_obj_val __attribute__((unused)), /**< function object */
33+
const jerry_value_t this_p __attribute__((unused)), /**< this arg */
34+
const jerry_value_t args_p[], /**< function arguments */
35+
const jerry_length_t args_cnt) /**< number of function arguments */
36+
{
37+
static const char *null_str = "\\u0000";
38+
39+
jerry_value_t ret_val = jerry_create_undefined ();
40+
41+
for (jerry_length_t arg_index = 0;
42+
jerry_value_is_undefined (ret_val) && arg_index < args_cnt;
43+
arg_index++)
44+
{
45+
jerry_value_t str_val = jerry_value_to_string (args_p[arg_index]);
46+
47+
if (!jerry_value_has_error_flag (str_val))
48+
{
49+
if (arg_index != 0)
50+
{
51+
jerryx_extfunc_print_char (' ');
52+
}
53+
54+
jerry_size_t substr_size;
55+
jerry_length_t substr_pos = 0;
56+
jerry_char_t substr_buf[256];
57+
58+
while ((substr_size = jerry_substring_to_char_buffer (str_val,
59+
substr_pos,
60+
substr_pos + 256,
61+
substr_buf,
62+
256)) != 0)
63+
{
64+
for (jerry_size_t chr_index = 0; chr_index < substr_size; chr_index++)
65+
{
66+
char chr = (char) substr_buf[chr_index];
67+
if (chr == '\0')
68+
{
69+
for (jerry_size_t null_index = 0; null_str[null_index] != 0; null_index++)
70+
{
71+
jerryx_port_extfunc_print_char (null_str[null_index]);
72+
}
73+
}
74+
else
75+
{
76+
jerryx_port_extfunc_print_char (chr);
77+
}
78+
}
79+
80+
substr_pos += substr_size;
81+
}
82+
83+
jerry_release_value (str_val);
84+
}
85+
else
86+
{
87+
ret_val = str_val;
88+
}
89+
}
90+
91+
jerryx_port_extfunc_print_char ('\n');
92+
93+
return ret_val;
94+
} /* jerryx_extfunc_print */
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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 "jerryscript-ext/extfunc.h"
17+
18+
19+
/**
20+
* Register a JavaScript function in the global object.
21+
*
22+
* Note:
23+
* returned value must be freed with jerry_release_value, when it is no longer needed.
24+
*
25+
* @return true value - if the operation was successful
26+
* value marked with error flag - otherwise
27+
*/
28+
jerry_value_t
29+
jerryx_extfunc_register_global_function (const jerry_char_t *name_p, /**< name of the function */
30+
jerry_external_handler_t handler_p) /**< function callback */
31+
{
32+
jerry_value_t global_obj_val = jerry_get_global_object ();
33+
jerry_value_t function_name_val = jerry_create_string (name_p);
34+
jerry_value_t function_val = jerry_create_external_function (handler_p);
35+
36+
jerry_value_t result_val = jerry_set_property (global_obj_val, function_name_val, function_val);
37+
38+
jerry_release_value (function_val);
39+
jerry_release_value (function_name_val);
40+
jerry_release_value (global_obj_val);
41+
42+
return result_val;
43+
} /* register_js_function */
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
#ifndef JERRYX_EXTFUNC_H
17+
#define JERRYX_EXTFUNC_H
18+
19+
#include "jerryscript.h"
20+
21+
#ifdef __cplusplus
22+
extern "C"
23+
{
24+
#endif /* __cplusplus */
25+
26+
/*
27+
* Handler registration helper
28+
*/
29+
30+
jerry_value_t jerryx_extfunc_register_global_function (const jerry_char_t *name_p, jerry_external_handler_t handler_p);
31+
32+
/*
33+
* Common external function handlers
34+
*/
35+
36+
jerry_value_t jerryx_extfunc_assert (const jerry_value_t func_obj_val, const jerry_value_t this_p,
37+
const jerry_value_t args_p[], const jerry_length_t args_cnt);
38+
jerry_value_t jerryx_extfunc_gc (const jerry_value_t func_obj_val, const jerry_value_t this_p,
39+
const jerry_value_t args_p[], const jerry_length_t args_cnt);
40+
jerry_value_t jerryx_extfunc_print (const jerry_value_t func_obj_val, const jerry_value_t this_p,
41+
const jerry_value_t args_p[], const jerry_length_t args_cnt);
42+
43+
/*
44+
* JerryX ExtFunc Port API
45+
*/
46+
47+
/**
48+
* Print a single character.
49+
*
50+
* @param c the character to print.
51+
*/
52+
void jerryx_port_extfunc_print_char (char c);
53+
54+
#ifdef __cplusplus
55+
}
56+
#endif /* __cplusplus */
57+
#endif /* !JERRYX_EXTFUNC_H */

0 commit comments

Comments
 (0)