Update jerry-port and jerry-ext (#4907)

Notable changes:
  - Updated and the port API interface, new functions have been added
    and some have been changed. The port library is now cleaned up to
    not have any dependency on jerry-core, as it should be. The port library
    is now strictly a collection of functions that implement
    embedding/platform specific behavior.
  - The default port implementation has been split for windows and unix.
    Implemented port functions have been categorized and reorganized,
    and marked with attribute((weak)) for better reusability.
  - External context allocation has been moved to the port API instead
    of a core API callback. The iterface has also been extended with a
    function to free the allocated context. When external context is
    enabled, jerry_init now automatically calls the port implementation
    to allocate the context and jerry_cleanup automatically calls the port
    to free the context.
  - jerry_port_log has been changed to no longer require formatting to
    be implemented by the port. The reason beind this is that it was vague what
    format specifiers were used by the engine, and in what manner. The port
    function now takes a zero-terminated string, and should only implement
    how the string should be logged.
  - Logging and log message formatting is now handled by the core jerry library
    where it can be implemented as necessary. Logging can be done through a new
    core API function, which uses the port to output the final log message.
  - Log level has been moved into jerry-core, and an API function has
    been added to set the log level. It should be the library that
    filters log messages based on the requested log level, instead of
    logging everything and requiring the user to do so.
  - Module resolving logic has been moved into jerry-core. There's no
    reason to have it in the port library and requiring embedders to
    duplicate the code. It also added an unnecessary dependency on
    jerry-core to the port. Platform specific behavior is still used through
    the port API, like resolving module specifiers, and reading source file
    contents. If necessary, the resolving logic can still be overridden as
    previously.
  - The jerry-ext library has also been cleaned up, and many utility
    functions have been added that previously were implemented in
    jerry-main. This allows easier reusability for some common operations,
    like printing unhandled exceptions or providing a repl console.
  - Debugger interaction with logged/printed messages has been fixed, so
    that it's no longer the port implementations responsibility to send
    the output to the debugger, as the port should have no notion of what a
    debugger is.  The printing and logging functions will now pass the
    result message to the debugger, if connected.
  - Cleaned up TZA handling in the date port implementation, and simplified
    the API function prototype.
  - Moved property access helper functions that use ASCII strings as
    keys from jerry-ext to the core API.

JerryScript-DCO-1.0-Signed-off-by: Dániel Bátyai dbatyai@inf.u-szeged.hu
This commit is contained in:
Dániel Bátyai
2022-01-20 13:53:47 +01:00
committed by GitHub
parent 79fd540ec9
commit ac1c48eeff
170 changed files with 4201 additions and 5698 deletions
+6 -5
View File
@@ -45,12 +45,13 @@ set(SOURCE_EXT
debugger/debugger-ws.c
handle-scope/handle-scope-allocator.c
handle-scope/handle-scope.c
handler/handler-assert.c
handler/handler-gc.c
handler/handler-print.c
handler/handler-register.c
handler/handler-source-name.c
module/module.c
util/handlers.c
util/print.c
util/properties.c
util/repl.c
util/sources.c
util/test262.c
)
add_library(${JERRY_EXT_NAME} ${SOURCE_EXT})
+4 -4
View File
@@ -91,9 +91,9 @@ void JERRY_ATTR_NORETURN jerry_unreachable (const char *file, const char *functi
/*
* Logging
*/
#define JERRYX_ERROR_MSG(...) jerry_port_log (JERRY_LOG_LEVEL_ERROR, __VA_ARGS__)
#define JERRYX_WARNING_MSG(...) jerry_port_log (JERRY_LOG_LEVEL_WARNING, __VA_ARGS__)
#define JERRYX_DEBUG_MSG(...) jerry_port_log (JERRY_LOG_LEVEL_DEBUG, __VA_ARGS__)
#define JERRYX_TRACE_MSG(...) jerry_port_log (JERRY_LOG_LEVEL_TRACE, __VA_ARGS__)
#define JERRYX_ERROR_MSG(...) jerry_log (JERRY_LOG_LEVEL_ERROR, __VA_ARGS__)
#define JERRYX_WARNING_MSG(...) jerry_log (JERRY_LOG_LEVEL_WARNING, __VA_ARGS__)
#define JERRYX_DEBUG_MSG(...) jerry_log (JERRY_LOG_LEVEL_DEBUG, __VA_ARGS__)
#define JERRYX_TRACE_MSG(...) jerry_log (JERRY_LOG_LEVEL_TRACE, __VA_ARGS__)
#endif /* !JEXT_COMMON_H */
+48
View File
@@ -13,6 +13,8 @@
* limitations under the License.
*/
#include <assert.h>
#include "jerryscript-ext/debugger.h"
#include "jext-common.h"
@@ -36,3 +38,49 @@ jerryx_debugger_after_connect (bool success) /**< tells whether the connection
JERRYX_UNUSED (success);
#endif /* defined (JERRY_DEBUGGER) && (JERRY_DEBUGGER == 1) */
} /* jerryx_debugger_after_connect */
/**
* Check that value contains the reset abort value.
*
* Note: if the value is the reset abort value, the value is released.
*
* return true, if reset abort
* false, otherwise
*/
bool
jerryx_debugger_is_reset (jerry_value_t value) /**< jerry value */
{
if (!jerry_value_is_abort (value))
{
return false;
}
jerry_value_t abort_value = jerry_exception_value (value, false);
if (!jerry_value_is_string (abort_value))
{
jerry_value_free (abort_value);
return false;
}
static const char restart_str[] = "r353t";
jerry_size_t str_size = jerry_string_size (abort_value, JERRY_ENCODING_CESU8);
bool is_reset = false;
if (str_size == sizeof (restart_str) - 1)
{
JERRY_VLA (jerry_char_t, str_buf, str_size);
jerry_string_to_buffer (abort_value, JERRY_ENCODING_CESU8, str_buf, str_size);
is_reset = memcmp (restart_str, (char *) (str_buf), str_size) == 0;
if (is_reset)
{
jerry_value_free (value);
}
}
jerry_value_free (abort_value);
return is_reset;
} /* jerryx_debugger_is_reset */
+4 -5
View File
@@ -45,11 +45,10 @@ typedef SOCKET jerryx_socket_t;
typedef char jerryx_socket_void_t;
typedef int jerryx_socket_size_t;
#else /* !_WIN32 */
#include <fcntl.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <unistd.h>
/* On *nix the EWOULDBLOCK errno value can be returned for non-blocking operations */
#define JERRYX_EWOULDBLOCK EWOULDBLOCK
@@ -130,10 +129,10 @@ jerryx_debugger_tcp_log_error (int errno_value) /**< error value to log */
(LPTSTR) &error_message,
0,
NULL);
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "TCP Error: %s\n", error_message);
JERRYX_ERROR_MSG ("TCP Error: %s\n", error_message);
LocalFree (error_message);
#else /* !_WIN32 */
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "TCP Error: %s\n", strerror (errno_value));
JERRYX_ERROR_MSG ("TCP Error: %s\n", strerror (errno_value));
#endif /* _WIN32 */
} /* jerryx_debugger_tcp_log_error */
-114
View File
@@ -1,114 +0,0 @@
/* 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 <inttypes.h>
#include "jerryscript-port.h"
#include "jerryscript-ext/handler.h"
/**
* Hard assert for scripts. The routine calls jerry_port_fatal on assertion failure.
*
* Notes:
* * If the `JERRY_FEATURE_LINE_INFO` runtime feature is enabled (build option: `JERRY_LINE_INFO`)
* a backtrace is also printed out.
*
* @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_fatal (const jerry_call_info_t *call_info_p, /**< call information */
const jerry_value_t args_p[], /**< function arguments */
const jerry_length_t args_cnt) /**< number of function arguments */
{
(void) call_info_p; /* unused */
if (args_cnt == 1 && jerry_value_is_true (args_p[0]))
{
return jerry_boolean (true);
}
/* Assert failed, print a bit of JS backtrace */
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Script Error: assertion failed\n");
if (jerry_feature_enabled (JERRY_FEATURE_LINE_INFO))
{
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Script backtrace (top 5):\n");
/* If the line info feature is disabled an empty array will be returned. */
jerry_value_t backtrace_array = jerry_backtrace (5);
uint32_t array_length = jerry_array_length (backtrace_array);
for (uint32_t idx = 0; idx < array_length; idx++)
{
jerry_value_t property = jerry_object_get_index (backtrace_array, idx);
/* On some systems the uint32_t values can't be printed with "%u" and
* on some systems it can be printed. To avoid differences in the uint32_t typdef
* The "PRIu32" macro is used to correctly add the formatter.
*/
jerry_port_log (JERRY_LOG_LEVEL_ERROR, " %" PRIu32 ": ", idx);
jerry_char_t string_buffer[256];
const jerry_length_t buffer_size = (jerry_length_t) (sizeof (string_buffer) - 1);
jerry_size_t copied_bytes = jerry_string_to_buffer (property, JERRY_ENCODING_UTF8, string_buffer, buffer_size);
string_buffer[copied_bytes] = '\0';
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "%s\n", string_buffer);
jerry_value_free (property);
}
jerry_value_free (backtrace_array);
}
jerry_port_fatal (ERR_FAILED_INTERNAL_ASSERTION);
} /* jerryx_handler_assert_fatal */
/**
* Soft assert for scripts. The routine throws an error on assertion failure.
*
* @return true - if only one argument was passed and that argument was a boolean true.
* error - otherwise.
*/
jerry_value_t
jerryx_handler_assert_throw (const jerry_call_info_t *call_info_p, /**< call information */
const jerry_value_t args_p[], /**< function arguments */
const jerry_length_t args_cnt) /**< number of function arguments */
{
(void) call_info_p; /* unused */
if (args_cnt == 1 && jerry_value_is_true (args_p[0]))
{
return jerry_boolean (true);
}
return jerry_throw_sz (JERRY_ERROR_COMMON, "assertion failed");
} /* jerryx_handler_assert_throw */
/**
* An alias to `jerryx_handler_assert_fatal`.
*
* @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_call_info_t *call_info_p, /**< call information */
const jerry_value_t args_p[], /**< function arguments */
const jerry_length_t args_cnt) /**< number of function arguments */
{
return jerryx_handler_assert_fatal (call_info_p, args_p, args_cnt);
} /* jerryx_handler_assert */
-80
View File
@@ -1,80 +0,0 @@
/* 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-debugger.h"
#include "jerryscript-port.h"
#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 jerry_port_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 `jerry_port_print_char`.
*
* @return undefined - if all arguments could be converted to strings,
* error - otherwise.
*/
jerry_value_t
jerryx_handler_print (const jerry_call_info_t *call_info_p, /**< call information */
const jerry_value_t args_p[], /**< function arguments */
const jerry_length_t args_cnt) /**< number of function arguments */
{
(void) call_info_p; /* unused */
jerry_value_t ret_val = jerry_undefined ();
for (jerry_length_t arg_index = 0; arg_index < args_cnt; arg_index++)
{
jerry_value_t str_val;
if (jerry_value_is_symbol (args_p[arg_index]))
{
str_val = jerry_symbol_descriptive_string (args_p[arg_index]);
}
else
{
str_val = jerry_value_to_string (args_p[arg_index]);
}
if (jerry_value_is_exception (str_val))
{
/* There is no need to free the undefined value. */
ret_val = str_val;
break;
}
if (arg_index > 0)
{
jerry_port_print_char (' ');
}
jerry_string_print (str_val);
jerry_value_free (str_val);
}
jerry_port_print_char ('\n');
return ret_val;
} /* jerryx_handler_print */
-40
View File
@@ -1,40 +0,0 @@
/* 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"
/**
* Get the source name (usually a file name) of the currently executed script or the given function object
*
* Note: returned value must be freed with jerry_value_free, when it is no longer needed
*
* @return JS string constructed from
* - the currently executed function object's source name, if the given value is undefined
* - source name of the function object, if the given value is a function object
* - "<anonymous>", otherwise
*/
jerry_value_t
jerryx_handler_source_name (const jerry_call_info_t *call_info_p, /**< call information */
const jerry_value_t args_p[], /**< function arguments */
const jerry_length_t args_cnt) /**< number of function arguments */
{
(void) call_info_p; /* unused */
jerry_value_t undefined_value = jerry_undefined ();
jerry_value_t source_name = jerry_source_name (args_cnt > 0 ? args_p[0] : undefined_value);
jerry_value_free (undefined_value);
return source_name;
} /* jerryx_handler_source_name */
@@ -35,6 +35,8 @@ bool jerryx_debugger_serial_create (const char *config);
bool jerryx_debugger_ws_create (void);
bool jerryx_debugger_rp_create (void);
bool jerryx_debugger_is_reset (jerry_value_t value);
JERRY_C_API_END
#endif /* !JERRYX_HANDLER_H */
@@ -0,0 +1,48 @@
/* 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_HANDLERS_H
#define JERRYX_HANDLERS_H
#include "jerryscript-types.h"
JERRY_C_API_BEGIN
jerry_value_t jerryx_handler_assert (const jerry_call_info_t *call_info_p,
const jerry_value_t args_p[],
const jerry_length_t args_cnt);
jerry_value_t
jerryx_handler_gc (const jerry_call_info_t *call_info_p, const jerry_value_t args_p[], const jerry_length_t args_cnt);
jerry_value_t jerryx_handler_print (const jerry_call_info_t *call_info_p,
const jerry_value_t args_p[],
const jerry_length_t args_cnt);
jerry_value_t jerryx_handler_source_name (const jerry_call_info_t *call_info_p,
const jerry_value_t args_p[],
const jerry_length_t args_cnt);
jerry_value_t jerryx_handler_create_realm (const jerry_call_info_t *call_info_p,
const jerry_value_t args_p[],
const jerry_length_t args_cnt);
void jerryx_handler_promise_reject (jerry_promise_event_type_t event_type,
const jerry_value_t object,
const jerry_value_t value,
void *user_p);
jerry_value_t jerryx_handler_source_received (const jerry_char_t *source_name_p,
size_t source_name_size,
const jerry_char_t *source_p,
size_t source_size,
void *user_p);
JERRY_C_API_END
#endif /* !JERRYX_HANDLERS_H */
@@ -13,23 +13,22 @@
* limitations under the License.
*/
#include "jerryscript-ext/handler.h"
#ifndef JERRYX_PRINT_H
#define JERRYX_PRINT_H
/**
* Expose garbage collector to scripts.
*
* @return undefined.
*/
jerry_value_t
jerryx_handler_gc (const jerry_call_info_t *call_info_p, /**< call information */
const jerry_value_t args_p[], /**< function arguments */
const jerry_length_t args_cnt) /**< number of function arguments */
{
(void) call_info_p; /* unused */
#include "jerryscript-port.h"
#include "jerryscript-types.h"
jerry_gc_mode_t mode =
((args_cnt > 0 && jerry_value_to_boolean (args_p[0])) ? JERRY_GC_PRESSURE_HIGH : JERRY_GC_PRESSURE_LOW);
JERRY_C_API_BEGIN
jerry_heap_gc (mode);
return jerry_undefined ();
} /* jerryx_handler_gc */
jerry_value_t jerryx_print_value (const jerry_value_t value);
void jerryx_print_byte (jerry_char_t ch);
void jerryx_print_buffer (const jerry_char_t *buffer_p, jerry_size_t buffer_size);
void jerryx_print_string (const char *str_p);
void jerryx_print_backtrace (unsigned depth);
void jerryx_print_unhandled_exception (jerry_value_t exception);
void jerryx_print_unhandled_rejection (jerry_value_t exception);
JERRY_C_API_END
#endif /* !JERRYX_PRINT_H */
@@ -13,10 +13,10 @@
* limitations under the License.
*/
#ifndef JERRYX_HANDLER_H
#define JERRYX_HANDLER_H
#ifndef JERRYX_PROPERTIES_H
#define JERRYX_PROPERTIES_H
#include "jerryscript.h"
#include "jerryscript-types.h"
JERRY_C_API_BEGIN
@@ -24,29 +24,7 @@ JERRY_C_API_BEGIN
* Handler registration helper
*/
jerry_value_t jerryx_handler_register_global (const char *name_p, jerry_external_handler_t handler_p);
/*
* Common external function handlers
*/
jerry_value_t jerryx_handler_assert_fatal (const jerry_call_info_t *call_info_p,
const jerry_value_t args_p[],
const jerry_length_t args_cnt);
jerry_value_t jerryx_handler_assert_throw (const jerry_call_info_t *call_info_p,
const jerry_value_t args_p[],
const jerry_length_t args_cnt);
jerry_value_t jerryx_handler_assert (const jerry_call_info_t *call_info_p,
const jerry_value_t args_p[],
const jerry_length_t args_cnt);
jerry_value_t
jerryx_handler_gc (const jerry_call_info_t *call_info_p, const jerry_value_t args_p[], const jerry_length_t args_cnt);
jerry_value_t jerryx_handler_print (const jerry_call_info_t *call_info_p,
const jerry_value_t args_p[],
const jerry_length_t args_cnt);
jerry_value_t jerryx_handler_source_name (const jerry_call_info_t *call_info_p,
const jerry_value_t args_p[],
const jerry_length_t args_cnt);
bool jerryx_register_global (const char *name_p, jerry_external_handler_t handler_p);
/**
* Struct used by the `jerryx_set_functions` method to
@@ -108,12 +86,6 @@ jerryx_register_result jerryx_set_properties (const jerry_value_t target_object,
void jerryx_release_property_entry (const jerryx_property_entry entries[],
const jerryx_register_result register_result);
jerry_value_t jerryx_set_property_str (const jerry_value_t target_object, const char *name, const jerry_value_t value);
jerry_value_t jerryx_get_property_str (const jerry_value_t target_object, const char *name);
bool jerryx_has_property_str (const jerry_value_t target_object, const char *name);
JERRY_C_API_END
#endif /* !JERRYX_HANDLER_H */
#endif /* !JERRYX_PROPERTIES_H */
+27
View File
@@ -0,0 +1,27 @@
/* 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_REPL_H
#define JERRYX_REPL_H
#include "jerryscript-types.h"
JERRY_C_API_BEGIN
void jerryx_repl (const char* prompt_p);
JERRY_C_API_END
#endif /* !JERRYX_REPL_H */
@@ -0,0 +1,31 @@
/* 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_SOURCES_H
#define JERRYX_SOURCES_H
#include "jerryscript-types.h"
JERRY_C_API_BEGIN
jerry_value_t jerryx_source_parse_script (const char* path);
jerry_value_t jerryx_source_exec_script (const char* path);
jerry_value_t jerryx_source_exec_module (const char* path);
jerry_value_t jerryx_source_exec_snapshot (const char* path, size_t function_index);
jerry_value_t jerryx_source_exec_stdin (void);
JERRY_C_API_END
#endif /* !JERRYX_EXEC_H */
@@ -0,0 +1,27 @@
/* 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_TEST262_H
#define JERRYX_TEST262_H
#include "jerryscript-types.h"
JERRY_C_API_BEGIN
void jerryx_test262_register (void);
JERRY_C_API_END
#endif /* !JERRYX_TEST262_H */
+210
View File
@@ -0,0 +1,210 @@
/* 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/handlers.h"
#include "jerryscript-core.h"
#include "jerryscript-debugger.h"
#include "jerryscript-port.h"
#include "jerryscript-ext/print.h"
/**
* Provide a 'print' implementation for scripts.
*
* The routine converts all of its arguments to strings and outputs them
* char-by-char using jerry_port_print_byte.
*
* 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 `jerry_port_print_byte`.
*
* @return undefined - if all arguments could be converted to strings,
* error - otherwise.
*/
jerry_value_t
jerryx_handler_print (const jerry_call_info_t *call_info_p, /**< call information */
const jerry_value_t args_p[], /**< function arguments */
const jerry_length_t args_cnt) /**< number of function arguments */
{
(void) call_info_p; /* unused */
for (jerry_length_t index = 0; index < args_cnt; index++)
{
if (index > 0)
{
jerryx_print_byte (' ');
}
jerry_value_t result = jerryx_print_value (args_p[index]);
if (jerry_value_is_exception (result))
{
return result;
}
}
jerryx_print_byte ('\n');
return jerry_undefined ();
} /* jerryx_handler_print */
/**
* Hard assert for scripts. The routine calls jerry_port_fatal on assertion failure.
*
* Notes:
* * If the `JERRY_FEATURE_LINE_INFO` runtime feature is enabled (build option: `JERRY_LINE_INFO`)
* a backtrace is also printed out.
*
* @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_call_info_t *call_info_p, /**< call information */
const jerry_value_t args_p[], /**< function arguments */
const jerry_length_t args_cnt) /**< number of function arguments */
{
(void) call_info_p; /* unused */
if (args_cnt == 1 && jerry_value_is_true (args_p[0]))
{
return jerry_boolean (true);
}
/* Assert failed, print a bit of JS backtrace */
jerry_log (JERRY_LOG_LEVEL_ERROR, "Script Error: assertion failed\n");
jerryx_print_backtrace (5);
jerry_port_fatal (JERRY_FATAL_FAILED_ASSERTION);
} /* jerryx_handler_assert_fatal */
/**
* Expose garbage collector to scripts.
*
* @return undefined.
*/
jerry_value_t
jerryx_handler_gc (const jerry_call_info_t *call_info_p, /**< call information */
const jerry_value_t args_p[], /**< function arguments */
const jerry_length_t args_cnt) /**< number of function arguments */
{
(void) call_info_p; /* unused */
jerry_gc_mode_t mode =
((args_cnt > 0 && jerry_value_to_boolean (args_p[0])) ? JERRY_GC_PRESSURE_HIGH : JERRY_GC_PRESSURE_LOW);
jerry_heap_gc (mode);
return jerry_undefined ();
} /* jerryx_handler_gc */
/**
* Get the resource name (usually a file name) of the currently executed script or the given function object
*
* Note: returned value must be freed with jerry_value_free, when it is no longer needed
*
* @return JS string constructed from
* - the currently executed function object's resource name, if the given value is undefined
* - resource name of the function object, if the given value is a function object
* - "<anonymous>", otherwise
*/
jerry_value_t
jerryx_handler_source_name (const jerry_call_info_t *call_info_p, /**< call information */
const jerry_value_t args_p[], /**< function arguments */
const jerry_length_t args_cnt) /**< number of function arguments */
{
(void) call_info_p; /* unused */
jerry_value_t undefined_value = jerry_undefined ();
jerry_value_t source_name = jerry_source_name (args_cnt > 0 ? args_p[0] : undefined_value);
jerry_value_free (undefined_value);
return source_name;
} /* jerryx_handler_source_name */
/**
* Create a new realm.
*
* @return new Realm object
*/
jerry_value_t
jerryx_handler_create_realm (const jerry_call_info_t *call_info_p, /**< call information */
const jerry_value_t args_p[], /**< function arguments */
const jerry_length_t args_cnt) /**< number of function arguments */
{
(void) call_info_p; /* unused */
(void) args_p; /* unused */
(void) args_cnt; /* unused */
return jerry_realm ();
} /* jerryx_handler_create_realm */
/**
* Handler for unhandled promise rejection events.
*/
void
jerryx_handler_promise_reject (jerry_promise_event_type_t event_type, /**< event type */
const jerry_value_t object, /**< target object */
const jerry_value_t value, /**< optional argument */
void *user_p) /**< user pointer passed to the callback */
{
(void) value;
(void) user_p;
if (event_type != JERRY_PROMISE_EVENT_REJECT_WITHOUT_HANDLER)
{
return;
}
jerry_value_t result = jerry_promise_result (object);
jerryx_print_unhandled_rejection (result);
jerry_value_free (result);
} /* jerryx_handler_promise_reject */
/**
* Runs the source code received by jerry_debugger_wait_for_client_source.
*
* @return result fo the source code execution
*/
jerry_value_t
jerryx_handler_source_received (const jerry_char_t *source_name_p, /**< resource name */
size_t source_name_size, /**< size of resource name */
const jerry_char_t *source_p, /**< source code */
size_t source_size, /**< source code size */
void *user_p) /**< user pointer */
{
(void) user_p; /* unused */
jerry_parse_options_t parse_options;
parse_options.options = JERRY_PARSE_HAS_SOURCE_NAME;
parse_options.source_name = jerry_string (source_name_p, (jerry_size_t) source_name_size, JERRY_ENCODING_UTF8);
jerry_value_t ret_val = jerry_parse (source_p, source_size, &parse_options);
jerry_value_free (parse_options.source_name);
if (!jerry_value_is_exception (ret_val))
{
jerry_value_t func_val = ret_val;
ret_val = jerry_run (func_val);
jerry_value_free (func_val);
}
return ret_val;
} /* jerryx_handler_source_received */
+367
View File
@@ -0,0 +1,367 @@
/* 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/print.h"
#include <assert.h>
#include <inttypes.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include "jerryscript-core.h"
#include "jerryscript-debugger.h"
#include "jerryscript-port.h"
/**
* Print buffer size
*/
#define JERRYX_PRINT_BUFFER_SIZE 64
/**
* Max line size that will be printed on a Syntax Error
*/
#define JERRYX_SYNTAX_ERROR_MAX_LINE_LENGTH 256
/**
* Struct for buffering print outpu
*/
typedef struct
{
jerry_size_t index; /**< write index */
jerry_char_t data[JERRYX_PRINT_BUFFER_SIZE]; /**< buffer data */
} jerryx_print_buffer_t;
/**
* Callback used by jerryx_print_value to batch written characters and print them in bulk.
* NULL bytes are escaped and written as '\u0000'.
*
* @param value: encoded byte value
* @param user_p: user pointer
*/
static void
jerryx_buffered_print (uint32_t value, void *user_p)
{
jerryx_print_buffer_t *buffer_p = (jerryx_print_buffer_t *) user_p;
if (value == '\0')
{
jerryx_print_buffer (buffer_p->data, buffer_p->index);
buffer_p->index = 0;
jerryx_print_string ("\\u0000");
return;
}
assert (value <= UINT8_MAX);
buffer_p->data[buffer_p->index++] = (uint8_t) value;
if (buffer_p->index >= JERRYX_PRINT_BUFFER_SIZE)
{
jerryx_print_buffer (buffer_p->data, buffer_p->index);
buffer_p->index = 0;
}
} /* jerryx_buffered_print */
/**
* Convert a value to string and print it to standard output.
* NULL characters are escaped to "\u0000", other characters are output as-is.
*
* @param value: input value
*/
jerry_value_t
jerryx_print_value (const jerry_value_t value)
{
jerry_value_t string;
if (jerry_value_is_symbol (value))
{
string = jerry_symbol_descriptive_string (value);
}
else
{
string = jerry_value_to_string (value);
if (jerry_value_is_exception (string))
{
return string;
}
}
jerryx_print_buffer_t buffer;
buffer.index = 0;
jerry_string_iterate (string, JERRY_ENCODING_UTF8, &jerryx_buffered_print, &buffer);
jerry_value_free (string);
jerryx_print_buffer (buffer.data, buffer.index);
return jerry_undefined ();
} /* jerryx_print */
/**
* Print a character to standard output, also sending it to the debugger, if connected.
*
* @param ch: input character
*/
void
jerryx_print_byte (jerry_char_t byte)
{
jerry_port_print_byte (byte);
#if JERRY_DEBUGGER
jerry_debugger_send_output (&byte, 1);
#endif /* JERRY_DEBUGGER */
} /* jerryx_print_char */
/**
* Print a buffer to standard output, also sending it to the debugger, if connected.
*
* @param buffer_p: inptut string buffer
* @param buffer_size: size of the string
*/
void
jerryx_print_buffer (const jerry_char_t *buffer_p, jerry_size_t buffer_size)
{
jerry_port_print_buffer (buffer_p, buffer_size);
#if JERRY_DEBUGGER
jerry_debugger_send_output (buffer_p, buffer_size);
#endif /* JERRY_DEBUGGER */
} /* jerryx_print_buffer */
/**
* Print a zero-terminated string to standard output, also sending it to the debugger, if connected.
*
* @param buffer_p: inptut string buffer
* @param buffer_size: size of the string
*/
void
jerryx_print_string (const char *str_p)
{
const jerry_char_t *buffer_p = (jerry_char_t *) str_p;
jerry_size_t buffer_size = (jerry_size_t) (strlen (str_p));
jerry_port_print_buffer (buffer_p, buffer_size);
#if JERRY_DEBUGGER
jerry_debugger_send_output (buffer_p, buffer_size);
#endif /* JERRY_DEBUGGER */
} /* jerryx_print_string */
/**
* Print backtrace as log messages up to a specific depth.
*
* @param depth: backtrace depth
*/
void
jerryx_print_backtrace (unsigned depth)
{
if (!jerry_feature_enabled (JERRY_FEATURE_LINE_INFO))
{
return;
}
jerry_log (JERRY_LOG_LEVEL_ERROR, "Script backtrace (top %u):\n", depth);
jerry_value_t backtrace_array = jerry_backtrace (depth);
unsigned array_length = jerry_array_length (backtrace_array);
for (unsigned idx = 0; idx < array_length; idx++)
{
jerry_value_t property = jerry_object_get_index (backtrace_array, idx);
jerry_char_t buffer[JERRYX_PRINT_BUFFER_SIZE];
jerry_size_t copied = jerry_string_to_buffer (property, JERRY_ENCODING_UTF8, buffer, JERRYX_PRINT_BUFFER_SIZE - 1);
buffer[copied] = '\0';
jerry_log (JERRY_LOG_LEVEL_ERROR, " %u: %s\n", idx, buffer);
jerry_value_free (property);
}
jerry_value_free (backtrace_array);
} /* jerryx_handler_assert_fatal */
/**
* Print an unhandled exception value
*
* The function will take ownership of the value, and free it.
*
* @param exception: unhandled exception value
*/
void
jerryx_print_unhandled_exception (jerry_value_t exception) /**< exception value */
{
assert (jerry_value_is_exception (exception));
jerry_value_t value = jerry_exception_value (exception, true);
JERRY_VLA (jerry_char_t, buffer_p, JERRYX_PRINT_BUFFER_SIZE);
jerry_value_t string = jerry_value_to_string (value);
jerry_size_t copied = jerry_string_to_buffer (string, JERRY_ENCODING_UTF8, buffer_p, JERRYX_PRINT_BUFFER_SIZE - 1);
buffer_p[copied] = '\0';
if (jerry_feature_enabled (JERRY_FEATURE_ERROR_MESSAGES) && jerry_error_type (value) == JERRY_ERROR_SYNTAX)
{
jerry_char_t *string_end_p = buffer_p + copied;
jerry_size_t err_line = 0;
jerry_size_t err_col = 0;
char *path_str_p = NULL;
char *path_str_end_p = NULL;
/* 1. parse column and line information */
for (jerry_char_t *current_p = buffer_p; current_p < string_end_p; current_p++)
{
if (*current_p == '[')
{
current_p++;
if (*current_p == '<')
{
break;
}
path_str_p = (char *) current_p;
while (current_p < string_end_p && *current_p != ':')
{
current_p++;
}
path_str_end_p = (char *) current_p++;
err_line = (unsigned int) strtol ((char *) current_p, (char **) &current_p, 10);
current_p++;
err_col = (unsigned int) strtol ((char *) current_p, NULL, 10);
break;
}
} /* for */
if (err_line > 0 && err_col > 0 && err_col < JERRYX_SYNTAX_ERROR_MAX_LINE_LENGTH)
{
/* Temporarily modify the error message, so we can use the path. */
*path_str_end_p = '\0';
jerry_size_t source_size;
jerry_char_t *source_p = jerry_port_source_read (path_str_p, &source_size);
/* Revert the error message. */
*path_str_end_p = ':';
if (source_p != NULL)
{
uint32_t curr_line = 1;
jerry_size_t pos = 0;
/* 2. seek and print */
while (pos < source_size && curr_line < err_line)
{
if (source_p[pos] == '\n')
{
curr_line++;
}
pos++;
}
/* Print character if:
* - The max line length is not reached.
* - The current position is valid (it is not the end of the source).
* - The current character is not a newline.
**/
for (uint32_t char_count = 0;
(char_count < JERRYX_SYNTAX_ERROR_MAX_LINE_LENGTH) && (pos < source_size) && (source_p[pos] != '\n');
char_count++, pos++)
{
jerry_log (JERRY_LOG_LEVEL_ERROR, "%c", source_p[pos]);
}
jerry_log (JERRY_LOG_LEVEL_ERROR, "\n");
jerry_port_source_free (source_p);
while (--err_col)
{
jerry_log (JERRY_LOG_LEVEL_ERROR, "~");
}
jerry_log (JERRY_LOG_LEVEL_ERROR, "^\n\n");
}
}
}
jerry_log (JERRY_LOG_LEVEL_ERROR, "Unhandled exception: %s\n", buffer_p);
jerry_value_free (string);
if (jerry_value_is_object (value))
{
jerry_value_t backtrace_val = jerry_object_get_sz (value, "stack");
if (jerry_value_is_array (backtrace_val))
{
uint32_t length = jerry_array_length (backtrace_val);
/* This length should be enough. */
if (length > 32)
{
length = 32;
}
for (unsigned i = 0; i < length; i++)
{
jerry_value_t item_val = jerry_object_get_index (backtrace_val, i);
if (jerry_value_is_string (item_val))
{
copied = jerry_string_to_buffer (item_val, JERRY_ENCODING_UTF8, buffer_p, JERRYX_PRINT_BUFFER_SIZE - 1);
buffer_p[copied] = '\0';
jerry_log (JERRY_LOG_LEVEL_ERROR, " %u: %s\n", i, buffer_p);
}
jerry_value_free (item_val);
}
}
jerry_value_free (backtrace_val);
}
jerry_value_free (value);
} /* jerryx_print_unhandled_exception */
/**
* Print unhandled promise rejection.
*
* @param result: promise rejection result
*/
void
jerryx_print_unhandled_rejection (jerry_value_t result) /**< result value */
{
jerry_value_t reason = jerry_value_to_string (result);
if (!jerry_value_is_exception (reason))
{
JERRY_VLA (jerry_char_t, buffer_p, JERRYX_PRINT_BUFFER_SIZE);
jerry_size_t copied = jerry_string_to_buffer (reason, JERRY_ENCODING_UTF8, buffer_p, JERRYX_PRINT_BUFFER_SIZE - 1);
buffer_p[copied] = '\0';
jerry_log (JERRY_LOG_LEVEL_WARNING, "Uncaught Promise rejection: %s\n", buffer_p);
}
else
{
jerry_log (JERRY_LOG_LEVEL_WARNING, "Uncaught Promise rejection: (reason cannot be converted to string)\n");
}
jerry_value_free (reason);
} /* jerryx_print_unhandled_rejection */
@@ -13,33 +13,34 @@
* limitations under the License.
*/
#include "jerryscript-ext/handler.h"
#include "jerryscript-ext/properties.h"
#include "jerryscript-core.h"
/**
* Register a JavaScript function in the global object.
*
* Note:
* returned value must be freed with jerry_value_free, when it is no longer needed.
*
* @return true value - if the operation was successful,
* error - otherwise.
* @return true - if the operation was successful,
* false - otherwise.
*/
jerry_value_t
jerryx_handler_register_global (const char *name_p, /**< name of the function */
jerry_external_handler_t handler_p) /**< function callback */
bool
jerryx_register_global (const char *name_p, /**< name of the function */
jerry_external_handler_t handler_p) /**< function callback */
{
jerry_value_t global_obj_val = jerry_current_realm ();
jerry_value_t function_name_val = jerry_string_sz (name_p);
jerry_value_t function_val = jerry_function_external (handler_p);
jerry_value_t result_val = jerry_object_set (global_obj_val, function_name_val, function_val);
bool result = jerry_value_is_true (result_val);
jerry_value_free (result_val);
jerry_value_free (function_val);
jerry_value_free (function_name_val);
jerry_value_free (global_obj_val);
return result_val;
} /* jerryx_handler_register_global */
return result;
} /* jerryx_register_global */
/**
* Set multiple properties on a target object.
@@ -115,81 +116,3 @@ jerryx_release_property_entry (const jerryx_property_entry entries[], /**< list
jerry_value_free (entries[idx].value);
}
} /* jerryx_release_property_entry */
/**
* Set a property to a specified value with a given name.
*
* Notes:
* - The operation performed is the same as what the `jerry_object_set` method.
* - The property name must be a zero terminated UTF-8 string.
* - There should be no '\0' (NULL) character in the name excluding the string terminator.
* - Returned value must be freed with jerry_value_free, when it is no longer needed.
*
* @return true value - if the operation was successful
* thrown error - otherwise
*/
jerry_value_t
jerryx_set_property_str (const jerry_value_t target_object, /**< target object */
const char *name, /**< property name */
const jerry_value_t value) /**< value to set */
{
jerry_value_t property_name_val = jerry_string_sz (name);
jerry_value_t result_val = jerry_object_set (target_object, property_name_val, value);
jerry_value_free (property_name_val);
return result_val;
} /* jerryx_set_property_str */
/**
* Get a property value of a specified object.
*
* Notes:
* - The operation performed is the same as what the `jerry_object_get` method.
* - The property name must be a zero terminated UTF-8 string.
* - There should be no '\0' (NULL) character in the name excluding the string terminator.
* - Returned value must be freed with jerry_value_free, when it is no longer needed.
*
* @return jerry_value_t - the property value
*/
jerry_value_t
jerryx_get_property_str (const jerry_value_t target_object, /**< target object */
const char *name) /**< property name */
{
jerry_value_t prop_name = jerry_string_sz (name);
jerry_value_t result_val = jerry_object_get (target_object, prop_name);
jerry_value_free (prop_name);
return result_val;
} /* jerryx_get_property_str */
/**
* Check if a property exists on an object.
*
* Notes:
* - The operation performed is the same as what the `jerry_object_has` method.
* - The property name must be a zero terminated UTF-8 string.
* - There should be no '\0' (NULL) character in the name excluding the string terminator.
*
* @return true - if the property exists on the given object.
* false - if there is no such property or there was an error accessing the property.
*/
bool
jerryx_has_property_str (const jerry_value_t target_object, /**< target object */
const char *name) /**< property name */
{
bool has_property = false;
jerry_value_t prop_name = jerry_string_sz (name);
jerry_value_t has_prop_val = jerry_object_has (target_object, prop_name);
if (!jerry_value_is_exception (has_prop_val))
{
has_property = jerry_value_is_true (has_prop_val);
}
jerry_value_free (has_prop_val);
jerry_value_free (prop_name);
return has_property;
} /* jerryx_has_property_str */
+97
View File
@@ -0,0 +1,97 @@
/* 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/repl.h"
#include <string.h>
#include "jerryscript-port.h"
#include "jerryscript.h"
#include "jerryscript-ext/print.h"
void
jerryx_repl (const char *prompt_p)
{
jerry_value_t result;
while (true)
{
jerryx_print_string (prompt_p);
jerry_size_t length;
jerry_char_t *line_p = jerry_port_line_read (&length);
if (line_p == NULL)
{
jerryx_print_byte ('\n');
return;
}
if (length == 0)
{
continue;
}
if (!jerry_validate_string (line_p, length, JERRY_ENCODING_UTF8))
{
jerry_port_line_free (line_p);
result = jerry_throw_sz (JERRY_ERROR_SYNTAX, "Input is not a valid UTF-8 string");
goto exception;
}
result = jerry_parse (line_p, length, NULL);
jerry_port_line_free (line_p);
if (jerry_value_is_exception (result))
{
goto exception;
}
jerry_value_t script = result;
result = jerry_run (script);
jerry_value_free (script);
if (jerry_value_is_exception (result))
{
goto exception;
}
jerry_value_t print_result = jerryx_print_value (result);
jerry_value_free (result);
result = print_result;
if (jerry_value_is_exception (result))
{
goto exception;
}
jerryx_print_byte ('\n');
jerry_value_free (result);
result = jerry_run_jobs ();
if (jerry_value_is_exception (result))
{
goto exception;
}
jerry_value_free (result);
continue;
exception:
jerryx_print_unhandled_exception (result);
}
} /* jerryx_repl */
+174
View File
@@ -0,0 +1,174 @@
/* 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/sources.h"
#include <assert.h>
#include <inttypes.h>
#include <stdlib.h>
#include <string.h>
#include "jerryscript-debugger.h"
#include "jerryscript-port.h"
#include "jerryscript.h"
#include "jerryscript-ext/print.h"
jerry_value_t
jerryx_source_parse_script (const char *path_p)
{
jerry_size_t source_size;
jerry_char_t *source_p = jerry_port_source_read (path_p, &source_size);
if (source_p == NULL)
{
jerry_log (JERRY_LOG_LEVEL_ERROR, "Failed to open file: %s\n", path_p);
return jerry_throw_sz (JERRY_ERROR_SYNTAX, "Source file not found");
}
if (!jerry_validate_string (source_p, source_size, JERRY_ENCODING_UTF8))
{
jerry_port_source_free (source_p);
return jerry_throw_sz (JERRY_ERROR_SYNTAX, "Input is not a valid UTF-8 encoded string.");
}
jerry_parse_options_t parse_options;
parse_options.options = JERRY_PARSE_HAS_SOURCE_NAME;
parse_options.source_name =
jerry_string ((const jerry_char_t *) path_p, (jerry_size_t) strlen (path_p), JERRY_ENCODING_UTF8);
jerry_value_t result = jerry_parse (source_p, source_size, &parse_options);
jerry_value_free (parse_options.source_name);
jerry_port_source_free (source_p);
return result;
} /* jerryx_source_parse_script */
jerry_value_t
jerryx_source_exec_script (const char *path_p)
{
jerry_value_t result = jerryx_source_parse_script (path_p);
if (!jerry_value_is_exception (result))
{
jerry_value_t script = result;
result = jerry_run (script);
jerry_value_free (script);
}
return result;
} /* jerryx_source_exec_script */
jerry_value_t
jerryx_source_exec_module (const char *path_p)
{
jerry_value_t specifier =
jerry_string ((const jerry_char_t *) path_p, (jerry_size_t) strlen (path_p), JERRY_ENCODING_UTF8);
jerry_value_t referrer = jerry_undefined ();
jerry_value_t module = jerry_module_resolve (specifier, referrer, NULL);
jerry_value_free (referrer);
jerry_value_free (specifier);
if (jerry_value_is_exception (module))
{
return module;
}
if (jerry_module_state (module) == JERRY_MODULE_STATE_UNLINKED)
{
jerry_value_t link_result = jerry_module_link (module, NULL, NULL);
if (jerry_value_is_exception (link_result))
{
jerry_value_free (module);
return link_result;
}
jerry_value_free (link_result);
}
jerry_value_t result = jerry_module_evaluate (module);
jerry_value_free (module);
jerry_module_cleanup (jerry_undefined ());
return result;
} /* jerryx_source_exec_module */
jerry_value_t
jerryx_source_exec_snapshot (const char *path_p, size_t function_index)
{
jerry_size_t source_size;
jerry_char_t *source_p = jerry_port_source_read (path_p, &source_size);
if (source_p == NULL)
{
jerry_log (JERRY_LOG_LEVEL_ERROR, "Failed to open file: %s\n", path_p);
return jerry_throw_sz (JERRY_ERROR_SYNTAX, "Snapshot file not found");
}
jerry_value_t result =
jerry_exec_snapshot ((uint32_t *) source_p, source_size, function_index, JERRY_SNAPSHOT_EXEC_COPY_DATA, NULL);
jerry_port_source_free (source_p);
return result;
} /* jerryx_source_exec_snapshot */
jerry_value_t
jerryx_source_exec_stdin (void)
{
jerry_char_t *source_p = NULL;
jerry_size_t source_size = 0;
while (true)
{
jerry_size_t line_size;
jerry_char_t *line_p = jerry_port_line_read (&line_size);
if (line_p == NULL)
{
break;
}
jerry_size_t new_size = source_size + line_size;
source_p = realloc (source_p, new_size);
memcpy (source_p + source_size, line_p, line_size);
jerry_port_line_free (line_p);
source_size = new_size;
}
if (!jerry_validate_string (source_p, source_size, JERRY_ENCODING_UTF8))
{
free (source_p);
return jerry_throw_sz (JERRY_ERROR_SYNTAX, "Input is not a valid UTF-8 encoded string.");
}
jerry_value_t result = jerry_parse (source_p, source_size, NULL);
free (source_p);
if (jerry_value_is_exception (result))
{
return result;
}
jerry_value_t script = result;
result = jerry_run (script);
jerry_value_free (script);
return result;
} /* jerryx_source_exec_stdin */
+163
View File
@@ -0,0 +1,163 @@
/* 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/test262.h"
#include <assert.h>
#include <string.h>
#include "jerryscript.h"
#include "jerryscript-ext/handlers.h"
/**
* Register a method for the $262 object.
*/
static void
jerryx_test262_register_function (jerry_value_t test262_obj, /** $262 object */
const char *name_p, /**< name of the function */
jerry_external_handler_t handler_p) /**< function callback */
{
jerry_value_t function_val = jerry_function_external (handler_p);
jerry_value_t result_val = jerry_object_set_sz (test262_obj, name_p, function_val);
jerry_value_free (function_val);
assert (!jerry_value_is_exception (result_val));
jerry_value_free (result_val);
} /* jerryx_test262_register_function */
/**
* $262.detachArrayBuffer
*
* A function which implements the DetachArrayBuffer abstract operation
*
* @return null value - if success
* value marked with error flag - otherwise
*/
static jerry_value_t
jerryx_test262_detach_array_buffer (const jerry_call_info_t *call_info_p, /**< call information */
const jerry_value_t args_p[], /**< function arguments */
const jerry_length_t args_cnt) /**< number of function arguments */
{
(void) call_info_p; /* unused */
if (args_cnt < 1 || !jerry_value_is_arraybuffer (args_p[0]))
{
return jerry_throw_sz (JERRY_ERROR_TYPE, "Expected an ArrayBuffer object");
}
/* TODO: support the optional 'key' argument */
return jerry_arraybuffer_detach (args_p[0]);
} /* jerryx_test262_detach_array_buffer */
/**
* $262.evalScript
*
* A function which accepts a string value as its first argument and executes it
*
* @return completion of the script parsing and execution.
*/
static jerry_value_t
jerryx_test262_eval_script (const jerry_call_info_t *call_info_p, /**< call information */
const jerry_value_t args_p[], /**< function arguments */
const jerry_length_t args_cnt) /**< number of function arguments */
{
(void) call_info_p; /* unused */
if (args_cnt < 1 || !jerry_value_is_string (args_p[0]))
{
return jerry_throw_sz (JERRY_ERROR_TYPE, "Expected a string");
}
jerry_value_t ret_value = jerry_parse_value (args_p[0], NULL);
if (!jerry_value_is_exception (ret_value))
{
jerry_value_t func_val = ret_value;
ret_value = jerry_run (func_val);
jerry_value_free (func_val);
}
return ret_value;
} /* jerryx_test262_eval_script */
static jerry_value_t jerryx_test262_create (jerry_value_t global_obj);
/**
* $262.createRealm
*
* A function which creates a new realm object, and returns a newly created $262 object
*
* @return a new $262 object
*/
static jerry_value_t
jerryx_test262_create_realm (const jerry_call_info_t *call_info_p, /**< call information */
const jerry_value_t args_p[], /**< function arguments */
const jerry_length_t args_cnt) /**< number of function arguments */
{
(void) call_info_p; /* unused */
(void) args_p; /* unused */
(void) args_cnt; /* unused */
jerry_value_t realm_object = jerry_realm ();
jerry_value_t previous_realm = jerry_set_realm (realm_object);
assert (!jerry_value_is_exception (previous_realm));
jerry_value_t test262_object = jerryx_test262_create (realm_object);
jerry_set_realm (previous_realm);
jerry_value_free (realm_object);
return test262_object;
} /* jerryx_test262_create_realm */
/**
* Create a new $262 object
*
* @return a new $262 object
*/
static jerry_value_t
jerryx_test262_create (jerry_value_t global_obj) /**< global object */
{
jerry_value_t test262_object = jerry_object ();
jerryx_test262_register_function (test262_object, "detachArrayBuffer", jerryx_test262_detach_array_buffer);
jerryx_test262_register_function (test262_object, "evalScript", jerryx_test262_eval_script);
jerryx_test262_register_function (test262_object, "createRealm", jerryx_test262_create_realm);
jerryx_test262_register_function (test262_object, "gc", jerryx_handler_gc);
jerry_value_t result = jerry_object_set_sz (test262_object, "global", global_obj);
assert (!jerry_value_is_exception (result));
jerry_value_free (result);
return test262_object;
} /* create_test262 */
/**
* Add a new test262 object to the current global object.
*/
void
jerryx_test262_register (void)
{
jerry_value_t global_obj = jerry_current_realm ();
jerry_value_t test262_obj = jerryx_test262_create (global_obj);
jerry_value_t result = jerry_object_set_sz (global_obj, "$262", test262_obj);
assert (!jerry_value_is_exception (result));
jerry_value_free (result);
jerry_value_free (test262_obj);
jerry_value_free (global_obj);
} /* jerryx_test262_register */