Update the webpage (#2549)

JerryScript-DCO-1.0-Signed-off-by: Zsolt Borbély zsborbely.u-szeged@partner.samsung.com
This commit is contained in:
Zsolt Borbély
2018-10-04 11:04:17 +02:00
committed by Akos Kiss
parent cf87970ef6
commit c846c4ab73
6 changed files with 231 additions and 199 deletions
+155 -117
View File
@@ -248,32 +248,32 @@ typedef struct
} jerry_context_data_manager_t; } jerry_context_data_manager_t;
``` ```
## jerry_instance_alloc_t ## jerry_context_alloc_t
**Summary** **Summary**
Function type for allocating buffer for JerryScript instance. Function type for allocating buffer for JerryScript context.
**Prototype** **Prototype**
```c ```c
typedef void *(*jerry_instance_alloc_t) (size_t size, void *cb_data_p); typedef void *(*jerry_context_alloc_t) (size_t size, void *cb_data_p);
``` ```
- `size` - allocation size - `size` - allocation size
- `cb_data_p` - pointer to user data - `cb_data_p` - pointer to user data
## jerry_instance_t ## jerry_context_t
**Summary** **Summary**
An opaque declaration of the JerryScript instance structure which is the header of the context space. An opaque declaration of the JerryScript context structure.
**Prototype** **Prototype**
```c ```c
typedef struct jerry_instance_t jerry_instance_t; typedef struct jerry_context_t jerry_context_t;
``` ```
## jerry_property_descriptor_t ## jerry_property_descriptor_t
@@ -644,7 +644,7 @@ Registers an external magic string array.
```c ```c
void void
jerry_register_magic_strings (const jerry_char_t **ex_str_items_p, jerry_register_magic_strings (const jerry_char_t * const *ex_str_items_p,
uint32_t count, uint32_t count,
const jerry_length_t *str_lengths_p); const jerry_length_t *str_lengths_p);
``` ```
@@ -667,11 +667,11 @@ main (void)
// must be static, because 'jerry_register_magic_strings' does not copy // must be static, because 'jerry_register_magic_strings' does not copy
// the items must be sorted by size at first, then lexicographically // the items must be sorted by size at first, then lexicographically
static const jerry_char_t *magic_string_items[] = { static const jerry_char_t * const magic_string_items[] = {
(const jerry_char_t *) "magicstring1", (const jerry_char_t *) "magicstring1",
(const jerry_char_t *) "magicstring2", (const jerry_char_t *) "magicstring2",
(const jerry_char_t *) "magicstring3" (const jerry_char_t *) "magicstring3"
}; };
uint32_t num_magic_string_items = (uint32_t) (sizeof (magic_string_items) / sizeof (jerry_char_t *)); uint32_t num_magic_string_items = (uint32_t) (sizeof (magic_string_items) / sizeof (jerry_char_t *));
// must be static, because 'jerry_register_magic_strings' does not copy // must be static, because 'jerry_register_magic_strings' does not copy
@@ -688,7 +688,7 @@ main (void)
- [jerry_init](#jerry_init) - [jerry_init](#jerry_init)
- [jerry_cleanup](#jerry_cleanup) - [jerry_cleanup](#jerry_cleanup)
- [jerry_parse_and_save_literals](#jerry_parse_and_save_literals) - [jerry_get_literals_from_snapshot](#jerry_get_literals_from_snapshot)
## jerry_get_memory_stats ## jerry_get_memory_stats
@@ -793,15 +793,14 @@ jerry_run_simple (const jerry_char_t *script_source_p,
[doctest]: # () [doctest]: # ()
```c ```c
#include <string.h>
#include "jerryscript.h" #include "jerryscript.h"
int int
main (void) main (void)
{ {
const jerry_char_t *script = (const jerry_char_t *) "print ('Hello, World!');"; const jerry_char_t script[] = "print ('Hello, World!');";
jerry_run_simple (script, strlen ((const char *) script), JERRY_INIT_EMPTY); jerry_run_simple (script, sizeof (script) - 1, JERRY_INIT_EMPTY);
} }
``` ```
@@ -849,7 +848,6 @@ jerry_parse (const jerry_char_t *resource_name_p, /**< resource name (usually a
[doctest]: # () [doctest]: # ()
```c ```c
#include <string.h>
#include "jerryscript.h" #include "jerryscript.h"
int int
@@ -858,9 +856,8 @@ main (void)
jerry_init (JERRY_INIT_EMPTY); jerry_init (JERRY_INIT_EMPTY);
const jerry_char_t 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 (NULL, 0, script, script_size, JERRY_PARSE_NO_OPTS); jerry_value_t parsed_code = jerry_parse (NULL, 0, script, sizeof (script) - 1, JERRY_PARSE_NO_OPTS);
jerry_release_value (parsed_code); jerry_release_value (parsed_code);
jerry_cleanup (); jerry_cleanup ();
@@ -934,20 +931,18 @@ jerry_run (const jerry_value_t func_val);
[doctest]: # () [doctest]: # ()
```c ```c
#include <string.h>
#include "jerryscript.h" #include "jerryscript.h"
int int
main (void) main (void)
{ {
const jerry_char_t script[] = "print ('Hello, World!');"; const jerry_char_t script[] = "print ('Hello, World!');";
size_t script_size = strlen ((const char *) script);
/* Initialize engine */ /* Initialize engine */
jerry_init (JERRY_INIT_EMPTY); jerry_init (JERRY_INIT_EMPTY);
/* Setup Global scope code */ /* Setup Global scope code */
jerry_value_t parsed_code = jerry_parse (NULL, 0, script, script_size, JERRY_PARSE_NO_OPTS); jerry_value_t parsed_code = jerry_parse (NULL, 0, script, sizeof (script) - 1, JERRY_PARSE_NO_OPTS);
if (!jerry_value_is_error (parsed_code)) if (!jerry_value_is_error (parsed_code))
{ {
@@ -1026,7 +1021,6 @@ jerry_run_all_enqueued_jobs (void)
[doctest]: # () [doctest]: # ()
```c ```c
#include <string.h>
#include "jerryscript.h" #include "jerryscript.h"
int int
@@ -1035,9 +1029,8 @@ main (void)
jerry_init (JERRY_INIT_EMPTY); jerry_init (JERRY_INIT_EMPTY);
const jerry_char_t script[] = "new Promise(function(f,r) { f('Hello, World!'); }).then(function(x) { print(x); });"; const jerry_char_t script[] = "new Promise(function(f,r) { f('Hello, World!'); }).then(function(x) { print(x); });";
size_t script_size = strlen ((const char *) script);
jerry_value_t parsed_code = jerry_parse (NULL, 0, script, script_size, JERRY_PARSE_NO_OPTS); jerry_value_t parsed_code = jerry_parse (NULL, 0, script, sizeof (script) - 1, JERRY_PARSE_NO_OPTS);
jerry_value_t script_value = jerry_run (parsed_code); jerry_value_t script_value = jerry_run (parsed_code);
jerry_value_t job_value = jerry_run_all_enqueued_jobs (); jerry_value_t job_value = jerry_run_all_enqueued_jobs ();
@@ -2999,10 +2992,10 @@ jerry_create_error_sz (jerry_error_t error_type,
```c ```c
{ {
const jerry_char_t *message = "error"; const jerry_char_t message[] = "error";
jerry_value_t error_obj = jerry_create_error_sz (JERRY_ERROR_COMMON, jerry_value_t error_obj = jerry_create_error_sz (JERRY_ERROR_COMMON,
message, message,
strlen ((const char *) message)); sizeof (message) - 1);
... // usage of error_obj ... // usage of error_obj
@@ -3330,7 +3323,7 @@ jerry_create_string_sz (const jerry_char_t *str_p,
{ {
const jerry_char_t char_array[] = "a string"; const jerry_char_t char_array[] = "a string";
jerry_value_t string_value = jerry_create_string_sz (char_array, jerry_value_t string_value = jerry_create_string_sz (char_array,
strlen ((const char *) char_array)); sizeof (char_array) - 1);
... // usage of string_value ... // usage of string_value
@@ -3408,7 +3401,7 @@ jerry_create_string_sz (const jerry_char_t *str_p,
{ {
const jerry_char_t char_array[] = "a string"; const jerry_char_t char_array[] = "a string";
jerry_value_t string_value = jerry_create_string_sz_from_utf8 (char_array, jerry_value_t string_value = jerry_create_string_sz_from_utf8 (char_array,
strlen ((const char *) char_array)); sizeof (char_array) - 1);
... // usage of string_value ... // usage of string_value
@@ -4802,16 +4795,15 @@ jerry_is_valid_utf8_string (const jerry_char_t *utf8_buf_p, /**< UTF-8 string */
[doctest]: # () [doctest]: # ()
```c ```c
#include <string.h>
#include "jerryscript.h" #include "jerryscript.h"
int int
main (void) main (void)
{ {
const jerry_char_t script[] = "print ('Hello, World!');"; const jerry_char_t script[] = "print ('Hello, World!');";
size_t script_size = strlen ((const char *) script); const jerry_size_t script_size = sizeof (script) - 1;
if (jerry_is_valid_utf8_string (script, (jerry_size_t) script_size)) if (jerry_is_valid_utf8_string (script, script_size))
{ {
jerry_run_simple (script, script_size, JERRY_INIT_EMPTY); jerry_run_simple (script, script_size, JERRY_INIT_EMPTY);
} }
@@ -4850,7 +4842,6 @@ jerry_is_valid_cesu8_string (const jerry_char_t *cesu8_buf_p, /**< CESU-8 string
[doctest]: # () [doctest]: # ()
```c ```c
#include <string.h>
#include "jerryscript.h" #include "jerryscript.h"
int int
@@ -4859,12 +4850,12 @@ main (void)
jerry_init (JERRY_INIT_EMPTY); jerry_init (JERRY_INIT_EMPTY);
const jerry_char_t script[] = "Hello, World!"; const jerry_char_t script[] = "Hello, World!";
size_t script_size = strlen ((const char *) script); const jerry_size_t script_size = sizeof (script) - 1;
if (jerry_is_valid_cesu8_string (script, (jerry_size_t) script_size)) if (jerry_is_valid_cesu8_string (script, script_size))
{ {
jerry_value_t string_value = jerry_create_string_sz (script, jerry_value_t string_value = jerry_create_string_sz (script,
(jerry_size_t) script_size); script_size);
// usage of string_value // usage of string_value
@@ -4885,28 +4876,74 @@ main (void)
- [jerry_substring_to_char_buffer](#jerry_substring_to_char_buffer) - [jerry_substring_to_char_buffer](#jerry_substring_to_char_buffer)
# External context functions # Dynamic memory management functions
## jerry_create_instance ## jerry_heap_alloc
**Summary** **Summary**
Creates a JerryScript instance for external context. Allocate memory on the engine's heap.
*Note*: This function may take away memory from the executed JavaScript code.
If any other dynamic memory allocation API is available (e.g., libc malloc), it
should be used instead.
**Prototype** **Prototype**
```c ```c
jerry_instance_t * void *jerry_heap_alloc (size_t size);
jerry_create_instance (uint32_t heap_size,
jerry_instance_alloc_t alloc,
void *cb_data_p);
``` ```
- `heap_size` - requested heap size of the JerryScript instance - `size`: size of the memory block.
- return value: non-NULL pointer, if the memory is successfully allocated,
NULL otherwise.
**See also**
- [jerry_heap_free](#jerry_heap_free)
## jerry_heap_free
**Summary**
Free memory allocated on the engine's heap.
**Prototype**
```c
void jerry_heap_free (void *mem_p, size_t size);
```
- `mem_p`: value returned by `jerry_heap_alloc`.
- `size`: same size as passed to `jerry_heap_alloc`.
**See also**
- [jerry_heap_alloc](#jerry_heap_alloc)
# External context functions
## jerry_create_context
**Summary**
Create an external JerryScript engine context.
**Prototype**
```c
jerry_context_t *
jerry_create_context (uint32_t heap_size,
jerry_context_alloc_t alloc,
void *cb_data_p);
```
- `heap_size` - requested heap size of the JerryScript context
- `alloc` - function for allocation - `alloc` - function for allocation
- `cb_data_p` - user data - `cb_data_p` - user data
- return value - return value
- pointer to the newly created JerryScript instance if success - pointer to the newly created JerryScript context if success
- NULL otherwise. - NULL otherwise.
**Example** **Example**
@@ -4920,19 +4957,19 @@ jerry_create_instance (uint32_t heap_size,
#include "jerryscript.h" #include "jerryscript.h"
#include "jerryscript-port.h" #include "jerryscript-port.h"
/* A different Thread Local Storage variable for each jerry instance. */ /* A different Thread Local Storage variable for each jerry context. */
__thread jerry_instance_t *tls_instance; __thread jerry_context_t *tls_context;
jerry_instance_t * jerry_context_t *
jerry_port_get_current_instance (void) jerry_port_get_current_context (void)
{ {
/* Returns the instance assigned to the thread. */ /* Returns the context assigned to the thread. */
return tls_instance; return tls_context;
} }
/* Allocate JerryScript heap for each thread. */ /* Allocate JerryScript heap for each thread. */
static void * static void *
instance_alloc_fn (size_t size, void *cb_data) context_alloc_fn (size_t size, void *cb_data)
{ {
(void) cb_data; (void) cb_data;
return malloc (size); return malloc (size);
@@ -4941,15 +4978,15 @@ instance_alloc_fn (size_t size, void *cb_data)
static void * static void *
thread_function (void *param) thread_function (void *param)
{ {
tls_instance = jerry_create_instance (512 * 1024, tls_context = jerry_create_context (512 * 1024,
instance_alloc_fn, context_alloc_fn,
NULL); NULL);
jerry_init (JERRY_INIT_EMPTY); jerry_init (JERRY_INIT_EMPTY);
/* Run the JerryScript instance (e.g.: jerry_parse & jerry_run) */ /* Run JerryScript in the context (e.g.: jerry_parse & jerry_run) */
jerry_cleanup (); jerry_cleanup ();
/* Deallocate JerryScript instance */ /* Deallocate JerryScript context */
free (tls_instance); free (tls_context);
return NULL; return NULL;
} }
@@ -4979,9 +5016,9 @@ main (void)
**See also** **See also**
- [jerry_instance_t](#jerry_instance_t) - [jerry_context_t](#jerry_context_t)
- [jerry_instance_alloc_t](#jerry_instance_alloc_t) - [jerry_context_alloc_t](#jerry_context_alloc_t)
- [jerry_port_get_current_instance](05.PORT-API.md#jerry_port_get_current_instance) - [jerry_port_get_current_context](05.PORT-API.md#jerry_port_get_current_context)
# Snapshot functions # Snapshot functions
@@ -5023,7 +5060,6 @@ jerry_generate_snapshot (const jerry_char_t *resource_name_p,
[doctest]: # () [doctest]: # ()
```c ```c
#include <string.h>
#include "jerryscript.h" #include "jerryscript.h"
int int
@@ -5032,13 +5068,13 @@ main (void)
jerry_init (JERRY_INIT_EMPTY); jerry_init (JERRY_INIT_EMPTY);
static uint32_t global_mode_snapshot_buffer[256]; static uint32_t global_mode_snapshot_buffer[256];
const jerry_char_t *code_to_snapshot_p = (const jerry_char_t *) "(function () { return 'string from snapshot'; }) ();"; const jerry_char_t script_to_snapshot[] = "(function () { return 'string from snapshot'; }) ();";
jerry_value_t generate_result; jerry_value_t generate_result;
generate_result = jerry_generate_snapshot (NULL, generate_result = jerry_generate_snapshot (NULL,
0, 0,
code_to_snapshot_p, script_to_snapshot,
strlen ((const char *) code_to_snapshot_p), sizeof (script_to_snapshot) - 1,
0, 0,
global_mode_snapshot_buffer, global_mode_snapshot_buffer,
sizeof (global_mode_snapshot_buffer) / sizeof (uint32_t)); sizeof (global_mode_snapshot_buffer) / sizeof (uint32_t));
@@ -5103,7 +5139,6 @@ jerry_generate_function_snapshot (const jerry_char_t *resource_name_p,
[doctest]: # () [doctest]: # ()
```c ```c
#include <string.h>
#include "jerryscript.h" #include "jerryscript.h"
int int
@@ -5112,16 +5147,16 @@ main (void)
jerry_init (JERRY_INIT_EMPTY); jerry_init (JERRY_INIT_EMPTY);
static uint32_t func_snapshot_buffer[256]; static uint32_t func_snapshot_buffer[256];
const jerry_char_t *args_p = (const jerry_char_t *) "a, b"; const jerry_char_t args[] = "a, b";
const jerry_char_t *src_p = (const jerry_char_t *) "return a + b;"; const jerry_char_t src[] = "return a + b;";
jerry_value_t generate_result; jerry_value_t generate_result;
generate_result = jerry_generate_function_snapshot (NULL, generate_result = jerry_generate_function_snapshot (NULL,
0, 0,
src_p, src,
strlen ((const char *) src_p), sizeof (src) - 1,
args_p, args,
strlen ((const char *) args_p), sizeof (args) - 1,
0, 0,
func_snapshot_buffer, func_snapshot_buffer,
sizeof (func_snapshot_buffer) / sizeof (uint32_t)); sizeof (func_snapshot_buffer) / sizeof (uint32_t));
@@ -5173,22 +5208,21 @@ jerry_exec_snapshot (const uint32_t *snapshot_p,
[doctest]: # () [doctest]: # ()
```c ```c
#include <string.h>
#include "jerryscript.h" #include "jerryscript.h"
int int
main (void) main (void)
{ {
static uint32_t global_mode_snapshot_buffer[256]; static uint32_t global_mode_snapshot_buffer[256];
const jerry_char_t *code_to_snapshot_p = (const jerry_char_t *) "(function () { return 'string from snapshot'; }) ();"; const jerry_char_t script_to_snapshot[] = "(function () { return 'string from snapshot'; }) ();";
jerry_init (JERRY_INIT_EMPTY); jerry_init (JERRY_INIT_EMPTY);
jerry_value_t generate_result; jerry_value_t generate_result;
generate_result = jerry_generate_snapshot (NULL, generate_result = jerry_generate_snapshot (NULL,
0, 0,
code_to_snapshot_p, script_to_snapshot,
strlen ((const char *) code_to_snapshot_p), sizeof (script_to_snapshot) - 1,
0, 0,
global_mode_snapshot_buffer, global_mode_snapshot_buffer,
sizeof (global_mode_snapshot_buffer) / sizeof (uint32_t)); sizeof (global_mode_snapshot_buffer) / sizeof (uint32_t));
@@ -5251,25 +5285,24 @@ jerry_load_function_snapshot (const uint32_t *snapshot_p,
[doctest]: # () [doctest]: # ()
```c ```c
#include <string.h>
#include "jerryscript.h" #include "jerryscript.h"
int int
main (void) main (void)
{ {
static uint32_t snapshot_buffer[256]; static uint32_t snapshot_buffer[256];
const jerry_char_t *args_p = (const jerry_char_t *)"a, b"; const jerry_char_t func_args[] = "a, b";
const jerry_char_t *src_p = (const jerry_char_t *) "return a + b;"; const jerry_char_t func_src[] = "return a + b;";
jerry_init (JERRY_INIT_EMPTY); jerry_init (JERRY_INIT_EMPTY);
jerry_value_t generate_result; jerry_value_t generate_result;
generate_result = jerry_generate_function_snapshot (NULL, generate_result = jerry_generate_function_snapshot (NULL,
0, 0,
src_p, func_src,
strlen ((const char *) src_p), sizeof (func_src) - 1,
args_p, func_args,
strlen ((const char *) args_p), sizeof (func_args) - 1,
false, false,
snapshot_buffer, snapshot_buffer,
sizeof (snapshot_buffer) / sizeof (uint32_t)); sizeof (snapshot_buffer) / sizeof (uint32_t));
@@ -5313,30 +5346,28 @@ main (void)
- [jerry_parse_and_save_function_snapshot](#jerry_parse_and_save_function_snapshot) - [jerry_parse_and_save_function_snapshot](#jerry_parse_and_save_function_snapshot)
## jerry_parse_and_save_literals ## jerry_get_literals_from_snapshot
**Summary** **Summary**
Collect the used literals from the given source code and save them into a specific file in a list or C format. Collect the used literals from the given snapshot and save them into a buffer in list or C format.
These literals are generated by the parser, they are valid identifiers and none of them are magic string. None of these literals are magic strings. In C format only valid identifiers are collected.
**Prototype** **Prototype**
```c ```c
size_t size_t
jerry_parse_and_save_literals (const jerry_char_t *source_p, jerry_get_literals_from_snapshot (const uint32_t *snapshot_p,
size_t source_size, size_t snapshot_size,
bool is_strict, jerry_char_t *lit_buf_p,
uint32_t *buffer_p, size_t lit_buf_size,
size_t buffer_size, bool is_c_format);
bool is_c_format);
``` ```
- `source_p` - script source, it must be a valid utf8 string. - `snapshot_p` - input snapshot buffer.
- `source_size` - script source size, in bytes. - `snapshot_size` - snapshot size, in bytes.
- `is_strict` - strict mode. - `lit_buf_p` - buffer to save literals to.
- `buffer_p` - buffer to save literals to. - `lit_buf_size` - the buffer's size.
- `buffer_size` - the buffer's size.
- `is_c_format` - the output format would be C-style (true) or a simple list (false). - `is_c_format` - the output format would be C-style (true) or a simple list (false).
- return value - return value
- the size of the literal-list, if it was generated succesfully (i.e. the list of literals isn't empty, - the size of the literal-list, if it was generated succesfully (i.e. the list of literals isn't empty,
@@ -5349,7 +5380,6 @@ jerry_parse_and_save_literals (const jerry_char_t *source_p,
```c ```c
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include "jerryscript.h" #include "jerryscript.h"
int int
@@ -5357,20 +5387,30 @@ main (void)
{ {
jerry_init (JERRY_INIT_EMPTY); jerry_init (JERRY_INIT_EMPTY);
static uint32_t save_literal_buffer[256]; static jerry_char_t literal_buffer[256];
const jerry_char_t *code_for_literal_save_p = (const jerry_char_t *) "var obj = { a:'aa', bb:'Bb' }"; static uint32_t snapshot_buffer[256];
const jerry_char_t script_for_literal_save[] = "var obj = { a:'aa', bb:'Bb' }";
size_t literal_sizes = jerry_parse_and_save_literals (code_for_literal_save_p, jerry_value_t generate_result = jerry_generate_snapshot (NULL,
strlen ((const char *) code_for_literal_save_p), 0,
false, script_for_literal_save,
save_literal_buffer, sizeof (script_for_literal_save) - 1,
sizeof (save_literal_buffer) / sizeof (uint32_t), 0,
true); snapshot_buffer,
256);
size_t snapshot_size = (size_t) jerry_get_number_value (generate_result);
jerry_release_value (generate_result);
if (literal_sizes != 0) const size_t literal_size = jerry_get_literals_from_snapshot (snapshot_buffer,
snapshot_size,
literal_buffer,
256,
true);
if (literal_size != 0)
{ {
FILE *literal_file_p = fopen ("literals.txt", "w"); FILE *literal_file_p = fopen ("literals.h", "wb");
fwrite (save_literal_buffer, sizeof (uint8_t), literal_sizes, literal_file_p); fwrite (literal_buffer, sizeof (uint8_t), literal_size, literal_file_p);
fclose (literal_file_p); fclose (literal_file_p);
} }
@@ -5426,7 +5466,6 @@ jerry_set_vm_exec_stop_callback (jerry_vm_exec_stop_callback_t stop_cb,
[doctest]: # (test="link") [doctest]: # (test="link")
```c ```c
#include <string.h>
#include "jerryscript.h" #include "jerryscript.h"
static int countdown = 10; static int countdown = 10;
@@ -5452,11 +5491,11 @@ main (void)
jerry_set_vm_exec_stop_callback (vm_exec_stop_callback, &countdown, 16); jerry_set_vm_exec_stop_callback (vm_exec_stop_callback, &countdown, 16);
// Inifinte loop. // Inifinte loop.
const char *src_p = "while(true) {}"; const jerry_char_t script[] = "while(true) {}";
jerry_value_t src = jerry_parse (NULL, 0, (jerry_char_t *) src_p, strlen (src_p), JERRY_PARSE_NO_OPTS); jerry_value_t parsed_code = jerry_parse (NULL, 0, script, sizeof (script) - 1, JERRY_PARSE_NO_OPTS);
jerry_release_value (jerry_run (src)); jerry_release_value (jerry_run (parsed_code));
jerry_release_value (src); jerry_release_value (parsed_code);
jerry_cleanup (); jerry_cleanup ();
} }
``` ```
@@ -5875,9 +5914,8 @@ jerry_value_t jerry_json_parse (const jerry_char_t *string_p, jerry_size_t strin
```c ```c
{ {
const char *data = "{\"name\": \"John\", \"age\": 5}"; const jerry_char_t data[] = "{\"name\": \"John\", \"age\": 5}";
jerry_size_t str_length = (jerry_size_t)strlen (data); jerry_value_t parsed_json = jerry_json_parse (data, sizeof (data) - 1);
jerry_value_t parsed_json = jerry_json_parse ((jerry_char_t*)data, str_length);
// parsed_json now conatins all data stored in data_in_json // parsed_json now conatins all data stored in data_in_json
+10 -23
View File
@@ -17,16 +17,14 @@ This guide is intended to introduce you to JerryScript embedding API through cre
[doctest]: # () [doctest]: # ()
```c ```c
#include <string.h>
#include "jerryscript.h" #include "jerryscript.h"
int int
main (void) main (void)
{ {
const jerry_char_t script[] = "var str = 'Hello, World!';"; const jerry_char_t script[] = "var str = 'Hello, World!';";
size_t script_size = strlen ((const char *) script);
bool ret_value = jerry_run_simple (script, script_size, JERRY_INIT_EMPTY); bool ret_value = jerry_run_simple (script, sizeof (script) - 1, JERRY_INIT_EMPTY);
return (ret_value ? 0 : 1); return (ret_value ? 0 : 1);
} }
@@ -47,7 +45,6 @@ Here we perform the same actions, as `jerry_run_simple`, while splitting into se
[doctest]: # () [doctest]: # ()
```c ```c
#include <string.h>
#include "jerryscript.h" #include "jerryscript.h"
#include "jerryscript-ext/handler.h" #include "jerryscript-ext/handler.h"
@@ -55,7 +52,6 @@ int
main (void) main (void)
{ {
const jerry_char_t script[] = "print ('Hello, World!');"; const jerry_char_t script[] = "print ('Hello, World!');";
size_t script_size = strlen ((const char *) script);
/* Initialize engine */ /* Initialize engine */
jerry_init (JERRY_INIT_EMPTY); jerry_init (JERRY_INIT_EMPTY);
@@ -65,7 +61,7 @@ main (void)
jerryx_handler_print); jerryx_handler_print);
/* Setup Global scope code */ /* Setup Global scope code */
jerry_value_t parsed_code = jerry_parse (NULL, 0, script, script_size, JERRY_PARSE_NO_OPTS); jerry_value_t parsed_code = jerry_parse (NULL, 0, script, sizeof (script) - 1, JERRY_PARSE_NO_OPTS);
if (!jerry_value_is_error (parsed_code)) if (!jerry_value_is_error (parsed_code))
{ {
@@ -93,7 +89,6 @@ Our code is more complex now, but it introduces possibilities to interact with J
[doctest]: # () [doctest]: # ()
```c ```c
#include <string.h>
#include "jerryscript.h" #include "jerryscript.h"
#include "jerryscript-ext/handler.h" #include "jerryscript-ext/handler.h"
@@ -114,7 +109,7 @@ main (void)
/* Evaluate script1 */ /* Evaluate script1 */
eval_ret = jerry_eval (script_1, eval_ret = jerry_eval (script_1,
strlen ((const char *) script_1), sizeof (script_1) - 1,
JERRY_PARSE_NO_OPTS); JERRY_PARSE_NO_OPTS);
/* Free JavaScript value, returned by eval */ /* Free JavaScript value, returned by eval */
@@ -122,7 +117,7 @@ main (void)
/* Evaluate script2 */ /* Evaluate script2 */
eval_ret = jerry_eval (script_2, eval_ret = jerry_eval (script_2,
strlen ((const char *) script_2), sizeof (script_2) - 1,
JERRY_PARSE_NO_OPTS); JERRY_PARSE_NO_OPTS);
/* Free JavaScript value, returned by eval */ /* Free JavaScript value, returned by eval */
@@ -142,7 +137,6 @@ This way, we execute two independent script parts in one execution environment.
[doctest]: # () [doctest]: # ()
```c ```c
#include <string.h>
#include "jerryscript.h" #include "jerryscript.h"
#include "jerryscript-ext/handler.h" #include "jerryscript-ext/handler.h"
@@ -178,7 +172,7 @@ main (void)
/* Now starting script that would output value of just initialized field */ /* Now starting script that would output value of just initialized field */
jerry_value_t eval_ret = jerry_eval (script, jerry_value_t eval_ret = jerry_eval (script,
strlen ((const char *) script), sizeof (script) - 1,
JERRY_PARSE_NO_OPTS); JERRY_PARSE_NO_OPTS);
/* Free JavaScript value, returned by eval */ /* Free JavaScript value, returned by eval */
@@ -207,7 +201,6 @@ The following example function will output a JavaScript value:
```c ```c
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include "jerryscript.h" #include "jerryscript.h"
static void static void
@@ -322,7 +315,7 @@ main (void)
} }
/* If the command is "quit", break the loop */ /* If the command is "quit", break the loop */
if (!strncmp (cmd, "quit\n", strlen ("quit\n"))) if (!strncmp (cmd, "quit\n", sizeof ("quit\n") - 1))
{ {
break; break;
} }
@@ -362,7 +355,6 @@ In this example we demonstrate how to use native function and structures in Java
[doctest]: # () [doctest]: # ()
```c ```c
#include <string.h>
#include "jerryscript.h" #include "jerryscript.h"
#include "jerryscript-ext/handler.h" #include "jerryscript-ext/handler.h"
@@ -427,10 +419,9 @@ main (void)
var str = MyObject.myFunc (); \ var str = MyObject.myFunc (); \
print (str); \ print (str); \
"; ";
size_t script_size = strlen ((const char *) script);
/* Evaluate script */ /* Evaluate script */
jerry_value_t eval_ret = jerry_eval (script, script_size, JERRY_PARSE_NO_OPTS); jerry_value_t eval_ret = jerry_eval (script, sizeof (script) - 1, JERRY_PARSE_NO_OPTS);
/* Free JavaScript value, returned by eval */ /* Free JavaScript value, returned by eval */
jerry_release_value (eval_ret); jerry_release_value (eval_ret);
@@ -455,7 +446,6 @@ Here we create a JS Object with `jerry_eval`, then extend it with a native funct
[doctest]: # () [doctest]: # ()
```c ```c
#include <string.h>
#include "jerryscript.h" #include "jerryscript.h"
#include "jerryscript-ext/handler.h" #include "jerryscript-ext/handler.h"
@@ -518,7 +508,7 @@ main (void)
/* Evaluate script */ /* Evaluate script */
my_js_obj_val = jerry_eval (my_js_object, my_js_obj_val = jerry_eval (my_js_object,
strlen ((const char *) my_js_object), sizeof (my_js_object) - 1,
JERRY_PARSE_NO_OPTS); JERRY_PARSE_NO_OPTS);
/* Create a JS function object and wrap into a jerry value */ /* Create a JS function object and wrap into a jerry value */
@@ -539,10 +529,9 @@ main (void)
MyObject.add2x (5); \ MyObject.add2x (5); \
print (MyObject.foo ()); \ print (MyObject.foo ()); \
"; ";
size_t script_size = strlen ((const char *) script);
/* Evaluate script */ /* Evaluate script */
jerry_value_t eval_ret = jerry_eval (script, script_size, JERRY_PARSE_NO_OPTS); jerry_value_t eval_ret = jerry_eval (script, sizeof (script) - 1, JERRY_PARSE_NO_OPTS);
/* Free JavaScript value, returned by eval */ /* Free JavaScript value, returned by eval */
jerry_release_value (eval_ret); jerry_release_value (eval_ret);
@@ -569,7 +558,6 @@ A recommended method is using `jerry_port_get_current_time()` or something based
[doctest]: # () [doctest]: # ()
```c ```c
#include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include "jerryscript.h" #include "jerryscript.h"
#include "jerryscript-port.h" #include "jerryscript-port.h"
@@ -583,7 +571,6 @@ main (void)
/* Generate a random number, and print it */ /* Generate a random number, and print it */
const jerry_char_t script[] = "var a = Math.random (); print(a)"; const jerry_char_t script[] = "var a = Math.random (); print(a)";
size_t script_size = strlen ((const char *) script);
/* Initialize the engine */ /* Initialize the engine */
jerry_init (JERRY_INIT_EMPTY); jerry_init (JERRY_INIT_EMPTY);
@@ -593,7 +580,7 @@ main (void)
jerryx_handler_print); jerryx_handler_print);
/* Evaluate the script */ /* Evaluate the script */
jerry_value_t eval_ret = jerry_eval (script, script_size, JERRY_PARSE_NO_OPTS); jerry_value_t eval_ret = jerry_eval (script, sizeof (script) - 1, JERRY_PARSE_NO_OPTS);
/* Free the JavaScript value returned by eval */ /* Free the JavaScript value returned by eval */
jerry_release_value (eval_ret); jerry_release_value (eval_ret);
+22 -19
View File
@@ -122,23 +122,25 @@ bool jerry_port_get_time_zone (jerry_time_zone_t *tz_p);
double jerry_port_get_current_time (void); double jerry_port_get_current_time (void);
``` ```
## External instance ## External context
Allow user to provide external buffer for jerry instance (which includes an isolated context and heap with other instances), so that user can config the heap size in runtime and run multiple JS apps simultaneously. Allow user to provide external buffer for isolated engine contexts, so that user
can configure the heap size at runtime and run multiple JS applications
simultaneously.
```c ```c
/** /**
* Get the current instance which contains the current context, heap and other * Get the current context of the engine. Each port should provide its own
* structures. Each port should provide its own implementation of this interface. * implementation of this interface.
* *
* Note: * Note:
* This port function is called by jerry-core when * This port function is called by jerry-core when
* JERRY_ENABLE_EXTERNAL_CONTEXT is defined. Otherwise this function is not * JERRY_ENABLE_EXTERNAL_CONTEXT is defined. Otherwise this function is not
* used. * used.
* *
* @return the pointer to the jerry instance. * @return the pointer to the engine context.
*/ */
struct jerry_instance_t *jerry_port_get_current_instance (void); struct jerry_context_t *jerry_port_get_current_context (void);
``` ```
## Sleep ## Sleep
@@ -243,37 +245,38 @@ double jerry_port_get_current_time (void)
return ((double) tv.tv_sec) * 1000.0 + ((double) tv.tv_usec) / 1000.0; return ((double) tv.tv_sec) * 1000.0 + ((double) tv.tv_usec) / 1000.0;
} /* jerry_port_get_current_time */ } /* jerry_port_get_current_time */
``` ```
## External instance
## External context
```c ```c
#include "jerryscript-port.h" #include "jerryscript-port.h"
#include "jerryscript-port-default.h" #include "jerryscript-port-default.h"
/** /**
* Pointer to the current instance. * Pointer to the current context.
* Note that it is a global variable, and is not a thread safe implementation. * Note that it is a global variable, and is not a thread safe implementation.
*/ */
static jerry_instance_t *current_instance_p = NULL; static jerry_context_t *current_context_p = NULL;
/** /**
* Set the current_instance_p as the passed pointer. * Set the current_context_p as the passed pointer.
*/ */
void void
jerry_port_default_set_instance (jerry_instance_t *instance_p) /**< points to the created instance */ jerry_port_default_set_context (jerry_context_t *context_p) /**< points to the created context */
{ {
current_instance_p = instance_p; current_context_p = context_p;
} /* jerry_port_default_set_instance */ } /* jerry_port_default_set_context */
/** /**
* Get the current instance. * Get the current context.
* *
* @return the pointer to the current instance * @return the pointer to the current context
*/ */
jerry_instance_t * jerry_context_t *
jerry_port_get_current_instance (void) jerry_port_get_current_context (void)
{ {
return current_instance_p; return current_context_p;
} /* jerry_port_get_current_instance */ } /* jerry_port_get_current_context */
``` ```
## Sleep ## Sleep
+1 -3
View File
@@ -261,7 +261,6 @@ jerry_debugger_stop_at_breakpoint (bool enable_stop_at_breakpoint)
[doctest]: # (test="link") [doctest]: # (test="link")
```c ```c
#include <string.h>
#include "jerryscript.h" #include "jerryscript.h"
#include "jerryscript-ext/debugger.h" #include "jerryscript-ext/debugger.h"
@@ -276,8 +275,7 @@ main (void)
// Protected execution of JavaScript code. // Protected execution of JavaScript code.
const jerry_char_t script[] = "42"; const jerry_char_t script[] = "42";
size_t script_size = strlen ((const char *) script); jerry_eval (script, sizeof (script) - 1, JERRY_PARSE_NO_OPTS);
jerry_eval (script, script_size, JERRY_PARSE_NO_OPTS);
jerry_debugger_stop_at_breakpoint (false); jerry_debugger_stop_at_breakpoint (false);
+41 -4
View File
@@ -10,18 +10,18 @@ permalink: /ext-reference-handler/
# Common external function handlers # Common external function handlers
## jerryx_handler_assert ## jerryx_handler_assert_fatal
**Summary** **Summary**
Assert for scripts. The routine calls `jerry_port_fatal` on assertion failure. Hard assert for scripts. The routine calls `jerry_port_fatal` on assertion failure.
**Prototype** **Prototype**
```c ```c
jerry_value_t jerry_value_t
jerryx_handler_assert (const jerry_value_t func_obj_val, const jerry_value_t this_p, jerryx_handler_assert_fatal (const jerry_value_t func_obj_val, const jerry_value_t this_p,
const jerry_value_t args_p[], const jerry_length_t args_cnt); const jerry_value_t args_p[], const jerry_length_t args_cnt);
``` ```
- `func_obj_val` - the function object that was called (unused). - `func_obj_val` - the function object that was called (unused).
@@ -37,6 +37,43 @@ jerryx_handler_assert (const jerry_value_t func_obj_val, const jerry_value_t thi
- [jerryx_handler_register_global](#jerryx_handler_register_global) - [jerryx_handler_register_global](#jerryx_handler_register_global)
## jerryx_handler_assert_throw
**Summary**
Soft assert for scripts. The routine throws an error on assertion failure.
**Prototype**
```c
jerry_value_t
jerryx_handler_assert_throw (const jerry_value_t func_obj_val, const jerry_value_t this_p,
const jerry_value_t args_p[], const jerry_length_t args_cnt);
```
- `func_obj_val` - the function object that was called (unused).
- `this_p` - the `this` value of the call (unused).
- `args_p` - the array of function arguments.
- `args_cnt` - the number of function arguments.
- return value - `jerry_value_t` representing boolean true, if only one argument
was passed and that argument was a boolean true, an error otherwise.
**See also**
- [jerryx_handler_register_global](#jerryx_handler_register_global)
## jerryx_handler_assert_fatal
**Summary**
An alias to `jerryx_handler_assert_fatal`.
**See also**
- [jerryx_handler_assert_fatal](#jerryx_handler_assert_fatal)
## jerryx_handler_gc ## jerryx_handler_gc
**Summary** **Summary**
+2 -33
View File
@@ -1,6 +1,6 @@
--- ---
layout: page layout: page
title: debugger transport title: 'Debugger Transport'
category: documents category: documents
permalink: /debugger-transport/ permalink: /debugger-transport/
--- ---
@@ -106,37 +106,6 @@ typedef bool (*jerry_debugger_transport_receive_t) (struct jerry_debugger_transp
# Transport interface API functions # Transport interface API functions
## jerry_debugger_transport_malloc
**Summary**
Allocates memory for the transport interface.
**Prototype**
```c
void * jerry_debugger_transport_malloc (size_t size);
```
- `size`: size of the memory block.
- return value: non-NULL pointer, if the memory is successfully allocated,
NULL otherwise.
## jerry_debugger_transport_free
**Summary**
Free memory allocated by [jerry_debugger_transport_malloc](#jerry_debugger_transport_malloc)
**Prototype**
```c
void jerry_debugger_transport_free (void *mem_p, size_t size);
```
- `header_p`: header of a transporation interface.
- `size`: total size of the transportation interface.
## jerry_debugger_transport_add ## jerry_debugger_transport_add
**Summary** **Summary**
@@ -190,7 +159,7 @@ bool jerry_debugger_transport_is_connected (void);
**Summary** **Summary**
Disconnect from the current debugger client. It does nothing if a client is Disconnect from the current debugger client. It does nothing if a client is
not connected, not connected.
**Prototype** **Prototype**