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)
duk__duplicate_ram_global_object(duk_hthread * thr)48 DUK_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. */
63 	duk_push_object_helper(ctx,
64 	                       DUK_HOBJECT_FLAG_EXTENSIBLE |
65 	                       DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_GLOBAL),
66 	                       DUK_BIDX_GLOBAL);
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 	 */
74 	duk_push_object_helper(ctx,
75 	                       DUK_HOBJECT_FLAG_EXTENSIBLE |
76 	                       DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_GLOBAL),
77 	                       DUK_BIDX_OBJECT_PROTOTYPE);
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) {
91 		DUK_ERROR_ALLOC_DEFMSG(thr);
92 		return;
93 	}
94 	DUK_ASSERT(DUK_HOBJECT_GET_PROPS(thr->heap, h2) != NULL);
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 
102 	DUK_ASSERT(DUK_HOBJECT_GET_PROPS(thr->heap, h1) == NULL);
103 	DUK_HOBJECT_SET_PROPS(thr->heap, h1, props);
104 	DUK_HOBJECT_SET_ESIZE(h1, DUK_HOBJECT_GET_ESIZE(h2));
105 	DUK_HOBJECT_SET_ENEXT(h1, DUK_HOBJECT_GET_ENEXT(h2));
106 	DUK_HOBJECT_SET_ASIZE(h1, DUK_HOBJECT_GET_ASIZE(h2));
107 	DUK_HOBJECT_SET_HSIZE(h1, DUK_HOBJECT_GET_HSIZE(h2));
108 #else
109 #error internal error in defines
110 #endif
111 
112 	duk_hobject_compact_props(thr, h1);
113 	DUK_ASSERT(thr->builtins[DUK_BIDX_GLOBAL] != NULL);
114 	DUK_ASSERT(!DUK_HEAPHDR_NEEDS_REFCOUNT_UPDATE((duk_heaphdr *) thr->builtins[DUK_BIDX_GLOBAL]));  /* no need to decref */
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 	 */
124 	duk_push_object_helper(ctx,
125 	                       DUK_HOBJECT_FLAG_EXTENSIBLE |
126 	                       DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_OBJENV),
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 ] */
131 	duk_xdef_prop_stridx(thr, -2, DUK_STRIDX_INT_TARGET, DUK_PROPDESC_FLAGS_NONE);
132 	/* provideThis=false */
133 
134 	duk_hobject_compact_props(thr, h1);
135 	DUK_ASSERT(thr->builtins[DUK_BIDX_GLOBAL_ENV] != NULL);
136 	DUK_ASSERT(!DUK_HEAPHDR_NEEDS_REFCOUNT_UPDATE((duk_heaphdr *) thr->builtins[DUK_BIDX_GLOBAL_ENV]));  /* no need to decref */
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 
duk_hthread_create_builtin_objects(duk_hthread * thr)145 DUK_INTERNAL void duk_hthread_create_builtin_objects(duk_hthread *thr) {
146 	/* Setup builtins from ROM objects.  All heaps/threads will share
147 	 * the same readonly objects.
148 	 */
149 	duk_small_uint_t i;
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 */
duk__push_stridx(duk_context * ctx,duk_bitdecoder_ctx * bd)168 DUK_LOCAL void duk__push_stridx(duk_context *ctx, duk_bitdecoder_ctx *bd) {
169 	duk_small_uint_t n;
170 
171 	n = (duk_small_uint_t) duk_bd_decode(bd, DUK__STRIDX_BITS);
172 	DUK_ASSERT_DISABLE(n >= 0);  /* unsigned */
173 	DUK_ASSERT(n < DUK_HEAP_NUM_STRINGS);
174 	duk_push_hstring_stridx(ctx, n);
175 }
duk__push_string(duk_context * ctx,duk_bitdecoder_ctx * bd)176 DUK_LOCAL void duk__push_string(duk_context *ctx, duk_bitdecoder_ctx *bd) {
177 	duk_small_uint_t n;
178 	duk_small_uint_t i;
179 	duk_uint8_t *p;
180 
181 	n = (duk_small_uint_t) duk_bd_decode(bd, DUK__STRING_LENGTH_BITS);
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 }
duk__push_stridx_or_string(duk_context * ctx,duk_bitdecoder_ctx * bd)188 DUK_LOCAL void duk__push_stridx_or_string(duk_context *ctx, duk_bitdecoder_ctx *bd) {
189 	if (duk_bd_decode_flag(bd)) {
190 		duk__push_string(ctx, bd);
191 	} else {
192 		duk__push_stridx(ctx, bd);
193 	}
194 }
duk__push_double(duk_context * ctx,duk_bitdecoder_ctx * bd)195 DUK_LOCAL void duk__push_double(duk_context *ctx, duk_bitdecoder_ctx *bd) {
196 	duk_double_union du;
197 	duk_small_uint_t i;
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 
duk_hthread_create_builtin_objects(duk_hthread * thr)209 DUK_INTERNAL void duk_hthread_create_builtin_objects(duk_hthread *thr) {
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 
235 	duk_require_stack(ctx, DUK_NUM_ALL_BUILTINS);
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 
243 		class_num = (duk_small_uint_t) duk_bd_decode(bd, DUK__CLASS_BITS);
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 
255 			natidx = (duk_small_uint_t) duk_bd_decode(bd, DUK__NATIDX_BITS);
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 			 */
273 			DUK_ASSERT(DUK_HOBJECT_HAS_STRICT(h));
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 			 */
281 			duk__push_stridx_or_string(ctx, bd);
282 			duk_xdef_prop_stridx(ctx,
283 			                     -2,
284 			                     DUK_STRIDX_NAME,
285 			                     (i == DUK_BIDX_FUNCTION_PROTOTYPE) ?
286 			                         DUK_PROPDESC_FLAGS_W : DUK_PROPDESC_FLAGS_NONE);
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)) {
293 				DUK_ASSERT(DUK_HOBJECT_HAS_CONSTRUCTABLE(h));
294 			} else {
295 				DUK_HOBJECT_CLEAR_CONSTRUCTABLE(h);
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 
304 			duk_push_object_helper(ctx,
305 			                       DUK_HOBJECT_FLAG_EXTENSIBLE,
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);
334 			duk_xdef_prop_stridx(ctx,
335 			                     -2,
336 			                     DUK_STRIDX_LENGTH,
337 			                     (class_num == DUK_HOBJECT_CLASS_ARRAY ?  /* only Array.prototype matches */
338 			                      DUK_PROPDESC_FLAGS_W : DUK_PROPDESC_FLAGS_NONE));
339 		}
340 
341 		/* enable exotic behaviors last */
342 
343 		if (class_num == DUK_HOBJECT_CLASS_ARRAY) {
344 			DUK_HOBJECT_SET_EXOTIC_ARRAY(h);
345 		}
346 		if (class_num == DUK_HOBJECT_CLASS_STRING) {
347 			DUK_HOBJECT_SET_EXOTIC_STRINGOBJ(h);
348 		}
349 
350 		/* some assertions */
351 
352 		DUK_ASSERT(DUK_HOBJECT_HAS_EXTENSIBLE(h));
353 		/* DUK_HOBJECT_FLAG_CONSTRUCTABLE varies */
354 		DUK_ASSERT(!DUK_HOBJECT_HAS_BOUND(h));
355 		DUK_ASSERT(!DUK_HOBJECT_HAS_COMPILEDFUNCTION(h));
356 		/* DUK_HOBJECT_FLAG_NATIVEFUNCTION varies */
357 		DUK_ASSERT(!DUK_HOBJECT_HAS_THREAD(h));
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 */
361 		           DUK_HOBJECT_HAS_NEWENV(h));
362 		DUK_ASSERT(!DUK_HOBJECT_HAS_NAMEBINDING(h));
363 		DUK_ASSERT(!DUK_HOBJECT_HAS_CREATEARGS(h));
364 		DUK_ASSERT(!DUK_HOBJECT_HAS_ENVRECCLOSED(h));
365 		/* DUK_HOBJECT_FLAG_EXOTIC_ARRAY varies */
366 		/* DUK_HOBJECT_FLAG_EXOTIC_STRINGOBJ varies */
367 		DUK_ASSERT(!DUK_HOBJECT_HAS_EXOTIC_ARGUMENTS(h));
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++) {
379 		duk_small_uint_t t;
380 		duk_small_uint_t num;
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 
386 		t = (duk_small_uint_t) duk_bd_decode(bd, DUK__BIDX_BITS);
387 		if (t != DUK__NO_BIDX_MARKER) {
388 			DUK_DDD(DUK_DDDPRINT("set internal prototype: built-in %ld", (long) t));
389 			DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, h, duk_require_hobject(ctx, t));
390 		}
391 
392 		t = (duk_small_uint_t) duk_bd_decode(bd, DUK__BIDX_BITS);
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));
400 			duk_xdef_prop_stridx_builtin(ctx, i, DUK_STRIDX_PROTOTYPE, t, DUK_PROPDESC_FLAGS_NONE);
401 		}
402 
403 		t = (duk_small_uint_t) duk_bd_decode(bd, DUK__BIDX_BITS);
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));
411 			duk_xdef_prop_stridx_builtin(ctx, i, DUK_STRIDX_CONSTRUCTOR, t, DUK_PROPDESC_FLAGS_WC);
412 		}
413 
414 		/* normal valued properties */
415 		num = (duk_small_uint_t) duk_bd_decode(bd, DUK__NUM_NORMAL_PROPS_BITS);
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 
420 			duk__push_stridx_or_string(ctx, bd);
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)) {
430 				prop_flags = (duk_small_uint_t) duk_bd_decode(bd, DUK__PROP_FLAGS_BITS);
431 			} else {
432 				prop_flags = DUK_PROPDESC_FLAGS_WC;
433 			}
434 
435 			t = (duk_small_uint_t) duk_bd_decode(bd, DUK__PROP_TYPE_BITS);
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) {
441 			case DUK__PROP_TYPE_DOUBLE: {
442 				duk__push_double(ctx, bd);
443 				break;
444 			}
445 			case DUK__PROP_TYPE_STRING: {
446 				duk__push_string(ctx, bd);
447 				break;
448 			}
449 			case DUK__PROP_TYPE_STRIDX: {
450 				duk__push_stridx(ctx, bd);
451 				break;
452 			}
453 			case DUK__PROP_TYPE_BUILTIN: {
454 				duk_small_uint_t bidx;
455 
456 				bidx = (duk_small_uint_t) duk_bd_decode(bd, DUK__BIDX_BITS);
457 				DUK_ASSERT(bidx != DUK__NO_BIDX_MARKER);
458 				duk_dup(ctx, (duk_idx_t) bidx);
459 				break;
460 			}
461 			case DUK__PROP_TYPE_UNDEFINED: {
462 				duk_push_undefined(ctx);
463 				break;
464 			}
465 			case DUK__PROP_TYPE_BOOLEAN_TRUE: {
466 				duk_push_true(ctx);
467 				break;
468 			}
469 			case DUK__PROP_TYPE_BOOLEAN_FALSE: {
470 				duk_push_false(ctx);
471 				break;
472 			}
473 			case DUK__PROP_TYPE_ACCESSOR: {
474 				duk_small_uint_t natidx_getter = (duk_small_uint_t) duk_bd_decode(bd, DUK__NATIDX_BITS);
475 				duk_small_uint_t natidx_setter = (duk_small_uint_t) duk_bd_decode(bd, DUK__NATIDX_BITS);
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 */
495 				duk_hobject_define_accessor_internal(thr,
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 */
506 				DUK_UNREACHABLE();
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 */
518 		num = (duk_small_uint_t) duk_bd_decode(bd, DUK__NUM_FUNC_PROPS_BITS);
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 
532 			duk__push_stridx_or_string(ctx, bd);
533 			h_key = duk_get_hstring(ctx, -1);
534 			DUK_ASSERT(h_key != NULL);
535 			DUK_UNREF(h_key);
536 			natidx = (duk_small_uint_t) duk_bd_decode(bd, DUK__NATIDX_BITS);
537 
538 			c_length = (duk_small_uint_t) duk_bd_decode(bd, DUK__LENGTH_PROP_BITS);
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 			 */
596 			DUK_HOBJECT_SET_STRICT((duk_hobject *) h_func);
597 
598 			/* No built-in functions are constructable except the top
599 			 * level ones (Number, etc).
600 			 */
601 			DUK_ASSERT(!DUK_HOBJECT_HAS_CONSTRUCTABLE((duk_hobject *) h_func));
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);
611 			duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_LENGTH, DUK_PROPDESC_FLAGS_NONE);
612 
613 			duk_dup(ctx, -2);
614 			duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_NAME, DUK_PROPDESC_FLAGS_NONE);
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 
632 			duk_xdef_prop(ctx, i, DUK_PROPDESC_FLAGS_WC);
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 
655 	duk_get_prop_stridx(ctx, DUK_BIDX_DATE_PROTOTYPE, DUK_STRIDX_TO_UTC_STRING);
656 	duk_xdef_prop_stridx(ctx, DUK_BIDX_DATE_PROTOTYPE, DUK_STRIDX_TO_GMT_STRING, DUK_PROPDESC_FLAGS_WC);
657 
658 	h = duk_require_hobject(ctx, DUK_BIDX_DOUBLE_ERROR);
659 	DUK_ASSERT(h != NULL);
660 	DUK_HOBJECT_CLEAR_EXTENSIBLE(h);
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"));
664 	(void) duk_hobject_delprop_raw(thr, thr->builtins[DUK_BIDX_OBJECT_PROTOTYPE], DUK_HTHREAD_STRING___PROTO__(thr), DUK_DELPROP_FLAG_THROW);
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"));
669 	(void) duk_hobject_delprop_raw(thr, thr->builtins[DUK_BIDX_OBJECT_CONSTRUCTOR], DUK_HTHREAD_STRING_SET_PROTOTYPE_OF(thr), DUK_DELPROP_FLAG_THROW);
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 */
773 	                DUK_USE_ARCH_STRING
774 			" "
775 	                DUK_USE_OS_STRING
776 			" "
777 	                DUK_USE_COMPILER_STRING);
778 	duk_xdef_prop_stridx(ctx, DUK_BIDX_DUKTAPE, DUK_STRIDX_ENV, DUK_PROPDESC_FLAGS_WC);
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++) {
805 		duk_hobject_compact_props(thr, duk_require_hobject(ctx, 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 
duk_hthread_copy_builtin_objects(duk_hthread * thr_from,duk_hthread * thr_to)828 DUK_INTERNAL void duk_hthread_copy_builtin_objects(duk_hthread *thr_from, duk_hthread *thr_to) {
829 	duk_small_uint_t i;
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 }
836