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:
@@ -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 */
|
||||
@@ -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 **) ¤t_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 */
|
||||
@@ -0,0 +1,118 @@
|
||||
/* 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/properties.h"
|
||||
|
||||
#include "jerryscript-core.h"
|
||||
|
||||
/**
|
||||
* Register a JavaScript function in the global object.
|
||||
*
|
||||
* @return true - if the operation was successful,
|
||||
* false - otherwise.
|
||||
*/
|
||||
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;
|
||||
} /* jerryx_register_global */
|
||||
|
||||
/**
|
||||
* Set multiple properties on a target object.
|
||||
*
|
||||
* The properties are an array of (name, property value) pairs and
|
||||
* this list must end with a (NULL, 0) entry.
|
||||
*
|
||||
* Notes:
|
||||
* - Each property value in the input array is released after a successful property registration.
|
||||
* - 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.
|
||||
* - The method `jerryx_release_property_entry` must be called if there is any failed registration
|
||||
* to release the values in the entries array.
|
||||
*
|
||||
* @return `jerryx_register_result` struct - if everything is ok with the (undefined, property entry count) values.
|
||||
* In case of error the (error object, registered property count) pair.
|
||||
*/
|
||||
jerryx_register_result
|
||||
jerryx_set_properties (const jerry_value_t target_object, /**< target object */
|
||||
const jerryx_property_entry entries[]) /**< array of method entries */
|
||||
{
|
||||
#define JERRYX_SET_PROPERTIES_RESULT(VALUE, IDX) ((jerryx_register_result){ VALUE, IDX })
|
||||
uint32_t idx = 0;
|
||||
|
||||
if (entries == NULL)
|
||||
{
|
||||
return JERRYX_SET_PROPERTIES_RESULT (jerry_undefined (), 0);
|
||||
}
|
||||
|
||||
for (; (entries[idx].name != NULL); idx++)
|
||||
{
|
||||
const jerryx_property_entry *entry = &entries[idx];
|
||||
|
||||
jerry_value_t prop_name = jerry_string_sz (entry->name);
|
||||
jerry_value_t result = jerry_object_set (target_object, prop_name, entry->value);
|
||||
|
||||
jerry_value_free (prop_name);
|
||||
|
||||
// By API definition:
|
||||
// The jerry_object_set returns TRUE if there is no problem
|
||||
// and error object if there is any problem.
|
||||
// Thus there is no need to check if the boolean value is false or not.
|
||||
if (!jerry_value_is_boolean (result))
|
||||
{
|
||||
return JERRYX_SET_PROPERTIES_RESULT (result, idx);
|
||||
}
|
||||
|
||||
jerry_value_free (entry->value);
|
||||
jerry_value_free (result);
|
||||
}
|
||||
|
||||
return JERRYX_SET_PROPERTIES_RESULT (jerry_undefined (), idx);
|
||||
#undef JERRYX_SET_PROPERTIES_RESULT
|
||||
} /* jerryx_set_properties */
|
||||
|
||||
/**
|
||||
* Release all jerry_value_t in a jerryx_property_entry array based on
|
||||
* a previous jerryx_set_properties call.
|
||||
*
|
||||
* In case of a successful registration it is safe to call this method.
|
||||
*/
|
||||
void
|
||||
jerryx_release_property_entry (const jerryx_property_entry entries[], /**< list of property entries */
|
||||
const jerryx_register_result register_result) /**< previous result of registration */
|
||||
{
|
||||
if (entries == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
for (uint32_t idx = register_result.registered; entries[idx].name != NULL; idx++)
|
||||
{
|
||||
jerry_value_free (entries[idx].value);
|
||||
}
|
||||
} /* jerryx_release_property_entry */
|
||||
@@ -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 */
|
||||
@@ -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 */
|
||||
@@ -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 */
|
||||
Reference in New Issue
Block a user