Update the webpage (#1941)

* Add new documents about autorelease values and module support

JerryScript-DCO-1.0-Signed-off-by: Zsolt Borbély zsborbely.u-szeged@partner.samsung.com
This commit is contained in:
Zsolt Borbély
2017-08-01 16:00:13 +02:00
committed by GitHub
parent 5d2b25659d
commit 49c24ca464
7 changed files with 923 additions and 153 deletions
+292 -116
View File
@@ -111,6 +111,25 @@ created by API functions has the error flag set.
typedef uint32_t jerry_value_t;
```
## jerry_context_data_manager_t
**Summary**
Structure that defines how a context data item will be initialized and deinitialized. JerryScript zeroes out the memory
for the item by default, and if the `init_cb` field is not NULL, it will be called with the pointer to the memory as
an additional custom initializer.
**Prototype**
```c
typedef struct
{
void (*init_cb) (void *); /**< callback responsible for initializing a context item, or NULL */
void (*deinit_cb) (void *); /**< callback responsible for deinitializing a context item */
size_t bytes_needed; /**< number of bytes to allocate for this manager */
} jerry_context_data_manager_t;
```
## jerry_property_descriptor_t
**Summary**
@@ -274,9 +293,7 @@ typedef jerry_value_t (*jerry_vm_exec_stop_callback_t) (void *user_p);
**Summary**
Initializes the JerryScript engine, making it possible to run JavaScript code and perform operations
on JavaScript values. See also [jerry_init_with_user_context](#jerry_init_with_user_context) if you
wish to initialize the JerryScript engine in such a way that its context contains a custom pointer
which you can later retrieve using [jerry_get_user_context](#jerry_get_user_context).
on JavaScript values.
**Prototype**
@@ -296,7 +313,13 @@ jerry_init (jerry_init_flag_t flags)
**Example**
[doctest]: # ()
```c
#include "jerryscript.h"
int
main (void)
{
jerry_init (JERRY_INIT_SHOW_OPCODES | JERRY_INIT_SHOW_REGEXP_OPCODES);
@@ -309,91 +332,13 @@ jerry_init (jerry_init_flag_t flags)
**See also**
- [jerry_cleanup](#jerry_cleanup)
- [jerry_init_with_user_context](#jerry_init_with_user_context)
## jerry_init_with_user_context
**Summary**
Calls [jerry_init](#jerry_init) to initialize the JerryScript engine, thereby making it possible
to run JavaScript code and perform operations on JavaScript values. In addition to the first
parameter this function accepts two more parameters with which it allows the caller to store a
`void *` pointer inside the context being initialized with `jerry_init ()`. The function calls the
callback given in its `init_cb` parameter to allocate the memory for the pointer and it stores the
function pointer given in the `deinit_cb` parameter along with the pointer so that it may be called
to free the stored pointer when `jerry_cleanup ()` is later called to dispose of the context.
**Prototype**
```c
void
jerry_init_with_user_context (jerry_init_flag_t flags,
jerry_user_context_init_cb init_cb,
jerry_user_context_deinit_cb deinit_cb);
```
`flags` - combination of various engine configuration flags:
- `JERRY_INIT_EMPTY` - no flags, just initialize in default configuration.
- `JERRY_INIT_SHOW_OPCODES` - print compiled byte-code.
- `JERRY_INIT_SHOW_REGEXP_OPCODES` - print compiled regexp byte-code.
- `JERRY_INIT_MEM_STATS` - dump memory statistics.
- `JERRY_INIT_MEM_STATS_SEPARATE` - dump memory statistics and reset peak values after parse.
- `JERRY_INIT_DEBUGGER` - enable all features required by debugging.
`init_cb` - a function pointer that will be called to allocate the custom pointer.
`deinit_cb` - a function pointer that will be called when the custom pointer must be freed.
**Example**
```c
void *
init_user_context (void)
{
void *return_value;
/* allocate and initialize return_value */
return return_value;
} /* init_user_context */
void
free_user_context (void *context)
{
/* free the value allocated above */
} /* free_user_context */
{
/* init_user_context () will be called before the call below returns */
jerry_init_with_user_context (JERRY_INIT_SHOW_OPCODES | JERRY_INIT_SHOW_REGEXP_OPCODES,
init_user_context,
free_user_context);
/* ... */
/* free_user_context () will be called before the call below returns */
jerry_cleanup ();
}
```
**See also**
- [jerry_cleanup](#jerry_cleanup)
- [jerry_get_user_context](#jerry_get_user_context)
## jerry_cleanup
**Summary**
Finish JavaScript engine execution, freeing memory and JavaScript values. If the context was
initialized with `jerry_init_with_user_context ()` and a `deinit_cb` was provided, then it will
be called to free the memory at the custom pointer which was associated with the context being
cleaned up.
Finish JavaScript engine execution, freeing memory and JavaScript values.
*Note*: JavaScript values, received from engine, will be inaccessible after the cleanup.
@@ -407,38 +352,86 @@ jerry_cleanup (void);
**See also**
- [jerry_init](#jerry_init)
- [jerry_init_with_user_context](#jerry_init_with_user_context)
## jerry_get_user_context
## jerry_get_context_data
**Summary**
Retrieve the pointer stored within the current context.
Retrieve a pointer to the item stored within the current context by the given manager.
*Note*: Since internally the pointer to a manager's context data item is linked to the next such pointer in a linked
list, it is inadvisable to invoke too many different managers, because doing so will increase the time it takes
to retrieve a manager's context data item, degrading performance. For example, try to keep the number of
managers below five.
**Prototype**
```c
void *
jerry_get_user_context (void);
jerry_get_context_data (const jerry_context_data_manager *manager_p);
```
- return value: the pointer that was assigned during `jerry_init_with_user_context ()`
- `manager_p`: the manager of this context data item.
- return value: the item created by `manager_p` when `jerry_get_context_data ()` was first called, or a new item created
by `manager_p`, which will be stored for future identical calls to `jerry_get_context_data ()`, and which will be
deinitialized using the `deinit_cb` callback provided by `manager_p` when the context will be destroyed.
**Example**
[doctest]: # (test="compile")
```c
#include "jerryscript.h"
typedef struct
{
/* ... */
my_context *custom_data = (my_context *) jerry_get_user_context ();
/* ... */
int my_data1;
double my_data2;
char *my_data3;
} my_context_data_t;
/* Define how context items will be initialized. */
static void
my_context_data_new (void *user_data_p)
{
my_context_data_t *my_data_p = (my_context_data_t *) user_data_p;
/*
* Initialize my_data_p. JerryScript will store it on the current context and return it whenever
* jerry_get_context_data () is called with a pointer to my_manager as defined below.
*/
}
/* Define how context items will be deinitialized */
static void
my_context_data_free (void *user_data_p)
{
my_context_data_t *my_data_p = ((my_context_data_t *) user_data_p);
/* Perform any necessary cleanup on my_data. JerryScript will free the pointer after this function completes. */
}
/* Wrap the creation and destruction functions into a manager */
static const jerry_context_data_manager_t my_manager =
{
.init_cb = my_context_data_new,
.deinit_cb = my_context_data_free,
.bytes_needed = sizeof (my_context_data_t)
};
/*
* Then, in some function in your code, you can retrieve an item of type my_context_data_t from the currently active
* context such that JerryScript will create and store such an item if one was not previously created
*/
static void
someplace_in_the_code (void)
{
my_context_data_t *my_data = (my_context_data_t *) jerry_get_context_data (&my_manager);
/* Perform useful things using the data found in my_data */
}
```
**See also**
- [jerry_init_with_user_context](#jerry_init_with_user_context)
- [jerry_cleanup](#jerry_cleanup)
## jerry_register_magic_strings
@@ -463,7 +456,13 @@ jerry_register_magic_strings (const jerry_char_ptr_t *ex_str_items_p,
**Example**
[doctest]: # ()
```c
#include "jerryscript.h"
int
main (void)
{
jerry_init (JERRY_INIT_EMPTY);
@@ -478,9 +477,9 @@ jerry_register_magic_strings (const jerry_char_ptr_t *ex_str_items_p,
// must be static, because 'jerry_register_magic_strings' does not copy
static const jerry_length_t magic_string_lengths[] = {
(jerry_length_t)strlen (magic_string_items[0]),
(jerry_length_t)strlen (magic_string_items[1]),
(jerry_length_t)strlen (magic_string_items[2])
12,
12,
12
};
jerry_register_magic_strings (magic_string_items, num_magic_string_items, magic_string_lengths);
}
@@ -512,7 +511,13 @@ jerry_get_memory_limits (size_t *out_data_bss_brk_limit_p,
**Example**
[doctest]: # ()
```c
#include "jerryscript.h"
int
main (void)
{
jerry_init (JERRY_INIT_EMPTY);
@@ -580,9 +585,16 @@ jerry_run_simple (const jerry_char_t *script_source_p,
**Example**
[doctest]: # ()
```c
#include <string.h>
#include "jerryscript.h"
int
main (void)
{
const jerry_char_t *script = "print ('Hello, World!');";
const jerry_char_t *script = (const jerry_char_t *) "print ('Hello, World!');";
jerry_run_simple (script, strlen ((const char *) script), JERRY_INIT_EMPTY);
}
@@ -624,7 +636,14 @@ jerry_parse (const jerry_char_t *source_p,
**Example**
[doctest]: # ()
```c
#include <string.h>
#include "jerryscript.h"
int
main (void)
{
jerry_init (JERRY_INIT_EMPTY);
@@ -699,7 +718,14 @@ jerry_run (const jerry_value_t func_val);
**Example**
[doctest]: # ()
```c
#include <string.h>
#include "jerryscript.h"
int
main (void)
{
const jerry_char_t script[] = "print ('Hello, World!');";
size_t script_size = strlen ((const char *) script);
@@ -767,6 +793,49 @@ jerry_eval (const jerry_char_t *source_p,
- [jerry_create_external_function](#jerry_create_external_function)
- [jerry_external_handler_t](#jerry_external_handler_t)
## jerry_run_all_enqueued_jobs
**Summary**
Run enqueued Promise jobs until the first thrown error or until all get executed.
**Prototype**
```c
jerry_value_t
jerry_run_all_enqueued_jobs (void)
```
- return value - result of last executed job, may be error value.
**Example**
[doctest]: # ()
```c
#include <string.h>
#include "jerryscript.h"
int
main (void)
{
jerry_init (JERRY_INIT_EMPTY);
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 (script, script_size, false);
jerry_value_t script_value = jerry_run (parsed_code);
jerry_value_t job_value = jerry_run_all_enqueued_jobs ();
jerry_release_value (job_value);
jerry_release_value (script_value);
jerry_release_value (parsed_code);
jerry_cleanup ();
}
```
# Get the global context
@@ -2562,6 +2631,7 @@ jerry_create_promise (void)
jerry_release_value (p);
}
```
**See also**
@@ -2759,19 +2829,22 @@ jerry_create_undefined (void);
**Summary**
Checks whether the object or it's prototype objects have the given property.
Checks whether the object or its prototype objects have the given property.
*Note*: Returned value must be freed with [jerry_release_value](#jerry_release_value) when it
is no longer needed.
**Prototype**
```c
bool
jerry_value_t
jerry_has_property (const jerry_value_t obj_val,
const jerry_value_t prop_name_val);
```
- `obj_val` - object value
- `prop_name_val` - property name
- return value
- return value - JavaScript boolean value that evaluates to
- true, if the property exists
- false, otherwise
@@ -2782,8 +2855,10 @@ jerry_has_property (const jerry_value_t obj_val,
jerry_value_t global_object = jerry_get_global_object ();
jerry_value_t prop_name = jerry_create_string ((const jerry_char_t *) "handler_field");
bool has_prop = jerry_has_property (global_object, prop_name);
jerry_value_t has_prop_js = jerry_has_property (global_object, prop_name);
bool has_prop = jerry_get_boolean_value (has_prop_js);
jerry_release_value (has_prop_js);
jerry_release_value (prop_name);
jerry_release_value (global_object);
}
@@ -2801,17 +2876,20 @@ jerry_has_property (const jerry_value_t obj_val,
Checks whether the object has the given property.
*Note*: Returned value must be freed with [jerry_release_value](#jerry_release_value) when it
is no longer needed.
**Prototype**
```c
bool
jerry_value_t
jerry_has_own_property (const jerry_value_t obj_val,
const jerry_value_t prop_name_val);
```
- `obj_val` - object value
- `prop_name_val` - property name
- return value
- return value - JavaScript boolean value that evaluates to
- true, if the property exists
- false, otherwise
@@ -2822,8 +2900,10 @@ jerry_has_own_property (const jerry_value_t obj_val,
jerry_value_t global_object = jerry_get_global_object ();
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);
jerry_value_t has_prop_js = jerry_has_own_property (global_object, prop_name);
bool has_prop = jerry_get_boolean_value (has_prop_js);
jerry_release_value (jas_prop_js);
jerry_release_value (prop_name);
jerry_release_value (global_object);
}
@@ -2873,9 +2953,55 @@ jerry_delete_property (const jerry_value_t obj_val,
- [jerry_has_property](#jerry_has_property)
- [jerry_has_own_property](#jerry_has_own_property)
- [jerry_delete_property_by_index](#jerry_delete_property_by_index)
- [jerry_get_property](#jerry_get_property)
## jerry_delete_property_by_index
**Summary**
Delete indexed property from the specified object.
**Prototype**
```c
bool
jerry_delete_property_by_index (const jerry_value_t obj_val,
uint32_t index);
```
- `obj_val` - object value
- `index` - index number
- return value
- true, if property was deleted successfully
- false, otherwise
**Example**
```c
{
jerry_value_t object;
... // create or acquire object
jerry_delete_property_by_index (object, 5);
jerry_release_value (object);
}
```
**See also**
- [jerry_has_property](#jerry_has_property)
- [jerry_has_own_property](#jerry_has_own_property)
- [jerry_delete_property](#jerry_delete_property)
- [jerry_get_property](#jerry_get_property)
- [jerry_set_property](#jerry_set_property)
- [jerry_get_property_by_index](#jerry_get_property_by_index)
- [jerry_set_property_by_index](#jerry_set_property_by_index)
## jerry_get_property
**Summary**
@@ -2918,6 +3044,7 @@ jerry_get_property (const jerry_value_t obj_val,
- [jerry_has_property](#jerry_has_property)
- [jerry_has_own_property](#jerry_has_own_property)
- [jerry_delete_property](#jerry_delete_property)
- [jerry_delete_property_by_index](#jerry_delete_property_by_index)
- [jerry_set_property](#jerry_set_property)
- [jerry_get_property_by_index](#jerry_get_property_by_index)
- [jerry_set_property_by_index](#jerry_set_property_by_index)
@@ -2968,6 +3095,7 @@ jerry_get_property_by_index (const jerry_value_t obj_val,
- [jerry_has_property](#jerry_has_property)
- [jerry_has_own_property](#jerry_has_own_property)
- [jerry_delete_property](#jerry_delete_property)
- [jerry_delete_property_by_index](#jerry_delete_property_by_index)
- [jerry_get_property](#jerry_get_property)
- [jerry_set_property](#jerry_set_property)
- [jerry_set_property_by_index](#jerry_set_property_by_index)
@@ -3028,6 +3156,7 @@ jerry_set_property (const jerry_value_t obj_val,
- [jerry_has_property](#jerry_has_property)
- [jerry_has_own_property](#jerry_has_own_property)
- [jerry_delete_property](#jerry_delete_property)
- [jerry_delete_property_by_index](#jerry_delete_property_by_index)
- [jerry_get_property](#jerry_get_property)
- [jerry_get_property_by_index](#jerry_get_property_by_index)
- [jerry_set_property_by_index](#jerry_set_property_by_index)
@@ -3082,6 +3211,7 @@ jerry_set_property_by_index (const jerry_value_t obj_val,
- [jerry_has_property](#jerry_has_property)
- [jerry_has_own_property](#jerry_has_own_property)
- [jerry_delete_property](#jerry_delete_property)
- [jerry_delete_property_by_index](#jerry_delete_property_by_index)
- [jerry_get_property](#jerry_get_property)
- [jerry_set_property](#jerry_set_property)
- [jerry_get_property_by_index](#jerry_get_property_by_index)
@@ -3611,6 +3741,9 @@ The pointer and the type information are previously associated with the object b
`out_native_pointer_p` is of the expected type, before casting
and dereferencing `out_native_pointer_p`.
*Note*: `out_native_pointer_p` and `out_native_info_p` can be NULL, and it means the
caller doesn't want to get the native_pointer or type infomation.
**Prototype**
```c
@@ -3808,7 +3941,14 @@ jerry_is_valid_utf8_string (const jerry_char_t *utf8_buf_p, /**< UTF-8 string */
**Example**
[doctest]: # ()
```c
#include <string.h>
#include "jerryscript.h"
int
main (void)
{
const jerry_char_t script[] = "print ('Hello, World!');";
size_t script_size = strlen ((const char *) script);
@@ -3849,7 +3989,14 @@ jerry_is_valid_cesu8_string (const jerry_char_t *cesu8_buf_p, /**< CESU-8 string
**Example**
[doctest]: # ()
```c
#include <string.h>
#include "jerryscript.h"
int
main (void)
{
jerry_init (JERRY_INIT_EMPTY);
@@ -3859,9 +4006,9 @@ jerry_is_valid_cesu8_string (const jerry_char_t *cesu8_buf_p, /**< CESU-8 string
if (jerry_is_valid_cesu8_string (script, (jerry_size_t) script_size))
{
jerry_value_t string_value = jerry_create_string_sz (script,
(jerry_size_t) script_size));
(jerry_size_t) script_size);
... // usage of string_value
// usage of string_value
jerry_release_value (string_value);
}
@@ -3914,12 +4061,19 @@ jerry_parse_and_save_snapshot (const jerry_char_t *source_p,
**Example**
[doctest]: # ()
```c
#include <string.h>
#include "jerryscript.h"
int
main (void)
{
jerry_init (JERRY_INIT_EMPTY);
static uint32_t global_mode_snapshot_buffer[256];
const jerry_char_t *code_to_snapshot_p = "(function () { return 'string from snapshot'; }) ();";
const jerry_char_t *code_to_snapshot_p = (const jerry_char_t *) "(function () { return 'string from snapshot'; }) ();";
size_t global_mode_snapshot_size = jerry_parse_and_save_snapshot (code_to_snapshot_p,
strlen ((const char *) code_to_snapshot_p),
@@ -3969,11 +4123,17 @@ jerry_exec_snapshot (const uint32_t *snapshot_p,
**Example**
[doctest]: # ()
```c
#include <string.h>
#include "jerryscript.h"
int
main (void)
{
jerry_value_t res;
static uint32_t global_mode_snapshot_buffer[256];
const jerry_char_t *code_to_snapshot_p = "(function () { return 'string from snapshot'; }) ();";
const jerry_char_t *code_to_snapshot_p = (const jerry_char_t *) "(function () { return 'string from snapshot'; }) ();";
jerry_init (JERRY_INIT_EMPTY);
size_t global_mode_snapshot_size = jerry_parse_and_save_snapshot (code_to_snapshot_p,
@@ -3986,9 +4146,10 @@ jerry_exec_snapshot (const uint32_t *snapshot_p,
jerry_init (JERRY_INIT_EMPTY);
res = (jerry_exec_snapshot (global_mode_snapshot_buffer,
global_mode_snapshot_size,
false);
jerry_value_t res = jerry_exec_snapshot (global_mode_snapshot_buffer,
global_mode_snapshot_size,
false);
jerry_release_value (res);
jerry_cleanup ();
}
@@ -4033,12 +4194,20 @@ jerry_parse_and_save_literals (const jerry_char_t *source_p,
**Example**
[doctest]: # (test="link")
```c
#include <stdio.h>
#include <string.h>
#include "jerryscript.h"
int
main (void)
{
jerry_init (JERRY_INIT_EMPTY);
static uint32_t save_literal_buffer[256];
const jerry_char_t *code_for_literal_save_p = "var obj = { a:'aa', bb:'Bb' }";
const jerry_char_t *code_for_literal_save_p = (const jerry_char_t *) "var obj = { a:'aa', bb:'Bb' }";
size_t literal_sizes = jerry_parse_and_save_literals (code_for_literal_save_p,
strlen ((const char *) code_for_literal_save_p),
@@ -4103,12 +4272,17 @@ jerry_set_vm_exec_stop_callback (jerry_vm_exec_stop_callback_t stop_cb,
**Example**
[doctest]: # (test="link")
```c
#include <string.h>
#include "jerryscript.h"
static int countdown = 10;
static jerry_value_t
vm_exec_stop_callback (void *user_p)
{
static int countdown = 10;
while (countdown > 0)
{
countdown--;
@@ -4119,6 +4293,8 @@ vm_exec_stop_callback (void *user_p)
return jerry_create_string ((const jerry_char_t *) "Abort script");
}
int
main (void)
{
jerry_init (JERRY_INIT_EMPTY);