Github User Fetcher 1.0.0
C Application with Server and GUI
Loading...
Searching...
No Matches
duktape-1.5.2/src-separate/duk_api_stack.c
Go to the documentation of this file.
1/*
2 * API calls related to general value stack manipulation: resizing the value
3 * stack, pushing and popping values, type checking and reading values,
4 * coercing values, etc.
5 *
6 * Also contains internal functions (such as duk_get_tval()), defined
7 * in duk_api_internal.h, with semantics similar to the public API.
8 */
9
10/* XXX: repetition of stack pre-checks -> helper or macro or inline */
11/* XXX: shared api error strings, and perhaps even throw code for rare cases? */
12
13#include "duk_internal.h"
14
15/*
16 * Forward declarations
17 */
18
20
21/*
22 * Global state for working around missing variadic macros
23 */
24
25#ifndef DUK_USE_VARIADIC_MACROS
28#endif
29
30/*
31 * Misc helpers
32 */
33
34/* Check that there's room to push one value. */
35#if defined(DUK_USE_VALSTACK_UNSAFE)
36/* Faster but value stack overruns are memory unsafe. */
37#define DUK__CHECK_SPACE() do { \
38 DUK_ASSERT(!(thr->valstack_top >= thr->valstack_end)); \
39 } while (0)
40#else
41#define DUK__CHECK_SPACE() do { \
42 if (DUK_UNLIKELY(thr->valstack_top >= thr->valstack_end)) { \
43 DUK_ERROR_API(thr, DUK_STR_PUSH_BEYOND_ALLOC_STACK); \
44 } \
45 } while (0)
46#endif
47
49
51 duk_hthread *thr;
52 duk_tval *tv;
55
56 thr = (duk_hthread *) ctx;
57
58 tv = duk_get_tval(ctx, index);
59 if (tv == NULL) {
60 goto error_notnumber;
61 }
62
63 /*
64 * Special cases like NaN and +/- Infinity are handled explicitly
65 * because a plain C coercion from double to int handles these cases
66 * in undesirable ways. For instance, NaN may coerce to INT_MIN
67 * (not zero), and INT_MAX + 1 may coerce to INT_MIN (not INT_MAX).
68 *
69 * This double-to-int coercion differs from ToInteger() because it
70 * has a finite range (ToInteger() allows e.g. +/- Infinity). It
71 * also differs from ToInt32() because the INT_MIN/INT_MAX clamping
72 * depends on the size of the int type on the platform. In particular,
73 * on platforms with a 64-bit int type, the full range is allowed.
74 */
75
76#if defined(DUK_USE_FASTINT)
77 if (DUK_TVAL_IS_FASTINT(tv)) {
78 duk_int64_t t = DUK_TVAL_GET_FASTINT(tv);
79#if (DUK_INT_MAX <= 0x7fffffffL)
80 /* Clamping only necessary for 32-bit ints. */
81 if (t < DUK_INT_MIN) {
82 t = DUK_INT_MIN;
83 } else if (t > DUK_INT_MAX) {
84 t = DUK_INT_MAX;
85 }
86#endif
87 return (duk_int_t) t;
88 }
89#endif
90
91 if (DUK_TVAL_IS_NUMBER(tv)) {
92 d = DUK_TVAL_GET_NUMBER(tv);
94 if (c == DUK_FP_NAN) {
95 return 0;
96 } else if (d < (duk_double_t) DUK_INT_MIN) {
97 /* covers -Infinity */
98 return DUK_INT_MIN;
99 } else if (d > (duk_double_t) DUK_INT_MAX) {
100 /* covers +Infinity */
101 return DUK_INT_MAX;
102 } else {
103 /* coerce towards zero */
104 return (duk_int_t) d;
105 }
106 }
107
108 error_notnumber:
109
110 if (require) {
112 /* not reachable */
113 }
114 return 0;
115}
116
118 duk_hthread *thr;
119 duk_tval *tv;
121 duk_double_t d;
122
123 /* Same as above but for unsigned int range. */
124
125 thr = (duk_hthread *) ctx;
126
127 tv = duk_get_tval(ctx, index);
128 if (tv == NULL) {
129 goto error_notnumber;
130 }
131
132#if defined(DUK_USE_FASTINT)
133 if (DUK_TVAL_IS_FASTINT(tv)) {
134 duk_int64_t t = DUK_TVAL_GET_FASTINT(tv);
135 if (t < 0) {
136 t = 0;
137 }
138#if (DUK_UINT_MAX <= 0xffffffffUL)
139 /* Clamping only necessary for 32-bit ints. */
140 else if (t > DUK_UINT_MAX) {
141 t = DUK_UINT_MAX;
142 }
143#endif
144 return (duk_uint_t) t;
145 }
146#endif
147
148 if (DUK_TVAL_IS_NUMBER(tv)) {
149 d = DUK_TVAL_GET_NUMBER(tv);
151 if (c == DUK_FP_NAN) {
152 return 0;
153 } else if (d < 0.0) {
154 /* covers -Infinity */
155 return (duk_uint_t) 0;
156 } else if (d > (duk_double_t) DUK_UINT_MAX) {
157 /* covers +Infinity */
158 return (duk_uint_t) DUK_UINT_MAX;
159 } else {
160 /* coerce towards zero */
161 return (duk_uint_t) d;
162 }
163 }
164
165 error_notnumber:
166
167 if (require) {
169 /* not reachable */
170 }
171 return 0;
172}
173
174/*
175 * Stack index validation/normalization and getting a stack duk_tval ptr.
176 *
177 * These are called by many API entrypoints so the implementations must be
178 * fast and "inlined".
179 *
180 * There's some repetition because of this; keep the functions in sync.
181 */
182
184 duk_hthread *thr = (duk_hthread *) ctx;
185 duk_uidx_t vs_size;
186 duk_uidx_t uindex;
187
190
191 /* Care must be taken to avoid pointer wrapping in the index
192 * validation. For instance, on a 32-bit platform with 8-byte
193 * duk_tval the index 0x20000000UL would wrap the memory space
194 * once.
195 */
196
197 /* Assume value stack sizes (in elements) fits into duk_idx_t. */
199 vs_size = (duk_uidx_t) (thr->valstack_top - thr->valstack_bottom);
200 DUK_ASSERT_DISABLE(vs_size >= 0); /* unsigned */
201
202 if (index < 0) {
203 uindex = vs_size + (duk_uidx_t) index;
204 } else {
205 /* since index non-negative */
207 uindex = (duk_uidx_t) index;
208 }
209
210 /* DUK_INVALID_INDEX won't be accepted as a valid index. */
211 DUK_ASSERT(vs_size + (duk_uidx_t) DUK_INVALID_INDEX >= vs_size);
212
213 if (DUK_LIKELY(uindex < vs_size)) {
214 return (duk_idx_t) uindex;
215 }
216 return DUK_INVALID_INDEX;
217}
218
220 duk_hthread *thr = (duk_hthread *) ctx;
221 duk_uidx_t vs_size;
222 duk_uidx_t uindex;
223
226
228 vs_size = (duk_uidx_t) (thr->valstack_top - thr->valstack_bottom);
229 DUK_ASSERT_DISABLE(vs_size >= 0); /* unsigned */
230
231 if (index < 0) {
232 uindex = vs_size + (duk_uidx_t) index;
233 } else {
235 uindex = (duk_uidx_t) index;
236 }
237
238 /* DUK_INVALID_INDEX won't be accepted as a valid index. */
239 DUK_ASSERT(vs_size + (duk_uidx_t) DUK_INVALID_INDEX >= vs_size);
240
241 if (DUK_LIKELY(uindex < vs_size)) {
242 return (duk_idx_t) uindex;
243 }
245 return 0; /* unreachable */
246}
247
249 duk_hthread *thr = (duk_hthread *) ctx;
250 duk_uidx_t vs_size;
251 duk_uidx_t uindex;
252
255
257 vs_size = (duk_uidx_t) (thr->valstack_top - thr->valstack_bottom);
258 DUK_ASSERT_DISABLE(vs_size >= 0); /* unsigned */
259
260 if (index < 0) {
261 uindex = vs_size + (duk_uidx_t) index;
262 } else {
264 uindex = (duk_uidx_t) index;
265 }
266
267 /* DUK_INVALID_INDEX won't be accepted as a valid index. */
268 DUK_ASSERT(vs_size + (duk_uidx_t) DUK_INVALID_INDEX >= vs_size);
269
270 if (DUK_LIKELY(uindex < vs_size)) {
271 return thr->valstack_bottom + uindex;
272 }
273 return NULL;
274}
275
277 duk_hthread *thr = (duk_hthread *) ctx;
278 duk_uidx_t vs_size;
279 duk_uidx_t uindex;
280
283
285 vs_size = (duk_uidx_t) (thr->valstack_top - thr->valstack_bottom);
286 DUK_ASSERT_DISABLE(vs_size >= 0); /* unsigned */
287
288 /* Use unsigned arithmetic to optimize comparison. */
289 if (index < 0) {
290 uindex = vs_size + (duk_uidx_t) index;
291 } else {
293 uindex = (duk_uidx_t) index;
294 }
295
296 /* DUK_INVALID_INDEX won't be accepted as a valid index. */
297 DUK_ASSERT(vs_size + (duk_uidx_t) DUK_INVALID_INDEX >= vs_size);
298
299 if (DUK_LIKELY(uindex < vs_size)) {
300 return thr->valstack_bottom + uindex;
301 }
303 return NULL;
304}
305
306/* Non-critical. */
313
314/* Non-critical. */
316 duk_hthread *thr = (duk_hthread *) ctx;
317
320
321 if (duk_normalize_index(ctx, index) < 0) {
323 return; /* unreachable */
324 }
325}
326
327/*
328 * Value stack top handling
329 */
330
332 duk_hthread *thr = (duk_hthread *) ctx;
333
335
336 return (duk_idx_t) (thr->valstack_top - thr->valstack_bottom);
337}
338
339/* Set stack top within currently allocated range, but don't reallocate.
340 * This is performance critical especially for call handling, so whenever
341 * changing, profile and look at generated code.
342 */
344 duk_hthread *thr = (duk_hthread *) ctx;
345 duk_uidx_t vs_size;
346 duk_uidx_t vs_limit;
347 duk_uidx_t uindex;
348 duk_tval *tv;
349
352
355 vs_size = (duk_uidx_t) (thr->valstack_top - thr->valstack_bottom);
356 vs_limit = (duk_uidx_t) (thr->valstack_end - thr->valstack_bottom);
357
358 if (index < 0) {
359 /* Negative indices are always within allocated stack but
360 * must not go below zero index.
361 */
362 uindex = vs_size + (duk_uidx_t) index;
363 } else {
364 /* Positive index can be higher than valstack top but must
365 * not go above allocated stack (equality is OK).
366 */
367 uindex = (duk_uidx_t) index;
368 }
369
370 /* DUK_INVALID_INDEX won't be accepted as a valid index. */
371 DUK_ASSERT(vs_size + (duk_uidx_t) DUK_INVALID_INDEX >= vs_size);
372 DUK_ASSERT(vs_size + (duk_uidx_t) DUK_INVALID_INDEX >= vs_limit);
373
374#if defined(DUK_USE_VALSTACK_UNSAFE)
375 DUK_ASSERT(uindex <= vs_limit);
376 DUK_UNREF(vs_limit);
377#else
378 if (DUK_UNLIKELY(uindex > vs_limit)) {
380 return; /* unreachable */
381 }
382#endif
383 DUK_ASSERT(uindex <= vs_limit);
384
385 /* Handle change in value stack top. Respect value stack
386 * initialization policy: 'undefined' above top. Note that
387 * DECREF may cause a side effect that reallocates valstack,
388 * so must relookup after DECREF.
389 */
390
391 if (uindex >= vs_size) {
392 /* Stack size increases or stays the same. */
393#if defined(DUK_USE_ASSERTIONS)
394 duk_uidx_t count;
395
396 count = uindex - vs_size;
397 while (count != 0) {
398 count--;
399 tv = thr->valstack_top + count;
401 }
402#endif
403 thr->valstack_top = thr->valstack_bottom + uindex;
404 } else {
405 /* Stack size decreases. */
406#if defined(DUK_USE_REFERENCE_COUNTING)
407 duk_uidx_t count;
408
409 count = vs_size - uindex;
410 DUK_ASSERT(count > 0);
411 while (count > 0) {
412 count--;
413 tv = --thr->valstack_top; /* tv -> value just before prev top value; must relookup */
414 DUK_ASSERT(tv >= thr->valstack_bottom);
415 DUK_TVAL_SET_UNDEFINED_UPDREF(thr, tv); /* side effects */
416 }
417#else /* DUK_USE_REFERENCE_COUNTING */
418 duk_uidx_t count;
419 duk_tval *tv_end;
420
421 count = vs_size - uindex;
422 tv = thr->valstack_top;
423 tv_end = tv - count;
424 DUK_ASSERT(tv > tv_end);
425 do {
426 tv--;
428 } while (tv != tv_end);
429 thr->valstack_top = tv_end;
430#endif /* DUK_USE_REFERENCE_COUNTING */
431 }
432}
433
435 duk_hthread *thr = (duk_hthread *) ctx;
436 duk_idx_t ret;
437
439
440 ret = ((duk_idx_t) (thr->valstack_top - thr->valstack_bottom)) - 1;
441 if (DUK_UNLIKELY(ret < 0)) {
442 /* Return invalid index; if caller uses this without checking
443 * in another API call, the index won't map to a valid stack
444 * entry.
445 */
446 return DUK_INVALID_INDEX;
447 }
448 return ret;
449}
450
452 duk_hthread *thr = (duk_hthread *) ctx;
453 duk_idx_t ret;
454
456
457 ret = ((duk_idx_t) (thr->valstack_top - thr->valstack_bottom)) - 1;
458 if (DUK_UNLIKELY(ret < 0)) {
459 DUK_ERROR_API_INDEX(thr, -1);
460 return 0; /* unreachable */
461 }
462 return ret;
463}
464
465/*
466 * Value stack resizing.
467 *
468 * This resizing happens above the current "top": the value stack can be
469 * grown or shrunk, but the "top" is not affected. The value stack cannot
470 * be resized to a size below the current "top".
471 *
472 * The low level reallocation primitive must carefully recompute all value
473 * stack pointers, and must also work if ALL pointers are NULL. The resize
474 * is quite tricky because the valstack realloc may cause a mark-and-sweep,
475 * which may run finalizers. Running finalizers may resize the valstack
476 * recursively (the same value stack we're working on). So, after realloc
477 * returns, we know that the valstack "top" should still be the same (there
478 * should not be live values above the "top"), but its underlying size and
479 * pointer may have changed.
480 */
481
482/* XXX: perhaps refactor this to allow caller to specify some parameters, or
483 * at least a 'compact' flag which skips any spare or round-up .. useful for
484 * emergency gc.
485 */
486
488 duk_hthread *thr = (duk_hthread *) ctx;
489 duk_ptrdiff_t old_bottom_offset;
490 duk_ptrdiff_t old_top_offset;
491 duk_ptrdiff_t old_end_offset_post;
492#ifdef DUK_USE_DEBUG
493 duk_ptrdiff_t old_end_offset_pre;
494 duk_tval *old_valstack_pre;
495 duk_tval *old_valstack_post;
496#endif
497 duk_tval *new_valstack;
498 duk_size_t new_alloc_size;
499 duk_tval *p;
500
502 DUK_ASSERT(thr != NULL);
503 DUK_ASSERT(thr->valstack_bottom >= thr->valstack);
506 DUK_ASSERT((duk_size_t) (thr->valstack_top - thr->valstack) <= new_size); /* can't resize below 'top' */
507 DUK_ASSERT(new_size <= thr->valstack_max); /* valstack limit caller has check, prevents wrapping */
508 DUK_ASSERT(new_size <= DUK_SIZE_MAX / sizeof(duk_tval)); /* specific assert for wrapping */
509
510 /* get pointer offsets for tweaking below */
511 old_bottom_offset = (((duk_uint8_t *) thr->valstack_bottom) - ((duk_uint8_t *) thr->valstack));
512 old_top_offset = (((duk_uint8_t *) thr->valstack_top) - ((duk_uint8_t *) thr->valstack));
513#ifdef DUK_USE_DEBUG
514 old_end_offset_pre = (((duk_uint8_t *) thr->valstack_end) - ((duk_uint8_t *) thr->valstack)); /* not very useful, used for debugging */
515 old_valstack_pre = thr->valstack;
516#endif
517
518 /* Allocate a new valstack.
519 *
520 * Note: cannot use a plain DUK_REALLOC() because a mark-and-sweep may
521 * invalidate the original thr->valstack base pointer inside the realloc
522 * process. See doc/memory-management.rst.
523 */
524
525 new_alloc_size = sizeof(duk_tval) * new_size;
526 new_valstack = (duk_tval *) DUK_REALLOC_INDIRECT(thr->heap, duk_hthread_get_valstack_ptr, (void *) thr, new_alloc_size);
527 if (!new_valstack) {
528 /* Because new_size != 0, if condition doesn't need to be
529 * (new_valstack != NULL || new_size == 0).
530 */
531 DUK_ASSERT(new_size != 0);
532 DUK_D(DUK_DPRINT("failed to resize valstack to %lu entries (%lu bytes)",
533 (unsigned long) new_size, (unsigned long) new_alloc_size));
534 return 0;
535 }
536
537 /* Note: the realloc may have triggered a mark-and-sweep which may
538 * have resized our valstack internally. However, the mark-and-sweep
539 * MUST NOT leave the stack bottom/top in a different state. Particular
540 * assumptions and facts:
541 *
542 * - The thr->valstack pointer may be different after realloc,
543 * and the offset between thr->valstack_end <-> thr->valstack
544 * may have changed.
545 * - The offset between thr->valstack_bottom <-> thr->valstack
546 * and thr->valstack_top <-> thr->valstack MUST NOT have changed,
547 * because mark-and-sweep must adhere to a strict stack policy.
548 * In other words, logical bottom and top MUST NOT have changed.
549 * - All values above the top are unreachable but are initialized
550 * to UNDEFINED, up to the post-realloc valstack_end.
551 * - 'old_end_offset' must be computed after realloc to be correct.
552 */
553
554 DUK_ASSERT((((duk_uint8_t *) thr->valstack_bottom) - ((duk_uint8_t *) thr->valstack)) == old_bottom_offset);
555 DUK_ASSERT((((duk_uint8_t *) thr->valstack_top) - ((duk_uint8_t *) thr->valstack)) == old_top_offset);
556
557 /* success, fixup pointers */
558 old_end_offset_post = (((duk_uint8_t *) thr->valstack_end) - ((duk_uint8_t *) thr->valstack)); /* must be computed after realloc */
559#ifdef DUK_USE_DEBUG
560 old_valstack_post = thr->valstack;
561#endif
562 thr->valstack = new_valstack;
563 thr->valstack_end = new_valstack + new_size;
564#if !defined(DUK_USE_PREFER_SIZE)
565 thr->valstack_size = new_size;
566#endif
567 thr->valstack_bottom = (duk_tval *) (void *) ((duk_uint8_t *) new_valstack + old_bottom_offset);
568 thr->valstack_top = (duk_tval *) (void *) ((duk_uint8_t *) new_valstack + old_top_offset);
569
570 DUK_ASSERT(thr->valstack_bottom >= thr->valstack);
573
574 /* useful for debugging */
575#ifdef DUK_USE_DEBUG
576 if (old_end_offset_pre != old_end_offset_post) {
577 DUK_D(DUK_DPRINT("valstack was resized during valstack_resize(), probably by mark-and-sweep; "
578 "end offset changed: %lu -> %lu",
579 (unsigned long) old_end_offset_pre,
580 (unsigned long) old_end_offset_post));
581 }
582 if (old_valstack_pre != old_valstack_post) {
583 DUK_D(DUK_DPRINT("valstack pointer changed during valstack_resize(), probably by mark-and-sweep: %p -> %p",
584 (void *) old_valstack_pre,
585 (void *) old_valstack_post));
586 }
587#endif
588
589 DUK_DD(DUK_DDPRINT("resized valstack to %lu elements (%lu bytes), bottom=%ld, top=%ld, "
590 "new pointers: start=%p end=%p bottom=%p top=%p",
591 (unsigned long) new_size, (unsigned long) new_alloc_size,
592 (long) (thr->valstack_bottom - thr->valstack),
593 (long) (thr->valstack_top - thr->valstack),
594 (void *) thr->valstack, (void *) thr->valstack_end,
595 (void *) thr->valstack_bottom, (void *) thr->valstack_top));
596
597 /* Init newly allocated slots (only). */
598 p = (duk_tval *) (void *) ((duk_uint8_t *) thr->valstack + old_end_offset_post);
599 while (p < thr->valstack_end) {
600 /* Never executed if new size is smaller. */
602 p++;
603 }
604
605 /* Assert for value stack initialization policy. */
606#if defined(DUK_USE_ASSERTIONS)
607 p = thr->valstack_top;
608 while (p < thr->valstack_end) {
610 p++;
611 }
612#endif
613
614 return 1;
615}
616
619 duk_size_t min_new_size,
620 duk_small_uint_t flags) {
621 duk_hthread *thr = (duk_hthread *) ctx;
622 duk_size_t old_size;
623 duk_size_t new_size;
624 duk_bool_t is_shrink = 0;
625 duk_small_uint_t shrink_flag = (flags & DUK_VSRESIZE_FLAG_SHRINK);
626 duk_small_uint_t compact_flag = (flags & DUK_VSRESIZE_FLAG_COMPACT);
627 duk_small_uint_t throw_flag = (flags & DUK_VSRESIZE_FLAG_THROW);
628
629 DUK_DDD(DUK_DDDPRINT("check valstack resize: min_new_size=%lu, curr_size=%ld, curr_top=%ld, "
630 "curr_bottom=%ld, shrink=%d, compact=%d, throw=%d",
631 (unsigned long) min_new_size,
632 (long) (thr->valstack_end - thr->valstack),
633 (long) (thr->valstack_top - thr->valstack),
634 (long) (thr->valstack_bottom - thr->valstack),
635 (int) shrink_flag, (int) compact_flag, (int) throw_flag));
636
638 DUK_ASSERT(thr != NULL);
639 DUK_ASSERT(thr->valstack_bottom >= thr->valstack);
642
643#if defined(DUK_USE_PREFER_SIZE)
644 old_size = (duk_size_t) (thr->valstack_end - thr->valstack);
645#else
647 old_size = thr->valstack_size;
648#endif
649
650 if (min_new_size <= old_size) {
651 is_shrink = 1;
652 if (!shrink_flag ||
653 old_size - min_new_size < DUK_VALSTACK_SHRINK_THRESHOLD) {
654 DUK_DDD(DUK_DDDPRINT("no need to grow or shrink valstack"));
655 return 1;
656 }
657 }
658
659 new_size = min_new_size;
660 if (!compact_flag) {
661 if (is_shrink) {
662 /* shrink case; leave some spare */
663 new_size += DUK_VALSTACK_SHRINK_SPARE;
664 }
665
666 /* round up roughly to next 'grow step' */
667 new_size = (new_size / DUK_VALSTACK_GROW_STEP + 1) * DUK_VALSTACK_GROW_STEP;
668 }
669
670 DUK_DD(DUK_DDPRINT("want to %s valstack: %lu -> %lu elements (min_new_size %lu)",
671 (const char *) (new_size > old_size ? "grow" : "shrink"),
672 (unsigned long) old_size, (unsigned long) new_size,
673 (unsigned long) min_new_size));
674
675 if (new_size > thr->valstack_max) {
676 /* Note: may be triggered even if minimal new_size would not reach the limit,
677 * plan limit accordingly (taking DUK_VALSTACK_GROW_STEP into account).
678 */
679 if (throw_flag) {
681 } else {
682 return 0;
683 }
684 }
685
686 /*
687 * When resizing the valstack, a mark-and-sweep may be triggered for
688 * the allocation of the new valstack. If the mark-and-sweep needs
689 * to use our thread for something, it may cause *the same valstack*
690 * to be resized recursively. This happens e.g. when mark-and-sweep
691 * finalizers are called. This is taken into account carefully in
692 * duk__resize_valstack().
693 *
694 * 'new_size' is known to be <= valstack_max, which ensures that
695 * size_t and pointer arithmetic won't wrap in duk__resize_valstack().
696 */
697
698 if (!duk__resize_valstack(ctx, new_size)) {
699 if (is_shrink) {
700 DUK_DD(DUK_DDPRINT("valstack resize failed, but is a shrink, ignore"));
701 return 1;
702 }
703
704 DUK_DD(DUK_DDPRINT("valstack resize failed"));
705
706 if (throw_flag) {
708 } else {
709 return 0;
710 }
711 }
712
713 DUK_DDD(DUK_DDDPRINT("valstack resize successful"));
714 return 1;
715}
716
718 duk_hthread *thr = (duk_hthread *) ctx;
719 duk_size_t min_new_size;
720
722 DUK_ASSERT(thr != NULL);
723
724 if (DUK_UNLIKELY(extra < 0)) {
725 /* Clamping to zero makes the API more robust to calling code
726 * calculation errors.
727 */
728 extra = 0;
729 }
730
731 min_new_size = (thr->valstack_top - thr->valstack) + extra + DUK_VALSTACK_INTERNAL_EXTRA;
732 return duk_valstack_resize_raw(ctx,
733 min_new_size, /* min_new_size */
734 0 /* no shrink */ | /* flags */
735 0 /* no compact */ |
736 0 /* no throw */);
737}
738
740 duk_hthread *thr = (duk_hthread *) ctx;
741 duk_size_t min_new_size;
742
744 DUK_ASSERT(thr != NULL);
745
746 if (DUK_UNLIKELY(extra < 0)) {
747 /* Clamping to zero makes the API more robust to calling code
748 * calculation errors.
749 */
750 extra = 0;
751 }
752
753 min_new_size = (thr->valstack_top - thr->valstack) + extra + DUK_VALSTACK_INTERNAL_EXTRA;
754 (void) duk_valstack_resize_raw(ctx,
755 min_new_size, /* min_new_size */
756 0 /* no shrink */ | /* flags */
757 0 /* no compact */ |
759}
760
762 duk_size_t min_new_size;
763
765
766 if (DUK_UNLIKELY(top < 0)) {
767 /* Clamping to zero makes the API more robust to calling code
768 * calculation errors.
769 */
770 top = 0;
771 }
772
773 min_new_size = top + DUK_VALSTACK_INTERNAL_EXTRA;
774 return duk_valstack_resize_raw(ctx,
775 min_new_size, /* min_new_size */
776 0 /* no shrink */ | /* flags */
777 0 /* no compact */ |
778 0 /* no throw */);
779}
780
782 duk_size_t min_new_size;
783
785
786 if (DUK_UNLIKELY(top < 0)) {
787 /* Clamping to zero makes the API more robust to calling code
788 * calculation errors.
789 */
790 top = 0;
791 }
792
793 min_new_size = top + DUK_VALSTACK_INTERNAL_EXTRA;
794 (void) duk_valstack_resize_raw(ctx,
795 min_new_size, /* min_new_size */
796 0 /* no shrink */ | /* flags */
797 0 /* no compact */ |
799}
800
801/*
802 * Basic stack manipulation: swap, dup, insert, replace, etc
803 */
804
806 duk_tval *tv1;
807 duk_tval *tv2;
808 duk_tval tv_tmp;
809
811
812 tv1 = duk_require_tval(ctx, index1);
813 DUK_ASSERT(tv1 != NULL);
814 tv2 = duk_require_tval(ctx, index2);
815 DUK_ASSERT(tv2 != NULL);
816
817 /* If tv1==tv2 this is a NOP, no check is needed */
818 DUK_TVAL_SET_TVAL(&tv_tmp, tv1);
819 DUK_TVAL_SET_TVAL(tv1, tv2);
820 DUK_TVAL_SET_TVAL(tv2, &tv_tmp);
821}
822
828
830 duk_hthread *thr;
831 duk_tval *tv_from;
832 duk_tval *tv_to;
833
835 thr = (duk_hthread *) ctx;
837
838 tv_from = duk_require_tval(ctx, from_index);
839 tv_to = thr->valstack_top++;
840 DUK_ASSERT(tv_from != NULL);
841 DUK_ASSERT(tv_to != NULL);
842 DUK_TVAL_SET_TVAL(tv_to, tv_from);
843 DUK_TVAL_INCREF(thr, tv_to); /* no side effects */
844}
845
847 duk_hthread *thr;
848 duk_tval *tv_from;
849 duk_tval *tv_to;
850
852 thr = (duk_hthread *) ctx;
854
855 if (thr->valstack_top - thr->valstack_bottom <= 0) {
856 DUK_ERROR_API_INDEX(thr, -1);
857 return; /* unreachable */
858 }
859 tv_from = thr->valstack_top - 1;
860 tv_to = thr->valstack_top++;
861 DUK_ASSERT(tv_from != NULL);
862 DUK_ASSERT(tv_to != NULL);
863 DUK_TVAL_SET_TVAL(tv_to, tv_from);
864 DUK_TVAL_INCREF(thr, tv_to); /* no side effects */
865}
866
868 duk_tval *p;
869 duk_tval *q;
870 duk_tval tv_tmp;
871 duk_size_t nbytes;
872
874
875 p = duk_require_tval(ctx, to_index);
876 DUK_ASSERT(p != NULL);
877 q = duk_require_tval(ctx, -1);
878 DUK_ASSERT(q != NULL);
879
880 DUK_ASSERT(q >= p);
881
882 /* nbytes
883 * <--------->
884 * [ ... | p | x | x | q ]
885 * => [ ... | q | p | x | x ]
886 */
887
888 nbytes = (duk_size_t) (((duk_uint8_t *) q) - ((duk_uint8_t *) p)); /* Note: 'q' is top-1 */
889
890 DUK_DDD(DUK_DDDPRINT("duk_insert: to_index=%ld, p=%p, q=%p, nbytes=%lu",
891 (long) to_index, (void *) p, (void *) q, (unsigned long) nbytes));
892
893 /* No net refcount changes. */
894
895 if (nbytes > 0) {
896 DUK_TVAL_SET_TVAL(&tv_tmp, q);
897 DUK_ASSERT(nbytes > 0);
898 DUK_MEMMOVE((void *) (p + 1), (const void *) p, (size_t) nbytes);
899 DUK_TVAL_SET_TVAL(p, &tv_tmp);
900 } else {
901 /* nop: insert top to top */
902 DUK_ASSERT(nbytes == 0);
903 DUK_ASSERT(p == q);
904 }
905}
906
908 duk_hthread *thr = (duk_hthread *) ctx;
909 duk_tval *tv1;
910 duk_tval *tv2;
911 duk_tval tv_tmp;
912
914
915 tv1 = duk_require_tval(ctx, -1);
916 DUK_ASSERT(tv1 != NULL);
917 tv2 = duk_require_tval(ctx, to_index);
918 DUK_ASSERT(tv2 != NULL);
919
920 /* For tv1 == tv2, both pointing to stack top, the end result
921 * is same as duk_pop(ctx).
922 */
923 DUK_TVAL_SET_TVAL(&tv_tmp, tv2);
924 DUK_TVAL_SET_TVAL(tv2, tv1);
926 thr->valstack_top--;
927 DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
928}
929
930DUK_EXTERNAL void duk_copy(duk_context *ctx, duk_idx_t from_index, duk_idx_t to_index) {
931 duk_hthread *thr = (duk_hthread *) ctx;
932 duk_tval *tv1;
933 duk_tval *tv2;
934
936 DUK_UNREF(thr); /* w/o refcounting */
937
938 tv1 = duk_require_tval(ctx, from_index);
939 DUK_ASSERT(tv1 != NULL);
940 tv2 = duk_require_tval(ctx, to_index);
941 DUK_ASSERT(tv2 != NULL);
942
943 /* For tv1 == tv2, this is a no-op (no explicit check needed). */
944 DUK_TVAL_SET_TVAL_UPDREF(thr, tv2, tv1); /* side effects */
945}
946
948 duk_hthread *thr = (duk_hthread *) ctx;
949 duk_tval *p;
950 duk_tval *q;
951#ifdef DUK_USE_REFERENCE_COUNTING
952 duk_tval tv_tmp;
953#endif
954 duk_size_t nbytes;
955
957
958 p = duk_require_tval(ctx, index);
959 DUK_ASSERT(p != NULL);
960 q = duk_require_tval(ctx, -1);
961 DUK_ASSERT(q != NULL);
962
963 DUK_ASSERT(q >= p);
964
965 /* nbytes zero size case
966 * <--------->
967 * [ ... | p | x | x | q ] [ ... | p==q ]
968 * => [ ... | x | x | q ] [ ... ]
969 */
970
971#ifdef DUK_USE_REFERENCE_COUNTING
972 /* use a temp: decref only when valstack reachable values are correct */
973 DUK_TVAL_SET_TVAL(&tv_tmp, p);
974#endif
975
976 nbytes = (duk_size_t) (((duk_uint8_t *) q) - ((duk_uint8_t *) p)); /* Note: 'q' is top-1 */
977 DUK_MEMMOVE((void *) p, (const void *) (p + 1), (size_t) nbytes); /* zero size not an issue: pointers are valid */
978
980 thr->valstack_top--;
981
982#ifdef DUK_USE_REFERENCE_COUNTING
983 DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
984#endif
985}
986
987/*
988 * Stack slice primitives
989 */
990
992 duk_hthread *to_thr = (duk_hthread *) to_ctx;
993 duk_hthread *from_thr = (duk_hthread *) from_ctx;
994 void *src;
995 duk_size_t nbytes;
996 duk_tval *p;
997 duk_tval *q;
998
999 /* XXX: several pointer comparison issues here */
1000
1001 DUK_ASSERT_CTX_VALID(to_ctx);
1002 DUK_ASSERT_CTX_VALID(from_ctx);
1003 DUK_ASSERT(to_ctx != NULL);
1004 DUK_ASSERT(from_ctx != NULL);
1005
1006 if (to_ctx == from_ctx) {
1008 return;
1009 }
1010 if ((count < 0) ||
1011 (count > (duk_idx_t) to_thr->valstack_max)) {
1012 /* Maximum value check ensures 'nbytes' won't wrap below. */
1014 return;
1015 }
1016
1017 nbytes = sizeof(duk_tval) * count;
1018 if (nbytes == 0) {
1019 return;
1020 }
1021 DUK_ASSERT(to_thr->valstack_top <= to_thr->valstack_end);
1022 if ((duk_size_t) ((duk_uint8_t *) to_thr->valstack_end - (duk_uint8_t *) to_thr->valstack_top) < nbytes) {
1024 }
1025 src = (void *) ((duk_uint8_t *) from_thr->valstack_top - nbytes);
1026 if (src < (void *) from_thr->valstack_bottom) {
1028 }
1029
1030 /* copy values (no overlap even if to_ctx == from_ctx; that's not
1031 * allowed now anyway)
1032 */
1033 DUK_ASSERT(nbytes > 0);
1034 DUK_MEMCPY((void *) to_thr->valstack_top, (const void *) src, (size_t) nbytes);
1035
1036 p = to_thr->valstack_top;
1037 to_thr->valstack_top = (duk_tval *) (void *) (((duk_uint8_t *) p) + nbytes);
1038
1039 if (is_copy) {
1040 /* Incref copies, keep originals. */
1041 q = to_thr->valstack_top;
1042 while (p < q) {
1043 DUK_TVAL_INCREF(to_thr, p); /* no side effects */
1044 p++;
1045 }
1046 } else {
1047 /* No net refcount change. */
1048 p = from_thr->valstack_top;
1049 q = (duk_tval *) (void *) (((duk_uint8_t *) p) - nbytes);
1050 from_thr->valstack_top = q;
1051
1052 while (p > q) {
1053 p--;
1055 /* XXX: fast primitive to set a bunch of values to UNDEFINED */
1056 }
1057 }
1058}
1059
1060/*
1061 * Get/require
1062 */
1063
1065 duk_hthread *thr = (duk_hthread *) ctx;
1066 duk_tval *tv;
1067
1069
1070 tv = duk_get_tval(ctx, index);
1071 if (tv && DUK_TVAL_IS_UNDEFINED(tv)) {
1072 return;
1073 }
1075 return; /* not reachable */
1076}
1077
1079 duk_hthread *thr = (duk_hthread *) ctx;
1080 duk_tval *tv;
1081
1083
1084 tv = duk_get_tval(ctx, index);
1085 if (tv && DUK_TVAL_IS_NULL(tv)) {
1086 return;
1087 }
1089 return; /* not reachable */
1090}
1091
1093 duk_bool_t ret = 0; /* default: false */
1094 duk_tval *tv;
1095
1097
1098 tv = duk_get_tval(ctx, index);
1099 if (tv && DUK_TVAL_IS_BOOLEAN(tv)) {
1100 ret = DUK_TVAL_GET_BOOLEAN(tv);
1101 }
1102
1103 DUK_ASSERT(ret == 0 || ret == 1);
1104 return ret;
1105}
1106
1108 duk_hthread *thr = (duk_hthread *) ctx;
1109 duk_tval *tv;
1110
1112
1113 tv = duk_get_tval(ctx, index);
1114 if (tv && DUK_TVAL_IS_BOOLEAN(tv)) {
1116 DUK_ASSERT(ret == 0 || ret == 1);
1117 return ret;
1118 }
1120 return 0; /* not reachable */
1121}
1122
1124 duk_double_union ret;
1125 duk_tval *tv;
1126
1128
1129 ret.d = DUK_DOUBLE_NAN; /* default: NaN */
1130 tv = duk_get_tval(ctx, index);
1131 if (tv && DUK_TVAL_IS_NUMBER(tv)) {
1132 ret.d = DUK_TVAL_GET_NUMBER(tv);
1133 }
1134
1135 /*
1136 * Number should already be in NaN-normalized form, but let's
1137 * normalize anyway.
1138 */
1139
1141 return ret.d;
1142}
1143
1145 duk_hthread *thr = (duk_hthread *) ctx;
1146 duk_tval *tv;
1147
1149
1150 tv = duk_get_tval(ctx, index);
1151 if (tv && DUK_TVAL_IS_NUMBER(tv)) {
1152 duk_double_union ret;
1153 ret.d = DUK_TVAL_GET_NUMBER(tv);
1154
1155 /*
1156 * Number should already be in NaN-normalized form,
1157 * but let's normalize anyway.
1158 */
1159
1161 return ret.d;
1162 }
1164 return DUK_DOUBLE_NAN; /* not reachable */
1165}
1166
1168 /* Custom coercion for API */
1170 return (duk_int_t) duk__api_coerce_d2i(ctx, index, 0 /*require*/);
1171}
1172
1174 /* Custom coercion for API */
1176 return (duk_uint_t) duk__api_coerce_d2ui(ctx, index, 0 /*require*/);
1177}
1178
1180 /* Custom coercion for API */
1182 return (duk_int_t) duk__api_coerce_d2i(ctx, index, 1 /*require*/);
1183}
1184
1186 /* Custom coercion for API */
1188 return (duk_uint_t) duk__api_coerce_d2ui(ctx, index, 1 /*require*/);
1189}
1190
1192 const char *ret;
1193 duk_tval *tv;
1194
1196
1197 /* default: NULL, length 0 */
1198 ret = NULL;
1199 if (out_len) {
1200 *out_len = 0;
1201 }
1202
1203 tv = duk_get_tval(ctx, index);
1204 if (tv && DUK_TVAL_IS_STRING(tv)) {
1205 /* Here we rely on duk_hstring instances always being zero
1206 * terminated even if the actual string is not.
1207 */
1209 DUK_ASSERT(h != NULL);
1210 ret = (const char *) DUK_HSTRING_GET_DATA(h);
1211 if (out_len) {
1212 *out_len = DUK_HSTRING_GET_BYTELEN(h);
1213 }
1214 }
1215
1216 return ret;
1217}
1218
1220 duk_hthread *thr = (duk_hthread *) ctx;
1221 const char *ret;
1222
1224
1225 /* Note: this check relies on the fact that even a zero-size string
1226 * has a non-NULL pointer.
1227 */
1228 ret = duk_get_lstring(ctx, index, out_len);
1229 if (ret) {
1230 return ret;
1231 }
1233 return NULL; /* not reachable */
1234}
1235
1238
1239 return duk_get_lstring(ctx, index, NULL);
1240}
1241
1244
1245 return duk_require_lstring(ctx, index, NULL);
1246}
1247
1249 duk_tval *tv;
1250
1252
1253 tv = duk_get_tval(ctx, index);
1254 if (tv && DUK_TVAL_IS_POINTER(tv)) {
1255 void *p = DUK_TVAL_GET_POINTER(tv); /* may be NULL */
1256 return (void *) p;
1257 }
1258
1259 return NULL;
1260}
1261
1263 duk_hthread *thr = (duk_hthread *) ctx;
1264 duk_tval *tv;
1265
1267
1268 /* Note: here we must be wary of the fact that a pointer may be
1269 * valid and be a NULL.
1270 */
1271 tv = duk_get_tval(ctx, index);
1272 if (tv && DUK_TVAL_IS_POINTER(tv)) {
1273 void *p = DUK_TVAL_GET_POINTER(tv); /* may be NULL */
1274 return (void *) p;
1275 }
1277 return NULL; /* not reachable */
1278}
1279
1280#if 0 /*unused*/
1281DUK_INTERNAL void *duk_get_voidptr(duk_context *ctx, duk_idx_t index) {
1282 duk_tval *tv;
1283
1285
1286 tv = duk_get_tval(ctx, index);
1287 if (tv && DUK_TVAL_IS_HEAP_ALLOCATED(tv)) {
1289 DUK_ASSERT(h != NULL);
1290 return (void *) h;
1291 }
1292
1293 return NULL;
1294}
1295#endif
1296
1298 duk_hthread *thr = (duk_hthread *) ctx;
1299 duk_tval *tv;
1300
1302 DUK_UNREF(thr);
1303
1304 if (out_size != NULL) {
1305 *out_size = 0;
1306 }
1307
1308 tv = duk_get_tval(ctx, index);
1309 if (tv && DUK_TVAL_IS_BUFFER(tv)) {
1311 DUK_ASSERT(h != NULL);
1312 if (out_size) {
1313 *out_size = DUK_HBUFFER_GET_SIZE(h);
1314 }
1315 return (void *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h); /* may be NULL (but only if size is 0) */
1316 }
1317
1318 if (throw_flag) {
1320 }
1321 return NULL;
1322}
1323
1325 return duk__get_buffer_helper(ctx, index, out_size, 0 /*throw_flag*/);
1326}
1327
1329 return duk__get_buffer_helper(ctx, index, out_size, 1 /*throw_flag*/);
1330}
1331
1333 duk_hthread *thr = (duk_hthread *) ctx;
1334 duk_tval *tv;
1335
1337 DUK_UNREF(thr);
1338
1339 if (out_size != NULL) {
1340 *out_size = 0;
1341 }
1342
1343 tv = duk_get_tval(ctx, index);
1344 if (tv == NULL) {
1345 goto fail;
1346 }
1347
1348 if (DUK_TVAL_IS_BUFFER(tv)) {
1350 DUK_ASSERT(h != NULL);
1351 if (out_size) {
1352 *out_size = DUK_HBUFFER_GET_SIZE(h);
1353 }
1354 return (void *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h); /* may be NULL (but only if size is 0) */
1355 } else if (DUK_TVAL_IS_OBJECT(tv)) {
1357 DUK_ASSERT(h != NULL);
1359 /* XXX: this is probably a useful shared helper: for a
1360 * duk_hbufferobject, get a validated buffer pointer/length.
1361 */
1362 duk_hbufferobject *h_bufobj = (duk_hbufferobject *) h;
1364
1365 if (h_bufobj->buf != NULL &&
1367 duk_uint8_t *p;
1368
1369 p = (duk_uint8_t *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h_bufobj->buf);
1370 if (out_size != NULL) {
1371 *out_size = (duk_size_t) h_bufobj->length;
1372 }
1373 return (void *) (p + h_bufobj->offset);
1374 }
1375 /* if slice not fully valid, treat as error */
1376 }
1377 }
1378
1379 fail:
1380 if (throw_flag) {
1382 }
1383 return NULL;
1384}
1385
1387 return duk__get_buffer_data_helper(ctx, index, out_size, 0 /*throw_flag*/);
1388}
1389
1391 return duk__get_buffer_data_helper(ctx, index, out_size, 1 /*throw_flag*/);
1392}
1393
1394/* Raw helper for getting a value from the stack, checking its tag.
1395 * The tag cannot be a number because numbers don't have an internal
1396 * tag in the packed representation.
1397 */
1398
1400 duk_tval *tv;
1401
1403
1404 tv = duk_get_tval(ctx, index);
1405 if (tv && (DUK_TVAL_GET_TAG(tv) == tag)) {
1406 duk_heaphdr *ret;
1407 ret = DUK_TVAL_GET_HEAPHDR(tv);
1408 DUK_ASSERT(ret != NULL); /* tagged null pointers should never occur */
1409 return ret;
1410 }
1411
1412 return (duk_heaphdr *) NULL;
1413}
1414
1418
1427
1431
1440
1444
1453
1461
1470
1478
1487
1495
1504
1506 duk_tval *tv;
1507 duk_hobject *h;
1509
1511
1512 tv = duk_get_tval(ctx, index);
1513 if (!tv) {
1514 return NULL;
1515 }
1516 if (!DUK_TVAL_IS_OBJECT(tv)) {
1517 return NULL;
1518 }
1519 h = DUK_TVAL_GET_OBJECT(tv);
1520 DUK_ASSERT(h != NULL);
1521
1523 return NULL;
1524 }
1526 f = (duk_hnativefunction *) h;
1527
1528 return f->func;
1529}
1530
1532 duk_hthread *thr = (duk_hthread *) ctx;
1533 duk_c_function ret;
1534
1536
1537 ret = duk_get_c_function(ctx, index);
1538 if (!ret) {
1540 }
1541 return ret;
1542}
1543
1549
1555
1561
1563 duk_tval *tv;
1564 void *ret;
1565
1567
1568 tv = duk_get_tval(ctx, index);
1569 if (tv && DUK_TVAL_IS_HEAP_ALLOCATED(tv)) {
1570 ret = (void *) DUK_TVAL_GET_HEAPHDR(tv);
1571 DUK_ASSERT(ret != NULL);
1572 return ret;
1573 }
1574
1575 return (void *) NULL;
1576}
1577
1579 duk_hthread *thr = (duk_hthread *) ctx;
1580 duk_tval *tv;
1581 void *ret;
1582
1584
1585 tv = duk_require_tval(ctx, index);
1586 DUK_ASSERT(tv != NULL);
1588 ret = (void *) DUK_TVAL_GET_HEAPHDR(tv);
1589 DUK_ASSERT(ret != NULL);
1590 return ret;
1591 }
1592
1594 return (void *) NULL; /* not reachable */
1595}
1596
1597#if 0
1598/* This would be pointless: we'd return NULL for both lightfuncs and
1599 * unexpected types.
1600 */
1601DUK_INTERNAL duk_hobject *duk_get_hobject_or_lfunc(duk_context *ctx, duk_idx_t index) {
1602}
1603#endif
1604
1605/* Useful for internal call sites where we either expect an object (function)
1606 * or a lightfunc. Accepts an object (returned as is) or a lightfunc (coerced
1607 * to an object). Return value is NULL if value is neither an object nor a
1608 * lightfunc.
1609 */
1611 duk_tval *tv;
1612
1614
1615 tv = duk_require_tval(ctx, index);
1616 DUK_ASSERT(tv != NULL);
1617 if (DUK_TVAL_IS_OBJECT(tv)) {
1618 return DUK_TVAL_GET_OBJECT(tv);
1619 } else if (DUK_TVAL_IS_LIGHTFUNC(tv)) {
1620 duk_to_object(ctx, index);
1621 return duk_require_hobject(ctx, index);
1622 }
1623
1624 return NULL;
1625}
1626
1627/* Useful for internal call sites where we either expect an object (function)
1628 * or a lightfunc. Returns NULL for a lightfunc.
1629 */
1631 duk_hthread *thr = (duk_hthread *) ctx;
1632 duk_tval *tv;
1633
1635
1636 tv = duk_require_tval(ctx, index);
1637 DUK_ASSERT(tv != NULL);
1638 if (DUK_TVAL_IS_OBJECT(tv)) {
1639 return DUK_TVAL_GET_OBJECT(tv);
1640 } else if (DUK_TVAL_IS_LIGHTFUNC(tv)) {
1641 return NULL;
1642 }
1644 return NULL; /* not reachable */
1645}
1646
1647/* Useful for internal call sites where we either expect an object (function)
1648 * or a lightfunc. Accepts an object (returned as is) or a lightfunc (coerced
1649 * to an object). Return value is never NULL.
1650 */
1652 duk_hthread *thr = (duk_hthread *) ctx;
1653 duk_tval *tv;
1654
1656
1657 tv = duk_require_tval(ctx, index);
1658 if (DUK_TVAL_IS_OBJECT(tv)) {
1659 return DUK_TVAL_GET_OBJECT(tv);
1660 } else if (DUK_TVAL_IS_LIGHTFUNC(tv)) {
1661 duk_to_object(ctx, index);
1662 return duk_require_hobject(ctx, index);
1663 }
1665 return NULL; /* not reachable */
1666}
1667
1669 duk_hobject *h;
1670
1672 DUK_ASSERT_DISABLE(classnum >= 0); /* unsigned */
1673 DUK_ASSERT(classnum <= DUK_HOBJECT_CLASS_MAX);
1674
1676 if (h != NULL && DUK_HOBJECT_GET_CLASS_NUMBER(h) != classnum) {
1677 h = NULL;
1678 }
1679 return h;
1680}
1681
1683 duk_hthread *thr;
1684 duk_hobject *h;
1685
1687 DUK_ASSERT_DISABLE(classnum >= 0); /* unsigned */
1688 DUK_ASSERT(classnum <= DUK_HOBJECT_CLASS_MAX);
1689 thr = (duk_hthread *) ctx;
1690
1692 if (!(h != NULL && DUK_HOBJECT_GET_CLASS_NUMBER(h) == classnum)) {
1693 duk_hstring *h_class;
1695 DUK_UNREF(h_class);
1696
1698 }
1699 return h;
1700}
1701
1703 duk_tval *tv;
1704
1706
1707 tv = duk_get_tval(ctx, index);
1708 if (!tv) {
1709 return 0;
1710 }
1711
1712 switch (DUK_TVAL_GET_TAG(tv)) {
1713 case DUK_TAG_UNDEFINED:
1714 case DUK_TAG_NULL:
1715 case DUK_TAG_BOOLEAN:
1716 case DUK_TAG_POINTER:
1717 return 0;
1718 case DUK_TAG_STRING: {
1720 DUK_ASSERT(h != NULL);
1722 }
1723 case DUK_TAG_OBJECT: {
1725 DUK_ASSERT(h != NULL);
1726 return (duk_size_t) duk_hobject_get_length((duk_hthread *) ctx, h);
1727 }
1728 case DUK_TAG_BUFFER: {
1730 DUK_ASSERT(h != NULL);
1731 return (duk_size_t) DUK_HBUFFER_GET_SIZE(h);
1732 }
1733 case DUK_TAG_LIGHTFUNC: {
1734 duk_small_uint_t lf_flags;
1735 lf_flags = DUK_TVAL_GET_LIGHTFUNC_FLAGS(tv);
1736 return (duk_size_t) DUK_LFUNC_FLAGS_GET_LENGTH(lf_flags);
1737 }
1738#if defined(DUK_USE_FASTINT)
1739 case DUK_TAG_FASTINT:
1740#endif
1741 default:
1742 /* number */
1745 return 0;
1746 }
1747
1749}
1750
1752 duk_hthread *thr = (duk_hthread *) ctx;
1753 duk_hobject *h;
1754
1756
1757 h = duk_get_hobject(ctx, index);
1758 if (!h) {
1759 return;
1760 }
1761
1762 duk_hobject_set_length(thr, h, (duk_uint32_t) length); /* XXX: typing */
1763}
1764
1765/*
1766 * Conversions and coercions
1767 *
1768 * The conversion/coercions are in-place operations on the value stack.
1769 * Some operations are implemented here directly, while others call a
1770 * helper in duk_js_ops.c after validating arguments.
1771 */
1772
1773/* E5 Section 8.12.8 */
1774
1776 if (duk_get_prop_stridx(ctx, index, func_stridx)) {
1777 /* [ ... func ] */
1778 if (duk_is_callable(ctx, -1)) {
1779 duk_dup(ctx, index); /* -> [ ... func this ] */
1780 duk_call_method(ctx, 0); /* -> [ ... retval ] */
1781 if (duk_is_primitive(ctx, -1)) {
1782 duk_replace(ctx, index);
1783 return 1;
1784 }
1785 /* [ ... retval ]; popped below */
1786 }
1787 }
1788 duk_pop(ctx); /* [ ... func/retval ] -> [ ... ] */
1789 return 0;
1790}
1791
1793 duk_hthread *thr = (duk_hthread *) ctx;
1794 duk_hobject *obj;
1795 /* inline initializer for coercers[] is not allowed by old compilers like BCC */
1796 duk_small_int_t coercers[2];
1797
1799 DUK_ASSERT(thr != NULL);
1800
1801 coercers[0] = DUK_STRIDX_VALUE_OF;
1802 coercers[1] = DUK_STRIDX_TO_STRING;
1803
1806
1807 if (hint == DUK_HINT_NONE) {
1809 hint = DUK_HINT_STRING;
1810 } else {
1811 hint = DUK_HINT_NUMBER;
1812 }
1813 }
1814
1815 if (hint == DUK_HINT_STRING) {
1816 coercers[0] = DUK_STRIDX_TO_STRING;
1817 coercers[1] = DUK_STRIDX_VALUE_OF;
1818 }
1819
1820 if (duk__defaultvalue_coerce_attempt(ctx, index, coercers[0])) {
1821 return;
1822 }
1823
1824 if (duk__defaultvalue_coerce_attempt(ctx, index, coercers[1])) {
1825 return;
1826 }
1827
1829}
1830
1832 duk_hthread *thr = (duk_hthread *) ctx;
1833 duk_tval *tv;
1834
1836 DUK_UNREF(thr);
1837
1838 tv = duk_require_tval(ctx, index);
1839 DUK_ASSERT(tv != NULL);
1840 DUK_TVAL_SET_UNDEFINED_UPDREF(thr, tv); /* side effects */
1841}
1842
1844 duk_hthread *thr = (duk_hthread *) ctx;
1845 duk_tval *tv;
1846
1848 DUK_UNREF(thr);
1849
1850 tv = duk_require_tval(ctx, index);
1851 DUK_ASSERT(tv != NULL);
1852 DUK_TVAL_SET_NULL_UPDREF(thr, tv); /* side effects */
1853}
1854
1855/* E5 Section 9.1 */
1858 DUK_ASSERT(hint == DUK_HINT_NONE || hint == DUK_HINT_NUMBER || hint == DUK_HINT_STRING);
1859
1861
1864 /* everything except object stay as is */
1865 return;
1866 }
1867 duk_to_defaultvalue(ctx, index, hint);
1868}
1869
1870/* E5 Section 9.2 */
1872 duk_hthread *thr = (duk_hthread *) ctx;
1873 duk_tval *tv;
1874 duk_bool_t val;
1875
1877 DUK_UNREF(thr);
1878
1880
1881 tv = duk_require_tval(ctx, index);
1882 DUK_ASSERT(tv != NULL);
1883
1884 val = duk_js_toboolean(tv);
1885 DUK_ASSERT(val == 0 || val == 1);
1886
1887 /* Note: no need to re-lookup tv, conversion is side effect free */
1888 DUK_ASSERT(tv != NULL);
1889 DUK_TVAL_SET_BOOLEAN_UPDREF(thr, tv, val); /* side effects */
1890 return val;
1891}
1892
1894 duk_hthread *thr = (duk_hthread *) ctx;
1895 duk_tval *tv;
1896 duk_double_t d;
1897
1899
1900 tv = duk_require_tval(ctx, index);
1901 DUK_ASSERT(tv != NULL);
1902 /* XXX: fastint? */
1903 d = duk_js_tonumber(thr, tv);
1904
1905 /* Note: need to re-lookup because ToNumber() may have side effects */
1906 tv = duk_require_tval(ctx, index);
1907 DUK_TVAL_SET_NUMBER_UPDREF(thr, tv, d); /* side effects */
1908 return d;
1909}
1910
1911/* XXX: combine all the integer conversions: they share everything
1912 * but the helper function for coercion.
1913 */
1914
1916
1918 duk_hthread *thr = (duk_hthread *) ctx;
1919 duk_tval *tv;
1920 duk_double_t d;
1921
1923
1924 tv = duk_require_tval(ctx, index);
1925 DUK_ASSERT(tv != NULL);
1926 d = coerce_func(thr, tv);
1927
1928 /* XXX: fastint? */
1929
1930 /* Relookup in case coerce_func() has side effects, e.g. ends up coercing an object */
1931 tv = duk_require_tval(ctx, index);
1932 DUK_TVAL_SET_NUMBER_UPDREF(thr, tv, d); /* side effects */
1933 return d;
1934}
1935
1937 /* Value coercion (in stack): ToInteger(), E5 Section 9.4
1938 * API return value coercion: custom
1939 */
1942 return (duk_int_t) duk__api_coerce_d2i(ctx, index, 0 /*require*/);
1943}
1944
1946 /* Value coercion (in stack): ToInteger(), E5 Section 9.4
1947 * API return value coercion: custom
1948 */
1951 return (duk_uint_t) duk__api_coerce_d2ui(ctx, index, 0 /*require*/);
1952}
1953
1955 duk_hthread *thr = (duk_hthread *) ctx;
1956 duk_tval *tv;
1957 duk_int32_t ret;
1958
1960
1961 tv = duk_require_tval(ctx, index);
1962 DUK_ASSERT(tv != NULL);
1963 ret = duk_js_toint32(thr, tv);
1964
1965 /* Relookup in case coerce_func() has side effects, e.g. ends up coercing an object */
1966 tv = duk_require_tval(ctx, index);
1967 DUK_TVAL_SET_FASTINT_I32_UPDREF(thr, tv, ret); /* side effects */
1968 return ret;
1969}
1970
1972 duk_hthread *thr = (duk_hthread *) ctx;
1973 duk_tval *tv;
1974 duk_uint32_t ret;
1975
1977
1978 tv = duk_require_tval(ctx, index);
1979 DUK_ASSERT(tv != NULL);
1980 ret = duk_js_touint32(thr, tv);
1981
1982 /* Relookup in case coerce_func() has side effects, e.g. ends up coercing an object */
1983 tv = duk_require_tval(ctx, index);
1984 DUK_TVAL_SET_FASTINT_U32_UPDREF(thr, tv, ret); /* side effects */
1985 return ret;
1986}
1987
1989 duk_hthread *thr = (duk_hthread *) ctx;
1990 duk_tval *tv;
1991 duk_uint16_t ret;
1992
1994
1995 tv = duk_require_tval(ctx, index);
1996 DUK_ASSERT(tv != NULL);
1997 ret = duk_js_touint16(thr, tv);
1998
1999 /* Relookup in case coerce_func() has side effects, e.g. ends up coercing an object */
2000 tv = duk_require_tval(ctx, index);
2001 DUK_TVAL_SET_FASTINT_U32_UPDREF(thr, tv, ret); /* side effects */
2002 return ret;
2003}
2004
2005#if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
2006/* Special coercion for Uint8ClampedArray. */
2008 duk_double_t d;
2009 duk_double_t t;
2010 duk_uint8_t ret;
2011
2012 /* XXX: Simplify this algorithm, should be possible to come up with
2013 * a shorter and faster algorithm by inspecting IEEE representation
2014 * directly.
2015 */
2016
2017 d = duk_to_number(ctx, index);
2018 if (d <= 0.0) {
2019 return 0;
2020 } else if (d >= 255) {
2021 return 255;
2022 } else if (DUK_ISNAN(d)) {
2023 /* Avoid NaN-to-integer coercion as it is compiler specific. */
2024 return 0;
2025 }
2026
2027 t = d - DUK_FLOOR(d);
2028 if (t == 0.5) {
2029 /* Exact halfway, round to even. */
2030 ret = (duk_uint8_t) d;
2031 ret = (ret + 1) & 0xfe; /* Example: d=3.5, t=0.5 -> ret = (3 + 1) & 0xfe = 4 & 0xfe = 4
2032 * Example: d=4.5, t=0.5 -> ret = (4 + 1) & 0xfe = 5 & 0xfe = 4
2033 */
2034 } else {
2035 /* Not halfway, round to nearest. */
2036 ret = (duk_uint8_t) (d + 0.5);
2037 }
2038 return ret;
2039}
2040#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
2041
2044
2045 (void) duk_to_string(ctx, index);
2046 return duk_require_lstring(ctx, index, out_len);
2047}
2048
2051
2052 duk_to_string(ctx, -1);
2053 return 1;
2054}
2055
2058
2060
2061 /* We intentionally ignore the duk_safe_call() return value and only
2062 * check the output type. This way we don't also need to check that
2063 * the returned value is indeed a string in the success case.
2064 */
2065
2066 duk_dup(ctx, index);
2067 (void) duk_safe_call(ctx, duk__safe_to_string_raw, 1 /*nargs*/, 1 /*nrets*/);
2068 if (!duk_is_string(ctx, -1)) {
2069 /* Error: try coercing error to string once. */
2070 (void) duk_safe_call(ctx, duk__safe_to_string_raw, 1 /*nargs*/, 1 /*nrets*/);
2071 if (!duk_is_string(ctx, -1)) {
2072 /* Double error */
2073 duk_pop(ctx);
2075 } else {
2076 ;
2077 }
2078 } else {
2079 ;
2080 }
2081 DUK_ASSERT(duk_is_string(ctx, -1));
2082 DUK_ASSERT(duk_get_string(ctx, -1) != NULL);
2083
2084 duk_replace(ctx, index);
2085 return duk_get_lstring(ctx, index, out_len);
2086}
2087
2088#if defined(DUK_USE_DEBUGGER_SUPPORT) /* only needed by debugger for now */
2089DUK_INTERNAL duk_hstring *duk_safe_to_hstring(duk_context *ctx, duk_idx_t index) {
2090 (void) duk_safe_to_string(ctx, index);
2093 return duk_get_hstring(ctx, index);
2094}
2095#endif
2096
2097/* Coerce top into Object.prototype.toString() output. */
2099 duk_hthread *thr;
2100 duk_uint_t typemask;
2101 duk_hstring *h_strclass;
2102
2104 thr = (duk_hthread *) ctx;
2105 DUK_UNREF(thr);
2106
2107 typemask = duk_get_type_mask(ctx, -1);
2108 if (typemask & DUK_TYPE_MASK_UNDEFINED) {
2109 h_strclass = DUK_HTHREAD_STRING_UC_UNDEFINED(thr);
2110 } else if (typemask & DUK_TYPE_MASK_NULL) {
2111 h_strclass = DUK_HTHREAD_STRING_UC_NULL(thr);
2112 } else {
2113 duk_hobject *h_obj;
2114
2115 duk_to_object(ctx, -1);
2116 h_obj = duk_get_hobject(ctx, -1);
2117 DUK_ASSERT(h_obj != NULL);
2118
2119 h_strclass = DUK_HOBJECT_GET_CLASS_STRING(thr->heap, h_obj);
2120 }
2121 DUK_ASSERT(h_strclass != NULL);
2122
2123 duk_pop(ctx);
2124 duk_push_sprintf(ctx, "[object %s]", (const char *) DUK_HSTRING_GET_DATA(h_strclass));
2125}
2126
2127#if !defined(DUK_USE_PARANOID_ERRORS)
2129 duk_hthread *thr;
2130 duk_hstring *h_strclass;
2131
2133 DUK_ASSERT(h != NULL);
2134 thr = (duk_hthread *) ctx;
2135 DUK_UNREF(thr);
2136
2137 h_strclass = DUK_HOBJECT_GET_CLASS_STRING(thr->heap, h);
2138 DUK_ASSERT(h_strclass != NULL);
2139 duk_push_sprintf(ctx, "[object %s]", (const char *) DUK_HSTRING_GET_DATA(h_strclass));
2140}
2141#endif /* !DUK_USE_PARANOID_ERRORS */
2142
2143/* XXX: other variants like uint, u32 etc */
2145 duk_hthread *thr = (duk_hthread *) ctx;
2146 duk_tval *tv;
2147 duk_tval tv_tmp;
2148 duk_double_t d, dmin, dmax;
2149 duk_int_t res;
2150 duk_bool_t clamped = 0;
2151
2153
2154 tv = duk_require_tval(ctx, index);
2155 DUK_ASSERT(tv != NULL);
2156 d = duk_js_tointeger(thr, tv); /* E5 Section 9.4, ToInteger() */
2157
2158 dmin = (duk_double_t) minval;
2159 dmax = (duk_double_t) maxval;
2160
2161 if (d < dmin) {
2162 clamped = 1;
2163 res = minval;
2164 d = dmin;
2165 } else if (d > dmax) {
2166 clamped = 1;
2167 res = maxval;
2168 d = dmax;
2169 } else {
2170 res = (duk_int_t) d;
2171 }
2172 DUK_UNREF(d); /* SCANBUILD: with suitable dmin/dmax limits 'd' is unused */
2173 /* 'd' and 'res' agree here */
2174
2175 /* Relookup in case duk_js_tointeger() ends up e.g. coercing an object. */
2176 tv = duk_get_tval(ctx, index);
2177 DUK_ASSERT(tv != NULL); /* not popped by side effect */
2178 DUK_TVAL_SET_TVAL(&tv_tmp, tv);
2179#if defined(DUK_USE_FASTINT)
2180#if (DUK_INT_MAX <= 0x7fffffffL)
2181 DUK_TVAL_SET_FASTINT_I32(tv, res);
2182#else
2183 /* Clamping needed if duk_int_t is 64 bits. */
2184 if (res >= DUK_FASTINT_MIN && res <= DUK_FASTINT_MAX) {
2185 DUK_TVAL_SET_FASTINT(tv, res);
2186 } else {
2187 DUK_TVAL_SET_NUMBER(tv, d);
2188 }
2189#endif
2190#else
2191 DUK_TVAL_SET_NUMBER(tv, d); /* no need to incref */
2192#endif
2193 DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
2194
2195 if (out_clamped) {
2196 *out_clamped = clamped;
2197 } else {
2198 /* coerced value is updated to value stack even when RangeError thrown */
2199 if (clamped) {
2201 }
2202 }
2203
2204 return res;
2205}
2206
2211
2213 return duk_to_int_clamped_raw(ctx, index, minval, maxval, NULL); /* out_clamped==NULL -> RangeError if outside range */
2214}
2215
2217 duk_hthread *thr = (duk_hthread *) ctx;
2218 duk_tval *tv;
2219
2221 DUK_UNREF(thr);
2222
2224
2225 tv = duk_require_tval(ctx, index);
2226 DUK_ASSERT(tv != NULL);
2227
2228 switch (DUK_TVAL_GET_TAG(tv)) {
2229 case DUK_TAG_UNDEFINED: {
2231 break;
2232 }
2233 case DUK_TAG_NULL: {
2235 break;
2236 }
2237 case DUK_TAG_BOOLEAN: {
2238 if (DUK_TVAL_GET_BOOLEAN(tv)) {
2240 } else {
2242 }
2243 break;
2244 }
2245 case DUK_TAG_STRING: {
2246 /* nop */
2247 goto skip_replace;
2248 }
2249 case DUK_TAG_OBJECT: {
2251 return duk_to_string(ctx, index); /* Note: recursive call */
2252 }
2253 case DUK_TAG_BUFFER: {
2255
2256 /* Note: this allows creation of internal strings. */
2257
2258 DUK_ASSERT(h != NULL);
2259 duk_push_lstring(ctx,
2260 (const char *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h),
2262 break;
2263 }
2264 case DUK_TAG_POINTER: {
2265 void *ptr = DUK_TVAL_GET_POINTER(tv);
2266 if (ptr != NULL) {
2267 duk_push_sprintf(ctx, DUK_STR_FMT_PTR, (void *) ptr);
2268 } else {
2269 /* Represent a null pointer as 'null' to be consistent with
2270 * the JX format variant. Native '%p' format for a NULL
2271 * pointer may be e.g. '(nil)'.
2272 */
2274 }
2275 break;
2276 }
2277 case DUK_TAG_LIGHTFUNC: {
2278 /* Should match Function.prototype.toString() */
2280 break;
2281 }
2282#if defined(DUK_USE_FASTINT)
2283 case DUK_TAG_FASTINT:
2284#endif
2285 default: {
2286 /* number */
2289 duk_push_tval(ctx, tv);
2291 10 /*radix*/,
2292 0 /*precision:shortest*/,
2293 0 /*force_exponential*/);
2294 break;
2295 }
2296 }
2297
2298 duk_replace(ctx, index);
2299
2300 skip_replace:
2301 return duk_require_string(ctx, index);
2302}
2303
2305 duk_hstring *ret;
2307 duk_to_string(ctx, index);
2308 ret = duk_get_hstring(ctx, index);
2309 DUK_ASSERT(ret != NULL);
2310 return ret;
2311}
2312
2314 duk_hthread *thr = (duk_hthread *) ctx;
2315 duk_hbuffer *h_buf;
2316 const duk_uint8_t *src_data;
2317 duk_size_t src_size;
2318 duk_uint8_t *dst_data;
2319
2321 DUK_UNREF(thr);
2322
2324
2325 h_buf = duk_get_hbuffer(ctx, index);
2326 if (h_buf != NULL) {
2327 /* Buffer is kept as is, with the fixed/dynamic nature of the
2328 * buffer only changed if requested. An external buffer
2329 * is converted into a non-external dynamic buffer in a
2330 * duk_to_dynamic_buffer() call.
2331 */
2332 duk_uint_t tmp;
2333 duk_uint8_t *tmp_ptr;
2334
2335 tmp_ptr = (duk_uint8_t *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h_buf);
2336 src_data = (const duk_uint8_t *) tmp_ptr;
2337 src_size = DUK_HBUFFER_GET_SIZE(h_buf);
2338
2340 if ((tmp == mode && !DUK_HBUFFER_HAS_EXTERNAL(h_buf)) ||
2341 mode == DUK_BUF_MODE_DONTCARE) {
2342 /* Note: src_data may be NULL if input is a zero-size
2343 * dynamic buffer.
2344 */
2345 dst_data = tmp_ptr;
2346 goto skip_copy;
2347 }
2348 } else {
2349 /* Non-buffer value is first ToString() coerced, then converted
2350 * to a buffer (fixed buffer is used unless a dynamic buffer is
2351 * explicitly requested).
2352 */
2353
2354 src_data = (const duk_uint8_t *) duk_to_lstring(ctx, index, &src_size);
2355 }
2356
2357 dst_data = (duk_uint8_t *) duk_push_buffer(ctx, src_size, (mode == DUK_BUF_MODE_DYNAMIC) /*dynamic*/);
2358 if (DUK_LIKELY(src_size > 0)) {
2359 /* When src_size == 0, src_data may be NULL (if source
2360 * buffer is dynamic), and dst_data may be NULL (if
2361 * target buffer is dynamic). Avoid zero-size memcpy()
2362 * with an invalid pointer.
2363 */
2364 DUK_MEMCPY((void *) dst_data, (const void *) src_data, (size_t) src_size);
2365 }
2366 duk_replace(ctx, index);
2367 skip_copy:
2368
2369 if (out_size) {
2370 *out_size = src_size;
2371 }
2372 return dst_data;
2373}
2374
2376 duk_tval *tv;
2377 void *res;
2378
2380
2382
2383 tv = duk_require_tval(ctx, index);
2384 DUK_ASSERT(tv != NULL);
2385
2386 switch (DUK_TVAL_GET_TAG(tv)) {
2387 case DUK_TAG_UNDEFINED:
2388 case DUK_TAG_NULL:
2389 case DUK_TAG_BOOLEAN:
2390 res = NULL;
2391 break;
2392 case DUK_TAG_POINTER:
2393 res = DUK_TVAL_GET_POINTER(tv);
2394 break;
2395 case DUK_TAG_STRING:
2396 case DUK_TAG_OBJECT:
2397 case DUK_TAG_BUFFER:
2398 /* Heap allocated: return heap pointer which is NOT useful
2399 * for the caller, except for debugging.
2400 */
2401 res = (void *) DUK_TVAL_GET_HEAPHDR(tv);
2402 break;
2403 case DUK_TAG_LIGHTFUNC:
2404 /* Function pointers do not always cast correctly to void *
2405 * (depends on memory and segmentation model for instance),
2406 * so they coerce to NULL.
2407 */
2408 res = NULL;
2409 break;
2410#if defined(DUK_USE_FASTINT)
2411 case DUK_TAG_FASTINT:
2412#endif
2413 default:
2414 /* number */
2417 res = NULL;
2418 break;
2419 }
2420
2421 duk_push_pointer(ctx, res);
2422 duk_replace(ctx, index);
2423 return res;
2424}
2425
2427 duk_hthread *thr = (duk_hthread *) ctx;
2428 duk_tval *tv;
2429 duk_uint_t flags = 0; /* shared flags for a subset of types */
2431
2433
2435
2436 tv = duk_require_tval(ctx, index);
2437 DUK_ASSERT(tv != NULL);
2438
2439 switch (DUK_TVAL_GET_TAG(tv)) {
2440 case DUK_TAG_UNDEFINED:
2441 case DUK_TAG_NULL: {
2443 break;
2444 }
2445 case DUK_TAG_BOOLEAN: {
2449 goto create_object;
2450 }
2451 case DUK_TAG_STRING: {
2456 goto create_object;
2457 }
2458 case DUK_TAG_OBJECT: {
2459 /* nop */
2460 break;
2461 }
2462 case DUK_TAG_BUFFER: {
2463 /* A plain buffer coerces to a Duktape.Buffer because it's the
2464 * object counterpart of the plain buffer value. But it might
2465 * still make more sense to produce an ArrayBuffer here?
2466 */
2467
2468 duk_hbufferobject *h_bufobj;
2469 duk_hbuffer *h_val;
2470
2471 h_val = DUK_TVAL_GET_BUFFER(tv);
2472 DUK_ASSERT(h_val != NULL);
2473
2474 h_bufobj = duk_push_bufferobject_raw(ctx,
2479 DUK_ASSERT(h_bufobj != NULL);
2482
2483 h_bufobj->buf = h_val;
2484 DUK_HBUFFER_INCREF(thr, h_val);
2485 DUK_ASSERT(h_bufobj->offset == 0);
2486 h_bufobj->length = (duk_uint_t) DUK_HBUFFER_GET_SIZE(h_val);
2487 DUK_ASSERT(h_bufobj->shift == 0);
2489
2491 goto replace_value;
2492 }
2493 case DUK_TAG_POINTER: {
2497 goto create_object;
2498 }
2499 case DUK_TAG_LIGHTFUNC: {
2500 /* Lightfunc coerces to a Function instance with concrete
2501 * properties. Since 'length' is virtual for Duktape/C
2502 * functions, don't need to define that.
2503 *
2504 * The result is made extensible to mimic what happens to
2505 * strings:
2506 * > Object.isExtensible(Object('foo'))
2507 * true
2508 */
2509 duk_small_uint_t lf_flags;
2510 duk_idx_t nargs;
2511 duk_small_uint_t lf_len;
2512 duk_c_function func;
2514
2515 DUK_TVAL_GET_LIGHTFUNC(tv, func, lf_flags);
2516
2517 nargs = (duk_idx_t) DUK_LFUNC_FLAGS_GET_NARGS(lf_flags);
2518 if (nargs == DUK_LFUNC_NARGS_VARARGS) {
2519 nargs = (duk_idx_t) DUK_VARARGS;
2520 }
2527 /* DUK_HOBJECT_FLAG_EXOTIC_DUKFUNC: omitted here intentionally */
2529 (void) duk__push_c_function_raw(ctx, func, nargs, flags);
2530
2531 lf_len = DUK_LFUNC_FLAGS_GET_LENGTH(lf_flags);
2532 if ((duk_idx_t) lf_len != nargs) {
2533 /* Explicit length is only needed if it differs from 'nargs'. */
2534 duk_push_int(ctx, (duk_int_t) lf_len);
2536 }
2537 duk_push_lightfunc_name(ctx, tv);
2539
2540 nf = duk_get_hnativefunction(ctx, -1);
2541 DUK_ASSERT(nf != NULL);
2542 nf->magic = (duk_int16_t) DUK_LFUNC_FLAGS_GET_MAGIC(lf_flags);
2543
2544 /* Enable DUKFUNC exotic behavior once properties are set up. */
2546 goto replace_value;
2547 }
2548#if defined(DUK_USE_FASTINT)
2549 case DUK_TAG_FASTINT:
2550#endif
2551 default: {
2557 goto create_object;
2558 }
2559 }
2560 return;
2561
2562 create_object:
2563 (void) duk_push_object_helper(ctx, flags, proto);
2564
2565 /* Note: Boolean prototype's internal value property is not writable,
2566 * but duk_xdef_prop_stridx() disregards the write protection. Boolean
2567 * instances are immutable.
2568 *
2569 * String and buffer special behaviors are already enabled which is not
2570 * ideal, but a write to the internal value is not affected by them.
2571 */
2572 duk_dup(ctx, index);
2574
2575 replace_value:
2576 duk_replace(ctx, index);
2577}
2578
2579/*
2580 * Type checking
2581 */
2582
2584 duk_tval *tv;
2585
2586 tv = duk_get_tval(ctx, index);
2587 if (!tv) {
2588 return 0;
2589 }
2590 return (DUK_TVAL_GET_TAG(tv) == tag);
2591}
2592
2594 duk_hobject *obj;
2595
2597
2598 obj = duk_get_hobject(ctx, index);
2599 if (obj) {
2600 return (DUK_HEAPHDR_CHECK_FLAG_BITS((duk_heaphdr *) obj, flag_mask) ? 1 : 0);
2601 }
2602 return 0;
2603}
2604
2606 duk_tval *tv;
2607
2609
2610 tv = duk_get_tval(ctx, index);
2611 if (!tv) {
2612 return DUK_TYPE_NONE;
2613 }
2614 switch (DUK_TVAL_GET_TAG(tv)) {
2615 case DUK_TAG_UNDEFINED:
2616 return DUK_TYPE_UNDEFINED;
2617 case DUK_TAG_NULL:
2618 return DUK_TYPE_NULL;
2619 case DUK_TAG_BOOLEAN:
2620 return DUK_TYPE_BOOLEAN;
2621 case DUK_TAG_STRING:
2622 return DUK_TYPE_STRING;
2623 case DUK_TAG_OBJECT:
2624 return DUK_TYPE_OBJECT;
2625 case DUK_TAG_BUFFER:
2626 return DUK_TYPE_BUFFER;
2627 case DUK_TAG_POINTER:
2628 return DUK_TYPE_POINTER;
2629 case DUK_TAG_LIGHTFUNC:
2630 return DUK_TYPE_LIGHTFUNC;
2631#if defined(DUK_USE_FASTINT)
2632 case DUK_TAG_FASTINT:
2633#endif
2634 default:
2635 /* Note: number has no explicit tag (in 8-byte representation) */
2638 return DUK_TYPE_NUMBER;
2639 }
2641}
2642
2643#if defined(DUK_USE_VERBOSE_ERRORS) && defined(DUK_USE_PARANOID_ERRORS)
2644DUK_LOCAL const char *duk__type_names[] = {
2645 "none",
2646 "undefined",
2647 "null",
2648 "boolean",
2649 "number",
2650 "string",
2651 "object",
2652 "buffer",
2653 "pointer",
2654 "lightfunc"
2655};
2656
2657DUK_INTERNAL const char *duk_get_type_name(duk_context *ctx, duk_idx_t index) {
2658 duk_int_t type_tag;
2659
2660 type_tag = duk_get_type(ctx, index);
2661 DUK_ASSERT(type_tag >= DUK_TYPE_MIN && type_tag <= DUK_TYPE_MAX);
2662 DUK_ASSERT(DUK_TYPE_MIN == 0 && sizeof(duk__type_names) / sizeof(const char *) == DUK_TYPE_MAX + 1);
2663
2664 return duk__type_names[type_tag];
2665}
2666#endif
2667
2670
2671 return (duk_get_type(ctx, index) == type) ? 1 : 0;
2672}
2673
2675 duk_tval *tv;
2676
2678
2679 tv = duk_get_tval(ctx, index);
2680 if (!tv) {
2681 return DUK_TYPE_MASK_NONE;
2682 }
2683 switch (DUK_TVAL_GET_TAG(tv)) {
2684 case DUK_TAG_UNDEFINED:
2686 case DUK_TAG_NULL:
2687 return DUK_TYPE_MASK_NULL;
2688 case DUK_TAG_BOOLEAN:
2689 return DUK_TYPE_MASK_BOOLEAN;
2690 case DUK_TAG_STRING:
2691 return DUK_TYPE_MASK_STRING;
2692 case DUK_TAG_OBJECT:
2693 return DUK_TYPE_MASK_OBJECT;
2694 case DUK_TAG_BUFFER:
2695 return DUK_TYPE_MASK_BUFFER;
2696 case DUK_TAG_POINTER:
2697 return DUK_TYPE_MASK_POINTER;
2698 case DUK_TAG_LIGHTFUNC:
2700#if defined(DUK_USE_FASTINT)
2701 case DUK_TAG_FASTINT:
2702#endif
2703 default:
2704 /* Note: number has no explicit tag (in 8-byte representation) */
2707 return DUK_TYPE_MASK_NUMBER;
2708 }
2710}
2711
2713 duk_hthread *thr = (duk_hthread *) ctx;
2714
2716
2717 if (duk_get_type_mask(ctx, index) & mask) {
2718 return 1;
2719 }
2720 if (mask & DUK_TYPE_MASK_THROW) {
2723 }
2724 return 0;
2725}
2726
2731
2736
2738 duk_tval *tv;
2739 duk_small_uint_t tag;
2740
2742
2743 tv = duk_get_tval(ctx, index);
2744 if (!tv) {
2745 return 0;
2746 }
2747 tag = DUK_TVAL_GET_TAG(tv);
2748 return (tag == DUK_TAG_UNDEFINED) || (tag == DUK_TAG_NULL);
2749}
2750
2755
2757 duk_tval *tv;
2758
2760
2761 /*
2762 * Number is special because it doesn't have a specific
2763 * tag in the 8-byte representation.
2764 */
2765
2766 /* XXX: shorter version for 12-byte representation? */
2767
2768 tv = duk_get_tval(ctx, index);
2769 if (!tv) {
2770 return 0;
2771 }
2772 return DUK_TVAL_IS_NUMBER(tv);
2773}
2774
2776 /* XXX: This will now return false for non-numbers, even though they would
2777 * coerce to NaN (as a general rule). In particular, duk_get_number()
2778 * returns a NaN for non-numbers, so should this function also return
2779 * true for non-numbers?
2780 */
2781
2782 duk_tval *tv;
2783
2785
2786 tv = duk_get_tval(ctx, index);
2787 if (!tv || !DUK_TVAL_IS_NUMBER(tv)) {
2788 return 0;
2789 }
2790 return DUK_ISNAN(DUK_TVAL_GET_NUMBER(tv));
2791}
2792
2797
2802
2807
2812
2817
2819 duk_hobject *obj;
2820
2822
2823 obj = duk_get_hobject(ctx, index);
2824 if (obj) {
2826 }
2827 return 0;
2828}
2829
2845
2852
2859
2866
2873
2875 duk_tval *tv;
2876
2878
2879 tv = duk_get_tval(ctx, index);
2880 if (tv && DUK_TVAL_IS_BUFFER(tv)) {
2882 DUK_ASSERT(h != NULL);
2883 return (DUK_HBUFFER_HAS_DYNAMIC(h) ? 0 : 1);
2884 }
2885 return 0;
2886}
2887
2889 duk_tval *tv;
2890
2892
2893 tv = duk_get_tval(ctx, index);
2894 if (tv && DUK_TVAL_IS_BUFFER(tv)) {
2896 DUK_ASSERT(h != NULL);
2897 return (DUK_HBUFFER_HAS_DYNAMIC(h) && !DUK_HBUFFER_HAS_EXTERNAL(h) ? 1 : 0);
2898 }
2899 return 0;
2900}
2901
2903 duk_tval *tv;
2904
2906
2907 tv = duk_get_tval(ctx, index);
2908 if (tv && DUK_TVAL_IS_BUFFER(tv)) {
2910 DUK_ASSERT(h != NULL);
2911 return (DUK_HBUFFER_HAS_DYNAMIC(h) && DUK_HBUFFER_HAS_EXTERNAL(h) ? 1 : 0);
2912 }
2913 return 0;
2914}
2915
2917 duk_hthread *thr = (duk_hthread *) ctx;
2918 duk_hobject *h;
2919 duk_uint_t sanity;
2920
2922
2923 h = duk_get_hobject(ctx, index);
2924
2926 do {
2927 if (!h) {
2928 return DUK_ERR_NONE;
2929 }
2930 if (h == thr->builtins[DUK_BIDX_EVAL_ERROR_PROTOTYPE]) {
2931 return DUK_ERR_EVAL_ERROR;
2932 }
2934 return DUK_ERR_RANGE_ERROR;
2935 }
2938 }
2940 return DUK_ERR_SYNTAX_ERROR;
2941 }
2942 if (h == thr->builtins[DUK_BIDX_TYPE_ERROR_PROTOTYPE]) {
2943 return DUK_ERR_TYPE_ERROR;
2944 }
2945 if (h == thr->builtins[DUK_BIDX_URI_ERROR_PROTOTYPE]) {
2946 return DUK_ERR_URI_ERROR;
2947 }
2948 if (h == thr->builtins[DUK_BIDX_ERROR_PROTOTYPE]) {
2949 return DUK_ERR_ERROR;
2950 }
2951
2952 h = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, h);
2953 } while (--sanity > 0);
2954
2955 return DUK_ERR_NONE;
2956}
2957
2958/*
2959 * Pushers
2960 */
2961
2963 duk_hthread *thr;
2964 duk_tval *tv_slot;
2965
2967 DUK_ASSERT(tv != NULL);
2968 thr = (duk_hthread *) ctx;
2970 tv_slot = thr->valstack_top++;
2971 DUK_TVAL_SET_TVAL(tv_slot, tv);
2972 DUK_TVAL_INCREF(thr, tv); /* no side effects */
2973}
2974
2976 duk_hthread *thr;
2977
2979 thr = (duk_hthread *) ctx;
2981
2982 /* Because value stack init policy is 'undefined above top',
2983 * we don't need to write, just assert.
2984 */
2985 thr->valstack_top++;
2987}
2988
2990 duk_hthread *thr;
2991 duk_tval *tv_slot;
2992
2994 thr = (duk_hthread *) ctx;
2996 tv_slot = thr->valstack_top++;
2997 DUK_TVAL_SET_NULL(tv_slot);
2998}
2999
3001 duk_hthread *thr;
3002 duk_tval *tv_slot;
3004
3006 thr = (duk_hthread *) ctx;
3008 b = (val ? 1 : 0); /* ensure value is 1 or 0 (not other non-zero) */
3009 tv_slot = thr->valstack_top++;
3010 DUK_TVAL_SET_BOOLEAN(tv_slot, b);
3011}
3012
3014 duk_hthread *thr;
3015 duk_tval *tv_slot;
3016
3018 thr = (duk_hthread *) ctx;
3020 tv_slot = thr->valstack_top++;
3022}
3023
3025 duk_hthread *thr;
3026 duk_tval *tv_slot;
3027
3029 thr = (duk_hthread *) ctx;
3031 tv_slot = thr->valstack_top++;
3033}
3034
3035/* normalize NaN which may not match our canonical internal NaN */
3037 duk_hthread *thr;
3038 duk_tval *tv_slot;
3040
3042 thr = (duk_hthread *) ctx;
3044 du.d = val;
3046 tv_slot = thr->valstack_top++;
3047 DUK_TVAL_SET_NUMBER(tv_slot, du.d);
3048}
3049
3051#if defined(DUK_USE_FASTINT)
3052 duk_hthread *thr;
3053 duk_tval *tv_slot;
3054
3056 thr = (duk_hthread *) ctx;
3058 tv_slot = thr->valstack_top++;
3059#if DUK_INT_MAX <= 0x7fffffffL
3060 DUK_TVAL_SET_FASTINT_I32(tv_slot, (duk_int32_t) val);
3061#else
3062 if (val >= DUK_FASTINT_MIN && val <= DUK_FASTINT_MAX) {
3063 DUK_TVAL_SET_FASTINT(tv_slot, (duk_int64_t) val);
3064 } else {
3065 duk_double_t = (duk_double_t) val;
3066 DUK_TVAL_SET_NUMBER(tv_slot, d);
3067 }
3068#endif
3069#else /* DUK_USE_FASTINT */
3070 duk_hthread *thr;
3071 duk_tval *tv_slot;
3072 duk_double_t d;
3073
3075 thr = (duk_hthread *) ctx;
3077 d = (duk_double_t) val;
3078 tv_slot = thr->valstack_top++;
3079 DUK_TVAL_SET_NUMBER(tv_slot, d);
3080#endif /* DUK_USE_FASTINT */
3081}
3082
3084#if defined(DUK_USE_FASTINT)
3085 duk_hthread *thr;
3086 duk_tval *tv_slot;
3087
3089 thr = (duk_hthread *) ctx;
3091 tv_slot = thr->valstack_top++;
3092#if DUK_UINT_MAX <= 0xffffffffUL
3093 DUK_TVAL_SET_FASTINT_U32(tv_slot, (duk_uint32_t) val);
3094#else
3095 if (val <= DUK_FASTINT_MAX) { /* val is unsigned so >= 0 */
3096 /* XXX: take advantage of val being unsigned, no need to mask */
3097 DUK_TVAL_SET_FASTINT(tv_slot, (duk_int64_t) val);
3098 } else {
3099 duk_double_t = (duk_double_t) val;
3100 DUK_TVAL_SET_NUMBER(tv_slot, d);
3101 }
3102#endif
3103#else /* DUK_USE_FASTINT */
3104 duk_hthread *thr;
3105 duk_tval *tv_slot;
3106 duk_double_t d;
3107
3109 thr = (duk_hthread *) ctx;
3111 d = (duk_double_t) val;
3112 tv_slot = thr->valstack_top++;
3113 DUK_TVAL_SET_NUMBER(tv_slot, d);
3114#endif /* DUK_USE_FASTINT */
3115}
3116
3118 duk_hthread *thr;
3119 duk_tval *tv_slot;
3121
3123 thr = (duk_hthread *) ctx;
3127 tv_slot = thr->valstack_top++;
3128 DUK_TVAL_SET_NUMBER(tv_slot, du.d);
3129}
3130
3131DUK_EXTERNAL const char *duk_push_lstring(duk_context *ctx, const char *str, duk_size_t len) {
3132 duk_hthread *thr = (duk_hthread *) ctx;
3133 duk_hstring *h;
3134 duk_tval *tv_slot;
3135
3137
3138 /* check stack before interning (avoid hanging temp) */
3139 if (thr->valstack_top >= thr->valstack_end) {
3141 }
3142
3143 /* NULL with zero length represents an empty string; NULL with higher
3144 * length is also now trated like an empty string although it is
3145 * a bit dubious. This is unlike duk_push_string() which pushes a
3146 * 'null' if the input string is a NULL.
3147 */
3148 if (!str) {
3149 len = 0;
3150 }
3151
3152 /* Check for maximum string length */
3153 if (len > DUK_HSTRING_MAX_BYTELEN) {
3155 }
3156
3157 h = duk_heap_string_intern_checked(thr, (const duk_uint8_t *) str, (duk_uint32_t) len);
3158 DUK_ASSERT(h != NULL);
3159
3160 tv_slot = thr->valstack_top++;
3161 DUK_TVAL_SET_STRING(tv_slot, h);
3162 DUK_HSTRING_INCREF(thr, h); /* no side effects */
3163
3164 return (const char *) DUK_HSTRING_GET_DATA(h);
3165}
3166
3167DUK_EXTERNAL const char *duk_push_string(duk_context *ctx, const char *str) {
3169
3170 if (str) {
3171 return duk_push_lstring(ctx, str, DUK_STRLEN(str));
3172 } else {
3173 duk_push_null(ctx);
3174 return NULL;
3175 }
3176}
3177
3178#ifdef DUK_USE_FILE_IO
3179/* This is a bit clunky because it is ANSI C portable. Should perhaps
3180 * relocate to another file because this is potentially platform
3181 * dependent.
3182 */
3183DUK_EXTERNAL const char *duk_push_string_file_raw(duk_context *ctx, const char *path, duk_uint_t flags) {
3184 duk_hthread *thr = (duk_hthread *) ctx;
3185 duk_file *f = NULL;
3186 char *buf;
3187 long sz; /* ANSI C typing */
3188
3190
3191 if (!path) {
3192 goto fail;
3193 }
3194 f = DUK_FOPEN(path, "rb");
3195 if (!f) {
3196 goto fail;
3197 }
3198 if (DUK_FSEEK(f, 0, SEEK_END) < 0) {
3199 goto fail;
3200 }
3201 sz = DUK_FTELL(f);
3202 if (sz < 0) {
3203 goto fail;
3204 }
3205 if (DUK_FSEEK(f, 0, SEEK_SET) < 0) {
3206 goto fail;
3207 }
3208 buf = (char *) duk_push_fixed_buffer(ctx, (duk_size_t) sz);
3209 DUK_ASSERT(buf != NULL);
3210 if ((duk_size_t) DUK_FREAD(buf, 1, (size_t) sz, f) != (duk_size_t) sz) {
3211 goto fail;
3212 }
3213 (void) DUK_FCLOSE(f); /* ignore fclose() error */
3214 f = NULL;
3215 return duk_to_string(ctx, -1);
3216
3217 fail:
3218 if (f) {
3219 DUK_FCLOSE(f);
3220 }
3221
3222 if (flags != 0) {
3223 DUK_ASSERT(flags == DUK_STRING_PUSH_SAFE); /* only flag now */
3224 duk_push_undefined(ctx);
3225 } else {
3226 /* XXX: string not shared because it is conditional */
3227 DUK_ERROR_TYPE(thr, "read file error");
3228 }
3229 return NULL;
3230}
3231#else
3232DUK_EXTERNAL const char *duk_push_string_file_raw(duk_context *ctx, const char *path, duk_uint_t flags) {
3233 duk_hthread *thr = (duk_hthread *) ctx;
3235 DUK_UNREF(path);
3236
3237 if (flags != 0) {
3238 DUK_ASSERT(flags == DUK_STRING_PUSH_SAFE); /* only flag now */
3239 duk_push_undefined(ctx);
3240 } else {
3241 /* XXX: string not shared because it is conditional */
3242 DUK_ERROR_UNSUPPORTED(thr, "file I/O disabled");
3243 }
3244 return NULL;
3245}
3246#endif /* DUK_USE_FILE_IO */
3247
3249 duk_hthread *thr;
3250 duk_tval *tv_slot;
3251
3253 thr = (duk_hthread *) ctx;
3255 tv_slot = thr->valstack_top++;
3256 DUK_TVAL_SET_POINTER(tv_slot, val);
3257}
3258
3260 duk_hthread *thr;
3261 duk_tval *tv_slot;
3262
3264 DUK_ASSERT_DISABLE(thr->callstack_top >= 0); /* avoid warning (unsigned) */
3265 thr = (duk_hthread *) ctx;
3268
3269 DUK_ASSERT(DUK_TVAL_IS_UNDEFINED(thr->valstack_top)); /* because of valstack init policy */
3270 tv_slot = thr->valstack_top++;
3271
3272 if (DUK_UNLIKELY(thr->callstack_top == 0)) {
3273 if (check_object_coercible) {
3274 goto type_error;
3275 }
3276 /* 'undefined' already on stack top */
3277 } else {
3278 duk_tval *tv;
3279
3280 /* 'this' binding is just before current activation's bottom */
3281 DUK_ASSERT(thr->valstack_bottom > thr->valstack);
3282 tv = thr->valstack_bottom - 1;
3283 if (check_object_coercible &&
3285 /* XXX: better macro for DUK_TVAL_IS_UNDEFINED_OR_NULL(tv) */
3286 goto type_error;
3287 }
3288
3289 DUK_TVAL_SET_TVAL(tv_slot, tv);
3290 DUK_TVAL_INCREF(thr, tv);
3291 }
3292 return;
3293
3294 type_error:
3296}
3297
3300
3301 duk__push_this_helper(ctx, 0 /*check_object_coercible*/);
3302}
3303
3306
3307 duk__push_this_helper(ctx, 1 /*check_object_coercible*/);
3308}
3309
3311 duk_hobject *h;
3312
3314
3315 duk__push_this_helper(ctx, 1 /*check_object_coercible*/);
3316 duk_to_object(ctx, -1);
3317 h = duk_get_hobject(ctx, -1);
3318 DUK_ASSERT(h != NULL);
3319 return h;
3320}
3321
3323 duk_hstring *h;
3324
3326
3327 duk__push_this_helper(ctx, 1 /*check_object_coercible*/);
3328 duk_to_string(ctx, -1);
3329 h = duk_get_hstring(ctx, -1);
3330 DUK_ASSERT(h != NULL);
3331 return h;
3332}
3333
3335 duk_hthread *thr;
3336
3337 DUK_ASSERT(ctx != NULL);
3338 thr = (duk_hthread *) ctx;
3339
3340 DUK_ASSERT(thr->callstack_top > 0); /* caller required to know */
3341 DUK_ASSERT(thr->valstack_bottom > thr->valstack); /* consequence of above */
3342 DUK_ASSERT(thr->valstack_bottom - 1 >= thr->valstack); /* 'this' binding exists */
3343
3344 return thr->valstack_bottom - 1;
3345}
3346
3348 duk_hthread *thr = (duk_hthread *) ctx;
3349 duk_activation *act;
3350
3352 DUK_ASSERT(thr != NULL);
3355
3357 if (act) {
3358 duk_push_tval(ctx, &act->tv_func);
3359 } else {
3360 duk_push_undefined(ctx);
3361 }
3362}
3363
3365 duk_hthread *thr = (duk_hthread *) ctx;
3366
3368 DUK_ASSERT(thr != NULL);
3369
3370 if (thr->heap->curr_thread) {
3372 } else {
3373 duk_push_undefined(ctx);
3374 }
3375}
3376
3382
3383/* XXX: size optimize */
3387 DUK_DDD(DUK_DDDPRINT("creating heap/global/thread stash on first use"));
3388 duk_pop(ctx);
3390 duk_dup_top(ctx);
3391 duk_xdef_prop_stridx(ctx, -3, DUK_STRIDX_INT_VALUE, DUK_PROPDESC_FLAGS_C); /* [ ... parent stash stash ] -> [ ... parent stash ] */
3392 }
3393 duk_remove(ctx, -2);
3394}
3395
3397 duk_hthread *thr = (duk_hthread *) ctx;
3398 duk_heap *heap;
3400 heap = thr->heap;
3401 DUK_ASSERT(heap->heap_object != NULL);
3402 duk_push_hobject(ctx, heap->heap_object);
3403 duk__push_stash(ctx);
3404}
3405
3411
3413 duk_hthread *thr = (duk_hthread *) ctx;
3415 if (!target_ctx) {
3417 return; /* not reached */
3418 }
3419 duk_push_hobject(ctx, (duk_hobject *) target_ctx);
3420 duk__push_stash(ctx);
3421}
3422
3423/* XXX: duk_ssize_t would be useful here */
3424DUK_LOCAL duk_int_t duk__try_push_vsprintf(duk_context *ctx, void *buf, duk_size_t sz, const char *fmt, va_list ap) {
3425 duk_int_t len;
3426
3428 DUK_UNREF(ctx);
3429
3430 /* NUL terminator handling doesn't matter here */
3431 len = DUK_VSNPRINTF((char *) buf, sz, fmt, ap);
3432 if (len < (duk_int_t) sz) {
3433 /* Return value of 'sz' or more indicates output was (potentially)
3434 * truncated.
3435 */
3436 return (duk_int_t) len;
3437 }
3438 return -1;
3439}
3440
3441DUK_EXTERNAL const char *duk_push_vsprintf(duk_context *ctx, const char *fmt, va_list ap) {
3442 duk_hthread *thr = (duk_hthread *) ctx;
3443 duk_uint8_t stack_buf[DUK_PUSH_SPRINTF_INITIAL_SIZE];
3445 duk_bool_t pushed_buf = 0;
3446 void *buf;
3447 duk_int_t len; /* XXX: duk_ssize_t */
3448 const char *res;
3449
3451
3452 /* special handling of fmt==NULL */
3453 if (!fmt) {
3454 duk_hstring *h_str;
3456 h_str = DUK_HTHREAD_STRING_EMPTY_STRING(thr); /* rely on interning, must be this string */
3457 return (const char *) DUK_HSTRING_GET_DATA(h_str);
3458 }
3459
3460 /* initial estimate based on format string */
3461 sz = DUK_STRLEN(fmt) + 16; /* format plus something to avoid just missing */
3464 }
3465 DUK_ASSERT(sz > 0);
3466
3467 /* Try to make do with a stack buffer to avoid allocating a temporary buffer.
3468 * This works 99% of the time which is quite nice.
3469 */
3470 for (;;) {
3471 va_list ap_copy; /* copied so that 'ap' can be reused */
3472
3473 if (sz <= sizeof(stack_buf)) {
3474 buf = stack_buf;
3475 } else if (!pushed_buf) {
3476 pushed_buf = 1;
3477 buf = duk_push_dynamic_buffer(ctx, sz);
3478 } else {
3479 buf = duk_resize_buffer(ctx, -1, sz);
3480 }
3481 DUK_ASSERT(buf != NULL);
3482
3483 DUK_VA_COPY(ap_copy, ap);
3484 len = duk__try_push_vsprintf(ctx, buf, sz, fmt, ap_copy);
3485 va_end(ap_copy);
3486 if (len >= 0) {
3487 break;
3488 }
3489
3490 /* failed, resize and try again */
3491 sz = sz * 2;
3494 }
3495 }
3496
3497 /* Cannot use duk_to_string() on the buffer because it is usually
3498 * larger than 'len'. Also, 'buf' is usually a stack buffer.
3499 */
3500 res = duk_push_lstring(ctx, (const char *) buf, (duk_size_t) len); /* [ buf? res ] */
3501 if (pushed_buf) {
3502 duk_remove(ctx, -2);
3503 }
3504 return res;
3505}
3506
3507DUK_EXTERNAL const char *duk_push_sprintf(duk_context *ctx, const char *fmt, ...) {
3508 va_list ap;
3509 const char *ret;
3510
3512
3513 /* allow fmt==NULL */
3514 va_start(ap, fmt);
3515 ret = duk_push_vsprintf(ctx, fmt, ap);
3516 va_end(ap);
3517
3518 return ret;
3519}
3520
3522 duk_hthread *thr = (duk_hthread *) ctx;
3523 duk_tval *tv_slot;
3524 duk_hobject *h;
3525 duk_idx_t ret;
3526
3528 DUK_ASSERT(prototype_bidx == -1 ||
3529 (prototype_bidx >= 0 && prototype_bidx < DUK_NUM_BUILTINS));
3530
3531 /* check stack first */
3532 if (thr->valstack_top >= thr->valstack_end) {
3534 }
3535
3536 h = duk_hobject_alloc(thr->heap, hobject_flags_and_class);
3537 if (!h) {
3539 }
3540
3541 DUK_DDD(DUK_DDDPRINT("created object with flags: 0x%08lx", (unsigned long) h->hdr.h_flags));
3542
3543 tv_slot = thr->valstack_top;
3544 DUK_TVAL_SET_OBJECT(tv_slot, h);
3545 DUK_HOBJECT_INCREF(thr, h); /* no side effects */
3546 ret = (duk_idx_t) (thr->valstack_top - thr->valstack_bottom);
3547 thr->valstack_top++;
3548
3549 /* object is now reachable */
3550
3551 if (prototype_bidx >= 0) {
3552 DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, h, thr->builtins[prototype_bidx]);
3553 } else {
3554 DUK_ASSERT(prototype_bidx == -1);
3556 }
3557
3558 return ret;
3559}
3560
3562 duk_hthread *thr = (duk_hthread *) ctx;
3563 duk_idx_t ret;
3564 duk_hobject *h;
3565
3567
3568 ret = duk_push_object_helper(ctx, hobject_flags_and_class, -1);
3569 h = duk_get_hobject(ctx, -1);
3570 DUK_ASSERT(h != NULL);
3573 return ret;
3574}
3575
3584
3586 duk_hthread *thr = (duk_hthread *) ctx;
3587 duk_hobject *obj;
3588 duk_idx_t ret;
3589
3591
3592 ret = duk_push_object_helper(ctx,
3597
3598 obj = duk_require_hobject(ctx, ret);
3599
3600 /*
3601 * An array must have a 'length' property (E5 Section 15.4.5.2).
3602 * The special array behavior flag must only be enabled once the
3603 * length property has been added.
3604 *
3605 * The internal property must be a number (and preferably a
3606 * fastint if fastint support is enabled).
3607 */
3608
3609 duk_push_int(ctx, 0);
3610#if defined(DUK_USE_FASTINT)
3611 DUK_ASSERT(DUK_TVAL_IS_FASTINT(duk_require_tval(ctx, -1)));
3612#endif
3613
3615 obj,
3619
3620 return ret;
3621}
3622
3624 duk_hthread *thr = (duk_hthread *) ctx;
3625 duk_hthread *obj;
3626 duk_idx_t ret;
3627 duk_tval *tv_slot;
3628
3630
3631 /* check stack first */
3632 if (thr->valstack_top >= thr->valstack_end) {
3634 }
3635
3636 obj = duk_hthread_alloc(thr->heap,
3640 if (!obj) {
3642 }
3644#if defined(DUK_USE_ROM_STRINGS)
3645 /* Nothing to initialize, strs[] is in ROM. */
3646#else
3647#if defined(DUK_USE_HEAPPTR16)
3648 obj->strs16 = thr->strs16;
3649#else
3650 obj->strs = thr->strs;
3651#endif
3652#endif
3653 DUK_DDD(DUK_DDDPRINT("created thread object with flags: 0x%08lx", (unsigned long) obj->obj.hdr.h_flags));
3654
3655 /* make the new thread reachable */
3656 tv_slot = thr->valstack_top;
3657 DUK_TVAL_SET_OBJECT(tv_slot, (duk_hobject *) obj);
3658 DUK_HTHREAD_INCREF(thr, obj);
3659 ret = (duk_idx_t) (thr->valstack_top - thr->valstack_bottom);
3660 thr->valstack_top++;
3661
3662 /* important to do this *after* pushing, to make the thread reachable for gc */
3663 if (!duk_hthread_init_stacks(thr->heap, obj)) {
3665 }
3666
3667 /* initialize built-ins - either by copying or creating new ones */
3668 if (flags & DUK_THREAD_NEW_GLOBAL_ENV) {
3670 } else {
3672 }
3673
3674 /* default prototype (Note: 'obj' must be reachable) */
3676
3677 /* Initial stack size satisfies the stack spare constraints so there
3678 * is no need to require stack here.
3679 */
3682
3683 return ret;
3684}
3685
3687 duk_hthread *thr = (duk_hthread *) ctx;
3689 duk_idx_t ret;
3690 duk_tval *tv_slot;
3691
3693
3694 /* check stack first */
3695 if (thr->valstack_top >= thr->valstack_end) {
3697 }
3698
3699 /* Template functions are not strictly constructable (they don't
3700 * have a "prototype" property for instance), so leave the
3701 * DUK_HOBJECT_FLAG_CONSRUCTABLE flag cleared here.
3702 */
3703
3708 if (!obj) {
3710 }
3711
3712 DUK_DDD(DUK_DDDPRINT("created compiled function object with flags: 0x%08lx", (unsigned long) obj->obj.hdr.h_flags));
3713
3714 tv_slot = thr->valstack_top;
3715 DUK_TVAL_SET_OBJECT(tv_slot, (duk_hobject *) obj);
3716 DUK_HOBJECT_INCREF(thr, obj);
3717 ret = (duk_idx_t) (thr->valstack_top - thr->valstack_bottom);
3718 thr->valstack_top++;
3719
3720 /* default prototype (Note: 'obj' must be reachable) */
3722
3723 return ret;
3724}
3725
3727 duk_hthread *thr = (duk_hthread *) ctx;
3729 duk_idx_t ret;
3730 duk_tval *tv_slot;
3731 duk_int16_t func_nargs;
3732
3734
3735 /* check stack first */
3736 if (thr->valstack_top >= thr->valstack_end) {
3738 }
3739 if (func == NULL) {
3740 goto api_error;
3741 }
3742 if (nargs >= 0 && nargs < DUK_HNATIVEFUNCTION_NARGS_MAX) {
3743 func_nargs = (duk_int16_t) nargs;
3744 } else if (nargs == DUK_VARARGS) {
3746 } else {
3747 goto api_error;
3748 }
3749
3750 obj = duk_hnativefunction_alloc(thr->heap, flags);
3751 if (!obj) {
3753 }
3754
3755 obj->func = func;
3756 obj->nargs = func_nargs;
3757
3758 DUK_DDD(DUK_DDDPRINT("created native function object with flags: 0x%08lx, nargs=%ld",
3759 (unsigned long) obj->obj.hdr.h_flags, (long) obj->nargs));
3760
3761 tv_slot = thr->valstack_top;
3762 DUK_TVAL_SET_OBJECT(tv_slot, (duk_hobject *) obj);
3763 DUK_HOBJECT_INCREF(thr, obj);
3764 ret = (duk_idx_t) (thr->valstack_top - thr->valstack_bottom);
3765 thr->valstack_top++;
3766
3767 /* default prototype (Note: 'obj' must be reachable) */
3769
3770 return ret;
3771
3772 api_error:
3774 return 0; /* not reached */
3775}
3776
3793
3809
3824
3826 duk_hthread *thr = (duk_hthread *) ctx;
3827 duk_tval tv_tmp;
3828 duk_small_uint_t lf_flags;
3829
3831
3832 /* check stack first */
3833 if (thr->valstack_top >= thr->valstack_end) {
3835 }
3836
3837 if (nargs >= DUK_LFUNC_NARGS_MIN && nargs <= DUK_LFUNC_NARGS_MAX) {
3838 /* as is */
3839 } else if (nargs == DUK_VARARGS) {
3841 } else {
3842 goto api_error;
3843 }
3844 if (!(length >= DUK_LFUNC_LENGTH_MIN && length <= DUK_LFUNC_LENGTH_MAX)) {
3845 goto api_error;
3846 }
3847 if (!(magic >= DUK_LFUNC_MAGIC_MIN && magic <= DUK_LFUNC_MAGIC_MAX)) {
3848 goto api_error;
3849 }
3850
3851 lf_flags = DUK_LFUNC_FLAGS_PACK(magic, length, nargs);
3852 DUK_TVAL_SET_LIGHTFUNC(&tv_tmp, func, lf_flags);
3853 duk_push_tval(ctx, &tv_tmp); /* XXX: direct valstack write */
3855 return ((duk_idx_t) (thr->valstack_top - thr->valstack_bottom)) - 1;
3856
3857 api_error:
3859 return 0; /* not reached */
3860}
3861
3863 duk_hthread *thr = (duk_hthread *) ctx;
3864 duk_hbufferobject *obj;
3865 duk_tval *tv_slot;
3866
3867 DUK_ASSERT(ctx != NULL);
3868 DUK_ASSERT(prototype_bidx >= 0);
3869
3870 /* check stack first */
3871 if (thr->valstack_top >= thr->valstack_end) {
3873 }
3874
3875 obj = duk_hbufferobject_alloc(thr->heap, hobject_flags_and_class);
3876 if (!obj) {
3878 }
3879
3880 DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, (duk_hobject *) obj, thr->builtins[prototype_bidx]);
3882
3883 tv_slot = thr->valstack_top;
3884 DUK_TVAL_SET_OBJECT(tv_slot, (duk_hobject *) obj);
3885 DUK_HOBJECT_INCREF(thr, obj);
3886 thr->valstack_top++;
3887
3888 return obj;
3889}
3890
3891/* XXX: There's quite a bit of overlap with buffer creation handling in
3892 * duk_bi_buffer.c. Look for overlap and refactor.
3893 */
3894#define DUK__PACK_ARGS(classnum,protobidx,elemtype,elemshift,isview) \
3895 (((classnum) << 24) | ((protobidx) << 16) | ((elemtype) << 8) | ((elemshift) << 4) | (isview))
3896
3897#if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
3898static const duk_uint32_t duk__bufobj_flags_lookup[] = {
3912};
3913#else /* DUK_USE_BUFFEROBJECT_SUPPORT */
3914/* Only allow Duktape.Buffer when support disabled. */
3915static const duk_uint32_t duk__bufobj_flags_lookup[] = {
3917};
3918#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
3919#undef DUK__PACK_ARGS
3920
3921DUK_EXTERNAL void duk_push_buffer_object(duk_context *ctx, duk_idx_t idx_buffer, duk_size_t byte_offset, duk_size_t byte_length, duk_uint_t flags) {
3922 duk_hthread *thr;
3923 duk_hbufferobject *h_bufobj;
3924 duk_hbuffer *h_val;
3925 duk_uint32_t tmp;
3926 duk_uint_t classnum;
3927 duk_uint_t protobidx;
3928 duk_uint_t lookupidx;
3929 duk_uint_t uint_offset, uint_length, uint_added;
3930
3932 thr = (duk_hthread *) ctx;
3933 DUK_UNREF(thr);
3934
3935 /* The underlying types for offset/length in duk_hbufferobject is
3936 * duk_uint_t; make sure argument values fit and that offset + length
3937 * does not wrap.
3938 */
3939 uint_offset = (duk_uint_t) byte_offset;
3940 uint_length = (duk_uint_t) byte_length;
3941 if (sizeof(duk_size_t) != sizeof(duk_uint_t)) {
3942 if ((duk_size_t) uint_offset != byte_offset || (duk_size_t) uint_length != byte_length) {
3943 goto range_error;
3944 }
3945 }
3946 uint_added = uint_offset + uint_length;
3947 if (uint_added < uint_offset) {
3948 goto range_error;
3949 }
3950 DUK_ASSERT(uint_added >= uint_offset && uint_added >= uint_length);
3951
3952 DUK_ASSERT_DISABLE(flags >= 0); /* flags is unsigned */
3953 lookupidx = flags & 0x0f; /* 4 low bits */
3954 if (lookupidx >= sizeof(duk__bufobj_flags_lookup) / sizeof(duk_uint32_t)) {
3955 goto arg_error;
3956 }
3957 tmp = duk__bufobj_flags_lookup[lookupidx];
3958 classnum = tmp >> 24;
3959 protobidx = (tmp >> 16) & 0xff;
3960
3961 h_val = duk_require_hbuffer(ctx, idx_buffer);
3962 DUK_ASSERT(h_val != NULL);
3963
3964 h_bufobj = duk_push_bufferobject_raw(ctx,
3968 protobidx);
3969 DUK_ASSERT(h_bufobj != NULL);
3970
3971 h_bufobj->buf = h_val;
3972 DUK_HBUFFER_INCREF(thr, h_val);
3973 h_bufobj->offset = uint_offset;
3974 h_bufobj->length = uint_length;
3975 h_bufobj->shift = (tmp >> 4) & 0x0f;
3976 h_bufobj->elem_type = (tmp >> 8) & 0xff;
3977 h_bufobj->is_view = tmp & 0x0f;
3979
3980#if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
3981 /* TypedArray views need an automatic ArrayBuffer which must be
3982 * provided as .buffer property of the view. Just create a new
3983 * ArrayBuffer sharing the same underlying buffer.
3984 */
3985 if (flags & DUK_BUFOBJ_CREATE_ARRBUF) {
3986 h_bufobj = duk_push_bufferobject_raw(ctx,
3991
3992 DUK_ASSERT(h_bufobj != NULL);
3993
3994 h_bufobj->buf = h_val;
3995 DUK_HBUFFER_INCREF(thr, h_val);
3996 h_bufobj->offset = uint_offset;
3997 h_bufobj->length = uint_length;
3998 DUK_ASSERT(h_bufobj->shift == 0);
4000 DUK_ASSERT(h_bufobj->is_view == 0);
4002
4004 duk_compact(ctx, -1);
4005 }
4006#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
4007
4008 return;
4009
4010 range_error:
4012 return; /* not reached */
4013
4014 arg_error:
4016 return; /* not reached */
4017}
4018
4019DUK_EXTERNAL duk_idx_t duk_push_error_object_va_raw(duk_context *ctx, duk_errcode_t err_code, const char *filename, duk_int_t line, const char *fmt, va_list ap) {
4020 duk_hthread *thr = (duk_hthread *) ctx;
4021 duk_idx_t ret;
4023#ifdef DUK_USE_AUGMENT_ERROR_CREATE
4024 duk_bool_t noblame_fileline;
4025#endif
4026
4028 DUK_ASSERT(thr != NULL);
4029 DUK_UNREF(filename);
4030 DUK_UNREF(line);
4031
4032 /* Error code also packs a tracedata related flag. */
4033#ifdef DUK_USE_AUGMENT_ERROR_CREATE
4034 noblame_fileline = err_code & DUK_ERRCODE_FLAG_NOBLAME_FILELINE;
4035#endif
4036 err_code = err_code & (~DUK_ERRCODE_FLAG_NOBLAME_FILELINE);
4037
4038 /* error gets its 'name' from the prototype */
4039 proto = duk_error_prototype_from_code(thr, err_code);
4043 proto);
4044
4045 /* ... and its 'message' from an instance property */
4046 if (fmt) {
4047 duk_push_vsprintf(ctx, fmt, ap);
4049 } else {
4050 /* If no explicit message given, put error code into message field
4051 * (as a number). This is not fully in keeping with the Ecmascript
4052 * error model because messages are supposed to be strings (Error
4053 * constructors use ToString() on their argument). However, it's
4054 * probably more useful than having a separate 'code' property.
4055 */
4056 duk_push_int(ctx, err_code);
4058 }
4059
4060 /* XXX: .code = err_code disabled, not sure if useful */
4061
4062 /* Creation time error augmentation */
4063#ifdef DUK_USE_AUGMENT_ERROR_CREATE
4064 /* filename may be NULL in which case file/line is not recorded */
4065 duk_err_augment_error_create(thr, thr, filename, line, noblame_fileline); /* may throw an error */
4066#endif
4067
4068 return ret;
4069}
4070
4071DUK_EXTERNAL duk_idx_t duk_push_error_object_raw(duk_context *ctx, duk_errcode_t err_code, const char *filename, duk_int_t line, const char *fmt, ...) {
4072 va_list ap;
4073 duk_idx_t ret;
4074
4076
4077 va_start(ap, fmt);
4078 ret = duk_push_error_object_va_raw(ctx, err_code, filename, line, fmt, ap);
4079 va_end(ap);
4080 return ret;
4081}
4082
4083#if !defined(DUK_USE_VARIADIC_MACROS)
4085 const char *filename = duk_api_global_filename;
4087 va_list ap;
4088 duk_idx_t ret;
4089
4091
4094 va_start(ap, fmt);
4095 ret = duk_push_error_object_va_raw(ctx, err_code, filename, line, fmt, ap);
4096 va_end(ap);
4097 return ret;
4098}
4099#endif /* DUK_USE_VARIADIC_MACROS */
4100
4102 duk_hthread *thr = (duk_hthread *) ctx;
4103 duk_tval *tv_slot;
4104 duk_hbuffer *h;
4105 void *buf_data;
4106
4108
4109 /* check stack first */
4110 if (thr->valstack_top >= thr->valstack_end) {
4112 }
4113
4114 /* Check for maximum buffer length. */
4115 if (size > DUK_HBUFFER_MAX_BYTELEN) {
4117 }
4118
4119 h = duk_hbuffer_alloc(thr->heap, size, flags, &buf_data);
4120 if (!h) {
4122 }
4123
4124 tv_slot = thr->valstack_top;
4125 DUK_TVAL_SET_BUFFER(tv_slot, h);
4126 DUK_HBUFFER_INCREF(thr, h);
4127 thr->valstack_top++;
4128
4129 return (void *) buf_data;
4130}
4131
4133 duk_hthread *thr = (duk_hthread *) ctx;
4134 duk_idx_t ret;
4135
4137
4138 ret = (duk_idx_t) (thr->valstack_top - thr->valstack_bottom);
4139
4140 if (ptr == NULL) {
4141 goto push_undefined;
4142 }
4143
4144 switch ((int) DUK_HEAPHDR_GET_TYPE((duk_heaphdr *) ptr)) {
4145 case DUK_HTYPE_STRING:
4146 duk_push_hstring(ctx, (duk_hstring *) ptr);
4147 break;
4148 case DUK_HTYPE_OBJECT:
4149 duk_push_hobject(ctx, (duk_hobject *) ptr);
4150 break;
4151 case DUK_HTYPE_BUFFER:
4152 duk_push_hbuffer(ctx, (duk_hbuffer *) ptr);
4153 break;
4154 default:
4155 goto push_undefined;
4156 }
4157 return ret;
4158
4159 push_undefined:
4160 duk_push_undefined(ctx);
4161 return ret;
4162}
4163
4170
4172 duk_tval tv;
4174 DUK_ASSERT(h != NULL);
4175 DUK_TVAL_SET_STRING(&tv, h);
4176 duk_push_tval(ctx, &tv);
4177}
4178
4180 duk_hthread *thr = (duk_hthread *) ctx;
4181 DUK_UNREF(thr);
4182 DUK_ASSERT(stridx >= 0 && stridx < DUK_HEAP_NUM_STRINGS);
4183 duk_push_hstring(ctx, DUK_HTHREAD_GET_STRING(thr, stridx));
4184}
4185
4187 duk_tval tv;
4189 DUK_ASSERT(h != NULL);
4190 DUK_TVAL_SET_OBJECT(&tv, h);
4191 duk_push_tval(ctx, &tv);
4192}
4193
4195 duk_tval tv;
4197 DUK_ASSERT(h != NULL);
4198 DUK_TVAL_SET_BUFFER(&tv, h);
4199 duk_push_tval(ctx, &tv);
4200}
4201
4203 duk_hthread *thr = (duk_hthread *) ctx;
4205 DUK_ASSERT(thr != NULL);
4206 DUK_ASSERT(builtin_idx >= 0 && builtin_idx < DUK_NUM_BUILTINS);
4207 DUK_ASSERT(thr->builtins[builtin_idx] != NULL);
4208 duk_push_hobject(ctx, thr->builtins[builtin_idx]);
4209}
4210
4211/*
4212 * Poppers
4213 */
4214
4216 duk_hthread *thr = (duk_hthread *) ctx;
4217 duk_tval *tv;
4218
4220
4221 if (DUK_UNLIKELY(count < 0)) {
4223 return;
4224 }
4225
4227 if (DUK_UNLIKELY((duk_size_t) (thr->valstack_top - thr->valstack_bottom) < (duk_size_t) count)) {
4229 }
4230
4231 /*
4232 * Must be very careful here, every DECREF may cause reallocation
4233 * of our valstack.
4234 */
4235
4236 /* XXX: inlined DECREF macro would be nice here: no NULL check,
4237 * refzero queueing but no refzero algorithm run (= no pointer
4238 * instability), inline code.
4239 */
4240
4241 /* XXX: optimize loops */
4242
4243#if defined(DUK_USE_REFERENCE_COUNTING)
4244 while (count > 0) {
4245 count--;
4246 tv = --thr->valstack_top; /* tv points to element just below prev top */
4247 DUK_ASSERT(tv >= thr->valstack_bottom);
4248 DUK_TVAL_SET_UNDEFINED_UPDREF(thr, tv); /* side effects */
4249 }
4250#else
4251 tv = thr->valstack_top;
4252 while (count > 0) {
4253 count--;
4254 tv--;
4255 DUK_ASSERT(tv >= thr->valstack_bottom);
4257 }
4258 thr->valstack_top = tv;
4259#endif
4260
4262}
4263
4264/* Popping one element is called so often that when footprint is not an issue,
4265 * compile a specialized function for it.
4266 */
4267#if defined(DUK_USE_PREFER_SIZE)
4268DUK_EXTERNAL void duk_pop(duk_context *ctx) {
4270 duk_pop_n(ctx, 1);
4271}
4272#else
4274 duk_hthread *thr = (duk_hthread *) ctx;
4275 duk_tval *tv;
4277
4279 if (DUK_UNLIKELY(thr->valstack_top == thr->valstack_bottom)) {
4281 }
4282
4283 tv = --thr->valstack_top; /* tv points to element just below prev top */
4284 DUK_ASSERT(tv >= thr->valstack_bottom);
4285#ifdef DUK_USE_REFERENCE_COUNTING
4286 DUK_TVAL_SET_UNDEFINED_UPDREF(thr, tv); /* side effects */
4287#else
4289#endif
4291}
4292#endif /* !DUK_USE_PREFER_SIZE */
4293
4296 duk_pop_n(ctx, 2);
4297}
4298
4301 duk_pop_n(ctx, 3);
4302}
4303
4304/*
4305 * Error throwing
4306 */
4307
4309 duk_hthread *thr = (duk_hthread *) ctx;
4310
4311 DUK_ASSERT(thr->valstack_bottom >= thr->valstack);
4313 DUK_ASSERT(thr->valstack_end >= thr->valstack_top);
4314
4315 if (thr->valstack_top == thr->valstack_bottom) {
4317 }
4318
4319 /* Errors are augmented when they are created, not when they are
4320 * thrown or re-thrown. The current error handler, however, runs
4321 * just before an error is thrown.
4322 */
4323
4324 /* Sync so that augmentation sees up-to-date activations, NULL
4325 * thr->ptr_curr_pc so that it's not used if side effects occur
4326 * in augmentation or longjmp handling.
4327 */
4329
4330#if defined(DUK_USE_AUGMENT_ERROR_THROW)
4331 DUK_DDD(DUK_DDDPRINT("THROW ERROR (API): %!dT (before throw augment)", (duk_tval *) duk_get_tval(ctx, -1)));
4333#endif
4334 DUK_DDD(DUK_DDDPRINT("THROW ERROR (API): %!dT (after throw augment)", (duk_tval *) duk_get_tval(ctx, -1)));
4335
4337
4338 /* thr->heap->lj.jmpbuf_ptr is checked by duk_err_longjmp() so we don't
4339 * need to check that here. If the value is NULL, a panic occurs because
4340 * we can't return.
4341 */
4342
4343 duk_err_longjmp(thr);
4345}
4346
4347DUK_EXTERNAL void duk_fatal(duk_context *ctx, duk_errcode_t err_code, const char *err_msg) {
4348 duk_hthread *thr = (duk_hthread *) ctx;
4349
4351 DUK_ASSERT(thr != NULL);
4352 DUK_ASSERT(thr->heap != NULL);
4353 DUK_ASSERT(thr->heap->fatal_func != NULL);
4354
4355 DUK_D(DUK_DPRINT("fatal error occurred, code %ld, message %s",
4356 (long) err_code, (const char *) err_msg));
4357
4358 /* fatal_func should be noreturn, but noreturn declarations on function
4359 * pointers has a very spotty support apparently so it's not currently
4360 * done.
4361 */
4362 thr->heap->fatal_func(ctx, err_code, err_msg);
4363
4364 DUK_PANIC(DUK_ERR_API_ERROR, "fatal handler returned");
4365}
4366
4367DUK_EXTERNAL void duk_error_va_raw(duk_context *ctx, duk_errcode_t err_code, const char *filename, duk_int_t line, const char *fmt, va_list ap) {
4369
4370 duk_push_error_object_va_raw(ctx, err_code, filename, line, fmt, ap);
4371 duk_throw(ctx);
4372}
4373
4374DUK_EXTERNAL void duk_error_raw(duk_context *ctx, duk_errcode_t err_code, const char *filename, duk_int_t line, const char *fmt, ...) {
4375 va_list ap;
4376
4378
4379 va_start(ap, fmt);
4380 duk_push_error_object_va_raw(ctx, err_code, filename, line, fmt, ap);
4381 va_end(ap);
4382 duk_throw(ctx);
4383}
4384
4385#if !defined(DUK_USE_VARIADIC_MACROS)
4386DUK_EXTERNAL void duk_error_stash(duk_context *ctx, duk_errcode_t err_code, const char *fmt, ...) {
4387 const char *filename;
4388 duk_int_t line;
4389 va_list ap;
4390
4392
4393 filename = duk_api_global_filename;
4394 line = duk_api_global_line;
4397
4398 va_start(ap, fmt);
4399 duk_push_error_object_va_raw(ctx, err_code, filename, line, fmt, ap);
4400 va_end(ap);
4401 duk_throw(ctx);
4402}
4403#endif /* DUK_USE_VARIADIC_MACROS */
4404
4405/*
4406 * Comparison
4407 */
4408
4410 duk_hthread *thr = (duk_hthread *) ctx;
4411 duk_tval *tv1, *tv2;
4412
4414
4415 tv1 = duk_get_tval(ctx, index1);
4416 tv2 = duk_get_tval(ctx, index2);
4417 if ((tv1 == NULL) || (tv2 == NULL)) {
4418 return 0;
4419 }
4420
4421 /* Coercion may be needed, the helper handles that by pushing the
4422 * tagged values to the stack.
4423 */
4424 return duk_js_equals(thr, tv1, tv2);
4425}
4426
4428 duk_tval *tv1, *tv2;
4429
4431
4432 tv1 = duk_get_tval(ctx, index1);
4433 tv2 = duk_get_tval(ctx, index2);
4434 if ((tv1 == NULL) || (tv2 == NULL)) {
4435 return 0;
4436 }
4437
4438 /* No coercions or other side effects, so safe */
4439 return duk_js_strict_equals(tv1, tv2);
4440}
4441
4442/*
4443 * instanceof
4444 */
4445
4447 duk_tval *tv1, *tv2;
4448
4450
4451 /* Index validation is strict, which differs from duk_equals().
4452 * The strict behavior mimics how instanceof itself works, e.g.
4453 * it is a TypeError if rval is not a -callable- object. It would
4454 * be somewhat inconsistent if rval would be allowed to be
4455 * non-existent without a TypeError.
4456 */
4457 tv1 = duk_require_tval(ctx, index1);
4458 DUK_ASSERT(tv1 != NULL);
4459 tv2 = duk_require_tval(ctx, index2);
4460 DUK_ASSERT(tv2 != NULL);
4461
4462 return duk_js_instanceof((duk_hthread *) ctx, tv1, tv2);
4463}
4464
4465/*
4466 * Lightfunc
4467 */
4468
4470 duk_c_function func;
4471
4473
4474 /* Lightfunc name, includes Duktape/C native function pointer, which
4475 * can often be used to locate the function from a symbol table.
4476 * The name also includes the 16-bit duk_tval flags field because it
4477 * includes the magic value. Because a single native function often
4478 * provides different functionality depending on the magic value, it
4479 * seems reasonably to include it in the name.
4480 *
4481 * On the other hand, a complicated name increases string table
4482 * pressure in low memory environments (but only when function name
4483 * is accessed).
4484 */
4485
4487 duk_push_sprintf(ctx, "light_");
4488 duk_push_string_funcptr(ctx, (duk_uint8_t *) &func, sizeof(func));
4489 duk_push_sprintf(ctx, "_%04x", (unsigned int) DUK_TVAL_GET_LIGHTFUNC_FLAGS(tv));
4490 duk_concat(ctx, 3);
4491}
4492
4495
4496 duk_push_string(ctx, "function ");
4497 duk_push_lightfunc_name(ctx, tv);
4498 duk_push_string(ctx, "() {\"light\"}");
4499 duk_concat(ctx, 3);
4500}
4501
4502/*
4503 * Function pointers
4504 *
4505 * Printing function pointers is non-portable, so we do that by hex printing
4506 * bytes from memory.
4507 */
4508
4510 duk_uint8_t buf[32 * 2];
4511 duk_uint8_t *p, *q;
4514
4515 DUK_ASSERT(sz <= 32); /* sanity limit for function pointer size */
4516
4517 p = buf;
4518#if defined(DUK_USE_INTEGER_LE)
4519 q = ptr + sz;
4520#else
4521 q = ptr;
4522#endif
4523 for (i = 0; i < sz; i++) {
4524#if defined(DUK_USE_INTEGER_LE)
4525 t = *(--q);
4526#else
4527 t = *(q++);
4528#endif
4529 *p++ = duk_lc_digits[t >> 4];
4530 *p++ = duk_lc_digits[t & 0x0f];
4531 }
4532
4533 duk_push_lstring(ctx, (const char *) buf, sz * 2);
4534}
4535
4536#if !defined(DUK_USE_PARANOID_ERRORS)
4537/*
4538 * Push readable string summarizing duk_tval. The operation is side effect
4539 * free and will only throw from internal errors (e.g. out of memory).
4540 * This is used by e.g. property access code to summarize a key/base safely,
4541 * and is not intended to be fast (but small and safe).
4542 */
4543
4544#define DUK__READABLE_STRING_MAXCHARS 32
4545
4546/* String sanitizer which escapes ASCII control characters and a few other
4547 * ASCII characters, passes Unicode as is, and replaces invalid UTF-8 with
4548 * question marks. No errors are thrown for any input string, except in out
4549 * of memory situations.
4550 */
4552 duk_hthread *thr;
4553 const duk_uint8_t *p, *p_start, *p_end;
4555 2 /*quotes*/ + 3 /*periods*/];
4556 duk_uint8_t *q;
4558 duk_small_uint_t nchars;
4559
4561 DUK_ASSERT(h_input != NULL);
4562 thr = (duk_hthread *) ctx;
4563
4564 p_start = (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h_input);
4565 p_end = p_start + DUK_HSTRING_GET_BYTELEN(h_input);
4566 p = p_start;
4567 q = buf;
4568
4569 nchars = 0;
4570 *q++ = (duk_uint8_t) DUK_ASC_SINGLEQUOTE;
4571 for (;;) {
4572 if (p >= p_end) {
4573 break;
4574 }
4575 if (nchars == DUK__READABLE_STRING_MAXCHARS) {
4576 *q++ = (duk_uint8_t) DUK_ASC_PERIOD;
4577 *q++ = (duk_uint8_t) DUK_ASC_PERIOD;
4578 *q++ = (duk_uint8_t) DUK_ASC_PERIOD;
4579 break;
4580 }
4581 if (duk_unicode_decode_xutf8(thr, &p, p_start, p_end, &cp)) {
4582 if (cp < 0x20 || cp == 0x7f || cp == DUK_ASC_SINGLEQUOTE || cp == DUK_ASC_BACKSLASH) {
4583 DUK_ASSERT(DUK_UNICODE_MAX_XUTF8_LENGTH >= 4); /* estimate is valid */
4584 DUK_ASSERT((cp >> 4) <= 0x0f);
4585 *q++ = (duk_uint8_t) DUK_ASC_BACKSLASH;
4586 *q++ = (duk_uint8_t) DUK_ASC_LC_X;
4587 *q++ = (duk_uint8_t) duk_lc_digits[cp >> 4];
4588 *q++ = (duk_uint8_t) duk_lc_digits[cp & 0x0f];
4589 } else {
4590 q += duk_unicode_encode_xutf8(cp, q);
4591 }
4592 } else {
4593 p++; /* advance manually */
4594 *q++ = (duk_uint8_t) DUK_ASC_QUESTION;
4595 }
4596 nchars++;
4597 }
4598 *q++ = (duk_uint8_t) DUK_ASC_SINGLEQUOTE;
4599
4600 duk_push_lstring(ctx, (const char *) buf, (duk_size_t) (q - buf));
4601}
4602
4604 duk_hthread *thr;
4605
4607 thr = (duk_hthread *) ctx;
4608 DUK_UNREF(thr);
4609
4610 if (tv == NULL) {
4611 duk_push_string(ctx, "none");
4612 } else {
4613 switch (DUK_TVAL_GET_TAG(tv)) {
4614 case DUK_TAG_STRING: {
4616 break;
4617 }
4618 case DUK_TAG_OBJECT: {
4620 DUK_ASSERT(h != NULL);
4622 break;
4623 }
4624 case DUK_TAG_BUFFER: {
4625 /* XXX: Hex encoded, length limited buffer summary here? */
4627 DUK_ASSERT(h != NULL);
4628 duk_push_sprintf(ctx, "[buffer:%ld]", (long) DUK_HBUFFER_GET_SIZE(h));
4629 break;
4630 }
4631 case DUK_TAG_POINTER: {
4632 /* Surround with parentheses like in JX, ensures NULL pointer
4633 * is distinguishable from null value ("(null)" vs "null").
4634 */
4635 duk_push_tval(ctx, tv);
4636 duk_push_sprintf(ctx, "(%s)", duk_to_string(ctx, -1));
4637 duk_remove(ctx, -2);
4638 break;
4639 }
4640 default: {
4641 duk_push_tval(ctx, tv);
4642 break;
4643 }
4644 }
4645 }
4646
4647 return duk_to_string(ctx, -1);
4648}
4649
4654#endif /* !DUK_USE_PARANOID_ERRORS */
4655
4656#undef DUK__CHECK_SPACE
4657#undef DUK__PACK_ARGS
4658#undef DUK__READABLE_STRING_MAXCHARS
const char * proto
Definition civetweb.c:18378
guint index
unsigned int duk_small_uint_t
duk_small_int_t duk_ret_t
#define DUK_VA_COPY(dest, src)
duk_int_fast32_t duk_int_t
duk_uint_fast32_t duk_uint_t
duk_small_int_t duk_bool_t
DUK_EXTERNAL void duk_concat(duk_context *ctx, duk_idx_t count)
#define DUK_HOBJECT_CLASS_BOOLEAN
#define DUK_TVAL_DECREF(thr, tv)
#define DUK_STR_STRING_TOO_LONG
#define DUK_BIDX_ARRAYBUFFER_PROTOTYPE
#define DUK_TVAL_SET_TVAL(v, x)
#define DUK_BIDX_UINT8ARRAY_PROTOTYPE
#define DUK_ERROR_RANGE(thr, msg)
#define DUK_HOBJECT_FLAG_NEWENV
#define DUK_TVAL_SET_NUMBER_UPDREF
#define DUK_ASSERT_CTX_VALID(ctx)
#define DUK_TVAL_SET_FASTINT_U32_UPDREF
#define DUK_HTHREAD_STRING_UC_UNDEFINED(thr)
#define DUK_STR_NUMBER_OUTSIDE_RANGE
#define DUK_TVAL_SET_NULL(tv)
#define DUK_HSTRING_GET_DATA(x)
DUK_INTERNAL_DECL void duk_err_setup_heap_ljstate(duk_hthread *thr, duk_small_int_t lj_type)
#define DUK_TVAL_SET_FASTINT_U32(tv, val)
DUK_INTERNAL_DECL duk_uint16_t duk_js_touint16(duk_hthread *thr, duk_tval *tv)
#define DUK_HEAPHDR_GET_TYPE(h)
#define DUK_HOBJECT_GET_PROTOTYPE(heap, h)
#define DUK_TVAL_IS_NUMBER(tv)
#define DUK_STR_NOT_COMPILEDFUNCTION
#define DUK_VALSTACK_SHRINK_THRESHOLD
#define DUK_HOBJECT_FLAG_CONSTRUCTABLE
#define DUK_ERROR_TYPE(thr, msg)
#define DUK_HOBJECT_CLASS_BUFFER
DUK_INTERNAL void duk_err_longjmp(duk_hthread *thr)
#define DUK_BIDX_EVAL_ERROR_PROTOTYPE
DUK_INTERNAL_DECL duk_bool_t duk_get_prop_stridx(duk_context *ctx, duk_idx_t obj_index, duk_small_int_t stridx)
#define DUK_STR_DEFAULTVALUE_COERCE_FAILED
#define DUK_HBUFFER_INCREF(thr, h)
DUK_INTERNAL duk_hbufferobject * duk_hbufferobject_alloc(duk_heap *heap, duk_uint_t hobject_flags)
#define DUK_LFUNC_NARGS_MAX
#define DUK_HOBJECT_CLASS_INT32ARRAY
#define DUK_VSRESIZE_FLAG_THROW
DUK_INTERNAL_DECL duk_small_int_t duk_unicode_encode_xutf8(duk_ucodepoint_t cp, duk_uint8_t *out)
#define DUK_HBUFFEROBJECT_ELEM_INT32
DUK_INTERNAL_DECL void duk_numconv_stringify(duk_context *ctx, duk_small_int_t radix, duk_small_int_t digits, duk_small_uint_t flags)
#define DUK_TVAL_SET_OBJECT(tv, hptr)
#define DUK_HOBJECT_CLASS_POINTER
#define DUK_TVAL_GET_OBJECT(tv)
#define DUK_HOBJECT_CLASS_NUMBER_TO_STRIDX(n)
DUK_EXTERNAL void duk_compact(duk_context *ctx, duk_idx_t obj_index)
DUK_INTERNAL_DECL duk_bool_t duk_js_instanceof(duk_hthread *thr, duk_tval *tv_x, duk_tval *tv_y)
#define DUK_HEAPHDR_CHECK_FLAG_BITS(h, bits)
DUK_INTERNAL_DECL duk_hnativefunction * duk_hnativefunction_alloc(duk_heap *heap, duk_uint_t hobject_flags)
#define DUK_HOBJECT_FLAG_NOTAIL
#define DUK_BIDX_INT8ARRAY_PROTOTYPE
#define DUK_HOBJECT_CLASS_OBJECT
#define DUK_STR_INVALID_CALL_ARGS
#define DUK_BIDX_OBJECT_PROTOTYPE
#define DUK_STR_NOT_NATIVEFUNCTION
#define DUK_BIDX_TYPE_ERROR_PROTOTYPE
#define DUK_TVAL_IS_NULL(tv)
#define DUK_HBUFFER_MAX_BYTELEN
#define DUK_HOBJECT_FLAG_COMPILEDFUNCTION
#define DUK_TVAL_IS_BUFFER(tv)
#define DUK_PUSH_SPRINTF_SANITY_LIMIT
#define DUK_LFUNC_LENGTH_MAX
#define DUK_STR_VALSTACK_LIMIT
#define DUK_TVAL_IS_UNDEFINED(tv)
#define DUK_HOBJECT_IS_COMPILEDFUNCTION(h)
#define DUK_TVAL_GET_LIGHTFUNC_FUNCPTR(tv)
DUK_INTERNAL_DECL duk_hstring * duk_heap_string_intern_checked(duk_hthread *thr, const duk_uint8_t *str, duk_uint32_t len)
#define DUK_BIDX_POINTER_PROTOTYPE
#define DUK_TVAL_SET_BUFFER(tv, hptr)
#define DUK_TVAL_IS_OBJECT(tv)
#define DUK_HOBJECT_CLASS_INT16ARRAY
#define DUK_BIDX_FUNCTION_PROTOTYPE
#define DUK_PROPDESC_FLAGS_NONE
#define DUK_TVAL_GET_BUFFER(tv)
#define DUK_STR_BUFFER_TOO_LONG
#define DUK_TVAL_SET_NULL_UPDREF
#define DUK_BIDX_DATAVIEW_PROTOTYPE
DUK_INTERNAL_DECL duk_double_t duk_js_tointeger(duk_hthread *thr, duk_tval *tv)
#define DUK_HBUFFEROBJECT_VALID_SLICE(h)
#define DUK_HOBJECT_CLASS_FUNCTION
#define DUK_HSTRING_GET_CHARLEN(x)
DUK_INTERNAL_DECL duk_uint32_t duk_js_touint32(duk_hthread *thr, duk_tval *tv)
#define DUK_HOBJECT_CLASS_ARRAY
#define DUK_ERROR_API_INDEX(thr, index)
DUK_INTERNAL_DECL duk_hcompiledfunction * duk_hcompiledfunction_alloc(duk_heap *heap, duk_uint_t hobject_flags)
#define DUK_HOBJECT_CLASS_DATE
#define DUK_TVAL_INCREF(thr, tv)
DUK_INTERNAL const duk_uint8_t duk_lc_digits[36]
#define DUK_REALLOC_INDIRECT(heap, cb, ud, newsize)
DUK_INTERNAL_DECL void duk_hthread_create_builtin_objects(duk_hthread *thr)
#define DUK_TVAL_GET_TAG(tv)
#define DUK_VSRESIZE_FLAG_SHRINK
#define DUK_HBUFFEROBJECT_ELEM_INT16
#define DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, h, p)
#define DUK_BIDX_STRING_PROTOTYPE
#define DUK_HOBJECT_CLASS_UINT8CLAMPEDARRAY
#define DUK_TVAL_IS_POINTER(tv)
#define DUK_STRIDX_LC_UNDEFINED
#define DUK_ASSERT_HBUFFEROBJECT_VALID(h)
#define DUK_TVAL_IS_UNUSED(tv)
#define DUK_HOBJECT_FLAG_BUFFEROBJECT
#define DUK_HOBJECT_GET_CLASS_STRING(heap, h)
#define DUK_BIDX_UINT8CLAMPEDARRAY_PROTOTYPE
#define DUK_BIDX_INT32ARRAY_PROTOTYPE
#define DUK_STR_NOT_OBJECT_COERCIBLE
#define DUK_HOBJECT_SET_EXOTIC_ARRAY(h)
#define DUK_ASSERT_DISABLE(x)
DUK_INTERNAL_DECL duk_hthread * duk_hthread_alloc(duk_heap *heap, duk_uint_t hobject_flags)
#define DUK_ERROR_API(thr, msg)
#define DUK_BIDX_FLOAT32ARRAY_PROTOTYPE
#define DUK_HOBJECT_CLASS_AS_FLAGS(v)
#define DUK_HOBJECT_CLASS_NUMBER
#define DUK_ERROR_ALLOC_DEFMSG(thr)
#define DUK_LFUNC_MAGIC_MIN
#define DUK_HTHREAD_STATE_INACTIVE
#define DUK_HTHREAD_STRING_UC_NULL(thr)
#define DUK_TVAL_SET_BOOLEAN_UPDREF
#define DUK_STR_INVALID_COUNT
#define DUK_LFUNC_NARGS_VARARGS
DUK_INTERNAL_DECL duk_hobject * duk_error_prototype_from_code(duk_hthread *thr, duk_errcode_t err_code)
#define DUK_HOBJECT_IS_THREAD(h)
#define DUK_TVAL_GET_STRING(tv)
DUK_INTERNAL_DECL duk_int32_t duk_js_toint32(duk_hthread *thr, duk_tval *tv)
#define DUK_TVAL_SET_FASTINT(tv, val)
DUK_INTERNAL_DECL void duk_err_augment_error_create(duk_hthread *thr, duk_hthread *thr_callstack, const char *filename, duk_int_t line, duk_bool_t noblame_fileline)
#define DUK_BIDX_ARRAY_PROTOTYPE
#define DUK_HSTRING_INCREF(thr, h)
#define DUK_HOBJECT_FLAG_EXOTIC_STRINGOBJ
#define DUK_ERROR_UNSUPPORTED(thr, msg)
#define DUK_TVAL_SET_TVAL_UPDREF
#define DUK_HOBJECT_IS_NATIVEFUNCTION(h)
#define DUK_PUSH_SPRINTF_INITIAL_SIZE
#define DUK_HTHREAD_INCREF(thr, h)
#define DUK_BIDX_ERROR_PROTOTYPE
#define DUK_TVAL_IS_STRING(tv)
#define DUK_LFUNC_FLAGS_PACK(magic, length, nargs)
#define DUK_HOBJECT_CLASS_ERROR
#define DUK_VSRESIZE_FLAG_COMPACT
#define DUK_BIDX_REFERENCE_ERROR_PROTOTYPE
#define DUK_BIDX_URI_ERROR_PROTOTYPE
#define DUK_HOBJECT_CLASS_FLOAT32ARRAY
#define DUK_HOBJECT_FLAG_BOUND
#define DUK_TVAL_SET_POINTER(tv, hptr)
#define DUK_HOBJECT_IS_BUFFEROBJECT(h)
#define DUK_TVAL_IS_LIGHTFUNC(tv)
#define DUK_HSTRING_GET_BYTELEN(x)
DUK_INTERNAL_DECL duk_bool_t duk_hthread_init_stacks(duk_heap *heap, duk_hthread *thr)
#define DUK_VALSTACK_GROW_STEP
#define DUK_HOBJECT_CLASS_UINT8ARRAY
#define DUK_BIDX_NUMBER_PROTOTYPE
#define DUK_STRIDX_EMPTY_STRING
#define DUK_BIDX_SYNTAX_ERROR_PROTOTYPE
#define DUK_BIDX_UINT32ARRAY_PROTOTYPE
#define DUK_HOBJECT_FLAG_EXOTIC_DUKFUNC
#define DUK_HBUFFEROBJECT_ELEM_FLOAT64
#define DUK_TVAL_SET_BOOLEAN_TRUE(v)
#define DUK_BIDX_FLOAT64ARRAY_PROTOTYPE
#define DUK_HBUFFEROBJECT_ELEM_UINT32
#define DUK_HBUFFEROBJECT_ELEM_UINT16
DUK_INTERNAL_DECL void duk_hthread_sync_and_null_currpc(duk_hthread *thr)
#define DUK_HOBJECT_CLASS_UINT32ARRAY
#define DUK_HOBJECT_CLASS_THREAD
#define DUK_ERROR_REQUIRE_TYPE_INDEX(thr, index, expectname, lowmemstr)
#define DUK_TVAL_IS_HEAP_ALLOCATED(tv)
#define DUK_HOBJECT_FLAG_STRICT
#define DUK_HSTRING_MAX_BYTELEN
#define DUK_HOBJECT_HAS_NATIVEFUNCTION(h)
#define DUK_HOBJECT_PROTOTYPE_CHAIN_SANITY
DUK_EXTERNAL duk_int_t duk_safe_call(duk_context *ctx, duk_safe_call_function func, duk_idx_t nargs, duk_idx_t nrets)
#define DUK_BIDX_BOOLEAN_PROTOTYPE
struct duk_tval_struct duk_tval
#define DUK_PANIC(code, msg)
#define DUK_LFUNC_LENGTH_MIN
DUK_INTERNAL_DECL void duk_err_augment_error_throw(duk_hthread *thr)
#define DUK_HOBJECT_CLASS_MAX
#define DUK_HOBJECT_FLAG_THREAD
#define DUK_VALSTACK_INITIAL_SIZE
#define DUK_LFUNC_FLAGS_GET_MAGIC(lf_flags)
DUK_EXTERNAL void duk_call_method(duk_context *ctx, duk_idx_t nargs)
#define DUK_STR_INVALID_CONTEXT
#define DUK_PROPDESC_FLAGS_WC
#define DUK_HTHREAD_GET_STRING(thr, idx)
#define duk_js_equals(thr, tv_x, tv_y)
#define DUK_BIDX_THREAD_PROTOTYPE
#define DUK_STR_PUSH_BEYOND_ALLOC_STACK
#define DUK_BIDX_BUFFER_PROTOTYPE
#define DUK_TVAL_GET_POINTER(tv)
#define DUK_HOBJECT_CLASS_INT8ARRAY
#define DUK_HNATIVEFUNCTION_NARGS_VARARGS
DUK_INTERNAL_DECL void duk_hobject_define_property_internal(duk_hthread *thr, duk_hobject *obj, duk_hstring *key, duk_small_uint_t flags)
DUK_INTERNAL_DECL duk_hbuffer * duk_hbuffer_alloc(duk_heap *heap, duk_size_t size, duk_small_uint_t flags, void **out_bufdata)
#define DUK_BIDX_NODEJS_BUFFER_PROTOTYPE
#define DUK_HOBJECT_FLAG_EXTENSIBLE
DUK_EXTERNAL void * duk_resize_buffer(duk_context *ctx, duk_idx_t index, duk_size_t new_size)
#define DUK_HOBJECT_FLAG_NATIVEFUNCTION
DUK_INTERNAL_DECL duk_uint32_t duk_hobject_get_length(duk_hthread *thr, duk_hobject *obj)
#define DUK_TVAL_GET_LIGHTFUNC(tv, out_fp, out_flags)
#define duk_js_strict_equals(tv_x, tv_y)
#define DUK_HOBJECT_CLASS_UINT16ARRAY
#define DUK_VALSTACK_API_ENTRY_MINIMUM
#define DUK_TVAL_SET_BOOLEAN_FALSE(v)
#define DUK_LFUNC_NARGS_MIN
#define DUK_HBUFFEROBJECT_ELEM_FLOAT32
#define DUK_TVAL_SET_FASTINT_I32(tv, val)
#define DUK_TVAL_GET_BOOLEAN(tv)
#define DUK_TVAL_SET_BOOLEAN(tv, val)
#define DUK_HBUFFER_GET_DATA_PTR(heap, x)
#define DUK_HOBJECT_INCREF(thr, h)
#define DUK_TVAL_SET_LIGHTFUNC(tv, fp, flags)
DUK_INTERNAL_DECL duk_double_t duk_js_tonumber(duk_hthread *thr, duk_tval *tv)
#define DUK_TVAL_SET_UNDEFINED_UPDREF
#define DUK_HTHREAD_STRING_EMPTY_STRING(thr)
#define DUK_TVAL_GET_NUMBER(tv)
#define DUK_HOBJECT_GET_CLASS_NUMBER(h)
#define DUK_HOBJECT_SET_EXOTIC_DUKFUNC(h)
#define DUK_LFUNC_FLAGS_GET_NARGS(lf_flags)
#define DUK_LFUNC_FLAGS_GET_LENGTH(lf_flags)
#define DUK_TVAL_GET_HEAPHDR(tv)
#define DUK_STRIDX_VALUE_OF
#define DUK_LFUNC_MAGIC_MAX
#define DUK_HBUFFEROBJECT_ELEM_UINT8CLAMPED
#define DUK_STR_SPRINTF_TOO_LONG
DUK_INTERNAL_DECL void duk_hobject_set_length(duk_hthread *thr, duk_hobject *obj, duk_uint32_t length)
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 void * duk_hthread_get_valstack_ptr(duk_heap *heap, void *ud)
#define DUK_BIDX_RANGE_ERROR_PROTOTYPE
#define DUK_HTHREAD_STRING_LENGTH(thr)
#define DUK_TVAL_SET_UNDEFINED(tv)
#define DUK_STR_NOT_UNDEFINED
#define DUK_STRIDX_UC_ERROR
#define DUK_STR_UNEXPECTED_TYPE
#define DUK_HOBJECT_HAS_EXTENSIBLE(h)
#define DUK_HOBJECT_CLASS_STRING
#define DUK_HBUFFER_HAS_EXTERNAL(x)
#define DUK_HNATIVEFUNCTION_NARGS_MAX
#define DUK_HBUFFER_HAS_DYNAMIC(x)
#define DUK_HOBJECT_CLASS_ARRAYBUFFER
#define DUK_ERRCODE_FLAG_NOBLAME_FILELINE
#define DUK_HOBJECT_CLASS_DATAVIEW
#define DUK_VALSTACK_SHRINK_SPARE
#define DUK_BIDX_INT16ARRAY_PROTOTYPE
#define DUK_TVAL_SET_STRING(tv, hptr)
#define DUK_UNICODE_MAX_XUTF8_LENGTH
#define DUK_TVAL_IS_BOOLEAN(tv)
#define DUK_BIDX_UINT16ARRAY_PROTOTYPE
DUK_INTERNAL_DECL duk_bool_t duk_js_toboolean(duk_tval *tv)
#define DUK_TVAL_SET_NUMBER(tv, val)
DUK_INTERNAL_DECL duk_small_int_t duk_unicode_decode_xutf8(duk_hthread *thr, const duk_uint8_t **ptr, const duk_uint8_t *ptr_start, const duk_uint8_t *ptr_end, duk_ucodepoint_t *out_cp)
#define DUK_HBUFFER_GET_SIZE(x)
#define DUK_TVAL_GET_LIGHTFUNC_FLAGS(tv)
#define DUK_HBUFFEROBJECT_ELEM_INT8
DUK_INTERNAL_DECL duk_hobject * duk_hobject_alloc(duk_heap *heap, duk_uint_t hobject_flags)
#define DUK_TVAL_SET_FASTINT_I32_UPDREF
#define DUK_HBUFFEROBJECT_ELEM_UINT8
#define DUK_HOBJECT_CLASS_FLOAT64ARRAY
#define DUK_VALSTACK_INTERNAL_EXTRA
DUK_INTERNAL_DECL void duk_hthread_copy_builtin_objects(duk_hthread *thr_from, duk_hthread *thr_to)
#define DUK_HOBJECT_FLAG_ARRAY_PART
DUK_INTERNAL_DECL duk_activation * duk_hthread_get_current_activation(duk_hthread *thr)
#define DUK_STRING_PUSH_SAFE
#define DUK_TYPE_MASK_STRING
#define DUK_DBLUNION_SET_NAN(u)
#define duk_is_primitive(ctx, index)
#define duk_push_fixed_buffer(ctx, size)
#define DUK_TYPE_MASK_OBJECT
#define DUK_BUF_MODE_DONTCARE
#define duk_safe_to_string(ctx, index)
#define DUK_BUF_MODE_DYNAMIC
#define DUK_DBLUNION_IS_NORMALIZED(u)
#define DUK_TYPE_MASK_BUFFER
#define DUK_DBLUNION_NORMALIZE_NAN_CHECK(u)
#define DUK_TYPE_MASK_LIGHTFUNC
#define DUK_TYPE_MASK_NUMBER
#define DUK_BUFOBJ_CREATE_ARRBUF
#define DUK_ERR_SYNTAX_ERROR
#define DUK_ERR_RANGE_ERROR
#define duk_push_buffer(ctx, size, dynamic)
#define DUK_TYPE_MASK_BOOLEAN
#define DUK_TYPE_MASK_THROW
duk_ret_t(* duk_c_function)(duk_context *ctx)
#define DUK_THREAD_NEW_GLOBAL_ENV
#define DUK_TYPE_MASK_POINTER
#define DUK_ERR_REFERENCE_ERROR
#define duk_is_callable(ctx, index)
#define duk_push_dynamic_buffer(ctx, size)
#define DUK_TYPE_MASK_UNDEFINED
DUK_EXTERNAL duk_bool_t duk_require_boolean(duk_context *ctx, duk_idx_t index)
DUK_EXTERNAL void * duk_to_pointer(duk_context *ctx, duk_idx_t index)
DUK_EXTERNAL void duk_error_stash(duk_context *ctx, duk_errcode_t err_code, const char *fmt,...)
DUK_INTERNAL void duk_push_c_function_noexotic(duk_context *ctx, duk_c_function func, duk_int_t nargs)
DUK_INTERNAL const char * duk_push_string_readable(duk_context *ctx, duk_idx_t index)
DUK_INTERNAL duk_hstring * duk_to_hstring(duk_context *ctx, duk_idx_t index)
DUK_EXTERNAL void * duk_push_buffer_raw(duk_context *ctx, duk_size_t size, duk_small_uint_t flags)
DUK_EXTERNAL void duk_pop_2(duk_context *ctx)
DUK_EXTERNAL const char * duk_push_string(duk_context *ctx, const char *str)
DUK_INTERNAL duk_hthread * duk_require_hthread(duk_context *ctx, duk_idx_t index)
DUK_EXTERNAL const char * duk_get_lstring(duk_context *ctx, duk_idx_t index, duk_size_t *out_len)
DUK_EXTERNAL duk_idx_t duk_normalize_index(duk_context *ctx, duk_idx_t index)
DUK_INTERNAL duk_int_t duk_to_int_clamped_raw(duk_context *ctx, duk_idx_t index, duk_int_t minval, duk_int_t maxval, duk_bool_t *out_clamped)
DUK_INTERNAL duk_idx_t duk_push_object_helper_proto(duk_context *ctx, duk_uint_t hobject_flags_and_class, duk_hobject *proto)
DUK_EXTERNAL void duk_throw(duk_context *ctx)
DUK_EXTERNAL duk_bool_t duk_is_undefined(duk_context *ctx, duk_idx_t index)
DUK_INTERNAL duk_hobject * duk_require_hobject(duk_context *ctx, duk_idx_t index)
DUK_EXTERNAL duk_idx_t duk_require_normalize_index(duk_context *ctx, duk_idx_t index)
DUK_EXTERNAL const char * duk_push_string_file_raw(duk_context *ctx, const char *path, duk_uint_t flags)
DUK_EXTERNAL void duk_push_this(duk_context *ctx)
DUK_EXTERNAL void duk_push_true(duk_context *ctx)
DUK_LOCAL void duk__push_stash(duk_context *ctx)
DUK_EXTERNAL duk_bool_t duk_is_fixed_buffer(duk_context *ctx, duk_idx_t index)
DUK_EXTERNAL duk_bool_t duk_is_thread(duk_context *ctx, duk_idx_t index)
DUK_EXTERNAL void duk_swap(duk_context *ctx, duk_idx_t index1, duk_idx_t index2)
DUK_EXTERNAL duk_double_t duk_require_number(duk_context *ctx, duk_idx_t index)
#define DUK__PACK_ARGS(classnum, protobidx, elemtype, elemshift, isview)
DUK_EXTERNAL duk_bool_t duk_is_pointer(duk_context *ctx, duk_idx_t index)
DUK_EXTERNAL duk_double_t duk_get_number(duk_context *ctx, duk_idx_t index)
DUK_EXTERNAL duk_errcode_t duk_get_error_code(duk_context *ctx, duk_idx_t index)
DUK_INTERNAL duk_int_t duk_to_int_check_range(duk_context *ctx, duk_idx_t index, duk_int_t minval, duk_int_t maxval)
DUK_EXTERNAL void duk_to_primitive(duk_context *ctx, duk_idx_t index, duk_int_t hint)
DUK_EXTERNAL duk_bool_t duk_get_boolean(duk_context *ctx, duk_idx_t index)
DUK_EXTERNAL duk_bool_t duk_check_type(duk_context *ctx, duk_idx_t index, duk_int_t type)
DUK_INTERNAL void duk_push_lightfunc_tostring(duk_context *ctx, duk_tval *tv)
DUK_EXTERNAL const char * duk_safe_to_lstring(duk_context *ctx, duk_idx_t index, duk_size_t *out_len)
DUK_EXTERNAL void duk_push_boolean(duk_context *ctx, duk_bool_t val)
DUK_LOCAL duk_bool_t duk__resize_valstack(duk_context *ctx, duk_size_t new_size)
DUK_EXTERNAL duk_bool_t duk_is_function(duk_context *ctx, duk_idx_t index)
DUK_EXTERNAL duk_int_t duk_api_global_line
DUK_EXTERNAL void duk_to_undefined(duk_context *ctx, duk_idx_t index)
DUK_LOCAL void duk__push_hstring_readable_unicode(duk_context *ctx, duk_hstring *h_input)
DUK_INTERNAL void duk_push_c_function_noconstruct_noexotic(duk_context *ctx, duk_c_function func, duk_int_t nargs)
DUK_EXTERNAL duk_bool_t duk_check_type_mask(duk_context *ctx, duk_idx_t index, duk_uint_t mask)
DUK_INTERNAL void duk_push_lightfunc_name(duk_context *ctx, duk_tval *tv)
DUK_EXTERNAL void * duk_require_heapptr(duk_context *ctx, duk_idx_t index)
DUK_EXTERNAL duk_idx_t duk_require_top_index(duk_context *ctx)
DUK_EXTERNAL void duk_require_null(duk_context *ctx, duk_idx_t index)
DUK_EXTERNAL duk_size_t duk_get_length(duk_context *ctx, duk_idx_t index)
DUK_INTERNAL void duk_set_length(duk_context *ctx, duk_idx_t index, duk_size_t length)
DUK_LOCAL duk_double_t duk__to_int_uint_helper(duk_context *ctx, duk_idx_t index, duk__toint_coercer coerce_func)
DUK_EXTERNAL duk_bool_t duk_is_valid_index(duk_context *ctx, duk_idx_t index)
DUK_LOCAL duk_bool_t duk__obj_flag_any_default_false(duk_context *ctx, duk_idx_t index, duk_uint_t flag_mask)
DUK_INTERNAL void duk_push_hstring_stridx(duk_context *ctx, duk_small_int_t stridx)
DUK_EXTERNAL void * duk_get_buffer(duk_context *ctx, duk_idx_t index, duk_size_t *out_size)
DUK_INTERNAL duk_hobject * duk_require_hobject_with_class(duk_context *ctx, duk_idx_t index, duk_small_uint_t classnum)
DUK_LOCAL duk_int_t duk__try_push_vsprintf(duk_context *ctx, void *buf, duk_size_t sz, const char *fmt, va_list ap)
DUK_EXTERNAL void duk_require_stack(duk_context *ctx, duk_idx_t extra)
DUK_EXTERNAL duk_bool_t duk_instanceof(duk_context *ctx, duk_idx_t index1, duk_idx_t index2)
DUK_EXTERNAL const char * duk_to_lstring(duk_context *ctx, duk_idx_t index, duk_size_t *out_len)
DUK_EXTERNAL duk_idx_t duk_push_error_object_va_raw(duk_context *ctx, duk_errcode_t err_code, const char *filename, duk_int_t line, const char *fmt, va_list ap)
DUK_EXTERNAL void * duk_get_pointer(duk_context *ctx, duk_idx_t index)
DUK_EXTERNAL duk_bool_t duk_equals(duk_context *ctx, duk_idx_t index1, duk_idx_t index2)
DUK_EXTERNAL duk_bool_t duk_to_boolean(duk_context *ctx, duk_idx_t index)
DUK_EXTERNAL const char * duk_push_lstring(duk_context *ctx, const char *str, duk_size_t len)
DUK_EXTERNAL void duk_remove(duk_context *ctx, duk_idx_t index)
DUK_INTERNAL duk_hcompiledfunction * duk_require_hcompiledfunction(duk_context *ctx, duk_idx_t index)
DUK_LOCAL_DECL duk_idx_t duk__push_c_function_raw(duk_context *ctx, duk_c_function func, duk_idx_t nargs, duk_uint_t flags)
DUK_INTERNAL duk_hnativefunction * duk_get_hnativefunction(duk_context *ctx, duk_idx_t index)
DUK_EXTERNAL duk_idx_t duk_push_c_function(duk_context *ctx, duk_c_function func, duk_int_t nargs)
DUK_INTERNAL duk_hobject * duk_get_hobject_or_lfunc_coerce(duk_context *ctx, duk_idx_t index)
DUK_EXTERNAL void duk_push_buffer_object(duk_context *ctx, duk_idx_t idx_buffer, duk_size_t byte_offset, duk_size_t byte_length, duk_uint_t flags)
DUK_EXTERNAL const char * duk_api_global_filename
DUK_EXTERNAL void duk_push_null(duk_context *ctx)
DUK_EXTERNAL void duk_push_int(duk_context *ctx, duk_int_t val)
DUK_EXTERNAL void duk_pop_3(duk_context *ctx)
DUK_EXTERNAL duk_int_t duk_get_int(duk_context *ctx, duk_idx_t index)
duk_double_t(* duk__toint_coercer)(duk_hthread *thr, duk_tval *tv)
DUK_EXTERNAL void duk_push_current_function(duk_context *ctx)
static const duk_uint32_t duk__bufobj_flags_lookup[]
DUK_EXTERNAL void duk_xcopymove_raw(duk_context *to_ctx, duk_context *from_ctx, duk_idx_t count, duk_bool_t is_copy)
DUK_EXTERNAL duk_double_t duk_to_number(duk_context *ctx, duk_idx_t index)
DUK_LOCAL void duk__push_this_helper(duk_context *ctx, duk_small_uint_t check_object_coercible)
DUK_EXTERNAL void duk_dup_top(duk_context *ctx)
DUK_EXTERNAL duk_bool_t duk_is_number(duk_context *ctx, duk_idx_t index)
DUK_EXTERNAL duk_int_t duk_get_type(duk_context *ctx, duk_idx_t index)
DUK_EXTERNAL void duk_replace(duk_context *ctx, duk_idx_t to_index)
DUK_EXTERNAL void duk_push_pointer(duk_context *ctx, void *val)
DUK_INTERNAL duk_uint8_t duk_to_uint8clamped(duk_context *ctx, duk_idx_t index)
DUK_EXTERNAL duk_idx_t duk_push_thread_raw(duk_context *ctx, duk_uint_t flags)
DUK_EXTERNAL void duk_require_stack_top(duk_context *ctx, duk_idx_t top)
DUK_INTERNAL void duk_push_hobject_bidx(duk_context *ctx, duk_small_int_t builtin_idx)
DUK_EXTERNAL void * duk_require_pointer(duk_context *ctx, duk_idx_t index)
DUK_EXTERNAL void duk_to_defaultvalue(duk_context *ctx, duk_idx_t index, duk_int_t hint)
DUK_EXTERNAL const char * duk_get_string(duk_context *ctx, duk_idx_t index)
DUK_EXTERNAL duk_idx_t duk_push_c_lightfunc(duk_context *ctx, duk_c_function func, duk_idx_t nargs, duk_idx_t length, duk_int_t magic)
DUK_EXTERNAL duk_bool_t duk_is_bound_function(duk_context *ctx, duk_idx_t index)
DUK_INTERNAL duk_bool_t duk_valstack_resize_raw(duk_context *ctx, duk_size_t min_new_size, duk_small_uint_t flags)
DUK_EXTERNAL duk_bool_t duk_is_object(duk_context *ctx, duk_idx_t index)
DUK_EXTERNAL duk_uint_t duk_get_type_mask(duk_context *ctx, duk_idx_t index)
DUK_EXTERNAL duk_bool_t duk_is_dynamic_buffer(duk_context *ctx, duk_idx_t index)
DUK_INTERNAL duk_hobject * duk_require_hobject_or_lfunc_coerce(duk_context *ctx, duk_idx_t index)
DUK_EXTERNAL duk_uint_t duk_to_uint(duk_context *ctx, duk_idx_t index)
DUK_EXTERNAL void * duk_require_buffer(duk_context *ctx, duk_idx_t index, duk_size_t *out_size)
DUK_EXTERNAL duk_bool_t duk_is_lightfunc(duk_context *ctx, duk_idx_t index)
DUK_EXTERNAL void duk_set_top(duk_context *ctx, duk_idx_t index)
DUK_INTERNAL duk_hbuffer * duk_require_hbuffer(duk_context *ctx, duk_idx_t index)
DUK_EXTERNAL duk_idx_t duk_push_object(duk_context *ctx)
DUK_EXTERNAL duk_bool_t duk_is_boolean(duk_context *ctx, duk_idx_t index)
DUK_EXTERNAL void duk_push_heap_stash(duk_context *ctx)
DUK_EXTERNAL duk_idx_t duk_get_top_index(duk_context *ctx)
DUK_EXTERNAL void duk_dup(duk_context *ctx, duk_idx_t from_index)
DUK_EXTERNAL const char * duk_push_vsprintf(duk_context *ctx, const char *fmt, va_list ap)
DUK_EXTERNAL void duk_insert(duk_context *ctx, duk_idx_t to_index)
DUK_EXTERNAL void * duk_require_buffer_data(duk_context *ctx, duk_idx_t index, duk_size_t *out_size)
DUK_INTERNAL duk_hbuffer * duk_get_hbuffer(duk_context *ctx, duk_idx_t index)
DUK_EXTERNAL duk_bool_t duk_is_null(duk_context *ctx, duk_idx_t index)
DUK_EXTERNAL duk_bool_t duk_is_external_buffer(duk_context *ctx, duk_idx_t index)
DUK_EXTERNAL duk_idx_t duk_get_top(duk_context *ctx)
DUK_LOCAL duk_bool_t duk__tag_check(duk_context *ctx, duk_idx_t index, duk_small_uint_t tag)
DUK_EXTERNAL void duk_copy(duk_context *ctx, duk_idx_t from_index, duk_idx_t to_index)
DUK_INTERNAL duk_hobject * duk_require_hobject_or_lfunc(duk_context *ctx, duk_idx_t index)
DUK_LOCAL duk_int_t duk__api_coerce_d2i(duk_context *ctx, duk_idx_t index, duk_bool_t require)
DUK_EXTERNAL duk_idx_t duk_push_error_object_stash(duk_context *ctx, duk_errcode_t err_code, const char *fmt,...)
DUK_INTERNAL void duk_push_tval(duk_context *ctx, duk_tval *tv)
DUK_INTERNAL void duk_to_object_class_string_top(duk_context *ctx)
DUK_LOCAL_DECL duk_heaphdr * duk__get_tagged_heaphdr_raw(duk_context *ctx, duk_idx_t index, duk_uint_t tag)
DUK_INTERNAL duk_hbufferobject * duk_push_bufferobject_raw(duk_context *ctx, duk_uint_t hobject_flags_and_class, duk_small_int_t prototype_bidx)
DUK_EXTERNAL void duk_push_uint(duk_context *ctx, duk_uint_t val)
DUK_EXTERNAL void * duk_get_buffer_data(duk_context *ctx, duk_idx_t index, duk_size_t *out_size)
DUK_INTERNAL void duk_push_hobject_class_string(duk_context *ctx, duk_hobject *h)
DUK_EXTERNAL void duk_to_null(duk_context *ctx, duk_idx_t index)
DUK_EXTERNAL duk_bool_t duk_is_buffer(duk_context *ctx, duk_idx_t index)
DUK_EXTERNAL void duk_push_thread_stash(duk_context *ctx, duk_context *target_ctx)
DUK_EXTERNAL void * duk_get_heapptr(duk_context *ctx, duk_idx_t index)
DUK_EXTERNAL void duk_push_undefined(duk_context *ctx)
DUK_INTERNAL duk_hstring * duk_push_this_coercible_to_string(duk_context *ctx)
DUK_INTERNAL duk_hcompiledfunction * duk_get_hcompiledfunction(duk_context *ctx, duk_idx_t index)
DUK_EXTERNAL duk_int32_t duk_to_int32(duk_context *ctx, duk_idx_t index)
DUK_EXTERNAL duk_bool_t duk_is_array(duk_context *ctx, duk_idx_t index)
DUK_EXTERNAL duk_uint32_t duk_to_uint32(duk_context *ctx, duk_idx_t index)
DUK_LOCAL void * duk__get_buffer_data_helper(duk_context *ctx, duk_idx_t index, duk_size_t *out_size, duk_bool_t throw_flag)
DUK_EXTERNAL const char * duk_push_sprintf(duk_context *ctx, const char *fmt,...)
DUK_EXTERNAL void duk_swap_top(duk_context *ctx, duk_idx_t index)
DUK_EXTERNAL duk_bool_t duk_is_nan(duk_context *ctx, duk_idx_t index)
DUK_EXTERNAL duk_uint_t duk_get_uint(duk_context *ctx, duk_idx_t index)
DUK_EXTERNAL void duk_push_number(duk_context *ctx, duk_double_t val)
DUK_EXTERNAL void duk_pop_n(duk_context *ctx, duk_idx_t count)
DUK_INTERNAL duk_tval * duk_require_tval(duk_context *ctx, duk_idx_t index)
DUK_EXTERNAL void duk_fatal(duk_context *ctx, duk_errcode_t err_code, const char *err_msg)
DUK_EXTERNAL void duk_require_valid_index(duk_context *ctx, duk_idx_t index)
DUK_EXTERNAL void duk_require_function(duk_context *ctx, duk_idx_t index)
DUK_EXTERNAL void duk_push_false(duk_context *ctx)
DUK_EXTERNAL void duk_push_current_thread(duk_context *ctx)
DUK_EXTERNAL void duk_push_global_stash(duk_context *ctx)
DUK_EXTERNAL void duk_error_raw(duk_context *ctx, duk_errcode_t err_code, const char *filename, duk_int_t line, const char *fmt,...)
DUK_EXTERNAL const char * duk_require_lstring(duk_context *ctx, duk_idx_t index, duk_size_t *out_len)
DUK_INTERNAL duk_idx_t duk_push_object_helper(duk_context *ctx, duk_uint_t hobject_flags_and_class, duk_small_int_t prototype_bidx)
DUK_INTERNAL duk_hthread * duk_get_hthread(duk_context *ctx, duk_idx_t index)
DUK_EXTERNAL duk_bool_t duk_is_null_or_undefined(duk_context *ctx, duk_idx_t index)
DUK_INTERNAL duk_idx_t duk_push_compiledfunction(duk_context *ctx)
DUK_INTERNAL duk_idx_t duk_push_object_internal(duk_context *ctx)
DUK_EXTERNAL void duk_push_global_object(duk_context *ctx)
DUK_EXTERNAL void duk_to_object(duk_context *ctx, duk_idx_t index)
DUK_INTERNAL void duk_push_this_check_object_coercible(duk_context *ctx)
DUK_INTERNAL duk_int_t duk_to_int_clamped(duk_context *ctx, duk_idx_t index, duk_idx_t minval, duk_idx_t maxval)
DUK_EXTERNAL const char * duk_to_string(duk_context *ctx, duk_idx_t index)
DUK_INTERNAL duk_tval * duk_get_borrowed_this_tval(duk_context *ctx)
DUK_EXTERNAL duk_uint16_t duk_to_uint16(duk_context *ctx, duk_idx_t index)
DUK_EXTERNAL void duk_pop(duk_context *ctx)
DUK_EXTERNAL duk_bool_t duk_strict_equals(duk_context *ctx, duk_idx_t index1, duk_idx_t index2)
DUK_INTERNAL duk_hobject * duk_get_hobject(duk_context *ctx, duk_idx_t index)
DUK_INTERNAL void duk_push_hobject(duk_context *ctx, duk_hobject *h)
DUK_EXTERNAL duk_bool_t duk_check_stack(duk_context *ctx, duk_idx_t extra)
DUK_EXTERNAL duk_idx_t duk_push_array(duk_context *ctx)
DUK_EXTERNAL void * duk_to_buffer_raw(duk_context *ctx, duk_idx_t index, duk_size_t *out_size, duk_uint_t mode)
DUK_EXTERNAL void duk_push_nan(duk_context *ctx)
DUK_EXTERNAL duk_c_function duk_require_c_function(duk_context *ctx, duk_idx_t index)
DUK_INTERNAL void duk_push_hstring(duk_context *ctx, duk_hstring *h)
DUK_INTERNAL duk_hstring * duk_get_hstring(duk_context *ctx, duk_idx_t index)
DUK_EXTERNAL duk_c_function duk_get_c_function(duk_context *ctx, duk_idx_t index)
DUK_EXTERNAL duk_bool_t duk_check_stack_top(duk_context *ctx, duk_idx_t top)
DUK_INTERNAL duk_hobject * duk_push_this_coercible_to_object(duk_context *ctx)
DUK_EXTERNAL duk_bool_t duk_is_string(duk_context *ctx, duk_idx_t index)
DUK_INTERNAL duk_hstring * duk_require_hstring(duk_context *ctx, duk_idx_t index)
DUK_INTERNAL duk_hnativefunction * duk_require_hnativefunction(duk_context *ctx, duk_idx_t index)
DUK_LOCAL duk_bool_t duk__defaultvalue_coerce_attempt(duk_context *ctx, duk_idx_t index, duk_small_int_t func_stridx)
DUK_EXTERNAL duk_context * duk_get_context(duk_context *ctx, duk_idx_t index)
DUK_EXTERNAL duk_uint_t duk_require_uint(duk_context *ctx, duk_idx_t index)
DUK_EXTERNAL duk_int_t duk_require_int(duk_context *ctx, duk_idx_t index)
DUK_LOCAL duk_uint_t duk__api_coerce_d2ui(duk_context *ctx, duk_idx_t index, duk_bool_t require)
DUK_EXTERNAL const char * duk_require_string(duk_context *ctx, duk_idx_t index)
DUK_EXTERNAL void duk_error_va_raw(duk_context *ctx, duk_errcode_t err_code, const char *filename, duk_int_t line, const char *fmt, va_list ap)
DUK_INTERNAL const char * duk_push_string_tval_readable(duk_context *ctx, duk_tval *tv)
DUK_EXTERNAL void duk_require_undefined(duk_context *ctx, duk_idx_t index)
DUK_LOCAL void * duk__get_buffer_helper(duk_context *ctx, duk_idx_t index, duk_size_t *out_size, duk_bool_t throw_flag)
DUK_EXTERNAL duk_bool_t duk_is_c_function(duk_context *ctx, duk_idx_t index)
DUK_INTERNAL void duk_push_hbuffer(duk_context *ctx, duk_hbuffer *h)
DUK_EXTERNAL duk_idx_t duk_push_heapptr(duk_context *ctx, void *ptr)
DUK_EXTERNAL duk_bool_t duk_is_ecmascript_function(duk_context *ctx, duk_idx_t index)
DUK_LOCAL duk_ret_t duk__safe_to_string_raw(duk_context *ctx)
DUK_INTERNAL duk_hobject * duk_get_hobject_with_class(duk_context *ctx, duk_idx_t index, duk_small_uint_t classnum)
DUK_EXTERNAL duk_context * duk_require_context(duk_context *ctx, duk_idx_t index)
DUK_EXTERNAL duk_idx_t duk_push_error_object_raw(duk_context *ctx, duk_errcode_t err_code, const char *filename, duk_int_t line, const char *fmt,...)
DUK_EXTERNAL duk_int_t duk_to_int(duk_context *ctx, duk_idx_t index)
DUK_INTERNAL void duk_push_string_funcptr(duk_context *ctx, duk_uint8_t *ptr, duk_size_t sz)
DUK_INTERNAL duk_tval * duk_get_tval(duk_context *ctx, duk_idx_t index)
#define NULL
Definition gmacros.h:924
#define mask(n)
duk_fatal_function fatal_func
duk_hobject * builtins[DUK_NUM_BUILTINS]