-
Notifications
You must be signed in to change notification settings - Fork 663
/
handle-scope.c
357 lines (327 loc) · 10.8 KB
/
handle-scope.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdlib.h>
#include "handle-scope-internal.h"
#include "jext-common.h"
JERRYX_STATIC_ASSERT (JERRYX_SCOPE_PRELIST_SIZE < 32, JERRYX_SCOPE_PRELIST_SIZE_MUST_BE_LESS_THAN_SIZE_OF_UINT8_T);
/**
* Opens a new handle scope and attach it to current global scope as a child scope.
*
* @param result - [out value] opened scope.
* @return status code, jerryx_handle_scope_ok if success.
*/
jerryx_handle_scope_status
jerryx_open_handle_scope (jerryx_handle_scope *result)
{
*result = jerryx_handle_scope_alloc ();
return jerryx_handle_scope_ok;
} /* jerryx_open_handle_scope */
/**
* Release all jerry values attached to given scope
*
* @param scope - the scope of handles to be released.
*/
void
jerryx_handle_scope_release_handles (jerryx_handle_scope scope)
{
size_t prelist_handle_count = scope->prelist_handle_count;
if (prelist_handle_count == JERRYX_HANDLE_PRELIST_SIZE && scope->handle_ptr != NULL)
{
jerryx_handle_t *a_handle = scope->handle_ptr;
while (a_handle != NULL)
{
jerry_value_free (a_handle->jval);
jerryx_handle_t *sibling = a_handle->sibling;
jerry_heap_free (a_handle, sizeof (jerryx_handle_t));
a_handle = sibling;
}
scope->handle_ptr = NULL;
prelist_handle_count = JERRYX_HANDLE_PRELIST_SIZE;
}
for (size_t idx = 0; idx < prelist_handle_count; idx++)
{
jerry_value_free (scope->handle_prelist[idx]);
}
scope->prelist_handle_count = 0;
} /* jerryx_handle_scope_release_handles */
/**
* Close the scope and its child scopes and release all jerry values that
* resides in the scopes.
* Scopes must be closed in the reverse order from which they were created.
*
* @param scope - the scope closed.
* @return status code, jerryx_handle_scope_ok if success.
*/
jerryx_handle_scope_status
jerryx_close_handle_scope (jerryx_handle_scope scope)
{
/**
* Release all handles related to given scope and its child scopes
*/
jerryx_handle_scope a_scope = scope;
do
{
jerryx_handle_scope_release_handles (a_scope);
jerryx_handle_scope child = jerryx_handle_scope_get_child (a_scope);
jerryx_handle_scope_free (a_scope);
a_scope = child;
} while (a_scope != NULL);
return jerryx_handle_scope_ok;
} /* jerryx_close_handle_scope */
/**
* Opens a new handle scope from which one object can be promoted to the outer scope
* and attach it to current global scope as a child scope.
*
* @param result - [out value] opened escapable handle scope.
* @return status code, jerryx_handle_scope_ok if success.
*/
jerryx_handle_scope_status
jerryx_open_escapable_handle_scope (jerryx_handle_scope *result)
{
return jerryx_open_handle_scope (result);
} /* jerryx_open_escapable_handle_scope */
/**
* Close the scope and its child scopes and release all jerry values that
* resides in the scopes.
* Scopes must be closed in the reverse order from which they were created.
*
* @param scope - the one to be closed.
* @return status code, jerryx_handle_scope_ok if success.
*/
jerryx_handle_scope_status
jerryx_close_escapable_handle_scope (jerryx_handle_scope scope)
{
return jerryx_close_handle_scope (scope);
} /* jerryx_close_escapable_handle_scope */
/**
* Internal helper.
* Escape a jerry value from the scope, yet did not promote it to outer scope.
* An assertion of if parent exists shall be made before invoking this function.
*
* @param scope - scope of the handle added to.
* @param idx - expected index of the handle in the scope's prelist.
* @returns escaped jerry value id
*/
jerry_value_t
jerryx_hand_scope_escape_handle_from_prelist (jerryx_handle_scope scope, size_t idx)
{
jerry_value_t jval = scope->handle_prelist[idx];
if (scope->prelist_handle_count == JERRYX_HANDLE_PRELIST_SIZE && scope->handle_ptr != NULL)
{
jerryx_handle_t *handle = scope->handle_ptr;
scope->handle_ptr = handle->sibling;
scope->handle_prelist[idx] = handle->jval;
jerry_heap_free (handle, sizeof (jerryx_handle_t));
return jval;
}
if (idx < JERRYX_HANDLE_PRELIST_SIZE - 1)
{
scope->handle_prelist[idx] = scope->handle_prelist[scope->prelist_handle_count - 1];
}
return jval;
} /* jerryx_hand_scope_escape_handle_from_prelist */
/**
* Internal helper.
* Escape a jerry value from the given escapable handle scope.
*
* @param scope - the expected scope to be escaped from.
* @param escapee - the jerry value to be escaped.
* @param result - [out value] escaped jerry value result.
* @param should_promote - true if the escaped value should be added to parent
* scope after escaped from the given handle scope.
* @return status code, jerryx_handle_scope_ok if success.
*/
jerryx_handle_scope_status
jerryx_escape_handle_internal (jerryx_escapable_handle_scope scope,
jerry_value_t escapee,
jerry_value_t *result,
bool should_promote)
{
if (scope->escaped)
{
return jerryx_escape_called_twice;
}
jerryx_handle_scope parent = jerryx_handle_scope_get_parent (scope);
if (parent == NULL)
{
return jerryx_handle_scope_mismatch;
}
bool found = false;
{
size_t found_idx = 0;
size_t prelist_count = scope->prelist_handle_count;
/**
* Search prelist in a reversed order since last added handle
* is possible the one to be escaped
*/
for (size_t idx_plus_1 = prelist_count; idx_plus_1 > 0; --idx_plus_1)
{
if (escapee == scope->handle_prelist[idx_plus_1 - 1])
{
found = true;
found_idx = idx_plus_1 - 1;
break;
}
}
if (found)
{
*result = jerryx_hand_scope_escape_handle_from_prelist (scope, found_idx);
--scope->prelist_handle_count;
if (should_promote)
{
scope->escaped = true;
/**
* Escape handle to parent scope
*/
jerryx_create_handle_in_scope (*result, jerryx_handle_scope_get_parent (scope));
}
return jerryx_handle_scope_ok;
}
};
if (scope->prelist_handle_count <= JERRYX_HANDLE_PRELIST_SIZE && scope->handle_ptr == NULL)
{
return jerryx_handle_scope_mismatch;
}
/**
* Handle chain list is already in an reversed order,
* search through it as it is
*/
jerryx_handle_t *handle = scope->handle_ptr;
jerryx_handle_t *memo_handle = NULL;
jerryx_handle_t *found_handle = NULL;
while (!found)
{
if (handle == NULL)
{
return jerryx_handle_scope_mismatch;
}
if (handle->jval != escapee)
{
memo_handle = handle;
handle = handle->sibling;
continue;
}
/**
* Remove found handle from current scope's handle chain
*/
found = true;
found_handle = handle;
if (memo_handle == NULL)
{
// found handle is the first handle in heap
scope->handle_ptr = found_handle->sibling;
}
else
{
memo_handle->sibling = found_handle->sibling;
}
}
if (should_promote)
{
/**
* Escape handle to parent scope
*/
*result = jerryx_handle_scope_add_handle_to (found_handle, parent);
scope->escaped = true;
}
return jerryx_handle_scope_ok;
} /* jerryx_escape_handle_internal */
/**
* Promotes the handle to the JavaScript object so that it is valid for the lifetime of
* the outer scope. It can only be called once per scope. If it is called more than
* once an error will be returned.
*
* @param scope - the expected scope to be escaped from.
* @param escapee - the jerry value to be escaped.
* @param result - [out value] escaped jerry value result.
* @return status code, jerryx_handle_scope_ok if success.
*/
jerryx_handle_scope_status
jerryx_escape_handle (jerryx_escapable_handle_scope scope, jerry_value_t escapee, jerry_value_t *result)
{
return jerryx_escape_handle_internal (scope, escapee, result, true);
} /* jerryx_escape_handle */
/**
* Escape a handle from scope yet do not promote it to the outer scope.
* Leave the handle's life time management up to user.
*
* @param scope - the expected scope to be removed from.
* @param escapee - the jerry value to be removed.
* @param result - [out value] removed jerry value result.
* @return status code, jerryx_handle_scope_ok if success.
*/
jerryx_handle_scope_status
jerryx_remove_handle (jerryx_escapable_handle_scope scope, jerry_value_t escapee, jerry_value_t *result)
{
return jerryx_escape_handle_internal (scope, escapee, result, false);
} /* jerryx_remove_handle */
/**
* Try to reuse given handle if possible while adding to the scope.
*
* @param handle - the one to be added to the scope.
* @param scope - the scope of handle to be added to.
* @returns the jerry value id wrapped by given handle.
*/
jerry_value_t
jerryx_handle_scope_add_handle_to (jerryx_handle_t *handle, jerryx_handle_scope scope)
{
size_t prelist_handle_count = scope->prelist_handle_count;
if (prelist_handle_count < JERRYX_HANDLE_PRELIST_SIZE)
{
++scope->prelist_handle_count;
jerry_value_t jval = handle->jval;
jerry_heap_free (handle, sizeof (jerryx_handle_t));
scope->handle_prelist[prelist_handle_count] = jval;
return jval;
}
handle->sibling = scope->handle_ptr;
scope->handle_ptr = handle;
return handle->jval;
} /* jerryx_handle_scope_add_handle_to */
/**
* Add given jerry value to the scope.
*
* @param jval - jerry value to be added to scope.
* @param scope - the scope of the jerry value been expected to be added to.
* @return jerry value that added to scope.
*/
jerry_value_t
jerryx_create_handle_in_scope (jerry_value_t jval, jerryx_handle_scope scope)
{
size_t prelist_handle_count = scope->prelist_handle_count;
if (prelist_handle_count < JERRYX_HANDLE_PRELIST_SIZE)
{
scope->handle_prelist[prelist_handle_count] = jval;
++scope->prelist_handle_count;
return jval;
}
jerryx_handle_t *handle = (jerryx_handle_t *) jerry_heap_alloc (sizeof (jerryx_handle_t));
JERRYX_ASSERT (handle != NULL);
handle->jval = jval;
handle->sibling = scope->handle_ptr;
scope->handle_ptr = handle;
return jval;
} /* jerryx_create_handle_in_scope */
/**
* Add given jerry value to current top scope.
*
* @param jval - jerry value to be added to scope.
* @return jerry value that added to scope.
*/
jerry_value_t
jerryx_create_handle (jerry_value_t jval)
{
return jerryx_create_handle_in_scope (jval, jerryx_handle_scope_get_current ());
} /* jerryx_create_handle */