Skip to content

Merge instance into context #2501

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 30 additions & 30 deletions docs/02.API-REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,32 +238,32 @@ typedef struct
} jerry_context_data_manager_t;
```

## jerry_instance_alloc_t
## jerry_context_alloc_t

**Summary**

Function type for allocating buffer for JerryScript instance.
Function type for allocating buffer for JerryScript context.

**Prototype**

```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
- `cb_data_p` - pointer to user data


## jerry_instance_t
## jerry_context_t

**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**

```c
typedef struct jerry_instance_t jerry_instance_t;
typedef struct jerry_context_t jerry_context_t;
```

## jerry_property_descriptor_t
Expand Down Expand Up @@ -4877,26 +4877,26 @@ main (void)

# External context functions

## jerry_create_instance
## jerry_create_context

**Summary**

Creates a JerryScript instance for external context.
Create an external JerryScript engine context.

**Prototype**

```c
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);
```

- `heap_size` - requested heap size of the JerryScript instance
- `heap_size` - requested heap size of the JerryScript context
- `alloc` - function for allocation
- `cb_data_p` - user data
- return value
- pointer to the newly created JerryScript instance if success
- pointer to the newly created JerryScript context if success
- NULL otherwise.

**Example**
Expand All @@ -4910,19 +4910,19 @@ jerry_create_instance (uint32_t heap_size,
#include "jerryscript.h"
#include "jerryscript-port.h"

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

jerry_instance_t *
jerry_port_get_current_instance (void)
jerry_context_t *
jerry_port_get_current_context (void)
{
/* Returns the instance assigned to the thread. */
return tls_instance;
/* Returns the context assigned to the thread. */
return tls_context;
}

/* Allocate JerryScript heap for each thread. */
static void *
instance_alloc_fn (size_t size, void *cb_data)
context_alloc_fn (size_t size, void *cb_data)
{
(void) cb_data;
return malloc (size);
Expand All @@ -4931,15 +4931,15 @@ instance_alloc_fn (size_t size, void *cb_data)
static void *
thread_function (void *param)
{
tls_instance = jerry_create_instance (512 * 1024,
instance_alloc_fn,
NULL);
tls_context = jerry_create_context (512 * 1024,
context_alloc_fn,
NULL);
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 ();

/* Deallocate JerryScript instance */
free (tls_instance);
/* Deallocate JerryScript context */
free (tls_context);

return NULL;
}
Expand Down Expand Up @@ -4969,9 +4969,9 @@ main (void)

**See also**

- [jerry_instance_t](#jerry_instance_t)
- [jerry_instance_alloc_t](#jerry_instance_alloc_t)
- [jerry_port_get_current_instance](05.PORT-API.md#jerry_port_get_current_instance)
- [jerry_context_t](#jerry_context_t)
- [jerry_context_alloc_t](#jerry_context_alloc_t)
- [jerry_port_get_current_context](05.PORT-API.md#jerry_port_get_current_context)


# Snapshot functions
Expand Down
41 changes: 22 additions & 19 deletions docs/05.PORT-API.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,23 +112,25 @@ bool jerry_port_get_time_zone (jerry_time_zone_t *tz_p);
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
/**
* 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);
```

## Sleep
Expand Down Expand Up @@ -233,37 +235,38 @@ double jerry_port_get_current_time (void)
return ((double) tv.tv_sec) * 1000.0 + ((double) tv.tv_usec) / 1000.0;
} /* jerry_port_get_current_time */
```
## External instance

## External context

```c
#include "jerryscript-port.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.
*/
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
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;
} /* jerry_port_default_set_instance */
current_context_p = context_p;
} /* 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_port_get_current_instance (void)
jerry_context_t *
jerry_port_get_current_context (void)
{
return current_instance_p;
} /* jerry_port_get_current_instance */
return current_context_p;
} /* jerry_port_get_current_context */
```

## Sleep
Expand Down
41 changes: 21 additions & 20 deletions jerry-core/api/jerry.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand All @@ -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 */

Expand All @@ -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
Expand Down
10 changes: 5 additions & 5 deletions jerry-core/include/jerryscript-core.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions jerry-core/include/jerryscript-port.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading