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
+36 -30
View File
@@ -32,7 +32,7 @@ $ export PKG_CONFIG_PATH=$(pwd)/example_install/lib/pkgconfig/
Test if the `pkg-config` works for JerryScript:
```sh
$ pkg-config --cflags --libs libjerry-core libjerry-port-default libjerry-ext libjerry-math
$ pkg-config --cflags --libs libjerry-core libjerry-port libjerry-ext libjerry-math
```
## Example 2. Split engine initialization and script execution.
@@ -85,7 +85,7 @@ main (void)
To compile it one can use the following command:
```sh
$ gcc api-example-2.c -o api-example-2 $(pkg-config --cflags --libs libjerry-core libjerry-port-default libjerry-math)
$ gcc api-example-2.c -o api-example-2 $(pkg-config --cflags --libs libjerry-core libjerry-port libjerry-math)
```
If everything is correct the application returns with a zero exit code:
@@ -148,7 +148,7 @@ main (void)
To compile it one can use the following command:
```sh
$ gcc api-example-3.c -o api-example-3 $(pkg-config --cflags --libs libjerry-core libjerry-port-default libjerry-math)
$ gcc api-example-3.c -o api-example-3 $(pkg-config --cflags --libs libjerry-core libjerry-port libjerry-math)
```
If everything is correct the application returns with a zero exit code:
@@ -251,7 +251,7 @@ main (void)
To compile it one can use the following command:
```sh
$ gcc api-example-4.c -o api-example-4 $(pkg-config --cflags --libs libjerry-core libjerry-port-default libjerry-math)
$ gcc api-example-4.c -o api-example-4 $(pkg-config --cflags --libs libjerry-core libjerry-port libjerry-math)
```
If everything is correct the application should print out the message present in the `print_handler` method:
@@ -370,7 +370,7 @@ main (void)
To compile it one can use the following command:
```sh
$ gcc api-example-5.c -o api-example-5 $(pkg-config --cflags --libs libjerry-core libjerry-port-default libjerry-math)
$ gcc api-example-5.c -o api-example-5 $(pkg-config --cflags --libs libjerry-core libjerry-port libjerry-math)
```
If everything is correct the application should print out the string passed for the `print` method in the JS code:
@@ -388,14 +388,14 @@ can be used by other applications.
In this example the following extension methods are used:
- `jerryx_handler_register_global`
- `jerryx_register_global`
- `jerryx_handler_print`
In further examples this "print" handler will be used.
```c
#include "jerryscript.h"
#include "jerryscript-ext/handler.h"
#include "jerryscript-ext/handlers.h"
int
main (void)
@@ -407,7 +407,7 @@ main (void)
jerry_init (JERRY_INIT_EMPTY);
/* Register 'print' function from the extensions to the global object */
jerryx_handler_register_global ("print", jerryx_handler_print);
jerryx_register_global ("print", jerryx_handler_print);
/* Setup Global scope code */
jerry_value_t parsed_code = jerry_parse (script, script_size, NULL);
@@ -434,10 +434,10 @@ main (void)
To compile it one can use the following command:
(**Note** that the `libjerry-ext` was added **before** the `libjerry-port-default` entry for the `pkg-config` call.
(**Note** that the `libjerry-ext` was added **before** the `libjerry-port` entry for the `pkg-config` call.
```sh
$ gcc api-example-6.c -o api-example-6 $(pkg-config --cflags --libs libjerry-core libjerry-ext libjerry-port-default libjerry-math)
$ gcc api-example-6.c -o api-example-6 $(pkg-config --cflags --libs libjerry-core libjerry-ext libjerry-port libjerry-math)
```
If everything is correct the application should print out the string passed for the `print` method in the JS code:
@@ -456,8 +456,10 @@ Use the following code as the `api-example-7.c` file:
[doctest]: # ()
```c
#include <stdio.h>
#include "jerryscript.h"
#include "jerryscript-ext/handler.h"
#include "jerryscript-ext/handlers.h"
#include "jerryscript-ext/properties.h"
int
main (void)
@@ -468,7 +470,7 @@ main (void)
jerry_init (JERRY_INIT_EMPTY);
/* Register 'print' function from the extensions */
jerryx_handler_register_global ("print", jerryx_handler_print);
jerryx_register_global ("print", jerryx_handler_print);
/* Getting pointer to the Global object */
jerry_value_t global_object = jerry_current_realm ();
@@ -509,10 +511,10 @@ main (void)
To compile it one can use the following command:
(**Note** that the `libjerry-ext` was added **before** the `libjerry-port-default` entry for the `pkg-config` call.
(**Note** that the `libjerry-ext` was added **before** the `libjerry-port` entry for the `pkg-config` call.
```sh
$ gcc api-example-7.c -o api-example-7 $(pkg-config --cflags --libs libjerry-core libjerry-ext libjerry-port-default libjerry-math)
$ gcc api-example-7.c -o api-example-7 $(pkg-config --cflags --libs libjerry-core libjerry-ext libjerry-port libjerry-math)
```
The sample will output 'Hello from C!'. However, now it is not just a part of the source script, but the value, dynamically supplied to the engine:
@@ -647,7 +649,8 @@ See the following `api-example-8-shell.c` file:
#include <stdlib.h>
#include <string.h>
#include "jerryscript.h"
#include "jerryscript-ext/handler.h"
#include "jerryscript-ext/handlers.h"
#include "jerryscript-ext/properties.h"
static void
print_value (const jerry_value_t jsvalue)
@@ -726,7 +729,7 @@ main (void)
jerry_init (JERRY_INIT_EMPTY);
/* Register 'print' function from the extensions */
jerryx_handler_register_global ("print", jerryx_handler_print);
jerryx_register_global ("print", jerryx_handler_print);
while (!is_done)
{
@@ -781,10 +784,10 @@ main (void)
To compile it one can use the following command:
(**Note** that the `libjerry-ext` was added **before** the `libjerry-port-default` entry for the `pkg-config` call.
(**Note** that the `libjerry-ext` was added **before** the `libjerry-port` entry for the `pkg-config` call.
```sh
$ gcc api-example-8-shell.c -o api-example-8-shell $(pkg-config --cflags --libs libjerry-core libjerry-ext libjerry-port-default libjerry-math)
$ gcc api-example-8-shell.c -o api-example-8-shell $(pkg-config --cflags --libs libjerry-core libjerry-ext libjerry-port libjerry-math)
```
The application reads lines from standard input and evaluates them, one after another. To try out run:
@@ -802,7 +805,8 @@ In this example (`api-example-9.c`) an object with a native function is added to
```c
#include "jerryscript.h"
#include "jerryscript-ext/handler.h"
#include "jerryscript-ext/handlers.h"
#include "jerryscript-ext/properties.h"
struct my_struct
{
@@ -827,7 +831,7 @@ main (void)
jerry_init (JERRY_INIT_EMPTY);
/* Register 'print' function from the extensions */
jerryx_handler_register_global ("print", jerryx_handler_print);
jerryx_register_global ("print", jerryx_handler_print);
/* Do something with the native object */
my_struct.msg = "Hello, World!";
@@ -879,10 +883,10 @@ main (void)
To compile it one can use the following command:
(**Note** that the `libjerry-ext` was added **before** the `libjerry-port-default` entry for the `pkg-config` call.
(**Note** that the `libjerry-ext` was added **before** the `libjerry-port` entry for the `pkg-config` call.
```sh
$ gcc api-example-9.c -o api-example-9 $(pkg-config --cflags --libs libjerry-core libjerry-ext libjerry-port-default libjerry-math)
$ gcc api-example-9.c -o api-example-9 $(pkg-config --cflags --libs libjerry-core libjerry-ext libjerry-port libjerry-math)
```
Execute the example with:
@@ -909,7 +913,8 @@ Use the following code for `api-example-10.c`:
```c
#include "jerryscript.h"
#include "jerryscript-ext/handler.h"
#include "jerryscript-ext/handlers.h"
#include "jerryscript-ext/properties.h"
/**
* Add param to 'this.x'
@@ -953,7 +958,7 @@ main (void)
jerry_init (JERRY_INIT_EMPTY);
/* Register 'print' function from the extensions */
jerryx_handler_register_global ("print", jerryx_handler_print);
jerryx_register_global ("print", jerryx_handler_print);
/* Create a JS object */
const jerry_char_t my_js_object[] = " \
@@ -1008,10 +1013,10 @@ main (void)
To compile it one can use the following command:
(**Note** that the `libjerry-ext` was added **before** the `libjerry-port-default` entry for the `pkg-config` call.
(**Note** that the `libjerry-ext` was added **before** the `libjerry-port` entry for the `pkg-config` call.
```sh
$ gcc api-example-10.c -o api-example-10 $(pkg-config --cflags --libs libjerry-core libjerry-ext libjerry-port-default libjerry-math)
$ gcc api-example-10.c -o api-example-10 $(pkg-config --cflags --libs libjerry-core libjerry-ext libjerry-port libjerry-math)
```
Execute the example with:
@@ -1028,7 +1033,7 @@ Value of x is 17
## Example 11. Changing the seed of pseudorandom generated numbers
If you want to change the seed of `Math.random()` generated numbers, you have to initialize the seed value with `srand`.
A recommended method is using `jerry_port_get_current_time()` or something based on a constantly changing value, therefore every run produces truly random numbers.
A recommended method is using `jerry_port_current_time()` or something based on a constantly changing value, therefore every run produces truly random numbers.
[doctest]: # ()
@@ -1036,13 +1041,14 @@ A recommended method is using `jerry_port_get_current_time()` or something based
#include <stdlib.h>
#include "jerryscript.h"
#include "jerryscript-port.h"
#include "jerryscript-ext/handler.h"
#include "jerryscript-ext/handlers.h"
#include "jerryscript-ext/properties.h"
int
main (void)
{
/* Initialize srand value */
union { double d; unsigned u; } now = { .d = jerry_port_get_current_time () };
union { double d; unsigned u; } now = { .d = jerry_port_current_time () };
srand (now.u);
/* Generate a random number, and print it */
@@ -1052,7 +1058,7 @@ main (void)
jerry_init (JERRY_INIT_EMPTY);
/* Register the print function */
jerryx_handler_register_global ("print", jerryx_handler_print);
jerryx_register_global ("print", jerryx_handler_print);
/* Evaluate the script */
jerry_value_t eval_ret = jerry_eval (script, sizeof (script) - 1, JERRY_PARSE_NO_OPTS);