Github User Fetcher 1.0.0
C Application with Server and GUI
Loading...
Searching...
No Matches
duktape-1.8.0/src-separate/duk_hthread_builtins.c
Go to the documentation of this file.
1/*
2 * Initialize built-in objects. Current thread must have a valstack
3 * and initialization errors may longjmp, so a setjmp() catch point
4 * must exist.
5 */
6
7#include "duk_internal.h"
8
9/*
10 * Encoding constants, must match genbuiltins.py
11 */
12
13#define DUK__CLASS_BITS 5
14#define DUK__BIDX_BITS 7
15#define DUK__STRIDX_BITS 9 /* XXX: try to optimize to 8 (would now be possible, <200 used) */
16#define DUK__NATIDX_BITS 8
17#define DUK__NUM_NORMAL_PROPS_BITS 6
18#define DUK__NUM_FUNC_PROPS_BITS 6
19#define DUK__PROP_FLAGS_BITS 3
20#define DUK__STRING_LENGTH_BITS 8
21#define DUK__STRING_CHAR_BITS 7
22#define DUK__LENGTH_PROP_BITS 3
23#define DUK__NARGS_BITS 3
24#define DUK__PROP_TYPE_BITS 3
25#define DUK__MAGIC_BITS 16
26
27#define DUK__NARGS_VARARGS_MARKER 0x07
28#define DUK__NO_CLASS_MARKER 0x00 /* 0 = DUK_HOBJECT_CLASS_UNUSED */
29#define DUK__NO_BIDX_MARKER 0x7f
30#define DUK__NO_STRIDX_MARKER 0xff
31
32#define DUK__PROP_TYPE_DOUBLE 0
33#define DUK__PROP_TYPE_STRING 1
34#define DUK__PROP_TYPE_STRIDX 2
35#define DUK__PROP_TYPE_BUILTIN 3
36#define DUK__PROP_TYPE_UNDEFINED 4
37#define DUK__PROP_TYPE_BOOLEAN_TRUE 5
38#define DUK__PROP_TYPE_BOOLEAN_FALSE 6
39#define DUK__PROP_TYPE_ACCESSOR 7
40
41/*
42 * Create built-in objects by parsing an init bitstream generated
43 * by genbuiltins.py.
44 */
45
46#if defined(DUK_USE_ROM_OBJECTS)
47#if defined(DUK_USE_ROM_GLOBAL_CLONE) || defined(DUK_USE_ROM_GLOBAL_INHERIT)
48DUK_LOCAL void duk__duplicate_ram_global_object(duk_hthread *thr) {
49 duk_context *ctx;
50 duk_hobject *h1;
51#if defined(DUK_USE_ROM_GLOBAL_CLONE)
52 duk_hobject *h2;
53 duk_uint8_t *props;
54 duk_size_t alloc_size;
55#endif
56
57 ctx = (duk_context *) thr;
58
59 /* XXX: refactor into internal helper, duk_clone_hobject() */
60
61#if defined(DUK_USE_ROM_GLOBAL_INHERIT)
62 /* Inherit from ROM-based global object: less RAM usage, less transparent. */
67 h1 = duk_get_hobject(ctx, -1);
68 DUK_ASSERT(h1 != NULL);
69#elif defined(DUK_USE_ROM_GLOBAL_CLONE)
70 /* Clone the properties of the ROM-based global object to create a
71 * fully RAM-based global object. Uses more memory than the inherit
72 * model but more compliant.
73 */
78 h1 = duk_get_hobject(ctx, -1);
79 DUK_ASSERT(h1 != NULL);
80 h2 = thr->builtins[DUK_BIDX_GLOBAL];
81 DUK_ASSERT(h2 != NULL);
82
83 /* Copy the property table verbatim; this handles attributes etc.
84 * For ROM objects it's not necessary (or possible) to update
85 * refcounts so leave them as is.
86 */
87 alloc_size = DUK_HOBJECT_P_ALLOC_SIZE(h2);
88 DUK_ASSERT(alloc_size > 0);
89 props = DUK_ALLOC(thr->heap, alloc_size);
90 if (!props) {
92 return;
93 }
95 DUK_MEMCPY((void *) props, (const void *) DUK_HOBJECT_GET_PROPS(thr->heap, h2), alloc_size);
96
97 /* XXX: keep property attributes or tweak them here?
98 * Properties will now be non-configurable even when they're
99 * normally configurable for the global object.
100 */
101
103 DUK_HOBJECT_SET_PROPS(thr->heap, h1, props);
108#else
109#error internal error in defines
110#endif
111
115 thr->builtins[DUK_BIDX_GLOBAL] = h1;
116 DUK_HOBJECT_INCREF(thr, h1);
117 DUK_D(DUK_DPRINT("duplicated global object: %!O", h1));
118
119
120 /* Create a fresh object environment for the global scope. This is
121 * needed so that the global scope points to the newly created RAM-based
122 * global object.
123 */
127 -1); /* no prototype */
128 h1 = duk_get_hobject(ctx, -1);
129 DUK_ASSERT(h1 != NULL);
130 duk_dup(ctx, -2); /* -> [ ... new_global new_globalenv new_global ] */
132 /* provideThis=false */
133
137 thr->builtins[DUK_BIDX_GLOBAL_ENV] = h1;
138 DUK_HOBJECT_INCREF(thr, h1);
139 DUK_D(DUK_DPRINT("duplicated global env: %!O", h1));
140
141 duk_pop_2(ctx);
142}
143#endif /* DUK_USE_ROM_GLOBAL_CLONE || DUK_USE_ROM_GLOBAL_INHERIT */
144
146 /* Setup builtins from ROM objects. All heaps/threads will share
147 * the same readonly objects.
148 */
150
151 for (i = 0; i < DUK_NUM_BUILTINS; i++) {
152 duk_hobject *h;
153 h = (duk_hobject *) DUK_LOSE_CONST(duk_rom_builtins_bidx[i]);
154 DUK_ASSERT(h != NULL);
155 thr->builtins[i] = h;
156 }
157
158#if defined(DUK_USE_ROM_GLOBAL_CLONE) || defined(DUK_USE_ROM_GLOBAL_INHERIT)
159 /* By default the global object is read-only which is often much
160 * more of an issue than having read-only built-in objects (like
161 * RegExp, Date, etc). Use a RAM-based copy of the global object
162 * and the global environment object for convenience.
163 */
164 duk__duplicate_ram_global_object(thr);
165#endif
166}
167#else /* DUK_USE_ROM_OBJECTS */
179 duk_uint8_t *p;
180
182 p = (duk_uint8_t *) duk_push_fixed_buffer(ctx, n);
183 for (i = 0; i < n; i++) {
184 *p++ = (duk_uint8_t) duk_bd_decode(bd, DUK__STRING_CHAR_BITS);
185 }
186 duk_to_string(ctx, -1);
187}
189 if (duk_bd_decode_flag(bd)) {
190 duk__push_string(ctx, bd);
191 } else {
192 duk__push_stridx(ctx, bd);
193 }
194}
198
199 for (i = 0; i < 8; i++) {
200 /* Encoding endianness must match target memory layout,
201 * build scripts and genbuiltins.py must ensure this.
202 */
203 du.uc[i] = (duk_uint8_t) duk_bd_decode(bd, 8);
204 }
205
206 duk_push_number(ctx, du.d); /* push operation normalizes NaNs */
207}
208
210 duk_context *ctx = (duk_context *) thr;
211 duk_bitdecoder_ctx bd_ctx;
212 duk_bitdecoder_ctx *bd = &bd_ctx; /* convenience */
213 duk_hobject *h;
214 duk_small_uint_t i, j;
215
216 DUK_D(DUK_DPRINT("INITBUILTINS BEGIN: DUK_NUM_BUILTINS=%d, DUK_NUM_BUILTINS_ALL=%d", (int) DUK_NUM_BUILTINS, (int) DUK_NUM_ALL_BUILTINS));
217
218 DUK_MEMZERO(&bd_ctx, sizeof(bd_ctx));
219 bd->data = (const duk_uint8_t *) duk_builtins_data;
220 bd->length = (duk_size_t) DUK_BUILTINS_DATA_LENGTH;
221
222 /*
223 * First create all built-in bare objects on the empty valstack.
224 *
225 * Built-ins in the index range [0,DUK_NUM_BUILTINS-1] have value
226 * stack indices matching their eventual thr->builtins[] index.
227 *
228 * Built-ins in the index range [DUK_NUM_BUILTINS,DUK_NUM_ALL_BUILTINS]
229 * will exist on the value stack during init but won't be placed
230 * into thr->builtins[]. These are objects referenced in some way
231 * from thr->builtins[] roots but which don't need to be indexed by
232 * Duktape through thr->builtins[] (e.g. user custom objects).
233 */
234
236
237 DUK_DD(DUK_DDPRINT("create empty built-ins"));
238 DUK_ASSERT_TOP(ctx, 0);
239 for (i = 0; i < DUK_NUM_ALL_BUILTINS; i++) {
240 duk_small_uint_t class_num;
241 duk_small_int_t len = -1; /* must be signed */
242
244 len = (duk_small_int_t) duk_bd_decode_flagged(bd, DUK__LENGTH_PROP_BITS, (duk_int32_t) -1 /*def_value*/);
245
246 if (class_num == DUK_HOBJECT_CLASS_FUNCTION) {
247 duk_small_uint_t natidx;
248 duk_int_t c_nargs; /* must hold DUK_VARARGS */
249 duk_c_function c_func;
250 duk_int16_t magic;
251
252 DUK_DDD(DUK_DDDPRINT("len=%ld", (long) len));
253 DUK_ASSERT(len >= 0);
254
256 c_func = duk_bi_native_functions[natidx];
257
258 c_nargs = (duk_small_uint_t) duk_bd_decode_flagged(bd, DUK__NARGS_BITS, len /*def_value*/);
259 if (c_nargs == DUK__NARGS_VARARGS_MARKER) {
260 c_nargs = DUK_VARARGS;
261 }
262
263 /* XXX: set magic directly here? (it could share the c_nargs arg) */
264 duk_push_c_function_noexotic(ctx, c_func, c_nargs);
265
266 h = duk_require_hobject(ctx, -1);
267 DUK_ASSERT(h != NULL);
268
269 /* Currently all built-in native functions are strict.
270 * duk_push_c_function() now sets strict flag, so
271 * assert for it.
272 */
274
275 /* XXX: function properties */
276
277 /* Built-in 'name' is not writable by default. Function '.name'
278 * is writable to allow user code to set a '.name' on a native
279 * function.
280 */
283 -2,
287
288 /* Almost all global level Function objects are constructable
289 * but not all: Function.prototype is a non-constructable,
290 * callable Function.
291 */
292 if (duk_bd_decode_flag(bd)) {
294 } else {
296 }
297
298 /* Cast converts magic to 16-bit signed value */
299 magic = (duk_int16_t) duk_bd_decode_flagged(bd, DUK__MAGIC_BITS, 0 /*def_value*/);
300 ((duk_hnativefunction *) h)->magic = magic;
301 } else {
302 /* XXX: ARRAY_PART for Array prototype? */
303
306 -1); /* no prototype or class yet */
307
308 h = duk_require_hobject(ctx, -1);
309 DUK_ASSERT(h != NULL);
310 }
311
312 DUK_HOBJECT_SET_CLASS_NUMBER(h, class_num);
313
314 if (i < DUK_NUM_BUILTINS) {
315 thr->builtins[i] = h;
316 DUK_HOBJECT_INCREF(thr, &h->hdr);
317 }
318
319 if (len >= 0) {
320 /*
321 * For top-level objects, 'length' property has the following
322 * default attributes: non-writable, non-enumerable, non-configurable
323 * (E5 Section 15).
324 *
325 * However, 'length' property for Array.prototype has attributes
326 * expected of an Array instance which are different: writable,
327 * non-enumerable, non-configurable (E5 Section 15.4.5.2).
328 *
329 * This is currently determined implicitly based on class; there are
330 * no attribute flags in the init data.
331 */
332
333 duk_push_int(ctx, len);
335 -2,
337 (class_num == DUK_HOBJECT_CLASS_ARRAY ? /* only Array.prototype matches */
339 }
340
341 /* enable exotic behaviors last */
342
343 if (class_num == DUK_HOBJECT_CLASS_ARRAY) {
345 }
346 if (class_num == DUK_HOBJECT_CLASS_STRING) {
348 }
349
350 /* some assertions */
351
353 /* DUK_HOBJECT_FLAG_CONSTRUCTABLE varies */
356 /* DUK_HOBJECT_FLAG_NATIVEFUNCTION varies */
358 DUK_ASSERT(!DUK_HOBJECT_HAS_ARRAY_PART(h)); /* currently, even for Array.prototype */
359 /* DUK_HOBJECT_FLAG_STRICT varies */
360 DUK_ASSERT(!DUK_HOBJECT_HAS_NATIVEFUNCTION(h) || /* all native functions have NEWENV */
365 /* DUK_HOBJECT_FLAG_EXOTIC_ARRAY varies */
366 /* DUK_HOBJECT_FLAG_EXOTIC_STRINGOBJ varies */
368
369 DUK_DDD(DUK_DDDPRINT("created built-in %ld, class=%ld, length=%ld", (long) i, (long) class_num, (long) len));
370 }
371
372 /*
373 * Then decode the builtins init data (see genbuiltins.py) to
374 * init objects
375 */
376
377 DUK_DD(DUK_DDPRINT("initialize built-in object properties"));
378 for (i = 0; i < DUK_NUM_ALL_BUILTINS; i++) {
381
382 DUK_DDD(DUK_DDDPRINT("initializing built-in object at index %ld", (long) i));
383 h = duk_require_hobject(ctx, i);
384 DUK_ASSERT(h != NULL);
385
387 if (t != DUK__NO_BIDX_MARKER) {
388 DUK_DDD(DUK_DDDPRINT("set internal prototype: built-in %ld", (long) t));
390 }
391
393 if (t != DUK__NO_BIDX_MARKER) {
394 /* 'prototype' property for all built-in objects (which have it) has attributes:
395 * [[Writable]] = false,
396 * [[Enumerable]] = false,
397 * [[Configurable]] = false
398 */
399 DUK_DDD(DUK_DDDPRINT("set external prototype: built-in %ld", (long) t));
401 }
402
404 if (t != DUK__NO_BIDX_MARKER) {
405 /* 'constructor' property for all built-in objects (which have it) has attributes:
406 * [[Writable]] = true,
407 * [[Enumerable]] = false,
408 * [[Configurable]] = true
409 */
410 DUK_DDD(DUK_DDDPRINT("set external constructor: built-in %ld", (long) t));
412 }
413
414 /* normal valued properties */
416 DUK_DDD(DUK_DDDPRINT("built-in object %ld, %ld normal valued properties", (long) i, (long) num));
417 for (j = 0; j < num; j++) {
418 duk_small_uint_t prop_flags;
419
421
422 /*
423 * Property attribute defaults are defined in E5 Section 15 (first
424 * few pages); there is a default for all properties and a special
425 * default for 'length' properties. Variation from the defaults is
426 * signaled using a single flag bit in the bitstream.
427 */
428
429 if (duk_bd_decode_flag(bd)) {
431 } else {
432 prop_flags = DUK_PROPDESC_FLAGS_WC;
433 }
434
436
437 DUK_DDD(DUK_DDDPRINT("built-in %ld, normal-valued property %ld, key %!T, flags 0x%02lx, type %ld",
438 (long) i, (long) j, duk_get_tval(ctx, -1), (unsigned long) prop_flags, (long) t));
439
440 switch (t) {
442 duk__push_double(ctx, bd);
443 break;
444 }
446 duk__push_string(ctx, bd);
447 break;
448 }
450 duk__push_stridx(ctx, bd);
451 break;
452 }
454 duk_small_uint_t bidx;
455
458 duk_dup(ctx, (duk_idx_t) bidx);
459 break;
460 }
463 break;
464 }
466 duk_push_true(ctx);
467 break;
468 }
470 duk_push_false(ctx);
471 break;
472 }
476 duk_c_function c_func_getter;
477 duk_c_function c_func_setter;
478
479 /* XXX: this is a bit awkward because there is no exposed helper
480 * in the API style, only this internal helper.
481 */
482 DUK_DDD(DUK_DDDPRINT("built-in accessor property: objidx=%ld, key=%!T, getteridx=%ld, setteridx=%ld, flags=0x%04lx",
483 (long) i, duk_get_tval(ctx, -1), (long) natidx_getter, (long) natidx_setter, (unsigned long) prop_flags));
484
485 c_func_getter = duk_bi_native_functions[natidx_getter];
486 c_func_setter = duk_bi_native_functions[natidx_setter];
487 duk_push_c_function_noconstruct_noexotic(ctx, c_func_getter, 0); /* always 0 args */
488 duk_push_c_function_noconstruct_noexotic(ctx, c_func_setter, 1); /* always 1 arg */
489
490 /* XXX: magic for getter/setter? use duk_def_prop()? */
491
492 DUK_ASSERT((prop_flags & DUK_PROPDESC_FLAG_WRITABLE) == 0); /* genbuiltins.py ensures */
493
494 prop_flags |= DUK_PROPDESC_FLAG_ACCESSOR; /* accessor flag not encoded explicitly */
496 duk_require_hobject(ctx, i),
497 duk_get_hstring(ctx, -3),
498 duk_require_hobject(ctx, -2),
499 duk_require_hobject(ctx, -1),
500 prop_flags);
501 duk_pop_3(ctx); /* key, getter and setter, now reachable through object */
502 goto skip_value;
503 }
504 default: {
505 /* exhaustive */
507 }
508 }
509
510 DUK_ASSERT((prop_flags & DUK_PROPDESC_FLAG_ACCESSOR) == 0);
511 duk_xdef_prop(ctx, i, prop_flags);
512
513 skip_value:
514 continue; /* avoid empty label at the end of a compound statement */
515 }
516
517 /* native function properties */
519 DUK_DDD(DUK_DDDPRINT("built-in object %ld, %ld function valued properties", (long) i, (long) num));
520 for (j = 0; j < num; j++) {
521 duk_hstring *h_key;
522 duk_small_uint_t natidx;
523 duk_int_t c_nargs; /* must hold DUK_VARARGS */
524 duk_small_uint_t c_length;
525 duk_int16_t magic;
526 duk_c_function c_func;
527 duk_hnativefunction *h_func;
528#if defined(DUK_USE_LIGHTFUNC_BUILTINS)
529 duk_small_int_t lightfunc_eligible;
530#endif
531
533 h_key = duk_get_hstring(ctx, -1);
534 DUK_ASSERT(h_key != NULL);
535 DUK_UNREF(h_key);
537
539 c_nargs = (duk_int_t) duk_bd_decode_flagged(bd, DUK__NARGS_BITS, (duk_int32_t) c_length /*def_value*/);
540 if (c_nargs == DUK__NARGS_VARARGS_MARKER) {
541 c_nargs = DUK_VARARGS;
542 }
543
544 c_func = duk_bi_native_functions[natidx];
545
546 DUK_DDD(DUK_DDDPRINT("built-in %ld, function-valued property %ld, key %!O, natidx %ld, length %ld, nargs %ld",
547 (long) i, (long) j, (duk_heaphdr *) h_key, (long) natidx, (long) c_length,
548 (c_nargs == DUK_VARARGS ? (long) -1 : (long) c_nargs)));
549
550 /* Cast converts magic to 16-bit signed value */
551 magic = (duk_int16_t) duk_bd_decode_flagged(bd, DUK__MAGIC_BITS, 0);
552
553#if defined(DUK_USE_LIGHTFUNC_BUILTINS)
554 lightfunc_eligible =
555 ((c_nargs >= DUK_LFUNC_NARGS_MIN && c_nargs <= DUK_LFUNC_NARGS_MAX) || (c_nargs == DUK_VARARGS)) &&
556 (c_length <= DUK_LFUNC_LENGTH_MAX) &&
557 (magic >= DUK_LFUNC_MAGIC_MIN && magic <= DUK_LFUNC_MAGIC_MAX);
558
559 if (h_key == DUK_HTHREAD_STRING_EVAL(thr) ||
560 h_key == DUK_HTHREAD_STRING_YIELD(thr) ||
561 h_key == DUK_HTHREAD_STRING_RESUME(thr) ||
562 h_key == DUK_HTHREAD_STRING_REQUIRE(thr)) {
563 /* These functions have trouble working as lightfuncs.
564 * Some of them have specific asserts and some may have
565 * additional properties (e.g. 'require.id' may be written).
566 */
567 DUK_D(DUK_DPRINT("reject as lightfunc: key=%!O, i=%d, j=%d", (duk_heaphdr *) h_key, (int) i, (int) j));
568 lightfunc_eligible = 0;
569 }
570
571 if (lightfunc_eligible) {
572 duk_tval tv_lfunc;
573 duk_small_uint_t lf_nargs = (c_nargs == DUK_VARARGS ? DUK_LFUNC_NARGS_VARARGS : c_nargs);
574 duk_small_uint_t lf_flags = DUK_LFUNC_FLAGS_PACK(magic, c_length, lf_nargs);
575 DUK_TVAL_SET_LIGHTFUNC(&tv_lfunc, c_func, lf_flags);
576 duk_push_tval(ctx, &tv_lfunc);
577 DUK_D(DUK_DPRINT("built-in function eligible as light function: i=%d, j=%d c_length=%ld, c_nargs=%ld, magic=%ld -> %!iT", (int) i, (int) j, (long) c_length, (long) c_nargs, (long) magic, duk_get_tval(ctx, -1)));
578 goto lightfunc_skip;
579 }
580
581 DUK_D(DUK_DPRINT("built-in function NOT ELIGIBLE as light function: i=%d, j=%d c_length=%ld, c_nargs=%ld, magic=%ld", (int) i, (int) j, (long) c_length, (long) c_nargs, (long) magic));
582#endif /* DUK_USE_LIGHTFUNC_BUILTINS */
583
584 /* [ (builtin objects) name ] */
585
586 duk_push_c_function_noconstruct_noexotic(ctx, c_func, c_nargs);
587 h_func = duk_require_hnativefunction(ctx, -1);
588 DUK_UNREF(h_func);
589
590 /* Currently all built-in native functions are strict.
591 * This doesn't matter for many functions, but e.g.
592 * String.prototype.charAt (and other string functions)
593 * rely on being strict so that their 'this' binding is
594 * not automatically coerced.
595 */
597
598 /* No built-in functions are constructable except the top
599 * level ones (Number, etc).
600 */
602
603 /* XXX: any way to avoid decoding magic bit; there are quite
604 * many function properties and relatively few with magic values.
605 */
606 h_func->magic = magic;
607
608 /* [ (builtin objects) name func ] */
609
610 duk_push_int(ctx, c_length);
612
613 duk_dup(ctx, -2);
615
616 /* XXX: other properties of function instances; 'arguments', 'caller'. */
617
618 DUK_DD(DUK_DDPRINT("built-in object %ld, function property %ld -> %!T",
619 (long) i, (long) j, (duk_tval *) duk_get_tval(ctx, -1)));
620
621 /* [ (builtin objects) name func ] */
622
623 /*
624 * The default property attributes are correct for all
625 * function valued properties of built-in objects now.
626 */
627
628#if defined(DUK_USE_LIGHTFUNC_BUILTINS)
629 lightfunc_skip:
630#endif
631
633
634 /* [ (builtin objects) ] */
635 }
636 }
637
638 /*
639 * Special post-tweaks, for cases not covered by the init data format.
640 *
641 * - Set Date.prototype.toGMTString to Date.prototype.toUTCString.
642 * toGMTString is required to have the same Function object as
643 * toUTCString in E5 Section B.2.6. Note that while Smjs respects
644 * this, V8 does not (the Function objects are distinct).
645 *
646 * - Make DoubleError non-extensible.
647 *
648 * - Add info about most important effective compile options to Duktape.
649 *
650 * - Possibly remove some properties (values or methods) which are not
651 * desirable with current feature options but are not currently
652 * conditional in init data.
653 */
654
657
659 DUK_ASSERT(h != NULL);
661
662#if !defined(DUK_USE_ES6_OBJECT_PROTO_PROPERTY)
663 DUK_DD(DUK_DDPRINT("delete Object.prototype.__proto__ built-in which is not enabled in features"));
665#endif
666
667#if !defined(DUK_USE_ES6_OBJECT_SETPROTOTYPEOF)
668 DUK_DD(DUK_DDPRINT("delete Object.setPrototypeOf built-in which is not enabled in features"));
670#endif
671
672 /* XXX: relocate */
673 duk_push_string(ctx,
674 /* Endianness indicator */
675#if defined(DUK_USE_INTEGER_LE)
676 "l"
677#elif defined(DUK_USE_INTEGER_BE)
678 "b"
679#elif defined(DUK_USE_INTEGER_ME) /* integer mixed endian not really used now */
680 "m"
681#else
682 "?"
683#endif
684#if defined(DUK_USE_DOUBLE_LE)
685 "l"
686#elif defined(DUK_USE_DOUBLE_BE)
687 "b"
688#elif defined(DUK_USE_DOUBLE_ME)
689 "m"
690#else
691 "?"
692#endif
693 " "
694 /* Packed or unpacked tval */
695#if defined(DUK_USE_PACKED_TVAL)
696 "p"
697#else
698 "u"
699#endif
700#if defined(DUK_USE_FASTINT)
701 "f"
702#endif
703 " "
704 /* Low memory options */
705#if defined(DUK_USE_STRTAB_CHAIN)
706 "c" /* chain */
707#elif defined(DUK_USE_STRTAB_PROBE)
708 "p" /* probe */
709#else
710 "?"
711#endif
712#if !defined(DUK_USE_HEAPPTR16) && !defined(DUK_DATAPTR16) && !defined(DUK_FUNCPTR16)
713 "n"
714#endif
715#if defined(DUK_USE_HEAPPTR16)
716 "h"
717#endif
718#if defined(DUK_USE_DATAPTR16)
719 "d"
720#endif
721#if defined(DUK_USE_FUNCPTR16)
722 "f"
723#endif
724#if defined(DUK_USE_REFCOUNT16)
725 "R"
726#endif
727#if defined(DUK_USE_STRHASH16)
728 "H"
729#endif
730#if defined(DUK_USE_STRLEN16)
731 "S"
732#endif
733#if defined(DUK_USE_BUFLEN16)
734 "B"
735#endif
736#if defined(DUK_USE_OBJSIZES16)
737 "O"
738#endif
739#if defined(DUK_USE_LIGHTFUNC_BUILTINS)
740 "L"
741#endif
742#if defined(DUK_USE_ROM_STRINGS) || defined(DUK_USE_ROM_OBJECTS)
743 /* XXX: This won't be shown in practice now
744 * because this code is not run when builtins
745 * are in ROM.
746 */
747 "Z"
748#endif
749 " "
750 /* Object property allocation layout */
751#if defined(DUK_USE_HOBJECT_LAYOUT_1)
752 "p1"
753#elif defined(DUK_USE_HOBJECT_LAYOUT_2)
754 "p2"
755#elif defined(DUK_USE_HOBJECT_LAYOUT_3)
756 "p3"
757#else
758 "p?"
759#endif
760 " "
761 /* Alignment guarantee */
762#if (DUK_USE_ALIGN_BY == 4)
763 "a4"
764#elif (DUK_USE_ALIGN_BY == 8)
765 "a8"
766#elif (DUK_USE_ALIGN_BY == 1)
767 "a1"
768#else
769#error invalid DUK_USE_ALIGN_BY
770#endif
771 " "
772 /* Architecture, OS, and compiler strings */
774 " "
776 " "
779
780 /*
781 * InitJS code - Ecmascript code evaluated from a built-in source
782 * which provides e.g. backward compatibility. User can also provide
783 * JS code to be evaluated at startup.
784 */
785
786#ifdef DUK_USE_BUILTIN_INITJS
787 /* XXX: compression */
788 DUK_DD(DUK_DDPRINT("running built-in initjs"));
789 duk_eval_string(ctx, (const char *) duk_initjs_data); /* initjs data is NUL terminated */
790 duk_pop(ctx);
791#endif /* DUK_USE_BUILTIN_INITJS */
792
793#ifdef DUK_USE_USER_INITJS
794 /* XXX: compression (as an option) */
795 DUK_DD(DUK_DDPRINT("running user initjs"));
796 duk_eval_string_noresult(ctx, (const char *) DUK_USE_USER_INITJS);
797#endif /* DUK_USE_USER_INITJS */
798
799 /*
800 * Since built-ins are not often extended, compact them.
801 */
802
803 DUK_DD(DUK_DDPRINT("compact built-ins"));
804 for (i = 0; i < DUK_NUM_ALL_BUILTINS; i++) {
806 }
807
808 DUK_D(DUK_DPRINT("INITBUILTINS END"));
809
810#ifdef DUK_USE_DDPRINT
811 for (i = 0; i < DUK_NUM_ALL_BUILTINS; i++) {
812 DUK_DD(DUK_DDPRINT("built-in object %ld after initialization and compacting: %!@iO",
813 (long) i, (duk_heaphdr *) duk_require_hobject(ctx, i)));
814 }
815#endif
816
817 /*
818 * Pop built-ins from stack: they are now INCREF'd and
819 * reachable from the builtins[] array or indirectly
820 * through builtins[].
821 */
822
823 duk_set_top(ctx, 0);
824 DUK_ASSERT_TOP(ctx, 0);
825}
826#endif /* DUK_USE_ROM_OBJECTS */
827
830
831 for (i = 0; i < DUK_NUM_BUILTINS; i++) {
832 thr_to->builtins[i] = thr_from->builtins[i];
833 DUK_HOBJECT_INCREF_ALLOWNULL(thr_to, thr_to->builtins[i]); /* side effect free */
834 }
835}
unsigned int duk_small_uint_t
#define DUK_LOSE_CONST(src)
duk_int_fast32_t duk_int_t
#define DUK_MEMZERO(p, n)
DUK_INTERNAL_DECL void duk_push_c_function_noexotic(duk_context *ctx, duk_c_function func, duk_int_t nargs)
DUK_INTERNAL_DECL duk_small_int_t duk_bd_decode_flag(duk_bitdecoder_ctx *ctx)
DUK_EXTERNAL void duk_pop_2(duk_context *ctx)
DUK_EXTERNAL const char * duk_push_string(duk_context *ctx, const char *str)
DUK_INTERNAL_DECL duk_hobject * duk_require_hobject(duk_context *ctx, duk_idx_t index)
#define DUK_HOBJECT_HAS_BOUND(h)
DUK_INTERNAL_DECL duk_bool_t duk_get_prop_stridx(duk_context *ctx, duk_idx_t obj_index, duk_small_int_t stridx)
DUK_EXTERNAL void duk_push_true(duk_context *ctx)
#define DUK_ALLOC(heap, size)
DUK_INTERNAL const duk_uint8_t duk_initjs_data[204]
#define DUK_STRIDX_TO_UTC_STRING
#define DUK_LFUNC_NARGS_MAX
#define DUK_DELPROP_FLAG_THROW
#define DUK_HOBJECT_GET_ENEXT(h)
#define DUK_HOBJECT_SET_EXOTIC_STRINGOBJ(h)
#define DUK_HOBJECT_GET_PROPS(heap, h)
#define DUK_BIDX_OBJECT_PROTOTYPE
DUK_INTERNAL_DECL void duk_xdef_prop_stridx_builtin(duk_context *ctx, duk_idx_t obj_index, duk_small_int_t stridx, duk_small_int_t builtin_idx, duk_small_uint_t desc_flags)
#define DUK_BIDX_DOUBLE_ERROR
DUK_INTERNAL_DECL void duk_push_c_function_noconstruct_noexotic(duk_context *ctx, duk_c_function func, duk_int_t nargs)
#define DUK_LFUNC_LENGTH_MAX
#define DUK_STRIDX_TO_GMT_STRING
#define DUK_HOBJECT_HAS_CREATEARGS(h)
#define DUK_BIDX_FUNCTION_PROTOTYPE
#define DUK_PROPDESC_FLAGS_NONE
DUK_INTERNAL_DECL void duk_push_hstring_stridx(duk_context *ctx, duk_small_int_t stridx)
#define DUK_HTHREAD_STRING___PROTO__(thr)
#define DUK_HOBJECT_CLASS_FUNCTION
DUK_EXTERNAL void duk_require_stack(duk_context *ctx, duk_idx_t extra)
#define DUK_HOBJECT_SET_PROPS(heap, h, x)
DUK_INTERNAL_DECL void duk_hobject_define_accessor_internal(duk_hthread *thr, duk_hobject *obj, duk_hstring *key, duk_hobject *getter, duk_hobject *setter, duk_small_uint_t propflags)
#define DUK_HOBJECT_CLASS_ARRAY
#define DUK_STRIDX_CONSTRUCTOR
#define DUK_PROPDESC_FLAG_ACCESSOR
#define DUK_HOBJECT_HAS_CONSTRUCTABLE(h)
#define DUK_HTHREAD_STRING_YIELD(thr)
#define DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, h, p)
#define DUK_HOBJECT_GET_ASIZE(h)
DUK_INTERNAL_DECL void duk_hobject_compact_props(duk_hthread *thr, duk_hobject *obj)
#define DUK_HOBJECT_SET_EXOTIC_ARRAY(h)
#define DUK_ASSERT_DISABLE(x)
DUK_EXTERNAL void duk_push_int(duk_context *ctx, duk_int_t val)
DUK_EXTERNAL void duk_pop_3(duk_context *ctx)
#define DUK_HOBJECT_CLASS_AS_FLAGS(v)
#define DUK_HOBJECT_SET_ESIZE(h, v)
#define DUK_ERROR_ALLOC_DEFMSG(thr)
#define DUK_LFUNC_MAGIC_MIN
DUK_INTERNAL const duk_c_function duk_bi_native_functions[149]
#define DUK_HOBJECT_SET_STRICT(h)
#define DUK_LFUNC_NARGS_VARARGS
DUK_INTERNAL_DECL duk_int32_t duk_bd_decode_flagged(duk_bitdecoder_ctx *ctx, duk_small_int_t bits, duk_int32_t def_value)
#define DUK_HTHREAD_STRING_RESUME(thr)
#define DUK_HOBJECT_HAS_EXOTIC_ARGUMENTS(h)
DUK_INTERNAL_DECL duk_bool_t duk_hobject_delprop_raw(duk_hthread *thr, duk_hobject *obj, duk_hstring *key, duk_small_uint_t flags)
#define DUK_HOBJECT_HAS_ARRAY_PART(h)
#define DUK_HOBJECT_CLASS_OBJENV
#define DUK_LFUNC_FLAGS_PACK(magic, length, nargs)
#define DUK_HOBJECT_GET_HSIZE(h)
DUK_EXTERNAL void duk_set_top(duk_context *ctx, duk_idx_t index)
#define DUK_HOBJECT_CLASS_GLOBAL
DUK_EXTERNAL void duk_dup(duk_context *ctx, duk_idx_t from_index)
#define DUK_HTHREAD_STRING_EVAL(thr)
DUK_INTERNAL_DECL void duk_push_tval(duk_context *ctx, duk_tval *tv)
#define DUK_HOBJECT_HAS_NATIVEFUNCTION(h)
#define DUK_HOBJECT_HAS_THREAD(h)
#define DUK_HOBJECT_SET_ENEXT(h, v)
DUK_EXTERNAL void duk_push_undefined(duk_context *ctx)
#define DUK_STRIDX_INT_TARGET
#define DUK_HTHREAD_STRING_REQUIRE(thr)
#define DUK_PROPDESC_FLAGS_WC
#define DUK_PROPDESC_FLAG_WRITABLE
DUK_EXTERNAL void duk_push_number(duk_context *ctx, duk_double_t val)
#define DUK_ASSERT_TOP(ctx, n)
DUK_EXTERNAL void duk_push_false(duk_context *ctx)
#define DUK_HOBJECT_FLAG_EXTENSIBLE
DUK_INTERNAL_DECL duk_idx_t duk_push_object_helper(duk_context *ctx, duk_uint_t hobject_flags_and_class, duk_small_int_t prototype_bidx)
#define DUK_HOBJECT_SET_ASIZE(h, v)
#define DUK_BIDX_OBJECT_CONSTRUCTOR
#define DUK_HOBJECT_HAS_NEWENV(h)
#define DUK_LFUNC_NARGS_MIN
DUK_EXTERNAL const char * duk_to_string(duk_context *ctx, duk_idx_t index)
#define DUK_HOBJECT_INCREF(thr, h)
DUK_EXTERNAL void duk_pop(duk_context *ctx)
#define DUK_TVAL_SET_LIGHTFUNC(tv, fp, flags)
DUK_INTERNAL_DECL duk_hobject * duk_get_hobject(duk_context *ctx, duk_idx_t index)
#define DUK_HOBJECT_SET_HSIZE(h, v)
#define DUK_HOBJECT_HAS_ENVRECCLOSED(h)
#define DUK_HOBJECT_CLEAR_CONSTRUCTABLE(h)
#define DUK_LFUNC_MAGIC_MAX
DUK_INTERNAL_DECL void duk_xdef_prop_stridx(duk_context *ctx, duk_idx_t obj_index, duk_small_int_t stridx, duk_small_uint_t desc_flags)
#define DUK_HOBJECT_CLEAR_EXTENSIBLE(h)
DUK_INTERNAL_DECL duk_hstring * duk_get_hstring(duk_context *ctx, duk_idx_t index)
#define DUK_HEAPHDR_NEEDS_REFCOUNT_UPDATE(h)
#define DUK_HOBJECT_GET_ESIZE(h)
DUK_INTERNAL_DECL duk_hnativefunction * duk_require_hnativefunction(duk_context *ctx, duk_idx_t index)
#define DUK_HOBJECT_HAS_EXTENSIBLE(h)
#define DUK_HOBJECT_CLASS_STRING
#define DUK_HOBJECT_HAS_STRICT(h)
#define DUK_HOBJECT_P_ALLOC_SIZE(h)
DUK_INTERNAL_DECL duk_int32_t duk_bd_decode(duk_bitdecoder_ctx *ctx, duk_small_int_t bits)
#define DUK_HOBJECT_SET_CLASS_NUMBER(h, v)
#define DUK_BIDX_DATE_PROTOTYPE
#define DUK_HOBJECT_INCREF_ALLOWNULL(thr, h)
#define DUK_HOBJECT_HAS_COMPILEDFUNCTION(h)
#define DUK_HTHREAD_STRING_SET_PROTOTYPE_OF(thr)
DUK_INTERNAL_DECL duk_tval * duk_get_tval(duk_context *ctx, duk_idx_t index)
#define DUK_HOBJECT_HAS_NAMEBINDING(h)
DUK_INTERNAL_DECL void duk_xdef_prop(duk_context *ctx, duk_idx_t obj_index, duk_small_uint_t desc_flags)
#define duk_push_fixed_buffer(ctx, size)
#define duk_eval_string(ctx, src)
duk_ret_t(* duk_c_function)(duk_context *ctx)
#define duk_eval_string_noresult(ctx, src)
DUK_INTERNAL void duk_hthread_create_builtin_objects(duk_hthread *thr)
DUK_LOCAL void duk__push_stridx(duk_context *ctx, duk_bitdecoder_ctx *bd)
DUK_LOCAL void duk__push_string(duk_context *ctx, duk_bitdecoder_ctx *bd)
DUK_LOCAL void duk__push_stridx_or_string(duk_context *ctx, duk_bitdecoder_ctx *bd)
DUK_LOCAL void duk__push_double(duk_context *ctx, duk_bitdecoder_ctx *bd)
DUK_INTERNAL void duk_hthread_copy_builtin_objects(duk_hthread *thr_from, duk_hthread *thr_to)
#define NULL
Definition gmacros.h:924
static void error(LoadState *S, const char *why)
duk_hobject * builtins[DUK_NUM_BUILTINS]