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 akiss@inf.u-szeged.hu
This commit is contained in:
Akos Kiss
2018-09-04 13:56:49 +02:00
committed by GitHub
parent d3d42f7685
commit 30b7a72344
9 changed files with 145 additions and 140 deletions
+21 -20
View File
@@ -159,8 +159,9 @@ jerry_init (jerry_init_flag_t flags) /**< combination of Jerry flags */
/* This function cannot be called twice unless jerry_cleanup is called. */
JERRY_ASSERT (!(JERRY_CONTEXT (status_flags) & ECMA_STATUS_API_AVAILABLE));
/* Zero out all members. */
memset (&JERRY_CONTEXT (JERRY_CONTEXT_FIRST_MEMBER), 0, sizeof (jerry_context_t));
/* Zero out all non-external members. */
memset (&JERRY_CONTEXT (JERRY_CONTEXT_FIRST_MEMBER), 0,
sizeof (jerry_context_t) - offsetof (jerry_context_t, JERRY_CONTEXT_FIRST_MEMBER));
JERRY_CONTEXT (jerry_init_flags) = flags;
@@ -2621,20 +2622,20 @@ jerry_is_valid_cesu8_string (const jerry_char_t *cesu8_buf_p, /**< CESU-8 string
} /* jerry_is_valid_cesu8_string */
/**
* Create a jerry instance for external context.
* Create an external engine context.
*
* @return the pointer to the instance.
* @return the pointer to the context.
*/
jerry_instance_t *
jerry_create_instance (uint32_t heap_size, /**< the size of heap */
jerry_instance_alloc_t alloc, /**< the alloc function */
void *cb_data_p) /**< the cb_data for alloc function */
jerry_context_t *
jerry_create_context (uint32_t heap_size, /**< the size of heap */
jerry_context_alloc_t alloc, /**< the alloc function */
void *cb_data_p) /**< the cb_data for alloc function */
{
JERRY_UNUSED (heap_size);
#ifdef JERRY_ENABLE_EXTERNAL_CONTEXT
size_t total_size = sizeof (jerry_instance_t) + JMEM_ALIGNMENT;
size_t total_size = sizeof (jerry_context_t) + JMEM_ALIGNMENT;
#ifndef JERRY_SYSTEM_ALLOCATOR
heap_size = JERRY_ALIGNUP (heap_size, JMEM_ALIGNMENT);
@@ -2650,30 +2651,30 @@ jerry_create_instance (uint32_t heap_size, /**< the size of heap */
total_size = JERRY_ALIGNUP (total_size, JMEM_ALIGNMENT);
jerry_instance_t *instance_p = (jerry_instance_t *) alloc (total_size, cb_data_p);
jerry_context_t *context_p = (jerry_context_t *) alloc (total_size, cb_data_p);
if (instance_p == NULL)
if (context_p == NULL)
{
return NULL;
}
memset (instance_p, 0, total_size);
memset (context_p, 0, total_size);
uintptr_t instance_ptr = ((uintptr_t) instance_p) + sizeof (jerry_instance_t);
instance_ptr = JERRY_ALIGNUP (instance_ptr, (uintptr_t) JMEM_ALIGNMENT);
uintptr_t context_ptr = ((uintptr_t) context_p) + sizeof (jerry_context_t);
context_ptr = JERRY_ALIGNUP (context_ptr, (uintptr_t) JMEM_ALIGNMENT);
uint8_t *byte_p = (uint8_t *) instance_ptr;
uint8_t *byte_p = (uint8_t *) context_ptr;
#ifndef JERRY_SYSTEM_ALLOCATOR
instance_p->heap_p = (jmem_heap_t *) byte_p;
instance_p->heap_size = heap_size;
context_p->heap_p = (jmem_heap_t *) byte_p;
context_p->heap_size = heap_size;
byte_p += heap_size;
#endif /* !JERRY_SYSTEM_ALLOCATOR */
JERRY_ASSERT (byte_p <= ((uint8_t *) instance_p) + total_size);
JERRY_ASSERT (byte_p <= ((uint8_t *) context_p) + total_size);
JERRY_UNUSED (byte_p);
return instance_p;
return context_p;
#else /* !JERRY_ENABLE_EXTERNAL_CONTEXT */
@@ -2683,7 +2684,7 @@ jerry_create_instance (uint32_t heap_size, /**< the size of heap */
return NULL;
#endif /* JERRY_ENABLE_EXTERNAL_CONTEXT */
} /* jerry_create_instance */
} /* jerry_create_context */
/**
* If JERRY_VM_EXEC_STOP is defined the callback passed to this function is
+5 -5
View File
@@ -281,9 +281,9 @@ typedef struct
} jerry_context_data_manager_t;
/**
* Function type for allocating buffer for JerryScript instance.
* Function type for allocating buffer for JerryScript context.
*/
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);
/**
* Type information of a native pointer.
@@ -294,9 +294,9 @@ typedef struct
} jerry_object_native_info_t;
/**
* An opaque declaration of the JerryScript instance structure.
* An opaque declaration of the JerryScript context structure.
*/
typedef struct jerry_instance_t jerry_instance_t;
typedef struct jerry_context_t jerry_context_t;
/**
* General engine functions.
@@ -517,7 +517,7 @@ bool jerry_is_valid_cesu8_string (const jerry_char_t *cesu8_buf_p, jerry_size_t
/*
* External context functions.
*/
jerry_instance_t *jerry_create_instance (uint32_t heap_size, jerry_instance_alloc_t alloc, void *cb_data_p);
jerry_context_t *jerry_create_context (uint32_t heap_size, jerry_context_alloc_t alloc, void *cb_data_p);
/**
* Miscellaneous functions.
+4 -4
View File
@@ -147,17 +147,17 @@ bool jerry_port_get_time_zone (jerry_time_zone_t *tz_p);
double jerry_port_get_current_time (void);
/**
* Get the current instance which contains the current context, heap and other
* structures. Each port should provide its own implementation of this interface.
* Get the current context of the engine. Each port should provide its own
* implementation of this interface.
*
* Note:
* This port function is called by jerry-core when
* JERRY_ENABLE_EXTERNAL_CONTEXT is defined. Otherwise this function is not
* 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);
/**
* Makes the process sleep for a given time.
+43 -42
View File
@@ -14,7 +14,7 @@
*/
/*
* Memory context for JerryScript
* Engine context for JerryScript
*/
#ifndef JCONTEXT_H
#define JCONTEXT_H
@@ -33,6 +33,22 @@
* @{
*/
#ifndef JERRY_SYSTEM_ALLOCATOR
/**
* Heap structure
*
* Memory blocks returned by the allocator must not start from the
* beginning of the heap area because offset 0 is reserved for
* JMEM_CP_NULL. This special constant is used in several places,
* e.g. it marks the end of the property chain list, so it cannot
* be eliminated from the project. Although the allocator cannot
* use the first 8 bytes of the heap, nothing prevents to use it
* for other purposes. Currently the free region start is stored
* there.
*/
typedef struct jmem_heap_t jmem_heap_t;
#endif /* !JERRY_SYSTEM_ALLOCATOR */
/**
* User context item
*/
@@ -46,7 +62,7 @@ typedef struct jerry_context_data_header
((uint8_t *) (item_p + 1))
/**
* First member of the jerry context
* First non-external member of the jerry context
*/
#define JERRY_CONTEXT_FIRST_MEMBER ecma_builtin_objects
@@ -56,9 +72,17 @@ typedef struct jerry_context_data_header
* The purpose of this header is storing
* all global variables for Jerry
*/
typedef struct
struct jerry_context_t
{
/* Update JERRY_CONTEXT_FIRST_MEMBER if the first member changes */
/* The value of external context members must be preserved across initializations and cleanups. */
#ifdef JERRY_ENABLE_EXTERNAL_CONTEXT
#ifndef JERRY_SYSTEM_ALLOCATOR
jmem_heap_t *heap_p; /**< point to the heap aligned to JMEM_ALIGNMENT. */
uint32_t heap_size; /**< size of the heap */
#endif /* !JERRY_SYSTEM_ALLOCATOR */
#endif /* JERRY_ENABLE_EXTERNAL_CONTEXT */
/* Update JERRY_CONTEXT_FIRST_MEMBER if the first non-external member changes */
ecma_object_t *ecma_builtin_objects[ECMA_BUILTIN_ID__COUNT]; /**< pointer to instances of built-in objects */
#ifndef CONFIG_DISABLE_REGEXP_BUILTIN
const re_compiled_code_t *re_cache[RE_CACHE_SIZE]; /**< regex cache */
@@ -137,7 +161,8 @@ typedef struct
/** hash table for caching the last access of properties */
ecma_lcache_hash_entry_t lcache[ECMA_LCACHE_HASH_ROWS_COUNT][ECMA_LCACHE_HASH_ROW_LENGTH];
#endif /* !CONFIG_ECMA_LCACHE_DISABLE */
} jerry_context_t;
};
#ifdef JERRY_ENABLE_EXTERNAL_CONTEXT
@@ -145,43 +170,31 @@ typedef struct
* This part is for JerryScript which uses external context.
*/
#ifndef JERRY_GET_CURRENT_INSTANCE
#ifndef JERRY_GET_CURRENT_CONTEXT
/**
* Default function if JERRY_GET_CURRENT_INSTANCE is not defined.
* Default function if JERRY_GET_CURRENT_CONTEXT is not defined.
*/
#define JERRY_GET_CURRENT_INSTANCE() (jerry_port_get_current_instance ())
#endif /* !JERRY_GET_CURRENT_INSTANCE */
#define JERRY_GET_CURRENT_CONTEXT() (jerry_port_get_current_context ())
#endif /* !JERRY_GET_CURRENT_CONTEXT */
#define JERRY_CONTEXT(field) (JERRY_GET_CURRENT_INSTANCE ()->context.field)
#define JERRY_CONTEXT(field) (JERRY_GET_CURRENT_CONTEXT ()->field)
#ifndef JERRY_SYSTEM_ALLOCATOR
#define JMEM_HEAP_SIZE (JERRY_GET_CURRENT_INSTANCE ()->heap_size)
#define JMEM_HEAP_SIZE (JERRY_CONTEXT (heap_size))
#define JMEM_HEAP_AREA_SIZE (JMEM_HEAP_SIZE - JMEM_ALIGNMENT)
typedef struct
struct jmem_heap_t
{
jmem_heap_free_t first; /**< first node in free region list */
uint8_t area[]; /**< heap area */
} jmem_heap_t;
#define JERRY_HEAP_CONTEXT(field) (JERRY_GET_CURRENT_INSTANCE ()->heap_p->field)
#endif /* !JERRY_SYSTEM_ALLOCATOR */
/**
* Description of jerry instance which is the header of the context space.
*/
struct jerry_instance_t
{
jerry_context_t context; /**< the context of the instance */
#ifndef JERRY_SYSTEM_ALLOCATOR
jmem_heap_t *heap_p; /**< point to the heap aligned to JMEM_ALIGNMENT. */
uint32_t heap_size; /**< size of the heap */
#endif /* !JERRY_SYSTEM_ALLOCATOR */
};
#define JERRY_HEAP_CONTEXT(field) (JERRY_CONTEXT (heap_p)->field)
#endif /* !JERRY_SYSTEM_ALLOCATOR */
#else /* !JERRY_ENABLE_EXTERNAL_CONTEXT */
/*
@@ -210,23 +223,11 @@ extern jerry_context_t jerry_global_context;
*/
#define JMEM_HEAP_AREA_SIZE (JMEM_HEAP_SIZE - JMEM_ALIGNMENT)
/**
* Heap structure
*
* Memory blocks returned by the allocator must not start from the
* beginning of the heap area because offset 0 is reserved for
* JMEM_CP_NULL. This special constant is used in several places,
* e.g. it marks the end of the property chain list, so it cannot
* be eliminated from the project. Although the allocator cannot
* use the first 8 bytes of the heap, nothing prevents to use it
* for other purposes. Currently the free region start is stored
* there.
*/
typedef struct
struct jmem_heap_t
{
jmem_heap_free_t first; /**< first node in free region list */
uint8_t area[JMEM_HEAP_AREA_SIZE]; /**< heap area */
} jmem_heap_t;
};
/**
* Global heap.