Github User Fetcher 1.0.0
C Application with Server and GUI
Loading...
Searching...
No Matches
duktape-1.5.2/src-separate/duk_api_heap.c
Go to the documentation of this file.
1/*
2 * Heap creation and destruction
3 */
4
5#include "duk_internal.h"
6
9 duk_realloc_function realloc_func,
10 duk_free_function free_func,
11 void *heap_udata,
12 duk_fatal_function fatal_handler) {
13 duk_heap *heap = NULL;
14 duk_context *ctx;
15
16 /* Assume that either all memory funcs are NULL or non-NULL, mixed
17 * cases will now be unsafe.
18 */
19
20 /* XXX: just assert non-NULL values here and make caller arguments
21 * do the defaulting to the default implementations (smaller code)?
22 */
23
24 if (!alloc_func) {
25 DUK_ASSERT(realloc_func == NULL);
26 DUK_ASSERT(free_func == NULL);
27#if defined(DUK_USE_PROVIDE_DEFAULT_ALLOC_FUNCTIONS)
28 alloc_func = duk_default_alloc_function;
29 realloc_func = duk_default_realloc_function;
30 free_func = duk_default_free_function;
31#else
32 DUK_D(DUK_DPRINT("no allocation functions given and no default providers"));
33 return NULL;
34#endif
35 } else {
36 DUK_ASSERT(realloc_func != NULL);
37 DUK_ASSERT(free_func != NULL);
38 }
39
40 if (!fatal_handler) {
41 fatal_handler = duk_default_fatal_handler;
42 }
43
44 DUK_ASSERT(alloc_func != NULL);
45 DUK_ASSERT(realloc_func != NULL);
46 DUK_ASSERT(free_func != NULL);
47 DUK_ASSERT(fatal_handler != NULL);
48
49 heap = duk_heap_alloc(alloc_func, realloc_func, free_func, heap_udata, fatal_handler);
50 if (!heap) {
51 return NULL;
52 }
53 ctx = (duk_context *) heap->heap_thread;
54 DUK_ASSERT(ctx != NULL);
55 DUK_ASSERT(((duk_hthread *) ctx)->heap != NULL);
56 return ctx;
57}
58
60 duk_hthread *thr = (duk_hthread *) ctx;
61 duk_heap *heap;
62
63 if (!ctx) {
64 return;
65 }
66 heap = thr->heap;
67 DUK_ASSERT(heap != NULL);
68
69 duk_heap_free(heap);
70}
71
72/* XXX: better place for this */
74 duk_hthread *thr = (duk_hthread *) ctx;
75 duk_hobject *h_glob;
76 duk_hobject *h_prev_glob;
77 duk_hobject *h_env;
78 duk_hobject *h_prev_env;
79
80 DUK_D(DUK_DPRINT("replace global object with: %!T", duk_get_tval(ctx, -1)));
81
82 h_glob = duk_require_hobject(ctx, -1);
83 DUK_ASSERT(h_glob != NULL);
84
85 /*
86 * Replace global object.
87 */
88
89 h_prev_glob = thr->builtins[DUK_BIDX_GLOBAL];
90 DUK_UNREF(h_prev_glob);
91 thr->builtins[DUK_BIDX_GLOBAL] = h_glob;
92 DUK_HOBJECT_INCREF(thr, h_glob);
93 DUK_HOBJECT_DECREF_ALLOWNULL(thr, h_prev_glob); /* side effects, in theory (referenced by global env) */
94
95 /*
96 * Replace lexical environment for global scope
97 *
98 * Create a new object environment for the global lexical scope.
99 * We can't just reset the _Target property of the current one,
100 * because the lexical scope is shared by other threads with the
101 * same (initial) built-ins.
102 */
103
104 (void) duk_push_object_helper(ctx,
107 -1); /* no prototype, updated below */
108
109 duk_dup(ctx, -2);
110 duk_dup(ctx, -3);
111
112 /* [ ... new_glob new_env new_glob new_glob ] */
113
116
117 /* [ ... new_glob new_env ] */
118
119 h_env = duk_get_hobject(ctx, -1);
120 DUK_ASSERT(h_env != NULL);
121
122 h_prev_env = thr->builtins[DUK_BIDX_GLOBAL_ENV];
123 thr->builtins[DUK_BIDX_GLOBAL_ENV] = h_env;
124 DUK_HOBJECT_INCREF(thr, h_env);
125 DUK_HOBJECT_DECREF_ALLOWNULL(thr, h_prev_env); /* side effects */
126 DUK_UNREF(h_env); /* without refcounts */
127 DUK_UNREF(h_prev_env);
128
129 /* [ ... new_glob new_env ] */
130
131 duk_pop_2(ctx);
132
133 /* [ ... ] */
134}
DUK_INTERNAL_DECL void duk_heap_free(duk_heap *heap)
#define DUK_HOBJECT_DECREF_ALLOWNULL(thr, h)
DUK_EXTERNAL void duk_pop_2(duk_context *ctx)
DUK_INTERNAL_DECL void duk_default_free_function(void *udata, void *ptr)
DUK_INTERNAL_DECL duk_hobject * duk_require_hobject(duk_context *ctx, duk_idx_t index)
DUK_INTERNAL_DECL void * duk_default_alloc_function(void *udata, duk_size_t size)
#define DUK_PROPDESC_FLAGS_NONE
#define DUK_HOBJECT_CLASS_AS_FLAGS(v)
#define DUK_HOBJECT_CLASS_OBJENV
DUK_INTERNAL void duk_default_fatal_handler(duk_context *ctx, duk_errcode_t code, const char *msg)
DUK_EXTERNAL void duk_dup(duk_context *ctx, duk_idx_t from_index)
#define DUK_STRIDX_INT_TARGET
#define DUK_HOBJECT_FLAG_EXTENSIBLE
DUK_INTERNAL_DECL duk_idx_t duk_push_object_helper(duk_context *ctx, duk_uint_t hobject_flags_and_class, duk_small_int_t prototype_bidx)
#define DUK_HOBJECT_INCREF(thr, h)
DUK_INTERNAL_DECL duk_hobject * duk_get_hobject(duk_context *ctx, duk_idx_t index)
DUK_INTERNAL_DECL void duk_xdef_prop_stridx(duk_context *ctx, duk_idx_t obj_index, duk_small_int_t stridx, duk_small_uint_t desc_flags)
DUK_INTERNAL_DECL duk_heap * duk_heap_alloc(duk_alloc_function alloc_func, duk_realloc_function realloc_func, duk_free_function free_func, void *heap_udata, duk_fatal_function fatal_func)
DUK_INTERNAL_DECL void * duk_default_realloc_function(void *udata, void *ptr, duk_size_t newsize)
DUK_INTERNAL_DECL duk_tval * duk_get_tval(duk_context *ctx, duk_idx_t index)
void *(* duk_alloc_function)(void *udata, duk_size_t size)
void(* duk_fatal_function)(duk_context *ctx, duk_errcode_t code, const char *msg)
void(* duk_free_function)(void *udata, void *ptr)
void *(* duk_realloc_function)(void *udata, void *ptr, duk_size_t size)
DUK_EXTERNAL void duk_set_global_object(duk_context *ctx)
DUK_EXTERNAL void duk_destroy_heap(duk_context *ctx)
DUK_EXTERNAL duk_context * duk_create_heap(duk_alloc_function alloc_func, duk_realloc_function realloc_func, duk_free_function free_func, void *heap_udata, duk_fatal_function fatal_handler)
#define NULL
Definition gmacros.h:924
duk_hobject * builtins[DUK_NUM_BUILTINS]