Skip to content

Commit df18930

Browse files
LaszloLangoakosthekiss
authored andcommitted
Added missing documentation of JerryScript instances to the API reference. (#2482)
JerryScript-DCO-1.0-Signed-off-by: László Langó [email protected]
1 parent 7d41d38 commit df18930

File tree

2 files changed

+128
-1
lines changed

2 files changed

+128
-1
lines changed

docs/02.API-REFERENCE.md

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,34 @@ typedef struct
250250
} jerry_context_data_manager_t;
251251
```
252252

253+
## jerry_instance_alloc_t
254+
255+
**Summary**
256+
257+
Function type for allocating buffer for JerryScript instance.
258+
259+
**Prototype**
260+
261+
```c
262+
typedef void *(*jerry_instance_alloc_t) (size_t size, void *cb_data_p);
263+
```
264+
265+
- `size` - allocation size
266+
- `cb_data_p` - pointer to user data
267+
268+
269+
## jerry_instance_t
270+
271+
**Summary**
272+
273+
An opaque declaration of the JerryScript instance structure which is the header of the context space.
274+
275+
**Prototype**
276+
277+
```c
278+
typedef struct jerry_instance_t jerry_instance_t;
279+
```
280+
253281
## jerry_property_descriptor_t
254282

255283
**Summary**
@@ -4859,6 +4887,105 @@ main (void)
48594887
- [jerry_substring_to_char_buffer](#jerry_substring_to_char_buffer)
48604888

48614889

4890+
# External context functions
4891+
4892+
## jerry_create_instance
4893+
4894+
**Summary**
4895+
4896+
Creates a JerryScript instance for external context.
4897+
4898+
**Prototype**
4899+
4900+
```c
4901+
jerry_instance_t *
4902+
jerry_create_instance (uint32_t heap_size,
4903+
jerry_instance_alloc_t alloc,
4904+
void *cb_data_p);
4905+
```
4906+
4907+
- `heap_size` - requested heap size of the JerryScript instance
4908+
- `alloc` - function for allocation
4909+
- `cb_data_p` - user data
4910+
- return value
4911+
- pointer to the newly created JerryScript instance if success
4912+
- NULL otherwise.
4913+
4914+
**Example**
4915+
4916+
[doctest]: # (test="compile")
4917+
4918+
```c
4919+
#include <stdlib.h>
4920+
#include <pthread.h>
4921+
4922+
#include "jerryscript.h"
4923+
#include "jerryscript-port.h"
4924+
4925+
/* A different Thread Local Storage variable for each jerry instance. */
4926+
__thread jerry_instance_t *tls_instance;
4927+
4928+
jerry_instance_t *
4929+
jerry_port_get_current_instance (void)
4930+
{
4931+
/* Returns the instance assigned to the thread. */
4932+
return tls_instance;
4933+
}
4934+
4935+
/* Allocate JerryScript heap for each thread. */
4936+
static void *
4937+
instance_alloc_fn (size_t size, void *cb_data)
4938+
{
4939+
(void) cb_data;
4940+
return malloc (size);
4941+
}
4942+
4943+
static void *
4944+
thread_function (void *param)
4945+
{
4946+
tls_instance = jerry_create_instance (512 * 1024,
4947+
instance_alloc_fn,
4948+
NULL);
4949+
jerry_init (JERRY_INIT_EMPTY);
4950+
/* Run the JerryScript instance (e.g.: jerry_parse & jerry_run) */
4951+
jerry_cleanup ();
4952+
4953+
/* Deallocate JerryScript instance */
4954+
free (tls_instance);
4955+
4956+
return NULL;
4957+
}
4958+
4959+
#define NUM_OF_THREADS 8
4960+
4961+
int
4962+
main (void)
4963+
{
4964+
pthread_t threads[NUM_OF_THREADS];
4965+
4966+
/* Create the threads. */
4967+
for (int i = 0; i < NUM_OF_THREADS; i++)
4968+
{
4969+
pthread_create (&threads[i], NULL, thread_function, (void *) (intptr_t) i);
4970+
}
4971+
4972+
/* Wait for the threads to complete, and release their resources. */
4973+
for (int i = 0; i < NUM_OF_THREADS; i++)
4974+
{
4975+
pthread_join (threads[i], NULL);
4976+
}
4977+
4978+
return 0;
4979+
}
4980+
```
4981+
4982+
**See also**
4983+
4984+
- [jerry_instance_t](#jerry_instance_t)
4985+
- [jerry_instance_alloc_t](#jerry_instance_alloc_t)
4986+
- [jerry_port_get_current_instance](05.PORT-API.md#jerry_port_get_current_instance)
4987+
4988+
48624989
# Snapshot functions
48634990

48644991
## jerry_generate_snapshot

jerry-core/include/jerryscript-core.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ typedef struct
299299
} jerry_object_native_info_t;
300300

301301
/**
302-
* A forward declaration of the JerryScript instance structure.
302+
* An opaque declaration of the JerryScript instance structure.
303303
*/
304304
typedef struct jerry_instance_t jerry_instance_t;
305305

0 commit comments

Comments
 (0)