mirror of
https://github.com/jerryscript-project/jerryscript.git
synced 2025-12-15 16:29:21 +00:00
Implement common external function handlers in jerry-ext (#1787)
Added `handler` module to `jerry-ext` to contain implementation of commonly used external function handlers: `assert`, `gc`, and `print`. Also adapted jerry-main to use jerry-ext/handler JerryScript-DCO-1.0-Signed-off-by: Akos Kiss akiss@inf.u-szeged.hu
This commit is contained in:
parent
68f9585b96
commit
a8f2d31bca
@ -47,6 +47,12 @@ if(JERRY_CMDLINE OR JERRY_CMDLINE_MINIMAL)
|
||||
set(JERRY_PORT_DEFAULT_MESSAGE " (FORCED BY CMDLINE)")
|
||||
endif()
|
||||
|
||||
if(JERRY_CMDLINE)
|
||||
set(JERRY_EXT "ON")
|
||||
|
||||
set(JERRY_EXT_MESSAGE " (FORCED BY CMDLINE)")
|
||||
endif()
|
||||
|
||||
if("${PLATFORM}" STREQUAL "DARWIN")
|
||||
set(JERRY_LIBC "OFF")
|
||||
set(JERRY_LIBM "OFF")
|
||||
|
||||
179
docs/10.EXT-REFERENCE-HANDLER.md
Normal file
179
docs/10.EXT-REFERENCE-HANDLER.md
Normal file
@ -0,0 +1,179 @@
|
||||
# Common external function handlers
|
||||
|
||||
## jerryx_handler_assert
|
||||
|
||||
**Summary**
|
||||
|
||||
Assert for scripts. The routine calls `jerry_port_fatal` on assertion failure.
|
||||
|
||||
**Prototype**
|
||||
|
||||
```c
|
||||
jerry_value_t
|
||||
jerryx_handler_assert (const jerry_value_t func_obj_val, const jerry_value_t this_p,
|
||||
const jerry_value_t args_p[], const jerry_length_t args_cnt);
|
||||
```
|
||||
|
||||
- `func_obj_val` - the function object that was called (unused).
|
||||
- `this_p` - the `this` value of the call (unused).
|
||||
- `args_p` - the array of function arguments.
|
||||
- `args_cnt` - the number of function arguments.
|
||||
- return value - `jerry_value_t` representing boolean true, if only one argument
|
||||
was passed and that argument was a boolean true. Note that the function does
|
||||
not return otherwise.
|
||||
|
||||
**See also**
|
||||
|
||||
- [jerryx_handler_register_global](#jerryx_handler_register_global)
|
||||
|
||||
|
||||
## jerryx_handler_gc
|
||||
|
||||
**Summary**
|
||||
|
||||
Expose garbage collector to scripts.
|
||||
|
||||
**Prototype**
|
||||
|
||||
```c
|
||||
jerry_value_t
|
||||
jerryx_handler_gc (const jerry_value_t func_obj_val, const jerry_value_t this_p,
|
||||
const jerry_value_t args_p[], const jerry_length_t args_cnt);
|
||||
```
|
||||
|
||||
- `func_obj_val` - the function object that was called (unused).
|
||||
- `this_p` - the `this` value of the call (unused).
|
||||
- `args_p` - the array of function arguments (unused).
|
||||
- `args_cnt` - the number of function arguments (unused).
|
||||
- return value - `jerry_value_t` representing `undefined`.
|
||||
|
||||
**See also**
|
||||
|
||||
- [jerryx_handler_register_global](#jerryx_handler_register_global)
|
||||
|
||||
|
||||
## jerryx_handler_print
|
||||
|
||||
**Summary**
|
||||
|
||||
Provide a `print` implementation for scripts. The routine converts all of its
|
||||
arguments to strings and outputs them char-by-char using
|
||||
`jerryx_port_handler_print_char`. The NUL character is output as "\u0000",
|
||||
other characters are output bytewise.
|
||||
|
||||
*Note*: This implementation does not use standard C `printf` to print its
|
||||
output. This allows more flexibility but also extends the core JerryScript
|
||||
engine port API. Applications that want to use `jerryx_handler_print` must
|
||||
ensure that their port implementation also provides
|
||||
`jerryx_port_handler_print_char`.
|
||||
|
||||
**Prototype**
|
||||
|
||||
```c
|
||||
jerry_value_t
|
||||
jerryx_handler_print (const jerry_value_t func_obj_val, const jerry_value_t this_p,
|
||||
const jerry_value_t args_p[], const jerry_length_t args_cnt);
|
||||
```
|
||||
|
||||
- `func_obj_val` - the function object that was called (unused).
|
||||
- `this_p` - the `this` value of the call (unused).
|
||||
- `args_p` - the array of function arguments.
|
||||
- `args_cnt` - the number of function arguments.
|
||||
- return value - `jerry_value_t` representing `undefined` if all arguments could
|
||||
be converted to strings, an `Error` otherwise.
|
||||
|
||||
**See also**
|
||||
|
||||
- [jerryx_handler_register_global](#jerryx_handler_register_global)
|
||||
- [jerryx_port_handler_print_char](#jerryx_port_handler_print_char)
|
||||
|
||||
|
||||
# Handler registration helper
|
||||
|
||||
## jerryx_handler_register_global
|
||||
|
||||
**Summary**
|
||||
|
||||
Register a JavaScript function in the global object.
|
||||
|
||||
*Note*: Returned value must be freed with `jerry_release_value`, when it is no
|
||||
longer needed.
|
||||
|
||||
**Prototype**
|
||||
|
||||
```c
|
||||
jerry_value_t
|
||||
jerryx_handler_register_global (const jerry_char_t *name_p,
|
||||
jerry_external_handler_t handler_p);
|
||||
```
|
||||
|
||||
- `name_p` - the name of the function to be registered.
|
||||
- `handler_p` - the address of the external function handler.
|
||||
- return value - `jerry_value_t` representing boolean true, if the operation was
|
||||
successful, an `Error` otherwise.
|
||||
|
||||
**Example**
|
||||
|
||||
```c
|
||||
#include "jerryscript.h"
|
||||
#include "jerryscript-ext/handler.h"
|
||||
|
||||
static const struct {
|
||||
const char *name_p;
|
||||
jerry_external_handler_t handler_p;
|
||||
} common_functions[] =
|
||||
{
|
||||
{ "assert", jerryx_handler_assert },
|
||||
{ "gc", jerryx_handler_gc },
|
||||
{ "print", jerryx_handler_print },
|
||||
{ NULL, NULL }
|
||||
};
|
||||
|
||||
static void register_common_functions ()
|
||||
{
|
||||
jerry_value_t ret = jerry_create_undefined ();
|
||||
|
||||
for (int i = 0; common_functions[i].name_p != NULL && !jerry_value_has_error_flag (ret); i++)
|
||||
{
|
||||
ret = jerryx_handler_register_global ((const jerry_char_t *) common_functions[i].name_p,
|
||||
common_functions[i].handler_p);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
# Port API extension
|
||||
|
||||
## jerryx_port_handler_print_char
|
||||
|
||||
**Summary**
|
||||
|
||||
Print a single character.
|
||||
|
||||
**Prototype**
|
||||
|
||||
```c
|
||||
void
|
||||
jerryx_port_handler_print_char (char c);
|
||||
```
|
||||
|
||||
- `c` - the character to print.
|
||||
|
||||
**Example**
|
||||
|
||||
```c
|
||||
/**
|
||||
* Print a character to stdout with printf.
|
||||
*/
|
||||
void
|
||||
jerryx_port_handler_print_char (char c)
|
||||
{
|
||||
printf ("%c", c);
|
||||
} /* jerryx_port_handler_print_char */
|
||||
```
|
||||
|
||||
**See also**
|
||||
|
||||
- [jerryx_handler_print](#jerryx_handler_print)
|
||||
@ -62,7 +62,7 @@ typedef enum
|
||||
*
|
||||
* Example: a libc-based port may implement this with exit() or abort(), or both.
|
||||
*/
|
||||
void jerry_port_fatal (jerry_fatal_code_t code);
|
||||
void jerry_port_fatal (jerry_fatal_code_t code) __attribute__((noreturn));
|
||||
|
||||
/*
|
||||
* I/O Port API
|
||||
|
||||
@ -20,7 +20,12 @@ project (${JERRY_EXT_NAME} C)
|
||||
set(INCLUDE_EXT "${CMAKE_CURRENT_SOURCE_DIR}/include")
|
||||
|
||||
# Source directories
|
||||
file(GLOB SOURCE_EXT arg/*.c)
|
||||
file(GLOB SOURCE_EXT_ARG arg/*.c)
|
||||
file(GLOB SOURCE_EXT_HANDLER handler/*.c)
|
||||
|
||||
set(SOURCE_EXT
|
||||
${SOURCE_EXT_ARG}
|
||||
${SOURCE_EXT_HANDLER})
|
||||
|
||||
add_library(${JERRY_EXT_NAME} STATIC ${SOURCE_EXT})
|
||||
|
||||
|
||||
45
jerry-ext/handler/handler-assert.c
Normal file
45
jerry-ext/handler/handler-assert.c
Normal file
@ -0,0 +1,45 @@
|
||||
/* 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 "jerryscript-ext/handler.h"
|
||||
#include "jerryscript-port.h"
|
||||
|
||||
/**
|
||||
* Assert for scripts. The routine calls jerry_port_fatal on assertion failure.
|
||||
*
|
||||
* @return true - if only one argument was passed and that argument was a boolean true.
|
||||
* Note that the function does not return otherwise.
|
||||
*/
|
||||
jerry_value_t
|
||||
jerryx_handler_assert (const jerry_value_t func_obj_val, /**< function object */
|
||||
const jerry_value_t this_p, /**< this arg */
|
||||
const jerry_value_t args_p[], /**< function arguments */
|
||||
const jerry_length_t args_cnt) /**< number of function arguments */
|
||||
{
|
||||
(void) func_obj_val; /* unused */
|
||||
(void) this_p; /* unused */
|
||||
|
||||
if (args_cnt == 1
|
||||
&& jerry_value_is_boolean (args_p[0])
|
||||
&& jerry_get_boolean_value (args_p[0]))
|
||||
{
|
||||
return jerry_create_boolean (true);
|
||||
}
|
||||
else
|
||||
{
|
||||
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Script Error: assertion failed\n");
|
||||
jerry_port_fatal (ERR_FAILED_INTERNAL_ASSERTION);
|
||||
}
|
||||
} /* jerryx_handler_assert */
|
||||
36
jerry-ext/handler/handler-gc.c
Normal file
36
jerry-ext/handler/handler-gc.c
Normal file
@ -0,0 +1,36 @@
|
||||
/* 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 "jerryscript-ext/handler.h"
|
||||
|
||||
/**
|
||||
* Expose garbage collector to scripts.
|
||||
*
|
||||
* @return undefined.
|
||||
*/
|
||||
jerry_value_t
|
||||
jerryx_handler_gc (const jerry_value_t func_obj_val, /**< function object */
|
||||
const jerry_value_t this_p, /**< this arg */
|
||||
const jerry_value_t args_p[], /**< function arguments */
|
||||
const jerry_length_t args_cnt) /**< number of function arguments */
|
||||
{
|
||||
(void) func_obj_val; /* unused */
|
||||
(void) this_p; /* unused */
|
||||
(void) args_p; /* unused */
|
||||
(void) args_cnt; /* unused */
|
||||
|
||||
jerry_gc ();
|
||||
return jerry_create_undefined ();
|
||||
} /* jerryx_handler_gc */
|
||||
103
jerry-ext/handler/handler-print.c
Normal file
103
jerry-ext/handler/handler-print.c
Normal file
@ -0,0 +1,103 @@
|
||||
/* 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 "jerryscript-ext/handler.h"
|
||||
|
||||
/**
|
||||
* Provide a 'print' implementation for scripts.
|
||||
*
|
||||
* The routine converts all of its arguments to strings and outputs them
|
||||
* char-by-char using jerryx_port_handler_print_char.
|
||||
*
|
||||
* The NUL character is output as "\u0000", other characters are output
|
||||
* bytewise.
|
||||
*
|
||||
* Note:
|
||||
* This implementation does not use standard C `printf` to print its
|
||||
* output. This allows more flexibility but also extends the core
|
||||
* JerryScript engine port API. Applications that want to use
|
||||
* `jerryx_handler_print` must ensure that their port implementation also
|
||||
* provides `jerryx_port_handler_print_char`.
|
||||
*
|
||||
* @return undefined - if all arguments could be converted to strings,
|
||||
* error - otherwise.
|
||||
*/
|
||||
jerry_value_t
|
||||
jerryx_handler_print (const jerry_value_t func_obj_val, /**< function object */
|
||||
const jerry_value_t this_p, /**< this arg */
|
||||
const jerry_value_t args_p[], /**< function arguments */
|
||||
const jerry_length_t args_cnt) /**< number of function arguments */
|
||||
{
|
||||
(void) func_obj_val; /* unused */
|
||||
(void) this_p; /* unused */
|
||||
|
||||
static const char *null_str = "\\u0000";
|
||||
|
||||
jerry_value_t ret_val = jerry_create_undefined ();
|
||||
|
||||
for (jerry_length_t arg_index = 0;
|
||||
jerry_value_is_undefined (ret_val) && arg_index < args_cnt;
|
||||
arg_index++)
|
||||
{
|
||||
jerry_value_t str_val = jerry_value_to_string (args_p[arg_index]);
|
||||
|
||||
if (!jerry_value_has_error_flag (str_val))
|
||||
{
|
||||
if (arg_index != 0)
|
||||
{
|
||||
jerryx_port_handler_print_char (' ');
|
||||
}
|
||||
|
||||
jerry_size_t substr_size;
|
||||
jerry_length_t substr_pos = 0;
|
||||
jerry_char_t substr_buf[256];
|
||||
|
||||
while ((substr_size = jerry_substring_to_char_buffer (str_val,
|
||||
substr_pos,
|
||||
substr_pos + 256,
|
||||
substr_buf,
|
||||
256)) != 0)
|
||||
{
|
||||
for (jerry_size_t chr_index = 0; chr_index < substr_size; chr_index++)
|
||||
{
|
||||
char chr = (char) substr_buf[chr_index];
|
||||
if (chr == '\0')
|
||||
{
|
||||
for (jerry_size_t null_index = 0; null_str[null_index] != 0; null_index++)
|
||||
{
|
||||
jerryx_port_handler_print_char (null_str[null_index]);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
jerryx_port_handler_print_char (chr);
|
||||
}
|
||||
}
|
||||
|
||||
substr_pos += substr_size;
|
||||
}
|
||||
|
||||
jerry_release_value (str_val);
|
||||
}
|
||||
else
|
||||
{
|
||||
ret_val = str_val;
|
||||
}
|
||||
}
|
||||
|
||||
jerryx_port_handler_print_char ('\n');
|
||||
|
||||
return ret_val;
|
||||
} /* jerryx_handler_print */
|
||||
42
jerry-ext/handler/handler-register.c
Normal file
42
jerry-ext/handler/handler-register.c
Normal file
@ -0,0 +1,42 @@
|
||||
/* 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 "jerryscript-ext/handler.h"
|
||||
|
||||
/**
|
||||
* Register a JavaScript function in the global object.
|
||||
*
|
||||
* Note:
|
||||
* returned value must be freed with jerry_release_value, when it is no longer needed.
|
||||
*
|
||||
* @return true value - if the operation was successful,
|
||||
* error - otherwise.
|
||||
*/
|
||||
jerry_value_t
|
||||
jerryx_handler_register_global (const jerry_char_t *name_p, /**< name of the function */
|
||||
jerry_external_handler_t handler_p) /**< function callback */
|
||||
{
|
||||
jerry_value_t global_obj_val = jerry_get_global_object ();
|
||||
jerry_value_t function_name_val = jerry_create_string (name_p);
|
||||
jerry_value_t function_val = jerry_create_external_function (handler_p);
|
||||
|
||||
jerry_value_t result_val = jerry_set_property (global_obj_val, function_name_val, function_val);
|
||||
|
||||
jerry_release_value (function_val);
|
||||
jerry_release_value (function_name_val);
|
||||
jerry_release_value (global_obj_val);
|
||||
|
||||
return result_val;
|
||||
} /* jerryx_handler_register_global */
|
||||
58
jerry-ext/include/jerryscript-ext/handler.h
Normal file
58
jerry-ext/include/jerryscript-ext/handler.h
Normal file
@ -0,0 +1,58 @@
|
||||
/* 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.
|
||||
*/
|
||||
|
||||
#ifndef JERRYX_HANDLER_H
|
||||
#define JERRYX_HANDLER_H
|
||||
|
||||
#include "jerryscript.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif /* __cplusplus */
|
||||
|
||||
/*
|
||||
* Handler registration helper
|
||||
*/
|
||||
|
||||
jerry_value_t jerryx_handler_register_global (const jerry_char_t *name_p,
|
||||
jerry_external_handler_t handler_p);
|
||||
|
||||
/*
|
||||
* Common external function handlers
|
||||
*/
|
||||
|
||||
jerry_value_t jerryx_handler_assert (const jerry_value_t func_obj_val, const jerry_value_t this_p,
|
||||
const jerry_value_t args_p[], const jerry_length_t args_cnt);
|
||||
jerry_value_t jerryx_handler_gc (const jerry_value_t func_obj_val, const jerry_value_t this_p,
|
||||
const jerry_value_t args_p[], const jerry_length_t args_cnt);
|
||||
jerry_value_t jerryx_handler_print (const jerry_value_t func_obj_val, const jerry_value_t this_p,
|
||||
const jerry_value_t args_p[], const jerry_length_t args_cnt);
|
||||
|
||||
/*
|
||||
* Port API extension
|
||||
*/
|
||||
|
||||
/**
|
||||
* Print a single character.
|
||||
*
|
||||
* @param c the character to print.
|
||||
*/
|
||||
void jerryx_port_handler_print_char (char c);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif /* __cplusplus */
|
||||
#endif /* !JERRYX_HANDLER_H */
|
||||
@ -58,7 +58,7 @@ endmacro()
|
||||
# Jerry standalones
|
||||
if(JERRY_CMDLINE)
|
||||
jerry_create_executable("jerry" "main-unix.c")
|
||||
target_link_libraries("jerry" jerry-port-default)
|
||||
target_link_libraries("jerry" jerry-ext jerry-port-default)
|
||||
endif()
|
||||
|
||||
if(JERRY_CMDLINE_MINIMAL)
|
||||
|
||||
@ -19,6 +19,7 @@
|
||||
#include <string.h>
|
||||
|
||||
#include "jerryscript.h"
|
||||
#include "jerryscript-ext/handler.h"
|
||||
#include "jerryscript-port.h"
|
||||
#include "jerryscript-port-default.h"
|
||||
|
||||
@ -78,117 +79,6 @@ read_file (const char *file_name,
|
||||
return (const uint32_t *) buffer;
|
||||
} /* read_file */
|
||||
|
||||
/**
|
||||
* Provide the 'assert' implementation for the engine.
|
||||
*
|
||||
* @return true - if only one argument was passed and the argument is a boolean true.
|
||||
*/
|
||||
static jerry_value_t
|
||||
assert_handler (const jerry_value_t func_obj_val __attribute__((unused)), /**< function object */
|
||||
const jerry_value_t this_p __attribute__((unused)), /**< this arg */
|
||||
const jerry_value_t args_p[], /**< function arguments */
|
||||
const jerry_length_t args_cnt) /**< number of function arguments */
|
||||
{
|
||||
if (args_cnt == 1
|
||||
&& jerry_value_is_boolean (args_p[0])
|
||||
&& jerry_get_boolean_value (args_p[0]))
|
||||
{
|
||||
return jerry_create_boolean (true);
|
||||
}
|
||||
else
|
||||
{
|
||||
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Script Error: assertion failed\n");
|
||||
exit (JERRY_STANDALONE_EXIT_CODE_FAIL);
|
||||
}
|
||||
} /* assert_handler */
|
||||
|
||||
/**
|
||||
* Provide the 'gc' implementation for the engine.
|
||||
*
|
||||
* @return undefined.
|
||||
*/
|
||||
static jerry_value_t
|
||||
gc_handler (const jerry_value_t func_obj_val __attribute__((unused)), /**< function object */
|
||||
const jerry_value_t this_p __attribute__((unused)), /**< this arg */
|
||||
const jerry_value_t args_p[] __attribute__((unused)), /**< function arguments */
|
||||
const jerry_length_t args_cnt __attribute__((unused))) /**< number of function arguments */
|
||||
{
|
||||
jerry_gc ();
|
||||
return jerry_create_undefined ();
|
||||
} /* gc_handler */
|
||||
|
||||
/**
|
||||
* Provide the 'print' implementation for the engine.
|
||||
*
|
||||
* The routine converts all of its arguments to strings and outputs them using
|
||||
* 'printf'.
|
||||
*
|
||||
* The NUL character is output as "\u0000", other code points are output using
|
||||
* "%c" format argument.
|
||||
*
|
||||
* @return undefined - if all arguments could be converted to strings,
|
||||
* error - otherwise.
|
||||
*/
|
||||
static jerry_value_t
|
||||
print_handler (const jerry_value_t func_obj_val __attribute__((unused)), /**< function object */
|
||||
const jerry_value_t this_p __attribute__((unused)), /**< this arg */
|
||||
const jerry_value_t args_p[], /**< function arguments */
|
||||
const jerry_length_t args_cnt) /**< number of function arguments */
|
||||
{
|
||||
jerry_value_t ret_val = jerry_create_undefined ();
|
||||
|
||||
for (jerry_length_t arg_index = 0;
|
||||
jerry_value_is_undefined (ret_val) && arg_index < args_cnt;
|
||||
arg_index++)
|
||||
{
|
||||
jerry_value_t str_val = jerry_value_to_string (args_p[arg_index]);
|
||||
|
||||
if (!jerry_value_has_error_flag (str_val))
|
||||
{
|
||||
if (arg_index != 0)
|
||||
{
|
||||
printf (" ");
|
||||
}
|
||||
|
||||
jerry_size_t substr_size;
|
||||
jerry_length_t substr_pos = 0;
|
||||
jerry_char_t substr_buf[256];
|
||||
|
||||
while ((substr_size = jerry_substring_to_char_buffer (str_val,
|
||||
substr_pos,
|
||||
substr_pos + 256,
|
||||
substr_buf,
|
||||
256)) != 0)
|
||||
{
|
||||
for (jerry_size_t chr_index = 0; chr_index < substr_size; chr_index++)
|
||||
{
|
||||
char chr = (char) substr_buf[chr_index];
|
||||
if (chr == '\0')
|
||||
{
|
||||
printf ("\\u0000");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf ("%c", chr);
|
||||
}
|
||||
}
|
||||
|
||||
substr_pos += substr_size;
|
||||
}
|
||||
|
||||
jerry_release_value (str_val);
|
||||
}
|
||||
else
|
||||
{
|
||||
ret_val = str_val;
|
||||
}
|
||||
}
|
||||
|
||||
printf ("\n");
|
||||
|
||||
return ret_val;
|
||||
} /* print_handler */
|
||||
|
||||
static void
|
||||
print_usage (const char *name)
|
||||
{
|
||||
@ -423,15 +313,7 @@ static void
|
||||
register_js_function (const char *name_p, /**< name of the function */
|
||||
jerry_external_handler_t handler_p) /**< function callback */
|
||||
{
|
||||
jerry_value_t global_obj_val = jerry_get_global_object ();
|
||||
|
||||
jerry_value_t function_val = jerry_create_external_function (handler_p);
|
||||
jerry_value_t function_name_val = jerry_create_string ((const jerry_char_t *) name_p);
|
||||
jerry_value_t result_val = jerry_set_property (global_obj_val, function_name_val, function_val);
|
||||
|
||||
jerry_release_value (function_name_val);
|
||||
jerry_release_value (function_val);
|
||||
jerry_release_value (global_obj_val);
|
||||
jerry_value_t result_val = jerryx_handler_register_global ((const jerry_char_t *) name_p, handler_p);
|
||||
|
||||
if (jerry_value_has_error_flag (result_val))
|
||||
{
|
||||
@ -654,9 +536,9 @@ main (int argc,
|
||||
#endif /* !CONFIG_DISABLE_ES2015_PROMISE_BUILTIN */
|
||||
jerry_init (flags);
|
||||
|
||||
register_js_function ("assert", assert_handler);
|
||||
register_js_function ("gc", gc_handler);
|
||||
register_js_function ("print", print_handler);
|
||||
register_js_function ("assert", jerryx_handler_assert);
|
||||
register_js_function ("gc", jerryx_handler_gc);
|
||||
register_js_function ("print", jerryx_handler_print);
|
||||
|
||||
jerry_value_t ret_value = jerry_create_undefined ();
|
||||
|
||||
@ -812,10 +694,10 @@ main (int argc,
|
||||
{
|
||||
/* Print return value */
|
||||
const jerry_value_t args[] = { ret_val_eval };
|
||||
jerry_value_t ret_val_print = print_handler (jerry_create_undefined (),
|
||||
jerry_create_undefined (),
|
||||
args,
|
||||
1);
|
||||
jerry_value_t ret_val_print = jerryx_handler_print (jerry_create_undefined (),
|
||||
jerry_create_undefined (),
|
||||
args,
|
||||
1);
|
||||
jerry_release_value (ret_val_print);
|
||||
#ifndef CONFIG_DISABLE_ES2015_PROMISE_BUILTIN
|
||||
jerry_release_value (ret_val_eval);
|
||||
|
||||
@ -32,8 +32,10 @@ set(DEFINES_PORT_DEFAULT _BSD_SOURCE _DEFAULT_SOURCE)
|
||||
foreach(JERRY_PORT_LIBRARY_NAME ${JERRY_PORT_DEFAULT_NAME} ${JERRY_PORT_DEFAULT_NAME}-minimal)
|
||||
add_library(${JERRY_PORT_LIBRARY_NAME} STATIC ${SOURCE_PORT_DEFAULT})
|
||||
target_include_directories(${JERRY_PORT_LIBRARY_NAME} PUBLIC ${INCLUDE_PORT_DEFAULT})
|
||||
target_include_directories(${JERRY_PORT_LIBRARY_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/jerry-core/include)
|
||||
target_include_directories(${JERRY_PORT_LIBRARY_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/jerry-ext/include)
|
||||
target_compile_definitions(${JERRY_PORT_LIBRARY_NAME} PRIVATE ${DEFINES_PORT_DEFAULT})
|
||||
target_link_libraries(${JERRY_PORT_LIBRARY_NAME} jerry-core)
|
||||
target_link_libraries(${JERRY_PORT_LIBRARY_NAME} jerry-core) # FIXME: remove this dependency as soon as possible
|
||||
endforeach()
|
||||
|
||||
target_compile_definitions(${JERRY_PORT_DEFAULT_NAME}-minimal PRIVATE DISABLE_EXTRA_API)
|
||||
|
||||
28
jerry-port/default/defaultx-handler.c
Normal file
28
jerry-port/default/defaultx-handler.c
Normal file
@ -0,0 +1,28 @@
|
||||
/* 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 "jerryscript-ext/handler.h"
|
||||
|
||||
/**
|
||||
* Default implementation of jerryx_port_handler_print_char. Uses 'printf' to
|
||||
* print a single character to standard output.
|
||||
*/
|
||||
void
|
||||
jerryx_port_handler_print_char (char c) /**< the character to print */
|
||||
{
|
||||
printf ("%c", c);
|
||||
} /* jerryx_port_handler_print_char */
|
||||
Loading…
x
Reference in New Issue
Block a user