Update the webpage (#5127)
The GitHub ribbon is also revived. Related issue: #5125 JerryScript-DCO-1.0-Signed-off-by: Zsolt Borbély zsborbely.u-szeged@partner.samsung.com
This commit is contained in:
+142
-175
@@ -42,40 +42,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
|
||||
```
|
||||
|
||||
## Example 1. Execute JavaScript from your application
|
||||
|
||||
The most basic example to test the engine is to create an `api-example-1.c` file containing the following code:
|
||||
|
||||
[doctest]: # ()
|
||||
|
||||
```c
|
||||
#include "jerryscript.h"
|
||||
|
||||
int
|
||||
main (void)
|
||||
{
|
||||
const jerry_char_t script[] = "var str = 'Hello, World!';";
|
||||
|
||||
bool ret_value = jerry_run_simple (script, sizeof (script) - 1, JERRY_INIT_EMPTY);
|
||||
|
||||
return (ret_value ? 0 : 1);
|
||||
}
|
||||
```
|
||||
|
||||
To compile it one can use the following command:
|
||||
|
||||
```sh
|
||||
$ gcc api-example-1.c -o api-example-1 $(pkg-config --cflags --libs libjerry-core libjerry-port-default libjerry-math)
|
||||
```
|
||||
|
||||
If everything is correct the application returns with a zero exit code:
|
||||
|
||||
```
|
||||
$ ./api-example-1
|
||||
$ echo $?
|
||||
$ pkg-config --cflags --libs libjerry-core libjerry-port libjerry-ext libjerry-math
|
||||
```
|
||||
|
||||
## Example 2. Split engine initialization and script execution.
|
||||
@@ -83,9 +50,9 @@ $ echo $?
|
||||
In this example the engine is initialized directly with the `jerry_init` method
|
||||
and cleaned up with the `jerry_cleanup` method. The example JavaScript code
|
||||
is directly parsed and executed via the `jerry_eval` method. Each `jerry_value_t`
|
||||
returned by the API methods is freed with the `jerry_release_value` method.
|
||||
returned by the API methods is freed with the `jerry_value_free` method.
|
||||
|
||||
To make sure that the code parsing and execution was ok, the `jerry_value_is_error`
|
||||
To make sure that the code parsing and execution was ok, the `jerry_value_is_exception`
|
||||
method is used to check for any errors.
|
||||
|
||||
Use the following code for the `api-example-2.c` file:
|
||||
@@ -113,10 +80,10 @@ main (void)
|
||||
JERRY_PARSE_NO_OPTS);
|
||||
|
||||
/* Check if there was any error (syntax or runtime) */
|
||||
bool run_ok = !jerry_value_is_error (eval_ret);
|
||||
bool run_ok = !jerry_value_is_exception (eval_ret);
|
||||
|
||||
/* Parsed source code must be freed */
|
||||
jerry_release_value (eval_ret);
|
||||
jerry_value_free (eval_ret);
|
||||
|
||||
/* Cleanup engine */
|
||||
jerry_cleanup ();
|
||||
@@ -128,7 +95,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:
|
||||
@@ -166,20 +133,20 @@ main (void)
|
||||
jerry_value_t parsed_code = jerry_parse (script, sizeof (script) - 1, NULL);
|
||||
|
||||
/* Check if there is any JS code parse error */
|
||||
if (!jerry_value_is_error (parsed_code))
|
||||
if (!jerry_value_is_exception (parsed_code))
|
||||
{
|
||||
/* Execute the parsed source code in the Global scope */
|
||||
jerry_value_t ret_value = jerry_run (parsed_code);
|
||||
|
||||
/* Check the execution return value if there is any error */
|
||||
run_ok = !jerry_value_is_error (ret_value);
|
||||
run_ok = !jerry_value_is_exception (ret_value);
|
||||
|
||||
/* Returned value must be freed */
|
||||
jerry_release_value (ret_value);
|
||||
jerry_value_free (ret_value);
|
||||
}
|
||||
|
||||
/* Parsed source code must be freed */
|
||||
jerry_release_value (parsed_code);
|
||||
jerry_value_free (parsed_code);
|
||||
|
||||
/* Cleanup engine */
|
||||
jerry_cleanup ();
|
||||
@@ -191,7 +158,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:
|
||||
@@ -210,10 +177,10 @@ In this example a very simple "print" method is added which prints out a static
|
||||
This method will be implemented in C and will be called from the JavaScript code.
|
||||
For this a few extra API methods are required:
|
||||
|
||||
- `jerry_get_global_object`
|
||||
- `jerry_create_string`
|
||||
- `jerry_set_property`
|
||||
- `jerry_create_external_function`
|
||||
- `jerry_current_realm`
|
||||
- `jerry_string_sz`
|
||||
- `jerry_object_set`
|
||||
- `jerry_function_external`
|
||||
|
||||
The `api-example-4.c` file should contain the following code:
|
||||
|
||||
@@ -233,7 +200,7 @@ print_handler (const jerry_call_info_t *call_info_p,
|
||||
printf ("Print handler was called\n");
|
||||
|
||||
/* Return an "undefined" value to the JavaScript engine */
|
||||
return jerry_create_undefined ();
|
||||
return jerry_undefined ();
|
||||
}
|
||||
|
||||
int
|
||||
@@ -248,40 +215,40 @@ main (void)
|
||||
/* Add the "print" method for the JavaScript global object */
|
||||
{
|
||||
/* Get the "global" object */
|
||||
jerry_value_t global_object = jerry_get_global_object ();
|
||||
jerry_value_t global_object = jerry_current_realm ();
|
||||
/* Create a "print" JS string */
|
||||
jerry_value_t property_name_print = jerry_create_string ((const jerry_char_t *) "print");
|
||||
jerry_value_t property_name_print = jerry_string_sz ("print");
|
||||
/* Create a function from a native C method (this function will be called from JS) */
|
||||
jerry_value_t property_value_func = jerry_create_external_function (print_handler);
|
||||
jerry_value_t property_value_func = jerry_function_external (print_handler);
|
||||
/* Add the "print" property with the function value to the "global" object */
|
||||
jerry_value_t set_result = jerry_set_property (global_object, property_name_print, property_value_func);
|
||||
jerry_value_t set_result = jerry_object_set (global_object, property_name_print, property_value_func);
|
||||
|
||||
/* Check if there was no error when adding the property (in this case it should never happen) */
|
||||
if (jerry_value_is_error (set_result)) {
|
||||
if (jerry_value_is_exception (set_result)) {
|
||||
printf ("Failed to add the 'print' property\n");
|
||||
}
|
||||
|
||||
/* Release all jerry_value_t-s */
|
||||
jerry_release_value (set_result);
|
||||
jerry_release_value (property_value_func);
|
||||
jerry_release_value (property_name_print);
|
||||
jerry_release_value (global_object);
|
||||
jerry_value_free (set_result);
|
||||
jerry_value_free (property_value_func);
|
||||
jerry_value_free (property_name_print);
|
||||
jerry_value_free (global_object);
|
||||
}
|
||||
|
||||
/* Setup Global scope code */
|
||||
jerry_value_t parsed_code = jerry_parse (script, script_size, NULL);
|
||||
|
||||
if (!jerry_value_is_error (parsed_code))
|
||||
if (!jerry_value_is_exception (parsed_code))
|
||||
{
|
||||
/* Execute the parsed source code in the Global scope */
|
||||
jerry_value_t ret_value = jerry_run (parsed_code);
|
||||
|
||||
/* Returned value must be freed */
|
||||
jerry_release_value (ret_value);
|
||||
jerry_value_free (ret_value);
|
||||
}
|
||||
|
||||
/* Parsed source code must be freed */
|
||||
jerry_release_value (parsed_code);
|
||||
jerry_value_free (parsed_code);
|
||||
|
||||
/* Cleanup engine */
|
||||
jerry_cleanup ();
|
||||
@@ -294,7 +261,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:
|
||||
@@ -314,7 +281,7 @@ argument (which probably comes from a JavaScript source) to a JS string and prin
|
||||
New API methods used:
|
||||
|
||||
- `jerry_value_to_string`
|
||||
- `jerry_string_to_utf8_char_buffer`
|
||||
- `jerry_string_to_buffer`
|
||||
|
||||
The `api-example-5.c` file should contain the following code:
|
||||
|
||||
@@ -342,17 +309,17 @@ print_handler (const jerry_call_info_t *call_info_p,
|
||||
* Please note that if the string does not fit into the buffer nothing will be copied.
|
||||
* More details on the API reference page
|
||||
*/
|
||||
jerry_size_t copied_bytes = jerry_string_to_utf8_char_buffer (string_value, buffer, sizeof (buffer) - 1);
|
||||
jerry_size_t copied_bytes = jerry_string_to_buffer (string_value, JERRY_ENCODING_UTF8, buffer, sizeof (buffer) - 1);
|
||||
buffer[copied_bytes] = '\0';
|
||||
|
||||
/* Release the "toString" result */
|
||||
jerry_release_value (string_value);
|
||||
jerry_value_free (string_value);
|
||||
|
||||
printf ("%s\n", (const char *)buffer);
|
||||
}
|
||||
|
||||
/* Return an "undefined" value to the JavaScript engine */
|
||||
return jerry_create_undefined ();
|
||||
return jerry_undefined ();
|
||||
}
|
||||
|
||||
int
|
||||
@@ -367,40 +334,40 @@ main (void)
|
||||
/* Add the "print" method for the JavaScript global object */
|
||||
{
|
||||
/* Get the "global" object */
|
||||
jerry_value_t global_object = jerry_get_global_object ();
|
||||
jerry_value_t global_object = jerry_current_realm ();
|
||||
/* Create a "print" JS string */
|
||||
jerry_value_t property_name_print = jerry_create_string ((const jerry_char_t *) "print");
|
||||
jerry_value_t property_name_print = jerry_string_sz ("print");
|
||||
/* Create a function from a native C method (this function will be called from JS) */
|
||||
jerry_value_t property_value_func = jerry_create_external_function (print_handler);
|
||||
jerry_value_t property_value_func = jerry_function_external (print_handler);
|
||||
/* Add the "print" property with the function value to the "global" object */
|
||||
jerry_value_t set_result = jerry_set_property (global_object, property_name_print, property_value_func);
|
||||
jerry_value_t set_result = jerry_object_set (global_object, property_name_print, property_value_func);
|
||||
|
||||
/* Check if there was no error when adding the property (in this case it should never happen) */
|
||||
if (jerry_value_is_error (set_result)) {
|
||||
if (jerry_value_is_exception (set_result)) {
|
||||
printf ("Failed to add the 'print' property\n");
|
||||
}
|
||||
|
||||
/* Release all jerry_value_t-s */
|
||||
jerry_release_value (set_result);
|
||||
jerry_release_value (property_value_func);
|
||||
jerry_release_value (property_name_print);
|
||||
jerry_release_value (global_object);
|
||||
jerry_value_free (set_result);
|
||||
jerry_value_free (property_value_func);
|
||||
jerry_value_free (property_name_print);
|
||||
jerry_value_free (global_object);
|
||||
}
|
||||
|
||||
/* Setup Global scope code */
|
||||
jerry_value_t parsed_code = jerry_parse (script, script_size, NULL);
|
||||
|
||||
if (!jerry_value_is_error (parsed_code))
|
||||
if (!jerry_value_is_exception (parsed_code))
|
||||
{
|
||||
/* Execute the parsed source code in the Global scope */
|
||||
jerry_value_t ret_value = jerry_run (parsed_code);
|
||||
|
||||
/* Returned value must be freed */
|
||||
jerry_release_value (ret_value);
|
||||
jerry_value_free (ret_value);
|
||||
}
|
||||
|
||||
/* Parsed source code must be freed */
|
||||
jerry_release_value (parsed_code);
|
||||
jerry_value_free (parsed_code);
|
||||
|
||||
/* Cleanup engine */
|
||||
jerry_cleanup ();
|
||||
@@ -413,7 +380,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:
|
||||
@@ -431,14 +398,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)
|
||||
@@ -450,23 +417,22 @@ main (void)
|
||||
jerry_init (JERRY_INIT_EMPTY);
|
||||
|
||||
/* Register 'print' function from the extensions to the global object */
|
||||
jerryx_handler_register_global ((const jerry_char_t *) "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);
|
||||
|
||||
if (!jerry_value_is_error (parsed_code))
|
||||
if (!jerry_value_is_exception (parsed_code))
|
||||
{
|
||||
/* Execute the parsed source code in the Global scope */
|
||||
jerry_value_t ret_value = jerry_run (parsed_code);
|
||||
|
||||
/* Returned value must be freed */
|
||||
jerry_release_value (ret_value);
|
||||
jerry_value_free (ret_value);
|
||||
}
|
||||
|
||||
/* Parsed source code must be freed */
|
||||
jerry_release_value (parsed_code);
|
||||
jerry_value_free (parsed_code);
|
||||
|
||||
/* Cleanup engine */
|
||||
jerry_cleanup ();
|
||||
@@ -478,10 +444,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:
|
||||
@@ -500,8 +466,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)
|
||||
@@ -512,30 +480,29 @@ main (void)
|
||||
jerry_init (JERRY_INIT_EMPTY);
|
||||
|
||||
/* Register 'print' function from the extensions */
|
||||
jerryx_handler_register_global ((const jerry_char_t *) "print",
|
||||
jerryx_handler_print);
|
||||
jerryx_register_global ("print", jerryx_handler_print);
|
||||
|
||||
/* Getting pointer to the Global object */
|
||||
jerry_value_t global_object = jerry_get_global_object ();
|
||||
jerry_value_t global_object = jerry_current_realm ();
|
||||
|
||||
/* Constructing strings */
|
||||
jerry_value_t prop_name = jerry_create_string ((const jerry_char_t *) "my_var");
|
||||
jerry_value_t prop_value = jerry_create_string ((const jerry_char_t *) "Hello from C!");
|
||||
jerry_value_t prop_name = jerry_string_sz ("my_var");
|
||||
jerry_value_t prop_value = jerry_string_sz ("Hello from C!");
|
||||
|
||||
/* Setting the string value as a property of the Global object */
|
||||
jerry_value_t set_result = jerry_set_property (global_object, prop_name, prop_value);
|
||||
jerry_value_t set_result = jerry_object_set (global_object, prop_name, prop_value);
|
||||
/* The 'set_result' should be checked if there was any error */
|
||||
if (jerry_value_is_error (set_result)) {
|
||||
if (jerry_value_is_exception (set_result)) {
|
||||
printf ("Failed to add the 'my_var' property\n");
|
||||
}
|
||||
jerry_release_value (set_result);
|
||||
jerry_value_free (set_result);
|
||||
|
||||
/* Releasing string values, as it is no longer necessary outside of engine */
|
||||
jerry_release_value (prop_name);
|
||||
jerry_release_value (prop_value);
|
||||
jerry_value_free (prop_name);
|
||||
jerry_value_free (prop_value);
|
||||
|
||||
/* Releasing the Global object */
|
||||
jerry_release_value (global_object);
|
||||
jerry_value_free (global_object);
|
||||
|
||||
/* Now starting script that would output value of just initialized field */
|
||||
jerry_value_t eval_ret = jerry_eval (script,
|
||||
@@ -543,7 +510,7 @@ main (void)
|
||||
JERRY_PARSE_NO_OPTS);
|
||||
|
||||
/* Free JavaScript value, returned by eval */
|
||||
jerry_release_value (eval_ret);
|
||||
jerry_value_free (eval_ret);
|
||||
|
||||
/* Freeing engine */
|
||||
jerry_cleanup ();
|
||||
@@ -554,10 +521,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:
|
||||
@@ -571,9 +538,9 @@ $ ./api-example-7
|
||||
JerryScript value can be a boolean, number, null, object, string, undefined or some special type of objects (arraybuffer, symbols, etc).
|
||||
|
||||
There is a special "error" value which wraps another value. This "error" can be created by throwing a JavaScript value from JS code
|
||||
or via API method(s). It is advised to check for this error with the `jerry_value_is_error` method as not all API methods
|
||||
can process error values. To extract the value from the "error" the API method `jerry_get_value_from_error` should be used.
|
||||
If an error object is created via API method (for example with `jerry_create_error`) the "error" value is automatically created.
|
||||
or via API method(s). It is advised to check for this error with the `jerry_value_is_exception` method as not all API methods
|
||||
can process error values. To extract the value from the "error" the API method `jerry_exception_value` should be used.
|
||||
If an error object is created via API method (for example with `jerry_error`) the "error" value is automatically created.
|
||||
|
||||
Notice the difference between error value and error object:
|
||||
- The error object is a object which was constructed via one of the `Error` objects (available from the global object or from API).
|
||||
@@ -590,7 +557,7 @@ var error_object = new Error ("error message");
|
||||
throw "message";
|
||||
```
|
||||
|
||||
To check what type a given `jerry_value_t` is the `jerry_value_is_*` methods or the `jerry_value_get_type` could be used.
|
||||
To check what type a given `jerry_value_t` is the `jerry_value_is_*` methods or the `jerry_value_type` could be used.
|
||||
For example the following code snippet could print out a few types (not all types are checked):
|
||||
|
||||
[doctest]: # (test="compile")
|
||||
@@ -605,14 +572,14 @@ print_value (const jerry_value_t jsvalue)
|
||||
{
|
||||
jerry_value_t value;
|
||||
/* If there is an error extract the object from it */
|
||||
if (jerry_value_is_error (jsvalue))
|
||||
if (jerry_value_is_exception (jsvalue))
|
||||
{
|
||||
printf ("Error value detected: ");
|
||||
value = jerry_get_value_from_error (jsvalue, false);
|
||||
value = jerry_exception_value (jsvalue, false);
|
||||
}
|
||||
else
|
||||
{
|
||||
value = jerry_acquire_value (jsvalue);
|
||||
value = jerry_value_copy (jsvalue);
|
||||
}
|
||||
|
||||
if (jerry_value_is_undefined (value))
|
||||
@@ -637,7 +604,7 @@ print_value (const jerry_value_t jsvalue)
|
||||
/* Float value */
|
||||
else if (jerry_value_is_number (value))
|
||||
{
|
||||
printf ("number: %lf", jerry_get_number_value (value));
|
||||
printf ("number: %lf", jerry_value_as_number (value));
|
||||
}
|
||||
/* String value */
|
||||
else if (jerry_value_is_string (value))
|
||||
@@ -645,11 +612,11 @@ print_value (const jerry_value_t jsvalue)
|
||||
jerry_char_t str_buf_p[256];
|
||||
|
||||
/* Determining required buffer size */
|
||||
jerry_size_t req_sz = jerry_get_string_size (value);
|
||||
jerry_size_t req_sz = jerry_string_size (value, JERRY_ENCODING_CESU8);
|
||||
|
||||
if (req_sz <= 255)
|
||||
{
|
||||
jerry_string_to_char_buffer (value, str_buf_p, req_sz);
|
||||
jerry_string_to_buffer (value, JERRY_ENCODING_CESU8, str_buf_p, req_sz);
|
||||
str_buf_p[req_sz] = '\0';
|
||||
printf ("%s", (const char *) str_buf_p);
|
||||
}
|
||||
@@ -665,7 +632,7 @@ print_value (const jerry_value_t jsvalue)
|
||||
}
|
||||
|
||||
printf ("\n");
|
||||
jerry_release_value (value);
|
||||
jerry_value_free (value);
|
||||
}
|
||||
```
|
||||
|
||||
@@ -692,21 +659,22 @@ 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)
|
||||
{
|
||||
jerry_value_t value;
|
||||
/* If there is an error extract the object from it */
|
||||
if (jerry_value_is_error (jsvalue))
|
||||
if (jerry_value_is_exception (jsvalue))
|
||||
{
|
||||
printf ("Error value detected: ");
|
||||
value = jerry_get_value_from_error (jsvalue, false);
|
||||
value = jerry_exception_value (jsvalue, false);
|
||||
}
|
||||
else
|
||||
{
|
||||
value = jerry_acquire_value (jsvalue);
|
||||
value = jerry_value_copy (jsvalue);
|
||||
}
|
||||
|
||||
if (jerry_value_is_undefined (value))
|
||||
@@ -731,7 +699,7 @@ print_value (const jerry_value_t jsvalue)
|
||||
/* Float value */
|
||||
else if (jerry_value_is_number (value))
|
||||
{
|
||||
printf ("number: %lf", jerry_get_number_value (value));
|
||||
printf ("number: %lf", jerry_value_as_number (value));
|
||||
}
|
||||
/* String value */
|
||||
else if (jerry_value_is_string (value))
|
||||
@@ -739,11 +707,11 @@ print_value (const jerry_value_t jsvalue)
|
||||
jerry_char_t str_buf_p[256];
|
||||
|
||||
/* Determining required buffer size */
|
||||
jerry_size_t req_sz = jerry_get_string_size (value);
|
||||
jerry_size_t req_sz = jerry_string_size (value, JERRY_ENCODING_CESU8);
|
||||
|
||||
if (req_sz <= 255)
|
||||
{
|
||||
jerry_string_to_char_buffer (value, str_buf_p, req_sz);
|
||||
jerry_string_to_buffer (value, JERRY_ENCODING_CESU8, str_buf_p, req_sz);
|
||||
str_buf_p[req_sz] = '\0';
|
||||
printf ("%s", (const char *) str_buf_p);
|
||||
}
|
||||
@@ -759,7 +727,7 @@ print_value (const jerry_value_t jsvalue)
|
||||
}
|
||||
|
||||
printf ("\n");
|
||||
jerry_release_value (value);
|
||||
jerry_value_free (value);
|
||||
}
|
||||
|
||||
int
|
||||
@@ -771,8 +739,7 @@ main (void)
|
||||
jerry_init (JERRY_INIT_EMPTY);
|
||||
|
||||
/* Register 'print' function from the extensions */
|
||||
jerryx_handler_register_global ((const jerry_char_t *) "print",
|
||||
jerryx_handler_print);
|
||||
jerryx_register_global ("print", jerryx_handler_print);
|
||||
|
||||
while (!is_done)
|
||||
{
|
||||
@@ -815,7 +782,7 @@ main (void)
|
||||
/* Print out the value */
|
||||
print_value (ret_val);
|
||||
|
||||
jerry_release_value (ret_val);
|
||||
jerry_value_free (ret_val);
|
||||
}
|
||||
|
||||
/* Cleanup engine */
|
||||
@@ -827,10 +794,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:
|
||||
@@ -848,7 +815,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
|
||||
{
|
||||
@@ -863,7 +831,7 @@ get_msg_handler (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 */
|
||||
{
|
||||
return jerry_create_string ((const jerry_char_t *) my_struct.msg);
|
||||
return jerry_string_sz (my_struct.msg);
|
||||
} /* get_msg_handler */
|
||||
|
||||
int
|
||||
@@ -873,33 +841,32 @@ main (void)
|
||||
jerry_init (JERRY_INIT_EMPTY);
|
||||
|
||||
/* Register 'print' function from the extensions */
|
||||
jerryx_handler_register_global ((const jerry_char_t *) "print",
|
||||
jerryx_handler_print);
|
||||
jerryx_register_global ("print", jerryx_handler_print);
|
||||
|
||||
/* Do something with the native object */
|
||||
my_struct.msg = "Hello, World!";
|
||||
|
||||
/* Create an empty JS object */
|
||||
jerry_value_t object = jerry_create_object ();
|
||||
jerry_value_t object = jerry_object ();
|
||||
|
||||
/* Create a JS function object and wrap into a jerry value */
|
||||
jerry_value_t func_obj = jerry_create_external_function (get_msg_handler);
|
||||
jerry_value_t func_obj = jerry_function_external (get_msg_handler);
|
||||
|
||||
/* Set the native function as a property of the empty JS object */
|
||||
jerry_value_t prop_name = jerry_create_string ((const jerry_char_t *) "myFunc");
|
||||
jerry_release_value (jerry_set_property (object, prop_name, func_obj));
|
||||
jerry_release_value (prop_name);
|
||||
jerry_release_value (func_obj);
|
||||
jerry_value_t prop_name = jerry_string_sz ("myFunc");
|
||||
jerry_value_free (jerry_object_set (object, prop_name, func_obj));
|
||||
jerry_value_free (prop_name);
|
||||
jerry_value_free (func_obj);
|
||||
|
||||
/* Wrap the JS object (not empty anymore) into a jerry api value */
|
||||
jerry_value_t global_object = jerry_get_global_object ();
|
||||
jerry_value_t global_object = jerry_current_realm ();
|
||||
|
||||
/* Add the JS object to the global context */
|
||||
prop_name = jerry_create_string ((const jerry_char_t *) "MyObject");
|
||||
jerry_release_value (jerry_set_property (global_object, prop_name, object));
|
||||
jerry_release_value (prop_name);
|
||||
jerry_release_value (object);
|
||||
jerry_release_value (global_object);
|
||||
prop_name = jerry_string_sz ("MyObject");
|
||||
jerry_value_free (jerry_object_set (global_object, prop_name, object));
|
||||
jerry_value_free (prop_name);
|
||||
jerry_value_free (object);
|
||||
jerry_value_free (global_object);
|
||||
|
||||
/* Now we have a "builtin" object called MyObject with a function called myFunc()
|
||||
*
|
||||
@@ -915,7 +882,7 @@ main (void)
|
||||
jerry_value_t eval_ret = jerry_eval (script, sizeof (script) - 1, JERRY_PARSE_NO_OPTS);
|
||||
|
||||
/* Free JavaScript value, returned by eval */
|
||||
jerry_release_value (eval_ret);
|
||||
jerry_value_free (eval_ret);
|
||||
|
||||
/* Cleanup engine */
|
||||
jerry_cleanup ();
|
||||
@@ -926,10 +893,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:
|
||||
@@ -956,7 +923,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'
|
||||
@@ -970,27 +938,27 @@ add_handler (const jerry_call_info_t *call_info_p, /**< call information */
|
||||
/* Note: that the argument count check is ignored for the example's case */
|
||||
|
||||
/* Get 'this.x' */
|
||||
jerry_value_t prop_name = jerry_create_string ((const jerry_char_t *) "x");
|
||||
jerry_value_t x_val = jerry_get_property (call_info_p->this_value, prop_name);
|
||||
jerry_value_t prop_name = jerry_string_sz ("x");
|
||||
jerry_value_t x_val = jerry_object_get (call_info_p->this_value, prop_name);
|
||||
|
||||
if (!jerry_value_is_error (x_val))
|
||||
if (!jerry_value_is_exception (x_val))
|
||||
{
|
||||
/* Convert Jerry API values to double */
|
||||
double x = jerry_get_number_value (x_val);
|
||||
double d = jerry_get_number_value (args_p[0]);
|
||||
double x = jerry_value_as_number (x_val);
|
||||
double d = jerry_value_as_number (args_p[0]);
|
||||
|
||||
/* Add the parameter to 'x' */
|
||||
jerry_value_t res_val = jerry_create_number (x + d);
|
||||
jerry_value_t res_val = jerry_number (x + d);
|
||||
|
||||
/* Set the new value of 'this.x' */
|
||||
jerry_release_value (jerry_set_property (call_info_p->this_value, prop_name, res_val));
|
||||
jerry_release_value (res_val);
|
||||
jerry_value_free (jerry_object_set (call_info_p->this_value, prop_name, res_val));
|
||||
jerry_value_free (res_val);
|
||||
}
|
||||
|
||||
jerry_release_value (x_val);
|
||||
jerry_release_value (prop_name);
|
||||
jerry_value_free (x_val);
|
||||
jerry_value_free (prop_name);
|
||||
|
||||
return jerry_create_undefined ();
|
||||
return jerry_undefined ();
|
||||
} /* add_handler */
|
||||
|
||||
int
|
||||
@@ -1000,8 +968,7 @@ main (void)
|
||||
jerry_init (JERRY_INIT_EMPTY);
|
||||
|
||||
/* Register 'print' function from the extensions */
|
||||
jerryx_handler_register_global ((const jerry_char_t *) "print",
|
||||
jerryx_handler_print);
|
||||
jerryx_register_global ("print", jerryx_handler_print);
|
||||
|
||||
/* Create a JS object */
|
||||
const jerry_char_t my_js_object[] = " \
|
||||
@@ -1023,16 +990,16 @@ main (void)
|
||||
JERRY_PARSE_NO_OPTS);
|
||||
|
||||
/* Create a JS function object and wrap into a jerry value */
|
||||
jerry_value_t add_func_obj = jerry_create_external_function (add_handler);
|
||||
jerry_value_t add_func_obj = jerry_function_external (add_handler);
|
||||
|
||||
/* Set the native function as a property of previously created MyObject */
|
||||
jerry_value_t prop_name = jerry_create_string ((const jerry_char_t *) "add2x");
|
||||
jerry_release_value (jerry_set_property (my_js_obj_val, prop_name, add_func_obj));
|
||||
jerry_release_value (add_func_obj);
|
||||
jerry_release_value (prop_name);
|
||||
jerry_value_t prop_name = jerry_string_sz ("add2x");
|
||||
jerry_value_free (jerry_object_set (my_js_obj_val, prop_name, add_func_obj));
|
||||
jerry_value_free (add_func_obj);
|
||||
jerry_value_free (prop_name);
|
||||
|
||||
/* Free JavaScript value, returned by eval (my_js_object) */
|
||||
jerry_release_value (my_js_obj_val);
|
||||
jerry_value_free (my_js_obj_val);
|
||||
|
||||
const jerry_char_t script[] = " \
|
||||
var str = MyObject.foo (); \
|
||||
@@ -1045,7 +1012,7 @@ main (void)
|
||||
jerry_value_t eval_ret = jerry_eval (script, sizeof (script) - 1, JERRY_PARSE_NO_OPTS);
|
||||
|
||||
/* Free JavaScript value, returned by eval */
|
||||
jerry_release_value (eval_ret);
|
||||
jerry_value_free (eval_ret);
|
||||
|
||||
/* Cleanup engine */
|
||||
jerry_cleanup ();
|
||||
@@ -1056,10 +1023,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:
|
||||
@@ -1076,7 +1043,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]: # ()
|
||||
|
||||
@@ -1084,13 +1051,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 */
|
||||
@@ -1100,14 +1068,13 @@ main (void)
|
||||
jerry_init (JERRY_INIT_EMPTY);
|
||||
|
||||
/* Register the print function */
|
||||
jerryx_handler_register_global ((const jerry_char_t *) "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);
|
||||
|
||||
/* Free the JavaScript value returned by eval */
|
||||
jerry_release_value (eval_ret);
|
||||
jerry_value_free (eval_ret);
|
||||
|
||||
/* Cleanup the engine */
|
||||
jerry_cleanup ();
|
||||
|
||||
Reference in New Issue
Block a user