From 8054b261947879fed8cf390f6ecb2f612c16120f Mon Sep 17 00:00:00 2001 From: Sami Vaarala Date: Wed, 10 Jul 2013 20:25:09 +0300 Subject: [PATCH] add fixed buffer check api; rework heap creation api --- src/duk_api.c | 76 +++++++++++++++++++++++++++++++++++++++++++ src/duk_api.h | 3 ++ src/duk_api_compile.c | 4 ++- src/duk_heap.h | 1 - src/duk_heap_alloc.c | 9 ----- 5 files changed, 82 insertions(+), 11 deletions(-) diff --git a/src/duk_api.c b/src/duk_api.c index 38506632..8dcc5242 100644 --- a/src/duk_api.c +++ b/src/duk_api.c @@ -1487,6 +1487,20 @@ int duk_is_dynamic(duk_context *ctx, int index) { return 0; } +int duk_is_fixed(duk_context *ctx, int index) { + duk_tval *tv; + + DUK_ASSERT(ctx != NULL); + + tv = duk_get_tval(ctx, index); + if (DUK_TVAL_IS_BUFFER(tv)) { + duk_hbuffer *h = DUK_TVAL_GET_BUFFER(tv); + DUK_ASSERT(h != NULL); + return (DUK_HBUFFER_HAS_DYNAMIC(h) ? 0 : 1); + } + return 0; +} + int duk_is_primitive(duk_context *ctx, int index) { return !duk_is_object(ctx, index); } @@ -2388,4 +2402,66 @@ int duk_strict_equals(duk_context *ctx, int index1, int index2) { return duk_js_strict_equals(tv1, tv2); } +/* + * Heap creation + */ + +duk_context *duk_create_heap(duk_alloc_function alloc_func, + duk_realloc_function realloc_func, + duk_free_function free_func, + void *alloc_udata, + duk_fatal_function fatal_handler) { + duk_heap *heap = NULL; + duk_context *ctx; + + /* Assume that either all memory funcs are NULL or non-NULL, mixed + * cases will now be unsafe. + */ + + /* FIXME: just assert non-NULL values here and make caller arguments + * do the defaulting to the default implementations (smaller code)? + */ + + if (!alloc_func) { + DUK_ASSERT(realloc_func == NULL); + DUK_ASSERT(free_func == NULL); + alloc_func = duk_default_alloc_function; + realloc_func = duk_default_realloc_function; + free_func = duk_default_free_function; + } else { + DUK_ASSERT(realloc_func != NULL); + DUK_ASSERT(free_func != NULL); + } + + if (!fatal_handler) { + fatal_handler = duk_default_fatal_handler; + } + + DUK_ASSERT(alloc_func != NULL); + DUK_ASSERT(realloc_func != NULL); + DUK_ASSERT(free_func != NULL); + DUK_ASSERT(fatal_handler != NULL); + + heap = duk_heap_alloc(alloc_func, realloc_func, free_func, alloc_udata, fatal_handler); + if (!heap) { + return NULL; + } + ctx = (duk_context *) heap->heap_thread; + DUK_ASSERT(ctx != NULL); + DUK_ASSERT(((duk_hthread *) ctx)->heap != NULL); + return ctx; +} + +void duk_destroy_heap(duk_context *ctx) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_heap *heap; + + if (!ctx) { + return; + } + heap = thr->heap; + DUK_ASSERT(heap != NULL); + + duk_heap_free(heap); +} diff --git a/src/duk_api.h b/src/duk_api.h index f59bb996..33aa6df9 100644 --- a/src/duk_api.h +++ b/src/duk_api.h @@ -164,6 +164,8 @@ duk_context *duk_create_heap(duk_alloc_function alloc_func, duk_fatal_function fatal_handler); void duk_destroy_heap(duk_context *ctx); +#define duk_create_heap_default() (duk_create_heap(NULL, NULL, NULL, NULL, NULL)) + /* * Memory management * @@ -320,6 +322,7 @@ int duk_is_thread(duk_context *ctx, int index); /* implies: duk int duk_is_callable(duk_context *ctx, int index); /* currently same as duk_is_function() */ int duk_is_dynamic(duk_context *ctx, int index); /* for buffer */ +int duk_is_fixed(duk_context *ctx, int index); /* for buffer */ int duk_is_primitive(duk_context *ctx, int index); /* E5 Section 9.1; anything but object is primitive */ int duk_is_object_coercible(duk_context *ctx, int index); /* E5 Section 9.10 */ diff --git a/src/duk_api_compile.c b/src/duk_api_compile.c index 4bfb1557..f9bc40b0 100644 --- a/src/duk_api_compile.c +++ b/src/duk_api_compile.c @@ -4,6 +4,8 @@ #include "duk_internal.h" +/* FIXME: placeholder */ + void duk_eval(duk_context *ctx, int flags) { duk_hthread *thr = (duk_hthread *) ctx; int comp_flags; @@ -26,7 +28,7 @@ void duk_eval(duk_context *ctx, int flags) { /* [ ... func_template closure ] */ duk_remove(ctx, -2); /* -> [ ... closure ] */ - duk_call(ctx, 0); /* -> [ ... result ] */ + duk_call(ctx, 0); /* -> [ ... result ] */ /* [ ... result ] */ } diff --git a/src/duk_heap.h b/src/duk_heap.h index 1ab7c11c..cefca669 100644 --- a/src/duk_heap.h +++ b/src/duk_heap.h @@ -345,7 +345,6 @@ duk_heap *duk_heap_alloc(duk_alloc_function alloc_func, duk_free_function free_func, void *alloc_udata, duk_fatal_function fatal_func); -duk_heap *duk_heap_alloc_default(void); void duk_heap_free(duk_heap *heap); void duk_heap_free_heaphdr_raw(duk_heap *heap, duk_heaphdr *hdr); diff --git a/src/duk_heap_alloc.c b/src/duk_heap_alloc.c index 44e2dce7..adcbe1c2 100644 --- a/src/duk_heap_alloc.c +++ b/src/duk_heap_alloc.c @@ -458,12 +458,3 @@ duk_heap *duk_heap_alloc(duk_alloc_function alloc_func, return NULL; } -duk_heap *duk_heap_alloc_default(void) { - return duk_heap_alloc(duk_default_alloc_function, - duk_default_realloc_function, - duk_default_free_function, - NULL, - duk_default_fatal_handler); -} - -