diff --git a/docs/01.GETTING-STARTED.md b/docs/01.GETTING-STARTED.md index b2bce19d2..534231e87 100644 --- a/docs/01.GETTING-STARTED.md +++ b/docs/01.GETTING-STARTED.md @@ -1,4 +1,4 @@ -### Setting Up Prerequisites +## Setting up prerequisites Currently, only Ubuntu 14.04+ is officially supported as primary development environment. @@ -19,38 +19,41 @@ sudo apt-get install gcc g++ gcc-arm-none-eabi cmake cppcheck vera++ python To make our scripts run correctly, several shell utilities should be available on the system: -- `find` - `awk` +- `bc` +- `find` +- `sed` -### Building Jerryscript +## Building JerryScript -##### To build debug version for Linux: +**To build debug version for Linux** ```bash python tools/build.py --debug ``` -##### To build debug version for Linux without LTO (Link Time Optimization): +**To build debug version for Linux without LTO (Link Time Optimization)** ```bash python tools/build.py --debug --lto=off ``` -##### Add custom arguments to CMake: +**Add custom arguments to CMake** ```bash python tools/build.py --cmake-param=CMAKE_PARAM ``` -##### Set a profile mode (full|minimal): +**Set a profile mode (full, minimal)** ```bash python tools/build.py --feature=full|minimal ``` -##### Use (jerry|compiler-default|external libc) libc: +**Use (jerry, compiler-default, external) libc** The default libc is jerry-libc, but you can use compiler-default libc or an external libc: + - compiler-default libc: ```bash @@ -63,7 +66,7 @@ python tools/build.py --jerry-libc=off --compiler-default-libc=on python tools/build.py --jerry-libc=off --compiler-default-libc=off --compile-flag="-I/path/to/libc/include" ``` -##### Add toolchain file: +**Add toolchain file** The ```cmake``` dir already contains some usable toolchain files, which you can use in the following format: @@ -77,72 +80,64 @@ For example the cross-compile to RaspberryPi 2 is something like this: python tools/build.py --toolchain=cmake/toolchain_linux_armv7l.cmake ``` -##### To get a list of all the available buildoptions for Linux: +**To get a list of all the available buildoptions for Linux** ```bash python tools/build.py --help ``` -### Checking patch +## Checking patch ```bash python tools/run-tests.py --precommit ``` +### Running only one type of test -#### Running only one type of test: - -##### To run build option tests: +**To run build option tests** ```bash python tools/run-tests.py --buildoption-test ``` -##### To run unittests: +**To run unittests** ```bash python tools/run-tests.py --unittests ``` -##### To run jerry-tests: +**To run jerry-tests** ```bash python tools/run-tests.py --jerry-tests ``` -##### To run jerry-test-suite: +**To run jerry-test-suite** ```bash python tools/run-tests.py --jerry-test-suite ``` -##### To run signed-off check: +**To run signed-off check** ```bash python tools/run-tests.py --check-signed-off ``` -##### To run cppcheck: +**To run cppcheck** ```bash python tools/run-tests.py --check-cppcheck ``` -##### To run vera check: +**To run vera check** ```bash python tools/run-tests.py --check-vera ``` -##### Use toolchain file: - -The cmake dir already contains some usable toolchain files, which you can use in the following format: - -python tools/run-tests.py --toolchain=TOOLCHAIN - -##### To get a list of all the available test options: +**To get a list of all the available test options** ```bash python tools/run-tests.py --help ``` - diff --git a/docs/02.API-REFERENCE.md b/docs/02.API-REFERENCE.md index 02c6ad305..510363e7c 100644 --- a/docs/02.API-REFERENCE.md +++ b/docs/02.API-REFERENCE.md @@ -13,6 +13,7 @@ Enum that contains the following elements: ## jerry_error_t Possible types of an error: + - JERRY_ERROR_COMMON - common error - JERRY_ERROR_EVAL - eval error - JERRY_ERROR_RANGE - range error @@ -33,6 +34,18 @@ Jerry's char value typedef uint8_t jerry_char_t; ``` +## jerry_char_ptr_t + +**Summary** + +Pointer to an array of character values + +**Prototype** + +```c +typedef jerry_char_t *jerry_char_ptr_t; +``` + ## jerry_size_t **Summary** @@ -130,7 +143,7 @@ Type of an external function handler **Prototype** ```c -typedef jerry_value_t (*jerry_external_handler_t) (const jerry_value_t function_obj_p, +typedef jerry_value_t (*jerry_external_handler_t) (const jerry_value_t function_obj, const jerry_value_t this_val, const jerry_value_t args_p[], const jerry_length_t args_count); @@ -157,7 +170,7 @@ Function type applied for each data property of an object **Prototype** ```c -typedef bool (*jerry_object_property_foreach_t) (const jerry_value_t property_name_p, +typedef bool (*jerry_object_property_foreach_t) (const jerry_value_t property_name, const jerry_value_t property_value, void *user_data_p); ``` @@ -233,14 +246,14 @@ Registers an external magic string array. ```c void -jerry_register_magic_strings (const jerry_char_ptr_t *ex_str_items, +jerry_register_magic_strings (const jerry_char_ptr_t *ex_str_items_p, uint32_t count, - const jerry_length_t *str_lengths); + const jerry_length_t *str_lengths_p); ``` -- `ex_str_items` - character arrays, representing external magic strings' contents +- `ex_str_items_p` - character arrays, representing external magic strings' contents - `count` - number of the strings -- `str_lengths` - lengths of the strings +- `str_lengths_p` - lengths of the strings **Example** @@ -296,8 +309,8 @@ jerry_get_memory_limits (size_t *out_data_bss_brk_limit_p, jerry_init (JERRY_INIT_EMPTY); size_t stack_limit; - size_t data_dss_brk_limit; - jerry_get_memory_limits (&stack_limit, &data_dss_brk_limit); + size_t data_bss_brk_limit; + jerry_get_memory_limits (&stack_limit, &data_bss_brk_limit); } ``` @@ -345,12 +358,12 @@ The simplest way to run JavaScript. ```c bool -jerry_run_simple (const jerry_char_t *script_source, +jerry_run_simple (const jerry_char_t *script_source_p, size_t script_source_size, jerry_init_flag_t flags); ``` -- `script_source` - source code, it must be a valid utf8 string. +- `script_source_p` - source code, it must be a valid utf8 string. - `script_source_size` - size of source code buffer, in bytes. - `jerry_init_flag_t` - combination of various engine configuration flags - return value @@ -407,7 +420,7 @@ jerry_parse (const jerry_char_t *source_p, { jerry_init (JERRY_INIT_EMPTY); - char script [] = "print ('Hello, World!');"; + const jerry_char_t script[] = "print ('Hello, World!');"; size_t script_size = strlen ((const char *) script); jerry_value_t parsed_code = jerry_parse (script, script_size, false); @@ -426,7 +439,7 @@ jerry_parse (const jerry_char_t *source_p, **Summary** -Run an EcmaScript function created by jerry_parse. +Run an EcmaScript function created by `jerry_parse`. *Note*: The code should be previously parsed with `jerry_parse`. @@ -434,7 +447,7 @@ Run an EcmaScript function created by jerry_parse. ```c jerry_value_t -jerry_run (jerry_value_t func_val); +jerry_run (const jerry_value_t func_val); ``` - `func_val` - function to run @@ -558,6 +571,8 @@ Functions to check the type of an API value ([jerry_value_t](#jerry_value_t)). **Summary** +Returns whether the given `jerry_value_t` is an array. + **Prototype** ```c @@ -1018,7 +1033,7 @@ Get raw data from API values. **Summary** -Gets the raw bool value form a `jerry_value_t`. +Gets the raw bool value from a `jerry_value_t`. **Prototype** @@ -1118,7 +1133,7 @@ jerry_get_string_size (const jerry_value_t value); ```c { - jerry_char_t char_array[] = "a string"; + const jerry_char_t char_array[] = "a string"; jerry_value_t string = jerry_create_string (char_array); jerry_size_t string_size = jerry_get_string_size (string); @@ -1155,7 +1170,7 @@ jerry_get_string_length (const jerry_value_t value); ```c { - jerry_char_t char_array[] = "a string"; + const jerry_char_t char_array[] = "a string"; jerry_value_t string = jerry_create_string (char_array); jerry_length_t string_length = jerry_get_string_length (string); @@ -1255,7 +1270,7 @@ jerry_get_array_length (const jerry_value_t value); # Converters of 'jerry_value_t' -Functions for convering API values to another value type. +Functions for converting API values to another value type. ## jerry_value_to_boolean @@ -1429,7 +1444,7 @@ jerry_value_to_string (const jerry_value_t value); - `value` - api value - return value - - converted srting value, if success + - converted string value, if success - thrown error, otherwise **Example** @@ -1451,7 +1466,7 @@ jerry_value_to_string (const jerry_value_t value); - [jerry_value_to_primitive](#jerry_value_to_primitive) -# Aquire and release API values +# Acquire and release API values ## jerry_acquire_value @@ -1620,7 +1635,7 @@ jerry_create_error (jerry_error_t error_type, ```c { jerry_value_t error_obj = jerry_create_error (JERRY_ERROR_TYPE, - (jerry_char_t * ) "error"); + (const jerry_char_t *) "error"); ... // usage of error_obj @@ -1660,10 +1675,10 @@ jerry_create_error_sz (jerry_error_t error_type, ```c { - jerry_char_t message[] = "error"; + const jerry_char_t *message = "error"; jerry_value_t error_obj = jerry_create_error_sz (JERRY_ERROR_COMMON, message, - strlen ((char *) message)); + strlen ((const char *) message)); ... // usage of error_obj @@ -1696,10 +1711,10 @@ jerry_create_external_function (jerry_external_handler_t handler_p); ```c static jerry_value_t -handler (const jerry_value_t function_obj_p, - const jerry_value_t this_p, +handler (const jerry_value_t function_obj, + const jerry_value_t this_val, const jerry_value_t args_p[], - const uint16_t args_cnt) + const jerry_length_t args_cnt) { printf ("native handler called!\n"); @@ -1711,7 +1726,7 @@ handler (const jerry_value_t function_obj_p, jerry_value_t glob_obj = jerry_get_global_object (); // after this, script can invoke the native handler through "handler_field (1, 2, 3);" - jerry_value_t prop_name = jerry_create_string ((jerry_char_t *) "handler_field"); + jerry_value_t prop_name = jerry_create_string ((const jerry_char_t *) "handler_field"); jerry_set_property (glob_obj, prop_name, func_val); jerry_release_value (prop_name); @@ -1953,7 +1968,7 @@ jerry_create_string_sz (const jerry_char_t *str_p, { const jerry_char_t char_array[] = "a string"; jerry_value_t string_value = jerry_create_string_sz (char_array, - strlen ((char *) char_array)); + strlen ((const char *) char_array)); ... // usage of string_value @@ -1971,7 +1986,7 @@ jerry_create_string_sz (const jerry_char_t *str_p, **Summary** -Creates a jerry_value_t representing an undefined value. +Creates a `jerry_value_t` representing an undefined value. **Prototype** @@ -2026,7 +2041,7 @@ jerry_has_property (const jerry_value_t obj_val, ```c { jerry_value_t global_object = jerry_get_global_object (); - jerry_value_t prop_name = jerry_create_string ((jerry_char_t *) "handler_field"); + jerry_value_t prop_name = jerry_create_string ((const jerry_char_t *) "handler_field"); bool has_prop = jerry_has_property (global_object, prop_name); @@ -2066,7 +2081,7 @@ jerry_has_own_property (const jerry_value_t obj_val, ```c { jerry_value_t global_object = jerry_get_global_object (); - jerry_value_t prop_name = jerry_create_string ((jerry_char_t *) "handler_field"); + jerry_value_t prop_name = jerry_create_string ((const jerry_char_t *) "handler_field"); bool has_prop = jerry_has_own_property (global_object, prop_name); @@ -2106,7 +2121,7 @@ jerry_delete_property (const jerry_value_t obj_val, ```c { jerry_value_t global_object = jerry_get_global_object (); - jerry_value_t prop_name = jerry_create_string ((jerry_char_t *) "my_prop"); + jerry_value_t prop_name = jerry_create_string ((const jerry_char_t *) "my_prop"); jerry_delete_property (global_object, prop_name); @@ -2187,7 +2202,7 @@ jerry_get_property_by_index (const jerry_value_t obj_val, ``` - `obj_val` - object value -- index - index number +- `index` - index number - return value - stored value on the specified index, if success - thrown exception, otherwise. @@ -2253,7 +2268,7 @@ jerry_set_property (const jerry_value_t obj_val, ... // create or acquire value to set jerry_value_t glob_obj = jerry_get_global_object (); - jerry_value_t prop_name = jerry_create_string ((jerry_char_t *) "my_prop"); + jerry_value_t prop_name = jerry_create_string ((const jerry_char_t *) "my_prop"); jerry_set_property (glob_obj, prop_name, value_to_set); @@ -2295,7 +2310,7 @@ jerry_set_property_by_index (const jerry_value_t obj_val, ``` - `obj_val` - object value -- index - index number +- `index` - index number - `value_to_set` - value to set - return value - true, if field value was set successfully @@ -2576,7 +2591,7 @@ is no longer needed. jerry_value_t jerry_construct_object (const jerry_value_t func_obj_val, const jerry_value_t args_p[], - uint16_t args_count); + jerry_size_t args_count); ``` - `func_obj_val` - function object to call @@ -2792,12 +2807,12 @@ Set native handle and an optional free callback for the specified object ```c void jerry_set_object_native_handle (const jerry_value_t obj_val, - uintptr_t handle, + uintptr_t handle_p, jerry_object_free_callback_t freecb_p); ``` - `obj_val` - object value to set handle in -- `handle` - handle value +- `handle_p` - handle value - `freecb_p` - pointer to "free" callback or NULL **Example** @@ -2840,14 +2855,13 @@ jerry_foreach_object_property (jerry_value_t obj_val, ``` - `obj_val` - object value -- `foreach_p` - foreach function, that will be apllied for each property +- `foreach_p` - foreach function, that will be applied for each property - `user_data_p` - user data for foreach function - return value - true, if object fields traversal was performed successfully, i.e.: - no unhandled exceptions were thrown in object fields traversal - object fields traversal was stopped on callback that returned false - - false, otherwise, if getter of field threw a exception or unhandled exceptions were thrown - during traversal + - false, otherwise **Example** @@ -2900,13 +2914,13 @@ jerry_parse_and_save_snapshot (const jerry_char_t *source_p, - `source_p` - script source, it must be a valid utf8 string. - `source_size` - script source size, in bytes. - `is_for_global` - snapshot would be executed as global (true) or eval (false). -- `is_strict` - stric mode +- `is_strict` - strict mode - `buffer_p` - buffer to save snapshot to. - `buffer_size` - the buffer's size. - return value - the size of snapshot, if it was generated succesfully (i.e. there are no syntax errors in source code, buffer size is sufficient, and snapshot support is enabled in current configuration through - JERRY_ENABLE_SNAPSHOT) + JERRY_ENABLE_SNAPSHOT_SAVE) - 0 otherwise. **Example** @@ -2916,10 +2930,10 @@ jerry_parse_and_save_snapshot (const jerry_char_t *source_p, jerry_init (JERRY_INIT_EMPTY); static uint8_t global_mode_snapshot_buffer[1024]; - const char *code_to_snapshot_p = "(function () { return 'string from snapshot'; }) ();"; + const jerry_char_t *code_to_snapshot_p = "(function () { return 'string from snapshot'; }) ();"; - size_t global_mode_snapshot_size = jerry_parse_and_save_snapshot ((jerry_char_t *) code_to_snapshot_p, - strlen (code_to_snapshot_p), + size_t global_mode_snapshot_size = jerry_parse_and_save_snapshot (code_to_snapshot_p, + strlen ((const char *) code_to_snapshot_p), true, false, global_mode_snapshot_buffer, @@ -2970,11 +2984,11 @@ jerry_exec_snapshot (const void *snapshot_p, { jerry_value_t res; static uint8_t global_mode_snapshot_buffer[1024]; - const char *code_to_snapshot_p = "(function () { return 'string from snapshot'; }) ();"; + const jerry_char_t *code_to_snapshot_p = "(function () { return 'string from snapshot'; }) ();"; jerry_init (JERRY_INIT_EMPTY); - size_t global_mode_snapshot_size = jerry_parse_and_save_snapshot ((jerry_char_t *) code_to_snapshot_p, - strlen (code_to_snapshot_p), + size_t global_mode_snapshot_size = jerry_parse_and_save_snapshot (code_to_snapshot_p, + strlen ((const char *) code_to_snapshot_p), true, global_mode_snapshot_buffer, sizeof (global_mode_snapshot_buffer)); diff --git a/docs/03.API-EXAMPLE.md b/docs/03.API-EXAMPLE.md index 3a574f168..9bba81349 100644 --- a/docs/03.API-EXAMPLE.md +++ b/docs/03.API-EXAMPLE.md @@ -9,14 +9,14 @@ This guide is intended to introduce you to JerryScript embedding API through cre #include "jerry-api.h" int -main (int argc, char * argv[]) +main (int argc, char *argv[]) { const jerry_char_t script[] = "print ('Hello, World!');"; size_t script_size = strlen ((const char *) script); bool ret_value = jerry_run_simple (script, script_size, JERRY_INIT_EMPTY); - return (ret_value ? 1 : 0); + return (ret_value ? 0 : 1); } ``` @@ -41,7 +41,7 @@ Here we perform the same actions, as `jerry_run_simple`, while splitting into se #include "jerry-api.h" int -main (int argc, char * argv[]) +main (int argc, char *argv[]) { const jerry_char_t script[] = "print ('Hello, World!');"; size_t script_size = strlen ((const char *) script); @@ -80,7 +80,7 @@ Our code is more complex now, but it introduces possibilities to interact with J #include "jerry-api.h" int -main (int argc, char * argv[]) +main (int argc, char *argv[]) { const jerry_char_t script_1[] = "var s = 'Hello, World!';"; const jerry_char_t script_2[] = "print (s);"; @@ -122,7 +122,7 @@ This way, we execute two independent script parts in one execution environment. #include "jerry-api.h" int -main (int argc, char * argv[]) { +main (int argc, char *argv[]) { const jerry_char_t str[] = "Hello, World!"; const jerry_char_t script[] = "print (s);"; @@ -214,6 +214,7 @@ print_value (const jerry_value_t value) jerry_char_t str_buf_p[req_sz]; jerry_string_to_char_buffer (value, str_buf_p, req_sz); + str_buf_p[req_sz] = '\0'; jerry_port_console ("%s", (const char *) str_buf_p); } @@ -248,10 +249,10 @@ Shell operation can be described with the following loop: #include "jerry-api.h" #include "jerry-port.h" -static void print_value (const jerry_api_value_t); +static void print_value (const jerry_value_t); int -main (int argc, char * argv[]) +main (int argc, char *argv[]) { bool is_done = false; @@ -260,7 +261,7 @@ main (int argc, char * argv[]) while (!is_done) { - char cmd [256]; + char cmd[256] = {}; char *cmd_tail = cmd; size_t len = 0; @@ -283,6 +284,12 @@ main (int argc, char * argv[]) len++; } + /* If the command is "quit", break the loop */ + if (!strncmp (cmd, "quit\n", strlen ("quit\n"))) + { + break; + } + jerry_value_t ret_val; /* Evaluate entered command */ @@ -337,7 +344,7 @@ get_msg_handler (const jerry_value_t func_value, /**< function object */ } /* get_msg_handler */ int -main (int argc, char * argv[]) +main (int argc, char *argv[]) { /* Initialize engine */ jerry_init (JERRY_INIT_EMPTY); @@ -439,7 +446,7 @@ add_handler (const jerry_value_t func_value, /**< function object */ } /* add_handler */ int -main (int argc, char * argv[]) +main (int argc, char *argv[]) { /* Initialize engine */ jerry_init (JERRY_INIT_EMPTY); @@ -505,4 +512,4 @@ Value of x is 17 ## Further steps -For further API description, please visit [API Reference page](https://samsung.github.io/jerryscript/API/) on [JerryScript home page](https://samsung.github.io/jerryscript/). +For further API description, please visit [API Reference page](https://samsung.github.io/jerryscript/api-reference/) on [JerryScript home page](https://samsung.github.io/jerryscript/). diff --git a/docs/04.INTERNALS.md b/docs/04.INTERNALS.md index 3389b86fc..7f5b9e70f 100644 --- a/docs/04.INTERNALS.md +++ b/docs/04.INTERNALS.md @@ -13,7 +13,7 @@ The lexer splits input string (ECMAScript program) into sequence of tokens. It i ## Scanner -Scanner (`./jerry-core/parser/js/js-parser-scanner.h`) pre-scans the input string to find certain tokens. For example, scanner determines whether the keyword `for` defines a general for or a for-in loop. Reading tokens in a while loop is not enough because a slash (`/`) can indicate the start of a regular expression or can be a division operator. +Scanner (`./jerry-core/parser/js/js-parser-scanner.c`) pre-scans the input string to find certain tokens. For example, scanner determines whether the keyword `for` defines a general for or a for-in loop. Reading tokens in a while loop is not enough because a slash (`/`) can indicate the start of a regular expression or can be a division operator. ## Expression Parser @@ -21,9 +21,9 @@ Expression parser is responsible for parsing JavaScript expressions. It is imple ## Statement Parser -JavaScript statements are parsed by this component. It uses the [Expression parser](#expression parser) to parse the constituent expressions. The implementation of Statement parser is located in `./jerry-core/parser/js/js-parser-statm.c`. +JavaScript statements are parsed by this component. It uses the [Expression parser](#expression-parser) to parse the constituent expressions. The implementation of Statement parser is located in `./jerry-core/parser/js/js-parser-statm.c`. -Function `parser_parse_source` carries out the parsing and compiling of the input EcmaScript source code. When a function appears in the source `parser_parse_source` calls `parser_parse_function` which is responsible for processing the source code of functions recursively including argument parsing and context handling. After the parsing, function `parser_post_processing` dumps the created opcodes and returns an ecma_compiled_code_t* that points to the compiled bytecode sequence. +Function `parser_parse_source` carries out the parsing and compiling of the input EcmaScript source code. When a function appears in the source `parser_parse_source` calls `parser_parse_function` which is responsible for processing the source code of functions recursively including argument parsing and context handling. After the parsing, function `parser_post_processing` dumps the created opcodes and returns an `ecma_compiled_code_t*` that points to the compiled bytecode sequence. The interactions between the major components shown on the following figure. @@ -61,9 +61,9 @@ There are three types of bytecode arguments in CBC: * __byte argument__: A value between 0 and 255, which often represents the argument count of call like opcodes (function call, new, eval, etc.). - * __literal argument__: An integer index which is greater or equal than zero and less than the `literal_end` field of the header. For further information see next section Literals (next). + * __literal argument__: An integer index which is greater or equal than zero and less than the `literal_end` field of the header. For further information see next section Literals (next). - * __relative branch__: An 1-3 byte long offset. The branch argument might also represent the end of an instruction range. For example the branch argument of `CBC_EXT_WITH_CREATE_CONTEXT` shows the end of a with statement. More precisely the position after the last instruction. + * __relative branch__: An 1-3 byte long offset. The branch argument might also represent the end of an instruction range. For example the branch argument of `CBC_EXT_WITH_CREATE_CONTEXT` shows the end of a `with` statement. More precisely the position after the last instruction. Argument combinations are limited to the following seven forms: @@ -92,30 +92,30 @@ There are two other sub-groups of identifiers. *Registers* are those identifiers There are two types of literal encoding in CBC. Both are variable length, where the length is one or two byte long. * __small__: maximum 511 literals can be encoded. - + One byte encoding for literals 0 - 254. - + ```c byte[0] = literal_index ``` Two byte encoding for literals 255 - 510. - + ```c byte[0] = 0xff byte[1] = literal_index - 0xff ``` * __full__: maximum 32767 literal can be encoded. - + One byte encoding for literals 0 - 127. - + ```c byte[0] = literal_index ``` Two byte encoding for literals 128 - 32767. - + ```c byte[0] = (literal_index >> 8) | 0x80 byte[1] = (literal_index & 0xff) @@ -135,66 +135,70 @@ Byte-codes can be placed into four main categories. Byte-codes of this category serve for placing objects onto the stack. As there are many instructions representing multiple atomic tasks in CBC, there are also many instructions for pushing objects onto the stack according to the number and the type of the arguments. The following table list a few of these opcodes with a brief description. -