Skip to content

Commit 30b7a72

Browse files
authored
Merge instance into context (#2501)
There was quite some confusion about terminology around instances and contexts. All the docs mentioned external contexts but functions and types were referring to instances, and the relation between these two concepts were not clear. This commit keeps (external) context as the only surviving concept. JerryScript-DCO-1.0-Signed-off-by: Akos Kiss [email protected]
1 parent d3d42f7 commit 30b7a72

File tree

9 files changed

+143
-138
lines changed

9 files changed

+143
-138
lines changed

docs/02.API-REFERENCE.md

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -238,32 +238,32 @@ typedef struct
238238
} jerry_context_data_manager_t;
239239
```
240240

241-
## jerry_instance_alloc_t
241+
## jerry_context_alloc_t
242242

243243
**Summary**
244244

245-
Function type for allocating buffer for JerryScript instance.
245+
Function type for allocating buffer for JerryScript context.
246246

247247
**Prototype**
248248

249249
```c
250-
typedef void *(*jerry_instance_alloc_t) (size_t size, void *cb_data_p);
250+
typedef void *(*jerry_context_alloc_t) (size_t size, void *cb_data_p);
251251
```
252252

253253
- `size` - allocation size
254254
- `cb_data_p` - pointer to user data
255255

256256

257-
## jerry_instance_t
257+
## jerry_context_t
258258

259259
**Summary**
260260

261-
An opaque declaration of the JerryScript instance structure which is the header of the context space.
261+
An opaque declaration of the JerryScript context structure.
262262

263263
**Prototype**
264264

265265
```c
266-
typedef struct jerry_instance_t jerry_instance_t;
266+
typedef struct jerry_context_t jerry_context_t;
267267
```
268268

269269
## jerry_property_descriptor_t
@@ -4877,26 +4877,26 @@ main (void)
48774877

48784878
# External context functions
48794879

4880-
## jerry_create_instance
4880+
## jerry_create_context
48814881

48824882
**Summary**
48834883

4884-
Creates a JerryScript instance for external context.
4884+
Create an external JerryScript engine context.
48854885

48864886
**Prototype**
48874887

48884888
```c
4889-
jerry_instance_t *
4890-
jerry_create_instance (uint32_t heap_size,
4891-
jerry_instance_alloc_t alloc,
4892-
void *cb_data_p);
4889+
jerry_context_t *
4890+
jerry_create_context (uint32_t heap_size,
4891+
jerry_context_alloc_t alloc,
4892+
void *cb_data_p);
48934893
```
48944894

4895-
- `heap_size` - requested heap size of the JerryScript instance
4895+
- `heap_size` - requested heap size of the JerryScript context
48964896
- `alloc` - function for allocation
48974897
- `cb_data_p` - user data
48984898
- return value
4899-
- pointer to the newly created JerryScript instance if success
4899+
- pointer to the newly created JerryScript context if success
49004900
- NULL otherwise.
49014901

49024902
**Example**
@@ -4910,19 +4910,19 @@ jerry_create_instance (uint32_t heap_size,
49104910
#include "jerryscript.h"
49114911
#include "jerryscript-port.h"
49124912

4913-
/* A different Thread Local Storage variable for each jerry instance. */
4914-
__thread jerry_instance_t *tls_instance;
4913+
/* A different Thread Local Storage variable for each jerry context. */
4914+
__thread jerry_context_t *tls_context;
49154915

4916-
jerry_instance_t *
4917-
jerry_port_get_current_instance (void)
4916+
jerry_context_t *
4917+
jerry_port_get_current_context (void)
49184918
{
4919-
/* Returns the instance assigned to the thread. */
4920-
return tls_instance;
4919+
/* Returns the context assigned to the thread. */
4920+
return tls_context;
49214921
}
49224922

49234923
/* Allocate JerryScript heap for each thread. */
49244924
static void *
4925-
instance_alloc_fn (size_t size, void *cb_data)
4925+
context_alloc_fn (size_t size, void *cb_data)
49264926
{
49274927
(void) cb_data;
49284928
return malloc (size);
@@ -4931,15 +4931,15 @@ instance_alloc_fn (size_t size, void *cb_data)
49314931
static void *
49324932
thread_function (void *param)
49334933
{
4934-
tls_instance = jerry_create_instance (512 * 1024,
4935-
instance_alloc_fn,
4936-
NULL);
4934+
tls_context = jerry_create_context (512 * 1024,
4935+
context_alloc_fn,
4936+
NULL);
49374937
jerry_init (JERRY_INIT_EMPTY);
4938-
/* Run the JerryScript instance (e.g.: jerry_parse & jerry_run) */
4938+
/* Run JerryScript in the context (e.g.: jerry_parse & jerry_run) */
49394939
jerry_cleanup ();
49404940

4941-
/* Deallocate JerryScript instance */
4942-
free (tls_instance);
4941+
/* Deallocate JerryScript context */
4942+
free (tls_context);
49434943

49444944
return NULL;
49454945
}
@@ -4969,9 +4969,9 @@ main (void)
49694969

49704970
**See also**
49714971

4972-
- [jerry_instance_t](#jerry_instance_t)
4973-
- [jerry_instance_alloc_t](#jerry_instance_alloc_t)
4974-
- [jerry_port_get_current_instance](05.PORT-API.md#jerry_port_get_current_instance)
4972+
- [jerry_context_t](#jerry_context_t)
4973+
- [jerry_context_alloc_t](#jerry_context_alloc_t)
4974+
- [jerry_port_get_current_context](05.PORT-API.md#jerry_port_get_current_context)
49754975

49764976

49774977
# Snapshot functions

docs/05.PORT-API.md

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -112,23 +112,25 @@ bool jerry_port_get_time_zone (jerry_time_zone_t *tz_p);
112112
double jerry_port_get_current_time (void);
113113
```
114114

