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

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

JerryScript-DCO-1.0-Signed-off-by: Dániel Bátyai dbatyai@inf.u-szeged.hu
This commit is contained in:
Dániel Bátyai
2022-01-20 13:53:47 +01:00
committed by GitHub
parent 79fd540ec9
commit ac1c48eeff
170 changed files with 4201 additions and 5698 deletions
+467
View File
@@ -0,0 +1,467 @@
/* 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 "cli.h"
#include <stdio.h>
#include <stdlib.h>
/*
* Fixed layout settings
*/
/**
* Wrap lines at:
*/
#define CLI_LINE_LENGTH 80
/**
* Indent various lines with:
*/
#define CLI_LINE_INDENT 2
/**
* Tab stop (for multi-column display) at:
*/
#define CLI_LINE_TAB 24
/**
* Declare a char VLA and concatenate a program name and a sub-command name
* (separated by a single space) into the new array. Useful for printing command
* line option usage summary for sub-commands.
*
* @param CMDNAME name of the new array variable.
* @param PROGNAME string containing the name of the program.
* @param CMD string continaing the name of the sub-command.
*/
#define CLI_CMD_NAME(CMDNAME, PROGNAME, CMD) \
char CMDNAME[strlen ((PROGNAME)) + strlen ((CMD)) + 2]; \
strncpy (CMDNAME, (PROGNAME), strlen ((PROGNAME))); \
CMDNAME[strlen ((PROGNAME))] = ' '; \
strncpy (CMDNAME + strlen ((PROGNAME)) + 1, (CMD), strlen ((CMD)) + 1)
/*
* Command line option handling
*/
/**
* Initialize a command line option processor.
*
* @return the state that should be passed to other cli_ functions.
*/
cli_state_t
cli_init (const cli_opt_t *options_p, /**< array of option definitions, terminated by CLI_OPT_DEFAULT */
int argc, /**< number of command line arguments */
char **argv) /**< array of command line arguments */
{
return (cli_state_t){ .error = NULL, .arg = NULL, .index = 1, .argc = argc, .argv = argv, .opts = options_p };
} /* cli_init */
/**
* Use another option list.
*/
void
cli_change_opts (cli_state_t *state_p, /**< state of the command line option processor */
const cli_opt_t *options_p) /**< array of option definitions, terminated by CLI_OPT_DEFAULT */
{
state_p->opts = options_p;
} /* cli_change_opts */
/**
* Checks whether the current argument is an option.
*
* Note:
* The state_p->error is not NULL on error and it contains the error message.
*
* @return the ID of the option that was found or a CLI_OPT_ constant otherwise.
*/
int
cli_consume_option (cli_state_t *state_p) /**< state of the command line option processor */
{
if (state_p->error != NULL)
{
return CLI_OPT_END;
}
if (state_p->index >= state_p->argc)
{
state_p->arg = NULL;
return CLI_OPT_END;
}
const char *arg = state_p->argv[state_p->index];
state_p->arg = arg;
if (arg[0] != '-')
{
return CLI_OPT_DEFAULT;
}
if (arg[1] == '-')
{
arg += 2;
for (const cli_opt_t *opt = state_p->opts; opt->id != CLI_OPT_DEFAULT; opt++)
{
if (opt->longopt != NULL && strcmp (arg, opt->longopt) == 0)
{
state_p->index++;
return opt->id;
}
}
state_p->error = "Unknown long option";
return CLI_OPT_END;
}
arg++;
for (const cli_opt_t *opt = state_p->opts; opt->id != CLI_OPT_DEFAULT; opt++)
{
if (opt->opt != NULL && strcmp (arg, opt->opt) == 0)
{
state_p->index++;
return opt->id;
}
}
state_p->error = "Unknown option";
return CLI_OPT_END;
} /* cli_consume_option */
/**
* Returns the next argument as string.
*
* Note:
* The state_p->error is not NULL on error and it contains the error message.
*
* @return argument string
*/
const char *
cli_consume_string (cli_state_t *state_p) /**< state of the command line option processor */
{
if (state_p->error != NULL)
{
return NULL;
}
if (state_p->index >= state_p->argc)
{
state_p->error = "Expected string argument";
state_p->arg = NULL;
return NULL;
}
state_p->arg = state_p->argv[state_p->index];
state_p->index++;
return state_p->arg;
} /* cli_consume_string */
/**
* Returns the next argument as integer.
*
* Note:
* The state_p->error is not NULL on error and it contains the error message.
*
* @return argument integer
*/
int
cli_consume_int (cli_state_t *state_p) /**< state of the command line option processor */
{
if (state_p->error != NULL)
{
return 0;
}
state_p->error = "Expected integer argument";
if (state_p->index >= state_p->argc)
{
state_p->arg = NULL;
return 0;
}
state_p->arg = state_p->argv[state_p->index];
char *endptr;
long int value = strtol (state_p->arg, &endptr, 10);
if (*endptr != '\0')
{
return 0;
}
state_p->error = NULL;
state_p->index++;
return (int) value;
} /* cli_consume_int */
/**
* Return next agument as path index.
*
* @return path index
*/
uint32_t
cli_consume_path (cli_state_t *state_p) /**< state of the command line option processor */
{
if (state_p->error != NULL)
{
return 0;
}
uint32_t path_index = (uint32_t) state_p->index;
cli_consume_string (state_p);
return path_index;
} /* cli_consume_path */
/*
* Print helper functions
*/
/**
* Pad with spaces.
*/
static void
cli_print_pad (int cnt) /**< number of spaces to print */
{
for (int i = 0; i < cnt; i++)
{
printf (" ");
}
} /* cli_print_pad */
/**
* Print the prefix of a string.
*/
static void
cli_print_prefix (const char *str, /**< string to print */
int len) /**< length of the prefix to print */
{
for (int i = 0; i < len; i++)
{
printf ("%c", *str++);
}
} /* cli_print_prefix */
/**
* Print usage summary of options.
*/
static void
cli_opt_usage (const char *prog_name_p, /**< program name, typically argv[0] */
const char *command_name_p, /**< command name if available */
const cli_opt_t *opts_p) /**< array of command line option definitions, terminated by CLI_OPT_DEFAULT */
{
int length = (int) strlen (prog_name_p);
const cli_opt_t *current_opt_p = opts_p;
printf ("%s", prog_name_p);
if (command_name_p != NULL)
{
int command_length = (int) strlen (command_name_p);
if (length + 1 + command_length > CLI_LINE_LENGTH)
{
length = CLI_LINE_INDENT - 1;
printf ("\n");
cli_print_pad (length);
}
printf (" %s", command_name_p);
}
while (current_opt_p->id != CLI_OPT_DEFAULT)
{
const char *opt_p = current_opt_p->opt;
int opt_length = 2 + 1;
if (opt_p == NULL)
{
opt_p = current_opt_p->longopt;
opt_length++;
}
opt_length += (int) strlen (opt_p);
if (length + 1 + opt_length >= CLI_LINE_LENGTH)
{
length = CLI_LINE_INDENT - 1;
printf ("\n");
cli_print_pad (length);
}
length += opt_length;
printf (" [");
if (current_opt_p->opt != NULL)
{
printf ("-%s", opt_p);
}
else
{
printf ("--%s", opt_p);
}
if (current_opt_p->meta != NULL)
{
printf (" %s", current_opt_p->meta);
}
printf ("]");
current_opt_p++;
}
if (current_opt_p->meta != NULL)
{
const char *opt_p = current_opt_p->meta;
int opt_length = (int) (2 + strlen (opt_p));
if (length + 1 + opt_length >= CLI_LINE_LENGTH)
{
length = CLI_LINE_INDENT - 1;
printf ("\n");
cli_print_pad (length);
}
printf (" [%s]", opt_p);
}
printf ("\n\n");
} /* cli_opt_usage */
/**
* Print a help message wrapped into the second column.
*/
static void
cli_print_help (const char *help) /**< the help message to print */
{
while (help != NULL && *help != 0)
{
int length = -1;
int i = 0;
for (; i < CLI_LINE_LENGTH - CLI_LINE_TAB && help[i] != 0; i++)
{
if (help[i] == ' ')
{
length = i;
}
}
if (length < 0 || i < CLI_LINE_LENGTH - CLI_LINE_TAB)
{
length = i;
}
cli_print_prefix (help, length);
help += length;
while (*help == ' ')
{
help++;
}
if (*help != 0)
{
printf ("\n");
cli_print_pad (CLI_LINE_TAB);
}
}
} /* cli_print_help */
/**
* Print detailed help for options.
*/
void
cli_help (const char *prog_name_p, /**< program name, typically argv[0] */
const char *command_name_p, /**< command name if available */
const cli_opt_t *options_p) /**< array of command line option definitions, terminated by CLI_OPT_DEFAULT */
{
cli_opt_usage (prog_name_p, command_name_p, options_p);
const cli_opt_t *opt_p = options_p;
while (opt_p->id != CLI_OPT_DEFAULT)
{
int length = CLI_LINE_INDENT;
cli_print_pad (CLI_LINE_INDENT);
if (opt_p->opt != NULL)
{
printf ("-%s", opt_p->opt);
length += (int) (strlen (opt_p->opt) + 1);
}
if (opt_p->opt != NULL && opt_p->longopt != NULL)
{
printf (", ");
length += 2;
}
if (opt_p->longopt != NULL)
{
printf ("--%s", opt_p->longopt);
length += (int) (strlen (opt_p->longopt) + 2);
}
if (opt_p->meta != NULL)
{
printf (" %s", opt_p->meta);
length += 1 + (int) strlen (opt_p->meta);
}
if (opt_p->help != NULL)
{
if (length >= CLI_LINE_TAB)
{
printf ("\n");
length = 0;
}
cli_print_pad (CLI_LINE_TAB - length);
length = CLI_LINE_TAB;
cli_print_help (opt_p->help);
}
printf ("\n");
opt_p++;
}
if (opt_p->help != NULL)
{
int length = 0;
if (opt_p->meta != NULL)
{
length = (int) (CLI_LINE_INDENT + strlen (opt_p->meta));
cli_print_pad (CLI_LINE_INDENT);
printf ("%s", opt_p->meta);
}
if (length >= CLI_LINE_TAB)
{
printf ("\n");
length = 0;
}
cli_print_pad (CLI_LINE_TAB - length);
cli_print_help (opt_p->help);
printf ("\n");
}
} /* cli_help */
+82
View File
@@ -0,0 +1,82 @@
/* 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 CLI_H
#define CLI_H
#include <stdint.h>
#include <string.h>
/**
* Command line option definition.
*/
typedef struct
{
int id; /**< unique ID of the option (CLI_OPT_DEFAULT, or anything >= 0) */
const char *opt; /**< short option variant (in the form of "x" without dashes) */
const char *longopt; /**< long option variant (in the form of "xxx" without dashes) */
const char *meta; /**< name(s) of the argument(s) of the option, for display only */
const char *help; /**< descriptive help message of the option */
} cli_opt_t;
/**
* Special marker for default option which also marks the end of the option list.
*/
#define CLI_OPT_DEFAULT -1
/**
* Returned by cli_consume_option () when no more options are available
* or an error occurred.
*/
#define CLI_OPT_END -2
/**
* State of the sub-command processor.
* No fields should be accessed other than error and arg.
*/
typedef struct
{
/* Public fields. */
const char *error; /**< public field for error message */
const char *arg; /**< last processed argument as string */
/* Private fields. */
int index; /**< current argument index */
int argc; /**< remaining number of arguments */
char **argv; /**< remaining arguments */
const cli_opt_t *opts; /**< options */
} cli_state_t;
/**
* Macro for writing command line option definition struct literals.
*/
#define CLI_OPT_DEF(...) /*(cli_opt_t)*/ \
{ \
__VA_ARGS__ \
}
/*
* Functions for CLI.
*/
cli_state_t cli_init (const cli_opt_t *options_p, int argc, char **argv);
void cli_change_opts (cli_state_t *state_p, const cli_opt_t *options_p);
int cli_consume_option (cli_state_t *state_p);
const char *cli_consume_string (cli_state_t *state_p);
int cli_consume_int (cli_state_t *state_p);
uint32_t cli_consume_path (cli_state_t *state_p);
void cli_help (const char *prog_name_p, const char *command_name_p, const cli_opt_t *options_p);
#endif /* !CLI_H */
+407
View File
@@ -0,0 +1,407 @@
/* 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 "options.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include "jerryscript-port.h"
#include "jerryscript.h"
#include "cli.h"
#include "jerryscript-ext/print.h"
/**
* Command line option IDs
*/
typedef enum
{
OPT_HELP,
OPT_VERSION,
OPT_MEM_STATS,
OPT_TEST262_OBJECT,
OPT_PARSE_ONLY,
OPT_SHOW_OP,
OPT_SHOW_RE_OP,
OPT_DEBUG_SERVER,
OPT_DEBUG_PORT,
OPT_DEBUG_CHANNEL,
OPT_DEBUG_PROTOCOL,
OPT_DEBUG_SERIAL_CONFIG,
OPT_DEBUGGER_WAIT_SOURCE,
OPT_EXEC_SNAP,
OPT_EXEC_SNAP_FUNC,
OPT_MODULE,
OPT_LOG_LEVEL,
OPT_NO_PROMPT,
OPT_CALL_ON_EXIT,
OPT_USE_STDIN,
} main_opt_id_t;
/**
* Command line options
*/
static const cli_opt_t main_opts[] = {
CLI_OPT_DEF (.id = OPT_HELP, .opt = "h", .longopt = "help", .help = "print this help and exit"),
CLI_OPT_DEF (.id = OPT_VERSION, .opt = "v", .longopt = "version", .help = "print tool and library version and exit"),
CLI_OPT_DEF (.id = OPT_MEM_STATS, .longopt = "mem-stats", .help = "dump memory statistics"),
CLI_OPT_DEF (.id = OPT_TEST262_OBJECT, .longopt = "test262-object", .help = "create test262 object"),
CLI_OPT_DEF (.id = OPT_PARSE_ONLY, .longopt = "parse-only", .help = "don't execute JS input"),
CLI_OPT_DEF (.id = OPT_SHOW_OP, .longopt = "show-opcodes", .help = "dump parser byte-code"),
CLI_OPT_DEF (.id = OPT_SHOW_RE_OP, .longopt = "show-regexp-opcodes", .help = "dump regexp byte-code"),
CLI_OPT_DEF (.id = OPT_DEBUG_SERVER,
.longopt = "start-debug-server",
.help = "start debug server and wait for a connecting client"),
CLI_OPT_DEF (.id = OPT_DEBUG_PORT,
.longopt = "debug-port",
.meta = "NUM",
.help = "debug server port (default: 5001)"),
CLI_OPT_DEF (.id = OPT_DEBUG_CHANNEL,
.longopt = "debug-channel",
.meta = "[websocket|rawpacket]",
.help = "Specify the debugger transmission channel (default: websocket)"),
CLI_OPT_DEF (.id = OPT_DEBUG_PROTOCOL,
.longopt = "debug-protocol",
.meta = "PROTOCOL",
.help = "Specify the transmission protocol over the communication channel (tcp|serial, default: tcp)"),
CLI_OPT_DEF (.id = OPT_DEBUG_SERIAL_CONFIG,
.longopt = "serial-config",
.meta = "OPTIONS_STRING",
.help = "Configure parameters for serial port (default: /dev/ttyS0,115200,8,N,1)"),
CLI_OPT_DEF (.id = OPT_DEBUGGER_WAIT_SOURCE,
.longopt = "debugger-wait-source",
.help = "wait for an executable source from the client"),
CLI_OPT_DEF (.id = OPT_EXEC_SNAP,
.longopt = "exec-snapshot",
.meta = "FILE",
.help = "execute input snapshot file(s)"),
CLI_OPT_DEF (.id = OPT_EXEC_SNAP_FUNC,
.longopt = "exec-snapshot-func",
.meta = "FILE NUM",
.help = "execute specific function from input snapshot file(s)"),
CLI_OPT_DEF (.id = OPT_MODULE, .opt = "m", .longopt = "module", .meta = "FILE", .help = "execute module file"),
CLI_OPT_DEF (.id = OPT_LOG_LEVEL, .longopt = "log-level", .meta = "NUM", .help = "set log level (0-3)"),
CLI_OPT_DEF (.id = OPT_NO_PROMPT, .longopt = "no-prompt", .help = "don't print prompt in REPL mode"),
CLI_OPT_DEF (.id = OPT_CALL_ON_EXIT,
.longopt = "call-on-exit",
.meta = "STRING",
.help = "invoke the specified function when the process is just about to exit"),
CLI_OPT_DEF (.id = OPT_USE_STDIN, .opt = "", .help = "read from standard input"),
CLI_OPT_DEF (.id = CLI_OPT_DEFAULT, .meta = "FILE", .help = "input JS file(s)")
};
/**
* Check whether a usage-related condition holds. If not, print an error
* message, print the usage, and terminate the application.
*/
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 */
const char *opt) /**< optional part of the error message */
{
if (!condition)
{
jerry_log (JERRY_LOG_LEVEL_ERROR, "%s: %s%s\n", name, msg, opt != NULL ? opt : "");
return false;
}
return true;
} /* check_usage */
/**
* Check whether JerryScript has a requested feature enabled or not. If not,
* print a warning message.
*
* @return the status of the feature.
*/
static bool
check_feature (jerry_feature_t feature, /**< feature to check */
const char *option) /**< command line option that triggered this check */
{
if (!jerry_feature_enabled (feature))
{
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
*
* @return true, if application should continue execution
* false, if application should exit
*/
bool
main_parse_args (int argc, /**< argc */
char **argv, /**< argv */
main_args_t *arguments_p) /**< [in/out] arguments reference */
{
arguments_p->source_count = 0;
arguments_p->debug_channel = "websocket";
arguments_p->debug_protocol = "tcp";
arguments_p->debug_serial_config = "/dev/ttyS0,115200,8,N,1";
arguments_p->debug_port = 5001;
arguments_p->exit_cb_name_p = NULL;
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))
{
switch (id)
{
case OPT_HELP:
{
cli_help (argv[0], NULL, main_opts);
arguments_p->parse_result = JERRY_STANDALONE_EXIT_CODE_OK;
return false;
}
case OPT_VERSION:
{
printf ("Version: %d.%d.%d%s\n",
JERRY_API_MAJOR_VERSION,
JERRY_API_MINOR_VERSION,
JERRY_API_PATCH_VERSION,
JERRY_COMMIT_HASH);
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_log_set_level (JERRY_LOG_LEVEL_DEBUG);
arguments_p->init_flags |= JERRY_INIT_MEM_STATS;
}
break;
}
case OPT_TEST262_OBJECT:
{
arguments_p->option_flags |= OPT_FLAG_TEST262_OBJECT;
break;
}
case OPT_PARSE_ONLY:
{
arguments_p->option_flags |= OPT_FLAG_PARSE_ONLY;
break;
}
case OPT_SHOW_OP:
{
if (check_feature (JERRY_FEATURE_PARSER_DUMP, cli_state.arg))
{
jerry_log_set_level (JERRY_LOG_LEVEL_DEBUG);
arguments_p->init_flags |= JERRY_INIT_SHOW_OPCODES;
}
break;
}
case OPT_CALL_ON_EXIT:
{
arguments_p->exit_cb_name_p = cli_consume_string (&cli_state);
break;
}
case OPT_SHOW_RE_OP:
{
if (check_feature (JERRY_FEATURE_REGEXP_DUMP, cli_state.arg))
{
jerry_log_set_level (JERRY_LOG_LEVEL_DEBUG);
arguments_p->init_flags |= JERRY_INIT_SHOW_REGEXP_OPCODES;
}
break;
}
case OPT_DEBUG_SERVER:
{
if (check_feature (JERRY_FEATURE_DEBUGGER, cli_state.arg))
{
arguments_p->option_flags |= OPT_FLAG_DEBUG_SERVER;
}
break;
}
case OPT_DEBUG_PORT:
{
if (check_feature (JERRY_FEATURE_DEBUGGER, cli_state.arg))
{
arguments_p->debug_port = (uint16_t) cli_consume_int (&cli_state);
}
break;
}
case OPT_DEBUG_CHANNEL:
{
if (check_feature (JERRY_FEATURE_DEBUGGER, cli_state.arg))
{
const char *debug_channel = cli_consume_string (&cli_state);
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;
}
break;
}
case OPT_DEBUG_PROTOCOL:
{
if (check_feature (JERRY_FEATURE_DEBUGGER, cli_state.arg))
{
const char *debug_protocol = cli_consume_string (&cli_state);
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;
}
break;
}
case OPT_DEBUG_SERIAL_CONFIG:
{
if (check_feature (JERRY_FEATURE_DEBUGGER, cli_state.arg))
{
arguments_p->debug_serial_config = cli_consume_string (&cli_state);
}
break;
}
case OPT_DEBUGGER_WAIT_SOURCE:
{
if (check_feature (JERRY_FEATURE_DEBUGGER, cli_state.arg))
{
arguments_p->option_flags |= OPT_FLAG_WAIT_SOURCE;
}
break;
}
case OPT_EXEC_SNAP:
{
const bool is_enabled = check_feature (JERRY_FEATURE_SNAPSHOT_EXEC, cli_state.arg);
const uint32_t path_index = cli_consume_path (&cli_state);
if (is_enabled)
{
main_source_t *source_p = arguments_p->sources_p + arguments_p->source_count;
arguments_p->source_count++;
source_p->type = SOURCE_SNAPSHOT;
source_p->path_index = path_index;
source_p->snapshot_index = 0;
}
break;
}
case OPT_EXEC_SNAP_FUNC:
{
const bool is_enabled = check_feature (JERRY_FEATURE_SNAPSHOT_EXEC, cli_state.arg);
const uint32_t path_index = cli_consume_path (&cli_state);
const uint16_t snapshot_index = (uint16_t) cli_consume_int (&cli_state);
if (is_enabled)
{
main_source_t *source_p = arguments_p->sources_p + arguments_p->source_count;
arguments_p->source_count++;
source_p->type = SOURCE_SNAPSHOT;
source_p->path_index = path_index;
source_p->snapshot_index = snapshot_index;
}
break;
}
case OPT_MODULE:
{
const uint32_t path_index = cli_consume_path (&cli_state);
main_source_t *source_p = arguments_p->sources_p + arguments_p->source_count;
arguments_p->source_count++;
source_p->type = SOURCE_MODULE;
source_p->path_index = path_index;
source_p->snapshot_index = 0;
break;
}
case OPT_LOG_LEVEL:
{
long int log_level = cli_consume_int (&cli_state);
if (!check_usage (log_level >= 0 && log_level <= 3,
argv[0],
"Error: invalid value for --log-level: ",
cli_state.arg))
{
return false;
}
jerry_log_set_level ((jerry_log_level_t) log_level);
break;
}
case OPT_NO_PROMPT:
{
arguments_p->option_flags |= OPT_FLAG_NO_PROMPT;
break;
}
case OPT_USE_STDIN:
{
arguments_p->option_flags |= OPT_FLAG_USE_STDIN;
break;
}
case CLI_OPT_DEFAULT:
{
main_source_t *source_p = arguments_p->sources_p + arguments_p->source_count;
arguments_p->source_count++;
source_p->type = SOURCE_SCRIPT;
source_p->path_index = cli_consume_path (&cli_state);
break;
}
default:
{
cli_state.error = "Internal error";
break;
}
}
}
if (cli_state.error != NULL)
{
if (cli_state.arg != NULL)
{
jerry_log (JERRY_LOG_LEVEL_ERROR, "Error: %s %s\n", cli_state.error, cli_state.arg);
}
else
{
jerry_log (JERRY_LOG_LEVEL_ERROR, "Error: %s\n", cli_state.error);
}
return false;
}
arguments_p->parse_result = JERRY_STANDALONE_EXIT_CODE_OK;
return true;
} /* main_parse_args */
+84
View File
@@ -0,0 +1,84 @@
/* 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_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.
*/
typedef enum
{
OPT_FLAG_EMPTY = 0,
OPT_FLAG_PARSE_ONLY = (1 << 0),
OPT_FLAG_DEBUG_SERVER = (1 << 1),
OPT_FLAG_WAIT_SOURCE = (1 << 2),
OPT_FLAG_NO_PROMPT = (1 << 3),
OPT_FLAG_USE_STDIN = (1 << 4),
OPT_FLAG_TEST262_OBJECT = (1u << 5),
} main_option_flags_t;
/**
* Source types
*/
typedef enum
{
SOURCE_SNAPSHOT,
SOURCE_MODULE,
SOURCE_SCRIPT,
} main_source_type_t;
/**
* Input source file.
*/
typedef struct
{
uint32_t path_index;
uint16_t snapshot_index;
uint16_t type;
} main_source_t;
/**
* Arguments struct to store parsed command line arguments.
*/
typedef struct
{
main_source_t *sources_p;
uint32_t source_count;
const char *debug_channel;
const char *debug_protocol;
const char *debug_serial_config;
uint16_t debug_port;
const char *exit_cb_name_p;
uint16_t option_flags;
uint16_t init_flags;
uint8_t parse_result;
} main_args_t;
bool main_parse_args (int argc, char **argv, main_args_t *arguments_p);
#endif /* !MAIN_OPTIONS_H */