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:
@@ -58,25 +58,25 @@ endmacro()
|
||||
|
||||
# Jerry with libfuzzer support
|
||||
if(JERRY_LIBFUZZER)
|
||||
jerry_create_executable("jerry-libfuzzer" "libfuzzer.c")
|
||||
target_link_libraries("jerry-libfuzzer" jerry-port-default -fsanitize=fuzzer)
|
||||
jerry_create_executable("jerry-libfuzzer" "main-libfuzzer.c")
|
||||
target_link_libraries("jerry-libfuzzer" jerry-port-fsanitize=fuzzer)
|
||||
endif()
|
||||
|
||||
# Jerry standalones
|
||||
if(JERRY_CMDLINE)
|
||||
jerry_create_executable("jerry" "main-jerry.c" "main-utils.c" "main-options.c" "cli.c")
|
||||
target_link_libraries("jerry" jerry-ext jerry-port-default)
|
||||
jerry_create_executable("jerry" "main-desktop.c" "arguments/options.c" "arguments/cli.c")
|
||||
target_link_libraries("jerry" jerry-ext jerry-port)
|
||||
endif()
|
||||
|
||||
if(JERRY_CMDLINE_TEST)
|
||||
jerry_create_executable("jerry-test" "main-jerry-test.c" "benchmarking.c")
|
||||
target_link_libraries("jerry-test" jerry-port-default)
|
||||
jerry_create_executable("jerry-test" "benchmark/main-benchmark.c" "benchmark/stubs.c")
|
||||
target_link_libraries("jerry-test" jerry-port)
|
||||
if (JERRY_TEST_STACK_MEASURE)
|
||||
target_compile_definitions("jerry-test" PRIVATE -DJERRY_TEST_STACK_MEASURE=1)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
if(JERRY_CMDLINE_SNAPSHOT)
|
||||
jerry_create_executable("jerry-snapshot" "main-jerry-snapshot.c" "cli.c")
|
||||
target_link_libraries("jerry-snapshot" jerry-port-default)
|
||||
jerry_create_executable("jerry-snapshot" "main-snapshot.c" "arguments/cli.c")
|
||||
target_link_libraries("jerry-snapshot" jerry-port)
|
||||
endif()
|
||||
|
||||
@@ -13,17 +13,17 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "main-options.h"
|
||||
#include "options.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "jerryscript-port-default.h"
|
||||
#include "jerryscript-port.h"
|
||||
#include "jerryscript.h"
|
||||
|
||||
#include "cli.h"
|
||||
#include "main-utils.h"
|
||||
#include "jerryscript-ext/print.h"
|
||||
|
||||
/**
|
||||
* Command line option IDs
|
||||
@@ -108,7 +108,7 @@ static const cli_opt_t main_opts[] = {
|
||||
* Check whether a usage-related condition holds. If not, print an error
|
||||
* message, print the usage, and terminate the application.
|
||||
*/
|
||||
static void
|
||||
static bool
|
||||
check_usage (bool condition, /**< the condition that must hold */
|
||||
const char *name, /**< name of the application (argv[0]) */
|
||||
const char *msg, /**< error message to print if condition does not hold */
|
||||
@@ -116,9 +116,11 @@ check_usage (bool condition, /**< the condition that must hold */
|
||||
{
|
||||
if (!condition)
|
||||
{
|
||||
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "%s: %s%s\n", name, msg, opt != NULL ? opt : "");
|
||||
exit (JERRY_STANDALONE_EXIT_CODE_FAIL);
|
||||
jerry_log (JERRY_LOG_LEVEL_ERROR, "%s: %s%s\n", name, msg, opt != NULL ? opt : "");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
} /* check_usage */
|
||||
|
||||
/**
|
||||
@@ -133,17 +135,21 @@ check_feature (jerry_feature_t feature, /**< feature to check */
|
||||
{
|
||||
if (!jerry_feature_enabled (feature))
|
||||
{
|
||||
jerry_port_default_set_log_level (JERRY_LOG_LEVEL_WARNING);
|
||||
jerry_port_log (JERRY_LOG_LEVEL_WARNING, "Ignoring '%s' option because this feature is disabled!\n", option);
|
||||
jerry_log_set_level (JERRY_LOG_LEVEL_WARNING);
|
||||
jerry_log (JERRY_LOG_LEVEL_WARNING, "Ignoring '%s' option because this feature is disabled!\n", option);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
} /* check_feature */
|
||||
|
||||
/**
|
||||
* parse input arguments
|
||||
* Parse input arguments
|
||||
*
|
||||
* @return true, if application should continue execution
|
||||
* false, if application should exit
|
||||
*/
|
||||
void
|
||||
bool
|
||||
main_parse_args (int argc, /**< argc */
|
||||
char **argv, /**< argv */
|
||||
main_args_t *arguments_p) /**< [in/out] arguments reference */
|
||||
@@ -159,6 +165,8 @@ main_parse_args (int argc, /**< argc */
|
||||
arguments_p->init_flags = JERRY_INIT_EMPTY;
|
||||
arguments_p->option_flags = OPT_FLAG_EMPTY;
|
||||
|
||||
arguments_p->parse_result = JERRY_STANDALONE_EXIT_CODE_FAIL;
|
||||
|
||||
cli_state_t cli_state = cli_init (main_opts, argc, argv);
|
||||
for (int id = cli_consume_option (&cli_state); id != CLI_OPT_END; id = cli_consume_option (&cli_state))
|
||||
{
|
||||
@@ -167,9 +175,9 @@ main_parse_args (int argc, /**< argc */
|
||||
case OPT_HELP:
|
||||
{
|
||||
cli_help (argv[0], NULL, main_opts);
|
||||
exit (JERRY_STANDALONE_EXIT_CODE_OK);
|
||||
|
||||
break;
|
||||
arguments_p->parse_result = JERRY_STANDALONE_EXIT_CODE_OK;
|
||||
return false;
|
||||
}
|
||||
case OPT_VERSION:
|
||||
{
|
||||
@@ -178,15 +186,15 @@ main_parse_args (int argc, /**< argc */
|
||||
JERRY_API_MINOR_VERSION,
|
||||
JERRY_API_PATCH_VERSION,
|
||||
JERRY_COMMIT_HASH);
|
||||
exit (JERRY_STANDALONE_EXIT_CODE_OK);
|
||||
|
||||
break;
|
||||
arguments_p->parse_result = JERRY_STANDALONE_EXIT_CODE_OK;
|
||||
return false;
|
||||
}
|
||||
case OPT_MEM_STATS:
|
||||
{
|
||||
if (check_feature (JERRY_FEATURE_HEAP_STATS, cli_state.arg))
|
||||
{
|
||||
jerry_port_default_set_log_level (JERRY_LOG_LEVEL_DEBUG);
|
||||
jerry_log_set_level (JERRY_LOG_LEVEL_DEBUG);
|
||||
arguments_p->init_flags |= JERRY_INIT_MEM_STATS;
|
||||
}
|
||||
break;
|
||||
@@ -205,7 +213,7 @@ main_parse_args (int argc, /**< argc */
|
||||
{
|
||||
if (check_feature (JERRY_FEATURE_PARSER_DUMP, cli_state.arg))
|
||||
{
|
||||
jerry_port_default_set_log_level (JERRY_LOG_LEVEL_DEBUG);
|
||||
jerry_log_set_level (JERRY_LOG_LEVEL_DEBUG);
|
||||
arguments_p->init_flags |= JERRY_INIT_SHOW_OPCODES;
|
||||
}
|
||||
break;
|
||||
@@ -219,7 +227,7 @@ main_parse_args (int argc, /**< argc */
|
||||
{
|
||||
if (check_feature (JERRY_FEATURE_REGEXP_DUMP, cli_state.arg))
|
||||
{
|
||||
jerry_port_default_set_log_level (JERRY_LOG_LEVEL_DEBUG);
|
||||
jerry_log_set_level (JERRY_LOG_LEVEL_DEBUG);
|
||||
arguments_p->init_flags |= JERRY_INIT_SHOW_REGEXP_OPCODES;
|
||||
}
|
||||
break;
|
||||
@@ -245,10 +253,13 @@ main_parse_args (int argc, /**< argc */
|
||||
if (check_feature (JERRY_FEATURE_DEBUGGER, cli_state.arg))
|
||||
{
|
||||
const char *debug_channel = cli_consume_string (&cli_state);
|
||||
check_usage (!strcmp (debug_channel, "websocket") || !strcmp (debug_channel, "rawpacket"),
|
||||
argv[0],
|
||||
"Error: invalid value for --debug-channel: ",
|
||||
cli_state.arg);
|
||||
if (!check_usage (!strcmp (debug_channel, "websocket") || !strcmp (debug_channel, "rawpacket"),
|
||||
argv[0],
|
||||
"Error: invalid value for --debug-channel: ",
|
||||
cli_state.arg))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
arguments_p->debug_channel = debug_channel;
|
||||
}
|
||||
@@ -259,10 +270,14 @@ main_parse_args (int argc, /**< argc */
|
||||
if (check_feature (JERRY_FEATURE_DEBUGGER, cli_state.arg))
|
||||
{
|
||||
const char *debug_protocol = cli_consume_string (&cli_state);
|
||||
check_usage (!strcmp (debug_protocol, "tcp") || !strcmp (debug_protocol, "serial"),
|
||||
argv[0],
|
||||
"Error: invalid value for --debug-protocol: ",
|
||||
cli_state.arg);
|
||||
|
||||
if (!check_usage (!strcmp (debug_protocol, "tcp") || !strcmp (debug_protocol, "serial"),
|
||||
argv[0],
|
||||
"Error: invalid value for --debug-protocol: ",
|
||||
cli_state.arg))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
arguments_p->debug_protocol = debug_protocol;
|
||||
}
|
||||
@@ -335,12 +350,15 @@ main_parse_args (int argc, /**< argc */
|
||||
case OPT_LOG_LEVEL:
|
||||
{
|
||||
long int log_level = cli_consume_int (&cli_state);
|
||||
check_usage (log_level >= 0 && log_level <= 3,
|
||||
argv[0],
|
||||
"Error: invalid value for --log-level: ",
|
||||
cli_state.arg);
|
||||
if (!check_usage (log_level >= 0 && log_level <= 3,
|
||||
argv[0],
|
||||
"Error: invalid value for --log-level: ",
|
||||
cli_state.arg))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
jerry_port_default_set_log_level ((jerry_log_level_t) log_level);
|
||||
jerry_log_set_level ((jerry_log_level_t) log_level);
|
||||
break;
|
||||
}
|
||||
case OPT_NO_PROMPT:
|
||||
@@ -374,13 +392,16 @@ main_parse_args (int argc, /**< argc */
|
||||
{
|
||||
if (cli_state.arg != NULL)
|
||||
{
|
||||
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: %s %s\n", cli_state.error, cli_state.arg);
|
||||
jerry_log (JERRY_LOG_LEVEL_ERROR, "Error: %s %s\n", cli_state.error, cli_state.arg);
|
||||
}
|
||||
else
|
||||
{
|
||||
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: %s\n", cli_state.error);
|
||||
jerry_log (JERRY_LOG_LEVEL_ERROR, "Error: %s\n", cli_state.error);
|
||||
}
|
||||
|
||||
exit (JERRY_STANDALONE_EXIT_CODE_FAIL);
|
||||
return false;
|
||||
}
|
||||
|
||||
arguments_p->parse_result = JERRY_STANDALONE_EXIT_CODE_OK;
|
||||
return true;
|
||||
} /* main_parse_args */
|
||||
@@ -16,8 +16,15 @@
|
||||
#ifndef MAIN_OPTIONS_H
|
||||
#define MAIN_OPTIONS_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
/**
|
||||
* Standalone Jerry exit codes
|
||||
*/
|
||||
#define JERRY_STANDALONE_EXIT_CODE_OK (0)
|
||||
#define JERRY_STANDALONE_EXIT_CODE_FAIL (1)
|
||||
|
||||
/**
|
||||
* Argument option flags.
|
||||
*/
|
||||
@@ -69,8 +76,9 @@ typedef struct
|
||||
|
||||
uint16_t option_flags;
|
||||
uint16_t init_flags;
|
||||
uint8_t parse_result;
|
||||
} main_args_t;
|
||||
|
||||
void main_parse_args (int argc, char **argv, main_args_t *arguments_p);
|
||||
bool main_parse_args (int argc, char **argv, main_args_t *arguments_p);
|
||||
|
||||
#endif /* !MAIN_OPTIONS_H */
|
||||
@@ -13,6 +13,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
@@ -38,14 +39,14 @@ read_file (const char *file_name, size_t *out_size_p)
|
||||
FILE *file = fopen (file_name, "rb");
|
||||
if (file == NULL)
|
||||
{
|
||||
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: failed to open file: %s\n", file_name);
|
||||
jerry_log (JERRY_LOG_LEVEL_ERROR, "Error: failed to open file: %s\n", file_name);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
size_t bytes_read = fread (buffer, 1u, sizeof (buffer), file);
|
||||
if (!bytes_read)
|
||||
{
|
||||
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: failed to read file: %s\n", file_name);
|
||||
jerry_log (JERRY_LOG_LEVEL_ERROR, "Error: failed to read file: %s\n", file_name);
|
||||
fclose (file);
|
||||
return NULL;
|
||||
}
|
||||
@@ -210,7 +211,7 @@ stack_usage (uint32_t *stack_top_p, size_t length_in_bytes)
|
||||
stack_p++;
|
||||
}
|
||||
|
||||
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Used stack: %d\n", (int) ((uint8_t *) stack_bottom_p - (uint8_t *) stack_p));
|
||||
jerry_log (JERRY_LOG_LEVEL_ERROR, "Used stack: %d\n", (int) ((uint8_t *) stack_bottom_p - (uint8_t *) stack_p));
|
||||
} /* stack_usage */
|
||||
|
||||
#else /* (JERRY_TEST_STACK_MEASURE) && (JERRY_TEST_STACK_MEASURE) */
|
||||
@@ -226,7 +227,7 @@ main (int main_argc, char **main_argv)
|
||||
{
|
||||
double d;
|
||||
unsigned u;
|
||||
} now = { .d = jerry_port_get_current_time () };
|
||||
} now = { .d = jerry_port_current_time () };
|
||||
srand (now.u);
|
||||
|
||||
argc = main_argc;
|
||||
@@ -247,7 +248,7 @@ main (int main_argc, char **main_argv)
|
||||
|
||||
if (result == JERRY_STANDALONE_EXIT_CODE_FAIL)
|
||||
{
|
||||
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Unhandled exception: Script Error!\n");
|
||||
jerry_log (JERRY_LOG_LEVEL_ERROR, "Unhandled exception: Script Error!\n");
|
||||
}
|
||||
|
||||
return result;
|
||||
@@ -0,0 +1,267 @@
|
||||
/* 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 <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "jerryscript-port.h"
|
||||
#include "jerryscript.h"
|
||||
|
||||
#include "arguments/options.h"
|
||||
#include "jerryscript-ext/debugger.h"
|
||||
#include "jerryscript-ext/handlers.h"
|
||||
#include "jerryscript-ext/print.h"
|
||||
#include "jerryscript-ext/properties.h"
|
||||
#include "jerryscript-ext/repl.h"
|
||||
#include "jerryscript-ext/sources.h"
|
||||
#include "jerryscript-ext/test262.h"
|
||||
|
||||
/**
|
||||
* Initialize random seed
|
||||
*/
|
||||
static void
|
||||
main_init_random_seed (void)
|
||||
{
|
||||
union
|
||||
{
|
||||
double d;
|
||||
unsigned u;
|
||||
} now = { .d = jerry_port_current_time () };
|
||||
|
||||
srand (now.u);
|
||||
} /* main_init_random_seed */
|
||||
|
||||
/**
|
||||
* Initialize debugger
|
||||
*/
|
||||
static bool
|
||||
main_init_debugger (main_args_t *arguments_p) /**< main arguments */
|
||||
{
|
||||
bool result = false;
|
||||
|
||||
if (!strcmp (arguments_p->debug_protocol, "tcp"))
|
||||
{
|
||||
result = jerryx_debugger_tcp_create (arguments_p->debug_port);
|
||||
}
|
||||
else
|
||||
{
|
||||
assert (!strcmp (arguments_p->debug_protocol, "serial"));
|
||||
result = jerryx_debugger_serial_create (arguments_p->debug_serial_config);
|
||||
}
|
||||
|
||||
if (!strcmp (arguments_p->debug_channel, "rawpacket"))
|
||||
{
|
||||
result = result && jerryx_debugger_rp_create ();
|
||||
}
|
||||
else
|
||||
{
|
||||
assert (!strcmp (arguments_p->debug_channel, "websocket"));
|
||||
result = result && jerryx_debugger_ws_create ();
|
||||
}
|
||||
|
||||
jerryx_debugger_after_connect (result);
|
||||
return result;
|
||||
} /* main_init_debugger */
|
||||
|
||||
/**
|
||||
* Inits the engine and the debugger
|
||||
*/
|
||||
static void
|
||||
main_init_engine (main_args_t *arguments_p) /**< main arguments */
|
||||
{
|
||||
jerry_init (arguments_p->init_flags);
|
||||
|
||||
jerry_promise_on_event (JERRY_PROMISE_EVENT_FILTER_ERROR, jerryx_handler_promise_reject, NULL);
|
||||
|
||||
if (arguments_p->option_flags & OPT_FLAG_DEBUG_SERVER)
|
||||
{
|
||||
if (!main_init_debugger (arguments_p))
|
||||
{
|
||||
jerry_log (JERRY_LOG_LEVEL_WARNING, "Failed to initialize debugger!\n");
|
||||
}
|
||||
}
|
||||
|
||||
if (arguments_p->option_flags & OPT_FLAG_TEST262_OBJECT)
|
||||
{
|
||||
jerryx_test262_register ();
|
||||
}
|
||||
|
||||
jerryx_register_global ("assert", jerryx_handler_assert);
|
||||
jerryx_register_global ("gc", jerryx_handler_gc);
|
||||
jerryx_register_global ("print", jerryx_handler_print);
|
||||
jerryx_register_global ("sourceName", jerryx_handler_source_name);
|
||||
jerryx_register_global ("createRealm", jerryx_handler_create_realm);
|
||||
} /* main_init_engine */
|
||||
|
||||
int
|
||||
main (int argc, char **argv)
|
||||
{
|
||||
main_init_random_seed ();
|
||||
JERRY_VLA (main_source_t, sources_p, argc);
|
||||
|
||||
main_args_t arguments;
|
||||
arguments.sources_p = sources_p;
|
||||
|
||||
if (!main_parse_args (argc, argv, &arguments))
|
||||
{
|
||||
return arguments.parse_result;
|
||||
}
|
||||
|
||||
restart:
|
||||
main_init_engine (&arguments);
|
||||
int return_code = JERRY_STANDALONE_EXIT_CODE_FAIL;
|
||||
jerry_value_t result;
|
||||
|
||||
for (uint32_t source_index = 0; source_index < arguments.source_count; source_index++)
|
||||
{
|
||||
main_source_t *source_file_p = sources_p + source_index;
|
||||
const char *file_path_p = argv[source_file_p->path_index];
|
||||
|
||||
switch (source_file_p->type)
|
||||
{
|
||||
case SOURCE_MODULE:
|
||||
{
|
||||
result = jerryx_source_exec_module (file_path_p);
|
||||
break;
|
||||
}
|
||||
case SOURCE_SNAPSHOT:
|
||||
{
|
||||
result = jerryx_source_exec_snapshot (file_path_p, source_file_p->snapshot_index);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
assert (source_file_p->type == SOURCE_SCRIPT);
|
||||
|
||||
if ((arguments.option_flags & OPT_FLAG_PARSE_ONLY) != 0)
|
||||
{
|
||||
result = jerryx_source_parse_script (file_path_p);
|
||||
}
|
||||
else
|
||||
{
|
||||
result = jerryx_source_exec_script (file_path_p);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (jerry_value_is_exception (result))
|
||||
{
|
||||
if (jerryx_debugger_is_reset (result))
|
||||
{
|
||||
jerry_cleanup ();
|
||||
|
||||
goto restart;
|
||||
}
|
||||
|
||||
jerryx_print_unhandled_exception (result);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
jerry_value_free (result);
|
||||
}
|
||||
|
||||
if (arguments.option_flags & OPT_FLAG_WAIT_SOURCE)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
jerry_debugger_wait_for_source_status_t receive_status;
|
||||
receive_status = jerry_debugger_wait_for_client_source (jerryx_handler_source_received, NULL, &result);
|
||||
|
||||
if (receive_status == JERRY_DEBUGGER_SOURCE_RECEIVE_FAILED)
|
||||
{
|
||||
jerry_log (JERRY_LOG_LEVEL_ERROR, "Connection aborted before source arrived.");
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if (receive_status == JERRY_DEBUGGER_SOURCE_END)
|
||||
{
|
||||
jerry_log (JERRY_LOG_LEVEL_DEBUG, "No more client source.\n");
|
||||
break;
|
||||
}
|
||||
|
||||
assert (receive_status == JERRY_DEBUGGER_CONTEXT_RESET_RECEIVED
|
||||
|| receive_status == JERRY_DEBUGGER_SOURCE_RECEIVED);
|
||||
|
||||
if (receive_status == JERRY_DEBUGGER_CONTEXT_RESET_RECEIVED || jerryx_debugger_is_reset (result))
|
||||
{
|
||||
jerry_cleanup ();
|
||||
goto restart;
|
||||
}
|
||||
|
||||
assert (receive_status == JERRY_DEBUGGER_SOURCE_RECEIVED);
|
||||
jerry_value_free (result);
|
||||
}
|
||||
}
|
||||
else if (arguments.option_flags & OPT_FLAG_USE_STDIN)
|
||||
{
|
||||
result = jerryx_source_exec_stdin ();
|
||||
|
||||
if (jerry_value_is_exception (result))
|
||||
{
|
||||
jerryx_print_unhandled_exception (result);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
jerry_value_free (result);
|
||||
}
|
||||
else if (arguments.source_count == 0)
|
||||
{
|
||||
const char *prompt_p = (arguments.option_flags & OPT_FLAG_NO_PROMPT) ? "" : "jerry> ";
|
||||
jerryx_repl (prompt_p);
|
||||
}
|
||||
|
||||
result = jerry_run_jobs ();
|
||||
|
||||
if (jerry_value_is_exception (result))
|
||||
{
|
||||
jerryx_print_unhandled_exception (result);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
jerry_value_free (result);
|
||||
|
||||
if (arguments.exit_cb_name_p != NULL)
|
||||
{
|
||||
jerry_value_t global = jerry_current_realm ();
|
||||
jerry_value_t callback_fn = jerry_object_get_sz (global, arguments.exit_cb_name_p);
|
||||
jerry_value_free (global);
|
||||
|
||||
if (jerry_value_is_function (callback_fn))
|
||||
{
|
||||
result = jerry_call (callback_fn, jerry_undefined (), NULL, 0);
|
||||
|
||||
if (jerry_value_is_exception (result))
|
||||
{
|
||||
jerryx_print_unhandled_exception (result);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
jerry_value_free (result);
|
||||
}
|
||||
|
||||
jerry_value_free (callback_fn);
|
||||
}
|
||||
|
||||
return_code = JERRY_STANDALONE_EXIT_CODE_OK;
|
||||
|
||||
exit:
|
||||
jerry_cleanup ();
|
||||
|
||||
return return_code;
|
||||
} /* main */
|
||||
@@ -1,371 +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 <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "jerryscript-port-default.h"
|
||||
#include "jerryscript-port.h"
|
||||
#include "jerryscript.h"
|
||||
|
||||
#include "jerryscript-ext/debugger.h"
|
||||
#include "jerryscript-ext/handler.h"
|
||||
#include "main-options.h"
|
||||
#include "main-utils.h"
|
||||
|
||||
/**
|
||||
* Temporal buffer size.
|
||||
*/
|
||||
#define JERRY_BUFFER_SIZE 256u
|
||||
|
||||
#if defined(JERRY_EXTERNAL_CONTEXT) && (JERRY_EXTERNAL_CONTEXT == 1)
|
||||
/**
|
||||
* The alloc function passed to jerry_context_create
|
||||
*/
|
||||
static void *
|
||||
context_alloc (size_t size, void *cb_data_p)
|
||||
{
|
||||
(void) cb_data_p; /* unused */
|
||||
return malloc (size);
|
||||
} /* context_alloc */
|
||||
#endif /* defined (JERRY_EXTERNAL_CONTEXT) && (JERRY_EXTERNAL_CONTEXT == 1) */
|
||||
|
||||
int
|
||||
main (int argc, char **argv)
|
||||
{
|
||||
union
|
||||
{
|
||||
double d;
|
||||
unsigned u;
|
||||
} now = { .d = jerry_port_get_current_time () };
|
||||
srand (now.u);
|
||||
|
||||
JERRY_VLA (main_source_t, sources_p, argc);
|
||||
|
||||
main_args_t arguments;
|
||||
arguments.sources_p = sources_p;
|
||||
|
||||
main_parse_args (argc, argv, &arguments);
|
||||
|
||||
#if defined(JERRY_EXTERNAL_CONTEXT) && (JERRY_EXTERNAL_CONTEXT == 1)
|
||||
jerry_context_t *context_p = jerry_context_alloc (JERRY_GLOBAL_HEAP_SIZE * 1024, context_alloc, NULL);
|
||||
jerry_port_default_set_current_context (context_p);
|
||||
#endif /* defined (JERRY_EXTERNAL_CONTEXT) && (JERRY_EXTERNAL_CONTEXT == 1) */
|
||||
|
||||
restart:
|
||||
main_init_engine (&arguments);
|
||||
int return_code = JERRY_STANDALONE_EXIT_CODE_FAIL;
|
||||
jerry_value_t ret_value;
|
||||
|
||||
for (uint32_t source_index = 0; source_index < arguments.source_count; source_index++)
|
||||
{
|
||||
main_source_t *source_file_p = sources_p + source_index;
|
||||
const char *file_path_p = argv[source_file_p->path_index];
|
||||
|
||||
if (source_file_p->type == SOURCE_MODULE)
|
||||
{
|
||||
jerry_value_t specifier =
|
||||
jerry_string ((const jerry_char_t *) file_path_p, (jerry_size_t) strlen (file_path_p), JERRY_ENCODING_UTF8);
|
||||
jerry_value_t referrer = jerry_undefined ();
|
||||
ret_value = jerry_port_module_resolve (specifier, referrer, NULL);
|
||||
jerry_value_free (referrer);
|
||||
jerry_value_free (specifier);
|
||||
|
||||
if (!jerry_value_is_exception (ret_value))
|
||||
{
|
||||
if (jerry_module_state (ret_value) != JERRY_MODULE_STATE_UNLINKED)
|
||||
{
|
||||
/* A module can be evaluated only once. */
|
||||
jerry_value_free (ret_value);
|
||||
continue;
|
||||
}
|
||||
|
||||
jerry_value_t link_val = jerry_module_link (ret_value, NULL, NULL);
|
||||
|
||||
if (jerry_value_is_exception (link_val))
|
||||
{
|
||||
jerry_value_free (ret_value);
|
||||
ret_value = link_val;
|
||||
}
|
||||
else
|
||||
{
|
||||
jerry_value_free (link_val);
|
||||
|
||||
jerry_value_t module_val = ret_value;
|
||||
ret_value = jerry_module_evaluate (module_val);
|
||||
jerry_value_free (module_val);
|
||||
}
|
||||
}
|
||||
|
||||
if (jerry_value_is_exception (ret_value))
|
||||
{
|
||||
main_print_unhandled_exception (ret_value);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
jerry_value_free (ret_value);
|
||||
continue;
|
||||
}
|
||||
|
||||
size_t source_size;
|
||||
uint8_t *source_p = jerry_port_read_source (file_path_p, &source_size);
|
||||
|
||||
if (source_p == NULL)
|
||||
{
|
||||
goto exit;
|
||||
}
|
||||
|
||||
switch (source_file_p->type)
|
||||
{
|
||||
case SOURCE_SNAPSHOT:
|
||||
{
|
||||
ret_value = jerry_exec_snapshot ((uint32_t *) source_p,
|
||||
source_size,
|
||||
source_file_p->snapshot_index,
|
||||
JERRY_SNAPSHOT_EXEC_COPY_DATA,
|
||||
NULL);
|
||||
|
||||
jerry_port_release_source (source_p);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
assert (source_file_p->type == SOURCE_SCRIPT || source_file_p->type == SOURCE_MODULE);
|
||||
|
||||
if (!jerry_validate_string ((jerry_char_t *) source_p, (jerry_size_t) source_size, JERRY_ENCODING_UTF8))
|
||||
{
|
||||
jerry_port_release_source (source_p);
|
||||
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: Input must be a valid UTF-8 string.");
|
||||
goto exit;
|
||||
}
|
||||
|
||||
jerry_parse_options_t parse_options;
|
||||
parse_options.options = JERRY_PARSE_HAS_SOURCE_NAME;
|
||||
parse_options.source_name =
|
||||
jerry_string ((const jerry_char_t *) file_path_p, (jerry_size_t) strlen (file_path_p), JERRY_ENCODING_UTF8);
|
||||
|
||||
ret_value = jerry_parse (source_p, source_size, &parse_options);
|
||||
|
||||
jerry_value_free (parse_options.source_name);
|
||||
jerry_port_release_source (source_p);
|
||||
|
||||
if (!jerry_value_is_exception (ret_value) && !(arguments.option_flags & OPT_FLAG_PARSE_ONLY))
|
||||
{
|
||||
jerry_value_t func_val = ret_value;
|
||||
ret_value = jerry_run (func_val);
|
||||
jerry_value_free (func_val);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (jerry_value_is_exception (ret_value))
|
||||
{
|
||||
if (main_is_value_reset (ret_value))
|
||||
{
|
||||
jerry_cleanup ();
|
||||
|
||||
goto restart;
|
||||
}
|
||||
|
||||
main_print_unhandled_exception (ret_value);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
jerry_value_free (ret_value);
|
||||
}
|
||||
|
||||
if (arguments.option_flags & OPT_FLAG_WAIT_SOURCE)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
jerry_debugger_wait_for_source_status_t receive_status;
|
||||
receive_status = jerry_debugger_wait_for_client_source (main_wait_for_source_callback, NULL, &ret_value);
|
||||
|
||||
if (receive_status == JERRY_DEBUGGER_SOURCE_RECEIVE_FAILED)
|
||||
{
|
||||
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Connection aborted before source arrived.");
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if (receive_status == JERRY_DEBUGGER_SOURCE_END)
|
||||
{
|
||||
jerry_port_log (JERRY_LOG_LEVEL_DEBUG, "No more client source.\n");
|
||||
break;
|
||||
}
|
||||
|
||||
assert (receive_status == JERRY_DEBUGGER_CONTEXT_RESET_RECEIVED
|
||||
|| receive_status == JERRY_DEBUGGER_SOURCE_RECEIVED);
|
||||
|
||||
if (receive_status == JERRY_DEBUGGER_CONTEXT_RESET_RECEIVED || main_is_value_reset (ret_value))
|
||||
{
|
||||
jerry_cleanup ();
|
||||
goto restart;
|
||||
}
|
||||
|
||||
assert (receive_status == JERRY_DEBUGGER_SOURCE_RECEIVED);
|
||||
jerry_value_free (ret_value);
|
||||
}
|
||||
}
|
||||
else if (arguments.option_flags & OPT_FLAG_USE_STDIN)
|
||||
{
|
||||
char buffer[JERRY_BUFFER_SIZE];
|
||||
char *source_p = NULL;
|
||||
size_t source_size = 0;
|
||||
|
||||
while (!feof (stdin))
|
||||
{
|
||||
size_t read_bytes = fread (buffer, 1u, JERRY_BUFFER_SIZE, stdin);
|
||||
|
||||
size_t new_size = source_size + read_bytes;
|
||||
source_p = realloc (source_p, new_size);
|
||||
|
||||
memcpy (source_p + source_size, buffer, read_bytes);
|
||||
source_size = new_size;
|
||||
}
|
||||
|
||||
ret_value = jerry_parse ((jerry_char_t *) source_p, source_size, NULL);
|
||||
free (source_p);
|
||||
|
||||
if (jerry_value_is_exception (ret_value))
|
||||
{
|
||||
main_print_unhandled_exception (ret_value);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
jerry_value_t func_val = ret_value;
|
||||
ret_value = jerry_run (func_val);
|
||||
jerry_value_free (func_val);
|
||||
|
||||
if (jerry_value_is_exception (ret_value))
|
||||
{
|
||||
main_print_unhandled_exception (ret_value);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
jerry_value_free (ret_value);
|
||||
}
|
||||
else if (arguments.source_count == 0)
|
||||
{
|
||||
const char *prompt = (arguments.option_flags & OPT_FLAG_NO_PROMPT) ? "" : "jerry> ";
|
||||
char buffer[JERRY_BUFFER_SIZE];
|
||||
|
||||
while (true)
|
||||
{
|
||||
printf ("%s", prompt);
|
||||
char *str_p = fgets (buffer, JERRY_BUFFER_SIZE, stdin);
|
||||
|
||||
if (str_p == NULL)
|
||||
{
|
||||
printf ("\n");
|
||||
break;
|
||||
}
|
||||
|
||||
size_t len = strlen (str_p);
|
||||
|
||||
if (len == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!jerry_validate_string ((jerry_char_t *) str_p, (jerry_size_t) len, JERRY_ENCODING_UTF8))
|
||||
{
|
||||
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: Input must be a valid UTF-8 string.\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
ret_value = jerry_parse ((jerry_char_t *) str_p, len, NULL);
|
||||
|
||||
if (jerry_value_is_exception (ret_value))
|
||||
{
|
||||
main_print_unhandled_exception (ret_value);
|
||||
continue;
|
||||
}
|
||||
|
||||
jerry_value_t func_val = ret_value;
|
||||
ret_value = jerry_run (func_val);
|
||||
jerry_value_free (func_val);
|
||||
|
||||
if (jerry_value_is_exception (ret_value))
|
||||
{
|
||||
main_print_unhandled_exception (ret_value);
|
||||
continue;
|
||||
}
|
||||
|
||||
const jerry_value_t args[] = { ret_value };
|
||||
jerry_value_t ret_val_print = jerryx_handler_print (NULL, args, 1);
|
||||
jerry_value_free (ret_val_print);
|
||||
jerry_value_free (ret_value);
|
||||
ret_value = jerry_run_jobs ();
|
||||
|
||||
if (jerry_value_is_exception (ret_value))
|
||||
{
|
||||
main_print_unhandled_exception (ret_value);
|
||||
continue;
|
||||
}
|
||||
|
||||
jerry_value_free (ret_value);
|
||||
}
|
||||
}
|
||||
|
||||
ret_value = jerry_run_jobs ();
|
||||
|
||||
if (jerry_value_is_exception (ret_value))
|
||||
{
|
||||
main_print_unhandled_exception (ret_value);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
jerry_value_free (ret_value);
|
||||
|
||||
if (arguments.exit_cb_name_p != NULL)
|
||||
{
|
||||
jerry_value_t global = jerry_current_realm ();
|
||||
jerry_value_t name_str = jerry_string_sz (arguments.exit_cb_name_p);
|
||||
jerry_value_t callback_fn = jerry_object_get (global, name_str);
|
||||
|
||||
jerry_value_free (global);
|
||||
jerry_value_free (name_str);
|
||||
|
||||
if (jerry_value_is_function (callback_fn))
|
||||
{
|
||||
ret_value = jerry_call (callback_fn, jerry_undefined (), NULL, 0);
|
||||
|
||||
if (jerry_value_is_exception (ret_value))
|
||||
{
|
||||
main_print_unhandled_exception (ret_value);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
jerry_value_free (ret_value);
|
||||
}
|
||||
|
||||
jerry_value_free (callback_fn);
|
||||
}
|
||||
|
||||
return_code = JERRY_STANDALONE_EXIT_CODE_OK;
|
||||
|
||||
exit:
|
||||
jerry_cleanup ();
|
||||
|
||||
#if defined(JERRY_EXTERNAL_CONTEXT) && (JERRY_EXTERNAL_CONTEXT == 1)
|
||||
free (context_p);
|
||||
#endif /* defined (JERRY_EXTERNAL_CONTEXT) && (JERRY_EXTERNAL_CONTEXT == 1) */
|
||||
|
||||
return return_code;
|
||||
} /* main */
|
||||
@@ -14,14 +14,14 @@
|
||||
*/
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "jerryscript-port-default.h"
|
||||
#include "jerryscript-port.h"
|
||||
#include "jerryscript.h"
|
||||
|
||||
#include "cli.h"
|
||||
#include "arguments/cli.h"
|
||||
|
||||
/**
|
||||
* Maximum size for loaded snapshots
|
||||
@@ -46,29 +46,6 @@ static const char *output_file_name_p = "js.snapshot";
|
||||
static jerry_length_t magic_string_lengths[JERRY_LITERAL_LENGTH];
|
||||
static const jerry_char_t *magic_string_items[JERRY_LITERAL_LENGTH];
|
||||
|
||||
#if defined(JERRY_EXTERNAL_CONTEXT) && (JERRY_EXTERNAL_CONTEXT == 1)
|
||||
/**
|
||||
* The alloc function passed to jerry_context_create
|
||||
*/
|
||||
static void *
|
||||
context_alloc (size_t size, void *cb_data_p)
|
||||
{
|
||||
(void) cb_data_p; /* unused */
|
||||
return malloc (size);
|
||||
} /* context_alloc */
|
||||
|
||||
/**
|
||||
* Create and set the default external context.
|
||||
*/
|
||||
static void
|
||||
context_init (void)
|
||||
{
|
||||
jerry_context_t *context_p = jerry_context_alloc (JERRY_GLOBAL_HEAP_SIZE * 1024, context_alloc, NULL);
|
||||
jerry_port_default_set_current_context (context_p);
|
||||
} /* context_init */
|
||||
|
||||
#endif /* defined (JERRY_EXTERNAL_CONTEXT) && (JERRY_EXTERNAL_CONTEXT == 1) */
|
||||
|
||||
/**
|
||||
* Check whether JerryScript has a requested feature enabled or not. If not,
|
||||
* print a warning message.
|
||||
@@ -81,8 +58,8 @@ check_feature (jerry_feature_t feature, /**< feature to check */
|
||||
{
|
||||
if (!jerry_feature_enabled (feature))
|
||||
{
|
||||
jerry_port_default_set_log_level (JERRY_LOG_LEVEL_WARNING);
|
||||
jerry_port_log (JERRY_LOG_LEVEL_WARNING, "Ignoring '%s' option because this feature is disabled!\n", option);
|
||||
jerry_log_set_level (JERRY_LOG_LEVEL_WARNING);
|
||||
jerry_log (JERRY_LOG_LEVEL_WARNING, "Ignoring '%s' option because this feature is disabled!\n", option);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
@@ -101,11 +78,11 @@ check_cli_error (const cli_state_t *const cli_state_p)
|
||||
{
|
||||
if (cli_state_p->arg != NULL)
|
||||
{
|
||||
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: %s %s\n", cli_state_p->error, cli_state_p->arg);
|
||||
jerry_log (JERRY_LOG_LEVEL_ERROR, "Error: %s %s\n", cli_state_p->error, cli_state_p->arg);
|
||||
}
|
||||
else
|
||||
{
|
||||
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: %s\n", cli_state_p->error);
|
||||
jerry_log (JERRY_LOG_LEVEL_ERROR, "Error: %s\n", cli_state_p->error);
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -128,7 +105,7 @@ read_file (uint8_t *input_pos_p, /**< next position in the input buffer */
|
||||
|
||||
if (file == NULL)
|
||||
{
|
||||
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: failed to open file: %s\n", file_name);
|
||||
jerry_log (JERRY_LOG_LEVEL_ERROR, "Error: failed to open file: %s\n", file_name);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -139,13 +116,13 @@ read_file (uint8_t *input_pos_p, /**< next position in the input buffer */
|
||||
|
||||
if (bytes_read == 0)
|
||||
{
|
||||
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: failed to read file: %s\n", file_name);
|
||||
jerry_log (JERRY_LOG_LEVEL_ERROR, "Error: failed to read file: %s\n", file_name);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (bytes_read == max_size)
|
||||
{
|
||||
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: file too large: %s\n", file_name);
|
||||
jerry_log (JERRY_LOG_LEVEL_ERROR, "Error: file too large: %s\n", file_name);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -166,7 +143,7 @@ print_unhandled_exception (jerry_value_t error_value) /**< error value */
|
||||
if (jerry_value_is_exception (err_str_val))
|
||||
{
|
||||
/* Avoid recursive error throws. */
|
||||
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Snapshot error: [value cannot be converted to string]\n");
|
||||
jerry_log (JERRY_LOG_LEVEL_ERROR, "Snapshot error: [value cannot be converted to string]\n");
|
||||
jerry_value_free (err_str_val);
|
||||
return;
|
||||
}
|
||||
@@ -175,7 +152,7 @@ print_unhandled_exception (jerry_value_t error_value) /**< error value */
|
||||
jerry_size_t bytes = jerry_string_to_buffer (err_str_val, JERRY_ENCODING_UTF8, err_str_buf, sizeof (err_str_buf) - 1);
|
||||
err_str_buf[bytes] = '\0';
|
||||
|
||||
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Snapshot error: %s\n", (char *) err_str_buf);
|
||||
jerry_log (JERRY_LOG_LEVEL_ERROR, "Snapshot error: %s\n", (char *) err_str_buf);
|
||||
jerry_value_free (err_str_val);
|
||||
} /* print_unhandled_exception */
|
||||
|
||||
@@ -266,7 +243,7 @@ process_generate (cli_state_t *cli_state_p, /**< cli state */
|
||||
{
|
||||
if (check_feature (JERRY_FEATURE_PARSER_DUMP, cli_state_p->arg))
|
||||
{
|
||||
jerry_port_default_set_log_level (JERRY_LOG_LEVEL_DEBUG);
|
||||
jerry_log_set_level (JERRY_LOG_LEVEL_DEBUG);
|
||||
flags |= JERRY_INIT_SHOW_OPCODES;
|
||||
}
|
||||
break;
|
||||
@@ -280,7 +257,7 @@ process_generate (cli_state_t *cli_state_p, /**< cli state */
|
||||
{
|
||||
if (file_name_p != NULL)
|
||||
{
|
||||
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: Exactly one input file must be specified\n");
|
||||
jerry_log (JERRY_LOG_LEVEL_ERROR, "Error: Exactly one input file must be specified\n");
|
||||
return JERRY_STANDALONE_EXIT_CODE_FAIL;
|
||||
}
|
||||
|
||||
@@ -292,7 +269,7 @@ process_generate (cli_state_t *cli_state_p, /**< cli state */
|
||||
|
||||
if (source_length == 0)
|
||||
{
|
||||
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Input file is empty\n");
|
||||
jerry_log (JERRY_LOG_LEVEL_ERROR, "Input file is empty\n");
|
||||
return JERRY_STANDALONE_EXIT_CODE_FAIL;
|
||||
}
|
||||
}
|
||||
@@ -313,19 +290,15 @@ process_generate (cli_state_t *cli_state_p, /**< cli state */
|
||||
|
||||
if (file_name_p == NULL)
|
||||
{
|
||||
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: Exactly one input file must be specified\n");
|
||||
jerry_log (JERRY_LOG_LEVEL_ERROR, "Error: Exactly one input file must be specified\n");
|
||||
return JERRY_STANDALONE_EXIT_CODE_FAIL;
|
||||
}
|
||||
|
||||
#if defined(JERRY_EXTERNAL_CONTEXT) && (JERRY_EXTERNAL_CONTEXT == 1)
|
||||
context_init ();
|
||||
#endif /* defined (JERRY_EXTERNAL_CONTEXT) && (JERRY_EXTERNAL_CONTEXT == 1) */
|
||||
|
||||
jerry_init (flags);
|
||||
|
||||
if (!jerry_validate_string (source_p, (jerry_size_t) source_length, JERRY_ENCODING_UTF8))
|
||||
{
|
||||
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: Input must be a valid UTF-8 string.\n");
|
||||
jerry_log (JERRY_LOG_LEVEL_ERROR, "Error: Input must be a valid UTF-8 string.\n");
|
||||
jerry_cleanup ();
|
||||
return JERRY_STANDALONE_EXIT_CODE_FAIL;
|
||||
}
|
||||
@@ -395,7 +368,7 @@ process_generate (cli_state_t *cli_state_p, /**< cli state */
|
||||
|
||||
if (jerry_value_is_exception (snapshot_result))
|
||||
{
|
||||
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: Generating snapshot failed!\n");
|
||||
jerry_log (JERRY_LOG_LEVEL_ERROR, "Error: Generating snapshot failed!\n");
|
||||
|
||||
snapshot_result = jerry_exception_value (snapshot_result, true);
|
||||
|
||||
@@ -412,7 +385,7 @@ process_generate (cli_state_t *cli_state_p, /**< cli state */
|
||||
FILE *snapshot_file_p = fopen (output_file_name_p, "wb");
|
||||
if (snapshot_file_p == NULL)
|
||||
{
|
||||
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: Unable to write snapshot file: '%s'\n", output_file_name_p);
|
||||
jerry_log (JERRY_LOG_LEVEL_ERROR, "Error: Unable to write snapshot file: '%s'\n", output_file_name_p);
|
||||
jerry_cleanup ();
|
||||
return JERRY_STANDALONE_EXIT_CODE_FAIL;
|
||||
}
|
||||
@@ -491,7 +464,7 @@ process_literal_dump (cli_state_t *cli_state_p, /**< cli state */
|
||||
}
|
||||
else
|
||||
{
|
||||
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: Unsupported literal dump format.");
|
||||
jerry_log (JERRY_LOG_LEVEL_ERROR, "Error: Unsupported literal dump format.");
|
||||
return JERRY_STANDALONE_EXIT_CODE_FAIL;
|
||||
}
|
||||
break;
|
||||
@@ -538,7 +511,7 @@ process_literal_dump (cli_state_t *cli_state_p, /**< cli state */
|
||||
|
||||
if (number_of_files < 1)
|
||||
{
|
||||
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: at least one input file must be specified.\n");
|
||||
jerry_log (JERRY_LOG_LEVEL_ERROR, "Error: at least one input file must be specified.\n");
|
||||
return JERRY_STANDALONE_EXIT_CODE_FAIL;
|
||||
}
|
||||
|
||||
@@ -570,7 +543,7 @@ process_literal_dump (cli_state_t *cli_state_p, /**< cli state */
|
||||
|
||||
if (merged_snapshot_size == 0)
|
||||
{
|
||||
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: %s\n", error_p);
|
||||
jerry_log (JERRY_LOG_LEVEL_ERROR, "Error: %s\n", error_p);
|
||||
jerry_cleanup ();
|
||||
return JERRY_STANDALONE_EXIT_CODE_FAIL;
|
||||
}
|
||||
@@ -586,8 +559,8 @@ process_literal_dump (cli_state_t *cli_state_p, /**< cli state */
|
||||
|
||||
if (lit_buf_sz == 0)
|
||||
{
|
||||
jerry_port_log (JERRY_LOG_LEVEL_ERROR,
|
||||
"Error: Literal saving failed! No literals were found in the input snapshot(s).\n");
|
||||
jerry_log (JERRY_LOG_LEVEL_ERROR,
|
||||
"Error: Literal saving failed! No literals were found in the input snapshot(s).\n");
|
||||
jerry_cleanup ();
|
||||
return JERRY_STANDALONE_EXIT_CODE_FAIL;
|
||||
}
|
||||
@@ -601,7 +574,7 @@ process_literal_dump (cli_state_t *cli_state_p, /**< cli state */
|
||||
|
||||
if (file_p == NULL)
|
||||
{
|
||||
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: cannot open file: '%s'\n", literals_file_name_p);
|
||||
jerry_log (JERRY_LOG_LEVEL_ERROR, "Error: cannot open file: '%s'\n", literals_file_name_p);
|
||||
jerry_cleanup ();
|
||||
return JERRY_STANDALONE_EXIT_CODE_FAIL;
|
||||
}
|
||||
@@ -702,7 +675,7 @@ process_merge (cli_state_t *cli_state_p, /**< cli state */
|
||||
|
||||
if (number_of_files < 2)
|
||||
{
|
||||
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: at least two input files must be passed.\n");
|
||||
jerry_log (JERRY_LOG_LEVEL_ERROR, "Error: at least two input files must be passed.\n");
|
||||
return JERRY_STANDALONE_EXIT_CODE_FAIL;
|
||||
}
|
||||
|
||||
@@ -722,7 +695,7 @@ process_merge (cli_state_t *cli_state_p, /**< cli state */
|
||||
|
||||
if (merged_snapshot_size == 0)
|
||||
{
|
||||
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: %s\n", error_p);
|
||||
jerry_log (JERRY_LOG_LEVEL_ERROR, "Error: %s\n", error_p);
|
||||
jerry_cleanup ();
|
||||
return JERRY_STANDALONE_EXIT_CODE_FAIL;
|
||||
}
|
||||
@@ -731,7 +704,7 @@ process_merge (cli_state_t *cli_state_p, /**< cli state */
|
||||
|
||||
if (file_p == NULL)
|
||||
{
|
||||
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: cannot open file: '%s'\n", output_file_name_p);
|
||||
jerry_log (JERRY_LOG_LEVEL_ERROR, "Error: cannot open file: '%s'\n", output_file_name_p);
|
||||
jerry_cleanup ();
|
||||
return JERRY_STANDALONE_EXIT_CODE_FAIL;
|
||||
}
|
||||
@@ -820,7 +793,7 @@ main (int argc, /**< number of arguments */
|
||||
return process_generate (&cli_state, argc, argv[0]);
|
||||
}
|
||||
|
||||
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: unknown command: %s\n\n", command_p);
|
||||
jerry_log (JERRY_LOG_LEVEL_ERROR, "Error: unknown command: %s\n\n", command_p);
|
||||
print_commands (argv[0]);
|
||||
|
||||
return JERRY_STANDALONE_EXIT_CODE_FAIL;
|
||||
@@ -1,522 +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 "main-utils.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "jerryscript-port-default.h"
|
||||
#include "jerryscript-port.h"
|
||||
#include "jerryscript.h"
|
||||
|
||||
#include "jerryscript-ext/debugger.h"
|
||||
#include "jerryscript-ext/handler.h"
|
||||
#include "main-options.h"
|
||||
|
||||
/**
|
||||
* Max line size that will be printed on a Syntax Error
|
||||
*/
|
||||
#define SYNTAX_ERROR_MAX_LINE_LENGTH 256
|
||||
|
||||
/**
|
||||
* Register a JavaScript function in the global object.
|
||||
*/
|
||||
static void
|
||||
main_register_global_function (const char *name_p, /**< name of the function */
|
||||
jerry_external_handler_t handler_p) /**< function callback */
|
||||
{
|
||||
jerry_value_t result_val = jerryx_handler_register_global (name_p, handler_p);
|
||||
assert (!jerry_value_is_exception (result_val));
|
||||
jerry_value_free (result_val);
|
||||
} /* main_register_global_function */
|
||||
|
||||
static jerry_value_t
|
||||
main_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 ();
|
||||
} /* main_create_realm */
|
||||
|
||||
/**
|
||||
* Register a method for the $262 object.
|
||||
*/
|
||||
static void
|
||||
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_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 (test262_obj, function_name_val, function_val);
|
||||
|
||||
jerry_value_free (function_val);
|
||||
jerry_value_free (function_name_val);
|
||||
|
||||
assert (!jerry_value_is_exception (result_val));
|
||||
jerry_value_free (result_val);
|
||||
} /* 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
|
||||
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]);
|
||||
} /* 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
|
||||
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_size_t str_size = jerry_string_size (args_p[0], JERRY_ENCODING_UTF8);
|
||||
jerry_char_t *str_buf_p = malloc (str_size * sizeof (jerry_char_t));
|
||||
|
||||
if (str_buf_p == NULL)
|
||||
{
|
||||
return jerry_throw_sz (JERRY_ERROR_RANGE, "Internal allocation error");
|
||||
}
|
||||
|
||||
jerry_string_to_buffer (args_p[0], JERRY_ENCODING_UTF8, str_buf_p, str_size);
|
||||
|
||||
jerry_value_t ret_value = jerry_parse (str_buf_p, str_size, 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);
|
||||
}
|
||||
|
||||
free (str_buf_p);
|
||||
|
||||
return ret_value;
|
||||
} /* test262_eval_script */
|
||||
|
||||
static jerry_value_t create_test262 (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
|
||||
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 = create_test262 (realm_object);
|
||||
jerry_set_realm (previous_realm);
|
||||
jerry_value_free (realm_object);
|
||||
|
||||
return test262_object;
|
||||
} /* test262_create_realm */
|
||||
|
||||
/**
|
||||
* Create a new $262 object
|
||||
*
|
||||
* @return a new $262 object
|
||||
*/
|
||||
static jerry_value_t
|
||||
create_test262 (jerry_value_t global_obj) /**< global object */
|
||||
{
|
||||
jerry_value_t test262_object = jerry_object ();
|
||||
|
||||
test262_register_function (test262_object, "detachArrayBuffer", test262_detach_array_buffer);
|
||||
test262_register_function (test262_object, "evalScript", test262_eval_script);
|
||||
test262_register_function (test262_object, "createRealm", test262_create_realm);
|
||||
test262_register_function (test262_object, "gc", jerryx_handler_gc);
|
||||
|
||||
jerry_value_t prop_name = jerry_string_sz ("global");
|
||||
jerry_value_t result = jerry_object_set (test262_object, prop_name, global_obj);
|
||||
assert (!jerry_value_is_exception (result));
|
||||
|
||||
jerry_value_free (prop_name);
|
||||
jerry_value_free (result);
|
||||
|
||||
prop_name = jerry_string_sz ("$262");
|
||||
result = jerry_object_set (global_obj, prop_name, test262_object);
|
||||
assert (!jerry_value_is_exception (result));
|
||||
|
||||
jerry_value_free (prop_name);
|
||||
jerry_value_free (result);
|
||||
|
||||
return test262_object;
|
||||
} /* create_test262 */
|
||||
|
||||
static void
|
||||
promise_callback (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; /* unused */
|
||||
(void) user_p; /* unused */
|
||||
const jerry_size_t max_allowed_size = 5 * 1024 - 1;
|
||||
|
||||
if (event_type != JERRY_PROMISE_EVENT_REJECT_WITHOUT_HANDLER)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
jerry_value_t reason = jerry_promise_result (object);
|
||||
jerry_value_t reason_to_string = jerry_value_to_string (reason);
|
||||
|
||||
if (!jerry_value_is_exception (reason_to_string))
|
||||
{
|
||||
jerry_size_t buffer_size = jerry_string_size (reason_to_string, JERRY_ENCODING_UTF8);
|
||||
|
||||
if (buffer_size > max_allowed_size)
|
||||
{
|
||||
buffer_size = max_allowed_size;
|
||||
}
|
||||
|
||||
JERRY_VLA (jerry_char_t, str_buf_p, buffer_size + 1);
|
||||
jerry_string_to_buffer (reason_to_string, JERRY_ENCODING_UTF8, str_buf_p, buffer_size);
|
||||
str_buf_p[buffer_size] = '\0';
|
||||
|
||||
jerry_port_log (JERRY_LOG_LEVEL_WARNING, "Uncaught Promise rejection: %s\n", str_buf_p);
|
||||
}
|
||||
else
|
||||
{
|
||||
jerry_port_log (JERRY_LOG_LEVEL_WARNING, "Uncaught Promise rejection (reason cannot be converted to string)\n");
|
||||
}
|
||||
|
||||
jerry_value_free (reason_to_string);
|
||||
jerry_value_free (reason);
|
||||
} /* promise_callback */
|
||||
|
||||
/**
|
||||
* Inits the engine and the debugger
|
||||
*/
|
||||
void
|
||||
main_init_engine (main_args_t *arguments_p) /**< main arguments */
|
||||
{
|
||||
jerry_init (arguments_p->init_flags);
|
||||
|
||||
jerry_promise_on_event (JERRY_PROMISE_EVENT_FILTER_ERROR, promise_callback, NULL);
|
||||
|
||||
if (arguments_p->option_flags & OPT_FLAG_DEBUG_SERVER)
|
||||
{
|
||||
bool protocol = false;
|
||||
|
||||
if (!strcmp (arguments_p->debug_protocol, "tcp"))
|
||||
{
|
||||
protocol = jerryx_debugger_tcp_create (arguments_p->debug_port);
|
||||
}
|
||||
else
|
||||
{
|
||||
assert (!strcmp (arguments_p->debug_protocol, "serial"));
|
||||
protocol = jerryx_debugger_serial_create (arguments_p->debug_serial_config);
|
||||
}
|
||||
|
||||
if (!strcmp (arguments_p->debug_channel, "rawpacket"))
|
||||
{
|
||||
jerryx_debugger_after_connect (protocol && jerryx_debugger_rp_create ());
|
||||
}
|
||||
else
|
||||
{
|
||||
assert (!strcmp (arguments_p->debug_channel, "websocket"));
|
||||
jerryx_debugger_after_connect (protocol && jerryx_debugger_ws_create ());
|
||||
}
|
||||
}
|
||||
if (arguments_p->option_flags & OPT_FLAG_TEST262_OBJECT)
|
||||
{
|
||||
jerry_value_t global_obj = jerry_current_realm ();
|
||||
jerry_value_t test262_object = create_test262 (global_obj);
|
||||
jerry_value_free (test262_object);
|
||||
jerry_value_free (global_obj);
|
||||
}
|
||||
main_register_global_function ("assert", jerryx_handler_assert);
|
||||
main_register_global_function ("gc", jerryx_handler_gc);
|
||||
main_register_global_function ("print", jerryx_handler_print);
|
||||
main_register_global_function ("sourceName", jerryx_handler_source_name);
|
||||
main_register_global_function ("createRealm", main_create_realm);
|
||||
} /* main_init_engine */
|
||||
|
||||
/**
|
||||
* Print an error value.
|
||||
*
|
||||
* Note: the error value will be released.
|
||||
*/
|
||||
void
|
||||
main_print_unhandled_exception (jerry_value_t error_value) /**< error value */
|
||||
{
|
||||
assert (jerry_value_is_exception (error_value));
|
||||
error_value = jerry_exception_value (error_value, true);
|
||||
|
||||
jerry_char_t err_str_buf[256];
|
||||
|
||||
jerry_value_t err_str_val = jerry_value_to_string (error_value);
|
||||
|
||||
jerry_size_t string_end =
|
||||
jerry_string_to_buffer (err_str_val, JERRY_ENCODING_UTF8, err_str_buf, sizeof (err_str_buf) - 1);
|
||||
err_str_buf[string_end] = '\0';
|
||||
|
||||
if (jerry_feature_enabled (JERRY_FEATURE_ERROR_MESSAGES) && jerry_error_type (error_value) == JERRY_ERROR_SYNTAX)
|
||||
{
|
||||
jerry_char_t *string_end_p = err_str_buf + string_end;
|
||||
unsigned int err_line = 0;
|
||||
unsigned int 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 = err_str_buf; 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 < SYNTAX_ERROR_MAX_LINE_LENGTH)
|
||||
{
|
||||
/* Temporarily modify the error message, so we can use the path. */
|
||||
*path_str_end_p = '\0';
|
||||
|
||||
size_t source_size;
|
||||
uint8_t *source_p = jerry_port_read_source (path_str_p, &source_size);
|
||||
|
||||
/* Revert the error message. */
|
||||
*path_str_end_p = ':';
|
||||
|
||||
if (source_p != NULL)
|
||||
{
|
||||
uint32_t curr_line = 1;
|
||||
uint32_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 < SYNTAX_ERROR_MAX_LINE_LENGTH) && (pos < source_size) && (source_p[pos] != '\n');
|
||||
char_count++, pos++)
|
||||
{
|
||||
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "%c", source_p[pos]);
|
||||
}
|
||||
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "\n");
|
||||
|
||||
jerry_port_release_source (source_p);
|
||||
|
||||
while (--err_col)
|
||||
{
|
||||
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "~");
|
||||
}
|
||||
|
||||
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "^\n\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Unhandled exception: %s\n", err_str_buf);
|
||||
jerry_value_free (err_str_val);
|
||||
|
||||
if (jerry_value_is_object (error_value))
|
||||
{
|
||||
jerry_value_t stack_str = jerry_string_sz ("stack");
|
||||
jerry_value_t backtrace_val = jerry_object_get (error_value, stack_str);
|
||||
jerry_value_free (stack_str);
|
||||
|
||||
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 (uint32_t i = 0; i < length; i++)
|
||||
{
|
||||
jerry_value_t item_val = jerry_object_get_index (backtrace_val, i);
|
||||
|
||||
if (jerry_value_is_string (item_val))
|
||||
{
|
||||
string_end = jerry_string_to_buffer (item_val, JERRY_ENCODING_UTF8, err_str_buf, sizeof (err_str_buf) - 1);
|
||||
err_str_buf[string_end] = '\0';
|
||||
|
||||
printf ("%6u: %s\n", i, err_str_buf);
|
||||
}
|
||||
|
||||
jerry_value_free (item_val);
|
||||
}
|
||||
}
|
||||
|
||||
jerry_value_free (backtrace_val);
|
||||
}
|
||||
|
||||
jerry_value_free (error_value);
|
||||
} /* main_print_unhandled_exception */
|
||||
|
||||
/**
|
||||
* Runs the source code received by jerry_debugger_wait_for_client_source.
|
||||
*
|
||||
* @return result fo the source code execution
|
||||
*/
|
||||
jerry_value_t
|
||||
main_wait_for_source_callback (const jerry_char_t *source_name_p, /**< source name */
|
||||
size_t source_name_size, /**< size of source 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;
|
||||
} /* main_wait_for_source_callback */
|
||||
|
||||
/**
|
||||
* Check that value contains the reset abort value.
|
||||
*
|
||||
* Note: if the value is the reset abort value, the value is release.
|
||||
*
|
||||
* return true, if reset abort
|
||||
* false, otherwise
|
||||
*/
|
||||
bool
|
||||
main_is_value_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;
|
||||
} /* main_is_value_reset */
|
||||
@@ -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.
|
||||
*/
|
||||
|
||||
#ifndef MAIN_UTILS_H
|
||||
#define MAIN_UTILS_H
|
||||
|
||||
#include "jerryscript.h"
|
||||
|
||||
#include "main-options.h"
|
||||
|
||||
/**
|
||||
* Standalone Jerry exit codes
|
||||
*/
|
||||
#define JERRY_STANDALONE_EXIT_CODE_OK (0)
|
||||
#define JERRY_STANDALONE_EXIT_CODE_FAIL (1)
|
||||
|
||||
void main_init_engine (main_args_t *arguments_p);
|
||||
void main_print_unhandled_exception (jerry_value_t error_value);
|
||||
|
||||
jerry_value_t main_wait_for_source_callback (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);
|
||||
|
||||
bool main_is_value_reset (jerry_value_t value);
|
||||
|
||||
#endif /* !MAIN_UTILS_H */
|
||||
Reference in New Issue
Block a user