115-
## External instance
115+
## External context
116116

117-
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.
117+
Allow user to provide external buffer for isolated engine contexts, so that user
118+
can configure the heap size at runtime and run multiple JS applications
119+
simultaneously.
118120

119121
```c
120122
/**
121-
* Get the current instance which contains the current context, heap and other
122-
* structures. Each port should provide its own implementation of this interface.
123+
* Get the current context of the engine. Each port should provide its own
124+
* implementation of this interface.
123125
*
124126
* Note:
125127
* This port function is called by jerry-core when
126128
* JERRY_ENABLE_EXTERNAL_CONTEXT is defined. Otherwise this function is not
127129
* used.
128130
*
129-
* @return the pointer to the jerry instance.
131+
* @return the pointer to the engine context.
130132
*/
131-
struct jerry_instance_t *jerry_port_get_current_instance (void);
133+
struct jerry_context_t *jerry_port_get_current_context (void);
132134
```
133135
134136
## Sleep
@@ -233,37 +235,38 @@ double jerry_port_get_current_time (void)
233235
return ((double) tv.tv_sec) * 1000.0 + ((double) tv.tv_usec) / 1000.0;
234236
} /* jerry_port_get_current_time */
235237
```
236-
## External instance
238+
239+
## External context
237240
238241
```c
239242
#include "jerryscript-port.h"
240243
#include "jerryscript-port-default.h"
241244
242245
/**
243-
* Pointer to the current instance.
246+
* Pointer to the current context.
244247
* Note that it is a global variable, and is not a thread safe implementation.
245248
*/
246-
static jerry_instance_t *current_instance_p = NULL;
249+
static jerry_context_t *current_context_p = NULL;
247250
248251
/**
249-
* Set the current_instance_p as the passed pointer.
252+
* Set the current_context_p as the passed pointer.
250253
*/
251254
void
252-
jerry_port_default_set_instance (jerry_instance_t *instance_p) /**< points to the created instance */
255+
jerry_port_default_set_context (jerry_context_t *context_p) /**< points to the created context */
253256
{
254-
current_instance_p = instance_p;
255-
} /* jerry_port_default_set_instance */
257+
current_context_p = context_p;
258+
} /* jerry_port_default_set_context */
256259
257260
/**
258-
* Get the current instance.
261+
* Get the current context.
259262
*
260-
* @return the pointer to the current instance
263+
* @return the pointer to the current context
261264
*/
262-
jerry_instance_t *
263-
jerry_port_get_current_instance (void)
265+
jerry_context_t *
266+
jerry_port_get_current_context (void)
264267
{
265-
return current_instance_p;
266-
} /* jerry_port_get_current_instance */
268+
return current_context_p;
269+
} /* jerry_port_get_current_context */
267270
```
268271

269272
## Sleep

jerry-core/api/jerry.c

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,9 @@ jerry_init (jerry_init_flag_t flags) /**< combination of Jerry flags */
159159
/* This function cannot be called twice unless jerry_cleanup is called. */
160160
JERRY_ASSERT (!(JERRY_CONTEXT (status_flags) & ECMA_STATUS_API_AVAILABLE));
161161

162-
/* Zero out all members. */
163-
memset (&JERRY_CONTEXT (JERRY_CONTEXT_FIRST_MEMBER), 0, sizeof (jerry_context_t));
162+
/* Zero out all non-external members. */
163+
memset (&JERRY_CONTEXT (JERRY_CONTEXT_FIRST_MEMBER), 0,
164+
sizeof (jerry_context_t) - offsetof (jerry_context_t, JERRY_CONTEXT_FIRST_MEMBER));
164165

165166
JERRY_CONTEXT (jerry_init_flags) = flags;
166167

@@ -2621,20 +2622,20 @@ jerry_is_valid_cesu8_string (const jerry_char_t *cesu8_buf_p, /**< CESU-8 string
26212622
} /* jerry_is_valid_cesu8_string */
26222623

26232624
/**
2624-
* Create a jerry instance for external context.
2625+
* Create an external engine context.
26252626
*
2626-
* @return the pointer to the instance.
2627+
* @return the pointer to the context.
26272628
*/
2628-
jerry_instance_t *
2629-
jerry_create_instance (uint32_t heap_size, /**< the size of heap */
2630-
jerry_instance_alloc_t alloc, /**< the alloc function */
2631-
void *cb_data_p) /**< the cb_data for alloc function */
2629+
jerry_context_t *
2630+
jerry_create_context (uint32_t heap_size, /**< the size of heap */
2631+
jerry_context_alloc_t alloc, /**< the alloc function */
2632+
void *cb_data_p) /**< the cb_data for alloc function */
26322633
{
26332634
JERRY_UNUSED (heap_size);
26342635

26352636
#ifdef JERRY_ENABLE_EXTERNAL_CONTEXT
26362637

2637-
size_t total_size = sizeof (jerry_instance_t) + JMEM_ALIGNMENT;
2638+
size_t total_size = sizeof (jerry_context_t) + JMEM_ALIGNMENT;
26382639

26392640
#ifndef JERRY_SYSTEM_ALLOCATOR
26402641
heap_size = JERRY_ALIGNUP (heap_size, JMEM_ALIGNMENT);
@@ -2650,30 +2651,30 @@ jerry_create_instance (uint32_t heap_size, /**< the size of heap */
26502651

26512652
total_size = JERRY_ALIGNUP (total_size, JMEM_ALIGNMENT);
26522653

2653-
jerry_instance_t *instance_p = (jerry_instance_t *) alloc (total_size, cb_data_p);
2654+
jerry_context_t *context_p = (jerry_context_t *) alloc (total_size, cb_data_p);
26542655

2655-
if (instance_p == NULL)
2656+
if (context_p == NULL)
26562657
{
26572658
return NULL;
26582659
}
26592660

2660-
memset (instance_p, 0, total_size);
2661+
memset (context_p, 0, total_size);
26612662

2662-
uintptr_t instance_ptr = ((uintptr_t) instance_p) + sizeof (jerry_instance_t);
2663-
instance_ptr = JERRY_ALIGNUP (instance_ptr, (uintptr_t) JMEM_ALIGNMENT);
2663+
uintptr_t context_ptr = ((uintptr_t) context_p) + sizeof (jerry_context_t);
2664+
context_ptr = JERRY_ALIGNUP (context_ptr, (uintptr_t) JMEM_ALIGNMENT);
26642665

2665-
uint8_t *byte_p = (uint8_t *) instance_ptr;
2666+
uint8_t *byte_p = (uint8_t *) context_ptr;
26662667

26672668
#ifndef JERRY_SYSTEM_ALLOCATOR
2668-
instance_p->heap_p = (jmem_heap_t *) byte_p;
2669-
instance_p->heap_size = heap_size;
2669+
context_p->heap_p = (jmem_heap_t *) byte_p;
2670+
context_p->heap_size = heap_size;
26702671
byte_p += heap_size;
26712672
#endif /* !JERRY_SYSTEM_ALLOCATOR */
26722673

2673-
JERRY_ASSERT (byte_p <= ((uint8_t *) instance_p) + total_size);
2674+
JERRY_ASSERT (byte_p <= ((uint8_t *) context_p) + total_size);
26742675

26752676
JERRY_UNUSED (byte_p);
2676-
return instance_p;
2677+
return context_p;
26772678

26782679
#else /* !JERRY_ENABLE_EXTERNAL_CONTEXT */
26792680

@@ -2683,7 +2684,7 @@ jerry_create_instance (uint32_t heap_size, /**< the size of heap */
26832684
return NULL;
26842685

26852686
#endif /* JERRY_ENABLE_EXTERNAL_CONTEXT */
2686-
} /* jerry_create_instance */
2687+
} /* jerry_create_context */
26872688

26882689
/**
26892690
* If JERRY_VM_EXEC_STOP is defined the callback passed to this function is

jerry-core/include/jerryscript-core.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -281,9 +281,9 @@ typedef struct
281281
} jerry_context_data_manager_t;
282282

283283
/**
284-
* Function type for allocating buffer for JerryScript instance.
284+
* Function type for allocating buffer for JerryScript context.
285285
*/
286-
typedef void *(*jerry_instance_alloc_t) (size_t size, void *cb_data_p);
286+
typedef void *(*jerry_context_alloc_t) (size_t size, void *cb_data_p);
287287

288288
/**
289289
* Type information of a native pointer.
@@ -294,9 +294,9 @@ typedef struct
294294
} jerry_object_native_info_t;
295295

296296
/**
297-
* An opaque declaration of the JerryScript instance structure.
297+
* An opaque declaration of the JerryScript context structure.
298298
*/
299-
typedef struct jerry_instance_t jerry_instance_t;
299+
typedef struct jerry_context_t jerry_context_t;
300300

301301
/**
302302
* General engine functions.
@@ -517,7 +517,7 @@ bool jerry_is_valid_cesu8_string (const jerry_char_t *cesu8_buf_p, jerry_size_t
517517
/*
518518
* External context functions.
519519
*/
520-
jerry_instance_t *jerry_create_instance (uint32_t heap_size, jerry_instance_alloc_t alloc, void *cb_data_p);
520+
jerry_context_t *jerry_create_context (uint32_t heap_size, jerry_context_alloc_t alloc, void *cb_data_p);
521521

522522
/**
523523
* Miscellaneous functions.

jerry-core/include/jerryscript-port.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,17 +147,17 @@ bool jerry_port_get_time_zone (jerry_time_zone_t *tz_p);
147147
double jerry_port_get_current_time (void);
148148

149149
/**
150-
* Get the current instance which contains the current context, heap and other
151-
* structures. Each port should provide its own implementation of this interface.
150+
* Get the current context of the engine. Each port should provide its own
151+
* implementation of this interface.
152152
*
153153
* Note:
154154
* This port function is called by jerry-core when
155155
* JERRY_ENABLE_EXTERNAL_CONTEXT is defined. Otherwise this function is not
156156
* used.
157157
*
158-
* @return the pointer to the jerry instance.
158+
* @return the pointer to the engine context.
159159
*/
160-
struct jerry_instance_t *jerry_port_get_current_instance (void);
160+
struct jerry_context_t *jerry_port_get_current_context (void);
161161

162162
/**
163163
* Makes the process sleep for a given time.

0 commit comments

Comments
 (0)