Github User Fetcher 1.0.0
C Application with Server and GUI
Loading...
Searching...
No Matches
duktape-1.8.0/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
8
14};
15
18 duk_realloc_function realloc_func,
19 duk_free_function free_func,
20 void *heap_udata,
21 duk_fatal_function fatal_handler) {
22 duk_heap *heap = NULL;
23 duk_context *ctx;
24
25 /* Assume that either all memory funcs are NULL or non-NULL, mixed
26 * cases will now be unsafe.
27 */
28
29 /* XXX: just assert non-NULL values here and make caller arguments
30 * do the defaulting to the default implementations (smaller code)?
31 */
32
33 if (!alloc_func) {
34 DUK_ASSERT(realloc_func == NULL);
35 DUK_ASSERT(free_func == NULL);
36#if defined(DUK_USE_PROVIDE_DEFAULT_ALLOC_FUNCTIONS)
37 alloc_func = duk_default_alloc_function;
38 realloc_func = duk_default_realloc_function;
39 free_func = duk_default_free_function;
40#else
41 DUK_D(DUK_DPRINT("no allocation functions given and no default providers"));
42 return NULL;
43#endif
44 } else {
45 DUK_ASSERT(realloc_func != NULL);
46 DUK_ASSERT(free_func != NULL);
47 }
48
49 if (!fatal_handler) {
50 fatal_handler = duk_default_fatal_handler;
51 }
52
53 DUK_ASSERT(alloc_func != NULL);
54 DUK_ASSERT(realloc_func != NULL);
55 DUK_ASSERT(free_func != NULL);
56 DUK_ASSERT(fatal_handler != NULL);
57
58 heap = duk_heap_alloc(alloc_func, realloc_func, free_func, heap_udata, fatal_handler);
59 if (!heap) {
60 return NULL;
61 }
62 ctx = (duk_context *) heap->heap_thread;
63 DUK_ASSERT(ctx != NULL);
64 DUK_ASSERT(((duk_hthread *) ctx)->heap != NULL);
65 return ctx;
66}
67
69 duk_hthread *thr = (duk_hthread *) ctx;
70 duk_heap *heap;
71
72 if (!ctx) {
73 return;
74 }
75 heap = thr->heap;
76 DUK_ASSERT(heap != NULL);
77
78 duk_heap_free(heap);
79}
80
82 duk_hthread *thr = (duk_hthread *) ctx;
83 duk_internal_thread_state *snapshot = (duk_internal_thread_state *) (void *) state;
84 duk_heap *heap;
85 duk_ljstate *lj;
86
88 DUK_ASSERT(thr != NULL);
89 DUK_ASSERT(thr->heap != NULL);
90 DUK_ASSERT(state != NULL); /* unvalidated */
91
92 heap = thr->heap;
93 lj = &heap->lj;
94
95 duk_push_tval(ctx, &lj->value1);
96 duk_push_tval(ctx, &lj->value2);
97
98 DUK_MEMCPY((void *) &snapshot->lj, (const void *) lj, sizeof(duk_ljstate));
99 snapshot->handling_error = heap->handling_error;
100 snapshot->curr_thread = heap->curr_thread;
102
103 lj->jmpbuf_ptr = NULL;
107 heap->handling_error = 0;
108 heap->curr_thread = NULL;
109 heap->call_recursion_depth = 0;
110}
111
113 duk_hthread *thr = (duk_hthread *) ctx;
114 const duk_internal_thread_state *snapshot = (const duk_internal_thread_state *) (const void *) state;
115 duk_heap *heap;
116
118 DUK_ASSERT(thr != NULL);
119 DUK_ASSERT(thr->heap != NULL);
120 DUK_ASSERT(state != NULL); /* unvalidated */
121
122 heap = thr->heap;
123
124 DUK_MEMCPY((void *) &heap->lj, (const void *) &snapshot->lj, sizeof(duk_ljstate));
125 heap->handling_error = snapshot->handling_error;
126 heap->curr_thread = snapshot->curr_thread;
128
129 duk_pop_2(ctx);
130}
131
132/* XXX: better place for this */
134 duk_hthread *thr = (duk_hthread *) ctx;
135 duk_hobject *h_glob;
136 duk_hobject *h_prev_glob;
137 duk_hobject *h_env;
138 duk_hobject *h_prev_env;
139
140 DUK_D(DUK_DPRINT("replace global object with: %!T", duk_get_tval(ctx, -1)));
141
142 h_glob = duk_require_hobject(ctx, -1);
143 DUK_ASSERT(h_glob != NULL);
144
145 /*
146 * Replace global object.
147 */
148
149 h_prev_glob = thr->builtins[DUK_BIDX_GLOBAL];
150 DUK_UNREF(h_prev_glob);
151 thr->builtins[DUK_BIDX_GLOBAL] = h_glob;
152 DUK_HOBJECT_INCREF(thr, h_glob);
153 DUK_HOBJECT_DECREF_ALLOWNULL(thr, h_prev_glob); /* side effects, in theory (referenced by global env) */
154
155 /*
156 * Replace lexical environment for global scope
157 *
158 * Create a new object environment for the global lexical scope.
159 * We can't just reset the _Target property of the current one,
160 * because the lexical scope is shared by other threads with the
161 * same (initial) built-ins.
162 */
163
164 (void) duk_push_object_helper(ctx,
167 -1); /* no prototype, updated below */
168
169 duk_dup(ctx, -2);
170 duk_dup(ctx, -3);
171
172 /* [ ... new_glob new_env new_glob new_glob ] */
173
176
177 /* [ ... new_glob new_env ] */
178
179 h_env = duk_get_hobject(ctx, -1);
180 DUK_ASSERT(h_env != NULL);
181
182 h_prev_env = thr->builtins[DUK_BIDX_GLOBAL_ENV];
183 thr->builtins[DUK_BIDX_GLOBAL_ENV] = h_env;
184 DUK_HOBJECT_INCREF(thr, h_env);
185 DUK_HOBJECT_DECREF_ALLOWNULL(thr, h_prev_env); /* side effects */
186 DUK_UNREF(h_env); /* without refcounts */
187 DUK_UNREF(h_prev_env);
188
189 /* [ ... new_glob new_env ] */
190
191 duk_pop_2(ctx);
192
193 /* [ ... ] */
194}
duk_int_fast32_t duk_int_t
duk_small_int_t duk_bool_t
DUK_INTERNAL_DECL void duk_heap_free(duk_heap *heap)
#define DUK_ASSERT_CTX_VALID(ctx)
#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)
DUK_INTERNAL_DECL void duk_push_tval(duk_context *ctx, duk_tval *tv)
#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)
#define DUK_TVAL_SET_UNDEFINED(tv)
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_resume(duk_context *ctx, const duk_thread_state *state)
DUK_EXTERNAL void duk_suspend(duk_context *ctx, duk_thread_state *state)
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]