Browse Source

Merge pull request #893 from oleavr/api-suspend-resume

Add suspend and resume API
pull/910/head
Sami Vaarala 8 years ago
committed by GitHub
parent
commit
ec11270134
  1. 2
      AUTHORS.rst
  2. 58
      src/duk_api_heap.c
  3. 9
      src/duk_api_public.h.in

2
AUTHORS.rst

@ -34,6 +34,7 @@ and agreed to irrevocably license their contributions under the Duktape
* Ondřej Jirman (https://github.com/megous) * Ondřej Jirman (https://github.com/megous)
* Saúl Ibarra Corretgé <saghul@gmail.com> * Saúl Ibarra Corretgé <saghul@gmail.com>
* Jeremy HU <huxingyi@msn.com> * Jeremy HU <huxingyi@msn.com>
* Ole André Vadla Ravnås (https://github.com/oleavr)
Other contributions Other contributions
=================== ===================
@ -71,7 +72,6 @@ bugs, provided ideas, etc; roughly in order of appearance):
* Michael Drake (https://github.com/tlsa) * Michael Drake (https://github.com/tlsa)
* https://github.com/chris-y * https://github.com/chris-y
* Laurent Zubiaur (https://github.com/lzubiaur) * Laurent Zubiaur (https://github.com/lzubiaur)
* Ole André Vadla Ravnås (https://github.com/oleavr)
If you are accidentally missing from this list, send me an e-mail If you are accidentally missing from this list, send me an e-mail
(``sami.vaarala@iki.fi``) and I'll fix the omission. (``sami.vaarala@iki.fi``) and I'll fix the omission.

58
src/duk_api_heap.c

@ -4,6 +4,15 @@
#include "duk_internal.h" #include "duk_internal.h"
typedef struct duk_internal_thread_state duk_internal_thread_state;
struct duk_internal_thread_state {
duk_ljstate lj;
duk_bool_t handling_error;
duk_hthread *curr_thread;
duk_int_t call_recursion_depth;
};
DUK_EXTERNAL DUK_EXTERNAL
duk_context *duk_create_heap(duk_alloc_function alloc_func, duk_context *duk_create_heap(duk_alloc_function alloc_func,
duk_realloc_function realloc_func, duk_realloc_function realloc_func,
@ -69,6 +78,55 @@ DUK_EXTERNAL void duk_destroy_heap(duk_context *ctx) {
duk_heap_free(heap); duk_heap_free(heap);
} }
DUK_EXTERNAL void duk_suspend(duk_context *ctx, duk_thread_state *state) {
duk_hthread *thr = (duk_hthread *) ctx;
duk_internal_thread_state *snapshot = (duk_internal_thread_state *) state;
duk_heap *heap;
duk_ljstate *lj;
DUK_ASSERT_CTX_VALID(ctx);
DUK_ASSERT(thr != NULL);
DUK_ASSERT(thr->heap != NULL);
heap = thr->heap;
lj = &heap->lj;
duk_push_tval(ctx, &lj->value1);
duk_push_tval(ctx, &lj->value2);
DUK_MEMMOVE((void *) &snapshot->lj, (const void *) lj, sizeof(duk_ljstate));
snapshot->handling_error = heap->handling_error;
snapshot->curr_thread = heap->curr_thread;
snapshot->call_recursion_depth = heap->call_recursion_depth;
lj->jmpbuf_ptr = NULL;
lj->type = DUK_LJ_TYPE_UNKNOWN;
DUK_TVAL_SET_UNDEFINED(&lj->value1);
DUK_TVAL_SET_UNDEFINED(&lj->value2);
heap->handling_error = 0;
heap->curr_thread = NULL;
heap->call_recursion_depth = 0;
}
DUK_EXTERNAL void duk_resume(duk_context *ctx, const duk_thread_state *state) {
duk_hthread *thr = (duk_hthread *) ctx;
const duk_internal_thread_state *snapshot = (const duk_internal_thread_state *) state;
duk_heap *heap;
DUK_ASSERT_CTX_VALID(ctx);
DUK_ASSERT(thr != NULL);
DUK_ASSERT(thr->heap != NULL);
heap = thr->heap;
DUK_MEMMOVE((void *) &heap->lj, (const void *) &snapshot->lj, sizeof(duk_ljstate));
heap->handling_error = snapshot->handling_error;
heap->curr_thread = snapshot->curr_thread;
heap->call_recursion_depth = snapshot->call_recursion_depth;
duk_pop_2(ctx);
}
/* XXX: better place for this */ /* XXX: better place for this */
DUK_EXTERNAL void duk_set_global_object(duk_context *ctx) { DUK_EXTERNAL void duk_set_global_object(duk_context *ctx) {
duk_hthread *thr = (duk_hthread *) ctx; duk_hthread *thr = (duk_hthread *) ctx;

9
src/duk_api_public.h.in

@ -32,6 +32,7 @@ extern "C" {
* in Duktape web documentation. * in Duktape web documentation.
*/ */
struct duk_thread_state;
struct duk_memory_functions; struct duk_memory_functions;
struct duk_function_list_entry; struct duk_function_list_entry;
struct duk_number_list_entry; struct duk_number_list_entry;
@ -40,6 +41,7 @@ struct duk_time_components;
/* duk_context is now defined in duk_config.h because it may also be /* duk_context is now defined in duk_config.h because it may also be
* referenced there by prototypes. * referenced there by prototypes.
*/ */
typedef struct duk_thread_state duk_thread_state;
typedef struct duk_memory_functions duk_memory_functions; typedef struct duk_memory_functions duk_memory_functions;
typedef struct duk_function_list_entry duk_function_list_entry; typedef struct duk_function_list_entry duk_function_list_entry;
typedef struct duk_number_list_entry duk_number_list_entry; typedef struct duk_number_list_entry duk_number_list_entry;
@ -61,6 +63,10 @@ typedef void (*duk_debug_write_flush_function) (void *udata);
typedef duk_idx_t (*duk_debug_request_function) (duk_context *ctx, void *udata, duk_idx_t nvalues); typedef duk_idx_t (*duk_debug_request_function) (duk_context *ctx, void *udata, duk_idx_t nvalues);
typedef void (*duk_debug_detached_function) (duk_context *ctx, void *udata); typedef void (*duk_debug_detached_function) (duk_context *ctx, void *udata);
struct duk_thread_state {
char data[128];
};
struct duk_memory_functions { struct duk_memory_functions {
duk_alloc_function alloc_func; duk_alloc_function alloc_func;
duk_realloc_function realloc_func; duk_realloc_function realloc_func;
@ -256,6 +262,9 @@ duk_context *duk_create_heap(duk_alloc_function alloc_func,
duk_fatal_function fatal_handler); duk_fatal_function fatal_handler);
DUK_EXTERNAL_DECL void duk_destroy_heap(duk_context *ctx); DUK_EXTERNAL_DECL void duk_destroy_heap(duk_context *ctx);
DUK_EXTERNAL_DECL void duk_suspend(duk_context *ctx, duk_thread_state *state);
DUK_EXTERNAL_DECL void duk_resume(duk_context *ctx, const duk_thread_state *state);
#define duk_create_heap_default() \ #define duk_create_heap_default() \
duk_create_heap(NULL, NULL, NULL, NULL, NULL) duk_create_heap(NULL, NULL, NULL, NULL, NULL)

Loading…
Cancel
Save