From 042fcb82b83d19bf08afd3367235ac71a60b3850 Mon Sep 17 00:00:00 2001 From: Vincent Sanders Date: Tue, 7 Jun 2016 16:01:04 +0100 Subject: Move javascript content handler as appropriate for updated source format --- javascript/duktape/duktape.c | 86513 ----------------------------------------- 1 file changed, 86513 deletions(-) delete mode 100644 javascript/duktape/duktape.c (limited to 'javascript/duktape/duktape.c') diff --git a/javascript/duktape/duktape.c b/javascript/duktape/duktape.c deleted file mode 100644 index eb4c77a04..000000000 --- a/javascript/duktape/duktape.c +++ /dev/null @@ -1,86513 +0,0 @@ -/* Omit from static analysis. */ -#ifndef __clang_analyzer__ -/* - * Single source autogenerated distributable for Duktape 1.5.0. - * - * Git commit 83d557704ee63f68ab40b6fcb00995c9b3d6777c (v1.5.0). - * Git branch master. - * - * See Duktape AUTHORS.rst and LICENSE.txt for copyright and - * licensing information. - */ - -/* LICENSE.txt */ -/* -* =============== -* Duktape license -* =============== -* -* (http://opensource.org/licenses/MIT) -* -* Copyright (c) 2013-2016 by Duktape authors (see AUTHORS.rst) -* -* Permission is hereby granted, free of charge, to any person obtaining a copy -* of this software and associated documentation files (the "Software"), to deal -* in the Software without restriction, including without limitation the rights -* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -* copies of the Software, and to permit persons to whom the Software is -* furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included in -* all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -* THE SOFTWARE. -*/ -/* AUTHORS.rst */ -/* -* =============== -* Duktape authors -* =============== -* -* Copyright -* ========= -* -* Duktape copyrights are held by its authors. Each author has a copyright -* to their contribution, and agrees to irrevocably license the contribution -* under the Duktape ``LICENSE.txt``. -* -* Authors -* ======= -* -* Please include an e-mail address, a link to your GitHub profile, or something -* similar to allow your contribution to be identified accurately. -* -* The following people have contributed code, website contents, or Wiki contents, -* and agreed to irrevocably license their contributions under the Duktape -* ``LICENSE.txt`` (in order of appearance): -* -* * Sami Vaarala -* * Niki Dobrev -* * Andreas \u00d6man -* * L\u00e1szl\u00f3 Lang\u00f3 -* * Legimet -* * Karl Skomski -* * Bruce Pascoe -* * Ren\u00e9 Hollander -* * Julien Hamaide (https://github.com/crazyjul) -* * Sebastian G\u00f6tte (https://github.com/jaseg) -* -* Other contributions -* =================== -* -* The following people have contributed something other than code (e.g. reported -* bugs, provided ideas, etc; roughly in order of appearance): -* -* * Greg Burns -* * Anthony Rabine -* * Carlos Costa -* * Aur\u00e9lien Bouilland -* * Preet Desai (Pris Matic) -* * judofyr (http://www.reddit.com/user/judofyr) -* * Jason Woofenden -* * Micha\u0142 Przyby\u015b -* * Anthony Howe -* * Conrad Pankoff -* * Jim Schimpf -* * Rajaran Gaunker (https://github.com/zimbabao) -* * Andreas \u00d6man -* * Doug Sanden -* * Josh Engebretson (https://github.com/JoshEngebretson) -* * Remo Eichenberger (https://github.com/remoe) -* * Mamod Mehyar (https://github.com/mamod) -* * David Demelier (https://github.com/markand) -* * Tim Caswell (https://github.com/creationix) -* * Mitchell Blank Jr (https://github.com/mitchblank) -* * https://github.com/yushli -* * Seo Sanghyeon (https://github.com/sanxiyn) -* * Han ChoongWoo (https://github.com/tunz) -* * Joshua Peek (https://github.com/josh) -* * Bruce E. Pascoe (https://github.com/fatcerberus) -* * https://github.com/Kelledin -* * https://github.com/sstruchtrup -* * Michael Drake (https://github.com/tlsa) -* * https://github.com/chris-y -* * Laurent Zubiaur (https://github.com/lzubiaur) -* * Ole Andr\u00e9 Vadla Ravn\u00e5s (https://github.com/oleavr) -* -* If you are accidentally missing from this list, send me an e-mail -* (``sami.vaarala@iki.fi``) and I'll fix the omission. -*/ -#line 1 "duk_internal.h" -/* - * Top-level include file to be used for all (internal) source files. - * - * Source files should not include individual header files, as they - * have not been designed to be individually included. - */ - -#ifndef DUK_INTERNAL_H_INCLUDED -#define DUK_INTERNAL_H_INCLUDED - -/* - * The 'duktape.h' header provides the public API, but also handles all - * compiler and platform specific feature detection, Duktape feature - * resolution, inclusion of system headers, etc. These have been merged - * because the public API is also dependent on e.g. detecting appropriate - * C types which is quite platform/compiler specific especially for a non-C99 - * build. The public API is also dependent on the resolved feature set. - * - * Some actions taken by the merged header (such as including system headers) - * are not appropriate for building a user application. The define - * DUK_COMPILING_DUKTAPE allows the merged header to skip/include some - * sections depending on what is being built. - */ - -#define DUK_COMPILING_DUKTAPE -#include "duktape.h" - -/* - * User declarations, e.g. prototypes for user functions used by Duktape - * macros. Concretely, if DUK_USE_PANIC_HANDLER is used and the macro - * value calls a user function, it needs to be declared for Duktape - * compilation to avoid warnings. - */ - -DUK_USE_USER_DECLARE() - -/* - * Duktape includes (other than duk_features.h) - * - * The header files expect to be included in an order which satisfies header - * dependencies correctly (the headers themselves don't include any other - * includes). Forward declarations are used to break circular struct/typedef - * dependencies. - */ - -#line 1 "duk_replacements.h" -#ifndef DUK_REPLACEMENTS_H_INCLUDED -#define DUK_REPLACEMENTS_H_INCLUDED - -#if !defined(DUK_SINGLE_FILE) -#if defined(DUK_USE_COMPUTED_INFINITY) -DUK_INTERNAL_DECL double duk_computed_infinity; -#endif -#if defined(DUK_USE_COMPUTED_NAN) -DUK_INTERNAL_DECL double duk_computed_nan; -#endif -#if defined(DUK_USE_REPL_FPCLASSIFY) -DUK_INTERNAL_DECL int duk_repl_fpclassify(double x); -#endif -#if defined(DUK_USE_REPL_SIGNBIT) -DUK_INTERNAL_DECL int duk_repl_signbit(double x); -#endif -#if defined(DUK_USE_REPL_ISFINITE) -DUK_INTERNAL_DECL int duk_repl_isfinite(double x); -#endif -#if defined(DUK_USE_REPL_ISNAN) -DUK_INTERNAL_DECL int duk_repl_isnan(double x); -#endif -#if defined(DUK_USE_REPL_ISINF) -DUK_INTERNAL_DECL int duk_repl_isinf(double x); -#endif -#endif /* !DUK_SINGLE_FILE */ - -#endif /* DUK_REPLACEMENTS_H_INCLUDED */ -#line 1 "duk_jmpbuf.h" -/* - * Wrapper for jmp_buf. - * - * This is used because jmp_buf is an array type for backward compatibility. - * Wrapping jmp_buf in a struct makes pointer references, sizeof, etc, - * behave more intuitively. - * - * http://en.wikipedia.org/wiki/Setjmp.h#Member_types - */ - -#ifndef DUK_JMPBUF_H_INCLUDED -#define DUK_JMPBUF_H_INCLUDED - -#if defined(DUK_USE_CPP_EXCEPTIONS) -struct duk_jmpbuf { - duk_small_int_t dummy; /* unused */ -}; -#else -struct duk_jmpbuf { - DUK_JMPBUF_TYPE jb; -}; -#endif - -#endif /* DUK_JMPBUF_H_INCLUDED */ -#line 1 "duk_exception.h" -/* - * Exception for Duktape internal throws when C++ exceptions are used - * for long control transfers. - * - * Doesn't inherit from any exception base class to minimize the chance - * that user code would accidentally catch this exception. - */ - -#ifndef DUK_EXCEPTION_H_INCLUDED -#define DUK_EXCEPTION_H_INCLUDED - -#if defined(DUK_USE_CPP_EXCEPTIONS) -class duk_internal_exception { - /* intentionally empty */ -}; -#endif - -#endif /* DUK_EXCEPTION_H_INCLUDED */ -#line 1 "duk_forwdecl.h" -/* - * Forward declarations for all Duktape structures. - */ - -#ifndef DUK_FORWDECL_H_INCLUDED -#define DUK_FORWDECL_H_INCLUDED - -/* - * Forward declarations - */ - -#if defined(DUK_USE_CPP_EXCEPTIONS) -class duk_internal_exception; -#else -struct duk_jmpbuf; -#endif - -/* duk_tval intentionally skipped */ -struct duk_heaphdr; -struct duk_heaphdr_string; -struct duk_hstring; -struct duk_hstring_external; -struct duk_hobject; -struct duk_hcompiledfunction; -struct duk_hnativefunction; -struct duk_hthread; -struct duk_hbufferobject; -struct duk_hbuffer; -struct duk_hbuffer_fixed; -struct duk_hbuffer_dynamic; -struct duk_hbuffer_external; - -struct duk_propaccessor; -union duk_propvalue; -struct duk_propdesc; - -struct duk_heap; -struct duk_breakpoint; - -struct duk_activation; -struct duk_catcher; -struct duk_strcache; -struct duk_ljstate; -struct duk_strtab_entry; - -#ifdef DUK_USE_DEBUG -struct duk_fixedbuffer; -#endif - -struct duk_bitdecoder_ctx; -struct duk_bitencoder_ctx; -struct duk_bufwriter_ctx; - -struct duk_token; -struct duk_re_token; -struct duk_lexer_point; -struct duk_lexer_ctx; -struct duk_lexer_codepoint; - -struct duk_compiler_instr; -struct duk_compiler_func; -struct duk_compiler_ctx; - -struct duk_re_matcher_ctx; -struct duk_re_compiler_ctx; - -#if defined(DUK_USE_CPP_EXCEPTIONS) -/* no typedef */ -#else -typedef struct duk_jmpbuf duk_jmpbuf; -#endif - -/* duk_tval intentionally skipped */ -typedef struct duk_heaphdr duk_heaphdr; -typedef struct duk_heaphdr_string duk_heaphdr_string; -typedef struct duk_hstring duk_hstring; -typedef struct duk_hstring_external duk_hstring_external; -typedef struct duk_hobject duk_hobject; -typedef struct duk_hcompiledfunction duk_hcompiledfunction; -typedef struct duk_hnativefunction duk_hnativefunction; -typedef struct duk_hbufferobject duk_hbufferobject; -typedef struct duk_hthread duk_hthread; -typedef struct duk_hbuffer duk_hbuffer; -typedef struct duk_hbuffer_fixed duk_hbuffer_fixed; -typedef struct duk_hbuffer_dynamic duk_hbuffer_dynamic; -typedef struct duk_hbuffer_external duk_hbuffer_external; - -typedef struct duk_propaccessor duk_propaccessor; -typedef union duk_propvalue duk_propvalue; -typedef struct duk_propdesc duk_propdesc; - -typedef struct duk_heap duk_heap; -typedef struct duk_breakpoint duk_breakpoint; - -typedef struct duk_activation duk_activation; -typedef struct duk_catcher duk_catcher; -typedef struct duk_strcache duk_strcache; -typedef struct duk_ljstate duk_ljstate; -typedef struct duk_strtab_entry duk_strtab_entry; - -#ifdef DUK_USE_DEBUG -typedef struct duk_fixedbuffer duk_fixedbuffer; -#endif - -typedef struct duk_bitdecoder_ctx duk_bitdecoder_ctx; -typedef struct duk_bitencoder_ctx duk_bitencoder_ctx; -typedef struct duk_bufwriter_ctx duk_bufwriter_ctx; - -typedef struct duk_token duk_token; -typedef struct duk_re_token duk_re_token; -typedef struct duk_lexer_point duk_lexer_point; -typedef struct duk_lexer_ctx duk_lexer_ctx; -typedef struct duk_lexer_codepoint duk_lexer_codepoint; - -typedef struct duk_compiler_instr duk_compiler_instr; -typedef struct duk_compiler_func duk_compiler_func; -typedef struct duk_compiler_ctx duk_compiler_ctx; - -typedef struct duk_re_matcher_ctx duk_re_matcher_ctx; -typedef struct duk_re_compiler_ctx duk_re_compiler_ctx; - -#endif /* DUK_FORWDECL_H_INCLUDED */ -#line 1 "duk_tval.h" -/* - * Tagged type definition (duk_tval) and accessor macros. - * - * Access all fields through the accessor macros, as the representation - * is quite tricky. - * - * There are two packed type alternatives: an 8-byte representation - * based on an IEEE double (preferred for compactness), and a 12-byte - * representation (portability). The latter is needed also in e.g. - * 64-bit environments (it usually pads to 16 bytes per value). - * - * Selecting the tagged type format involves many trade-offs (memory - * use, size and performance of generated code, portability, etc), - * see doc/types.rst for a detailed discussion (especially of how the - * IEEE double format is used to pack tagged values). - * - * NB: because macro arguments are often expressions, macros should - * avoid evaluating their argument more than once. - */ - -#ifndef DUK_TVAL_H_INCLUDED -#define DUK_TVAL_H_INCLUDED - -/* sanity */ -#if !defined(DUK_USE_DOUBLE_LE) && !defined(DUK_USE_DOUBLE_ME) && !defined(DUK_USE_DOUBLE_BE) -#error unsupported: cannot determine byte order variant -#endif - -#if defined(DUK_USE_PACKED_TVAL) -/* ======================================================================== */ - -/* - * Packed 8-byte representation - */ - -/* use duk_double_union as duk_tval directly */ -typedef union duk_double_union duk_tval; - -/* tags */ -#define DUK_TAG_NORMALIZED_NAN 0x7ff8UL /* the NaN variant we use */ -/* avoid tag 0xfff0, no risk of confusion with negative infinity */ -#if defined(DUK_USE_FASTINT) -#define DUK_TAG_FASTINT 0xfff1UL /* embed: integer value */ -#endif -#define DUK_TAG_UNUSED 0xfff2UL /* marker; not actual tagged value */ -#define DUK_TAG_UNDEFINED 0xfff3UL /* embed: nothing */ -#define DUK_TAG_NULL 0xfff4UL /* embed: nothing */ -#define DUK_TAG_BOOLEAN 0xfff5UL /* embed: 0 or 1 (false or true) */ -/* DUK_TAG_NUMBER would logically go here, but it has multiple 'tags' */ -#define DUK_TAG_POINTER 0xfff6UL /* embed: void ptr */ -#define DUK_TAG_LIGHTFUNC 0xfff7UL /* embed: func ptr */ -#define DUK_TAG_STRING 0xfff8UL /* embed: duk_hstring ptr */ -#define DUK_TAG_OBJECT 0xfff9UL /* embed: duk_hobject ptr */ -#define DUK_TAG_BUFFER 0xfffaUL /* embed: duk_hbuffer ptr */ - -/* for convenience */ -#define DUK_XTAG_BOOLEAN_FALSE 0xfff50000UL -#define DUK_XTAG_BOOLEAN_TRUE 0xfff50001UL - -/* two casts to avoid gcc warning: "warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]" */ -#if defined(DUK_USE_64BIT_OPS) -#if defined(DUK_USE_DOUBLE_ME) -#define DUK__TVAL_SET_TAGGEDPOINTER(v,h,tag) do { \ - (v)->ull[DUK_DBL_IDX_ULL0] = (((duk_uint64_t) (tag)) << 16) | (((duk_uint64_t) (duk_uint32_t) (h)) << 32); \ - } while (0) -#else -#define DUK__TVAL_SET_TAGGEDPOINTER(v,h,tag) do { \ - (v)->ull[DUK_DBL_IDX_ULL0] = (((duk_uint64_t) (tag)) << 48) | ((duk_uint64_t) (duk_uint32_t) (h)); \ - } while (0) -#endif -#else /* DUK_USE_64BIT_OPS */ -#define DUK__TVAL_SET_TAGGEDPOINTER(v,h,tag) do { \ - (v)->ui[DUK_DBL_IDX_UI0] = ((duk_uint32_t) (tag)) << 16; \ - (v)->ui[DUK_DBL_IDX_UI1] = (duk_uint32_t) (h); \ - } while (0) -#endif /* DUK_USE_64BIT_OPS */ - -#if defined(DUK_USE_64BIT_OPS) -/* Double casting for pointer to avoid gcc warning (cast from pointer to integer of different size) */ -#if defined(DUK_USE_DOUBLE_ME) -#define DUK__TVAL_SET_LIGHTFUNC(v,fp,flags) do { \ - (v)->ull[DUK_DBL_IDX_ULL0] = (((duk_uint64_t) DUK_TAG_LIGHTFUNC) << 16) | \ - ((duk_uint64_t) (flags)) | \ - (((duk_uint64_t) (duk_uint32_t) (fp)) << 32); \ - } while (0) -#else -#define DUK__TVAL_SET_LIGHTFUNC(v,fp,flags) do { \ - (v)->ull[DUK_DBL_IDX_ULL0] = (((duk_uint64_t) DUK_TAG_LIGHTFUNC) << 48) | \ - (((duk_uint64_t) (flags)) << 32) | \ - ((duk_uint64_t) (duk_uint32_t) (fp)); \ - } while (0) -#endif -#else /* DUK_USE_64BIT_OPS */ -#define DUK__TVAL_SET_LIGHTFUNC(v,fp,flags) do { \ - (v)->ui[DUK_DBL_IDX_UI0] = (((duk_uint32_t) DUK_TAG_LIGHTFUNC) << 16) | ((duk_uint32_t) (flags)); \ - (v)->ui[DUK_DBL_IDX_UI1] = (duk_uint32_t) (fp); \ - } while (0) -#endif /* DUK_USE_64BIT_OPS */ - -#if defined(DUK_USE_FASTINT) -/* Note: masking is done for 'i' to deal with negative numbers correctly */ -#if defined(DUK_USE_DOUBLE_ME) -#define DUK__TVAL_SET_FASTINT(v,i) do { \ - (v)->ui[DUK_DBL_IDX_UI0] = ((duk_uint32_t) DUK_TAG_FASTINT) << 16 | (((duk_uint32_t) ((i) >> 32)) & 0x0000ffffUL); \ - (v)->ui[DUK_DBL_IDX_UI1] = (duk_uint32_t) (i); \ - } while (0) -#define DUK__TVAL_SET_FASTINT_U32(v,i) do { \ - (v)->ui[DUK_DBL_IDX_UI0] = ((duk_uint32_t) DUK_TAG_FASTINT) << 16; \ - (v)->ui[DUK_DBL_IDX_UI1] = (duk_uint32_t) (i); \ - } while (0) -#else -#define DUK__TVAL_SET_FASTINT(v,i) do { \ - (v)->ull[DUK_DBL_IDX_ULL0] = (((duk_uint64_t) DUK_TAG_FASTINT) << 48) | (((duk_uint64_t) (i)) & 0x0000ffffffffffffULL); \ - } while (0) -#define DUK__TVAL_SET_FASTINT_U32(v,i) do { \ - (v)->ull[DUK_DBL_IDX_ULL0] = (((duk_uint64_t) DUK_TAG_FASTINT) << 48) | (duk_uint64_t) (i); \ - } while (0) -#endif - -#define DUK__TVAL_SET_FASTINT_I32(v,i) do { \ - duk_int64_t duk__tmp = (duk_int64_t) (i); \ - DUK_TVAL_SET_FASTINT((v), duk__tmp); \ - } while (0) - -/* XXX: clumsy sign extend and masking of 16 topmost bits */ -#if defined(DUK_USE_DOUBLE_ME) -#define DUK__TVAL_GET_FASTINT(v) (((duk_int64_t) ((((duk_uint64_t) (v)->ui[DUK_DBL_IDX_UI0]) << 32) | ((duk_uint64_t) (v)->ui[DUK_DBL_IDX_UI1]))) << 16 >> 16) -#else -#define DUK__TVAL_GET_FASTINT(v) ((((duk_int64_t) (v)->ull[DUK_DBL_IDX_ULL0]) << 16) >> 16) -#endif -#define DUK__TVAL_GET_FASTINT_U32(v) ((v)->ui[DUK_DBL_IDX_UI1]) -#define DUK__TVAL_GET_FASTINT_I32(v) ((duk_int32_t) (v)->ui[DUK_DBL_IDX_UI1]) -#endif /* DUK_USE_FASTINT */ - -#define DUK_TVAL_SET_UNDEFINED(v) do { \ - (v)->us[DUK_DBL_IDX_US0] = (duk_uint16_t) DUK_TAG_UNDEFINED; \ - } while (0) -#define DUK_TVAL_SET_UNUSED(v) do { \ - (v)->us[DUK_DBL_IDX_US0] = (duk_uint16_t) DUK_TAG_UNUSED; \ - } while (0) -#define DUK_TVAL_SET_NULL(v) do { \ - (v)->us[DUK_DBL_IDX_US0] = (duk_uint16_t) DUK_TAG_NULL; \ - } while (0) - -#define DUK_TVAL_SET_BOOLEAN(v,val) DUK_DBLUNION_SET_HIGH32((v), (((duk_uint32_t) DUK_TAG_BOOLEAN) << 16) | ((duk_uint32_t) (val))) - -#define DUK_TVAL_SET_NAN(v) DUK_DBLUNION_SET_NAN_FULL((v)) - -/* Assumes that caller has normalized NaNs, otherwise trouble ahead. */ -#if defined(DUK_USE_FASTINT) -#define DUK_TVAL_SET_DOUBLE(v,d) do { \ - duk_double_t duk__dblval; \ - duk__dblval = (d); \ - DUK_ASSERT_DOUBLE_IS_NORMALIZED(duk__dblval); \ - DUK_DBLUNION_SET_DOUBLE((v), duk__dblval); \ - } while (0) -#define DUK_TVAL_SET_FASTINT(v,i) DUK__TVAL_SET_FASTINT((v), (i)) -#define DUK_TVAL_SET_FASTINT_I32(v,i) DUK__TVAL_SET_FASTINT_I32((v), (i)) -#define DUK_TVAL_SET_FASTINT_U32(v,i) DUK__TVAL_SET_FASTINT_U32((v), (i)) -#define DUK_TVAL_SET_NUMBER_CHKFAST(v,d) duk_tval_set_number_chkfast((v), (d)) -#define DUK_TVAL_SET_NUMBER(v,d) DUK_TVAL_SET_DOUBLE((v), (d)) -#define DUK_TVAL_CHKFAST_INPLACE(v) do { \ - duk_tval *duk__tv; \ - duk_double_t duk__d; \ - duk__tv = (v); \ - if (DUK_TVAL_IS_DOUBLE(duk__tv)) { \ - duk__d = DUK_TVAL_GET_DOUBLE(duk__tv); \ - DUK_TVAL_SET_NUMBER_CHKFAST(duk__tv, duk__d); \ - } \ - } while (0) -#else -#define DUK_TVAL_SET_DOUBLE(v,d) do { \ - duk_double_t duk__dblval; \ - duk__dblval = (d); \ - DUK_ASSERT_DOUBLE_IS_NORMALIZED(duk__dblval); \ - DUK_DBLUNION_SET_DOUBLE((v), duk__dblval); \ - } while (0) -#define DUK_TVAL_SET_FASTINT(v,i) DUK_TVAL_SET_DOUBLE((v), (duk_double_t) (i)) /* XXX: fast int-to-double */ -#define DUK_TVAL_SET_FASTINT_I32(v,i) DUK_TVAL_SET_DOUBLE((v), (duk_double_t) (i)) -#define DUK_TVAL_SET_FASTINT_U32(v,i) DUK_TVAL_SET_DOUBLE((v), (duk_double_t) (i)) -#define DUK_TVAL_SET_NUMBER_CHKFAST(v,d) DUK_TVAL_SET_DOUBLE((v), (d)) -#define DUK_TVAL_SET_NUMBER(v,d) DUK_TVAL_SET_DOUBLE((v), (d)) -#define DUK_TVAL_CHKFAST_INPLACE(v) do { } while (0) -#endif - -#define DUK_TVAL_SET_LIGHTFUNC(v,fp,flags) DUK__TVAL_SET_LIGHTFUNC((v), (fp), (flags)) -#define DUK_TVAL_SET_STRING(v,h) DUK__TVAL_SET_TAGGEDPOINTER((v), (h), DUK_TAG_STRING) -#define DUK_TVAL_SET_OBJECT(v,h) DUK__TVAL_SET_TAGGEDPOINTER((v), (h), DUK_TAG_OBJECT) -#define DUK_TVAL_SET_BUFFER(v,h) DUK__TVAL_SET_TAGGEDPOINTER((v), (h), DUK_TAG_BUFFER) -#define DUK_TVAL_SET_POINTER(v,p) DUK__TVAL_SET_TAGGEDPOINTER((v), (p), DUK_TAG_POINTER) - -#define DUK_TVAL_SET_TVAL(v,x) do { *(v) = *(x); } while (0) - -/* getters */ -#define DUK_TVAL_GET_BOOLEAN(v) ((int) (v)->us[DUK_DBL_IDX_US1]) -#if defined(DUK_USE_FASTINT) -#define DUK_TVAL_GET_DOUBLE(v) ((v)->d) -#define DUK_TVAL_GET_FASTINT(v) DUK__TVAL_GET_FASTINT((v)) -#define DUK_TVAL_GET_FASTINT_U32(v) DUK__TVAL_GET_FASTINT_U32((v)) -#define DUK_TVAL_GET_FASTINT_I32(v) DUK__TVAL_GET_FASTINT_I32((v)) -#define DUK_TVAL_GET_NUMBER(v) duk_tval_get_number_packed((v)) -#else -#define DUK_TVAL_GET_NUMBER(v) ((v)->d) -#define DUK_TVAL_GET_DOUBLE(v) ((v)->d) -#endif -#define DUK_TVAL_GET_LIGHTFUNC(v,out_fp,out_flags) do { \ - (out_flags) = (v)->ui[DUK_DBL_IDX_UI0] & 0xffffUL; \ - (out_fp) = (duk_c_function) (v)->ui[DUK_DBL_IDX_UI1]; \ - } while (0) -#define DUK_TVAL_GET_LIGHTFUNC_FUNCPTR(v) ((duk_c_function) ((v)->ui[DUK_DBL_IDX_UI1])) -#define DUK_TVAL_GET_LIGHTFUNC_FLAGS(v) (((int) (v)->ui[DUK_DBL_IDX_UI0]) & 0xffffUL) -#define DUK_TVAL_GET_STRING(v) ((duk_hstring *) (v)->vp[DUK_DBL_IDX_VP1]) -#define DUK_TVAL_GET_OBJECT(v) ((duk_hobject *) (v)->vp[DUK_DBL_IDX_VP1]) -#define DUK_TVAL_GET_BUFFER(v) ((duk_hbuffer *) (v)->vp[DUK_DBL_IDX_VP1]) -#define DUK_TVAL_GET_POINTER(v) ((void *) (v)->vp[DUK_DBL_IDX_VP1]) -#define DUK_TVAL_GET_HEAPHDR(v) ((duk_heaphdr *) (v)->vp[DUK_DBL_IDX_VP1]) - -/* decoding */ -#define DUK_TVAL_GET_TAG(v) ((duk_small_uint_t) (v)->us[DUK_DBL_IDX_US0]) - -#define DUK_TVAL_IS_UNDEFINED(v) (DUK_TVAL_GET_TAG((v)) == DUK_TAG_UNDEFINED) -#define DUK_TVAL_IS_UNUSED(v) (DUK_TVAL_GET_TAG((v)) == DUK_TAG_UNUSED) -#define DUK_TVAL_IS_NULL(v) (DUK_TVAL_GET_TAG((v)) == DUK_TAG_NULL) -#define DUK_TVAL_IS_BOOLEAN(v) (DUK_TVAL_GET_TAG((v)) == DUK_TAG_BOOLEAN) -#define DUK_TVAL_IS_BOOLEAN_TRUE(v) ((v)->ui[DUK_DBL_IDX_UI0] == DUK_XTAG_BOOLEAN_TRUE) -#define DUK_TVAL_IS_BOOLEAN_FALSE(v) ((v)->ui[DUK_DBL_IDX_UI0] == DUK_XTAG_BOOLEAN_FALSE) -#define DUK_TVAL_IS_LIGHTFUNC(v) (DUK_TVAL_GET_TAG((v)) == DUK_TAG_LIGHTFUNC) -#define DUK_TVAL_IS_STRING(v) (DUK_TVAL_GET_TAG((v)) == DUK_TAG_STRING) -#define DUK_TVAL_IS_OBJECT(v) (DUK_TVAL_GET_TAG((v)) == DUK_TAG_OBJECT) -#define DUK_TVAL_IS_BUFFER(v) (DUK_TVAL_GET_TAG((v)) == DUK_TAG_BUFFER) -#define DUK_TVAL_IS_POINTER(v) (DUK_TVAL_GET_TAG((v)) == DUK_TAG_POINTER) -#if defined(DUK_USE_FASTINT) -/* 0xfff0 is -Infinity */ -#define DUK_TVAL_IS_DOUBLE(v) (DUK_TVAL_GET_TAG((v)) <= 0xfff0UL) -#define DUK_TVAL_IS_FASTINT(v) (DUK_TVAL_GET_TAG((v)) == DUK_TAG_FASTINT) -#define DUK_TVAL_IS_NUMBER(v) (DUK_TVAL_GET_TAG((v)) <= 0xfff1UL) -#else -#define DUK_TVAL_IS_NUMBER(v) (DUK_TVAL_GET_TAG((v)) <= 0xfff0UL) -#define DUK_TVAL_IS_DOUBLE(v) DUK_TVAL_IS_NUMBER((v)) -#endif - -/* This is performance critical because it appears in every DECREF. */ -#define DUK_TVAL_IS_HEAP_ALLOCATED(v) (DUK_TVAL_GET_TAG((v)) >= DUK_TAG_STRING) - -#if defined(DUK_USE_FASTINT) -DUK_INTERNAL_DECL duk_double_t duk_tval_get_number_packed(duk_tval *tv); -#endif - -#else /* DUK_USE_PACKED_TVAL */ -/* ======================================================================== */ - -/* - * Portable 12-byte representation - */ - -/* Note: not initializing all bytes is normally not an issue: Duktape won't - * read or use the uninitialized bytes so valgrind won't issue warnings. - * In some special cases a harmless valgrind warning may be issued though. - * For example, the DumpHeap debugger command writes out a compiled function's - * 'data' area as is, including any uninitialized bytes, which causes a - * valgrind warning. - */ - -typedef struct duk_tval_struct duk_tval; - -struct duk_tval_struct { - duk_small_uint_t t; - duk_small_uint_t v_extra; - union { - duk_double_t d; - duk_small_int_t i; -#if defined(DUK_USE_FASTINT) - duk_int64_t fi; /* if present, forces 16-byte duk_tval */ -#endif - void *voidptr; - duk_hstring *hstring; - duk_hobject *hobject; - duk_hcompiledfunction *hcompiledfunction; - duk_hnativefunction *hnativefunction; - duk_hthread *hthread; - duk_hbuffer *hbuffer; - duk_heaphdr *heaphdr; - duk_c_function lightfunc; - } v; -}; - -#define DUK__TAG_NUMBER 0 /* not exposed */ -#if defined(DUK_USE_FASTINT) -#define DUK_TAG_FASTINT 1 -#endif -#define DUK_TAG_UNDEFINED 2 -#define DUK_TAG_NULL 3 -#define DUK_TAG_BOOLEAN 4 -#define DUK_TAG_POINTER 5 -#define DUK_TAG_LIGHTFUNC 6 -#define DUK_TAG_UNUSED 7 /* marker; not actual tagged type */ -#define DUK_TAG_STRING 8 /* first heap allocated, match bit boundary */ -#define DUK_TAG_OBJECT 9 -#define DUK_TAG_BUFFER 10 - -/* DUK__TAG_NUMBER is intentionally first, as it is the default clause in code - * to support the 8-byte representation. Further, it is a non-heap-allocated - * type so it should come before DUK_TAG_STRING. Finally, it should not break - * the tag value ranges covered by case-clauses in a switch-case. - */ - -/* setters */ -#define DUK_TVAL_SET_UNDEFINED(tv) do { \ - (tv)->t = DUK_TAG_UNDEFINED; \ - } while (0) - -#define DUK_TVAL_SET_UNUSED(tv) do { \ - (tv)->t = DUK_TAG_UNUSED; \ - } while (0) - -#define DUK_TVAL_SET_NULL(tv) do { \ - (tv)->t = DUK_TAG_NULL; \ - } while (0) - -#define DUK_TVAL_SET_BOOLEAN(tv,val) do { \ - (tv)->t = DUK_TAG_BOOLEAN; \ - (tv)->v.i = (val); \ - } while (0) - -#if defined(DUK_USE_FASTINT) -#define DUK_TVAL_SET_DOUBLE(tv,val) do { \ - (tv)->t = DUK__TAG_NUMBER; \ - (tv)->v.d = (val); \ - } while (0) -#define DUK_TVAL_SET_FASTINT(tv,val) do { \ - (tv)->t = DUK_TAG_FASTINT; \ - (tv)->v.fi = (val); \ - } while (0) -#define DUK_TVAL_SET_FASTINT_U32(tv,val) do { \ - (tv)->t = DUK_TAG_FASTINT; \ - (tv)->v.fi = (duk_int64_t) (val); \ - } while (0) -#define DUK_TVAL_SET_FASTINT_I32(tv,val) do { \ - (tv)->t = DUK_TAG_FASTINT; \ - (tv)->v.fi = (duk_int64_t) (val); \ - } while (0) -#define DUK_TVAL_SET_NUMBER_CHKFAST(tv,d) \ - duk_tval_set_number_chkfast((tv), (d)) -#define DUK_TVAL_SET_NUMBER(tv,val) \ - DUK_TVAL_SET_DOUBLE((tv), (val)) -#define DUK_TVAL_CHKFAST_INPLACE(v) do { \ - duk_tval *duk__tv; \ - duk_double_t duk__d; \ - duk__tv = (v); \ - if (DUK_TVAL_IS_DOUBLE(duk__tv)) { \ - duk__d = DUK_TVAL_GET_DOUBLE(duk__tv); \ - DUK_TVAL_SET_NUMBER_CHKFAST(duk__tv, duk__d); \ - } \ - } while (0) -#else -#define DUK_TVAL_SET_DOUBLE(tv,d) \ - DUK_TVAL_SET_NUMBER((tv), (d)) -#define DUK_TVAL_SET_FASTINT(tv,val) \ - DUK_TVAL_SET_NUMBER((tv), (duk_double_t) (val)) /* XXX: fast int-to-double */ -#define DUK_TVAL_SET_FASTINT_U32(tv,val) \ - DUK_TVAL_SET_NUMBER((tv), (duk_double_t) (val)) -#define DUK_TVAL_SET_FASTINT_I32(tv,val) \ - DUK_TVAL_SET_NUMBER((tv), (duk_double_t) (val)) -#define DUK_TVAL_SET_NUMBER(tv,val) do { \ - (tv)->t = DUK__TAG_NUMBER; \ - (tv)->v.d = (val); \ - } while (0) -#define DUK_TVAL_SET_NUMBER_CHKFAST(tv,d) \ - DUK_TVAL_SET_NUMBER((tv), (d)) -#define DUK_TVAL_CHKFAST_INPLACE(v) do { } while (0) -#endif /* DUK_USE_FASTINT */ - -#define DUK_TVAL_SET_POINTER(tv,hptr) do { \ - (tv)->t = DUK_TAG_POINTER; \ - (tv)->v.voidptr = (hptr); \ - } while (0) - -#define DUK_TVAL_SET_LIGHTFUNC(tv,fp,flags) do { \ - (tv)->t = DUK_TAG_LIGHTFUNC; \ - (tv)->v_extra = (flags); \ - (tv)->v.lightfunc = (duk_c_function) (fp); \ - } while (0) - -#define DUK_TVAL_SET_STRING(tv,hptr) do { \ - (tv)->t = DUK_TAG_STRING; \ - (tv)->v.hstring = (hptr); \ - } while (0) - -#define DUK_TVAL_SET_OBJECT(tv,hptr) do { \ - (tv)->t = DUK_TAG_OBJECT; \ - (tv)->v.hobject = (hptr); \ - } while (0) - -#define DUK_TVAL_SET_BUFFER(tv,hptr) do { \ - (tv)->t = DUK_TAG_BUFFER; \ - (tv)->v.hbuffer = (hptr); \ - } while (0) - -#define DUK_TVAL_SET_NAN(tv) do { \ - /* in non-packed representation we don't care about which NaN is used */ \ - (tv)->t = DUK__TAG_NUMBER; \ - (tv)->v.d = DUK_DOUBLE_NAN; \ - } while (0) - -#define DUK_TVAL_SET_TVAL(v,x) do { *(v) = *(x); } while (0) - -/* getters */ -#define DUK_TVAL_GET_BOOLEAN(tv) ((tv)->v.i) -#if defined(DUK_USE_FASTINT) -#define DUK_TVAL_GET_DOUBLE(tv) ((tv)->v.d) -#define DUK_TVAL_GET_FASTINT(tv) ((tv)->v.fi) -#define DUK_TVAL_GET_FASTINT_U32(tv) ((duk_uint32_t) ((tv)->v.fi)) -#define DUK_TVAL_GET_FASTINT_I32(tv) ((duk_int32_t) ((tv)->v.fi)) -#if 0 -#define DUK_TVAL_GET_NUMBER(tv) (DUK_TVAL_IS_FASTINT((tv)) ? \ - (duk_double_t) DUK_TVAL_GET_FASTINT((tv)) : \ - DUK_TVAL_GET_DOUBLE((tv))) -#define DUK_TVAL_GET_NUMBER(tv) duk_tval_get_number_unpacked((tv)) -#else -/* This seems reasonable overall. */ -#define DUK_TVAL_GET_NUMBER(tv) (DUK_TVAL_IS_FASTINT((tv)) ? \ - duk_tval_get_number_unpacked_fastint((tv)) : \ - DUK_TVAL_GET_DOUBLE((tv))) -#endif -#else -#define DUK_TVAL_GET_NUMBER(tv) ((tv)->v.d) -#define DUK_TVAL_GET_DOUBLE(tv) ((tv)->v.d) -#endif /* DUK_USE_FASTINT */ -#define DUK_TVAL_GET_POINTER(tv) ((tv)->v.voidptr) -#define DUK_TVAL_GET_LIGHTFUNC(tv,out_fp,out_flags) do { \ - (out_flags) = (duk_uint32_t) (tv)->v_extra; \ - (out_fp) = (tv)->v.lightfunc; \ - } while (0) -#define DUK_TVAL_GET_LIGHTFUNC_FUNCPTR(tv) ((tv)->v.lightfunc) -#define DUK_TVAL_GET_LIGHTFUNC_FLAGS(tv) ((duk_uint32_t) ((tv)->v_extra)) -#define DUK_TVAL_GET_STRING(tv) ((tv)->v.hstring) -#define DUK_TVAL_GET_OBJECT(tv) ((tv)->v.hobject) -#define DUK_TVAL_GET_BUFFER(tv) ((tv)->v.hbuffer) -#define DUK_TVAL_GET_HEAPHDR(tv) ((tv)->v.heaphdr) - -/* decoding */ -#define DUK_TVAL_GET_TAG(tv) ((tv)->t) -#define DUK_TVAL_IS_UNDEFINED(tv) ((tv)->t == DUK_TAG_UNDEFINED) -#define DUK_TVAL_IS_UNUSED(tv) ((tv)->t == DUK_TAG_UNUSED) -#define DUK_TVAL_IS_NULL(tv) ((tv)->t == DUK_TAG_NULL) -#define DUK_TVAL_IS_BOOLEAN(tv) ((tv)->t == DUK_TAG_BOOLEAN) -#define DUK_TVAL_IS_BOOLEAN_TRUE(tv) (((tv)->t == DUK_TAG_BOOLEAN) && ((tv)->v.i != 0)) -#define DUK_TVAL_IS_BOOLEAN_FALSE(tv) (((tv)->t == DUK_TAG_BOOLEAN) && ((tv)->v.i == 0)) -#if defined(DUK_USE_FASTINT) -#define DUK_TVAL_IS_DOUBLE(tv) ((tv)->t == DUK__TAG_NUMBER) -#define DUK_TVAL_IS_FASTINT(tv) ((tv)->t == DUK_TAG_FASTINT) -#define DUK_TVAL_IS_NUMBER(tv) ((tv)->t == DUK__TAG_NUMBER || \ - (tv)->t == DUK_TAG_FASTINT) -#else -#define DUK_TVAL_IS_NUMBER(tv) ((tv)->t == DUK__TAG_NUMBER) -#define DUK_TVAL_IS_DOUBLE(v) DUK_TVAL_IS_NUMBER((v)) -#endif /* DUK_USE_FASTINT */ -#define DUK_TVAL_IS_POINTER(tv) ((tv)->t == DUK_TAG_POINTER) -#define DUK_TVAL_IS_LIGHTFUNC(tv) ((tv)->t == DUK_TAG_LIGHTFUNC) -#define DUK_TVAL_IS_STRING(tv) ((tv)->t == DUK_TAG_STRING) -#define DUK_TVAL_IS_OBJECT(tv) ((tv)->t == DUK_TAG_OBJECT) -#define DUK_TVAL_IS_BUFFER(tv) ((tv)->t == DUK_TAG_BUFFER) - -/* This is performance critical because it's needed for every DECREF. - * Take advantage of the fact that the first heap allocated tag is 8, - * so that bit 3 is set for all heap allocated tags (and never set for - * non-heap-allocated tags). - */ -#if 0 -#define DUK_TVAL_IS_HEAP_ALLOCATED(tv) ((tv)->t >= DUK_TAG_STRING) -#endif -#define DUK_TVAL_IS_HEAP_ALLOCATED(tv) ((tv)->t & 0x08) - -#if defined(DUK_USE_FASTINT) -#if 0 -DUK_INTERNAL_DECL duk_double_t duk_tval_get_number_unpacked(duk_tval *tv); -#endif -DUK_INTERNAL_DECL duk_double_t duk_tval_get_number_unpacked_fastint(duk_tval *tv); -#endif - -#endif /* DUK_USE_PACKED_TVAL */ - -/* - * Convenience (independent of representation) - */ - -#define DUK_TVAL_SET_BOOLEAN_TRUE(v) DUK_TVAL_SET_BOOLEAN(v, 1) -#define DUK_TVAL_SET_BOOLEAN_FALSE(v) DUK_TVAL_SET_BOOLEAN(v, 0) - -/* Lightfunc flags packing and unpacking. */ -/* Sign extend: 0x0000##00 -> 0x##000000 -> sign extend to 0xssssss## */ -#define DUK_LFUNC_FLAGS_GET_MAGIC(lf_flags) \ - ((((duk_int32_t) (lf_flags)) << 16) >> 24) -#define DUK_LFUNC_FLAGS_GET_LENGTH(lf_flags) \ - (((lf_flags) >> 4) & 0x0f) -#define DUK_LFUNC_FLAGS_GET_NARGS(lf_flags) \ - ((lf_flags) & 0x0f) -#define DUK_LFUNC_FLAGS_PACK(magic,length,nargs) \ - (((magic) & 0xff) << 8) | ((length) << 4) | (nargs) - -#define DUK_LFUNC_NARGS_VARARGS 0x0f /* varargs marker */ -#define DUK_LFUNC_NARGS_MIN 0x00 -#define DUK_LFUNC_NARGS_MAX 0x0e /* max, excl. varargs marker */ -#define DUK_LFUNC_LENGTH_MIN 0x00 -#define DUK_LFUNC_LENGTH_MAX 0x0f -#define DUK_LFUNC_MAGIC_MIN (-0x80) -#define DUK_LFUNC_MAGIC_MAX 0x7f - -/* fastint constants etc */ -#if defined(DUK_USE_FASTINT) -#define DUK_FASTINT_MIN (-0x800000000000LL) -#define DUK_FASTINT_MAX 0x7fffffffffffLL -#define DUK_FASTINT_BITS 48 - -DUK_INTERNAL_DECL void duk_tval_set_number_chkfast(duk_tval *tv, duk_double_t x); -#endif - -#endif /* DUK_TVAL_H_INCLUDED */ -#line 1 "duk_builtins.h" -/* - * Automatically generated by genbuiltins.py, do not edit! - */ - -#ifndef DUK_BUILTINS_H_INCLUDED -#define DUK_BUILTINS_H_INCLUDED - -#if defined(DUK_USE_ROM_STRINGS) -#error ROM support not enabled, rerun make_dist.py with --rom-support -#else /* DUK_USE_ROM_STRINGS */ -#define DUK_STRIDX_UC_UNDEFINED 0 /* 'Undefined' */ -#define DUK_HEAP_STRING_UC_UNDEFINED(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_UNDEFINED) -#define DUK_HTHREAD_STRING_UC_UNDEFINED(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_UNDEFINED) -#define DUK_STRIDX_UC_NULL 1 /* 'Null' */ -#define DUK_HEAP_STRING_UC_NULL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_NULL) -#define DUK_HTHREAD_STRING_UC_NULL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_NULL) -#define DUK_STRIDX_UC_ARGUMENTS 2 /* 'Arguments' */ -#define DUK_HEAP_STRING_UC_ARGUMENTS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_ARGUMENTS) -#define DUK_HTHREAD_STRING_UC_ARGUMENTS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_ARGUMENTS) -#define DUK_STRIDX_UC_OBJECT 3 /* 'Object' */ -#define DUK_HEAP_STRING_UC_OBJECT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_OBJECT) -#define DUK_HTHREAD_STRING_UC_OBJECT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_OBJECT) -#define DUK_STRIDX_UC_FUNCTION 4 /* 'Function' */ -#define DUK_HEAP_STRING_UC_FUNCTION(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_FUNCTION) -#define DUK_HTHREAD_STRING_UC_FUNCTION(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_FUNCTION) -#define DUK_STRIDX_ARRAY 5 /* 'Array' */ -#define DUK_HEAP_STRING_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ARRAY) -#define DUK_HTHREAD_STRING_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ARRAY) -#define DUK_STRIDX_UC_STRING 6 /* 'String' */ -#define DUK_HEAP_STRING_UC_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_STRING) -#define DUK_HTHREAD_STRING_UC_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_STRING) -#define DUK_STRIDX_UC_BOOLEAN 7 /* 'Boolean' */ -#define DUK_HEAP_STRING_UC_BOOLEAN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_BOOLEAN) -#define DUK_HTHREAD_STRING_UC_BOOLEAN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_BOOLEAN) -#define DUK_STRIDX_UC_NUMBER 8 /* 'Number' */ -#define DUK_HEAP_STRING_UC_NUMBER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_NUMBER) -#define DUK_HTHREAD_STRING_UC_NUMBER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_NUMBER) -#define DUK_STRIDX_DATE 9 /* 'Date' */ -#define DUK_HEAP_STRING_DATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DATE) -#define DUK_HTHREAD_STRING_DATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DATE) -#define DUK_STRIDX_REG_EXP 10 /* 'RegExp' */ -#define DUK_HEAP_STRING_REG_EXP(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_REG_EXP) -#define DUK_HTHREAD_STRING_REG_EXP(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_REG_EXP) -#define DUK_STRIDX_UC_ERROR 11 /* 'Error' */ -#define DUK_HEAP_STRING_UC_ERROR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_ERROR) -#define DUK_HTHREAD_STRING_UC_ERROR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_ERROR) -#define DUK_STRIDX_MATH 12 /* 'Math' */ -#define DUK_HEAP_STRING_MATH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MATH) -#define DUK_HTHREAD_STRING_MATH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MATH) -#define DUK_STRIDX_JSON 13 /* 'JSON' */ -#define DUK_HEAP_STRING_JSON(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON) -#define DUK_HTHREAD_STRING_JSON(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON) -#define DUK_STRIDX_EMPTY_STRING 14 /* '' */ -#define DUK_HEAP_STRING_EMPTY_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EMPTY_STRING) -#define DUK_HTHREAD_STRING_EMPTY_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EMPTY_STRING) -#define DUK_STRIDX_ARRAY_BUFFER 15 /* 'ArrayBuffer' */ -#define DUK_HEAP_STRING_ARRAY_BUFFER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ARRAY_BUFFER) -#define DUK_HTHREAD_STRING_ARRAY_BUFFER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ARRAY_BUFFER) -#define DUK_STRIDX_DATA_VIEW 16 /* 'DataView' */ -#define DUK_HEAP_STRING_DATA_VIEW(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DATA_VIEW) -#define DUK_HTHREAD_STRING_DATA_VIEW(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DATA_VIEW) -#define DUK_STRIDX_INT8_ARRAY 17 /* 'Int8Array' */ -#define DUK_HEAP_STRING_INT8_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT8_ARRAY) -#define DUK_HTHREAD_STRING_INT8_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT8_ARRAY) -#define DUK_STRIDX_UINT8_ARRAY 18 /* 'Uint8Array' */ -#define DUK_HEAP_STRING_UINT8_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UINT8_ARRAY) -#define DUK_HTHREAD_STRING_UINT8_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UINT8_ARRAY) -#define DUK_STRIDX_UINT8_CLAMPED_ARRAY 19 /* 'Uint8ClampedArray' */ -#define DUK_HEAP_STRING_UINT8_CLAMPED_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UINT8_CLAMPED_ARRAY) -#define DUK_HTHREAD_STRING_UINT8_CLAMPED_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UINT8_CLAMPED_ARRAY) -#define DUK_STRIDX_INT16_ARRAY 20 /* 'Int16Array' */ -#define DUK_HEAP_STRING_INT16_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT16_ARRAY) -#define DUK_HTHREAD_STRING_INT16_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT16_ARRAY) -#define DUK_STRIDX_UINT16_ARRAY 21 /* 'Uint16Array' */ -#define DUK_HEAP_STRING_UINT16_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UINT16_ARRAY) -#define DUK_HTHREAD_STRING_UINT16_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UINT16_ARRAY) -#define DUK_STRIDX_INT32_ARRAY 22 /* 'Int32Array' */ -#define DUK_HEAP_STRING_INT32_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT32_ARRAY) -#define DUK_HTHREAD_STRING_INT32_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT32_ARRAY) -#define DUK_STRIDX_UINT32_ARRAY 23 /* 'Uint32Array' */ -#define DUK_HEAP_STRING_UINT32_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UINT32_ARRAY) -#define DUK_HTHREAD_STRING_UINT32_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UINT32_ARRAY) -#define DUK_STRIDX_FLOAT32_ARRAY 24 /* 'Float32Array' */ -#define DUK_HEAP_STRING_FLOAT32_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FLOAT32_ARRAY) -#define DUK_HTHREAD_STRING_FLOAT32_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FLOAT32_ARRAY) -#define DUK_STRIDX_FLOAT64_ARRAY 25 /* 'Float64Array' */ -#define DUK_HEAP_STRING_FLOAT64_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FLOAT64_ARRAY) -#define DUK_HTHREAD_STRING_FLOAT64_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FLOAT64_ARRAY) -#define DUK_STRIDX_GLOBAL 26 /* 'global' */ -#define DUK_HEAP_STRING_GLOBAL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GLOBAL) -#define DUK_HTHREAD_STRING_GLOBAL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GLOBAL) -#define DUK_STRIDX_OBJ_ENV 27 /* 'ObjEnv' */ -#define DUK_HEAP_STRING_OBJ_ENV(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_OBJ_ENV) -#define DUK_HTHREAD_STRING_OBJ_ENV(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_OBJ_ENV) -#define DUK_STRIDX_DEC_ENV 28 /* 'DecEnv' */ -#define DUK_HEAP_STRING_DEC_ENV(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DEC_ENV) -#define DUK_HTHREAD_STRING_DEC_ENV(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DEC_ENV) -#define DUK_STRIDX_UC_BUFFER 29 /* 'Buffer' */ -#define DUK_HEAP_STRING_UC_BUFFER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_BUFFER) -#define DUK_HTHREAD_STRING_UC_BUFFER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_BUFFER) -#define DUK_STRIDX_UC_POINTER 30 /* 'Pointer' */ -#define DUK_HEAP_STRING_UC_POINTER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_POINTER) -#define DUK_HTHREAD_STRING_UC_POINTER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_POINTER) -#define DUK_STRIDX_UC_THREAD 31 /* 'Thread' */ -#define DUK_HEAP_STRING_UC_THREAD(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_THREAD) -#define DUK_HTHREAD_STRING_UC_THREAD(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_THREAD) -#define DUK_STRIDX_EVAL 32 /* 'eval' */ -#define DUK_HEAP_STRING_EVAL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EVAL) -#define DUK_HTHREAD_STRING_EVAL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EVAL) -#define DUK_STRIDX_DEFINE_PROPERTY 33 /* 'defineProperty' */ -#define DUK_HEAP_STRING_DEFINE_PROPERTY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DEFINE_PROPERTY) -#define DUK_HTHREAD_STRING_DEFINE_PROPERTY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DEFINE_PROPERTY) -#define DUK_STRIDX_VALUE 34 /* 'value' */ -#define DUK_HEAP_STRING_VALUE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_VALUE) -#define DUK_HTHREAD_STRING_VALUE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_VALUE) -#define DUK_STRIDX_WRITABLE 35 /* 'writable' */ -#define DUK_HEAP_STRING_WRITABLE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITABLE) -#define DUK_HTHREAD_STRING_WRITABLE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITABLE) -#define DUK_STRIDX_CONFIGURABLE 36 /* 'configurable' */ -#define DUK_HEAP_STRING_CONFIGURABLE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CONFIGURABLE) -#define DUK_HTHREAD_STRING_CONFIGURABLE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CONFIGURABLE) -#define DUK_STRIDX_ENUMERABLE 37 /* 'enumerable' */ -#define DUK_HEAP_STRING_ENUMERABLE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ENUMERABLE) -#define DUK_HTHREAD_STRING_ENUMERABLE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ENUMERABLE) -#define DUK_STRIDX_JOIN 38 /* 'join' */ -#define DUK_HEAP_STRING_JOIN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JOIN) -#define DUK_HTHREAD_STRING_JOIN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JOIN) -#define DUK_STRIDX_TO_LOCALE_STRING 39 /* 'toLocaleString' */ -#define DUK_HEAP_STRING_TO_LOCALE_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_LOCALE_STRING) -#define DUK_HTHREAD_STRING_TO_LOCALE_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_LOCALE_STRING) -#define DUK_STRIDX_VALUE_OF 40 /* 'valueOf' */ -#define DUK_HEAP_STRING_VALUE_OF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_VALUE_OF) -#define DUK_HTHREAD_STRING_VALUE_OF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_VALUE_OF) -#define DUK_STRIDX_TO_UTC_STRING 41 /* 'toUTCString' */ -#define DUK_HEAP_STRING_TO_UTC_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_UTC_STRING) -#define DUK_HTHREAD_STRING_TO_UTC_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_UTC_STRING) -#define DUK_STRIDX_TO_ISO_STRING 42 /* 'toISOString' */ -#define DUK_HEAP_STRING_TO_ISO_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_ISO_STRING) -#define DUK_HTHREAD_STRING_TO_ISO_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_ISO_STRING) -#define DUK_STRIDX_TO_GMT_STRING 43 /* 'toGMTString' */ -#define DUK_HEAP_STRING_TO_GMT_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_GMT_STRING) -#define DUK_HTHREAD_STRING_TO_GMT_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_GMT_STRING) -#define DUK_STRIDX_SOURCE 44 /* 'source' */ -#define DUK_HEAP_STRING_SOURCE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SOURCE) -#define DUK_HTHREAD_STRING_SOURCE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SOURCE) -#define DUK_STRIDX_IGNORE_CASE 45 /* 'ignoreCase' */ -#define DUK_HEAP_STRING_IGNORE_CASE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IGNORE_CASE) -#define DUK_HTHREAD_STRING_IGNORE_CASE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IGNORE_CASE) -#define DUK_STRIDX_MULTILINE 46 /* 'multiline' */ -#define DUK_HEAP_STRING_MULTILINE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MULTILINE) -#define DUK_HTHREAD_STRING_MULTILINE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MULTILINE) -#define DUK_STRIDX_LAST_INDEX 47 /* 'lastIndex' */ -#define DUK_HEAP_STRING_LAST_INDEX(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LAST_INDEX) -#define DUK_HTHREAD_STRING_LAST_INDEX(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LAST_INDEX) -#define DUK_STRIDX_ESCAPED_EMPTY_REGEXP 48 /* '(?:)' */ -#define DUK_HEAP_STRING_ESCAPED_EMPTY_REGEXP(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ESCAPED_EMPTY_REGEXP) -#define DUK_HTHREAD_STRING_ESCAPED_EMPTY_REGEXP(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ESCAPED_EMPTY_REGEXP) -#define DUK_STRIDX_INDEX 49 /* 'index' */ -#define DUK_HEAP_STRING_INDEX(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INDEX) -#define DUK_HTHREAD_STRING_INDEX(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INDEX) -#define DUK_STRIDX_PROTOTYPE 50 /* 'prototype' */ -#define DUK_HEAP_STRING_PROTOTYPE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PROTOTYPE) -#define DUK_HTHREAD_STRING_PROTOTYPE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PROTOTYPE) -#define DUK_STRIDX_CONSTRUCTOR 51 /* 'constructor' */ -#define DUK_HEAP_STRING_CONSTRUCTOR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CONSTRUCTOR) -#define DUK_HTHREAD_STRING_CONSTRUCTOR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CONSTRUCTOR) -#define DUK_STRIDX_MESSAGE 52 /* 'message' */ -#define DUK_HEAP_STRING_MESSAGE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MESSAGE) -#define DUK_HTHREAD_STRING_MESSAGE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MESSAGE) -#define DUK_STRIDX_LC_BOOLEAN 53 /* 'boolean' */ -#define DUK_HEAP_STRING_LC_BOOLEAN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_BOOLEAN) -#define DUK_HTHREAD_STRING_LC_BOOLEAN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_BOOLEAN) -#define DUK_STRIDX_LC_NUMBER 54 /* 'number' */ -#define DUK_HEAP_STRING_LC_NUMBER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_NUMBER) -#define DUK_HTHREAD_STRING_LC_NUMBER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_NUMBER) -#define DUK_STRIDX_LC_STRING 55 /* 'string' */ -#define DUK_HEAP_STRING_LC_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_STRING) -#define DUK_HTHREAD_STRING_LC_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_STRING) -#define DUK_STRIDX_LC_OBJECT 56 /* 'object' */ -#define DUK_HEAP_STRING_LC_OBJECT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_OBJECT) -#define DUK_HTHREAD_STRING_LC_OBJECT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_OBJECT) -#define DUK_STRIDX_LC_UNDEFINED 57 /* 'undefined' */ -#define DUK_HEAP_STRING_LC_UNDEFINED(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_UNDEFINED) -#define DUK_HTHREAD_STRING_LC_UNDEFINED(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_UNDEFINED) -#define DUK_STRIDX_NAN 58 /* 'NaN' */ -#define DUK_HEAP_STRING_NAN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_NAN) -#define DUK_HTHREAD_STRING_NAN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_NAN) -#define DUK_STRIDX_INFINITY 59 /* 'Infinity' */ -#define DUK_HEAP_STRING_INFINITY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INFINITY) -#define DUK_HTHREAD_STRING_INFINITY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INFINITY) -#define DUK_STRIDX_MINUS_INFINITY 60 /* '-Infinity' */ -#define DUK_HEAP_STRING_MINUS_INFINITY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MINUS_INFINITY) -#define DUK_HTHREAD_STRING_MINUS_INFINITY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MINUS_INFINITY) -#define DUK_STRIDX_MINUS_ZERO 61 /* '-0' */ -#define DUK_HEAP_STRING_MINUS_ZERO(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MINUS_ZERO) -#define DUK_HTHREAD_STRING_MINUS_ZERO(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MINUS_ZERO) -#define DUK_STRIDX_COMMA 62 /* ',' */ -#define DUK_HEAP_STRING_COMMA(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_COMMA) -#define DUK_HTHREAD_STRING_COMMA(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_COMMA) -#define DUK_STRIDX_SPACE 63 /* ' ' */ -#define DUK_HEAP_STRING_SPACE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SPACE) -#define DUK_HTHREAD_STRING_SPACE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SPACE) -#define DUK_STRIDX_NEWLINE_4SPACE 64 /* '\n ' */ -#define DUK_HEAP_STRING_NEWLINE_4SPACE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_NEWLINE_4SPACE) -#define DUK_HTHREAD_STRING_NEWLINE_4SPACE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_NEWLINE_4SPACE) -#define DUK_STRIDX_BRACKETED_ELLIPSIS 65 /* '[...]' */ -#define DUK_HEAP_STRING_BRACKETED_ELLIPSIS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_BRACKETED_ELLIPSIS) -#define DUK_HTHREAD_STRING_BRACKETED_ELLIPSIS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_BRACKETED_ELLIPSIS) -#define DUK_STRIDX_INVALID_DATE 66 /* 'Invalid Date' */ -#define DUK_HEAP_STRING_INVALID_DATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INVALID_DATE) -#define DUK_HTHREAD_STRING_INVALID_DATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INVALID_DATE) -#define DUK_STRIDX_LC_ARGUMENTS 67 /* 'arguments' */ -#define DUK_HEAP_STRING_LC_ARGUMENTS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_ARGUMENTS) -#define DUK_HTHREAD_STRING_LC_ARGUMENTS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_ARGUMENTS) -#define DUK_STRIDX_CALLEE 68 /* 'callee' */ -#define DUK_HEAP_STRING_CALLEE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CALLEE) -#define DUK_HTHREAD_STRING_CALLEE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CALLEE) -#define DUK_STRIDX_CALLER 69 /* 'caller' */ -#define DUK_HEAP_STRING_CALLER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CALLER) -#define DUK_HTHREAD_STRING_CALLER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CALLER) -#define DUK_STRIDX_HAS 70 /* 'has' */ -#define DUK_HEAP_STRING_HAS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_HAS) -#define DUK_HTHREAD_STRING_HAS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_HAS) -#define DUK_STRIDX_GET 71 /* 'get' */ -#define DUK_HEAP_STRING_GET(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET) -#define DUK_HTHREAD_STRING_GET(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET) -#define DUK_STRIDX_DELETE_PROPERTY 72 /* 'deleteProperty' */ -#define DUK_HEAP_STRING_DELETE_PROPERTY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DELETE_PROPERTY) -#define DUK_HTHREAD_STRING_DELETE_PROPERTY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DELETE_PROPERTY) -#define DUK_STRIDX_ENUMERATE 73 /* 'enumerate' */ -#define DUK_HEAP_STRING_ENUMERATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ENUMERATE) -#define DUK_HTHREAD_STRING_ENUMERATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ENUMERATE) -#define DUK_STRIDX_OWN_KEYS 74 /* 'ownKeys' */ -#define DUK_HEAP_STRING_OWN_KEYS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_OWN_KEYS) -#define DUK_HTHREAD_STRING_OWN_KEYS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_OWN_KEYS) -#define DUK_STRIDX_SET_PROTOTYPE_OF 75 /* 'setPrototypeOf' */ -#define DUK_HEAP_STRING_SET_PROTOTYPE_OF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_PROTOTYPE_OF) -#define DUK_HTHREAD_STRING_SET_PROTOTYPE_OF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_PROTOTYPE_OF) -#define DUK_STRIDX___PROTO__ 76 /* '__proto__' */ -#define DUK_HEAP_STRING___PROTO__(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX___PROTO__) -#define DUK_HTHREAD_STRING___PROTO__(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX___PROTO__) -#define DUK_STRIDX_REQUIRE 77 /* 'require' */ -#define DUK_HEAP_STRING_REQUIRE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_REQUIRE) -#define DUK_HTHREAD_STRING_REQUIRE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_REQUIRE) -#define DUK_STRIDX_ID 78 /* 'id' */ -#define DUK_HEAP_STRING_ID(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ID) -#define DUK_HTHREAD_STRING_ID(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ID) -#define DUK_STRIDX_EXPORTS 79 /* 'exports' */ -#define DUK_HEAP_STRING_EXPORTS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EXPORTS) -#define DUK_HTHREAD_STRING_EXPORTS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EXPORTS) -#define DUK_STRIDX_FILENAME 80 /* 'filename' */ -#define DUK_HEAP_STRING_FILENAME(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FILENAME) -#define DUK_HTHREAD_STRING_FILENAME(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FILENAME) -#define DUK_STRIDX_TO_STRING 81 /* 'toString' */ -#define DUK_HEAP_STRING_TO_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_STRING) -#define DUK_HTHREAD_STRING_TO_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_STRING) -#define DUK_STRIDX_TO_JSON 82 /* 'toJSON' */ -#define DUK_HEAP_STRING_TO_JSON(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_JSON) -#define DUK_HTHREAD_STRING_TO_JSON(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_JSON) -#define DUK_STRIDX_TYPE 83 /* 'type' */ -#define DUK_HEAP_STRING_TYPE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TYPE) -#define DUK_HTHREAD_STRING_TYPE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TYPE) -#define DUK_STRIDX_DATA 84 /* 'data' */ -#define DUK_HEAP_STRING_DATA(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DATA) -#define DUK_HTHREAD_STRING_DATA(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DATA) -#define DUK_STRIDX_LENGTH 85 /* 'length' */ -#define DUK_HEAP_STRING_LENGTH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LENGTH) -#define DUK_HTHREAD_STRING_LENGTH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LENGTH) -#define DUK_STRIDX_BYTE_LENGTH 86 /* 'byteLength' */ -#define DUK_HEAP_STRING_BYTE_LENGTH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_BYTE_LENGTH) -#define DUK_HTHREAD_STRING_BYTE_LENGTH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_BYTE_LENGTH) -#define DUK_STRIDX_BYTE_OFFSET 87 /* 'byteOffset' */ -#define DUK_HEAP_STRING_BYTE_OFFSET(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_BYTE_OFFSET) -#define DUK_HTHREAD_STRING_BYTE_OFFSET(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_BYTE_OFFSET) -#define DUK_STRIDX_BYTES_PER_ELEMENT 88 /* 'BYTES_PER_ELEMENT' */ -#define DUK_HEAP_STRING_BYTES_PER_ELEMENT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_BYTES_PER_ELEMENT) -#define DUK_HTHREAD_STRING_BYTES_PER_ELEMENT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_BYTES_PER_ELEMENT) -#define DUK_STRIDX_SET 89 /* 'set' */ -#define DUK_HEAP_STRING_SET(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET) -#define DUK_HTHREAD_STRING_SET(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET) -#define DUK_STRIDX_STACK 90 /* 'stack' */ -#define DUK_HEAP_STRING_STACK(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_STACK) -#define DUK_HTHREAD_STRING_STACK(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_STACK) -#define DUK_STRIDX_PC 91 /* 'pc' */ -#define DUK_HEAP_STRING_PC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PC) -#define DUK_HTHREAD_STRING_PC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PC) -#define DUK_STRIDX_LINE_NUMBER 92 /* 'lineNumber' */ -#define DUK_HEAP_STRING_LINE_NUMBER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LINE_NUMBER) -#define DUK_HTHREAD_STRING_LINE_NUMBER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LINE_NUMBER) -#define DUK_STRIDX_INT_TRACEDATA 93 /* '\xffTracedata' */ -#define DUK_HEAP_STRING_INT_TRACEDATA(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_TRACEDATA) -#define DUK_HTHREAD_STRING_INT_TRACEDATA(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_TRACEDATA) -#define DUK_STRIDX_NAME 94 /* 'name' */ -#define DUK_HEAP_STRING_NAME(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_NAME) -#define DUK_HTHREAD_STRING_NAME(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_NAME) -#define DUK_STRIDX_FILE_NAME 95 /* 'fileName' */ -#define DUK_HEAP_STRING_FILE_NAME(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FILE_NAME) -#define DUK_HTHREAD_STRING_FILE_NAME(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FILE_NAME) -#define DUK_STRIDX_LC_BUFFER 96 /* 'buffer' */ -#define DUK_HEAP_STRING_LC_BUFFER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_BUFFER) -#define DUK_HTHREAD_STRING_LC_BUFFER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_BUFFER) -#define DUK_STRIDX_LC_POINTER 97 /* 'pointer' */ -#define DUK_HEAP_STRING_LC_POINTER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_POINTER) -#define DUK_HTHREAD_STRING_LC_POINTER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_POINTER) -#define DUK_STRIDX_INT_VALUE 98 /* '\xffValue' */ -#define DUK_HEAP_STRING_INT_VALUE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_VALUE) -#define DUK_HTHREAD_STRING_INT_VALUE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_VALUE) -#define DUK_STRIDX_INT_NEXT 99 /* '\xffNext' */ -#define DUK_HEAP_STRING_INT_NEXT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_NEXT) -#define DUK_HTHREAD_STRING_INT_NEXT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_NEXT) -#define DUK_STRIDX_INT_BYTECODE 100 /* '\xffBytecode' */ -#define DUK_HEAP_STRING_INT_BYTECODE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_BYTECODE) -#define DUK_HTHREAD_STRING_INT_BYTECODE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_BYTECODE) -#define DUK_STRIDX_INT_FORMALS 101 /* '\xffFormals' */ -#define DUK_HEAP_STRING_INT_FORMALS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_FORMALS) -#define DUK_HTHREAD_STRING_INT_FORMALS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_FORMALS) -#define DUK_STRIDX_INT_VARMAP 102 /* '\xffVarmap' */ -#define DUK_HEAP_STRING_INT_VARMAP(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_VARMAP) -#define DUK_HTHREAD_STRING_INT_VARMAP(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_VARMAP) -#define DUK_STRIDX_INT_LEXENV 103 /* '\xffLexenv' */ -#define DUK_HEAP_STRING_INT_LEXENV(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_LEXENV) -#define DUK_HTHREAD_STRING_INT_LEXENV(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_LEXENV) -#define DUK_STRIDX_INT_VARENV 104 /* '\xffVarenv' */ -#define DUK_HEAP_STRING_INT_VARENV(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_VARENV) -#define DUK_HTHREAD_STRING_INT_VARENV(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_VARENV) -#define DUK_STRIDX_INT_SOURCE 105 /* '\xffSource' */ -#define DUK_HEAP_STRING_INT_SOURCE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_SOURCE) -#define DUK_HTHREAD_STRING_INT_SOURCE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_SOURCE) -#define DUK_STRIDX_INT_PC2LINE 106 /* '\xffPc2line' */ -#define DUK_HEAP_STRING_INT_PC2LINE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_PC2LINE) -#define DUK_HTHREAD_STRING_INT_PC2LINE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_PC2LINE) -#define DUK_STRIDX_INT_ARGS 107 /* '\xffArgs' */ -#define DUK_HEAP_STRING_INT_ARGS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_ARGS) -#define DUK_HTHREAD_STRING_INT_ARGS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_ARGS) -#define DUK_STRIDX_INT_MAP 108 /* '\xffMap' */ -#define DUK_HEAP_STRING_INT_MAP(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_MAP) -#define DUK_HTHREAD_STRING_INT_MAP(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_MAP) -#define DUK_STRIDX_INT_FINALIZER 109 /* '\xffFinalizer' */ -#define DUK_HEAP_STRING_INT_FINALIZER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_FINALIZER) -#define DUK_HTHREAD_STRING_INT_FINALIZER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_FINALIZER) -#define DUK_STRIDX_INT_HANDLER 110 /* '\xffHandler' */ -#define DUK_HEAP_STRING_INT_HANDLER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_HANDLER) -#define DUK_HTHREAD_STRING_INT_HANDLER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_HANDLER) -#define DUK_STRIDX_INT_CALLEE 111 /* '\xffCallee' */ -#define DUK_HEAP_STRING_INT_CALLEE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_CALLEE) -#define DUK_HTHREAD_STRING_INT_CALLEE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_CALLEE) -#define DUK_STRIDX_INT_THREAD 112 /* '\xffThread' */ -#define DUK_HEAP_STRING_INT_THREAD(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_THREAD) -#define DUK_HTHREAD_STRING_INT_THREAD(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_THREAD) -#define DUK_STRIDX_INT_REGBASE 113 /* '\xffRegbase' */ -#define DUK_HEAP_STRING_INT_REGBASE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_REGBASE) -#define DUK_HTHREAD_STRING_INT_REGBASE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_REGBASE) -#define DUK_STRIDX_INT_TARGET 114 /* '\xffTarget' */ -#define DUK_HEAP_STRING_INT_TARGET(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_TARGET) -#define DUK_HTHREAD_STRING_INT_TARGET(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_TARGET) -#define DUK_STRIDX_INT_THIS 115 /* '\xffThis' */ -#define DUK_HEAP_STRING_INT_THIS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_THIS) -#define DUK_HTHREAD_STRING_INT_THIS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_THIS) -#define DUK_STRIDX_COMPILE 116 /* 'compile' */ -#define DUK_HEAP_STRING_COMPILE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_COMPILE) -#define DUK_HTHREAD_STRING_COMPILE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_COMPILE) -#define DUK_STRIDX_INPUT 117 /* 'input' */ -#define DUK_HEAP_STRING_INPUT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INPUT) -#define DUK_HTHREAD_STRING_INPUT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INPUT) -#define DUK_STRIDX_ERR_CREATE 118 /* 'errCreate' */ -#define DUK_HEAP_STRING_ERR_CREATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ERR_CREATE) -#define DUK_HTHREAD_STRING_ERR_CREATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ERR_CREATE) -#define DUK_STRIDX_ERR_THROW 119 /* 'errThrow' */ -#define DUK_HEAP_STRING_ERR_THROW(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ERR_THROW) -#define DUK_HTHREAD_STRING_ERR_THROW(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ERR_THROW) -#define DUK_STRIDX_MOD_SEARCH 120 /* 'modSearch' */ -#define DUK_HEAP_STRING_MOD_SEARCH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MOD_SEARCH) -#define DUK_HTHREAD_STRING_MOD_SEARCH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MOD_SEARCH) -#define DUK_STRIDX_MOD_LOADED 121 /* 'modLoaded' */ -#define DUK_HEAP_STRING_MOD_LOADED(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MOD_LOADED) -#define DUK_HTHREAD_STRING_MOD_LOADED(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MOD_LOADED) -#define DUK_STRIDX_ENV 122 /* 'env' */ -#define DUK_HEAP_STRING_ENV(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ENV) -#define DUK_HTHREAD_STRING_ENV(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ENV) -#define DUK_STRIDX_HEX 123 /* 'hex' */ -#define DUK_HEAP_STRING_HEX(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_HEX) -#define DUK_HTHREAD_STRING_HEX(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_HEX) -#define DUK_STRIDX_BASE64 124 /* 'base64' */ -#define DUK_HEAP_STRING_BASE64(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_BASE64) -#define DUK_HTHREAD_STRING_BASE64(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_BASE64) -#define DUK_STRIDX_JX 125 /* 'jx' */ -#define DUK_HEAP_STRING_JX(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JX) -#define DUK_HTHREAD_STRING_JX(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JX) -#define DUK_STRIDX_JC 126 /* 'jc' */ -#define DUK_HEAP_STRING_JC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JC) -#define DUK_HTHREAD_STRING_JC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JC) -#define DUK_STRIDX_RESUME 127 /* 'resume' */ -#define DUK_HEAP_STRING_RESUME(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_RESUME) -#define DUK_HTHREAD_STRING_RESUME(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_RESUME) -#define DUK_STRIDX_FMT 128 /* 'fmt' */ -#define DUK_HEAP_STRING_FMT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FMT) -#define DUK_HTHREAD_STRING_FMT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FMT) -#define DUK_STRIDX_RAW 129 /* 'raw' */ -#define DUK_HEAP_STRING_RAW(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_RAW) -#define DUK_HTHREAD_STRING_RAW(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_RAW) -#define DUK_STRIDX_LC_TRACE 130 /* 'trace' */ -#define DUK_HEAP_STRING_LC_TRACE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_TRACE) -#define DUK_HTHREAD_STRING_LC_TRACE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_TRACE) -#define DUK_STRIDX_LC_DEBUG 131 /* 'debug' */ -#define DUK_HEAP_STRING_LC_DEBUG(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_DEBUG) -#define DUK_HTHREAD_STRING_LC_DEBUG(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_DEBUG) -#define DUK_STRIDX_LC_INFO 132 /* 'info' */ -#define DUK_HEAP_STRING_LC_INFO(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_INFO) -#define DUK_HTHREAD_STRING_LC_INFO(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_INFO) -#define DUK_STRIDX_LC_WARN 133 /* 'warn' */ -#define DUK_HEAP_STRING_LC_WARN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_WARN) -#define DUK_HTHREAD_STRING_LC_WARN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_WARN) -#define DUK_STRIDX_LC_ERROR 134 /* 'error' */ -#define DUK_HEAP_STRING_LC_ERROR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_ERROR) -#define DUK_HTHREAD_STRING_LC_ERROR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_ERROR) -#define DUK_STRIDX_LC_FATAL 135 /* 'fatal' */ -#define DUK_HEAP_STRING_LC_FATAL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_FATAL) -#define DUK_HTHREAD_STRING_LC_FATAL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_FATAL) -#define DUK_STRIDX_LC_N 136 /* 'n' */ -#define DUK_HEAP_STRING_LC_N(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_N) -#define DUK_HTHREAD_STRING_LC_N(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_N) -#define DUK_STRIDX_LC_L 137 /* 'l' */ -#define DUK_HEAP_STRING_LC_L(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_L) -#define DUK_HTHREAD_STRING_LC_L(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_L) -#define DUK_STRIDX_CLOG 138 /* 'clog' */ -#define DUK_HEAP_STRING_CLOG(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CLOG) -#define DUK_HTHREAD_STRING_CLOG(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CLOG) -#define DUK_STRIDX_TO_LOG_STRING 139 /* 'toLogString' */ -#define DUK_HEAP_STRING_TO_LOG_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_LOG_STRING) -#define DUK_HTHREAD_STRING_TO_LOG_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_LOG_STRING) -#define DUK_STRIDX_JSON_EXT_UNDEFINED 140 /* '{"_undef":true}' */ -#define DUK_HEAP_STRING_JSON_EXT_UNDEFINED(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON_EXT_UNDEFINED) -#define DUK_HTHREAD_STRING_JSON_EXT_UNDEFINED(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON_EXT_UNDEFINED) -#define DUK_STRIDX_JSON_EXT_NAN 141 /* '{"_nan":true}' */ -#define DUK_HEAP_STRING_JSON_EXT_NAN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON_EXT_NAN) -#define DUK_HTHREAD_STRING_JSON_EXT_NAN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON_EXT_NAN) -#define DUK_STRIDX_JSON_EXT_POSINF 142 /* '{"_inf":true}' */ -#define DUK_HEAP_STRING_JSON_EXT_POSINF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON_EXT_POSINF) -#define DUK_HTHREAD_STRING_JSON_EXT_POSINF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON_EXT_POSINF) -#define DUK_STRIDX_JSON_EXT_NEGINF 143 /* '{"_ninf":true}' */ -#define DUK_HEAP_STRING_JSON_EXT_NEGINF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON_EXT_NEGINF) -#define DUK_HTHREAD_STRING_JSON_EXT_NEGINF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON_EXT_NEGINF) -#define DUK_STRIDX_JSON_EXT_FUNCTION1 144 /* '{"_func":true}' */ -#define DUK_HEAP_STRING_JSON_EXT_FUNCTION1(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON_EXT_FUNCTION1) -#define DUK_HTHREAD_STRING_JSON_EXT_FUNCTION1(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON_EXT_FUNCTION1) -#define DUK_STRIDX_JSON_EXT_FUNCTION2 145 /* '{_func:true}' */ -#define DUK_HEAP_STRING_JSON_EXT_FUNCTION2(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON_EXT_FUNCTION2) -#define DUK_HTHREAD_STRING_JSON_EXT_FUNCTION2(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON_EXT_FUNCTION2) -#define DUK_STRIDX_BREAK 146 /* 'break' */ -#define DUK_HEAP_STRING_BREAK(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_BREAK) -#define DUK_HTHREAD_STRING_BREAK(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_BREAK) -#define DUK_STRIDX_CASE 147 /* 'case' */ -#define DUK_HEAP_STRING_CASE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CASE) -#define DUK_HTHREAD_STRING_CASE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CASE) -#define DUK_STRIDX_CATCH 148 /* 'catch' */ -#define DUK_HEAP_STRING_CATCH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CATCH) -#define DUK_HTHREAD_STRING_CATCH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CATCH) -#define DUK_STRIDX_CONTINUE 149 /* 'continue' */ -#define DUK_HEAP_STRING_CONTINUE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CONTINUE) -#define DUK_HTHREAD_STRING_CONTINUE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CONTINUE) -#define DUK_STRIDX_DEBUGGER 150 /* 'debugger' */ -#define DUK_HEAP_STRING_DEBUGGER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DEBUGGER) -#define DUK_HTHREAD_STRING_DEBUGGER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DEBUGGER) -#define DUK_STRIDX_DEFAULT 151 /* 'default' */ -#define DUK_HEAP_STRING_DEFAULT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DEFAULT) -#define DUK_HTHREAD_STRING_DEFAULT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DEFAULT) -#define DUK_STRIDX_DELETE 152 /* 'delete' */ -#define DUK_HEAP_STRING_DELETE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DELETE) -#define DUK_HTHREAD_STRING_DELETE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DELETE) -#define DUK_STRIDX_DO 153 /* 'do' */ -#define DUK_HEAP_STRING_DO(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DO) -#define DUK_HTHREAD_STRING_DO(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DO) -#define DUK_STRIDX_ELSE 154 /* 'else' */ -#define DUK_HEAP_STRING_ELSE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ELSE) -#define DUK_HTHREAD_STRING_ELSE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ELSE) -#define DUK_STRIDX_FINALLY 155 /* 'finally' */ -#define DUK_HEAP_STRING_FINALLY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FINALLY) -#define DUK_HTHREAD_STRING_FINALLY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FINALLY) -#define DUK_STRIDX_FOR 156 /* 'for' */ -#define DUK_HEAP_STRING_FOR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FOR) -#define DUK_HTHREAD_STRING_FOR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FOR) -#define DUK_STRIDX_LC_FUNCTION 157 /* 'function' */ -#define DUK_HEAP_STRING_LC_FUNCTION(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_FUNCTION) -#define DUK_HTHREAD_STRING_LC_FUNCTION(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_FUNCTION) -#define DUK_STRIDX_IF 158 /* 'if' */ -#define DUK_HEAP_STRING_IF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IF) -#define DUK_HTHREAD_STRING_IF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IF) -#define DUK_STRIDX_IN 159 /* 'in' */ -#define DUK_HEAP_STRING_IN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IN) -#define DUK_HTHREAD_STRING_IN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IN) -#define DUK_STRIDX_INSTANCEOF 160 /* 'instanceof' */ -#define DUK_HEAP_STRING_INSTANCEOF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INSTANCEOF) -#define DUK_HTHREAD_STRING_INSTANCEOF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INSTANCEOF) -#define DUK_STRIDX_NEW 161 /* 'new' */ -#define DUK_HEAP_STRING_NEW(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_NEW) -#define DUK_HTHREAD_STRING_NEW(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_NEW) -#define DUK_STRIDX_RETURN 162 /* 'return' */ -#define DUK_HEAP_STRING_RETURN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_RETURN) -#define DUK_HTHREAD_STRING_RETURN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_RETURN) -#define DUK_STRIDX_SWITCH 163 /* 'switch' */ -#define DUK_HEAP_STRING_SWITCH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SWITCH) -#define DUK_HTHREAD_STRING_SWITCH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SWITCH) -#define DUK_STRIDX_THIS 164 /* 'this' */ -#define DUK_HEAP_STRING_THIS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_THIS) -#define DUK_HTHREAD_STRING_THIS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_THIS) -#define DUK_STRIDX_THROW 165 /* 'throw' */ -#define DUK_HEAP_STRING_THROW(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_THROW) -#define DUK_HTHREAD_STRING_THROW(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_THROW) -#define DUK_STRIDX_TRY 166 /* 'try' */ -#define DUK_HEAP_STRING_TRY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TRY) -#define DUK_HTHREAD_STRING_TRY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TRY) -#define DUK_STRIDX_TYPEOF 167 /* 'typeof' */ -#define DUK_HEAP_STRING_TYPEOF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TYPEOF) -#define DUK_HTHREAD_STRING_TYPEOF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TYPEOF) -#define DUK_STRIDX_VAR 168 /* 'var' */ -#define DUK_HEAP_STRING_VAR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_VAR) -#define DUK_HTHREAD_STRING_VAR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_VAR) -#define DUK_STRIDX_CONST 169 /* 'const' */ -#define DUK_HEAP_STRING_CONST(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CONST) -#define DUK_HTHREAD_STRING_CONST(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CONST) -#define DUK_STRIDX_VOID 170 /* 'void' */ -#define DUK_HEAP_STRING_VOID(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_VOID) -#define DUK_HTHREAD_STRING_VOID(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_VOID) -#define DUK_STRIDX_WHILE 171 /* 'while' */ -#define DUK_HEAP_STRING_WHILE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WHILE) -#define DUK_HTHREAD_STRING_WHILE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WHILE) -#define DUK_STRIDX_WITH 172 /* 'with' */ -#define DUK_HEAP_STRING_WITH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WITH) -#define DUK_HTHREAD_STRING_WITH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WITH) -#define DUK_STRIDX_CLASS 173 /* 'class' */ -#define DUK_HEAP_STRING_CLASS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CLASS) -#define DUK_HTHREAD_STRING_CLASS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CLASS) -#define DUK_STRIDX_ENUM 174 /* 'enum' */ -#define DUK_HEAP_STRING_ENUM(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ENUM) -#define DUK_HTHREAD_STRING_ENUM(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ENUM) -#define DUK_STRIDX_EXPORT 175 /* 'export' */ -#define DUK_HEAP_STRING_EXPORT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EXPORT) -#define DUK_HTHREAD_STRING_EXPORT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EXPORT) -#define DUK_STRIDX_EXTENDS 176 /* 'extends' */ -#define DUK_HEAP_STRING_EXTENDS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EXTENDS) -#define DUK_HTHREAD_STRING_EXTENDS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EXTENDS) -#define DUK_STRIDX_IMPORT 177 /* 'import' */ -#define DUK_HEAP_STRING_IMPORT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IMPORT) -#define DUK_HTHREAD_STRING_IMPORT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IMPORT) -#define DUK_STRIDX_SUPER 178 /* 'super' */ -#define DUK_HEAP_STRING_SUPER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SUPER) -#define DUK_HTHREAD_STRING_SUPER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SUPER) -#define DUK_STRIDX_LC_NULL 179 /* 'null' */ -#define DUK_HEAP_STRING_LC_NULL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_NULL) -#define DUK_HTHREAD_STRING_LC_NULL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_NULL) -#define DUK_STRIDX_TRUE 180 /* 'true' */ -#define DUK_HEAP_STRING_TRUE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TRUE) -#define DUK_HTHREAD_STRING_TRUE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TRUE) -#define DUK_STRIDX_FALSE 181 /* 'false' */ -#define DUK_HEAP_STRING_FALSE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FALSE) -#define DUK_HTHREAD_STRING_FALSE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FALSE) -#define DUK_STRIDX_IMPLEMENTS 182 /* 'implements' */ -#define DUK_HEAP_STRING_IMPLEMENTS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IMPLEMENTS) -#define DUK_HTHREAD_STRING_IMPLEMENTS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IMPLEMENTS) -#define DUK_STRIDX_INTERFACE 183 /* 'interface' */ -#define DUK_HEAP_STRING_INTERFACE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INTERFACE) -#define DUK_HTHREAD_STRING_INTERFACE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INTERFACE) -#define DUK_STRIDX_LET 184 /* 'let' */ -#define DUK_HEAP_STRING_LET(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LET) -#define DUK_HTHREAD_STRING_LET(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LET) -#define DUK_STRIDX_PACKAGE 185 /* 'package' */ -#define DUK_HEAP_STRING_PACKAGE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PACKAGE) -#define DUK_HTHREAD_STRING_PACKAGE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PACKAGE) -#define DUK_STRIDX_PRIVATE 186 /* 'private' */ -#define DUK_HEAP_STRING_PRIVATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PRIVATE) -#define DUK_HTHREAD_STRING_PRIVATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PRIVATE) -#define DUK_STRIDX_PROTECTED 187 /* 'protected' */ -#define DUK_HEAP_STRING_PROTECTED(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PROTECTED) -#define DUK_HTHREAD_STRING_PROTECTED(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PROTECTED) -#define DUK_STRIDX_PUBLIC 188 /* 'public' */ -#define DUK_HEAP_STRING_PUBLIC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PUBLIC) -#define DUK_HTHREAD_STRING_PUBLIC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PUBLIC) -#define DUK_STRIDX_STATIC 189 /* 'static' */ -#define DUK_HEAP_STRING_STATIC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_STATIC) -#define DUK_HTHREAD_STRING_STATIC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_STATIC) -#define DUK_STRIDX_YIELD 190 /* 'yield' */ -#define DUK_HEAP_STRING_YIELD(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_YIELD) -#define DUK_HTHREAD_STRING_YIELD(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_YIELD) - -#define DUK_HEAP_NUM_STRINGS 191 -#define DUK_STRIDX_START_RESERVED 146 -#define DUK_STRIDX_START_STRICT_RESERVED 182 -#define DUK_STRIDX_END_RESERVED 191 /* exclusive endpoint */ - -/* To convert a heap stridx to a token number, subtract - * DUK_STRIDX_START_RESERVED and add DUK_TOK_START_RESERVED. - */ -#if !defined(DUK_SINGLE_FILE) -DUK_INTERNAL_DECL const duk_uint8_t duk_strings_data[1049]; -#endif /* !DUK_SINGLE_FILE */ -#define DUK_STRDATA_MAX_STRLEN 17 -#define DUK_STRDATA_DATA_LENGTH 1049 -#endif /* DUK_USE_ROM_STRINGS */ - -#if defined(DUK_USE_ROM_OBJECTS) -#error ROM support not enabled, rerun make_dist.py with --rom-support -#else -DUK_INTERNAL_DECL duk_ret_t duk_bi_object_constructor(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_function_constructor(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_function_prototype(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_array_constructor(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_string_constructor(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_boolean_constructor(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_number_constructor(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_date_constructor(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_regexp_constructor(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_error_constructor_shared(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_type_error_thrower(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_proxy_constructor(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_thread_constructor(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_buffer_constructor(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_pointer_constructor(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_logger_constructor(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_arraybuffer_constructor(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_dataview_constructor(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_typedarray_constructor(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_nodejs_buffer_constructor(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_eval(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_parse_int(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_parse_float(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_is_nan(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_is_finite(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_decode_uri(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_decode_uri_component(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_encode_uri(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_encode_uri_component(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_escape(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_unescape(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_print_helper(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_require(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_object_getprototype_shared(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_object_setprototype_shared(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_object_constructor_get_own_property_descriptor(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_object_constructor_keys_shared(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_object_constructor_create(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_object_constructor_define_property(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_object_constructor_define_properties(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_object_constructor_seal_freeze_shared(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_object_constructor_prevent_extensions(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_object_constructor_is_sealed_frozen_shared(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_object_constructor_is_extensible(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_object_prototype_to_string(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_object_prototype_to_locale_string(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_object_prototype_value_of(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_object_prototype_has_own_property(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_object_prototype_is_prototype_of(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_object_prototype_property_is_enumerable(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_function_prototype_to_string(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_function_prototype_apply(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_function_prototype_call(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_function_prototype_bind(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_array_constructor_is_array(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_to_string(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_join_shared(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_concat(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_pop(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_push(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_reverse(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_shift(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_slice(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_sort(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_splice(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_unshift(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_indexof_shared(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_iter_shared(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_reduce_shared(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_string_constructor_from_char_code(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_to_string(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_char_at(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_char_code_at(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_concat(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_indexof_shared(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_locale_compare(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_match(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_replace(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_search(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_slice(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_split(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_substring(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_caseconv_shared(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_trim(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_substr(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_boolean_prototype_tostring_shared(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_number_prototype_to_string(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_number_prototype_to_locale_string(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_number_prototype_value_of(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_number_prototype_to_fixed(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_number_prototype_to_exponential(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_number_prototype_to_precision(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_date_constructor_parse(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_date_constructor_utc(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_date_constructor_now(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_date_prototype_tostring_shared(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_date_prototype_to_json(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_date_prototype_value_of(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_date_prototype_get_shared(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_date_prototype_get_timezone_offset(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_date_prototype_set_time(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_date_prototype_set_shared(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_regexp_prototype_exec(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_regexp_prototype_test(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_regexp_prototype_to_string(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_error_prototype_stack_getter(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_error_prototype_stack_setter(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_error_prototype_filename_getter(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_error_prototype_filename_setter(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_error_prototype_linenumber_getter(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_error_prototype_linenumber_setter(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_error_prototype_to_string(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_math_object_onearg_shared(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_math_object_twoarg_shared(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_math_object_max(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_math_object_min(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_math_object_random(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_json_object_parse(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_json_object_stringify(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_duktape_object_info(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_duktape_object_act(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_duktape_object_gc(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_duktape_object_fin(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_duktape_object_enc(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_duktape_object_dec(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_duktape_object_compact(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_thread_yield(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_thread_resume(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_thread_current(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_buffer_prototype_tostring_shared(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_pointer_prototype_tostring_shared(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_logger_prototype_fmt(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_logger_prototype_raw(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_logger_prototype_log_shared(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_arraybuffer_isview(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_buffer_slice_shared(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_buffer_readfield(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_buffer_writefield(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_typedarray_set(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_nodejs_buffer_concat(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_nodejs_buffer_is_encoding(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_nodejs_buffer_is_buffer(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_nodejs_buffer_byte_length(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_buffer_compare_shared(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_nodejs_buffer_tostring(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_nodejs_buffer_tojson(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_nodejs_buffer_fill(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_nodejs_buffer_copy(duk_context *ctx); -DUK_INTERNAL_DECL duk_ret_t duk_bi_nodejs_buffer_write(duk_context *ctx); -#if !defined(DUK_SINGLE_FILE) -DUK_INTERNAL_DECL const duk_c_function duk_bi_native_functions[149]; -#endif /* !DUK_SINGLE_FILE */ -#if defined(DUK_USE_BUILTIN_INITJS) -#if !defined(DUK_SINGLE_FILE) -DUK_INTERNAL_DECL const duk_uint8_t duk_initjs_data[187]; -#endif /* !DUK_SINGLE_FILE */ -#define DUK_BUILTIN_INITJS_DATA_LENGTH 187 -#endif /* DUK_USE_BUILTIN_INITJS */ -#define DUK_BIDX_GLOBAL 0 -#define DUK_BIDX_GLOBAL_ENV 1 -#define DUK_BIDX_OBJECT_CONSTRUCTOR 2 -#define DUK_BIDX_OBJECT_PROTOTYPE 3 -#define DUK_BIDX_FUNCTION_CONSTRUCTOR 4 -#define DUK_BIDX_FUNCTION_PROTOTYPE 5 -#define DUK_BIDX_ARRAY_CONSTRUCTOR 6 -#define DUK_BIDX_ARRAY_PROTOTYPE 7 -#define DUK_BIDX_STRING_CONSTRUCTOR 8 -#define DUK_BIDX_STRING_PROTOTYPE 9 -#define DUK_BIDX_BOOLEAN_CONSTRUCTOR 10 -#define DUK_BIDX_BOOLEAN_PROTOTYPE 11 -#define DUK_BIDX_NUMBER_CONSTRUCTOR 12 -#define DUK_BIDX_NUMBER_PROTOTYPE 13 -#define DUK_BIDX_DATE_CONSTRUCTOR 14 -#define DUK_BIDX_DATE_PROTOTYPE 15 -#define DUK_BIDX_REGEXP_CONSTRUCTOR 16 -#define DUK_BIDX_REGEXP_PROTOTYPE 17 -#define DUK_BIDX_ERROR_CONSTRUCTOR 18 -#define DUK_BIDX_ERROR_PROTOTYPE 19 -#define DUK_BIDX_EVAL_ERROR_CONSTRUCTOR 20 -#define DUK_BIDX_EVAL_ERROR_PROTOTYPE 21 -#define DUK_BIDX_RANGE_ERROR_CONSTRUCTOR 22 -#define DUK_BIDX_RANGE_ERROR_PROTOTYPE 23 -#define DUK_BIDX_REFERENCE_ERROR_CONSTRUCTOR 24 -#define DUK_BIDX_REFERENCE_ERROR_PROTOTYPE 25 -#define DUK_BIDX_SYNTAX_ERROR_CONSTRUCTOR 26 -#define DUK_BIDX_SYNTAX_ERROR_PROTOTYPE 27 -#define DUK_BIDX_TYPE_ERROR_CONSTRUCTOR 28 -#define DUK_BIDX_TYPE_ERROR_PROTOTYPE 29 -#define DUK_BIDX_URI_ERROR_CONSTRUCTOR 30 -#define DUK_BIDX_URI_ERROR_PROTOTYPE 31 -#define DUK_BIDX_MATH 32 -#define DUK_BIDX_JSON 33 -#define DUK_BIDX_TYPE_ERROR_THROWER 34 -#define DUK_BIDX_PROXY_CONSTRUCTOR 35 -#define DUK_BIDX_DUKTAPE 36 -#define DUK_BIDX_THREAD_CONSTRUCTOR 37 -#define DUK_BIDX_THREAD_PROTOTYPE 38 -#define DUK_BIDX_BUFFER_CONSTRUCTOR 39 -#define DUK_BIDX_BUFFER_PROTOTYPE 40 -#define DUK_BIDX_POINTER_CONSTRUCTOR 41 -#define DUK_BIDX_POINTER_PROTOTYPE 42 -#define DUK_BIDX_LOGGER_CONSTRUCTOR 43 -#define DUK_BIDX_LOGGER_PROTOTYPE 44 -#define DUK_BIDX_DOUBLE_ERROR 45 -#define DUK_BIDX_ARRAYBUFFER_CONSTRUCTOR 46 -#define DUK_BIDX_ARRAYBUFFER_PROTOTYPE 47 -#define DUK_BIDX_DATAVIEW_CONSTRUCTOR 48 -#define DUK_BIDX_DATAVIEW_PROTOTYPE 49 -#define DUK_BIDX_TYPEDARRAY_PROTOTYPE 50 -#define DUK_BIDX_INT8ARRAY_CONSTRUCTOR 51 -#define DUK_BIDX_INT8ARRAY_PROTOTYPE 52 -#define DUK_BIDX_UINT8ARRAY_CONSTRUCTOR 53 -#define DUK_BIDX_UINT8ARRAY_PROTOTYPE 54 -#define DUK_BIDX_UINT8CLAMPEDARRAY_CONSTRUCTOR 55 -#define DUK_BIDX_UINT8CLAMPEDARRAY_PROTOTYPE 56 -#define DUK_BIDX_INT16ARRAY_CONSTRUCTOR 57 -#define DUK_BIDX_INT16ARRAY_PROTOTYPE 58 -#define DUK_BIDX_UINT16ARRAY_CONSTRUCTOR 59 -#define DUK_BIDX_UINT16ARRAY_PROTOTYPE 60 -#define DUK_BIDX_INT32ARRAY_CONSTRUCTOR 61 -#define DUK_BIDX_INT32ARRAY_PROTOTYPE 62 -#define DUK_BIDX_UINT32ARRAY_CONSTRUCTOR 63 -#define DUK_BIDX_UINT32ARRAY_PROTOTYPE 64 -#define DUK_BIDX_FLOAT32ARRAY_CONSTRUCTOR 65 -#define DUK_BIDX_FLOAT32ARRAY_PROTOTYPE 66 -#define DUK_BIDX_FLOAT64ARRAY_CONSTRUCTOR 67 -#define DUK_BIDX_FLOAT64ARRAY_PROTOTYPE 68 -#define DUK_BIDX_NODEJS_BUFFER_CONSTRUCTOR 69 -#define DUK_BIDX_NODEJS_BUFFER_PROTOTYPE 70 -#define DUK_NUM_BUILTINS 71 -#define DUK_NUM_BIDX_BUILTINS 71 -#define DUK_NUM_ALL_BUILTINS 71 -#if defined(DUK_USE_DOUBLE_LE) -#if !defined(DUK_SINGLE_FILE) -DUK_INTERNAL_DECL const duk_uint8_t duk_builtins_data[3833]; -#endif /* !DUK_SINGLE_FILE */ -#define DUK_BUILTINS_DATA_LENGTH 3833 -#elif defined(DUK_USE_DOUBLE_BE) -#if !defined(DUK_SINGLE_FILE) -DUK_INTERNAL_DECL const duk_uint8_t duk_builtins_data[3833]; -#endif /* !DUK_SINGLE_FILE */ -#define DUK_BUILTINS_DATA_LENGTH 3833 -#elif defined(DUK_USE_DOUBLE_ME) -#if !defined(DUK_SINGLE_FILE) -DUK_INTERNAL_DECL const duk_uint8_t duk_builtins_data[3833]; -#endif /* !DUK_SINGLE_FILE */ -#define DUK_BUILTINS_DATA_LENGTH 3833 -#else -#error invalid endianness defines -#endif -#endif /* DUK_USE_ROM_OBJECTS */ -#endif /* DUK_BUILTINS_H_INCLUDED */ -#line 52 "duk_internal.h" - -#line 1 "duk_util.h" -/* - * Utilities - */ - -#ifndef DUK_UTIL_H_INCLUDED -#define DUK_UTIL_H_INCLUDED - -#define DUK_UTIL_MIN_HASH_PRIME 17 /* must match genhashsizes.py */ - -#define DUK_UTIL_GET_HASH_PROBE_STEP(hash) (duk_util_probe_steps[(hash) & 0x1f]) - -/* - * Endian conversion - */ - -#if defined(DUK_USE_INTEGER_LE) -#define DUK_HTON32(x) DUK_BSWAP32((x)) -#define DUK_NTOH32(x) DUK_BSWAP32((x)) -#define DUK_HTON16(x) DUK_BSWAP16((x)) -#define DUK_NTOH16(x) DUK_BSWAP16((x)) -#elif defined(DUK_USE_INTEGER_BE) -#define DUK_HTON32(x) (x) -#define DUK_NTOH32(x) (x) -#define DUK_HTON16(x) (x) -#define DUK_NTOH16(x) (x) -#else -#error internal error, endianness defines broken -#endif - -/* - * Bitstream decoder - */ - -struct duk_bitdecoder_ctx { - const duk_uint8_t *data; - duk_size_t offset; - duk_size_t length; - duk_uint32_t currval; - duk_small_int_t currbits; -}; - -/* - * Bitstream encoder - */ - -struct duk_bitencoder_ctx { - duk_uint8_t *data; - duk_size_t offset; - duk_size_t length; - duk_uint32_t currval; - duk_small_int_t currbits; - duk_small_int_t truncated; -}; - -/* - * Raw write/read macros for big endian, unaligned basic values. - * Caller ensures there's enough space. The macros update the pointer - * argument automatically on resizes. The idiom seems a bit odd, but - * leads to compact code. - */ - -#define DUK_RAW_WRITE_U8(ptr,val) do { \ - *(ptr)++ = (duk_uint8_t) (val); \ - } while (0) -#define DUK_RAW_WRITE_U16_BE(ptr,val) duk_raw_write_u16_be(&(ptr), (duk_uint16_t) (val)) -#define DUK_RAW_WRITE_U32_BE(ptr,val) duk_raw_write_u32_be(&(ptr), (duk_uint32_t) (val)) -#define DUK_RAW_WRITE_DOUBLE_BE(ptr,val) duk_raw_write_double_be(&(ptr), (duk_double_t) (val)) -#define DUK_RAW_WRITE_XUTF8(ptr,val) do { \ - /* 'ptr' is evaluated both as LHS and RHS. */ \ - duk_uint8_t *duk__ptr; \ - duk_small_int_t duk__len; \ - duk__ptr = (duk_uint8_t *) (ptr); \ - duk__len = duk_unicode_encode_xutf8((duk_ucodepoint_t) (val), duk__ptr); \ - duk__ptr += duk__len; \ - (ptr) = duk__ptr; \ - } while (0) -#define DUK_RAW_WRITE_CESU8(ptr,val) do { \ - /* 'ptr' is evaluated both as LHS and RHS. */ \ - duk_uint8_t *duk__ptr; \ - duk_small_int_t duk__len; \ - duk__ptr = (duk_uint8_t *) (ptr); \ - duk__len = duk_unicode_encode_cesu8((duk_ucodepoint_t) (val), duk__ptr); \ - duk__ptr += duk__len; \ - (ptr) = duk__ptr; \ - } while (0) - -#define DUK_RAW_READ_U8(ptr) ((duk_uint8_t) (*(ptr)++)) -#define DUK_RAW_READ_U16_BE(ptr) duk_raw_read_u16_be(&(ptr)); -#define DUK_RAW_READ_U32_BE(ptr) duk_raw_read_u32_be(&(ptr)); -#define DUK_RAW_READ_DOUBLE_BE(ptr) duk_raw_read_double_be(&(ptr)); - -/* - * Buffer writer (dynamic buffer only) - * - * Helper for writing to a dynamic buffer with a concept of a "spare" area - * to reduce resizes. You can ensure there is enough space beforehand and - * then write for a while without further checks, relying on a stable data - * pointer. Spare handling is automatic so call sites only indicate how - * much data they need right now. - * - * There are several ways to write using bufwriter. The best approach - * depends mainly on how much performance matters over code footprint. - * The key issues are (1) ensuring there is space and (2) keeping the - * pointers consistent. Fast code should ensure space for multiple writes - * with one ensure call. Fastest inner loop code can temporarily borrow - * the 'p' pointer but must write it back eventually. - * - * Be careful to ensure all macro arguments (other than static pointers like - * 'thr' and 'bw_ctx') are evaluated exactly once, using temporaries if - * necessary (if that's not possible, there should be a note near the macro). - * Buffer write arguments often contain arithmetic etc so this is - * particularly important here. - */ - -/* XXX: Migrate bufwriter and other read/write helpers to its own header? */ - -struct duk_bufwriter_ctx { - duk_uint8_t *p; - duk_uint8_t *p_base; - duk_uint8_t *p_limit; - duk_hbuffer_dynamic *buf; -}; - -#define DUK_BW_SPARE_ADD 64 -#define DUK_BW_SPARE_SHIFT 4 /* 2^4 -> 1/16 = 6.25% spare */ - -/* Initialization and finalization (compaction), converting to other types. */ - -#define DUK_BW_INIT_PUSHBUF(thr,bw_ctx,sz) do { \ - duk_bw_init_pushbuf((thr), (bw_ctx), (sz)); \ - } while (0) -#define DUK_BW_INIT_WITHBUF(thr,bw_ctx,buf) do { \ - duk_bw_init((thr), (bw_ctx), (buf)); \ - } while (0) -#define DUK_BW_COMPACT(thr,bw_ctx) do { \ - /* Make underlying buffer compact to match DUK_BW_GET_SIZE(). */ \ - duk_bw_compact((thr), (bw_ctx)); \ - } while (0) -#define DUK_BW_PUSH_AS_STRING(thr,bw_ctx) do { \ - duk_push_lstring((duk_context *) (thr), \ - (const char *) (bw_ctx)->p_base, \ - (duk_size_t) ((bw_ctx)->p - (bw_ctx)->p_base)); \ - } while (0) -/* Pointers may be NULL for a while when 'buf' size is zero and before any - * ENSURE calls have been made. Once an ENSURE has been made, the pointers - * are required to be non-NULL so that it's always valid to use memcpy() and - * memmove(), even for zero size. - */ -#define DUK_BW_ASSERT_VALID_EXPR(thr,bw_ctx) \ - DUK_ASSERT_EXPR((bw_ctx) != NULL && \ - (bw_ctx)->buf != NULL && \ - ((DUK_HBUFFER_DYNAMIC_GET_SIZE((bw_ctx)->buf) == 0) || \ - ((bw_ctx)->p != NULL && \ - (bw_ctx)->p_base != NULL && \ - (bw_ctx)->p_limit != NULL && \ - (bw_ctx)->p_limit >= (bw_ctx)->p_base && \ - (bw_ctx)->p >= (bw_ctx)->p_base && \ - (bw_ctx)->p <= (bw_ctx)->p_limit))) -#define DUK_BW_ASSERT_VALID(thr,bw_ctx) do { \ - DUK_BW_ASSERT_VALID_EXPR((thr), (bw_ctx)); \ - } while (0) - -/* Working with the pointer and current size. */ - -#define DUK_BW_GET_PTR(thr,bw_ctx) \ - ((bw_ctx)->p) -#define DUK_BW_SET_PTR(thr,bw_ctx,ptr) do { \ - (bw_ctx)->p = (ptr); \ - } while (0) -#define DUK_BW_ADD_PTR(thr,bw_ctx,delta) do { \ - (bw_ctx)->p += (delta); \ - } while (0) -#define DUK_BW_GET_BASEPTR(thr,bw_ctx) \ - ((bw_ctx)->p_base) -#define DUK_BW_GET_LIMITPTR(thr,bw_ctx) \ - ((bw_ctx)->p_limit) -#define DUK_BW_GET_SIZE(thr,bw_ctx) \ - ((duk_size_t) ((bw_ctx)->p - (bw_ctx)->p_base)) -#define DUK_BW_SET_SIZE(thr,bw_ctx,sz) do { \ - DUK_ASSERT((duk_size_t) (sz) <= (duk_size_t) ((bw_ctx)->p - (bw_ctx)->p_base)); \ - (bw_ctx)->p = (bw_ctx)->p_base + (sz); \ - } while (0) -#define DUK_BW_RESET_SIZE(thr,bw_ctx) do { \ - /* Reset to zero size, keep current limit. */ \ - (bw_ctx)->p = (bw_ctx)->p_base; \ - } while (0) -#define DUK_BW_GET_BUFFER(thr,bw_ctx) \ - ((bw_ctx)->buf) - -/* Ensuring (reserving) space. */ - -#define DUK_BW_ENSURE(thr,bw_ctx,sz) do { \ - duk_size_t duk__sz, duk__space; \ - DUK_BW_ASSERT_VALID((thr), (bw_ctx)); \ - duk__sz = (sz); \ - duk__space = (duk_size_t) ((bw_ctx)->p_limit - (bw_ctx)->p); \ - if (duk__space < duk__sz) { \ - (void) duk_bw_resize((thr), (bw_ctx), duk__sz); \ - } \ - } while (0) -/* NOTE: Multiple evaluation of 'ptr' in this macro. */ -/* XXX: Rework to use an always-inline function? */ -#define DUK_BW_ENSURE_RAW(thr,bw_ctx,sz,ptr) \ - (((duk_size_t) ((bw_ctx)->p_limit - (ptr)) >= (sz)) ? \ - (ptr) : \ - ((bw_ctx)->p = (ptr), duk_bw_resize((thr),(bw_ctx),(sz)))) -#define DUK_BW_ENSURE_GETPTR(thr,bw_ctx,sz) \ - DUK_BW_ENSURE_RAW((thr), (bw_ctx), (sz), (bw_ctx)->p) -#define DUK_BW_ASSERT_SPACE_EXPR(thr,bw_ctx,sz) \ - (DUK_BW_ASSERT_VALID_EXPR((thr), (bw_ctx)), \ - DUK_ASSERT_EXPR((duk_size_t) ((bw_ctx)->p_limit - (bw_ctx)->p) >= (duk_size_t) (sz))) -#define DUK_BW_ASSERT_SPACE(thr,bw_ctx,sz) do { \ - DUK_BW_ASSERT_SPACE_EXPR((thr), (bw_ctx), (sz)); \ - } while (0) - -/* Miscellaneous. */ - -#define DUK_BW_SETPTR_AND_COMPACT(thr,bw_ctx,ptr) do { \ - (bw_ctx)->p = (ptr); \ - duk_bw_compact((thr), (bw_ctx)); \ - } while (0) - -/* Fast write calls which assume you control the spare beforehand. - * Multibyte write variants exist and use a temporary write pointer - * because byte writes alias with anything: with a stored pointer - * explicit pointer load/stores get generated (e.g. gcc -Os). - */ - -#define DUK_BW_WRITE_RAW_U8(thr,bw_ctx,val) do { \ - DUK_BW_ASSERT_SPACE((thr), (bw_ctx), 1); \ - *(bw_ctx)->p++ = (duk_uint8_t) (val); \ - } while (0) -#define DUK_BW_WRITE_RAW_U8_2(thr,bw_ctx,val1,val2) do { \ - duk_uint8_t *duk__p; \ - DUK_BW_ASSERT_SPACE((thr), (bw_ctx), 2); \ - duk__p = (bw_ctx)->p; \ - *duk__p++ = (duk_uint8_t) (val1); \ - *duk__p++ = (duk_uint8_t) (val2); \ - (bw_ctx)->p = duk__p; \ - } while (0) -#define DUK_BW_WRITE_RAW_U8_3(thr,bw_ctx,val1,val2,val3) do { \ - duk_uint8_t *duk__p; \ - DUK_BW_ASSERT_SPACE((thr), (bw_ctx), 3); \ - duk__p = (bw_ctx)->p; \ - *duk__p++ = (duk_uint8_t) (val1); \ - *duk__p++ = (duk_uint8_t) (val2); \ - *duk__p++ = (duk_uint8_t) (val3); \ - (bw_ctx)->p = duk__p; \ - } while (0) -#define DUK_BW_WRITE_RAW_U8_4(thr,bw_ctx,val1,val2,val3,val4) do { \ - duk_uint8_t *duk__p; \ - DUK_BW_ASSERT_SPACE((thr), (bw_ctx), 4); \ - duk__p = (bw_ctx)->p; \ - *duk__p++ = (duk_uint8_t) (val1); \ - *duk__p++ = (duk_uint8_t) (val2); \ - *duk__p++ = (duk_uint8_t) (val3); \ - *duk__p++ = (duk_uint8_t) (val4); \ - (bw_ctx)->p = duk__p; \ - } while (0) -#define DUK_BW_WRITE_RAW_U8_5(thr,bw_ctx,val1,val2,val3,val4,val5) do { \ - duk_uint8_t *duk__p; \ - DUK_BW_ASSERT_SPACE((thr), (bw_ctx), 5); \ - duk__p = (bw_ctx)->p; \ - *duk__p++ = (duk_uint8_t) (val1); \ - *duk__p++ = (duk_uint8_t) (val2); \ - *duk__p++ = (duk_uint8_t) (val3); \ - *duk__p++ = (duk_uint8_t) (val4); \ - *duk__p++ = (duk_uint8_t) (val5); \ - (bw_ctx)->p = duk__p; \ - } while (0) -#define DUK_BW_WRITE_RAW_U8_6(thr,bw_ctx,val1,val2,val3,val4,val5,val6) do { \ - duk_uint8_t *duk__p; \ - DUK_BW_ASSERT_SPACE((thr), (bw_ctx), 6); \ - duk__p = (bw_ctx)->p; \ - *duk__p++ = (duk_uint8_t) (val1); \ - *duk__p++ = (duk_uint8_t) (val2); \ - *duk__p++ = (duk_uint8_t) (val3); \ - *duk__p++ = (duk_uint8_t) (val4); \ - *duk__p++ = (duk_uint8_t) (val5); \ - *duk__p++ = (duk_uint8_t) (val6); \ - (bw_ctx)->p = duk__p; \ - } while (0) -#define DUK_BW_WRITE_RAW_XUTF8(thr,bw_ctx,cp) do { \ - duk_ucodepoint_t duk__cp; \ - duk_small_int_t duk__enc_len; \ - duk__cp = (cp); \ - DUK_BW_ASSERT_SPACE((thr), (bw_ctx), duk_unicode_get_xutf8_length(duk__cp)); \ - duk__enc_len = duk_unicode_encode_xutf8(duk__cp, (bw_ctx)->p); \ - (bw_ctx)->p += duk__enc_len; \ - } while (0) -#define DUK_BW_WRITE_RAW_CESU8(thr,bw_ctx,cp) do { \ - duk_ucodepoint_t duk__cp; \ - duk_small_int_t duk__enc_len; \ - duk__cp = (duk_ucodepoint_t) (cp); \ - DUK_BW_ASSERT_SPACE((thr), (bw_ctx), duk_unicode_get_cesu8_length(duk__cp)); \ - duk__enc_len = duk_unicode_encode_cesu8(duk__cp, (bw_ctx)->p); \ - (bw_ctx)->p += duk__enc_len; \ - } while (0) -/* XXX: add temporary duk__p pointer here too; sharing */ -#define DUK_BW_WRITE_RAW_BYTES(thr,bw_ctx,valptr,valsz) do { \ - const void *duk__valptr; \ - duk_size_t duk__valsz; \ - duk__valptr = (const void *) (valptr); \ - duk__valsz = (duk_size_t) (valsz); \ - DUK_MEMCPY((void *) ((bw_ctx)->p), duk__valptr, duk__valsz); \ - (bw_ctx)->p += duk__valsz; \ - } while (0) -#define DUK_BW_WRITE_RAW_CSTRING(thr,bw_ctx,val) do { \ - const duk_uint8_t *duk__val; \ - duk_size_t duk__val_len; \ - duk__val = (const duk_uint8_t *) (val); \ - duk__val_len = DUK_STRLEN((const char *) duk__val); \ - DUK_MEMCPY((void *) ((bw_ctx)->p), (const void *) duk__val, duk__val_len); \ - (bw_ctx)->p += duk__val_len; \ - } while (0) -#define DUK_BW_WRITE_RAW_HSTRING(thr,bw_ctx,val) do { \ - duk_size_t duk__val_len; \ - duk__val_len = DUK_HSTRING_GET_BYTELEN((val)); \ - DUK_MEMCPY((void *) ((bw_ctx)->p), (const void *) DUK_HSTRING_GET_DATA((val)), duk__val_len); \ - (bw_ctx)->p += duk__val_len; \ - } while (0) -#define DUK_BW_WRITE_RAW_HBUFFER(thr,bw_ctx,val) do { \ - duk_size_t duk__val_len; \ - duk__val_len = DUK_HBUFFER_GET_SIZE((val)); \ - DUK_MEMCPY((void *) ((bw_ctx)->p), (const void *) DUK_HBUFFER_GET_DATA_PTR((thr)->heap, (val)), duk__val_len); \ - (bw_ctx)->p += duk__val_len; \ - } while (0) -#define DUK_BW_WRITE_RAW_HBUFFER_FIXED(thr,bw_ctx,val) do { \ - duk_size_t duk__val_len; \ - duk__val_len = DUK_HBUFFER_FIXED_GET_SIZE((val)); \ - DUK_MEMCPY((void *) ((bw_ctx)->p), (const void *) DUK_HBUFFER_FIXED_GET_DATA_PTR((thr)->heap, (val)), duk__val_len); \ - (bw_ctx)->p += duk__val_len; \ - } while (0) -#define DUK_BW_WRITE_RAW_HBUFFER_DYNAMIC(thr,bw_ctx,val) do { \ - duk_size_t duk__val_len; \ - duk__val_len = DUK_HBUFFER_DYNAMIC_GET_SIZE((val)); \ - DUK_MEMCPY((void *) ((bw_ctx)->p), (const void *) DUK_HBUFFER_DYNAMIC_GET_DATA_PTR((thr)->heap, (val)), duk__val_len); \ - (bw_ctx)->p += duk__val_len; \ - } while (0) - -/* Append bytes from a slice already in the buffer. */ -#define DUK_BW_WRITE_RAW_SLICE(thr,bw,dst_off,dst_len) \ - duk_bw_write_raw_slice((thr), (bw), (dst_off), (dst_len)) - -/* Insert bytes in the middle of the buffer from an external buffer. */ -#define DUK_BW_INSERT_RAW_BYTES(thr,bw,dst_off,buf,len) \ - duk_bw_insert_raw_bytes((thr), (bw), (dst_off), (buf), (len)) - -/* Insert bytes in the middle of the buffer from a slice already - * in the buffer. Source offset is interpreted "before" the operation. - */ -#define DUK_BW_INSERT_RAW_SLICE(thr,bw,dst_off,src_off,len) \ - duk_bw_insert_raw_slice((thr), (bw), (dst_off), (src_off), (len)) - -/* Insert a reserved area somewhere in the buffer; caller fills it. - * Evaluates to a (duk_uint_t *) pointing to the start of the reserved - * area for convenience. - */ -#define DUK_BW_INSERT_RAW_AREA(thr,bw,off,len) \ - duk_bw_insert_raw_area((thr), (bw), (off), (len)) - -/* Remove a slice from inside buffer. */ -#define DUK_BW_REMOVE_RAW_SLICE(thr,bw,off,len) \ - duk_bw_remove_raw_slice((thr), (bw), (off), (len)) - -/* Safe write calls which will ensure space first. */ - -#define DUK_BW_WRITE_ENSURE_U8(thr,bw_ctx,val) do { \ - DUK_BW_ENSURE((thr), (bw_ctx), 1); \ - DUK_BW_WRITE_RAW_U8((thr), (bw_ctx), (val)); \ - } while (0) -#define DUK_BW_WRITE_ENSURE_U8_2(thr,bw_ctx,val1,val2) do { \ - DUK_BW_ENSURE((thr), (bw_ctx), 2); \ - DUK_BW_WRITE_RAW_U8_2((thr), (bw_ctx), (val1), (val2)); \ - } while (0) -#define DUK_BW_WRITE_ENSURE_U8_3(thr,bw_ctx,val1,val2,val3) do { \ - DUK_BW_ENSURE((thr), (bw_ctx), 3); \ - DUK_BW_WRITE_RAW_U8_3((thr), (bw_ctx), (val1), (val2), (val3)); \ - } while (0) -#define DUK_BW_WRITE_ENSURE_U8_4(thr,bw_ctx,val1,val2,val3,val4) do { \ - DUK_BW_ENSURE((thr), (bw_ctx), 4); \ - DUK_BW_WRITE_RAW_U8_4((thr), (bw_ctx), (val1), (val2), (val3), (val4)); \ - } while (0) -#define DUK_BW_WRITE_ENSURE_U8_5(thr,bw_ctx,val1,val2,val3,val4,val5) do { \ - DUK_BW_ENSURE((thr), (bw_ctx), 5); \ - DUK_BW_WRITE_RAW_U8_5((thr), (bw_ctx), (val1), (val2), (val3), (val4), (val5)); \ - } while (0) -#define DUK_BW_WRITE_ENSURE_U8_6(thr,bw_ctx,val1,val2,val3,val4,val5,val6) do { \ - DUK_BW_ENSURE((thr), (bw_ctx), 6); \ - DUK_BW_WRITE_RAW_U8_6((thr), (bw_ctx), (val1), (val2), (val3), (val4), (val5), (val6)); \ - } while (0) -#define DUK_BW_WRITE_ENSURE_XUTF8(thr,bw_ctx,cp) do { \ - DUK_BW_ENSURE((thr), (bw_ctx), DUK_UNICODE_MAX_XUTF8_LENGTH); \ - DUK_BW_WRITE_RAW_XUTF8((thr), (bw_ctx), (cp)); \ - } while (0) -#define DUK_BW_WRITE_ENSURE_CESU8(thr,bw_ctx,cp) do { \ - DUK_BW_ENSURE((thr), (bw_ctx), DUK_UNICODE_MAX_CESU8_LENGTH); \ - DUK_BW_WRITE_RAW_CESU8((thr), (bw_ctx), (cp)); \ - } while (0) -/* XXX: add temporary duk__p pointer here too; sharing */ -#define DUK_BW_WRITE_ENSURE_BYTES(thr,bw_ctx,valptr,valsz) do { \ - const void *duk__valptr; \ - duk_size_t duk__valsz; \ - duk__valptr = (const void *) (valptr); \ - duk__valsz = (duk_size_t) (valsz); \ - DUK_BW_ENSURE((thr), (bw_ctx), duk__valsz); \ - DUK_MEMCPY((void *) ((bw_ctx)->p), duk__valptr, duk__valsz); \ - (bw_ctx)->p += duk__valsz; \ - } while (0) -#define DUK_BW_WRITE_ENSURE_CSTRING(thr,bw_ctx,val) do { \ - const duk_uint8_t *duk__val; \ - duk_size_t duk__val_len; \ - duk__val = (const duk_uint8_t *) (val); \ - duk__val_len = DUK_STRLEN((const char *) duk__val); \ - DUK_BW_ENSURE((thr), (bw_ctx), duk__val_len); \ - DUK_MEMCPY((void *) ((bw_ctx)->p), (const void *) duk__val, duk__val_len); \ - (bw_ctx)->p += duk__val_len; \ - } while (0) -#define DUK_BW_WRITE_ENSURE_HSTRING(thr,bw_ctx,val) do { \ - duk_size_t duk__val_len; \ - duk__val_len = DUK_HSTRING_GET_BYTELEN((val)); \ - DUK_BW_ENSURE((thr), (bw_ctx), duk__val_len); \ - DUK_MEMCPY((void *) ((bw_ctx)->p), (const void *) DUK_HSTRING_GET_DATA((val)), duk__val_len); \ - (bw_ctx)->p += duk__val_len; \ - } while (0) -#define DUK_BW_WRITE_ENSURE_HBUFFER(thr,bw_ctx,val) do { \ - duk_size_t duk__val_len; \ - duk__val_len = DUK_HBUFFER_GET_SIZE((val)); \ - DUK_BW_ENSURE((thr), (bw_ctx), duk__val_len); \ - DUK_MEMCPY((void *) ((bw_ctx)->p), (const void *) DUK_HBUFFER_GET_DATA_PTR((thr)->heap, (val)), duk__val_len); \ - (bw_ctx)->p += duk__val_len; \ - } while (0) -#define DUK_BW_WRITE_ENSURE_HBUFFER_FIXED(thr,bw_ctx,val) do { \ - duk_size_t duk__val_len; \ - duk__val_len = DUK_HBUFFER_FIXED_GET_SIZE((val)); \ - DUK_BW_ENSURE((thr), (bw_ctx), duk__val_len); \ - DUK_MEMCPY((void *) ((bw_ctx)->p), (const void *) DUK_HBUFFER_FIXED_GET_DATA_PTR((thr)->heap, (val)), duk__val_len); \ - (bw_ctx)->p += duk__val_len; \ - } while (0) -#define DUK_BW_WRITE_ENSURE_HBUFFER_DYNAMIC(thr,bw_ctx,val) do { \ - duk_size_t duk__val_len; \ - duk__val_len = DUK_HBUFFER_DYNAMIC_GET_SIZE((val)); \ - DUK_BW_ENSURE((thr), (bw_ctx), duk__val_len); \ - DUK_MEMCPY((void *) ((bw_ctx)->p), (const void *) DUK_HBUFFER_DYNAMIC_GET_DATA_PTR((thr)->heap, (val)), duk__val_len); \ - (bw_ctx)->p += duk__val_len; \ - } while (0) - -#define DUK_BW_WRITE_ENSURE_SLICE(thr,bw,dst_off,dst_len) \ - duk_bw_write_ensure_slice((thr), (bw), (dst_off), (dst_len)) -#define DUK_BW_INSERT_ENSURE_BYTES(thr,bw,dst_off,buf,len) \ - duk_bw_insert_ensure_bytes((thr), (bw), (dst_off), (buf), (len)) -#define DUK_BW_INSERT_ENSURE_SLICE(thr,bw,dst_off,src_off,len) \ - duk_bw_insert_ensure_slice((thr), (bw), (dst_off), (src_off), (len)) -#define DUK_BW_INSERT_ENSURE_AREA(thr,bw,off,len) \ - /* Evaluates to (duk_uint8_t *) pointing to start of area. */ \ - duk_bw_insert_ensure_area((thr), (bw), (off), (len)) -#define DUK_BW_REMOVE_ENSURE_SLICE(thr,bw,off,len) \ - /* No difference between raw/ensure because the buffer shrinks. */ \ - DUK_BW_REMOVE_RAW_SLICE((thr), (bw), (off), (len)) - -/* - * Externs and prototypes - */ - -#if !defined(DUK_SINGLE_FILE) -DUK_INTERNAL_DECL const duk_uint8_t duk_lc_digits[36]; -DUK_INTERNAL_DECL const duk_uint8_t duk_uc_nybbles[16]; -DUK_INTERNAL_DECL const duk_int8_t duk_hex_dectab[256]; -#if defined(DUK_USE_HEX_FASTPATH) -DUK_INTERNAL_DECL const duk_int16_t duk_hex_dectab_shift4[256]; -DUK_INTERNAL_DECL const duk_uint16_t duk_hex_enctab[256]; -#endif -#if defined(DUK_USE_BASE64_FASTPATH) -DUK_INTERNAL_DECL const duk_uint8_t duk_base64_enctab[64]; -DUK_INTERNAL_DECL const duk_int8_t duk_base64_dectab[256]; -#endif -#endif /* !DUK_SINGLE_FILE */ - -/* Note: assumes that duk_util_probe_steps size is 32 */ -#if defined(DUK_USE_HOBJECT_HASH_PART) || defined(DUK_USE_STRTAB_PROBE) -#if !defined(DUK_SINGLE_FILE) -DUK_INTERNAL_DECL duk_uint8_t duk_util_probe_steps[32]; -#endif /* !DUK_SINGLE_FILE */ -#endif - -#if defined(DUK_USE_STRHASH_DENSE) -DUK_INTERNAL_DECL duk_uint32_t duk_util_hashbytes(const duk_uint8_t *data, duk_size_t len, duk_uint32_t seed); -#endif - -#if defined(DUK_USE_HOBJECT_HASH_PART) || defined(DUK_USE_STRTAB_PROBE) -DUK_INTERNAL_DECL duk_uint32_t duk_util_get_hash_prime(duk_uint32_t size); -#endif - -DUK_INTERNAL_DECL duk_int32_t duk_bd_decode(duk_bitdecoder_ctx *ctx, duk_small_int_t bits); -DUK_INTERNAL_DECL duk_small_int_t duk_bd_decode_flag(duk_bitdecoder_ctx *ctx); -DUK_INTERNAL_DECL duk_int32_t duk_bd_decode_flagged(duk_bitdecoder_ctx *ctx, duk_small_int_t bits, duk_int32_t def_value); - -DUK_INTERNAL_DECL void duk_be_encode(duk_bitencoder_ctx *ctx, duk_uint32_t data, duk_small_int_t bits); -DUK_INTERNAL_DECL void duk_be_finish(duk_bitencoder_ctx *ctx); - -DUK_INTERNAL_DECL duk_uint32_t duk_util_tinyrandom_get_bits(duk_hthread *thr, duk_small_int_t n); -DUK_INTERNAL_DECL duk_double_t duk_util_tinyrandom_get_double(duk_hthread *thr); - -DUK_INTERNAL_DECL void duk_bw_init(duk_hthread *thr, duk_bufwriter_ctx *bw_ctx, duk_hbuffer_dynamic *h_buf); -DUK_INTERNAL_DECL void duk_bw_init_pushbuf(duk_hthread *thr, duk_bufwriter_ctx *bw_ctx, duk_size_t buf_size); -DUK_INTERNAL_DECL duk_uint8_t *duk_bw_resize(duk_hthread *thr, duk_bufwriter_ctx *bw_ctx, duk_size_t sz); -DUK_INTERNAL_DECL void duk_bw_compact(duk_hthread *thr, duk_bufwriter_ctx *bw_ctx); -DUK_INTERNAL_DECL void duk_bw_write_raw_slice(duk_hthread *thr, duk_bufwriter_ctx *bw, duk_size_t src_off, duk_size_t len); -DUK_INTERNAL_DECL void duk_bw_write_ensure_slice(duk_hthread *thr, duk_bufwriter_ctx *bw, duk_size_t src_off, duk_size_t len); -DUK_INTERNAL_DECL void duk_bw_insert_raw_bytes(duk_hthread *thr, duk_bufwriter_ctx *bw, duk_size_t dst_off, const duk_uint8_t *buf, duk_size_t len); -DUK_INTERNAL_DECL void duk_bw_insert_ensure_bytes(duk_hthread *thr, duk_bufwriter_ctx *bw, duk_size_t dst_off, const duk_uint8_t *buf, duk_size_t len); -DUK_INTERNAL_DECL void duk_bw_insert_raw_slice(duk_hthread *thr, duk_bufwriter_ctx *bw, duk_size_t dst_off, duk_size_t src_off, duk_size_t len); -DUK_INTERNAL_DECL void duk_bw_insert_ensure_slice(duk_hthread *thr, duk_bufwriter_ctx *bw, duk_size_t dst_off, duk_size_t src_off, duk_size_t len); -DUK_INTERNAL_DECL duk_uint8_t *duk_bw_insert_raw_area(duk_hthread *thr, duk_bufwriter_ctx *bw, duk_size_t off, duk_size_t len); -DUK_INTERNAL_DECL duk_uint8_t *duk_bw_insert_ensure_area(duk_hthread *thr, duk_bufwriter_ctx *bw, duk_size_t off, duk_size_t len); -DUK_INTERNAL_DECL void duk_bw_remove_raw_slice(duk_hthread *thr, duk_bufwriter_ctx *bw, duk_size_t off, duk_size_t len); -/* No duk_bw_remove_ensure_slice(), functionality would be identical. */ - -DUK_INTERNAL_DECL DUK_INLINE duk_uint16_t duk_raw_read_u16_be(duk_uint8_t **p); -DUK_INTERNAL_DECL DUK_INLINE duk_uint32_t duk_raw_read_u32_be(duk_uint8_t **p); -DUK_INTERNAL_DECL DUK_INLINE duk_double_t duk_raw_read_double_be(duk_uint8_t **p); -DUK_INTERNAL_DECL DUK_INLINE void duk_raw_write_u16_be(duk_uint8_t **p, duk_uint16_t val); -DUK_INTERNAL_DECL DUK_INLINE void duk_raw_write_u32_be(duk_uint8_t **p, duk_uint32_t val); -DUK_INTERNAL_DECL DUK_INLINE void duk_raw_write_double_be(duk_uint8_t **p, duk_double_t val); - -#if defined(DUK_USE_DEBUGGER_SUPPORT) /* For now only needed by the debugger. */ -DUK_INTERNAL void duk_byteswap_bytes(duk_uint8_t *p, duk_small_uint_t len); -#endif - -#endif /* DUK_UTIL_H_INCLUDED */ -#line 1 "duk_strings.h" -/* - * Shared error messages: declarations and macros - * - * Error messages are accessed through macros with fine-grained, explicit - * error message distinctions. Concrete error messages are selected by the - * macros and multiple macros can map to the same concrete string to save - * on code footprint. This allows flexible footprint/verbosity tuning with - * minimal code impact. There are a few limitations to this approach: - * (1) switching between plain messages and format strings doesn't work - * conveniently, and (2) conditional strings are a bit awkward to handle. - * - * Because format strings behave differently in the call site (they need to - * be followed by format arguments), they have a special prefix (DUK_STR_FMT_ - * and duk_str_fmt_). - * - * On some compilers using explicit shared strings is preferable; on others - * it may be better to use straight literals because the compiler will combine - * them anyway, and such strings won't end up unnecessarily in a symbol table. - */ - -#ifndef DUK_ERRMSG_H_INCLUDED -#define DUK_ERRMSG_H_INCLUDED - -#define DUK_STR_INTERNAL_ERROR duk_str_internal_error -#define DUK_STR_INVALID_COUNT duk_str_invalid_count -#define DUK_STR_INVALID_CALL_ARGS duk_str_invalid_call_args -#define DUK_STR_NOT_CONSTRUCTABLE duk_str_not_constructable -#define DUK_STR_NOT_CALLABLE duk_str_not_callable -#define DUK_STR_NOT_EXTENSIBLE duk_str_not_extensible -#define DUK_STR_NOT_WRITABLE duk_str_not_writable -#define DUK_STR_NOT_CONFIGURABLE duk_str_not_configurable - -#if !defined(DUK_SINGLE_FILE) -DUK_INTERNAL_DECL const char *duk_str_internal_error; -DUK_INTERNAL_DECL const char *duk_str_invalid_count; -DUK_INTERNAL_DECL const char *duk_str_invalid_call_args; -DUK_INTERNAL_DECL const char *duk_str_not_constructable; -DUK_INTERNAL_DECL const char *duk_str_not_callable; -DUK_INTERNAL_DECL const char *duk_str_not_extensible; -DUK_INTERNAL_DECL const char *duk_str_not_writable; -DUK_INTERNAL_DECL const char *duk_str_not_configurable; -#endif /* !DUK_SINGLE_FILE */ - -#define DUK_STR_INVALID_CONTEXT duk_str_invalid_context -#define DUK_STR_INVALID_INDEX duk_str_invalid_call_args -#define DUK_STR_PUSH_BEYOND_ALLOC_STACK duk_str_push_beyond_alloc_stack -#define DUK_STR_NOT_UNDEFINED duk_str_unexpected_type -#define DUK_STR_NOT_NULL duk_str_unexpected_type -#define DUK_STR_NOT_BOOLEAN duk_str_unexpected_type -#define DUK_STR_NOT_NUMBER duk_str_unexpected_type -#define DUK_STR_NOT_STRING duk_str_unexpected_type -#define DUK_STR_NOT_OBJECT duk_str_unexpected_type -#define DUK_STR_NOT_POINTER duk_str_unexpected_type -#define DUK_STR_NOT_BUFFER duk_str_not_buffer /* still in use with verbose messages */ -#define DUK_STR_UNEXPECTED_TYPE duk_str_unexpected_type -#define DUK_STR_NOT_THREAD duk_str_unexpected_type -#define DUK_STR_NOT_COMPILEDFUNCTION duk_str_unexpected_type -#define DUK_STR_NOT_NATIVEFUNCTION duk_str_unexpected_type -#define DUK_STR_NOT_C_FUNCTION duk_str_unexpected_type -#define DUK_STR_NOT_FUNCTION duk_str_unexpected_type -#define DUK_STR_NOT_REGEXP duk_str_unexpected_type -#define DUK_STR_DEFAULTVALUE_COERCE_FAILED duk_str_defaultvalue_coerce_failed -#define DUK_STR_NUMBER_OUTSIDE_RANGE duk_str_number_outside_range -#define DUK_STR_NOT_OBJECT_COERCIBLE duk_str_not_object_coercible -#define DUK_STR_STRING_TOO_LONG duk_str_string_too_long -#define DUK_STR_BUFFER_TOO_LONG duk_str_buffer_too_long -#define DUK_STR_SPRINTF_TOO_LONG duk_str_sprintf_too_long -#define DUK_STR_ALLOC_FAILED duk_str_alloc_failed -#define DUK_STR_POP_TOO_MANY duk_str_pop_too_many -#define DUK_STR_WRONG_BUFFER_TYPE duk_str_wrong_buffer_type -#define DUK_STR_ENCODE_FAILED duk_str_encode_failed -#define DUK_STR_DECODE_FAILED duk_str_decode_failed -#define DUK_STR_NO_SOURCECODE duk_str_no_sourcecode -#define DUK_STR_CONCAT_RESULT_TOO_LONG duk_str_concat_result_too_long -#define DUK_STR_UNIMPLEMENTED duk_str_unimplemented -#define DUK_STR_UNSUPPORTED duk_str_unsupported -#define DUK_STR_ARRAY_LENGTH_OVER_2G duk_str_array_length_over_2g - -#if !defined(DUK_SINGLE_FILE) -DUK_INTERNAL_DECL const char *duk_str_invalid_context; -DUK_INTERNAL_DECL const char *duk_str_push_beyond_alloc_stack; -DUK_INTERNAL_DECL const char *duk_str_not_buffer; -DUK_INTERNAL_DECL const char *duk_str_unexpected_type; -DUK_INTERNAL_DECL const char *duk_str_defaultvalue_coerce_failed; -DUK_INTERNAL_DECL const char *duk_str_number_outside_range; -DUK_INTERNAL_DECL const char *duk_str_not_object_coercible; -DUK_INTERNAL_DECL const char *duk_str_string_too_long; -DUK_INTERNAL_DECL const char *duk_str_buffer_too_long; -DUK_INTERNAL_DECL const char *duk_str_sprintf_too_long; -DUK_INTERNAL_DECL const char *duk_str_alloc_failed; -DUK_INTERNAL_DECL const char *duk_str_pop_too_many; -DUK_INTERNAL_DECL const char *duk_str_wrong_buffer_type; -DUK_INTERNAL_DECL const char *duk_str_encode_failed; -DUK_INTERNAL_DECL const char *duk_str_decode_failed; -DUK_INTERNAL_DECL const char *duk_str_no_sourcecode; -DUK_INTERNAL_DECL const char *duk_str_concat_result_too_long; -DUK_INTERNAL_DECL const char *duk_str_unimplemented; -DUK_INTERNAL_DECL const char *duk_str_unsupported; -DUK_INTERNAL_DECL const char *duk_str_array_length_over_2g; -#endif /* !DUK_SINGLE_FILE */ - -#define DUK_STR_FMT_PTR duk_str_fmt_ptr -#define DUK_STR_FMT_INVALID_JSON duk_str_fmt_invalid_json -#define DUK_STR_JSONDEC_RECLIMIT duk_str_jsondec_reclimit -#define DUK_STR_JSONENC_RECLIMIT duk_str_jsonenc_reclimit -#define DUK_STR_CYCLIC_INPUT duk_str_cyclic_input - -#if !defined(DUK_SINGLE_FILE) -DUK_INTERNAL_DECL const char *duk_str_fmt_ptr; -DUK_INTERNAL_DECL const char *duk_str_fmt_invalid_json; -DUK_INTERNAL_DECL const char *duk_str_jsondec_reclimit; -DUK_INTERNAL_DECL const char *duk_str_jsonenc_reclimit; -DUK_INTERNAL_DECL const char *duk_str_cyclic_input; -#endif /* !DUK_SINGLE_FILE */ - -#define DUK_STR_PROXY_REVOKED duk_str_proxy_revoked -#define DUK_STR_INVALID_BASE duk_str_invalid_base -#define DUK_STR_STRICT_CALLER_READ duk_str_strict_caller_read -#define DUK_STR_PROXY_REJECTED duk_str_proxy_rejected -#define DUK_STR_INVALID_ARRAY_LENGTH duk_str_invalid_array_length -#define DUK_STR_ARRAY_LENGTH_WRITE_FAILED duk_str_array_length_write_failed -#define DUK_STR_ARRAY_LENGTH_NOT_WRITABLE duk_str_array_length_not_writable -#define DUK_STR_SETTER_UNDEFINED duk_str_setter_undefined -#define DUK_STR_REDEFINE_VIRT_PROP duk_str_redefine_virt_prop -#define DUK_STR_INVALID_DESCRIPTOR duk_str_invalid_descriptor -#define DUK_STR_PROPERTY_IS_VIRTUAL duk_str_property_is_virtual - -#if !defined(DUK_SINGLE_FILE) -DUK_INTERNAL_DECL const char *duk_str_proxy_revoked; -DUK_INTERNAL_DECL const char *duk_str_invalid_base; -DUK_INTERNAL_DECL const char *duk_str_strict_caller_read; -DUK_INTERNAL_DECL const char *duk_str_proxy_rejected; -DUK_INTERNAL_DECL const char *duk_str_invalid_array_length; -DUK_INTERNAL_DECL const char *duk_str_array_length_write_failed; -DUK_INTERNAL_DECL const char *duk_str_array_length_not_writable; -DUK_INTERNAL_DECL const char *duk_str_setter_undefined; -DUK_INTERNAL_DECL const char *duk_str_redefine_virt_prop; -DUK_INTERNAL_DECL const char *duk_str_invalid_descriptor; -DUK_INTERNAL_DECL const char *duk_str_property_is_virtual; -#endif /* !DUK_SINGLE_FILE */ - -#define DUK_STR_PARSE_ERROR duk_str_parse_error -#define DUK_STR_DUPLICATE_LABEL duk_str_duplicate_label -#define DUK_STR_INVALID_LABEL duk_str_invalid_label -#define DUK_STR_INVALID_ARRAY_LITERAL duk_str_invalid_array_literal -#define DUK_STR_INVALID_OBJECT_LITERAL duk_str_invalid_object_literal -#define DUK_STR_INVALID_VAR_DECLARATION duk_str_invalid_var_declaration -#define DUK_STR_CANNOT_DELETE_IDENTIFIER duk_str_cannot_delete_identifier -#define DUK_STR_INVALID_EXPRESSION duk_str_invalid_expression -#define DUK_STR_INVALID_LVALUE duk_str_invalid_lvalue -#define DUK_STR_EXPECTED_IDENTIFIER duk_str_expected_identifier -#define DUK_STR_EMPTY_EXPR_NOT_ALLOWED duk_str_empty_expr_not_allowed -#define DUK_STR_INVALID_FOR duk_str_invalid_for -#define DUK_STR_INVALID_SWITCH duk_str_invalid_switch -#define DUK_STR_INVALID_BREAK_CONT_LABEL duk_str_invalid_break_cont_label -#define DUK_STR_INVALID_RETURN duk_str_invalid_return -#define DUK_STR_INVALID_TRY duk_str_invalid_try -#define DUK_STR_INVALID_THROW duk_str_invalid_throw -#define DUK_STR_WITH_IN_STRICT_MODE duk_str_with_in_strict_mode -#define DUK_STR_FUNC_STMT_NOT_ALLOWED duk_str_func_stmt_not_allowed -#define DUK_STR_UNTERMINATED_STMT duk_str_unterminated_stmt -#define DUK_STR_INVALID_ARG_NAME duk_str_invalid_arg_name -#define DUK_STR_INVALID_FUNC_NAME duk_str_invalid_func_name -#define DUK_STR_INVALID_GETSET_NAME duk_str_invalid_getset_name -#define DUK_STR_FUNC_NAME_REQUIRED duk_str_func_name_required - -#if !defined(DUK_SINGLE_FILE) -DUK_INTERNAL_DECL const char *duk_str_parse_error; -DUK_INTERNAL_DECL const char *duk_str_duplicate_label; -DUK_INTERNAL_DECL const char *duk_str_invalid_label; -DUK_INTERNAL_DECL const char *duk_str_invalid_array_literal; -DUK_INTERNAL_DECL const char *duk_str_invalid_object_literal; -DUK_INTERNAL_DECL const char *duk_str_invalid_var_declaration; -DUK_INTERNAL_DECL const char *duk_str_cannot_delete_identifier; -DUK_INTERNAL_DECL const char *duk_str_invalid_expression; -DUK_INTERNAL_DECL const char *duk_str_invalid_lvalue; -DUK_INTERNAL_DECL const char *duk_str_expected_identifier; -DUK_INTERNAL_DECL const char *duk_str_empty_expr_not_allowed; -DUK_INTERNAL_DECL const char *duk_str_invalid_for; -DUK_INTERNAL_DECL const char *duk_str_invalid_switch; -DUK_INTERNAL_DECL const char *duk_str_invalid_break_cont_label; -DUK_INTERNAL_DECL const char *duk_str_invalid_return; -DUK_INTERNAL_DECL const char *duk_str_invalid_try; -DUK_INTERNAL_DECL const char *duk_str_invalid_throw; -DUK_INTERNAL_DECL const char *duk_str_with_in_strict_mode; -DUK_INTERNAL_DECL const char *duk_str_func_stmt_not_allowed; -DUK_INTERNAL_DECL const char *duk_str_unterminated_stmt; -DUK_INTERNAL_DECL const char *duk_str_invalid_arg_name; -DUK_INTERNAL_DECL const char *duk_str_invalid_func_name; -DUK_INTERNAL_DECL const char *duk_str_invalid_getset_name; -DUK_INTERNAL_DECL const char *duk_str_func_name_required; -#endif /* !DUK_SINGLE_FILE */ - -#define DUK_STR_INVALID_QUANTIFIER_NO_ATOM duk_str_invalid_quantifier_no_atom -#define DUK_STR_INVALID_QUANTIFIER_VALUES duk_str_invalid_quantifier_values -#define DUK_STR_QUANTIFIER_TOO_MANY_COPIES duk_str_quantifier_too_many_copies -#define DUK_STR_UNEXPECTED_CLOSING_PAREN duk_str_unexpected_closing_paren -#define DUK_STR_UNEXPECTED_END_OF_PATTERN duk_str_unexpected_end_of_pattern -#define DUK_STR_UNEXPECTED_REGEXP_TOKEN duk_str_unexpected_regexp_token -#define DUK_STR_INVALID_REGEXP_FLAGS duk_str_invalid_regexp_flags -#define DUK_STR_INVALID_BACKREFS duk_str_invalid_backrefs - -#if !defined(DUK_SINGLE_FILE) -DUK_INTERNAL_DECL const char *duk_str_invalid_quantifier_no_atom; -DUK_INTERNAL_DECL const char *duk_str_invalid_quantifier_values; -DUK_INTERNAL_DECL const char *duk_str_quantifier_too_many_copies; -DUK_INTERNAL_DECL const char *duk_str_unexpected_closing_paren; -DUK_INTERNAL_DECL const char *duk_str_unexpected_end_of_pattern; -DUK_INTERNAL_DECL const char *duk_str_unexpected_regexp_token; -DUK_INTERNAL_DECL const char *duk_str_invalid_regexp_flags; -DUK_INTERNAL_DECL const char *duk_str_invalid_backrefs; -#endif /* !DUK_SINGLE_FILE */ - -#define DUK_STR_VALSTACK_LIMIT duk_str_valstack_limit -#define DUK_STR_CALLSTACK_LIMIT duk_str_callstack_limit -#define DUK_STR_CATCHSTACK_LIMIT duk_str_catchstack_limit -#define DUK_STR_PROTOTYPE_CHAIN_LIMIT duk_str_prototype_chain_limit -#define DUK_STR_BOUND_CHAIN_LIMIT duk_str_bound_chain_limit -#define DUK_STR_C_CALLSTACK_LIMIT duk_str_c_callstack_limit -#define DUK_STR_COMPILER_RECURSION_LIMIT duk_str_compiler_recursion_limit -#define DUK_STR_BYTECODE_LIMIT duk_str_bytecode_limit -#define DUK_STR_REG_LIMIT duk_str_reg_limit -#define DUK_STR_TEMP_LIMIT duk_str_temp_limit -#define DUK_STR_CONST_LIMIT duk_str_const_limit -#define DUK_STR_FUNC_LIMIT duk_str_func_limit -#define DUK_STR_REGEXP_COMPILER_RECURSION_LIMIT duk_str_regexp_compiler_recursion_limit -#define DUK_STR_REGEXP_EXECUTOR_RECURSION_LIMIT duk_str_regexp_executor_recursion_limit -#define DUK_STR_REGEXP_EXECUTOR_STEP_LIMIT duk_str_regexp_executor_step_limit - -#if !defined(DUK_SINGLE_FILE) -DUK_INTERNAL_DECL const char *duk_str_valstack_limit; -DUK_INTERNAL_DECL const char *duk_str_callstack_limit; -DUK_INTERNAL_DECL const char *duk_str_catchstack_limit; -DUK_INTERNAL_DECL const char *duk_str_prototype_chain_limit; -DUK_INTERNAL_DECL const char *duk_str_bound_chain_limit; -DUK_INTERNAL_DECL const char *duk_str_c_callstack_limit; -DUK_INTERNAL_DECL const char *duk_str_compiler_recursion_limit; -DUK_INTERNAL_DECL const char *duk_str_bytecode_limit; -DUK_INTERNAL_DECL const char *duk_str_reg_limit; -DUK_INTERNAL_DECL const char *duk_str_temp_limit; -DUK_INTERNAL_DECL const char *duk_str_const_limit; -DUK_INTERNAL_DECL const char *duk_str_func_limit; -DUK_INTERNAL_DECL const char *duk_str_regexp_compiler_recursion_limit; -DUK_INTERNAL_DECL const char *duk_str_regexp_executor_recursion_limit; -DUK_INTERNAL_DECL const char *duk_str_regexp_executor_step_limit; -#endif /* !DUK_SINGLE_FILE */ - -#if !defined(DUK_SINGLE_FILE) -DUK_INTERNAL_DECL const char *duk_str_anon; -#endif /* !DUK_SINGLE_FILE */ - -#endif /* DUK_ERRMSG_H_INCLUDED */ -#line 1 "duk_js_bytecode.h" -/* - * Ecmascript bytecode - */ - -#ifndef DUK_JS_BYTECODE_H_INCLUDED -#define DUK_JS_BYTECODE_H_INCLUDED - -/* - * Logical instruction layout - * ========================== - * - * !3!3!2!2!2!2!2!2!2!2!2!2!1!1!1!1!1!1!1!1!1!1! ! ! ! ! ! ! ! ! ! ! - * !1!0!9!8!7!6!5!4!3!2!1!0!9!8!7!6!5!4!3!2!1!0!9!8!7!6!5!4!3!2!1!0! - * +---------------------------------------------------+-----------+ - * ! C ! B ! A ! OP ! - * +---------------------------------------------------+-----------+ - * - * OP (6 bits): opcode (DUK_OP_*), access should be fastest - * A (8 bits): typically a target register number - * B (9 bits): typically first source register/constant number - * C (9 bits): typically second source register/constant number - * - * Some instructions combine BC or ABC together for larger parameter values. - * Signed integers (e.g. jump offsets) are encoded as unsigned, with an opcode - * specific bias. B and C may denote a register or a constant, see - * DUK_BC_ISREG() and DUK_BC_ISCONST(). - * - * Note: macro naming is a bit misleading, e.g. "ABC" in macro name but - * the field layout is logically "CBA". - */ - -typedef duk_uint32_t duk_instr_t; - -#define DUK_DEC_OP(x) ((x) & 0x3fUL) -#define DUK_DEC_A(x) (((x) >> 6) & 0xffUL) -#define DUK_DEC_B(x) (((x) >> 14) & 0x1ffUL) -#define DUK_DEC_C(x) (((x) >> 23) & 0x1ffUL) -#define DUK_DEC_BC(x) (((x) >> 14) & 0x3ffffUL) -#define DUK_DEC_ABC(x) (((x) >> 6) & 0x3ffffffUL) - -#define DUK_ENC_OP(op) ((duk_instr_t) (op)) -#define DUK_ENC_OP_ABC(op,abc) ((duk_instr_t) ( \ - (((duk_instr_t) (abc)) << 6) | \ - ((duk_instr_t) (op)) \ - )) -#define DUK_ENC_OP_A_BC(op,a,bc) ((duk_instr_t) ( \ - (((duk_instr_t) (bc)) << 14) | \ - (((duk_instr_t) (a)) << 6) | \ - ((duk_instr_t) (op)) \ - )) -#define DUK_ENC_OP_A_B_C(op,a,b,c) ((duk_instr_t) ( \ - (((duk_instr_t) (c)) << 23) | \ - (((duk_instr_t) (b)) << 14) | \ - (((duk_instr_t) (a)) << 6) | \ - ((duk_instr_t) (op)) \ - )) -#define DUK_ENC_OP_A_B(op,a,b) DUK_ENC_OP_A_B_C(op,a,b,0) -#define DUK_ENC_OP_A(op,a) DUK_ENC_OP_A_B_C(op,a,0,0) - -/* Constants should be signed so that signed arithmetic involving them - * won't cause values to be coerced accidentally to unsigned. - */ -#define DUK_BC_OP_MIN 0 -#define DUK_BC_OP_MAX 0x3fL -#define DUK_BC_A_MIN 0 -#define DUK_BC_A_MAX 0xffL -#define DUK_BC_B_MIN 0 -#define DUK_BC_B_MAX 0x1ffL -#define DUK_BC_C_MIN 0 -#define DUK_BC_C_MAX 0x1ffL -#define DUK_BC_BC_MIN 0 -#define DUK_BC_BC_MAX 0x3ffffL -#define DUK_BC_ABC_MIN 0 -#define DUK_BC_ABC_MAX 0x3ffffffL -#define DUK_BC_EXTRAOP_MIN DUK_BC_A_MIN -#define DUK_BC_EXTRAOP_MAX DUK_BC_A_MAX - -#define DUK_OP_LDREG 0 -#define DUK_OP_STREG 1 -#define DUK_OP_LDCONST 2 -#define DUK_OP_LDINT 3 -#define DUK_OP_LDINTX 4 -#define DUK_OP_MPUTOBJ 5 -#define DUK_OP_MPUTOBJI 6 -#define DUK_OP_MPUTARR 7 -#define DUK_OP_MPUTARRI 8 -#define DUK_OP_NEW 9 -#define DUK_OP_NEWI 10 -#define DUK_OP_REGEXP 11 -#define DUK_OP_CSREG 12 -#define DUK_OP_CSREGI 13 -#define DUK_OP_GETVAR 14 -#define DUK_OP_PUTVAR 15 -#define DUK_OP_DECLVAR 16 -#define DUK_OP_DELVAR 17 -#define DUK_OP_CSVAR 18 -#define DUK_OP_CSVARI 19 -#define DUK_OP_CLOSURE 20 -#define DUK_OP_GETPROP 21 -#define DUK_OP_PUTPROP 22 -#define DUK_OP_DELPROP 23 -#define DUK_OP_CSPROP 24 -#define DUK_OP_CSPROPI 25 -#define DUK_OP_ADD 26 -#define DUK_OP_SUB 27 -#define DUK_OP_MUL 28 -#define DUK_OP_DIV 29 -#define DUK_OP_MOD 30 -#define DUK_OP_BAND 31 -#define DUK_OP_BOR 32 -#define DUK_OP_BXOR 33 -#define DUK_OP_BASL 34 -#define DUK_OP_BLSR 35 -#define DUK_OP_BASR 36 -#define DUK_OP_EQ 37 -#define DUK_OP_NEQ 38 -#define DUK_OP_SEQ 39 -#define DUK_OP_SNEQ 40 -#define DUK_OP_GT 41 -#define DUK_OP_GE 42 -#define DUK_OP_LT 43 -#define DUK_OP_LE 44 -#define DUK_OP_IF 45 -#define DUK_OP_JUMP 46 -#define DUK_OP_RETURN 47 -#define DUK_OP_CALL 48 -#define DUK_OP_CALLI 49 -#define DUK_OP_TRYCATCH 50 -#define DUK_OP_EXTRA 51 -#define DUK_OP_PREINCR 52 /* pre/post opcode values have constraints, */ -#define DUK_OP_PREDECR 53 /* see duk_js_executor.c */ -#define DUK_OP_POSTINCR 54 -#define DUK_OP_POSTDECR 55 -#define DUK_OP_PREINCV 56 -#define DUK_OP_PREDECV 57 -#define DUK_OP_POSTINCV 58 -#define DUK_OP_POSTDECV 59 -#define DUK_OP_PREINCP 60 -#define DUK_OP_PREDECP 61 -#define DUK_OP_POSTINCP 62 -#define DUK_OP_POSTDECP 63 -#define DUK_OP_NONE 64 /* dummy value used as marker */ - -/* DUK_OP_EXTRA, sub-operation in A */ -#define DUK_EXTRAOP_NOP 0 -#define DUK_EXTRAOP_INVALID 1 -#define DUK_EXTRAOP_LDTHIS 2 -#define DUK_EXTRAOP_LDUNDEF 3 -#define DUK_EXTRAOP_LDNULL 4 -#define DUK_EXTRAOP_LDTRUE 5 -#define DUK_EXTRAOP_LDFALSE 6 -#define DUK_EXTRAOP_NEWOBJ 7 -#define DUK_EXTRAOP_NEWARR 8 -#define DUK_EXTRAOP_SETALEN 9 -#define DUK_EXTRAOP_TYPEOF 10 -#define DUK_EXTRAOP_TYPEOFID 11 -#define DUK_EXTRAOP_INITENUM 12 -#define DUK_EXTRAOP_NEXTENUM 13 -#define DUK_EXTRAOP_INITSET 14 -#define DUK_EXTRAOP_INITSETI 15 -#define DUK_EXTRAOP_INITGET 16 -#define DUK_EXTRAOP_INITGETI 17 -#define DUK_EXTRAOP_ENDTRY 18 -#define DUK_EXTRAOP_ENDCATCH 19 -#define DUK_EXTRAOP_ENDFIN 20 -#define DUK_EXTRAOP_THROW 21 -#define DUK_EXTRAOP_INVLHS 22 -#define DUK_EXTRAOP_UNM 23 -#define DUK_EXTRAOP_UNP 24 -#define DUK_EXTRAOP_DEBUGGER 25 -#define DUK_EXTRAOP_BREAK 26 -#define DUK_EXTRAOP_CONTINUE 27 -#define DUK_EXTRAOP_BNOT 28 -#define DUK_EXTRAOP_LNOT 29 -#define DUK_EXTRAOP_INSTOF 30 -#define DUK_EXTRAOP_IN 31 -#define DUK_EXTRAOP_LABEL 32 -#define DUK_EXTRAOP_ENDLABEL 33 - -/* DUK_OP_CALL flags in A */ -#define DUK_BC_CALL_FLAG_TAILCALL (1 << 0) -#define DUK_BC_CALL_FLAG_EVALCALL (1 << 1) - -/* DUK_OP_TRYCATCH flags in A */ -#define DUK_BC_TRYCATCH_FLAG_HAVE_CATCH (1 << 0) -#define DUK_BC_TRYCATCH_FLAG_HAVE_FINALLY (1 << 1) -#define DUK_BC_TRYCATCH_FLAG_CATCH_BINDING (1 << 2) -#define DUK_BC_TRYCATCH_FLAG_WITH_BINDING (1 << 3) - -/* DUK_OP_RETURN flags in A */ -#define DUK_BC_RETURN_FLAG_HAVE_RETVAL (1 << 0) - -/* DUK_OP_DECLVAR flags in A; bottom bits are reserved for propdesc flags (DUK_PROPDESC_FLAG_XXX) */ -#define DUK_BC_DECLVAR_FLAG_UNDEF_VALUE (1 << 4) /* use 'undefined' for value automatically */ -#define DUK_BC_DECLVAR_FLAG_FUNC_DECL (1 << 5) /* function declaration */ - -/* misc constants and helper macros */ -#define DUK_BC_REGLIMIT 256 /* if B/C is >= this value, refers to a const */ -#define DUK_BC_ISREG(x) ((x) < DUK_BC_REGLIMIT) -#define DUK_BC_ISCONST(x) ((x) >= DUK_BC_REGLIMIT) -#define DUK_BC_LDINT_BIAS (1L << 17) -#define DUK_BC_LDINTX_SHIFT 18 -#define DUK_BC_JUMP_BIAS (1L << 25) - -#endif /* DUK_JS_BYTECODE_H_INCLUDED */ -#line 1 "duk_lexer.h" -/* - * Lexer defines. - */ - -#ifndef DUK_LEXER_H_INCLUDED -#define DUK_LEXER_H_INCLUDED - -typedef void (*duk_re_range_callback)(void *user, duk_codepoint_t r1, duk_codepoint_t r2, duk_bool_t direct); - -/* - * A token is interpreted as any possible production of InputElementDiv - * and InputElementRegExp, see E5 Section 7 in its entirety. Note that - * the E5 "Token" production does not cover all actual tokens of the - * language (which is explicitly stated in the specification, Section 7.5). - * Null and boolean literals are defined as part of both ReservedWord - * (E5 Section 7.6.1) and Literal (E5 Section 7.8) productions. Here, - * null and boolean values have literal tokens, and are not reserved - * words. - * - * Decimal literal negative/positive sign is -not- part of DUK_TOK_NUMBER. - * The number tokens always have a non-negative value. The unary minus - * operator in "-1.0" is optimized during compilation to yield a single - * negative constant. - * - * Token numbering is free except that reserved words are required to be - * in a continuous range and in a particular order. See genstrings.py. - */ - -#define DUK_LEXER_INITCTX(ctx) duk_lexer_initctx((ctx)) - -#define DUK_LEXER_SETPOINT(ctx,pt) duk_lexer_setpoint((ctx), (pt)) - -#define DUK_LEXER_GETPOINT(ctx,pt) do { (pt)->offset = (ctx)->window[0].offset; \ - (pt)->line = (ctx)->window[0].line; } while (0) - -/* currently 6 characters of lookup are actually needed (duk_lexer.c) */ -#define DUK_LEXER_WINDOW_SIZE 6 -#if defined(DUK_USE_LEXER_SLIDING_WINDOW) -#define DUK_LEXER_BUFFER_SIZE 64 -#endif - -#define DUK_TOK_MINVAL 0 - -/* returned after EOF (infinite amount) */ -#define DUK_TOK_EOF 0 - -/* identifier names (E5 Section 7.6) */ -#define DUK_TOK_IDENTIFIER 1 - -/* reserved words: keywords */ -#define DUK_TOK_START_RESERVED 2 -#define DUK_TOK_BREAK 2 -#define DUK_TOK_CASE 3 -#define DUK_TOK_CATCH 4 -#define DUK_TOK_CONTINUE 5 -#define DUK_TOK_DEBUGGER 6 -#define DUK_TOK_DEFAULT 7 -#define DUK_TOK_DELETE 8 -#define DUK_TOK_DO 9 -#define DUK_TOK_ELSE 10 -#define DUK_TOK_FINALLY 11 -#define DUK_TOK_FOR 12 -#define DUK_TOK_FUNCTION 13 -#define DUK_TOK_IF 14 -#define DUK_TOK_IN 15 -#define DUK_TOK_INSTANCEOF 16 -#define DUK_TOK_NEW 17 -#define DUK_TOK_RETURN 18 -#define DUK_TOK_SWITCH 19 -#define DUK_TOK_THIS 20 -#define DUK_TOK_THROW 21 -#define DUK_TOK_TRY 22 -#define DUK_TOK_TYPEOF 23 -#define DUK_TOK_VAR 24 -#define DUK_TOK_CONST 25 -#define DUK_TOK_VOID 26 -#define DUK_TOK_WHILE 27 -#define DUK_TOK_WITH 28 - -/* reserved words: future reserved words */ -#define DUK_TOK_CLASS 29 -#define DUK_TOK_ENUM 30 -#define DUK_TOK_EXPORT 31 -#define DUK_TOK_EXTENDS 32 -#define DUK_TOK_IMPORT 33 -#define DUK_TOK_SUPER 34 - -/* "null", "true", and "false" are always reserved words. - * Note that "get" and "set" are not! - */ -#define DUK_TOK_NULL 35 -#define DUK_TOK_TRUE 36 -#define DUK_TOK_FALSE 37 - -/* reserved words: additional future reserved words in strict mode */ -#define DUK_TOK_START_STRICT_RESERVED 38 /* inclusive */ -#define DUK_TOK_IMPLEMENTS 38 -#define DUK_TOK_INTERFACE 39 -#define DUK_TOK_LET 40 -#define DUK_TOK_PACKAGE 41 -#define DUK_TOK_PRIVATE 42 -#define DUK_TOK_PROTECTED 43 -#define DUK_TOK_PUBLIC 44 -#define DUK_TOK_STATIC 45 -#define DUK_TOK_YIELD 46 - -#define DUK_TOK_END_RESERVED 47 /* exclusive */ - -/* "get" and "set" are tokens but NOT ReservedWords. They are currently - * parsed and identifiers and these defines are actually now unused. - */ -#define DUK_TOK_GET 47 -#define DUK_TOK_SET 48 - -/* punctuators (unlike the spec, also includes "/" and "/=") */ -#define DUK_TOK_LCURLY 49 -#define DUK_TOK_RCURLY 50 -#define DUK_TOK_LBRACKET 51 -#define DUK_TOK_RBRACKET 52 -#define DUK_TOK_LPAREN 53 -#define DUK_TOK_RPAREN 54 -#define DUK_TOK_PERIOD 55 -#define DUK_TOK_SEMICOLON 56 -#define DUK_TOK_COMMA 57 -#define DUK_TOK_LT 58 -#define DUK_TOK_GT 59 -#define DUK_TOK_LE 60 -#define DUK_TOK_GE 61 -#define DUK_TOK_EQ 62 -#define DUK_TOK_NEQ 63 -#define DUK_TOK_SEQ 64 -#define DUK_TOK_SNEQ 65 -#define DUK_TOK_ADD 66 -#define DUK_TOK_SUB 67 -#define DUK_TOK_MUL 68 -#define DUK_TOK_DIV 69 -#define DUK_TOK_MOD 70 -#define DUK_TOK_INCREMENT 71 -#define DUK_TOK_DECREMENT 72 -#define DUK_TOK_ALSHIFT 73 /* named "arithmetic" because result is signed */ -#define DUK_TOK_ARSHIFT 74 -#define DUK_TOK_RSHIFT 75 -#define DUK_TOK_BAND 76 -#define DUK_TOK_BOR 77 -#define DUK_TOK_BXOR 78 -#define DUK_TOK_LNOT 79 -#define DUK_TOK_BNOT 80 -#define DUK_TOK_LAND 81 -#define DUK_TOK_LOR 82 -#define DUK_TOK_QUESTION 83 -#define DUK_TOK_COLON 84 -#define DUK_TOK_EQUALSIGN 85 -#define DUK_TOK_ADD_EQ 86 -#define DUK_TOK_SUB_EQ 87 -#define DUK_TOK_MUL_EQ 88 -#define DUK_TOK_DIV_EQ 89 -#define DUK_TOK_MOD_EQ 90 -#define DUK_TOK_ALSHIFT_EQ 91 -#define DUK_TOK_ARSHIFT_EQ 92 -#define DUK_TOK_RSHIFT_EQ 93 -#define DUK_TOK_BAND_EQ 94 -#define DUK_TOK_BOR_EQ 95 -#define DUK_TOK_BXOR_EQ 96 - -/* literals (E5 Section 7.8), except null, true, false, which are treated - * like reserved words (above). - */ -#define DUK_TOK_NUMBER 97 -#define DUK_TOK_STRING 98 -#define DUK_TOK_REGEXP 99 - -#define DUK_TOK_MAXVAL 99 /* inclusive */ - -/* Convert heap string index to a token (reserved words) */ -#define DUK_STRIDX_TO_TOK(x) ((x) - DUK_STRIDX_START_RESERVED + DUK_TOK_START_RESERVED) - -/* Sanity check */ -#if (DUK_TOK_MAXVAL > 255) -#error DUK_TOK_MAXVAL too large, code assumes it fits into 8 bits -#endif - -/* Sanity checks for string and token defines */ -#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_BREAK) != DUK_TOK_BREAK) -#error mismatch in token defines -#endif -#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_CASE) != DUK_TOK_CASE) -#error mismatch in token defines -#endif -#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_CATCH) != DUK_TOK_CATCH) -#error mismatch in token defines -#endif -#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_CONTINUE) != DUK_TOK_CONTINUE) -#error mismatch in token defines -#endif -#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_DEBUGGER) != DUK_TOK_DEBUGGER) -#error mismatch in token defines -#endif -#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_DEFAULT) != DUK_TOK_DEFAULT) -#error mismatch in token defines -#endif -#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_DELETE) != DUK_TOK_DELETE) -#error mismatch in token defines -#endif -#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_DO) != DUK_TOK_DO) -#error mismatch in token defines -#endif -#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_ELSE) != DUK_TOK_ELSE) -#error mismatch in token defines -#endif -#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_FINALLY) != DUK_TOK_FINALLY) -#error mismatch in token defines -#endif -#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_FOR) != DUK_TOK_FOR) -#error mismatch in token defines -#endif -#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_LC_FUNCTION) != DUK_TOK_FUNCTION) -#error mismatch in token defines -#endif -#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_IF) != DUK_TOK_IF) -#error mismatch in token defines -#endif -#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_IN) != DUK_TOK_IN) -#error mismatch in token defines -#endif -#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_INSTANCEOF) != DUK_TOK_INSTANCEOF) -#error mismatch in token defines -#endif -#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_NEW) != DUK_TOK_NEW) -#error mismatch in token defines -#endif -#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_RETURN) != DUK_TOK_RETURN) -#error mismatch in token defines -#endif -#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_SWITCH) != DUK_TOK_SWITCH) -#error mismatch in token defines -#endif -#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_THIS) != DUK_TOK_THIS) -#error mismatch in token defines -#endif -#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_THROW) != DUK_TOK_THROW) -#error mismatch in token defines -#endif -#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_TRY) != DUK_TOK_TRY) -#error mismatch in token defines -#endif -#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_TYPEOF) != DUK_TOK_TYPEOF) -#error mismatch in token defines -#endif -#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_VAR) != DUK_TOK_VAR) -#error mismatch in token defines -#endif -#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_VOID) != DUK_TOK_VOID) -#error mismatch in token defines -#endif -#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_WHILE) != DUK_TOK_WHILE) -#error mismatch in token defines -#endif -#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_WITH) != DUK_TOK_WITH) -#error mismatch in token defines -#endif -#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_CLASS) != DUK_TOK_CLASS) -#error mismatch in token defines -#endif -#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_CONST) != DUK_TOK_CONST) -#error mismatch in token defines -#endif -#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_ENUM) != DUK_TOK_ENUM) -#error mismatch in token defines -#endif -#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_EXPORT) != DUK_TOK_EXPORT) -#error mismatch in token defines -#endif -#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_EXTENDS) != DUK_TOK_EXTENDS) -#error mismatch in token defines -#endif -#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_IMPORT) != DUK_TOK_IMPORT) -#error mismatch in token defines -#endif -#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_SUPER) != DUK_TOK_SUPER) -#error mismatch in token defines -#endif -#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_LC_NULL) != DUK_TOK_NULL) -#error mismatch in token defines -#endif -#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_TRUE) != DUK_TOK_TRUE) -#error mismatch in token defines -#endif -#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_FALSE) != DUK_TOK_FALSE) -#error mismatch in token defines -#endif -#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_IMPLEMENTS) != DUK_TOK_IMPLEMENTS) -#error mismatch in token defines -#endif -#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_INTERFACE) != DUK_TOK_INTERFACE) -#error mismatch in token defines -#endif -#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_LET) != DUK_TOK_LET) -#error mismatch in token defines -#endif -#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_PACKAGE) != DUK_TOK_PACKAGE) -#error mismatch in token defines -#endif -#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_PRIVATE) != DUK_TOK_PRIVATE) -#error mismatch in token defines -#endif -#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_PROTECTED) != DUK_TOK_PROTECTED) -#error mismatch in token defines -#endif -#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_PUBLIC) != DUK_TOK_PUBLIC) -#error mismatch in token defines -#endif -#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_STATIC) != DUK_TOK_STATIC) -#error mismatch in token defines -#endif -#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_YIELD) != DUK_TOK_YIELD) -#error mismatch in token defines -#endif - -/* Regexp tokens */ -#define DUK_RETOK_EOF 0 -#define DUK_RETOK_DISJUNCTION 1 -#define DUK_RETOK_QUANTIFIER 2 -#define DUK_RETOK_ASSERT_START 3 -#define DUK_RETOK_ASSERT_END 4 -#define DUK_RETOK_ASSERT_WORD_BOUNDARY 5 -#define DUK_RETOK_ASSERT_NOT_WORD_BOUNDARY 6 -#define DUK_RETOK_ASSERT_START_POS_LOOKAHEAD 7 -#define DUK_RETOK_ASSERT_START_NEG_LOOKAHEAD 8 -#define DUK_RETOK_ATOM_PERIOD 9 -#define DUK_RETOK_ATOM_CHAR 10 -#define DUK_RETOK_ATOM_DIGIT 11 -#define DUK_RETOK_ATOM_NOT_DIGIT 12 -#define DUK_RETOK_ATOM_WHITE 13 -#define DUK_RETOK_ATOM_NOT_WHITE 14 -#define DUK_RETOK_ATOM_WORD_CHAR 15 -#define DUK_RETOK_ATOM_NOT_WORD_CHAR 16 -#define DUK_RETOK_ATOM_BACKREFERENCE 17 -#define DUK_RETOK_ATOM_START_CAPTURE_GROUP 18 -#define DUK_RETOK_ATOM_START_NONCAPTURE_GROUP 19 -#define DUK_RETOK_ATOM_START_CHARCLASS 20 -#define DUK_RETOK_ATOM_START_CHARCLASS_INVERTED 21 -#define DUK_RETOK_ATOM_END_GROUP 22 - -/* Constants for duk_lexer_ctx.buf. */ -#define DUK_LEXER_TEMP_BUF_LIMIT 256 - -/* A token value. Can be memcpy()'d, but note that slot1/slot2 values are on the valstack. - * Some fields (like num, str1, str2) are only valid for specific token types and may have - * stale values otherwise. - */ -struct duk_token { - duk_small_int_t t; /* token type (with reserved word identification) */ - duk_small_int_t t_nores; /* token type (with reserved words as DUK_TOK_IDENTIFER) */ - duk_double_t num; /* numeric value of token */ - duk_hstring *str1; /* string 1 of token (borrowed, stored to ctx->slot1_idx) */ - duk_hstring *str2; /* string 2 of token (borrowed, stored to ctx->slot2_idx) */ - duk_size_t start_offset; /* start byte offset of token in lexer input */ - duk_int_t start_line; /* start line of token (first char) */ - duk_int_t num_escapes; /* number of escapes and line continuations (for directive prologue) */ - duk_bool_t lineterm; /* token was preceded by a lineterm */ - duk_bool_t allow_auto_semi; /* token allows automatic semicolon insertion (eof or preceded by newline) */ -}; - -#define DUK_RE_QUANTIFIER_INFINITE ((duk_uint32_t) 0xffffffffUL) - -/* A regexp token value. */ -struct duk_re_token { - duk_small_int_t t; /* token type */ - duk_small_int_t greedy; - duk_uint_fast32_t num; /* numeric value (character, count) */ - duk_uint_fast32_t qmin; - duk_uint_fast32_t qmax; -}; - -/* A structure for 'snapshotting' a point for rewinding */ -struct duk_lexer_point { - duk_size_t offset; - duk_int_t line; -}; - -/* Lexer codepoint with additional info like offset/line number */ -struct duk_lexer_codepoint { - duk_codepoint_t codepoint; - duk_size_t offset; - duk_int_t line; -}; - -/* Lexer context. Same context is used for Ecmascript and Regexp parsing. */ -struct duk_lexer_ctx { -#if defined(DUK_USE_LEXER_SLIDING_WINDOW) - duk_lexer_codepoint *window; /* unicode code points, window[0] is always next, points to 'buffer' */ - duk_lexer_codepoint buffer[DUK_LEXER_BUFFER_SIZE]; -#else - duk_lexer_codepoint window[DUK_LEXER_WINDOW_SIZE]; /* unicode code points, window[0] is always next */ -#endif - - duk_hthread *thr; /* thread; minimizes argument passing */ - - const duk_uint8_t *input; /* input string (may be a user pointer) */ - duk_size_t input_length; /* input byte length */ - duk_size_t input_offset; /* input offset for window leading edge (not window[0]) */ - duk_int_t input_line; /* input linenumber at input_offset (not window[0]), init to 1 */ - - duk_idx_t slot1_idx; /* valstack slot for 1st token value */ - duk_idx_t slot2_idx; /* valstack slot for 2nd token value */ - duk_idx_t buf_idx; /* valstack slot for temp buffer */ - duk_hbuffer_dynamic *buf; /* temp accumulation buffer */ - duk_bufwriter_ctx bw; /* bufwriter for temp accumulation */ - - duk_int_t token_count; /* number of tokens parsed */ - duk_int_t token_limit; /* maximum token count before error (sanity backstop) */ -}; - -/* - * Prototypes - */ - -DUK_INTERNAL_DECL void duk_lexer_initctx(duk_lexer_ctx *lex_ctx); - -DUK_INTERNAL_DECL void duk_lexer_setpoint(duk_lexer_ctx *lex_ctx, duk_lexer_point *pt); - -DUK_INTERNAL_DECL -void duk_lexer_parse_js_input_element(duk_lexer_ctx *lex_ctx, - duk_token *out_token, - duk_bool_t strict_mode, - duk_bool_t regexp_mode); -#ifdef DUK_USE_REGEXP_SUPPORT -DUK_INTERNAL_DECL void duk_lexer_parse_re_token(duk_lexer_ctx *lex_ctx, duk_re_token *out_token); -DUK_INTERNAL_DECL void duk_lexer_parse_re_ranges(duk_lexer_ctx *lex_ctx, duk_re_range_callback gen_range, void *userdata); -#endif /* DUK_USE_REGEXP_SUPPORT */ - -#endif /* DUK_LEXER_H_INCLUDED */ -#line 1 "duk_js_compiler.h" -/* - * Ecmascript compiler. - */ - -#ifndef DUK_JS_COMPILER_H_INCLUDED -#define DUK_JS_COMPILER_H_INCLUDED - -/* ecmascript compiler limits */ -#define DUK_COMPILER_TOKEN_LIMIT 100000000L /* 1e8: protects against deeply nested inner functions */ - -/* maximum loopcount for peephole optimization */ -#define DUK_COMPILER_PEEPHOLE_MAXITER 3 - -/* maximum bytecode length in instructions */ -#define DUK_COMPILER_MAX_BYTECODE_LENGTH (256L * 1024L * 1024L) /* 1 GB */ - -/* - * Compiler intermediate values - * - * Intermediate values describe either plain values (e.g. strings or - * numbers) or binary operations which have not yet been coerced into - * either a left-hand-side or right-hand-side role (e.g. object property). - */ - -#define DUK_IVAL_NONE 0 /* no value */ -#define DUK_IVAL_PLAIN 1 /* register, constant, or value */ -#define DUK_IVAL_ARITH 2 /* binary arithmetic; DUK_OP_ADD, DUK_OP_EQ, other binary ops */ -#define DUK_IVAL_ARITH_EXTRAOP 3 /* binary arithmetic using extraops; DUK_EXTRAOP_INSTOF etc */ -#define DUK_IVAL_PROP 4 /* property access */ -#define DUK_IVAL_VAR 5 /* variable access */ - -#define DUK_ISPEC_NONE 0 /* no value */ -#define DUK_ISPEC_VALUE 1 /* value resides in 'valstack_idx' */ -#define DUK_ISPEC_REGCONST 2 /* value resides in a register or constant */ - -/* bit mask which indicates that a regconst is a constant instead of a register */ -#define DUK_JS_CONST_MARKER 0x80000000UL - -/* type to represent a reg/const reference during compilation */ -typedef duk_uint32_t duk_regconst_t; - -/* type to represent a straight register reference, with <0 indicating none */ -typedef duk_int32_t duk_reg_t; - -typedef struct { - duk_small_uint_t t; /* DUK_ISPEC_XXX */ - duk_regconst_t regconst; - duk_idx_t valstack_idx; /* always set; points to a reserved valstack slot */ -} duk_ispec; - -typedef struct { - /* - * PLAIN: x1 - * ARITH: x1 x2 - * PROP: x1.x2 - * VAR: x1 (name) - */ - - /* XXX: can be optimized for smaller footprint esp. on 32-bit environments */ - duk_small_uint_t t; /* DUK_IVAL_XXX */ - duk_small_uint_t op; /* bytecode opcode (or extraop) for binary ops */ - duk_ispec x1; - duk_ispec x2; -} duk_ivalue; - -/* - * Bytecode instruction representation during compilation - * - * Contains the actual instruction and (optionally) debug info. - */ - -struct duk_compiler_instr { - duk_instr_t ins; -#if defined(DUK_USE_PC2LINE) - duk_uint32_t line; -#endif -}; - -/* - * Compiler state - */ - -#define DUK_LABEL_FLAG_ALLOW_BREAK (1 << 0) -#define DUK_LABEL_FLAG_ALLOW_CONTINUE (1 << 1) - -#define DUK_DECL_TYPE_VAR 0 -#define DUK_DECL_TYPE_FUNC 1 - -/* XXX: optimize to 16 bytes */ -typedef struct { - duk_small_uint_t flags; - duk_int_t label_id; /* numeric label_id (-1 reserved as marker) */ - duk_hstring *h_label; /* borrowed label name */ - duk_int_t catch_depth; /* catch depth at point of definition */ - duk_int_t pc_label; /* pc of label statement: - * pc+1: break jump site - * pc+2: continue jump site - */ - - /* Fast jumps (which avoid longjmp) jump directly to the jump sites - * which are always known even while the iteration/switch statement - * is still being parsed. A final peephole pass "straightens out" - * the jumps. - */ -} duk_labelinfo; - -/* Compiling state of one function, eventually converted to duk_hcompiledfunction */ -struct duk_compiler_func { - /* These pointers are at the start of the struct so that they pack - * nicely. Mixing pointers and integer values is bad on some - * platforms (e.g. if int is 32 bits and pointers are 64 bits). - */ - - duk_bufwriter_ctx bw_code; /* bufwriter for code */ - - duk_hstring *h_name; /* function name (borrowed reference), ends up in _name */ - /* h_code: held in bw_code */ - duk_hobject *h_consts; /* array */ - duk_hobject *h_funcs; /* array of function templates: [func1, offset1, line1, func2, offset2, line2] - * offset/line points to closing brace to allow skipping on pass 2 - */ - duk_hobject *h_decls; /* array of declarations: [ name1, val1, name2, val2, ... ] - * valN = (typeN) | (fnum << 8), where fnum is inner func number (0 for vars) - * record function and variable declarations in pass 1 - */ - duk_hobject *h_labelnames; /* array of active label names */ - duk_hbuffer_dynamic *h_labelinfos; /* C array of duk_labelinfo */ - duk_hobject *h_argnames; /* array of formal argument names (-> _Formals) */ - duk_hobject *h_varmap; /* variable map for pass 2 (identifier -> register number or null (unmapped)) */ - - /* value stack indices for tracking objects */ - /* code_idx: not needed */ - duk_idx_t consts_idx; - duk_idx_t funcs_idx; - duk_idx_t decls_idx; - duk_idx_t labelnames_idx; - duk_idx_t labelinfos_idx; - duk_idx_t argnames_idx; - duk_idx_t varmap_idx; - - /* temp reg handling */ - duk_reg_t temp_first; /* first register that is a temporary (below: variables) */ - duk_reg_t temp_next; /* next temporary register to allocate */ - duk_reg_t temp_max; /* highest value of temp_reg (temp_max - 1 is highest used reg) */ - - /* shuffle registers if large number of regs/consts */ - duk_reg_t shuffle1; - duk_reg_t shuffle2; - duk_reg_t shuffle3; - - /* stats for current expression being parsed */ - duk_int_t nud_count; - duk_int_t led_count; - duk_int_t paren_level; /* parenthesis count, 0 = top level */ - duk_bool_t expr_lhs; /* expression is left-hand-side compatible */ - duk_bool_t allow_in; /* current paren level allows 'in' token */ - - /* misc */ - duk_int_t stmt_next; /* statement id allocation (running counter) */ - duk_int_t label_next; /* label id allocation (running counter) */ - duk_int_t catch_depth; /* catch stack depth */ - duk_int_t with_depth; /* with stack depth (affects identifier lookups) */ - duk_int_t fnum_next; /* inner function numbering */ - duk_int_t num_formals; /* number of formal arguments */ - duk_reg_t reg_stmt_value; /* register for writing value of 'non-empty' statements (global or eval code), -1 is marker */ -#if defined(DUK_USE_DEBUGGER_SUPPORT) - duk_int_t min_line; /* XXX: typing (duk_hcompiledfunction has duk_uint32_t) */ - duk_int_t max_line; -#endif - - /* status booleans */ - duk_bool_t is_function; /* is an actual function (not global/eval code) */ - duk_bool_t is_eval; /* is eval code */ - duk_bool_t is_global; /* is global code */ - duk_bool_t is_setget; /* is a setter/getter */ - duk_bool_t is_decl; /* is a function declaration (as opposed to function expression) */ - duk_bool_t is_strict; /* function is strict */ - duk_bool_t is_notail; /* function must not be tail called */ - duk_bool_t in_directive_prologue; /* parsing in "directive prologue", recognize directives */ - duk_bool_t in_scanning; /* parsing in "scanning" phase (first pass) */ - duk_bool_t may_direct_eval; /* function may call direct eval */ - duk_bool_t id_access_arguments; /* function refers to 'arguments' identifier */ - duk_bool_t id_access_slow; /* function makes one or more slow path accesses */ - duk_bool_t is_arguments_shadowed; /* argument/function declaration shadows 'arguments' */ - duk_bool_t needs_shuffle; /* function needs shuffle registers */ - duk_bool_t reject_regexp_in_adv; /* reject RegExp literal on next advance() call; needed for handling IdentifierName productions */ -}; - -struct duk_compiler_ctx { - duk_hthread *thr; - - /* filename being compiled (ends up in functions' '_filename' property) */ - duk_hstring *h_filename; /* borrowed reference */ - - /* lexing (tokenization) state (contains two valstack slot indices) */ - duk_lexer_ctx lex; - - /* current and previous token for parsing */ - duk_token prev_token; - duk_token curr_token; - duk_idx_t tok11_idx; /* curr_token slot1 (matches 'lex' slot1_idx) */ - duk_idx_t tok12_idx; /* curr_token slot2 (matches 'lex' slot2_idx) */ - duk_idx_t tok21_idx; /* prev_token slot1 */ - duk_idx_t tok22_idx; /* prev_token slot2 */ - - /* recursion limit */ - duk_int_t recursion_depth; - duk_int_t recursion_limit; - - /* code emission temporary */ - duk_int_t emit_jumpslot_pc; - - /* current function being compiled (embedded instead of pointer for more compact access) */ - duk_compiler_func curr_func; -}; - -/* - * Prototypes - */ - -#define DUK_JS_COMPILE_FLAG_EVAL (1 << 0) /* source is eval code (not global) */ -#define DUK_JS_COMPILE_FLAG_STRICT (1 << 1) /* strict outer context */ -#define DUK_JS_COMPILE_FLAG_FUNCEXPR (1 << 2) /* source is a function expression (used for Function constructor) */ - -DUK_INTERNAL_DECL void duk_js_compile(duk_hthread *thr, const duk_uint8_t *src_buffer, duk_size_t src_length, duk_small_uint_t flags); - -#endif /* DUK_JS_COMPILER_H_INCLUDED */ -#line 1 "duk_regexp.h" -/* - * Regular expression structs, constants, and bytecode defines. - */ - -#ifndef DUK_REGEXP_H_INCLUDED -#define DUK_REGEXP_H_INCLUDED - -/* maximum bytecode copies for {n,m} quantifiers */ -#define DUK_RE_MAX_ATOM_COPIES 1000 - -/* regexp compilation limits */ -#define DUK_RE_COMPILE_TOKEN_LIMIT 100000000L /* 1e8 */ - -/* regexp execution limits */ -#define DUK_RE_EXECUTE_STEPS_LIMIT 1000000000L /* 1e9 */ - -/* regexp opcodes */ -#define DUK_REOP_MATCH 1 -#define DUK_REOP_CHAR 2 -#define DUK_REOP_PERIOD 3 -#define DUK_REOP_RANGES 4 -#define DUK_REOP_INVRANGES 5 -#define DUK_REOP_JUMP 6 -#define DUK_REOP_SPLIT1 7 -#define DUK_REOP_SPLIT2 8 -#define DUK_REOP_SQMINIMAL 9 -#define DUK_REOP_SQGREEDY 10 -#define DUK_REOP_SAVE 11 -#define DUK_REOP_WIPERANGE 12 -#define DUK_REOP_LOOKPOS 13 -#define DUK_REOP_LOOKNEG 14 -#define DUK_REOP_BACKREFERENCE 15 -#define DUK_REOP_ASSERT_START 16 -#define DUK_REOP_ASSERT_END 17 -#define DUK_REOP_ASSERT_WORD_BOUNDARY 18 -#define DUK_REOP_ASSERT_NOT_WORD_BOUNDARY 19 - -/* flags */ -#define DUK_RE_FLAG_GLOBAL (1 << 0) -#define DUK_RE_FLAG_IGNORE_CASE (1 << 1) -#define DUK_RE_FLAG_MULTILINE (1 << 2) - -struct duk_re_matcher_ctx { - duk_hthread *thr; - - duk_uint32_t re_flags; - const duk_uint8_t *input; - const duk_uint8_t *input_end; - const duk_uint8_t *bytecode; - const duk_uint8_t *bytecode_end; - const duk_uint8_t **saved; /* allocated from valstack (fixed buffer) */ - duk_uint32_t nsaved; - duk_uint32_t recursion_depth; - duk_uint32_t recursion_limit; - duk_uint32_t steps_count; - duk_uint32_t steps_limit; -}; - -struct duk_re_compiler_ctx { - duk_hthread *thr; - - duk_uint32_t re_flags; - duk_lexer_ctx lex; - duk_re_token curr_token; - duk_bufwriter_ctx bw; - duk_uint32_t captures; /* highest capture number emitted so far (used as: ++captures) */ - duk_uint32_t highest_backref; - duk_uint32_t recursion_depth; - duk_uint32_t recursion_limit; - duk_uint32_t nranges; /* internal temporary value, used for char classes */ -}; - -/* - * Prototypes - */ - -DUK_INTERNAL_DECL void duk_regexp_compile(duk_hthread *thr); -DUK_INTERNAL_DECL void duk_regexp_create_instance(duk_hthread *thr); -DUK_INTERNAL_DECL void duk_regexp_match(duk_hthread *thr); -DUK_INTERNAL_DECL void duk_regexp_match_force_global(duk_hthread *thr); /* hacky helper for String.prototype.split() */ - -#endif /* DUK_REGEXP_H_INCLUDED */ -#line 1 "duk_heaphdr.h" -/* - * Heap header definition and assorted macros, including ref counting. - * Access all fields through the accessor macros. - */ - -#ifndef DUK_HEAPHDR_H_INCLUDED -#define DUK_HEAPHDR_H_INCLUDED - -/* - * Common heap header - * - * All heap objects share the same flags and refcount fields. Objects other - * than strings also need to have a single or double linked list pointers - * for insertion into the "heap allocated" list. Strings are held in the - * heap-wide string table so they don't need link pointers. - * - * Technically, 'h_refcount' must be wide enough to guarantee that it cannot - * wrap (otherwise objects might be freed incorrectly after wrapping). This - * means essentially that the refcount field must be as wide as data pointers. - * On 64-bit platforms this means that the refcount needs to be 64 bits even - * if an 'int' is 32 bits. This is a bit unfortunate, and compromising on - * this might be reasonable in the future. - * - * Heap header size on 32-bit platforms: 8 bytes without reference counting, - * 16 bytes with reference counting. - */ - -struct duk_heaphdr { - duk_uint32_t h_flags; - -#if defined(DUK_USE_REFERENCE_COUNTING) -#if defined(DUK_USE_REFCOUNT16) - duk_uint16_t h_refcount16; -#else - duk_size_t h_refcount; -#endif -#endif - -#if defined(DUK_USE_HEAPPTR16) - duk_uint16_t h_next16; -#else - duk_heaphdr *h_next; -#endif - -#if defined(DUK_USE_DOUBLE_LINKED_HEAP) - /* refcounting requires direct heap frees, which in turn requires a dual linked heap */ -#if defined(DUK_USE_HEAPPTR16) - duk_uint16_t h_prev16; -#else - duk_heaphdr *h_prev; -#endif -#endif - - /* When DUK_USE_HEAPPTR16 (and DUK_USE_REFCOUNT16) is in use, the - * struct won't align nicely to 4 bytes. This 16-bit extra field - * is added to make the alignment clean; the field can be used by - * heap objects when 16-bit packing is used. This field is now - * conditional to DUK_USE_HEAPPTR16 only, but it is intended to be - * used with DUK_USE_REFCOUNT16 and DUK_USE_DOUBLE_LINKED_HEAP; - * this only matter to low memory environments anyway. - */ -#if defined(DUK_USE_HEAPPTR16) - duk_uint16_t h_extra16; -#endif -}; - -struct duk_heaphdr_string { - /* 16 bits would be enough for shared heaphdr flags and duk_hstring - * flags. The initial parts of duk_heaphdr_string and duk_heaphdr - * must match so changing the flags field size here would be quite - * awkward. However, to minimize struct size, we can pack at least - * 16 bits of duk_hstring data into the flags field. - */ - duk_uint32_t h_flags; - -#if defined(DUK_USE_REFERENCE_COUNTING) -#if defined(DUK_USE_REFCOUNT16) - duk_uint16_t h_refcount16; - duk_uint16_t h_strextra16; /* round out to 8 bytes */ -#else - duk_size_t h_refcount; -#endif -#endif -}; - -#define DUK_HEAPHDR_FLAGS_TYPE_MASK 0x00000003UL -#define DUK_HEAPHDR_FLAGS_FLAG_MASK (~DUK_HEAPHDR_FLAGS_TYPE_MASK) - - /* 2 bits for heap type */ -#define DUK_HEAPHDR_FLAGS_HEAP_START 2 /* 5 heap flags */ -#define DUK_HEAPHDR_FLAGS_USER_START 7 /* 25 user flags */ - -#define DUK_HEAPHDR_HEAP_FLAG_NUMBER(n) (DUK_HEAPHDR_FLAGS_HEAP_START + (n)) -#define DUK_HEAPHDR_USER_FLAG_NUMBER(n) (DUK_HEAPHDR_FLAGS_USER_START + (n)) -#define DUK_HEAPHDR_HEAP_FLAG(n) (1UL << (DUK_HEAPHDR_FLAGS_HEAP_START + (n))) -#define DUK_HEAPHDR_USER_FLAG(n) (1UL << (DUK_HEAPHDR_FLAGS_USER_START + (n))) - -#define DUK_HEAPHDR_FLAG_REACHABLE DUK_HEAPHDR_HEAP_FLAG(0) /* mark-and-sweep: reachable */ -#define DUK_HEAPHDR_FLAG_TEMPROOT DUK_HEAPHDR_HEAP_FLAG(1) /* mark-and-sweep: children not processed */ -#define DUK_HEAPHDR_FLAG_FINALIZABLE DUK_HEAPHDR_HEAP_FLAG(2) /* mark-and-sweep: finalizable (on current pass) */ -#define DUK_HEAPHDR_FLAG_FINALIZED DUK_HEAPHDR_HEAP_FLAG(3) /* mark-and-sweep: finalized (on previous pass) */ -#define DUK_HEAPHDR_FLAG_READONLY DUK_HEAPHDR_HEAP_FLAG(4) /* read-only object, in code section */ - -#define DUK_HTYPE_MIN 1 -#define DUK_HTYPE_STRING 1 -#define DUK_HTYPE_OBJECT 2 -#define DUK_HTYPE_BUFFER 3 -#define DUK_HTYPE_MAX 3 - -#if defined(DUK_USE_HEAPPTR16) -#define DUK_HEAPHDR_GET_NEXT(heap,h) \ - ((duk_heaphdr *) DUK_USE_HEAPPTR_DEC16((heap)->heap_udata, (h)->h_next16)) -#define DUK_HEAPHDR_SET_NEXT(heap,h,val) do { \ - (h)->h_next16 = DUK_USE_HEAPPTR_ENC16((heap)->heap_udata, (void *) val); \ - } while (0) -#else -#define DUK_HEAPHDR_GET_NEXT(heap,h) ((h)->h_next) -#define DUK_HEAPHDR_SET_NEXT(heap,h,val) do { \ - (h)->h_next = (val); \ - } while (0) -#endif - -#if defined(DUK_USE_DOUBLE_LINKED_HEAP) -#if defined(DUK_USE_HEAPPTR16) -#define DUK_HEAPHDR_GET_PREV(heap,h) \ - ((duk_heaphdr *) DUK_USE_HEAPPTR_DEC16((heap)->heap_udata, (h)->h_prev16)) -#define DUK_HEAPHDR_SET_PREV(heap,h,val) do { \ - (h)->h_prev16 = DUK_USE_HEAPPTR_ENC16((heap)->heap_udata, (void *) (val)); \ - } while (0) -#else -#define DUK_HEAPHDR_GET_PREV(heap,h) ((h)->h_prev) -#define DUK_HEAPHDR_SET_PREV(heap,h,val) do { \ - (h)->h_prev = (val); \ - } while (0) -#endif -#endif - -#if defined(DUK_USE_REFERENCE_COUNTING) -#if defined(DUK_USE_REFCOUNT16) -#define DUK_HEAPHDR_GET_REFCOUNT(h) ((h)->h_refcount16) -#define DUK_HEAPHDR_SET_REFCOUNT(h,val) do { \ - (h)->h_refcount16 = (val); \ - } while (0) -#define DUK_HEAPHDR_PREINC_REFCOUNT(h) (++(h)->h_refcount16) /* result: updated refcount */ -#define DUK_HEAPHDR_PREDEC_REFCOUNT(h) (--(h)->h_refcount16) /* result: updated refcount */ -#else -#define DUK_HEAPHDR_GET_REFCOUNT(h) ((h)->h_refcount) -#define DUK_HEAPHDR_SET_REFCOUNT(h,val) do { \ - (h)->h_refcount = (val); \ - } while (0) -#define DUK_HEAPHDR_PREINC_REFCOUNT(h) (++(h)->h_refcount) /* result: updated refcount */ -#define DUK_HEAPHDR_PREDEC_REFCOUNT(h) (--(h)->h_refcount) /* result: updated refcount */ -#endif -#else -/* refcount macros not defined without refcounting, caller must #ifdef now */ -#endif /* DUK_USE_REFERENCE_COUNTING */ - -/* - * Note: type is treated as a field separate from flags, so some masking is - * involved in the macros below. - */ - -#define DUK_HEAPHDR_GET_FLAGS_RAW(h) ((h)->h_flags) - -#define DUK_HEAPHDR_GET_FLAGS(h) ((h)->h_flags & DUK_HEAPHDR_FLAGS_FLAG_MASK) -#define DUK_HEAPHDR_SET_FLAGS(h,val) do { \ - (h)->h_flags = ((h)->h_flags & ~(DUK_HEAPHDR_FLAGS_FLAG_MASK)) | (val); \ - } while (0) - -#define DUK_HEAPHDR_GET_TYPE(h) ((h)->h_flags & DUK_HEAPHDR_FLAGS_TYPE_MASK) -#define DUK_HEAPHDR_SET_TYPE(h,val) do { \ - (h)->h_flags = ((h)->h_flags & ~(DUK_HEAPHDR_FLAGS_TYPE_MASK)) | (val); \ - } while (0) - -#define DUK_HEAPHDR_HTYPE_VALID(h) ( \ - DUK_HEAPHDR_GET_TYPE((h)) >= DUK_HTYPE_MIN && \ - DUK_HEAPHDR_GET_TYPE((h)) <= DUK_HTYPE_MAX \ - ) - -#define DUK_HEAPHDR_SET_TYPE_AND_FLAGS(h,tval,fval) do { \ - (h)->h_flags = ((tval) & DUK_HEAPHDR_FLAGS_TYPE_MASK) | \ - ((fval) & DUK_HEAPHDR_FLAGS_FLAG_MASK); \ - } while (0) - -#define DUK_HEAPHDR_SET_FLAG_BITS(h,bits) do { \ - DUK_ASSERT(((bits) & ~(DUK_HEAPHDR_FLAGS_FLAG_MASK)) == 0); \ - (h)->h_flags |= (bits); \ - } while (0) - -#define DUK_HEAPHDR_CLEAR_FLAG_BITS(h,bits) do { \ - DUK_ASSERT(((bits) & ~(DUK_HEAPHDR_FLAGS_FLAG_MASK)) == 0); \ - (h)->h_flags &= ~((bits)); \ - } while (0) - -#define DUK_HEAPHDR_CHECK_FLAG_BITS(h,bits) (((h)->h_flags & (bits)) != 0) - -#define DUK_HEAPHDR_SET_REACHABLE(h) DUK_HEAPHDR_SET_FLAG_BITS((h),DUK_HEAPHDR_FLAG_REACHABLE) -#define DUK_HEAPHDR_CLEAR_REACHABLE(h) DUK_HEAPHDR_CLEAR_FLAG_BITS((h),DUK_HEAPHDR_FLAG_REACHABLE) -#define DUK_HEAPHDR_HAS_REACHABLE(h) DUK_HEAPHDR_CHECK_FLAG_BITS((h),DUK_HEAPHDR_FLAG_REACHABLE) - -#define DUK_HEAPHDR_SET_TEMPROOT(h) DUK_HEAPHDR_SET_FLAG_BITS((h),DUK_HEAPHDR_FLAG_TEMPROOT) -#define DUK_HEAPHDR_CLEAR_TEMPROOT(h) DUK_HEAPHDR_CLEAR_FLAG_BITS((h),DUK_HEAPHDR_FLAG_TEMPROOT) -#define DUK_HEAPHDR_HAS_TEMPROOT(h) DUK_HEAPHDR_CHECK_FLAG_BITS((h),DUK_HEAPHDR_FLAG_TEMPROOT) - -#define DUK_HEAPHDR_SET_FINALIZABLE(h) DUK_HEAPHDR_SET_FLAG_BITS((h),DUK_HEAPHDR_FLAG_FINALIZABLE) -#define DUK_HEAPHDR_CLEAR_FINALIZABLE(h) DUK_HEAPHDR_CLEAR_FLAG_BITS((h),DUK_HEAPHDR_FLAG_FINALIZABLE) -#define DUK_HEAPHDR_HAS_FINALIZABLE(h) DUK_HEAPHDR_CHECK_FLAG_BITS((h),DUK_HEAPHDR_FLAG_FINALIZABLE) - -#define DUK_HEAPHDR_SET_FINALIZED(h) DUK_HEAPHDR_SET_FLAG_BITS((h),DUK_HEAPHDR_FLAG_FINALIZED) -#define DUK_HEAPHDR_CLEAR_FINALIZED(h) DUK_HEAPHDR_CLEAR_FLAG_BITS((h),DUK_HEAPHDR_FLAG_FINALIZED) -#define DUK_HEAPHDR_HAS_FINALIZED(h) DUK_HEAPHDR_CHECK_FLAG_BITS((h),DUK_HEAPHDR_FLAG_FINALIZED) - -#define DUK_HEAPHDR_SET_READONLY(h) DUK_HEAPHDR_SET_FLAG_BITS((h),DUK_HEAPHDR_FLAG_READONLY) -#define DUK_HEAPHDR_CLEAR_READONLY(h) DUK_HEAPHDR_CLEAR_FLAG_BITS((h),DUK_HEAPHDR_FLAG_READONLY) -#define DUK_HEAPHDR_HAS_READONLY(h) DUK_HEAPHDR_CHECK_FLAG_BITS((h),DUK_HEAPHDR_FLAG_READONLY) - -/* get or set a range of flags; m=first bit number, n=number of bits */ -#define DUK_HEAPHDR_GET_FLAG_RANGE(h,m,n) (((h)->h_flags >> (m)) & ((1UL << (n)) - 1UL)) - -#define DUK_HEAPHDR_SET_FLAG_RANGE(h,m,n,v) do { \ - (h)->h_flags = \ - ((h)->h_flags & (~(((1 << (n)) - 1) << (m)))) \ - | ((v) << (m)); \ - } while (0) - -/* init pointer fields to null */ -#if defined(DUK_USE_DOUBLE_LINKED_HEAP) -#define DUK_HEAPHDR_INIT_NULLS(h) do { \ - DUK_HEAPHDR_SET_NEXT((h), (void *) NULL); \ - DUK_HEAPHDR_SET_PREV((h), (void *) NULL); \ - } while (0) -#else -#define DUK_HEAPHDR_INIT_NULLS(h) do { \ - DUK_HEAPHDR_SET_NEXT((h), (void *) NULL); \ - } while (0) -#endif - -#define DUK_HEAPHDR_STRING_INIT_NULLS(h) /* currently nop */ - -/* - * Assert helpers - */ - -/* Check that prev/next links are consistent: if e.g. h->prev is != NULL, - * h->prev->next should point back to h. - */ -#if defined(DUK_USE_DOUBLE_LINKED_HEAP) && defined(DUK_USE_ASSERTIONS) -#define DUK_ASSERT_HEAPHDR_LINKS(heap,h) do { \ - if ((h) != NULL) { \ - duk_heaphdr *h__prev, *h__next; \ - h__prev = DUK_HEAPHDR_GET_PREV((heap), (h)); \ - h__next = DUK_HEAPHDR_GET_NEXT((heap), (h)); \ - DUK_ASSERT(h__prev == NULL || (DUK_HEAPHDR_GET_NEXT((heap), h__prev) == (h))); \ - DUK_ASSERT(h__next == NULL || (DUK_HEAPHDR_GET_PREV((heap), h__next) == (h))); \ - } \ - } while (0) -#else -#define DUK_ASSERT_HEAPHDR_LINKS(heap,h) do {} while (0) -#endif - -/* - * Reference counting helper macros. The macros take a thread argument - * and must thus always be executed in a specific thread context. The - * thread argument is needed for features like finalization. Currently - * it is not required for INCREF, but it is included just in case. - * - * Note that 'raw' macros such as DUK_HEAPHDR_GET_REFCOUNT() are not - * defined without DUK_USE_REFERENCE_COUNTING, so caller must #ifdef - * around them. - */ - -#if defined(DUK_USE_REFERENCE_COUNTING) - -#if defined(DUK_USE_ROM_OBJECTS) -/* With ROM objects "needs refcount update" is true when the value is - * heap allocated and is not a ROM object. - */ -/* XXX: double evaluation for 'tv' argument. */ -#define DUK_TVAL_NEEDS_REFCOUNT_UPDATE(tv) \ - (DUK_TVAL_IS_HEAP_ALLOCATED((tv)) && !DUK_HEAPHDR_HAS_READONLY(DUK_TVAL_GET_HEAPHDR((tv)))) -#define DUK_HEAPHDR_NEEDS_REFCOUNT_UPDATE(h) (!DUK_HEAPHDR_HAS_READONLY((h))) -#else /* DUK_USE_ROM_OBJECTS */ -/* Without ROM objects "needs refcount update" == is heap allocated. */ -#define DUK_TVAL_NEEDS_REFCOUNT_UPDATE(tv) DUK_TVAL_IS_HEAP_ALLOCATED((tv)) -#define DUK_HEAPHDR_NEEDS_REFCOUNT_UPDATE(h) 1 -#endif /* DUK_USE_ROM_OBJECTS */ - -/* Fast variants, inline refcount operations except for refzero handling. - * Can be used explicitly when speed is always more important than size. - * For a good compiler and a single file build, these are basically the - * same as a forced inline. - */ -#define DUK_TVAL_INCREF_FAST(thr,tv) do { \ - duk_tval *duk__tv = (tv); \ - DUK_ASSERT(duk__tv != NULL); \ - if (DUK_TVAL_NEEDS_REFCOUNT_UPDATE(duk__tv)) { \ - duk_heaphdr *duk__h = DUK_TVAL_GET_HEAPHDR(duk__tv); \ - DUK_ASSERT(duk__h != NULL); \ - DUK_ASSERT(DUK_HEAPHDR_HTYPE_VALID(duk__h)); \ - DUK_HEAPHDR_PREINC_REFCOUNT(duk__h); \ - } \ - } while (0) -#define DUK_TVAL_DECREF_FAST(thr,tv) do { \ - duk_tval *duk__tv = (tv); \ - DUK_ASSERT(duk__tv != NULL); \ - if (DUK_TVAL_NEEDS_REFCOUNT_UPDATE(duk__tv)) { \ - duk_heaphdr *duk__h = DUK_TVAL_GET_HEAPHDR(duk__tv); \ - DUK_ASSERT(duk__h != NULL); \ - DUK_ASSERT(DUK_HEAPHDR_HTYPE_VALID(duk__h)); \ - DUK_ASSERT(DUK_HEAPHDR_GET_REFCOUNT(duk__h) > 0); \ - if (DUK_HEAPHDR_PREDEC_REFCOUNT(duk__h) == 0) { \ - duk_heaphdr_refzero((thr), duk__h); \ - } \ - } \ - } while (0) -#define DUK_HEAPHDR_INCREF_FAST(thr,h) do { \ - duk_heaphdr *duk__h = (duk_heaphdr *) (h); \ - DUK_ASSERT(duk__h != NULL); \ - DUK_ASSERT(DUK_HEAPHDR_HTYPE_VALID(duk__h)); \ - if (DUK_HEAPHDR_NEEDS_REFCOUNT_UPDATE(duk__h)) { \ - DUK_HEAPHDR_PREINC_REFCOUNT(duk__h); \ - } \ - } while (0) -#define DUK_HEAPHDR_DECREF_FAST(thr,h) do { \ - duk_heaphdr *duk__h = (duk_heaphdr *) (h); \ - DUK_ASSERT(duk__h != NULL); \ - DUK_ASSERT(DUK_HEAPHDR_HTYPE_VALID(duk__h)); \ - DUK_ASSERT(DUK_HEAPHDR_GET_REFCOUNT(duk__h) > 0); \ - if (DUK_HEAPHDR_NEEDS_REFCOUNT_UPDATE(duk__h)) { \ - if (DUK_HEAPHDR_PREDEC_REFCOUNT(duk__h) == 0) { \ - duk_heaphdr_refzero((thr), duk__h); \ - } \ - } \ - } while (0) - -/* Slow variants, call to a helper to reduce code size. - * Can be used explicitly when size is always more important than speed. - */ -#define DUK_TVAL_INCREF_SLOW(thr,tv) do { \ - duk_tval_incref((tv)); \ - } while (0) -#define DUK_TVAL_DECREF_SLOW(thr,tv) do { \ - duk_tval_decref((thr), (tv)); \ - } while (0) -#define DUK_HEAPHDR_INCREF_SLOW(thr,h) do { \ - duk_heaphdr_incref((duk_heaphdr *) (h)); \ - } while (0) -#define DUK_HEAPHDR_DECREF_SLOW(thr,h) do { \ - duk_heaphdr_decref((thr), (duk_heaphdr *) (h)); \ - } while (0) - -/* Default variants. Selection depends on speed/size preference. - * Concretely: with gcc 4.8.1 -Os x64 the difference in final binary - * is about +1kB for _FAST variants. - */ -#if defined(DUK_USE_FAST_REFCOUNT_DEFAULT) -#define DUK_TVAL_INCREF(thr,tv) DUK_TVAL_INCREF_FAST((thr),(tv)) -#define DUK_TVAL_DECREF(thr,tv) DUK_TVAL_DECREF_FAST((thr),(tv)) -#define DUK_HEAPHDR_INCREF(thr,h) DUK_HEAPHDR_INCREF_FAST((thr),(h)) -#define DUK_HEAPHDR_DECREF(thr,h) DUK_HEAPHDR_DECREF_FAST((thr),(h)) -#else -#define DUK_TVAL_INCREF(thr,tv) DUK_TVAL_INCREF_SLOW((thr),(tv)) -#define DUK_TVAL_DECREF(thr,tv) DUK_TVAL_DECREF_SLOW((thr),(tv)) -#define DUK_HEAPHDR_INCREF(thr,h) DUK_HEAPHDR_INCREF_SLOW((thr),(h)) -#define DUK_HEAPHDR_DECREF(thr,h) DUK_HEAPHDR_DECREF_SLOW((thr),(h)) -#endif - -/* Casting convenience. */ -#define DUK_HSTRING_INCREF(thr,h) DUK_HEAPHDR_INCREF((thr),(duk_heaphdr *) (h)) -#define DUK_HSTRING_DECREF(thr,h) DUK_HEAPHDR_DECREF((thr),(duk_heaphdr *) (h)) -#define DUK_HOBJECT_INCREF(thr,h) DUK_HEAPHDR_INCREF((thr),(duk_heaphdr *) (h)) -#define DUK_HOBJECT_DECREF(thr,h) DUK_HEAPHDR_DECREF((thr),(duk_heaphdr *) (h)) -#define DUK_HBUFFER_INCREF(thr,h) DUK_HEAPHDR_INCREF((thr),(duk_heaphdr *) (h)) -#define DUK_HBUFFER_DECREF(thr,h) DUK_HEAPHDR_DECREF((thr),(duk_heaphdr *) (h)) -#define DUK_HCOMPILEDFUNCTION_INCREF(thr,h) DUK_HEAPHDR_INCREF((thr),(duk_heaphdr *) &(h)->obj) -#define DUK_HCOMPILEDFUNCTION_DECREF(thr,h) DUK_HEAPHDR_DECREF((thr),(duk_heaphdr *) &(h)->obj) -#define DUK_HNATIVEFUNCTION_INCREF(thr,h) DUK_HEAPHDR_INCREF((thr),(duk_heaphdr *) &(h)->obj) -#define DUK_HNATIVEFUNCTION_DECREF(thr,h) DUK_HEAPHDR_DECREF((thr),(duk_heaphdr *) &(h)->obj) -#define DUK_HBUFFEROBJECT_INCREF(thr,h) DUK_HEAPHDR_INCREF((thr),(duk_heaphdr *) &(h)->obj) -#define DUK_HBUFFEROBJECT_DECREF(thr,h) DUK_HEAPHDR_DECREF((thr),(duk_heaphdr *) &(h)->obj) -#define DUK_HTHREAD_INCREF(thr,h) DUK_HEAPHDR_INCREF((thr),(duk_heaphdr *) &(h)->obj) -#define DUK_HTHREAD_DECREF(thr,h) DUK_HEAPHDR_DECREF((thr),(duk_heaphdr *) &(h)->obj) - -/* Convenience for some situations; the above macros don't allow NULLs - * for performance reasons. - */ -#define DUK_HOBJECT_INCREF_ALLOWNULL(thr,h) do { \ - if ((h) != NULL) { \ - DUK_HEAPHDR_INCREF((thr), (duk_heaphdr *) (h)); \ - } \ - } while (0) -#define DUK_HOBJECT_DECREF_ALLOWNULL(thr,h) do { \ - if ((h) != NULL) { \ - DUK_HEAPHDR_DECREF((thr), (duk_heaphdr *) (h)); \ - } \ - } while (0) - -/* - * Macros to set a duk_tval and update refcount of the target (decref the - * old value and incref the new value if necessary). This is both performance - * and footprint critical; any changes made should be measured for size/speed. - */ - -#define DUK_TVAL_SET_UNDEFINED_UPDREF_ALT0(thr,tvptr_dst) do { \ - duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \ - DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \ - DUK_TVAL_SET_UNDEFINED(tv__dst); \ - DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \ - } while (0) - -#define DUK_TVAL_SET_UNUSED_UPDREF_ALT0(thr,tvptr_dst) do { \ - duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \ - DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \ - DUK_TVAL_SET_UNUSED(tv__dst); \ - DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \ - } while (0) - -#define DUK_TVAL_SET_NULL_UPDREF_ALT0(thr,tvptr_dst) do { \ - duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \ - DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \ - DUK_TVAL_SET_NULL(tv__dst); \ - DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \ - } while (0) - -#define DUK_TVAL_SET_BOOLEAN_UPDREF_ALT0(thr,tvptr_dst,newval) do { \ - duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \ - DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \ - DUK_TVAL_SET_BOOLEAN(tv__dst, (newval)); \ - DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \ - } while (0) - -#define DUK_TVAL_SET_NUMBER_UPDREF_ALT0(thr,tvptr_dst,newval) do { \ - duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \ - DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \ - DUK_TVAL_SET_NUMBER(tv__dst, (newval)); \ - DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \ - } while (0) -#define DUK_TVAL_SET_NUMBER_CHKFAST_UPDREF_ALT0(thr,tvptr_dst,newval) do { \ - duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \ - DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \ - DUK_TVAL_SET_NUMBER_CHKFAST(tv__dst, (newval)); \ - DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \ - } while (0) -#define DUK_TVAL_SET_DOUBLE_UPDREF_ALT0(thr,tvptr_dst,newval) do { \ - duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \ - DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \ - DUK_TVAL_SET_DOUBLE(tv__dst, (newval)); \ - DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \ - } while (0) -#define DUK_TVAL_SET_NAN_UPDREF_ALT0(thr,tvptr_dst) do { \ - duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \ - DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \ - DUK_TVAL_SET_NAN(tv__dst); \ - DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \ - } while (0) -#if defined(DUK_USE_FASTINT) -#define DUK_TVAL_SET_FASTINT_UPDREF_ALT0(thr,tvptr_dst,newval) do { \ - duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \ - DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \ - DUK_TVAL_SET_FASTINT(tv__dst, (newval)); \ - DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \ - } while (0) -#define DUK_TVAL_SET_FASTINT_I32_UPDREF_ALT0(thr,tvptr_dst,newval) do { \ - duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \ - DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \ - DUK_TVAL_SET_FASTINT_I32(tv__dst, (newval)); \ - DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \ - } while (0) -#define DUK_TVAL_SET_FASTINT_U32_UPDREF_ALT0(thr,tvptr_dst,newval) do { \ - duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \ - DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \ - DUK_TVAL_SET_FASTINT_U32(tv__dst, (newval)); \ - DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \ - } while (0) -#else -#define DUK_TVAL_SET_DOUBLE_CAST_UPDREF(thr,tvptr_dst,newval) \ - DUK_TVAL_SET_DOUBLE_UPDREF((thr), (tvptr_dst), (duk_double_t) (newval)) -#endif /* DUK_USE_FASTINT */ - -#define DUK_TVAL_SET_LIGHTFUNC_UPDREF_ALT0(thr,tvptr_dst,lf_v,lf_fp,lf_flags) do { \ - duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \ - DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \ - DUK_TVAL_SET_LIGHTFUNC(tv__dst, (lf_v), (lf_fp), (lf_flags)); \ - DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \ - } while (0) - -#define DUK_TVAL_SET_STRING_UPDREF_ALT0(thr,tvptr_dst,newval) do { \ - duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \ - DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \ - DUK_TVAL_SET_STRING(tv__dst, (newval)); \ - DUK_HSTRING_INCREF((thr), (newval)); \ - DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \ - } while (0) - -#define DUK_TVAL_SET_OBJECT_UPDREF_ALT0(thr,tvptr_dst,newval) do { \ - duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \ - DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \ - DUK_TVAL_SET_OBJECT(tv__dst, (newval)); \ - DUK_HOBJECT_INCREF((thr), (newval)); \ - DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \ - } while (0) - -#define DUK_TVAL_SET_BUFFER_UPDREF_ALT0(thr,tvptr_dst,newval) do { \ - duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \ - DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \ - DUK_TVAL_SET_BUFFER(tv__dst, (newval)); \ - DUK_HBUFFER_INCREF((thr), (newval)); \ - DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \ - } while (0) - -#define DUK_TVAL_SET_POINTER_UPDREF_ALT0(thr,tvptr_dst,newval) do { \ - duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \ - DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \ - DUK_TVAL_SET_POINTER(tv__dst, (newval)); \ - DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \ - } while (0) - -/* DUK_TVAL_SET_TVAL_UPDREF() is used a lot in executor, property lookups, - * etc, so it's very important for performance. Measure when changing. - * - * NOTE: the source and destination duk_tval pointers may be the same, and - * the macros MUST deal with that correctly. - */ - -/* Original idiom used, minimal code size. */ -#define DUK_TVAL_SET_TVAL_UPDREF_ALT0(thr,tvptr_dst,tvptr_src) do { \ - duk_tval *tv__dst, *tv__src; duk_tval tv__tmp; \ - tv__dst = (tvptr_dst); tv__src = (tvptr_src); \ - DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \ - DUK_TVAL_SET_TVAL(tv__dst, tv__src); \ - DUK_TVAL_INCREF((thr), tv__src); \ - DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \ - } while (0) - -/* Faster alternative: avoid making a temporary copy of tvptr_dst and use - * fast incref/decref macros. - */ -#define DUK_TVAL_SET_TVAL_UPDREF_ALT1(thr,tvptr_dst,tvptr_src) do { \ - duk_tval *tv__dst, *tv__src; duk_heaphdr *h__obj; \ - tv__dst = (tvptr_dst); tv__src = (tvptr_src); \ - DUK_TVAL_INCREF_FAST((thr), tv__src); \ - if (DUK_TVAL_NEEDS_REFCOUNT_UPDATE(tv__dst)) { \ - h__obj = DUK_TVAL_GET_HEAPHDR(tv__dst); \ - DUK_ASSERT(h__obj != NULL); \ - DUK_TVAL_SET_TVAL(tv__dst, tv__src); \ - DUK_HEAPHDR_DECREF_FAST((thr), h__obj); /* side effects */ \ - } else { \ - DUK_TVAL_SET_TVAL(tv__dst, tv__src); \ - } \ - } while (0) - -/* XXX: no optimized variants yet */ -#define DUK_TVAL_SET_UNDEFINED_UPDREF DUK_TVAL_SET_UNDEFINED_UPDREF_ALT0 -#define DUK_TVAL_SET_UNUSED_UPDREF DUK_TVAL_SET_UNUSED_UPDREF_ALT0 -#define DUK_TVAL_SET_NULL_UPDREF DUK_TVAL_SET_NULL_UPDREF_ALT0 -#define DUK_TVAL_SET_BOOLEAN_UPDREF DUK_TVAL_SET_BOOLEAN_UPDREF_ALT0 -#define DUK_TVAL_SET_NUMBER_UPDREF DUK_TVAL_SET_NUMBER_UPDREF_ALT0 -#define DUK_TVAL_SET_NUMBER_CHKFAST_UPDREF DUK_TVAL_SET_NUMBER_CHKFAST_UPDREF_ALT0 -#define DUK_TVAL_SET_DOUBLE_UPDREF DUK_TVAL_SET_DOUBLE_UPDREF_ALT0 -#define DUK_TVAL_SET_NAN_UPDREF DUK_TVAL_SET_NAN_UPDREF_ALT0 -#if defined(DUK_USE_FASTINT) -#define DUK_TVAL_SET_FASTINT_UPDREF DUK_TVAL_SET_FASTINT_UPDREF_ALT0 -#define DUK_TVAL_SET_FASTINT_I32_UPDREF DUK_TVAL_SET_FASTINT_I32_UPDREF_ALT0 -#define DUK_TVAL_SET_FASTINT_U32_UPDREF DUK_TVAL_SET_FASTINT_U32_UPDREF_ALT0 -#else -#define DUK_TVAL_SET_FASTINT_UPDREF DUK_TVAL_SET_DOUBLE_CAST_UPDREF /* XXX: fast int-to-double */ -#define DUK_TVAL_SET_FASTINT_I32_UPDREF DUK_TVAL_SET_DOUBLE_CAST_UPDREF -#define DUK_TVAL_SET_FASTINT_U32_UPDREF DUK_TVAL_SET_DOUBLE_CAST_UPDREF -#endif /* DUK_USE_FASTINT */ -#define DUK_TVAL_SET_LIGHTFUNC_UPDREF DUK_TVAL_SET_LIGHTFUNC_UPDREF_ALT0 -#define DUK_TVAL_SET_STRING_UPDREF DUK_TVAL_SET_STRING_UPDREF_ALT0 -#define DUK_TVAL_SET_OBJECT_UPDREF DUK_TVAL_SET_OBJECT_UPDREF_ALT0 -#define DUK_TVAL_SET_BUFFER_UPDREF DUK_TVAL_SET_BUFFER_UPDREF_ALT0 -#define DUK_TVAL_SET_POINTER_UPDREF DUK_TVAL_SET_POINTER_UPDREF_ALT0 - -#if defined(DUK_USE_FAST_REFCOUNT_DEFAULT) -/* Optimized for speed. */ -#define DUK_TVAL_SET_TVAL_UPDREF DUK_TVAL_SET_TVAL_UPDREF_ALT1 -#define DUK_TVAL_SET_TVAL_UPDREF_FAST DUK_TVAL_SET_TVAL_UPDREF_ALT1 -#define DUK_TVAL_SET_TVAL_UPDREF_SLOW DUK_TVAL_SET_TVAL_UPDREF_ALT0 -#else -/* Optimized for size. */ -#define DUK_TVAL_SET_TVAL_UPDREF DUK_TVAL_SET_TVAL_UPDREF_ALT0 -#define DUK_TVAL_SET_TVAL_UPDREF_FAST DUK_TVAL_SET_TVAL_UPDREF_ALT0 -#define DUK_TVAL_SET_TVAL_UPDREF_SLOW DUK_TVAL_SET_TVAL_UPDREF_ALT0 -#endif - -#else /* DUK_USE_REFERENCE_COUNTING */ - -#define DUK_TVAL_INCREF_FAST(thr,v) do {} while (0) /* nop */ -#define DUK_TVAL_DECREF_FAST(thr,v) do {} while (0) /* nop */ -#define DUK_TVAL_INCREF_SLOW(thr,v) do {} while (0) /* nop */ -#define DUK_TVAL_DECREF_SLOW(thr,v) do {} while (0) /* nop */ -#define DUK_TVAL_INCREF(thr,v) do {} while (0) /* nop */ -#define DUK_TVAL_DECREF(thr,v) do {} while (0) /* nop */ -#define DUK_HEAPHDR_INCREF_FAST(thr,h) do {} while (0) /* nop */ -#define DUK_HEAPHDR_DECREF_FAST(thr,h) do {} while (0) /* nop */ -#define DUK_HEAPHDR_INCREF_SLOW(thr,h) do {} while (0) /* nop */ -#define DUK_HEAPHDR_DECREF_SLOW(thr,h) do {} while (0) /* nop */ -#define DUK_HEAPHDR_INCREF(thr,h) do {} while (0) /* nop */ -#define DUK_HEAPHDR_DECREF(thr,h) do {} while (0) /* nop */ -#define DUK_HSTRING_INCREF(thr,h) do {} while (0) /* nop */ -#define DUK_HSTRING_DECREF(thr,h) do {} while (0) /* nop */ -#define DUK_HOBJECT_INCREF(thr,h) do {} while (0) /* nop */ -#define DUK_HOBJECT_DECREF(thr,h) do {} while (0) /* nop */ -#define DUK_HBUFFER_INCREF(thr,h) do {} while (0) /* nop */ -#define DUK_HBUFFER_DECREF(thr,h) do {} while (0) /* nop */ -#define DUK_HCOMPILEDFUNCTION_INCREF(thr,h) do {} while (0) /* nop */ -#define DUK_HCOMPILEDFUNCTION_DECREF(thr,h) do {} while (0) /* nop */ -#define DUK_HNATIVEFUNCTION_INCREF(thr,h) do {} while (0) /* nop */ -#define DUK_HNATIVEFUNCTION_DECREF(thr,h) do {} while (0) /* nop */ -#define DUK_HBUFFEROBJECT_INCREF(thr,h) do {} while (0) /* nop */ -#define DUK_HBUFFEROBJECT_DECREF(thr,h) do {} while (0) /* nop */ -#define DUK_HTHREAD_INCREF(thr,h) do {} while (0) /* nop */ -#define DUK_HTHREAD_DECREF(thr,h) do {} while (0) /* nop */ -#define DUK_HOBJECT_INCREF_ALLOWNULL(thr,h) do {} while (0) /* nop */ -#define DUK_HOBJECT_DECREF_ALLOWNULL(thr,h) do {} while (0) /* nop */ - -#define DUK_TVAL_SET_UNDEFINED_UPDREF_ALT0(thr,tvptr_dst) do { \ - duk_tval *tv__dst; tv__dst = (tvptr_dst); \ - DUK_TVAL_SET_UNDEFINED(tv__dst); \ - DUK_UNREF((thr)); \ - } while (0) - -#define DUK_TVAL_SET_UNUSED_UPDREF_ALT0(thr,tvptr_dst) do { \ - duk_tval *tv__dst; tv__dst = (tvptr_dst); \ - DUK_TVAL_SET_UNUSED(tv__dst); \ - DUK_UNREF((thr)); \ - } while (0) - -#define DUK_TVAL_SET_NULL_UPDREF_ALT0(thr,tvptr_dst) do { \ - duk_tval *tv__dst; tv__dst = (tvptr_dst); \ - DUK_TVAL_SET_NULL(tv__dst); \ - DUK_UNREF((thr)); \ - } while (0) - -#define DUK_TVAL_SET_BOOLEAN_UPDREF_ALT0(thr,tvptr_dst,newval) do { \ - duk_tval *tv__dst; tv__dst = (tvptr_dst); \ - DUK_TVAL_SET_BOOLEAN(tv__dst, (newval)); \ - DUK_UNREF((thr)); \ - } while (0) - -#define DUK_TVAL_SET_NUMBER_UPDREF_ALT0(thr,tvptr_dst,newval) do { \ - duk_tval *tv__dst; tv__dst = (tvptr_dst); \ - DUK_TVAL_SET_NUMBER(tv__dst, (newval)); \ - DUK_UNREF((thr)); \ - } while (0) -#define DUK_TVAL_SET_NUMBER_CHKFAST_UPDREF_ALT0(thr,tvptr_dst,newval) do { \ - duk_tval *tv__dst; tv__dst = (tvptr_dst); \ - DUK_TVAL_SET_NUMBER_CHKFAST(tv__dst, (newval)); \ - DUK_UNREF((thr)); \ - } while (0) -#define DUK_TVAL_SET_DOUBLE_UPDREF_ALT0(thr,tvptr_dst,newval) do { \ - duk_tval *tv__dst; tv__dst = (tvptr_dst); \ - DUK_TVAL_SET_DOUBLE(tv__dst, (newval)); \ - DUK_UNREF((thr)); \ - } while (0) -#define DUK_TVAL_SET_NAN_UPDREF_ALT0(thr,tvptr_dst) do { \ - duk_tval *tv__dst; tv__dst = (tvptr_dst); \ - DUK_TVAL_SET_NAN(tv__dst); \ - DUK_UNREF((thr)); \ - } while (0) -#if defined(DUK_USE_FASTINT) -#define DUK_TVAL_SET_FASTINT_UPDREF_ALT0(thr,tvptr_dst,newval) do { \ - duk_tval *tv__dst; tv__dst = (tvptr_dst); \ - DUK_TVAL_SET_FASTINT(tv__dst, (newval)); \ - DUK_UNREF((thr)); \ - } while (0) -#define DUK_TVAL_SET_FASTINT_I32_UPDREF_ALT0(thr,tvptr_dst,newval) do { \ - duk_tval *tv__dst; tv__dst = (tvptr_dst); \ - DUK_TVAL_SET_FASTINT_I32(tv__dst, (newval)); \ - DUK_UNREF((thr)); \ - } while (0) -#define DUK_TVAL_SET_FASTINT_U32_UPDREF_ALT0(thr,tvptr_dst,newval) do { \ - duk_tval *tv__dst; tv__dst = (tvptr_dst); \ - DUK_TVAL_SET_FASTINT_U32(tv__dst, (newval)); \ - DUK_UNREF((thr)); \ - } while (0) -#else -#define DUK_TVAL_SET_DOUBLE_CAST_UPDREF(thr,tvptr_dst,newval) \ - DUK_TVAL_SET_DOUBLE_UPDREF((thr), (tvptr_dst), (duk_double_t) (newval)) -#endif /* DUK_USE_FASTINT */ - -#define DUK_TVAL_SET_LIGHTFUNC_UPDREF_ALT0(thr,tvptr_dst,lf_v,lf_fp,lf_flags) do { \ - duk_tval *tv__dst; tv__dst = (tvptr_dst); \ - DUK_TVAL_SET_LIGHTFUNC(tv__dst, (lf_v), (lf_fp), (lf_flags)); \ - DUK_UNREF((thr)); \ - } while (0) - -#define DUK_TVAL_SET_STRING_UPDREF_ALT0(thr,tvptr_dst,newval) do { \ - duk_tval *tv__dst; tv__dst = (tvptr_dst); \ - DUK_TVAL_SET_STRING(tv__dst, (newval)); \ - DUK_UNREF((thr)); \ - } while (0) - -#define DUK_TVAL_SET_OBJECT_UPDREF_ALT0(thr,tvptr_dst,newval) do { \ - duk_tval *tv__dst; tv__dst = (tvptr_dst); \ - DUK_TVAL_SET_OBJECT(tv__dst, (newval)); \ - DUK_UNREF((thr)); \ - } while (0) - -#define DUK_TVAL_SET_BUFFER_UPDREF_ALT0(thr,tvptr_dst,newval) do { \ - duk_tval *tv__dst; tv__dst = (tvptr_dst); \ - DUK_TVAL_SET_BUFFER(tv__dst, (newval)); \ - DUK_UNREF((thr)); \ - } while (0) - -#define DUK_TVAL_SET_POINTER_UPDREF_ALT0(thr,tvptr_dst,newval) do { \ - duk_tval *tv__dst; tv__dst = (tvptr_dst); \ - DUK_TVAL_SET_POINTER(tv__dst, (newval)); \ - DUK_UNREF((thr)); \ - } while (0) - -#define DUK_TVAL_SET_TVAL_UPDREF_ALT0(thr,tvptr_dst,tvptr_src) do { \ - duk_tval *tv__dst, *tv__src; \ - tv__dst = (tvptr_dst); tv__src = (tvptr_src); \ - DUK_TVAL_SET_TVAL(tv__dst, tv__src); \ - DUK_UNREF((thr)); \ - } while (0) - -#define DUK_TVAL_SET_UNDEFINED_UPDREF DUK_TVAL_SET_UNDEFINED_UPDREF_ALT0 -#define DUK_TVAL_SET_UNUSED_UPDREF DUK_TVAL_SET_UNUSED_UPDREF_ALT0 -#define DUK_TVAL_SET_NULL_UPDREF DUK_TVAL_SET_NULL_UPDREF_ALT0 -#define DUK_TVAL_SET_BOOLEAN_UPDREF DUK_TVAL_SET_BOOLEAN_UPDREF_ALT0 -#define DUK_TVAL_SET_NUMBER_UPDREF DUK_TVAL_SET_NUMBER_UPDREF_ALT0 -#define DUK_TVAL_SET_NUMBER_CHKFAST_UPDREF DUK_TVAL_SET_NUMBER_CHKFAST_UPDREF_ALT0 -#define DUK_TVAL_SET_DOUBLE_UPDREF DUK_TVAL_SET_DOUBLE_UPDREF_ALT0 -#define DUK_TVAL_SET_NAN_UPDREF DUK_TVAL_SET_NAN_UPDREF_ALT0 -#if defined(DUK_USE_FASTINT) -#define DUK_TVAL_SET_FASTINT_UPDREF DUK_TVAL_SET_FASTINT_UPDREF_ALT0 -#define DUK_TVAL_SET_FASTINT_I32_UPDREF DUK_TVAL_SET_FASTINT_I32_UPDREF_ALT0 -#define DUK_TVAL_SET_FASTINT_U32_UPDREF DUK_TVAL_SET_FASTINT_U32_UPDREF_ALT0 -#else -#define DUK_TVAL_SET_FASTINT_UPDREF DUK_TVAL_SET_DOUBLE_CAST_UPDREF /* XXX: fast-int-to-double */ -#define DUK_TVAL_SET_FASTINT_I32_UPDREF DUK_TVAL_SET_DOUBLE_CAST_UPDREF -#define DUK_TVAL_SET_FASTINT_U32_UPDREF DUK_TVAL_SET_DOUBLE_CAST_UPDREF -#endif /* DUK_USE_FASTINT */ -#define DUK_TVAL_SET_LIGHTFUNC_UPDREF DUK_TVAL_SET_LIGHTFUNC_UPDREF_ALT0 -#define DUK_TVAL_SET_STRING_UPDREF DUK_TVAL_SET_STRING_UPDREF_ALT0 -#define DUK_TVAL_SET_OBJECT_UPDREF DUK_TVAL_SET_OBJECT_UPDREF_ALT0 -#define DUK_TVAL_SET_BUFFER_UPDREF DUK_TVAL_SET_BUFFER_UPDREF_ALT0 -#define DUK_TVAL_SET_POINTER_UPDREF DUK_TVAL_SET_POINTER_UPDREF_ALT0 - -#define DUK_TVAL_SET_TVAL_UPDREF DUK_TVAL_SET_TVAL_UPDREF_ALT0 -#define DUK_TVAL_SET_TVAL_UPDREF_FAST DUK_TVAL_SET_TVAL_UPDREF_ALT0 -#define DUK_TVAL_SET_TVAL_UPDREF_SLOW DUK_TVAL_SET_TVAL_UPDREF_ALT0 - -#endif /* DUK_USE_REFERENCE_COUNTING */ - -#endif /* DUK_HEAPHDR_H_INCLUDED */ -#line 1 "duk_api_internal.h" -/* - * Internal API calls which have (stack and other) semantics similar - * to the public API. - */ - -#ifndef DUK_API_INTERNAL_H_INCLUDED -#define DUK_API_INTERNAL_H_INCLUDED - -/* duk_push_sprintf constants */ -#define DUK_PUSH_SPRINTF_INITIAL_SIZE 256L -#define DUK_PUSH_SPRINTF_SANITY_LIMIT (1L * 1024L * 1024L * 1024L) - -/* Flag ORed to err_code to indicate __FILE__ / __LINE__ is not - * blamed as source of error for error fileName / lineNumber. - */ -#define DUK_ERRCODE_FLAG_NOBLAME_FILELINE (1L << 24) - -/* Valstack resize flags */ -#define DUK_VSRESIZE_FLAG_SHRINK (1 << 0) -#define DUK_VSRESIZE_FLAG_COMPACT (1 << 1) -#define DUK_VSRESIZE_FLAG_THROW (1 << 2) - -/* Current convention is to use duk_size_t for value stack sizes and global indices, - * and duk_idx_t for local frame indices. - */ -DUK_INTERNAL_DECL -duk_bool_t duk_valstack_resize_raw(duk_context *ctx, - duk_size_t min_new_size, - duk_small_uint_t flags); - -#if defined(DUK_USE_VERBOSE_ERRORS) && defined(DUK_USE_PARANOID_ERRORS) -DUK_INTERNAL_DECL const char *duk_get_type_name(duk_context *ctx, duk_idx_t index); -#endif - -DUK_INTERNAL_DECL duk_tval *duk_get_tval(duk_context *ctx, duk_idx_t index); -DUK_INTERNAL_DECL duk_tval *duk_require_tval(duk_context *ctx, duk_idx_t index); -DUK_INTERNAL_DECL void duk_push_tval(duk_context *ctx, duk_tval *tv); - -/* Push the current 'this' binding; throw TypeError if binding is not object - * coercible (CheckObjectCoercible). - */ -DUK_INTERNAL_DECL void duk_push_this_check_object_coercible(duk_context *ctx); - -/* duk_push_this() + CheckObjectCoercible() + duk_to_object() */ -DUK_INTERNAL_DECL duk_hobject *duk_push_this_coercible_to_object(duk_context *ctx); - -/* duk_push_this() + CheckObjectCoercible() + duk_to_string() */ -DUK_INTERNAL_DECL duk_hstring *duk_push_this_coercible_to_string(duk_context *ctx); - -/* Get a borrowed duk_tval pointer to the current 'this' binding. Caller must - * make sure there's an active callstack entry. Note that the returned pointer - * is unstable with regards to side effects. - */ -DUK_INTERNAL_DECL duk_tval *duk_get_borrowed_this_tval(duk_context *ctx); - -/* XXX: add fastint support? */ -#define duk_push_u64(ctx,val) \ - duk_push_number((ctx), (duk_double_t) (val)) -#define duk_push_i64(ctx,val) \ - duk_push_number((ctx), (duk_double_t) (val)) - -/* duk_push_(u)int() is guaranteed to support at least (un)signed 32-bit range */ -#define duk_push_u32(ctx,val) \ - duk_push_uint((ctx), (duk_uint_t) (val)) -#define duk_push_i32(ctx,val) \ - duk_push_int((ctx), (duk_int_t) (val)) - -/* sometimes stack and array indices need to go on the stack */ -#define duk_push_idx(ctx,val) \ - duk_push_int((ctx), (duk_int_t) (val)) -#define duk_push_uarridx(ctx,val) \ - duk_push_uint((ctx), (duk_uint_t) (val)) -#define duk_push_size_t(ctx,val) \ - duk_push_uint((ctx), (duk_uint_t) (val)) /* XXX: assumed to fit for now */ - -DUK_INTERNAL_DECL duk_hstring *duk_get_hstring(duk_context *ctx, duk_idx_t index); -DUK_INTERNAL_DECL duk_hobject *duk_get_hobject(duk_context *ctx, duk_idx_t index); -DUK_INTERNAL_DECL duk_hbuffer *duk_get_hbuffer(duk_context *ctx, duk_idx_t index); -DUK_INTERNAL_DECL duk_hthread *duk_get_hthread(duk_context *ctx, duk_idx_t index); -DUK_INTERNAL_DECL duk_hcompiledfunction *duk_get_hcompiledfunction(duk_context *ctx, duk_idx_t index); -DUK_INTERNAL_DECL duk_hnativefunction *duk_get_hnativefunction(duk_context *ctx, duk_idx_t index); - -DUK_INTERNAL_DECL duk_hobject *duk_get_hobject_with_class(duk_context *ctx, duk_idx_t index, duk_small_uint_t classnum); - -#if 0 /* This would be pointless: unexpected type and lightfunc would both return NULL */ -DUK_INTERNAL_DECL duk_hobject *duk_get_hobject_or_lfunc(duk_context *ctx, duk_idx_t index); -#endif -DUK_INTERNAL_DECL duk_hobject *duk_get_hobject_or_lfunc_coerce(duk_context *ctx, duk_idx_t index); - -#if 0 /*unused*/ -DUK_INTERNAL_DECL void *duk_get_voidptr(duk_context *ctx, duk_idx_t index); -#endif - -DUK_INTERNAL_DECL duk_hstring *duk_to_hstring(duk_context *ctx, duk_idx_t index); -#if defined(DUK_USE_DEBUGGER_SUPPORT) /* only needed by debugger for now */ -DUK_INTERNAL_DECL duk_hstring *duk_safe_to_hstring(duk_context *ctx, duk_idx_t index); -#endif -DUK_INTERNAL_DECL void duk_to_object_class_string_top(duk_context *ctx); -#if !defined(DUK_USE_PARANOID_ERRORS) -DUK_INTERNAL_DECL void duk_push_hobject_class_string(duk_context *ctx, duk_hobject *h); -#endif - -DUK_INTERNAL_DECL 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); /* out_clamped=NULL, RangeError if outside range */ -DUK_INTERNAL_DECL duk_int_t duk_to_int_clamped(duk_context *ctx, duk_idx_t index, duk_int_t minval, duk_int_t maxval); -DUK_INTERNAL_DECL duk_int_t duk_to_int_check_range(duk_context *ctx, duk_idx_t index, duk_int_t minval, duk_int_t maxval); -#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) -DUK_INTERNAL_DECL duk_uint8_t duk_to_uint8clamped(duk_context *ctx, duk_idx_t index); -#endif - -DUK_INTERNAL_DECL duk_hstring *duk_require_hstring(duk_context *ctx, duk_idx_t index); -DUK_INTERNAL_DECL duk_hobject *duk_require_hobject(duk_context *ctx, duk_idx_t index); -DUK_INTERNAL_DECL duk_hbuffer *duk_require_hbuffer(duk_context *ctx, duk_idx_t index); -DUK_INTERNAL_DECL duk_hthread *duk_require_hthread(duk_context *ctx, duk_idx_t index); -DUK_INTERNAL_DECL duk_hcompiledfunction *duk_require_hcompiledfunction(duk_context *ctx, duk_idx_t index); -DUK_INTERNAL_DECL duk_hnativefunction *duk_require_hnativefunction(duk_context *ctx, duk_idx_t index); - -DUK_INTERNAL_DECL duk_hobject *duk_require_hobject_with_class(duk_context *ctx, duk_idx_t index, duk_small_uint_t classnum); - -DUK_INTERNAL_DECL duk_hobject *duk_require_hobject_or_lfunc(duk_context *ctx, duk_idx_t index); -DUK_INTERNAL_DECL duk_hobject *duk_require_hobject_or_lfunc_coerce(duk_context *ctx, duk_idx_t index); - -DUK_INTERNAL_DECL void duk_push_hstring(duk_context *ctx, duk_hstring *h); -DUK_INTERNAL_DECL void duk_push_hstring_stridx(duk_context *ctx, duk_small_int_t stridx); -DUK_INTERNAL_DECL void duk_push_hobject(duk_context *ctx, duk_hobject *h); -DUK_INTERNAL_DECL void duk_push_hbuffer(duk_context *ctx, duk_hbuffer *h); -#define duk_push_hthread(ctx,h) \ - duk_push_hobject((ctx), (duk_hobject *) (h)) -#define duk_push_hcompiledfunction(ctx,h) \ - duk_push_hobject((ctx), (duk_hobject *) (h)) -#define duk_push_hnativefunction(ctx,h) \ - duk_push_hobject((ctx), (duk_hobject *) (h)) -DUK_INTERNAL_DECL void duk_push_hobject_bidx(duk_context *ctx, duk_small_int_t builtin_idx); -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); -DUK_INTERNAL_DECL duk_idx_t duk_push_object_helper_proto(duk_context *ctx, duk_uint_t hobject_flags_and_class, duk_hobject *proto); -DUK_INTERNAL_DECL duk_idx_t duk_push_object_internal(duk_context *ctx); -DUK_INTERNAL_DECL duk_idx_t duk_push_compiledfunction(duk_context *ctx); -DUK_INTERNAL_DECL void duk_push_c_function_noexotic(duk_context *ctx, duk_c_function func, duk_int_t nargs); -DUK_INTERNAL_DECL void duk_push_c_function_noconstruct_noexotic(duk_context *ctx, duk_c_function func, duk_int_t nargs); - -DUK_INTERNAL_DECL void duk_push_string_funcptr(duk_context *ctx, duk_uint8_t *ptr, duk_size_t sz); -DUK_INTERNAL_DECL void duk_push_lightfunc_name(duk_context *ctx, duk_tval *tv); -DUK_INTERNAL_DECL void duk_push_lightfunc_tostring(duk_context *ctx, duk_tval *tv); -DUK_INTERNAL_DECL duk_hbufferobject *duk_push_bufferobject_raw(duk_context *ctx, duk_uint_t hobject_flags_and_class, duk_small_int_t prototype_bidx); - -#if !defined(DUK_USE_PARANOID_ERRORS) -DUK_INTERNAL_DECL const char *duk_push_string_readable(duk_context *ctx, duk_idx_t index); -DUK_INTERNAL_DECL const char *duk_push_string_tval_readable(duk_context *ctx, duk_tval *tv); -#endif - -DUK_INTERNAL_DECL duk_bool_t duk_get_prop_stridx(duk_context *ctx, duk_idx_t obj_index, duk_small_int_t stridx); /* [] -> [val] */ -DUK_INTERNAL_DECL duk_bool_t duk_put_prop_stridx(duk_context *ctx, duk_idx_t obj_index, duk_small_int_t stridx); /* [val] -> [] */ -DUK_INTERNAL_DECL duk_bool_t duk_del_prop_stridx(duk_context *ctx, duk_idx_t obj_index, duk_small_int_t stridx); /* [] -> [] */ -DUK_INTERNAL_DECL duk_bool_t duk_has_prop_stridx(duk_context *ctx, duk_idx_t obj_index, duk_small_int_t stridx); /* [] -> [] */ - -DUK_INTERNAL_DECL duk_bool_t duk_get_prop_stridx_boolean(duk_context *ctx, duk_idx_t obj_index, duk_small_int_t stridx, duk_bool_t *out_has_prop); /* [] -> [] */ - -DUK_INTERNAL_DECL void duk_xdef_prop(duk_context *ctx, duk_idx_t obj_index, duk_small_uint_t desc_flags); /* [key val] -> [] */ -DUK_INTERNAL_DECL void duk_xdef_prop_index(duk_context *ctx, duk_idx_t obj_index, duk_uarridx_t arr_index, duk_small_uint_t desc_flags); /* [val] -> [] */ -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); /* [val] -> [] */ -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); /* [] -> [] */ -DUK_INTERNAL_DECL void duk_xdef_prop_stridx_thrower(duk_context *ctx, duk_idx_t obj_index, duk_small_int_t stridx, duk_small_uint_t desc_flags); /* [] -> [] */ - -/* These are macros for now, but could be separate functions to reduce code - * footprint (check call site count before refactoring). - */ -#define duk_xdef_prop_wec(ctx,obj_index) \ - duk_xdef_prop((ctx), (obj_index), DUK_PROPDESC_FLAGS_WEC) -#define duk_xdef_prop_index_wec(ctx,obj_index,arr_index) \ - duk_xdef_prop_index((ctx), (obj_index), (arr_index), DUK_PROPDESC_FLAGS_WEC) -#define duk_xdef_prop_stridx_wec(ctx,obj_index,stridx) \ - duk_xdef_prop_stridx((ctx), (obj_index), (stridx), DUK_PROPDESC_FLAGS_WEC) - -/* Set object 'length'. */ -DUK_INTERNAL_DECL void duk_set_length(duk_context *ctx, duk_idx_t index, duk_size_t length); - -/* Raw internal valstack access macros: access is unsafe so call site - * must have a guarantee that the index is valid. When that is the case, - * using these macro results in faster and smaller code than duk_get_tval(). - * Both 'ctx' and 'idx' are evaluted multiple times, but only for asserts. - */ -#define DUK_ASSERT_VALID_NEGIDX(ctx,idx) \ - (DUK_ASSERT_EXPR((idx) < 0), DUK_ASSERT_EXPR(duk_is_valid_index((ctx), (idx)))) -#define DUK_ASSERT_VALID_POSIDX(ctx,idx) \ - (DUK_ASSERT_EXPR((idx) >= 0), DUK_ASSERT_EXPR(duk_is_valid_index((ctx), (idx)))) -#define DUK_GET_TVAL_NEGIDX(ctx,idx) \ - (DUK_ASSERT_VALID_NEGIDX((ctx),(idx)), ((duk_hthread *) (ctx))->valstack_top + (idx)) -#define DUK_GET_TVAL_POSIDX(ctx,idx) \ - (DUK_ASSERT_VALID_POSIDX((ctx),(idx)), ((duk_hthread *) (ctx))->valstack_bottom + (idx)) -#define DUK_GET_HOBJECT_NEGIDX(ctx,idx) \ - (DUK_ASSERT_VALID_NEGIDX((ctx),(idx)), DUK_TVAL_GET_OBJECT(((duk_hthread *) (ctx))->valstack_top + (idx))) -#define DUK_GET_HOBJECT_POSIDX(ctx,idx) \ - (DUK_ASSERT_VALID_POSIDX((ctx),(idx)), DUK_TVAL_GET_OBJECT(((duk_hthread *) (ctx))->valstack_bottom + (idx))) - -#endif /* DUK_API_INTERNAL_H_INCLUDED */ -#line 1 "duk_hstring.h" -/* - * Heap string representation. - * - * Strings are byte sequences ordinarily stored in extended UTF-8 format, - * allowing values larger than the official UTF-8 range (used internally) - * and also allowing UTF-8 encoding of surrogate pairs (CESU-8 format). - * Strings may also be invalid UTF-8 altogether which is the case e.g. with - * strings used as internal property names and raw buffers converted to - * strings. In such cases the 'clen' field contains an inaccurate value. - * - * Ecmascript requires support for 32-bit long strings. However, since each - * 16-bit codepoint can take 3 bytes in CESU-8, this representation can only - * support about 1.4G codepoint long strings in extreme cases. This is not - * really a practical issue. - */ - -#ifndef DUK_HSTRING_H_INCLUDED -#define DUK_HSTRING_H_INCLUDED - -/* Impose a maximum string length for now. Restricted artificially to - * ensure adding a heap header length won't overflow size_t. The limit - * should be synchronized with DUK_HBUFFER_MAX_BYTELEN. - * - * E5.1 makes provisions to support strings longer than 4G characters. - * This limit should be eliminated on 64-bit platforms (and increased - * closer to maximum support on 32-bit platforms). - */ - -#if defined(DUK_USE_STRLEN16) -#define DUK_HSTRING_MAX_BYTELEN (0x0000ffffUL) -#else -#define DUK_HSTRING_MAX_BYTELEN (0x7fffffffUL) -#endif - -/* XXX: could add flags for "is valid CESU-8" (Ecmascript compatible strings), - * "is valid UTF-8", "is valid extended UTF-8" (internal strings are not, - * regexp bytecode is), and "contains non-BMP characters". These are not - * needed right now. - */ - -#define DUK_HSTRING_FLAG_ASCII DUK_HEAPHDR_USER_FLAG(0) /* string is ASCII, clen == blen */ -#define DUK_HSTRING_FLAG_ARRIDX DUK_HEAPHDR_USER_FLAG(1) /* string is a valid array index */ -#define DUK_HSTRING_FLAG_INTERNAL DUK_HEAPHDR_USER_FLAG(2) /* string is internal */ -#define DUK_HSTRING_FLAG_RESERVED_WORD DUK_HEAPHDR_USER_FLAG(3) /* string is a reserved word (non-strict) */ -#define DUK_HSTRING_FLAG_STRICT_RESERVED_WORD DUK_HEAPHDR_USER_FLAG(4) /* string is a reserved word (strict) */ -#define DUK_HSTRING_FLAG_EVAL_OR_ARGUMENTS DUK_HEAPHDR_USER_FLAG(5) /* string is 'eval' or 'arguments' */ -#define DUK_HSTRING_FLAG_EXTDATA DUK_HEAPHDR_USER_FLAG(6) /* string data is external (duk_hstring_external) */ - -#define DUK_HSTRING_HAS_ASCII(x) DUK_HEAPHDR_CHECK_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_ASCII) -#define DUK_HSTRING_HAS_ARRIDX(x) DUK_HEAPHDR_CHECK_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_ARRIDX) -#define DUK_HSTRING_HAS_INTERNAL(x) DUK_HEAPHDR_CHECK_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_INTERNAL) -#define DUK_HSTRING_HAS_RESERVED_WORD(x) DUK_HEAPHDR_CHECK_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_RESERVED_WORD) -#define DUK_HSTRING_HAS_STRICT_RESERVED_WORD(x) DUK_HEAPHDR_CHECK_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_STRICT_RESERVED_WORD) -#define DUK_HSTRING_HAS_EVAL_OR_ARGUMENTS(x) DUK_HEAPHDR_CHECK_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_EVAL_OR_ARGUMENTS) -#define DUK_HSTRING_HAS_EXTDATA(x) DUK_HEAPHDR_CHECK_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_EXTDATA) - -#define DUK_HSTRING_SET_ASCII(x) DUK_HEAPHDR_SET_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_ASCII) -#define DUK_HSTRING_SET_ARRIDX(x) DUK_HEAPHDR_SET_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_ARRIDX) -#define DUK_HSTRING_SET_INTERNAL(x) DUK_HEAPHDR_SET_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_INTERNAL) -#define DUK_HSTRING_SET_RESERVED_WORD(x) DUK_HEAPHDR_SET_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_RESERVED_WORD) -#define DUK_HSTRING_SET_STRICT_RESERVED_WORD(x) DUK_HEAPHDR_SET_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_STRICT_RESERVED_WORD) -#define DUK_HSTRING_SET_EVAL_OR_ARGUMENTS(x) DUK_HEAPHDR_SET_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_EVAL_OR_ARGUMENTS) -#define DUK_HSTRING_SET_EXTDATA(x) DUK_HEAPHDR_SET_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_EXTDATA) - -#define DUK_HSTRING_CLEAR_ASCII(x) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_ASCII) -#define DUK_HSTRING_CLEAR_ARRIDX(x) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_ARRIDX) -#define DUK_HSTRING_CLEAR_INTERNAL(x) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_INTERNAL) -#define DUK_HSTRING_CLEAR_RESERVED_WORD(x) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_RESERVED_WORD) -#define DUK_HSTRING_CLEAR_STRICT_RESERVED_WORD(x) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_STRICT_RESERVED_WORD) -#define DUK_HSTRING_CLEAR_EVAL_OR_ARGUMENTS(x) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_EVAL_OR_ARGUMENTS) -#define DUK_HSTRING_CLEAR_EXTDATA(x) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_EXTDATA) - -#if 0 /* Slightly smaller code without explicit flag, but explicit flag - * is very useful when 'clen' is dropped. - */ -#define DUK_HSTRING_IS_ASCII(x) (DUK_HSTRING_GET_BYTELEN((x)) == DUK_HSTRING_GET_CHARLEN((x))) -#endif -#define DUK_HSTRING_IS_ASCII(x) DUK_HSTRING_HAS_ASCII((x)) -#define DUK_HSTRING_IS_EMPTY(x) (DUK_HSTRING_GET_BYTELEN((x)) == 0) - -#if defined(DUK_USE_STRHASH16) -#define DUK_HSTRING_GET_HASH(x) ((x)->hdr.h_flags >> 16) -#define DUK_HSTRING_SET_HASH(x,v) do { \ - (x)->hdr.h_flags = ((x)->hdr.h_flags & 0x0000ffffUL) | ((v) << 16); \ - } while (0) -#else -#define DUK_HSTRING_GET_HASH(x) ((x)->hash) -#define DUK_HSTRING_SET_HASH(x,v) do { \ - (x)->hash = (v); \ - } while (0) -#endif - -#if defined(DUK_USE_STRLEN16) -#define DUK_HSTRING_GET_BYTELEN(x) ((x)->hdr.h_strextra16) -#define DUK_HSTRING_SET_BYTELEN(x,v) do { \ - (x)->hdr.h_strextra16 = (v); \ - } while (0) -#if defined(DUK_USE_HSTRING_CLEN) -#define DUK_HSTRING_GET_CHARLEN(x) ((x)->clen16) -#define DUK_HSTRING_SET_CHARLEN(x,v) do { \ - (x)->clen16 = (v); \ - } while (0) -#else -#define DUK_HSTRING_GET_CHARLEN(x) duk_hstring_get_charlen((x)) -#define DUK_HSTRING_SET_CHARLEN(x,v) do { \ - DUK_ASSERT(0); /* should never be called */ \ - } while (0) -#endif -#else -#define DUK_HSTRING_GET_BYTELEN(x) ((x)->blen) -#define DUK_HSTRING_SET_BYTELEN(x,v) do { \ - (x)->blen = (v); \ - } while (0) -#define DUK_HSTRING_GET_CHARLEN(x) ((x)->clen) -#define DUK_HSTRING_SET_CHARLEN(x,v) do { \ - (x)->clen = (v); \ - } while (0) -#endif - -#if defined(DUK_USE_HSTRING_EXTDATA) -#define DUK_HSTRING_GET_EXTDATA(x) \ - ((x)->extdata) -#define DUK_HSTRING_GET_DATA(x) \ - (DUK_HSTRING_HAS_EXTDATA((x)) ? \ - DUK_HSTRING_GET_EXTDATA((const duk_hstring_external *) (x)) : ((const duk_uint8_t *) ((x) + 1))) -#else -#define DUK_HSTRING_GET_DATA(x) \ - ((const duk_uint8_t *) ((x) + 1)) -#endif - -#define DUK_HSTRING_GET_DATA_END(x) \ - (DUK_HSTRING_GET_DATA((x)) + (x)->blen) - -/* marker value; in E5 2^32-1 is not a valid array index (2^32-2 is highest valid) */ -#define DUK_HSTRING_NO_ARRAY_INDEX (0xffffffffUL) - -/* get array index related to string (or return DUK_HSTRING_NO_ARRAY_INDEX); - * avoids helper call if string has no array index value. - */ -#define DUK_HSTRING_GET_ARRIDX_FAST(h) \ - (DUK_HSTRING_HAS_ARRIDX((h)) ? duk_js_to_arrayindex_string_helper((h)) : DUK_HSTRING_NO_ARRAY_INDEX) - -/* slower but more compact variant */ -#define DUK_HSTRING_GET_ARRIDX_SLOW(h) \ - (duk_js_to_arrayindex_string_helper((h))) - -/* - * Misc - */ - -struct duk_hstring { - /* Smaller heaphdr than for other objects, because strings are held - * in string intern table which requires no link pointers. Much of - * the 32-bit flags field is unused by flags, so we can stuff a 16-bit - * field in there. - */ - duk_heaphdr_string hdr; - - /* Note: we could try to stuff a partial hash (e.g. 16 bits) into the - * shared heap header. Good hashing needs more hash bits though. - */ - - /* string hash */ -#if defined(DUK_USE_STRHASH16) - /* If 16-bit hash is in use, stuff it into duk_heaphdr_string flags. */ -#else - duk_uint32_t hash; -#endif - - /* length in bytes (not counting NUL term) */ -#if defined(DUK_USE_STRLEN16) - /* placed in duk_heaphdr_string */ -#else - duk_uint32_t blen; -#endif - - /* length in codepoints (must be E5 compatible) */ -#if defined(DUK_USE_STRLEN16) -#if defined(DUK_USE_HSTRING_CLEN) - duk_uint16_t clen16; -#else - /* computed live */ -#endif -#else - duk_uint32_t clen; -#endif - - /* - * String value of 'blen+1' bytes follows (+1 for NUL termination - * convenience for C API). No alignment needs to be guaranteed - * for strings, but fields above should guarantee alignment-by-4 - * (but not alignment-by-8). - */ -}; - -/* The external string struct is defined even when the feature is inactive. */ -struct duk_hstring_external { - duk_hstring str; - - /* - * For an external string, the NUL-terminated string data is stored - * externally. The user must guarantee that data behind this pointer - * doesn't change while it's used. - */ - - const duk_uint8_t *extdata; -}; - -/* - * Prototypes - */ - -DUK_INTERNAL_DECL duk_ucodepoint_t duk_hstring_char_code_at_raw(duk_hthread *thr, duk_hstring *h, duk_uint_t pos); - -#if !defined(DUK_USE_HSTRING_CLEN) -DUK_INTERNAL_DECL duk_size_t duk_hstring_get_charlen(duk_hstring *h); -#endif - -#endif /* DUK_HSTRING_H_INCLUDED */ -#line 1 "duk_hobject.h" -/* - * Heap object representation. - * - * Heap objects are used for Ecmascript objects, arrays, and functions, - * but also for internal control like declarative and object environment - * records. Compiled functions, native functions, and threads are also - * objects but with an extended C struct. - * - * Objects provide the required Ecmascript semantics and exotic behaviors - * especially for property access. - * - * Properties are stored in three conceptual parts: - * - * 1. A linear 'entry part' contains ordered key-value-attributes triples - * and is the main method of string properties. - * - * 2. An optional linear 'array part' is used for array objects to store a - * (dense) range of [0,N[ array indexed entries with default attributes - * (writable, enumerable, configurable). If the array part would become - * sparse or non-default attributes are required, the array part is - * abandoned and moved to the 'entry part'. - * - * 3. An optional 'hash part' is used to optimize lookups of the entry - * part; it is used only for objects with sufficiently many properties - * and can be abandoned without loss of information. - * - * These three conceptual parts are stored in a single memory allocated area. - * This minimizes memory allocation overhead but also means that all three - * parts are resized together, and makes property access a bit complicated. - */ - -#ifndef DUK_HOBJECT_H_INCLUDED -#define DUK_HOBJECT_H_INCLUDED - -/* Object flag. There are currently 26 flag bits available. Make sure - * this stays in sync with debugger object inspection code. - */ -#define DUK_HOBJECT_FLAG_EXTENSIBLE DUK_HEAPHDR_USER_FLAG(0) /* object is extensible */ -#define DUK_HOBJECT_FLAG_CONSTRUCTABLE DUK_HEAPHDR_USER_FLAG(1) /* object is constructable */ -#define DUK_HOBJECT_FLAG_BOUND DUK_HEAPHDR_USER_FLAG(2) /* object established using Function.prototype.bind() */ -#define DUK_HOBJECT_FLAG_COMPILEDFUNCTION DUK_HEAPHDR_USER_FLAG(4) /* object is a compiled function (duk_hcompiledfunction) */ -#define DUK_HOBJECT_FLAG_NATIVEFUNCTION DUK_HEAPHDR_USER_FLAG(5) /* object is a native function (duk_hnativefunction) */ -#define DUK_HOBJECT_FLAG_BUFFEROBJECT DUK_HEAPHDR_USER_FLAG(6) /* object is a buffer object (duk_hbufferobject) (always exotic) */ -#define DUK_HOBJECT_FLAG_THREAD DUK_HEAPHDR_USER_FLAG(7) /* object is a thread (duk_hthread) */ -#define DUK_HOBJECT_FLAG_ARRAY_PART DUK_HEAPHDR_USER_FLAG(8) /* object has an array part (a_size may still be 0) */ -#define DUK_HOBJECT_FLAG_STRICT DUK_HEAPHDR_USER_FLAG(9) /* function: function object is strict */ -#define DUK_HOBJECT_FLAG_NOTAIL DUK_HEAPHDR_USER_FLAG(10) /* function: function must not be tail called */ -#define DUK_HOBJECT_FLAG_NEWENV DUK_HEAPHDR_USER_FLAG(11) /* function: create new environment when called (see duk_hcompiledfunction) */ -#define DUK_HOBJECT_FLAG_NAMEBINDING DUK_HEAPHDR_USER_FLAG(12) /* function: create binding for func name (function templates only, used for named function expressions) */ -#define DUK_HOBJECT_FLAG_CREATEARGS DUK_HEAPHDR_USER_FLAG(13) /* function: create an arguments object on function call */ -#define DUK_HOBJECT_FLAG_ENVRECCLOSED DUK_HEAPHDR_USER_FLAG(14) /* envrec: (declarative) record is closed */ -#define DUK_HOBJECT_FLAG_EXOTIC_ARRAY DUK_HEAPHDR_USER_FLAG(15) /* 'Array' object, array length and index exotic behavior */ -#define DUK_HOBJECT_FLAG_EXOTIC_STRINGOBJ DUK_HEAPHDR_USER_FLAG(16) /* 'String' object, array index exotic behavior */ -#define DUK_HOBJECT_FLAG_EXOTIC_ARGUMENTS DUK_HEAPHDR_USER_FLAG(17) /* 'Arguments' object and has arguments exotic behavior (non-strict callee) */ -#define DUK_HOBJECT_FLAG_EXOTIC_DUKFUNC DUK_HEAPHDR_USER_FLAG(18) /* Duktape/C (nativefunction) object, exotic 'length' */ -#define DUK_HOBJECT_FLAG_EXOTIC_PROXYOBJ DUK_HEAPHDR_USER_FLAG(19) /* 'Proxy' object */ - -#define DUK_HOBJECT_FLAG_CLASS_BASE DUK_HEAPHDR_USER_FLAG_NUMBER(20) -#define DUK_HOBJECT_FLAG_CLASS_BITS 5 - -#define DUK_HOBJECT_GET_CLASS_NUMBER(h) \ - DUK_HEAPHDR_GET_FLAG_RANGE(&(h)->hdr, DUK_HOBJECT_FLAG_CLASS_BASE, DUK_HOBJECT_FLAG_CLASS_BITS) -#define DUK_HOBJECT_SET_CLASS_NUMBER(h,v) \ - DUK_HEAPHDR_SET_FLAG_RANGE(&(h)->hdr, DUK_HOBJECT_FLAG_CLASS_BASE, DUK_HOBJECT_FLAG_CLASS_BITS, (v)) - -#define DUK_HOBJECT_GET_CLASS_MASK(h) \ - (1UL << DUK_HEAPHDR_GET_FLAG_RANGE(&(h)->hdr, DUK_HOBJECT_FLAG_CLASS_BASE, DUK_HOBJECT_FLAG_CLASS_BITS)) - -/* Macro for creating flag initializer from a class number. - * Unsigned type cast is needed to avoid warnings about coercing - * a signed integer to an unsigned one; the largest class values - * have the highest bit (bit 31) set which causes this. - */ -#define DUK_HOBJECT_CLASS_AS_FLAGS(v) (((duk_uint_t) (v)) << DUK_HOBJECT_FLAG_CLASS_BASE) - -/* E5 Section 8.6.2 + custom classes */ -#define DUK_HOBJECT_CLASS_UNUSED 0 -#define DUK_HOBJECT_CLASS_ARGUMENTS 1 -#define DUK_HOBJECT_CLASS_ARRAY 2 -#define DUK_HOBJECT_CLASS_BOOLEAN 3 -#define DUK_HOBJECT_CLASS_DATE 4 -#define DUK_HOBJECT_CLASS_ERROR 5 -#define DUK_HOBJECT_CLASS_FUNCTION 6 -#define DUK_HOBJECT_CLASS_JSON 7 -#define DUK_HOBJECT_CLASS_MATH 8 -#define DUK_HOBJECT_CLASS_NUMBER 9 -#define DUK_HOBJECT_CLASS_OBJECT 10 -#define DUK_HOBJECT_CLASS_REGEXP 11 -#define DUK_HOBJECT_CLASS_STRING 12 -#define DUK_HOBJECT_CLASS_GLOBAL 13 -#define DUK_HOBJECT_CLASS_OBJENV 14 /* custom */ -#define DUK_HOBJECT_CLASS_DECENV 15 /* custom */ -#define DUK_HOBJECT_CLASS_BUFFER 16 /* custom; implies DUK_HOBJECT_IS_BUFFEROBJECT */ -#define DUK_HOBJECT_CLASS_POINTER 17 /* custom */ -#define DUK_HOBJECT_CLASS_THREAD 18 /* custom; implies DUK_HOBJECT_IS_THREAD */ -#define DUK_HOBJECT_CLASS_ARRAYBUFFER 19 /* implies DUK_HOBJECT_IS_BUFFEROBJECT */ -#define DUK_HOBJECT_CLASS_DATAVIEW 20 -#define DUK_HOBJECT_CLASS_INT8ARRAY 21 -#define DUK_HOBJECT_CLASS_UINT8ARRAY 22 -#define DUK_HOBJECT_CLASS_UINT8CLAMPEDARRAY 23 -#define DUK_HOBJECT_CLASS_INT16ARRAY 24 -#define DUK_HOBJECT_CLASS_UINT16ARRAY 25 -#define DUK_HOBJECT_CLASS_INT32ARRAY 26 -#define DUK_HOBJECT_CLASS_UINT32ARRAY 27 -#define DUK_HOBJECT_CLASS_FLOAT32ARRAY 28 -#define DUK_HOBJECT_CLASS_FLOAT64ARRAY 29 -#define DUK_HOBJECT_CLASS_MAX 29 - -/* class masks */ -#define DUK_HOBJECT_CMASK_ALL ((1UL << (DUK_HOBJECT_CLASS_MAX + 1)) - 1UL) -#define DUK_HOBJECT_CMASK_UNUSED (1UL << DUK_HOBJECT_CLASS_UNUSED) -#define DUK_HOBJECT_CMASK_ARGUMENTS (1UL << DUK_HOBJECT_CLASS_ARGUMENTS) -#define DUK_HOBJECT_CMASK_ARRAY (1UL << DUK_HOBJECT_CLASS_ARRAY) -#define DUK_HOBJECT_CMASK_BOOLEAN (1UL << DUK_HOBJECT_CLASS_BOOLEAN) -#define DUK_HOBJECT_CMASK_DATE (1UL << DUK_HOBJECT_CLASS_DATE) -#define DUK_HOBJECT_CMASK_ERROR (1UL << DUK_HOBJECT_CLASS_ERROR) -#define DUK_HOBJECT_CMASK_FUNCTION (1UL << DUK_HOBJECT_CLASS_FUNCTION) -#define DUK_HOBJECT_CMASK_JSON (1UL << DUK_HOBJECT_CLASS_JSON) -#define DUK_HOBJECT_CMASK_MATH (1UL << DUK_HOBJECT_CLASS_MATH) -#define DUK_HOBJECT_CMASK_NUMBER (1UL << DUK_HOBJECT_CLASS_NUMBER) -#define DUK_HOBJECT_CMASK_OBJECT (1UL << DUK_HOBJECT_CLASS_OBJECT) -#define DUK_HOBJECT_CMASK_REGEXP (1UL << DUK_HOBJECT_CLASS_REGEXP) -#define DUK_HOBJECT_CMASK_STRING (1UL << DUK_HOBJECT_CLASS_STRING) -#define DUK_HOBJECT_CMASK_GLOBAL (1UL << DUK_HOBJECT_CLASS_GLOBAL) -#define DUK_HOBJECT_CMASK_OBJENV (1UL << DUK_HOBJECT_CLASS_OBJENV) -#define DUK_HOBJECT_CMASK_DECENV (1UL << DUK_HOBJECT_CLASS_DECENV) -#define DUK_HOBJECT_CMASK_BUFFER (1UL << DUK_HOBJECT_CLASS_BUFFER) -#define DUK_HOBJECT_CMASK_POINTER (1UL << DUK_HOBJECT_CLASS_POINTER) -#define DUK_HOBJECT_CMASK_THREAD (1UL << DUK_HOBJECT_CLASS_THREAD) -#define DUK_HOBJECT_CMASK_ARRAYBUFFER (1UL << DUK_HOBJECT_CLASS_ARRAYBUFFER) -#define DUK_HOBJECT_CMASK_DATAVIEW (1UL << DUK_HOBJECT_CLASS_DATAVIEW) -#define DUK_HOBJECT_CMASK_INT8ARRAY (1UL << DUK_HOBJECT_CLASS_INT8ARRAY) -#define DUK_HOBJECT_CMASK_UINT8ARRAY (1UL << DUK_HOBJECT_CLASS_UINT8ARRAY) -#define DUK_HOBJECT_CMASK_UINT8CLAMPEDARRAY (1UL << DUK_HOBJECT_CLASS_UINT8CLAMPEDARRAY) -#define DUK_HOBJECT_CMASK_INT16ARRAY (1UL << DUK_HOBJECT_CLASS_INT16ARRAY) -#define DUK_HOBJECT_CMASK_UINT16ARRAY (1UL << DUK_HOBJECT_CLASS_UINT16ARRAY) -#define DUK_HOBJECT_CMASK_INT32ARRAY (1UL << DUK_HOBJECT_CLASS_INT32ARRAY) -#define DUK_HOBJECT_CMASK_UINT32ARRAY (1UL << DUK_HOBJECT_CLASS_UINT32ARRAY) -#define DUK_HOBJECT_CMASK_FLOAT32ARRAY (1UL << DUK_HOBJECT_CLASS_FLOAT32ARRAY) -#define DUK_HOBJECT_CMASK_FLOAT64ARRAY (1UL << DUK_HOBJECT_CLASS_FLOAT64ARRAY) - -#define DUK_HOBJECT_CMASK_ALL_BUFFEROBJECTS \ - (DUK_HOBJECT_CMASK_BUFFER | \ - DUK_HOBJECT_CMASK_ARRAYBUFFER | \ - DUK_HOBJECT_CMASK_DATAVIEW | \ - DUK_HOBJECT_CMASK_INT8ARRAY | \ - DUK_HOBJECT_CMASK_UINT8ARRAY | \ - DUK_HOBJECT_CMASK_UINT8CLAMPEDARRAY | \ - DUK_HOBJECT_CMASK_INT16ARRAY | \ - DUK_HOBJECT_CMASK_UINT16ARRAY | \ - DUK_HOBJECT_CMASK_INT32ARRAY | \ - DUK_HOBJECT_CMASK_UINT32ARRAY | \ - DUK_HOBJECT_CMASK_FLOAT32ARRAY | \ - DUK_HOBJECT_CMASK_FLOAT64ARRAY) - -#define DUK_HOBJECT_IS_OBJENV(h) (DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_OBJENV) -#define DUK_HOBJECT_IS_DECENV(h) (DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_DECENV) -#define DUK_HOBJECT_IS_ENV(h) (DUK_HOBJECT_IS_OBJENV((h)) || DUK_HOBJECT_IS_DECENV((h))) -#define DUK_HOBJECT_IS_ARRAY(h) (DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_ARRAY) -#define DUK_HOBJECT_IS_COMPILEDFUNCTION(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_COMPILEDFUNCTION) -#define DUK_HOBJECT_IS_NATIVEFUNCTION(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NATIVEFUNCTION) -#define DUK_HOBJECT_IS_BUFFEROBJECT(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_BUFFEROBJECT) -#define DUK_HOBJECT_IS_THREAD(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_THREAD) - -#define DUK_HOBJECT_IS_NONBOUND_FUNCTION(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, \ - DUK_HOBJECT_FLAG_COMPILEDFUNCTION | \ - DUK_HOBJECT_FLAG_NATIVEFUNCTION) - -#define DUK_HOBJECT_IS_FUNCTION(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, \ - DUK_HOBJECT_FLAG_BOUND | \ - DUK_HOBJECT_FLAG_COMPILEDFUNCTION | \ - DUK_HOBJECT_FLAG_NATIVEFUNCTION) - -#define DUK_HOBJECT_IS_CALLABLE(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, \ - DUK_HOBJECT_FLAG_BOUND | \ - DUK_HOBJECT_FLAG_COMPILEDFUNCTION | \ - DUK_HOBJECT_FLAG_NATIVEFUNCTION) - -/* object has any exotic behavior(s) */ -#define DUK_HOBJECT_EXOTIC_BEHAVIOR_FLAGS (DUK_HOBJECT_FLAG_EXOTIC_ARRAY | \ - DUK_HOBJECT_FLAG_EXOTIC_ARGUMENTS | \ - DUK_HOBJECT_FLAG_EXOTIC_STRINGOBJ | \ - DUK_HOBJECT_FLAG_EXOTIC_DUKFUNC | \ - DUK_HOBJECT_FLAG_BUFFEROBJECT | \ - DUK_HOBJECT_FLAG_EXOTIC_PROXYOBJ) - -#define DUK_HOBJECT_HAS_EXOTIC_BEHAVIOR(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_EXOTIC_BEHAVIOR_FLAGS) - -#define DUK_HOBJECT_HAS_EXTENSIBLE(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXTENSIBLE) -#define DUK_HOBJECT_HAS_CONSTRUCTABLE(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_CONSTRUCTABLE) -#define DUK_HOBJECT_HAS_BOUND(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_BOUND) -#define DUK_HOBJECT_HAS_COMPILEDFUNCTION(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_COMPILEDFUNCTION) -#define DUK_HOBJECT_HAS_NATIVEFUNCTION(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NATIVEFUNCTION) -#define DUK_HOBJECT_HAS_BUFFEROBJECT(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_BUFFEROBJECT) -#define DUK_HOBJECT_HAS_THREAD(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_THREAD) -#define DUK_HOBJECT_HAS_ARRAY_PART(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_ARRAY_PART) -#define DUK_HOBJECT_HAS_STRICT(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_STRICT) -#define DUK_HOBJECT_HAS_NOTAIL(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NOTAIL) -#define DUK_HOBJECT_HAS_NEWENV(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NEWENV) -#define DUK_HOBJECT_HAS_NAMEBINDING(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NAMEBINDING) -#define DUK_HOBJECT_HAS_CREATEARGS(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_CREATEARGS) -#define DUK_HOBJECT_HAS_ENVRECCLOSED(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_ENVRECCLOSED) -#define DUK_HOBJECT_HAS_EXOTIC_ARRAY(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_ARRAY) -#define DUK_HOBJECT_HAS_EXOTIC_STRINGOBJ(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_STRINGOBJ) -#define DUK_HOBJECT_HAS_EXOTIC_ARGUMENTS(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_ARGUMENTS) -#define DUK_HOBJECT_HAS_EXOTIC_DUKFUNC(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_DUKFUNC) -#define DUK_HOBJECT_HAS_EXOTIC_PROXYOBJ(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_PROXYOBJ) - -#define DUK_HOBJECT_SET_EXTENSIBLE(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXTENSIBLE) -#define DUK_HOBJECT_SET_CONSTRUCTABLE(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_CONSTRUCTABLE) -#define DUK_HOBJECT_SET_BOUND(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_BOUND) -#define DUK_HOBJECT_SET_COMPILEDFUNCTION(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_COMPILEDFUNCTION) -#define DUK_HOBJECT_SET_NATIVEFUNCTION(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NATIVEFUNCTION) -#define DUK_HOBJECT_SET_BUFFEROBJECT(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_BUFFEROBJECT) -#define DUK_HOBJECT_SET_THREAD(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_THREAD) -#define DUK_HOBJECT_SET_ARRAY_PART(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_ARRAY_PART) -#define DUK_HOBJECT_SET_STRICT(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_STRICT) -#define DUK_HOBJECT_SET_NOTAIL(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NOTAIL) -#define DUK_HOBJECT_SET_NEWENV(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NEWENV) -#define DUK_HOBJECT_SET_NAMEBINDING(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NAMEBINDING) -#define DUK_HOBJECT_SET_CREATEARGS(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_CREATEARGS) -#define DUK_HOBJECT_SET_ENVRECCLOSED(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_ENVRECCLOSED) -#define DUK_HOBJECT_SET_EXOTIC_ARRAY(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_ARRAY) -#define DUK_HOBJECT_SET_EXOTIC_STRINGOBJ(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_STRINGOBJ) -#define DUK_HOBJECT_SET_EXOTIC_ARGUMENTS(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_ARGUMENTS) -#define DUK_HOBJECT_SET_EXOTIC_DUKFUNC(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_DUKFUNC) -#define DUK_HOBJECT_SET_EXOTIC_PROXYOBJ(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_PROXYOBJ) - -#define DUK_HOBJECT_CLEAR_EXTENSIBLE(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXTENSIBLE) -#define DUK_HOBJECT_CLEAR_CONSTRUCTABLE(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_CONSTRUCTABLE) -#define DUK_HOBJECT_CLEAR_BOUND(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_BOUND) -#define DUK_HOBJECT_CLEAR_COMPILEDFUNCTION(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_COMPILEDFUNCTION) -#define DUK_HOBJECT_CLEAR_NATIVEFUNCTION(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NATIVEFUNCTION) -#define DUK_HOBJECT_CLEAR_BUFFEROBJECT(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_BUFFEROBJECT) -#define DUK_HOBJECT_CLEAR_THREAD(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_THREAD) -#define DUK_HOBJECT_CLEAR_ARRAY_PART(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_ARRAY_PART) -#define DUK_HOBJECT_CLEAR_STRICT(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_STRICT) -#define DUK_HOBJECT_CLEAR_NOTAIL(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NOTAIL) -#define DUK_HOBJECT_CLEAR_NEWENV(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NEWENV) -#define DUK_HOBJECT_CLEAR_NAMEBINDING(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NAMEBINDING) -#define DUK_HOBJECT_CLEAR_CREATEARGS(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_CREATEARGS) -#define DUK_HOBJECT_CLEAR_ENVRECCLOSED(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_ENVRECCLOSED) -#define DUK_HOBJECT_CLEAR_EXOTIC_ARRAY(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_ARRAY) -#define DUK_HOBJECT_CLEAR_EXOTIC_STRINGOBJ(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_STRINGOBJ) -#define DUK_HOBJECT_CLEAR_EXOTIC_ARGUMENTS(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_ARGUMENTS) -#define DUK_HOBJECT_CLEAR_EXOTIC_DUKFUNC(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_DUKFUNC) -#define DUK_HOBJECT_CLEAR_EXOTIC_PROXYOBJ(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_PROXYOBJ) - -/* flags used for property attributes in duk_propdesc and packed flags */ -#define DUK_PROPDESC_FLAG_WRITABLE (1 << 0) /* E5 Section 8.6.1 */ -#define DUK_PROPDESC_FLAG_ENUMERABLE (1 << 1) /* E5 Section 8.6.1 */ -#define DUK_PROPDESC_FLAG_CONFIGURABLE (1 << 2) /* E5 Section 8.6.1 */ -#define DUK_PROPDESC_FLAG_ACCESSOR (1 << 3) /* accessor */ -#define DUK_PROPDESC_FLAG_VIRTUAL (1 << 4) /* property is virtual: used in duk_propdesc, never stored - * (used by e.g. buffer virtual properties) - */ -#define DUK_PROPDESC_FLAGS_MASK (DUK_PROPDESC_FLAG_WRITABLE | \ - DUK_PROPDESC_FLAG_ENUMERABLE | \ - DUK_PROPDESC_FLAG_CONFIGURABLE | \ - DUK_PROPDESC_FLAG_ACCESSOR) - -/* additional flags which are passed in the same flags argument as property - * flags but are not stored in object properties. - */ -#define DUK_PROPDESC_FLAG_NO_OVERWRITE (1 << 4) /* internal define property: skip write silently if exists */ - -/* convenience */ -#define DUK_PROPDESC_FLAGS_NONE 0 -#define DUK_PROPDESC_FLAGS_W (DUK_PROPDESC_FLAG_WRITABLE) -#define DUK_PROPDESC_FLAGS_E (DUK_PROPDESC_FLAG_ENUMERABLE) -#define DUK_PROPDESC_FLAGS_C (DUK_PROPDESC_FLAG_CONFIGURABLE) -#define DUK_PROPDESC_FLAGS_WE (DUK_PROPDESC_FLAG_WRITABLE | DUK_PROPDESC_FLAG_ENUMERABLE) -#define DUK_PROPDESC_FLAGS_WC (DUK_PROPDESC_FLAG_WRITABLE | DUK_PROPDESC_FLAG_CONFIGURABLE) -#define DUK_PROPDESC_FLAGS_EC (DUK_PROPDESC_FLAG_ENUMERABLE | DUK_PROPDESC_FLAG_CONFIGURABLE) -#define DUK_PROPDESC_FLAGS_WEC (DUK_PROPDESC_FLAG_WRITABLE | \ - DUK_PROPDESC_FLAG_ENUMERABLE | \ - DUK_PROPDESC_FLAG_CONFIGURABLE) - -/* flags for duk_hobject_get_own_propdesc() and variants */ -#define DUK_GETDESC_FLAG_PUSH_VALUE (1 << 0) /* push value to stack */ -#define DUK_GETDESC_FLAG_IGNORE_PROTOLOOP (1 << 1) /* don't throw for prototype loop */ - -/* - * Macro for object validity check - * - * Assert for currently guaranteed relations between flags, for instance. - */ - -#define DUK_ASSERT_HOBJECT_VALID(h) do { \ - DUK_ASSERT((h) != NULL); \ - DUK_ASSERT(!DUK_HOBJECT_IS_CALLABLE((h)) || \ - DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_FUNCTION); \ - DUK_ASSERT(!DUK_HOBJECT_IS_BUFFEROBJECT((h)) || \ - (DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_BUFFER || \ - DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_ARRAYBUFFER || \ - DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_DATAVIEW || \ - DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_INT8ARRAY || \ - DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_UINT8ARRAY || \ - DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_UINT8CLAMPEDARRAY || \ - DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_INT16ARRAY || \ - DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_UINT16ARRAY || \ - DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_INT32ARRAY || \ - DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_UINT32ARRAY || \ - DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_FLOAT32ARRAY || \ - DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_FLOAT64ARRAY)); \ - } while (0) - -/* - * Macros to access the 'props' allocation. - */ - -#if defined(DUK_USE_HEAPPTR16) -#define DUK_HOBJECT_GET_PROPS(heap,h) \ - ((duk_uint8_t *) DUK_USE_HEAPPTR_DEC16((heap)->heap_udata, ((duk_heaphdr *) (h))->h_extra16)) -#define DUK_HOBJECT_SET_PROPS(heap,h,x) do { \ - ((duk_heaphdr *) (h))->h_extra16 = DUK_USE_HEAPPTR_ENC16((heap)->heap_udata, (void *) (x)); \ - } while (0) -#else -#define DUK_HOBJECT_GET_PROPS(heap,h) \ - ((h)->props) -#define DUK_HOBJECT_SET_PROPS(heap,h,x) do { \ - (h)->props = (duk_uint8_t *) (x); \ - } while (0) -#endif - -#if defined(DUK_USE_HOBJECT_LAYOUT_1) -/* LAYOUT 1 */ -#define DUK_HOBJECT_E_GET_KEY_BASE(heap,h) \ - ((duk_hstring **) (void *) ( \ - DUK_HOBJECT_GET_PROPS((heap), (h)) \ - )) -#define DUK_HOBJECT_E_GET_VALUE_BASE(heap,h) \ - ((duk_propvalue *) (void *) ( \ - DUK_HOBJECT_GET_PROPS((heap), (h)) + \ - DUK_HOBJECT_GET_ESIZE((h)) * sizeof(duk_hstring *) \ - )) -#define DUK_HOBJECT_E_GET_FLAGS_BASE(heap,h) \ - ((duk_uint8_t *) (void *) ( \ - DUK_HOBJECT_GET_PROPS((heap), (h)) + DUK_HOBJECT_GET_ESIZE((h)) * (sizeof(duk_hstring *) + sizeof(duk_propvalue)) \ - )) -#define DUK_HOBJECT_A_GET_BASE(heap,h) \ - ((duk_tval *) (void *) ( \ - DUK_HOBJECT_GET_PROPS((heap), (h)) + \ - DUK_HOBJECT_GET_ESIZE((h)) * (sizeof(duk_hstring *) + sizeof(duk_propvalue) + sizeof(duk_uint8_t)) \ - )) -#define DUK_HOBJECT_H_GET_BASE(heap,h) \ - ((duk_uint32_t *) (void *) ( \ - DUK_HOBJECT_GET_PROPS((heap), (h)) + \ - DUK_HOBJECT_GET_ESIZE((h)) * (sizeof(duk_hstring *) + sizeof(duk_propvalue) + sizeof(duk_uint8_t)) + \ - DUK_HOBJECT_GET_ASIZE((h)) * sizeof(duk_tval) \ - )) -#define DUK_HOBJECT_P_COMPUTE_SIZE(n_ent,n_arr,n_hash) \ - ( \ - (n_ent) * (sizeof(duk_hstring *) + sizeof(duk_propvalue) + sizeof(duk_uint8_t)) + \ - (n_arr) * sizeof(duk_tval) + \ - (n_hash) * sizeof(duk_uint32_t) \ - ) -#define DUK_HOBJECT_P_SET_REALLOC_PTRS(p_base,set_e_k,set_e_pv,set_e_f,set_a,set_h,n_ent,n_arr,n_hash) do { \ - (set_e_k) = (duk_hstring **) (void *) (p_base); \ - (set_e_pv) = (duk_propvalue *) (void *) ((set_e_k) + (n_ent)); \ - (set_e_f) = (duk_uint8_t *) (void *) ((set_e_pv) + (n_ent)); \ - (set_a) = (duk_tval *) (void *) ((set_e_f) + (n_ent)); \ - (set_h) = (duk_uint32_t *) (void *) ((set_a) + (n_arr)); \ - } while (0) -#elif defined(DUK_USE_HOBJECT_LAYOUT_2) -/* LAYOUT 2 */ -#if (DUK_USE_ALIGN_BY == 4) -#define DUK_HOBJECT_E_FLAG_PADDING(e_sz) ((4 - (e_sz)) & 0x03) -#elif (DUK_USE_ALIGN_BY == 8) -#define DUK_HOBJECT_E_FLAG_PADDING(e_sz) ((8 - (e_sz)) & 0x07) -#elif (DUK_USE_ALIGN_BY == 1) -#define DUK_HOBJECT_E_FLAG_PADDING(e_sz) 0 -#else -#error invalid DUK_USE_ALIGN_BY -#endif -#define DUK_HOBJECT_E_GET_KEY_BASE(heap,h) \ - ((duk_hstring **) (void *) ( \ - DUK_HOBJECT_GET_PROPS((heap), (h)) + \ - DUK_HOBJECT_GET_ESIZE((h)) * sizeof(duk_propvalue) \ - )) -#define DUK_HOBJECT_E_GET_VALUE_BASE(heap,h) \ - ((duk_propvalue *) (void *) ( \ - DUK_HOBJECT_GET_PROPS((heap), (h)) \ - )) -#define DUK_HOBJECT_E_GET_FLAGS_BASE(heap,h) \ - ((duk_uint8_t *) (void *) ( \ - DUK_HOBJECT_GET_PROPS((heap), (h)) + DUK_HOBJECT_GET_ESIZE((h)) * (sizeof(duk_hstring *) + sizeof(duk_propvalue)) \ - )) -#define DUK_HOBJECT_A_GET_BASE(heap,h) \ - ((duk_tval *) (void *) ( \ - DUK_HOBJECT_GET_PROPS((heap), (h)) + \ - DUK_HOBJECT_GET_ESIZE((h)) * (sizeof(duk_hstring *) + sizeof(duk_propvalue) + sizeof(duk_uint8_t)) + \ - DUK_HOBJECT_E_FLAG_PADDING(DUK_HOBJECT_GET_ESIZE((h))) \ - )) -#define DUK_HOBJECT_H_GET_BASE(heap,h) \ - ((duk_uint32_t *) (void *) ( \ - DUK_HOBJECT_GET_PROPS((heap), (h)) + \ - DUK_HOBJECT_GET_ESIZE((h)) * (sizeof(duk_hstring *) + sizeof(duk_propvalue) + sizeof(duk_uint8_t)) + \ - DUK_HOBJECT_E_FLAG_PADDING(DUK_HOBJECT_GET_ESIZE((h))) + \ - DUK_HOBJECT_GET_ASIZE((h)) * sizeof(duk_tval) \ - )) -#define DUK_HOBJECT_P_COMPUTE_SIZE(n_ent,n_arr,n_hash) \ - ( \ - (n_ent) * (sizeof(duk_hstring *) + sizeof(duk_propvalue) + sizeof(duk_uint8_t)) + \ - DUK_HOBJECT_E_FLAG_PADDING((n_ent)) + \ - (n_arr) * sizeof(duk_tval) + \ - (n_hash) * sizeof(duk_uint32_t) \ - ) -#define DUK_HOBJECT_P_SET_REALLOC_PTRS(p_base,set_e_k,set_e_pv,set_e_f,set_a,set_h,n_ent,n_arr,n_hash) do { \ - (set_e_pv) = (duk_propvalue *) (void *) (p_base); \ - (set_e_k) = (duk_hstring **) (void *) ((set_e_pv) + (n_ent)); \ - (set_e_f) = (duk_uint8_t *) (void *) ((set_e_k) + (n_ent)); \ - (set_a) = (duk_tval *) (void *) (((duk_uint8_t *) (set_e_f)) + \ - sizeof(duk_uint8_t) * (n_ent) + \ - DUK_HOBJECT_E_FLAG_PADDING((n_ent))); \ - (set_h) = (duk_uint32_t *) (void *) ((set_a) + (n_arr)); \ - } while (0) -#elif defined(DUK_USE_HOBJECT_LAYOUT_3) -/* LAYOUT 3 */ -#define DUK_HOBJECT_E_GET_KEY_BASE(heap,h) \ - ((duk_hstring **) (void *) ( \ - DUK_HOBJECT_GET_PROPS((heap), (h)) + \ - DUK_HOBJECT_GET_ESIZE((h)) * sizeof(duk_propvalue) + \ - DUK_HOBJECT_GET_ASIZE((h)) * sizeof(duk_tval) \ - )) -#define DUK_HOBJECT_E_GET_VALUE_BASE(heap,h) \ - ((duk_propvalue *) (void *) ( \ - DUK_HOBJECT_GET_PROPS((heap), (h)) \ - )) -#define DUK_HOBJECT_E_GET_FLAGS_BASE(heap,h) \ - ((duk_uint8_t *) (void *) ( \ - DUK_HOBJECT_GET_PROPS((heap), (h)) + \ - DUK_HOBJECT_GET_ESIZE((h)) * (sizeof(duk_propvalue) + sizeof(duk_hstring *)) + \ - DUK_HOBJECT_GET_ASIZE((h)) * sizeof(duk_tval) + \ - DUK_HOBJECT_GET_HSIZE((h)) * sizeof(duk_uint32_t) \ - )) -#define DUK_HOBJECT_A_GET_BASE(heap,h) \ - ((duk_tval *) (void *) ( \ - DUK_HOBJECT_GET_PROPS((heap), (h)) + \ - DUK_HOBJECT_GET_ESIZE((h)) * sizeof(duk_propvalue) \ - )) -#define DUK_HOBJECT_H_GET_BASE(heap,h) \ - ((duk_uint32_t *) (void *) ( \ - DUK_HOBJECT_GET_PROPS((heap), (h)) + \ - DUK_HOBJECT_GET_ESIZE((h)) * (sizeof(duk_propvalue) + sizeof(duk_hstring *)) + \ - DUK_HOBJECT_GET_ASIZE((h)) * sizeof(duk_tval) \ - )) -#define DUK_HOBJECT_P_COMPUTE_SIZE(n_ent,n_arr,n_hash) \ - ( \ - (n_ent) * (sizeof(duk_propvalue) + sizeof(duk_hstring *) + sizeof(duk_uint8_t)) + \ - (n_arr) * sizeof(duk_tval) + \ - (n_hash) * sizeof(duk_uint32_t) \ - ) -#define DUK_HOBJECT_P_SET_REALLOC_PTRS(p_base,set_e_k,set_e_pv,set_e_f,set_a,set_h,n_ent,n_arr,n_hash) do { \ - (set_e_pv) = (duk_propvalue *) (void *) (p_base); \ - (set_a) = (duk_tval *) (void *) ((set_e_pv) + (n_ent)); \ - (set_e_k) = (duk_hstring **) (void *) ((set_a) + (n_arr)); \ - (set_h) = (duk_uint32_t *) (void *) ((set_e_k) + (n_ent)); \ - (set_e_f) = (duk_uint8_t *) (void *) ((set_h) + (n_hash)); \ - } while (0) -#else -#error invalid hobject layout defines -#endif /* hobject property layout */ - -#define DUK_HOBJECT_P_ALLOC_SIZE(h) \ - DUK_HOBJECT_P_COMPUTE_SIZE(DUK_HOBJECT_GET_ESIZE((h)), DUK_HOBJECT_GET_ASIZE((h)), DUK_HOBJECT_GET_HSIZE((h))) - -#define DUK_HOBJECT_E_GET_KEY(heap,h,i) (DUK_HOBJECT_E_GET_KEY_BASE((heap), (h))[(i)]) -#define DUK_HOBJECT_E_GET_KEY_PTR(heap,h,i) (&DUK_HOBJECT_E_GET_KEY_BASE((heap), (h))[(i)]) -#define DUK_HOBJECT_E_GET_VALUE(heap,h,i) (DUK_HOBJECT_E_GET_VALUE_BASE((heap), (h))[(i)]) -#define DUK_HOBJECT_E_GET_VALUE_PTR(heap,h,i) (&DUK_HOBJECT_E_GET_VALUE_BASE((heap), (h))[(i)]) -#define DUK_HOBJECT_E_GET_VALUE_TVAL(heap,h,i) (DUK_HOBJECT_E_GET_VALUE((heap), (h), (i)).v) -#define DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(heap,h,i) (&DUK_HOBJECT_E_GET_VALUE((heap), (h), (i)).v) -#define DUK_HOBJECT_E_GET_VALUE_GETTER(heap,h,i) (DUK_HOBJECT_E_GET_VALUE((heap), (h), (i)).a.get) -#define DUK_HOBJECT_E_GET_VALUE_GETTER_PTR(heap,h,i) (&DUK_HOBJECT_E_GET_VALUE((heap), (h), (i)).a.get) -#define DUK_HOBJECT_E_GET_VALUE_SETTER(heap,h,i) (DUK_HOBJECT_E_GET_VALUE((heap), (h), (i)).a.set) -#define DUK_HOBJECT_E_GET_VALUE_SETTER_PTR(heap,h,i) (&DUK_HOBJECT_E_GET_VALUE((heap), (h), (i)).a.set) -#define DUK_HOBJECT_E_GET_FLAGS(heap,h,i) (DUK_HOBJECT_E_GET_FLAGS_BASE((heap), (h))[(i)]) -#define DUK_HOBJECT_E_GET_FLAGS_PTR(heap,h,i) (&DUK_HOBJECT_E_GET_FLAGS_BASE((heap), (h))[(i)]) -#define DUK_HOBJECT_A_GET_VALUE(heap,h,i) (DUK_HOBJECT_A_GET_BASE((heap), (h))[(i)]) -#define DUK_HOBJECT_A_GET_VALUE_PTR(heap,h,i) (&DUK_HOBJECT_A_GET_BASE((heap), (h))[(i)]) -#define DUK_HOBJECT_H_GET_INDEX(heap,h,i) (DUK_HOBJECT_H_GET_BASE((heap), (h))[(i)]) -#define DUK_HOBJECT_H_GET_INDEX_PTR(heap,h,i) (&DUK_HOBJECT_H_GET_BASE((heap), (h))[(i)]) - -#define DUK_HOBJECT_E_SET_KEY(heap,h,i,k) do { \ - DUK_HOBJECT_E_GET_KEY((heap), (h), (i)) = (k); \ - } while (0) -#define DUK_HOBJECT_E_SET_VALUE(heap,h,i,v) do { \ - DUK_HOBJECT_E_GET_VALUE((heap), (h), (i)) = (v); \ - } while (0) -#define DUK_HOBJECT_E_SET_VALUE_TVAL(heap,h,i,v) do { \ - DUK_HOBJECT_E_GET_VALUE((heap), (h), (i)).v = (v); \ - } while (0) -#define DUK_HOBJECT_E_SET_VALUE_GETTER(heap,h,i,v) do { \ - DUK_HOBJECT_E_GET_VALUE((heap), (h), (i)).a.get = (v); \ - } while (0) -#define DUK_HOBJECT_E_SET_VALUE_SETTER(heap,h,i,v) do { \ - DUK_HOBJECT_E_GET_VALUE((heap), (h), (i)).a.set = (v); \ - } while (0) -#define DUK_HOBJECT_E_SET_FLAGS(heap,h,i,f) do { \ - DUK_HOBJECT_E_GET_FLAGS((heap), (h), (i)) = (duk_uint8_t) (f); \ - } while (0) -#define DUK_HOBJECT_A_SET_VALUE(heap,h,i,v) do { \ - DUK_HOBJECT_A_GET_VALUE((heap), (h), (i)) = (v); \ - } while (0) -#define DUK_HOBJECT_A_SET_VALUE_TVAL(heap,h,i,v) \ - DUK_HOBJECT_A_SET_VALUE((heap), (h), (i), (v)) /* alias for above */ -#define DUK_HOBJECT_H_SET_INDEX(heap,h,i,v) do { \ - DUK_HOBJECT_H_GET_INDEX((heap), (h), (i)) = (v); \ - } while (0) - -#define DUK_HOBJECT_E_SET_FLAG_BITS(heap,h,i,mask) do { \ - DUK_HOBJECT_E_GET_FLAGS_BASE((heap), (h))[(i)] |= (mask); \ - } while (0) - -#define DUK_HOBJECT_E_CLEAR_FLAG_BITS(heap,h,i,mask) do { \ - DUK_HOBJECT_E_GET_FLAGS_BASE((heap), (h))[(i)] &= ~(mask); \ - } while (0) - -#define DUK_HOBJECT_E_SLOT_IS_WRITABLE(heap,h,i) ((DUK_HOBJECT_E_GET_FLAGS((heap), (h), (i)) & DUK_PROPDESC_FLAG_WRITABLE) != 0) -#define DUK_HOBJECT_E_SLOT_IS_ENUMERABLE(heap,h,i) ((DUK_HOBJECT_E_GET_FLAGS((heap), (h), (i)) & DUK_PROPDESC_FLAG_ENUMERABLE) != 0) -#define DUK_HOBJECT_E_SLOT_IS_CONFIGURABLE(heap,h,i) ((DUK_HOBJECT_E_GET_FLAGS((heap), (h), (i)) & DUK_PROPDESC_FLAG_CONFIGURABLE) != 0) -#define DUK_HOBJECT_E_SLOT_IS_ACCESSOR(heap,h,i) ((DUK_HOBJECT_E_GET_FLAGS((heap), (h), (i)) & DUK_PROPDESC_FLAG_ACCESSOR) != 0) - -#define DUK_HOBJECT_E_SLOT_SET_WRITABLE(heap,h,i) DUK_HOBJECT_E_SET_FLAG_BITS((heap), (h), (i),DUK_PROPDESC_FLAG_WRITABLE) -#define DUK_HOBJECT_E_SLOT_SET_ENUMERABLE(heap,h,i) DUK_HOBJECT_E_SET_FLAG_BITS((heap), (h), (i),DUK_PROPDESC_FLAG_ENUMERABLE) -#define DUK_HOBJECT_E_SLOT_SET_CONFIGURABLE(heap,h,i) DUK_HOBJECT_E_SET_FLAG_BITS((heap), (h), (i),DUK_PROPDESC_FLAG_CONFIGURABLE) -#define DUK_HOBJECT_E_SLOT_SET_ACCESSOR(heap,h,i) DUK_HOBJECT_E_SET_FLAG_BITS((heap), (h), (i),DUK_PROPDESC_FLAG_ACCESSOR) - -#define DUK_HOBJECT_E_SLOT_CLEAR_WRITABLE(heap,h,i) DUK_HOBJECT_E_CLEAR_FLAG_BITS((heap), (h), (i),DUK_PROPDESC_FLAG_WRITABLE) -#define DUK_HOBJECT_E_SLOT_CLEAR_ENUMERABLE(heap,h,i) DUK_HOBJECT_E_CLEAR_FLAG_BITS((heap), (h), (i),DUK_PROPDESC_FLAG_ENUMERABLE) -#define DUK_HOBJECT_E_SLOT_CLEAR_CONFIGURABLE(heap,h,i) DUK_HOBJECT_E_CLEAR_FLAG_BITS((heap), (h), (i),DUK_PROPDESC_FLAG_CONFIGURABLE) -#define DUK_HOBJECT_E_SLOT_CLEAR_ACCESSOR(heap,h,i) DUK_HOBJECT_E_CLEAR_FLAG_BITS((heap), (h), (i),DUK_PROPDESC_FLAG_ACCESSOR) - -#define DUK_PROPDESC_IS_WRITABLE(p) (((p)->flags & DUK_PROPDESC_FLAG_WRITABLE) != 0) -#define DUK_PROPDESC_IS_ENUMERABLE(p) (((p)->flags & DUK_PROPDESC_FLAG_ENUMERABLE) != 0) -#define DUK_PROPDESC_IS_CONFIGURABLE(p) (((p)->flags & DUK_PROPDESC_FLAG_CONFIGURABLE) != 0) -#define DUK_PROPDESC_IS_ACCESSOR(p) (((p)->flags & DUK_PROPDESC_FLAG_ACCESSOR) != 0) - -#define DUK_HOBJECT_HASHIDX_UNUSED 0xffffffffUL -#define DUK_HOBJECT_HASHIDX_DELETED 0xfffffffeUL - -/* - * Macros for accessing size fields - */ - -#if defined(DUK_USE_OBJSIZES16) -#define DUK_HOBJECT_GET_ESIZE(h) ((h)->e_size16) -#define DUK_HOBJECT_SET_ESIZE(h,v) do { (h)->e_size16 = (v); } while (0) -#define DUK_HOBJECT_GET_ENEXT(h) ((h)->e_next16) -#define DUK_HOBJECT_SET_ENEXT(h,v) do { (h)->e_next16 = (v); } while (0) -#define DUK_HOBJECT_POSTINC_ENEXT(h) ((h)->e_next16++) -#define DUK_HOBJECT_GET_ASIZE(h) ((h)->a_size16) -#define DUK_HOBJECT_SET_ASIZE(h,v) do { (h)->a_size16 = (v); } while (0) -#if defined(DUK_USE_HOBJECT_HASH_PART) -#define DUK_HOBJECT_GET_HSIZE(h) ((h)->h_size16) -#define DUK_HOBJECT_SET_HSIZE(h,v) do { (h)->h_size16 = (v); } while (0) -#else -#define DUK_HOBJECT_GET_HSIZE(h) 0 -#define DUK_HOBJECT_SET_HSIZE(h,v) do { DUK_ASSERT((v) == 0); } while (0) -#endif -#else -#define DUK_HOBJECT_GET_ESIZE(h) ((h)->e_size) -#define DUK_HOBJECT_SET_ESIZE(h,v) do { (h)->e_size = (v); } while (0) -#define DUK_HOBJECT_GET_ENEXT(h) ((h)->e_next) -#define DUK_HOBJECT_SET_ENEXT(h,v) do { (h)->e_next = (v); } while (0) -#define DUK_HOBJECT_POSTINC_ENEXT(h) ((h)->e_next++) -#define DUK_HOBJECT_GET_ASIZE(h) ((h)->a_size) -#define DUK_HOBJECT_SET_ASIZE(h,v) do { (h)->a_size = (v); } while (0) -#if defined(DUK_USE_HOBJECT_HASH_PART) -#define DUK_HOBJECT_GET_HSIZE(h) ((h)->h_size) -#define DUK_HOBJECT_SET_HSIZE(h,v) do { (h)->h_size = (v); } while (0) -#else -#define DUK_HOBJECT_GET_HSIZE(h) 0 -#define DUK_HOBJECT_SET_HSIZE(h,v) do { DUK_ASSERT((v) == 0); } while (0) -#endif -#endif - -/* - * Misc - */ - -/* Maximum prototype traversal depth. Sanity limit which handles e.g. - * prototype loops (even complex ones like 1->2->3->4->2->3->4->2->3->4). - */ -#define DUK_HOBJECT_PROTOTYPE_CHAIN_SANITY 10000L - -/* Maximum traversal depth for "bound function" chains. */ -#define DUK_HOBJECT_BOUND_CHAIN_SANITY 10000L - -/* - * Ecmascript [[Class]] - */ - -/* range check not necessary because all 4-bit values are mapped */ -#define DUK_HOBJECT_CLASS_NUMBER_TO_STRIDX(n) duk_class_number_to_stridx[(n)] - -#define DUK_HOBJECT_GET_CLASS_STRING(heap,h) \ - DUK_HEAP_GET_STRING( \ - (heap), \ - DUK_HOBJECT_CLASS_NUMBER_TO_STRIDX(DUK_HOBJECT_GET_CLASS_NUMBER((h))) \ - ) - -/* - * Macros for property handling - */ - -#if defined(DUK_USE_HEAPPTR16) -#define DUK_HOBJECT_GET_PROTOTYPE(heap,h) \ - ((duk_hobject *) DUK_USE_HEAPPTR_DEC16((heap)->heap_udata, (h)->prototype16)) -#define DUK_HOBJECT_SET_PROTOTYPE(heap,h,x) do { \ - (h)->prototype16 = DUK_USE_HEAPPTR_ENC16((heap)->heap_udata, (void *) (x)); \ - } while (0) -#else -#define DUK_HOBJECT_GET_PROTOTYPE(heap,h) \ - ((h)->prototype) -#define DUK_HOBJECT_SET_PROTOTYPE(heap,h,x) do { \ - (h)->prototype = (x); \ - } while (0) -#endif - -/* note: this updates refcounts */ -#define DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr,h,p) duk_hobject_set_prototype_updref((thr), (h), (p)) - -/* - * Resizing and hash behavior - */ - -/* Sanity limit on max number of properties (allocated, not necessarily used). - * This is somewhat arbitrary, but if we're close to 2**32 properties some - * algorithms will fail (e.g. hash size selection, next prime selection). - * Also, we use negative array/entry table indices to indicate 'not found', - * so anything above 0x80000000 will cause trouble now. - */ -#if defined(DUK_USE_OBJSIZES16) -#define DUK_HOBJECT_MAX_PROPERTIES 0x0000ffffUL -#else -#define DUK_HOBJECT_MAX_PROPERTIES 0x7fffffffUL /* 2**31-1 ~= 2G properties */ -#endif - -/* higher value conserves memory; also note that linear scan is cache friendly */ -#define DUK_HOBJECT_E_USE_HASH_LIMIT 32 - -/* hash size relative to entries size: for value X, approx. hash_prime(e_size + e_size / X) */ -#define DUK_HOBJECT_H_SIZE_DIVISOR 4 /* hash size approx. 1.25 times entries size */ - -/* if new_size < L * old_size, resize without abandon check; L = 3-bit fixed point, e.g. 9 -> 9/8 = 112.5% */ -#define DUK_HOBJECT_A_FAST_RESIZE_LIMIT 9 /* 112.5%, i.e. new size less than 12.5% higher -> fast resize */ - -/* if density < L, abandon array part, L = 3-bit fixed point, e.g. 2 -> 2/8 = 25% */ -/* limit is quite low: one array entry is 8 bytes, one normal entry is 4+1+8+4 = 17 bytes (with hash entry) */ -#define DUK_HOBJECT_A_ABANDON_LIMIT 2 /* 25%, i.e. less than 25% used -> abandon */ - -/* internal align target for props allocation, must be 2*n for some n */ -#if (DUK_USE_ALIGN_BY == 4) -#define DUK_HOBJECT_ALIGN_TARGET 4 -#elif (DUK_USE_ALIGN_BY == 8) -#define DUK_HOBJECT_ALIGN_TARGET 8 -#elif (DUK_USE_ALIGN_BY == 1) -#define DUK_HOBJECT_ALIGN_TARGET 1 -#else -#error invalid DUK_USE_ALIGN_BY -#endif - -/* controls for minimum entry part growth */ -#define DUK_HOBJECT_E_MIN_GROW_ADD 16 -#define DUK_HOBJECT_E_MIN_GROW_DIVISOR 8 /* 2^3 -> 1/8 = 12.5% min growth */ - -/* controls for minimum array part growth */ -#define DUK_HOBJECT_A_MIN_GROW_ADD 16 -#define DUK_HOBJECT_A_MIN_GROW_DIVISOR 8 /* 2^3 -> 1/8 = 12.5% min growth */ - -/* probe sequence */ -#define DUK_HOBJECT_HASH_INITIAL(hash,h_size) ((hash) % (h_size)) -#define DUK_HOBJECT_HASH_PROBE_STEP(hash) DUK_UTIL_GET_HASH_PROBE_STEP((hash)) - -/* - * PC-to-line constants - */ - -#define DUK_PC2LINE_SKIP 64 - -/* maximum length for a SKIP-1 diffstream: 35 bits per entry, rounded up to bytes */ -#define DUK_PC2LINE_MAX_DIFF_LENGTH (((DUK_PC2LINE_SKIP - 1) * 35 + 7) / 8) - -/* - * Struct defs - */ - -struct duk_propaccessor { - duk_hobject *get; - duk_hobject *set; -}; - -union duk_propvalue { - /* The get/set pointers could be 16-bit pointer compressed but it - * would make no difference on 32-bit platforms because duk_tval is - * 8 bytes or more anyway. - */ - duk_tval v; - duk_propaccessor a; -}; - -struct duk_propdesc { - /* read-only values 'lifted' for ease of use */ - duk_small_int_t flags; - duk_hobject *get; - duk_hobject *set; - - /* for updating (all are set to < 0 for virtual properties) */ - duk_int_t e_idx; /* prop index in 'entry part', < 0 if not there */ - duk_int_t h_idx; /* prop index in 'hash part', < 0 if not there */ - duk_int_t a_idx; /* prop index in 'array part', < 0 if not there */ -}; - -struct duk_hobject { - duk_heaphdr hdr; - - /* - * 'props' contains {key,value,flags} entries, optional array entries, and - * an optional hash lookup table for non-array entries in a single 'sliced' - * allocation. There are several layout options, which differ slightly in - * generated code size/speed and alignment/padding; duk_features.h selects - * the layout used. - * - * Layout 1 (DUK_USE_HOBJECT_LAYOUT_1): - * - * e_size * sizeof(duk_hstring *) bytes of entry keys (e_next gc reachable) - * e_size * sizeof(duk_propvalue) bytes of entry values (e_next gc reachable) - * e_size * sizeof(duk_uint8_t) bytes of entry flags (e_next gc reachable) - * a_size * sizeof(duk_tval) bytes of (opt) array values (plain only) (all gc reachable) - * h_size * sizeof(duk_uint32_t) bytes of (opt) hash indexes to entries (e_size), - * 0xffffffffUL = unused, 0xfffffffeUL = deleted - * - * Layout 2 (DUK_USE_HOBJECT_LAYOUT_2): - * - * e_size * sizeof(duk_propvalue) bytes of entry values (e_next gc reachable) - * e_size * sizeof(duk_hstring *) bytes of entry keys (e_next gc reachable) - * e_size * sizeof(duk_uint8_t) + pad bytes of entry flags (e_next gc reachable) - * a_size * sizeof(duk_tval) bytes of (opt) array values (plain only) (all gc reachable) - * h_size * sizeof(duk_uint32_t) bytes of (opt) hash indexes to entries (e_size), - * 0xffffffffUL = unused, 0xfffffffeUL = deleted - * - * Layout 3 (DUK_USE_HOBJECT_LAYOUT_3): - * - * e_size * sizeof(duk_propvalue) bytes of entry values (e_next gc reachable) - * a_size * sizeof(duk_tval) bytes of (opt) array values (plain only) (all gc reachable) - * e_size * sizeof(duk_hstring *) bytes of entry keys (e_next gc reachable) - * h_size * sizeof(duk_uint32_t) bytes of (opt) hash indexes to entries (e_size), - * 0xffffffffUL = unused, 0xfffffffeUL = deleted - * e_size * sizeof(duk_uint8_t) bytes of entry flags (e_next gc reachable) - * - * In layout 1, the 'e_next' count is rounded to 4 or 8 on platforms - * requiring 4 or 8 byte alignment. This ensures proper alignment - * for the entries, at the cost of memory footprint. However, it's - * probably preferable to use another layout on such platforms instead. - * - * In layout 2, the key and value parts are swapped to avoid padding - * the key array on platforms requiring alignment by 8. The flags part - * is padded to get alignment for array entries. The 'e_next' count does - * not need to be rounded as in layout 1. - * - * In layout 3, entry values and array values are always aligned properly, - * and assuming pointers are at most 8 bytes, so are the entry keys. Hash - * indices will be properly aligned (assuming pointers are at least 4 bytes). - * Finally, flags don't need additional alignment. This layout provides - * compact allocations without padding (even on platforms with alignment - * requirements) at the cost of a bit slower lookups. - * - * Objects with few keys don't have a hash index; keys are looked up linearly, - * which is cache efficient because the keys are consecutive. Larger objects - * have a hash index part which contains integer indexes to the entries part. - * - * A single allocation reduces memory allocation overhead but requires more - * work when any part needs to be resized. A sliced allocation for entries - * makes linear key matching faster on most platforms (more locality) and - * skimps on flags size (which would be followed by 3 bytes of padding in - * most architectures if entries were placed in a struct). - * - * 'props' also contains internal properties distinguished with a non-BMP - * prefix. Often used properties should be placed early in 'props' whenever - * possible to make accessing them as fast a possible. - */ - -#if defined(DUK_USE_HEAPPTR16) - /* Located in duk_heaphdr h_extra16. Subclasses of duk_hobject (like - * duk_hcompiledfunction) are not free to use h_extra16 for this reason. - */ -#else - duk_uint8_t *props; -#endif - - /* prototype: the only internal property lifted outside 'e' as it is so central */ -#if defined(DUK_USE_HEAPPTR16) - duk_uint16_t prototype16; -#else - duk_hobject *prototype; -#endif - -#if defined(DUK_USE_OBJSIZES16) - duk_uint16_t e_size16; - duk_uint16_t e_next16; - duk_uint16_t a_size16; -#if defined(DUK_USE_HOBJECT_HASH_PART) - duk_uint16_t h_size16; -#endif -#else - duk_uint32_t e_size; /* entry part size */ - duk_uint32_t e_next; /* index for next new key ([0,e_next[ are gc reachable) */ - duk_uint32_t a_size; /* array part size (entirely gc reachable) */ -#if defined(DUK_USE_HOBJECT_HASH_PART) - duk_uint32_t h_size; /* hash part size or 0 if unused */ -#endif -#endif -}; - -/* - * Exposed data - */ - -#if !defined(DUK_SINGLE_FILE) -DUK_INTERNAL_DECL duk_uint8_t duk_class_number_to_stridx[32]; -#endif /* !DUK_SINGLE_FILE */ - -/* - * Prototypes - */ - -/* alloc and init */ -DUK_INTERNAL_DECL duk_hobject *duk_hobject_alloc(duk_heap *heap, duk_uint_t hobject_flags); -#if 0 /* unused */ -DUK_INTERNAL_DECL duk_hobject *duk_hobject_alloc_checked(duk_hthread *thr, duk_uint_t hobject_flags); -#endif -DUK_INTERNAL_DECL duk_hcompiledfunction *duk_hcompiledfunction_alloc(duk_heap *heap, duk_uint_t hobject_flags); -DUK_INTERNAL_DECL duk_hnativefunction *duk_hnativefunction_alloc(duk_heap *heap, duk_uint_t hobject_flags); -DUK_INTERNAL duk_hbufferobject *duk_hbufferobject_alloc(duk_heap *heap, duk_uint_t hobject_flags); -DUK_INTERNAL_DECL duk_hthread *duk_hthread_alloc(duk_heap *heap, duk_uint_t hobject_flags); - -/* low-level property functions */ -DUK_INTERNAL_DECL void duk_hobject_find_existing_entry(duk_heap *heap, duk_hobject *obj, duk_hstring *key, duk_int_t *e_idx, duk_int_t *h_idx); -DUK_INTERNAL_DECL duk_tval *duk_hobject_find_existing_entry_tval_ptr(duk_heap *heap, duk_hobject *obj, duk_hstring *key); -DUK_INTERNAL_DECL duk_tval *duk_hobject_find_existing_entry_tval_ptr_and_attrs(duk_heap *heap, duk_hobject *obj, duk_hstring *key, duk_int_t *out_attrs); -DUK_INTERNAL_DECL duk_tval *duk_hobject_find_existing_array_entry_tval_ptr(duk_heap *heap, duk_hobject *obj, duk_uarridx_t i); -DUK_INTERNAL_DECL duk_bool_t duk_hobject_get_own_propdesc(duk_hthread *thr, duk_hobject *obj, duk_hstring *key, duk_propdesc *out_desc, duk_small_uint_t flags); - -/* XXX: when optimizing for guaranteed property slots, use a guaranteed - * slot for internal value; this call can then access it directly. - */ -#define duk_hobject_get_internal_value_tval_ptr(heap,obj) \ - duk_hobject_find_existing_entry_tval_ptr((heap), (obj), DUK_HEAP_STRING_INT_VALUE((heap))) - -/* core property functions */ -DUK_INTERNAL_DECL duk_bool_t duk_hobject_getprop(duk_hthread *thr, duk_tval *tv_obj, duk_tval *tv_key); -DUK_INTERNAL_DECL duk_bool_t duk_hobject_putprop(duk_hthread *thr, duk_tval *tv_obj, duk_tval *tv_key, duk_tval *tv_val, duk_bool_t throw_flag); -DUK_INTERNAL_DECL duk_bool_t duk_hobject_delprop(duk_hthread *thr, duk_tval *tv_obj, duk_tval *tv_key, duk_bool_t throw_flag); -DUK_INTERNAL_DECL duk_bool_t duk_hobject_hasprop(duk_hthread *thr, duk_tval *tv_obj, duk_tval *tv_key); - -/* internal property functions */ -#define DUK_DELPROP_FLAG_THROW (1 << 0) -#define DUK_DELPROP_FLAG_FORCE (1 << 1) -DUK_INTERNAL_DECL duk_bool_t duk_hobject_delprop_raw(duk_hthread *thr, duk_hobject *obj, duk_hstring *key, duk_small_uint_t flags); -DUK_INTERNAL_DECL duk_bool_t duk_hobject_hasprop_raw(duk_hthread *thr, duk_hobject *obj, duk_hstring *key); -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 void duk_hobject_define_property_internal_arridx(duk_hthread *thr, duk_hobject *obj, duk_uarridx_t arr_idx, duk_small_uint_t flags); -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); -DUK_INTERNAL_DECL void duk_hobject_set_length(duk_hthread *thr, duk_hobject *obj, duk_uint32_t length); /* XXX: duk_uarridx_t? */ -DUK_INTERNAL_DECL void duk_hobject_set_length_zero(duk_hthread *thr, duk_hobject *obj); -DUK_INTERNAL_DECL duk_uint32_t duk_hobject_get_length(duk_hthread *thr, duk_hobject *obj); /* XXX: duk_uarridx_t? */ - -/* helpers for defineProperty() and defineProperties() */ -DUK_INTERNAL_DECL -void duk_hobject_prepare_property_descriptor(duk_context *ctx, - duk_idx_t idx_in, - duk_uint_t *out_defprop_flags, - duk_idx_t *out_idx_value, - duk_hobject **out_getter, - duk_hobject **out_setter); -DUK_INTERNAL_DECL -void duk_hobject_define_property_helper(duk_context *ctx, - duk_uint_t defprop_flags, - duk_hobject *obj, - duk_hstring *key, - duk_idx_t idx_value, - duk_hobject *get, - duk_hobject *set); - -/* Object built-in methods */ -DUK_INTERNAL_DECL duk_ret_t duk_hobject_object_get_own_property_descriptor(duk_context *ctx); -DUK_INTERNAL_DECL void duk_hobject_object_seal_freeze_helper(duk_hthread *thr, duk_hobject *obj, duk_bool_t is_freeze); -DUK_INTERNAL_DECL duk_bool_t duk_hobject_object_is_sealed_frozen_helper(duk_hthread *thr, duk_hobject *obj, duk_bool_t is_frozen); -DUK_INTERNAL_DECL duk_bool_t duk_hobject_object_ownprop_helper(duk_context *ctx, duk_small_uint_t required_desc_flags); - -/* internal properties */ -DUK_INTERNAL_DECL duk_bool_t duk_hobject_get_internal_value(duk_heap *heap, duk_hobject *obj, duk_tval *tv); -DUK_INTERNAL_DECL duk_hstring *duk_hobject_get_internal_value_string(duk_heap *heap, duk_hobject *obj); - -/* hobject management functions */ -DUK_INTERNAL_DECL void duk_hobject_compact_props(duk_hthread *thr, duk_hobject *obj); - -/* ES6 proxy */ -#if defined(DUK_USE_ES6_PROXY) -DUK_INTERNAL_DECL duk_bool_t duk_hobject_proxy_check(duk_hthread *thr, duk_hobject *obj, duk_hobject **out_target, duk_hobject **out_handler); -DUK_INTERNAL_DECL duk_hobject *duk_hobject_resolve_proxy_target(duk_hthread *thr, duk_hobject *obj); -#endif - -/* enumeration */ -DUK_INTERNAL_DECL void duk_hobject_enumerator_create(duk_context *ctx, duk_small_uint_t enum_flags); -DUK_INTERNAL_DECL duk_ret_t duk_hobject_get_enumerated_keys(duk_context *ctx, duk_small_uint_t enum_flags); -DUK_INTERNAL_DECL duk_bool_t duk_hobject_enumerator_next(duk_context *ctx, duk_bool_t get_value); - -/* macros */ -DUK_INTERNAL_DECL void duk_hobject_set_prototype_updref(duk_hthread *thr, duk_hobject *h, duk_hobject *p); - -/* finalization */ -DUK_INTERNAL_DECL void duk_hobject_run_finalizer(duk_hthread *thr, duk_hobject *obj); - -/* pc2line */ -#if defined(DUK_USE_PC2LINE) -DUK_INTERNAL_DECL void duk_hobject_pc2line_pack(duk_hthread *thr, duk_compiler_instr *instrs, duk_uint_fast32_t length); -DUK_INTERNAL_DECL duk_uint_fast32_t duk_hobject_pc2line_query(duk_context *ctx, duk_idx_t idx_func, duk_uint_fast32_t pc); -#endif - -/* misc */ -DUK_INTERNAL_DECL duk_bool_t duk_hobject_prototype_chain_contains(duk_hthread *thr, duk_hobject *h, duk_hobject *p, duk_bool_t ignore_loop); - -#endif /* DUK_HOBJECT_H_INCLUDED */ -#line 1 "duk_hcompiledfunction.h" -/* - * Heap compiled function (Ecmascript function) representation. - * - * There is a single data buffer containing the Ecmascript function's - * bytecode, constants, and inner functions. - */ - -#ifndef DUK_HCOMPILEDFUNCTION_H_INCLUDED -#define DUK_HCOMPILEDFUNCTION_H_INCLUDED - -/* - * Field accessor macros - */ - -/* XXX: casts could be improved, especially for GET/SET DATA */ - -#if defined(DUK_USE_HEAPPTR16) -#define DUK_HCOMPILEDFUNCTION_GET_DATA(heap,h) \ - ((duk_hbuffer_fixed *) (void *) DUK_USE_HEAPPTR_DEC16((heap)->heap_udata, (h)->data16)) -#define DUK_HCOMPILEDFUNCTION_SET_DATA(heap,h,v) do { \ - (h)->data16 = DUK_USE_HEAPPTR_ENC16((heap)->heap_udata, (void *) (v)); \ - } while (0) -#define DUK_HCOMPILEDFUNCTION_GET_FUNCS(heap,h) \ - ((duk_hobject **) (void *) (DUK_USE_HEAPPTR_DEC16((heap)->heap_udata, (h)->funcs16))) -#define DUK_HCOMPILEDFUNCTION_SET_FUNCS(heap,h,v) do { \ - (h)->funcs16 = DUK_USE_HEAPPTR_ENC16((heap)->heap_udata, (void *) (v)); \ - } while (0) -#define DUK_HCOMPILEDFUNCTION_GET_BYTECODE(heap,h) \ - ((duk_instr_t *) (void *) (DUK_USE_HEAPPTR_DEC16((heap)->heap_udata, (h)->bytecode16))) -#define DUK_HCOMPILEDFUNCTION_SET_BYTECODE(heap,h,v) do { \ - (h)->bytecode16 = DUK_USE_HEAPPTR_ENC16((heap)->heap_udata, (void *) (v)); \ - } while (0) -#else -#define DUK_HCOMPILEDFUNCTION_GET_DATA(heap,h) \ - ((duk_hbuffer_fixed *) (void *) (h)->data) -#define DUK_HCOMPILEDFUNCTION_SET_DATA(heap,h,v) do { \ - (h)->data = (duk_hbuffer *) (v); \ - } while (0) -#define DUK_HCOMPILEDFUNCTION_GET_FUNCS(heap,h) \ - ((h)->funcs) -#define DUK_HCOMPILEDFUNCTION_SET_FUNCS(heap,h,v) do { \ - (h)->funcs = (v); \ - } while (0) -#define DUK_HCOMPILEDFUNCTION_GET_BYTECODE(heap,h) \ - ((h)->bytecode) -#define DUK_HCOMPILEDFUNCTION_SET_BYTECODE(heap,h,v) do { \ - (h)->bytecode = (v); \ - } while (0) -#endif - -/* - * Accessor macros for function specific data areas - */ - -/* Note: assumes 'data' is always a fixed buffer */ -#define DUK_HCOMPILEDFUNCTION_GET_BUFFER_BASE(heap,h) \ - DUK_HBUFFER_FIXED_GET_DATA_PTR((heap), DUK_HCOMPILEDFUNCTION_GET_DATA((heap), (h))) - -#define DUK_HCOMPILEDFUNCTION_GET_CONSTS_BASE(heap,h) \ - ((duk_tval *) (void *) DUK_HCOMPILEDFUNCTION_GET_BUFFER_BASE((heap), (h))) - -#define DUK_HCOMPILEDFUNCTION_GET_FUNCS_BASE(heap,h) \ - DUK_HCOMPILEDFUNCTION_GET_FUNCS((heap), (h)) - -#define DUK_HCOMPILEDFUNCTION_GET_CODE_BASE(heap,h) \ - DUK_HCOMPILEDFUNCTION_GET_BYTECODE((heap), (h)) - -#define DUK_HCOMPILEDFUNCTION_GET_CONSTS_END(heap,h) \ - ((duk_tval *) (void *) DUK_HCOMPILEDFUNCTION_GET_FUNCS((heap), (h))) - -#define DUK_HCOMPILEDFUNCTION_GET_FUNCS_END(heap,h) \ - ((duk_hobject **) (void *) DUK_HCOMPILEDFUNCTION_GET_BYTECODE((heap), (h))) - -/* XXX: double evaluation of DUK_HCOMPILEDFUNCTION_GET_DATA() */ -#define DUK_HCOMPILEDFUNCTION_GET_CODE_END(heap,h) \ - ((duk_instr_t *) (void *) (DUK_HBUFFER_FIXED_GET_DATA_PTR((heap), DUK_HCOMPILEDFUNCTION_GET_DATA((heap), (h))) + \ - DUK_HBUFFER_GET_SIZE((duk_hbuffer *) DUK_HCOMPILEDFUNCTION_GET_DATA((heap), h)))) - -#define DUK_HCOMPILEDFUNCTION_GET_CONSTS_SIZE(heap,h) \ - ( \ - (duk_size_t) \ - ( \ - ((const duk_uint8_t *) DUK_HCOMPILEDFUNCTION_GET_CONSTS_END((heap), (h))) - \ - ((const duk_uint8_t *) DUK_HCOMPILEDFUNCTION_GET_CONSTS_BASE((heap), (h))) \ - ) \ - ) - -#define DUK_HCOMPILEDFUNCTION_GET_FUNCS_SIZE(heap,h) \ - ( \ - (duk_size_t) \ - ( \ - ((const duk_uint8_t *) DUK_HCOMPILEDFUNCTION_GET_FUNCS_END((heap), (h))) - \ - ((const duk_uint8_t *) DUK_HCOMPILEDFUNCTION_GET_FUNCS_BASE((heap), (h))) \ - ) \ - ) - -#define DUK_HCOMPILEDFUNCTION_GET_CODE_SIZE(heap,h) \ - ( \ - (duk_size_t) \ - ( \ - ((const duk_uint8_t *) DUK_HCOMPILEDFUNCTION_GET_CODE_END((heap),(h))) - \ - ((const duk_uint8_t *) DUK_HCOMPILEDFUNCTION_GET_CODE_BASE((heap),(h))) \ - ) \ - ) - -#define DUK_HCOMPILEDFUNCTION_GET_CONSTS_COUNT(heap,h) \ - ((duk_size_t) (DUK_HCOMPILEDFUNCTION_GET_CONSTS_SIZE((heap), (h)) / sizeof(duk_tval))) - -#define DUK_HCOMPILEDFUNCTION_GET_FUNCS_COUNT(heap,h) \ - ((duk_size_t) (DUK_HCOMPILEDFUNCTION_GET_FUNCS_SIZE((heap), (h)) / sizeof(duk_hobject *))) - -#define DUK_HCOMPILEDFUNCTION_GET_CODE_COUNT(heap,h) \ - ((duk_size_t) (DUK_HCOMPILEDFUNCTION_GET_CODE_SIZE((heap), (h)) / sizeof(duk_instr_t))) - - -/* - * Main struct - */ - -struct duk_hcompiledfunction { - /* shared object part */ - duk_hobject obj; - - /* - * Pointers to function data area for faster access. Function - * data is a buffer shared between all closures of the same - * "template" function. The data buffer is always fixed (non- - * dynamic, hence stable), with a layout as follows: - * - * constants (duk_tval) - * inner functions (duk_hobject *) - * bytecode (duk_instr_t) - * - * Note: bytecode end address can be computed from 'data' buffer - * size. It is not strictly necessary functionally, assuming - * bytecode never jumps outside its allocated area. However, - * it's a safety/robustness feature for avoiding the chance of - * executing random data as bytecode due to a compiler error. - * - * Note: values in the data buffer must be incref'd (they will - * be decref'd on release) for every compiledfunction referring - * to the 'data' element. - */ - - /* Data area, fixed allocation, stable data ptrs. */ -#if defined(DUK_USE_HEAPPTR16) - duk_uint16_t data16; -#else - duk_hbuffer *data; -#endif - - /* No need for constants pointer (= same as data). - * - * When using 16-bit packing alignment to 4 is nice. 'funcs' will be - * 4-byte aligned because 'constants' are duk_tvals. For now the - * inner function pointers are not compressed, so that 'bytecode' will - * also be 4-byte aligned. - */ -#if defined(DUK_USE_HEAPPTR16) - duk_uint16_t funcs16; - duk_uint16_t bytecode16; -#else - duk_hobject **funcs; - duk_instr_t *bytecode; -#endif - - /* - * 'nregs' registers are allocated on function entry, at most 'nargs' - * are initialized to arguments, and the rest to undefined. Arguments - * above 'nregs' are not mapped to registers. All registers in the - * active stack range must be initialized because they are GC reachable. - * 'nargs' is needed so that if the function is given more than 'nargs' - * arguments, the additional arguments do not 'clobber' registers - * beyond 'nregs' which must be consistently initialized to undefined. - * - * Usually there is no need to know which registers are mapped to - * local variables. Registers may be allocated to variable in any - * way (even including gaps). However, a register-variable mapping - * must be the same for the duration of the function execution and - * the register cannot be used for anything else. - * - * When looking up variables by name, the '_Varmap' map is used. - * When an activation closes, registers mapped to arguments are - * copied into the environment record based on the same map. The - * reverse map (from register to variable) is not currently needed - * at run time, except for debugging, so it is not maintained. - */ - - duk_uint16_t nregs; /* regs to allocate */ - duk_uint16_t nargs; /* number of arguments allocated to regs */ - - /* - * Additional control information is placed into the object itself - * as internal properties to avoid unnecessary fields for the - * majority of functions. The compiler tries to omit internal - * control fields when possible. - * - * Function templates: - * - * { - * name: "func", // declaration, named function expressions - * fileName: - * _Varmap: { "arg1": 0, "arg2": 1, "varname": 2 }, - * _Formals: [ "arg1", "arg2" ], - * _Source: "function func(arg1, arg2) { ... }", - * _Pc2line: , - * } - * - * Function instances: - * - * { - * length: 2, - * prototype: { constructor: }, - * caller: , - * arguments: , - * name: "func", // declaration, named function expressions - * fileName: - * _Varmap: { "arg1": 0, "arg2": 1, "varname": 2 }, - * _Formals: [ "arg1", "arg2" ], - * _Source: "function func(arg1, arg2) { ... }", - * _Pc2line: , - * _Varenv: , - * _Lexenv: - * } - * - * More detailed description of these properties can be found - * in the documentation. - */ - -#if defined(DUK_USE_DEBUGGER_SUPPORT) - /* Line number range for function. Needed during debugging to - * determine active breakpoints. - */ - duk_uint32_t start_line; - duk_uint32_t end_line; -#endif -}; - -#endif /* DUK_HCOMPILEDFUNCTION_H_INCLUDED */ -#line 1 "duk_hnativefunction.h" -/* - * Heap native function representation. - */ - -#ifndef DUK_HNATIVEFUNCTION_H_INCLUDED -#define DUK_HNATIVEFUNCTION_H_INCLUDED - -#define DUK_HNATIVEFUNCTION_NARGS_VARARGS ((duk_int16_t) -1) -#define DUK_HNATIVEFUNCTION_NARGS_MAX ((duk_int16_t) 0x7fff) - -struct duk_hnativefunction { - /* shared object part */ - duk_hobject obj; - - duk_c_function func; - duk_int16_t nargs; - duk_int16_t magic; - - /* The 'magic' field allows an opaque 16-bit field to be accessed by the - * Duktape/C function. This allows, for instance, the same native function - * to be used for a set of very similar functions, with the 'magic' field - * providing the necessary non-argument flags / values to guide the behavior - * of the native function. The value is signed on purpose: it is easier to - * convert a signed value to unsigned (simply AND with 0xffff) than vice - * versa. - * - * Note: cannot place nargs/magic into the heaphdr flags, because - * duk_hobject takes almost all flags already (and needs the spare). - */ -}; - -#endif /* DUK_HNATIVEFUNCTION_H_INCLUDED */ -#line 1 "duk_hbufferobject.h" -/* - * Heap Buffer object representation. Used for all Buffer variants. - */ - -#ifndef DUK_HBUFFEROBJECT_H_INCLUDED -#define DUK_HBUFFEROBJECT_H_INCLUDED - -/* All element accessors are host endian now (driven by TypedArray spec). */ -#define DUK_HBUFFEROBJECT_ELEM_UINT8 0 -#define DUK_HBUFFEROBJECT_ELEM_UINT8CLAMPED 1 -#define DUK_HBUFFEROBJECT_ELEM_INT8 2 -#define DUK_HBUFFEROBJECT_ELEM_UINT16 3 -#define DUK_HBUFFEROBJECT_ELEM_INT16 4 -#define DUK_HBUFFEROBJECT_ELEM_UINT32 5 -#define DUK_HBUFFEROBJECT_ELEM_INT32 6 -#define DUK_HBUFFEROBJECT_ELEM_FLOAT32 7 -#define DUK_HBUFFEROBJECT_ELEM_FLOAT64 8 -#define DUK_HBUFFEROBJECT_ELEM_MAX 8 - -#define DUK_ASSERT_HBUFFEROBJECT_VALID(h) do { \ - DUK_ASSERT((h) != NULL); \ - DUK_ASSERT((h)->shift <= 3); \ - DUK_ASSERT((h)->elem_type <= DUK_HBUFFEROBJECT_ELEM_MAX); \ - DUK_ASSERT(((h)->shift == 0 && (h)->elem_type == DUK_HBUFFEROBJECT_ELEM_UINT8) || \ - ((h)->shift == 0 && (h)->elem_type == DUK_HBUFFEROBJECT_ELEM_UINT8CLAMPED) || \ - ((h)->shift == 0 && (h)->elem_type == DUK_HBUFFEROBJECT_ELEM_INT8) || \ - ((h)->shift == 1 && (h)->elem_type == DUK_HBUFFEROBJECT_ELEM_UINT16) || \ - ((h)->shift == 1 && (h)->elem_type == DUK_HBUFFEROBJECT_ELEM_INT16) || \ - ((h)->shift == 2 && (h)->elem_type == DUK_HBUFFEROBJECT_ELEM_UINT32) || \ - ((h)->shift == 2 && (h)->elem_type == DUK_HBUFFEROBJECT_ELEM_INT32) || \ - ((h)->shift == 2 && (h)->elem_type == DUK_HBUFFEROBJECT_ELEM_FLOAT32) || \ - ((h)->shift == 3 && (h)->elem_type == DUK_HBUFFEROBJECT_ELEM_FLOAT64)); \ - DUK_ASSERT((h)->is_view == 0 || (h)->is_view == 1); \ - DUK_ASSERT(DUK_HOBJECT_IS_BUFFEROBJECT((duk_hobject *) (h))); \ - if ((h)->buf == NULL) { \ - DUK_ASSERT((h)->offset == 0); \ - DUK_ASSERT((h)->length == 0); \ - } else { \ - /* No assertions for offset or length; in particular, \ - * it's OK for length to be longer than underlying \ - * buffer. Just ensure they don't wrap when added. \ - */ \ - DUK_ASSERT((h)->offset + (h)->length >= (h)->offset); \ - } \ - } while (0) - -/* Get the current data pointer (caller must ensure buf != NULL) as a - * duk_uint8_t ptr. - */ -#define DUK_HBUFFEROBJECT_GET_SLICE_BASE(heap,h) \ - (DUK_ASSERT_EXPR((h) != NULL), DUK_ASSERT_EXPR((h)->buf != NULL), \ - (((duk_uint8_t *) DUK_HBUFFER_GET_DATA_PTR((heap), (h)->buf)) + (h)->offset)) - -/* True if slice is full, i.e. offset is zero and length covers the entire - * buffer. This status may change independently of the duk_hbufferobject if - * the underlying buffer is dynamic and changes without the hbufferobject - * being changed. - */ -#define DUK_HBUFFEROBJECT_FULL_SLICE(h) \ - (DUK_ASSERT_EXPR((h) != NULL), DUK_ASSERT_EXPR((h)->buf != NULL), \ - ((h)->offset == 0 && (h)->length == DUK_HBUFFER_GET_SIZE((h)->buf))) - -/* Validate that the whole slice [0,length[ is contained in the underlying - * buffer. Caller must ensure 'buf' != NULL. - */ -#define DUK_HBUFFEROBJECT_VALID_SLICE(h) \ - (DUK_ASSERT_EXPR((h) != NULL), DUK_ASSERT_EXPR((h)->buf != NULL), \ - ((h)->offset + (h)->length <= DUK_HBUFFER_GET_SIZE((h)->buf))) - -/* Validate byte read/write for virtual 'offset', i.e. check that the - * offset, taking into account h->offset, is within the underlying - * buffer size. This is a safety check which is needed to ensure - * that even a misconfigured duk_hbufferobject never causes memory - * unsafe behavior (e.g. if an underlying dynamic buffer changes - * after being setup). Caller must ensure 'buf' != NULL. - */ -#define DUK_HBUFFEROBJECT_VALID_BYTEOFFSET_INCL(h,off) \ - (DUK_ASSERT_EXPR((h) != NULL), DUK_ASSERT_EXPR((h)->buf != NULL), \ - ((h)->offset + (off) < DUK_HBUFFER_GET_SIZE((h)->buf))) - -#define DUK_HBUFFEROBJECT_VALID_BYTEOFFSET_EXCL(h,off) \ - (DUK_ASSERT_EXPR((h) != NULL), DUK_ASSERT_EXPR((h)->buf != NULL), \ - ((h)->offset + (off) <= DUK_HBUFFER_GET_SIZE((h)->buf))) - -/* Clamp an input byte length (already assumed to be within the nominal - * duk_hbufferobject 'length') to the current dynamic buffer limits to - * yield a byte length limit that's safe for memory accesses. This value - * can be invalidated by any side effect because it may trigger a user - * callback that resizes the underlying buffer. - */ -#define DUK_HBUFFEROBJECT_CLAMP_BYTELENGTH(h,len) \ - (DUK_ASSERT_EXPR((h) != NULL), \ - duk_hbufferobject_clamp_bytelength((h), (len))) - -struct duk_hbufferobject { - /* Shared object part. */ - duk_hobject obj; - - /* Underlying buffer (refcounted), may be NULL. */ - duk_hbuffer *buf; - - /* Slice and accessor information. - * - * Because the underlying buffer may be dynamic, these may be - * invalidated by the buffer being modified so that both offset - * and length should be validated before every access. Behavior - * when the underlying buffer has changed doesn't need to be clean: - * virtual 'length' doesn't need to be affected, reads can return - * zero/NaN, and writes can be ignored. - * - * Note that a data pointer cannot be precomputed because 'buf' may - * be dynamic and its pointer unstable. - */ - - duk_uint_t offset; /* byte offset to buf */ - duk_uint_t length; /* byte index limit for element access, exclusive */ - duk_uint8_t shift; /* element size shift: - * 0 = u8/i8 - * 1 = u16/i16 - * 2 = u32/i32/float - * 3 = double - */ - duk_uint8_t elem_type; /* element type */ - duk_uint8_t is_view; -}; - -#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) -DUK_INTERNAL_DECL duk_uint_t duk_hbufferobject_clamp_bytelength(duk_hbufferobject *h_bufobj, duk_uint_t len); -#endif -DUK_INTERNAL_DECL void duk_hbufferobject_push_validated_read(duk_context *ctx, duk_hbufferobject *h_bufobj, duk_uint8_t *p, duk_small_uint_t elem_size); -DUK_INTERNAL_DECL void duk_hbufferobject_validated_write(duk_context *ctx, duk_hbufferobject *h_bufobj, duk_uint8_t *p, duk_small_uint_t elem_size); - -#endif /* DUK_HBUFFEROBJECT_H_INCLUDED */ -#line 1 "duk_hthread.h" -/* - * Heap thread object representation. - * - * duk_hthread is also the 'context' (duk_context) for exposed APIs - * which mostly operate on the topmost frame of the value stack. - */ - -#ifndef DUK_HTHREAD_H_INCLUDED -#define DUK_HTHREAD_H_INCLUDED - -/* - * Stack constants - */ - -#define DUK_VALSTACK_GROW_STEP 128 /* roughly 1 kiB */ -#define DUK_VALSTACK_SHRINK_THRESHOLD 256 /* roughly 2 kiB */ -#define DUK_VALSTACK_SHRINK_SPARE 64 /* roughly 0.5 kiB */ -#define DUK_VALSTACK_INITIAL_SIZE 128 /* roughly 1.0 kiB -> but rounds up to DUK_VALSTACK_GROW_STEP in practice */ -#define DUK_VALSTACK_INTERNAL_EXTRA 64 /* internal extra elements assumed on function entry, - * always added to user-defined 'extra' for e.g. the - * duk_check_stack() call. - */ -#define DUK_VALSTACK_API_ENTRY_MINIMUM DUK_API_ENTRY_STACK - /* number of elements guaranteed to be user accessible - * (in addition to call arguments) on Duktape/C function entry. - */ - -/* Note: DUK_VALSTACK_INITIAL_SIZE must be >= DUK_VALSTACK_API_ENTRY_MINIMUM - * + DUK_VALSTACK_INTERNAL_EXTRA so that the initial stack conforms to spare - * requirements. - */ - -#define DUK_VALSTACK_DEFAULT_MAX 1000000L - -#define DUK_CALLSTACK_GROW_STEP 8 /* roughly 256 bytes */ -#define DUK_CALLSTACK_SHRINK_THRESHOLD 16 /* roughly 512 bytes */ -#define DUK_CALLSTACK_SHRINK_SPARE 8 /* roughly 256 bytes */ -#define DUK_CALLSTACK_INITIAL_SIZE 8 -#define DUK_CALLSTACK_DEFAULT_MAX 10000L - -#define DUK_CATCHSTACK_GROW_STEP 4 /* roughly 64 bytes */ -#define DUK_CATCHSTACK_SHRINK_THRESHOLD 8 /* roughly 128 bytes */ -#define DUK_CATCHSTACK_SHRINK_SPARE 4 /* roughly 64 bytes */ -#define DUK_CATCHSTACK_INITIAL_SIZE 4 -#define DUK_CATCHSTACK_DEFAULT_MAX 10000L - -/* - * Activation defines - */ - -#define DUK_ACT_FLAG_STRICT (1 << 0) /* function executes in strict mode */ -#define DUK_ACT_FLAG_TAILCALLED (1 << 1) /* activation has tail called one or more times */ -#define DUK_ACT_FLAG_CONSTRUCT (1 << 2) /* function executes as a constructor (called via "new") */ -#define DUK_ACT_FLAG_PREVENT_YIELD (1 << 3) /* activation prevents yield (native call or "new") */ -#define DUK_ACT_FLAG_DIRECT_EVAL (1 << 4) /* activation is a direct eval call */ -#define DUK_ACT_FLAG_BREAKPOINT_ACTIVE (1 << 5) /* activation has active breakpoint(s) */ - -#define DUK_ACT_GET_FUNC(act) ((act)->func) - -/* - * Flags for __FILE__ / __LINE__ registered into tracedata - */ - -#define DUK_TB_FLAG_NOBLAME_FILELINE (1 << 0) /* don't report __FILE__ / __LINE__ as fileName/lineNumber */ - -/* - * Catcher defines - */ - -/* flags field: LLLLLLFT, L = label (24 bits), F = flags (4 bits), T = type (4 bits) */ -#define DUK_CAT_TYPE_MASK 0x0000000fUL -#define DUK_CAT_TYPE_BITS 4 -#define DUK_CAT_LABEL_MASK 0xffffff00UL -#define DUK_CAT_LABEL_BITS 24 -#define DUK_CAT_LABEL_SHIFT 8 - -#define DUK_CAT_FLAG_CATCH_ENABLED (1 << 4) /* catch part will catch */ -#define DUK_CAT_FLAG_FINALLY_ENABLED (1 << 5) /* finally part will catch */ -#define DUK_CAT_FLAG_CATCH_BINDING_ENABLED (1 << 6) /* request to create catch binding */ -#define DUK_CAT_FLAG_LEXENV_ACTIVE (1 << 7) /* catch or with binding is currently active */ - -#define DUK_CAT_TYPE_UNKNOWN 0 -#define DUK_CAT_TYPE_TCF 1 -#define DUK_CAT_TYPE_LABEL 2 - -#define DUK_CAT_GET_TYPE(c) ((c)->flags & DUK_CAT_TYPE_MASK) -#define DUK_CAT_GET_LABEL(c) (((c)->flags & DUK_CAT_LABEL_MASK) >> DUK_CAT_LABEL_SHIFT) - -#define DUK_CAT_HAS_CATCH_ENABLED(c) ((c)->flags & DUK_CAT_FLAG_CATCH_ENABLED) -#define DUK_CAT_HAS_FINALLY_ENABLED(c) ((c)->flags & DUK_CAT_FLAG_FINALLY_ENABLED) -#define DUK_CAT_HAS_CATCH_BINDING_ENABLED(c) ((c)->flags & DUK_CAT_FLAG_CATCH_BINDING_ENABLED) -#define DUK_CAT_HAS_LEXENV_ACTIVE(c) ((c)->flags & DUK_CAT_FLAG_LEXENV_ACTIVE) - -#define DUK_CAT_SET_CATCH_ENABLED(c) do { \ - (c)->flags |= DUK_CAT_FLAG_CATCH_ENABLED; \ - } while (0) -#define DUK_CAT_SET_FINALLY_ENABLED(c) do { \ - (c)->flags |= DUK_CAT_FLAG_FINALLY_ENABLED; \ - } while (0) -#define DUK_CAT_SET_CATCH_BINDING_ENABLED(c) do { \ - (c)->flags |= DUK_CAT_FLAG_CATCH_BINDING_ENABLED; \ - } while (0) -#define DUK_CAT_SET_LEXENV_ACTIVE(c) do { \ - (c)->flags |= DUK_CAT_FLAG_LEXENV_ACTIVE; \ - } while (0) - -#define DUK_CAT_CLEAR_CATCH_ENABLED(c) do { \ - (c)->flags &= ~DUK_CAT_FLAG_CATCH_ENABLED; \ - } while (0) -#define DUK_CAT_CLEAR_FINALLY_ENABLED(c) do { \ - (c)->flags &= ~DUK_CAT_FLAG_FINALLY_ENABLED; \ - } while (0) -#define DUK_CAT_CLEAR_CATCH_BINDING_ENABLED(c) do { \ - (c)->flags &= ~DUK_CAT_FLAG_CATCH_BINDING_ENABLED; \ - } while (0) -#define DUK_CAT_CLEAR_LEXENV_ACTIVE(c) do { \ - (c)->flags &= ~DUK_CAT_FLAG_LEXENV_ACTIVE; \ - } while (0) - -/* - * Thread defines - */ - -#if defined(DUK_USE_ROM_STRINGS) -#define DUK_HTHREAD_GET_STRING(thr,idx) \ - ((duk_hstring *) DUK_LOSE_CONST(duk_rom_strings_stridx[(idx)])) -#else /* DUK_USE_ROM_STRINGS */ -#if defined(DUK_USE_HEAPPTR16) -#define DUK_HTHREAD_GET_STRING(thr,idx) \ - ((duk_hstring *) DUK_USE_HEAPPTR_DEC16((thr)->heap->heap_udata, (thr)->strs16[(idx)])) -#else -#define DUK_HTHREAD_GET_STRING(thr,idx) \ - ((thr)->strs[(idx)]) -#endif -#endif /* DUK_USE_ROM_STRINGS */ - -#define DUK_HTHREAD_GET_CURRENT_ACTIVATION(thr) (&(thr)->callstack[(thr)->callstack_top - 1]) - -/* values for the state field */ -#define DUK_HTHREAD_STATE_INACTIVE 1 /* thread not currently running */ -#define DUK_HTHREAD_STATE_RUNNING 2 /* thread currently running (only one at a time) */ -#define DUK_HTHREAD_STATE_RESUMED 3 /* thread resumed another thread (active but not running) */ -#define DUK_HTHREAD_STATE_YIELDED 4 /* thread has yielded */ -#define DUK_HTHREAD_STATE_TERMINATED 5 /* thread has terminated */ - -/* Executor interrupt default interval when nothing else requires a - * smaller value. The default interval must be small enough to allow - * for reasonable execution timeout checking but large enough to keep - * impact on execution performance low. - */ -#if defined(DUK_USE_INTERRUPT_COUNTER) -#define DUK_HTHREAD_INTCTR_DEFAULT (256L * 1024L) -#endif - -/* - * Assert context is valid: non-NULL pointer, fields look sane. - * - * This is used by public API call entrypoints to catch invalid 'ctx' pointers - * as early as possible; invalid 'ctx' pointers cause very odd and difficult to - * diagnose behavior so it's worth checking even when the check is not 100%. - */ - -#if defined(DUK_USE_PREFER_SIZE) -#define DUK_ASSERT_CTX_VSSIZE(ctx) /*nop*/ -#else -#define DUK_ASSERT_CTX_VSSIZE(ctx) \ - DUK_ASSERT((duk_size_t) (((duk_hthread *) (ctx))->valstack_end - ((duk_hthread *) (ctx))->valstack) == \ - ((duk_hthread *) (ctx))->valstack_size) -#endif -#define DUK_ASSERT_CTX_VALID(ctx) do { \ - DUK_ASSERT((ctx) != NULL); \ - DUK_ASSERT(DUK_HEAPHDR_GET_TYPE((duk_heaphdr *) (ctx)) == DUK_HTYPE_OBJECT); \ - DUK_ASSERT(DUK_HOBJECT_IS_THREAD((duk_hobject *) (ctx))); \ - DUK_ASSERT(((duk_hthread *) (ctx))->unused1 == 0); \ - DUK_ASSERT(((duk_hthread *) (ctx))->unused2 == 0); \ - DUK_ASSERT(((duk_hthread *) (ctx))->valstack != NULL); \ - DUK_ASSERT(((duk_hthread *) (ctx))->valstack_end >= ((duk_hthread *) (ctx))->valstack); \ - DUK_ASSERT(((duk_hthread *) (ctx))->valstack_top >= ((duk_hthread *) (ctx))->valstack); \ - DUK_ASSERT(((duk_hthread *) (ctx))->valstack_top >= ((duk_hthread *) (ctx))->valstack_bottom); \ - DUK_ASSERT(((duk_hthread *) (ctx))->valstack_end >= ((duk_hthread *) (ctx))->valstack_top); \ - DUK_ASSERT_CTX_VSSIZE((ctx)); \ - } while (0) - -/* - * Struct defines - */ - -/* XXX: for a memory-code tradeoff, remove 'func' and make it's access either a function - * or a macro. This would make the activation 32 bytes long on 32-bit platforms again. - */ - -/* Note: it's nice if size is 2^N (at least for 32-bit platforms). */ -struct duk_activation { - duk_tval tv_func; /* borrowed: full duk_tval for function being executed; for lightfuncs */ - duk_hobject *func; /* borrowed: function being executed; for bound function calls, this is the final, real function, NULL for lightfuncs */ - duk_hobject *var_env; /* current variable environment (may be NULL if delayed) */ - duk_hobject *lex_env; /* current lexical environment (may be NULL if delayed) */ -#ifdef DUK_USE_NONSTD_FUNC_CALLER_PROPERTY - /* Previous value of 'func' caller, restored when unwound. Only in use - * when 'func' is non-strict. - */ - duk_hobject *prev_caller; -#endif - - duk_instr_t *curr_pc; /* next instruction to execute (points to 'func' bytecode, stable pointer), NULL for native calls */ -#if defined(DUK_USE_DEBUGGER_SUPPORT) - duk_uint32_t prev_line; /* needed for stepping */ -#endif - duk_small_uint_t flags; - - /* idx_bottom and idx_retval are only used for book-keeping of - * Ecmascript-initiated calls, to allow returning to an Ecmascript - * function properly. They are duk_size_t to match the convention - * that value stack sizes are duk_size_t and local frame indices - * are duk_idx_t. - */ - - /* Bottom of valstack for this activation, used to reset - * valstack_bottom on return; index is absolute. Note: - * idx_top not needed because top is set to 'nregs' always - * when returning to an Ecmascript activation. - */ - duk_size_t idx_bottom; - - /* Return value when returning to this activation (points to caller - * reg, not callee reg); index is absolute (only set if activation is - * not topmost). - * - * Note: idx_bottom is always set, while idx_retval is only applicable - * for activations below the topmost one. Currently idx_retval for - * the topmost activation is considered garbage (and it not initialized - * on entry or cleared on return; may contain previous or garbage - * values). - */ - duk_size_t idx_retval; - - /* Current 'this' binding is the value just below idx_bottom. - * Previously, 'this' binding was handled with an index to the - * (calling) valstack. This works for everything except tail - * calls, which must not "cumulate" valstack temps. - */ -}; - -/* Note: it's nice if size is 2^N (not 4x4 = 16 bytes on 32 bit) */ -struct duk_catcher { - duk_hstring *h_varname; /* borrowed reference to catch variable name (or NULL if none) */ - /* (reference is valid as long activation exists) */ - duk_instr_t *pc_base; /* resume execution from pc_base or pc_base+1 (points to 'func' bytecode, stable pointer) */ - duk_size_t callstack_index; /* callstack index of related activation */ - duk_size_t idx_base; /* idx_base and idx_base+1 get completion value and type */ - duk_uint32_t flags; /* type and control flags, label number */ -}; - -struct duk_hthread { - /* Shared object part */ - duk_hobject obj; - - /* Pointer to bytecode executor's 'curr_pc' variable. Used to copy - * the current PC back into the topmost activation when activation - * state is about to change (or "syncing" is otherwise needed). This - * is rather awkward but important for performance, see execution.rst. - */ - duk_instr_t **ptr_curr_pc; - - /* Backpointers. */ - duk_heap *heap; - - /* Current strictness flag: affects API calls. */ - duk_uint8_t strict; - - /* Thread state. */ - duk_uint8_t state; - duk_uint8_t unused1; - duk_uint8_t unused2; - - /* Sanity limits for stack sizes. */ - duk_size_t valstack_max; - duk_size_t callstack_max; - duk_size_t catchstack_max; - - /* XXX: Valstack, callstack, and catchstack are currently assumed - * to have non-NULL pointers. Relaxing this would not lead to big - * benefits (except perhaps for terminated threads). - */ - - /* Value stack: these are expressed as pointers for faster stack manipulation. - * [valstack,valstack_top[ is GC-reachable, [valstack_top,valstack_end[ is - * not GC-reachable but kept initialized as 'undefined'. - */ - duk_tval *valstack; /* start of valstack allocation */ - duk_tval *valstack_end; /* end of valstack allocation (exclusive) */ - duk_tval *valstack_bottom; /* bottom of current frame */ - duk_tval *valstack_top; /* top of current frame (exclusive) */ -#if !defined(DUK_USE_PREFER_SIZE) - duk_size_t valstack_size; /* cached: valstack_end - valstack (in entries, not bytes) */ -#endif - - /* Call stack. [0,callstack_top[ is GC reachable. */ - duk_activation *callstack; - duk_size_t callstack_size; /* allocation size */ - duk_size_t callstack_top; /* next to use, highest used is top - 1 */ - duk_size_t callstack_preventcount; /* number of activation records in callstack preventing a yield */ - - /* Catch stack. [0,catchstack_top[ is GC reachable. */ - duk_catcher *catchstack; - duk_size_t catchstack_size; /* allocation size */ - duk_size_t catchstack_top; /* next to use, highest used is top - 1 */ - - /* Yield/resume book-keeping. */ - duk_hthread *resumer; /* who resumed us (if any) */ - - /* Current compiler state (if any), used for augmenting SyntaxErrors. */ - duk_compiler_ctx *compile_ctx; - -#if defined(DUK_USE_INTERRUPT_COUNTER) - /* Interrupt counter for triggering a slow path check for execution - * timeout, debugger interaction such as breakpoints, etc. The value - * is valid for the current running thread, and both the init and - * counter values are copied whenever a thread switch occurs. It's - * important for the counter to be conveniently accessible for the - * bytecode executor inner loop for performance reasons. - */ - duk_int_t interrupt_counter; /* countdown state */ - duk_int_t interrupt_init; /* start value for current countdown */ -#endif - - /* Builtin-objects; may or may not be shared with other threads, - * threads existing in different "compartments" will have different - * built-ins. Must be stored on a per-thread basis because there - * is no intermediate structure for a thread group / compartment. - * This takes quite a lot of space, currently 43x4 = 172 bytes on - * 32-bit platforms. - * - * In some cases the builtins array could be ROM based, but it's - * sometimes edited (e.g. for sandboxing) so it's better to keep - * this array in RAM. - */ - duk_hobject *builtins[DUK_NUM_BUILTINS]; - - /* Convenience copies from heap/vm for faster access. */ -#if defined(DUK_USE_ROM_STRINGS) - /* No field needed when strings are in ROM. */ -#else -#if defined(DUK_USE_HEAPPTR16) - duk_uint16_t *strs16; -#else - duk_hstring **strs; -#endif -#endif -}; - -/* - * Prototypes - */ - -DUK_INTERNAL_DECL void duk_hthread_copy_builtin_objects(duk_hthread *thr_from, duk_hthread *thr_to); -DUK_INTERNAL_DECL void duk_hthread_create_builtin_objects(duk_hthread *thr); -DUK_INTERNAL_DECL duk_bool_t duk_hthread_init_stacks(duk_heap *heap, duk_hthread *thr); -DUK_INTERNAL_DECL void duk_hthread_terminate(duk_hthread *thr); - -DUK_INTERNAL_DECL void duk_hthread_callstack_grow(duk_hthread *thr); -DUK_INTERNAL_DECL void duk_hthread_callstack_shrink_check(duk_hthread *thr); -DUK_INTERNAL_DECL void duk_hthread_callstack_unwind(duk_hthread *thr, duk_size_t new_top); -DUK_INTERNAL_DECL void duk_hthread_catchstack_grow(duk_hthread *thr); -DUK_INTERNAL_DECL void duk_hthread_catchstack_shrink_check(duk_hthread *thr); -DUK_INTERNAL_DECL void duk_hthread_catchstack_unwind(duk_hthread *thr, duk_size_t new_top); - -DUK_INTERNAL_DECL duk_activation *duk_hthread_get_current_activation(duk_hthread *thr); -DUK_INTERNAL_DECL void *duk_hthread_get_valstack_ptr(duk_heap *heap, void *ud); /* indirect allocs */ -DUK_INTERNAL_DECL void *duk_hthread_get_callstack_ptr(duk_heap *heap, void *ud); /* indirect allocs */ -DUK_INTERNAL_DECL void *duk_hthread_get_catchstack_ptr(duk_heap *heap, void *ud); /* indirect allocs */ - -#if defined(DUK_USE_DEBUGGER_SUPPORT) -DUK_INTERNAL_DECL duk_uint_fast32_t duk_hthread_get_act_curr_pc(duk_hthread *thr, duk_activation *act); -#endif -DUK_INTERNAL_DECL duk_uint_fast32_t duk_hthread_get_act_prev_pc(duk_hthread *thr, duk_activation *act); -DUK_INTERNAL_DECL void duk_hthread_sync_currpc(duk_hthread *thr); -DUK_INTERNAL_DECL void duk_hthread_sync_and_null_currpc(duk_hthread *thr); - -#endif /* DUK_HTHREAD_H_INCLUDED */ -#line 1 "duk_hbuffer.h" -/* - * Heap buffer representation. - * - * Heap allocated user data buffer which is either: - * - * 1. A fixed size buffer (data follows header statically) - * 2. A dynamic size buffer (data pointer follows header) - * - * The data pointer for a variable size buffer of zero size may be NULL. - */ - -#ifndef DUK_HBUFFER_H_INCLUDED -#define DUK_HBUFFER_H_INCLUDED - -/* - * Flags - * - * Fixed buffer: 0 - * Dynamic buffer: DUK_HBUFFER_FLAG_DYNAMIC - * External buffer: DUK_HBUFFER_FLAG_DYNAMIC | DUK_HBUFFER_FLAG_EXTERNAL - */ - -#define DUK_HBUFFER_FLAG_DYNAMIC DUK_HEAPHDR_USER_FLAG(0) /* buffer is behind a pointer, dynamic or external */ -#define DUK_HBUFFER_FLAG_EXTERNAL DUK_HEAPHDR_USER_FLAG(1) /* buffer pointer is to an externally allocated buffer */ - -#define DUK_HBUFFER_HAS_DYNAMIC(x) DUK_HEAPHDR_CHECK_FLAG_BITS(&(x)->hdr, DUK_HBUFFER_FLAG_DYNAMIC) -#define DUK_HBUFFER_HAS_EXTERNAL(x) DUK_HEAPHDR_CHECK_FLAG_BITS(&(x)->hdr, DUK_HBUFFER_FLAG_EXTERNAL) - -#define DUK_HBUFFER_SET_DYNAMIC(x) DUK_HEAPHDR_SET_FLAG_BITS(&(x)->hdr, DUK_HBUFFER_FLAG_DYNAMIC) -#define DUK_HBUFFER_SET_EXTERNAL(x) DUK_HEAPHDR_SET_FLAG_BITS(&(x)->hdr, DUK_HBUFFER_FLAG_EXTERNAL) - -#define DUK_HBUFFER_CLEAR_DYNAMIC(x) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(x)->hdr, DUK_HBUFFER_FLAG_DYNAMIC) -#define DUK_HBUFFER_CLEAR_EXTERNAL(x) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(x)->hdr, DUK_HBUFFER_FLAG_EXTERNAL) - -/* - * Misc defines - */ - -/* Impose a maximum buffer length for now. Restricted artificially to - * ensure resize computations or adding a heap header length won't - * overflow size_t and that a signed duk_int_t can hold a buffer - * length. The limit should be synchronized with DUK_HSTRING_MAX_BYTELEN. - */ - -#if defined(DUK_USE_BUFLEN16) -#define DUK_HBUFFER_MAX_BYTELEN (0x0000ffffUL) -#else -/* Intentionally not 0x7fffffffUL; at least JSON code expects that - * 2*len + 2 fits in 32 bits. - */ -#define DUK_HBUFFER_MAX_BYTELEN (0x7ffffffeUL) -#endif - -/* - * Field access - */ - -/* Get/set the current user visible size, without accounting for a dynamic - * buffer's "spare" (= usable size). - */ -#if defined(DUK_USE_BUFLEN16) -/* size stored in duk_heaphdr unused flag bits */ -#define DUK_HBUFFER_GET_SIZE(x) ((x)->hdr.h_flags >> 16) -#define DUK_HBUFFER_SET_SIZE(x,v) do { \ - duk_size_t duk__v; \ - duk__v = (v); \ - DUK_ASSERT(duk__v <= 0xffffUL); \ - (x)->hdr.h_flags = ((x)->hdr.h_flags & 0x0000ffffUL) | (((duk_uint32_t) duk__v) << 16); \ - } while (0) -#define DUK_HBUFFER_ADD_SIZE(x,dv) do { \ - (x)->hdr.h_flags += ((dv) << 16); \ - } while (0) -#define DUK_HBUFFER_SUB_SIZE(x,dv) do { \ - (x)->hdr.h_flags -= ((dv) << 16); \ - } while (0) -#else -#define DUK_HBUFFER_GET_SIZE(x) (((duk_hbuffer *) (x))->size) -#define DUK_HBUFFER_SET_SIZE(x,v) do { \ - ((duk_hbuffer *) (x))->size = (v); \ - } while (0) -#define DUK_HBUFFER_ADD_SIZE(x,dv) do { \ - (x)->size += (dv); \ - } while (0) -#define DUK_HBUFFER_SUB_SIZE(x,dv) do { \ - (x)->size -= (dv); \ - } while (0) -#endif - -#define DUK_HBUFFER_FIXED_GET_SIZE(x) DUK_HBUFFER_GET_SIZE((duk_hbuffer *) (x)) -#define DUK_HBUFFER_FIXED_SET_SIZE(x,v) DUK_HBUFFER_SET_SIZE((duk_hbuffer *) (x)) - -#define DUK_HBUFFER_DYNAMIC_GET_SIZE(x) DUK_HBUFFER_GET_SIZE((duk_hbuffer *) (x)) -#define DUK_HBUFFER_DYNAMIC_SET_SIZE(x,v) DUK_HBUFFER_SET_SIZE((duk_hbuffer *) (x), (v)) -#define DUK_HBUFFER_DYNAMIC_ADD_SIZE(x,dv) DUK_HBUFFER_ADD_SIZE((duk_hbuffer *) (x), (dv)) -#define DUK_HBUFFER_DYNAMIC_SUB_SIZE(x,dv) DUK_HBUFFER_SUB_SIZE((duk_hbuffer *) (x), (dv)) - -#define DUK_HBUFFER_EXTERNAL_GET_SIZE(x) DUK_HBUFFER_GET_SIZE((duk_hbuffer *) (x)) -#define DUK_HBUFFER_EXTERNAL_SET_SIZE(x,v) DUK_HBUFFER_SET_SIZE((duk_hbuffer *) (x), (v)) - -#define DUK_HBUFFER_FIXED_GET_DATA_PTR(heap,x) ((duk_uint8_t *) (((duk_hbuffer_fixed *) (x)) + 1)) - -#if defined(DUK_USE_HEAPPTR16) -#define DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(heap,x) \ - ((void *) DUK_USE_HEAPPTR_DEC16((heap)->heap_udata, ((duk_heaphdr *) (x))->h_extra16)) -#define DUK_HBUFFER_DYNAMIC_SET_DATA_PTR(heap,x,v) do { \ - ((duk_heaphdr *) (x))->h_extra16 = DUK_USE_HEAPPTR_ENC16((heap)->heap_udata, (void *) (v)); \ - } while (0) -#define DUK_HBUFFER_DYNAMIC_SET_DATA_PTR_NULL(heap,x) do { \ - ((duk_heaphdr *) (x))->h_extra16 = 0; /* assume 0 <=> NULL */ \ - } while (0) -#else -#define DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(heap,x) ((x)->curr_alloc) -#define DUK_HBUFFER_DYNAMIC_SET_DATA_PTR(heap,x,v) do { \ - (x)->curr_alloc = (void *) (v); \ - } while (0) -#define DUK_HBUFFER_DYNAMIC_SET_DATA_PTR_NULL(heap,x) do { \ - (x)->curr_alloc = (void *) NULL; \ - } while (0) -#endif - -/* No pointer compression because pointer is potentially outside of - * Duktape heap. - */ -#if defined(DUK_USE_HEAPPTR16) -#define DUK_HBUFFER_EXTERNAL_GET_DATA_PTR(heap,x) \ - ((void *) (x)->curr_alloc) -#define DUK_HBUFFER_EXTERNAL_SET_DATA_PTR(heap,x,v) do { \ - (x)->curr_alloc = (void *) (v); \ - } while (0) -#define DUK_HBUFFER_EXTERNAL_SET_DATA_PTR_NULL(heap,x) do { \ - (x)->curr_alloc = (void *) NULL; \ - } while (0) -#else -#define DUK_HBUFFER_EXTERNAL_GET_DATA_PTR(heap,x) \ - ((void *) (x)->curr_alloc) -#define DUK_HBUFFER_EXTERNAL_SET_DATA_PTR(heap,x,v) do { \ - (x)->curr_alloc = (void *) (v); \ - } while (0) -#define DUK_HBUFFER_EXTERNAL_SET_DATA_PTR_NULL(heap,x) do { \ - (x)->curr_alloc = (void *) NULL; \ - } while (0) -#endif - -/* Get a pointer to the current buffer contents (matching current allocation - * size). May be NULL for zero size dynamic/external buffer. - */ -#if defined(DUK_USE_HEAPPTR16) -#define DUK_HBUFFER_GET_DATA_PTR(heap,x) ( \ - DUK_HBUFFER_HAS_DYNAMIC((x)) ? \ - ( \ - DUK_HBUFFER_HAS_EXTERNAL((x)) ? \ - DUK_HBUFFER_EXTERNAL_GET_DATA_PTR((heap), (duk_hbuffer_external *) (x)) : \ - DUK_HBUFFER_DYNAMIC_GET_DATA_PTR((heap), (duk_hbuffer_dynamic *) (x)) \ - ) : \ - DUK_HBUFFER_FIXED_GET_DATA_PTR((heap), (duk_hbuffer_fixed *) (x)) \ - ) -#else -/* Without heap pointer compression duk_hbuffer_dynamic and duk_hbuffer_external - * have the same layout so checking for fixed vs. dynamic (or external) is enough. - */ -#define DUK_HBUFFER_GET_DATA_PTR(heap,x) ( \ - DUK_HBUFFER_HAS_DYNAMIC((x)) ? \ - DUK_HBUFFER_DYNAMIC_GET_DATA_PTR((heap), (duk_hbuffer_dynamic *) (x)) : \ - DUK_HBUFFER_FIXED_GET_DATA_PTR((heap), (duk_hbuffer_fixed *) (x)) \ - ) -#endif - -/* - * Structs - */ - -/* Shared prefix for all buffer types. */ -struct duk_hbuffer { - duk_heaphdr hdr; - - /* It's not strictly necessary to track the current size, but - * it is useful for writing robust native code. - */ - - /* Current size (not counting a dynamic buffer's "spare"). */ -#if defined(DUK_USE_BUFLEN16) - /* Stored in duk_heaphdr unused flags. */ -#else - duk_size_t size; -#endif - - /* - * Data following the header depends on the DUK_HBUFFER_FLAG_DYNAMIC - * flag. - * - * If the flag is clear (the buffer is a fixed size one), the buffer - * data follows the header directly, consisting of 'size' bytes. - * - * If the flag is set, the actual buffer is allocated separately, and - * a few control fields follow the header. Specifically: - * - * - a "void *" pointing to the current allocation - * - a duk_size_t indicating the full allocated size (always >= 'size') - * - * If DUK_HBUFFER_FLAG_EXTERNAL is set, the buffer has been allocated - * by user code, so that Duktape won't be able to resize it and won't - * free it. This allows buffers to point to e.g. an externally - * allocated structure such as a frame buffer. - * - * Unlike strings, no terminator byte (NUL) is guaranteed after the - * data. This would be convenient, but would pad aligned user buffers - * unnecessarily upwards in size. For instance, if user code requested - * a 64-byte dynamic buffer, 65 bytes would actually be allocated which - * would then potentially round upwards to perhaps 68 or 72 bytes. - */ -}; - -/* Fixed buffer; data follows struct, with proper alignment guaranteed by - * struct size. - */ -#if (DUK_USE_ALIGN_BY == 8) && defined(DUK_USE_PACK_MSVC_PRAGMA) -#pragma pack(push, 8) -#endif -struct duk_hbuffer_fixed { - /* A union is used here as a portable struct size / alignment trick: - * by adding a 32-bit or a 64-bit (unused) union member, the size of - * the struct is effectively forced to be a multiple of 4 or 8 bytes - * (respectively) without increasing the size of the struct unless - * necessary. - */ - union { - struct { - duk_heaphdr hdr; -#if defined(DUK_USE_BUFLEN16) - /* Stored in duk_heaphdr unused flags. */ -#else - duk_size_t size; -#endif - } s; -#if (DUK_USE_ALIGN_BY == 4) - duk_uint32_t dummy_for_align4; -#elif (DUK_USE_ALIGN_BY == 8) - duk_double_t dummy_for_align8; -#elif (DUK_USE_ALIGN_BY == 1) - /* no extra padding */ -#else -#error invalid DUK_USE_ALIGN_BY -#endif - } u; - - /* - * Data follows the struct header. The struct size is padded by the - * compiler based on the struct members. This guarantees that the - * buffer data will be aligned-by-4 but not necessarily aligned-by-8. - * - * On platforms where alignment does not matter, the struct padding - * could be removed (if there is any). On platforms where alignment - * by 8 is required, the struct size must be forced to be a multiple - * of 8 by some means. Without it, some user code may break, and also - * Duktape itself breaks (e.g. the compiler stores duk_tvals in a - * dynamic buffer). - */ -} -#if (DUK_USE_ALIGN_BY == 8) && defined(DUK_USE_PACK_GCC_ATTR) -__attribute__ ((aligned (8))) -#elif (DUK_USE_ALIGN_BY == 8) && defined(DUK_USE_PACK_CLANG_ATTR) -__attribute__ ((aligned (8))) -#endif -; -#if (DUK_USE_ALIGN_BY == 8) && defined(DUK_USE_PACK_MSVC_PRAGMA) -#pragma pack(pop) -#endif - -/* Dynamic buffer with 'curr_alloc' pointing to a dynamic area allocated using - * heap allocation primitives. Also used for external buffers when low memory - * options are not used. - */ -struct duk_hbuffer_dynamic { - duk_heaphdr hdr; - -#if defined(DUK_USE_BUFLEN16) - /* Stored in duk_heaphdr unused flags. */ -#else - duk_size_t size; -#endif - -#if defined(DUK_USE_HEAPPTR16) - /* Stored in duk_heaphdr h_extra16. */ -#else - void *curr_alloc; /* may be NULL if alloc_size == 0 */ -#endif - - /* - * Allocation size for 'curr_alloc' is alloc_size. There is no - * automatic NUL terminator for buffers (see above for rationale). - * - * 'curr_alloc' is explicitly allocated with heap allocation - * primitives and will thus always have alignment suitable for - * e.g. duk_tval and an IEEE double. - */ -}; - -/* External buffer with 'curr_alloc' managed by user code and pointing to an - * arbitrary address. When heap pointer compression is not used, this struct - * has the same layout as duk_hbuffer_dynamic. - */ -struct duk_hbuffer_external { - duk_heaphdr hdr; - -#if defined(DUK_USE_BUFLEN16) - /* Stored in duk_heaphdr unused flags. */ -#else - duk_size_t size; -#endif - - /* Cannot be compressed as a heap pointer because may point to - * an arbitrary address. - */ - void *curr_alloc; /* may be NULL if alloc_size == 0 */ -}; - -/* - * Prototypes - */ - -DUK_INTERNAL_DECL duk_hbuffer *duk_hbuffer_alloc(duk_heap *heap, duk_size_t size, duk_small_uint_t flags, void **out_bufdata); -DUK_INTERNAL_DECL void *duk_hbuffer_get_dynalloc_ptr(duk_heap *heap, void *ud); /* indirect allocs */ - -/* dynamic buffer ops */ -DUK_INTERNAL_DECL void duk_hbuffer_resize(duk_hthread *thr, duk_hbuffer_dynamic *buf, duk_size_t new_size); -DUK_INTERNAL_DECL void duk_hbuffer_reset(duk_hthread *thr, duk_hbuffer_dynamic *buf); - -#endif /* DUK_HBUFFER_H_INCLUDED */ -#line 1 "duk_heap.h" -/* - * Heap structure. - * - * Heap contains allocated heap objects, interned strings, and built-in - * strings for one or more threads. - */ - -#ifndef DUK_HEAP_H_INCLUDED -#define DUK_HEAP_H_INCLUDED - -/* alloc function typedefs in duktape.h */ - -/* - * Heap flags - */ - -#define DUK_HEAP_FLAG_MARKANDSWEEP_RUNNING (1 << 0) /* mark-and-sweep is currently running */ -#define DUK_HEAP_FLAG_MARKANDSWEEP_RECLIMIT_REACHED (1 << 1) /* mark-and-sweep marking reached a recursion limit and must use multi-pass marking */ -#define DUK_HEAP_FLAG_REFZERO_FREE_RUNNING (1 << 2) /* refcount code is processing refzero list */ -#define DUK_HEAP_FLAG_ERRHANDLER_RUNNING (1 << 3) /* an error handler (user callback to augment/replace error) is running */ -#define DUK_HEAP_FLAG_INTERRUPT_RUNNING (1 << 4) /* executor interrupt running (used to avoid nested interrupts) */ -#define DUK_HEAP_FLAG_FINALIZER_NORESCUE (1 << 5) /* heap destruction ongoing, finalizer rescue no longer possible */ - -#define DUK__HEAP_HAS_FLAGS(heap,bits) ((heap)->flags & (bits)) -#define DUK__HEAP_SET_FLAGS(heap,bits) do { \ - (heap)->flags |= (bits); \ - } while (0) -#define DUK__HEAP_CLEAR_FLAGS(heap,bits) do { \ - (heap)->flags &= ~(bits); \ - } while (0) - -#define DUK_HEAP_HAS_MARKANDSWEEP_RUNNING(heap) DUK__HEAP_HAS_FLAGS((heap), DUK_HEAP_FLAG_MARKANDSWEEP_RUNNING) -#define DUK_HEAP_HAS_MARKANDSWEEP_RECLIMIT_REACHED(heap) DUK__HEAP_HAS_FLAGS((heap), DUK_HEAP_FLAG_MARKANDSWEEP_RECLIMIT_REACHED) -#define DUK_HEAP_HAS_REFZERO_FREE_RUNNING(heap) DUK__HEAP_HAS_FLAGS((heap), DUK_HEAP_FLAG_REFZERO_FREE_RUNNING) -#define DUK_HEAP_HAS_ERRHANDLER_RUNNING(heap) DUK__HEAP_HAS_FLAGS((heap), DUK_HEAP_FLAG_ERRHANDLER_RUNNING) -#define DUK_HEAP_HAS_INTERRUPT_RUNNING(heap) DUK__HEAP_HAS_FLAGS((heap), DUK_HEAP_FLAG_INTERRUPT_RUNNING) -#define DUK_HEAP_HAS_FINALIZER_NORESCUE(heap) DUK__HEAP_HAS_FLAGS((heap), DUK_HEAP_FLAG_FINALIZER_NORESCUE) - -#define DUK_HEAP_SET_MARKANDSWEEP_RUNNING(heap) DUK__HEAP_SET_FLAGS((heap), DUK_HEAP_FLAG_MARKANDSWEEP_RUNNING) -#define DUK_HEAP_SET_MARKANDSWEEP_RECLIMIT_REACHED(heap) DUK__HEAP_SET_FLAGS((heap), DUK_HEAP_FLAG_MARKANDSWEEP_RECLIMIT_REACHED) -#define DUK_HEAP_SET_REFZERO_FREE_RUNNING(heap) DUK__HEAP_SET_FLAGS((heap), DUK_HEAP_FLAG_REFZERO_FREE_RUNNING) -#define DUK_HEAP_SET_ERRHANDLER_RUNNING(heap) DUK__HEAP_SET_FLAGS((heap), DUK_HEAP_FLAG_ERRHANDLER_RUNNING) -#define DUK_HEAP_SET_INTERRUPT_RUNNING(heap) DUK__HEAP_SET_FLAGS((heap), DUK_HEAP_FLAG_INTERRUPT_RUNNING) -#define DUK_HEAP_SET_FINALIZER_NORESCUE(heap) DUK__HEAP_SET_FLAGS((heap), DUK_HEAP_FLAG_FINALIZER_NORESCUE) - -#define DUK_HEAP_CLEAR_MARKANDSWEEP_RUNNING(heap) DUK__HEAP_CLEAR_FLAGS((heap), DUK_HEAP_FLAG_MARKANDSWEEP_RUNNING) -#define DUK_HEAP_CLEAR_MARKANDSWEEP_RECLIMIT_REACHED(heap) DUK__HEAP_CLEAR_FLAGS((heap), DUK_HEAP_FLAG_MARKANDSWEEP_RECLIMIT_REACHED) -#define DUK_HEAP_CLEAR_REFZERO_FREE_RUNNING(heap) DUK__HEAP_CLEAR_FLAGS((heap), DUK_HEAP_FLAG_REFZERO_FREE_RUNNING) -#define DUK_HEAP_CLEAR_ERRHANDLER_RUNNING(heap) DUK__HEAP_CLEAR_FLAGS((heap), DUK_HEAP_FLAG_ERRHANDLER_RUNNING) -#define DUK_HEAP_CLEAR_INTERRUPT_RUNNING(heap) DUK__HEAP_CLEAR_FLAGS((heap), DUK_HEAP_FLAG_INTERRUPT_RUNNING) -#define DUK_HEAP_CLEAR_FINALIZER_NORESCUE(heap) DUK__HEAP_CLEAR_FLAGS((heap), DUK_HEAP_FLAG_FINALIZER_NORESCUE) - -/* - * Longjmp types, also double as identifying continuation type for a rethrow (in 'finally') - */ - -#define DUK_LJ_TYPE_UNKNOWN 0 /* unused */ -#define DUK_LJ_TYPE_THROW 1 /* value1 -> error object */ -#define DUK_LJ_TYPE_YIELD 2 /* value1 -> yield value, iserror -> error / normal */ -#define DUK_LJ_TYPE_RESUME 3 /* value1 -> resume value, value2 -> resumee thread, iserror -> error/normal */ -#define DUK_LJ_TYPE_BREAK 4 /* value1 -> label number, pseudo-type to indicate a break continuation (for ENDFIN) */ -#define DUK_LJ_TYPE_CONTINUE 5 /* value1 -> label number, pseudo-type to indicate a continue continuation (for ENDFIN) */ -#define DUK_LJ_TYPE_RETURN 6 /* value1 -> return value, pseudo-type to indicate a return continuation (for ENDFIN) */ -#define DUK_LJ_TYPE_NORMAL 7 /* no value, pseudo-type to indicate a normal continuation (for ENDFIN) */ - -/* - * Mark-and-sweep flags - * - * These are separate from heap level flags now but could be merged. - * The heap structure only contains a 'base mark-and-sweep flags' - * field and the GC caller can impose further flags. - */ - -#define DUK_MS_FLAG_EMERGENCY (1 << 0) /* emergency mode: try extra hard */ -#define DUK_MS_FLAG_NO_STRINGTABLE_RESIZE (1 << 1) /* don't resize stringtable (but may sweep it); needed during stringtable resize */ -#define DUK_MS_FLAG_NO_OBJECT_COMPACTION (1 << 2) /* don't compact objects; needed during object property allocation resize */ -#define DUK_MS_FLAG_NO_FINALIZERS (1 << 3) /* don't run finalizers; leave finalizable objects in finalize_list for next round */ -#define DUK_MS_FLAG_SKIP_FINALIZERS (1 << 4) /* don't run finalizers; queue finalizable objects back to heap_allocated */ - -/* - * Thread switching - * - * To switch heap->curr_thread, use the macro below so that interrupt counters - * get updated correctly. The macro allows a NULL target thread because that - * happens e.g. in call handling. - */ - -#if defined(DUK_USE_INTERRUPT_COUNTER) -#define DUK_HEAP_SWITCH_THREAD(heap,newthr) duk_heap_switch_thread((heap), (newthr)) -#else -#define DUK_HEAP_SWITCH_THREAD(heap,newthr) do { \ - (heap)->curr_thread = (newthr); \ - } while (0) -#endif - -/* - * Other heap related defines - */ - -/* Mark-and-sweep interval is relative to combined count of objects and - * strings kept in the heap during the latest mark-and-sweep pass. - * Fixed point .8 multiplier and .0 adder. Trigger count (interval) is - * decreased by each (re)allocation attempt (regardless of size), and each - * refzero processed object. - * - * 'SKIP' indicates how many (re)allocations to wait until a retry if - * GC is skipped because there is no thread do it with yet (happens - * only during init phases). - */ -#if defined(DUK_USE_MARK_AND_SWEEP) -#if defined(DUK_USE_REFERENCE_COUNTING) -#define DUK_HEAP_MARK_AND_SWEEP_TRIGGER_MULT 12800L /* 50x heap size */ -#define DUK_HEAP_MARK_AND_SWEEP_TRIGGER_ADD 1024L -#define DUK_HEAP_MARK_AND_SWEEP_TRIGGER_SKIP 256L -#else -#define DUK_HEAP_MARK_AND_SWEEP_TRIGGER_MULT 256L /* 1x heap size */ -#define DUK_HEAP_MARK_AND_SWEEP_TRIGGER_ADD 1024L -#define DUK_HEAP_MARK_AND_SWEEP_TRIGGER_SKIP 256L -#endif -#endif - -/* Stringcache is used for speeding up char-offset-to-byte-offset - * translations for non-ASCII strings. - */ -#define DUK_HEAP_STRCACHE_SIZE 4 -#define DUK_HEAP_STRINGCACHE_NOCACHE_LIMIT 16 /* strings up to the this length are not cached */ - -/* helper to insert a (non-string) heap object into heap allocated list */ -#define DUK_HEAP_INSERT_INTO_HEAP_ALLOCATED(heap,hdr) duk_heap_insert_into_heap_allocated((heap),(hdr)) - -/* - * Stringtable - */ - -/* initial stringtable size, must be prime and higher than DUK_UTIL_MIN_HASH_PRIME */ -#define DUK_STRTAB_INITIAL_SIZE 17 - -/* indicates a deleted string; any fixed non-NULL, non-hstring pointer works */ -#define DUK_STRTAB_DELETED_MARKER(heap) ((duk_hstring *) heap) - -/* resizing parameters */ -#define DUK_STRTAB_MIN_FREE_DIVISOR 4 /* load factor max 75% */ -#define DUK_STRTAB_MIN_USED_DIVISOR 4 /* load factor min 25% */ -#define DUK_STRTAB_GROW_ST_SIZE(n) ((n) + (n)) /* used entries + approx 100% -> reset load to 50% */ - -#define DUK_STRTAB_U32_MAX_STRLEN 10 /* 4'294'967'295 */ -#define DUK_STRTAB_HIGHEST_32BIT_PRIME 0xfffffffbUL - -/* probe sequence (open addressing) */ -#define DUK_STRTAB_HASH_INITIAL(hash,h_size) ((hash) % (h_size)) -#define DUK_STRTAB_HASH_PROBE_STEP(hash) DUK_UTIL_GET_HASH_PROBE_STEP((hash)) - -/* fixed top level hashtable size (separate chaining) */ -#define DUK_STRTAB_CHAIN_SIZE DUK_USE_STRTAB_CHAIN_SIZE - -/* - * Built-in strings - */ - -/* heap string indices are autogenerated in duk_strings.h */ -#if defined(DUK_USE_ROM_STRINGS) -#define DUK_HEAP_GET_STRING(heap,idx) \ - ((duk_hstring *) DUK_LOSE_CONST(duk_rom_strings_stridx[(idx)])) -#else /* DUK_USE_ROM_STRINGS */ -#if defined(DUK_USE_HEAPPTR16) -#define DUK_HEAP_GET_STRING(heap,idx) \ - ((duk_hstring *) DUK_USE_HEAPPTR_DEC16((heap)->heap_udata, (heap)->strs16[(idx)])) -#else -#define DUK_HEAP_GET_STRING(heap,idx) \ - ((heap)->strs[(idx)]) -#endif -#endif /* DUK_USE_ROM_STRINGS */ - -/* - * Raw memory calls: relative to heap, but no GC interaction - */ - -#define DUK_ALLOC_RAW(heap,size) \ - ((heap)->alloc_func((heap)->heap_udata, (size))) - -#define DUK_REALLOC_RAW(heap,ptr,newsize) \ - ((heap)->realloc_func((heap)->heap_udata, (void *) (ptr), (newsize))) - -#define DUK_FREE_RAW(heap,ptr) \ - ((heap)->free_func((heap)->heap_udata, (void *) (ptr))) - -/* - * Memory calls: relative to heap, GC interaction, but no error throwing. - * - * XXX: Currently a mark-and-sweep triggered by memory allocation will run - * using the heap->heap_thread. This thread is also used for running - * mark-and-sweep finalization; this is not ideal because it breaks the - * isolation between multiple global environments. - * - * Notes: - * - * - DUK_FREE() is required to ignore NULL and any other possible return - * value of a zero-sized alloc/realloc (same as ANSI C free()). - * - * - There is no DUK_REALLOC_ZEROED because we don't assume to know the - * old size. Caller must zero the reallocated memory. - * - * - DUK_REALLOC_INDIRECT() must be used when a mark-and-sweep triggered - * by an allocation failure might invalidate the original 'ptr', thus - * causing a realloc retry to use an invalid pointer. Example: we're - * reallocating the value stack and a finalizer resizes the same value - * stack during mark-and-sweep. The indirect variant requests for the - * current location of the pointer being reallocated using a callback - * right before every realloc attempt; this circuitous approach is used - * to avoid strict aliasing issues in a more straightforward indirect - * pointer (void **) approach. Note: the pointer in the storage - * location is read but is NOT updated; the caller must do that. - */ - -/* callback for indirect reallocs, request for current pointer */ -typedef void *(*duk_mem_getptr)(duk_heap *heap, void *ud); - -#define DUK_ALLOC(heap,size) duk_heap_mem_alloc((heap), (size)) -#define DUK_ALLOC_ZEROED(heap,size) duk_heap_mem_alloc_zeroed((heap), (size)) -#define DUK_REALLOC(heap,ptr,newsize) duk_heap_mem_realloc((heap), (ptr), (newsize)) -#define DUK_REALLOC_INDIRECT(heap,cb,ud,newsize) duk_heap_mem_realloc_indirect((heap), (cb), (ud), (newsize)) -#define DUK_FREE(heap,ptr) duk_heap_mem_free((heap), (ptr)) - -/* - * Memory constants - */ - -#define DUK_HEAP_ALLOC_FAIL_MARKANDSWEEP_LIMIT 5 /* Retry allocation after mark-and-sweep for this - * many times. A single mark-and-sweep round is - * not guaranteed to free all unreferenced memory - * because of finalization (in fact, ANY number of - * rounds is strictly not enough). - */ - -#define DUK_HEAP_ALLOC_FAIL_MARKANDSWEEP_EMERGENCY_LIMIT 3 /* Starting from this round, use emergency mode - * for mark-and-sweep. - */ - -/* - * Debugger support - */ - -/* Maximum number of breakpoints. Only breakpoints that are set are - * consulted so increasing this has no performance impact. - */ -#define DUK_HEAP_MAX_BREAKPOINTS 16 - -/* Opcode interval for a Date-based status/peek rate limit check. Only - * relevant when debugger is attached. Requesting a timestamp may be a - * slow operation on some platforms so this shouldn't be too low. On the - * other hand a high value makes Duktape react to a pause request slowly. - */ -#define DUK_HEAP_DBG_RATELIMIT_OPCODES 4000 - -/* Milliseconds between status notify and transport peeks. */ -#define DUK_HEAP_DBG_RATELIMIT_MILLISECS 200 - -/* Step types */ -#define DUK_STEP_TYPE_NONE 0 -#define DUK_STEP_TYPE_INTO 1 -#define DUK_STEP_TYPE_OVER 2 -#define DUK_STEP_TYPE_OUT 3 - -struct duk_breakpoint { - duk_hstring *filename; - duk_uint32_t line; -}; - -#if defined(DUK_USE_DEBUGGER_SUPPORT) -#define DUK_HEAP_IS_DEBUGGER_ATTACHED(heap) ((heap)->dbg_read_cb != NULL) -#define DUK_HEAP_CLEAR_STEP_STATE(heap) do { \ - (heap)->dbg_step_type = DUK_STEP_TYPE_NONE; \ - (heap)->dbg_step_thread = NULL; \ - (heap)->dbg_step_csindex = 0; \ - (heap)->dbg_step_startline = 0; \ - } while (0) -#define DUK_HEAP_SET_PAUSED(heap) do { \ - (heap)->dbg_paused = 1; \ - (heap)->dbg_state_dirty = 1; \ - DUK_HEAP_CLEAR_STEP_STATE((heap)); \ - } while (0) -#define DUK_HEAP_CLEAR_PAUSED(heap) do { \ - (heap)->dbg_paused = 0; \ - (heap)->dbg_state_dirty = 1; \ - DUK_HEAP_CLEAR_STEP_STATE((heap)); \ - } while (0) -#define DUK_HEAP_IS_PAUSED(heap) ((heap)->dbg_paused) -#endif /* DUK_USE_DEBUGGER_SUPPORT */ - -/* - * String cache should ideally be at duk_hthread level, but that would - * cause string finalization to slow down relative to the number of - * threads; string finalization must check the string cache for "weak" - * references to the string being finalized to avoid dead pointers. - * - * Thus, string caches are now at the heap level now. - */ - -struct duk_strcache { - duk_hstring *h; - duk_uint32_t bidx; - duk_uint32_t cidx; -}; - -/* - * Longjmp state, contains the information needed to perform a longjmp. - * Longjmp related values are written to value1, value2, and iserror. - */ - -struct duk_ljstate { - duk_jmpbuf *jmpbuf_ptr; /* current setjmp() catchpoint */ - duk_small_uint_t type; /* longjmp type */ - duk_bool_t iserror; /* isError flag for yield */ - duk_tval value1; /* 1st related value (type specific) */ - duk_tval value2; /* 2nd related value (type specific) */ -}; - -/* - * Stringtable entry for fixed size stringtable - */ - -struct duk_strtab_entry { -#if defined(DUK_USE_HEAPPTR16) - /* A 16-bit listlen makes sense with 16-bit heap pointers: there - * won't be space for 64k strings anyway. - */ - duk_uint16_t listlen; /* if 0, 'str16' used, if > 0, 'strlist16' used */ - union { - duk_uint16_t strlist16; - duk_uint16_t str16; - } u; -#else - duk_size_t listlen; /* if 0, 'str' used, if > 0, 'strlist' used */ - union { - duk_hstring **strlist; - duk_hstring *str; - } u; -#endif -}; - -/* - * Main heap structure - */ - -struct duk_heap { - duk_small_uint_t flags; - - /* Allocator functions. */ - duk_alloc_function alloc_func; - duk_realloc_function realloc_func; - duk_free_function free_func; - - /* Heap udata, used for allocator functions but also for other heap - * level callbacks like pointer compression, etc. - */ - void *heap_udata; - - /* Precomputed pointers when using 16-bit heap pointer packing. */ -#if defined(DUK_USE_HEAPPTR16) - duk_uint16_t heapptr_null16; - duk_uint16_t heapptr_deleted16; -#endif - - /* Fatal error handling, called e.g. when a longjmp() is needed but - * lj.jmpbuf_ptr is NULL. fatal_func must never return; it's not - * declared as "noreturn" because doing that for typedefs is a bit - * challenging portability-wise. - */ - duk_fatal_function fatal_func; - - /* allocated heap objects */ - duk_heaphdr *heap_allocated; - - /* work list for objects whose refcounts are zero but which have not been - * "finalized"; avoids recursive C calls when refcounts go to zero in a - * chain of objects. - */ -#if defined(DUK_USE_REFERENCE_COUNTING) - duk_heaphdr *refzero_list; - duk_heaphdr *refzero_list_tail; -#endif - -#if defined(DUK_USE_MARK_AND_SWEEP) - /* mark-and-sweep control */ -#if defined(DUK_USE_VOLUNTARY_GC) - duk_int_t mark_and_sweep_trigger_counter; -#endif - duk_int_t mark_and_sweep_recursion_depth; - - /* mark-and-sweep flags automatically active (used for critical sections) */ - duk_small_uint_t mark_and_sweep_base_flags; - - /* work list for objects to be finalized (by mark-and-sweep) */ - duk_heaphdr *finalize_list; -#endif - - /* longjmp state */ - duk_ljstate lj; - - /* marker for detecting internal "double faults", see duk_error_throw.c */ - duk_bool_t handling_error; - - /* heap thread, used internally and for finalization */ - duk_hthread *heap_thread; - - /* current thread */ - duk_hthread *curr_thread; /* currently running thread */ - - /* heap level "stash" object (e.g., various reachability roots) */ - duk_hobject *heap_object; - - /* duk_handle_call / duk_handle_safe_call recursion depth limiting */ - duk_int_t call_recursion_depth; - duk_int_t call_recursion_limit; - - /* mix-in value for computing string hashes; should be reasonably unpredictable */ - duk_uint32_t hash_seed; - - /* rnd_state for duk_util_tinyrandom.c */ - duk_uint32_t rnd_state; - - /* For manual debugging: instruction count based on executor and - * interrupt counter book-keeping. Inspect debug logs to see how - * they match up. - */ -#if defined(DUK_USE_INTERRUPT_COUNTER) && defined(DUK_USE_DEBUG) - duk_int_t inst_count_exec; - duk_int_t inst_count_interrupt; -#endif - - /* debugger */ - -#if defined(DUK_USE_DEBUGGER_SUPPORT) - /* callbacks and udata; dbg_read_cb != NULL is used to indicate attached state */ - duk_debug_read_function dbg_read_cb; /* required, NULL implies detached */ - duk_debug_write_function dbg_write_cb; /* required */ - duk_debug_peek_function dbg_peek_cb; - duk_debug_read_flush_function dbg_read_flush_cb; - duk_debug_write_flush_function dbg_write_flush_cb; - duk_debug_request_function dbg_request_cb; - duk_debug_detached_function dbg_detached_cb; - void *dbg_udata; - - /* debugger state, only relevant when attached */ - duk_bool_t dbg_processing; /* currently processing messages or breakpoints: don't enter message processing recursively (e.g. no breakpoints when processing debugger eval) */ - duk_bool_t dbg_paused; /* currently paused: talk with debug client until step/resume */ - duk_bool_t dbg_state_dirty; /* resend state next time executor is about to run */ - duk_bool_t dbg_force_restart; /* force executor restart to recheck breakpoints; used to handle function returns (see GH-303) */ - duk_bool_t dbg_detaching; /* debugger detaching; used to avoid calling detach handler recursively */ - duk_small_uint_t dbg_step_type; /* step type: none, step into, step over, step out */ - duk_hthread *dbg_step_thread; /* borrowed; NULL if no step state (NULLed in unwind) */ - duk_size_t dbg_step_csindex; /* callstack index */ - duk_uint32_t dbg_step_startline; /* starting line number */ - duk_breakpoint dbg_breakpoints[DUK_HEAP_MAX_BREAKPOINTS]; /* breakpoints: [0,breakpoint_count[ gc reachable */ - duk_small_uint_t dbg_breakpoint_count; - duk_breakpoint *dbg_breakpoints_active[DUK_HEAP_MAX_BREAKPOINTS + 1]; /* currently active breakpoints: NULL term, borrowed pointers */ - /* XXX: make active breakpoints actual copies instead of pointers? */ - - /* These are for rate limiting Status notifications and transport peeking. */ - duk_uint32_t dbg_exec_counter; /* cumulative opcode execution count (overflows are OK) */ - duk_uint32_t dbg_last_counter; /* value of dbg_exec_counter when we last did a Date-based check */ - duk_double_t dbg_last_time; /* time when status/peek was last done (Date-based rate limit) */ - - /* Used to support single-byte stream lookahead. */ - duk_bool_t dbg_have_next_byte; - duk_uint8_t dbg_next_byte; -#endif - - /* string intern table (weak refs) */ -#if defined(DUK_USE_STRTAB_PROBE) -#if defined(DUK_USE_HEAPPTR16) - duk_uint16_t *strtable16; -#else - duk_hstring **strtable; -#endif - duk_uint32_t st_size; /* alloc size in elements */ - duk_uint32_t st_used; /* used elements (includes DELETED) */ -#endif - - /* XXX: static alloc is OK until separate chaining stringtable - * resizing is implemented. - */ -#if defined(DUK_USE_STRTAB_CHAIN) - duk_strtab_entry strtable[DUK_STRTAB_CHAIN_SIZE]; -#endif - - /* string access cache (codepoint offset -> byte offset) for fast string - * character looping; 'weak' reference which needs special handling in GC. - */ - duk_strcache strcache[DUK_HEAP_STRCACHE_SIZE]; - - /* built-in strings */ -#if defined(DUK_USE_ROM_STRINGS) - /* No field needed when strings are in ROM. */ -#else -#if defined(DUK_USE_HEAPPTR16) - duk_uint16_t strs16[DUK_HEAP_NUM_STRINGS]; -#else - duk_hstring *strs[DUK_HEAP_NUM_STRINGS]; -#endif -#endif -}; - -/* - * Prototypes - */ - -DUK_INTERNAL_DECL -duk_heap *duk_heap_alloc(duk_alloc_function alloc_func, - duk_realloc_function realloc_func, - duk_free_function free_func, - void *heap_udata, - duk_fatal_function fatal_func); -DUK_INTERNAL_DECL void duk_heap_free(duk_heap *heap); -DUK_INTERNAL_DECL void duk_free_hobject_inner(duk_heap *heap, duk_hobject *h); -DUK_INTERNAL_DECL void duk_free_hbuffer_inner(duk_heap *heap, duk_hbuffer *h); -DUK_INTERNAL_DECL void duk_free_hstring_inner(duk_heap *heap, duk_hstring *h); -DUK_INTERNAL_DECL void duk_heap_free_heaphdr_raw(duk_heap *heap, duk_heaphdr *hdr); - -DUK_INTERNAL_DECL void duk_heap_insert_into_heap_allocated(duk_heap *heap, duk_heaphdr *hdr); -#if defined(DUK_USE_DOUBLE_LINKED_HEAP) && defined(DUK_USE_REFERENCE_COUNTING) -DUK_INTERNAL_DECL void duk_heap_remove_any_from_heap_allocated(duk_heap *heap, duk_heaphdr *hdr); -#endif -#if defined(DUK_USE_INTERRUPT_COUNTER) -DUK_INTERNAL_DECL void duk_heap_switch_thread(duk_heap *heap, duk_hthread *new_thr); -#endif - -#if 0 /*unused*/ -DUK_INTERNAL_DECL duk_hstring *duk_heap_string_lookup(duk_heap *heap, const duk_uint8_t *str, duk_uint32_t blen); -#endif -DUK_INTERNAL_DECL duk_hstring *duk_heap_string_intern(duk_heap *heap, const duk_uint8_t *str, duk_uint32_t blen); -DUK_INTERNAL_DECL duk_hstring *duk_heap_string_intern_checked(duk_hthread *thr, const duk_uint8_t *str, duk_uint32_t len); -#if 0 /*unused*/ -DUK_INTERNAL_DECL duk_hstring *duk_heap_string_lookup_u32(duk_heap *heap, duk_uint32_t val); -#endif -DUK_INTERNAL_DECL duk_hstring *duk_heap_string_intern_u32(duk_heap *heap, duk_uint32_t val); -DUK_INTERNAL_DECL duk_hstring *duk_heap_string_intern_u32_checked(duk_hthread *thr, duk_uint32_t val); -#if defined(DUK_USE_REFERENCE_COUNTING) -DUK_INTERNAL_DECL void duk_heap_string_remove(duk_heap *heap, duk_hstring *h); -#endif -#if defined(DUK_USE_MARK_AND_SWEEP) && defined(DUK_USE_MS_STRINGTABLE_RESIZE) -DUK_INTERNAL_DECL void duk_heap_force_strtab_resize(duk_heap *heap); -#endif -DUK_INTERNAL void duk_heap_free_strtab(duk_heap *heap); -#if defined(DUK_USE_DEBUG) -DUK_INTERNAL void duk_heap_dump_strtab(duk_heap *heap); -#endif - - -DUK_INTERNAL_DECL void duk_heap_strcache_string_remove(duk_heap *heap, duk_hstring *h); -DUK_INTERNAL_DECL duk_uint_fast32_t duk_heap_strcache_offset_char2byte(duk_hthread *thr, duk_hstring *h, duk_uint_fast32_t char_offset); - -#if defined(DUK_USE_PROVIDE_DEFAULT_ALLOC_FUNCTIONS) -DUK_INTERNAL_DECL void *duk_default_alloc_function(void *udata, duk_size_t size); -DUK_INTERNAL_DECL void *duk_default_realloc_function(void *udata, void *ptr, duk_size_t newsize); -DUK_INTERNAL_DECL void duk_default_free_function(void *udata, void *ptr); -#endif - -DUK_INTERNAL_DECL void *duk_heap_mem_alloc(duk_heap *heap, duk_size_t size); -DUK_INTERNAL_DECL void *duk_heap_mem_alloc_zeroed(duk_heap *heap, duk_size_t size); -DUK_INTERNAL_DECL void *duk_heap_mem_realloc(duk_heap *heap, void *ptr, duk_size_t newsize); -DUK_INTERNAL_DECL void *duk_heap_mem_realloc_indirect(duk_heap *heap, duk_mem_getptr cb, void *ud, duk_size_t newsize); -DUK_INTERNAL_DECL void duk_heap_mem_free(duk_heap *heap, void *ptr); - -#ifdef DUK_USE_REFERENCE_COUNTING -#if !defined(DUK_USE_FAST_REFCOUNT_DEFAULT) -DUK_INTERNAL_DECL void duk_tval_incref(duk_tval *tv); -#endif -#if 0 /* unused */ -DUK_INTERNAL_DECL void duk_tval_incref_allownull(duk_tval *tv); -#endif -DUK_INTERNAL_DECL void duk_tval_decref(duk_hthread *thr, duk_tval *tv); -#if 0 /* unused */ -DUK_INTERNAL_DECL void duk_tval_decref_allownull(duk_hthread *thr, duk_tval *tv); -#endif -#if !defined(DUK_USE_FAST_REFCOUNT_DEFAULT) -DUK_INTERNAL_DECL void duk_heaphdr_incref(duk_heaphdr *h); -#endif -#if 0 /* unused */ -DUK_INTERNAL_DECL void duk_heaphdr_incref_allownull(duk_heaphdr *h); -#endif -DUK_INTERNAL_DECL void duk_heaphdr_decref(duk_hthread *thr, duk_heaphdr *h); -DUK_INTERNAL_DECL void duk_heaphdr_decref_allownull(duk_hthread *thr, duk_heaphdr *h); -DUK_INTERNAL_DECL void duk_heaphdr_refzero(duk_hthread *thr, duk_heaphdr *h); -DUK_INTERNAL_DECL void duk_heaphdr_refcount_finalize(duk_hthread *thr, duk_heaphdr *hdr); -#else -/* no refcounting */ -#endif - -#if defined(DUK_USE_MARK_AND_SWEEP) -DUK_INTERNAL_DECL duk_bool_t duk_heap_mark_and_sweep(duk_heap *heap, duk_small_uint_t flags); -#endif - -DUK_INTERNAL_DECL duk_uint32_t duk_heap_hashstring(duk_heap *heap, const duk_uint8_t *str, duk_size_t len); - -#endif /* DUK_HEAP_H_INCLUDED */ -#line 1 "duk_debugger.h" -#ifndef DUK_DEBUGGER_H_INCLUDED -#define DUK_DEBUGGER_H_INCLUDED - -/* Debugger protocol version is defined in the public API header. */ - -/* Initial bytes for markers. */ -#define DUK_DBG_IB_EOM 0x00 -#define DUK_DBG_IB_REQUEST 0x01 -#define DUK_DBG_IB_REPLY 0x02 -#define DUK_DBG_IB_ERROR 0x03 -#define DUK_DBG_IB_NOTIFY 0x04 - -/* Other initial bytes. */ -#define DUK_DBG_IB_INT4 0x10 -#define DUK_DBG_IB_STR4 0x11 -#define DUK_DBG_IB_STR2 0x12 -#define DUK_DBG_IB_BUF4 0x13 -#define DUK_DBG_IB_BUF2 0x14 -#define DUK_DBG_IB_UNUSED 0x15 -#define DUK_DBG_IB_UNDEFINED 0x16 -#define DUK_DBG_IB_NULL 0x17 -#define DUK_DBG_IB_TRUE 0x18 -#define DUK_DBG_IB_FALSE 0x19 -#define DUK_DBG_IB_NUMBER 0x1a -#define DUK_DBG_IB_OBJECT 0x1b -#define DUK_DBG_IB_POINTER 0x1c -#define DUK_DBG_IB_LIGHTFUNC 0x1d -#define DUK_DBG_IB_HEAPPTR 0x1e -/* The short string/integer initial bytes starting from 0x60 don't have - * defines now. - */ - -/* Error codes. */ -#define DUK_DBG_ERR_UNKNOWN 0x00 -#define DUK_DBG_ERR_UNSUPPORTED 0x01 -#define DUK_DBG_ERR_TOOMANY 0x02 -#define DUK_DBG_ERR_NOTFOUND 0x03 -#define DUK_DBG_ERR_APPLICATION 0x04 - -/* Commands and notifys initiated by Duktape. */ -#define DUK_DBG_CMD_STATUS 0x01 -#define DUK_DBG_CMD_PRINT 0x02 -#define DUK_DBG_CMD_ALERT 0x03 -#define DUK_DBG_CMD_LOG 0x04 -#define DUK_DBG_CMD_THROW 0x05 -#define DUK_DBG_CMD_DETACHING 0x06 -#define DUK_DBG_CMD_APPNOTIFY 0x07 - -/* Commands initiated by debug client. */ -#define DUK_DBG_CMD_BASICINFO 0x10 -#define DUK_DBG_CMD_TRIGGERSTATUS 0x11 -#define DUK_DBG_CMD_PAUSE 0x12 -#define DUK_DBG_CMD_RESUME 0x13 -#define DUK_DBG_CMD_STEPINTO 0x14 -#define DUK_DBG_CMD_STEPOVER 0x15 -#define DUK_DBG_CMD_STEPOUT 0x16 -#define DUK_DBG_CMD_LISTBREAK 0x17 -#define DUK_DBG_CMD_ADDBREAK 0x18 -#define DUK_DBG_CMD_DELBREAK 0x19 -#define DUK_DBG_CMD_GETVAR 0x1a -#define DUK_DBG_CMD_PUTVAR 0x1b -#define DUK_DBG_CMD_GETCALLSTACK 0x1c -#define DUK_DBG_CMD_GETLOCALS 0x1d -#define DUK_DBG_CMD_EVAL 0x1e -#define DUK_DBG_CMD_DETACH 0x1f -#define DUK_DBG_CMD_DUMPHEAP 0x20 -#define DUK_DBG_CMD_GETBYTECODE 0x21 -#define DUK_DBG_CMD_APPREQUEST 0x22 -#define DUK_DBG_CMD_GETHEAPOBJINFO 0x23 -#define DUK_DBG_CMD_GETOBJPROPDESC 0x24 -#define DUK_DBG_CMD_GETOBJPROPDESCRANGE 0x25 - -/* The low 8 bits map directly to duk_hobject.h DUK_PROPDESC_FLAG_xxx. - * The remaining flags are specific to the debugger. - */ -#define DUK_DBG_PROPFLAG_INTERNAL (1 << 8) - -#if defined(DUK_USE_DEBUGGER_SUPPORT) -DUK_INTERNAL_DECL void duk_debug_do_detach(duk_heap *heap); - -DUK_INTERNAL_DECL duk_bool_t duk_debug_read_peek(duk_hthread *thr); -DUK_INTERNAL_DECL void duk_debug_write_flush(duk_hthread *thr); - -DUK_INTERNAL_DECL void duk_debug_skip_bytes(duk_hthread *thr, duk_size_t length); -DUK_INTERNAL_DECL void duk_debug_skip_byte(duk_hthread *thr); - -DUK_INTERNAL_DECL void duk_debug_read_bytes(duk_hthread *thr, duk_uint8_t *data, duk_size_t length); -DUK_INTERNAL_DECL duk_uint8_t duk_debug_read_byte(duk_hthread *thr); -DUK_INTERNAL_DECL duk_int32_t duk_debug_read_int(duk_hthread *thr); -DUK_INTERNAL_DECL duk_hstring *duk_debug_read_hstring(duk_hthread *thr); -/* XXX: exposed duk_debug_read_pointer */ -/* XXX: exposed duk_debug_read_buffer */ -/* XXX: exposed duk_debug_read_hbuffer */ -#if 0 -DUK_INTERNAL_DECL duk_heaphdr *duk_debug_read_heapptr(duk_hthread *thr); -#endif -#if defined(DUK_USE_DEBUGGER_INSPECT) -DUK_INTERNAL_DECL duk_heaphdr *duk_debug_read_any_ptr(duk_hthread *thr); -#endif -DUK_INTERNAL_DECL duk_tval *duk_debug_read_tval(duk_hthread *thr); - -DUK_INTERNAL_DECL void duk_debug_write_bytes(duk_hthread *thr, const duk_uint8_t *data, duk_size_t length); -DUK_INTERNAL_DECL void duk_debug_write_byte(duk_hthread *thr, duk_uint8_t x); -DUK_INTERNAL_DECL void duk_debug_write_unused(duk_hthread *thr); -DUK_INTERNAL_DECL void duk_debug_write_undefined(duk_hthread *thr); -#if defined(DUK_USE_DEBUGGER_INSPECT) -DUK_INTERNAL_DECL void duk_debug_write_null(duk_hthread *thr); -#endif -DUK_INTERNAL_DECL void duk_debug_write_boolean(duk_hthread *thr, duk_uint_t val); -DUK_INTERNAL_DECL void duk_debug_write_int(duk_hthread *thr, duk_int32_t x); -DUK_INTERNAL_DECL void duk_debug_write_uint(duk_hthread *thr, duk_uint32_t x); -DUK_INTERNAL_DECL void duk_debug_write_string(duk_hthread *thr, const char *data, duk_size_t length); -DUK_INTERNAL_DECL void duk_debug_write_cstring(duk_hthread *thr, const char *data); -DUK_INTERNAL_DECL void duk_debug_write_hstring(duk_hthread *thr, duk_hstring *h); -DUK_INTERNAL_DECL void duk_debug_write_buffer(duk_hthread *thr, const char *data, duk_size_t length); -DUK_INTERNAL_DECL void duk_debug_write_hbuffer(duk_hthread *thr, duk_hbuffer *h); -DUK_INTERNAL_DECL void duk_debug_write_pointer(duk_hthread *thr, void *ptr); -#if defined(DUK_USE_DEBUGGER_DUMPHEAP) || defined(DUK_USE_DEBUGGER_INSPECT) -DUK_INTERNAL_DECL void duk_debug_write_heapptr(duk_hthread *thr, duk_heaphdr *h); -#endif -DUK_INTERNAL_DECL void duk_debug_write_hobject(duk_hthread *thr, duk_hobject *obj); -DUK_INTERNAL_DECL void duk_debug_write_tval(duk_hthread *thr, duk_tval *tv); -#if 0 /* unused */ -DUK_INTERNAL_DECL void duk_debug_write_request(duk_hthread *thr, duk_small_uint_t command); -#endif -DUK_INTERNAL_DECL void duk_debug_write_reply(duk_hthread *thr); -DUK_INTERNAL_DECL void duk_debug_write_error_eom(duk_hthread *thr, duk_small_uint_t err_code, const char *msg); -DUK_INTERNAL_DECL void duk_debug_write_notify(duk_hthread *thr, duk_small_uint_t command); -DUK_INTERNAL_DECL void duk_debug_write_eom(duk_hthread *thr); - -DUK_INTERNAL_DECL duk_uint_fast32_t duk_debug_curr_line(duk_hthread *thr); -DUK_INTERNAL_DECL void duk_debug_send_status(duk_hthread *thr); -#if defined(DUK_USE_DEBUGGER_THROW_NOTIFY) -DUK_INTERNAL_DECL void duk_debug_send_throw(duk_hthread *thr, duk_bool_t fatal); -#endif - -DUK_INTERNAL_DECL void duk_debug_halt_execution(duk_hthread *thr, duk_bool_t use_prev_pc); -DUK_INTERNAL_DECL duk_bool_t duk_debug_process_messages(duk_hthread *thr, duk_bool_t no_block); - -DUK_INTERNAL_DECL duk_small_int_t duk_debug_add_breakpoint(duk_hthread *thr, duk_hstring *filename, duk_uint32_t line); -DUK_INTERNAL_DECL duk_bool_t duk_debug_remove_breakpoint(duk_hthread *thr, duk_small_uint_t breakpoint_index); -#endif - -#endif /* DUK_DEBUGGER_H_INCLUDED */ -#line 1 "duk_debug.h" -/* - * Debugging macros, DUK_DPRINT() and its variants in particular. - * - * DUK_DPRINT() allows formatted debug prints, and supports standard - * and Duktape specific formatters. See duk_debug_vsnprintf.c for details. - * - * DUK_D(x), DUK_DD(x), and DUK_DDD(x) are used together with log macros - * for technical reasons. They are concretely used to hide 'x' from the - * compiler when the corresponding log level is disabled. This allows - * clean builds on non-C99 compilers, at the cost of more verbose code. - * Examples: - * - * DUK_D(DUK_DPRINT("foo")); - * DUK_DD(DUK_DDPRINT("foo")); - * DUK_DDD(DUK_DDDPRINT("foo")); - * - * This approach is preferable to the old "double parentheses" hack because - * double parentheses make the C99 solution worse: __FILE__ and __LINE__ can - * no longer be added transparently without going through globals, which - * works poorly with threading. - */ - -#ifndef DUK_DEBUG_H_INCLUDED -#define DUK_DEBUG_H_INCLUDED - -#ifdef DUK_USE_DEBUG - -#if defined(DUK_USE_DPRINT) -#define DUK_D(x) x -#else -#define DUK_D(x) do { } while (0) /* omit */ -#endif - -#if defined(DUK_USE_DDPRINT) -#define DUK_DD(x) x -#else -#define DUK_DD(x) do { } while (0) /* omit */ -#endif - -#if defined(DUK_USE_DDDPRINT) -#define DUK_DDD(x) x -#else -#define DUK_DDD(x) do { } while (0) /* omit */ -#endif - -/* - * Exposed debug macros: debugging enabled - */ - -#define DUK_LEVEL_DEBUG 1 -#define DUK_LEVEL_DDEBUG 2 -#define DUK_LEVEL_DDDEBUG 3 - -#ifdef DUK_USE_VARIADIC_MACROS - -/* Note: combining __FILE__, __LINE__, and __func__ into fmt would be - * possible compile time, but waste some space with shared function names. - */ -#define DUK__DEBUG_LOG(lev,...) duk_debug_log((duk_small_int_t) (lev), DUK_FILE_MACRO, (duk_int_t) DUK_LINE_MACRO, DUK_FUNC_MACRO, __VA_ARGS__); - -#define DUK_DPRINT(...) DUK__DEBUG_LOG(DUK_LEVEL_DEBUG, __VA_ARGS__) - -#ifdef DUK_USE_DDPRINT -#define DUK_DDPRINT(...) DUK__DEBUG_LOG(DUK_LEVEL_DDEBUG, __VA_ARGS__) -#else -#define DUK_DDPRINT(...) -#endif - -#ifdef DUK_USE_DDDPRINT -#define DUK_DDDPRINT(...) DUK__DEBUG_LOG(DUK_LEVEL_DDDEBUG, __VA_ARGS__) -#else -#define DUK_DDDPRINT(...) -#endif - -#else /* DUK_USE_VARIADIC_MACROS */ - -#define DUK__DEBUG_STASH(lev) \ - (void) DUK_SNPRINTF(duk_debug_file_stash, DUK_DEBUG_STASH_SIZE, "%s", (const char *) DUK_FILE_MACRO), \ - duk_debug_file_stash[DUK_DEBUG_STASH_SIZE - 1] = (char) 0; \ - (void) DUK_SNPRINTF(duk_debug_line_stash, DUK_DEBUG_STASH_SIZE, "%ld", (long) DUK_LINE_MACRO), \ - duk_debug_line_stash[DUK_DEBUG_STASH_SIZE - 1] = (char) 0; \ - (void) DUK_SNPRINTF(duk_debug_func_stash, DUK_DEBUG_STASH_SIZE, "%s", (const char *) DUK_FUNC_MACRO), \ - duk_debug_func_stash[DUK_DEBUG_STASH_SIZE - 1] = (char) 0; \ - (void) (duk_debug_level_stash = (lev)) - -/* Without variadic macros resort to comma expression trickery to handle debug - * prints. This generates a lot of harmless warnings. These hacks are not - * needed normally because DUK_D() and friends will hide the entire debug log - * statement from the compiler. - */ - -#ifdef DUK_USE_DPRINT -#define DUK_DPRINT DUK__DEBUG_STASH(DUK_LEVEL_DEBUG), (void) duk_debug_log /* args go here in parens */ -#else -#define DUK_DPRINT 0 && /* args go here as a comma expression in parens */ -#endif - -#ifdef DUK_USE_DDPRINT -#define DUK_DDPRINT DUK__DEBUG_STASH(DUK_LEVEL_DDEBUG), (void) duk_debug_log /* args go here in parens */ -#else -#define DUK_DDPRINT 0 && /* args */ -#endif - -#ifdef DUK_USE_DDDPRINT -#define DUK_DDDPRINT DUK__DEBUG_STASH(DUK_LEVEL_DDDEBUG), (void) duk_debug_log /* args go here in parens */ -#else -#define DUK_DDDPRINT 0 && /* args */ -#endif - -#endif /* DUK_USE_VARIADIC_MACROS */ - -#else /* DUK_USE_DEBUG */ - -/* - * Exposed debug macros: debugging disabled - */ - -#define DUK_D(x) do { } while (0) /* omit */ -#define DUK_DD(x) do { } while (0) /* omit */ -#define DUK_DDD(x) do { } while (0) /* omit */ - -#ifdef DUK_USE_VARIADIC_MACROS - -#define DUK_DPRINT(...) -#define DUK_DDPRINT(...) -#define DUK_DDDPRINT(...) - -#else /* DUK_USE_VARIADIC_MACROS */ - -#define DUK_DPRINT 0 && /* args go here as a comma expression in parens */ -#define DUK_DDPRINT 0 && /* args */ -#define DUK_DDDPRINT 0 && /* args */ - -#endif /* DUK_USE_VARIADIC_MACROS */ - -#endif /* DUK_USE_DEBUG */ - -/* - * Structs - */ - -#ifdef DUK_USE_DEBUG -struct duk_fixedbuffer { - duk_uint8_t *buffer; - duk_size_t length; - duk_size_t offset; - duk_bool_t truncated; -}; -#endif - -/* - * Prototypes - */ - -#ifdef DUK_USE_DEBUG -DUK_INTERNAL_DECL duk_int_t duk_debug_vsnprintf(char *str, duk_size_t size, const char *format, va_list ap); -#if 0 /*unused*/ -DUK_INTERNAL_DECL duk_int_t duk_debug_snprintf(char *str, duk_size_t size, const char *format, ...); -#endif -DUK_INTERNAL_DECL void duk_debug_format_funcptr(char *buf, duk_size_t buf_size, duk_uint8_t *fptr, duk_size_t fptr_size); - -#ifdef DUK_USE_VARIADIC_MACROS -DUK_INTERNAL_DECL void duk_debug_log(duk_small_int_t level, const char *file, duk_int_t line, const char *func, const char *fmt, ...); -#else /* DUK_USE_VARIADIC_MACROS */ -/* parameter passing, not thread safe */ -#define DUK_DEBUG_STASH_SIZE 128 -#if !defined(DUK_SINGLE_FILE) -DUK_INTERNAL_DECL char duk_debug_file_stash[DUK_DEBUG_STASH_SIZE]; -DUK_INTERNAL_DECL char duk_debug_line_stash[DUK_DEBUG_STASH_SIZE]; -DUK_INTERNAL_DECL char duk_debug_func_stash[DUK_DEBUG_STASH_SIZE]; -DUK_INTERNAL_DECL duk_small_int_t duk_debug_level_stash; -#endif -DUK_INTERNAL_DECL void duk_debug_log(const char *fmt, ...); -#endif /* DUK_USE_VARIADIC_MACROS */ - -DUK_INTERNAL_DECL void duk_fb_put_bytes(duk_fixedbuffer *fb, const duk_uint8_t *buffer, duk_size_t length); -DUK_INTERNAL_DECL void duk_fb_put_byte(duk_fixedbuffer *fb, duk_uint8_t x); -DUK_INTERNAL_DECL void duk_fb_put_cstring(duk_fixedbuffer *fb, const char *x); -DUK_INTERNAL_DECL void duk_fb_sprintf(duk_fixedbuffer *fb, const char *fmt, ...); -DUK_INTERNAL_DECL void duk_fb_put_funcptr(duk_fixedbuffer *fb, duk_uint8_t *fptr, duk_size_t fptr_size); -DUK_INTERNAL_DECL duk_bool_t duk_fb_is_full(duk_fixedbuffer *fb); - -#endif /* DUK_USE_DEBUG */ - -#endif /* DUK_DEBUG_H_INCLUDED */ -#line 1 "duk_error.h" -/* - * Error handling macros, assertion macro, error codes. - * - * There are three level of 'errors': - * - * 1. Ordinary errors, relative to a thread, cause a longjmp, catchable. - * 2. Fatal errors, relative to a heap, cause fatal handler to be called. - * 3. Panic errors, unrelated to a heap and cause a process exit. - * - * Panics are used by the default fatal error handler and by debug code - * such as assertions. By providing a proper fatal error handler, user - * code can avoid panics in non-debug builds. - */ - -#ifndef DUK_ERROR_H_INCLUDED -#define DUK_ERROR_H_INCLUDED - -/* - * Error codes: defined in duktape.h - * - * Error codes are used as a shorthand to throw exceptions from inside - * the implementation. The appropriate Ecmascript object is constructed - * based on the code. Ecmascript code throws objects directly. The error - * codes are defined in the public API header because they are also used - * by calling code. - */ - -/* - * Normal error - * - * Normal error is thrown with a longjmp() through the current setjmp() - * catchpoint record in the duk_heap. The 'curr_thread' of the duk_heap - * identifies the throwing thread. - * - * Error formatting is usually unnecessary. The error macros provide a - * zero argument version (no formatting) and separate macros for small - * argument counts. Variadic macros are not used to avoid portability - * issues and avoid the need for stash-based workarounds when they're not - * available. Vararg calls are avoided for non-formatted error calls - * because vararg call sites are larger than normal, and there are a lot - * of call sites with no formatting. - * - * Note that special formatting provided by debug macros is NOT available. - * - * The _RAW variants allow the caller to specify file and line. This makes - * it easier to write checked calls which want to use the call site of the - * checked function, not the error macro call inside the checked function. - */ - -#if defined(DUK_USE_VERBOSE_ERRORS) - -/* Because there are quite many call sites, pack error code (require at most - * 8-bit) into a single argument. - */ -#define DUK_ERROR(thr,err,msg) do { \ - duk_errcode_t duk__err = (err); duk_int_t duk__line = (duk_int_t) DUK_LINE_MACRO; \ - DUK_ASSERT(duk__err >= 0 && duk__err <= 0xff); DUK_ASSERT(duk__line >= 0 && duk__line <= 0x00ffffffL); \ - duk_err_handle_error((thr), DUK_FILE_MACRO, (((duk_uint_t) duk__err) << 24) | ((duk_uint_t) duk__line), (msg)); \ - } while (0) -#define DUK_ERROR_RAW(thr,file,line,err,msg) do { \ - duk_errcode_t duk__err = (err); duk_int_t duk__line = (duk_int_t) (line); \ - DUK_ASSERT(duk__err >= 0 && duk__err <= 0xff); DUK_ASSERT(duk__line >= 0 && duk__line <= 0x00ffffffL); \ - duk_err_handle_error((thr), (file), (((duk_uint_t) duk__err) << 24) | ((duk_uint_t) duk__line), (msg)); \ - } while (0) - -#define DUK_ERROR_FMT1(thr,err,fmt,arg1) do { \ - duk_errcode_t duk__err = (err); duk_int_t duk__line = (duk_int_t) DUK_LINE_MACRO; \ - DUK_ASSERT(duk__err >= 0 && duk__err <= 0xff); DUK_ASSERT(duk__line >= 0 && duk__line <= 0x00ffffffL); \ - duk_err_handle_error_fmt((thr), DUK_FILE_MACRO, (((duk_uint_t) duk__err) << 24) | ((duk_uint_t) duk__line), (fmt), (arg1)); \ - } while (0) -#define DUK_ERROR_RAW_FMT1(thr,file,line,err,fmt,arg1) do { \ - duk_errcode_t duk__err = (err); duk_int_t duk__line = (duk_int_t) (line); \ - DUK_ASSERT(duk__err >= 0 && duk__err <= 0xff); DUK_ASSERT(duk__line >= 0 && duk__line <= 0x00ffffffL); \ - duk_err_handle_error_fmt((thr), (file), (((duk_uint_t) duk__err) << 24) | ((duk_uint_t) duk__line), (fmt), (arg1)); \ - } while (0) - -#define DUK_ERROR_FMT2(thr,err,fmt,arg1,arg2) do { \ - duk_errcode_t duk__err = (err); duk_int_t duk__line = (duk_int_t) DUK_LINE_MACRO; \ - DUK_ASSERT(duk__err >= 0 && duk__err <= 0xff); DUK_ASSERT(duk__line >= 0 && duk__line <= 0x00ffffffL); \ - duk_err_handle_error_fmt((thr), DUK_FILE_MACRO, (((duk_uint_t) duk__err) << 24) | ((duk_uint_t) duk__line), (fmt), (arg1), (arg2)); \ - } while (0) -#define DUK_ERROR_RAW_FMT2(thr,file,line,err,fmt,arg1,arg2) do { \ - duk_errcode_t duk__err = (err); duk_int_t duk__line = (duk_int_t) (line); \ - DUK_ASSERT(duk__err >= 0 && duk__err <= 0xff); DUK_ASSERT(duk__line >= 0 && duk__line <= 0x00ffffffL); \ - duk_err_handle_error_fmt((thr), (file), (((duk_uint_t) duk__err) << 24) | ((duk_uint_t) duk__line), (fmt), (arg1), (arg2)); \ - } while (0) - -#define DUK_ERROR_FMT3(thr,err,fmt,arg1,arg2,arg3) do { \ - duk_errcode_t duk__err = (err); duk_int_t duk__line = (duk_int_t) DUK_LINE_MACRO; \ - DUK_ASSERT(duk__err >= 0 && duk__err <= 0xff); DUK_ASSERT(duk__line >= 0 && duk__line <= 0x00ffffffL); \ - duk_err_handle_error_fmt((thr), DUK_FILE_MACRO, (((duk_uint_t) duk__err) << 24) | ((duk_uint_t) duk__line), (fmt), (arg1), (arg2), (arg3)); \ - } while (0) -#define DUK_ERROR_RAW_FMT3(thr,file,line,err,fmt,arg1,arg2,arg3) do { \ - duk_errcode_t duk__err = (err); duk_int_t duk__line = (duk_int_t) (line); \ - DUK_ASSERT(duk__err >= 0 && duk__err <= 0xff); DUK_ASSERT(duk__line >= 0 && duk__line <= 0x00ffffffL); \ - duk_err_handle_error_fmt((thr), (file), (((duk_uint_t) duk__err) << 24) | ((duk_uint_t) duk__line), (fmt), (arg1), (arg2), (arg3)); \ - } while (0) - -#define DUK_ERROR_FMT4(thr,err,fmt,arg1,arg2,arg3,arg4) do { \ - duk_errcode_t duk__err = (err); duk_int_t duk__line = (duk_int_t) DUK_LINE_MACRO; \ - DUK_ASSERT(duk__err >= 0 && duk__err <= 0xff); DUK_ASSERT(duk__line >= 0 && duk__line <= 0x00ffffffL); \ - duk_err_handle_error_fmt((thr), DUK_FILE_MACRO, (((duk_uint_t) duk__err) << 24) | ((duk_uint_t) duk__line), (fmt), (arg1), (arg2), (arg3), (arg4)); \ - } while (0) -#define DUK_ERROR_RAW_FMT4(thr,file,line,err,fmt,arg1,arg2,arg3,arg4) do { \ - duk_errcode_t duk__err = (err); duk_int_t duk__line = (duk_int_t) (line); \ - DUK_ASSERT(duk__err >= 0 && duk__err <= 0xff); DUK_ASSERT(duk__line >= 0 && duk__line <= 0x00ffffffL); \ - duk_err_handle_error_fmt((thr), (file), (((duk_uint_t) duk__err) << 24) | ((duk_uint_t) duk__line), (fmt), (arg1), (arg2), (arg3), (arg4)); \ - } while (0) - -#else /* DUK_USE_VERBOSE_ERRORS */ - -#define DUK_ERROR(thr,err,msg) duk_err_handle_error((thr), (err)) -#define DUK_ERROR_RAW(thr,file,line,err,msg) duk_err_handle_error((thr), (err)) - -#define DUK_ERROR_FMT1(thr,err,fmt,arg1) DUK_ERROR((thr),(err),(fmt)) -#define DUK_ERROR_RAW_FMT1(thr,file,line,err,fmt,arg1) DUK_ERROR_RAW((thr),(file),(line),(err),(fmt)) - -#define DUK_ERROR_FMT2(thr,err,fmt,arg1,arg2) DUK_ERROR((thr),(err),(fmt)) -#define DUK_ERROR_RAW_FMT2(thr,file,line,err,fmt,arg1,arg2) DUK_ERROR_RAW((thr),(file),(line),(err),(fmt)) - -#define DUK_ERROR_FMT3(thr,err,fmt,arg1,arg2,arg3) DUK_ERROR((thr),(err),(fmt)) -#define DUK_ERROR_RAW_FMT3(thr,file,line,err,fmt,arg1,arg2,arg3) DUK_ERROR_RAW((thr),(file),(line),(err),(fmt)) - -#define DUK_ERROR_FMT4(thr,err,fmt,arg1,arg2,arg3,arg4) DUK_ERROR((thr),(err),(fmt)) -#define DUK_ERROR_RAW_FMT4(thr,file,line,err,fmt,arg1,arg2,arg3,arg4) DUK_ERROR_RAW((thr),(file),(line),(err),(fmt)) - -#endif /* DUK_USE_VERBOSE_ERRORS */ - -/* - * Fatal error - * - * There are no fatal error macros at the moment. There are so few call - * sites that the fatal error handler is called directly. - */ - -/* - * Panic error - * - * Panic errors are not relative to either a heap or a thread, and cause - * DUK_PANIC() macro to be invoked. Unless a user provides DUK_USE_PANIC_HANDLER, - * DUK_PANIC() calls a helper which prints out the error and causes a process - * exit. - * - * The user can override the macro to provide custom handling. A macro is - * used to allow the user to have inline panic handling if desired (without - * causing a potentially risky function call). - * - * Panics are only used in debug code such as assertions, and by the default - * fatal error handler. - */ - -#if defined(DUK_USE_PANIC_HANDLER) -/* already defined, good */ -#define DUK_PANIC(code,msg) DUK_USE_PANIC_HANDLER((code),(msg)) -#else -#define DUK_PANIC(code,msg) duk_default_panic_handler((code),(msg)) -#endif /* DUK_USE_PANIC_HANDLER */ - -/* - * Assert macro: failure causes panic. - */ - -#if defined(DUK_USE_ASSERTIONS) - -/* the message should be a compile time constant without formatting (less risk); - * we don't care about assertion text size because they're not used in production - * builds. - */ -#define DUK_ASSERT(x) do { \ - if (!(x)) { \ - DUK_PANIC(DUK_ERR_ASSERTION_ERROR, \ - "assertion failed: " #x \ - " (" DUK_FILE_MACRO ":" DUK_MACRO_STRINGIFY(DUK_LINE_MACRO) ")"); \ - } \ - } while (0) - -/* Assertion compatible inside a comma expression, evaluates to void. - * Currently not compatible with DUK_USE_PANIC_HANDLER() which may have - * a statement block. - */ -#if defined(DUK_USE_PANIC_HANDLER) -/* XXX: resolve macro definition issue or call through a helper function? */ -#define DUK_ASSERT_EXPR(x) ((void) 0) -#else -#define DUK_ASSERT_EXPR(x) \ - ((void) ((x) ? 0 : (DUK_PANIC(DUK_ERR_ASSERTION_ERROR, \ - "assertion failed: " #x \ - " (" DUK_FILE_MACRO ":" DUK_MACRO_STRINGIFY(DUK_LINE_MACRO) ")"), 0))) -#endif - -#else /* DUK_USE_ASSERTIONS */ - -#define DUK_ASSERT(x) do { /* assertion omitted */ } while (0) - -#define DUK_ASSERT_EXPR(x) ((void) 0) - -#endif /* DUK_USE_ASSERTIONS */ - -/* this variant is used when an assert would generate a compile warning by - * being always true (e.g. >= 0 comparison for an unsigned value - */ -#define DUK_ASSERT_DISABLE(x) do { /* assertion disabled */ } while (0) - -/* - * Assertion helpers - */ - -#if defined(DUK_USE_ASSERTIONS) && defined(DUK_USE_REFERENCE_COUNTING) -#define DUK_ASSERT_REFCOUNT_NONZERO_HEAPHDR(h) do { \ - DUK_ASSERT((h) == NULL || DUK_HEAPHDR_GET_REFCOUNT((duk_heaphdr *) (h)) > 0); \ - } while (0) -#define DUK_ASSERT_REFCOUNT_NONZERO_TVAL(tv) do { \ - if ((tv) != NULL && DUK_TVAL_IS_HEAP_ALLOCATED((tv))) { \ - DUK_ASSERT(DUK_HEAPHDR_GET_REFCOUNT(DUK_TVAL_GET_HEAPHDR((tv))) > 0); \ - } \ - } while (0) -#else -#define DUK_ASSERT_REFCOUNT_NONZERO_HEAPHDR(h) /* no refcount check */ -#define DUK_ASSERT_REFCOUNT_NONZERO_TVAL(tv) /* no refcount check */ -#endif - -#define DUK_ASSERT_TOP(ctx,n) DUK_ASSERT((duk_idx_t) duk_get_top((ctx)) == (duk_idx_t) (n)) - -#if defined(DUK_USE_ASSERTIONS) && defined(DUK_USE_PACKED_TVAL) -#define DUK_ASSERT_DOUBLE_IS_NORMALIZED(dval) do { \ - duk_double_union duk__assert_tmp_du; \ - duk__assert_tmp_du.d = (dval); \ - DUK_ASSERT(DUK_DBLUNION_IS_NORMALIZED(&duk__assert_tmp_du)); \ - } while (0) -#else -#define DUK_ASSERT_DOUBLE_IS_NORMALIZED(dval) /* nop */ -#endif - -/* - * Helper for valstack space - * - * Caller of DUK_ASSERT_VALSTACK_SPACE() estimates the number of free stack entries - * required for its own use, and any child calls which are not (a) Duktape API calls - * or (b) Duktape calls which involve extending the valstack (e.g. getter call). - */ - -#define DUK_VALSTACK_ASSERT_EXTRA 5 /* this is added to checks to allow for Duktape - * API calls in addition to function's own use - */ -#if defined(DUK_USE_ASSERTIONS) -#define DUK_ASSERT_VALSTACK_SPACE(thr,n) do { \ - DUK_ASSERT((thr) != NULL); \ - DUK_ASSERT((thr)->valstack_end - (thr)->valstack_top >= (n) + DUK_VALSTACK_ASSERT_EXTRA); \ - } while (0) -#else -#define DUK_ASSERT_VALSTACK_SPACE(thr,n) /* no valstack space check */ -#endif - -/* - * Error throwing helpers - * - * The goal is to provide verbose and configurable error messages. Call - * sites should be clean in source code and compile to a small footprint. - * Small footprint is also useful for performance because small cold paths - * reduce code cache pressure. Adding macros here only makes sense if there - * are enough call sites to get concrete benefits. - */ - -#if defined(DUK_USE_VERBOSE_ERRORS) -/* Verbose errors with key/value summaries (non-paranoid) or without key/value - * summaries (paranoid, for some security sensitive environments), the paranoid - * vs. non-paranoid distinction affects only a few specific errors. - */ -#if defined(DUK_USE_PARANOID_ERRORS) -#define DUK_ERROR_REQUIRE_TYPE_INDEX(thr,index,expectname,lowmemstr) do { \ - duk_err_require_type_index((thr), DUK_FILE_MACRO, (duk_int_t) DUK_LINE_MACRO, (index), (expectname)); \ - } while (0) -#else /* DUK_USE_PARANOID_ERRORS */ -#define DUK_ERROR_REQUIRE_TYPE_INDEX(thr,index,expectname,lowmemstr) do { \ - duk_err_require_type_index((thr), DUK_FILE_MACRO, (duk_int_t) DUK_LINE_MACRO, (index), (expectname)); \ - } while (0) -#endif /* DUK_USE_PARANOID_ERRORS */ - -#define DUK_ERROR_UNIMPLEMENTED(thr,msg) do { \ - DUK_ERROR((thr), DUK_ERR_UNIMPLEMENTED_ERROR, (msg)); \ - } while (0) -#define DUK_ERROR_UNIMPLEMENTED_DEFMSG(thr) do { \ - duk_err_unimplemented_defmsg((thr), DUK_FILE_MACRO, (duk_int_t) DUK_LINE_MACRO); \ - } while (0) -#define DUK_ERROR_UNSUPPORTED(thr,msg) do { \ - DUK_ERROR((thr), DUK_ERR_UNSUPPORTED_ERROR, (msg)); \ - } while (0) -#ifndef DUK_USE_BYTECODE_DUMP_SUPPORT -#define DUK_ERROR_UNSUPPORTED_DEFMSG(thr) do { \ - duk_err_unsupported_defmsg((thr), DUK_FILE_MACRO, (duk_int_t) DUK_LINE_MACRO); \ - } while (0) -#endif -#define DUK_ERROR_INTERNAL(thr,msg) do { \ - duk_err_internal((thr), DUK_FILE_MACRO, (duk_int_t) DUK_LINE_MACRO, (msg)); \ - } while (0) -#define DUK_ERROR_INTERNAL_DEFMSG(thr) do { \ - duk_err_internal_defmsg((thr), DUK_FILE_MACRO, (duk_int_t) DUK_LINE_MACRO); \ - } while (0) -#define DUK_ERROR_ALLOC(thr,msg) do { \ - duk_err_alloc((thr), DUK_FILE_MACRO, (duk_int_t) DUK_LINE_MACRO, (msg)); \ - } while (0) -#define DUK_ERROR_ALLOC_DEFMSG(thr) do { \ - DUK_ERROR_ALLOC((thr), DUK_STR_ALLOC_FAILED); \ - } while (0) -/* DUK_ERR_ASSERTION_ERROR: no macros needed */ -#define DUK_ERROR_API_INDEX(thr,index) do { \ - duk_err_api_index((thr), DUK_FILE_MACRO, (duk_int_t) DUK_LINE_MACRO, (index)); \ - } while (0) -#define DUK_ERROR_API(thr,msg) do { \ - duk_err_api((thr), DUK_FILE_MACRO, (duk_int_t) DUK_LINE_MACRO, (msg)); \ - } while (0) -/* DUK_ERR_UNCAUGHT_ERROR: no macros needed */ -/* DUK_ERR_ERROR: no macros needed */ -/* DUK_ERR_EVAL: no macros needed */ -#define DUK_ERROR_RANGE(thr,msg) do { \ - duk_err_range((thr), DUK_FILE_MACRO, (duk_int_t) DUK_LINE_MACRO, (msg)); \ - } while (0) -/* DUK_ERR_REFERENCE_ERROR: no macros needed */ -#define DUK_ERROR_SYNTAX(thr,msg) do { \ - DUK_ERROR((thr), DUK_ERR_SYNTAX_ERROR, (msg)); \ - } while (0) -#define DUK_ERROR_TYPE(thr,msg) do { \ - DUK_ERROR((thr), DUK_ERR_TYPE_ERROR, (msg)); \ - } while (0) -/* DUK_ERR_URI_ERROR: no macros needed */ -#else /* DUK_USE_VERBOSE_ERRORS */ -/* Non-verbose errors for low memory targets: no file, line, or message. */ - -#define DUK_ERROR_REQUIRE_TYPE_INDEX(thr,index,expectname,lowmemstr) do { \ - duk_err_type((thr)); \ - } while (0) - -#define DUK_ERROR_UNIMPLEMENTED(thr,msg) do { \ - duk_err_unimplemented((thr)); \ - } while (0) -#define DUK_ERROR_UNIMPLEMENTED_DEFMSG(thr) do { \ - duk_err_unimplemented((thr)); \ - } while (0) -#define DUK_ERROR_UNSUPPORTED(thr,msg) do { \ - duk_err_unsupported((thr)); \ - } while (0) -#define DUK_ERROR_UNSUPPORTED_DEFMSG(thr) do { \ - duk_err_unsupported((thr)); \ - } while (0) -#define DUK_ERROR_INTERNAL(thr,msg) do { \ - duk_err_internal((thr)); \ - } while (0) -#define DUK_ERROR_INTERNAL_DEFMSG(thr) do { \ - duk_err_internal((thr)); \ - } while (0) -#define DUK_ERROR_ALLOC(thr,msg) do { \ - duk_err_alloc((thr)); \ - } while (0) -#define DUK_ERROR_ALLOC_DEFMSG(thr) do { \ - duk_err_alloc((thr)); \ - } while (0) -#define DUK_ERROR_API_INDEX(thr,index) do { \ - duk_err_api((thr)); \ - } while (0) -#define DUK_ERROR_API(thr,msg) do { \ - duk_err_api((thr)); \ - } while (0) -#define DUK_ERROR_RANGE(thr,msg) do { \ - duk_err_range((thr)); \ - } while (0) -#define DUK_ERROR_SYNTAX(thr,msg) do { \ - duk_err_syntax((thr)); \ - } while (0) -#define DUK_ERROR_TYPE(thr,msg) do { \ - duk_err_type((thr)); \ - } while (0) -#endif /* DUK_USE_VERBOSE_ERRORS */ - -/* - * Prototypes - */ - -#if defined(DUK_USE_VERBOSE_ERRORS) -DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_handle_error(duk_hthread *thr, const char *filename, duk_uint_t line_and_code, const char *msg)); -DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_handle_error_fmt(duk_hthread *thr, const char *filename, duk_uint_t line_and_code, const char *fmt, ...)); -#else /* DUK_USE_VERBOSE_ERRORS */ -DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_handle_error(duk_hthread *thr, duk_errcode_t code)); -#endif /* DUK_USE_VERBOSE_ERRORS */ - -#if defined(DUK_USE_VERBOSE_ERRORS) -DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_create_and_throw(duk_hthread *thr, duk_errcode_t code, const char *msg, const char *filename, duk_int_t line)); -#else -DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_create_and_throw(duk_hthread *thr, duk_errcode_t code)); -#endif - -DUK_NORETURN(DUK_INTERNAL_DECL void duk_error_throw_from_negative_rc(duk_hthread *thr, duk_ret_t rc)); - -#if defined(DUK_USE_AUGMENT_ERROR_CREATE) -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); -#endif -#if defined(DUK_USE_AUGMENT_ERROR_THROW) -DUK_INTERNAL_DECL void duk_err_augment_error_throw(duk_hthread *thr); -#endif - -#if defined(DUK_USE_VERBOSE_ERRORS) -#if defined(DUK_USE_PARANOID_ERRORS) -DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_require_type_index(duk_hthread *thr, const char *filename, duk_int_t linenumber, duk_idx_t index, const char *expect_name)); -#else -DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_require_type_index(duk_hthread *thr, const char *filename, duk_int_t linenumber, duk_idx_t index, const char *expect_name)); -#endif -DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_api_index(duk_hthread *thr, const char *filename, duk_int_t linenumber, duk_idx_t index)); -DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_api(duk_hthread *thr, const char *filename, duk_int_t linenumber, const char *message)); -DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_range(duk_hthread *thr, const char *filename, duk_int_t linenumber, const char *message)); -DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_unimplemented_defmsg(duk_hthread *thr, const char *filename, duk_int_t linenumber)); -#ifndef DUK_USE_BYTECODE_DUMP_SUPPORT -DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_unsupported_defmsg(duk_hthread *thr, const char *filename, duk_int_t linenumber)); -#endif -DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_internal_defmsg(duk_hthread *thr, const char *filename, duk_int_t linenumber)); -DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_internal(duk_hthread *thr, const char *filename, duk_int_t linenumber, const char *message)); -DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_alloc(duk_hthread *thr, const char *filename, duk_int_t linenumber, const char *message)); -#else /* DUK_VERBOSE_ERRORS */ -DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_range(duk_hthread *thr)); -DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_syntax(duk_hthread *thr)); -DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_type(duk_hthread *thr)); -DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_api(duk_hthread *thr)); -DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_unimplemented(duk_hthread *thr)); -DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_unsupported(duk_hthread *thr)); -DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_internal(duk_hthread *thr)); -DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_alloc(duk_hthread *thr)); -#endif /* DUK_VERBOSE_ERRORS */ - -DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_longjmp(duk_hthread *thr)); - -DUK_NORETURN(DUK_INTERNAL_DECL void duk_default_fatal_handler(duk_context *ctx, duk_errcode_t code, const char *msg)); - -#if !defined(DUK_USE_PANIC_HANDLER) -DUK_NORETURN(DUK_INTERNAL_DECL void duk_default_panic_handler(duk_errcode_t code, const char *msg)); -#endif - -DUK_INTERNAL_DECL void duk_err_setup_heap_ljstate(duk_hthread *thr, duk_small_int_t lj_type); - -DUK_INTERNAL_DECL duk_hobject *duk_error_prototype_from_code(duk_hthread *thr, duk_errcode_t err_code); - -#endif /* DUK_ERROR_H_INCLUDED */ -#line 1 "duk_unicode.h" -/* - * Unicode helpers - */ - -#ifndef DUK_UNICODE_H_INCLUDED -#define DUK_UNICODE_H_INCLUDED - -/* - * UTF-8 / XUTF-8 / CESU-8 constants - */ - -#define DUK_UNICODE_MAX_XUTF8_LENGTH 7 /* up to 36 bit codepoints */ -#define DUK_UNICODE_MAX_XUTF8_BMP_LENGTH 3 /* all codepoints up to U+FFFF */ -#define DUK_UNICODE_MAX_CESU8_LENGTH 6 /* all codepoints up to U+10FFFF */ -#define DUK_UNICODE_MAX_CESU8_BMP_LENGTH 3 /* all codepoints up to U+FFFF */ - -/* - * Useful Unicode codepoints - * - * Integer constants must be signed to avoid unexpected coercions - * in comparisons. - */ - -#define DUK_UNICODE_CP_ZWNJ 0x200cL /* zero-width non-joiner */ -#define DUK_UNICODE_CP_ZWJ 0x200dL /* zero-width joiner */ -#define DUK_UNICODE_CP_REPLACEMENT_CHARACTER 0xfffdL /* http://en.wikipedia.org/wiki/Replacement_character#Replacement_character */ - -/* - * ASCII character constants - * - * C character literals like 'x' have a platform specific value and do - * not match ASCII (UTF-8) values on e.g. EBCDIC platforms. So, use - * these (admittedly awkward) constants instead. These constants must - * also have signed values to avoid unexpected coercions in comparisons. - * - * http://en.wikipedia.org/wiki/ASCII - */ - -#define DUK_ASC_NUL 0x00 -#define DUK_ASC_SOH 0x01 -#define DUK_ASC_STX 0x02 -#define DUK_ASC_ETX 0x03 -#define DUK_ASC_EOT 0x04 -#define DUK_ASC_ENQ 0x05 -#define DUK_ASC_ACK 0x06 -#define DUK_ASC_BEL 0x07 -#define DUK_ASC_BS 0x08 -#define DUK_ASC_HT 0x09 -#define DUK_ASC_LF 0x0a -#define DUK_ASC_VT 0x0b -#define DUK_ASC_FF 0x0c -#define DUK_ASC_CR 0x0d -#define DUK_ASC_SO 0x0e -#define DUK_ASC_SI 0x0f -#define DUK_ASC_DLE 0x10 -#define DUK_ASC_DC1 0x11 -#define DUK_ASC_DC2 0x12 -#define DUK_ASC_DC3 0x13 -#define DUK_ASC_DC4 0x14 -#define DUK_ASC_NAK 0x15 -#define DUK_ASC_SYN 0x16 -#define DUK_ASC_ETB 0x17 -#define DUK_ASC_CAN 0x18 -#define DUK_ASC_EM 0x19 -#define DUK_ASC_SUB 0x1a -#define DUK_ASC_ESC 0x1b -#define DUK_ASC_FS 0x1c -#define DUK_ASC_GS 0x1d -#define DUK_ASC_RS 0x1e -#define DUK_ASC_US 0x1f -#define DUK_ASC_SPACE 0x20 -#define DUK_ASC_EXCLAMATION 0x21 -#define DUK_ASC_DOUBLEQUOTE 0x22 -#define DUK_ASC_HASH 0x23 -#define DUK_ASC_DOLLAR 0x24 -#define DUK_ASC_PERCENT 0x25 -#define DUK_ASC_AMP 0x26 -#define DUK_ASC_SINGLEQUOTE 0x27 -#define DUK_ASC_LPAREN 0x28 -#define DUK_ASC_RPAREN 0x29 -#define DUK_ASC_STAR 0x2a -#define DUK_ASC_PLUS 0x2b -#define DUK_ASC_COMMA 0x2c -#define DUK_ASC_MINUS 0x2d -#define DUK_ASC_PERIOD 0x2e -#define DUK_ASC_SLASH 0x2f -#define DUK_ASC_0 0x30 -#define DUK_ASC_1 0x31 -#define DUK_ASC_2 0x32 -#define DUK_ASC_3 0x33 -#define DUK_ASC_4 0x34 -#define DUK_ASC_5 0x35 -#define DUK_ASC_6 0x36 -#define DUK_ASC_7 0x37 -#define DUK_ASC_8 0x38 -#define DUK_ASC_9 0x39 -#define DUK_ASC_COLON 0x3a -#define DUK_ASC_SEMICOLON 0x3b -#define DUK_ASC_LANGLE 0x3c -#define DUK_ASC_EQUALS 0x3d -#define DUK_ASC_RANGLE 0x3e -#define DUK_ASC_QUESTION 0x3f -#define DUK_ASC_ATSIGN 0x40 -#define DUK_ASC_UC_A 0x41 -#define DUK_ASC_UC_B 0x42 -#define DUK_ASC_UC_C 0x43 -#define DUK_ASC_UC_D 0x44 -#define DUK_ASC_UC_E 0x45 -#define DUK_ASC_UC_F 0x46 -#define DUK_ASC_UC_G 0x47 -#define DUK_ASC_UC_H 0x48 -#define DUK_ASC_UC_I 0x49 -#define DUK_ASC_UC_J 0x4a -#define DUK_ASC_UC_K 0x4b -#define DUK_ASC_UC_L 0x4c -#define DUK_ASC_UC_M 0x4d -#define DUK_ASC_UC_N 0x4e -#define DUK_ASC_UC_O 0x4f -#define DUK_ASC_UC_P 0x50 -#define DUK_ASC_UC_Q 0x51 -#define DUK_ASC_UC_R 0x52 -#define DUK_ASC_UC_S 0x53 -#define DUK_ASC_UC_T 0x54 -#define DUK_ASC_UC_U 0x55 -#define DUK_ASC_UC_V 0x56 -#define DUK_ASC_UC_W 0x57 -#define DUK_ASC_UC_X 0x58 -#define DUK_ASC_UC_Y 0x59 -#define DUK_ASC_UC_Z 0x5a -#define DUK_ASC_LBRACKET 0x5b -#define DUK_ASC_BACKSLASH 0x5c -#define DUK_ASC_RBRACKET 0x5d -#define DUK_ASC_CARET 0x5e -#define DUK_ASC_UNDERSCORE 0x5f -#define DUK_ASC_GRAVE 0x60 -#define DUK_ASC_LC_A 0x61 -#define DUK_ASC_LC_B 0x62 -#define DUK_ASC_LC_C 0x63 -#define DUK_ASC_LC_D 0x64 -#define DUK_ASC_LC_E 0x65 -#define DUK_ASC_LC_F 0x66 -#define DUK_ASC_LC_G 0x67 -#define DUK_ASC_LC_H 0x68 -#define DUK_ASC_LC_I 0x69 -#define DUK_ASC_LC_J 0x6a -#define DUK_ASC_LC_K 0x6b -#define DUK_ASC_LC_L 0x6c -#define DUK_ASC_LC_M 0x6d -#define DUK_ASC_LC_N 0x6e -#define DUK_ASC_LC_O 0x6f -#define DUK_ASC_LC_P 0x70 -#define DUK_ASC_LC_Q 0x71 -#define DUK_ASC_LC_R 0x72 -#define DUK_ASC_LC_S 0x73 -#define DUK_ASC_LC_T 0x74 -#define DUK_ASC_LC_U 0x75 -#define DUK_ASC_LC_V 0x76 -#define DUK_ASC_LC_W 0x77 -#define DUK_ASC_LC_X 0x78 -#define DUK_ASC_LC_Y 0x79 -#define DUK_ASC_LC_Z 0x7a -#define DUK_ASC_LCURLY 0x7b -#define DUK_ASC_PIPE 0x7c -#define DUK_ASC_RCURLY 0x7d -#define DUK_ASC_TILDE 0x7e -#define DUK_ASC_DEL 0x7f - -/* - * Unicode tables - */ - -#ifdef DUK_USE_SOURCE_NONBMP -/* - * Automatically generated by extract_chars.py, do not edit! - */ - -extern const duk_uint8_t duk_unicode_ids_noa[791]; -#else -/* - * Automatically generated by extract_chars.py, do not edit! - */ - -extern const duk_uint8_t duk_unicode_ids_noabmp[611]; -#endif - -#ifdef DUK_USE_SOURCE_NONBMP -/* - * Automatically generated by extract_chars.py, do not edit! - */ - -extern const duk_uint8_t duk_unicode_ids_m_let_noa[42]; -#else -/* - * Automatically generated by extract_chars.py, do not edit! - */ - -extern const duk_uint8_t duk_unicode_ids_m_let_noabmp[24]; -#endif - -#ifdef DUK_USE_SOURCE_NONBMP -/* - * Automatically generated by extract_chars.py, do not edit! - */ - -extern const duk_uint8_t duk_unicode_idp_m_ids_noa[397]; -#else -/* - * Automatically generated by extract_chars.py, do not edit! - */ - -extern const duk_uint8_t duk_unicode_idp_m_ids_noabmp[348]; -#endif - -/* - * Automatically generated by extract_caseconv.py, do not edit! - */ - -extern const duk_uint8_t duk_unicode_caseconv_uc[1288]; -extern const duk_uint8_t duk_unicode_caseconv_lc[616]; - -#if defined(DUK_USE_REGEXP_CANON_WORKAROUND) -/* - * Automatically generated by extract_caseconv.py, do not edit! - */ - -extern const duk_uint16_t duk_unicode_re_canon_lookup[65536]; -#endif - -/* - * Extern - */ - -/* duk_unicode_support.c */ -#if !defined(DUK_SINGLE_FILE) -DUK_INTERNAL_DECL const duk_uint8_t duk_unicode_xutf8_markers[7]; -DUK_INTERNAL_DECL const duk_uint16_t duk_unicode_re_ranges_digit[2]; -DUK_INTERNAL_DECL const duk_uint16_t duk_unicode_re_ranges_white[22]; -DUK_INTERNAL_DECL const duk_uint16_t duk_unicode_re_ranges_wordchar[8]; -DUK_INTERNAL_DECL const duk_uint16_t duk_unicode_re_ranges_not_digit[4]; -DUK_INTERNAL_DECL const duk_uint16_t duk_unicode_re_ranges_not_white[24]; -DUK_INTERNAL_DECL const duk_uint16_t duk_unicode_re_ranges_not_wordchar[10]; -DUK_INTERNAL_DECL const duk_int8_t duk_is_idchar_tab[128]; -#endif /* !DUK_SINGLE_FILE */ - -/* - * Prototypes - */ - -DUK_INTERNAL_DECL duk_small_int_t duk_unicode_get_xutf8_length(duk_ucodepoint_t cp); -#if defined(DUK_USE_ASSERTIONS) -DUK_INTERNAL_DECL duk_small_int_t duk_unicode_get_cesu8_length(duk_ucodepoint_t cp); -#endif -DUK_INTERNAL_DECL duk_small_int_t duk_unicode_encode_xutf8(duk_ucodepoint_t cp, duk_uint8_t *out); -DUK_INTERNAL_DECL duk_small_int_t duk_unicode_encode_cesu8(duk_ucodepoint_t cp, duk_uint8_t *out); -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); -DUK_INTERNAL_DECL duk_ucodepoint_t duk_unicode_decode_xutf8_checked(duk_hthread *thr, const duk_uint8_t **ptr, const duk_uint8_t *ptr_start, const duk_uint8_t *ptr_end); -DUK_INTERNAL_DECL duk_size_t duk_unicode_unvalidated_utf8_length(const duk_uint8_t *data, duk_size_t blen); -DUK_INTERNAL_DECL duk_small_int_t duk_unicode_is_whitespace(duk_codepoint_t cp); -DUK_INTERNAL_DECL duk_small_int_t duk_unicode_is_line_terminator(duk_codepoint_t cp); -DUK_INTERNAL_DECL duk_small_int_t duk_unicode_is_identifier_start(duk_codepoint_t cp); -DUK_INTERNAL_DECL duk_small_int_t duk_unicode_is_identifier_part(duk_codepoint_t cp); -DUK_INTERNAL_DECL duk_small_int_t duk_unicode_is_letter(duk_codepoint_t cp); -DUK_INTERNAL_DECL void duk_unicode_case_convert_string(duk_hthread *thr, duk_bool_t uppercase); -DUK_INTERNAL_DECL duk_codepoint_t duk_unicode_re_canonicalize_char(duk_hthread *thr, duk_codepoint_t cp); -DUK_INTERNAL_DECL duk_small_int_t duk_unicode_re_is_wordchar(duk_codepoint_t cp); - -#endif /* DUK_UNICODE_H_INCLUDED */ -#line 1 "duk_json.h" -/* - * Defines for JSON, especially duk_bi_json.c. - */ - -#ifndef DUK_JSON_H_INCLUDED -#define DUK_JSON_H_INCLUDED - -/* Encoding/decoding flags */ -#define DUK_JSON_FLAG_ASCII_ONLY (1 << 0) /* escape any non-ASCII characters */ -#define DUK_JSON_FLAG_AVOID_KEY_QUOTES (1 << 1) /* avoid key quotes when key is an ASCII Identifier */ -#define DUK_JSON_FLAG_EXT_CUSTOM (1 << 2) /* extended types: custom encoding */ -#define DUK_JSON_FLAG_EXT_COMPATIBLE (1 << 3) /* extended types: compatible encoding */ - -/* How much stack to require on entry to object/array encode */ -#define DUK_JSON_ENC_REQSTACK 32 - -/* How much stack to require on entry to object/array decode */ -#define DUK_JSON_DEC_REQSTACK 32 - -/* How large a loop detection stack to use */ -#define DUK_JSON_ENC_LOOPARRAY 64 - -/* Encoding state. Heap object references are all borrowed. */ -typedef struct { - duk_hthread *thr; - duk_bufwriter_ctx bw; /* output bufwriter */ - duk_hobject *h_replacer; /* replacer function */ - duk_hstring *h_gap; /* gap (if empty string, NULL) */ - duk_idx_t idx_proplist; /* explicit PropertyList */ - duk_idx_t idx_loop; /* valstack index of loop detection object */ - duk_small_uint_t flags; - duk_small_uint_t flag_ascii_only; - duk_small_uint_t flag_avoid_key_quotes; -#if defined(DUK_USE_JX) || defined(DUK_USE_JC) - duk_small_uint_t flag_ext_custom; - duk_small_uint_t flag_ext_compatible; - duk_small_uint_t flag_ext_custom_or_compatible; -#endif - duk_int_t recursion_depth; - duk_int_t recursion_limit; - duk_uint_t mask_for_undefined; /* type bit mask: types which certainly produce 'undefined' */ -#if defined(DUK_USE_JX) || defined(DUK_USE_JC) - duk_small_uint_t stridx_custom_undefined; - duk_small_uint_t stridx_custom_nan; - duk_small_uint_t stridx_custom_neginf; - duk_small_uint_t stridx_custom_posinf; - duk_small_uint_t stridx_custom_function; -#endif - duk_hobject *visiting[DUK_JSON_ENC_LOOPARRAY]; /* indexed by recursion_depth */ -} duk_json_enc_ctx; - -typedef struct { - duk_hthread *thr; - const duk_uint8_t *p; - const duk_uint8_t *p_start; - const duk_uint8_t *p_end; - duk_idx_t idx_reviver; - duk_small_uint_t flags; -#if defined(DUK_USE_JX) || defined(DUK_USE_JC) - duk_small_uint_t flag_ext_custom; - duk_small_uint_t flag_ext_compatible; - duk_small_uint_t flag_ext_custom_or_compatible; -#endif - duk_int_t recursion_depth; - duk_int_t recursion_limit; -} duk_json_dec_ctx; - -#endif /* DUK_JSON_H_INCLUDED */ -#line 1 "duk_js.h" -/* - * Ecmascript execution, support primitives. - */ - -#ifndef DUK_JS_H_INCLUDED -#define DUK_JS_H_INCLUDED - -/* Flags for call handling. */ -#define DUK_CALL_FLAG_IGNORE_RECLIMIT (1 << 0) /* duk_handle_call_xxx: call ignores C recursion limit (for errhandler calls) */ -#define DUK_CALL_FLAG_CONSTRUCTOR_CALL (1 << 1) /* duk_handle_call_xxx: constructor call (i.e. called as 'new Foo()') */ -#define DUK_CALL_FLAG_IS_RESUME (1 << 2) /* duk_handle_ecma_call_setup: setup for a resume() */ -#define DUK_CALL_FLAG_IS_TAILCALL (1 << 3) /* duk_handle_ecma_call_setup: setup for a tail call */ -#define DUK_CALL_FLAG_DIRECT_EVAL (1 << 4) /* call is a direct eval call */ - -/* Flags for duk_js_equals_helper(). */ -#define DUK_EQUALS_FLAG_SAMEVALUE (1 << 0) /* use SameValue instead of non-strict equality */ -#define DUK_EQUALS_FLAG_STRICT (1 << 1) /* use strict equality instead of non-strict equality */ - -/* Flags for duk_js_compare_helper(). */ -#define DUK_COMPARE_FLAG_EVAL_LEFT_FIRST (1 << 0) /* eval left argument first */ -#define DUK_COMPARE_FLAG_NEGATE (1 << 1) /* negate result */ - -/* conversions, coercions, comparison, etc */ -DUK_INTERNAL_DECL duk_bool_t duk_js_toboolean(duk_tval *tv); -DUK_INTERNAL_DECL duk_double_t duk_js_tonumber(duk_hthread *thr, duk_tval *tv); -DUK_INTERNAL_DECL duk_double_t duk_js_tointeger_number(duk_double_t x); -DUK_INTERNAL_DECL duk_double_t duk_js_tointeger(duk_hthread *thr, duk_tval *tv); -DUK_INTERNAL_DECL duk_uint32_t duk_js_touint32(duk_hthread *thr, duk_tval *tv); -DUK_INTERNAL_DECL duk_int32_t duk_js_toint32(duk_hthread *thr, duk_tval *tv); -DUK_INTERNAL_DECL duk_uint16_t duk_js_touint16(duk_hthread *thr, duk_tval *tv); -DUK_INTERNAL_DECL duk_small_int_t duk_js_to_arrayindex_raw_string(const duk_uint8_t *str, duk_uint32_t blen, duk_uarridx_t *out_idx); -DUK_INTERNAL_DECL duk_uarridx_t duk_js_to_arrayindex_string_helper(duk_hstring *h); -DUK_INTERNAL_DECL duk_bool_t duk_js_equals_helper(duk_hthread *thr, duk_tval *tv_x, duk_tval *tv_y, duk_small_int_t flags); -DUK_INTERNAL_DECL duk_small_int_t duk_js_data_compare(const duk_uint8_t *buf1, const duk_uint8_t *buf2, duk_size_t len1, duk_size_t len2); -DUK_INTERNAL_DECL duk_small_int_t duk_js_string_compare(duk_hstring *h1, duk_hstring *h2); -#if 0 /* unused */ -DUK_INTERNAL_DECL duk_small_int_t duk_js_buffer_compare(duk_heap *heap, duk_hbuffer *h1, duk_hbuffer *h2); -#endif -DUK_INTERNAL_DECL duk_bool_t duk_js_compare_helper(duk_hthread *thr, duk_tval *tv_x, duk_tval *tv_y, duk_small_int_t flags); -DUK_INTERNAL_DECL duk_bool_t duk_js_instanceof(duk_hthread *thr, duk_tval *tv_x, duk_tval *tv_y); -DUK_INTERNAL_DECL duk_bool_t duk_js_in(duk_hthread *thr, duk_tval *tv_x, duk_tval *tv_y); -DUK_INTERNAL_DECL duk_hstring *duk_js_typeof(duk_hthread *thr, duk_tval *tv_x); - -#define duk_js_equals(thr,tv_x,tv_y) \ - duk_js_equals_helper((thr), (tv_x), (tv_y), 0) -#define duk_js_strict_equals(tv_x,tv_y) \ - duk_js_equals_helper(NULL, (tv_x), (tv_y), DUK_EQUALS_FLAG_STRICT) -#define duk_js_samevalue(tv_x,tv_y) \ - duk_js_equals_helper(NULL, (tv_x), (tv_y), DUK_EQUALS_FLAG_SAMEVALUE) - -/* E5 Sections 11.8.1, 11.8.5; x < y */ -#define duk_js_lessthan(thr,tv_x,tv_y) \ - duk_js_compare_helper((thr), (tv_x), (tv_Y), DUK_COMPARE_FLAG_EVAL_LEFT_FIRST) - -/* E5 Sections 11.8.2, 11.8.5; x > y --> y < x */ -#define duk_js_greaterthan(thr,tv_x,tv_y) \ - duk_js_compare_helper((thr), (tv_y), (tv_x), 0) - -/* E5 Sections 11.8.3, 11.8.5; x <= y --> not (x > y) --> not (y < x) */ -#define duk_js_lessthanorequal(thr,tv_x,tv_y) \ - duk_js_compare_helper((thr), (tv_y), (tv_x), DUK_COMPARE_FLAG_NEGATE) - -/* E5 Sections 11.8.4, 11.8.5; x >= y --> not (x < y) */ -#define duk_js_greaterthanorequal(thr,tv_x,tv_y) \ - duk_js_compare_helper((thr), (tv_x), (tv_y), DUK_COMPARE_FLAG_EVAL_LEFT_FIRST | DUK_COMPARE_FLAG_NEGATE) - -/* identifiers and environment handling */ -#if 0 /*unused*/ -DUK_INTERNAL duk_bool_t duk_js_hasvar_envrec(duk_hthread *thr, duk_hobject *env, duk_hstring *name); -#endif -DUK_INTERNAL_DECL duk_bool_t duk_js_getvar_envrec(duk_hthread *thr, duk_hobject *env, duk_hstring *name, duk_bool_t throw_flag); -DUK_INTERNAL_DECL duk_bool_t duk_js_getvar_activation(duk_hthread *thr, duk_activation *act, duk_hstring *name, duk_bool_t throw_flag); -DUK_INTERNAL_DECL void duk_js_putvar_envrec(duk_hthread *thr, duk_hobject *env, duk_hstring *name, duk_tval *val, duk_bool_t strict); -DUK_INTERNAL_DECL void duk_js_putvar_activation(duk_hthread *thr, duk_activation *act, duk_hstring *name, duk_tval *val, duk_bool_t strict); -#if 0 /*unused*/ -DUK_INTERNAL_DECL duk_bool_t duk_js_delvar_envrec(duk_hthread *thr, duk_hobject *env, duk_hstring *name); -#endif -DUK_INTERNAL_DECL duk_bool_t duk_js_delvar_activation(duk_hthread *thr, duk_activation *act, duk_hstring *name); -DUK_INTERNAL_DECL duk_bool_t duk_js_declvar_activation(duk_hthread *thr, duk_activation *act, duk_hstring *name, duk_tval *val, duk_small_int_t prop_flags, duk_bool_t is_func_decl); -DUK_INTERNAL_DECL void duk_js_init_activation_environment_records_delayed(duk_hthread *thr, duk_activation *act); -DUK_INTERNAL_DECL void duk_js_close_environment_record(duk_hthread *thr, duk_hobject *env, duk_hobject *func, duk_size_t regbase); -DUK_INTERNAL_DECL duk_hobject *duk_create_activation_environment_record(duk_hthread *thr, duk_hobject *func, duk_size_t idx_bottom); -DUK_INTERNAL_DECL -void duk_js_push_closure(duk_hthread *thr, - duk_hcompiledfunction *fun_temp, - duk_hobject *outer_var_env, - duk_hobject *outer_lex_env, - duk_bool_t add_auto_proto); - -/* call handling */ -DUK_INTERNAL_DECL duk_int_t duk_handle_call_protected(duk_hthread *thr, duk_idx_t num_stack_args, duk_small_uint_t call_flags); -DUK_INTERNAL_DECL void duk_handle_call_unprotected(duk_hthread *thr, duk_idx_t num_stack_args, duk_small_uint_t call_flags); -DUK_INTERNAL_DECL duk_int_t duk_handle_safe_call(duk_hthread *thr, duk_safe_call_function func, duk_idx_t num_stack_args, duk_idx_t num_stack_res); -DUK_INTERNAL_DECL duk_bool_t duk_handle_ecma_call_setup(duk_hthread *thr, duk_idx_t num_stack_args, duk_small_uint_t call_flags); - -/* bytecode execution */ -DUK_INTERNAL_DECL void duk_js_execute_bytecode(duk_hthread *exec_thr); - -#endif /* DUK_JS_H_INCLUDED */ -#line 1 "duk_numconv.h" -#ifndef DUK_NUMCONV_H_INCLUDED -#define DUK_NUMCONV_H_INCLUDED - -/* - * Number-to-string conversion. The semantics of these is very tightly - * bound with the Ecmascript semantics required for call sites. - */ - -/* Output a specified number of digits instead of using the shortest - * form. Used for toPrecision() and toFixed(). - */ -#define DUK_N2S_FLAG_FIXED_FORMAT (1 << 0) - -/* Force exponential format. Used for toExponential(). */ -#define DUK_N2S_FLAG_FORCE_EXP (1 << 1) - -/* If number would need zero padding (for whole number part), use - * exponential format instead. E.g. if input number is 12300, 3 - * digits are generated ("123"), output "1.23e+4" instead of "12300". - * Used for toPrecision(). - */ -#define DUK_N2S_FLAG_NO_ZERO_PAD (1 << 2) - -/* Digit count indicates number of fractions (i.e. an absolute - * digit index instead of a relative one). Used together with - * DUK_N2S_FLAG_FIXED_FORMAT for toFixed(). - */ -#define DUK_N2S_FLAG_FRACTION_DIGITS (1 << 3) - -/* - * String-to-number conversion - */ - -/* Maximum exponent value when parsing numbers. This is not strictly - * compliant as there should be no upper limit, but as we parse the - * exponent without a bigint, impose some limit. - */ -#define DUK_S2N_MAX_EXPONENT 1000000000 - -/* Trim white space (= allow leading and trailing whitespace) */ -#define DUK_S2N_FLAG_TRIM_WHITE (1 << 0) - -/* Allow exponent */ -#define DUK_S2N_FLAG_ALLOW_EXP (1 << 1) - -/* Allow trailing garbage (e.g. treat "123foo" as "123) */ -#define DUK_S2N_FLAG_ALLOW_GARBAGE (1 << 2) - -/* Allow leading plus sign */ -#define DUK_S2N_FLAG_ALLOW_PLUS (1 << 3) - -/* Allow leading minus sign */ -#define DUK_S2N_FLAG_ALLOW_MINUS (1 << 4) - -/* Allow 'Infinity' */ -#define DUK_S2N_FLAG_ALLOW_INF (1 << 5) - -/* Allow fraction part */ -#define DUK_S2N_FLAG_ALLOW_FRAC (1 << 6) - -/* Allow naked fraction (e.g. ".123") */ -#define DUK_S2N_FLAG_ALLOW_NAKED_FRAC (1 << 7) - -/* Allow empty fraction (e.g. "123.") */ -#define DUK_S2N_FLAG_ALLOW_EMPTY_FRAC (1 << 8) - -/* Allow empty string to be interpreted as 0 */ -#define DUK_S2N_FLAG_ALLOW_EMPTY_AS_ZERO (1 << 9) - -/* Allow leading zeroes (e.g. "0123" -> "123") */ -#define DUK_S2N_FLAG_ALLOW_LEADING_ZERO (1 << 10) - -/* Allow automatic detection of hex base ("0x" or "0X" prefix), - * overrides radix argument and forces integer mode. - */ -#define DUK_S2N_FLAG_ALLOW_AUTO_HEX_INT (1 << 11) - -/* Allow automatic detection of octal base, overrides radix - * argument and forces integer mode. - */ -#define DUK_S2N_FLAG_ALLOW_AUTO_OCT_INT (1 << 12) - -/* - * Prototypes - */ - -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); -DUK_INTERNAL_DECL void duk_numconv_parse(duk_context *ctx, duk_small_int_t radix, duk_small_uint_t flags); - -#endif /* DUK_NUMCONV_H_INCLUDED */ -#line 1 "duk_bi_protos.h" -/* - * Prototypes for built-in functions not automatically covered by the - * header declarations emitted by genbuiltins.py. - */ - -#ifndef DUK_BUILTIN_PROTOS_H_INCLUDED -#define DUK_BUILTIN_PROTOS_H_INCLUDED - -/* Buffer size needed for duk_bi_date_format_timeval(). - * Accurate value is 32 + 1 for NUL termination: - * >>> len('+123456-01-23T12:34:56.123+12:34') - * 32 - * Include additional space to be safe. - */ -#define DUK_BI_DATE_ISO8601_BUFSIZE 48 - -/* Maximum length of CommonJS module identifier to resolve. Length includes - * both current module ID, requested (possibly relative) module ID, and a - * slash in between. - */ -#define DUK_BI_COMMONJS_MODULE_ID_LIMIT 256 - -/* Helpers exposed for internal use */ -DUK_INTERNAL_DECL void duk_bi_date_timeval_to_parts(duk_double_t d, duk_int_t *parts, duk_double_t *dparts, duk_small_uint_t flags); -DUK_INTERNAL_DECL duk_double_t duk_bi_date_get_timeval_from_dparts(duk_double_t *dparts, duk_small_uint_t flags); -DUK_INTERNAL_DECL void duk_bi_date_format_timeval(duk_double_t timeval, duk_uint8_t *out_buf); -DUK_INTERNAL_DECL duk_bool_t duk_bi_date_is_leap_year(duk_int_t year); -DUK_INTERNAL_DECL duk_bool_t duk_bi_date_timeval_in_valid_range(duk_double_t x); -DUK_INTERNAL_DECL duk_bool_t duk_bi_date_year_in_valid_range(duk_double_t year); -DUK_INTERNAL_DECL duk_bool_t duk_bi_date_timeval_in_leeway_range(duk_double_t x); -/* Built-in providers */ -#if defined(DUK_USE_DATE_NOW_GETTIMEOFDAY) -DUK_INTERNAL_DECL duk_double_t duk_bi_date_get_now_gettimeofday(duk_context *ctx); -#endif -#if defined(DUK_USE_DATE_NOW_TIME) -DUK_INTERNAL_DECL duk_double_t duk_bi_date_get_now_time(duk_context *ctx); -#endif -#if defined(DUK_USE_DATE_NOW_WINDOWS) -DUK_INTERNAL_DECL duk_double_t duk_bi_date_get_now_windows(duk_context *ctx); -#endif -#if defined(DUK_USE_DATE_TZO_GMTIME_R) || defined(DUK_USE_DATE_TZO_GMTIME) -DUK_INTERNAL_DECL duk_int_t duk_bi_date_get_local_tzoffset_gmtime(duk_double_t d); -#endif -#if defined(DUK_USE_DATE_TZO_WINDOWS) -DUK_INTERNAL_DECL duk_int_t duk_bi_date_get_local_tzoffset_windows(duk_double_t d); -#endif -#if defined(DUK_USE_DATE_PRS_STRPTIME) -DUK_INTERNAL_DECL duk_bool_t duk_bi_date_parse_string_strptime(duk_context *ctx, const char *str); -#endif -#if defined(DUK_USE_DATE_PRS_GETDATE) -DUK_INTERNAL_DECL duk_bool_t duk_bi_date_parse_string_getdate(duk_context *ctx, const char *str); -#endif -#if defined(DUK_USE_DATE_FMT_STRFTIME) -DUK_INTERNAL_DECL duk_bool_t duk_bi_date_format_parts_strftime(duk_context *ctx, duk_int_t *parts, duk_int_t tzoffset, duk_small_uint_t flags); -#endif - -DUK_INTERNAL_DECL -void duk_bi_json_parse_helper(duk_context *ctx, - duk_idx_t idx_value, - duk_idx_t idx_reviver, - duk_small_uint_t flags); -DUK_INTERNAL_DECL -void duk_bi_json_stringify_helper(duk_context *ctx, - duk_idx_t idx_value, - duk_idx_t idx_replacer, - duk_idx_t idx_space, - duk_small_uint_t flags); - -#endif /* DUK_BUILTIN_PROTOS_H_INCLUDED */ -#line 1 "duk_selftest.h" -/* - * Selftest code - */ - -#ifndef DUK_SELFTEST_H_INCLUDED -#define DUK_SELFTEST_H_INCLUDED - -#if defined(DUK_USE_SELF_TESTS) -DUK_INTERNAL_DECL void duk_selftest_run_tests(void); -#endif - -#endif /* DUK_SELFTEST_H_INCLUDED */ -#line 78 "duk_internal.h" - -#endif /* DUK_INTERNAL_H_INCLUDED */ -#line 1 "duk_replacements.c" -/* - * Replacements for missing platform functions. - * - * Unlike the originals, fpclassify() and signbit() replacements don't - * work on any floating point types, only doubles. The C typing here - * mimics the standard prototypes. - */ - -/* include removed: duk_internal.h */ - -#if defined(DUK_USE_COMPUTED_NAN) -DUK_INTERNAL double duk_computed_nan; -#endif - -#if defined(DUK_USE_COMPUTED_INFINITY) -DUK_INTERNAL double duk_computed_infinity; -#endif - -#if defined(DUK_USE_REPL_FPCLASSIFY) -DUK_INTERNAL int duk_repl_fpclassify(double x) { - duk_double_union u; - duk_uint_fast16_t expt; - duk_small_int_t mzero; - - u.d = x; - expt = (duk_uint_fast16_t) (u.us[DUK_DBL_IDX_US0] & 0x7ff0UL); - if (expt > 0x0000UL && expt < 0x7ff0UL) { - /* expt values [0x001,0x7fe] = normal */ - return DUK_FP_NORMAL; - } - - mzero = (u.ui[DUK_DBL_IDX_UI1] == 0 && (u.ui[DUK_DBL_IDX_UI0] & 0x000fffffUL) == 0); - if (expt == 0x0000UL) { - /* expt 0x000 is zero/subnormal */ - if (mzero) { - return DUK_FP_ZERO; - } else { - return DUK_FP_SUBNORMAL; - } - } else { - /* expt 0xfff is infinite/nan */ - if (mzero) { - return DUK_FP_INFINITE; - } else { - return DUK_FP_NAN; - } - } -} -#endif - -#if defined(DUK_USE_REPL_SIGNBIT) -DUK_INTERNAL int duk_repl_signbit(double x) { - duk_double_union u; - u.d = x; - return (int) (u.uc[DUK_DBL_IDX_UC0] & 0x80UL); -} -#endif - -#if defined(DUK_USE_REPL_ISFINITE) -DUK_INTERNAL int duk_repl_isfinite(double x) { - int c = DUK_FPCLASSIFY(x); - if (c == DUK_FP_NAN || c == DUK_FP_INFINITE) { - return 0; - } else { - return 1; - } -} -#endif - -#if defined(DUK_USE_REPL_ISNAN) -DUK_INTERNAL int duk_repl_isnan(double x) { - int c = DUK_FPCLASSIFY(x); - return (c == DUK_FP_NAN); -} -#endif - -#if defined(DUK_USE_REPL_ISINF) -DUK_INTERNAL int duk_repl_isinf(double x) { - int c = DUK_FPCLASSIFY(x); - return (c == DUK_FP_INFINITE); -} -#endif -#line 1 "duk_strings.c" -/* - * Shared error message strings - * - * To minimize code footprint, try to share error messages inside Duktape - * code. Modern compilers will do this automatically anyway, this is mostly - * for older compilers. - */ - -/* include removed: duk_internal.h */ - -/* Mostly API and built-in method related */ -DUK_INTERNAL const char *duk_str_internal_error = "internal error"; -DUK_INTERNAL const char *duk_str_invalid_count = "invalid count"; -DUK_INTERNAL const char *duk_str_invalid_call_args = "invalid call args"; -DUK_INTERNAL const char *duk_str_not_constructable = "not constructable"; -DUK_INTERNAL const char *duk_str_not_callable = "not callable"; -DUK_INTERNAL const char *duk_str_not_extensible = "not extensible"; -DUK_INTERNAL const char *duk_str_not_writable = "not writable"; -DUK_INTERNAL const char *duk_str_not_configurable = "not configurable"; - -DUK_INTERNAL const char *duk_str_invalid_context = "invalid context"; -DUK_INTERNAL const char *duk_str_push_beyond_alloc_stack = "attempt to push beyond currently allocated stack"; -DUK_INTERNAL const char *duk_str_not_buffer = "not buffer"; /* still in use with verbose messages */ -DUK_INTERNAL const char *duk_str_unexpected_type = "unexpected type"; -DUK_INTERNAL const char *duk_str_defaultvalue_coerce_failed = "[[DefaultValue]] coerce failed"; -DUK_INTERNAL const char *duk_str_number_outside_range = "number outside range"; -DUK_INTERNAL const char *duk_str_not_object_coercible = "not object coercible"; -DUK_INTERNAL const char *duk_str_string_too_long = "string too long"; -DUK_INTERNAL const char *duk_str_buffer_too_long = "buffer too long"; -DUK_INTERNAL const char *duk_str_sprintf_too_long = "sprintf message too long"; -DUK_INTERNAL const char *duk_str_alloc_failed = "alloc failed"; -DUK_INTERNAL const char *duk_str_pop_too_many = "attempt to pop too many entries"; -DUK_INTERNAL const char *duk_str_wrong_buffer_type = "wrong buffer type"; -DUK_INTERNAL const char *duk_str_encode_failed = "encode failed"; -DUK_INTERNAL const char *duk_str_decode_failed = "decode failed"; -DUK_INTERNAL const char *duk_str_no_sourcecode = "no sourcecode"; -DUK_INTERNAL const char *duk_str_concat_result_too_long = "concat result too long"; -DUK_INTERNAL const char *duk_str_unimplemented = "unimplemented"; -DUK_INTERNAL const char *duk_str_unsupported = "unsupported"; -DUK_INTERNAL const char *duk_str_array_length_over_2g = "array length over 2G"; - -/* JSON */ -DUK_INTERNAL const char *duk_str_fmt_ptr = "%p"; -DUK_INTERNAL const char *duk_str_fmt_invalid_json = "invalid json (at offset %ld)"; -DUK_INTERNAL const char *duk_str_jsondec_reclimit = "json decode recursion limit"; -DUK_INTERNAL const char *duk_str_jsonenc_reclimit = "json encode recursion limit"; -DUK_INTERNAL const char *duk_str_cyclic_input = "cyclic input"; - -/* Object property access */ -DUK_INTERNAL const char *duk_str_proxy_revoked = "proxy revoked"; -DUK_INTERNAL const char *duk_str_invalid_base = "invalid base value"; -DUK_INTERNAL const char *duk_str_strict_caller_read = "attempt to read strict 'caller'"; -DUK_INTERNAL const char *duk_str_proxy_rejected = "proxy rejected"; -DUK_INTERNAL const char *duk_str_invalid_array_length = "invalid array length"; -DUK_INTERNAL const char *duk_str_array_length_write_failed = "array length write failed"; -DUK_INTERNAL const char *duk_str_array_length_not_writable = "array length non-writable"; -DUK_INTERNAL const char *duk_str_setter_undefined = "setter undefined"; -DUK_INTERNAL const char *duk_str_redefine_virt_prop = "attempt to redefine virtual property"; -DUK_INTERNAL const char *duk_str_invalid_descriptor = "invalid descriptor"; -DUK_INTERNAL const char *duk_str_property_is_virtual = "property is virtual"; - -/* Compiler */ -DUK_INTERNAL const char *duk_str_parse_error = "parse error"; -DUK_INTERNAL const char *duk_str_duplicate_label = "duplicate label"; -DUK_INTERNAL const char *duk_str_invalid_label = "invalid label"; -DUK_INTERNAL const char *duk_str_invalid_array_literal = "invalid array literal"; -DUK_INTERNAL const char *duk_str_invalid_object_literal = "invalid object literal"; -DUK_INTERNAL const char *duk_str_invalid_var_declaration = "invalid variable declaration"; -DUK_INTERNAL const char *duk_str_cannot_delete_identifier = "cannot delete identifier"; -DUK_INTERNAL const char *duk_str_invalid_expression = "invalid expression"; -DUK_INTERNAL const char *duk_str_invalid_lvalue = "invalid lvalue"; -DUK_INTERNAL const char *duk_str_expected_identifier = "expected identifier"; -DUK_INTERNAL const char *duk_str_empty_expr_not_allowed = "empty expression not allowed"; -DUK_INTERNAL const char *duk_str_invalid_for = "invalid for statement"; -DUK_INTERNAL const char *duk_str_invalid_switch = "invalid switch statement"; -DUK_INTERNAL const char *duk_str_invalid_break_cont_label = "invalid break/continue label"; -DUK_INTERNAL const char *duk_str_invalid_return = "invalid return"; -DUK_INTERNAL const char *duk_str_invalid_try = "invalid try"; -DUK_INTERNAL const char *duk_str_invalid_throw = "invalid throw"; -DUK_INTERNAL const char *duk_str_with_in_strict_mode = "with in strict mode"; -DUK_INTERNAL const char *duk_str_func_stmt_not_allowed = "function statement not allowed"; -DUK_INTERNAL const char *duk_str_unterminated_stmt = "unterminated statement"; -DUK_INTERNAL const char *duk_str_invalid_arg_name = "invalid argument name"; -DUK_INTERNAL const char *duk_str_invalid_func_name = "invalid function name"; -DUK_INTERNAL const char *duk_str_invalid_getset_name = "invalid getter/setter name"; -DUK_INTERNAL const char *duk_str_func_name_required = "function name required"; - -/* Regexp */ -DUK_INTERNAL const char *duk_str_invalid_quantifier_no_atom = "quantifier without preceding atom"; -DUK_INTERNAL const char *duk_str_invalid_quantifier_values = "quantifier values invalid (qmin > qmax)"; -DUK_INTERNAL const char *duk_str_quantifier_too_many_copies = "quantifier expansion requires too many atom copies"; -DUK_INTERNAL const char *duk_str_unexpected_closing_paren = "unexpected closing parenthesis"; -DUK_INTERNAL const char *duk_str_unexpected_end_of_pattern = "unexpected end of pattern"; -DUK_INTERNAL const char *duk_str_unexpected_regexp_token = "unexpected token in regexp"; -DUK_INTERNAL const char *duk_str_invalid_regexp_flags = "invalid regexp flags"; -DUK_INTERNAL const char *duk_str_invalid_backrefs = "invalid backreference(s)"; - -/* Limits */ -DUK_INTERNAL const char *duk_str_valstack_limit = "valstack limit"; -DUK_INTERNAL const char *duk_str_callstack_limit = "callstack limit"; -DUK_INTERNAL const char *duk_str_catchstack_limit = "catchstack limit"; -DUK_INTERNAL const char *duk_str_prototype_chain_limit = "prototype chain limit"; -DUK_INTERNAL const char *duk_str_bound_chain_limit = "function call bound chain limit"; -DUK_INTERNAL const char *duk_str_c_callstack_limit = "C call stack depth limit"; -DUK_INTERNAL const char *duk_str_compiler_recursion_limit = "compiler recursion limit"; -DUK_INTERNAL const char *duk_str_bytecode_limit = "bytecode limit"; -DUK_INTERNAL const char *duk_str_reg_limit = "register limit"; -DUK_INTERNAL const char *duk_str_temp_limit = "temp limit"; -DUK_INTERNAL const char *duk_str_const_limit = "const limit"; -DUK_INTERNAL const char *duk_str_func_limit = "function limit"; -DUK_INTERNAL const char *duk_str_regexp_compiler_recursion_limit = "regexp compiler recursion limit"; -DUK_INTERNAL const char *duk_str_regexp_executor_recursion_limit = "regexp executor recursion limit"; -DUK_INTERNAL const char *duk_str_regexp_executor_step_limit = "regexp step limit"; - -/* Misc */ -#line 1 "duk_debug_macros.c" -/* - * Debugging macro calls. - */ - -/* include removed: duk_internal.h */ - -#ifdef DUK_USE_DEBUG - -/* - * Debugging enabled - */ - -#include -#include -#include - -#define DUK__DEBUG_BUFSIZE DUK_USE_DEBUG_BUFSIZE -DUK_LOCAL char duk__debug_buf[DUK__DEBUG_BUFSIZE]; - -DUK_LOCAL const char *duk__get_level_string(duk_small_int_t level) { - switch ((int) level) { - case DUK_LEVEL_DEBUG: - return "D"; - case DUK_LEVEL_DDEBUG: - return "DD"; - case DUK_LEVEL_DDDEBUG: - return "DDD"; - } - return "???"; -} - -#ifdef DUK_USE_DPRINT_COLORS - -/* http://en.wikipedia.org/wiki/ANSI_escape_code */ -#define DUK__TERM_REVERSE "\x1b[7m" -#define DUK__TERM_BRIGHT "\x1b[1m" -#define DUK__TERM_RESET "\x1b[0m" -#define DUK__TERM_BLUE "\x1b[34m" -#define DUK__TERM_RED "\x1b[31m" - -DUK_LOCAL const char *duk__get_term_1(duk_small_int_t level) { - DUK_UNREF(level); - return (const char *) DUK__TERM_RED; -} - -DUK_LOCAL const char *duk__get_term_2(duk_small_int_t level) { - switch ((int) level) { - case DUK_LEVEL_DEBUG: - return (const char *) (DUK__TERM_RESET DUK__TERM_BRIGHT); - case DUK_LEVEL_DDEBUG: - return (const char *) (DUK__TERM_RESET); - case DUK_LEVEL_DDDEBUG: - return (const char *) (DUK__TERM_RESET DUK__TERM_BLUE); - } - return (const char *) DUK__TERM_RESET; -} - -DUK_LOCAL const char *duk__get_term_3(duk_small_int_t level) { - DUK_UNREF(level); - return (const char *) DUK__TERM_RESET; -} - -#else - -DUK_LOCAL const char *duk__get_term_1(duk_small_int_t level) { - DUK_UNREF(level); - return (const char *) ""; -} - -DUK_LOCAL const char *duk__get_term_2(duk_small_int_t level) { - DUK_UNREF(level); - return (const char *) ""; -} - -DUK_LOCAL const char *duk__get_term_3(duk_small_int_t level) { - DUK_UNREF(level); - return (const char *) ""; -} - -#endif /* DUK_USE_DPRINT_COLORS */ - -#ifdef DUK_USE_VARIADIC_MACROS - -DUK_INTERNAL void duk_debug_log(duk_small_int_t level, const char *file, duk_int_t line, const char *func, const char *fmt, ...) { - va_list ap; - - va_start(ap, fmt); - - DUK_MEMZERO((void *) duk__debug_buf, (size_t) DUK__DEBUG_BUFSIZE); - duk_debug_vsnprintf(duk__debug_buf, DUK__DEBUG_BUFSIZE - 1, fmt, ap); - - DUK_FPRINTF(DUK_STDERR, "%s[%s] %s:%ld (%s):%s %s%s\n", - (const char *) duk__get_term_1(level), - (const char *) duk__get_level_string(level), - (const char *) file, - (long) line, - (const char *) func, - (const char *) duk__get_term_2(level), - (const char *) duk__debug_buf, - (const char *) duk__get_term_3(level)); - DUK_FFLUSH(DUK_STDERR); - - va_end(ap); -} - -#else /* DUK_USE_VARIADIC_MACROS */ - -DUK_INTERNAL char duk_debug_file_stash[DUK_DEBUG_STASH_SIZE]; -DUK_INTERNAL char duk_debug_line_stash[DUK_DEBUG_STASH_SIZE]; -DUK_INTERNAL char duk_debug_func_stash[DUK_DEBUG_STASH_SIZE]; -DUK_INTERNAL duk_small_int_t duk_debug_level_stash; - -DUK_INTERNAL void duk_debug_log(const char *fmt, ...) { - va_list ap; - duk_small_int_t level = duk_debug_level_stash; - - va_start(ap, fmt); - - DUK_MEMZERO((void *) duk__debug_buf, (size_t) DUK__DEBUG_BUFSIZE); - duk_debug_vsnprintf(duk__debug_buf, DUK__DEBUG_BUFSIZE - 1, fmt, ap); - - DUK_FPRINTF(DUK_STDERR, "%s[%s] %s:%s (%s):%s %s%s\n", - (const char *) duk__get_term_1(level), - (const char *) duk__get_level_string(duk_debug_level_stash), - (const char *) duk_debug_file_stash, - (const char *) duk_debug_line_stash, - (const char *) duk_debug_func_stash, - (const char *) duk__get_term_2(level), - (const char *) duk__debug_buf, - (const char *) duk__get_term_3(level)); - DUK_FFLUSH(DUK_STDERR); - - va_end(ap); -} - -#endif /* DUK_USE_VARIADIC_MACROS */ - -#else /* DUK_USE_DEBUG */ - -/* - * Debugging disabled - */ - -#endif /* DUK_USE_DEBUG */ -#line 1 "duk_builtins.c" -/* - * Automatically generated by genbuiltins.py, do not edit! - */ - -/* include removed: duk_internal.h */ - -#if defined(DUK_USE_ROM_STRINGS) -#error ROM support not enabled, rerun make_dist.py with --rom-support -#else /* DUK_USE_ROM_STRINGS */ -DUK_INTERNAL const duk_uint8_t duk_strings_data[1049] = { -79,104,209,144,168,105,6,78,182,139,90,122,8,154,140,35,103,35,117,193,73, -5,52,116,180,104,166,135,52,189,4,98,12,27,178,156,80,211,31,161,115,150, -64,52,221,109,24,18,68,157,24,38,67,118,36,55,73,119,151,164,140,93,18,117, -128,153,201,228,201,205,2,250,8,196,24,232,104,82,146,40,232,193,48,118, -168,37,147,212,54,127,113,208,70,32,194,187,68,54,127,113,208,70,32,196, -123,68,54,127,113,209,44,12,121,7,208,70,32,194,186,134,207,236,126,219, -160,140,65,133,246,136,108,254,199,237,186,8,196,24,87,80,217,253,159,217, -116,17,136,48,190,209,13,159,217,253,151,65,24,131,12,233,86,224,79,236, -254,203,160,140,65,134,116,171,112,39,246,223,105,208,70,32,193,140,183,4, -11,55,92,20,244,141,169,186,50,11,164,109,77,208,208,165,36,79,215,185,13, -153,34,110,204,241,32,6,66,84,11,112,200,84,52,157,124,92,242,70,120,45,64, -186,17,22,138,38,0,172,140,19,154,84,26,145,0,86,69,17,180,97,34,0,172,132, -75,144,215,77,221,91,132,5,147,178,156,80,211,30,160,93,9,215,21,115,119, -169,49,75,211,138,26,101,205,222,68,157,47,78,40,105,151,55,120,204,156, -189,56,161,166,52,157,72,136,138,65,154,232,147,162,4,136,150,81,115,66, -208,210,37,96,148,250,134,140,151,39,212,125,255,221,125,73,80,209,146,233, -124,93,55,79,15,34,196,230,202,113,160,166,232,157,132,148,128,98,28,46, -114,200,6,153,180,96,73,19,74,113,67,76,103,5,36,20,211,70,140,133,67,72, -49,245,160,235,81,212,52,168,106,39,132,253,111,80,210,161,168,158,5,245, -191,96,31,172,15,208,23,226,190,131,232,62,131,232,11,251,127,93,245,223, -93,251,172,234,27,80,45,3,250,14,140,19,34,65,19,81,132,108,228,97,1,107, -33,12,32,45,100,136,206,9,12,196,155,134,69,146,100,235,226,231,146,51,194, -72,218,48,145,4,200,119,89,189,81,49,39,72,147,235,226,233,186,120,121,58, -226,167,90,124,93,55,107,71,137,33,68,68,130,64,206,75,189,209,156,144,84, -44,141,3,8,137,187,178,156,80,211,26,110,242,100,230,146,120,121,8,48,76,6, -89,26,105,157,65,196,201,213,145,166,153,212,28,76,157,113,75,34,78,62,14, -38,73,105,228,142,136,178,48,141,152,228,73,150,83,0,148,39,137,75,67,73, -214,209,129,36,85,190,206,32,17,6,9,128,141,3,8,130,161,100,235,64,194,24, -52,41,73,19,189,200,108,201,19,111,181,2,232,66,239,173,37,230,157,244,56, -153,4,225,145,27,233,93,22,1,114,62,251,80,69,128,121,247,213,146,228,109, -79,190,212,17,35,106,125,246,78,164,68,68,111,175,23,217,45,13,33,119,208, -68,210,38,250,192,61,91,233,80,208,45,25,36,81,190,156,13,26,201,19,239, -162,2,214,66,31,125,153,226,64,13,27,236,72,96,130,68,62,251,48,68,196,153, -119,217,157,18,56,156,199,161,100,42,26,250,77,36,140,122,40,144,19,34,9, -24,246,103,139,172,150,56,125,145,1,17,29,44,112,250,183,0,100,24,200,218, -140,228,185,130,9,19,237,190,208,73,184,146,35,68,146,163,8,50,178,99,136, -44,89,196,2,33,70,64,208,196,67,74,226,88,17,105,73,24,186,37,40,38,5,133, -161,89,4,183,25,115,119,86,227,118,83,138,26,103,255,223,209,106,141,25,11, -244,95,117,56,208,159,250,223,251,250,45,52,13,250,47,186,156,104,79,253, -111,253,253,22,144,210,253,23,221,78,52,39,254,187,254,254,139,77,67,75, -244,95,117,56,208,159,250,239,251,250,45,22,141,23,209,125,212,227,66,127, -235,63,239,69,163,69,247,83,141,9,255,165,12,72,5,16,64,145,10,32,76,71,64, -156,217,161,180,34,6,64,208,198,36,78,50,20,20,92,204,50,44,147,32,134,226, -17,114,33,202,134,129,107,192,202,232,160,180,104,166,135,52,72,40,144,213, -33,178,152,26,34,56,163,105,44,104,146,116,139,77,43,34,98,57,38,116,72, -179,60,93,97,206,56,52,240,242,56,163,168,34,81,57,178,153,42,228,12,182, -58,22,66,89,19,57,68,176,74,68,35,104,195,18,239,116,102,114,94,100,104, -228,100,49,238,140,203,42,60,145,35,104,181,146,113,161,10,80,46,68,82,24, -245,145,132,108,228,148,54,100,137,64,34,13,100,153,222,1,40,6,33,223,20, -84,19,34,95,23,76,130,153,6,103,208,43,64,141,41,130,104,17,112,130,44,96, -}; -#endif /* DUK_USE_ROM_STRINGS */ - -#if defined(DUK_USE_ROM_OBJECTS) -#error ROM support not enabled, rerun make_dist.py with --rom-support -#else /* DUK_USE_ROM_OBJECTS */ -/* native functions: 149 */ -DUK_INTERNAL const duk_c_function duk_bi_native_functions[149] = { - duk_bi_array_constructor, - duk_bi_array_constructor_is_array, - duk_bi_array_prototype_concat, - duk_bi_array_prototype_indexof_shared, - duk_bi_array_prototype_iter_shared, - duk_bi_array_prototype_join_shared, - duk_bi_array_prototype_pop, - duk_bi_array_prototype_push, - duk_bi_array_prototype_reduce_shared, - duk_bi_array_prototype_reverse, - duk_bi_array_prototype_shift, - duk_bi_array_prototype_slice, - duk_bi_array_prototype_sort, - duk_bi_array_prototype_splice, - duk_bi_array_prototype_to_string, - duk_bi_array_prototype_unshift, - duk_bi_arraybuffer_constructor, - duk_bi_arraybuffer_isview, - duk_bi_boolean_constructor, - duk_bi_boolean_prototype_tostring_shared, - duk_bi_buffer_compare_shared, - duk_bi_buffer_constructor, - duk_bi_buffer_prototype_tostring_shared, - duk_bi_buffer_readfield, - duk_bi_buffer_slice_shared, - duk_bi_buffer_writefield, - duk_bi_dataview_constructor, - duk_bi_date_constructor, - duk_bi_date_constructor_now, - duk_bi_date_constructor_parse, - duk_bi_date_constructor_utc, - duk_bi_date_prototype_get_shared, - duk_bi_date_prototype_get_timezone_offset, - duk_bi_date_prototype_set_shared, - duk_bi_date_prototype_set_time, - duk_bi_date_prototype_to_json, - duk_bi_date_prototype_tostring_shared, - duk_bi_date_prototype_value_of, - duk_bi_duktape_object_act, - duk_bi_duktape_object_compact, - duk_bi_duktape_object_dec, - duk_bi_duktape_object_enc, - duk_bi_duktape_object_fin, - duk_bi_duktape_object_gc, - duk_bi_duktape_object_info, - duk_bi_error_constructor_shared, - duk_bi_error_prototype_filename_getter, - duk_bi_error_prototype_filename_setter, - duk_bi_error_prototype_linenumber_getter, - duk_bi_error_prototype_linenumber_setter, - duk_bi_error_prototype_stack_getter, - duk_bi_error_prototype_stack_setter, - duk_bi_error_prototype_to_string, - duk_bi_function_constructor, - duk_bi_function_prototype, - duk_bi_function_prototype_apply, - duk_bi_function_prototype_bind, - duk_bi_function_prototype_call, - duk_bi_function_prototype_to_string, - duk_bi_global_object_decode_uri, - duk_bi_global_object_decode_uri_component, - duk_bi_global_object_encode_uri, - duk_bi_global_object_encode_uri_component, - duk_bi_global_object_escape, - duk_bi_global_object_eval, - duk_bi_global_object_is_finite, - duk_bi_global_object_is_nan, - duk_bi_global_object_parse_float, - duk_bi_global_object_parse_int, - duk_bi_global_object_print_helper, - duk_bi_global_object_require, - duk_bi_global_object_unescape, - duk_bi_json_object_parse, - duk_bi_json_object_stringify, - duk_bi_logger_constructor, - duk_bi_logger_prototype_fmt, - duk_bi_logger_prototype_log_shared, - duk_bi_logger_prototype_raw, - duk_bi_math_object_max, - duk_bi_math_object_min, - duk_bi_math_object_onearg_shared, - duk_bi_math_object_random, - duk_bi_math_object_twoarg_shared, - duk_bi_nodejs_buffer_byte_length, - duk_bi_nodejs_buffer_concat, - duk_bi_nodejs_buffer_constructor, - duk_bi_nodejs_buffer_copy, - duk_bi_nodejs_buffer_fill, - duk_bi_nodejs_buffer_is_buffer, - duk_bi_nodejs_buffer_is_encoding, - duk_bi_nodejs_buffer_tojson, - duk_bi_nodejs_buffer_tostring, - duk_bi_nodejs_buffer_write, - duk_bi_number_constructor, - duk_bi_number_prototype_to_exponential, - duk_bi_number_prototype_to_fixed, - duk_bi_number_prototype_to_locale_string, - duk_bi_number_prototype_to_precision, - duk_bi_number_prototype_to_string, - duk_bi_number_prototype_value_of, - duk_bi_object_constructor, - duk_bi_object_constructor_create, - duk_bi_object_constructor_define_properties, - duk_bi_object_constructor_define_property, - duk_bi_object_constructor_get_own_property_descriptor, - duk_bi_object_constructor_is_extensible, - duk_bi_object_constructor_is_sealed_frozen_shared, - duk_bi_object_constructor_keys_shared, - duk_bi_object_constructor_prevent_extensions, - duk_bi_object_constructor_seal_freeze_shared, - duk_bi_object_getprototype_shared, - duk_bi_object_prototype_has_own_property, - duk_bi_object_prototype_is_prototype_of, - duk_bi_object_prototype_property_is_enumerable, - duk_bi_object_prototype_to_locale_string, - duk_bi_object_prototype_to_string, - duk_bi_object_prototype_value_of, - duk_bi_object_setprototype_shared, - duk_bi_pointer_constructor, - duk_bi_pointer_prototype_tostring_shared, - duk_bi_proxy_constructor, - duk_bi_regexp_constructor, - duk_bi_regexp_prototype_exec, - duk_bi_regexp_prototype_test, - duk_bi_regexp_prototype_to_string, - duk_bi_string_constructor, - duk_bi_string_constructor_from_char_code, - duk_bi_string_prototype_caseconv_shared, - duk_bi_string_prototype_char_at, - duk_bi_string_prototype_char_code_at, - duk_bi_string_prototype_concat, - duk_bi_string_prototype_indexof_shared, - duk_bi_string_prototype_locale_compare, - duk_bi_string_prototype_match, - duk_bi_string_prototype_replace, - duk_bi_string_prototype_search, - duk_bi_string_prototype_slice, - duk_bi_string_prototype_split, - duk_bi_string_prototype_substr, - duk_bi_string_prototype_substring, - duk_bi_string_prototype_to_string, - duk_bi_string_prototype_trim, - duk_bi_thread_constructor, - duk_bi_thread_current, - duk_bi_thread_resume, - duk_bi_thread_yield, - duk_bi_type_error_thrower, - duk_bi_typedarray_constructor, - duk_bi_typedarray_set, -}; -#if defined(DUK_USE_BUILTIN_INITJS) -DUK_INTERNAL const duk_uint8_t duk_initjs_data[187] = { -40,102,117,110,99,116,105,111,110,40,100,44,97,41,123,102,117,110,99,116, -105,111,110,32,98,40,97,44,98,44,99,41,123,79,98,106,101,99,116,46,100,101, -102,105,110,101,80,114,111,112,101,114,116,121,40,97,44,98,44,123,118,97, -108,117,101,58,99,44,119,114,105,116,97,98,108,101,58,33,48,44,101,110,117, -109,101,114,97,98,108,101,58,33,49,44,99,111,110,102,105,103,117,114,97,98, -108,101,58,33,48,125,41,125,98,40,97,46,76,111,103,103,101,114,44,34,99, -108,111,103,34,44,110,101,119,32,97,46,76,111,103,103,101,114,40,34,67,34, -41,41,59,98,40,97,44,34,109,111,100,76,111,97,100,101,100,34,44,123,125,41, -125,41,40,116,104,105,115,44,68,117,107,116,97,112,101,41,59,10,0, -}; -#endif /* DUK_USE_BUILTIN_INITJS */ -#if defined(DUK_USE_DOUBLE_LE) -DUK_INTERNAL const duk_uint8_t duk_builtins_data[3833] = { -105,195,75,32,3,148,52,154,248,9,26,13,128,112,105,0,240,22,20,26,95,124,6, -152,52,137,0,120,99,74,239,129,18,70,241,191,2,98,13,79,32,42,88,210,90,2, -240,1,50,141,37,168,76,94,216,118,69,229,203,127,44,0,84,163,73,106,21,75, -14,236,249,98,242,229,191,150,0,46,81,164,181,14,165,151,54,94,89,119,99, -203,23,151,45,252,176,1,146,141,37,168,93,63,59,186,97,241,23,151,45,252, -176,1,178,141,37,168,77,79,60,50,197,229,203,127,44,0,116,163,73,106,17,86, -148,152,188,185,111,229,128,15,148,129,198,137,36,58,166,142,91,251,212, -243,195,44,94,92,183,242,13,79,8,45,14,91,252,121,148,52,199,120,63,72,105, -21,240,118,128,210,237,224,245,17,165,43,224,211,55,231,207,151,148,161,70, -145,0,31,40,107,26,2,18,138,26,228,192,142,0,16,161,174,76,9,74,26,228,192, -158,0,8,161,174,76,10,96,2,42,26,228,192,174,0,26,161,174,76,11,96,3,74,26, -228,192,190,0,44,161,174,76,12,96,3,202,26,228,192,206,0,70,161,169,84,14, -202,3,255,254,32,234,0,0,0,0,0,0,7,195,248,119,0,0,0,0,0,0,3,193,252,57, -136,1,152,32,16,194,0,166,24,6,49,0,57,138,2,12,96,18,99,128,163,32,5,153, -40,76,94,216,118,69,229,203,127,35,41,10,165,135,118,124,177,121,114,223, -200,203,67,169,101,205,151,150,93,216,242,197,229,203,127,35,49,11,167,231, -119,76,62,34,242,229,191,145,154,132,212,243,195,44,94,92,183,242,51,144, -138,180,164,197,229,203,127,35,60,6,26,0,52,208,193,226,117,215,211,15,12, -166,146,11,67,150,255,30,77,24,58,113,64,243,92,8,27,0,68,217,130,70,212, -19,54,224,161,185,5,77,216,44,111,65,115,126,12,28,16,100,225,156,16,32,18, -17,195,15,46,121,100,238,232,136,136,87,12,60,185,229,141,179,126,30,136, -100,130,233,231,59,12,228,34,66,52,243,141,167,118,158,153,80,73,9,201,151, -30,252,153,106,210,146,118,72,150,76,184,247,228,203,86,148,152,123,246, -240,223,187,46,238,135,132,132,229,221,143,126,76,181,105,73,61,36,75,46, -236,123,242,101,171,74,76,61,251,120,111,221,151,119,67,226,65,178,243,199, -135,134,83,242,66,58,238,203,207,30,30,25,81,201,5,225,203,78,238,136,163, -208,92,59,50,242,232,138,62,0,2,38,163,19,255,255,224,142,80,192,0,20,31, -240,14,135,103,203,210,135,45,253,55,244,243,195,44,252,205,197,0,1,18,221, -82,0,3,24,207,151,164,254,251,168,114,223,195,47,46,158,98,101,231,143,150, -158,29,55,242,104,68,79,62,94,147,251,238,161,203,127,12,188,186,121,157, -135,110,94,109,100,131,99,229,151,15,76,172,168,8,89,217,16,201,151,54,157, -217,104,114,223,195,47,46,154,114,243,102,68,19,158,92,59,27,73,6,205,203, -46,95,89,91,74,0,3,17,225,203,47,108,187,186,69,241,211,46,238,122,119,238, -230,216,72,70,158,116,242,225,217,151,35,81,33,26,121,198,229,191,214,93, -205,69,0,1,134,105,231,23,199,76,187,185,233,197,179,43,73,32,154,242,249, -230,214,80,0,31,255,193,2,38,103,110,117,24,81,115,0,78,228,0,161,208,16, -237,24,121,207,239,186,135,45,252,50,242,233,229,188,144,221,60,232,114, -223,211,127,79,60,50,207,204,224,72,167,14,91,248,101,229,211,204,158,113, -119,117,219,151,150,28,91,50,184,144,40,95,224,0,15,248,64,4,20,78,129,5, -195,195,134,207,38,232,130,99,195,179,97,201,244,19,22,157,217,14,15,130, -135,254,0,48,125,60,224,242,229,135,200,9,1,255,12,2,162,136,112,2,112,80, -128,0,193,177,239,221,143,15,64,35,224,152,20,144,62,27,248,3,2,9,195,175, -61,0,231,208,126,89,123,101,229,207,40,72,32,188,244,105,205,208,40,16,94, -123,52,227,202,22,136,39,61,252,186,6,18,13,207,134,205,56,242,134,175,65, -250,238,231,163,78,110,129,231,208,125,59,178,101,241,63,48,25,248,0,12,47, -102,30,125,36,238,201,151,196,252,192,103,255,255,240,92,189,178,242,242,8, -105,4,231,191,110,80,67,80,0,24,62,109,252,162,225,199,160,16,212,0,10,7, -183,15,0,67,80,0,56,54,109,59,58,101,228,8,106,0,9,6,229,151,39,92,121,66, -15,192,0,97,124,178,228,235,143,45,45,57,244,116,8,63,255,255,10,39,248,0, -195,51,114,223,182,30,140,60,161,239,201,149,248,248,31,241,0,140,80,129, -202,10,49,128,10,35,1,6,199,163,15,40,61,32,9,10,199,163,15,40,123,242,101, -131,210,4,144,108,123,247,99,195,210,8,250,15,167,118,76,190,39,230,131,52, -133,236,195,207,164,157,217,50,248,159,154,12,212,0,6,27,179,126,60,59,50, -195,223,183,134,30,89,97,9,5,219,135,166,61,16,164,131,242,203,195,102,28, -121,97,145,6,231,151,15,44,122,33,201,5,231,179,78,60,177,8,130,243,225, -179,79,72,148,66,121,245,197,207,167,45,59,179,197,162,23,211,124,205,253, -242,242,135,135,158,87,240,68,122,111,153,191,30,29,153,102,111,239,151, -148,60,60,242,191,130,23,211,125,94,28,50,242,135,135,158,87,240,128,0,196, -122,111,153,191,30,29,153,106,240,225,151,148,60,60,242,191,132,0,6,9,211, -150,157,177,160,131,115,235,139,159,78,81,72,10,47,248,0,3,254,40,17,138, -48,66,136,152,64,0,66,129,48,5,27,252,88,76,216,54,47,214,131,50,172,88,31, -255,255,255,255,255,253,239,240,153,178,103,95,173,6,101,88,176,0,64,0,0,0, -0,0,0,3,168,0,0,0,0,0,0,31,15,241,26,19,233,201,169,38,180,91,242,103,70, -147,58,77,75,48,0,0,0,0,0,0,60,31,226,51,162,199,131,82,77,104,183,228,206, -141,38,116,154,150,96,0,0,0,0,0,0,120,127,128,15,248,192,70,40,0,0,0,0,0,0, -0,0,3,10,44,68,9,216,8,20,49,130,15,211,124,109,62,50,228,95,36,55,166,248, -190,56,111,221,151,119,77,56,118,47,18,23,211,125,14,89,113,233,231,167, -126,230,18,5,31,252,0,224,188,48,242,231,148,116,144,58,181,33,143,127,64, -247,111,238,56,0,127,199,2,49,72,0,0,0,0,0,0,248,127,180,81,36,4,51,166, -248,152,122,101,167,211,150,157,217,201,2,0,3,12,233,190,166,157,185,105, -244,229,167,118,114,64,128,1,4,228,129,0,3,137,116,223,51,126,60,59,50,196, -195,211,45,62,156,180,238,206,72,16,0,72,151,77,243,55,227,195,179,45,77, -59,114,211,233,203,78,236,228,129,0,5,10,73,2,0,12,21,18,4,0,28,82,35,32, -80,74,8,62,124,189,42,105,219,148,148,16,188,249,122,70,235,179,101,156, -184,121,15,132,0,34,29,159,47,74,181,33,198,235,179,101,156,184,121,15,132, -0,38,17,159,47,73,187,247,116,208,62,16,0,168,94,124,189,42,212,135,55,126, -238,154,7,194,0,23,7,207,151,164,76,61,50,143,132,0,50,21,159,47,74,181,33, -196,195,211,40,248,64,3,96,217,242,244,137,135,200,248,64,3,161,57,242,244, -171,82,28,76,62,71,194,0,31,8,207,151,164,141,253,121,115,31,8,0,132,47,62, -94,149,106,67,145,191,175,46,99,225,0,17,133,103,203,210,110,157,221,122, -101,230,62,16,1,40,110,124,189,42,212,135,55,78,238,189,50,243,31,8,0,156, -43,62,94,148,242,227,223,187,39,49,240,128,10,67,115,229,233,86,164,58,121, -113,239,221,147,152,248,64,5,97,249,242,244,155,167,102,205,60,242,227,223, -187,39,49,240,128,11,68,179,229,233,86,164,57,186,118,108,211,207,46,61, -251,178,115,31,8,0,188,71,62,94,149,52,237,203,235,126,236,179,243,102,231, -151,161,0,32,252,242,244,169,167,110,82,34,67,249,229,233,55,78,205,154, -121,229,199,191,118,78,100,37,0,24,137,115,203,210,173,72,115,116,236,217, -167,158,92,123,247,100,230,66,80,1,152,87,60,189,41,229,199,191,118,78,100, -43,224,3,80,222,121,122,85,169,14,158,92,123,247,100,230,66,190,0,55,10, -231,151,164,221,59,186,244,203,204,133,252,0,114,27,207,47,74,181,33,205, -211,187,175,76,188,200,95,192,7,97,28,242,244,145,191,175,46,100,51,224,3, -208,190,121,122,85,169,14,70,254,188,185,144,207,128,15,193,249,229,233,19, -15,76,164,37,0,32,133,115,203,210,173,72,113,48,244,202,66,80,2,24,71,60, -189,38,239,221,211,65,10,248,1,20,47,158,94,149,106,67,155,191,119,77,4,43, -224,4,112,190,121,122,70,235,179,101,156,184,121,16,191,128,18,67,185,229, -233,86,164,56,221,118,108,179,151,15,34,23,240,2,88,62,124,189,44,229,195, -200,124,32,4,208,126,121,122,89,203,135,145,9,64,9,194,145,254,0,0,255,144, -24,100,130,14,0,16,176,2,192,129,11,33,12,1,168,193,108,96,186,48,95,32,0, -0,0,0,0,0,0,0,56,38,95,25,113,189,18,9,211,47,62,143,100,20,95,0,20,159, -240,0,7,252,144,162,241,2,195,66,7,11,89,204,140,197,252,229,197,226,230, -115,3,16,69,19,64,5,43,252,0,9,255,40,16,188,33,49,123,97,217,23,151,45, -252,131,66,7,0,20,191,240,0,39,252,176,66,240,133,82,195,187,62,88,188,185, -111,228,26,16,56,0,166,127,128,1,63,230,2,23,132,58,150,92,217,121,101,221, -143,44,94,92,183,242,13,8,28,0,83,127,192,0,159,243,65,11,194,23,79,206, -238,152,124,69,229,203,127,32,208,129,192,5,59,252,0,9,255,56,16,188,33,53, -60,240,203,23,151,45,252,131,66,7,0,20,255,240,0,39,252,240,66,240,132,85, -165,38,47,46,91,249,6,132,14,0,31,255,228,64,98,192,105,87,20,139,10,191,5, -64,130,76,156,197,132,1,101,91,91,187,22,176,36,8,28,201,204,160,119,156, -253,127,33,23,115,31,193,102,79,142,202,44,15,232,34,182,84,113,95,115,248, -52,201,241,216,176,139,0,59,148,152,85,239,47,108,254,5,66,76,1,130,212,69, -79,178,16,148,8,61,58,52,170,49,190,202,6,105,219,251,52,245,7,49,252,22, -157,26,85,25,64,205,59,127,102,158,160,246,63,74,7,135,23,53,2,65,48,227, -223,205,64,160,0,48,76,60,244,238,80,40,0,20,19,15,76,59,148,10,0,7,5,195, -211,14,230,74,72,130,99,203,167,98,129,64,1,32,120,247,243,80,40,0,44,15, -47,142,10,5,0,6,130,230,217,191,127,37,2,128,3,192,246,111,206,160,80,0, -136,30,220,62,19,151,160,123,116,238,79,94,129,240,223,221,73,32,0,48,110, -88,119,100,223,181,68,16,94,91,250,238,200,160,80,0,152,31,61,59,148,10,0, -21,4,231,199,151,69,2,128,5,192,250,97,220,160,80,0,192,127,255,128,20,23, -134,30,92,242,164,34,19,207,167,45,59,179,233,205,229,37,129,127,255,0,0, -191,255,128,0,63,255,197,131,246,203,203,158,157,251,160,0,0,0,0,0,65,98, -32,3,166,156,30,53,32,249,165,131,76,223,159,62,94,70,172,114,16,176,144, -60,56,250,19,18,5,159,25,89,32,121,180,238,42,30,129,229,221,140,164,122,7, -147,46,50,129,232,62,61,251,120,97,199,208,156,129,83,127,0,50,250,69,3, -252,131,32,248,250,242,229,151,119,72,240,3,254,148,0,2,168,254,0,0,255, -167,0,33,68,88,32,0,33,64,176,2,170,254,0,0,255,169,0,33,69,220,32,0,33,67, -184,2,172,254,0,0,255,171,8,137,144,0,0,0,0,0,0,0,128,68,73,4,195,187,126, -226,8,4,178,16,41,164,32,147,7,136,52,193,240,0,18,17,48,124,0,8,133,76,31, -0,3,33,147,7,192,1,8,116,193,240,0,82,127,255,132,47,65,11,137,191,174,45, -153,98,242,229,191,144,105,4,95,47,46,91,249,32,211,185,6,94,92,183,242,65, -163,14,236,155,52,238,206,0,85,255,192,6,13,167,157,109,57,123,136,144,31, -245,192,3,5,231,179,78,60,163,9,0,2,10,199,248,0,3,254,192,4,32,249,242, -244,147,187,163,129,116,128,24,66,51,229,233,87,78,238,142,5,210,0,65,8, -207,151,164,157,221,24,182,23,72,1,140,39,62,94,149,116,238,232,197,176, -186,64,8,97,25,242,244,147,187,163,54,66,233,0,50,132,231,203,210,174,157, -221,25,178,23,72,1,20,43,62,94,145,182,111,195,209,155,33,116,128,17,194, -179,229,233,27,102,252,61,27,52,23,72,1,36,31,158,94,146,119,116,112,50, -208,3,8,71,60,189,42,233,221,209,192,203,64,8,33,28,242,244,147,187,163,22, -195,45,0,49,132,243,203,210,174,157,221,24,182,25,104,1,12,35,158,94,146, -119,116,102,200,101,160,6,80,158,121,122,85,211,187,163,54,67,45,0,34,133, -115,203,210,54,205,248,122,51,100,50,208,2,56,87,60,189,35,108,223,135,163, -102,131,45,0,36,7,255,248,1,11,50,136,132,115,235,139,15,46,88,124,140,36, -0,4,43,79,224,139,16,0,0,0,0,0,0,60,15,192,101,253,152,0,5,109,252,17,98,0, -0,0,0,0,0,7,129,248,12,191,181,0,0,174,63,130,44,64,0,0,0,0,0,0,240,63,1, -151,246,224,0,21,215,240,69,136,0,0,0,0,0,0,0,8,0,50,254,228,0,2,188,254,8, -177,0,0,0,0,0,0,0,1,0,6,95,221,128,0,87,223,193,22,32,0,0,0,0,0,0,8,32,0, -203,251,208,0,11,3,248,34,196,0,0,0,0,0,0,1,4,0,25,127,126,0,1,97,127,4,88, -128,0,0,0,0,0,0,32,128,3,47,240,64,0,44,79,224,139,16,0,0,0,0,0,0,8,16,0, -101,254,24,0,5,141,252,1,96,216,247,238,199,135,162,162,33,90,121,197,221, -143,126,77,59,179,172,146,17,167,156,46,185,179,101,228,176,65,89,77,16, -124,123,246,240,195,203,40,162,64,0,193,255,138,5,144,158,89,112,228,171, -39,119,71,2,232,132,114,203,135,36,157,221,28,11,164,0,66,25,203,46,28,149, -100,238,232,197,180,200,162,233,0,1,134,114,203,135,37,89,59,186,49,109,10, -40,186,64,2,97,124,178,225,201,39,119,70,45,166,69,23,72,0,140,47,150,92, -57,36,238,232,197,180,40,162,233,0,25,134,114,203,135,37,89,59,186,51,101, -50,40,186,64,0,161,156,178,225,201,86,78,238,140,217,66,138,46,144,0,168, -95,44,184,114,73,221,209,155,41,145,69,210,0,37,11,229,151,14,73,59,186,51, -101,10,40,186,64,6,161,124,178,225,201,27,102,252,61,38,69,23,72,0,28,47, -150,92,57,35,108,223,135,164,40,162,233,0,11,134,114,203,135,36,77,253,113, -108,203,50,40,186,64,1,33,156,178,225,201,19,127,92,91,50,194,138,46,144,0, -200,87,44,184,114,85,147,187,164,200,162,237,0,5,133,114,203,135,37,89,59, -186,66,138,46,208,0,216,79,44,184,114,73,221,210,100,81,118,128,10,194,121, -101,195,146,78,238,144,162,139,180,0,118,21,223,150,158,153,106,201,221, -209,192,203,33,61,249,105,233,150,78,238,142,6,90,0,33,13,239,203,79,76, -181,100,238,232,197,180,200,163,45,0,1,134,247,229,167,166,90,178,119,116, -98,218,20,81,150,128,4,195,59,242,211,211,44,157,221,24,182,153,20,101,160, -2,48,206,252,180,244,203,39,119,70,45,161,69,25,104,0,204,55,191,45,61,50, -213,147,187,163,54,83,34,140,180,0,10,27,223,150,158,153,106,201,221,209, -155,40,81,70,90,0,21,12,239,203,79,76,178,119,116,102,202,100,81,150,128,9, -67,59,242,211,211,44,157,221,25,178,133,20,101,160,3,80,206,252,180,244, -203,27,102,252,61,38,69,25,104,0,28,51,191,45,61,50,198,217,191,15,72,81, -70,90,0,23,13,239,203,79,76,177,55,245,197,179,44,200,163,45,0,4,134,247, -229,167,166,88,155,250,226,217,150,20,81,150,128,6,66,251,242,211,211,45, -89,59,186,76,138,51,16,0,88,95,126,90,122,101,171,39,119,72,81,70,98,0,27, -10,239,203,79,76,178,119,116,153,20,102,32,2,176,174,252,180,244,203,39, -119,72,81,70,98,0,58,40,173,176,82,90,4,19,54,157,155,21,217,6,203,199,174, -29,156,197,9,7,199,191,111,12,60,178,138,20,0,6,9,143,127,15,42,208,130, -243,217,167,30,81,132,65,123,242,211,211,42,228,0, -}; -#elif defined(DUK_USE_DOUBLE_BE) -DUK_INTERNAL const duk_uint8_t duk_builtins_data[3833] = { -105,195,75,32,3,148,52,154,248,9,26,13,128,112,105,0,240,22,20,26,95,124,6, -152,52,137,0,120,99,74,239,129,18,70,241,191,2,98,13,79,32,42,88,210,90,2, -240,1,50,141,37,168,76,94,216,118,69,229,203,127,44,0,84,163,73,106,21,75, -14,236,249,98,242,229,191,150,0,46,81,164,181,14,165,151,54,94,89,119,99, -203,23,151,45,252,176,1,146,141,37,168,93,63,59,186,97,241,23,151,45,252, -176,1,178,141,37,168,77,79,60,50,197,229,203,127,44,0,116,163,73,106,17,86, -148,152,188,185,111,229,128,15,148,129,198,137,36,58,166,142,91,251,212, -243,195,44,94,92,183,242,13,79,8,45,14,91,252,121,148,52,199,120,63,72,105, -21,240,118,128,210,237,224,245,17,165,43,224,211,55,231,207,151,148,161,70, -145,0,31,40,107,26,2,18,138,26,228,192,142,0,16,161,174,76,9,74,26,228,192, -158,0,8,161,174,76,10,96,2,42,26,228,192,174,0,26,161,174,76,11,96,3,74,26, -228,192,190,0,44,161,174,76,12,96,3,202,26,228,192,206,0,70,161,169,84,14, -202,3,255,254,32,234,3,255,192,0,0,0,0,0,0,119,1,255,192,0,0,0,0,0,0,57, -136,1,152,32,16,194,0,166,24,6,49,0,57,138,2,12,96,18,99,128,163,32,5,153, -40,76,94,216,118,69,229,203,127,35,41,10,165,135,118,124,177,121,114,223, -200,203,67,169,101,205,151,150,93,216,242,197,229,203,127,35,49,11,167,231, -119,76,62,34,242,229,191,145,154,132,212,243,195,44,94,92,183,242,51,144, -138,180,164,197,229,203,127,35,60,6,26,0,52,208,193,226,117,215,211,15,12, -166,146,11,67,150,255,30,77,24,58,113,64,243,92,8,27,0,68,217,130,70,212, -19,54,224,161,185,5,77,216,44,111,65,115,126,12,28,16,100,225,156,16,32,18, -17,195,15,46,121,100,238,232,136,136,87,12,60,185,229,141,179,126,30,136, -100,130,233,231,59,12,228,34,66,52,243,141,167,118,158,153,80,73,9,201,151, -30,252,153,106,210,146,118,72,150,76,184,247,228,203,86,148,152,123,246, -240,223,187,46,238,135,132,132,229,221,143,126,76,181,105,73,61,36,75,46, -236,123,242,101,171,74,76,61,251,120,111,221,151,119,67,226,65,178,243,199, -135,134,83,242,66,58,238,203,207,30,30,25,81,201,5,225,203,78,238,136,163, -208,92,59,50,242,232,138,62,0,2,38,163,19,255,255,224,142,80,192,0,20,31, -240,14,135,103,203,210,135,45,253,55,244,243,195,44,252,205,197,0,1,18,221, -82,0,3,24,207,151,164,254,251,168,114,223,195,47,46,158,98,101,231,143,150, -158,29,55,242,104,68,79,62,94,147,251,238,161,203,127,12,188,186,121,157, -135,110,94,109,100,131,99,229,151,15,76,172,168,8,89,217,16,201,151,54,157, -217,104,114,223,195,47,46,154,114,243,102,68,19,158,92,59,27,73,6,205,203, -46,95,89,91,74,0,3,17,225,203,47,108,187,186,69,241,211,46,238,122,119,238, -230,216,72,70,158,116,242,225,217,151,35,81,33,26,121,198,229,191,214,93, -205,69,0,1,134,105,231,23,199,76,187,185,233,197,179,43,73,32,154,242,249, -230,214,80,0,31,255,193,2,38,103,110,117,24,81,115,0,78,228,0,161,208,16, -237,24,121,207,239,186,135,45,252,50,242,233,229,188,144,221,60,232,114, -223,211,127,79,60,50,207,204,224,72,167,14,91,248,101,229,211,204,158,113, -119,117,219,151,150,28,91,50,184,144,40,95,224,0,15,248,64,4,20,78,129,5, -195,195,134,207,38,232,130,99,195,179,97,201,244,19,22,157,217,14,15,130, -135,254,0,48,125,60,224,242,229,135,200,9,1,255,12,2,162,136,112,2,112,80, -128,0,193,177,239,221,143,15,64,35,224,152,20,144,62,27,248,3,2,9,195,175, -61,0,231,208,126,89,123,101,229,207,40,72,32,188,244,105,205,208,40,16,94, -123,52,227,202,22,136,39,61,252,186,6,18,13,207,134,205,56,242,134,175,65, -250,238,231,163,78,110,129,231,208,125,59,178,101,241,63,48,25,248,0,12,47, -102,30,125,36,238,201,151,196,252,192,103,255,255,240,92,189,178,242,242,8, -105,4,231,191,110,80,67,80,0,24,62,109,252,162,225,199,160,16,212,0,10,7, -183,15,0,67,80,0,56,54,109,59,58,101,228,8,106,0,9,6,229,151,39,92,121,66, -15,192,0,97,124,178,228,235,143,45,45,57,244,116,8,63,255,255,10,39,248,0, -195,51,114,223,182,30,140,60,161,239,201,149,248,248,31,241,0,140,80,129, -202,10,49,128,10,35,1,6,199,163,15,40,61,32,9,10,199,163,15,40,123,242,101, -131,210,4,144,108,123,247,99,195,210,8,250,15,167,118,76,190,39,230,131,52, -133,236,195,207,164,157,217,50,248,159,154,12,212,0,6,27,179,126,60,59,50, -195,223,183,134,30,89,97,9,5,219,135,166,61,16,164,131,242,203,195,102,28, -121,97,145,6,231,151,15,44,122,33,201,5,231,179,78,60,177,8,130,243,225, -179,79,72,148,66,121,245,197,207,167,45,59,179,197,162,23,211,124,205,253, -242,242,135,135,158,87,240,68,122,111,153,191,30,29,153,102,111,239,151, -148,60,60,242,191,130,23,211,125,94,28,50,242,135,135,158,87,240,128,0,196, -122,111,153,191,30,29,153,106,240,225,151,148,60,60,242,191,132,0,6,9,211, -150,157,177,160,131,115,235,139,159,78,81,72,10,47,248,0,3,254,40,17,138, -48,66,136,152,64,0,66,129,48,5,27,252,88,76,216,54,47,214,131,50,172,88,15, -253,255,255,255,255,255,255,240,153,178,103,95,173,6,101,88,176,0,0,0,0,0, -0,0,0,67,168,15,255,0,0,0,0,0,0,17,26,19,233,201,169,38,180,91,242,103,70, -147,58,77,75,48,31,252,0,0,0,0,0,0,34,51,162,199,131,82,77,104,183,228,206, -141,38,116,154,150,96,127,248,0,0,0,0,0,0,0,15,248,192,70,40,0,0,0,0,0,0,0, -0,3,10,44,68,9,216,8,20,49,130,15,211,124,109,62,50,228,95,36,55,166,248, -190,56,111,221,151,119,77,56,118,47,18,23,211,125,14,89,113,233,231,167, -126,230,18,5,31,252,0,224,188,48,242,231,148,116,144,58,181,33,143,127,64, -247,111,238,56,0,127,199,2,49,72,127,248,0,0,0,0,0,0,180,81,36,4,51,166, -248,152,122,101,167,211,150,157,217,201,2,0,3,12,233,190,166,157,185,105, -244,229,167,118,114,64,128,1,4,228,129,0,3,137,116,223,51,126,60,59,50,196, -195,211,45,62,156,180,238,206,72,16,0,72,151,77,243,55,227,195,179,45,77, -59,114,211,233,203,78,236,228,129,0,5,10,73,2,0,12,21,18,4,0,28,82,35,32, -80,74,8,62,124,189,42,105,219,148,148,16,188,249,122,70,235,179,101,156, -184,121,15,132,0,34,29,159,47,74,181,33,198,235,179,101,156,184,121,15,132, -0,38,17,159,47,73,187,247,116,208,62,16,0,168,94,124,189,42,212,135,55,126, -238,154,7,194,0,23,7,207,151,164,76,61,50,143,132,0,50,21,159,47,74,181,33, -196,195,211,40,248,64,3,96,217,242,244,137,135,200,248,64,3,161,57,242,244, -171,82,28,76,62,71,194,0,31,8,207,151,164,141,253,121,115,31,8,0,132,47,62, -94,149,106,67,145,191,175,46,99,225,0,17,133,103,203,210,110,157,221,122, -101,230,62,16,1,40,110,124,189,42,212,135,55,78,238,189,50,243,31,8,0,156, -43,62,94,148,242,227,223,187,39,49,240,128,10,67,115,229,233,86,164,58,121, -113,239,221,147,152,248,64,5,97,249,242,244,155,167,102,205,60,242,227,223, -187,39,49,240,128,11,68,179,229,233,86,164,57,186,118,108,211,207,46,61, -251,178,115,31,8,0,188,71,62,94,149,52,237,203,235,126,236,179,243,102,231, -151,161,0,32,252,242,244,169,167,110,82,34,67,249,229,233,55,78,205,154, -121,229,199,191,118,78,100,37,0,24,137,115,203,210,173,72,115,116,236,217, -167,158,92,123,247,100,230,66,80,1,152,87,60,189,41,229,199,191,118,78,100, -43,224,3,80,222,121,122,85,169,14,158,92,123,247,100,230,66,190,0,55,10, -231,151,164,221,59,186,244,203,204,133,252,0,114,27,207,47,74,181,33,205, -211,187,175,76,188,200,95,192,7,97,28,242,244,145,191,175,46,100,51,224,3, -208,190,121,122,85,169,14,70,254,188,185,144,207,128,15,193,249,229,233,19, -15,76,164,37,0,32,133,115,203,210,173,72,113,48,244,202,66,80,2,24,71,60, -189,38,239,221,211,65,10,248,1,20,47,158,94,149,106,67,155,191,119,77,4,43, -224,4,112,190,121,122,70,235,179,101,156,184,121,16,191,128,18,67,185,229, -233,86,164,56,221,118,108,179,151,15,34,23,240,2,88,62,124,189,44,229,195, -200,124,32,4,208,126,121,122,89,203,135,145,9,64,9,194,145,254,0,0,255,144, -24,100,130,14,0,16,176,2,192,129,11,33,12,1,168,193,108,96,186,48,95,32,0, -0,0,0,0,0,0,0,56,38,95,25,113,189,18,9,211,47,62,143,100,20,95,0,20,159, -240,0,7,252,144,162,241,2,195,66,7,11,89,204,140,197,252,229,197,226,230, -115,3,16,69,19,64,5,43,252,0,9,255,40,16,188,33,49,123,97,217,23,151,45, -252,131,66,7,0,20,191,240,0,39,252,176,66,240,133,82,195,187,62,88,188,185, -111,228,26,16,56,0,166,127,128,1,63,230,2,23,132,58,150,92,217,121,101,221, -143,44,94,92,183,242,13,8,28,0,83,127,192,0,159,243,65,11,194,23,79,206, -238,152,124,69,229,203,127,32,208,129,192,5,59,252,0,9,255,56,16,188,33,53, -60,240,203,23,151,45,252,131,66,7,0,20,255,240,0,39,252,240,66,240,132,85, -165,38,47,46,91,249,6,132,14,0,31,255,228,64,98,192,64,5,191,10,139,20,87, -105,130,76,156,197,132,4,0,38,187,27,187,85,81,104,28,201,204,160,31,243, -23,33,127,125,28,247,193,102,79,142,202,44,3,255,113,84,118,82,184,47,232, -52,201,241,216,176,139,0,255,111,45,236,84,155,148,58,5,66,76,4,0,146,31, -181,68,66,209,136,61,58,52,170,49,190,202,1,255,53,4,243,51,249,222,108,22, -157,26,85,25,64,63,246,160,158,102,127,59,205,74,7,135,23,53,2,65,48,227, -223,205,64,160,0,48,76,60,244,238,80,40,0,20,19,15,76,59,148,10,0,7,5,195, -211,14,230,74,72,130,99,203,167,98,129,64,1,32,120,247,243,80,40,0,44,15, -47,142,10,5,0,6,130,230,217,191,127,37,2,128,3,192,246,111,206,160,80,0, -136,30,220,62,19,151,160,123,116,238,79,94,129,240,223,221,73,32,0,48,110, -88,119,100,223,181,68,16,94,91,250,238,200,160,80,0,152,31,61,59,148,10,0, -21,4,231,199,151,69,2,128,5,192,250,97,220,160,80,0,192,127,255,128,20,23, -134,30,92,242,164,34,19,207,167,45,59,179,233,205,229,37,129,127,255,0,0, -191,255,128,0,63,255,197,131,246,203,203,158,157,251,160,32,98,65,0,0,0,0, -0,3,166,156,30,53,32,249,165,131,76,223,159,62,94,70,172,114,16,176,144,60, -56,250,19,18,5,159,25,89,32,121,180,238,42,30,129,229,221,140,164,122,7, -147,46,50,129,232,62,61,251,120,97,199,208,156,129,83,127,0,50,250,69,3, -252,131,32,248,250,242,229,151,119,72,240,3,254,148,0,2,168,254,0,0,255, -167,0,33,68,88,32,0,33,64,176,2,170,254,0,0,255,169,0,33,69,220,32,0,33,67, -184,2,172,254,0,0,255,171,8,137,144,128,0,0,0,0,0,0,0,68,73,4,195,187,126, -226,8,4,178,16,41,164,32,147,7,136,52,193,240,0,18,17,48,124,0,8,133,76,31, -0,3,33,147,7,192,1,8,116,193,240,0,82,127,255,132,47,65,11,137,191,174,45, -153,98,242,229,191,144,105,4,95,47,46,91,249,32,211,185,6,94,92,183,242,65, -163,14,236,155,52,238,206,0,85,255,192,6,13,167,157,109,57,123,136,144,31, -245,192,3,5,231,179,78,60,163,9,0,2,10,199,248,0,3,254,192,4,32,249,242, -244,147,187,163,129,116,128,24,66,51,229,233,87,78,238,142,5,210,0,65,8, -207,151,164,157,221,24,182,23,72,1,140,39,62,94,149,116,238,232,197,176, -186,64,8,97,25,242,244,147,187,163,54,66,233,0,50,132,231,203,210,174,157, -221,25,178,23,72,1,20,43,62,94,145,182,111,195,209,155,33,116,128,17,194, -179,229,233,27,102,252,61,27,52,23,72,1,36,31,158,94,146,119,116,112,50, -208,3,8,71,60,189,42,233,221,209,192,203,64,8,33,28,242,244,147,187,163,22, -195,45,0,49,132,243,203,210,174,157,221,24,182,25,104,1,12,35,158,94,146, -119,116,102,200,101,160,6,80,158,121,122,85,211,187,163,54,67,45,0,34,133, -115,203,210,54,205,248,122,51,100,50,208,2,56,87,60,189,35,108,223,135,163, -102,131,45,0,36,7,255,248,1,11,50,136,132,115,235,139,15,46,88,124,140,36, -0,4,43,79,224,139,16,15,252,0,0,0,0,0,0,0,101,253,152,0,5,109,252,17,98,1, -255,128,0,0,0,0,0,0,12,191,181,0,0,174,63,130,44,64,63,240,0,0,0,0,0,0,1, -151,246,224,0,21,215,240,69,136,8,0,0,0,0,0,0,0,0,50,254,228,0,2,188,254,8, -177,1,0,0,0,0,0,0,0,0,6,95,221,128,0,87,223,193,22,32,32,8,0,0,0,0,0,0,0, -203,251,208,0,11,3,248,34,196,4,1,0,0,0,0,0,0,0,25,127,126,0,1,97,127,4,88, -128,128,32,0,0,0,0,0,0,3,47,240,64,0,44,79,224,139,16,16,8,0,0,0,0,0,0,0, -101,254,24,0,5,141,252,1,96,216,247,238,199,135,162,162,33,90,121,197,221, -143,126,77,59,179,172,146,17,167,156,46,185,179,101,228,176,65,89,77,16, -124,123,246,240,195,203,40,162,64,0,193,255,138,5,144,158,89,112,228,171, -39,119,71,2,232,132,114,203,135,36,157,221,28,11,164,0,66,25,203,46,28,149, -100,238,232,197,180,200,162,233,0,1,134,114,203,135,37,89,59,186,49,109,10, -40,186,64,2,97,124,178,225,201,39,119,70,45,166,69,23,72,0,140,47,150,92, -57,36,238,232,197,180,40,162,233,0,25,134,114,203,135,37,89,59,186,51,101, -50,40,186,64,0,161,156,178,225,201,86,78,238,140,217,66,138,46,144,0,168, -95,44,184,114,73,221,209,155,41,145,69,210,0,37,11,229,151,14,73,59,186,51, -101,10,40,186,64,6,161,124,178,225,201,27,102,252,61,38,69,23,72,0,28,47, -150,92,57,35,108,223,135,164,40,162,233,0,11,134,114,203,135,36,77,253,113, -108,203,50,40,186,64,1,33,156,178,225,201,19,127,92,91,50,194,138,46,144,0, -200,87,44,184,114,85,147,187,164,200,162,237,0,5,133,114,203,135,37,89,59, -186,66,138,46,208,0,216,79,44,184,114,73,221,210,100,81,118,128,10,194,121, -101,195,146,78,238,144,162,139,180,0,118,21,223,150,158,153,106,201,221, -209,192,203,33,61,249,105,233,150,78,238,142,6,90,0,33,13,239,203,79,76, -181,100,238,232,197,180,200,163,45,0,1,134,247,229,167,166,90,178,119,116, -98,218,20,81,150,128,4,195,59,242,211,211,44,157,221,24,182,153,20,101,160, -2,48,206,252,180,244,203,39,119,70,45,161,69,25,104,0,204,55,191,45,61,50, -213,147,187,163,54,83,34,140,180,0,10,27,223,150,158,153,106,201,221,209, -155,40,81,70,90,0,21,12,239,203,79,76,178,119,116,102,202,100,81,150,128,9, -67,59,242,211,211,44,157,221,25,178,133,20,101,160,3,80,206,252,180,244, -203,27,102,252,61,38,69,25,104,0,28,51,191,45,61,50,198,217,191,15,72,81, -70,90,0,23,13,239,203,79,76,177,55,245,197,179,44,200,163,45,0,4,134,247, -229,167,166,88,155,250,226,217,150,20,81,150,128,6,66,251,242,211,211,45, -89,59,186,76,138,51,16,0,88,95,126,90,122,101,171,39,119,72,81,70,98,0,27, -10,239,203,79,76,178,119,116,153,20,102,32,2,176,174,252,180,244,203,39, -119,72,81,70,98,0,58,40,173,176,82,90,4,19,54,157,155,21,217,6,203,199,174, -29,156,197,9,7,199,191,111,12,60,178,138,20,0,6,9,143,127,15,42,208,130, -243,217,167,30,81,132,65,123,242,211,211,42,228,0, -}; -#elif defined(DUK_USE_DOUBLE_ME) -DUK_INTERNAL const duk_uint8_t duk_builtins_data[3833] = { -105,195,75,32,3,148,52,154,248,9,26,13,128,112,105,0,240,22,20,26,95,124,6, -152,52,137,0,120,99,74,239,129,18,70,241,191,2,98,13,79,32,42,88,210,90,2, -240,1,50,141,37,168,76,94,216,118,69,229,203,127,44,0,84,163,73,106,21,75, -14,236,249,98,242,229,191,150,0,46,81,164,181,14,165,151,54,94,89,119,99, -203,23,151,45,252,176,1,146,141,37,168,93,63,59,186,97,241,23,151,45,252, -176,1,178,141,37,168,77,79,60,50,197,229,203,127,44,0,116,163,73,106,17,86, -148,152,188,185,111,229,128,15,148,129,198,137,36,58,166,142,91,251,212, -243,195,44,94,92,183,242,13,79,8,45,14,91,252,121,148,52,199,120,63,72,105, -21,240,118,128,210,237,224,245,17,165,43,224,211,55,231,207,151,148,161,70, -145,0,31,40,107,26,2,18,138,26,228,192,142,0,16,161,174,76,9,74,26,228,192, -158,0,8,161,174,76,10,96,2,42,26,228,192,174,0,26,161,174,76,11,96,3,74,26, -228,192,190,0,44,161,174,76,12,96,3,202,26,228,192,206,0,70,161,169,84,14, -202,3,255,254,32,234,0,0,7,195,248,0,0,0,0,119,0,0,3,193,252,0,0,0,0,57, -136,1,152,32,16,194,0,166,24,6,49,0,57,138,2,12,96,18,99,128,163,32,5,153, -40,76,94,216,118,69,229,203,127,35,41,10,165,135,118,124,177,121,114,223, -200,203,67,169,101,205,151,150,93,216,242,197,229,203,127,35,49,11,167,231, -119,76,62,34,242,229,191,145,154,132,212,243,195,44,94,92,183,242,51,144, -138,180,164,197,229,203,127,35,60,6,26,0,52,208,193,226,117,215,211,15,12, -166,146,11,67,150,255,30,77,24,58,113,64,243,92,8,27,0,68,217,130,70,212, -19,54,224,161,185,5,77,216,44,111,65,115,126,12,28,16,100,225,156,16,32,18, -17,195,15,46,121,100,238,232,136,136,87,12,60,185,229,141,179,126,30,136, -100,130,233,231,59,12,228,34,66,52,243,141,167,118,158,153,80,73,9,201,151, -30,252,153,106,210,146,118,72,150,76,184,247,228,203,86,148,152,123,246, -240,223,187,46,238,135,132,132,229,221,143,126,76,181,105,73,61,36,75,46, -236,123,242,101,171,74,76,61,251,120,111,221,151,119,67,226,65,178,243,199, -135,134,83,242,66,58,238,203,207,30,30,25,81,201,5,225,203,78,238,136,163, -208,92,59,50,242,232,138,62,0,2,38,163,19,255,255,224,142,80,192,0,20,31, -240,14,135,103,203,210,135,45,253,55,244,243,195,44,252,205,197,0,1,18,221, -82,0,3,24,207,151,164,254,251,168,114,223,195,47,46,158,98,101,231,143,150, -158,29,55,242,104,68,79,62,94,147,251,238,161,203,127,12,188,186,121,157, -135,110,94,109,100,131,99,229,151,15,76,172,168,8,89,217,16,201,151,54,157, -217,104,114,223,195,47,46,154,114,243,102,68,19,158,92,59,27,73,6,205,203, -46,95,89,91,74,0,3,17,225,203,47,108,187,186,69,241,211,46,238,122,119,238, -230,216,72,70,158,116,242,225,217,151,35,81,33,26,121,198,229,191,214,93, -205,69,0,1,134,105,231,23,199,76,187,185,233,197,179,43,73,32,154,242,249, -230,214,80,0,31,255,193,2,38,103,110,117,24,81,115,0,78,228,0,161,208,16, -237,24,121,207,239,186,135,45,252,50,242,233,229,188,144,221,60,232,114, -223,211,127,79,60,50,207,204,224,72,167,14,91,248,101,229,211,204,158,113, -119,117,219,151,150,28,91,50,184,144,40,95,224,0,15,248,64,4,20,78,129,5, -195,195,134,207,38,232,130,99,195,179,97,201,244,19,22,157,217,14,15,130, -135,254,0,48,125,60,224,242,229,135,200,9,1,255,12,2,162,136,112,2,112,80, -128,0,193,177,239,221,143,15,64,35,224,152,20,144,62,27,248,3,2,9,195,175, -61,0,231,208,126,89,123,101,229,207,40,72,32,188,244,105,205,208,40,16,94, -123,52,227,202,22,136,39,61,252,186,6,18,13,207,134,205,56,242,134,175,65, -250,238,231,163,78,110,129,231,208,125,59,178,101,241,63,48,25,248,0,12,47, -102,30,125,36,238,201,151,196,252,192,103,255,255,240,92,189,178,242,242,8, -105,4,231,191,110,80,67,80,0,24,62,109,252,162,225,199,160,16,212,0,10,7, -183,15,0,67,80,0,56,54,109,59,58,101,228,8,106,0,9,6,229,151,39,92,121,66, -15,192,0,97,124,178,228,235,143,45,45,57,244,116,8,63,255,255,10,39,248,0, -195,51,114,223,182,30,140,60,161,239,201,149,248,248,31,241,0,140,80,129, -202,10,49,128,10,35,1,6,199,163,15,40,61,32,9,10,199,163,15,40,123,242,101, -131,210,4,144,108,123,247,99,195,210,8,250,15,167,118,76,190,39,230,131,52, -133,236,195,207,164,157,217,50,248,159,154,12,212,0,6,27,179,126,60,59,50, -195,223,183,134,30,89,97,9,5,219,135,166,61,16,164,131,242,203,195,102,28, -121,97,145,6,231,151,15,44,122,33,201,5,231,179,78,60,177,8,130,243,225, -179,79,72,148,66,121,245,197,207,167,45,59,179,197,162,23,211,124,205,253, -242,242,135,135,158,87,240,68,122,111,153,191,30,29,153,102,111,239,151, -148,60,60,242,191,130,23,211,125,94,28,50,242,135,135,158,87,240,128,0,196, -122,111,153,191,30,29,153,106,240,225,151,148,60,60,242,191,132,0,6,9,211, -150,157,177,160,131,115,235,139,159,78,81,72,10,47,248,0,3,254,40,17,138, -48,66,136,152,64,0,66,129,48,5,27,252,88,76,216,54,47,214,131,50,172,88,31, -255,253,239,255,255,255,255,240,153,178,103,95,173,6,101,88,176,0,0,0,0,0, -64,0,0,3,168,0,0,31,15,224,0,0,0,17,26,19,233,201,169,38,180,91,242,103,70, -147,58,77,75,48,0,0,60,31,192,0,0,0,34,51,162,199,131,82,77,104,183,228, -206,141,38,116,154,150,96,0,0,120,127,128,0,0,0,0,15,248,192,70,40,0,0,0,0, -0,0,0,0,3,10,44,68,9,216,8,20,49,130,15,211,124,109,62,50,228,95,36,55,166, -248,190,56,111,221,151,119,77,56,118,47,18,23,211,125,14,89,113,233,231, -167,126,230,18,5,31,252,0,224,188,48,242,231,148,116,144,58,181,33,143,127, -64,247,111,238,56,0,127,199,2,49,72,0,0,248,127,0,0,0,0,180,81,36,4,51,166, -248,152,122,101,167,211,150,157,217,201,2,0,3,12,233,190,166,157,185,105, -244,229,167,118,114,64,128,1,4,228,129,0,3,137,116,223,51,126,60,59,50,196, -195,211,45,62,156,180,238,206,72,16,0,72,151,77,243,55,227,195,179,45,77, -59,114,211,233,203,78,236,228,129,0,5,10,73,2,0,12,21,18,4,0,28,82,35,32, -80,74,8,62,124,189,42,105,219,148,148,16,188,249,122,70,235,179,101,156, -184,121,15,132,0,34,29,159,47,74,181,33,198,235,179,101,156,184,121,15,132, -0,38,17,159,47,73,187,247,116,208,62,16,0,168,94,124,189,42,212,135,55,126, -238,154,7,194,0,23,7,207,151,164,76,61,50,143,132,0,50,21,159,47,74,181,33, -196,195,211,40,248,64,3,96,217,242,244,137,135,200,248,64,3,161,57,242,244, -171,82,28,76,62,71,194,0,31,8,207,151,164,141,253,121,115,31,8,0,132,47,62, -94,149,106,67,145,191,175,46,99,225,0,17,133,103,203,210,110,157,221,122, -101,230,62,16,1,40,110,124,189,42,212,135,55,78,238,189,50,243,31,8,0,156, -43,62,94,148,242,227,223,187,39,49,240,128,10,67,115,229,233,86,164,58,121, -113,239,221,147,152,248,64,5,97,249,242,244,155,167,102,205,60,242,227,223, -187,39,49,240,128,11,68,179,229,233,86,164,57,186,118,108,211,207,46,61, -251,178,115,31,8,0,188,71,62,94,149,52,237,203,235,126,236,179,243,102,231, -151,161,0,32,252,242,244,169,167,110,82,34,67,249,229,233,55,78,205,154, -121,229,199,191,118,78,100,37,0,24,137,115,203,210,173,72,115,116,236,217, -167,158,92,123,247,100,230,66,80,1,152,87,60,189,41,229,199,191,118,78,100, -43,224,3,80,222,121,122,85,169,14,158,92,123,247,100,230,66,190,0,55,10, -231,151,164,221,59,186,244,203,204,133,252,0,114,27,207,47,74,181,33,205, -211,187,175,76,188,200,95,192,7,97,28,242,244,145,191,175,46,100,51,224,3, -208,190,121,122,85,169,14,70,254,188,185,144,207,128,15,193,249,229,233,19, -15,76,164,37,0,32,133,115,203,210,173,72,113,48,244,202,66,80,2,24,71,60, -189,38,239,221,211,65,10,248,1,20,47,158,94,149,106,67,155,191,119,77,4,43, -224,4,112,190,121,122,70,235,179,101,156,184,121,16,191,128,18,67,185,229, -233,86,164,56,221,118,108,179,151,15,34,23,240,2,88,62,124,189,44,229,195, -200,124,32,4,208,126,121,122,89,203,135,145,9,64,9,194,145,254,0,0,255,144, -24,100,130,14,0,16,176,2,192,129,11,33,12,1,168,193,108,96,186,48,95,32,0, -0,0,0,0,0,0,0,56,38,95,25,113,189,18,9,211,47,62,143,100,20,95,0,20,159, -240,0,7,252,144,162,241,2,195,66,7,11,89,204,140,197,252,229,197,226,230, -115,3,16,69,19,64,5,43,252,0,9,255,40,16,188,33,49,123,97,217,23,151,45, -252,131,66,7,0,20,191,240,0,39,252,176,66,240,133,82,195,187,62,88,188,185, -111,228,26,16,56,0,166,127,128,1,63,230,2,23,132,58,150,92,217,121,101,221, -143,44,94,92,183,242,13,8,28,0,83,127,192,0,159,243,65,11,194,23,79,206, -238,152,124,69,229,203,127,32,208,129,192,5,59,252,0,9,255,56,16,188,33,53, -60,240,203,23,151,45,252,131,66,7,0,20,255,240,0,39,252,240,66,240,132,85, -165,38,47,46,91,249,6,132,14,0,31,255,228,64,98,192,10,191,5,64,105,87,20, -139,130,76,156,197,132,11,22,176,36,1,101,91,91,184,28,201,204,160,33,23, -115,31,247,156,253,127,65,102,79,142,202,44,4,113,95,115,255,232,34,182,88, -52,201,241,216,176,139,1,239,47,108,252,59,148,152,86,5,66,76,15,178,16, -148,1,130,212,69,72,61,58,52,170,49,190,202,4,245,7,49,254,105,219,251,52, -22,157,26,85,25,64,158,160,246,63,205,59,127,102,74,7,135,23,53,2,65,48, -227,223,205,64,160,0,48,76,60,244,238,80,40,0,20,19,15,76,59,148,10,0,7,5, -195,211,14,230,74,72,130,99,203,167,98,129,64,1,32,120,247,243,80,40,0,44, -15,47,142,10,5,0,6,130,230,217,191,127,37,2,128,3,192,246,111,206,160,80,0, -136,30,220,62,19,151,160,123,116,238,79,94,129,240,223,221,73,32,0,48,110, -88,119,100,223,181,68,16,94,91,250,238,200,160,80,0,152,31,61,59,148,10,0, -21,4,231,199,151,69,2,128,5,192,250,97,220,160,80,0,192,127,255,128,20,23, -134,30,92,242,164,34,19,207,167,45,59,179,233,205,229,37,129,127,255,0,0, -191,255,128,0,63,255,197,131,246,203,203,158,157,251,160,0,65,98,32,0,0,0, -0,3,166,156,30,53,32,249,165,131,76,223,159,62,94,70,172,114,16,176,144,60, -56,250,19,18,5,159,25,89,32,121,180,238,42,30,129,229,221,140,164,122,7, -147,46,50,129,232,62,61,251,120,97,199,208,156,129,83,127,0,50,250,69,3, -252,131,32,248,250,242,229,151,119,72,240,3,254,148,0,2,168,254,0,0,255, -167,0,33,68,88,32,0,33,64,176,2,170,254,0,0,255,169,0,33,69,220,32,0,33,67, -184,2,172,254,0,0,255,171,8,137,144,0,0,0,128,0,0,0,0,68,73,4,195,187,126, -226,8,4,178,16,41,164,32,147,7,136,52,193,240,0,18,17,48,124,0,8,133,76,31, -0,3,33,147,7,192,1,8,116,193,240,0,82,127,255,132,47,65,11,137,191,174,45, -153,98,242,229,191,144,105,4,95,47,46,91,249,32,211,185,6,94,92,183,242,65, -163,14,236,155,52,238,206,0,85,255,192,6,13,167,157,109,57,123,136,144,31, -245,192,3,5,231,179,78,60,163,9,0,2,10,199,248,0,3,254,192,4,32,249,242, -244,147,187,163,129,116,128,24,66,51,229,233,87,78,238,142,5,210,0,65,8, -207,151,164,157,221,24,182,23,72,1,140,39,62,94,149,116,238,232,197,176, -186,64,8,97,25,242,244,147,187,163,54,66,233,0,50,132,231,203,210,174,157, -221,25,178,23,72,1,20,43,62,94,145,182,111,195,209,155,33,116,128,17,194, -179,229,233,27,102,252,61,27,52,23,72,1,36,31,158,94,146,119,116,112,50, -208,3,8,71,60,189,42,233,221,209,192,203,64,8,33,28,242,244,147,187,163,22, -195,45,0,49,132,243,203,210,174,157,221,24,182,25,104,1,12,35,158,94,146, -119,116,102,200,101,160,6,80,158,121,122,85,211,187,163,54,67,45,0,34,133, -115,203,210,54,205,248,122,51,100,50,208,2,56,87,60,189,35,108,223,135,163, -102,131,45,0,36,7,255,248,1,11,50,136,132,115,235,139,15,46,88,124,140,36, -0,4,43,79,224,139,16,0,0,60,15,192,0,0,0,0,101,253,152,0,5,109,252,17,98,0, -0,7,129,248,0,0,0,0,12,191,181,0,0,174,63,130,44,64,0,0,240,63,0,0,0,0,1, -151,246,224,0,21,215,240,69,136,0,0,0,8,0,0,0,0,0,50,254,228,0,2,188,254,8, -177,0,0,0,1,0,0,0,0,0,6,95,221,128,0,87,223,193,22,32,0,0,8,32,0,0,0,0,0, -203,251,208,0,11,3,248,34,196,0,0,1,4,0,0,0,0,0,25,127,126,0,1,97,127,4,88, -128,0,0,32,128,0,0,0,0,3,47,240,64,0,44,79,224,139,16,0,0,8,16,0,0,0,0,0, -101,254,24,0,5,141,252,1,96,216,247,238,199,135,162,162,33,90,121,197,221, -143,126,77,59,179,172,146,17,167,156,46,185,179,101,228,176,65,89,77,16, -124,123,246,240,195,203,40,162,64,0,193,255,138,5,144,158,89,112,228,171, -39,119,71,2,232,132,114,203,135,36,157,221,28,11,164,0,66,25,203,46,28,149, -100,238,232,197,180,200,162,233,0,1,134,114,203,135,37,89,59,186,49,109,10, -40,186,64,2,97,124,178,225,201,39,119,70,45,166,69,23,72,0,140,47,150,92, -57,36,238,232,197,180,40,162,233,0,25,134,114,203,135,37,89,59,186,51,101, -50,40,186,64,0,161,156,178,225,201,86,78,238,140,217,66,138,46,144,0,168, -95,44,184,114,73,221,209,155,41,145,69,210,0,37,11,229,151,14,73,59,186,51, -101,10,40,186,64,6,161,124,178,225,201,27,102,252,61,38,69,23,72,0,28,47, -150,92,57,35,108,223,135,164,40,162,233,0,11,134,114,203,135,36,77,253,113, -108,203,50,40,186,64,1,33,156,178,225,201,19,127,92,91,50,194,138,46,144,0, -200,87,44,184,114,85,147,187,164,200,162,237,0,5,133,114,203,135,37,89,59, -186,66,138,46,208,0,216,79,44,184,114,73,221,210,100,81,118,128,10,194,121, -101,195,146,78,238,144,162,139,180,0,118,21,223,150,158,153,106,201,221, -209,192,203,33,61,249,105,233,150,78,238,142,6,90,0,33,13,239,203,79,76, -181,100,238,232,197,180,200,163,45,0,1,134,247,229,167,166,90,178,119,116, -98,218,20,81,150,128,4,195,59,242,211,211,44,157,221,24,182,153,20,101,160, -2,48,206,252,180,244,203,39,119,70,45,161,69,25,104,0,204,55,191,45,61,50, -213,147,187,163,54,83,34,140,180,0,10,27,223,150,158,153,106,201,221,209, -155,40,81,70,90,0,21,12,239,203,79,76,178,119,116,102,202,100,81,150,128,9, -67,59,242,211,211,44,157,221,25,178,133,20,101,160,3,80,206,252,180,244, -203,27,102,252,61,38,69,25,104,0,28,51,191,45,61,50,198,217,191,15,72,81, -70,90,0,23,13,239,203,79,76,177,55,245,197,179,44,200,163,45,0,4,134,247, -229,167,166,88,155,250,226,217,150,20,81,150,128,6,66,251,242,211,211,45, -89,59,186,76,138,51,16,0,88,95,126,90,122,101,171,39,119,72,81,70,98,0,27, -10,239,203,79,76,178,119,116,153,20,102,32,2,176,174,252,180,244,203,39, -119,72,81,70,98,0,58,40,173,176,82,90,4,19,54,157,155,21,217,6,203,199,174, -29,156,197,9,7,199,191,111,12,60,178,138,20,0,6,9,143,127,15,42,208,130, -243,217,167,30,81,132,65,123,242,211,211,42,228,0, -}; -#else -#error invalid endianness defines -#endif -#endif /* DUK_USE_ROM_OBJECTS */ -#line 1 "duk_error_macros.c" -/* - * Error, fatal, and panic handling. - */ - -/* include removed: duk_internal.h */ - -#define DUK__ERRFMT_BUFSIZE 256 /* size for formatting buffers */ - -#if defined(DUK_USE_VERBOSE_ERRORS) - -DUK_INTERNAL void duk_err_handle_error_fmt(duk_hthread *thr, const char *filename, duk_uint_t line_and_code, const char *fmt, ...) { - va_list ap; - char msg[DUK__ERRFMT_BUFSIZE]; - va_start(ap, fmt); - (void) DUK_VSNPRINTF(msg, sizeof(msg), fmt, ap); - msg[sizeof(msg) - 1] = (char) 0; - duk_err_create_and_throw(thr, (duk_errcode_t) (line_and_code >> 24), msg, filename, (duk_int_t) (line_and_code & 0x00ffffffL)); - va_end(ap); /* dead code, but ensures portability (see Linux man page notes) */ -} - -DUK_INTERNAL void duk_err_handle_error(duk_hthread *thr, const char *filename, duk_uint_t line_and_code, const char *msg) { - duk_err_create_and_throw(thr, (duk_errcode_t) (line_and_code >> 24), msg, filename, (duk_int_t) (line_and_code & 0x00ffffffL)); -} - -#else /* DUK_USE_VERBOSE_ERRORS */ - -DUK_INTERNAL void duk_err_handle_error(duk_hthread *thr, duk_errcode_t code) { - duk_err_create_and_throw(thr, code); -} - -#endif /* DUK_USE_VERBOSE_ERRORS */ - -/* - * Error throwing helpers - */ - -#if defined(DUK_USE_VERBOSE_ERRORS) -#if defined(DUK_USE_PARANOID_ERRORS) -DUK_INTERNAL void duk_err_require_type_index(duk_hthread *thr, const char *filename, duk_int_t linenumber, duk_idx_t index, const char *expect_name) { - DUK_ERROR_RAW_FMT3(thr, filename, linenumber, DUK_ERR_TYPE_ERROR, "%s required, found %s (stack index %ld)", - expect_name, duk_get_type_name((duk_context *) thr, index), (long) index); -} -#else -DUK_INTERNAL void duk_err_require_type_index(duk_hthread *thr, const char *filename, duk_int_t linenumber, duk_idx_t index, const char *expect_name) { - DUK_ERROR_RAW_FMT3(thr, filename, linenumber, DUK_ERR_TYPE_ERROR, "%s required, found %s (stack index %ld)", - expect_name, duk_push_string_readable((duk_context *) thr, index), (long) index); -} -#endif -DUK_INTERNAL void duk_err_range(duk_hthread *thr, const char *filename, duk_int_t linenumber, const char *message) { - DUK_ERROR_RAW(thr, filename, linenumber, DUK_ERR_RANGE_ERROR, message); -} -DUK_INTERNAL void duk_err_api_index(duk_hthread *thr, const char *filename, duk_int_t linenumber, duk_idx_t index) { - DUK_ERROR_RAW_FMT1(thr, filename, linenumber, DUK_ERR_API_ERROR, "invalid stack index %ld", (long) (index)); -} -DUK_INTERNAL void duk_err_api(duk_hthread *thr, const char *filename, duk_int_t linenumber, const char *message) { - DUK_ERROR_RAW(thr, filename, linenumber, DUK_ERR_API_ERROR, message); -} -DUK_INTERNAL void duk_err_unimplemented_defmsg(duk_hthread *thr, const char *filename, duk_int_t linenumber) { - DUK_ERROR_RAW(thr, filename, linenumber, DUK_ERR_UNIMPLEMENTED_ERROR, DUK_STR_UNIMPLEMENTED); -} -#ifndef DUK_USE_BYTECODE_DUMP_SUPPORT -DUK_INTERNAL void duk_err_unsupported_defmsg(duk_hthread *thr, const char *filename, duk_int_t linenumber) { - DUK_ERROR_RAW(thr, filename, linenumber, DUK_ERR_UNSUPPORTED_ERROR, DUK_STR_UNSUPPORTED); -} -#endif -DUK_INTERNAL void duk_err_internal_defmsg(duk_hthread *thr, const char *filename, duk_int_t linenumber) { - DUK_ERROR_RAW(thr, filename, linenumber, DUK_ERR_INTERNAL_ERROR, DUK_STR_INTERNAL_ERROR); -} -DUK_INTERNAL void duk_err_internal(duk_hthread *thr, const char *filename, duk_int_t linenumber, const char *message) { - DUK_ERROR_RAW(thr, filename, linenumber, DUK_ERR_INTERNAL_ERROR, message); -} -DUK_INTERNAL void duk_err_alloc(duk_hthread *thr, const char *filename, duk_int_t linenumber, const char *message) { - DUK_ERROR_RAW(thr, filename, linenumber, DUK_ERR_ALLOC_ERROR, message); -} -#else -/* The file/line arguments are NULL and 0, they're ignored by DUK_ERROR_RAW() - * when non-verbose errors are used. - */ -DUK_INTERNAL void duk_err_type(duk_hthread *thr) { - DUK_ERROR_RAW(thr, NULL, 0, DUK_ERR_TYPE_ERROR, NULL); -} -DUK_INTERNAL void duk_err_api(duk_hthread *thr) { - DUK_ERROR_RAW(thr, NULL, 0, DUK_ERR_API_ERROR, NULL); -} -DUK_INTERNAL void duk_err_range(duk_hthread *thr) { - DUK_ERROR_RAW(thr, NULL, 0, DUK_ERR_RANGE_ERROR, NULL); -} -DUK_INTERNAL void duk_err_syntax(duk_hthread *thr) { - DUK_ERROR_RAW(thr, NULL, 0, DUK_ERR_SYNTAX_ERROR, NULL); -} -DUK_INTERNAL void duk_err_unimplemented(duk_hthread *thr) { - DUK_ERROR_RAW(thr, NULL, 0, DUK_ERR_UNIMPLEMENTED_ERROR, NULL); -} -DUK_INTERNAL void duk_err_unsupported(duk_hthread *thr) { - DUK_ERROR_RAW(thr, NULL, 0, DUK_ERR_UNSUPPORTED_ERROR, NULL); -} -DUK_INTERNAL void duk_err_internal(duk_hthread *thr) { - DUK_ERROR_RAW(thr, NULL, 0, DUK_ERR_INTERNAL_ERROR, NULL); -} -DUK_INTERNAL void duk_err_alloc(duk_hthread *thr) { - DUK_ERROR_RAW(thr, NULL, thr, DUK_ERR_ALLOC_ERROR, NULL); -} -#endif - -/* - * Default fatal error handler - */ - -DUK_INTERNAL void duk_default_fatal_handler(duk_context *ctx, duk_errcode_t code, const char *msg) { - DUK_UNREF(ctx); -#if defined(DUK_USE_FILE_IO) - DUK_FPRINTF(DUK_STDERR, "FATAL %ld: %s\n", (long) code, (const char *) (msg ? msg : "null")); - DUK_FFLUSH(DUK_STDERR); -#else - /* omit print */ -#endif - DUK_D(DUK_DPRINT("default fatal handler called, code %ld -> calling DUK_PANIC()", (long) code)); - DUK_PANIC(code, msg); - DUK_UNREACHABLE(); -} - -/* - * Default panic handler - */ - -#if !defined(DUK_USE_PANIC_HANDLER) -DUK_INTERNAL void duk_default_panic_handler(duk_errcode_t code, const char *msg) { -#if defined(DUK_USE_FILE_IO) - DUK_FPRINTF(DUK_STDERR, "PANIC %ld: %s (" -#if defined(DUK_USE_PANIC_ABORT) - "calling abort" -#elif defined(DUK_USE_PANIC_EXIT) - "calling exit" -#elif defined(DUK_USE_PANIC_SEGFAULT) - "segfaulting on purpose" -#else -#error no DUK_USE_PANIC_xxx macro defined -#endif - ")\n", (long) code, (const char *) (msg ? msg : "null")); - DUK_FFLUSH(DUK_STDERR); -#else - /* omit print */ - DUK_UNREF(code); - DUK_UNREF(msg); -#endif - -#if defined(DUK_USE_PANIC_ABORT) - DUK_ABORT(); -#elif defined(DUK_USE_PANIC_EXIT) - DUK_EXIT(-1); -#elif defined(DUK_USE_PANIC_SEGFAULT) - /* exit() afterwards to satisfy "noreturn" */ - DUK_CAUSE_SEGFAULT(); /* SCANBUILD: "Dereference of null pointer", normal */ - DUK_EXIT(-1); -#else -#error no DUK_USE_PANIC_xxx macro defined -#endif - - DUK_UNREACHABLE(); -} -#endif /* !DUK_USE_PANIC_HANDLER */ - -#undef DUK__ERRFMT_BUFSIZE -#line 1 "duk_unicode_support.c" -/* - * Various Unicode help functions for character classification predicates, - * case conversion, decoding, etc. - */ - -/* include removed: duk_internal.h */ - -/* - * Fast path tables - */ - -#if defined(DUK_USE_IDCHAR_FASTPATH) -DUK_INTERNAL const duk_int8_t duk_is_idchar_tab[128] = { - /* 0: not IdentifierStart or IdentifierPart - * 1: IdentifierStart and IdentifierPart - * -1: IdentifierPart only - */ - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00...0x0f */ - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10...0x1f */ - 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20...0x2f */ - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, /* 0x30...0x3f */ - 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x40...0x4f */ - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 0x50...0x5f */ - 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x60...0x6f */ - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 /* 0x70...0x7f */ -}; -#endif - -/* - * XUTF-8 and CESU-8 encoding/decoding - */ - -DUK_INTERNAL duk_small_int_t duk_unicode_get_xutf8_length(duk_ucodepoint_t cp) { - duk_uint_fast32_t x = (duk_uint_fast32_t) cp; - if (x < 0x80UL) { - /* 7 bits */ - return 1; - } else if (x < 0x800UL) { - /* 11 bits */ - return 2; - } else if (x < 0x10000UL) { - /* 16 bits */ - return 3; - } else if (x < 0x200000UL) { - /* 21 bits */ - return 4; - } else if (x < 0x4000000UL) { - /* 26 bits */ - return 5; - } else if (x < (duk_ucodepoint_t) 0x80000000UL) { - /* 31 bits */ - return 6; - } else { - /* 36 bits */ - return 7; - } -} - -#if defined(DUK_USE_ASSERTIONS) -DUK_INTERNAL duk_small_int_t duk_unicode_get_cesu8_length(duk_ucodepoint_t cp) { - duk_uint_fast32_t x = (duk_uint_fast32_t) cp; - if (x < 0x80UL) { - /* 7 bits */ - return 1; - } else if (x < 0x800UL) { - /* 11 bits */ - return 2; - } else if (x < 0x10000UL) { - /* 16 bits */ - return 3; - } else { - /* Encoded as surrogate pair, each encoding to 3 bytes for - * 6 bytes total. Codepoints above U+10FFFF encode as 6 bytes - * too, see duk_unicode_encode_cesu8(). - */ - return 3 + 3; - } -} -#endif /* DUK_USE_ASSERTIONS */ - -DUK_INTERNAL const duk_uint8_t duk_unicode_xutf8_markers[7] = { - 0x00, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe -}; - -/* Encode to extended UTF-8; 'out' must have space for at least - * DUK_UNICODE_MAX_XUTF8_LENGTH bytes. Allows encoding of any - * 32-bit (unsigned) codepoint. - */ -DUK_INTERNAL duk_small_int_t duk_unicode_encode_xutf8(duk_ucodepoint_t cp, duk_uint8_t *out) { - duk_uint_fast32_t x = (duk_uint_fast32_t) cp; - duk_small_int_t len; - duk_uint8_t marker; - duk_small_int_t i; - - len = duk_unicode_get_xutf8_length(cp); - DUK_ASSERT(len > 0); - - marker = duk_unicode_xutf8_markers[len - 1]; /* 64-bit OK because always >= 0 */ - - i = len; - DUK_ASSERT(i > 0); - do { - i--; - if (i > 0) { - out[i] = (duk_uint8_t) (0x80 + (x & 0x3f)); - x >>= 6; - } else { - /* Note: masking of 'x' is not necessary because of - * range check and shifting -> no bits overlapping - * the marker should be set. - */ - out[0] = (duk_uint8_t) (marker + x); - } - } while (i > 0); - - return len; -} - -/* Encode to CESU-8; 'out' must have space for at least - * DUK_UNICODE_MAX_CESU8_LENGTH bytes; codepoints above U+10FFFF - * will encode to garbage but won't overwrite the output buffer. - */ -DUK_INTERNAL duk_small_int_t duk_unicode_encode_cesu8(duk_ucodepoint_t cp, duk_uint8_t *out) { - duk_uint_fast32_t x = (duk_uint_fast32_t) cp; - duk_small_int_t len; - - if (x < 0x80UL) { - out[0] = (duk_uint8_t) x; - len = 1; - } else if (x < 0x800UL) { - out[0] = (duk_uint8_t) (0xc0 + ((x >> 6) & 0x1f)); - out[1] = (duk_uint8_t) (0x80 + (x & 0x3f)); - len = 2; - } else if (x < 0x10000UL) { - /* surrogate pairs get encoded here */ - out[0] = (duk_uint8_t) (0xe0 + ((x >> 12) & 0x0f)); - out[1] = (duk_uint8_t) (0x80 + ((x >> 6) & 0x3f)); - out[2] = (duk_uint8_t) (0x80 + (x & 0x3f)); - len = 3; - } else { - /* - * Unicode codepoints above U+FFFF are encoded as surrogate - * pairs here. This ensures that all CESU-8 codepoints are - * 16-bit values as expected in Ecmascript. The surrogate - * pairs always get a 3-byte encoding (each) in CESU-8. - * See: http://en.wikipedia.org/wiki/Surrogate_pair - * - * 20-bit codepoint, 10 bits (A and B) per surrogate pair: - * - * x = 0b00000000 0000AAAA AAAAAABB BBBBBBBB - * sp1 = 0b110110AA AAAAAAAA (0xd800 + ((x >> 10) & 0x3ff)) - * sp2 = 0b110111BB BBBBBBBB (0xdc00 + (x & 0x3ff)) - * - * Encoded into CESU-8: - * - * sp1 -> 0b11101101 (0xe0 + ((sp1 >> 12) & 0x0f)) - * -> 0b1010AAAA (0x80 + ((sp1 >> 6) & 0x3f)) - * -> 0b10AAAAAA (0x80 + (sp1 & 0x3f)) - * sp2 -> 0b11101101 (0xe0 + ((sp2 >> 12) & 0x0f)) - * -> 0b1011BBBB (0x80 + ((sp2 >> 6) & 0x3f)) - * -> 0b10BBBBBB (0x80 + (sp2 & 0x3f)) - * - * Note that 0x10000 must be subtracted first. The code below - * avoids the sp1, sp2 temporaries which saves around 20 bytes - * of code. - */ - - x -= 0x10000UL; - - out[0] = (duk_uint8_t) (0xed); - out[1] = (duk_uint8_t) (0xa0 + ((x >> 16) & 0x0f)); - out[2] = (duk_uint8_t) (0x80 + ((x >> 10) & 0x3f)); - out[3] = (duk_uint8_t) (0xed); - out[4] = (duk_uint8_t) (0xb0 + ((x >> 6) & 0x0f)); - out[5] = (duk_uint8_t) (0x80 + (x & 0x3f)); - len = 6; - } - - return len; -} - -/* Decode helper. Return zero on error. */ -DUK_INTERNAL 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) { - const duk_uint8_t *p; - duk_uint32_t res; - duk_uint_fast8_t ch; - duk_small_int_t n; - - DUK_UNREF(thr); - - p = *ptr; - if (p < ptr_start || p >= ptr_end) { - goto fail; - } - - /* - * UTF-8 decoder which accepts longer than standard byte sequences. - * This allows full 32-bit code points to be used. - */ - - ch = (duk_uint_fast8_t) (*p++); - if (ch < 0x80) { - /* 0xxx xxxx [7 bits] */ - res = (duk_uint32_t) (ch & 0x7f); - n = 0; - } else if (ch < 0xc0) { - /* 10xx xxxx -> invalid */ - goto fail; - } else if (ch < 0xe0) { - /* 110x xxxx 10xx xxxx [11 bits] */ - res = (duk_uint32_t) (ch & 0x1f); - n = 1; - } else if (ch < 0xf0) { - /* 1110 xxxx 10xx xxxx 10xx xxxx [16 bits] */ - res = (duk_uint32_t) (ch & 0x0f); - n = 2; - } else if (ch < 0xf8) { - /* 1111 0xxx 10xx xxxx 10xx xxxx 10xx xxxx [21 bits] */ - res = (duk_uint32_t) (ch & 0x07); - n = 3; - } else if (ch < 0xfc) { - /* 1111 10xx 10xx xxxx 10xx xxxx 10xx xxxx 10xx xxxx [26 bits] */ - res = (duk_uint32_t) (ch & 0x03); - n = 4; - } else if (ch < 0xfe) { - /* 1111 110x 10xx xxxx 10xx xxxx 10xx xxxx 10xx xxxx 10xx xxxx [31 bits] */ - res = (duk_uint32_t) (ch & 0x01); - n = 5; - } else if (ch < 0xff) { - /* 1111 1110 10xx xxxx 10xx xxxx 10xx xxxx 10xx xxxx 10xx xxxx 10xx xxxx [36 bits] */ - res = (duk_uint32_t) (0); - n = 6; - } else { - /* 8-byte format could be: - * 1111 1111 10xx xxxx 10xx xxxx 10xx xxxx 10xx xxxx 10xx xxxx 10xx xxxx 10xx xxxx [41 bits] - * - * However, this format would not have a zero bit following the - * leading one bits and would not allow 0xFF to be used as an - * "invalid xutf-8" marker for internal keys. Further, 8-byte - * encodings (up to 41 bit code points) are not currently needed. - */ - goto fail; - } - - DUK_ASSERT(p >= ptr_start); /* verified at beginning */ - if (p + n > ptr_end) { - /* check pointer at end */ - goto fail; - } - - while (n > 0) { - DUK_ASSERT(p >= ptr_start && p < ptr_end); - res = res << 6; - res += (duk_uint32_t) ((*p++) & 0x3f); - n--; - } - - *ptr = p; - *out_cp = res; - return 1; - - fail: - return 0; -} - -/* used by e.g. duk_regexp_executor.c, string built-ins */ -DUK_INTERNAL duk_ucodepoint_t duk_unicode_decode_xutf8_checked(duk_hthread *thr, const duk_uint8_t **ptr, const duk_uint8_t *ptr_start, const duk_uint8_t *ptr_end) { - duk_ucodepoint_t cp; - - if (duk_unicode_decode_xutf8(thr, ptr, ptr_start, ptr_end, &cp)) { - return cp; - } - DUK_ERROR_INTERNAL(thr, "utf-8 decode failed"); /* XXX: 'internal error' is a bit of a misnomer */ - DUK_UNREACHABLE(); - return 0; -} - -/* Compute (extended) utf-8 length without codepoint encoding validation, - * used for string interning. - * - * NOTE: This algorithm is performance critical, more so than string hashing - * in some cases. It is needed when interning a string and needs to scan - * every byte of the string with no skipping. Having an ASCII fast path - * is useful if possible in the algorithm. The current algorithms were - * chosen from several variants, based on x64 gcc -O2 testing. See: - * https://github.com/svaarala/duktape/pull/422 - * - * NOTE: must match src/dukutil.py:duk_unicode_unvalidated_utf8_length(). - */ - -#if defined(DUK_USE_PREFER_SIZE) -/* Small variant; roughly 150 bytes smaller than the fast variant. */ -DUK_INTERNAL duk_size_t duk_unicode_unvalidated_utf8_length(const duk_uint8_t *data, duk_size_t blen) { - const duk_uint8_t *p; - const duk_uint8_t *p_end; - duk_size_t ncont; - duk_size_t clen; - - p = data; - p_end = data + blen; - ncont = 0; - while (p != p_end) { - duk_uint8_t x; - x = *p++; - if (DUK_UNLIKELY(x >= 0x80 && x <= 0xbf)) { - ncont++; - } - } - - DUK_ASSERT(ncont <= blen); - clen = blen - ncont; - DUK_ASSERT(clen <= blen); - return clen; -} -#else /* DUK_USE_PREFER_SIZE */ -/* This seems like a good overall approach. Fast path for ASCII in 4 byte - * blocks. - */ -DUK_INTERNAL duk_size_t duk_unicode_unvalidated_utf8_length(const duk_uint8_t *data, duk_size_t blen) { - const duk_uint8_t *p; - const duk_uint8_t *p_end; - const duk_uint32_t *p32_end; - const duk_uint32_t *p32; - duk_size_t ncont; - duk_size_t clen; - - ncont = 0; /* number of continuation (non-initial) bytes in [0x80,0xbf] */ - p = data; - p_end = data + blen; - if (blen < 16) { - goto skip_fastpath; - } - - /* Align 'p' to 4; the input data may have arbitrary alignment. - * End of string check not needed because blen >= 16. - */ - while (((duk_size_t) (const void *) p) & 0x03U) { - duk_uint8_t x; - x = *p++; - if (DUK_UNLIKELY(x >= 0x80 && x <= 0xbf)) { - ncont++; - } - } - - /* Full, aligned 4-byte reads. */ - p32_end = (const duk_uint32_t *) (const void *) (p + ((duk_size_t) (p_end - p) & (duk_size_t) (~0x03))); - p32 = (const duk_uint32_t *) (const void *) p; - while (p32 != (const duk_uint32_t *) p32_end) { - duk_uint32_t x; - x = *p32++; - if (DUK_LIKELY((x & 0x80808080UL) == 0)) { - ; /* ASCII fast path */ - } else { - /* Flip highest bit of each byte which changes - * the bit pattern 10xxxxxx into 00xxxxxx which - * allows an easy bit mask test. - */ - x ^= 0x80808080UL; - if (DUK_UNLIKELY(!(x & 0xc0000000UL))) { - ncont++; - } - if (DUK_UNLIKELY(!(x & 0x00c00000UL))) { - ncont++; - } - if (DUK_UNLIKELY(!(x & 0x0000c000UL))) { - ncont++; - } - if (DUK_UNLIKELY(!(x & 0x000000c0UL))) { - ncont++; - } - } - } - p = (const duk_uint8_t *) p32; - /* Fall through to handle the rest. */ - - skip_fastpath: - while (p != p_end) { - duk_uint8_t x; - x = *p++; - if (DUK_UNLIKELY(x >= 0x80 && x <= 0xbf)) { - ncont++; - } - } - - DUK_ASSERT(ncont <= blen); - clen = blen - ncont; - DUK_ASSERT(clen <= blen); - return clen; -} -#endif /* DUK_USE_PREFER_SIZE */ - -/* - * Unicode range matcher - * - * Matches a codepoint against a packed bitstream of character ranges. - * Used for slow path Unicode matching. - */ - -/* Must match src/extract_chars.py, generate_match_table3(). */ -DUK_LOCAL duk_uint32_t duk__uni_decode_value(duk_bitdecoder_ctx *bd_ctx) { - duk_uint32_t t; - - t = (duk_uint32_t) duk_bd_decode(bd_ctx, 4); - if (t <= 0x0eU) { - return t; - } - t = (duk_uint32_t) duk_bd_decode(bd_ctx, 8); - if (t <= 0xfdU) { - return t + 0x0f; - } - if (t == 0xfeU) { - t = (duk_uint32_t) duk_bd_decode(bd_ctx, 12); - return t + 0x0fU + 0xfeU; - } else { - t = (duk_uint32_t) duk_bd_decode(bd_ctx, 24); - return t + 0x0fU + 0xfeU + 0x1000UL; - } -} - -DUK_LOCAL duk_small_int_t duk__uni_range_match(const duk_uint8_t *unitab, duk_size_t unilen, duk_codepoint_t cp) { - duk_bitdecoder_ctx bd_ctx; - duk_codepoint_t prev_re; - - DUK_MEMZERO(&bd_ctx, sizeof(bd_ctx)); - bd_ctx.data = (const duk_uint8_t *) unitab; - bd_ctx.length = (duk_size_t) unilen; - - prev_re = 0; - for (;;) { - duk_codepoint_t r1, r2; - r1 = (duk_codepoint_t) duk__uni_decode_value(&bd_ctx); - if (r1 == 0) { - break; - } - r2 = (duk_codepoint_t) duk__uni_decode_value(&bd_ctx); - - r1 = prev_re + r1; - r2 = r1 + r2; - prev_re = r2; - - /* [r1,r2] is the range */ - - DUK_DDD(DUK_DDDPRINT("duk__uni_range_match: cp=%06lx range=[0x%06lx,0x%06lx]", - (unsigned long) cp, (unsigned long) r1, (unsigned long) r2)); - if (cp >= r1 && cp <= r2) { - return 1; - } - } - - return 0; -} - -/* - * "WhiteSpace" production check. - */ - -DUK_INTERNAL duk_small_int_t duk_unicode_is_whitespace(duk_codepoint_t cp) { - /* - * E5 Section 7.2 specifies six characters specifically as - * white space: - * - * 0009;;Cc;0;S;;;;;N;CHARACTER TABULATION;;;; - * 000B;;Cc;0;S;;;;;N;LINE TABULATION;;;; - * 000C;;Cc;0;WS;;;;;N;FORM FEED (FF);;;; - * 0020;SPACE;Zs;0;WS;;;;;N;;;;; - * 00A0;NO-BREAK SPACE;Zs;0;CS; 0020;;;;N;NON-BREAKING SPACE;;;; - * FEFF;ZERO WIDTH NO-BREAK SPACE;Cf;0;BN;;;;;N;BYTE ORDER MARK;;;; - * - * It also specifies any Unicode category 'Zs' characters as white - * space. These can be extracted with the "src/extract_chars.py" script. - * Current result: - * - * RAW OUTPUT: - * =========== - * 0020;SPACE;Zs;0;WS;;;;;N;;;;; - * 00A0;NO-BREAK SPACE;Zs;0;CS; 0020;;;;N;NON-BREAKING SPACE;;;; - * 1680;OGHAM SPACE MARK;Zs;0;WS;;;;;N;;;;; - * 180E;MONGOLIAN VOWEL SEPARATOR;Zs;0;WS;;;;;N;;;;; - * 2000;EN QUAD;Zs;0;WS;2002;;;;N;;;;; - * 2001;EM QUAD;Zs;0;WS;2003;;;;N;;;;; - * 2002;EN SPACE;Zs;0;WS; 0020;;;;N;;;;; - * 2003;EM SPACE;Zs;0;WS; 0020;;;;N;;;;; - * 2004;THREE-PER-EM SPACE;Zs;0;WS; 0020;;;;N;;;;; - * 2005;FOUR-PER-EM SPACE;Zs;0;WS; 0020;;;;N;;;;; - * 2006;SIX-PER-EM SPACE;Zs;0;WS; 0020;;;;N;;;;; - * 2007;FIGURE SPACE;Zs;0;WS; 0020;;;;N;;;;; - * 2008;PUNCTUATION SPACE;Zs;0;WS; 0020;;;;N;;;;; - * 2009;THIN SPACE;Zs;0;WS; 0020;;;;N;;;;; - * 200A;HAIR SPACE;Zs;0;WS; 0020;;;;N;;;;; - * 202F;NARROW NO-BREAK SPACE;Zs;0;CS; 0020;;;;N;;;;; - * 205F;MEDIUM MATHEMATICAL SPACE;Zs;0;WS; 0020;;;;N;;;;; - * 3000;IDEOGRAPHIC SPACE;Zs;0;WS; 0020;;;;N;;;;; - * - * RANGES: - * ======= - * 0x0020 - * 0x00a0 - * 0x1680 - * 0x180e - * 0x2000 ... 0x200a - * 0x202f - * 0x205f - * 0x3000 - * - * A manual decoder (below) is probably most compact for this. - */ - - duk_uint_fast8_t lo; - duk_uint_fast32_t hi; - - /* cp == -1 (EOF) never matches and causes return value 0 */ - - lo = (duk_uint_fast8_t) (cp & 0xff); - hi = (duk_uint_fast32_t) (cp >> 8); /* does not fit into an uchar */ - - if (hi == 0x0000UL) { - if (lo == 0x09U || lo == 0x0bU || lo == 0x0cU || - lo == 0x20U || lo == 0xa0U) { - return 1; - } - } else if (hi == 0x0020UL) { - if (lo <= 0x0aU || lo == 0x2fU || lo == 0x5fU) { - return 1; - } - } else if (cp == 0x1680L || cp == 0x180eL || cp == 0x3000L || - cp == 0xfeffL) { - return 1; - } - - return 0; -} - -/* - * "LineTerminator" production check. - */ - -DUK_INTERNAL duk_small_int_t duk_unicode_is_line_terminator(duk_codepoint_t cp) { - /* - * E5 Section 7.3 - * - * A LineTerminatorSequence essentially merges sequences - * into a single line terminator. This must be handled by the caller. - */ - - if (cp == 0x000aL || cp == 0x000dL || cp == 0x2028L || - cp == 0x2029L) { - return 1; - } - - return 0; -} - -/* - * "IdentifierStart" production check. - */ - -DUK_INTERNAL duk_small_int_t duk_unicode_is_identifier_start(duk_codepoint_t cp) { - /* - * E5 Section 7.6: - * - * IdentifierStart: - * UnicodeLetter - * $ - * _ - * \ UnicodeEscapeSequence - * - * IdentifierStart production has one multi-character production: - * - * \ UnicodeEscapeSequence - * - * The '\' character is -not- matched by this function. Rather, the caller - * should decode the escape and then call this function to check whether the - * decoded character is acceptable (see discussion in E5 Section 7.6). - * - * The "UnicodeLetter" alternative of the production allows letters - * from various Unicode categories. These can be extracted with the - * "src/extract_chars.py" script. - * - * Because the result has hundreds of Unicode codepoint ranges, matching - * for any values >= 0x80 are done using a very slow range-by-range scan - * and a packed range format. - * - * The ASCII portion (codepoints 0x00 ... 0x7f) is fast-pathed below because - * it matters the most. The ASCII related ranges of IdentifierStart are: - * - * 0x0041 ... 0x005a ['A' ... 'Z'] - * 0x0061 ... 0x007a ['a' ... 'z'] - * 0x0024 ['$'] - * 0x005f ['_'] - */ - - /* ASCII (and EOF) fast path -- quick accept and reject */ - if (cp <= 0x7fL) { -#if defined(DUK_USE_IDCHAR_FASTPATH) - return (cp >= 0) && (duk_is_idchar_tab[cp] > 0); -#else - if ((cp >= 'a' && cp <= 'z') || - (cp >= 'A' && cp <= 'Z') || - cp == '_' || cp == '$') { - return 1; - } - return 0; -#endif - } - - /* Non-ASCII slow path (range-by-range linear comparison), very slow */ - -#ifdef DUK_USE_SOURCE_NONBMP - if (duk__uni_range_match(duk_unicode_ids_noa, - (duk_size_t) sizeof(duk_unicode_ids_noa), - (duk_codepoint_t) cp)) { - return 1; - } - return 0; -#else - if (cp < 0x10000L) { - if (duk__uni_range_match(duk_unicode_ids_noabmp, - sizeof(duk_unicode_ids_noabmp), - (duk_codepoint_t) cp)) { - return 1; - } - return 0; - } else { - /* without explicit non-BMP support, assume non-BMP characters - * are always accepted as identifier characters. - */ - return 1; - } -#endif -} - -/* - * "IdentifierPart" production check. - */ - -DUK_INTERNAL duk_small_int_t duk_unicode_is_identifier_part(duk_codepoint_t cp) { - /* - * E5 Section 7.6: - * - * IdentifierPart: - * IdentifierStart - * UnicodeCombiningMark - * UnicodeDigit - * UnicodeConnectorPunctuation - * [U+200C] - * [U+200D] - * - * IdentifierPart production has one multi-character production - * as part of its IdentifierStart alternative. The '\' character - * of an escape sequence is not matched here, see discussion in - * duk_unicode_is_identifier_start(). - * - * To match non-ASCII characters (codepoints >= 0x80), a very slow - * linear range-by-range scan is used. The codepoint is first compared - * to the IdentifierStart ranges, and if it doesn't match, then to a - * set consisting of code points in IdentifierPart but not in - * IdentifierStart. This is done to keep the unicode range data small, - * at the expense of speed. - * - * The ASCII fast path consists of: - * - * 0x0030 ... 0x0039 ['0' ... '9', UnicodeDigit] - * 0x0041 ... 0x005a ['A' ... 'Z', IdentifierStart] - * 0x0061 ... 0x007a ['a' ... 'z', IdentifierStart] - * 0x0024 ['$', IdentifierStart] - * 0x005f ['_', IdentifierStart and - * UnicodeConnectorPunctuation] - * - * UnicodeCombiningMark has no code points <= 0x7f. - * - * The matching code reuses the "identifier start" tables, and then - * consults a separate range set for characters in "identifier part" - * but not in "identifier start". These can be extracted with the - * "src/extract_chars.py" script. - * - * UnicodeCombiningMark -> categories Mn, Mc - * UnicodeDigit -> categories Nd - * UnicodeConnectorPunctuation -> categories Pc - */ - - /* ASCII (and EOF) fast path -- quick accept and reject */ - if (cp <= 0x7fL) { -#if defined(DUK_USE_IDCHAR_FASTPATH) - return (cp >= 0) && (duk_is_idchar_tab[cp] != 0); -#else - if ((cp >= 'a' && cp <= 'z') || - (cp >= 'A' && cp <= 'Z') || - (cp >= '0' && cp <= '9') || - cp == '_' || cp == '$') { - return 1; - } - return 0; -#endif - } - - /* Non-ASCII slow path (range-by-range linear comparison), very slow */ - -#ifdef DUK_USE_SOURCE_NONBMP - if (duk__uni_range_match(duk_unicode_ids_noa, - sizeof(duk_unicode_ids_noa), - (duk_codepoint_t) cp) || - duk__uni_range_match(duk_unicode_idp_m_ids_noa, - sizeof(duk_unicode_idp_m_ids_noa), - (duk_codepoint_t) cp)) { - return 1; - } - return 0; -#else - if (cp < 0x10000L) { - if (duk__uni_range_match(duk_unicode_ids_noabmp, - sizeof(duk_unicode_ids_noabmp), - (duk_codepoint_t) cp) || - duk__uni_range_match(duk_unicode_idp_m_ids_noabmp, - sizeof(duk_unicode_idp_m_ids_noabmp), - (duk_codepoint_t) cp)) { - return 1; - } - return 0; - } else { - /* without explicit non-BMP support, assume non-BMP characters - * are always accepted as identifier characters. - */ - return 1; - } -#endif -} - -/* - * Unicode letter check. - */ - -DUK_INTERNAL duk_small_int_t duk_unicode_is_letter(duk_codepoint_t cp) { - /* - * Unicode letter is now taken to be the categories: - * - * Lu, Ll, Lt, Lm, Lo - * - * (Not sure if this is exactly correct.) - * - * The ASCII fast path consists of: - * - * 0x0041 ... 0x005a ['A' ... 'Z'] - * 0x0061 ... 0x007a ['a' ... 'z'] - */ - - /* ASCII (and EOF) fast path -- quick accept and reject */ - if (cp <= 0x7fL) { - if ((cp >= 'a' && cp <= 'z') || - (cp >= 'A' && cp <= 'Z')) { - return 1; - } - return 0; - } - - /* Non-ASCII slow path (range-by-range linear comparison), very slow */ - -#ifdef DUK_USE_SOURCE_NONBMP - if (duk__uni_range_match(duk_unicode_ids_noa, - sizeof(duk_unicode_ids_noa), - (duk_codepoint_t) cp) && - !duk__uni_range_match(duk_unicode_ids_m_let_noa, - sizeof(duk_unicode_ids_m_let_noa), - (duk_codepoint_t) cp)) { - return 1; - } - return 0; -#else - if (cp < 0x10000L) { - if (duk__uni_range_match(duk_unicode_ids_noabmp, - sizeof(duk_unicode_ids_noabmp), - (duk_codepoint_t) cp) && - !duk__uni_range_match(duk_unicode_ids_m_let_noabmp, - sizeof(duk_unicode_ids_m_let_noabmp), - (duk_codepoint_t) cp)) { - return 1; - } - return 0; - } else { - /* without explicit non-BMP support, assume non-BMP characters - * are always accepted as letters. - */ - return 1; - } -#endif -} - -/* - * Complex case conversion helper which decodes a bit-packed conversion - * control stream generated by unicode/extract_caseconv.py. The conversion - * is very slow because it runs through the conversion data in a linear - * fashion to save space (which is why ASCII characters have a special - * fast path before arriving here). - * - * The particular bit counts etc have been determined experimentally to - * be small but still sufficient, and must match the Python script - * (src/extract_caseconv.py). - * - * The return value is the case converted codepoint or -1 if the conversion - * results in multiple characters (this is useful for regexp Canonicalization - * operation). If 'buf' is not NULL, the result codepoint(s) are also - * appended to the hbuffer. - * - * Context and locale specific rules must be checked before consulting - * this function. - */ - -DUK_LOCAL -duk_codepoint_t duk__slow_case_conversion(duk_hthread *thr, - duk_bufwriter_ctx *bw, - duk_codepoint_t cp, - duk_bitdecoder_ctx *bd_ctx) { - duk_small_int_t skip = 0; - duk_small_int_t n; - duk_small_int_t t; - duk_small_int_t count; - duk_codepoint_t tmp_cp; - duk_codepoint_t start_i; - duk_codepoint_t start_o; - - DUK_UNREF(thr); - DUK_ASSERT(bd_ctx != NULL); - - DUK_DDD(DUK_DDDPRINT("slow case conversion for codepoint: %ld", (long) cp)); - - /* range conversion with a "skip" */ - DUK_DDD(DUK_DDDPRINT("checking ranges")); - for (;;) { - skip++; - n = (duk_small_int_t) duk_bd_decode(bd_ctx, 6); - if (n == 0x3f) { - /* end marker */ - break; - } - DUK_DDD(DUK_DDDPRINT("skip=%ld, n=%ld", (long) skip, (long) n)); - - while (n--) { - start_i = (duk_codepoint_t) duk_bd_decode(bd_ctx, 16); - start_o = (duk_codepoint_t) duk_bd_decode(bd_ctx, 16); - count = (duk_small_int_t) duk_bd_decode(bd_ctx, 7); - DUK_DDD(DUK_DDDPRINT("range: start_i=%ld, start_o=%ld, count=%ld, skip=%ld", - (long) start_i, (long) start_o, (long) count, (long) skip)); - - if (cp >= start_i) { - tmp_cp = cp - start_i; /* always >= 0 */ - if (tmp_cp < (duk_codepoint_t) count * (duk_codepoint_t) skip && - (tmp_cp % (duk_codepoint_t) skip) == 0) { - DUK_DDD(DUK_DDDPRINT("range matches input codepoint")); - cp = start_o + tmp_cp; - goto single; - } - } - } - } - - /* 1:1 conversion */ - n = (duk_small_int_t) duk_bd_decode(bd_ctx, 6); - DUK_DDD(DUK_DDDPRINT("checking 1:1 conversions (count %ld)", (long) n)); - while (n--) { - start_i = (duk_codepoint_t) duk_bd_decode(bd_ctx, 16); - start_o = (duk_codepoint_t) duk_bd_decode(bd_ctx, 16); - DUK_DDD(DUK_DDDPRINT("1:1 conversion %ld -> %ld", (long) start_i, (long) start_o)); - if (cp == start_i) { - DUK_DDD(DUK_DDDPRINT("1:1 matches input codepoint")); - cp = start_o; - goto single; - } - } - - /* complex, multicharacter conversion */ - n = (duk_small_int_t) duk_bd_decode(bd_ctx, 7); - DUK_DDD(DUK_DDDPRINT("checking 1:n conversions (count %ld)", (long) n)); - while (n--) { - start_i = (duk_codepoint_t) duk_bd_decode(bd_ctx, 16); - t = (duk_small_int_t) duk_bd_decode(bd_ctx, 2); - DUK_DDD(DUK_DDDPRINT("1:n conversion %ld -> %ld chars", (long) start_i, (long) t)); - if (cp == start_i) { - DUK_DDD(DUK_DDDPRINT("1:n matches input codepoint")); - if (bw != NULL) { - while (t--) { - tmp_cp = (duk_codepoint_t) duk_bd_decode(bd_ctx, 16); - DUK_BW_WRITE_RAW_XUTF8(thr, bw, (duk_ucodepoint_t) tmp_cp); - } - } - return -1; - } else { - while (t--) { - (void) duk_bd_decode(bd_ctx, 16); - } - } - } - - /* default: no change */ - DUK_DDD(DUK_DDDPRINT("no rule matches, output is same as input")); - /* fall through */ - - single: - if (bw != NULL) { - DUK_BW_WRITE_RAW_XUTF8(thr, bw, (duk_ucodepoint_t) cp); - } - return cp; -} - -/* - * Case conversion helper, with context/local sensitivity. - * For proper case conversion, one needs to know the character - * and the preceding and following characters, as well as - * locale/language. - */ - -/* XXX: add 'language' argument when locale/language sensitive rule - * support added. - */ -DUK_LOCAL -duk_codepoint_t duk__case_transform_helper(duk_hthread *thr, - duk_bufwriter_ctx *bw, - duk_codepoint_t cp, - duk_codepoint_t prev, - duk_codepoint_t next, - duk_bool_t uppercase) { - duk_bitdecoder_ctx bd_ctx; - - /* fast path for ASCII */ - if (cp < 0x80L) { - /* XXX: there are language sensitive rules for the ASCII range. - * If/when language/locale support is implemented, they need to - * be implemented here for the fast path. There are no context - * sensitive rules for ASCII range. - */ - - if (uppercase) { - if (cp >= 'a' && cp <= 'z') { - cp = cp - 'a' + 'A'; - } - } else { - if (cp >= 'A' && cp <= 'Z') { - cp = cp - 'A' + 'a'; - } - } - - if (bw != NULL) { - DUK_BW_WRITE_RAW_U8(thr, bw, (duk_uint8_t) cp); - } - return cp; - } - - /* context and locale specific rules which cannot currently be represented - * in the caseconv bitstream: hardcoded rules in C - */ - if (uppercase) { - /* XXX: turkish / azeri */ - } else { - /* - * Final sigma context specific rule. This is a rather tricky - * rule and this handling is probably not 100% correct now. - * The rule is not locale/language specific so it is supported. - */ - - if (cp == 0x03a3L && /* U+03A3 = GREEK CAPITAL LETTER SIGMA */ - duk_unicode_is_letter(prev) && /* prev exists and is not a letter */ - !duk_unicode_is_letter(next)) { /* next does not exist or next is not a letter */ - /* Capital sigma occurred at "end of word", lowercase to - * U+03C2 = GREEK SMALL LETTER FINAL SIGMA. Otherwise - * fall through and let the normal rules lowercase it to - * U+03C3 = GREEK SMALL LETTER SIGMA. - */ - cp = 0x03c2L; - goto singlechar; - } - - /* XXX: lithuanian not implemented */ - /* XXX: lithuanian, explicit dot rules */ - /* XXX: turkish / azeri, lowercase rules */ - } - - /* 1:1 or special conversions, but not locale/context specific: script generated rules */ - DUK_MEMZERO(&bd_ctx, sizeof(bd_ctx)); - if (uppercase) { - bd_ctx.data = (const duk_uint8_t *) duk_unicode_caseconv_uc; - bd_ctx.length = (duk_size_t) sizeof(duk_unicode_caseconv_uc); - } else { - bd_ctx.data = (const duk_uint8_t *) duk_unicode_caseconv_lc; - bd_ctx.length = (duk_size_t) sizeof(duk_unicode_caseconv_lc); - } - return duk__slow_case_conversion(thr, bw, cp, &bd_ctx); - - singlechar: - if (bw != NULL) { - DUK_BW_WRITE_RAW_XUTF8(thr, bw, (duk_ucodepoint_t) cp); - } - return cp; - - /* unused now, not needed until Turkish/Azeri */ -#if 0 - nochar: - return -1; -#endif -} - -/* - * Replace valstack top with case converted version. - */ - -DUK_INTERNAL void duk_unicode_case_convert_string(duk_hthread *thr, duk_small_int_t uppercase) { - duk_context *ctx = (duk_context *) thr; - duk_hstring *h_input; - duk_bufwriter_ctx bw_alloc; - duk_bufwriter_ctx *bw; - const duk_uint8_t *p, *p_start, *p_end; - duk_codepoint_t prev, curr, next; - - h_input = duk_require_hstring(ctx, -1); - DUK_ASSERT(h_input != NULL); - - bw = &bw_alloc; - DUK_BW_INIT_PUSHBUF(thr, bw, DUK_HSTRING_GET_BYTELEN(h_input)); - - /* [ ... input buffer ] */ - - p_start = (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h_input); - p_end = p_start + DUK_HSTRING_GET_BYTELEN(h_input); - p = p_start; - - prev = -1; DUK_UNREF(prev); - curr = -1; - next = -1; - for (;;) { - prev = curr; - curr = next; - next = -1; - if (p < p_end) { - next = (int) duk_unicode_decode_xutf8_checked(thr, &p, p_start, p_end); - } else { - /* end of input and last char has been processed */ - if (curr < 0) { - break; - } - } - - /* on first round, skip */ - if (curr >= 0) { - /* XXX: could add a fast path to process chunks of input codepoints, - * but relative benefit would be quite small. - */ - - /* Ensure space for maximum multi-character result; estimate is overkill. */ - DUK_BW_ENSURE(thr, bw, 8 * DUK_UNICODE_MAX_XUTF8_LENGTH); - - duk__case_transform_helper(thr, - bw, - (duk_codepoint_t) curr, - prev, - next, - uppercase); - } - } - - DUK_BW_COMPACT(thr, bw); - duk_to_string(ctx, -1); /* invalidates h_buf pointer */ - duk_remove(ctx, -2); -} - -#ifdef DUK_USE_REGEXP_SUPPORT - -/* - * Canonicalize() abstract operation needed for canonicalization of individual - * codepoints during regexp compilation and execution, see E5 Section 15.10.2.8. - * Note that codepoints are canonicalized one character at a time, so no context - * specific rules can apply. Locale specific rules can apply, though. - */ - -DUK_INTERNAL duk_codepoint_t duk_unicode_re_canonicalize_char(duk_hthread *thr, duk_codepoint_t cp) { -#if defined(DUK_USE_REGEXP_CANON_WORKAROUND) - /* Fast canonicalization lookup at the cost of 128kB footprint. */ - DUK_ASSERT(cp >= 0); - DUK_UNREF(thr); - if (DUK_LIKELY(cp < 0x10000L)) { - return (duk_codepoint_t) duk_unicode_re_canon_lookup[cp]; - } - return cp; -#else /* DUK_USE_REGEXP_CANON_WORKAROUND */ - duk_codepoint_t y; - - y = duk__case_transform_helper(thr, - NULL, /* NULL is allowed, no output */ - cp, /* curr char */ - -1, /* prev char */ - -1, /* next char */ - 1); /* uppercase */ - - if ((y < 0) || (cp >= 0x80 && y < 0x80)) { - /* multiple codepoint conversion or non-ASCII mapped to ASCII - * --> leave as is. - */ - return cp; - } - - return y; -#endif /* DUK_USE_REGEXP_CANON_WORKAROUND */ -} - -/* - * E5 Section 15.10.2.6 "IsWordChar" abstract operation. Assume - * x < 0 for characters read outside the string. - */ - -DUK_INTERNAL duk_small_int_t duk_unicode_re_is_wordchar(duk_codepoint_t x) { - /* - * Note: the description in E5 Section 15.10.2.6 has a typo, it - * contains 'A' twice and lacks 'a'; the intent is [0-9a-zA-Z_]. - */ - if ((x >= '0' && x <= '9') || - (x >= 'a' && x <= 'z') || - (x >= 'A' && x <= 'Z') || - (x == '_')) { - return 1; - } - return 0; -} - -/* - * Regexp range tables - */ - -/* exposed because lexer needs these too */ -DUK_INTERNAL const duk_uint16_t duk_unicode_re_ranges_digit[2] = { - (duk_uint16_t) 0x0030UL, (duk_uint16_t) 0x0039UL, -}; -DUK_INTERNAL const duk_uint16_t duk_unicode_re_ranges_white[22] = { - (duk_uint16_t) 0x0009UL, (duk_uint16_t) 0x000DUL, - (duk_uint16_t) 0x0020UL, (duk_uint16_t) 0x0020UL, - (duk_uint16_t) 0x00A0UL, (duk_uint16_t) 0x00A0UL, - (duk_uint16_t) 0x1680UL, (duk_uint16_t) 0x1680UL, - (duk_uint16_t) 0x180EUL, (duk_uint16_t) 0x180EUL, - (duk_uint16_t) 0x2000UL, (duk_uint16_t) 0x200AUL, - (duk_uint16_t) 0x2028UL, (duk_uint16_t) 0x2029UL, - (duk_uint16_t) 0x202FUL, (duk_uint16_t) 0x202FUL, - (duk_uint16_t) 0x205FUL, (duk_uint16_t) 0x205FUL, - (duk_uint16_t) 0x3000UL, (duk_uint16_t) 0x3000UL, - (duk_uint16_t) 0xFEFFUL, (duk_uint16_t) 0xFEFFUL, -}; -DUK_INTERNAL const duk_uint16_t duk_unicode_re_ranges_wordchar[8] = { - (duk_uint16_t) 0x0030UL, (duk_uint16_t) 0x0039UL, - (duk_uint16_t) 0x0041UL, (duk_uint16_t) 0x005AUL, - (duk_uint16_t) 0x005FUL, (duk_uint16_t) 0x005FUL, - (duk_uint16_t) 0x0061UL, (duk_uint16_t) 0x007AUL, -}; -DUK_INTERNAL const duk_uint16_t duk_unicode_re_ranges_not_digit[4] = { - (duk_uint16_t) 0x0000UL, (duk_uint16_t) 0x002FUL, - (duk_uint16_t) 0x003AUL, (duk_uint16_t) 0xFFFFUL, -}; -DUK_INTERNAL const duk_uint16_t duk_unicode_re_ranges_not_white[24] = { - (duk_uint16_t) 0x0000UL, (duk_uint16_t) 0x0008UL, - (duk_uint16_t) 0x000EUL, (duk_uint16_t) 0x001FUL, - (duk_uint16_t) 0x0021UL, (duk_uint16_t) 0x009FUL, - (duk_uint16_t) 0x00A1UL, (duk_uint16_t) 0x167FUL, - (duk_uint16_t) 0x1681UL, (duk_uint16_t) 0x180DUL, - (duk_uint16_t) 0x180FUL, (duk_uint16_t) 0x1FFFUL, - (duk_uint16_t) 0x200BUL, (duk_uint16_t) 0x2027UL, - (duk_uint16_t) 0x202AUL, (duk_uint16_t) 0x202EUL, - (duk_uint16_t) 0x2030UL, (duk_uint16_t) 0x205EUL, - (duk_uint16_t) 0x2060UL, (duk_uint16_t) 0x2FFFUL, - (duk_uint16_t) 0x3001UL, (duk_uint16_t) 0xFEFEUL, - (duk_uint16_t) 0xFF00UL, (duk_uint16_t) 0xFFFFUL, -}; -DUK_INTERNAL const duk_uint16_t duk_unicode_re_ranges_not_wordchar[10] = { - (duk_uint16_t) 0x0000UL, (duk_uint16_t) 0x002FUL, - (duk_uint16_t) 0x003AUL, (duk_uint16_t) 0x0040UL, - (duk_uint16_t) 0x005BUL, (duk_uint16_t) 0x005EUL, - (duk_uint16_t) 0x0060UL, (duk_uint16_t) 0x0060UL, - (duk_uint16_t) 0x007BUL, (duk_uint16_t) 0xFFFFUL, -}; - -#endif /* DUK_USE_REGEXP_SUPPORT */ -#line 1 "duk_util_misc.c" -/* - * Misc util stuff - */ - -/* include removed: duk_internal.h */ - -/* - * Lowercase digits for radix values 2 to 36. Also doubles as lowercase - * hex nybble table. - */ - -DUK_INTERNAL const duk_uint8_t duk_lc_digits[36] = { - DUK_ASC_0, DUK_ASC_1, DUK_ASC_2, DUK_ASC_3, - DUK_ASC_4, DUK_ASC_5, DUK_ASC_6, DUK_ASC_7, - DUK_ASC_8, DUK_ASC_9, DUK_ASC_LC_A, DUK_ASC_LC_B, - DUK_ASC_LC_C, DUK_ASC_LC_D, DUK_ASC_LC_E, DUK_ASC_LC_F, - DUK_ASC_LC_G, DUK_ASC_LC_H, DUK_ASC_LC_I, DUK_ASC_LC_J, - DUK_ASC_LC_K, DUK_ASC_LC_L, DUK_ASC_LC_M, DUK_ASC_LC_N, - DUK_ASC_LC_O, DUK_ASC_LC_P, DUK_ASC_LC_Q, DUK_ASC_LC_R, - DUK_ASC_LC_S, DUK_ASC_LC_T, DUK_ASC_LC_U, DUK_ASC_LC_V, - DUK_ASC_LC_W, DUK_ASC_LC_X, DUK_ASC_LC_Y, DUK_ASC_LC_Z -}; - -DUK_INTERNAL const duk_uint8_t duk_uc_nybbles[16] = { - DUK_ASC_0, DUK_ASC_1, DUK_ASC_2, DUK_ASC_3, - DUK_ASC_4, DUK_ASC_5, DUK_ASC_6, DUK_ASC_7, - DUK_ASC_8, DUK_ASC_9, DUK_ASC_UC_A, DUK_ASC_UC_B, - DUK_ASC_UC_C, DUK_ASC_UC_D, DUK_ASC_UC_E, DUK_ASC_UC_F -}; - -/* - * Table for hex decoding ASCII hex digits - */ - -DUK_INTERNAL const duk_int8_t duk_hex_dectab[256] = { - /* -1 if invalid */ - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x00-0x0f */ - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x10-0x1f */ - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x20-0x2f */ - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /* 0x30-0x3f */ - -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x40-0x4f */ - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x50-0x5f */ - -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x60-0x6f */ - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x70-0x7f */ - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x80-0x8f */ - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x90-0x9f */ - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0xa0-0xaf */ - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0xb0-0xbf */ - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0xc0-0xcf */ - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0xd0-0xdf */ - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0xe0-0xef */ - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 /* 0xf0-0xff */ -}; - -#if defined(DUK_USE_HEX_FASTPATH) -/* Preshifted << 4. Must use 16-bit entry to allow negative value signaling. */ -DUK_INTERNAL const duk_int16_t duk_hex_dectab_shift4[256] = { - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x00-0x0f */ - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x10-0x1f */ - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x20-0x2f */ - 0x00, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80, 0x90, -1, -1, -1, -1, -1, -1, /* 0x30-0x3f */ - -1, 0xa0, 0xb0, 0xc0, 0xd0, 0xe0, 0xf0, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x40-0x4f */ - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x50-0x5f */ - -1, 0xa0, 0xb0, 0xc0, 0xd0, 0xe0, 0xf0, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x60-0x6f */ - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x70-0x7f */ - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x80-0x8f */ - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x90-0x9f */ - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0xa0-0xaf */ - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0xb0-0xbf */ - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0xc0-0xcf */ - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0xd0-0xdf */ - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0xe0-0xef */ - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 /* 0xf0-0xff */ -}; -#endif - -/* - * Table for hex encoding bytes - */ - -#if defined(DUK_USE_HEX_FASTPATH) -/* Lookup to encode one byte directly into 2 characters: - * - * def genhextab(bswap): - * for i in xrange(256): - * t = chr(i).encode('hex') - * if bswap: - * t = t[1] + t[0] - * print('0x' + t.encode('hex') + 'U') - * print('big endian'); genhextab(False) - * print('little endian'); genhextab(True) -*/ -DUK_INTERNAL const duk_uint16_t duk_hex_enctab[256] = { -#if defined(DUK_USE_INTEGER_BE) - 0x3030U, 0x3031U, 0x3032U, 0x3033U, 0x3034U, 0x3035U, 0x3036U, 0x3037U, - 0x3038U, 0x3039U, 0x3061U, 0x3062U, 0x3063U, 0x3064U, 0x3065U, 0x3066U, - 0x3130U, 0x3131U, 0x3132U, 0x3133U, 0x3134U, 0x3135U, 0x3136U, 0x3137U, - 0x3138U, 0x3139U, 0x3161U, 0x3162U, 0x3163U, 0x3164U, 0x3165U, 0x3166U, - 0x3230U, 0x3231U, 0x3232U, 0x3233U, 0x3234U, 0x3235U, 0x3236U, 0x3237U, - 0x3238U, 0x3239U, 0x3261U, 0x3262U, 0x3263U, 0x3264U, 0x3265U, 0x3266U, - 0x3330U, 0x3331U, 0x3332U, 0x3333U, 0x3334U, 0x3335U, 0x3336U, 0x3337U, - 0x3338U, 0x3339U, 0x3361U, 0x3362U, 0x3363U, 0x3364U, 0x3365U, 0x3366U, - 0x3430U, 0x3431U, 0x3432U, 0x3433U, 0x3434U, 0x3435U, 0x3436U, 0x3437U, - 0x3438U, 0x3439U, 0x3461U, 0x3462U, 0x3463U, 0x3464U, 0x3465U, 0x3466U, - 0x3530U, 0x3531U, 0x3532U, 0x3533U, 0x3534U, 0x3535U, 0x3536U, 0x3537U, - 0x3538U, 0x3539U, 0x3561U, 0x3562U, 0x3563U, 0x3564U, 0x3565U, 0x3566U, - 0x3630U, 0x3631U, 0x3632U, 0x3633U, 0x3634U, 0x3635U, 0x3636U, 0x3637U, - 0x3638U, 0x3639U, 0x3661U, 0x3662U, 0x3663U, 0x3664U, 0x3665U, 0x3666U, - 0x3730U, 0x3731U, 0x3732U, 0x3733U, 0x3734U, 0x3735U, 0x3736U, 0x3737U, - 0x3738U, 0x3739U, 0x3761U, 0x3762U, 0x3763U, 0x3764U, 0x3765U, 0x3766U, - 0x3830U, 0x3831U, 0x3832U, 0x3833U, 0x3834U, 0x3835U, 0x3836U, 0x3837U, - 0x3838U, 0x3839U, 0x3861U, 0x3862U, 0x3863U, 0x3864U, 0x3865U, 0x3866U, - 0x3930U, 0x3931U, 0x3932U, 0x3933U, 0x3934U, 0x3935U, 0x3936U, 0x3937U, - 0x3938U, 0x3939U, 0x3961U, 0x3962U, 0x3963U, 0x3964U, 0x3965U, 0x3966U, - 0x6130U, 0x6131U, 0x6132U, 0x6133U, 0x6134U, 0x6135U, 0x6136U, 0x6137U, - 0x6138U, 0x6139U, 0x6161U, 0x6162U, 0x6163U, 0x6164U, 0x6165U, 0x6166U, - 0x6230U, 0x6231U, 0x6232U, 0x6233U, 0x6234U, 0x6235U, 0x6236U, 0x6237U, - 0x6238U, 0x6239U, 0x6261U, 0x6262U, 0x6263U, 0x6264U, 0x6265U, 0x6266U, - 0x6330U, 0x6331U, 0x6332U, 0x6333U, 0x6334U, 0x6335U, 0x6336U, 0x6337U, - 0x6338U, 0x6339U, 0x6361U, 0x6362U, 0x6363U, 0x6364U, 0x6365U, 0x6366U, - 0x6430U, 0x6431U, 0x6432U, 0x6433U, 0x6434U, 0x6435U, 0x6436U, 0x6437U, - 0x6438U, 0x6439U, 0x6461U, 0x6462U, 0x6463U, 0x6464U, 0x6465U, 0x6466U, - 0x6530U, 0x6531U, 0x6532U, 0x6533U, 0x6534U, 0x6535U, 0x6536U, 0x6537U, - 0x6538U, 0x6539U, 0x6561U, 0x6562U, 0x6563U, 0x6564U, 0x6565U, 0x6566U, - 0x6630U, 0x6631U, 0x6632U, 0x6633U, 0x6634U, 0x6635U, 0x6636U, 0x6637U, - 0x6638U, 0x6639U, 0x6661U, 0x6662U, 0x6663U, 0x6664U, 0x6665U, 0x6666U -#else /* DUK_USE_INTEGER_BE */ - 0x3030U, 0x3130U, 0x3230U, 0x3330U, 0x3430U, 0x3530U, 0x3630U, 0x3730U, - 0x3830U, 0x3930U, 0x6130U, 0x6230U, 0x6330U, 0x6430U, 0x6530U, 0x6630U, - 0x3031U, 0x3131U, 0x3231U, 0x3331U, 0x3431U, 0x3531U, 0x3631U, 0x3731U, - 0x3831U, 0x3931U, 0x6131U, 0x6231U, 0x6331U, 0x6431U, 0x6531U, 0x6631U, - 0x3032U, 0x3132U, 0x3232U, 0x3332U, 0x3432U, 0x3532U, 0x3632U, 0x3732U, - 0x3832U, 0x3932U, 0x6132U, 0x6232U, 0x6332U, 0x6432U, 0x6532U, 0x6632U, - 0x3033U, 0x3133U, 0x3233U, 0x3333U, 0x3433U, 0x3533U, 0x3633U, 0x3733U, - 0x3833U, 0x3933U, 0x6133U, 0x6233U, 0x6333U, 0x6433U, 0x6533U, 0x6633U, - 0x3034U, 0x3134U, 0x3234U, 0x3334U, 0x3434U, 0x3534U, 0x3634U, 0x3734U, - 0x3834U, 0x3934U, 0x6134U, 0x6234U, 0x6334U, 0x6434U, 0x6534U, 0x6634U, - 0x3035U, 0x3135U, 0x3235U, 0x3335U, 0x3435U, 0x3535U, 0x3635U, 0x3735U, - 0x3835U, 0x3935U, 0x6135U, 0x6235U, 0x6335U, 0x6435U, 0x6535U, 0x6635U, - 0x3036U, 0x3136U, 0x3236U, 0x3336U, 0x3436U, 0x3536U, 0x3636U, 0x3736U, - 0x3836U, 0x3936U, 0x6136U, 0x6236U, 0x6336U, 0x6436U, 0x6536U, 0x6636U, - 0x3037U, 0x3137U, 0x3237U, 0x3337U, 0x3437U, 0x3537U, 0x3637U, 0x3737U, - 0x3837U, 0x3937U, 0x6137U, 0x6237U, 0x6337U, 0x6437U, 0x6537U, 0x6637U, - 0x3038U, 0x3138U, 0x3238U, 0x3338U, 0x3438U, 0x3538U, 0x3638U, 0x3738U, - 0x3838U, 0x3938U, 0x6138U, 0x6238U, 0x6338U, 0x6438U, 0x6538U, 0x6638U, - 0x3039U, 0x3139U, 0x3239U, 0x3339U, 0x3439U, 0x3539U, 0x3639U, 0x3739U, - 0x3839U, 0x3939U, 0x6139U, 0x6239U, 0x6339U, 0x6439U, 0x6539U, 0x6639U, - 0x3061U, 0x3161U, 0x3261U, 0x3361U, 0x3461U, 0x3561U, 0x3661U, 0x3761U, - 0x3861U, 0x3961U, 0x6161U, 0x6261U, 0x6361U, 0x6461U, 0x6561U, 0x6661U, - 0x3062U, 0x3162U, 0x3262U, 0x3362U, 0x3462U, 0x3562U, 0x3662U, 0x3762U, - 0x3862U, 0x3962U, 0x6162U, 0x6262U, 0x6362U, 0x6462U, 0x6562U, 0x6662U, - 0x3063U, 0x3163U, 0x3263U, 0x3363U, 0x3463U, 0x3563U, 0x3663U, 0x3763U, - 0x3863U, 0x3963U, 0x6163U, 0x6263U, 0x6363U, 0x6463U, 0x6563U, 0x6663U, - 0x3064U, 0x3164U, 0x3264U, 0x3364U, 0x3464U, 0x3564U, 0x3664U, 0x3764U, - 0x3864U, 0x3964U, 0x6164U, 0x6264U, 0x6364U, 0x6464U, 0x6564U, 0x6664U, - 0x3065U, 0x3165U, 0x3265U, 0x3365U, 0x3465U, 0x3565U, 0x3665U, 0x3765U, - 0x3865U, 0x3965U, 0x6165U, 0x6265U, 0x6365U, 0x6465U, 0x6565U, 0x6665U, - 0x3066U, 0x3166U, 0x3266U, 0x3366U, 0x3466U, 0x3566U, 0x3666U, 0x3766U, - 0x3866U, 0x3966U, 0x6166U, 0x6266U, 0x6366U, 0x6466U, 0x6566U, 0x6666U -#endif /* DUK_USE_INTEGER_BE */ -}; -#endif /* DUK_USE_HEX_FASTPATH */ - -/* - * Table for base-64 encoding - */ - -#if defined(DUK_USE_BASE64_FASTPATH) -DUK_INTERNAL const duk_uint8_t duk_base64_enctab[64] = { - 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, /* A...P */ - 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, /* Q...f */ - 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, /* g...v */ - 0x77, 0x78, 0x79, 0x7a, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x2b, 0x2f /* w.../ */ -}; -#endif /* DUK_USE_BASE64_FASTPATH */ - -/* - * Table for base-64 decoding - */ - -#if defined(DUK_USE_BASE64_FASTPATH) -DUK_INTERNAL const duk_int8_t duk_base64_dectab[256] = { - /* -1 = error, -2 = allowed whitespace, -3 = padding ('='), 0...63 decoded bytes */ - -1, -1, -1, -1, -1, -1, -1, -1, -1, -2, -2, -1, -1, -2, -1, -1, /* 0x00...0x0f */ - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x10...0x1f */ - -2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, /* 0x20...0x2f */ - 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -3, -1, -1, /* 0x30...0x3f */ - -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 0x40...0x4f */ - 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, /* 0x50...0x5f */ - -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, /* 0x60...0x6f */ - 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, /* 0x70...0x7f */ - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x80...0x8f */ - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x90...0x9f */ - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0xa0...0xaf */ - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0xb0...0xbf */ - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0xc0...0xcf */ - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0xd0...0xdf */ - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0xe0...0xef */ - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 /* 0xf0...0xff */ -}; -#endif /* DUK_USE_BASE64_FASTPATH */ - -/* - * Arbitrary byteswap for potentially unaligned values - * - * Used to byteswap pointers e.g. in debugger code. - */ - -#if defined(DUK_USE_DEBUGGER_SUPPORT) /* For now only needed by the debugger. */ -DUK_INTERNAL void duk_byteswap_bytes(duk_uint8_t *p, duk_small_uint_t len) { - duk_uint8_t tmp; - duk_uint8_t *q = p + len - 1; - - while (p - q < 0) { - tmp = *p; - *p = *q; - *q = tmp; - p++; - q--; - } -} -#endif -#line 1 "duk_util_hashprime.c" -/* - * Round a number upwards to a prime (not usually the nearest one). - * - * Uses a table of successive 32-bit primes whose ratio is roughly - * constant. This keeps the relative upwards 'rounding error' bounded - * and the data size small. A simple 'predict-correct' compression is - * used to compress primes to one byte per prime. See genhashsizes.py - * for details. - * - * The minimum prime returned here must be coordinated with the possible - * probe sequence steps in duk_hobject and duk_heap stringtable. - */ - -/* include removed: duk_internal.h */ - -/* Awkward inclusion condition: drop out of compilation if not needed by any - * call site: object hash part or probing stringtable. - */ -#if defined(DUK_USE_HOBJECT_HASH_PART) || defined(DUK_USE_STRTAB_PROBE) - -/* hash size ratio goal, must match genhashsizes.py */ -#define DUK__HASH_SIZE_RATIO 1177 /* floor(1.15 * (1 << 10)) */ - -/* prediction corrections for prime list (see genhashsizes.py) */ -DUK_LOCAL const duk_int8_t duk__hash_size_corrections[] = { - 17, /* minimum prime */ - 4, 3, 4, 1, 4, 1, 1, 2, 2, 2, 2, 1, 6, 6, 9, 5, 1, 2, 2, 5, 1, 3, 3, 3, - 5, 4, 4, 2, 4, 8, 3, 4, 23, 2, 4, 7, 8, 11, 2, 12, 15, 10, 1, 1, 5, 1, 5, - 8, 9, 17, 14, 10, 7, 5, 2, 46, 21, 1, 9, 9, 4, 4, 10, 23, 36, 6, 20, 29, - 18, 6, 19, 21, 16, 11, 5, 5, 48, 9, 1, 39, 14, 8, 4, 29, 9, 1, 15, 48, 12, - 22, 6, 15, 27, 4, 2, 17, 28, 8, 9, 4, 5, 8, 3, 3, 8, 37, 11, 15, 8, 30, - 43, 6, 33, 41, 5, 20, 32, 41, 38, 24, 77, 14, 19, 11, 4, 35, 18, 19, 41, - 10, 23, 16, 9, 2, - -1 -}; - -/* probe steps (see genhashsizes.py), currently assumed to be 32 entries long - * (DUK_UTIL_GET_HASH_PROBE_STEP macro). - */ -DUK_INTERNAL duk_uint8_t duk_util_probe_steps[32] = { - 2, 3, 5, 7, 11, 13, 19, 31, 41, 47, 59, 67, 73, 79, 89, 101, 103, 107, - 109, 127, 137, 139, 149, 157, 163, 167, 173, 181, 191, 193, 197, 199 -}; - -DUK_INTERNAL duk_uint32_t duk_util_get_hash_prime(duk_uint32_t size) { - const duk_int8_t *p = duk__hash_size_corrections; - duk_uint32_t curr; - - curr = (duk_uint32_t) *p++; - for (;;) { - duk_small_int_t t = (duk_small_int_t) *p++; - if (t < 0) { - /* may happen if size is very close to 2^32-1 */ - break; - } - - /* prediction: portable variant using doubles if 64-bit values not available */ -#ifdef DUK_USE_64BIT_OPS - curr = (duk_uint32_t) ((((duk_uint64_t) curr) * ((duk_uint64_t) DUK__HASH_SIZE_RATIO)) >> 10); -#else - /* 32-bit x 11-bit = 43-bit, fits accurately into a double */ - curr = (duk_uint32_t) DUK_FLOOR(((double) curr) * ((double) DUK__HASH_SIZE_RATIO) / 1024.0); -#endif - - /* correction */ - curr += t; - - DUK_DDD(DUK_DDDPRINT("size=%ld, curr=%ld", (long) size, (long) curr)); - - if (curr >= size) { - return curr; - } - } - return 0; -} - -#endif /* DUK_USE_HOBJECT_HASH_PART || DUK_USE_STRTAB_PROBE */ -#line 1 "duk_hobject_class.c" -/* - * Hobject Ecmascript [[Class]]. - */ - -/* include removed: duk_internal.h */ - -#if (DUK_STRIDX_UC_ARGUMENTS > 255) -#error constant too large -#endif -#if (DUK_STRIDX_ARRAY > 255) -#error constant too large -#endif -#if (DUK_STRIDX_UC_BOOLEAN > 255) -#error constant too large -#endif -#if (DUK_STRIDX_DATE > 255) -#error constant too large -#endif -#if (DUK_STRIDX_UC_ERROR > 255) -#error constant too large -#endif -#if (DUK_STRIDX_UC_FUNCTION > 255) -#error constant too large -#endif -#if (DUK_STRIDX_JSON > 255) -#error constant too large -#endif -#if (DUK_STRIDX_MATH > 255) -#error constant too large -#endif -#if (DUK_STRIDX_UC_NUMBER > 255) -#error constant too large -#endif -#if (DUK_STRIDX_UC_OBJECT > 255) -#error constant too large -#endif -#if (DUK_STRIDX_REG_EXP > 255) -#error constant too large -#endif -#if (DUK_STRIDX_UC_STRING > 255) -#error constant too large -#endif -#if (DUK_STRIDX_GLOBAL > 255) -#error constant too large -#endif -#if (DUK_STRIDX_OBJ_ENV > 255) -#error constant too large -#endif -#if (DUK_STRIDX_DEC_ENV > 255) -#error constant too large -#endif -#if (DUK_STRIDX_UC_BUFFER > 255) -#error constant too large -#endif -#if (DUK_STRIDX_UC_POINTER > 255) -#error constant too large -#endif -#if (DUK_STRIDX_UC_THREAD > 255) -#error constant too large -#endif -#if (DUK_STRIDX_ARRAY_BUFFER > 255) -#error constant too large -#endif -#if (DUK_STRIDX_DATA_VIEW > 255) -#error constant too large -#endif -#if (DUK_STRIDX_INT8_ARRAY > 255) -#error constant too large -#endif -#if (DUK_STRIDX_UINT8_ARRAY > 255) -#error constant too large -#endif -#if (DUK_STRIDX_UINT8_CLAMPED_ARRAY > 255) -#error constant too large -#endif -#if (DUK_STRIDX_INT16_ARRAY > 255) -#error constant too large -#endif -#if (DUK_STRIDX_UINT16_ARRAY > 255) -#error constant too large -#endif -#if (DUK_STRIDX_INT32_ARRAY > 255) -#error constant too large -#endif -#if (DUK_STRIDX_UINT32_ARRAY > 255) -#error constant too large -#endif -#if (DUK_STRIDX_FLOAT32_ARRAY > 255) -#error constant too large -#endif -#if (DUK_STRIDX_FLOAT64_ARRAY > 255) -#error constant too large -#endif -#if (DUK_STRIDX_EMPTY_STRING > 255) -#error constant too large -#endif - -/* Note: assumes that these string indexes are 8-bit, genstrings.py must ensure that */ -DUK_INTERNAL duk_uint8_t duk_class_number_to_stridx[32] = { - DUK_STRIDX_EMPTY_STRING, /* UNUSED, intentionally empty */ - DUK_STRIDX_UC_ARGUMENTS, - DUK_STRIDX_ARRAY, - DUK_STRIDX_UC_BOOLEAN, - DUK_STRIDX_DATE, - DUK_STRIDX_UC_ERROR, - DUK_STRIDX_UC_FUNCTION, - DUK_STRIDX_JSON, - DUK_STRIDX_MATH, - DUK_STRIDX_UC_NUMBER, - DUK_STRIDX_UC_OBJECT, - DUK_STRIDX_REG_EXP, - DUK_STRIDX_UC_STRING, - DUK_STRIDX_GLOBAL, - DUK_STRIDX_OBJ_ENV, - DUK_STRIDX_DEC_ENV, - DUK_STRIDX_UC_BUFFER, - DUK_STRIDX_UC_POINTER, - DUK_STRIDX_UC_THREAD, - DUK_STRIDX_ARRAY_BUFFER, - DUK_STRIDX_DATA_VIEW, - DUK_STRIDX_INT8_ARRAY, - DUK_STRIDX_UINT8_ARRAY, - DUK_STRIDX_UINT8_CLAMPED_ARRAY, - DUK_STRIDX_INT16_ARRAY, - DUK_STRIDX_UINT16_ARRAY, - DUK_STRIDX_INT32_ARRAY, - DUK_STRIDX_UINT32_ARRAY, - DUK_STRIDX_FLOAT32_ARRAY, - DUK_STRIDX_FLOAT64_ARRAY, - DUK_STRIDX_EMPTY_STRING, /* UNUSED, intentionally empty */ - DUK_STRIDX_EMPTY_STRING, /* UNUSED, intentionally empty */ -}; -#line 1 "duk_alloc_default.c" -/* - * Default allocation functions. - * - * Assumes behavior such as malloc allowing zero size, yielding - * a NULL or a unique pointer which is a no-op for free. - */ - -/* include removed: duk_internal.h */ - -#if defined(DUK_USE_PROVIDE_DEFAULT_ALLOC_FUNCTIONS) -DUK_INTERNAL void *duk_default_alloc_function(void *udata, duk_size_t size) { - void *res; - DUK_UNREF(udata); - res = DUK_ANSI_MALLOC(size); - DUK_DDD(DUK_DDDPRINT("default alloc function: %lu -> %p", - (unsigned long) size, (void *) res)); - return res; -} - -DUK_INTERNAL void *duk_default_realloc_function(void *udata, void *ptr, duk_size_t newsize) { - void *res; - DUK_UNREF(udata); - res = DUK_ANSI_REALLOC(ptr, newsize); - DUK_DDD(DUK_DDDPRINT("default realloc function: %p %lu -> %p", - (void *) ptr, (unsigned long) newsize, (void *) res)); - return res; -} - -DUK_INTERNAL void duk_default_free_function(void *udata, void *ptr) { - DUK_DDD(DUK_DDDPRINT("default free function: %p", (void *) ptr)); - DUK_UNREF(udata); - DUK_ANSI_FREE(ptr); -} -#endif /* DUK_USE_PROVIDE_DEFAULT_ALLOC_FUNCTIONS */ -#line 1 "duk_api_buffer.c" -/* - * Buffer - */ - -/* include removed: duk_internal.h */ - -DUK_EXTERNAL void *duk_resize_buffer(duk_context *ctx, duk_idx_t index, duk_size_t new_size) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_hbuffer_dynamic *h; - - DUK_ASSERT_CTX_VALID(ctx); - - h = (duk_hbuffer_dynamic *) duk_require_hbuffer(ctx, index); - DUK_ASSERT(h != NULL); - - if (!(DUK_HBUFFER_HAS_DYNAMIC(h) && !DUK_HBUFFER_HAS_EXTERNAL(h))) { - DUK_ERROR_TYPE(thr, DUK_STR_WRONG_BUFFER_TYPE); - } - - /* maximum size check is handled by callee */ - duk_hbuffer_resize(thr, h, new_size); - - return DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(thr->heap, h); -} - -DUK_EXTERNAL void *duk_steal_buffer(duk_context *ctx, duk_idx_t index, duk_size_t *out_size) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_hbuffer_dynamic *h; - void *ptr; - duk_size_t sz; - - DUK_ASSERT(ctx != NULL); - - h = (duk_hbuffer_dynamic *) duk_require_hbuffer(ctx, index); - DUK_ASSERT(h != NULL); - - if (!(DUK_HBUFFER_HAS_DYNAMIC(h) && !DUK_HBUFFER_HAS_EXTERNAL(h))) { - DUK_ERROR_TYPE(thr, DUK_STR_WRONG_BUFFER_TYPE); - } - - /* Forget the previous allocation, setting size to 0 and alloc to - * NULL. Caller is responsible for freeing the previous allocation. - * Getting the allocation and clearing it is done in the same API - * call to avoid any chance of a realloc. - */ - ptr = DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(thr->heap, h); - sz = DUK_HBUFFER_DYNAMIC_GET_SIZE(h); - if (out_size) { - *out_size = sz; - } - DUK_HBUFFER_DYNAMIC_SET_DATA_PTR_NULL(thr->heap, h); - DUK_HBUFFER_DYNAMIC_SET_SIZE(h, 0); - - return ptr; -} - -DUK_EXTERNAL void duk_config_buffer(duk_context *ctx, duk_idx_t index, void *ptr, duk_size_t len) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_hbuffer_external *h; - - DUK_ASSERT(ctx != NULL); - - h = (duk_hbuffer_external *) duk_require_hbuffer(ctx, index); - DUK_ASSERT(h != NULL); - - if (!DUK_HBUFFER_HAS_EXTERNAL(h)) { - DUK_ERROR_TYPE(thr, DUK_STR_WRONG_BUFFER_TYPE); - } - DUK_ASSERT(DUK_HBUFFER_HAS_DYNAMIC(h)); - - DUK_HBUFFER_EXTERNAL_SET_DATA_PTR(thr->heap, h, ptr); - DUK_HBUFFER_EXTERNAL_SET_SIZE(h, len); -} -#line 1 "duk_api_bytecode.c" -/* - * Bytecode dump/load - * - * The bytecode load primitive is more important performance-wise than the - * dump primitive. - * - * Unlike most Duktape API calls, bytecode dump/load is not guaranteed to be - * memory safe for invalid arguments - caller beware! There's little point - * in trying to achieve memory safety unless bytecode instructions are also - * validated which is not easy to do with indirect register references etc. - */ - -/* include removed: duk_internal.h */ - -#if defined(DUK_USE_BYTECODE_DUMP_SUPPORT) - -#define DUK__SER_MARKER 0xff -#define DUK__SER_VERSION 0x00 -#define DUK__SER_STRING 0x00 -#define DUK__SER_NUMBER 0x01 -#define DUK__BYTECODE_INITIAL_ALLOC 256 - -/* - * Dump/load helpers, xxx_raw() helpers do no buffer checks - */ - -DUK_LOCAL duk_uint8_t *duk__load_string_raw(duk_context *ctx, duk_uint8_t *p) { - duk_uint32_t len; - - len = DUK_RAW_READ_U32_BE(p); - duk_push_lstring(ctx, (const char *) p, len); - p += len; - return p; -} - -DUK_LOCAL duk_uint8_t *duk__load_buffer_raw(duk_context *ctx, duk_uint8_t *p) { - duk_uint32_t len; - duk_uint8_t *buf; - - len = DUK_RAW_READ_U32_BE(p); - buf = (duk_uint8_t *) duk_push_fixed_buffer(ctx, (duk_size_t) len); - DUK_ASSERT(buf != NULL); - DUK_MEMCPY((void *) buf, (const void *) p, (size_t) len); - p += len; - return p; -} - -DUK_LOCAL duk_uint8_t *duk__dump_hstring_raw(duk_uint8_t *p, duk_hstring *h) { - duk_size_t len; - duk_uint32_t tmp32; - - DUK_ASSERT(h != NULL); - - len = DUK_HSTRING_GET_BYTELEN(h); - DUK_ASSERT(len <= 0xffffffffUL); /* string limits */ - tmp32 = (duk_uint32_t) len; - DUK_RAW_WRITE_U32_BE(p, tmp32); - DUK_MEMCPY((void *) p, - (const void *) DUK_HSTRING_GET_DATA(h), - len); - p += len; - return p; -} - -DUK_LOCAL duk_uint8_t *duk__dump_hbuffer_raw(duk_hthread *thr, duk_uint8_t *p, duk_hbuffer *h) { - duk_size_t len; - duk_uint32_t tmp32; - - DUK_ASSERT(thr != NULL); - DUK_ASSERT(h != NULL); - DUK_UNREF(thr); - - len = DUK_HBUFFER_GET_SIZE(h); - DUK_ASSERT(len <= 0xffffffffUL); /* buffer limits */ - tmp32 = (duk_uint32_t) len; - DUK_RAW_WRITE_U32_BE(p, tmp32); - DUK_MEMCPY((void *) p, - (const void *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h), - len); - p += len; - return p; -} - -DUK_LOCAL duk_uint8_t *duk__dump_string_prop(duk_hthread *thr, duk_uint8_t *p, duk_bufwriter_ctx *bw_ctx, duk_hobject *func, duk_small_uint_t stridx) { - duk_hstring *h_str; - duk_tval *tv; - - tv = duk_hobject_find_existing_entry_tval_ptr(thr->heap, (duk_hobject *) func, DUK_HTHREAD_GET_STRING(thr, stridx)); - if (tv != NULL && DUK_TVAL_IS_STRING(tv)) { - h_str = DUK_TVAL_GET_STRING(tv); - DUK_ASSERT(h_str != NULL); - } else { - h_str = DUK_HTHREAD_STRING_EMPTY_STRING(thr); - DUK_ASSERT(h_str != NULL); - } - DUK_ASSERT(DUK_HSTRING_MAX_BYTELEN <= 0x7fffffffUL); /* ensures no overflow */ - p = DUK_BW_ENSURE_RAW(thr, bw_ctx, 4 + DUK_HSTRING_GET_BYTELEN(h_str), p); - p = duk__dump_hstring_raw(p, h_str); - return p; -} - -DUK_LOCAL duk_uint8_t *duk__dump_buffer_prop(duk_hthread *thr, duk_uint8_t *p, duk_bufwriter_ctx *bw_ctx, duk_hobject *func, duk_small_uint_t stridx) { - duk_tval *tv; - - tv = duk_hobject_find_existing_entry_tval_ptr(thr->heap, (duk_hobject *) func, DUK_HTHREAD_GET_STRING(thr, stridx)); - if (tv != NULL && DUK_TVAL_IS_BUFFER(tv)) { - duk_hbuffer *h_buf; - h_buf = DUK_TVAL_GET_BUFFER(tv); - DUK_ASSERT(h_buf != NULL); - DUK_ASSERT(DUK_HBUFFER_MAX_BYTELEN <= 0x7fffffffUL); /* ensures no overflow */ - p = DUK_BW_ENSURE_RAW(thr, bw_ctx, 4 + DUK_HBUFFER_GET_SIZE(h_buf), p); - p = duk__dump_hbuffer_raw(thr, p, h_buf); - } else { - p = DUK_BW_ENSURE_RAW(thr, bw_ctx, 4, p); - DUK_RAW_WRITE_U32_BE(p, 0); - } - return p; -} - -DUK_LOCAL duk_uint8_t *duk__dump_uint32_prop(duk_hthread *thr, duk_uint8_t *p, duk_bufwriter_ctx *bw_ctx, duk_hobject *func, duk_small_uint_t stridx, duk_uint32_t def_value) { - duk_tval *tv; - duk_uint32_t val; - - tv = duk_hobject_find_existing_entry_tval_ptr(thr->heap, (duk_hobject *) func, DUK_HTHREAD_GET_STRING(thr, stridx)); - if (tv != NULL && DUK_TVAL_IS_NUMBER(tv)) { - val = (duk_uint32_t) DUK_TVAL_GET_NUMBER(tv); - } else { - val = def_value; - } - p = DUK_BW_ENSURE_RAW(thr, bw_ctx, 4, p); - DUK_RAW_WRITE_U32_BE(p, val); - return p; -} - -DUK_LOCAL duk_uint8_t *duk__dump_varmap(duk_hthread *thr, duk_uint8_t *p, duk_bufwriter_ctx *bw_ctx, duk_hobject *func) { - duk_tval *tv; - - tv = duk_hobject_find_existing_entry_tval_ptr(thr->heap, (duk_hobject *) func, DUK_HTHREAD_STRING_INT_VARMAP(thr)); - if (tv != NULL && DUK_TVAL_IS_OBJECT(tv)) { - duk_hobject *h; - duk_uint_fast32_t i; - - h = DUK_TVAL_GET_OBJECT(tv); - DUK_ASSERT(h != NULL); - - /* We know _Varmap only has own properties so walk property - * table directly. We also know _Varmap is dense and all - * values are numbers; assert for these. GC and finalizers - * shouldn't affect _Varmap so side effects should be fine. - */ - for (i = 0; i < (duk_uint_fast32_t) DUK_HOBJECT_GET_ENEXT(h); i++) { - duk_hstring *key; - duk_tval *tv_val; - duk_uint32_t val; - - key = DUK_HOBJECT_E_GET_KEY(thr->heap, h, i); - DUK_ASSERT(key != NULL); /* _Varmap is dense */ - DUK_ASSERT(!DUK_HOBJECT_E_SLOT_IS_ACCESSOR(thr->heap, h, i)); - tv_val = DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, h, i); - DUK_ASSERT(tv_val != NULL); - DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_val)); /* known to be number; in fact an integer */ -#if defined(DUK_USE_FASTINT) - DUK_ASSERT(DUK_TVAL_IS_FASTINT(tv_val)); - DUK_ASSERT(DUK_TVAL_GET_FASTINT(tv_val) == (duk_int64_t) DUK_TVAL_GET_FASTINT_U32(tv_val)); /* known to be 32-bit */ - val = DUK_TVAL_GET_FASTINT_U32(tv_val); -#else - val = (duk_uint32_t) DUK_TVAL_GET_NUMBER(tv_val); -#endif - - DUK_ASSERT(DUK_HSTRING_MAX_BYTELEN <= 0x7fffffffUL); /* ensures no overflow */ - p = DUK_BW_ENSURE_RAW(thr, bw_ctx, 4 + DUK_HSTRING_GET_BYTELEN(key) + 4, p); - p = duk__dump_hstring_raw(p, key); - DUK_RAW_WRITE_U32_BE(p, val); - } - } - p = DUK_BW_ENSURE_RAW(thr, bw_ctx, 4, p); - DUK_RAW_WRITE_U32_BE(p, 0); /* end of _Varmap */ - return p; -} - -DUK_LOCAL duk_uint8_t *duk__dump_formals(duk_hthread *thr, duk_uint8_t *p, duk_bufwriter_ctx *bw_ctx, duk_hobject *func) { - duk_tval *tv; - - tv = duk_hobject_find_existing_entry_tval_ptr(thr->heap, (duk_hobject *) func, DUK_HTHREAD_STRING_INT_FORMALS(thr)); - if (tv != NULL && DUK_TVAL_IS_OBJECT(tv)) { - duk_hobject *h; - duk_uint_fast32_t i; - - h = DUK_TVAL_GET_OBJECT(tv); - DUK_ASSERT(h != NULL); - - /* We know _Formals is dense and all entries will be in the - * array part. GC and finalizers shouldn't affect _Formals - * so side effects should be fine. - */ - for (i = 0; i < (duk_uint_fast32_t) DUK_HOBJECT_GET_ASIZE(h); i++) { - duk_tval *tv_val; - duk_hstring *varname; - - tv_val = DUK_HOBJECT_A_GET_VALUE_PTR(thr->heap, h, i); - DUK_ASSERT(tv_val != NULL); - if (DUK_TVAL_IS_STRING(tv_val)) { - /* Array is dense and contains only strings, but ASIZE may - * be larger than used part and there are UNUSED entries. - */ - varname = DUK_TVAL_GET_STRING(tv_val); - DUK_ASSERT(varname != NULL); - - DUK_ASSERT(DUK_HSTRING_MAX_BYTELEN <= 0x7fffffffUL); /* ensures no overflow */ - p = DUK_BW_ENSURE_RAW(thr, bw_ctx, 4 + DUK_HSTRING_GET_BYTELEN(varname), p); - p = duk__dump_hstring_raw(p, varname); - } - } - } - p = DUK_BW_ENSURE_RAW(thr, bw_ctx, 4, p); - DUK_RAW_WRITE_U32_BE(p, 0); /* end of _Formals */ - return p; -} - -static duk_uint8_t *duk__dump_func(duk_context *ctx, duk_hcompiledfunction *func, duk_bufwriter_ctx *bw_ctx, duk_uint8_t *p) { - duk_hthread *thr; - duk_tval *tv, *tv_end; - duk_instr_t *ins, *ins_end; - duk_hobject **fn, **fn_end; - duk_hstring *h_str; - duk_uint32_t count_instr; - duk_uint32_t tmp32; - duk_uint16_t tmp16; - duk_double_t d; - - thr = (duk_hthread *) ctx; - DUK_UNREF(ctx); - DUK_UNREF(thr); - - DUK_DD(DUK_DDPRINT("dumping function %p to %p: " - "consts=[%p,%p[ (%ld bytes, %ld items), " - "funcs=[%p,%p[ (%ld bytes, %ld items), " - "code=[%p,%p[ (%ld bytes, %ld items)", - (void *) func, - (void *) p, - (void *) DUK_HCOMPILEDFUNCTION_GET_CONSTS_BASE(thr->heap, func), - (void *) DUK_HCOMPILEDFUNCTION_GET_CONSTS_END(thr->heap, func), - (long) DUK_HCOMPILEDFUNCTION_GET_CONSTS_SIZE(thr->heap, func), - (long) DUK_HCOMPILEDFUNCTION_GET_CONSTS_COUNT(thr->heap, func), - (void *) DUK_HCOMPILEDFUNCTION_GET_FUNCS_BASE(thr->heap, func), - (void *) DUK_HCOMPILEDFUNCTION_GET_FUNCS_END(thr->heap, func), - (long) DUK_HCOMPILEDFUNCTION_GET_FUNCS_SIZE(thr->heap, func), - (long) DUK_HCOMPILEDFUNCTION_GET_FUNCS_COUNT(thr->heap, func), - (void *) DUK_HCOMPILEDFUNCTION_GET_CODE_BASE(thr->heap, func), - (void *) DUK_HCOMPILEDFUNCTION_GET_CODE_END(thr->heap, func), - (long) DUK_HCOMPILEDFUNCTION_GET_CODE_SIZE(thr->heap, func), - (long) DUK_HCOMPILEDFUNCTION_GET_CODE_COUNT(thr->heap, func))); - - DUK_ASSERT(DUK_USE_ESBC_MAX_BYTES <= 0x7fffffffUL); /* ensures no overflow */ - count_instr = (duk_uint32_t) DUK_HCOMPILEDFUNCTION_GET_CODE_COUNT(thr->heap, func); - p = DUK_BW_ENSURE_RAW(thr, bw_ctx, 3 * 4 + 2 * 2 + 3 * 4 + count_instr * 4, p); - - /* Fixed header info. */ - tmp32 = count_instr; - DUK_RAW_WRITE_U32_BE(p, tmp32); - tmp32 = (duk_uint32_t) DUK_HCOMPILEDFUNCTION_GET_CONSTS_COUNT(thr->heap, func); - DUK_RAW_WRITE_U32_BE(p, tmp32); - tmp32 = (duk_uint32_t) DUK_HCOMPILEDFUNCTION_GET_FUNCS_COUNT(thr->heap, func); - DUK_RAW_WRITE_U32_BE(p, tmp32); - tmp16 = func->nregs; - DUK_RAW_WRITE_U16_BE(p, tmp16); - tmp16 = func->nargs; - DUK_RAW_WRITE_U16_BE(p, tmp16); -#if defined(DUK_USE_DEBUGGER_SUPPORT) - tmp32 = func->start_line; - DUK_RAW_WRITE_U32_BE(p, tmp32); - tmp32 = func->end_line; - DUK_RAW_WRITE_U32_BE(p, tmp32); -#else - DUK_RAW_WRITE_U32_BE(p, 0); - DUK_RAW_WRITE_U32_BE(p, 0); -#endif - tmp32 = ((duk_heaphdr *) func)->h_flags & DUK_HEAPHDR_FLAGS_FLAG_MASK; - DUK_RAW_WRITE_U32_BE(p, tmp32); - - /* Bytecode instructions: endian conversion needed unless - * platform is big endian. - */ - ins = DUK_HCOMPILEDFUNCTION_GET_CODE_BASE(thr->heap, func); - ins_end = DUK_HCOMPILEDFUNCTION_GET_CODE_END(thr->heap, func); - DUK_ASSERT((duk_size_t) (ins_end - ins) == (duk_size_t) count_instr); -#if defined(DUK_USE_INTEGER_BE) - DUK_MEMCPY((void *) p, (const void *) ins, (size_t) (ins_end - ins)); - p += (size_t) (ins_end - ins); -#else - while (ins != ins_end) { - tmp32 = (duk_uint32_t) (*ins); - DUK_RAW_WRITE_U32_BE(p, tmp32); - ins++; - } -#endif - - /* Constants: variable size encoding. */ - tv = DUK_HCOMPILEDFUNCTION_GET_CONSTS_BASE(thr->heap, func); - tv_end = DUK_HCOMPILEDFUNCTION_GET_CONSTS_END(thr->heap, func); - while (tv != tv_end) { - /* constants are strings or numbers now */ - DUK_ASSERT(DUK_TVAL_IS_STRING(tv) || - DUK_TVAL_IS_NUMBER(tv)); - - if (DUK_TVAL_IS_STRING(tv)) { - h_str = DUK_TVAL_GET_STRING(tv); - DUK_ASSERT(h_str != NULL); - DUK_ASSERT(DUK_HSTRING_MAX_BYTELEN <= 0x7fffffffUL); /* ensures no overflow */ - p = DUK_BW_ENSURE_RAW(thr, bw_ctx, 1 + 4 + DUK_HSTRING_GET_BYTELEN(h_str), p), - *p++ = DUK__SER_STRING; - p = duk__dump_hstring_raw(p, h_str); - } else { - DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv)); - p = DUK_BW_ENSURE_RAW(thr, bw_ctx, 1 + 8, p); - *p++ = DUK__SER_NUMBER; - d = DUK_TVAL_GET_NUMBER(tv); - DUK_RAW_WRITE_DOUBLE_BE(p, d); - } - tv++; - } - - /* Inner functions recursively. */ - fn = (duk_hobject **) DUK_HCOMPILEDFUNCTION_GET_FUNCS_BASE(thr->heap, func); - fn_end = (duk_hobject **) DUK_HCOMPILEDFUNCTION_GET_FUNCS_END(thr->heap, func); - while (fn != fn_end) { - /* XXX: This causes recursion up to inner function depth - * which is normally not an issue, e.g. mark-and-sweep uses - * a recursion limiter to avoid C stack issues. Avoiding - * this would mean some sort of a work list or just refusing - * to serialize deep functions. - */ - DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION(*fn)); - p = duk__dump_func(ctx, (duk_hcompiledfunction *) *fn, bw_ctx, p); - fn++; - } - - /* Object extra properties. - * - * There are some difference between function templates and functions. - * For example, function templates don't have .length and nargs is - * normally used to instantiate the functions. - */ - - p = duk__dump_uint32_prop(thr, p, bw_ctx, (duk_hobject *) func, DUK_STRIDX_LENGTH, (duk_uint32_t) func->nargs); - p = duk__dump_string_prop(thr, p, bw_ctx, (duk_hobject *) func, DUK_STRIDX_NAME); - p = duk__dump_string_prop(thr, p, bw_ctx, (duk_hobject *) func, DUK_STRIDX_FILE_NAME); - p = duk__dump_buffer_prop(thr, p, bw_ctx, (duk_hobject *) func, DUK_STRIDX_INT_PC2LINE); - p = duk__dump_varmap(thr, p, bw_ctx, (duk_hobject *) func); - p = duk__dump_formals(thr, p, bw_ctx, (duk_hobject *) func); - - DUK_DD(DUK_DDPRINT("serialized function %p -> final pointer %p", (void *) func, (void *) p)); - - return p; -} - -/* Load a function from bytecode. The function object returned here must - * match what is created by duk_js_push_closure() with respect to its flags, - * properties, etc. - * - * NOTE: there are intentionally no input buffer length / bound checks. - * Adding them would be easy but wouldn't ensure memory safety as untrusted - * or broken bytecode is unsafe during execution unless the opcodes themselves - * are validated (which is quite complex, especially for indirect opcodes). - */ - -#define DUK__ASSERT_LEFT(n) do { \ - DUK_ASSERT((duk_size_t) (p_end - p) >= (duk_size_t) (n)); \ - } while (0) - -static duk_uint8_t *duk__load_func(duk_context *ctx, duk_uint8_t *p, duk_uint8_t *p_end) { - duk_hthread *thr; - duk_hcompiledfunction *h_fun; - duk_hbuffer *h_data; - duk_size_t data_size; - duk_uint32_t count_instr, count_const, count_funcs; - duk_uint32_t n; - duk_uint32_t tmp32; - duk_small_uint_t const_type; - duk_uint8_t *fun_data; - duk_uint8_t *q; - duk_idx_t idx_base; - duk_tval *tv1; - duk_uarridx_t arr_idx; - - /* XXX: There's some overlap with duk_js_closure() here, but - * seems difficult to share code. Ensure that the final function - * looks the same as created by duk_js_closure(). - */ - - DUK_ASSERT(ctx != NULL); - thr = (duk_hthread *) ctx; - - DUK_DD(DUK_DDPRINT("loading function, p=%p, p_end=%p", (void *) p, (void *) p_end)); - - DUK__ASSERT_LEFT(3 * 4); - count_instr = DUK_RAW_READ_U32_BE(p); - count_const = DUK_RAW_READ_U32_BE(p); - count_funcs = DUK_RAW_READ_U32_BE(p); - - data_size = sizeof(duk_tval) * count_const + - sizeof(duk_hobject *) * count_funcs + - sizeof(duk_instr_t) * count_instr; - - DUK_DD(DUK_DDPRINT("instr=%ld, const=%ld, funcs=%ld, data_size=%ld", - (long) count_instr, (long) count_const, - (long) count_const, (long) data_size)); - - /* Value stack is used to ensure reachability of constants and - * inner functions being loaded. Require enough space to handle - * large functions correctly. - */ - duk_require_stack(ctx, 2 + count_const + count_funcs); - idx_base = duk_get_top(ctx); - - /* Push function object, init flags etc. This must match - * duk_js_push_closure() quite carefully. - */ - duk_push_compiledfunction(ctx); - h_fun = duk_get_hcompiledfunction(ctx, -1); - DUK_ASSERT(h_fun != NULL); - DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION((duk_hobject *) h_fun)); - DUK_ASSERT(DUK_HCOMPILEDFUNCTION_GET_DATA(thr->heap, h_fun) == NULL); - DUK_ASSERT(DUK_HCOMPILEDFUNCTION_GET_FUNCS(thr->heap, h_fun) == NULL); - DUK_ASSERT(DUK_HCOMPILEDFUNCTION_GET_BYTECODE(thr->heap, h_fun) == NULL); - - h_fun->nregs = DUK_RAW_READ_U16_BE(p); - h_fun->nargs = DUK_RAW_READ_U16_BE(p); -#if defined(DUK_USE_DEBUGGER_SUPPORT) - h_fun->start_line = DUK_RAW_READ_U32_BE(p); - h_fun->end_line = DUK_RAW_READ_U32_BE(p); -#else - p += 8; /* skip line info */ -#endif - - /* duk_hcompiledfunction flags; quite version specific */ - tmp32 = DUK_RAW_READ_U32_BE(p); - DUK_HEAPHDR_SET_FLAGS((duk_heaphdr *) h_fun, tmp32); - - /* standard prototype */ - DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, &h_fun->obj, thr->builtins[DUK_BIDX_FUNCTION_PROTOTYPE]); - - /* assert just a few critical flags */ - DUK_ASSERT(DUK_HEAPHDR_GET_TYPE((duk_heaphdr *) h_fun) == DUK_HTYPE_OBJECT); - DUK_ASSERT(!DUK_HOBJECT_HAS_BOUND(&h_fun->obj)); - DUK_ASSERT(DUK_HOBJECT_HAS_COMPILEDFUNCTION(&h_fun->obj)); - DUK_ASSERT(!DUK_HOBJECT_HAS_NATIVEFUNCTION(&h_fun->obj)); - DUK_ASSERT(!DUK_HOBJECT_HAS_THREAD(&h_fun->obj)); - DUK_ASSERT(!DUK_HOBJECT_HAS_EXOTIC_ARRAY(&h_fun->obj)); - DUK_ASSERT(!DUK_HOBJECT_HAS_EXOTIC_STRINGOBJ(&h_fun->obj)); - DUK_ASSERT(!DUK_HOBJECT_HAS_EXOTIC_ARGUMENTS(&h_fun->obj)); - - /* Create function 'data' buffer but don't attach it yet. */ - fun_data = (duk_uint8_t *) duk_push_fixed_buffer(ctx, data_size); - DUK_ASSERT(fun_data != NULL); - - /* Load bytecode instructions. */ - DUK_ASSERT(sizeof(duk_instr_t) == 4); - DUK__ASSERT_LEFT(count_instr * sizeof(duk_instr_t)); -#if defined(DUK_USE_INTEGER_BE) - q = fun_data + sizeof(duk_tval) * count_const + sizeof(duk_hobject *) * count_funcs; - DUK_MEMCPY((void *) q, - (const void *) p, - sizeof(duk_instr_t) * count_instr); - p += sizeof(duk_instr_t) * count_instr; -#else - q = fun_data + sizeof(duk_tval) * count_const + sizeof(duk_hobject *) * count_funcs; - for (n = count_instr; n > 0; n--) { - *((duk_instr_t *) (void *) q) = DUK_RAW_READ_U32_BE(p); - q += sizeof(duk_instr_t); - } -#endif - - /* Load constants onto value stack but don't yet copy to buffer. */ - for (n = count_const; n > 0; n--) { - DUK__ASSERT_LEFT(1); - const_type = DUK_RAW_READ_U8(p); - switch (const_type) { - case DUK__SER_STRING: { - p = duk__load_string_raw(ctx, p); - break; - } - case DUK__SER_NUMBER: { - /* Important to do a fastint check so that constants are - * properly read back as fastints. - */ - duk_tval tv_tmp; - duk_double_t val; - DUK__ASSERT_LEFT(8); - val = DUK_RAW_READ_DOUBLE_BE(p); - DUK_TVAL_SET_NUMBER_CHKFAST(&tv_tmp, val); - duk_push_tval(ctx, &tv_tmp); - break; - } - default: { - goto format_error; - } - } - } - - /* Load inner functions to value stack, but don't yet copy to buffer. */ - for (n = count_funcs; n > 0; n--) { - p = duk__load_func(ctx, p, p_end); - if (p == NULL) { - goto format_error; - } - } - - /* With constants and inner functions on value stack, we can now - * atomically finish the function 'data' buffer, bump refcounts, - * etc. - * - * Here we take advantage of the value stack being just a duk_tval - * array: we can just memcpy() the constants as long as we incref - * them afterwards. - */ - - h_data = (duk_hbuffer *) duk_get_hbuffer(ctx, idx_base + 1); - DUK_ASSERT(h_data != NULL); - DUK_ASSERT(!DUK_HBUFFER_HAS_DYNAMIC(h_data)); - DUK_HCOMPILEDFUNCTION_SET_DATA(thr->heap, h_fun, h_data); - DUK_HBUFFER_INCREF(thr, h_data); - - tv1 = duk_get_tval(ctx, idx_base + 2); /* may be NULL if no constants or inner funcs */ - DUK_ASSERT((count_const == 0 && count_funcs == 0) || tv1 != NULL); - - q = fun_data; - if (count_const > 0) { - /* Explicit zero size check to avoid NULL 'tv1'. */ - DUK_MEMCPY((void *) q, (const void *) tv1, sizeof(duk_tval) * count_const); - for (n = count_const; n > 0; n--) { - DUK_TVAL_INCREF_FAST(thr, (duk_tval *) (void *) q); /* no side effects */ - q += sizeof(duk_tval); - } - tv1 += count_const; - } - - DUK_HCOMPILEDFUNCTION_SET_FUNCS(thr->heap, h_fun, (duk_hobject **) (void *) q); - for (n = count_funcs; n > 0; n--) { - duk_hobject *h_obj; - - DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv1)); - h_obj = DUK_TVAL_GET_OBJECT(tv1); - DUK_ASSERT(h_obj != NULL); - tv1++; - DUK_HOBJECT_INCREF(thr, h_obj); - - *((duk_hobject **) (void *) q) = h_obj; - q += sizeof(duk_hobject *); - } - - DUK_HCOMPILEDFUNCTION_SET_BYTECODE(thr->heap, h_fun, (duk_instr_t *) (void *) q); - - /* The function object is now reachable and refcounts are fine, - * so we can pop off all the temporaries. - */ - DUK_DDD(DUK_DDDPRINT("function is reachable, reset top; func: %!iT", duk_get_tval(ctx, idx_base))); - duk_set_top(ctx, idx_base + 1); - - /* Setup function properties. */ - tmp32 = DUK_RAW_READ_U32_BE(p); - duk_push_u32(ctx, tmp32); - duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_LENGTH, DUK_PROPDESC_FLAGS_NONE); - - p = duk__load_string_raw(ctx, p); - if (DUK_HOBJECT_HAS_NAMEBINDING((duk_hobject *) h_fun)) { - /* Original function instance/template had NAMEBINDING. - * Must create a lexical environment on loading to allow - * recursive functions like 'function foo() { foo(); }'. - */ - duk_hobject *proto; - - proto = thr->builtins[DUK_BIDX_GLOBAL_ENV]; - (void) duk_push_object_helper_proto(ctx, - DUK_HOBJECT_FLAG_EXTENSIBLE | - DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_DECENV), - proto); - duk_dup(ctx, -2); /* -> [ func funcname env funcname ] */ - duk_dup(ctx, idx_base); /* -> [ func funcname env funcname func ] */ - duk_xdef_prop(ctx, -3, DUK_PROPDESC_FLAGS_NONE); /* -> [ func funcname env ] */ - duk_xdef_prop_stridx(ctx, idx_base, DUK_STRIDX_INT_LEXENV, DUK_PROPDESC_FLAGS_WC); - /* since closure has NEWENV, never define DUK_STRIDX_INT_VARENV, as it - * will be ignored anyway - */ - } - duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_NAME, DUK_PROPDESC_FLAGS_NONE); - - p = duk__load_string_raw(ctx, p); - duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_FILE_NAME, DUK_PROPDESC_FLAGS_WC); - - duk_push_object(ctx); - duk_dup(ctx, -2); - duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_CONSTRUCTOR, DUK_PROPDESC_FLAGS_WC); /* func.prototype.constructor = func */ - duk_compact(ctx, -1); - duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_PROTOTYPE, DUK_PROPDESC_FLAGS_W); - - p = duk__load_buffer_raw(ctx, p); - duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_INT_PC2LINE, DUK_PROPDESC_FLAGS_WC); - - duk_push_object(ctx); /* _Varmap */ - for (;;) { - /* XXX: awkward */ - p = duk__load_string_raw(ctx, p); - if (duk_get_length(ctx, -1) == 0) { - duk_pop(ctx); - break; - } - tmp32 = DUK_RAW_READ_U32_BE(p); - duk_push_u32(ctx, tmp32); - duk_put_prop(ctx, -3); - } - duk_compact(ctx, -1); - duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_INT_VARMAP, DUK_PROPDESC_FLAGS_NONE); - - duk_push_array(ctx); /* _Formals */ - for (arr_idx = 0; ; arr_idx++) { - /* XXX: awkward */ - p = duk__load_string_raw(ctx, p); - if (duk_get_length(ctx, -1) == 0) { - duk_pop(ctx); - break; - } - duk_put_prop_index(ctx, -2, arr_idx); - } - duk_compact(ctx, -1); - duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_INT_FORMALS, DUK_PROPDESC_FLAGS_NONE); - - /* Return with final function pushed on stack top. */ - DUK_DD(DUK_DDPRINT("final loaded function: %!iT", duk_get_tval(ctx, -1))); - DUK_ASSERT_TOP(ctx, idx_base + 1); - return p; - - format_error: - return NULL; -} - -DUK_EXTERNAL void duk_dump_function(duk_context *ctx) { - duk_hthread *thr; - duk_hcompiledfunction *func; - duk_bufwriter_ctx bw_ctx_alloc; - duk_bufwriter_ctx *bw_ctx = &bw_ctx_alloc; - duk_uint8_t *p; - - DUK_ASSERT(ctx != NULL); - thr = (duk_hthread *) ctx; - - /* Bound functions don't have all properties so we'd either need to - * lookup the non-bound target function or reject bound functions. - * For now, bound functions are rejected. - */ - func = duk_require_hcompiledfunction(ctx, -1); - DUK_ASSERT(func != NULL); - DUK_ASSERT(!DUK_HOBJECT_HAS_BOUND(&func->obj)); - - /* Estimating the result size beforehand would be costly, so - * start with a reasonable size and extend as needed. - */ - DUK_BW_INIT_PUSHBUF(thr, bw_ctx, DUK__BYTECODE_INITIAL_ALLOC); - p = DUK_BW_GET_PTR(thr, bw_ctx); - *p++ = DUK__SER_MARKER; - *p++ = DUK__SER_VERSION; - p = duk__dump_func(ctx, func, bw_ctx, p); - DUK_BW_SET_PTR(thr, bw_ctx, p); - DUK_BW_COMPACT(thr, bw_ctx); - - DUK_DD(DUK_DDPRINT("serialized result: %!T", duk_get_tval(ctx, -1))); - - duk_remove(ctx, -2); /* [ ... func buf ] -> [ ... buf ] */ -} - -DUK_EXTERNAL void duk_load_function(duk_context *ctx) { - duk_hthread *thr; - duk_uint8_t *p_buf, *p, *p_end; - duk_size_t sz; - - DUK_ASSERT(ctx != NULL); - thr = (duk_hthread *) ctx; - DUK_UNREF(ctx); - - p_buf = (duk_uint8_t *) duk_require_buffer(ctx, -1, &sz); - DUK_ASSERT(p_buf != NULL); - - /* The caller is responsible for being sure that bytecode being loaded - * is valid and trusted. Invalid bytecode can cause memory unsafe - * behavior directly during loading or later during bytecode execution - * (instruction validation would be quite complex to implement). - * - * This signature check is the only sanity check for detecting - * accidental invalid inputs. The initial 0xFF byte ensures no - * ordinary string will be accepted by accident. - */ - p = p_buf; - p_end = p_buf + sz; - if (sz < 2 || p[0] != DUK__SER_MARKER || p[1] != DUK__SER_VERSION) { - goto format_error; - } - p += 2; - - p = duk__load_func(ctx, p, p_end); - if (p == NULL) { - goto format_error; - } - - duk_remove(ctx, -2); /* [ ... buf func ] -> [ ... func ] */ - return; - - format_error: - DUK_ERROR_TYPE(thr, DUK_STR_DECODE_FAILED); -} - -#undef DUK__SER_MARKER -#undef DUK__SER_VERSION -#undef DUK__SER_STRING -#undef DUK__SER_NUMBER -#undef DUK__BYTECODE_INITIAL_ALLOC - -#else /* DUK_USE_BYTECODE_DUMP_SUPPORT */ - -DUK_EXTERNAL void duk_dump_function(duk_context *ctx) { - DUK_ERROR_UNSUPPORTED_DEFMSG((duk_hthread *) ctx); -} - -DUK_EXTERNAL void duk_load_function(duk_context *ctx) { - DUK_ERROR_UNSUPPORTED_DEFMSG((duk_hthread *) ctx); -} - -#endif /* DUK_USE_BYTECODE_DUMP_SUPPORT */ -#line 1 "duk_api_call.c" -/* - * Calls. - * - * Protected variants should avoid ever throwing an error. - */ - -/* include removed: duk_internal.h */ - -/* Prepare value stack for a method call through an object property. - * May currently throw an error e.g. when getting the property. - */ -DUK_LOCAL void duk__call_prop_prep_stack(duk_context *ctx, duk_idx_t normalized_obj_index, duk_idx_t nargs) { - DUK_ASSERT_CTX_VALID(ctx); - - DUK_DDD(DUK_DDDPRINT("duk__call_prop_prep_stack, normalized_obj_index=%ld, nargs=%ld, stacktop=%ld", - (long) normalized_obj_index, (long) nargs, (long) duk_get_top(ctx))); - - /* [... key arg1 ... argN] */ - - /* duplicate key */ - duk_dup(ctx, -nargs - 1); /* Note: -nargs alone would fail for nargs == 0, this is OK */ - duk_get_prop(ctx, normalized_obj_index); - - DUK_DDD(DUK_DDDPRINT("func: %!T", (duk_tval *) duk_get_tval(ctx, -1))); - - /* [... key arg1 ... argN func] */ - - duk_replace(ctx, -nargs - 2); - - /* [... func arg1 ... argN] */ - - duk_dup(ctx, normalized_obj_index); - duk_insert(ctx, -nargs - 1); - - /* [... func this arg1 ... argN] */ -} - -DUK_EXTERNAL void duk_call(duk_context *ctx, duk_idx_t nargs) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_small_uint_t call_flags; - duk_idx_t idx_func; - - DUK_ASSERT_CTX_VALID(ctx); - DUK_ASSERT(thr != NULL); - - idx_func = duk_get_top(ctx) - nargs - 1; - if (idx_func < 0 || nargs < 0) { - /* note that we can't reliably pop anything here */ - DUK_ERROR_API(thr, DUK_STR_INVALID_CALL_ARGS); - } - - /* XXX: awkward; we assume there is space for this, overwrite - * directly instead? - */ - duk_push_undefined(ctx); - duk_insert(ctx, idx_func + 1); - - call_flags = 0; /* not protected, respect reclimit, not constructor */ - - duk_handle_call_unprotected(thr, /* thread */ - nargs, /* num_stack_args */ - call_flags); /* call_flags */ -} - -DUK_EXTERNAL void duk_call_method(duk_context *ctx, duk_idx_t nargs) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_small_uint_t call_flags; - duk_idx_t idx_func; - - DUK_ASSERT_CTX_VALID(ctx); - DUK_ASSERT(thr != NULL); - - idx_func = duk_get_top(ctx) - nargs - 2; /* must work for nargs <= 0 */ - if (idx_func < 0 || nargs < 0) { - /* note that we can't reliably pop anything here */ - DUK_ERROR_API(thr, DUK_STR_INVALID_CALL_ARGS); - } - - call_flags = 0; /* not protected, respect reclimit, not constructor */ - - duk_handle_call_unprotected(thr, /* thread */ - nargs, /* num_stack_args */ - call_flags); /* call_flags */ -} - -DUK_EXTERNAL void duk_call_prop(duk_context *ctx, duk_idx_t obj_index, duk_idx_t nargs) { - /* - * XXX: if duk_handle_call() took values through indices, this could be - * made much more sensible. However, duk_handle_call() needs to fudge - * the 'this' and 'func' values to handle bound function chains, which - * is now done "in-place", so this is not a trivial change. - */ - - DUK_ASSERT_CTX_VALID(ctx); - - obj_index = duk_require_normalize_index(ctx, obj_index); /* make absolute */ - - duk__call_prop_prep_stack(ctx, obj_index, nargs); - - duk_call_method(ctx, nargs); -} - -DUK_EXTERNAL duk_int_t duk_pcall(duk_context *ctx, duk_idx_t nargs) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_small_uint_t call_flags; - duk_idx_t idx_func; - duk_int_t rc; - - DUK_ASSERT_CTX_VALID(ctx); - DUK_ASSERT(thr != NULL); - - idx_func = duk_get_top(ctx) - nargs - 1; /* must work for nargs <= 0 */ - if (idx_func < 0 || nargs < 0) { - /* We can't reliably pop anything here because the stack input - * shape is incorrect. So we throw an error; if the caller has - * no catch point for this, a fatal error will occur. Another - * alternative would be to just return an error. But then the - * stack would be in an unknown state which might cause some - * very hard to diagnose problems later on. Also note that even - * if we did not throw an error here, the underlying call handler - * might STILL throw an out-of-memory error or some other internal - * fatal error. - */ - DUK_ERROR_API(thr, DUK_STR_INVALID_CALL_ARGS); - return DUK_EXEC_ERROR; /* unreachable */ - } - - /* awkward; we assume there is space for this */ - duk_push_undefined(ctx); - duk_insert(ctx, idx_func + 1); - - call_flags = 0; /* respect reclimit, not constructor */ - - rc = duk_handle_call_protected(thr, /* thread */ - nargs, /* num_stack_args */ - call_flags); /* call_flags */ - - return rc; -} - -DUK_EXTERNAL duk_int_t duk_pcall_method(duk_context *ctx, duk_idx_t nargs) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_small_uint_t call_flags; - duk_idx_t idx_func; - duk_int_t rc; - - DUK_ASSERT_CTX_VALID(ctx); - DUK_ASSERT(thr != NULL); - - idx_func = duk_get_top(ctx) - nargs - 2; /* must work for nargs <= 0 */ - if (idx_func < 0 || nargs < 0) { - /* See comments in duk_pcall(). */ - DUK_ERROR_API(thr, DUK_STR_INVALID_CALL_ARGS); - return DUK_EXEC_ERROR; /* unreachable */ - } - - call_flags = 0; /* respect reclimit, not constructor */ - - rc = duk_handle_call_protected(thr, /* thread */ - nargs, /* num_stack_args */ - call_flags); /* call_flags */ - - return rc; -} - -DUK_LOCAL duk_ret_t duk__pcall_prop_raw(duk_context *ctx) { - duk_idx_t obj_index; - duk_idx_t nargs; - - /* Get the original arguments. Note that obj_index may be a relative - * index so the stack must have the same top when we use it. - */ - - DUK_ASSERT_CTX_VALID(ctx); - - obj_index = (duk_idx_t) duk_get_int(ctx, -2); - nargs = (duk_idx_t) duk_get_int(ctx, -1); - duk_pop_2(ctx); - - obj_index = duk_require_normalize_index(ctx, obj_index); /* make absolute */ - duk__call_prop_prep_stack(ctx, obj_index, nargs); - duk_call_method(ctx, nargs); - return 1; -} - -DUK_EXTERNAL duk_int_t duk_pcall_prop(duk_context *ctx, duk_idx_t obj_index, duk_idx_t nargs) { - /* - * Must be careful to catch errors related to value stack manipulation - * and property lookup, not just the call itself. - */ - - DUK_ASSERT_CTX_VALID(ctx); - - duk_push_idx(ctx, obj_index); - duk_push_idx(ctx, nargs); - - /* Inputs: explicit arguments (nargs), +1 for key, +2 for obj_index/nargs passing. - * If the value stack does not contain enough args, an error is thrown; this matches - * behavior of the other protected call API functions. - */ - return duk_safe_call(ctx, duk__pcall_prop_raw, nargs + 1 + 2 /*nargs*/, 1 /*nrets*/); -} - -DUK_EXTERNAL duk_int_t duk_safe_call(duk_context *ctx, duk_safe_call_function func, duk_idx_t nargs, duk_idx_t nrets) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_int_t rc; - - DUK_ASSERT_CTX_VALID(ctx); - DUK_ASSERT(thr != NULL); - - if (duk_get_top(ctx) < nargs || nrets < 0) { - /* See comments in duk_pcall(). */ - DUK_ERROR_API(thr, DUK_STR_INVALID_CALL_ARGS); - return DUK_EXEC_ERROR; /* unreachable */ - } - - rc = duk_handle_safe_call(thr, /* thread */ - func, /* func */ - nargs, /* num_stack_args */ - nrets); /* num_stack_res */ - - return rc; -} - -DUK_EXTERNAL void duk_new(duk_context *ctx, duk_idx_t nargs) { - /* - * There are two [[Construct]] operations in the specification: - * - * - E5 Section 13.2.2: for Function objects - * - E5 Section 15.3.4.5.2: for "bound" Function objects - * - * The chain of bound functions is resolved in Section 15.3.4.5.2, - * with arguments "piling up" until the [[Construct]] internal - * method is called on the final, actual Function object. Note - * that the "prototype" property is looked up *only* from the - * final object, *before* calling the constructor. - * - * Currently we follow the bound function chain here to get the - * "prototype" property value from the final, non-bound function. - * However, we let duk_handle_call() handle the argument "piling" - * when the constructor is called. The bound function chain is - * thus now processed twice. - * - * When constructing new Array instances, an unnecessary object is - * created and discarded now: the standard [[Construct]] creates an - * object, and calls the Array constructor. The Array constructor - * returns an Array instance, which is used as the result value for - * the "new" operation; the object created before the Array constructor - * call is discarded. - * - * This would be easy to fix, e.g. by knowing that the Array constructor - * will always create a replacement object and skip creating the fallback - * object in that case. - * - * Note: functions called via "new" need to know they are called as a - * constructor. For instance, built-in constructors behave differently - * depending on how they are called. - */ - - /* XXX: merge this with duk_js_call.c, as this function implements - * core semantics (or perhaps merge the two files altogether). - */ - - duk_hthread *thr = (duk_hthread *) ctx; - duk_hobject *proto; - duk_hobject *cons; - duk_hobject *fallback; - duk_idx_t idx_cons; - duk_small_uint_t call_flags; - - DUK_ASSERT_CTX_VALID(ctx); - - /* [... constructor arg1 ... argN] */ - - idx_cons = duk_require_normalize_index(ctx, -nargs - 1); - - DUK_DDD(DUK_DDDPRINT("top=%ld, nargs=%ld, idx_cons=%ld", - (long) duk_get_top(ctx), (long) nargs, (long) idx_cons)); - - /* XXX: code duplication */ - - /* - * Figure out the final, non-bound constructor, to get "prototype" - * property. - */ - - duk_dup(ctx, idx_cons); - for (;;) { - cons = duk_get_hobject(ctx, -1); - if (cons == NULL || !DUK_HOBJECT_HAS_CONSTRUCTABLE(cons)) { - /* Checking constructability from anything else than the - * initial constructor is not strictly necessary, but a - * nice sanity check. - */ - goto not_constructable; - } - if (!DUK_HOBJECT_HAS_BOUND(cons)) { - break; - } - duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_TARGET); /* -> [... cons target] */ - duk_remove(ctx, -2); /* -> [... target] */ - } - DUK_ASSERT(cons != NULL && !DUK_HOBJECT_HAS_BOUND(cons)); - - /* [... constructor arg1 ... argN final_cons] */ - - /* - * Create "fallback" object to be used as the object instance, - * unless the constructor returns a replacement value. - * Its internal prototype needs to be set based on "prototype" - * property of the constructor. - */ - - duk_push_object(ctx); /* class Object, extensible */ - - /* [... constructor arg1 ... argN final_cons fallback] */ - - duk_get_prop_stridx(ctx, -2, DUK_STRIDX_PROTOTYPE); - proto = duk_get_hobject(ctx, -1); - if (!proto) { - DUK_DDD(DUK_DDDPRINT("constructor has no 'prototype' property, or value not an object " - "-> leave standard Object prototype as fallback prototype")); - } else { - DUK_DDD(DUK_DDDPRINT("constructor has 'prototype' property with object value " - "-> set fallback prototype to that value: %!iO", (duk_heaphdr *) proto)); - fallback = duk_get_hobject(ctx, -2); - DUK_ASSERT(fallback != NULL); - DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, fallback, proto); - } - duk_pop(ctx); - - /* [... constructor arg1 ... argN final_cons fallback] */ - - /* - * Manipulate callstack for the call. - */ - - duk_dup_top(ctx); - duk_insert(ctx, idx_cons + 1); /* use fallback as 'this' value */ - duk_insert(ctx, idx_cons); /* also stash it before constructor, - * in case we need it (as the fallback value) - */ - duk_pop(ctx); /* pop final_cons */ - - - /* [... fallback constructor fallback(this) arg1 ... argN]; - * Note: idx_cons points to first 'fallback', not 'constructor'. - */ - - DUK_DDD(DUK_DDDPRINT("before call, idx_cons+1 (constructor) -> %!T, idx_cons+2 (fallback/this) -> %!T, " - "nargs=%ld, top=%ld", - (duk_tval *) duk_get_tval(ctx, idx_cons + 1), - (duk_tval *) duk_get_tval(ctx, idx_cons + 2), - (long) nargs, - (long) duk_get_top(ctx))); - - /* - * Call the constructor function (called in "constructor mode"). - */ - - call_flags = DUK_CALL_FLAG_CONSTRUCTOR_CALL; /* not protected, respect reclimit, is a constructor call */ - - duk_handle_call_unprotected(thr, /* thread */ - nargs, /* num_stack_args */ - call_flags); /* call_flags */ - - /* [... fallback retval] */ - - DUK_DDD(DUK_DDDPRINT("constructor call finished, fallback=%!iT, retval=%!iT", - (duk_tval *) duk_get_tval(ctx, -2), - (duk_tval *) duk_get_tval(ctx, -1))); - - /* - * Determine whether to use the constructor return value as the created - * object instance or not. - */ - - if (duk_is_object(ctx, -1)) { - duk_remove(ctx, -2); - } else { - duk_pop(ctx); - } - - /* - * Augment created errors upon creation (not when they are thrown or - * rethrown). __FILE__ and __LINE__ are not desirable here; the call - * stack reflects the caller which is correct. - */ - -#ifdef DUK_USE_AUGMENT_ERROR_CREATE - duk_hthread_sync_currpc(thr); - duk_err_augment_error_create(thr, thr, NULL, 0, 1 /*noblame_fileline*/); -#endif - - /* [... retval] */ - - return; - - not_constructable: - DUK_ERROR_TYPE(thr, DUK_STR_NOT_CONSTRUCTABLE); -} - -DUK_LOCAL duk_ret_t duk__pnew_helper(duk_context *ctx) { - duk_uint_t nargs; - - nargs = duk_to_uint(ctx, -1); - duk_pop(ctx); - - duk_new(ctx, nargs); - return 1; -} - -DUK_EXTERNAL duk_int_t duk_pnew(duk_context *ctx, duk_idx_t nargs) { - duk_int_t rc; - - DUK_ASSERT_CTX_VALID(ctx); - - /* For now, just use duk_safe_call() to wrap duk_new(). We can't - * simply use a protected duk_handle_call() because there's post - * processing which might throw. It should be possible to ensure - * the post processing never throws (except in internal errors and - * out of memory etc which are always allowed) and then remove this - * wrapper. - */ - - duk_push_uint(ctx, nargs); - rc = duk_safe_call(ctx, duk__pnew_helper, nargs + 2 /*nargs*/, 1 /*nrets*/); - return rc; -} - -DUK_EXTERNAL duk_bool_t duk_is_constructor_call(duk_context *ctx) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_activation *act; - - DUK_ASSERT_CTX_VALID(ctx); - DUK_ASSERT(thr != NULL); - DUK_ASSERT_DISABLE(thr->callstack_top >= 0); - - act = duk_hthread_get_current_activation(thr); - DUK_ASSERT(act != NULL); /* because callstack_top > 0 */ - return ((act->flags & DUK_ACT_FLAG_CONSTRUCT) != 0 ? 1 : 0); -} - -DUK_EXTERNAL duk_bool_t duk_is_strict_call(duk_context *ctx) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_activation *act; - - /* For user code this could just return 1 (strict) always - * because all Duktape/C functions are considered strict, - * and strict is also the default when nothing is running. - * However, Duktape may call this function internally when - * the current activation is an Ecmascript function, so - * this cannot be replaced by a 'return 1' without fixing - * the internal call sites. - */ - - DUK_ASSERT_CTX_VALID(ctx); - DUK_ASSERT(thr != NULL); - DUK_ASSERT_DISABLE(thr->callstack_top >= 0); - - act = duk_hthread_get_current_activation(thr); - if (act == NULL) { - /* Strict by default. */ - return 1; - } - return ((act->flags & DUK_ACT_FLAG_STRICT) != 0 ? 1 : 0); -} - -/* - * Duktape/C function magic - */ - -DUK_EXTERNAL duk_int_t duk_get_current_magic(duk_context *ctx) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_activation *act; - duk_hobject *func; - - DUK_ASSERT_CTX_VALID(ctx); - DUK_ASSERT(thr != NULL); - DUK_ASSERT_DISABLE(thr->callstack_top >= 0); - - act = duk_hthread_get_current_activation(thr); - if (act) { - func = DUK_ACT_GET_FUNC(act); - if (!func) { - duk_tval *tv = &act->tv_func; - duk_small_uint_t lf_flags; - lf_flags = DUK_TVAL_GET_LIGHTFUNC_FLAGS(tv); - return (duk_int_t) DUK_LFUNC_FLAGS_GET_MAGIC(lf_flags); - } - DUK_ASSERT(func != NULL); - - if (DUK_HOBJECT_IS_NATIVEFUNCTION(func)) { - duk_hnativefunction *nf = (duk_hnativefunction *) func; - return (duk_int_t) nf->magic; - } - } - return 0; -} - -DUK_EXTERNAL duk_int_t duk_get_magic(duk_context *ctx, duk_idx_t index) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_tval *tv; - duk_hobject *h; - - DUK_ASSERT_CTX_VALID(ctx); - - tv = duk_require_tval(ctx, index); - if (DUK_TVAL_IS_OBJECT(tv)) { - h = DUK_TVAL_GET_OBJECT(tv); - DUK_ASSERT(h != NULL); - if (!DUK_HOBJECT_HAS_NATIVEFUNCTION(h)) { - goto type_error; - } - return (duk_int_t) ((duk_hnativefunction *) h)->magic; - } else if (DUK_TVAL_IS_LIGHTFUNC(tv)) { - duk_small_uint_t lf_flags = DUK_TVAL_GET_LIGHTFUNC_FLAGS(tv); - return (duk_int_t) DUK_LFUNC_FLAGS_GET_MAGIC(lf_flags); - } - - /* fall through */ - type_error: - DUK_ERROR_TYPE(thr, DUK_STR_UNEXPECTED_TYPE); - return 0; -} - -DUK_EXTERNAL void duk_set_magic(duk_context *ctx, duk_idx_t index, duk_int_t magic) { - duk_hnativefunction *nf; - - DUK_ASSERT_CTX_VALID(ctx); - - nf = duk_require_hnativefunction(ctx, index); - DUK_ASSERT(nf != NULL); - nf->magic = (duk_int16_t) magic; -} -#line 1 "duk_api_codec.c" -/* - * Encoding and decoding basic formats: hex, base64. - * - * These are in-place operations which may allow an optimized implementation. - * - * Base-64: https://tools.ietf.org/html/rfc4648#section-4 - */ - -/* include removed: duk_internal.h */ - -/* Shared handling for encode/decode argument. Fast path handling for - * buffer and string values because they're the most common. In particular, - * avoid creating a temporary string or buffer when possible. - */ -DUK_LOCAL const duk_uint8_t *duk__prep_codec_arg(duk_context *ctx, duk_idx_t index, duk_size_t *out_len) { - DUK_ASSERT(duk_is_valid_index(ctx, index)); /* checked by caller */ - if (duk_is_buffer(ctx, index)) { - return (const duk_uint8_t *) duk_get_buffer(ctx, index, out_len); - } else { - return (const duk_uint8_t *) duk_to_lstring(ctx, index, out_len); - } -} - -#if defined(DUK_USE_BASE64_FASTPATH) -DUK_LOCAL void duk__base64_encode_helper(const duk_uint8_t *src, duk_size_t srclen, duk_uint8_t *dst) { - duk_uint_t t; - duk_size_t n_full, n_full3, n_final; - const duk_uint8_t *src_end_fast; - - n_full = srclen / 3; /* full 3-byte -> 4-char conversions */ - n_full3 = n_full * 3; - n_final = srclen - n_full3; - DUK_ASSERT_DISABLE(n_final >= 0); - DUK_ASSERT(n_final <= 2); - - src_end_fast = src + n_full3; - while (DUK_UNLIKELY(src != src_end_fast)) { - t = (duk_uint_t) (*src++); - t = (t << 8) + (duk_uint_t) (*src++); - t = (t << 8) + (duk_uint_t) (*src++); - - *dst++ = duk_base64_enctab[t >> 18]; - *dst++ = duk_base64_enctab[(t >> 12) & 0x3f]; - *dst++ = duk_base64_enctab[(t >> 6) & 0x3f]; - *dst++ = duk_base64_enctab[t & 0x3f]; - -#if 0 /* Tested: not faster on x64 */ - /* aaaaaabb bbbbcccc ccdddddd */ - dst[0] = duk_base64_enctab[(src[0] >> 2) & 0x3f]; - dst[1] = duk_base64_enctab[((src[0] << 4) & 0x30) | ((src[1] >> 4) & 0x0f)]; - dst[2] = duk_base64_enctab[((src[1] << 2) & 0x3f) | ((src[2] >> 6) & 0x03)]; - dst[3] = duk_base64_enctab[src[2] & 0x3f]; - src += 3; dst += 4; -#endif - } - - switch (n_final) { - /* case 0: nop */ - case 1: { - /* XX== */ - t = (duk_uint_t) (*src++); - *dst++ = duk_base64_enctab[t >> 2]; /* XXXXXX-- */ - *dst++ = duk_base64_enctab[(t << 4) & 0x3f]; /* ------XX */ - *dst++ = DUK_ASC_EQUALS; - *dst++ = DUK_ASC_EQUALS; - break; - } - case 2: { - /* XXX= */ - t = (duk_uint_t) (*src++); - t = (t << 8) + (duk_uint_t) (*src++); - *dst++ = duk_base64_enctab[t >> 10]; /* XXXXXX-- -------- */ - *dst++ = duk_base64_enctab[(t >> 4) & 0x3f]; /* ------XX XXXX---- */ - *dst++ = duk_base64_enctab[(t << 2) & 0x3f]; /* -------- ----XXXX */ - *dst++ = DUK_ASC_EQUALS; - break; - } - } -} -#else /* DUK_USE_BASE64_FASTPATH */ -DUK_LOCAL void duk__base64_encode_helper(const duk_uint8_t *src, duk_size_t srclen, duk_uint8_t *dst) { - duk_small_uint_t i, snip; - duk_uint_t t; - duk_uint_fast8_t x, y; - const duk_uint8_t *src_end; - - src_end = src + srclen; - - while (src < src_end) { - /* read 3 bytes into 't', padded by zero */ - snip = 4; - t = 0; - for (i = 0; i < 3; i++) { - t = t << 8; - if (src >= src_end) { - snip--; - } else { - t += (duk_uint_t) (*src++); - } - } - - /* - * Missing bytes snip base64 example - * 0 4 XXXX - * 1 3 XXX= - * 2 2 XX== - */ - - DUK_ASSERT(snip >= 2 && snip <= 4); - - for (i = 0; i < 4; i++) { - x = (duk_uint_fast8_t) ((t >> 18) & 0x3f); - t = t << 6; - - /* A straightforward 64-byte lookup would be faster - * and cleaner, but this is shorter. - */ - if (i >= snip) { - y = '='; - } else if (x <= 25) { - y = x + 'A'; - } else if (x <= 51) { - y = x - 26 + 'a'; - } else if (x <= 61) { - y = x - 52 + '0'; - } else if (x == 62) { - y = '+'; - } else { - y = '/'; - } - - *dst++ = (duk_uint8_t) y; - } - } -} -#endif /* DUK_USE_BASE64_FASTPATH */ - -#if defined(DUK_USE_BASE64_FASTPATH) -DUK_LOCAL duk_bool_t duk__base64_decode_helper(const duk_uint8_t *src, duk_size_t srclen, duk_uint8_t *dst, duk_uint8_t **out_dst_final) { - duk_int_t x; - duk_int_t t; - duk_small_uint_t n_equal; - duk_small_uint_t n_chars; - const duk_uint8_t *src_end; - const duk_uint8_t *src_end_safe; - - src_end = src + srclen; - src_end_safe = src_end - 4; /* if 'src < src_end_safe', safe to read 4 bytes */ - - /* Innermost fast path processes 4 valid base-64 characters at a time - * but bails out on whitespace, padding chars ('=') and invalid chars. - * Once the slow path segment has been processed, we return to the - * inner fast path again. This handles e.g. base64 with newlines - * reasonably well because the majority of a line is in the fast path. - */ - for (;;) { - /* Fast path, handle units with just actual encoding characters. */ - - while (src <= src_end_safe) { - /* The lookup byte is intentionally sign extended to (at least) - * 32 bits and then ORed. This ensures that is at least 1 byte - * is negative, the highest bit of 't' will be set at the end - * and we don't need to check every byte. - */ - DUK_DDD(DUK_DDDPRINT("fast loop: src=%p, src_end_safe=%p, src_end=%p", - (const void *) src, (const void *) src_end_safe, (const void *) src_end)); - - t = (duk_int_t) duk_base64_dectab[*src++]; - t = (t << 6) | (duk_int_t) duk_base64_dectab[*src++]; - t = (t << 6) | (duk_int_t) duk_base64_dectab[*src++]; - t = (t << 6) | (duk_int_t) duk_base64_dectab[*src++]; - - if (DUK_UNLIKELY(t < 0)) { - DUK_DDD(DUK_DDDPRINT("fast loop unit was not clean, process one slow path unit")); - src -= 4; - break; - } - - DUK_ASSERT(t <= 0xffffffL); - DUK_ASSERT((t >> 24) == 0); - *dst++ = (duk_uint8_t) (t >> 16); - *dst++ = (duk_uint8_t) ((t >> 8) & 0xff); - *dst++ = (duk_uint8_t) (t & 0xff); - } - - /* Handle one slow path unit (or finish if we're done). */ - - n_equal = 0; - n_chars = 0; - t = 0; - for (;;) { - DUK_DDD(DUK_DDDPRINT("slow loop: src=%p, src_end=%p, n_chars=%ld, n_equal=%ld, t=%ld", - (const void *) src, (const void *) src_end, (long) n_chars, (long) n_equal, (long) t)); - - if (DUK_UNLIKELY(src >= src_end)) { - goto done; /* two level break */ - } - - x = duk_base64_dectab[*src++]; - if (DUK_UNLIKELY(x < 0)) { - if (x == -2) { - continue; /* allowed ascii whitespace */ - } else if (x == -3) { - n_equal++; - t <<= 6; - } else { - DUK_ASSERT(x == -1); - goto error; - } - } else { - DUK_ASSERT(x >= 0 && x <= 63); - if (n_equal > 0) { - /* Don't allow actual chars after equal sign. */ - goto error; - } - t = (t << 6) + x; - } - - if (DUK_UNLIKELY(n_chars == 3)) { - /* Emit 3 bytes and backtrack if there was padding. There's - * always space for the whole 3 bytes so no check needed. - */ - DUK_ASSERT(t <= 0xffffffL); - DUK_ASSERT((t >> 24) == 0); - *dst++ = (duk_uint8_t) (t >> 16); - *dst++ = (duk_uint8_t) ((t >> 8) & 0xff); - *dst++ = (duk_uint8_t) (t & 0xff); - - if (DUK_UNLIKELY(n_equal > 0)) { - DUK_ASSERT(n_equal <= 4); - - /* There may be whitespace between the equal signs. */ - if (n_equal == 1) { - /* XXX= */ - dst -= 1; - } else if (n_equal == 2) { - /* XX== */ - dst -= 2; - } else { - goto error; /* invalid padding */ - } - - /* Continue parsing after padding, allows concatenated, - * padded base64. - */ - } - break; /* back to fast loop */ - } else { - n_chars++; - } - } - } - done: - DUK_DDD(DUK_DDDPRINT("done; src=%p, src_end=%p, n_chars=%ld", - (const void *) src, (const void *) src_end, (long) n_chars)); - - DUK_ASSERT(src == src_end); - - if (n_chars != 0) { - /* Here we'd have the option of decoding unpadded base64 - * (e.g. "xxxxyy" instead of "xxxxyy==". Currently not - * accepted. - */ - goto error; - } - - *out_dst_final = dst; - return 1; - - error: - return 0; -} -#else /* DUK_USE_BASE64_FASTPATH */ -DUK_LOCAL duk_bool_t duk__base64_decode_helper(const duk_uint8_t *src, duk_size_t srclen, duk_uint8_t *dst, duk_uint8_t **out_dst_final) { - duk_uint_t t; - duk_uint_fast8_t x, y; - duk_small_uint_t group_idx; - duk_small_uint_t n_equal; - const duk_uint8_t *src_end; - - src_end = src + srclen; - t = 0; - group_idx = 0; - n_equal = 0; - - while (src < src_end) { - x = *src++; - - if (x >= 'A' && x <= 'Z') { - y = x - 'A' + 0; - } else if (x >= 'a' && x <= 'z') { - y = x - 'a' + 26; - } else if (x >= '0' && x <= '9') { - y = x - '0' + 52; - } else if (x == '+') { - y = 62; - } else if (x == '/') { - y = 63; - } else if (x == '=') { - /* We don't check the zero padding bytes here right now - * (that they're actually zero). This seems to be common - * behavior for base-64 decoders. - */ - - n_equal++; - t <<= 6; /* shift in zeroes */ - goto skip_add; - } else if (x == 0x09 || x == 0x0a || x == 0x0d || x == 0x20) { - /* allow basic ASCII whitespace */ - continue; - } else { - goto error; - } - - if (n_equal > 0) { - /* Don't allow mixed padding and actual chars. */ - goto error; - } - t = (t << 6) + y; - skip_add: - - if (group_idx == 3) { - /* output 3 bytes from 't' */ - *dst++ = (duk_uint8_t) ((t >> 16) & 0xff); - *dst++ = (duk_uint8_t) ((t >> 8) & 0xff); - *dst++ = (duk_uint8_t) (t & 0xff); - - if (DUK_UNLIKELY(n_equal > 0)) { - /* Backtrack. */ - DUK_ASSERT(n_equal <= 4); - if (n_equal == 1) { - dst -= 1; - } else if (n_equal == 2) { - dst -= 2; - } else { - goto error; /* invalid padding */ - } - - /* Here we can choose either to end parsing and ignore - * whatever follows, or to continue parsing in case - * multiple (possibly padded) base64 strings have been - * concatenated. Currently, keep on parsing. - */ - n_equal = 0; - } - - t = 0; - group_idx = 0; - } else { - group_idx++; - } - } - - if (group_idx != 0) { - /* Here we'd have the option of decoding unpadded base64 - * (e.g. "xxxxyy" instead of "xxxxyy==". Currently not - * accepted. - */ - goto error; - } - - *out_dst_final = dst; - return 1; - - error: - return 0; -} -#endif /* DUK_USE_BASE64_FASTPATH */ - -DUK_EXTERNAL const char *duk_base64_encode(duk_context *ctx, duk_idx_t index) { - duk_hthread *thr = (duk_hthread *) ctx; - const duk_uint8_t *src; - duk_size_t srclen; - duk_size_t dstlen; - duk_uint8_t *dst; - const char *ret; - - DUK_ASSERT_CTX_VALID(ctx); - - /* XXX: optimize for string inputs: no need to coerce to a buffer - * which makes a copy of the input. - */ - - index = duk_require_normalize_index(ctx, index); - src = duk__prep_codec_arg(ctx, index, &srclen); - /* Note: for srclen=0, src may be NULL */ - - /* Computation must not wrap; this limit works for 32-bit size_t: - * >>> srclen = 3221225469 - * >>> '%x' % ((srclen + 2) / 3 * 4) - * 'fffffffc' - */ - if (srclen > 3221225469UL) { - goto type_error; - } - dstlen = (srclen + 2) / 3 * 4; - dst = (duk_uint8_t *) duk_push_fixed_buffer(ctx, dstlen); - - duk__base64_encode_helper((const duk_uint8_t *) src, srclen, dst); - - ret = duk_to_string(ctx, -1); - duk_replace(ctx, index); - return ret; - - type_error: - DUK_ERROR_TYPE(thr, DUK_STR_ENCODE_FAILED); - return NULL; /* never here */ -} - -DUK_EXTERNAL void duk_base64_decode(duk_context *ctx, duk_idx_t index) { - duk_hthread *thr = (duk_hthread *) ctx; - const duk_uint8_t *src; - duk_size_t srclen; - duk_size_t dstlen; - duk_uint8_t *dst; - duk_uint8_t *dst_final; - duk_bool_t retval; - - DUK_ASSERT_CTX_VALID(ctx); - - /* XXX: optimize for buffer inputs: no need to coerce to a string - * which causes an unnecessary interning. - */ - - index = duk_require_normalize_index(ctx, index); - src = duk__prep_codec_arg(ctx, index, &srclen); - - /* Computation must not wrap, only srclen + 3 is at risk of - * wrapping because after that the number gets smaller. - * This limit works for 32-bit size_t: - * 0x100000000 - 3 - 1 = 4294967292 - */ - if (srclen > 4294967292UL) { - goto type_error; - } - dstlen = (srclen + 3) / 4 * 3; /* upper limit, assuming no whitespace etc */ - dst = (duk_uint8_t *) duk_push_dynamic_buffer(ctx, dstlen); - /* Note: for dstlen=0, dst may be NULL */ - - retval = duk__base64_decode_helper((const duk_uint8_t *) src, srclen, dst, &dst_final); - if (!retval) { - goto type_error; - } - - /* XXX: convert to fixed buffer? */ - (void) duk_resize_buffer(ctx, -1, (duk_size_t) (dst_final - dst)); - duk_replace(ctx, index); - return; - - type_error: - DUK_ERROR_TYPE(thr, DUK_STR_DECODE_FAILED); -} - -DUK_EXTERNAL const char *duk_hex_encode(duk_context *ctx, duk_idx_t index) { - const duk_uint8_t *inp; - duk_size_t len; - duk_size_t i; - duk_uint8_t *buf; - const char *ret; -#if defined(DUK_USE_HEX_FASTPATH) - duk_size_t len_safe; - duk_uint16_t *p16; -#endif - - DUK_ASSERT_CTX_VALID(ctx); - - index = duk_require_normalize_index(ctx, index); - inp = duk__prep_codec_arg(ctx, index, &len); - DUK_ASSERT(inp != NULL || len == 0); - - /* Fixed buffer, no zeroing because we'll fill all the data. */ - buf = (duk_uint8_t *) duk_push_buffer_raw(ctx, len * 2, DUK_BUF_FLAG_NOZERO /*flags*/); - DUK_ASSERT(buf != NULL); - -#if defined(DUK_USE_HEX_FASTPATH) - DUK_ASSERT((((duk_size_t) buf) & 0x01U) == 0); /* pointer is aligned, guaranteed for fixed buffer */ - p16 = (duk_uint16_t *) (void *) buf; - len_safe = len & ~0x03U; - for (i = 0; i < len_safe; i += 4) { - p16[0] = duk_hex_enctab[inp[i]]; - p16[1] = duk_hex_enctab[inp[i + 1]]; - p16[2] = duk_hex_enctab[inp[i + 2]]; - p16[3] = duk_hex_enctab[inp[i + 3]]; - p16 += 4; - } - for (; i < len; i++) { - *p16++ = duk_hex_enctab[inp[i]]; - } -#else /* DUK_USE_HEX_FASTPATH */ - for (i = 0; i < len; i++) { - duk_small_uint_t t; - t = (duk_small_uint_t) inp[i]; - buf[i*2 + 0] = duk_lc_digits[t >> 4]; - buf[i*2 + 1] = duk_lc_digits[t & 0x0f]; - } -#endif /* DUK_USE_HEX_FASTPATH */ - - /* XXX: Using a string return value forces a string intern which is - * not always necessary. As a rough performance measure, hex encode - * time for tests/perf/test-hex-encode.js dropped from ~35s to ~15s - * without string coercion. Change to returning a buffer and let the - * caller coerce to string if necessary? - */ - - ret = duk_to_string(ctx, -1); - duk_replace(ctx, index); - return ret; -} - -DUK_EXTERNAL void duk_hex_decode(duk_context *ctx, duk_idx_t index) { - duk_hthread *thr = (duk_hthread *) ctx; - const duk_uint8_t *inp; - duk_size_t len; - duk_size_t i; - duk_int_t t; - duk_uint8_t *buf; -#if defined(DUK_USE_HEX_FASTPATH) - duk_int_t chk; - duk_uint8_t *p; - duk_size_t len_safe; -#endif - - DUK_ASSERT_CTX_VALID(ctx); - - index = duk_require_normalize_index(ctx, index); - inp = duk__prep_codec_arg(ctx, index, &len); - DUK_ASSERT(inp != NULL || len == 0); - - if (len & 0x01) { - goto type_error; - } - - /* Fixed buffer, no zeroing because we'll fill all the data. */ - buf = (duk_uint8_t *) duk_push_buffer_raw(ctx, len / 2, DUK_BUF_FLAG_NOZERO /*flags*/); - DUK_ASSERT(buf != NULL); - -#if defined(DUK_USE_HEX_FASTPATH) - p = buf; - len_safe = len & ~0x07U; - for (i = 0; i < len_safe; i += 8) { - t = ((duk_int_t) duk_hex_dectab_shift4[inp[i]]) | - ((duk_int_t) duk_hex_dectab[inp[i + 1]]); - chk = t; - p[0] = (duk_uint8_t) t; - t = ((duk_int_t) duk_hex_dectab_shift4[inp[i + 2]]) | - ((duk_int_t) duk_hex_dectab[inp[i + 3]]); - chk |= t; - p[1] = (duk_uint8_t) t; - t = ((duk_int_t) duk_hex_dectab_shift4[inp[i + 4]]) | - ((duk_int_t) duk_hex_dectab[inp[i + 5]]); - chk |= t; - p[2] = (duk_uint8_t) t; - t = ((duk_int_t) duk_hex_dectab_shift4[inp[i + 6]]) | - ((duk_int_t) duk_hex_dectab[inp[i + 7]]); - chk |= t; - p[3] = (duk_uint8_t) t; - p += 4; - - /* Check if any lookup above had a negative result. */ - if (DUK_UNLIKELY(chk < 0)) { - goto type_error; - } - } - for (; i < len; i += 2) { - t = (((duk_int_t) duk_hex_dectab[inp[i]]) << 4) | - ((duk_int_t) duk_hex_dectab[inp[i + 1]]); - if (DUK_UNLIKELY(t < 0)) { - goto type_error; - } - *p++ = (duk_uint8_t) t; - } -#else /* DUK_USE_HEX_FASTPATH */ - for (i = 0; i < len; i += 2) { - /* For invalid characters the value -1 gets extended to - * at least 16 bits. If either nybble is invalid, the - * resulting 't' will be < 0. - */ - t = (((duk_int_t) duk_hex_dectab[inp[i]]) << 4) | - ((duk_int_t) duk_hex_dectab[inp[i + 1]]); - if (DUK_UNLIKELY(t < 0)) { - goto type_error; - } - buf[i >> 1] = (duk_uint8_t) t; - } -#endif /* DUK_USE_HEX_FASTPATH */ - - duk_replace(ctx, index); - return; - - type_error: - DUK_ERROR_TYPE(thr, DUK_STR_DECODE_FAILED); -} - -DUK_EXTERNAL const char *duk_json_encode(duk_context *ctx, duk_idx_t index) { -#ifdef DUK_USE_ASSERTIONS - duk_idx_t top_at_entry; -#endif - const char *ret; - - DUK_ASSERT_CTX_VALID(ctx); -#ifdef DUK_USE_ASSERTIONS - top_at_entry = duk_get_top(ctx); -#endif - - index = duk_require_normalize_index(ctx, index); - duk_bi_json_stringify_helper(ctx, - index /*idx_value*/, - DUK_INVALID_INDEX /*idx_replacer*/, - DUK_INVALID_INDEX /*idx_space*/, - 0 /*flags*/); - DUK_ASSERT(duk_is_string(ctx, -1)); - duk_replace(ctx, index); - ret = duk_get_string(ctx, index); - - DUK_ASSERT(duk_get_top(ctx) == top_at_entry); - - return ret; -} - -DUK_EXTERNAL void duk_json_decode(duk_context *ctx, duk_idx_t index) { -#ifdef DUK_USE_ASSERTIONS - duk_idx_t top_at_entry; -#endif - - DUK_ASSERT_CTX_VALID(ctx); -#ifdef DUK_USE_ASSERTIONS - top_at_entry = duk_get_top(ctx); -#endif - - index = duk_require_normalize_index(ctx, index); - duk_bi_json_parse_helper(ctx, - index /*idx_value*/, - DUK_INVALID_INDEX /*idx_reviver*/, - 0 /*flags*/); - duk_replace(ctx, index); - - DUK_ASSERT(duk_get_top(ctx) == top_at_entry); -} -#line 1 "duk_api_compile.c" -/* - * Compilation and evaluation - */ - -/* include removed: duk_internal.h */ - -typedef struct duk__compile_raw_args duk__compile_raw_args; -struct duk__compile_raw_args { - duk_size_t src_length; /* should be first on 64-bit platforms */ - const duk_uint8_t *src_buffer; - duk_uint_t flags; -}; - -/* Eval is just a wrapper now. */ -DUK_EXTERNAL duk_int_t duk_eval_raw(duk_context *ctx, const char *src_buffer, duk_size_t src_length, duk_uint_t flags) { - duk_uint_t comp_flags; - duk_int_t rc; - - DUK_ASSERT_CTX_VALID(ctx); - - /* Note: strictness is *not* inherited from the current Duktape/C. - * This would be confusing because the current strictness state - * depends on whether we're running inside a Duktape/C activation - * (= strict mode) or outside of any activation (= non-strict mode). - * See tests/api/test-eval-strictness.c for more discussion. - */ - - /* [ ... source? filename? ] (depends on flags) */ - - comp_flags = flags; - comp_flags |= DUK_COMPILE_EVAL; - rc = duk_compile_raw(ctx, src_buffer, src_length, comp_flags); /* may be safe, or non-safe depending on flags */ - - /* [ ... closure/error ] */ - - if (rc != DUK_EXEC_SUCCESS) { - rc = DUK_EXEC_ERROR; - goto got_rc; - } - - duk_push_global_object(ctx); /* explicit 'this' binding, see GH-164 */ - - if (flags & DUK_COMPILE_SAFE) { - rc = duk_pcall_method(ctx, 0); - } else { - duk_call_method(ctx, 0); - rc = DUK_EXEC_SUCCESS; - } - - /* [ ... result/error ] */ - - got_rc: - if (flags & DUK_COMPILE_NORESULT) { - duk_pop(ctx); - } - - return rc; -} - -/* Helper which can be called both directly and with duk_safe_call(). */ -DUK_LOCAL duk_ret_t duk__do_compile(duk_context *ctx) { - duk_hthread *thr = (duk_hthread *) ctx; - duk__compile_raw_args *comp_args; - duk_uint_t flags; - duk_small_uint_t comp_flags; - duk_hcompiledfunction *h_templ; - - DUK_ASSERT_CTX_VALID(ctx); - - /* Note: strictness is not inherited from the current Duktape/C - * context. Otherwise it would not be possible to compile - * non-strict code inside a Duktape/C activation (which is - * always strict now). See tests/api/test-eval-strictness.c - * for discussion. - */ - - /* [ ... source? filename? &comp_args ] (depends on flags) */ - - comp_args = (duk__compile_raw_args *) duk_require_pointer(ctx, -1); - flags = comp_args->flags; - duk_pop(ctx); - - /* [ ... source? filename? ] */ - - if (flags & DUK_COMPILE_NOFILENAME) { - /* Automatic filename: 'eval' or 'input'. */ - duk_push_hstring_stridx(ctx, (flags & DUK_COMPILE_EVAL) ? DUK_STRIDX_EVAL : DUK_STRIDX_INPUT); - } - - /* [ ... source? filename ] */ - - if (!comp_args->src_buffer) { - duk_hstring *h_sourcecode; - - h_sourcecode = duk_get_hstring(ctx, -2); - if ((flags & DUK_COMPILE_NOSOURCE) || /* args incorrect */ - (h_sourcecode == NULL)) { /* e.g. duk_push_string_file_raw() pushed undefined */ - /* XXX: when this error is caused by a nonexistent - * file given to duk_peval_file() or similar, the - * error message is not the best possible. - */ - DUK_ERROR_API(thr, DUK_STR_NO_SOURCECODE); - } - DUK_ASSERT(h_sourcecode != NULL); - comp_args->src_buffer = (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h_sourcecode); - comp_args->src_length = (duk_size_t) DUK_HSTRING_GET_BYTELEN(h_sourcecode); - } - DUK_ASSERT(comp_args->src_buffer != NULL); - - /* XXX: unnecessary translation of flags */ - comp_flags = 0; - if (flags & DUK_COMPILE_EVAL) { - comp_flags |= DUK_JS_COMPILE_FLAG_EVAL; - } - if (flags & DUK_COMPILE_FUNCTION) { - comp_flags |= DUK_JS_COMPILE_FLAG_EVAL | - DUK_JS_COMPILE_FLAG_FUNCEXPR; - } - if (flags & DUK_COMPILE_STRICT) { - comp_flags |= DUK_JS_COMPILE_FLAG_STRICT; - } - - /* [ ... source? filename ] */ - - duk_js_compile(thr, comp_args->src_buffer, comp_args->src_length, comp_flags); - - /* [ ... source? func_template ] */ - - if (flags & DUK_COMPILE_NOSOURCE) { - ; - } else { - duk_remove(ctx, -2); - } - - /* [ ... func_template ] */ - - h_templ = (duk_hcompiledfunction *) duk_get_hobject(ctx, -1); - DUK_ASSERT(h_templ != NULL); - duk_js_push_closure(thr, - h_templ, - thr->builtins[DUK_BIDX_GLOBAL_ENV], - thr->builtins[DUK_BIDX_GLOBAL_ENV], - 1 /*add_auto_proto*/); - duk_remove(ctx, -2); /* -> [ ... closure ] */ - - /* [ ... closure ] */ - - return 1; -} - -DUK_EXTERNAL duk_int_t duk_compile_raw(duk_context *ctx, const char *src_buffer, duk_size_t src_length, duk_uint_t flags) { - duk__compile_raw_args comp_args_alloc; - duk__compile_raw_args *comp_args = &comp_args_alloc; - - DUK_ASSERT_CTX_VALID(ctx); - - if ((flags & DUK_COMPILE_STRLEN) && (src_buffer != NULL)) { - /* String length is computed here to avoid multiple evaluation - * of a macro argument in the calling side. - */ - src_length = DUK_STRLEN(src_buffer); - } - - comp_args->src_buffer = (const duk_uint8_t *) src_buffer; - comp_args->src_length = src_length; - comp_args->flags = flags; - duk_push_pointer(ctx, (void *) comp_args); - - /* [ ... source? filename? &comp_args ] (depends on flags) */ - - if (flags & DUK_COMPILE_SAFE) { - duk_int_t rc; - duk_int_t nargs; - duk_int_t nrets = 1; - - /* Arguments can be: [ source? filename? &comp_args] so that - * nargs is 1 to 3. Call site encodes the correct nargs count - * directly into flags. - */ - nargs = flags & 0x07; - DUK_ASSERT(nargs == (1 + - ((flags & DUK_COMPILE_NOSOURCE) ? 0 : 1) + - ((flags & DUK_COMPILE_NOFILENAME) ? 0 : 1))); - rc = duk_safe_call(ctx, duk__do_compile, nargs, nrets); - - /* [ ... closure ] */ - return rc; - } - - (void) duk__do_compile(ctx); - - /* [ ... closure ] */ - return DUK_EXEC_SUCCESS; -} -#line 1 "duk_api_debug.c" -/* - * Debugging related API calls - */ - -/* include removed: duk_internal.h */ - -DUK_EXTERNAL void duk_push_context_dump(duk_context *ctx) { - duk_idx_t idx; - duk_idx_t top; - - DUK_ASSERT_CTX_VALID(ctx); - - /* We don't duk_require_stack() here now, but rely on the caller having - * enough space. - */ - - top = duk_get_top(ctx); - duk_push_array(ctx); - for (idx = 0; idx < top; idx++) { - duk_dup(ctx, idx); - duk_put_prop_index(ctx, -2, idx); - } - - /* XXX: conversion errors should not propagate outwards. - * Perhaps values need to be coerced individually? - */ - duk_bi_json_stringify_helper(ctx, - duk_get_top_index(ctx), /*idx_value*/ - DUK_INVALID_INDEX, /*idx_replacer*/ - DUK_INVALID_INDEX, /*idx_space*/ - DUK_JSON_FLAG_EXT_CUSTOM | - DUK_JSON_FLAG_ASCII_ONLY | - DUK_JSON_FLAG_AVOID_KEY_QUOTES /*flags*/); - - duk_push_sprintf(ctx, "ctx: top=%ld, stack=%s", (long) top, (const char *) duk_safe_to_string(ctx, -1)); - duk_replace(ctx, -3); /* [ ... arr jsonx(arr) res ] -> [ ... res jsonx(arr) ] */ - duk_pop(ctx); - DUK_ASSERT(duk_is_string(ctx, -1)); -} - -#if defined(DUK_USE_DEBUGGER_SUPPORT) - -DUK_EXTERNAL void duk_debugger_attach_custom(duk_context *ctx, - duk_debug_read_function read_cb, - duk_debug_write_function write_cb, - duk_debug_peek_function peek_cb, - duk_debug_read_flush_function read_flush_cb, - duk_debug_write_flush_function write_flush_cb, - duk_debug_request_function request_cb, - duk_debug_detached_function detached_cb, - void *udata) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_heap *heap; - const char *str; - duk_size_t len; - - /* XXX: should there be an error or an automatic detach if - * already attached? - */ - - DUK_D(DUK_DPRINT("application called duk_debugger_attach()")); - - DUK_ASSERT_CTX_VALID(ctx); - DUK_ASSERT(read_cb != NULL); - DUK_ASSERT(write_cb != NULL); - /* Other callbacks are optional. */ - - heap = thr->heap; - heap->dbg_read_cb = read_cb; - heap->dbg_write_cb = write_cb; - heap->dbg_peek_cb = peek_cb; - heap->dbg_read_flush_cb = read_flush_cb; - heap->dbg_write_flush_cb = write_flush_cb; - heap->dbg_request_cb = request_cb; - heap->dbg_detached_cb = detached_cb; - heap->dbg_udata = udata; - heap->dbg_have_next_byte = 0; - - /* Start in paused state. */ - heap->dbg_processing = 0; - heap->dbg_paused = 1; - heap->dbg_state_dirty = 1; - heap->dbg_force_restart = 0; - heap->dbg_step_type = 0; - heap->dbg_step_thread = NULL; - heap->dbg_step_csindex = 0; - heap->dbg_step_startline = 0; - heap->dbg_exec_counter = 0; - heap->dbg_last_counter = 0; - heap->dbg_last_time = 0.0; - - /* Send version identification and flush right afterwards. Note that - * we must write raw, unframed bytes here. - */ - duk_push_sprintf(ctx, "%ld %ld %s %s\n", - (long) DUK_DEBUG_PROTOCOL_VERSION, - (long) DUK_VERSION, - (const char *) DUK_GIT_DESCRIBE, - (const char *) DUK_USE_TARGET_INFO); - str = duk_get_lstring(ctx, -1, &len); - DUK_ASSERT(str != NULL); - duk_debug_write_bytes(thr, (const duk_uint8_t *) str, len); - duk_debug_write_flush(thr); - duk_pop(ctx); -} - -DUK_EXTERNAL void duk_debugger_detach(duk_context *ctx) { - duk_hthread *thr; - - DUK_D(DUK_DPRINT("application called duk_debugger_detach()")); - - DUK_ASSERT_CTX_VALID(ctx); - thr = (duk_hthread *) ctx; - DUK_ASSERT(thr != NULL); - DUK_ASSERT(thr->heap != NULL); - - /* Can be called multiple times with no harm. */ - duk_debug_do_detach(thr->heap); -} - -DUK_EXTERNAL void duk_debugger_cooperate(duk_context *ctx) { - duk_hthread *thr; - duk_bool_t processed_messages; - - DUK_ASSERT_CTX_VALID(ctx); - thr = (duk_hthread *) ctx; - DUK_ASSERT(thr != NULL); - DUK_ASSERT(thr->heap != NULL); - - if (!DUK_HEAP_IS_DEBUGGER_ATTACHED(thr->heap)) { - return; - } - if (thr->callstack_top > 0 || thr->heap->dbg_processing) { - /* Calling duk_debugger_cooperate() while Duktape is being - * called into is not supported. This is not a 100% check - * but prevents any damage in most cases. - */ - return; - } - - processed_messages = duk_debug_process_messages(thr, 1 /*no_block*/); - DUK_UNREF(processed_messages); -} - -DUK_EXTERNAL duk_bool_t duk_debugger_notify(duk_context *ctx, duk_idx_t nvalues) { - duk_hthread *thr; - duk_idx_t top; - duk_idx_t idx; - duk_bool_t ret = 0; - - DUK_ASSERT_CTX_VALID(ctx); - thr = (duk_hthread *) ctx; - DUK_ASSERT(thr != NULL); - DUK_ASSERT(thr->heap != NULL); - - DUK_D(DUK_DPRINT("application called duk_debugger_notify() with nvalues=%ld", (long) nvalues)); - - top = duk_get_top(ctx); - if (top < nvalues) { - DUK_ERROR_API(thr, "not enough stack values for notify"); - return ret; /* unreachable */ - } - if (DUK_HEAP_IS_DEBUGGER_ATTACHED(thr->heap)) { - duk_debug_write_notify(thr, DUK_DBG_CMD_APPNOTIFY); - for (idx = top - nvalues; idx < top; idx++) { - duk_tval *tv = DUK_GET_TVAL_POSIDX(ctx, idx); - duk_debug_write_tval(thr, tv); - } - duk_debug_write_eom(thr); - - /* Return non-zero (true) if we have a good reason to believe - * the notify was delivered; if we're still attached at least - * a transport error was not indicated by the transport write - * callback. This is not a 100% guarantee of course. - */ - if (DUK_HEAP_IS_DEBUGGER_ATTACHED(thr->heap)) { - ret = 1; - } - } - duk_pop_n(ctx, nvalues); - return ret; -} - -DUK_EXTERNAL void duk_debugger_pause(duk_context *ctx) { - duk_hthread *thr; - - DUK_ASSERT_CTX_VALID(ctx); - thr = (duk_hthread *) ctx; - DUK_ASSERT(thr != NULL); - DUK_ASSERT(thr->heap != NULL); - - DUK_D(DUK_DPRINT("application called duk_debugger_pause()")); - - /* Treat like a debugger statement: ignore when not attached. */ - if (DUK_HEAP_IS_DEBUGGER_ATTACHED(thr->heap)) { - DUK_HEAP_SET_PAUSED(thr->heap); - - /* Pause on the next opcode executed. This is always safe to do even - * inside the debugger message loop: the interrupt counter will be reset - * to its proper value when the message loop exits. - */ - thr->interrupt_init = 1; - thr->interrupt_counter = 0; - } -} - -#else /* DUK_USE_DEBUGGER_SUPPORT */ - -DUK_EXTERNAL void duk_debugger_attach_custom(duk_context *ctx, - duk_debug_read_function read_cb, - duk_debug_write_function write_cb, - duk_debug_peek_function peek_cb, - duk_debug_read_flush_function read_flush_cb, - duk_debug_write_flush_function write_flush_cb, - duk_debug_request_function request_cb, - duk_debug_detached_function detached_cb, - void *udata) { - DUK_ASSERT_CTX_VALID(ctx); - DUK_UNREF(read_cb); - DUK_UNREF(write_cb); - DUK_UNREF(peek_cb); - DUK_UNREF(read_flush_cb); - DUK_UNREF(write_flush_cb); - DUK_UNREF(request_cb); - DUK_UNREF(detached_cb); - DUK_UNREF(udata); - DUK_ERROR_API((duk_hthread *) ctx, "no debugger support"); -} - -DUK_EXTERNAL void duk_debugger_detach(duk_context *ctx) { - DUK_ASSERT_CTX_VALID(ctx); - DUK_ERROR_API((duk_hthread *) ctx, "no debugger support"); -} - -DUK_EXTERNAL void duk_debugger_cooperate(duk_context *ctx) { - /* nop */ - DUK_ASSERT_CTX_VALID(ctx); - DUK_UNREF(ctx); -} - -DUK_EXTERNAL duk_bool_t duk_debugger_notify(duk_context *ctx, duk_idx_t nvalues) { - duk_idx_t top; - - DUK_ASSERT_CTX_VALID(ctx); - - top = duk_get_top(ctx); - if (top < nvalues) { - DUK_ERROR_API((duk_hthread *) ctx, "not enough stack values for notify"); - return 0; /* unreachable */ - } - - /* No debugger support, just pop values. */ - duk_pop_n(ctx, nvalues); - return 0; -} - -DUK_EXTERNAL void duk_debugger_pause(duk_context *ctx) { - /* Treat like debugger statement: nop */ - DUK_ASSERT_CTX_VALID(ctx); - DUK_UNREF(ctx); -} - -#endif /* DUK_USE_DEBUGGER_SUPPORT */ -#line 1 "duk_api_heap.c" -/* - * Heap creation and destruction - */ - -/* include removed: duk_internal.h */ - -DUK_EXTERNAL -duk_context *duk_create_heap(duk_alloc_function alloc_func, - duk_realloc_function realloc_func, - duk_free_function free_func, - void *heap_udata, - duk_fatal_function fatal_handler) { - duk_heap *heap = NULL; - duk_context *ctx; - - /* Assume that either all memory funcs are NULL or non-NULL, mixed - * cases will now be unsafe. - */ - - /* XXX: just assert non-NULL values here and make caller arguments - * do the defaulting to the default implementations (smaller code)? - */ - - if (!alloc_func) { - DUK_ASSERT(realloc_func == NULL); - DUK_ASSERT(free_func == NULL); -#if defined(DUK_USE_PROVIDE_DEFAULT_ALLOC_FUNCTIONS) - alloc_func = duk_default_alloc_function; - realloc_func = duk_default_realloc_function; - free_func = duk_default_free_function; -#else - DUK_D(DUK_DPRINT("no allocation functions given and no default providers")); - return NULL; -#endif - } else { - DUK_ASSERT(realloc_func != NULL); - DUK_ASSERT(free_func != NULL); - } - - if (!fatal_handler) { - fatal_handler = duk_default_fatal_handler; - } - - DUK_ASSERT(alloc_func != NULL); - DUK_ASSERT(realloc_func != NULL); - DUK_ASSERT(free_func != NULL); - DUK_ASSERT(fatal_handler != NULL); - - heap = duk_heap_alloc(alloc_func, realloc_func, free_func, heap_udata, fatal_handler); - if (!heap) { - return NULL; - } - ctx = (duk_context *) heap->heap_thread; - DUK_ASSERT(ctx != NULL); - DUK_ASSERT(((duk_hthread *) ctx)->heap != NULL); - return ctx; -} - -DUK_EXTERNAL void duk_destroy_heap(duk_context *ctx) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_heap *heap; - - if (!ctx) { - return; - } - heap = thr->heap; - DUK_ASSERT(heap != NULL); - - duk_heap_free(heap); -} - -/* XXX: better place for this */ -DUK_EXTERNAL void duk_set_global_object(duk_context *ctx) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_hobject *h_glob; - duk_hobject *h_prev_glob; - duk_hobject *h_env; - duk_hobject *h_prev_env; - - DUK_D(DUK_DPRINT("replace global object with: %!T", duk_get_tval(ctx, -1))); - - h_glob = duk_require_hobject(ctx, -1); - DUK_ASSERT(h_glob != NULL); - - /* - * Replace global object. - */ - - h_prev_glob = thr->builtins[DUK_BIDX_GLOBAL]; - DUK_UNREF(h_prev_glob); - thr->builtins[DUK_BIDX_GLOBAL] = h_glob; - DUK_HOBJECT_INCREF(thr, h_glob); - DUK_HOBJECT_DECREF_ALLOWNULL(thr, h_prev_glob); /* side effects, in theory (referenced by global env) */ - - /* - * Replace lexical environment for global scope - * - * Create a new object environment for the global lexical scope. - * We can't just reset the _Target property of the current one, - * because the lexical scope is shared by other threads with the - * same (initial) built-ins. - */ - - (void) duk_push_object_helper(ctx, - DUK_HOBJECT_FLAG_EXTENSIBLE | - DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_OBJENV), - -1); /* no prototype, updated below */ - - duk_dup(ctx, -2); - duk_dup(ctx, -3); - - /* [ ... new_glob new_env new_glob new_glob ] */ - - duk_xdef_prop_stridx(thr, -3, DUK_STRIDX_INT_TARGET, DUK_PROPDESC_FLAGS_NONE); - duk_xdef_prop_stridx(thr, -2, DUK_STRIDX_INT_THIS, DUK_PROPDESC_FLAGS_NONE); - - /* [ ... new_glob new_env ] */ - - h_env = duk_get_hobject(ctx, -1); - DUK_ASSERT(h_env != NULL); - - h_prev_env = thr->builtins[DUK_BIDX_GLOBAL_ENV]; - thr->builtins[DUK_BIDX_GLOBAL_ENV] = h_env; - DUK_HOBJECT_INCREF(thr, h_env); - DUK_HOBJECT_DECREF_ALLOWNULL(thr, h_prev_env); /* side effects */ - DUK_UNREF(h_env); /* without refcounts */ - DUK_UNREF(h_prev_env); - - /* [ ... new_glob new_env ] */ - - duk_pop_2(ctx); - - /* [ ... ] */ -} -#line 1 "duk_api_logging.c" -/* - * Logging - * - * Current logging primitive is a sprintf-style log which is convenient - * for most C code. Another useful primitive would be to log N arguments - * from value stack (like the Ecmascript binding does). - */ - -/* include removed: duk_internal.h */ - -DUK_EXTERNAL void duk_log_va(duk_context *ctx, duk_int_t level, const char *fmt, va_list ap) { - /* stridx_logfunc[] must be static to allow initializer with old compilers like BCC */ - static const duk_uint16_t stridx_logfunc[6] = { - DUK_STRIDX_LC_TRACE, DUK_STRIDX_LC_DEBUG, DUK_STRIDX_LC_INFO, - DUK_STRIDX_LC_WARN, DUK_STRIDX_LC_ERROR, DUK_STRIDX_LC_FATAL - }; - - DUK_ASSERT_CTX_VALID(ctx); - - if (level < 0) { - level = 0; - } else if (level > (int) (sizeof(stridx_logfunc) / sizeof(duk_uint16_t)) - 1) { - level = (int) (sizeof(stridx_logfunc) / sizeof(duk_uint16_t)) - 1; - } - - duk_push_hobject_bidx(ctx, DUK_BIDX_LOGGER_CONSTRUCTOR); - duk_get_prop_stridx(ctx, -1, DUK_STRIDX_CLOG); - duk_get_prop_stridx(ctx, -1, stridx_logfunc[level]); - duk_dup(ctx, -2); - - /* [ ... Logger clog logfunc clog ] */ - - duk_push_vsprintf(ctx, fmt, ap); - - /* [ ... Logger clog logfunc clog(=this) msg ] */ - - duk_call_method(ctx, 1 /*nargs*/); - - /* [ ... Logger clog res ] */ - - duk_pop_3(ctx); -} - -DUK_EXTERNAL void duk_log(duk_context *ctx, duk_int_t level, const char *fmt, ...) { - va_list ap; - - DUK_ASSERT_CTX_VALID(ctx); - - va_start(ap, fmt); - duk_log_va(ctx, level, fmt, ap); - va_end(ap); -} -#line 1 "duk_api_memory.c" -/* - * Memory calls. - */ - -/* include removed: duk_internal.h */ - -DUK_EXTERNAL void *duk_alloc_raw(duk_context *ctx, duk_size_t size) { - duk_hthread *thr = (duk_hthread *) ctx; - - DUK_ASSERT_CTX_VALID(ctx); - - return DUK_ALLOC_RAW(thr->heap, size); -} - -DUK_EXTERNAL void duk_free_raw(duk_context *ctx, void *ptr) { - duk_hthread *thr = (duk_hthread *) ctx; - - DUK_ASSERT_CTX_VALID(ctx); - - DUK_FREE_RAW(thr->heap, ptr); -} - -DUK_EXTERNAL void *duk_realloc_raw(duk_context *ctx, void *ptr, duk_size_t size) { - duk_hthread *thr = (duk_hthread *) ctx; - - DUK_ASSERT_CTX_VALID(ctx); - - return DUK_REALLOC_RAW(thr->heap, ptr, size); -} - -DUK_EXTERNAL void *duk_alloc(duk_context *ctx, duk_size_t size) { - duk_hthread *thr = (duk_hthread *) ctx; - - DUK_ASSERT_CTX_VALID(ctx); - - return DUK_ALLOC(thr->heap, size); -} - -DUK_EXTERNAL void duk_free(duk_context *ctx, void *ptr) { - duk_hthread *thr = (duk_hthread *) ctx; - - DUK_ASSERT_CTX_VALID(ctx); - - DUK_FREE(thr->heap, ptr); -} - -DUK_EXTERNAL void *duk_realloc(duk_context *ctx, void *ptr, duk_size_t size) { - duk_hthread *thr = (duk_hthread *) ctx; - - DUK_ASSERT_CTX_VALID(ctx); - - /* - * Note: since this is an exposed API call, there should be - * no way a mark-and-sweep could have a side effect on the - * memory allocation behind 'ptr'; the pointer should never - * be something that Duktape wants to change. - * - * Thus, no need to use DUK_REALLOC_INDIRECT (and we don't - * have the storage location here anyway). - */ - - return DUK_REALLOC(thr->heap, ptr, size); -} - -DUK_EXTERNAL void duk_get_memory_functions(duk_context *ctx, duk_memory_functions *out_funcs) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_heap *heap; - - DUK_ASSERT_CTX_VALID(ctx); - DUK_ASSERT(out_funcs != NULL); - DUK_ASSERT(thr != NULL); - DUK_ASSERT(thr->heap != NULL); - - heap = thr->heap; - out_funcs->alloc_func = heap->alloc_func; - out_funcs->realloc_func = heap->realloc_func; - out_funcs->free_func = heap->free_func; - out_funcs->udata = heap->heap_udata; -} - -DUK_EXTERNAL void duk_gc(duk_context *ctx, duk_uint_t flags) { -#ifdef DUK_USE_MARK_AND_SWEEP - duk_hthread *thr = (duk_hthread *) ctx; - duk_heap *heap; - - DUK_UNREF(flags); - - /* NULL accepted */ - if (!ctx) { - return; - } - DUK_ASSERT_CTX_VALID(ctx); - heap = thr->heap; - DUK_ASSERT(heap != NULL); - - DUK_D(DUK_DPRINT("mark-and-sweep requested by application")); - duk_heap_mark_and_sweep(heap, 0); -#else - DUK_D(DUK_DPRINT("mark-and-sweep requested by application but mark-and-sweep not enabled, ignoring")); - DUK_UNREF(ctx); - DUK_UNREF(flags); -#endif -} -#line 1 "duk_api_object.c" -/* - * Object handling: property access and other support functions. - */ - -/* include removed: duk_internal.h */ - -/* - * Property handling - * - * The API exposes only the most common property handling functions. - * The caller can invoke Ecmascript built-ins for full control (e.g. - * defineProperty, getOwnPropertyDescriptor). - */ - -DUK_EXTERNAL duk_bool_t duk_get_prop(duk_context *ctx, duk_idx_t obj_index) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_tval *tv_obj; - duk_tval *tv_key; - duk_bool_t rc; - - DUK_ASSERT_CTX_VALID(ctx); - - /* Note: copying tv_obj and tv_key to locals to shield against a valstack - * resize is not necessary for a property get right now. - */ - - tv_obj = duk_require_tval(ctx, obj_index); - tv_key = duk_require_tval(ctx, -1); - - rc = duk_hobject_getprop(thr, tv_obj, tv_key); - DUK_ASSERT(rc == 0 || rc == 1); - /* a value is left on stack regardless of rc */ - - duk_remove(ctx, -2); /* remove key */ - return rc; /* 1 if property found, 0 otherwise */ -} - -DUK_EXTERNAL duk_bool_t duk_get_prop_string(duk_context *ctx, duk_idx_t obj_index, const char *key) { - DUK_ASSERT_CTX_VALID(ctx); - DUK_ASSERT(key != NULL); - - obj_index = duk_require_normalize_index(ctx, obj_index); - duk_push_string(ctx, key); - return duk_get_prop(ctx, obj_index); -} - -DUK_EXTERNAL duk_bool_t duk_get_prop_index(duk_context *ctx, duk_idx_t obj_index, duk_uarridx_t arr_index) { - DUK_ASSERT_CTX_VALID(ctx); - - obj_index = duk_require_normalize_index(ctx, obj_index); - duk_push_uarridx(ctx, arr_index); - return duk_get_prop(ctx, obj_index); -} - -DUK_INTERNAL duk_bool_t duk_get_prop_stridx(duk_context *ctx, duk_idx_t obj_index, duk_small_int_t stridx) { - duk_hthread *thr = (duk_hthread *) ctx; - - DUK_ASSERT_CTX_VALID(ctx); - DUK_ASSERT_DISABLE(stridx >= 0); - DUK_ASSERT(stridx < DUK_HEAP_NUM_STRINGS); - DUK_UNREF(thr); - - obj_index = duk_require_normalize_index(ctx, obj_index); - duk_push_hstring(ctx, DUK_HTHREAD_GET_STRING(thr, stridx)); - return duk_get_prop(ctx, obj_index); -} - -DUK_INTERNAL duk_bool_t duk_get_prop_stridx_boolean(duk_context *ctx, duk_idx_t obj_index, duk_small_int_t stridx, duk_bool_t *out_has_prop) { - duk_bool_t rc; - - DUK_ASSERT_CTX_VALID(ctx); - DUK_ASSERT_DISABLE(stridx >= 0); - DUK_ASSERT(stridx < DUK_HEAP_NUM_STRINGS); - - rc = duk_get_prop_stridx(ctx, obj_index, stridx); - if (out_has_prop) { - *out_has_prop = rc; - } - rc = duk_to_boolean(ctx, -1); - DUK_ASSERT(rc == 0 || rc == 1); - duk_pop(ctx); - return rc; -} - -DUK_EXTERNAL duk_bool_t duk_put_prop(duk_context *ctx, duk_idx_t obj_index) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_tval *tv_obj; - duk_tval *tv_key; - duk_tval *tv_val; - duk_small_int_t throw_flag; - duk_bool_t rc; - - DUK_ASSERT_CTX_VALID(ctx); - - /* Note: copying tv_obj and tv_key to locals to shield against a valstack - * resize is not necessary for a property put right now (putprop protects - * against it internally). - */ - - tv_obj = duk_require_tval(ctx, obj_index); - tv_key = duk_require_tval(ctx, -2); - tv_val = duk_require_tval(ctx, -1); - throw_flag = duk_is_strict_call(ctx); - - rc = duk_hobject_putprop(thr, tv_obj, tv_key, tv_val, throw_flag); - DUK_ASSERT(rc == 0 || rc == 1); - - duk_pop_2(ctx); /* remove key and value */ - return rc; /* 1 if property found, 0 otherwise */ -} - -DUK_EXTERNAL duk_bool_t duk_put_prop_string(duk_context *ctx, duk_idx_t obj_index, const char *key) { - DUK_ASSERT_CTX_VALID(ctx); - DUK_ASSERT(key != NULL); - - obj_index = duk_require_normalize_index(ctx, obj_index); - duk_push_string(ctx, key); - duk_swap_top(ctx, -2); /* [val key] -> [key val] */ - return duk_put_prop(ctx, obj_index); -} - -DUK_EXTERNAL duk_bool_t duk_put_prop_index(duk_context *ctx, duk_idx_t obj_index, duk_uarridx_t arr_index) { - DUK_ASSERT_CTX_VALID(ctx); - - obj_index = duk_require_normalize_index(ctx, obj_index); - duk_push_uarridx(ctx, arr_index); - duk_swap_top(ctx, -2); /* [val key] -> [key val] */ - return duk_put_prop(ctx, obj_index); -} - -DUK_INTERNAL duk_bool_t duk_put_prop_stridx(duk_context *ctx, duk_idx_t obj_index, duk_small_int_t stridx) { - duk_hthread *thr = (duk_hthread *) ctx; - - DUK_ASSERT_CTX_VALID(ctx); - DUK_ASSERT_DISABLE(stridx >= 0); - DUK_ASSERT(stridx < DUK_HEAP_NUM_STRINGS); - DUK_UNREF(thr); - - obj_index = duk_require_normalize_index(ctx, obj_index); - duk_push_hstring(ctx, DUK_HTHREAD_GET_STRING(thr, stridx)); - duk_swap_top(ctx, -2); /* [val key] -> [key val] */ - return duk_put_prop(ctx, obj_index); -} - -DUK_EXTERNAL duk_bool_t duk_del_prop(duk_context *ctx, duk_idx_t obj_index) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_tval *tv_obj; - duk_tval *tv_key; - duk_small_int_t throw_flag; - duk_bool_t rc; - - DUK_ASSERT_CTX_VALID(ctx); - - /* Note: copying tv_obj and tv_key to locals to shield against a valstack - * resize is not necessary for a property delete right now. - */ - - tv_obj = duk_require_tval(ctx, obj_index); - tv_key = duk_require_tval(ctx, -1); - throw_flag = duk_is_strict_call(ctx); - - rc = duk_hobject_delprop(thr, tv_obj, tv_key, throw_flag); - DUK_ASSERT(rc == 0 || rc == 1); - - duk_pop(ctx); /* remove key */ - return rc; -} - -DUK_EXTERNAL duk_bool_t duk_del_prop_string(duk_context *ctx, duk_idx_t obj_index, const char *key) { - DUK_ASSERT_CTX_VALID(ctx); - DUK_ASSERT(key != NULL); - - obj_index = duk_require_normalize_index(ctx, obj_index); - duk_push_string(ctx, key); - return duk_del_prop(ctx, obj_index); -} - -DUK_EXTERNAL duk_bool_t duk_del_prop_index(duk_context *ctx, duk_idx_t obj_index, duk_uarridx_t arr_index) { - DUK_ASSERT_CTX_VALID(ctx); - - obj_index = duk_require_normalize_index(ctx, obj_index); - duk_push_uarridx(ctx, arr_index); - return duk_del_prop(ctx, obj_index); -} - -DUK_INTERNAL duk_bool_t duk_del_prop_stridx(duk_context *ctx, duk_idx_t obj_index, duk_small_int_t stridx) { - duk_hthread *thr = (duk_hthread *) ctx; - - DUK_ASSERT_CTX_VALID(ctx); - DUK_ASSERT_DISABLE(stridx >= 0); - DUK_ASSERT(stridx < DUK_HEAP_NUM_STRINGS); - DUK_UNREF(thr); - - obj_index = duk_require_normalize_index(ctx, obj_index); - duk_push_hstring(ctx, DUK_HTHREAD_GET_STRING(thr, stridx)); - return duk_del_prop(ctx, obj_index); -} - -DUK_EXTERNAL duk_bool_t duk_has_prop(duk_context *ctx, duk_idx_t obj_index) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_tval *tv_obj; - duk_tval *tv_key; - duk_bool_t rc; - - DUK_ASSERT_CTX_VALID(ctx); - - /* Note: copying tv_obj and tv_key to locals to shield against a valstack - * resize is not necessary for a property existence check right now. - */ - - tv_obj = duk_require_tval(ctx, obj_index); - tv_key = duk_require_tval(ctx, -1); - - rc = duk_hobject_hasprop(thr, tv_obj, tv_key); - DUK_ASSERT(rc == 0 || rc == 1); - - duk_pop(ctx); /* remove key */ - return rc; /* 1 if property found, 0 otherwise */ -} - -DUK_EXTERNAL duk_bool_t duk_has_prop_string(duk_context *ctx, duk_idx_t obj_index, const char *key) { - DUK_ASSERT_CTX_VALID(ctx); - DUK_ASSERT(key != NULL); - - obj_index = duk_require_normalize_index(ctx, obj_index); - duk_push_string(ctx, key); - return duk_has_prop(ctx, obj_index); -} - -DUK_EXTERNAL duk_bool_t duk_has_prop_index(duk_context *ctx, duk_idx_t obj_index, duk_uarridx_t arr_index) { - DUK_ASSERT_CTX_VALID(ctx); - - obj_index = duk_require_normalize_index(ctx, obj_index); - duk_push_uarridx(ctx, arr_index); - return duk_has_prop(ctx, obj_index); -} - -DUK_INTERNAL duk_bool_t duk_has_prop_stridx(duk_context *ctx, duk_idx_t obj_index, duk_small_int_t stridx) { - duk_hthread *thr = (duk_hthread *) ctx; - - DUK_ASSERT_CTX_VALID(ctx); - DUK_ASSERT_DISABLE(stridx >= 0); - DUK_ASSERT(stridx < DUK_HEAP_NUM_STRINGS); - DUK_UNREF(thr); - - obj_index = duk_require_normalize_index(ctx, obj_index); - duk_push_hstring(ctx, DUK_HTHREAD_GET_STRING(thr, stridx)); - return duk_has_prop(ctx, obj_index); -} - -/* Define own property without inheritance looks and such. This differs from - * [[DefineOwnProperty]] because special behaviors (like Array 'length') are - * not invoked by this method. The caller must be careful to invoke any such - * behaviors if necessary. - */ -DUK_INTERNAL void duk_xdef_prop(duk_context *ctx, duk_idx_t obj_index, duk_small_uint_t desc_flags) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_hobject *obj; - duk_hstring *key; - - DUK_ASSERT_CTX_VALID(ctx); - - obj = duk_require_hobject(ctx, obj_index); - DUK_ASSERT(obj != NULL); - key = duk_to_hstring(ctx, -2); - DUK_ASSERT(key != NULL); - DUK_ASSERT(duk_require_tval(ctx, -1) != NULL); - - duk_hobject_define_property_internal(thr, obj, key, desc_flags); - - duk_pop(ctx); /* pop key */ -} - -DUK_INTERNAL void duk_xdef_prop_index(duk_context *ctx, duk_idx_t obj_index, duk_uarridx_t arr_index, duk_small_uint_t desc_flags) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_hobject *obj; - - DUK_ASSERT_CTX_VALID(ctx); - - obj = duk_require_hobject(ctx, obj_index); - DUK_ASSERT(obj != NULL); - - duk_hobject_define_property_internal_arridx(thr, obj, arr_index, desc_flags); - /* value popped by call */ -} - -DUK_INTERNAL 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_hthread *thr = (duk_hthread *) ctx; - duk_hobject *obj; - duk_hstring *key; - - DUK_ASSERT_CTX_VALID(ctx); - DUK_ASSERT_DISABLE(stridx >= 0); - DUK_ASSERT(stridx < DUK_HEAP_NUM_STRINGS); - - obj = duk_require_hobject(ctx, obj_index); - DUK_ASSERT(obj != NULL); - key = DUK_HTHREAD_GET_STRING(thr, stridx); - DUK_ASSERT(key != NULL); - DUK_ASSERT(duk_require_tval(ctx, -1) != NULL); - - duk_hobject_define_property_internal(thr, obj, key, desc_flags); - /* value popped by call */ -} - -DUK_INTERNAL 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) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_hobject *obj; - duk_hstring *key; - - DUK_ASSERT_CTX_VALID(ctx); - DUK_ASSERT_DISABLE(stridx >= 0); - DUK_ASSERT(stridx < DUK_HEAP_NUM_STRINGS); - DUK_ASSERT_DISABLE(builtin_idx >= 0); - DUK_ASSERT(builtin_idx < DUK_NUM_BUILTINS); - - obj = duk_require_hobject(ctx, obj_index); - DUK_ASSERT(obj != NULL); - key = DUK_HTHREAD_GET_STRING(thr, stridx); - DUK_ASSERT(key != NULL); - - duk_push_hobject(ctx, thr->builtins[builtin_idx]); - duk_hobject_define_property_internal(thr, obj, key, desc_flags); - /* value popped by call */ -} - -/* This is a rare property helper; it sets the global thrower (E5 Section 13.2.3) - * setter/getter into an object property. This is needed by the 'arguments' - * object creation code, function instance creation code, and Function.prototype.bind(). - */ - -DUK_INTERNAL void duk_xdef_prop_stridx_thrower(duk_context *ctx, duk_idx_t obj_index, duk_small_int_t stridx, duk_small_uint_t desc_flags) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_hobject *obj = duk_require_hobject(ctx, obj_index); - duk_hobject *thrower = thr->builtins[DUK_BIDX_TYPE_ERROR_THROWER]; - duk_hobject_define_accessor_internal(thr, obj, DUK_HTHREAD_GET_STRING(thr, stridx), thrower, thrower, desc_flags); -} - -/* Object.defineProperty() equivalent C binding. */ -DUK_EXTERNAL void duk_def_prop(duk_context *ctx, duk_idx_t obj_index, duk_uint_t flags) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_idx_t idx_base; - duk_hobject *obj; - duk_hstring *key; - duk_idx_t idx_value; - duk_hobject *get; - duk_hobject *set; - duk_uint_t is_data_desc; - duk_uint_t is_acc_desc; - - DUK_ASSERT_CTX_VALID(ctx); - - obj = duk_require_hobject(ctx, obj_index); - - is_data_desc = flags & (DUK_DEFPROP_HAVE_VALUE | DUK_DEFPROP_HAVE_WRITABLE); - is_acc_desc = flags & (DUK_DEFPROP_HAVE_GETTER | DUK_DEFPROP_HAVE_SETTER); - if (is_data_desc && is_acc_desc) { - /* "Have" flags must not be conflicting so that they would - * apply to both a plain property and an accessor at the same - * time. - */ - goto fail_invalid_desc; - } - - idx_base = duk_get_top_index(ctx); - if (flags & DUK_DEFPROP_HAVE_SETTER) { - duk_require_type_mask(ctx, idx_base, DUK_TYPE_MASK_UNDEFINED | - DUK_TYPE_MASK_OBJECT | - DUK_TYPE_MASK_LIGHTFUNC); - set = duk_get_hobject_or_lfunc_coerce(ctx, idx_base); - if (set != NULL && !DUK_HOBJECT_IS_CALLABLE(set)) { - goto fail_not_callable; - } - idx_base--; - } else { - set = NULL; - } - if (flags & DUK_DEFPROP_HAVE_GETTER) { - duk_require_type_mask(ctx, idx_base, DUK_TYPE_MASK_UNDEFINED | - DUK_TYPE_MASK_OBJECT | - DUK_TYPE_MASK_LIGHTFUNC); - get = duk_get_hobject_or_lfunc_coerce(ctx, idx_base); - if (get != NULL && !DUK_HOBJECT_IS_CALLABLE(get)) { - goto fail_not_callable; - } - idx_base--; - } else { - get = NULL; - } - if (flags & DUK_DEFPROP_HAVE_VALUE) { - idx_value = idx_base; - idx_base--; - } else { - idx_value = (duk_idx_t) -1; - } - key = duk_require_hstring(ctx, idx_base); - - duk_require_valid_index(ctx, idx_base); - - duk_hobject_define_property_helper(ctx, - flags /*defprop_flags*/, - obj, - key, - idx_value, - get, - set); - - /* Clean up stack */ - - duk_set_top(ctx, idx_base); - - /* [ ... obj ... ] */ - - return; - - fail_invalid_desc: - DUK_ERROR_TYPE(thr, DUK_STR_INVALID_DESCRIPTOR); - return; - - fail_not_callable: - DUK_ERROR_TYPE(thr, DUK_STR_NOT_CALLABLE); - return; -} - -/* - * Object related - * - * Note: seal() and freeze() are accessible through Ecmascript bindings, - * and are not exposed through the API. - */ - -DUK_EXTERNAL void duk_compact(duk_context *ctx, duk_idx_t obj_index) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_hobject *obj; - - DUK_ASSERT_CTX_VALID(ctx); - - obj = duk_get_hobject(ctx, obj_index); - if (obj) { - /* Note: this may fail, caller should protect the call if necessary */ - duk_hobject_compact_props(thr, obj); - } -} - -/* XXX: the duk_hobject_enum.c stack APIs should be reworked */ - -DUK_EXTERNAL void duk_enum(duk_context *ctx, duk_idx_t obj_index, duk_uint_t enum_flags) { - DUK_ASSERT_CTX_VALID(ctx); - - duk_dup(ctx, obj_index); - duk_require_hobject_or_lfunc_coerce(ctx, -1); - duk_hobject_enumerator_create(ctx, enum_flags); /* [target] -> [enum] */ -} - -DUK_EXTERNAL duk_bool_t duk_next(duk_context *ctx, duk_idx_t enum_index, duk_bool_t get_value) { - DUK_ASSERT_CTX_VALID(ctx); - - duk_require_hobject(ctx, enum_index); - duk_dup(ctx, enum_index); - return duk_hobject_enumerator_next(ctx, get_value); -} - -/* - * Helpers for writing multiple properties - */ - -DUK_EXTERNAL void duk_put_function_list(duk_context *ctx, duk_idx_t obj_index, const duk_function_list_entry *funcs) { - const duk_function_list_entry *ent = funcs; - - DUK_ASSERT_CTX_VALID(ctx); - - obj_index = duk_require_normalize_index(ctx, obj_index); - if (ent != NULL) { - while (ent->key != NULL) { - duk_push_c_function(ctx, ent->value, ent->nargs); - duk_put_prop_string(ctx, obj_index, ent->key); - ent++; - } - } -} - -DUK_EXTERNAL void duk_put_number_list(duk_context *ctx, duk_idx_t obj_index, const duk_number_list_entry *numbers) { - const duk_number_list_entry *ent = numbers; - - DUK_ASSERT_CTX_VALID(ctx); - - obj_index = duk_require_normalize_index(ctx, obj_index); - if (ent != NULL) { - while (ent->key != NULL) { - duk_push_number(ctx, ent->value); - duk_put_prop_string(ctx, obj_index, ent->key); - ent++; - } - } -} - -/* - * Shortcut for accessing global object properties - */ - -DUK_EXTERNAL duk_bool_t duk_get_global_string(duk_context *ctx, const char *key) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_bool_t ret; - - DUK_ASSERT_CTX_VALID(ctx); - DUK_ASSERT(thr->builtins[DUK_BIDX_GLOBAL] != NULL); - - /* XXX: direct implementation */ - - duk_push_hobject(ctx, thr->builtins[DUK_BIDX_GLOBAL]); - ret = duk_get_prop_string(ctx, -1, key); - duk_remove(ctx, -2); - return ret; -} - -DUK_EXTERNAL duk_bool_t duk_put_global_string(duk_context *ctx, const char *key) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_bool_t ret; - - DUK_ASSERT_CTX_VALID(ctx); - DUK_ASSERT(thr->builtins[DUK_BIDX_GLOBAL] != NULL); - - /* XXX: direct implementation */ - - duk_push_hobject(ctx, thr->builtins[DUK_BIDX_GLOBAL]); - duk_insert(ctx, -2); - ret = duk_put_prop_string(ctx, -2, key); /* [ ... global val ] -> [ ... global ] */ - duk_pop(ctx); - return ret; -} - -/* - * Object prototype - */ - -DUK_EXTERNAL void duk_get_prototype(duk_context *ctx, duk_idx_t index) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_hobject *obj; - duk_hobject *proto; - - DUK_ASSERT_CTX_VALID(ctx); - DUK_UNREF(thr); - - obj = duk_require_hobject(ctx, index); - DUK_ASSERT(obj != NULL); - - /* XXX: shared helper for duk_push_hobject_or_undefined()? */ - proto = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, obj); - if (proto) { - duk_push_hobject(ctx, proto); - } else { - duk_push_undefined(ctx); - } -} - -DUK_EXTERNAL void duk_set_prototype(duk_context *ctx, duk_idx_t index) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_hobject *obj; - duk_hobject *proto; - - DUK_ASSERT_CTX_VALID(ctx); - - obj = duk_require_hobject(ctx, index); - DUK_ASSERT(obj != NULL); - duk_require_type_mask(ctx, -1, DUK_TYPE_MASK_UNDEFINED | - DUK_TYPE_MASK_OBJECT); - proto = duk_get_hobject(ctx, -1); - /* proto can also be NULL here (allowed explicitly) */ - -#if defined(DUK_USE_ROM_OBJECTS) - if (DUK_HEAPHDR_HAS_READONLY((duk_heaphdr *) obj)) { - DUK_ERROR_TYPE(thr, DUK_STR_NOT_CONFIGURABLE); /* XXX: "read only object"? */ - return; - } -#endif - - DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, obj, proto); - - duk_pop(ctx); -} - -/* - * Object finalizer - */ - -/* XXX: these could be implemented as macros calling an internal function - * directly. - * XXX: same issue as with Duktape.fin: there's no way to delete the property - * now (just set it to undefined). - */ -DUK_EXTERNAL void duk_get_finalizer(duk_context *ctx, duk_idx_t index) { - DUK_ASSERT_CTX_VALID(ctx); - - duk_get_prop_stridx(ctx, index, DUK_STRIDX_INT_FINALIZER); -} - -DUK_EXTERNAL void duk_set_finalizer(duk_context *ctx, duk_idx_t index) { - DUK_ASSERT_CTX_VALID(ctx); - - duk_put_prop_stridx(ctx, index, DUK_STRIDX_INT_FINALIZER); -} -#line 1 "duk_api_stack.c" -/* - * API calls related to general value stack manipulation: resizing the value - * stack, pushing and popping values, type checking and reading values, - * coercing values, etc. - * - * Also contains internal functions (such as duk_get_tval()), defined - * in duk_api_internal.h, with semantics similar to the public API. - */ - -/* XXX: repetition of stack pre-checks -> helper or macro or inline */ -/* XXX: shared api error strings, and perhaps even throw code for rare cases? */ - -/* include removed: duk_internal.h */ - -/* - * Forward declarations - */ - -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); - -/* - * Global state for working around missing variadic macros - */ - -#ifndef DUK_USE_VARIADIC_MACROS -DUK_EXTERNAL const char *duk_api_global_filename = NULL; -DUK_EXTERNAL duk_int_t duk_api_global_line = 0; -#endif - -/* - * Misc helpers - */ - -/* Check that there's room to push one value. */ -#if defined(DUK_USE_VALSTACK_UNSAFE) -/* Faster but value stack overruns are memory unsafe. */ -#define DUK__CHECK_SPACE() do { \ - DUK_ASSERT(!(thr->valstack_top >= thr->valstack_end)); \ - } while (0) -#else -#define DUK__CHECK_SPACE() do { \ - if (DUK_UNLIKELY(thr->valstack_top >= thr->valstack_end)) { \ - DUK_ERROR_API(thr, DUK_STR_PUSH_BEYOND_ALLOC_STACK); \ - } \ - } while (0) -#endif - -DUK_LOCAL_DECL duk_heaphdr *duk__get_tagged_heaphdr_raw(duk_context *ctx, duk_idx_t index, duk_uint_t tag); - -DUK_LOCAL duk_int_t duk__api_coerce_d2i(duk_context *ctx, duk_idx_t index, duk_bool_t require) { - duk_hthread *thr; - duk_tval *tv; - duk_small_int_t c; - duk_double_t d; - - thr = (duk_hthread *) ctx; - - tv = duk_get_tval(ctx, index); - if (tv == NULL) { - goto error_notnumber; - } - - /* - * Special cases like NaN and +/- Infinity are handled explicitly - * because a plain C coercion from double to int handles these cases - * in undesirable ways. For instance, NaN may coerce to INT_MIN - * (not zero), and INT_MAX + 1 may coerce to INT_MIN (not INT_MAX). - * - * This double-to-int coercion differs from ToInteger() because it - * has a finite range (ToInteger() allows e.g. +/- Infinity). It - * also differs from ToInt32() because the INT_MIN/INT_MAX clamping - * depends on the size of the int type on the platform. In particular, - * on platforms with a 64-bit int type, the full range is allowed. - */ - -#if defined(DUK_USE_FASTINT) - if (DUK_TVAL_IS_FASTINT(tv)) { - duk_int64_t t = DUK_TVAL_GET_FASTINT(tv); -#if (DUK_INT_MAX <= 0x7fffffffL) - /* Clamping only necessary for 32-bit ints. */ - if (t < DUK_INT_MIN) { - t = DUK_INT_MIN; - } else if (t > DUK_INT_MAX) { - t = DUK_INT_MAX; - } -#endif - return (duk_int_t) t; - } -#endif - - if (DUK_TVAL_IS_NUMBER(tv)) { - d = DUK_TVAL_GET_NUMBER(tv); - c = (duk_small_int_t) DUK_FPCLASSIFY(d); - if (c == DUK_FP_NAN) { - return 0; - } else if (d < (duk_double_t) DUK_INT_MIN) { - /* covers -Infinity */ - return DUK_INT_MIN; - } else if (d > (duk_double_t) DUK_INT_MAX) { - /* covers +Infinity */ - return DUK_INT_MAX; - } else { - /* coerce towards zero */ - return (duk_int_t) d; - } - } - - error_notnumber: - - if (require) { - DUK_ERROR_REQUIRE_TYPE_INDEX(thr, index, "number", DUK_STR_NOT_NUMBER); - /* not reachable */ - } - return 0; -} - -DUK_LOCAL duk_uint_t duk__api_coerce_d2ui(duk_context *ctx, duk_idx_t index, duk_bool_t require) { - duk_hthread *thr; - duk_tval *tv; - duk_small_int_t c; - duk_double_t d; - - /* Same as above but for unsigned int range. */ - - thr = (duk_hthread *) ctx; - - tv = duk_get_tval(ctx, index); - if (tv == NULL) { - goto error_notnumber; - } - -#if defined(DUK_USE_FASTINT) - if (DUK_TVAL_IS_FASTINT(tv)) { - duk_int64_t t = DUK_TVAL_GET_FASTINT(tv); - if (t < 0) { - t = 0; - } -#if (DUK_UINT_MAX <= 0xffffffffUL) - /* Clamping only necessary for 32-bit ints. */ - else if (t > DUK_UINT_MAX) { - t = DUK_UINT_MAX; - } -#endif - return (duk_uint_t) t; - } -#endif - - if (DUK_TVAL_IS_NUMBER(tv)) { - d = DUK_TVAL_GET_NUMBER(tv); - c = (duk_small_int_t) DUK_FPCLASSIFY(d); - if (c == DUK_FP_NAN) { - return 0; - } else if (d < 0.0) { - /* covers -Infinity */ - return (duk_uint_t) 0; - } else if (d > (duk_double_t) DUK_UINT_MAX) { - /* covers +Infinity */ - return (duk_uint_t) DUK_UINT_MAX; - } else { - /* coerce towards zero */ - return (duk_uint_t) d; - } - } - - error_notnumber: - - if (require) { - DUK_ERROR_REQUIRE_TYPE_INDEX(thr, index, "number", DUK_STR_NOT_NUMBER); - /* not reachable */ - } - return 0; -} - -/* - * Stack index validation/normalization and getting a stack duk_tval ptr. - * - * These are called by many API entrypoints so the implementations must be - * fast and "inlined". - * - * There's some repetition because of this; keep the functions in sync. - */ - -DUK_EXTERNAL duk_idx_t duk_normalize_index(duk_context *ctx, duk_idx_t index) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_uidx_t vs_size; - duk_uidx_t uindex; - - DUK_ASSERT_CTX_VALID(ctx); - DUK_ASSERT(DUK_INVALID_INDEX < 0); - - /* Care must be taken to avoid pointer wrapping in the index - * validation. For instance, on a 32-bit platform with 8-byte - * duk_tval the index 0x20000000UL would wrap the memory space - * once. - */ - - /* Assume value stack sizes (in elements) fits into duk_idx_t. */ - DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom); - vs_size = (duk_uidx_t) (thr->valstack_top - thr->valstack_bottom); - DUK_ASSERT_DISABLE(vs_size >= 0); /* unsigned */ - - if (index < 0) { - uindex = vs_size + (duk_uidx_t) index; - } else { - /* since index non-negative */ - DUK_ASSERT(index != DUK_INVALID_INDEX); - uindex = (duk_uidx_t) index; - } - - /* DUK_INVALID_INDEX won't be accepted as a valid index. */ - DUK_ASSERT(vs_size + (duk_uidx_t) DUK_INVALID_INDEX >= vs_size); - - if (DUK_LIKELY(uindex < vs_size)) { - return (duk_idx_t) uindex; - } - return DUK_INVALID_INDEX; -} - -DUK_EXTERNAL duk_idx_t duk_require_normalize_index(duk_context *ctx, duk_idx_t index) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_uidx_t vs_size; - duk_uidx_t uindex; - - DUK_ASSERT_CTX_VALID(ctx); - DUK_ASSERT(DUK_INVALID_INDEX < 0); - - DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom); - vs_size = (duk_uidx_t) (thr->valstack_top - thr->valstack_bottom); - DUK_ASSERT_DISABLE(vs_size >= 0); /* unsigned */ - - if (index < 0) { - uindex = vs_size + (duk_uidx_t) index; - } else { - DUK_ASSERT(index != DUK_INVALID_INDEX); - uindex = (duk_uidx_t) index; - } - - /* DUK_INVALID_INDEX won't be accepted as a valid index. */ - DUK_ASSERT(vs_size + (duk_uidx_t) DUK_INVALID_INDEX >= vs_size); - - if (DUK_LIKELY(uindex < vs_size)) { - return (duk_idx_t) uindex; - } - DUK_ERROR_API_INDEX(thr, index); - return 0; /* unreachable */ -} - -DUK_INTERNAL duk_tval *duk_get_tval(duk_context *ctx, duk_idx_t index) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_uidx_t vs_size; - duk_uidx_t uindex; - - DUK_ASSERT_CTX_VALID(ctx); - DUK_ASSERT(DUK_INVALID_INDEX < 0); - - DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom); - vs_size = (duk_uidx_t) (thr->valstack_top - thr->valstack_bottom); - DUK_ASSERT_DISABLE(vs_size >= 0); /* unsigned */ - - if (index < 0) { - uindex = vs_size + (duk_uidx_t) index; - } else { - DUK_ASSERT(index != DUK_INVALID_INDEX); - uindex = (duk_uidx_t) index; - } - - /* DUK_INVALID_INDEX won't be accepted as a valid index. */ - DUK_ASSERT(vs_size + (duk_uidx_t) DUK_INVALID_INDEX >= vs_size); - - if (DUK_LIKELY(uindex < vs_size)) { - return thr->valstack_bottom + uindex; - } - return NULL; -} - -DUK_INTERNAL duk_tval *duk_require_tval(duk_context *ctx, duk_idx_t index) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_uidx_t vs_size; - duk_uidx_t uindex; - - DUK_ASSERT_CTX_VALID(ctx); - DUK_ASSERT(DUK_INVALID_INDEX < 0); - - DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom); - vs_size = (duk_uidx_t) (thr->valstack_top - thr->valstack_bottom); - DUK_ASSERT_DISABLE(vs_size >= 0); /* unsigned */ - - /* Use unsigned arithmetic to optimize comparison. */ - if (index < 0) { - uindex = vs_size + (duk_uidx_t) index; - } else { - DUK_ASSERT(index != DUK_INVALID_INDEX); - uindex = (duk_uidx_t) index; - } - - /* DUK_INVALID_INDEX won't be accepted as a valid index. */ - DUK_ASSERT(vs_size + (duk_uidx_t) DUK_INVALID_INDEX >= vs_size); - - if (DUK_LIKELY(uindex < vs_size)) { - return thr->valstack_bottom + uindex; - } - DUK_ERROR_API_INDEX(thr, index); - return NULL; -} - -/* Non-critical. */ -DUK_EXTERNAL duk_bool_t duk_is_valid_index(duk_context *ctx, duk_idx_t index) { - DUK_ASSERT_CTX_VALID(ctx); - DUK_ASSERT(DUK_INVALID_INDEX < 0); - - return (duk_normalize_index(ctx, index) >= 0); -} - -/* Non-critical. */ -DUK_EXTERNAL void duk_require_valid_index(duk_context *ctx, duk_idx_t index) { - duk_hthread *thr = (duk_hthread *) ctx; - - DUK_ASSERT_CTX_VALID(ctx); - DUK_ASSERT(DUK_INVALID_INDEX < 0); - - if (duk_normalize_index(ctx, index) < 0) { - DUK_ERROR_API_INDEX(thr, index); - return; /* unreachable */ - } -} - -/* - * Value stack top handling - */ - -DUK_EXTERNAL duk_idx_t duk_get_top(duk_context *ctx) { - duk_hthread *thr = (duk_hthread *) ctx; - - DUK_ASSERT_CTX_VALID(ctx); - - return (duk_idx_t) (thr->valstack_top - thr->valstack_bottom); -} - -/* Set stack top within currently allocated range, but don't reallocate. - * This is performance critical especially for call handling, so whenever - * changing, profile and look at generated code. - */ -DUK_EXTERNAL void duk_set_top(duk_context *ctx, duk_idx_t index) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_uidx_t vs_size; - duk_uidx_t vs_limit; - duk_uidx_t uindex; - duk_tval *tv; - - DUK_ASSERT_CTX_VALID(ctx); - DUK_ASSERT(DUK_INVALID_INDEX < 0); - - DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom); - DUK_ASSERT(thr->valstack_end >= thr->valstack_bottom); - vs_size = (duk_uidx_t) (thr->valstack_top - thr->valstack_bottom); - vs_limit = (duk_uidx_t) (thr->valstack_end - thr->valstack_bottom); - - if (index < 0) { - /* Negative indices are always within allocated stack but - * must not go below zero index. - */ - uindex = vs_size + (duk_uidx_t) index; - } else { - /* Positive index can be higher than valstack top but must - * not go above allocated stack (equality is OK). - */ - uindex = (duk_uidx_t) index; - } - - /* DUK_INVALID_INDEX won't be accepted as a valid index. */ - DUK_ASSERT(vs_size + (duk_uidx_t) DUK_INVALID_INDEX >= vs_size); - DUK_ASSERT(vs_size + (duk_uidx_t) DUK_INVALID_INDEX >= vs_limit); - -#if defined(DUK_USE_VALSTACK_UNSAFE) - DUK_ASSERT(uindex <= vs_limit); - DUK_UNREF(vs_limit); -#else - if (DUK_UNLIKELY(uindex > vs_limit)) { - DUK_ERROR_API_INDEX(thr, index); - return; /* unreachable */ - } -#endif - DUK_ASSERT(uindex <= vs_limit); - - /* Handle change in value stack top. Respect value stack - * initialization policy: 'undefined' above top. Note that - * DECREF may cause a side effect that reallocates valstack, - * so must relookup after DECREF. - */ - - if (uindex >= vs_size) { - /* Stack size increases or stays the same. */ -#if defined(DUK_USE_ASSERTIONS) - duk_uidx_t count; - - count = uindex - vs_size; - while (count != 0) { - count--; - tv = thr->valstack_top + count; - DUK_ASSERT(DUK_TVAL_IS_UNDEFINED(tv)); - } -#endif - thr->valstack_top = thr->valstack_bottom + uindex; - } else { - /* Stack size decreases. */ -#if defined(DUK_USE_REFERENCE_COUNTING) - duk_uidx_t count; - - count = vs_size - uindex; - DUK_ASSERT(count > 0); - while (count > 0) { - count--; - tv = --thr->valstack_top; /* tv -> value just before prev top value; must relookup */ - DUK_ASSERT(tv >= thr->valstack_bottom); - DUK_TVAL_SET_UNDEFINED_UPDREF(thr, tv); /* side effects */ - } -#else /* DUK_USE_REFERENCE_COUNTING */ - duk_uidx_t count; - duk_tval *tv_end; - - count = vs_size - uindex; - tv = thr->valstack_top; - tv_end = tv - count; - DUK_ASSERT(tv > tv_end); - do { - tv--; - DUK_TVAL_SET_UNDEFINED(tv); - } while (tv != tv_end); - thr->valstack_top = tv_end; -#endif /* DUK_USE_REFERENCE_COUNTING */ - } -} - -DUK_EXTERNAL duk_idx_t duk_get_top_index(duk_context *ctx) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_idx_t ret; - - DUK_ASSERT_CTX_VALID(ctx); - - ret = ((duk_idx_t) (thr->valstack_top - thr->valstack_bottom)) - 1; - if (DUK_UNLIKELY(ret < 0)) { - /* Return invalid index; if caller uses this without checking - * in another API call, the index won't map to a valid stack - * entry. - */ - return DUK_INVALID_INDEX; - } - return ret; -} - -DUK_EXTERNAL duk_idx_t duk_require_top_index(duk_context *ctx) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_idx_t ret; - - DUK_ASSERT_CTX_VALID(ctx); - - ret = ((duk_idx_t) (thr->valstack_top - thr->valstack_bottom)) - 1; - if (DUK_UNLIKELY(ret < 0)) { - DUK_ERROR_API_INDEX(thr, -1); - return 0; /* unreachable */ - } - return ret; -} - -/* - * Value stack resizing. - * - * This resizing happens above the current "top": the value stack can be - * grown or shrunk, but the "top" is not affected. The value stack cannot - * be resized to a size below the current "top". - * - * The low level reallocation primitive must carefully recompute all value - * stack pointers, and must also work if ALL pointers are NULL. The resize - * is quite tricky because the valstack realloc may cause a mark-and-sweep, - * which may run finalizers. Running finalizers may resize the valstack - * recursively (the same value stack we're working on). So, after realloc - * returns, we know that the valstack "top" should still be the same (there - * should not be live values above the "top"), but its underlying size and - * pointer may have changed. - */ - -/* XXX: perhaps refactor this to allow caller to specify some parameters, or - * at least a 'compact' flag which skips any spare or round-up .. useful for - * emergency gc. - */ - -DUK_LOCAL duk_bool_t duk__resize_valstack(duk_context *ctx, duk_size_t new_size) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_ptrdiff_t old_bottom_offset; - duk_ptrdiff_t old_top_offset; - duk_ptrdiff_t old_end_offset_post; -#ifdef DUK_USE_DEBUG - duk_ptrdiff_t old_end_offset_pre; - duk_tval *old_valstack_pre; - duk_tval *old_valstack_post; -#endif - duk_tval *new_valstack; - duk_size_t new_alloc_size; - duk_tval *p; - - DUK_ASSERT_CTX_VALID(ctx); - DUK_ASSERT(thr != NULL); - DUK_ASSERT(thr->valstack_bottom >= thr->valstack); - DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom); - DUK_ASSERT(thr->valstack_end >= thr->valstack_top); - DUK_ASSERT((duk_size_t) (thr->valstack_top - thr->valstack) <= new_size); /* can't resize below 'top' */ - DUK_ASSERT(new_size <= thr->valstack_max); /* valstack limit caller has check, prevents wrapping */ - DUK_ASSERT(new_size <= DUK_SIZE_MAX / sizeof(duk_tval)); /* specific assert for wrapping */ - - /* get pointer offsets for tweaking below */ - old_bottom_offset = (((duk_uint8_t *) thr->valstack_bottom) - ((duk_uint8_t *) thr->valstack)); - old_top_offset = (((duk_uint8_t *) thr->valstack_top) - ((duk_uint8_t *) thr->valstack)); -#ifdef DUK_USE_DEBUG - old_end_offset_pre = (((duk_uint8_t *) thr->valstack_end) - ((duk_uint8_t *) thr->valstack)); /* not very useful, used for debugging */ - old_valstack_pre = thr->valstack; -#endif - - /* Allocate a new valstack. - * - * Note: cannot use a plain DUK_REALLOC() because a mark-and-sweep may - * invalidate the original thr->valstack base pointer inside the realloc - * process. See doc/memory-management.rst. - */ - - new_alloc_size = sizeof(duk_tval) * new_size; - new_valstack = (duk_tval *) DUK_REALLOC_INDIRECT(thr->heap, duk_hthread_get_valstack_ptr, (void *) thr, new_alloc_size); - if (!new_valstack) { - /* Because new_size != 0, if condition doesn't need to be - * (new_valstack != NULL || new_size == 0). - */ - DUK_ASSERT(new_size != 0); - DUK_D(DUK_DPRINT("failed to resize valstack to %lu entries (%lu bytes)", - (unsigned long) new_size, (unsigned long) new_alloc_size)); - return 0; - } - - /* Note: the realloc may have triggered a mark-and-sweep which may - * have resized our valstack internally. However, the mark-and-sweep - * MUST NOT leave the stack bottom/top in a different state. Particular - * assumptions and facts: - * - * - The thr->valstack pointer may be different after realloc, - * and the offset between thr->valstack_end <-> thr->valstack - * may have changed. - * - The offset between thr->valstack_bottom <-> thr->valstack - * and thr->valstack_top <-> thr->valstack MUST NOT have changed, - * because mark-and-sweep must adhere to a strict stack policy. - * In other words, logical bottom and top MUST NOT have changed. - * - All values above the top are unreachable but are initialized - * to UNDEFINED, up to the post-realloc valstack_end. - * - 'old_end_offset' must be computed after realloc to be correct. - */ - - DUK_ASSERT((((duk_uint8_t *) thr->valstack_bottom) - ((duk_uint8_t *) thr->valstack)) == old_bottom_offset); - DUK_ASSERT((((duk_uint8_t *) thr->valstack_top) - ((duk_uint8_t *) thr->valstack)) == old_top_offset); - - /* success, fixup pointers */ - old_end_offset_post = (((duk_uint8_t *) thr->valstack_end) - ((duk_uint8_t *) thr->valstack)); /* must be computed after realloc */ -#ifdef DUK_USE_DEBUG - old_valstack_post = thr->valstack; -#endif - thr->valstack = new_valstack; - thr->valstack_end = new_valstack + new_size; -#if !defined(DUK_USE_PREFER_SIZE) - thr->valstack_size = new_size; -#endif - thr->valstack_bottom = (duk_tval *) (void *) ((duk_uint8_t *) new_valstack + old_bottom_offset); - thr->valstack_top = (duk_tval *) (void *) ((duk_uint8_t *) new_valstack + old_top_offset); - - DUK_ASSERT(thr->valstack_bottom >= thr->valstack); - DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom); - DUK_ASSERT(thr->valstack_end >= thr->valstack_top); - - /* useful for debugging */ -#ifdef DUK_USE_DEBUG - if (old_end_offset_pre != old_end_offset_post) { - DUK_D(DUK_DPRINT("valstack was resized during valstack_resize(), probably by mark-and-sweep; " - "end offset changed: %lu -> %lu", - (unsigned long) old_end_offset_pre, - (unsigned long) old_end_offset_post)); - } - if (old_valstack_pre != old_valstack_post) { - DUK_D(DUK_DPRINT("valstack pointer changed during valstack_resize(), probably by mark-and-sweep: %p -> %p", - (void *) old_valstack_pre, - (void *) old_valstack_post)); - } -#endif - - DUK_DD(DUK_DDPRINT("resized valstack to %lu elements (%lu bytes), bottom=%ld, top=%ld, " - "new pointers: start=%p end=%p bottom=%p top=%p", - (unsigned long) new_size, (unsigned long) new_alloc_size, - (long) (thr->valstack_bottom - thr->valstack), - (long) (thr->valstack_top - thr->valstack), - (void *) thr->valstack, (void *) thr->valstack_end, - (void *) thr->valstack_bottom, (void *) thr->valstack_top)); - - /* Init newly allocated slots (only). */ - p = (duk_tval *) (void *) ((duk_uint8_t *) thr->valstack + old_end_offset_post); - while (p < thr->valstack_end) { - /* Never executed if new size is smaller. */ - DUK_TVAL_SET_UNDEFINED(p); - p++; - } - - /* Assert for value stack initialization policy. */ -#if defined(DUK_USE_ASSERTIONS) - p = thr->valstack_top; - while (p < thr->valstack_end) { - DUK_ASSERT(DUK_TVAL_IS_UNDEFINED(p)); - p++; - } -#endif - - return 1; -} - -DUK_INTERNAL -duk_bool_t duk_valstack_resize_raw(duk_context *ctx, - duk_size_t min_new_size, - duk_small_uint_t flags) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_size_t old_size; - duk_size_t new_size; - duk_bool_t is_shrink = 0; - duk_small_uint_t shrink_flag = (flags & DUK_VSRESIZE_FLAG_SHRINK); - duk_small_uint_t compact_flag = (flags & DUK_VSRESIZE_FLAG_COMPACT); - duk_small_uint_t throw_flag = (flags & DUK_VSRESIZE_FLAG_THROW); - - DUK_DDD(DUK_DDDPRINT("check valstack resize: min_new_size=%lu, curr_size=%ld, curr_top=%ld, " - "curr_bottom=%ld, shrink=%d, compact=%d, throw=%d", - (unsigned long) min_new_size, - (long) (thr->valstack_end - thr->valstack), - (long) (thr->valstack_top - thr->valstack), - (long) (thr->valstack_bottom - thr->valstack), - (int) shrink_flag, (int) compact_flag, (int) throw_flag)); - - DUK_ASSERT_CTX_VALID(ctx); - DUK_ASSERT(thr != NULL); - DUK_ASSERT(thr->valstack_bottom >= thr->valstack); - DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom); - DUK_ASSERT(thr->valstack_end >= thr->valstack_top); - -#if defined(DUK_USE_PREFER_SIZE) - old_size = (duk_size_t) (thr->valstack_end - thr->valstack); -#else - DUK_ASSERT((duk_size_t) (thr->valstack_end - thr->valstack) == thr->valstack_size); - old_size = thr->valstack_size; -#endif - - if (min_new_size <= old_size) { - is_shrink = 1; - if (!shrink_flag || - old_size - min_new_size < DUK_VALSTACK_SHRINK_THRESHOLD) { - DUK_DDD(DUK_DDDPRINT("no need to grow or shrink valstack")); - return 1; - } - } - - new_size = min_new_size; - if (!compact_flag) { - if (is_shrink) { - /* shrink case; leave some spare */ - new_size += DUK_VALSTACK_SHRINK_SPARE; - } - - /* round up roughly to next 'grow step' */ - new_size = (new_size / DUK_VALSTACK_GROW_STEP + 1) * DUK_VALSTACK_GROW_STEP; - } - - DUK_DD(DUK_DDPRINT("want to %s valstack: %lu -> %lu elements (min_new_size %lu)", - (const char *) (new_size > old_size ? "grow" : "shrink"), - (unsigned long) old_size, (unsigned long) new_size, - (unsigned long) min_new_size)); - - if (new_size > thr->valstack_max) { - /* Note: may be triggered even if minimal new_size would not reach the limit, - * plan limit accordingly (taking DUK_VALSTACK_GROW_STEP into account). - */ - if (throw_flag) { - DUK_ERROR_RANGE(thr, DUK_STR_VALSTACK_LIMIT); - } else { - return 0; - } - } - - /* - * When resizing the valstack, a mark-and-sweep may be triggered for - * the allocation of the new valstack. If the mark-and-sweep needs - * to use our thread for something, it may cause *the same valstack* - * to be resized recursively. This happens e.g. when mark-and-sweep - * finalizers are called. This is taken into account carefully in - * duk__resize_valstack(). - * - * 'new_size' is known to be <= valstack_max, which ensures that - * size_t and pointer arithmetic won't wrap in duk__resize_valstack(). - */ - - if (!duk__resize_valstack(ctx, new_size)) { - if (is_shrink) { - DUK_DD(DUK_DDPRINT("valstack resize failed, but is a shrink, ignore")); - return 1; - } - - DUK_DD(DUK_DDPRINT("valstack resize failed")); - - if (throw_flag) { - DUK_ERROR_ALLOC_DEFMSG(thr); - } else { - return 0; - } - } - - DUK_DDD(DUK_DDDPRINT("valstack resize successful")); - return 1; -} - -DUK_EXTERNAL duk_bool_t duk_check_stack(duk_context *ctx, duk_idx_t extra) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_size_t min_new_size; - - DUK_ASSERT_CTX_VALID(ctx); - DUK_ASSERT(thr != NULL); - - if (DUK_UNLIKELY(extra < 0)) { - /* Clamping to zero makes the API more robust to calling code - * calculation errors. - */ - extra = 0; - } - - min_new_size = (thr->valstack_top - thr->valstack) + extra + DUK_VALSTACK_INTERNAL_EXTRA; - return duk_valstack_resize_raw(ctx, - min_new_size, /* min_new_size */ - 0 /* no shrink */ | /* flags */ - 0 /* no compact */ | - 0 /* no throw */); -} - -DUK_EXTERNAL void duk_require_stack(duk_context *ctx, duk_idx_t extra) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_size_t min_new_size; - - DUK_ASSERT_CTX_VALID(ctx); - DUK_ASSERT(thr != NULL); - - if (DUK_UNLIKELY(extra < 0)) { - /* Clamping to zero makes the API more robust to calling code - * calculation errors. - */ - extra = 0; - } - - min_new_size = (thr->valstack_top - thr->valstack) + extra + DUK_VALSTACK_INTERNAL_EXTRA; - (void) duk_valstack_resize_raw(ctx, - min_new_size, /* min_new_size */ - 0 /* no shrink */ | /* flags */ - 0 /* no compact */ | - DUK_VSRESIZE_FLAG_THROW); -} - -DUK_EXTERNAL duk_bool_t duk_check_stack_top(duk_context *ctx, duk_idx_t top) { - duk_size_t min_new_size; - - DUK_ASSERT_CTX_VALID(ctx); - - if (DUK_UNLIKELY(top < 0)) { - /* Clamping to zero makes the API more robust to calling code - * calculation errors. - */ - top = 0; - } - - min_new_size = top + DUK_VALSTACK_INTERNAL_EXTRA; - return duk_valstack_resize_raw(ctx, - min_new_size, /* min_new_size */ - 0 /* no shrink */ | /* flags */ - 0 /* no compact */ | - 0 /* no throw */); -} - -DUK_EXTERNAL void duk_require_stack_top(duk_context *ctx, duk_idx_t top) { - duk_size_t min_new_size; - - DUK_ASSERT_CTX_VALID(ctx); - - if (DUK_UNLIKELY(top < 0)) { - /* Clamping to zero makes the API more robust to calling code - * calculation errors. - */ - top = 0; - } - - min_new_size = top + DUK_VALSTACK_INTERNAL_EXTRA; - (void) duk_valstack_resize_raw(ctx, - min_new_size, /* min_new_size */ - 0 /* no shrink */ | /* flags */ - 0 /* no compact */ | - DUK_VSRESIZE_FLAG_THROW); -} - -/* - * Basic stack manipulation: swap, dup, insert, replace, etc - */ - -DUK_EXTERNAL void duk_swap(duk_context *ctx, duk_idx_t index1, duk_idx_t index2) { - duk_tval *tv1; - duk_tval *tv2; - duk_tval tv_tmp; - - DUK_ASSERT_CTX_VALID(ctx); - - tv1 = duk_require_tval(ctx, index1); - DUK_ASSERT(tv1 != NULL); - tv2 = duk_require_tval(ctx, index2); - DUK_ASSERT(tv2 != NULL); - - /* If tv1==tv2 this is a NOP, no check is needed */ - DUK_TVAL_SET_TVAL(&tv_tmp, tv1); - DUK_TVAL_SET_TVAL(tv1, tv2); - DUK_TVAL_SET_TVAL(tv2, &tv_tmp); -} - -DUK_EXTERNAL void duk_swap_top(duk_context *ctx, duk_idx_t index) { - DUK_ASSERT_CTX_VALID(ctx); - - duk_swap(ctx, index, -1); -} - -DUK_EXTERNAL void duk_dup(duk_context *ctx, duk_idx_t from_index) { - duk_hthread *thr; - duk_tval *tv_from; - duk_tval *tv_to; - - DUK_ASSERT_CTX_VALID(ctx); - thr = (duk_hthread *) ctx; - DUK__CHECK_SPACE(); - - tv_from = duk_require_tval(ctx, from_index); - tv_to = thr->valstack_top++; - DUK_ASSERT(tv_from != NULL); - DUK_ASSERT(tv_to != NULL); - DUK_TVAL_SET_TVAL(tv_to, tv_from); - DUK_TVAL_INCREF(thr, tv_to); /* no side effects */ -} - -DUK_EXTERNAL void duk_dup_top(duk_context *ctx) { - duk_hthread *thr; - duk_tval *tv_from; - duk_tval *tv_to; - - DUK_ASSERT_CTX_VALID(ctx); - thr = (duk_hthread *) ctx; - DUK__CHECK_SPACE(); - - if (thr->valstack_top - thr->valstack_bottom <= 0) { - DUK_ERROR_API_INDEX(thr, -1); - return; /* unreachable */ - } - tv_from = thr->valstack_top - 1; - tv_to = thr->valstack_top++; - DUK_ASSERT(tv_from != NULL); - DUK_ASSERT(tv_to != NULL); - DUK_TVAL_SET_TVAL(tv_to, tv_from); - DUK_TVAL_INCREF(thr, tv_to); /* no side effects */ -} - -DUK_EXTERNAL void duk_insert(duk_context *ctx, duk_idx_t to_index) { - duk_tval *p; - duk_tval *q; - duk_tval tv_tmp; - duk_size_t nbytes; - - DUK_ASSERT_CTX_VALID(ctx); - - p = duk_require_tval(ctx, to_index); - DUK_ASSERT(p != NULL); - q = duk_require_tval(ctx, -1); - DUK_ASSERT(q != NULL); - - DUK_ASSERT(q >= p); - - /* nbytes - * <---------> - * [ ... | p | x | x | q ] - * => [ ... | q | p | x | x ] - */ - - nbytes = (duk_size_t) (((duk_uint8_t *) q) - ((duk_uint8_t *) p)); /* Note: 'q' is top-1 */ - - DUK_DDD(DUK_DDDPRINT("duk_insert: to_index=%ld, p=%p, q=%p, nbytes=%lu", - (long) to_index, (void *) p, (void *) q, (unsigned long) nbytes)); - - /* No net refcount changes. */ - - if (nbytes > 0) { - DUK_TVAL_SET_TVAL(&tv_tmp, q); - DUK_ASSERT(nbytes > 0); - DUK_MEMMOVE((void *) (p + 1), (const void *) p, (size_t) nbytes); - DUK_TVAL_SET_TVAL(p, &tv_tmp); - } else { - /* nop: insert top to top */ - DUK_ASSERT(nbytes == 0); - DUK_ASSERT(p == q); - } -} - -DUK_EXTERNAL void duk_replace(duk_context *ctx, duk_idx_t to_index) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_tval *tv1; - duk_tval *tv2; - duk_tval tv_tmp; - - DUK_ASSERT_CTX_VALID(ctx); - - tv1 = duk_require_tval(ctx, -1); - DUK_ASSERT(tv1 != NULL); - tv2 = duk_require_tval(ctx, to_index); - DUK_ASSERT(tv2 != NULL); - - /* For tv1 == tv2, both pointing to stack top, the end result - * is same as duk_pop(ctx). - */ - DUK_TVAL_SET_TVAL(&tv_tmp, tv2); - DUK_TVAL_SET_TVAL(tv2, tv1); - DUK_TVAL_SET_UNDEFINED(tv1); - thr->valstack_top--; - DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */ -} - -DUK_EXTERNAL void duk_copy(duk_context *ctx, duk_idx_t from_index, duk_idx_t to_index) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_tval *tv1; - duk_tval *tv2; - - DUK_ASSERT_CTX_VALID(ctx); - DUK_UNREF(thr); /* w/o refcounting */ - - tv1 = duk_require_tval(ctx, from_index); - DUK_ASSERT(tv1 != NULL); - tv2 = duk_require_tval(ctx, to_index); - DUK_ASSERT(tv2 != NULL); - - /* For tv1 == tv2, this is a no-op (no explicit check needed). */ - DUK_TVAL_SET_TVAL_UPDREF(thr, tv2, tv1); /* side effects */ -} - -DUK_EXTERNAL void duk_remove(duk_context *ctx, duk_idx_t index) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_tval *p; - duk_tval *q; -#ifdef DUK_USE_REFERENCE_COUNTING - duk_tval tv_tmp; -#endif - duk_size_t nbytes; - - DUK_ASSERT_CTX_VALID(ctx); - - p = duk_require_tval(ctx, index); - DUK_ASSERT(p != NULL); - q = duk_require_tval(ctx, -1); - DUK_ASSERT(q != NULL); - - DUK_ASSERT(q >= p); - - /* nbytes zero size case - * <---------> - * [ ... | p | x | x | q ] [ ... | p==q ] - * => [ ... | x | x | q ] [ ... ] - */ - -#ifdef DUK_USE_REFERENCE_COUNTING - /* use a temp: decref only when valstack reachable values are correct */ - DUK_TVAL_SET_TVAL(&tv_tmp, p); -#endif - - nbytes = (duk_size_t) (((duk_uint8_t *) q) - ((duk_uint8_t *) p)); /* Note: 'q' is top-1 */ - DUK_MEMMOVE((void *) p, (const void *) (p + 1), (size_t) nbytes); /* zero size not an issue: pointers are valid */ - - DUK_TVAL_SET_UNDEFINED(q); - thr->valstack_top--; - -#ifdef DUK_USE_REFERENCE_COUNTING - DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */ -#endif -} - -/* - * Stack slice primitives - */ - -DUK_EXTERNAL void duk_xcopymove_raw(duk_context *to_ctx, duk_context *from_ctx, duk_idx_t count, duk_bool_t is_copy) { - duk_hthread *to_thr = (duk_hthread *) to_ctx; - duk_hthread *from_thr = (duk_hthread *) from_ctx; - void *src; - duk_size_t nbytes; - duk_tval *p; - duk_tval *q; - - /* XXX: several pointer comparison issues here */ - - DUK_ASSERT_CTX_VALID(to_ctx); - DUK_ASSERT_CTX_VALID(from_ctx); - DUK_ASSERT(to_ctx != NULL); - DUK_ASSERT(from_ctx != NULL); - - if (to_ctx == from_ctx) { - DUK_ERROR_API(to_thr, DUK_STR_INVALID_CONTEXT); - return; - } - if ((count < 0) || - (count > (duk_idx_t) to_thr->valstack_max)) { - /* Maximum value check ensures 'nbytes' won't wrap below. */ - DUK_ERROR_API(to_thr, DUK_STR_INVALID_COUNT); - return; - } - - nbytes = sizeof(duk_tval) * count; - if (nbytes == 0) { - return; - } - DUK_ASSERT(to_thr->valstack_top <= to_thr->valstack_end); - if ((duk_size_t) ((duk_uint8_t *) to_thr->valstack_end - (duk_uint8_t *) to_thr->valstack_top) < nbytes) { - DUK_ERROR_API(to_thr, DUK_STR_PUSH_BEYOND_ALLOC_STACK); - } - src = (void *) ((duk_uint8_t *) from_thr->valstack_top - nbytes); - if (src < (void *) from_thr->valstack_bottom) { - DUK_ERROR_API(to_thr, DUK_STR_INVALID_COUNT); - } - - /* copy values (no overlap even if to_ctx == from_ctx; that's not - * allowed now anyway) - */ - DUK_ASSERT(nbytes > 0); - DUK_MEMCPY((void *) to_thr->valstack_top, (const void *) src, (size_t) nbytes); - - p = to_thr->valstack_top; - to_thr->valstack_top = (duk_tval *) (void *) (((duk_uint8_t *) p) + nbytes); - - if (is_copy) { - /* Incref copies, keep originals. */ - q = to_thr->valstack_top; - while (p < q) { - DUK_TVAL_INCREF(to_thr, p); /* no side effects */ - p++; - } - } else { - /* No net refcount change. */ - p = from_thr->valstack_top; - q = (duk_tval *) (void *) (((duk_uint8_t *) p) - nbytes); - from_thr->valstack_top = q; - - while (p > q) { - p--; - DUK_TVAL_SET_UNDEFINED(p); - /* XXX: fast primitive to set a bunch of values to UNDEFINED */ - } - } -} - -/* - * Get/require - */ - -DUK_EXTERNAL void duk_require_undefined(duk_context *ctx, duk_idx_t index) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_tval *tv; - - DUK_ASSERT_CTX_VALID(ctx); - - tv = duk_get_tval(ctx, index); - if (tv && DUK_TVAL_IS_UNDEFINED(tv)) { - return; - } - DUK_ERROR_REQUIRE_TYPE_INDEX(thr, index, "undefined", DUK_STR_NOT_UNDEFINED); - return; /* not reachable */ -} - -DUK_EXTERNAL void duk_require_null(duk_context *ctx, duk_idx_t index) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_tval *tv; - - DUK_ASSERT_CTX_VALID(ctx); - - tv = duk_get_tval(ctx, index); - if (tv && DUK_TVAL_IS_NULL(tv)) { - return; - } - DUK_ERROR_REQUIRE_TYPE_INDEX(thr, index, "null", DUK_STR_NOT_NULL); - return; /* not reachable */ -} - -DUK_EXTERNAL duk_bool_t duk_get_boolean(duk_context *ctx, duk_idx_t index) { - duk_bool_t ret = 0; /* default: false */ - duk_tval *tv; - - DUK_ASSERT_CTX_VALID(ctx); - - tv = duk_get_tval(ctx, index); - if (tv && DUK_TVAL_IS_BOOLEAN(tv)) { - ret = DUK_TVAL_GET_BOOLEAN(tv); - } - - DUK_ASSERT(ret == 0 || ret == 1); - return ret; -} - -DUK_EXTERNAL duk_bool_t duk_require_boolean(duk_context *ctx, duk_idx_t index) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_tval *tv; - - DUK_ASSERT_CTX_VALID(ctx); - - tv = duk_get_tval(ctx, index); - if (tv && DUK_TVAL_IS_BOOLEAN(tv)) { - duk_bool_t ret = DUK_TVAL_GET_BOOLEAN(tv); - DUK_ASSERT(ret == 0 || ret == 1); - return ret; - } - DUK_ERROR_REQUIRE_TYPE_INDEX(thr, index, "boolean", DUK_STR_NOT_BOOLEAN); - return 0; /* not reachable */ -} - -DUK_EXTERNAL duk_double_t duk_get_number(duk_context *ctx, duk_idx_t index) { - duk_double_union ret; - duk_tval *tv; - - DUK_ASSERT_CTX_VALID(ctx); - - ret.d = DUK_DOUBLE_NAN; /* default: NaN */ - tv = duk_get_tval(ctx, index); - if (tv && DUK_TVAL_IS_NUMBER(tv)) { - ret.d = DUK_TVAL_GET_NUMBER(tv); - } - - /* - * Number should already be in NaN-normalized form, but let's - * normalize anyway. - */ - - DUK_DBLUNION_NORMALIZE_NAN_CHECK(&ret); - return ret.d; -} - -DUK_EXTERNAL duk_double_t duk_require_number(duk_context *ctx, duk_idx_t index) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_tval *tv; - - DUK_ASSERT_CTX_VALID(ctx); - - tv = duk_get_tval(ctx, index); - if (tv && DUK_TVAL_IS_NUMBER(tv)) { - duk_double_union ret; - ret.d = DUK_TVAL_GET_NUMBER(tv); - - /* - * Number should already be in NaN-normalized form, - * but let's normalize anyway. - */ - - DUK_DBLUNION_NORMALIZE_NAN_CHECK(&ret); - return ret.d; - } - DUK_ERROR_REQUIRE_TYPE_INDEX(thr, index, "number", DUK_STR_NOT_NUMBER); - return DUK_DOUBLE_NAN; /* not reachable */ -} - -DUK_EXTERNAL duk_int_t duk_get_int(duk_context *ctx, duk_idx_t index) { - /* Custom coercion for API */ - DUK_ASSERT_CTX_VALID(ctx); - return (duk_int_t) duk__api_coerce_d2i(ctx, index, 0 /*require*/); -} - -DUK_EXTERNAL duk_uint_t duk_get_uint(duk_context *ctx, duk_idx_t index) { - /* Custom coercion for API */ - DUK_ASSERT_CTX_VALID(ctx); - return (duk_uint_t) duk__api_coerce_d2ui(ctx, index, 0 /*require*/); -} - -DUK_EXTERNAL duk_int_t duk_require_int(duk_context *ctx, duk_idx_t index) { - /* Custom coercion for API */ - DUK_ASSERT_CTX_VALID(ctx); - return (duk_int_t) duk__api_coerce_d2i(ctx, index, 1 /*require*/); -} - -DUK_EXTERNAL duk_uint_t duk_require_uint(duk_context *ctx, duk_idx_t index) { - /* Custom coercion for API */ - DUK_ASSERT_CTX_VALID(ctx); - return (duk_uint_t) duk__api_coerce_d2ui(ctx, index, 1 /*require*/); -} - -DUK_EXTERNAL const char *duk_get_lstring(duk_context *ctx, duk_idx_t index, duk_size_t *out_len) { - const char *ret; - duk_tval *tv; - - DUK_ASSERT_CTX_VALID(ctx); - - /* default: NULL, length 0 */ - ret = NULL; - if (out_len) { - *out_len = 0; - } - - tv = duk_get_tval(ctx, index); - if (tv && DUK_TVAL_IS_STRING(tv)) { - /* Here we rely on duk_hstring instances always being zero - * terminated even if the actual string is not. - */ - duk_hstring *h = DUK_TVAL_GET_STRING(tv); - DUK_ASSERT(h != NULL); - ret = (const char *) DUK_HSTRING_GET_DATA(h); - if (out_len) { - *out_len = DUK_HSTRING_GET_BYTELEN(h); - } - } - - return ret; -} - -DUK_EXTERNAL const char *duk_require_lstring(duk_context *ctx, duk_idx_t index, duk_size_t *out_len) { - duk_hthread *thr = (duk_hthread *) ctx; - const char *ret; - - DUK_ASSERT_CTX_VALID(ctx); - - /* Note: this check relies on the fact that even a zero-size string - * has a non-NULL pointer. - */ - ret = duk_get_lstring(ctx, index, out_len); - if (ret) { - return ret; - } - DUK_ERROR_REQUIRE_TYPE_INDEX(thr, index, "string", DUK_STR_NOT_STRING); - return NULL; /* not reachable */ -} - -DUK_EXTERNAL const char *duk_get_string(duk_context *ctx, duk_idx_t index) { - DUK_ASSERT_CTX_VALID(ctx); - - return duk_get_lstring(ctx, index, NULL); -} - -DUK_EXTERNAL const char *duk_require_string(duk_context *ctx, duk_idx_t index) { - DUK_ASSERT_CTX_VALID(ctx); - - return duk_require_lstring(ctx, index, NULL); -} - -DUK_EXTERNAL void *duk_get_pointer(duk_context *ctx, duk_idx_t index) { - duk_tval *tv; - - DUK_ASSERT_CTX_VALID(ctx); - - tv = duk_get_tval(ctx, index); - if (tv && DUK_TVAL_IS_POINTER(tv)) { - void *p = DUK_TVAL_GET_POINTER(tv); /* may be NULL */ - return (void *) p; - } - - return NULL; -} - -DUK_EXTERNAL void *duk_require_pointer(duk_context *ctx, duk_idx_t index) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_tval *tv; - - DUK_ASSERT_CTX_VALID(ctx); - - /* Note: here we must be wary of the fact that a pointer may be - * valid and be a NULL. - */ - tv = duk_get_tval(ctx, index); - if (tv && DUK_TVAL_IS_POINTER(tv)) { - void *p = DUK_TVAL_GET_POINTER(tv); /* may be NULL */ - return (void *) p; - } - DUK_ERROR_REQUIRE_TYPE_INDEX(thr, index, "pointer", DUK_STR_NOT_POINTER); - return NULL; /* not reachable */ -} - -#if 0 /*unused*/ -DUK_INTERNAL void *duk_get_voidptr(duk_context *ctx, duk_idx_t index) { - duk_tval *tv; - - DUK_ASSERT_CTX_VALID(ctx); - - tv = duk_get_tval(ctx, index); - if (tv && DUK_TVAL_IS_HEAP_ALLOCATED(tv)) { - duk_heaphdr *h = DUK_TVAL_GET_HEAPHDR(tv); - DUK_ASSERT(h != NULL); - return (void *) h; - } - - return NULL; -} -#endif - -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_hthread *thr = (duk_hthread *) ctx; - duk_tval *tv; - - DUK_ASSERT_CTX_VALID(ctx); - DUK_UNREF(thr); - - if (out_size != NULL) { - *out_size = 0; - } - - tv = duk_get_tval(ctx, index); - if (tv && DUK_TVAL_IS_BUFFER(tv)) { - duk_hbuffer *h = DUK_TVAL_GET_BUFFER(tv); - DUK_ASSERT(h != NULL); - if (out_size) { - *out_size = DUK_HBUFFER_GET_SIZE(h); - } - return (void *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h); /* may be NULL (but only if size is 0) */ - } - - if (throw_flag) { - DUK_ERROR_REQUIRE_TYPE_INDEX(thr, index, "buffer", DUK_STR_NOT_BUFFER); - } - return NULL; -} - -DUK_EXTERNAL void *duk_get_buffer(duk_context *ctx, duk_idx_t index, duk_size_t *out_size) { - return duk__get_buffer_helper(ctx, index, out_size, 0 /*throw_flag*/); -} - -DUK_EXTERNAL void *duk_require_buffer(duk_context *ctx, duk_idx_t index, duk_size_t *out_size) { - return duk__get_buffer_helper(ctx, index, out_size, 1 /*throw_flag*/); -} - -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_hthread *thr = (duk_hthread *) ctx; - duk_tval *tv; - - DUK_ASSERT_CTX_VALID(ctx); - DUK_UNREF(thr); - - if (out_size != NULL) { - *out_size = 0; - } - - tv = duk_get_tval(ctx, index); - if (tv == NULL) { - goto fail; - } - - if (DUK_TVAL_IS_BUFFER(tv)) { - duk_hbuffer *h = DUK_TVAL_GET_BUFFER(tv); - DUK_ASSERT(h != NULL); - if (out_size) { - *out_size = DUK_HBUFFER_GET_SIZE(h); - } - return (void *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h); /* may be NULL (but only if size is 0) */ - } else if (DUK_TVAL_IS_OBJECT(tv)) { - duk_hobject *h = DUK_TVAL_GET_OBJECT(tv); - DUK_ASSERT(h != NULL); - if (DUK_HOBJECT_IS_BUFFEROBJECT(h)) { - /* XXX: this is probably a useful shared helper: for a - * duk_hbufferobject, get a validated buffer pointer/length. - */ - duk_hbufferobject *h_bufobj = (duk_hbufferobject *) h; - DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj); - - if (h_bufobj->buf != NULL && - DUK_HBUFFEROBJECT_VALID_SLICE(h_bufobj)) { - duk_uint8_t *p; - - p = (duk_uint8_t *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h_bufobj->buf); - if (out_size != NULL) { - *out_size = (duk_size_t) h_bufobj->length; - } - return (void *) (p + h_bufobj->offset); - } - /* if slice not fully valid, treat as error */ - } - } - - fail: - if (throw_flag) { - DUK_ERROR_REQUIRE_TYPE_INDEX(thr, index, "buffer", DUK_STR_NOT_BUFFER); - } - return NULL; -} - -DUK_EXTERNAL void *duk_get_buffer_data(duk_context *ctx, duk_idx_t index, duk_size_t *out_size) { - return duk__get_buffer_data_helper(ctx, index, out_size, 0 /*throw_flag*/); -} - -DUK_EXTERNAL void *duk_require_buffer_data(duk_context *ctx, duk_idx_t index, duk_size_t *out_size) { - return duk__get_buffer_data_helper(ctx, index, out_size, 1 /*throw_flag*/); -} - -/* Raw helper for getting a value from the stack, checking its tag. - * The tag cannot be a number because numbers don't have an internal - * tag in the packed representation. - */ - -DUK_LOCAL duk_heaphdr *duk__get_tagged_heaphdr_raw(duk_context *ctx, duk_idx_t index, duk_uint_t tag) { - duk_tval *tv; - - DUK_ASSERT_CTX_VALID(ctx); - - tv = duk_get_tval(ctx, index); - if (tv && (DUK_TVAL_GET_TAG(tv) == tag)) { - duk_heaphdr *ret; - ret = DUK_TVAL_GET_HEAPHDR(tv); - DUK_ASSERT(ret != NULL); /* tagged null pointers should never occur */ - return ret; - } - - return (duk_heaphdr *) NULL; -} - -DUK_INTERNAL duk_hstring *duk_get_hstring(duk_context *ctx, duk_idx_t index) { - return (duk_hstring *) duk__get_tagged_heaphdr_raw(ctx, index, DUK_TAG_STRING); -} - -DUK_INTERNAL duk_hstring *duk_require_hstring(duk_context *ctx, duk_idx_t index) { - duk_heaphdr *h; - h = duk__get_tagged_heaphdr_raw(ctx, index, DUK_TAG_STRING); - if (h == NULL) { - DUK_ERROR_REQUIRE_TYPE_INDEX(ctx, index, "string", DUK_STR_NOT_STRING); - } - return (duk_hstring *) h; -} - -DUK_INTERNAL duk_hobject *duk_get_hobject(duk_context *ctx, duk_idx_t index) { - return (duk_hobject *) duk__get_tagged_heaphdr_raw(ctx, index, DUK_TAG_OBJECT); -} - -DUK_INTERNAL duk_hobject *duk_require_hobject(duk_context *ctx, duk_idx_t index) { - duk_heaphdr *h; - h = duk__get_tagged_heaphdr_raw(ctx, index, DUK_TAG_OBJECT); - if (h == NULL) { - DUK_ERROR_REQUIRE_TYPE_INDEX(ctx, index, "object", DUK_STR_NOT_OBJECT); - } - return (duk_hobject *) h; -} - -DUK_INTERNAL duk_hbuffer *duk_get_hbuffer(duk_context *ctx, duk_idx_t index) { - return (duk_hbuffer *) duk__get_tagged_heaphdr_raw(ctx, index, DUK_TAG_BUFFER); -} - -DUK_INTERNAL duk_hbuffer *duk_require_hbuffer(duk_context *ctx, duk_idx_t index) { - duk_heaphdr *h; - h = duk__get_tagged_heaphdr_raw(ctx, index, DUK_TAG_BUFFER); - if (h == NULL) { - DUK_ERROR_REQUIRE_TYPE_INDEX(ctx, index, "buffer", DUK_STR_NOT_BUFFER); - } - return (duk_hbuffer *) h; -} - -DUK_INTERNAL duk_hthread *duk_get_hthread(duk_context *ctx, duk_idx_t index) { - duk_hobject *h = (duk_hobject *) duk__get_tagged_heaphdr_raw(ctx, index, DUK_TAG_OBJECT); - if (h != NULL && !DUK_HOBJECT_IS_THREAD(h)) { - h = NULL; - } - return (duk_hthread *) h; -} - -DUK_INTERNAL duk_hthread *duk_require_hthread(duk_context *ctx, duk_idx_t index) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_hobject *h = (duk_hobject *) duk__get_tagged_heaphdr_raw(ctx, index, DUK_TAG_OBJECT); - if (!(h != NULL && DUK_HOBJECT_IS_THREAD(h))) { - DUK_ERROR_REQUIRE_TYPE_INDEX(thr, index, "thread", DUK_STR_NOT_THREAD); - } - return (duk_hthread *) h; -} - -DUK_INTERNAL duk_hcompiledfunction *duk_get_hcompiledfunction(duk_context *ctx, duk_idx_t index) { - duk_hobject *h = (duk_hobject *) duk__get_tagged_heaphdr_raw(ctx, index, DUK_TAG_OBJECT); - if (h != NULL && !DUK_HOBJECT_IS_COMPILEDFUNCTION(h)) { - h = NULL; - } - return (duk_hcompiledfunction *) h; -} - -DUK_INTERNAL duk_hcompiledfunction *duk_require_hcompiledfunction(duk_context *ctx, duk_idx_t index) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_hobject *h = (duk_hobject *) duk__get_tagged_heaphdr_raw(ctx, index, DUK_TAG_OBJECT); - if (!(h != NULL && DUK_HOBJECT_IS_COMPILEDFUNCTION(h))) { - DUK_ERROR_REQUIRE_TYPE_INDEX(thr, index, "compiledfunction", DUK_STR_NOT_COMPILEDFUNCTION); - } - return (duk_hcompiledfunction *) h; -} - -DUK_INTERNAL duk_hnativefunction *duk_get_hnativefunction(duk_context *ctx, duk_idx_t index) { - duk_hobject *h = (duk_hobject *) duk__get_tagged_heaphdr_raw(ctx, index, DUK_TAG_OBJECT); - if (h != NULL && !DUK_HOBJECT_IS_NATIVEFUNCTION(h)) { - h = NULL; - } - return (duk_hnativefunction *) h; -} - -DUK_INTERNAL duk_hnativefunction *duk_require_hnativefunction(duk_context *ctx, duk_idx_t index) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_hobject *h = (duk_hobject *) duk__get_tagged_heaphdr_raw(ctx, index, DUK_TAG_OBJECT); - if (!(h != NULL && DUK_HOBJECT_IS_NATIVEFUNCTION(h))) { - DUK_ERROR_REQUIRE_TYPE_INDEX(thr, index, "nativefunction", DUK_STR_NOT_NATIVEFUNCTION); - } - return (duk_hnativefunction *) h; -} - -DUK_EXTERNAL duk_c_function duk_get_c_function(duk_context *ctx, duk_idx_t index) { - duk_tval *tv; - duk_hobject *h; - duk_hnativefunction *f; - - DUK_ASSERT_CTX_VALID(ctx); - - tv = duk_get_tval(ctx, index); - if (!tv) { - return NULL; - } - if (!DUK_TVAL_IS_OBJECT(tv)) { - return NULL; - } - h = DUK_TVAL_GET_OBJECT(tv); - DUK_ASSERT(h != NULL); - - if (!DUK_HOBJECT_IS_NATIVEFUNCTION(h)) { - return NULL; - } - DUK_ASSERT(DUK_HOBJECT_HAS_NATIVEFUNCTION(h)); - f = (duk_hnativefunction *) h; - - return f->func; -} - -DUK_EXTERNAL duk_c_function duk_require_c_function(duk_context *ctx, duk_idx_t index) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_c_function ret; - - DUK_ASSERT_CTX_VALID(ctx); - - ret = duk_get_c_function(ctx, index); - if (!ret) { - DUK_ERROR_REQUIRE_TYPE_INDEX(thr, index, "nativefunction", DUK_STR_NOT_NATIVEFUNCTION); - } - return ret; -} - -DUK_EXTERNAL void duk_require_function(duk_context *ctx, duk_idx_t index) { - if (!duk_is_function(ctx, index)) { - DUK_ERROR_REQUIRE_TYPE_INDEX((duk_hthread *) ctx, index, "function", DUK_STR_NOT_FUNCTION); - } -} - -DUK_EXTERNAL duk_context *duk_get_context(duk_context *ctx, duk_idx_t index) { - DUK_ASSERT_CTX_VALID(ctx); - - return (duk_context *) duk_get_hthread(ctx, index); -} - -DUK_EXTERNAL duk_context *duk_require_context(duk_context *ctx, duk_idx_t index) { - DUK_ASSERT_CTX_VALID(ctx); - - return (duk_context *) duk_require_hthread(ctx, index); -} - -DUK_EXTERNAL void *duk_get_heapptr(duk_context *ctx, duk_idx_t index) { - duk_tval *tv; - void *ret; - - DUK_ASSERT_CTX_VALID(ctx); - - tv = duk_get_tval(ctx, index); - if (tv && DUK_TVAL_IS_HEAP_ALLOCATED(tv)) { - ret = (void *) DUK_TVAL_GET_HEAPHDR(tv); - DUK_ASSERT(ret != NULL); - return ret; - } - - return (void *) NULL; -} - -DUK_EXTERNAL void *duk_require_heapptr(duk_context *ctx, duk_idx_t index) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_tval *tv; - void *ret; - - DUK_ASSERT_CTX_VALID(ctx); - - tv = duk_require_tval(ctx, index); - DUK_ASSERT(tv != NULL); - if (DUK_TVAL_IS_HEAP_ALLOCATED(tv)) { - ret = (void *) DUK_TVAL_GET_HEAPHDR(tv); - DUK_ASSERT(ret != NULL); - return ret; - } - - DUK_ERROR_REQUIRE_TYPE_INDEX(thr, index, "heapobject", DUK_STR_UNEXPECTED_TYPE); - return (void *) NULL; /* not reachable */ -} - -#if 0 -/* This would be pointless: we'd return NULL for both lightfuncs and - * unexpected types. - */ -DUK_INTERNAL duk_hobject *duk_get_hobject_or_lfunc(duk_context *ctx, duk_idx_t index) { -} -#endif - -/* Useful for internal call sites where we either expect an object (function) - * or a lightfunc. Accepts an object (returned as is) or a lightfunc (coerced - * to an object). Return value is NULL if value is neither an object nor a - * lightfunc. - */ -DUK_INTERNAL duk_hobject *duk_get_hobject_or_lfunc_coerce(duk_context *ctx, duk_idx_t index) { - duk_tval *tv; - - DUK_ASSERT_CTX_VALID(ctx); - - tv = duk_require_tval(ctx, index); - DUK_ASSERT(tv != NULL); - if (DUK_TVAL_IS_OBJECT(tv)) { - return DUK_TVAL_GET_OBJECT(tv); - } else if (DUK_TVAL_IS_LIGHTFUNC(tv)) { - duk_to_object(ctx, index); - return duk_require_hobject(ctx, index); - } - - return NULL; -} - -/* Useful for internal call sites where we either expect an object (function) - * or a lightfunc. Returns NULL for a lightfunc. - */ -DUK_INTERNAL duk_hobject *duk_require_hobject_or_lfunc(duk_context *ctx, duk_idx_t index) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_tval *tv; - - DUK_ASSERT_CTX_VALID(ctx); - - tv = duk_require_tval(ctx, index); - DUK_ASSERT(tv != NULL); - if (DUK_TVAL_IS_OBJECT(tv)) { - return DUK_TVAL_GET_OBJECT(tv); - } else if (DUK_TVAL_IS_LIGHTFUNC(tv)) { - return NULL; - } - DUK_ERROR_REQUIRE_TYPE_INDEX(thr, index, "object", DUK_STR_NOT_OBJECT); - return NULL; /* not reachable */ -} - -/* Useful for internal call sites where we either expect an object (function) - * or a lightfunc. Accepts an object (returned as is) or a lightfunc (coerced - * to an object). Return value is never NULL. - */ -DUK_INTERNAL duk_hobject *duk_require_hobject_or_lfunc_coerce(duk_context *ctx, duk_idx_t index) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_tval *tv; - - DUK_ASSERT_CTX_VALID(ctx); - - tv = duk_require_tval(ctx, index); - if (DUK_TVAL_IS_OBJECT(tv)) { - return DUK_TVAL_GET_OBJECT(tv); - } else if (DUK_TVAL_IS_LIGHTFUNC(tv)) { - duk_to_object(ctx, index); - return duk_require_hobject(ctx, index); - } - DUK_ERROR_REQUIRE_TYPE_INDEX(thr, index, "object", DUK_STR_NOT_OBJECT); - return NULL; /* not reachable */ -} - -DUK_INTERNAL duk_hobject *duk_get_hobject_with_class(duk_context *ctx, duk_idx_t index, duk_small_uint_t classnum) { - duk_hobject *h; - - DUK_ASSERT_CTX_VALID(ctx); - DUK_ASSERT_DISABLE(classnum >= 0); /* unsigned */ - DUK_ASSERT(classnum <= DUK_HOBJECT_CLASS_MAX); - - h = (duk_hobject *) duk__get_tagged_heaphdr_raw(ctx, index, DUK_TAG_OBJECT); - if (h != NULL && DUK_HOBJECT_GET_CLASS_NUMBER(h) != classnum) { - h = NULL; - } - return h; -} - -DUK_INTERNAL duk_hobject *duk_require_hobject_with_class(duk_context *ctx, duk_idx_t index, duk_small_uint_t classnum) { - duk_hthread *thr; - duk_hobject *h; - - DUK_ASSERT_CTX_VALID(ctx); - DUK_ASSERT_DISABLE(classnum >= 0); /* unsigned */ - DUK_ASSERT(classnum <= DUK_HOBJECT_CLASS_MAX); - thr = (duk_hthread *) ctx; - - h = (duk_hobject *) duk__get_tagged_heaphdr_raw(ctx, index, DUK_TAG_OBJECT); - if (!(h != NULL && DUK_HOBJECT_GET_CLASS_NUMBER(h) == classnum)) { - duk_hstring *h_class; - h_class = DUK_HTHREAD_GET_STRING(thr, DUK_HOBJECT_CLASS_NUMBER_TO_STRIDX(classnum)); - DUK_UNREF(h_class); - - DUK_ERROR_REQUIRE_TYPE_INDEX(thr, index, (const char *) DUK_HSTRING_GET_DATA(h_class), DUK_STR_UNEXPECTED_TYPE); - } - return h; -} - -DUK_EXTERNAL duk_size_t duk_get_length(duk_context *ctx, duk_idx_t index) { - duk_tval *tv; - - DUK_ASSERT_CTX_VALID(ctx); - - tv = duk_get_tval(ctx, index); - if (!tv) { - return 0; - } - - switch (DUK_TVAL_GET_TAG(tv)) { - case DUK_TAG_UNDEFINED: - case DUK_TAG_NULL: - case DUK_TAG_BOOLEAN: - case DUK_TAG_POINTER: - return 0; - case DUK_TAG_STRING: { - duk_hstring *h = DUK_TVAL_GET_STRING(tv); - DUK_ASSERT(h != NULL); - return (duk_size_t) DUK_HSTRING_GET_CHARLEN(h); - } - case DUK_TAG_OBJECT: { - duk_hobject *h = DUK_TVAL_GET_OBJECT(tv); - DUK_ASSERT(h != NULL); - return (duk_size_t) duk_hobject_get_length((duk_hthread *) ctx, h); - } - case DUK_TAG_BUFFER: { - duk_hbuffer *h = DUK_TVAL_GET_BUFFER(tv); - DUK_ASSERT(h != NULL); - return (duk_size_t) DUK_HBUFFER_GET_SIZE(h); - } - case DUK_TAG_LIGHTFUNC: { - duk_small_uint_t lf_flags; - lf_flags = DUK_TVAL_GET_LIGHTFUNC_FLAGS(tv); - return (duk_size_t) DUK_LFUNC_FLAGS_GET_LENGTH(lf_flags); - } -#if defined(DUK_USE_FASTINT) - case DUK_TAG_FASTINT: -#endif - default: - /* number */ - DUK_ASSERT(!DUK_TVAL_IS_UNUSED(tv)); - DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv)); - return 0; - } - - DUK_UNREACHABLE(); -} - -DUK_INTERNAL void duk_set_length(duk_context *ctx, duk_idx_t index, duk_size_t length) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_hobject *h; - - DUK_ASSERT_CTX_VALID(ctx); - - h = duk_get_hobject(ctx, index); - if (!h) { - return; - } - - duk_hobject_set_length(thr, h, (duk_uint32_t) length); /* XXX: typing */ -} - -/* - * Conversions and coercions - * - * The conversion/coercions are in-place operations on the value stack. - * Some operations are implemented here directly, while others call a - * helper in duk_js_ops.c after validating arguments. - */ - -/* E5 Section 8.12.8 */ - -DUK_LOCAL duk_bool_t duk__defaultvalue_coerce_attempt(duk_context *ctx, duk_idx_t index, duk_small_int_t func_stridx) { - if (duk_get_prop_stridx(ctx, index, func_stridx)) { - /* [ ... func ] */ - if (duk_is_callable(ctx, -1)) { - duk_dup(ctx, index); /* -> [ ... func this ] */ - duk_call_method(ctx, 0); /* -> [ ... retval ] */ - if (duk_is_primitive(ctx, -1)) { - duk_replace(ctx, index); - return 1; - } - /* [ ... retval ]; popped below */ - } - } - duk_pop(ctx); /* [ ... func/retval ] -> [ ... ] */ - return 0; -} - -DUK_EXTERNAL void duk_to_defaultvalue(duk_context *ctx, duk_idx_t index, duk_int_t hint) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_hobject *obj; - /* inline initializer for coercers[] is not allowed by old compilers like BCC */ - duk_small_int_t coercers[2]; - - DUK_ASSERT_CTX_VALID(ctx); - DUK_ASSERT(thr != NULL); - - coercers[0] = DUK_STRIDX_VALUE_OF; - coercers[1] = DUK_STRIDX_TO_STRING; - - index = duk_require_normalize_index(ctx, index); - obj = duk_require_hobject_or_lfunc(ctx, index); - - if (hint == DUK_HINT_NONE) { - if (obj != NULL && DUK_HOBJECT_GET_CLASS_NUMBER(obj) == DUK_HOBJECT_CLASS_DATE) { - hint = DUK_HINT_STRING; - } else { - hint = DUK_HINT_NUMBER; - } - } - - if (hint == DUK_HINT_STRING) { - coercers[0] = DUK_STRIDX_TO_STRING; - coercers[1] = DUK_STRIDX_VALUE_OF; - } - - if (duk__defaultvalue_coerce_attempt(ctx, index, coercers[0])) { - return; - } - - if (duk__defaultvalue_coerce_attempt(ctx, index, coercers[1])) { - return; - } - - DUK_ERROR_TYPE(thr, DUK_STR_DEFAULTVALUE_COERCE_FAILED); -} - -DUK_EXTERNAL void duk_to_undefined(duk_context *ctx, duk_idx_t index) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_tval *tv; - - DUK_ASSERT_CTX_VALID(ctx); - DUK_UNREF(thr); - - tv = duk_require_tval(ctx, index); - DUK_ASSERT(tv != NULL); - DUK_TVAL_SET_UNDEFINED_UPDREF(thr, tv); /* side effects */ -} - -DUK_EXTERNAL void duk_to_null(duk_context *ctx, duk_idx_t index) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_tval *tv; - - DUK_ASSERT_CTX_VALID(ctx); - DUK_UNREF(thr); - - tv = duk_require_tval(ctx, index); - DUK_ASSERT(tv != NULL); - DUK_TVAL_SET_NULL_UPDREF(thr, tv); /* side effects */ -} - -/* E5 Section 9.1 */ -DUK_EXTERNAL void duk_to_primitive(duk_context *ctx, duk_idx_t index, duk_int_t hint) { - DUK_ASSERT_CTX_VALID(ctx); - DUK_ASSERT(hint == DUK_HINT_NONE || hint == DUK_HINT_NUMBER || hint == DUK_HINT_STRING); - - index = duk_require_normalize_index(ctx, index); - - if (!duk_check_type_mask(ctx, index, DUK_TYPE_MASK_OBJECT | - DUK_TYPE_MASK_LIGHTFUNC)) { - /* everything except object stay as is */ - return; - } - duk_to_defaultvalue(ctx, index, hint); -} - -/* E5 Section 9.2 */ -DUK_EXTERNAL duk_bool_t duk_to_boolean(duk_context *ctx, duk_idx_t index) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_tval *tv; - duk_bool_t val; - - DUK_ASSERT_CTX_VALID(ctx); - DUK_UNREF(thr); - - index = duk_require_normalize_index(ctx, index); - - tv = duk_require_tval(ctx, index); - DUK_ASSERT(tv != NULL); - - val = duk_js_toboolean(tv); - DUK_ASSERT(val == 0 || val == 1); - - /* Note: no need to re-lookup tv, conversion is side effect free */ - DUK_ASSERT(tv != NULL); - DUK_TVAL_SET_BOOLEAN_UPDREF(thr, tv, val); /* side effects */ - return val; -} - -DUK_EXTERNAL duk_double_t duk_to_number(duk_context *ctx, duk_idx_t index) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_tval *tv; - duk_double_t d; - - DUK_ASSERT_CTX_VALID(ctx); - - tv = duk_require_tval(ctx, index); - DUK_ASSERT(tv != NULL); - /* XXX: fastint? */ - d = duk_js_tonumber(thr, tv); - - /* Note: need to re-lookup because ToNumber() may have side effects */ - tv = duk_require_tval(ctx, index); - DUK_TVAL_SET_NUMBER_UPDREF(thr, tv, d); /* side effects */ - return d; -} - -/* XXX: combine all the integer conversions: they share everything - * but the helper function for coercion. - */ - -typedef duk_double_t (*duk__toint_coercer)(duk_hthread *thr, duk_tval *tv); - -DUK_LOCAL duk_double_t duk__to_int_uint_helper(duk_context *ctx, duk_idx_t index, duk__toint_coercer coerce_func) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_tval *tv; - duk_double_t d; - - DUK_ASSERT_CTX_VALID(ctx); - - tv = duk_require_tval(ctx, index); - DUK_ASSERT(tv != NULL); - d = coerce_func(thr, tv); - - /* XXX: fastint? */ - - /* Relookup in case coerce_func() has side effects, e.g. ends up coercing an object */ - tv = duk_require_tval(ctx, index); - DUK_TVAL_SET_NUMBER_UPDREF(thr, tv, d); /* side effects */ - return d; -} - -DUK_EXTERNAL duk_int_t duk_to_int(duk_context *ctx, duk_idx_t index) { - /* Value coercion (in stack): ToInteger(), E5 Section 9.4 - * API return value coercion: custom - */ - DUK_ASSERT_CTX_VALID(ctx); - (void) duk__to_int_uint_helper(ctx, index, duk_js_tointeger); - return (duk_int_t) duk__api_coerce_d2i(ctx, index, 0 /*require*/); -} - -DUK_EXTERNAL duk_uint_t duk_to_uint(duk_context *ctx, duk_idx_t index) { - /* Value coercion (in stack): ToInteger(), E5 Section 9.4 - * API return value coercion: custom - */ - DUK_ASSERT_CTX_VALID(ctx); - (void) duk__to_int_uint_helper(ctx, index, duk_js_tointeger); - return (duk_uint_t) duk__api_coerce_d2ui(ctx, index, 0 /*require*/); -} - -DUK_EXTERNAL duk_int32_t duk_to_int32(duk_context *ctx, duk_idx_t index) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_tval *tv; - duk_int32_t ret; - - DUK_ASSERT_CTX_VALID(ctx); - - tv = duk_require_tval(ctx, index); - DUK_ASSERT(tv != NULL); - ret = duk_js_toint32(thr, tv); - - /* Relookup in case coerce_func() has side effects, e.g. ends up coercing an object */ - tv = duk_require_tval(ctx, index); - DUK_TVAL_SET_FASTINT_I32_UPDREF(thr, tv, ret); /* side effects */ - return ret; -} - -DUK_EXTERNAL duk_uint32_t duk_to_uint32(duk_context *ctx, duk_idx_t index) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_tval *tv; - duk_uint32_t ret; - - DUK_ASSERT_CTX_VALID(ctx); - - tv = duk_require_tval(ctx, index); - DUK_ASSERT(tv != NULL); - ret = duk_js_touint32(thr, tv); - - /* Relookup in case coerce_func() has side effects, e.g. ends up coercing an object */ - tv = duk_require_tval(ctx, index); - DUK_TVAL_SET_FASTINT_U32_UPDREF(thr, tv, ret); /* side effects */ - return ret; -} - -DUK_EXTERNAL duk_uint16_t duk_to_uint16(duk_context *ctx, duk_idx_t index) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_tval *tv; - duk_uint16_t ret; - - DUK_ASSERT_CTX_VALID(ctx); - - tv = duk_require_tval(ctx, index); - DUK_ASSERT(tv != NULL); - ret = duk_js_touint16(thr, tv); - - /* Relookup in case coerce_func() has side effects, e.g. ends up coercing an object */ - tv = duk_require_tval(ctx, index); - DUK_TVAL_SET_FASTINT_U32_UPDREF(thr, tv, ret); /* side effects */ - return ret; -} - -#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) -/* Special coercion for Uint8ClampedArray. */ -DUK_INTERNAL duk_uint8_t duk_to_uint8clamped(duk_context *ctx, duk_idx_t index) { - duk_double_t d; - duk_double_t t; - duk_uint8_t ret; - - /* XXX: Simplify this algorithm, should be possible to come up with - * a shorter and faster algorithm by inspecting IEEE representation - * directly. - */ - - d = duk_to_number(ctx, index); - if (d <= 0.0) { - return 0; - } else if (d >= 255) { - return 255; - } else if (DUK_ISNAN(d)) { - /* Avoid NaN-to-integer coercion as it is compiler specific. */ - return 0; - } - - t = d - DUK_FLOOR(d); - if (t == 0.5) { - /* Exact halfway, round to even. */ - ret = (duk_uint8_t) d; - ret = (ret + 1) & 0xfe; /* Example: d=3.5, t=0.5 -> ret = (3 + 1) & 0xfe = 4 & 0xfe = 4 - * Example: d=4.5, t=0.5 -> ret = (4 + 1) & 0xfe = 5 & 0xfe = 4 - */ - } else { - /* Not halfway, round to nearest. */ - ret = (duk_uint8_t) (d + 0.5); - } - return ret; -} -#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ - -DUK_EXTERNAL const char *duk_to_lstring(duk_context *ctx, duk_idx_t index, duk_size_t *out_len) { - DUK_ASSERT_CTX_VALID(ctx); - - (void) duk_to_string(ctx, index); - return duk_require_lstring(ctx, index, out_len); -} - -DUK_LOCAL duk_ret_t duk__safe_to_string_raw(duk_context *ctx) { - DUK_ASSERT_CTX_VALID(ctx); - - duk_to_string(ctx, -1); - return 1; -} - -DUK_EXTERNAL const char *duk_safe_to_lstring(duk_context *ctx, duk_idx_t index, duk_size_t *out_len) { - DUK_ASSERT_CTX_VALID(ctx); - - index = duk_require_normalize_index(ctx, index); - - /* We intentionally ignore the duk_safe_call() return value and only - * check the output type. This way we don't also need to check that - * the returned value is indeed a string in the success case. - */ - - duk_dup(ctx, index); - (void) duk_safe_call(ctx, duk__safe_to_string_raw, 1 /*nargs*/, 1 /*nrets*/); - if (!duk_is_string(ctx, -1)) { - /* Error: try coercing error to string once. */ - (void) duk_safe_call(ctx, duk__safe_to_string_raw, 1 /*nargs*/, 1 /*nrets*/); - if (!duk_is_string(ctx, -1)) { - /* Double error */ - duk_pop(ctx); - duk_push_hstring_stridx(ctx, DUK_STRIDX_UC_ERROR); - } else { - ; - } - } else { - ; - } - DUK_ASSERT(duk_is_string(ctx, -1)); - DUK_ASSERT(duk_get_string(ctx, -1) != NULL); - - duk_replace(ctx, index); - return duk_get_lstring(ctx, index, out_len); -} - -#if defined(DUK_USE_DEBUGGER_SUPPORT) /* only needed by debugger for now */ -DUK_INTERNAL duk_hstring *duk_safe_to_hstring(duk_context *ctx, duk_idx_t index) { - (void) duk_safe_to_string(ctx, index); - DUK_ASSERT(duk_is_string(ctx, index)); - DUK_ASSERT(duk_get_hstring(ctx, index) != NULL); - return duk_get_hstring(ctx, index); -} -#endif - -/* Coerce top into Object.prototype.toString() output. */ -DUK_INTERNAL void duk_to_object_class_string_top(duk_context *ctx) { - duk_hthread *thr; - duk_uint_t typemask; - duk_hstring *h_strclass; - - DUK_ASSERT_CTX_VALID(ctx); - thr = (duk_hthread *) ctx; - DUK_UNREF(thr); - - typemask = duk_get_type_mask(ctx, -1); - if (typemask & DUK_TYPE_MASK_UNDEFINED) { - h_strclass = DUK_HTHREAD_STRING_UC_UNDEFINED(thr); - } else if (typemask & DUK_TYPE_MASK_NULL) { - h_strclass = DUK_HTHREAD_STRING_UC_NULL(thr); - } else { - duk_hobject *h_obj; - - duk_to_object(ctx, -1); - h_obj = duk_get_hobject(ctx, -1); - DUK_ASSERT(h_obj != NULL); - - h_strclass = DUK_HOBJECT_GET_CLASS_STRING(thr->heap, h_obj); - } - DUK_ASSERT(h_strclass != NULL); - - duk_pop(ctx); - duk_push_sprintf(ctx, "[object %s]", (const char *) DUK_HSTRING_GET_DATA(h_strclass)); -} - -#if !defined(DUK_USE_PARANOID_ERRORS) -DUK_INTERNAL void duk_push_hobject_class_string(duk_context *ctx, duk_hobject *h) { - duk_hthread *thr; - duk_hstring *h_strclass; - - DUK_ASSERT_CTX_VALID(ctx); - DUK_ASSERT(h != NULL); - thr = (duk_hthread *) ctx; - DUK_UNREF(thr); - - h_strclass = DUK_HOBJECT_GET_CLASS_STRING(thr->heap, h); - DUK_ASSERT(h_strclass != NULL); - duk_push_sprintf(ctx, "[object %s]", (const char *) DUK_HSTRING_GET_DATA(h_strclass)); -} -#endif /* !DUK_USE_PARANOID_ERRORS */ - -/* XXX: other variants like uint, u32 etc */ -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_hthread *thr = (duk_hthread *) ctx; - duk_tval *tv; - duk_tval tv_tmp; - duk_double_t d, dmin, dmax; - duk_int_t res; - duk_bool_t clamped = 0; - - DUK_ASSERT_CTX_VALID(ctx); - - tv = duk_require_tval(ctx, index); - DUK_ASSERT(tv != NULL); - d = duk_js_tointeger(thr, tv); /* E5 Section 9.4, ToInteger() */ - - dmin = (duk_double_t) minval; - dmax = (duk_double_t) maxval; - - if (d < dmin) { - clamped = 1; - res = minval; - d = dmin; - } else if (d > dmax) { - clamped = 1; - res = maxval; - d = dmax; - } else { - res = (duk_int_t) d; - } - DUK_UNREF(d); /* SCANBUILD: with suitable dmin/dmax limits 'd' is unused */ - /* 'd' and 'res' agree here */ - - /* Relookup in case duk_js_tointeger() ends up e.g. coercing an object. */ - tv = duk_get_tval(ctx, index); - DUK_ASSERT(tv != NULL); /* not popped by side effect */ - DUK_TVAL_SET_TVAL(&tv_tmp, tv); -#if defined(DUK_USE_FASTINT) -#if (DUK_INT_MAX <= 0x7fffffffL) - DUK_TVAL_SET_FASTINT_I32(tv, res); -#else - /* Clamping needed if duk_int_t is 64 bits. */ - if (res >= DUK_FASTINT_MIN && res <= DUK_FASTINT_MAX) { - DUK_TVAL_SET_FASTINT(tv, res); - } else { - DUK_TVAL_SET_NUMBER(tv, d); - } -#endif -#else - DUK_TVAL_SET_NUMBER(tv, d); /* no need to incref */ -#endif - DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */ - - if (out_clamped) { - *out_clamped = clamped; - } else { - /* coerced value is updated to value stack even when RangeError thrown */ - if (clamped) { - DUK_ERROR_RANGE(thr, DUK_STR_NUMBER_OUTSIDE_RANGE); - } - } - - return res; -} - -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_bool_t dummy; - return duk_to_int_clamped_raw(ctx, index, minval, maxval, &dummy); -} - -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) { - return duk_to_int_clamped_raw(ctx, index, minval, maxval, NULL); /* out_clamped==NULL -> RangeError if outside range */ -} - -DUK_EXTERNAL const char *duk_to_string(duk_context *ctx, duk_idx_t index) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_tval *tv; - - DUK_ASSERT_CTX_VALID(ctx); - DUK_UNREF(thr); - - index = duk_require_normalize_index(ctx, index); - - tv = duk_require_tval(ctx, index); - DUK_ASSERT(tv != NULL); - - switch (DUK_TVAL_GET_TAG(tv)) { - case DUK_TAG_UNDEFINED: { - duk_push_hstring_stridx(ctx, DUK_STRIDX_LC_UNDEFINED); - break; - } - case DUK_TAG_NULL: { - duk_push_hstring_stridx(ctx, DUK_STRIDX_LC_NULL); - break; - } - case DUK_TAG_BOOLEAN: { - if (DUK_TVAL_GET_BOOLEAN(tv)) { - duk_push_hstring_stridx(ctx, DUK_STRIDX_TRUE); - } else { - duk_push_hstring_stridx(ctx, DUK_STRIDX_FALSE); - } - break; - } - case DUK_TAG_STRING: { - /* nop */ - goto skip_replace; - } - case DUK_TAG_OBJECT: { - duk_to_primitive(ctx, index, DUK_HINT_STRING); - return duk_to_string(ctx, index); /* Note: recursive call */ - } - case DUK_TAG_BUFFER: { - duk_hbuffer *h = DUK_TVAL_GET_BUFFER(tv); - - /* Note: this allows creation of internal strings. */ - - DUK_ASSERT(h != NULL); - duk_push_lstring(ctx, - (const char *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h), - (duk_size_t) DUK_HBUFFER_GET_SIZE(h)); - break; - } - case DUK_TAG_POINTER: { - void *ptr = DUK_TVAL_GET_POINTER(tv); - if (ptr != NULL) { - duk_push_sprintf(ctx, DUK_STR_FMT_PTR, (void *) ptr); - } else { - /* Represent a null pointer as 'null' to be consistent with - * the JX format variant. Native '%p' format for a NULL - * pointer may be e.g. '(nil)'. - */ - duk_push_hstring_stridx(ctx, DUK_STRIDX_LC_NULL); - } - break; - } - case DUK_TAG_LIGHTFUNC: { - /* Should match Function.prototype.toString() */ - duk_push_lightfunc_tostring(ctx, tv); - break; - } -#if defined(DUK_USE_FASTINT) - case DUK_TAG_FASTINT: -#endif - default: { - /* number */ - DUK_ASSERT(!DUK_TVAL_IS_UNUSED(tv)); - DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv)); - duk_push_tval(ctx, tv); - duk_numconv_stringify(ctx, - 10 /*radix*/, - 0 /*precision:shortest*/, - 0 /*force_exponential*/); - break; - } - } - - duk_replace(ctx, index); - - skip_replace: - return duk_require_string(ctx, index); -} - -DUK_INTERNAL duk_hstring *duk_to_hstring(duk_context *ctx, duk_idx_t index) { - duk_hstring *ret; - DUK_ASSERT_CTX_VALID(ctx); - duk_to_string(ctx, index); - ret = duk_get_hstring(ctx, index); - DUK_ASSERT(ret != NULL); - return ret; -} - -DUK_EXTERNAL void *duk_to_buffer_raw(duk_context *ctx, duk_idx_t index, duk_size_t *out_size, duk_uint_t mode) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_hbuffer *h_buf; - const duk_uint8_t *src_data; - duk_size_t src_size; - duk_uint8_t *dst_data; - - DUK_ASSERT_CTX_VALID(ctx); - DUK_UNREF(thr); - - index = duk_require_normalize_index(ctx, index); - - h_buf = duk_get_hbuffer(ctx, index); - if (h_buf != NULL) { - /* Buffer is kept as is, with the fixed/dynamic nature of the - * buffer only changed if requested. An external buffer - * is converted into a non-external dynamic buffer in a - * duk_to_dynamic_buffer() call. - */ - duk_uint_t tmp; - duk_uint8_t *tmp_ptr; - - tmp_ptr = (duk_uint8_t *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h_buf); - src_data = (const duk_uint8_t *) tmp_ptr; - src_size = DUK_HBUFFER_GET_SIZE(h_buf); - - tmp = (DUK_HBUFFER_HAS_DYNAMIC(h_buf) ? DUK_BUF_MODE_DYNAMIC : DUK_BUF_MODE_FIXED); - if ((tmp == mode && !DUK_HBUFFER_HAS_EXTERNAL(h_buf)) || - mode == DUK_BUF_MODE_DONTCARE) { - /* Note: src_data may be NULL if input is a zero-size - * dynamic buffer. - */ - dst_data = tmp_ptr; - goto skip_copy; - } - } else { - /* Non-buffer value is first ToString() coerced, then converted - * to a buffer (fixed buffer is used unless a dynamic buffer is - * explicitly requested). - */ - - src_data = (const duk_uint8_t *) duk_to_lstring(ctx, index, &src_size); - } - - dst_data = (duk_uint8_t *) duk_push_buffer(ctx, src_size, (mode == DUK_BUF_MODE_DYNAMIC) /*dynamic*/); - if (DUK_LIKELY(src_size > 0)) { - /* When src_size == 0, src_data may be NULL (if source - * buffer is dynamic), and dst_data may be NULL (if - * target buffer is dynamic). Avoid zero-size memcpy() - * with an invalid pointer. - */ - DUK_MEMCPY((void *) dst_data, (const void *) src_data, (size_t) src_size); - } - duk_replace(ctx, index); - skip_copy: - - if (out_size) { - *out_size = src_size; - } - return dst_data; -} - -DUK_EXTERNAL void *duk_to_pointer(duk_context *ctx, duk_idx_t index) { - duk_tval *tv; - void *res; - - DUK_ASSERT_CTX_VALID(ctx); - - index = duk_require_normalize_index(ctx, index); - - tv = duk_require_tval(ctx, index); - DUK_ASSERT(tv != NULL); - - switch (DUK_TVAL_GET_TAG(tv)) { - case DUK_TAG_UNDEFINED: - case DUK_TAG_NULL: - case DUK_TAG_BOOLEAN: - res = NULL; - break; - case DUK_TAG_POINTER: - res = DUK_TVAL_GET_POINTER(tv); - break; - case DUK_TAG_STRING: - case DUK_TAG_OBJECT: - case DUK_TAG_BUFFER: - /* Heap allocated: return heap pointer which is NOT useful - * for the caller, except for debugging. - */ - res = (void *) DUK_TVAL_GET_HEAPHDR(tv); - break; - case DUK_TAG_LIGHTFUNC: - /* Function pointers do not always cast correctly to void * - * (depends on memory and segmentation model for instance), - * so they coerce to NULL. - */ - res = NULL; - break; -#if defined(DUK_USE_FASTINT) - case DUK_TAG_FASTINT: -#endif - default: - /* number */ - DUK_ASSERT(!DUK_TVAL_IS_UNUSED(tv)); - DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv)); - res = NULL; - break; - } - - duk_push_pointer(ctx, res); - duk_replace(ctx, index); - return res; -} - -DUK_EXTERNAL void duk_to_object(duk_context *ctx, duk_idx_t index) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_tval *tv; - duk_uint_t flags = 0; /* shared flags for a subset of types */ - duk_small_int_t proto = 0; - - DUK_ASSERT_CTX_VALID(ctx); - - index = duk_require_normalize_index(ctx, index); - - tv = duk_require_tval(ctx, index); - DUK_ASSERT(tv != NULL); - - switch (DUK_TVAL_GET_TAG(tv)) { - case DUK_TAG_UNDEFINED: - case DUK_TAG_NULL: { - DUK_ERROR_TYPE(thr, DUK_STR_NOT_OBJECT_COERCIBLE); - break; - } - case DUK_TAG_BOOLEAN: { - flags = DUK_HOBJECT_FLAG_EXTENSIBLE | - DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_BOOLEAN); - proto = DUK_BIDX_BOOLEAN_PROTOTYPE; - goto create_object; - } - case DUK_TAG_STRING: { - flags = DUK_HOBJECT_FLAG_EXTENSIBLE | - DUK_HOBJECT_FLAG_EXOTIC_STRINGOBJ | - DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_STRING); - proto = DUK_BIDX_STRING_PROTOTYPE; - goto create_object; - } - case DUK_TAG_OBJECT: { - /* nop */ - break; - } - case DUK_TAG_BUFFER: { - /* A plain buffer coerces to a Duktape.Buffer because it's the - * object counterpart of the plain buffer value. But it might - * still make more sense to produce an ArrayBuffer here? - */ - - duk_hbufferobject *h_bufobj; - duk_hbuffer *h_val; - - h_val = DUK_TVAL_GET_BUFFER(tv); - DUK_ASSERT(h_val != NULL); - - h_bufobj = duk_push_bufferobject_raw(ctx, - DUK_HOBJECT_FLAG_EXTENSIBLE | - DUK_HOBJECT_FLAG_BUFFEROBJECT | - DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_BUFFER), - DUK_BIDX_BUFFER_PROTOTYPE); - DUK_ASSERT(h_bufobj != NULL); - DUK_ASSERT(DUK_HOBJECT_HAS_EXTENSIBLE((duk_hobject *) h_bufobj)); - DUK_ASSERT(DUK_HOBJECT_IS_BUFFEROBJECT((duk_hobject *) h_bufobj)); - - h_bufobj->buf = h_val; - DUK_HBUFFER_INCREF(thr, h_val); - DUK_ASSERT(h_bufobj->offset == 0); - h_bufobj->length = (duk_uint_t) DUK_HBUFFER_GET_SIZE(h_val); - DUK_ASSERT(h_bufobj->shift == 0); - DUK_ASSERT(h_bufobj->elem_type == DUK_HBUFFEROBJECT_ELEM_UINT8); - - DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj); - goto replace_value; - } - case DUK_TAG_POINTER: { - flags = DUK_HOBJECT_FLAG_EXTENSIBLE | - DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_POINTER); - proto = DUK_BIDX_POINTER_PROTOTYPE; - goto create_object; - } - case DUK_TAG_LIGHTFUNC: { - /* Lightfunc coerces to a Function instance with concrete - * properties. Since 'length' is virtual for Duktape/C - * functions, don't need to define that. - * - * The result is made extensible to mimic what happens to - * strings: - * > Object.isExtensible(Object('foo')) - * true - */ - duk_small_uint_t lf_flags; - duk_idx_t nargs; - duk_small_uint_t lf_len; - duk_c_function func; - duk_hnativefunction *nf; - - DUK_TVAL_GET_LIGHTFUNC(tv, func, lf_flags); - - nargs = (duk_idx_t) DUK_LFUNC_FLAGS_GET_NARGS(lf_flags); - if (nargs == DUK_LFUNC_NARGS_VARARGS) { - nargs = (duk_idx_t) DUK_VARARGS; - } - flags = DUK_HOBJECT_FLAG_EXTENSIBLE | - DUK_HOBJECT_FLAG_CONSTRUCTABLE | - DUK_HOBJECT_FLAG_NATIVEFUNCTION | - DUK_HOBJECT_FLAG_NEWENV | - DUK_HOBJECT_FLAG_STRICT | - DUK_HOBJECT_FLAG_NOTAIL | - /* DUK_HOBJECT_FLAG_EXOTIC_DUKFUNC: omitted here intentionally */ - DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_FUNCTION); - (void) duk__push_c_function_raw(ctx, func, nargs, flags); - - lf_len = DUK_LFUNC_FLAGS_GET_LENGTH(lf_flags); - if ((duk_idx_t) lf_len != nargs) { - /* Explicit length is only needed if it differs from 'nargs'. */ - duk_push_int(ctx, (duk_int_t) lf_len); - duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_LENGTH, DUK_PROPDESC_FLAGS_NONE); - } - duk_push_lightfunc_name(ctx, tv); - duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_NAME, DUK_PROPDESC_FLAGS_NONE); - - nf = duk_get_hnativefunction(ctx, -1); - DUK_ASSERT(nf != NULL); - nf->magic = (duk_int16_t) DUK_LFUNC_FLAGS_GET_MAGIC(lf_flags); - - /* Enable DUKFUNC exotic behavior once properties are set up. */ - DUK_HOBJECT_SET_EXOTIC_DUKFUNC((duk_hobject *) nf); - goto replace_value; - } -#if defined(DUK_USE_FASTINT) - case DUK_TAG_FASTINT: -#endif - default: { - DUK_ASSERT(!DUK_TVAL_IS_UNUSED(tv)); - DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv)); - flags = DUK_HOBJECT_FLAG_EXTENSIBLE | - DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_NUMBER); - proto = DUK_BIDX_NUMBER_PROTOTYPE; - goto create_object; - } - } - return; - - create_object: - (void) duk_push_object_helper(ctx, flags, proto); - - /* Note: Boolean prototype's internal value property is not writable, - * but duk_xdef_prop_stridx() disregards the write protection. Boolean - * instances are immutable. - * - * String and buffer special behaviors are already enabled which is not - * ideal, but a write to the internal value is not affected by them. - */ - duk_dup(ctx, index); - duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_INT_VALUE, DUK_PROPDESC_FLAGS_NONE); - - replace_value: - duk_replace(ctx, index); -} - -/* - * Type checking - */ - -DUK_LOCAL duk_bool_t duk__tag_check(duk_context *ctx, duk_idx_t index, duk_small_uint_t tag) { - duk_tval *tv; - - tv = duk_get_tval(ctx, index); - if (!tv) { - return 0; - } - return (DUK_TVAL_GET_TAG(tv) == tag); -} - -DUK_LOCAL duk_bool_t duk__obj_flag_any_default_false(duk_context *ctx, duk_idx_t index, duk_uint_t flag_mask) { - duk_hobject *obj; - - DUK_ASSERT_CTX_VALID(ctx); - - obj = duk_get_hobject(ctx, index); - if (obj) { - return (DUK_HEAPHDR_CHECK_FLAG_BITS((duk_heaphdr *) obj, flag_mask) ? 1 : 0); - } - return 0; -} - -DUK_EXTERNAL duk_int_t duk_get_type(duk_context *ctx, duk_idx_t index) { - duk_tval *tv; - - DUK_ASSERT_CTX_VALID(ctx); - - tv = duk_get_tval(ctx, index); - if (!tv) { - return DUK_TYPE_NONE; - } - switch (DUK_TVAL_GET_TAG(tv)) { - case DUK_TAG_UNDEFINED: - return DUK_TYPE_UNDEFINED; - case DUK_TAG_NULL: - return DUK_TYPE_NULL; - case DUK_TAG_BOOLEAN: - return DUK_TYPE_BOOLEAN; - case DUK_TAG_STRING: - return DUK_TYPE_STRING; - case DUK_TAG_OBJECT: - return DUK_TYPE_OBJECT; - case DUK_TAG_BUFFER: - return DUK_TYPE_BUFFER; - case DUK_TAG_POINTER: - return DUK_TYPE_POINTER; - case DUK_TAG_LIGHTFUNC: - return DUK_TYPE_LIGHTFUNC; -#if defined(DUK_USE_FASTINT) - case DUK_TAG_FASTINT: -#endif - default: - /* Note: number has no explicit tag (in 8-byte representation) */ - DUK_ASSERT(!DUK_TVAL_IS_UNUSED(tv)); - DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv)); - return DUK_TYPE_NUMBER; - } - DUK_UNREACHABLE(); -} - -#if defined(DUK_USE_VERBOSE_ERRORS) && defined(DUK_USE_PARANOID_ERRORS) -DUK_LOCAL const char *duk__type_names[] = { - "none", - "undefined", - "null", - "boolean", - "number", - "string", - "object", - "buffer", - "pointer", - "lightfunc" -}; - -DUK_INTERNAL const char *duk_get_type_name(duk_context *ctx, duk_idx_t index) { - duk_int_t type_tag; - - type_tag = duk_get_type(ctx, index); - DUK_ASSERT(type_tag >= DUK_TYPE_MIN && type_tag <= DUK_TYPE_MAX); - DUK_ASSERT(DUK_TYPE_MIN == 0 && sizeof(duk__type_names) / sizeof(const char *) == DUK_TYPE_MAX + 1); - - return duk__type_names[type_tag]; -} -#endif - -DUK_EXTERNAL duk_bool_t duk_check_type(duk_context *ctx, duk_idx_t index, duk_int_t type) { - DUK_ASSERT_CTX_VALID(ctx); - - return (duk_get_type(ctx, index) == type) ? 1 : 0; -} - -DUK_EXTERNAL duk_uint_t duk_get_type_mask(duk_context *ctx, duk_idx_t index) { - duk_tval *tv; - - DUK_ASSERT_CTX_VALID(ctx); - - tv = duk_get_tval(ctx, index); - if (!tv) { - return DUK_TYPE_MASK_NONE; - } - switch (DUK_TVAL_GET_TAG(tv)) { - case DUK_TAG_UNDEFINED: - return DUK_TYPE_MASK_UNDEFINED; - case DUK_TAG_NULL: - return DUK_TYPE_MASK_NULL; - case DUK_TAG_BOOLEAN: - return DUK_TYPE_MASK_BOOLEAN; - case DUK_TAG_STRING: - return DUK_TYPE_MASK_STRING; - case DUK_TAG_OBJECT: - return DUK_TYPE_MASK_OBJECT; - case DUK_TAG_BUFFER: - return DUK_TYPE_MASK_BUFFER; - case DUK_TAG_POINTER: - return DUK_TYPE_MASK_POINTER; - case DUK_TAG_LIGHTFUNC: - return DUK_TYPE_MASK_LIGHTFUNC; -#if defined(DUK_USE_FASTINT) - case DUK_TAG_FASTINT: -#endif - default: - /* Note: number has no explicit tag (in 8-byte representation) */ - DUK_ASSERT(!DUK_TVAL_IS_UNUSED(tv)); - DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv)); - return DUK_TYPE_MASK_NUMBER; - } - DUK_UNREACHABLE(); -} - -DUK_EXTERNAL duk_bool_t duk_check_type_mask(duk_context *ctx, duk_idx_t index, duk_uint_t mask) { - duk_hthread *thr = (duk_hthread *) ctx; - - DUK_ASSERT_CTX_VALID(ctx); - - if (duk_get_type_mask(ctx, index) & mask) { - return 1; - } - if (mask & DUK_TYPE_MASK_THROW) { - DUK_ERROR_TYPE(thr, DUK_STR_UNEXPECTED_TYPE); - DUK_UNREACHABLE(); - } - return 0; -} - -DUK_EXTERNAL duk_bool_t duk_is_undefined(duk_context *ctx, duk_idx_t index) { - DUK_ASSERT_CTX_VALID(ctx); - return duk__tag_check(ctx, index, DUK_TAG_UNDEFINED); -} - -DUK_EXTERNAL duk_bool_t duk_is_null(duk_context *ctx, duk_idx_t index) { - DUK_ASSERT_CTX_VALID(ctx); - return duk__tag_check(ctx, index, DUK_TAG_NULL); -} - -DUK_EXTERNAL duk_bool_t duk_is_null_or_undefined(duk_context *ctx, duk_idx_t index) { - duk_tval *tv; - duk_small_uint_t tag; - - DUK_ASSERT_CTX_VALID(ctx); - - tv = duk_get_tval(ctx, index); - if (!tv) { - return 0; - } - tag = DUK_TVAL_GET_TAG(tv); - return (tag == DUK_TAG_UNDEFINED) || (tag == DUK_TAG_NULL); -} - -DUK_EXTERNAL duk_bool_t duk_is_boolean(duk_context *ctx, duk_idx_t index) { - DUK_ASSERT_CTX_VALID(ctx); - return duk__tag_check(ctx, index, DUK_TAG_BOOLEAN); -} - -DUK_EXTERNAL duk_bool_t duk_is_number(duk_context *ctx, duk_idx_t index) { - duk_tval *tv; - - DUK_ASSERT_CTX_VALID(ctx); - - /* - * Number is special because it doesn't have a specific - * tag in the 8-byte representation. - */ - - /* XXX: shorter version for 12-byte representation? */ - - tv = duk_get_tval(ctx, index); - if (!tv) { - return 0; - } - return DUK_TVAL_IS_NUMBER(tv); -} - -DUK_EXTERNAL duk_bool_t duk_is_nan(duk_context *ctx, duk_idx_t index) { - /* XXX: This will now return false for non-numbers, even though they would - * coerce to NaN (as a general rule). In particular, duk_get_number() - * returns a NaN for non-numbers, so should this function also return - * true for non-numbers? - */ - - duk_tval *tv; - - DUK_ASSERT_CTX_VALID(ctx); - - tv = duk_get_tval(ctx, index); - if (!tv || !DUK_TVAL_IS_NUMBER(tv)) { - return 0; - } - return DUK_ISNAN(DUK_TVAL_GET_NUMBER(tv)); -} - -DUK_EXTERNAL duk_bool_t duk_is_string(duk_context *ctx, duk_idx_t index) { - DUK_ASSERT_CTX_VALID(ctx); - return duk__tag_check(ctx, index, DUK_TAG_STRING); -} - -DUK_EXTERNAL duk_bool_t duk_is_object(duk_context *ctx, duk_idx_t index) { - DUK_ASSERT_CTX_VALID(ctx); - return duk__tag_check(ctx, index, DUK_TAG_OBJECT); -} - -DUK_EXTERNAL duk_bool_t duk_is_buffer(duk_context *ctx, duk_idx_t index) { - DUK_ASSERT_CTX_VALID(ctx); - return duk__tag_check(ctx, index, DUK_TAG_BUFFER); -} - -DUK_EXTERNAL duk_bool_t duk_is_pointer(duk_context *ctx, duk_idx_t index) { - DUK_ASSERT_CTX_VALID(ctx); - return duk__tag_check(ctx, index, DUK_TAG_POINTER); -} - -DUK_EXTERNAL duk_bool_t duk_is_lightfunc(duk_context *ctx, duk_idx_t index) { - DUK_ASSERT_CTX_VALID(ctx); - return duk__tag_check(ctx, index, DUK_TAG_LIGHTFUNC); -} - -DUK_EXTERNAL duk_bool_t duk_is_array(duk_context *ctx, duk_idx_t index) { - duk_hobject *obj; - - DUK_ASSERT_CTX_VALID(ctx); - - obj = duk_get_hobject(ctx, index); - if (obj) { - return (DUK_HOBJECT_GET_CLASS_NUMBER(obj) == DUK_HOBJECT_CLASS_ARRAY ? 1 : 0); - } - return 0; -} - -DUK_EXTERNAL duk_bool_t duk_is_function(duk_context *ctx, duk_idx_t index) { - duk_tval *tv; - - DUK_ASSERT_CTX_VALID(ctx); - - tv = duk_get_tval(ctx, index); - if (tv && DUK_TVAL_IS_LIGHTFUNC(tv)) { - return 1; - } - return duk__obj_flag_any_default_false(ctx, - index, - DUK_HOBJECT_FLAG_COMPILEDFUNCTION | - DUK_HOBJECT_FLAG_NATIVEFUNCTION | - DUK_HOBJECT_FLAG_BOUND); -} - -DUK_EXTERNAL duk_bool_t duk_is_c_function(duk_context *ctx, duk_idx_t index) { - DUK_ASSERT_CTX_VALID(ctx); - return duk__obj_flag_any_default_false(ctx, - index, - DUK_HOBJECT_FLAG_NATIVEFUNCTION); -} - -DUK_EXTERNAL duk_bool_t duk_is_ecmascript_function(duk_context *ctx, duk_idx_t index) { - DUK_ASSERT_CTX_VALID(ctx); - return duk__obj_flag_any_default_false(ctx, - index, - DUK_HOBJECT_FLAG_COMPILEDFUNCTION); -} - -DUK_EXTERNAL duk_bool_t duk_is_bound_function(duk_context *ctx, duk_idx_t index) { - DUK_ASSERT_CTX_VALID(ctx); - return duk__obj_flag_any_default_false(ctx, - index, - DUK_HOBJECT_FLAG_BOUND); -} - -DUK_EXTERNAL duk_bool_t duk_is_thread(duk_context *ctx, duk_idx_t index) { - DUK_ASSERT_CTX_VALID(ctx); - return duk__obj_flag_any_default_false(ctx, - index, - DUK_HOBJECT_FLAG_THREAD); -} - -DUK_EXTERNAL duk_bool_t duk_is_fixed_buffer(duk_context *ctx, duk_idx_t index) { - duk_tval *tv; - - DUK_ASSERT_CTX_VALID(ctx); - - tv = duk_get_tval(ctx, index); - if (tv && DUK_TVAL_IS_BUFFER(tv)) { - duk_hbuffer *h = DUK_TVAL_GET_BUFFER(tv); - DUK_ASSERT(h != NULL); - return (DUK_HBUFFER_HAS_DYNAMIC(h) ? 0 : 1); - } - return 0; -} - -DUK_EXTERNAL duk_bool_t duk_is_dynamic_buffer(duk_context *ctx, duk_idx_t index) { - duk_tval *tv; - - DUK_ASSERT_CTX_VALID(ctx); - - tv = duk_get_tval(ctx, index); - if (tv && DUK_TVAL_IS_BUFFER(tv)) { - duk_hbuffer *h = DUK_TVAL_GET_BUFFER(tv); - DUK_ASSERT(h != NULL); - return (DUK_HBUFFER_HAS_DYNAMIC(h) && !DUK_HBUFFER_HAS_EXTERNAL(h) ? 1 : 0); - } - return 0; -} - -DUK_EXTERNAL duk_bool_t duk_is_external_buffer(duk_context *ctx, duk_idx_t index) { - duk_tval *tv; - - DUK_ASSERT_CTX_VALID(ctx); - - tv = duk_get_tval(ctx, index); - if (tv && DUK_TVAL_IS_BUFFER(tv)) { - duk_hbuffer *h = DUK_TVAL_GET_BUFFER(tv); - DUK_ASSERT(h != NULL); - return (DUK_HBUFFER_HAS_DYNAMIC(h) && DUK_HBUFFER_HAS_EXTERNAL(h) ? 1 : 0); - } - return 0; -} - -DUK_EXTERNAL duk_errcode_t duk_get_error_code(duk_context *ctx, duk_idx_t index) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_hobject *h; - duk_uint_t sanity; - - DUK_ASSERT_CTX_VALID(ctx); - - h = duk_get_hobject(ctx, index); - - sanity = DUK_HOBJECT_PROTOTYPE_CHAIN_SANITY; - do { - if (!h) { - return DUK_ERR_NONE; - } - if (h == thr->builtins[DUK_BIDX_EVAL_ERROR_PROTOTYPE]) { - return DUK_ERR_EVAL_ERROR; - } - if (h == thr->builtins[DUK_BIDX_RANGE_ERROR_PROTOTYPE]) { - return DUK_ERR_RANGE_ERROR; - } - if (h == thr->builtins[DUK_BIDX_REFERENCE_ERROR_PROTOTYPE]) { - return DUK_ERR_REFERENCE_ERROR; - } - if (h == thr->builtins[DUK_BIDX_SYNTAX_ERROR_PROTOTYPE]) { - return DUK_ERR_SYNTAX_ERROR; - } - if (h == thr->builtins[DUK_BIDX_TYPE_ERROR_PROTOTYPE]) { - return DUK_ERR_TYPE_ERROR; - } - if (h == thr->builtins[DUK_BIDX_URI_ERROR_PROTOTYPE]) { - return DUK_ERR_URI_ERROR; - } - if (h == thr->builtins[DUK_BIDX_ERROR_PROTOTYPE]) { - return DUK_ERR_ERROR; - } - - h = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, h); - } while (--sanity > 0); - - return DUK_ERR_NONE; -} - -/* - * Pushers - */ - -DUK_INTERNAL void duk_push_tval(duk_context *ctx, duk_tval *tv) { - duk_hthread *thr; - duk_tval *tv_slot; - - DUK_ASSERT_CTX_VALID(ctx); - DUK_ASSERT(tv != NULL); - thr = (duk_hthread *) ctx; - DUK__CHECK_SPACE(); - tv_slot = thr->valstack_top++; - DUK_TVAL_SET_TVAL(tv_slot, tv); - DUK_TVAL_INCREF(thr, tv); /* no side effects */ -} - -DUK_EXTERNAL void duk_push_undefined(duk_context *ctx) { - duk_hthread *thr; - - DUK_ASSERT_CTX_VALID(ctx); - thr = (duk_hthread *) ctx; - DUK__CHECK_SPACE(); - - /* Because value stack init policy is 'undefined above top', - * we don't need to write, just assert. - */ - thr->valstack_top++; - DUK_ASSERT(DUK_TVAL_IS_UNDEFINED(thr->valstack_top - 1)); -} - -DUK_EXTERNAL void duk_push_null(duk_context *ctx) { - duk_hthread *thr; - duk_tval *tv_slot; - - DUK_ASSERT_CTX_VALID(ctx); - thr = (duk_hthread *) ctx; - DUK__CHECK_SPACE(); - tv_slot = thr->valstack_top++; - DUK_TVAL_SET_NULL(tv_slot); -} - -DUK_EXTERNAL void duk_push_boolean(duk_context *ctx, duk_bool_t val) { - duk_hthread *thr; - duk_tval *tv_slot; - duk_small_int_t b; - - DUK_ASSERT_CTX_VALID(ctx); - thr = (duk_hthread *) ctx; - DUK__CHECK_SPACE(); - b = (val ? 1 : 0); /* ensure value is 1 or 0 (not other non-zero) */ - tv_slot = thr->valstack_top++; - DUK_TVAL_SET_BOOLEAN(tv_slot, b); -} - -DUK_EXTERNAL void duk_push_true(duk_context *ctx) { - duk_hthread *thr; - duk_tval *tv_slot; - - DUK_ASSERT_CTX_VALID(ctx); - thr = (duk_hthread *) ctx; - DUK__CHECK_SPACE(); - tv_slot = thr->valstack_top++; - DUK_TVAL_SET_BOOLEAN_TRUE(tv_slot); -} - -DUK_EXTERNAL void duk_push_false(duk_context *ctx) { - duk_hthread *thr; - duk_tval *tv_slot; - - DUK_ASSERT_CTX_VALID(ctx); - thr = (duk_hthread *) ctx; - DUK__CHECK_SPACE(); - tv_slot = thr->valstack_top++; - DUK_TVAL_SET_BOOLEAN_FALSE(tv_slot); -} - -/* normalize NaN which may not match our canonical internal NaN */ -DUK_EXTERNAL void duk_push_number(duk_context *ctx, duk_double_t val) { - duk_hthread *thr; - duk_tval *tv_slot; - duk_double_union du; - - DUK_ASSERT_CTX_VALID(ctx); - thr = (duk_hthread *) ctx; - DUK__CHECK_SPACE(); - du.d = val; - DUK_DBLUNION_NORMALIZE_NAN_CHECK(&du); - tv_slot = thr->valstack_top++; - DUK_TVAL_SET_NUMBER(tv_slot, du.d); -} - -DUK_EXTERNAL void duk_push_int(duk_context *ctx, duk_int_t val) { -#if defined(DUK_USE_FASTINT) - duk_hthread *thr; - duk_tval *tv_slot; - - DUK_ASSERT_CTX_VALID(ctx); - thr = (duk_hthread *) ctx; - DUK__CHECK_SPACE(); - tv_slot = thr->valstack_top++; -#if DUK_INT_MAX <= 0x7fffffffL - DUK_TVAL_SET_FASTINT_I32(tv_slot, (duk_int32_t) val); -#else - if (val >= DUK_FASTINT_MIN && val <= DUK_FASTINT_MAX) { - DUK_TVAL_SET_FASTINT(tv_slot, (duk_int64_t) val); - } else { - duk_double_t = (duk_double_t) val; - DUK_TVAL_SET_NUMBER(tv_slot, d); - } -#endif -#else /* DUK_USE_FASTINT */ - duk_hthread *thr; - duk_tval *tv_slot; - duk_double_t d; - - DUK_ASSERT_CTX_VALID(ctx); - thr = (duk_hthread *) ctx; - DUK__CHECK_SPACE(); - d = (duk_double_t) val; - tv_slot = thr->valstack_top++; - DUK_TVAL_SET_NUMBER(tv_slot, d); -#endif /* DUK_USE_FASTINT */ -} - -DUK_EXTERNAL void duk_push_uint(duk_context *ctx, duk_uint_t val) { -#if defined(DUK_USE_FASTINT) - duk_hthread *thr; - duk_tval *tv_slot; - - DUK_ASSERT_CTX_VALID(ctx); - thr = (duk_hthread *) ctx; - DUK__CHECK_SPACE(); - tv_slot = thr->valstack_top++; -#if DUK_UINT_MAX <= 0xffffffffUL - DUK_TVAL_SET_FASTINT_U32(tv_slot, (duk_uint32_t) val); -#else - if (val <= DUK_FASTINT_MAX) { /* val is unsigned so >= 0 */ - /* XXX: take advantage of val being unsigned, no need to mask */ - DUK_TVAL_SET_FASTINT(tv_slot, (duk_int64_t) val); - } else { - duk_double_t = (duk_double_t) val; - DUK_TVAL_SET_NUMBER(tv_slot, d); - } -#endif -#else /* DUK_USE_FASTINT */ - duk_hthread *thr; - duk_tval *tv_slot; - duk_double_t d; - - DUK_ASSERT_CTX_VALID(ctx); - thr = (duk_hthread *) ctx; - DUK__CHECK_SPACE(); - d = (duk_double_t) val; - tv_slot = thr->valstack_top++; - DUK_TVAL_SET_NUMBER(tv_slot, d); -#endif /* DUK_USE_FASTINT */ -} - -DUK_EXTERNAL void duk_push_nan(duk_context *ctx) { - duk_hthread *thr; - duk_tval *tv_slot; - duk_double_union du; - - DUK_ASSERT_CTX_VALID(ctx); - thr = (duk_hthread *) ctx; - DUK__CHECK_SPACE(); - DUK_DBLUNION_SET_NAN(&du); - DUK_ASSERT(DUK_DBLUNION_IS_NORMALIZED(&du)); - tv_slot = thr->valstack_top++; - DUK_TVAL_SET_NUMBER(tv_slot, du.d); -} - -DUK_EXTERNAL const char *duk_push_lstring(duk_context *ctx, const char *str, duk_size_t len) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_hstring *h; - duk_tval *tv_slot; - - DUK_ASSERT_CTX_VALID(ctx); - - /* check stack before interning (avoid hanging temp) */ - if (thr->valstack_top >= thr->valstack_end) { - DUK_ERROR_API(thr, DUK_STR_PUSH_BEYOND_ALLOC_STACK); - } - - /* NULL with zero length represents an empty string; NULL with higher - * length is also now trated like an empty string although it is - * a bit dubious. This is unlike duk_push_string() which pushes a - * 'null' if the input string is a NULL. - */ - if (!str) { - len = 0; - } - - /* Check for maximum string length */ - if (len > DUK_HSTRING_MAX_BYTELEN) { - DUK_ERROR_RANGE(thr, DUK_STR_STRING_TOO_LONG); - } - - h = duk_heap_string_intern_checked(thr, (const duk_uint8_t *) str, (duk_uint32_t) len); - DUK_ASSERT(h != NULL); - - tv_slot = thr->valstack_top++; - DUK_TVAL_SET_STRING(tv_slot, h); - DUK_HSTRING_INCREF(thr, h); /* no side effects */ - - return (const char *) DUK_HSTRING_GET_DATA(h); -} - -DUK_EXTERNAL const char *duk_push_string(duk_context *ctx, const char *str) { - DUK_ASSERT_CTX_VALID(ctx); - - if (str) { - return duk_push_lstring(ctx, str, DUK_STRLEN(str)); - } else { - duk_push_null(ctx); - return NULL; - } -} - -#ifdef DUK_USE_FILE_IO -/* This is a bit clunky because it is ANSI C portable. Should perhaps - * relocate to another file because this is potentially platform - * dependent. - */ -DUK_EXTERNAL const char *duk_push_string_file_raw(duk_context *ctx, const char *path, duk_uint_t flags) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_file *f = NULL; - char *buf; - long sz; /* ANSI C typing */ - - DUK_ASSERT_CTX_VALID(ctx); - - if (!path) { - goto fail; - } - f = DUK_FOPEN(path, "rb"); - if (!f) { - goto fail; - } - if (DUK_FSEEK(f, 0, SEEK_END) < 0) { - goto fail; - } - sz = DUK_FTELL(f); - if (sz < 0) { - goto fail; - } - if (DUK_FSEEK(f, 0, SEEK_SET) < 0) { - goto fail; - } - buf = (char *) duk_push_fixed_buffer(ctx, (duk_size_t) sz); - DUK_ASSERT(buf != NULL); - if ((duk_size_t) DUK_FREAD(buf, 1, (size_t) sz, f) != (duk_size_t) sz) { - goto fail; - } - (void) DUK_FCLOSE(f); /* ignore fclose() error */ - f = NULL; - return duk_to_string(ctx, -1); - - fail: - if (f) { - DUK_FCLOSE(f); - } - - if (flags != 0) { - DUK_ASSERT(flags == DUK_STRING_PUSH_SAFE); /* only flag now */ - duk_push_undefined(ctx); - } else { - /* XXX: string not shared because it is conditional */ - DUK_ERROR_TYPE(thr, "read file error"); - } - return NULL; -} -#else -DUK_EXTERNAL const char *duk_push_string_file_raw(duk_context *ctx, const char *path, duk_uint_t flags) { - duk_hthread *thr = (duk_hthread *) ctx; - DUK_ASSERT_CTX_VALID(ctx); - DUK_UNREF(path); - - if (flags != 0) { - DUK_ASSERT(flags == DUK_STRING_PUSH_SAFE); /* only flag now */ - duk_push_undefined(ctx); - } else { - /* XXX: string not shared because it is conditional */ - DUK_ERROR_UNSUPPORTED(thr, "file I/O disabled"); - } - return NULL; -} -#endif /* DUK_USE_FILE_IO */ - -DUK_EXTERNAL void duk_push_pointer(duk_context *ctx, void *val) { - duk_hthread *thr; - duk_tval *tv_slot; - - DUK_ASSERT_CTX_VALID(ctx); - thr = (duk_hthread *) ctx; - DUK__CHECK_SPACE(); - tv_slot = thr->valstack_top++; - DUK_TVAL_SET_POINTER(tv_slot, val); -} - -DUK_LOCAL void duk__push_this_helper(duk_context *ctx, duk_small_uint_t check_object_coercible) { - duk_hthread *thr; - duk_tval *tv_slot; - - DUK_ASSERT_CTX_VALID(ctx); - DUK_ASSERT_DISABLE(thr->callstack_top >= 0); /* avoid warning (unsigned) */ - thr = (duk_hthread *) ctx; - DUK_ASSERT(thr->callstack_top <= thr->callstack_size); - DUK__CHECK_SPACE(); - - DUK_ASSERT(DUK_TVAL_IS_UNDEFINED(thr->valstack_top)); /* because of valstack init policy */ - tv_slot = thr->valstack_top++; - - if (DUK_UNLIKELY(thr->callstack_top == 0)) { - if (check_object_coercible) { - goto type_error; - } - /* 'undefined' already on stack top */ - } else { - duk_tval *tv; - - /* 'this' binding is just before current activation's bottom */ - DUK_ASSERT(thr->valstack_bottom > thr->valstack); - tv = thr->valstack_bottom - 1; - if (check_object_coercible && - (DUK_TVAL_IS_UNDEFINED(tv) || DUK_TVAL_IS_NULL(tv))) { - /* XXX: better macro for DUK_TVAL_IS_UNDEFINED_OR_NULL(tv) */ - goto type_error; - } - - DUK_TVAL_SET_TVAL(tv_slot, tv); - DUK_TVAL_INCREF(thr, tv); - } - return; - - type_error: - DUK_ERROR_TYPE(thr, DUK_STR_NOT_OBJECT_COERCIBLE); -} - -DUK_EXTERNAL void duk_push_this(duk_context *ctx) { - DUK_ASSERT_CTX_VALID(ctx); - - duk__push_this_helper(ctx, 0 /*check_object_coercible*/); -} - -DUK_INTERNAL void duk_push_this_check_object_coercible(duk_context *ctx) { - DUK_ASSERT_CTX_VALID(ctx); - - duk__push_this_helper(ctx, 1 /*check_object_coercible*/); -} - -DUK_INTERNAL duk_hobject *duk_push_this_coercible_to_object(duk_context *ctx) { - duk_hobject *h; - - DUK_ASSERT_CTX_VALID(ctx); - - duk__push_this_helper(ctx, 1 /*check_object_coercible*/); - duk_to_object(ctx, -1); - h = duk_get_hobject(ctx, -1); - DUK_ASSERT(h != NULL); - return h; -} - -DUK_INTERNAL duk_hstring *duk_push_this_coercible_to_string(duk_context *ctx) { - duk_hstring *h; - - DUK_ASSERT_CTX_VALID(ctx); - - duk__push_this_helper(ctx, 1 /*check_object_coercible*/); - duk_to_string(ctx, -1); - h = duk_get_hstring(ctx, -1); - DUK_ASSERT(h != NULL); - return h; -} - -DUK_INTERNAL duk_tval *duk_get_borrowed_this_tval(duk_context *ctx) { - duk_hthread *thr; - - DUK_ASSERT(ctx != NULL); - thr = (duk_hthread *) ctx; - - DUK_ASSERT(thr->callstack_top > 0); /* caller required to know */ - DUK_ASSERT(thr->valstack_bottom > thr->valstack); /* consequence of above */ - DUK_ASSERT(thr->valstack_bottom - 1 >= thr->valstack); /* 'this' binding exists */ - - return thr->valstack_bottom - 1; -} - -DUK_EXTERNAL void duk_push_current_function(duk_context *ctx) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_activation *act; - - DUK_ASSERT_CTX_VALID(ctx); - DUK_ASSERT(thr != NULL); - DUK_ASSERT_DISABLE(thr->callstack_top >= 0); - DUK_ASSERT(thr->callstack_top <= thr->callstack_size); - - act = duk_hthread_get_current_activation(thr); - if (act) { - duk_push_tval(ctx, &act->tv_func); - } else { - duk_push_undefined(ctx); - } -} - -DUK_EXTERNAL void duk_push_current_thread(duk_context *ctx) { - duk_hthread *thr = (duk_hthread *) ctx; - - DUK_ASSERT_CTX_VALID(ctx); - DUK_ASSERT(thr != NULL); - - if (thr->heap->curr_thread) { - duk_push_hobject(ctx, (duk_hobject *) thr->heap->curr_thread); - } else { - duk_push_undefined(ctx); - } -} - -DUK_EXTERNAL void duk_push_global_object(duk_context *ctx) { - DUK_ASSERT_CTX_VALID(ctx); - - duk_push_hobject_bidx(ctx, DUK_BIDX_GLOBAL); -} - -/* XXX: size optimize */ -DUK_LOCAL void duk__push_stash(duk_context *ctx) { - DUK_ASSERT_CTX_VALID(ctx); - if (!duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_VALUE)) { - DUK_DDD(DUK_DDDPRINT("creating heap/global/thread stash on first use")); - duk_pop(ctx); - duk_push_object_internal(ctx); - duk_dup_top(ctx); - duk_xdef_prop_stridx(ctx, -3, DUK_STRIDX_INT_VALUE, DUK_PROPDESC_FLAGS_C); /* [ ... parent stash stash ] -> [ ... parent stash ] */ - } - duk_remove(ctx, -2); -} - -DUK_EXTERNAL void duk_push_heap_stash(duk_context *ctx) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_heap *heap; - DUK_ASSERT_CTX_VALID(ctx); - heap = thr->heap; - DUK_ASSERT(heap->heap_object != NULL); - duk_push_hobject(ctx, heap->heap_object); - duk__push_stash(ctx); -} - -DUK_EXTERNAL void duk_push_global_stash(duk_context *ctx) { - DUK_ASSERT_CTX_VALID(ctx); - duk_push_global_object(ctx); - duk__push_stash(ctx); -} - -DUK_EXTERNAL void duk_push_thread_stash(duk_context *ctx, duk_context *target_ctx) { - duk_hthread *thr = (duk_hthread *) ctx; - DUK_ASSERT_CTX_VALID(ctx); - if (!target_ctx) { - DUK_ERROR_API(thr, DUK_STR_INVALID_CALL_ARGS); - return; /* not reached */ - } - duk_push_hobject(ctx, (duk_hobject *) target_ctx); - duk__push_stash(ctx); -} - -/* XXX: duk_ssize_t would be useful here */ -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_int_t len; - - DUK_ASSERT_CTX_VALID(ctx); - DUK_UNREF(ctx); - - /* NUL terminator handling doesn't matter here */ - len = DUK_VSNPRINTF((char *) buf, sz, fmt, ap); - if (len < (duk_int_t) sz) { - /* Return value of 'sz' or more indicates output was (potentially) - * truncated. - */ - return (duk_int_t) len; - } - return -1; -} - -DUK_EXTERNAL const char *duk_push_vsprintf(duk_context *ctx, const char *fmt, va_list ap) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_uint8_t stack_buf[DUK_PUSH_SPRINTF_INITIAL_SIZE]; - duk_size_t sz = DUK_PUSH_SPRINTF_INITIAL_SIZE; - duk_bool_t pushed_buf = 0; - void *buf; - duk_int_t len; /* XXX: duk_ssize_t */ - const char *res; - - DUK_ASSERT_CTX_VALID(ctx); - - /* special handling of fmt==NULL */ - if (!fmt) { - duk_hstring *h_str; - duk_push_hstring_stridx(ctx, DUK_STRIDX_EMPTY_STRING); - h_str = DUK_HTHREAD_STRING_EMPTY_STRING(thr); /* rely on interning, must be this string */ - return (const char *) DUK_HSTRING_GET_DATA(h_str); - } - - /* initial estimate based on format string */ - sz = DUK_STRLEN(fmt) + 16; /* format plus something to avoid just missing */ - if (sz < DUK_PUSH_SPRINTF_INITIAL_SIZE) { - sz = DUK_PUSH_SPRINTF_INITIAL_SIZE; - } - DUK_ASSERT(sz > 0); - - /* Try to make do with a stack buffer to avoid allocating a temporary buffer. - * This works 99% of the time which is quite nice. - */ - for (;;) { - va_list ap_copy; /* copied so that 'ap' can be reused */ - - if (sz <= sizeof(stack_buf)) { - buf = stack_buf; - } else if (!pushed_buf) { - pushed_buf = 1; - buf = duk_push_dynamic_buffer(ctx, sz); - } else { - buf = duk_resize_buffer(ctx, -1, sz); - } - DUK_ASSERT(buf != NULL); - - DUK_VA_COPY(ap_copy, ap); - len = duk__try_push_vsprintf(ctx, buf, sz, fmt, ap_copy); - va_end(ap_copy); - if (len >= 0) { - break; - } - - /* failed, resize and try again */ - sz = sz * 2; - if (sz >= DUK_PUSH_SPRINTF_SANITY_LIMIT) { - DUK_ERROR_API(thr, DUK_STR_SPRINTF_TOO_LONG); - } - } - - /* Cannot use duk_to_string() on the buffer because it is usually - * larger than 'len'. Also, 'buf' is usually a stack buffer. - */ - res = duk_push_lstring(ctx, (const char *) buf, (duk_size_t) len); /* [ buf? res ] */ - if (pushed_buf) { - duk_remove(ctx, -2); - } - return res; -} - -DUK_EXTERNAL const char *duk_push_sprintf(duk_context *ctx, const char *fmt, ...) { - va_list ap; - const char *ret; - - DUK_ASSERT_CTX_VALID(ctx); - - /* allow fmt==NULL */ - va_start(ap, fmt); - ret = duk_push_vsprintf(ctx, fmt, ap); - va_end(ap); - - return ret; -} - -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_hthread *thr = (duk_hthread *) ctx; - duk_tval *tv_slot; - duk_hobject *h; - duk_idx_t ret; - - DUK_ASSERT_CTX_VALID(ctx); - DUK_ASSERT(prototype_bidx == -1 || - (prototype_bidx >= 0 && prototype_bidx < DUK_NUM_BUILTINS)); - - /* check stack first */ - if (thr->valstack_top >= thr->valstack_end) { - DUK_ERROR_API(thr, DUK_STR_PUSH_BEYOND_ALLOC_STACK); - } - - h = duk_hobject_alloc(thr->heap, hobject_flags_and_class); - if (!h) { - DUK_ERROR_ALLOC_DEFMSG(thr); - } - - DUK_DDD(DUK_DDDPRINT("created object with flags: 0x%08lx", (unsigned long) h->hdr.h_flags)); - - tv_slot = thr->valstack_top; - DUK_TVAL_SET_OBJECT(tv_slot, h); - DUK_HOBJECT_INCREF(thr, h); /* no side effects */ - ret = (duk_idx_t) (thr->valstack_top - thr->valstack_bottom); - thr->valstack_top++; - - /* object is now reachable */ - - if (prototype_bidx >= 0) { - DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, h, thr->builtins[prototype_bidx]); - } else { - DUK_ASSERT(prototype_bidx == -1); - DUK_ASSERT(DUK_HOBJECT_GET_PROTOTYPE(thr->heap, h) == NULL); - } - - return ret; -} - -DUK_INTERNAL duk_idx_t duk_push_object_helper_proto(duk_context *ctx, duk_uint_t hobject_flags_and_class, duk_hobject *proto) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_idx_t ret; - duk_hobject *h; - - DUK_ASSERT_CTX_VALID(ctx); - - ret = duk_push_object_helper(ctx, hobject_flags_and_class, -1); - h = duk_get_hobject(ctx, -1); - DUK_ASSERT(h != NULL); - DUK_ASSERT(DUK_HOBJECT_GET_PROTOTYPE(thr->heap, h) == NULL); - DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, h, proto); - return ret; -} - -DUK_EXTERNAL duk_idx_t duk_push_object(duk_context *ctx) { - DUK_ASSERT_CTX_VALID(ctx); - - return duk_push_object_helper(ctx, - DUK_HOBJECT_FLAG_EXTENSIBLE | - DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_OBJECT), - DUK_BIDX_OBJECT_PROTOTYPE); -} - -DUK_EXTERNAL duk_idx_t duk_push_array(duk_context *ctx) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_hobject *obj; - duk_idx_t ret; - - DUK_ASSERT_CTX_VALID(ctx); - - ret = duk_push_object_helper(ctx, - DUK_HOBJECT_FLAG_EXTENSIBLE | - DUK_HOBJECT_FLAG_ARRAY_PART | - DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_ARRAY), - DUK_BIDX_ARRAY_PROTOTYPE); - - obj = duk_require_hobject(ctx, ret); - - /* - * An array must have a 'length' property (E5 Section 15.4.5.2). - * The special array behavior flag must only be enabled once the - * length property has been added. - * - * The internal property must be a number (and preferably a - * fastint if fastint support is enabled). - */ - - duk_push_int(ctx, 0); -#if defined(DUK_USE_FASTINT) - DUK_ASSERT(DUK_TVAL_IS_FASTINT(duk_require_tval(ctx, -1))); -#endif - - duk_hobject_define_property_internal(thr, - obj, - DUK_HTHREAD_STRING_LENGTH(thr), - DUK_PROPDESC_FLAGS_W); - DUK_HOBJECT_SET_EXOTIC_ARRAY(obj); - - return ret; -} - -DUK_EXTERNAL duk_idx_t duk_push_thread_raw(duk_context *ctx, duk_uint_t flags) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_hthread *obj; - duk_idx_t ret; - duk_tval *tv_slot; - - DUK_ASSERT_CTX_VALID(ctx); - - /* check stack first */ - if (thr->valstack_top >= thr->valstack_end) { - DUK_ERROR_API(thr, DUK_STR_PUSH_BEYOND_ALLOC_STACK); - } - - obj = duk_hthread_alloc(thr->heap, - DUK_HOBJECT_FLAG_EXTENSIBLE | - DUK_HOBJECT_FLAG_THREAD | - DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_THREAD)); - if (!obj) { - DUK_ERROR_ALLOC_DEFMSG(thr); - } - obj->state = DUK_HTHREAD_STATE_INACTIVE; -#if defined(DUK_USE_ROM_STRINGS) - /* Nothing to initialize, strs[] is in ROM. */ -#else -#if defined(DUK_USE_HEAPPTR16) - obj->strs16 = thr->strs16; -#else - obj->strs = thr->strs; -#endif -#endif - DUK_DDD(DUK_DDDPRINT("created thread object with flags: 0x%08lx", (unsigned long) obj->obj.hdr.h_flags)); - - /* make the new thread reachable */ - tv_slot = thr->valstack_top; - DUK_TVAL_SET_OBJECT(tv_slot, (duk_hobject *) obj); - DUK_HTHREAD_INCREF(thr, obj); - ret = (duk_idx_t) (thr->valstack_top - thr->valstack_bottom); - thr->valstack_top++; - - /* important to do this *after* pushing, to make the thread reachable for gc */ - if (!duk_hthread_init_stacks(thr->heap, obj)) { - DUK_ERROR_ALLOC_DEFMSG(thr); - } - - /* initialize built-ins - either by copying or creating new ones */ - if (flags & DUK_THREAD_NEW_GLOBAL_ENV) { - duk_hthread_create_builtin_objects(obj); - } else { - duk_hthread_copy_builtin_objects(thr, obj); - } - - /* default prototype (Note: 'obj' must be reachable) */ - DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, (duk_hobject *) obj, obj->builtins[DUK_BIDX_THREAD_PROTOTYPE]); - - /* Initial stack size satisfies the stack spare constraints so there - * is no need to require stack here. - */ - DUK_ASSERT(DUK_VALSTACK_INITIAL_SIZE >= - DUK_VALSTACK_API_ENTRY_MINIMUM + DUK_VALSTACK_INTERNAL_EXTRA); - - return ret; -} - -DUK_INTERNAL duk_idx_t duk_push_compiledfunction(duk_context *ctx) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_hcompiledfunction *obj; - duk_idx_t ret; - duk_tval *tv_slot; - - DUK_ASSERT_CTX_VALID(ctx); - - /* check stack first */ - if (thr->valstack_top >= thr->valstack_end) { - DUK_ERROR_API(thr, DUK_STR_PUSH_BEYOND_ALLOC_STACK); - } - - /* Template functions are not strictly constructable (they don't - * have a "prototype" property for instance), so leave the - * DUK_HOBJECT_FLAG_CONSRUCTABLE flag cleared here. - */ - - obj = duk_hcompiledfunction_alloc(thr->heap, - DUK_HOBJECT_FLAG_EXTENSIBLE | - DUK_HOBJECT_FLAG_COMPILEDFUNCTION | - DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_FUNCTION)); - if (!obj) { - DUK_ERROR_ALLOC_DEFMSG(thr); - } - - DUK_DDD(DUK_DDDPRINT("created compiled function object with flags: 0x%08lx", (unsigned long) obj->obj.hdr.h_flags)); - - tv_slot = thr->valstack_top; - DUK_TVAL_SET_OBJECT(tv_slot, (duk_hobject *) obj); - DUK_HOBJECT_INCREF(thr, obj); - ret = (duk_idx_t) (thr->valstack_top - thr->valstack_bottom); - thr->valstack_top++; - - /* default prototype (Note: 'obj' must be reachable) */ - DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, (duk_hobject *) obj, thr->builtins[DUK_BIDX_FUNCTION_PROTOTYPE]); - - return ret; -} - -DUK_LOCAL duk_idx_t duk__push_c_function_raw(duk_context *ctx, duk_c_function func, duk_idx_t nargs, duk_uint_t flags) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_hnativefunction *obj; - duk_idx_t ret; - duk_tval *tv_slot; - duk_int16_t func_nargs; - - DUK_ASSERT_CTX_VALID(ctx); - - /* check stack first */ - if (thr->valstack_top >= thr->valstack_end) { - DUK_ERROR_API(thr, DUK_STR_PUSH_BEYOND_ALLOC_STACK); - } - if (func == NULL) { - goto api_error; - } - if (nargs >= 0 && nargs < DUK_HNATIVEFUNCTION_NARGS_MAX) { - func_nargs = (duk_int16_t) nargs; - } else if (nargs == DUK_VARARGS) { - func_nargs = DUK_HNATIVEFUNCTION_NARGS_VARARGS; - } else { - goto api_error; - } - - obj = duk_hnativefunction_alloc(thr->heap, flags); - if (!obj) { - DUK_ERROR_ALLOC_DEFMSG(thr); - } - - obj->func = func; - obj->nargs = func_nargs; - - DUK_DDD(DUK_DDDPRINT("created native function object with flags: 0x%08lx, nargs=%ld", - (unsigned long) obj->obj.hdr.h_flags, (long) obj->nargs)); - - tv_slot = thr->valstack_top; - DUK_TVAL_SET_OBJECT(tv_slot, (duk_hobject *) obj); - DUK_HOBJECT_INCREF(thr, obj); - ret = (duk_idx_t) (thr->valstack_top - thr->valstack_bottom); - thr->valstack_top++; - - /* default prototype (Note: 'obj' must be reachable) */ - DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, (duk_hobject *) obj, thr->builtins[DUK_BIDX_FUNCTION_PROTOTYPE]); - - return ret; - - api_error: - DUK_ERROR_API(thr, DUK_STR_INVALID_CALL_ARGS); - return 0; /* not reached */ -} - -DUK_EXTERNAL duk_idx_t duk_push_c_function(duk_context *ctx, duk_c_function func, duk_int_t nargs) { - duk_uint_t flags; - - DUK_ASSERT_CTX_VALID(ctx); - - flags = DUK_HOBJECT_FLAG_EXTENSIBLE | - DUK_HOBJECT_FLAG_CONSTRUCTABLE | - DUK_HOBJECT_FLAG_NATIVEFUNCTION | - DUK_HOBJECT_FLAG_NEWENV | - DUK_HOBJECT_FLAG_STRICT | - DUK_HOBJECT_FLAG_NOTAIL | - DUK_HOBJECT_FLAG_EXOTIC_DUKFUNC | - DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_FUNCTION); - - return duk__push_c_function_raw(ctx, func, nargs, flags); -} - -DUK_INTERNAL void duk_push_c_function_noexotic(duk_context *ctx, duk_c_function func, duk_int_t nargs) { - duk_uint_t flags; - - DUK_ASSERT_CTX_VALID(ctx); - - flags = DUK_HOBJECT_FLAG_EXTENSIBLE | - DUK_HOBJECT_FLAG_CONSTRUCTABLE | - DUK_HOBJECT_FLAG_NATIVEFUNCTION | - DUK_HOBJECT_FLAG_NEWENV | - DUK_HOBJECT_FLAG_STRICT | - DUK_HOBJECT_FLAG_NOTAIL | - DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_FUNCTION); - - (void) duk__push_c_function_raw(ctx, func, nargs, flags); -} - -DUK_INTERNAL void duk_push_c_function_noconstruct_noexotic(duk_context *ctx, duk_c_function func, duk_int_t nargs) { - duk_uint_t flags; - - DUK_ASSERT_CTX_VALID(ctx); - - flags = DUK_HOBJECT_FLAG_EXTENSIBLE | - DUK_HOBJECT_FLAG_NATIVEFUNCTION | - DUK_HOBJECT_FLAG_NEWENV | - DUK_HOBJECT_FLAG_STRICT | - DUK_HOBJECT_FLAG_NOTAIL | - DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_FUNCTION); - - (void) duk__push_c_function_raw(ctx, func, nargs, flags); -} - -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_hthread *thr = (duk_hthread *) ctx; - duk_tval tv_tmp; - duk_small_uint_t lf_flags; - - DUK_ASSERT_CTX_VALID(ctx); - - /* check stack first */ - if (thr->valstack_top >= thr->valstack_end) { - DUK_ERROR_API(thr, DUK_STR_PUSH_BEYOND_ALLOC_STACK); - } - - if (nargs >= DUK_LFUNC_NARGS_MIN && nargs <= DUK_LFUNC_NARGS_MAX) { - /* as is */ - } else if (nargs == DUK_VARARGS) { - nargs = DUK_LFUNC_NARGS_VARARGS; - } else { - goto api_error; - } - if (!(length >= DUK_LFUNC_LENGTH_MIN && length <= DUK_LFUNC_LENGTH_MAX)) { - goto api_error; - } - if (!(magic >= DUK_LFUNC_MAGIC_MIN && magic <= DUK_LFUNC_MAGIC_MAX)) { - goto api_error; - } - - lf_flags = DUK_LFUNC_FLAGS_PACK(magic, length, nargs); - DUK_TVAL_SET_LIGHTFUNC(&tv_tmp, func, lf_flags); - duk_push_tval(ctx, &tv_tmp); /* XXX: direct valstack write */ - DUK_ASSERT(thr->valstack_top != thr->valstack_bottom); - return ((duk_idx_t) (thr->valstack_top - thr->valstack_bottom)) - 1; - - api_error: - DUK_ERROR_API(thr, DUK_STR_INVALID_CALL_ARGS); - return 0; /* not reached */ -} - -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_hthread *thr = (duk_hthread *) ctx; - duk_hbufferobject *obj; - duk_tval *tv_slot; - - DUK_ASSERT(ctx != NULL); - DUK_ASSERT(prototype_bidx >= 0); - - /* check stack first */ - if (thr->valstack_top >= thr->valstack_end) { - DUK_ERROR_API(thr, DUK_STR_PUSH_BEYOND_ALLOC_STACK); - } - - obj = duk_hbufferobject_alloc(thr->heap, hobject_flags_and_class); - if (!obj) { - DUK_ERROR_ALLOC_DEFMSG(thr); - } - - DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, (duk_hobject *) obj, thr->builtins[prototype_bidx]); - DUK_ASSERT_HBUFFEROBJECT_VALID(obj); - - tv_slot = thr->valstack_top; - DUK_TVAL_SET_OBJECT(tv_slot, (duk_hobject *) obj); - DUK_HOBJECT_INCREF(thr, obj); - thr->valstack_top++; - - return obj; -} - -/* XXX: There's quite a bit of overlap with buffer creation handling in - * duk_bi_buffer.c. Look for overlap and refactor. - */ -#define DUK__PACK_ARGS(classnum,protobidx,elemtype,elemshift,isview) \ - (((classnum) << 24) | ((protobidx) << 16) | ((elemtype) << 8) | ((elemshift) << 4) | (isview)) - -#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) -static const duk_uint32_t duk__bufobj_flags_lookup[] = { - DUK__PACK_ARGS(DUK_HOBJECT_CLASS_BUFFER, DUK_BIDX_BUFFER_PROTOTYPE, DUK_HBUFFEROBJECT_ELEM_UINT8, 0, 0), /* DUK_BUFOBJ_DUKTAPE_BUFFER */ - DUK__PACK_ARGS(DUK_HOBJECT_CLASS_BUFFER, DUK_BIDX_NODEJS_BUFFER_PROTOTYPE, DUK_HBUFFEROBJECT_ELEM_UINT8, 0, 0), /* DUK_BUFOBJ_NODEJS_BUFFER */ - DUK__PACK_ARGS(DUK_HOBJECT_CLASS_ARRAYBUFFER, DUK_BIDX_ARRAYBUFFER_PROTOTYPE, DUK_HBUFFEROBJECT_ELEM_UINT8, 0, 0), /* DUK_BUFOBJ_ARRAYBUFFER */ - DUK__PACK_ARGS(DUK_HOBJECT_CLASS_DATAVIEW, DUK_BIDX_DATAVIEW_PROTOTYPE, DUK_HBUFFEROBJECT_ELEM_UINT8, 0, 1), /* DUK_BUFOBJ_DATAVIEW */ - DUK__PACK_ARGS(DUK_HOBJECT_CLASS_INT8ARRAY, DUK_BIDX_INT8ARRAY_PROTOTYPE, DUK_HBUFFEROBJECT_ELEM_INT8, 0, 1), /* DUK_BUFOBJ_INT8ARRAY */ - DUK__PACK_ARGS(DUK_HOBJECT_CLASS_UINT8ARRAY, DUK_BIDX_UINT8ARRAY_PROTOTYPE, DUK_HBUFFEROBJECT_ELEM_UINT8, 0, 1), /* DUK_BUFOBJ_UINT8ARRAY */ - DUK__PACK_ARGS(DUK_HOBJECT_CLASS_UINT8CLAMPEDARRAY, DUK_BIDX_UINT8CLAMPEDARRAY_PROTOTYPE, DUK_HBUFFEROBJECT_ELEM_UINT8CLAMPED, 0, 1), /* DUK_BUFOBJ_UINT8CLAMPEDARRAY */ - DUK__PACK_ARGS(DUK_HOBJECT_CLASS_INT16ARRAY, DUK_BIDX_INT16ARRAY_PROTOTYPE, DUK_HBUFFEROBJECT_ELEM_INT16, 1, 1), /* DUK_BUFOBJ_INT16ARRAY */ - DUK__PACK_ARGS(DUK_HOBJECT_CLASS_UINT16ARRAY, DUK_BIDX_UINT16ARRAY_PROTOTYPE, DUK_HBUFFEROBJECT_ELEM_UINT16, 1, 1), /* DUK_BUFOBJ_UINT16ARRAY */ - DUK__PACK_ARGS(DUK_HOBJECT_CLASS_INT32ARRAY, DUK_BIDX_INT32ARRAY_PROTOTYPE, DUK_HBUFFEROBJECT_ELEM_INT32, 2, 1), /* DUK_BUFOBJ_INT32ARRAY */ - DUK__PACK_ARGS(DUK_HOBJECT_CLASS_UINT32ARRAY, DUK_BIDX_UINT32ARRAY_PROTOTYPE, DUK_HBUFFEROBJECT_ELEM_UINT32, 2, 1), /* DUK_BUFOBJ_UINT32ARRAY */ - DUK__PACK_ARGS(DUK_HOBJECT_CLASS_FLOAT32ARRAY, DUK_BIDX_FLOAT32ARRAY_PROTOTYPE, DUK_HBUFFEROBJECT_ELEM_FLOAT32, 2, 1), /* DUK_BUFOBJ_FLOAT32ARRAY */ - DUK__PACK_ARGS(DUK_HOBJECT_CLASS_FLOAT64ARRAY, DUK_BIDX_FLOAT64ARRAY_PROTOTYPE, DUK_HBUFFEROBJECT_ELEM_FLOAT64, 3, 1) /* DUK_BUFOBJ_FLOAT64ARRAY */ -}; -#else /* DUK_USE_BUFFEROBJECT_SUPPORT */ -/* Only allow Duktape.Buffer when support disabled. */ -static const duk_uint32_t duk__bufobj_flags_lookup[] = { - DUK__PACK_ARGS(DUK_HOBJECT_CLASS_BUFFER, DUK_BIDX_BUFFER_PROTOTYPE, DUK_HBUFFEROBJECT_ELEM_UINT8, 0, 0) /* DUK_BUFOBJ_DUKTAPE_BUFFER */ -}; -#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ -#undef DUK__PACK_ARGS - -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_hthread *thr; - duk_hbufferobject *h_bufobj; - duk_hbuffer *h_val; - duk_uint32_t tmp; - duk_uint_t classnum; - duk_uint_t protobidx; - duk_uint_t lookupidx; - duk_uint_t uint_offset, uint_length, uint_added; - - DUK_ASSERT_CTX_VALID(ctx); - thr = (duk_hthread *) ctx; - DUK_UNREF(thr); - - /* The underlying types for offset/length in duk_hbufferobject is - * duk_uint_t; make sure argument values fit and that offset + length - * does not wrap. - */ - uint_offset = (duk_uint_t) byte_offset; - uint_length = (duk_uint_t) byte_length; - if (sizeof(duk_size_t) != sizeof(duk_uint_t)) { - if ((duk_size_t) uint_offset != byte_offset || (duk_size_t) uint_length != byte_length) { - goto range_error; - } - } - uint_added = uint_offset + uint_length; - if (uint_added < uint_offset) { - goto range_error; - } - DUK_ASSERT(uint_added >= uint_offset && uint_added >= uint_length); - - DUK_ASSERT_DISABLE(flags >= 0); /* flags is unsigned */ - lookupidx = flags & 0x0f; /* 4 low bits */ - if (lookupidx >= sizeof(duk__bufobj_flags_lookup) / sizeof(duk_uint32_t)) { - goto arg_error; - } - tmp = duk__bufobj_flags_lookup[lookupidx]; - classnum = tmp >> 24; - protobidx = (tmp >> 16) & 0xff; - - h_val = duk_require_hbuffer(ctx, idx_buffer); - DUK_ASSERT(h_val != NULL); - - h_bufobj = duk_push_bufferobject_raw(ctx, - DUK_HOBJECT_FLAG_EXTENSIBLE | - DUK_HOBJECT_FLAG_BUFFEROBJECT | - DUK_HOBJECT_CLASS_AS_FLAGS(classnum), - protobidx); - DUK_ASSERT(h_bufobj != NULL); - - h_bufobj->buf = h_val; - DUK_HBUFFER_INCREF(thr, h_val); - h_bufobj->offset = uint_offset; - h_bufobj->length = uint_length; - h_bufobj->shift = (tmp >> 4) & 0x0f; - h_bufobj->elem_type = (tmp >> 8) & 0xff; - h_bufobj->is_view = tmp & 0x0f; - DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj); - -#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) - /* TypedArray views need an automatic ArrayBuffer which must be - * provided as .buffer property of the view. Just create a new - * ArrayBuffer sharing the same underlying buffer. - */ - if (flags & DUK_BUFOBJ_CREATE_ARRBUF) { - h_bufobj = duk_push_bufferobject_raw(ctx, - DUK_HOBJECT_FLAG_EXTENSIBLE | - DUK_HOBJECT_FLAG_BUFFEROBJECT | - DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_ARRAYBUFFER), - DUK_BIDX_ARRAYBUFFER_PROTOTYPE); - - DUK_ASSERT(h_bufobj != NULL); - - h_bufobj->buf = h_val; - DUK_HBUFFER_INCREF(thr, h_val); - h_bufobj->offset = uint_offset; - h_bufobj->length = uint_length; - DUK_ASSERT(h_bufobj->shift == 0); - h_bufobj->elem_type = DUK_HBUFFEROBJECT_ELEM_UINT8; - DUK_ASSERT(h_bufobj->is_view == 0); - DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj); - - duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_LC_BUFFER, DUK_PROPDESC_FLAGS_NONE); - duk_compact(ctx, -1); - } -#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ - - return; - - range_error: - DUK_ERROR_RANGE(thr, DUK_STR_INVALID_CALL_ARGS); - return; /* not reached */ - - arg_error: - DUK_ERROR_TYPE(thr, DUK_STR_INVALID_CALL_ARGS); - return; /* not reached */ -} - -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_hthread *thr = (duk_hthread *) ctx; - duk_idx_t ret; - duk_hobject *proto; -#ifdef DUK_USE_AUGMENT_ERROR_CREATE - duk_bool_t noblame_fileline; -#endif - - DUK_ASSERT_CTX_VALID(ctx); - DUK_ASSERT(thr != NULL); - DUK_UNREF(filename); - DUK_UNREF(line); - - /* Error code also packs a tracedata related flag. */ -#ifdef DUK_USE_AUGMENT_ERROR_CREATE - noblame_fileline = err_code & DUK_ERRCODE_FLAG_NOBLAME_FILELINE; -#endif - err_code = err_code & (~DUK_ERRCODE_FLAG_NOBLAME_FILELINE); - - /* error gets its 'name' from the prototype */ - proto = duk_error_prototype_from_code(thr, err_code); - ret = duk_push_object_helper_proto(ctx, - DUK_HOBJECT_FLAG_EXTENSIBLE | - DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_ERROR), - proto); - - /* ... and its 'message' from an instance property */ - if (fmt) { - duk_push_vsprintf(ctx, fmt, ap); - duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_MESSAGE, DUK_PROPDESC_FLAGS_WC); - } else { - /* If no explicit message given, put error code into message field - * (as a number). This is not fully in keeping with the Ecmascript - * error model because messages are supposed to be strings (Error - * constructors use ToString() on their argument). However, it's - * probably more useful than having a separate 'code' property. - */ - duk_push_int(ctx, err_code); - duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_MESSAGE, DUK_PROPDESC_FLAGS_WC); - } - - /* XXX: .code = err_code disabled, not sure if useful */ - - /* Creation time error augmentation */ -#ifdef DUK_USE_AUGMENT_ERROR_CREATE - /* filename may be NULL in which case file/line is not recorded */ - duk_err_augment_error_create(thr, thr, filename, line, noblame_fileline); /* may throw an error */ -#endif - - return ret; -} - -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, ...) { - va_list ap; - duk_idx_t ret; - - DUK_ASSERT_CTX_VALID(ctx); - - va_start(ap, fmt); - ret = duk_push_error_object_va_raw(ctx, err_code, filename, line, fmt, ap); - va_end(ap); - return ret; -} - -#if !defined(DUK_USE_VARIADIC_MACROS) -DUK_EXTERNAL duk_idx_t duk_push_error_object_stash(duk_context *ctx, duk_errcode_t err_code, const char *fmt, ...) { - const char *filename = duk_api_global_filename; - duk_int_t line = duk_api_global_line; - va_list ap; - duk_idx_t ret; - - DUK_ASSERT_CTX_VALID(ctx); - - duk_api_global_filename = NULL; - duk_api_global_line = 0; - va_start(ap, fmt); - ret = duk_push_error_object_va_raw(ctx, err_code, filename, line, fmt, ap); - va_end(ap); - return ret; -} -#endif /* DUK_USE_VARIADIC_MACROS */ - -DUK_EXTERNAL void *duk_push_buffer_raw(duk_context *ctx, duk_size_t size, duk_small_uint_t flags) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_tval *tv_slot; - duk_hbuffer *h; - void *buf_data; - - DUK_ASSERT_CTX_VALID(ctx); - - /* check stack first */ - if (thr->valstack_top >= thr->valstack_end) { - DUK_ERROR_API(thr, DUK_STR_PUSH_BEYOND_ALLOC_STACK); - } - - /* Check for maximum buffer length. */ - if (size > DUK_HBUFFER_MAX_BYTELEN) { - DUK_ERROR_RANGE(thr, DUK_STR_BUFFER_TOO_LONG); - } - - h = duk_hbuffer_alloc(thr->heap, size, flags, &buf_data); - if (!h) { - DUK_ERROR_ALLOC_DEFMSG(thr); - } - - tv_slot = thr->valstack_top; - DUK_TVAL_SET_BUFFER(tv_slot, h); - DUK_HBUFFER_INCREF(thr, h); - thr->valstack_top++; - - return (void *) buf_data; -} - -DUK_EXTERNAL duk_idx_t duk_push_heapptr(duk_context *ctx, void *ptr) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_idx_t ret; - - DUK_ASSERT_CTX_VALID(ctx); - - ret = (duk_idx_t) (thr->valstack_top - thr->valstack_bottom); - - if (ptr == NULL) { - goto push_undefined; - } - - switch ((int) DUK_HEAPHDR_GET_TYPE((duk_heaphdr *) ptr)) { - case DUK_HTYPE_STRING: - duk_push_hstring(ctx, (duk_hstring *) ptr); - break; - case DUK_HTYPE_OBJECT: - duk_push_hobject(ctx, (duk_hobject *) ptr); - break; - case DUK_HTYPE_BUFFER: - duk_push_hbuffer(ctx, (duk_hbuffer *) ptr); - break; - default: - goto push_undefined; - } - return ret; - - push_undefined: - duk_push_undefined(ctx); - return ret; -} - -DUK_INTERNAL duk_idx_t duk_push_object_internal(duk_context *ctx) { - return duk_push_object_helper(ctx, - DUK_HOBJECT_FLAG_EXTENSIBLE | - DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_OBJECT), - -1); /* no prototype */ -} - -DUK_INTERNAL void duk_push_hstring(duk_context *ctx, duk_hstring *h) { - duk_tval tv; - DUK_ASSERT_CTX_VALID(ctx); - DUK_ASSERT(h != NULL); - DUK_TVAL_SET_STRING(&tv, h); - duk_push_tval(ctx, &tv); -} - -DUK_INTERNAL void duk_push_hstring_stridx(duk_context *ctx, duk_small_int_t stridx) { - duk_hthread *thr = (duk_hthread *) ctx; - DUK_UNREF(thr); - DUK_ASSERT(stridx >= 0 && stridx < DUK_HEAP_NUM_STRINGS); - duk_push_hstring(ctx, DUK_HTHREAD_GET_STRING(thr, stridx)); -} - -DUK_INTERNAL void duk_push_hobject(duk_context *ctx, duk_hobject *h) { - duk_tval tv; - DUK_ASSERT_CTX_VALID(ctx); - DUK_ASSERT(h != NULL); - DUK_TVAL_SET_OBJECT(&tv, h); - duk_push_tval(ctx, &tv); -} - -DUK_INTERNAL void duk_push_hbuffer(duk_context *ctx, duk_hbuffer *h) { - duk_tval tv; - DUK_ASSERT_CTX_VALID(ctx); - DUK_ASSERT(h != NULL); - DUK_TVAL_SET_BUFFER(&tv, h); - duk_push_tval(ctx, &tv); -} - -DUK_INTERNAL void duk_push_hobject_bidx(duk_context *ctx, duk_small_int_t builtin_idx) { - duk_hthread *thr = (duk_hthread *) ctx; - DUK_ASSERT_CTX_VALID(ctx); - DUK_ASSERT(thr != NULL); - DUK_ASSERT(builtin_idx >= 0 && builtin_idx < DUK_NUM_BUILTINS); - DUK_ASSERT(thr->builtins[builtin_idx] != NULL); - duk_push_hobject(ctx, thr->builtins[builtin_idx]); -} - -/* - * Poppers - */ - -DUK_EXTERNAL void duk_pop_n(duk_context *ctx, duk_idx_t count) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_tval *tv; - - DUK_ASSERT_CTX_VALID(ctx); - - if (DUK_UNLIKELY(count < 0)) { - DUK_ERROR_API(thr, DUK_STR_INVALID_COUNT); - return; - } - - DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom); - if (DUK_UNLIKELY((duk_size_t) (thr->valstack_top - thr->valstack_bottom) < (duk_size_t) count)) { - DUK_ERROR_API(thr, DUK_STR_POP_TOO_MANY); - } - - /* - * Must be very careful here, every DECREF may cause reallocation - * of our valstack. - */ - - /* XXX: inlined DECREF macro would be nice here: no NULL check, - * refzero queueing but no refzero algorithm run (= no pointer - * instability), inline code. - */ - - /* XXX: optimize loops */ - -#if defined(DUK_USE_REFERENCE_COUNTING) - while (count > 0) { - count--; - tv = --thr->valstack_top; /* tv points to element just below prev top */ - DUK_ASSERT(tv >= thr->valstack_bottom); - DUK_TVAL_SET_UNDEFINED_UPDREF(thr, tv); /* side effects */ - } -#else - tv = thr->valstack_top; - while (count > 0) { - count--; - tv--; - DUK_ASSERT(tv >= thr->valstack_bottom); - DUK_TVAL_SET_UNDEFINED(tv); - } - thr->valstack_top = tv; -#endif - - DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom); -} - -/* Popping one element is called so often that when footprint is not an issue, - * compile a specialized function for it. - */ -#if defined(DUK_USE_PREFER_SIZE) -DUK_EXTERNAL void duk_pop(duk_context *ctx) { - DUK_ASSERT_CTX_VALID(ctx); - duk_pop_n(ctx, 1); -} -#else -DUK_EXTERNAL void duk_pop(duk_context *ctx) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_tval *tv; - DUK_ASSERT_CTX_VALID(ctx); - - DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom); - if (DUK_UNLIKELY(thr->valstack_top == thr->valstack_bottom)) { - DUK_ERROR_API(thr, DUK_STR_POP_TOO_MANY); - } - - tv = --thr->valstack_top; /* tv points to element just below prev top */ - DUK_ASSERT(tv >= thr->valstack_bottom); -#ifdef DUK_USE_REFERENCE_COUNTING - DUK_TVAL_SET_UNDEFINED_UPDREF(thr, tv); /* side effects */ -#else - DUK_TVAL_SET_UNDEFINED(tv); -#endif - DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom); -} -#endif /* !DUK_USE_PREFER_SIZE */ - -DUK_EXTERNAL void duk_pop_2(duk_context *ctx) { - DUK_ASSERT_CTX_VALID(ctx); - duk_pop_n(ctx, 2); -} - -DUK_EXTERNAL void duk_pop_3(duk_context *ctx) { - DUK_ASSERT_CTX_VALID(ctx); - duk_pop_n(ctx, 3); -} - -/* - * Error throwing - */ - -DUK_EXTERNAL void duk_throw(duk_context *ctx) { - duk_hthread *thr = (duk_hthread *) ctx; - - DUK_ASSERT(thr->valstack_bottom >= thr->valstack); - DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom); - DUK_ASSERT(thr->valstack_end >= thr->valstack_top); - - if (thr->valstack_top == thr->valstack_bottom) { - DUK_ERROR_API(thr, DUK_STR_INVALID_CALL_ARGS); - } - - /* Errors are augmented when they are created, not when they are - * thrown or re-thrown. The current error handler, however, runs - * just before an error is thrown. - */ - - /* Sync so that augmentation sees up-to-date activations, NULL - * thr->ptr_curr_pc so that it's not used if side effects occur - * in augmentation or longjmp handling. - */ - duk_hthread_sync_and_null_currpc(thr); - -#if defined(DUK_USE_AUGMENT_ERROR_THROW) - DUK_DDD(DUK_DDDPRINT("THROW ERROR (API): %!dT (before throw augment)", (duk_tval *) duk_get_tval(ctx, -1))); - duk_err_augment_error_throw(thr); -#endif - DUK_DDD(DUK_DDDPRINT("THROW ERROR (API): %!dT (after throw augment)", (duk_tval *) duk_get_tval(ctx, -1))); - - duk_err_setup_heap_ljstate(thr, DUK_LJ_TYPE_THROW); - - /* thr->heap->lj.jmpbuf_ptr is checked by duk_err_longjmp() so we don't - * need to check that here. If the value is NULL, a panic occurs because - * we can't return. - */ - - duk_err_longjmp(thr); - DUK_UNREACHABLE(); -} - -DUK_EXTERNAL void duk_fatal(duk_context *ctx, duk_errcode_t err_code, const char *err_msg) { - duk_hthread *thr = (duk_hthread *) ctx; - - DUK_ASSERT_CTX_VALID(ctx); - DUK_ASSERT(thr != NULL); - DUK_ASSERT(thr->heap != NULL); - DUK_ASSERT(thr->heap->fatal_func != NULL); - - DUK_D(DUK_DPRINT("fatal error occurred, code %ld, message %s", - (long) err_code, (const char *) err_msg)); - - /* fatal_func should be noreturn, but noreturn declarations on function - * pointers has a very spotty support apparently so it's not currently - * done. - */ - thr->heap->fatal_func(ctx, err_code, err_msg); - - DUK_PANIC(DUK_ERR_API_ERROR, "fatal handler returned"); -} - -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_ASSERT_CTX_VALID(ctx); - - duk_push_error_object_va_raw(ctx, err_code, filename, line, fmt, ap); - duk_throw(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, ...) { - va_list ap; - - DUK_ASSERT_CTX_VALID(ctx); - - va_start(ap, fmt); - duk_push_error_object_va_raw(ctx, err_code, filename, line, fmt, ap); - va_end(ap); - duk_throw(ctx); -} - -#if !defined(DUK_USE_VARIADIC_MACROS) -DUK_EXTERNAL void duk_error_stash(duk_context *ctx, duk_errcode_t err_code, const char *fmt, ...) { - const char *filename; - duk_int_t line; - va_list ap; - - DUK_ASSERT_CTX_VALID(ctx); - - filename = duk_api_global_filename; - line = duk_api_global_line; - duk_api_global_filename = NULL; - duk_api_global_line = 0; - - va_start(ap, fmt); - duk_push_error_object_va_raw(ctx, err_code, filename, line, fmt, ap); - va_end(ap); - duk_throw(ctx); -} -#endif /* DUK_USE_VARIADIC_MACROS */ - -/* - * Comparison - */ - -DUK_EXTERNAL duk_bool_t duk_equals(duk_context *ctx, duk_idx_t index1, duk_idx_t index2) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_tval *tv1, *tv2; - - DUK_ASSERT_CTX_VALID(ctx); - - tv1 = duk_get_tval(ctx, index1); - tv2 = duk_get_tval(ctx, index2); - if ((tv1 == NULL) || (tv2 == NULL)) { - return 0; - } - - /* Coercion may be needed, the helper handles that by pushing the - * tagged values to the stack. - */ - return duk_js_equals(thr, tv1, tv2); -} - -DUK_EXTERNAL duk_bool_t duk_strict_equals(duk_context *ctx, duk_idx_t index1, duk_idx_t index2) { - duk_tval *tv1, *tv2; - - DUK_ASSERT_CTX_VALID(ctx); - - tv1 = duk_get_tval(ctx, index1); - tv2 = duk_get_tval(ctx, index2); - if ((tv1 == NULL) || (tv2 == NULL)) { - return 0; - } - - /* No coercions or other side effects, so safe */ - return duk_js_strict_equals(tv1, tv2); -} - -/* - * instanceof - */ - -DUK_EXTERNAL duk_bool_t duk_instanceof(duk_context *ctx, duk_idx_t index1, duk_idx_t index2) { - duk_tval *tv1, *tv2; - - DUK_ASSERT_CTX_VALID(ctx); - - /* Index validation is strict, which differs from duk_equals(). - * The strict behavior mimics how instanceof itself works, e.g. - * it is a TypeError if rval is not a -callable- object. It would - * be somewhat inconsistent if rval would be allowed to be - * non-existent without a TypeError. - */ - tv1 = duk_require_tval(ctx, index1); - DUK_ASSERT(tv1 != NULL); - tv2 = duk_require_tval(ctx, index2); - DUK_ASSERT(tv2 != NULL); - - return duk_js_instanceof((duk_hthread *) ctx, tv1, tv2); -} - -/* - * Lightfunc - */ - -DUK_INTERNAL void duk_push_lightfunc_name(duk_context *ctx, duk_tval *tv) { - duk_c_function func; - - DUK_ASSERT(DUK_TVAL_IS_LIGHTFUNC(tv)); - - /* Lightfunc name, includes Duktape/C native function pointer, which - * can often be used to locate the function from a symbol table. - * The name also includes the 16-bit duk_tval flags field because it - * includes the magic value. Because a single native function often - * provides different functionality depending on the magic value, it - * seems reasonably to include it in the name. - * - * On the other hand, a complicated name increases string table - * pressure in low memory environments (but only when function name - * is accessed). - */ - - func = DUK_TVAL_GET_LIGHTFUNC_FUNCPTR(tv); - duk_push_sprintf(ctx, "light_"); - duk_push_string_funcptr(ctx, (duk_uint8_t *) &func, sizeof(func)); - duk_push_sprintf(ctx, "_%04x", (unsigned int) DUK_TVAL_GET_LIGHTFUNC_FLAGS(tv)); - duk_concat(ctx, 3); -} - -DUK_INTERNAL void duk_push_lightfunc_tostring(duk_context *ctx, duk_tval *tv) { - DUK_ASSERT(DUK_TVAL_IS_LIGHTFUNC(tv)); - - duk_push_string(ctx, "function "); - duk_push_lightfunc_name(ctx, tv); - duk_push_string(ctx, "() {\"light\"}"); - duk_concat(ctx, 3); -} - -/* - * Function pointers - * - * Printing function pointers is non-portable, so we do that by hex printing - * bytes from memory. - */ - -DUK_INTERNAL void duk_push_string_funcptr(duk_context *ctx, duk_uint8_t *ptr, duk_size_t sz) { - duk_uint8_t buf[32 * 2]; - duk_uint8_t *p, *q; - duk_small_uint_t i; - duk_small_uint_t t; - - DUK_ASSERT(sz <= 32); /* sanity limit for function pointer size */ - - p = buf; -#if defined(DUK_USE_INTEGER_LE) - q = ptr + sz; -#else - q = ptr; -#endif - for (i = 0; i < sz; i++) { -#if defined(DUK_USE_INTEGER_LE) - t = *(--q); -#else - t = *(q++); -#endif - *p++ = duk_lc_digits[t >> 4]; - *p++ = duk_lc_digits[t & 0x0f]; - } - - duk_push_lstring(ctx, (const char *) buf, sz * 2); -} - -#if !defined(DUK_USE_PARANOID_ERRORS) -/* - * Push readable string summarizing duk_tval. The operation is side effect - * free and will only throw from internal errors (e.g. out of memory). - * This is used by e.g. property access code to summarize a key/base safely, - * and is not intended to be fast (but small and safe). - */ - -#define DUK__READABLE_STRING_MAXCHARS 32 - -/* String sanitizer which escapes ASCII control characters and a few other - * ASCII characters, passes Unicode as is, and replaces invalid UTF-8 with - * question marks. No errors are thrown for any input string, except in out - * of memory situations. - */ -DUK_LOCAL void duk__push_hstring_readable_unicode(duk_context *ctx, duk_hstring *h_input) { - duk_hthread *thr; - const duk_uint8_t *p, *p_start, *p_end; - duk_uint8_t buf[DUK_UNICODE_MAX_XUTF8_LENGTH * DUK__READABLE_STRING_MAXCHARS + - 2 /*quotes*/ + 3 /*periods*/]; - duk_uint8_t *q; - duk_ucodepoint_t cp; - duk_small_uint_t nchars; - - DUK_ASSERT_CTX_VALID(ctx); - DUK_ASSERT(h_input != NULL); - thr = (duk_hthread *) ctx; - - p_start = (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h_input); - p_end = p_start + DUK_HSTRING_GET_BYTELEN(h_input); - p = p_start; - q = buf; - - nchars = 0; - *q++ = (duk_uint8_t) DUK_ASC_SINGLEQUOTE; - for (;;) { - if (p >= p_end) { - break; - } - if (nchars == DUK__READABLE_STRING_MAXCHARS) { - *q++ = (duk_uint8_t) DUK_ASC_PERIOD; - *q++ = (duk_uint8_t) DUK_ASC_PERIOD; - *q++ = (duk_uint8_t) DUK_ASC_PERIOD; - break; - } - if (duk_unicode_decode_xutf8(thr, &p, p_start, p_end, &cp)) { - if (cp < 0x20 || cp == 0x7f || cp == DUK_ASC_SINGLEQUOTE || cp == DUK_ASC_BACKSLASH) { - DUK_ASSERT(DUK_UNICODE_MAX_XUTF8_LENGTH >= 4); /* estimate is valid */ - DUK_ASSERT((cp >> 4) <= 0x0f); - *q++ = (duk_uint8_t) DUK_ASC_BACKSLASH; - *q++ = (duk_uint8_t) DUK_ASC_LC_X; - *q++ = (duk_uint8_t) duk_lc_digits[cp >> 4]; - *q++ = (duk_uint8_t) duk_lc_digits[cp & 0x0f]; - } else { - q += duk_unicode_encode_xutf8(cp, q); - } - } else { - p++; /* advance manually */ - *q++ = (duk_uint8_t) DUK_ASC_QUESTION; - } - nchars++; - } - *q++ = (duk_uint8_t) DUK_ASC_SINGLEQUOTE; - - duk_push_lstring(ctx, (const char *) buf, (duk_size_t) (q - buf)); -} - -DUK_INTERNAL const char *duk_push_string_tval_readable(duk_context *ctx, duk_tval *tv) { - duk_hthread *thr; - - DUK_ASSERT_CTX_VALID(ctx); - thr = (duk_hthread *) ctx; - DUK_UNREF(thr); - - if (tv == NULL) { - duk_push_string(ctx, "none"); - } else { - switch (DUK_TVAL_GET_TAG(tv)) { - case DUK_TAG_STRING: { - duk__push_hstring_readable_unicode(ctx, DUK_TVAL_GET_STRING(tv)); - break; - } - case DUK_TAG_OBJECT: { - duk_hobject *h = DUK_TVAL_GET_OBJECT(tv); - DUK_ASSERT(h != NULL); - duk_push_hobject_class_string(ctx, h); - break; - } - case DUK_TAG_BUFFER: { - /* XXX: Hex encoded, length limited buffer summary here? */ - duk_hbuffer *h = DUK_TVAL_GET_BUFFER(tv); - DUK_ASSERT(h != NULL); - duk_push_sprintf(ctx, "[buffer:%ld]", (long) DUK_HBUFFER_GET_SIZE(h)); - break; - } - case DUK_TAG_POINTER: { - /* Surround with parentheses like in JX, ensures NULL pointer - * is distinguishable from null value ("(null)" vs "null"). - */ - duk_push_tval(ctx, tv); - duk_push_sprintf(ctx, "(%s)", duk_to_string(ctx, -1)); - duk_remove(ctx, -2); - break; - } - default: { - duk_push_tval(ctx, tv); - break; - } - } - } - - return duk_to_string(ctx, -1); -} - -DUK_INTERNAL const char *duk_push_string_readable(duk_context *ctx, duk_idx_t index) { - DUK_ASSERT_CTX_VALID(ctx); - return duk_push_string_tval_readable(ctx, duk_get_tval(ctx, index)); -} -#endif /* !DUK_USE_PARANOID_ERRORS */ - -#undef DUK__CHECK_SPACE -#undef DUK__PACK_ARGS -#undef DUK__READABLE_STRING_MAXCHARS -#line 1 "duk_api_string.c" -/* - * String manipulation - */ - -/* include removed: duk_internal.h */ - -DUK_LOCAL void duk__concat_and_join_helper(duk_context *ctx, duk_idx_t count_in, duk_bool_t is_join) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_uint_t count; - duk_uint_t i; - duk_size_t idx; - duk_size_t len; - duk_hstring *h; - duk_uint8_t *buf; - - DUK_ASSERT_CTX_VALID(ctx); - - if (DUK_UNLIKELY(count_in <= 0)) { - if (count_in < 0) { - DUK_ERROR_API(thr, DUK_STR_INVALID_COUNT); - return; - } - DUK_ASSERT(count_in == 0); - duk_push_hstring_stridx(ctx, DUK_STRIDX_EMPTY_STRING); - return; - } - count = (duk_uint_t) count_in; - - if (is_join) { - duk_size_t t1, t2, limit; - h = duk_to_hstring(ctx, -((duk_idx_t) count) - 1); - DUK_ASSERT(h != NULL); - - /* A bit tricky overflow test, see doc/code-issues.rst. */ - t1 = (duk_size_t) DUK_HSTRING_GET_BYTELEN(h); - t2 = (duk_size_t) (count - 1); - limit = (duk_size_t) DUK_HSTRING_MAX_BYTELEN; - if (DUK_UNLIKELY(t2 != 0 && t1 > limit / t2)) { - /* Combined size of separators already overflows */ - goto error_overflow; - } - len = (duk_size_t) (t1 * t2); - } else { - len = (duk_size_t) 0; - } - - for (i = count; i >= 1; i--) { - duk_size_t new_len; - duk_to_string(ctx, -((duk_idx_t) i)); - h = duk_require_hstring(ctx, -((duk_idx_t) i)); - new_len = len + (duk_size_t) DUK_HSTRING_GET_BYTELEN(h); - - /* Impose a string maximum length, need to handle overflow - * correctly. - */ - if (new_len < len || /* wrapped */ - new_len > (duk_size_t) DUK_HSTRING_MAX_BYTELEN) { - goto error_overflow; - } - len = new_len; - } - - DUK_DDD(DUK_DDDPRINT("join/concat %lu strings, total length %lu bytes", - (unsigned long) count, (unsigned long) len)); - - /* use stack allocated buffer to ensure reachability in errors (e.g. intern error) */ - buf = (duk_uint8_t *) duk_push_fixed_buffer(ctx, len); - DUK_ASSERT(buf != NULL); - - /* [... (sep) str1 str2 ... strN buf] */ - - idx = 0; - for (i = count; i >= 1; i--) { - if (is_join && i != count) { - h = duk_require_hstring(ctx, -((duk_idx_t) count) - 2); /* extra -1 for buffer */ - DUK_MEMCPY(buf + idx, DUK_HSTRING_GET_DATA(h), DUK_HSTRING_GET_BYTELEN(h)); - idx += DUK_HSTRING_GET_BYTELEN(h); - } - h = duk_require_hstring(ctx, -((duk_idx_t) i) - 1); /* extra -1 for buffer */ - DUK_MEMCPY(buf + idx, DUK_HSTRING_GET_DATA(h), DUK_HSTRING_GET_BYTELEN(h)); - idx += DUK_HSTRING_GET_BYTELEN(h); - } - - DUK_ASSERT(idx == len); - - /* [... (sep) str1 str2 ... strN buf] */ - - /* get rid of the strings early to minimize memory use before intern */ - - if (is_join) { - duk_replace(ctx, -((duk_idx_t) count) - 2); /* overwrite sep */ - duk_pop_n(ctx, count); - } else { - duk_replace(ctx, -((duk_idx_t) count) - 1); /* overwrite str1 */ - duk_pop_n(ctx, count-1); - } - - /* [... buf] */ - - (void) duk_to_string(ctx, -1); - - /* [... res] */ - return; - - error_overflow: - DUK_ERROR_RANGE(thr, DUK_STR_CONCAT_RESULT_TOO_LONG); -} - -DUK_EXTERNAL void duk_concat(duk_context *ctx, duk_idx_t count) { - DUK_ASSERT_CTX_VALID(ctx); - - duk__concat_and_join_helper(ctx, count, 0 /*is_join*/); -} - -DUK_EXTERNAL void duk_join(duk_context *ctx, duk_idx_t count) { - DUK_ASSERT_CTX_VALID(ctx); - - duk__concat_and_join_helper(ctx, count, 1 /*is_join*/); -} - -/* XXX: could map/decode be unified with duk_unicode_support.c code? - * Case conversion needs also the character surroundings though. - */ - -DUK_EXTERNAL void duk_decode_string(duk_context *ctx, duk_idx_t index, duk_decode_char_function callback, void *udata) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_hstring *h_input; - const duk_uint8_t *p, *p_start, *p_end; - duk_codepoint_t cp; - - DUK_ASSERT_CTX_VALID(ctx); - - h_input = duk_require_hstring(ctx, index); - DUK_ASSERT(h_input != NULL); - - p_start = (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h_input); - p_end = p_start + DUK_HSTRING_GET_BYTELEN(h_input); - p = p_start; - - for (;;) { - if (p >= p_end) { - break; - } - cp = (int) duk_unicode_decode_xutf8_checked(thr, &p, p_start, p_end); - callback(udata, cp); - } -} - -DUK_EXTERNAL void duk_map_string(duk_context *ctx, duk_idx_t index, duk_map_char_function callback, void *udata) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_hstring *h_input; - duk_bufwriter_ctx bw_alloc; - duk_bufwriter_ctx *bw; - const duk_uint8_t *p, *p_start, *p_end; - duk_codepoint_t cp; - - DUK_ASSERT_CTX_VALID(ctx); - - index = duk_normalize_index(ctx, index); - - h_input = duk_require_hstring(ctx, index); - DUK_ASSERT(h_input != NULL); - - bw = &bw_alloc; - DUK_BW_INIT_PUSHBUF(thr, bw, DUK_HSTRING_GET_BYTELEN(h_input)); /* reasonable output estimate */ - - p_start = (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h_input); - p_end = p_start + DUK_HSTRING_GET_BYTELEN(h_input); - p = p_start; - - for (;;) { - /* XXX: could write output in chunks with fewer ensure calls, - * but relative benefit would be small here. - */ - - if (p >= p_end) { - break; - } - cp = (int) duk_unicode_decode_xutf8_checked(thr, &p, p_start, p_end); - cp = callback(udata, cp); - - DUK_BW_WRITE_ENSURE_XUTF8(thr, bw, cp); - } - - DUK_BW_COMPACT(thr, bw); - duk_to_string(ctx, -1); - duk_replace(ctx, index); -} - -DUK_EXTERNAL void duk_substring(duk_context *ctx, duk_idx_t index, duk_size_t start_offset, duk_size_t end_offset) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_hstring *h; - duk_hstring *res; - duk_size_t start_byte_offset; - duk_size_t end_byte_offset; - - DUK_ASSERT_CTX_VALID(ctx); - - index = duk_require_normalize_index(ctx, index); - h = duk_require_hstring(ctx, index); - DUK_ASSERT(h != NULL); - - if (end_offset >= DUK_HSTRING_GET_CHARLEN(h)) { - end_offset = DUK_HSTRING_GET_CHARLEN(h); - } - if (start_offset > end_offset) { - start_offset = end_offset; - } - - DUK_ASSERT_DISABLE(start_offset >= 0); - DUK_ASSERT(start_offset <= end_offset && start_offset <= DUK_HSTRING_GET_CHARLEN(h)); - DUK_ASSERT_DISABLE(end_offset >= 0); - DUK_ASSERT(end_offset >= start_offset && end_offset <= DUK_HSTRING_GET_CHARLEN(h)); - - /* guaranteed by string limits */ - DUK_ASSERT(start_offset <= DUK_UINT32_MAX); - DUK_ASSERT(end_offset <= DUK_UINT32_MAX); - - start_byte_offset = (duk_size_t) duk_heap_strcache_offset_char2byte(thr, h, (duk_uint_fast32_t) start_offset); - end_byte_offset = (duk_size_t) duk_heap_strcache_offset_char2byte(thr, h, (duk_uint_fast32_t) end_offset); - - DUK_ASSERT(end_byte_offset >= start_byte_offset); - DUK_ASSERT(end_byte_offset - start_byte_offset <= DUK_UINT32_MAX); /* guaranteed by string limits */ - - /* no size check is necessary */ - res = duk_heap_string_intern_checked(thr, - DUK_HSTRING_GET_DATA(h) + start_byte_offset, - (duk_uint32_t) (end_byte_offset - start_byte_offset)); - - duk_push_hstring(ctx, res); - duk_replace(ctx, index); -} - -/* XXX: this is quite clunky. Add Unicode helpers to scan backwards and - * forwards with a callback to process codepoints? - */ -DUK_EXTERNAL void duk_trim(duk_context *ctx, duk_idx_t index) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_hstring *h; - const duk_uint8_t *p, *p_start, *p_end, *p_tmp1, *p_tmp2; /* pointers for scanning */ - const duk_uint8_t *q_start, *q_end; /* start (incl) and end (excl) of trimmed part */ - duk_codepoint_t cp; - - DUK_ASSERT_CTX_VALID(ctx); - - index = duk_require_normalize_index(ctx, index); - h = duk_require_hstring(ctx, index); - DUK_ASSERT(h != NULL); - - p_start = DUK_HSTRING_GET_DATA(h); - p_end = p_start + DUK_HSTRING_GET_BYTELEN(h); - - p = p_start; - while (p < p_end) { - p_tmp1 = p; - cp = (duk_codepoint_t) duk_unicode_decode_xutf8_checked(thr, &p_tmp1, p_start, p_end); - if (!(duk_unicode_is_whitespace(cp) || duk_unicode_is_line_terminator(cp))) { - break; - } - p = p_tmp1; - } - q_start = p; - if (p == p_end) { - /* entire string is whitespace */ - q_end = p; - goto scan_done; - } - - p = p_end; - while (p > p_start) { - p_tmp1 = p; - while (p > p_start) { - p--; - if (((*p) & 0xc0) != 0x80) { - break; - } - } - p_tmp2 = p; - - cp = (duk_codepoint_t) duk_unicode_decode_xutf8_checked(thr, &p_tmp2, p_start, p_end); - if (!(duk_unicode_is_whitespace(cp) || duk_unicode_is_line_terminator(cp))) { - p = p_tmp1; - break; - } - } - q_end = p; - - scan_done: - /* This may happen when forward and backward scanning disagree - * (possible for non-extended-UTF-8 strings). - */ - if (q_end < q_start) { - q_end = q_start; - } - - DUK_ASSERT(q_start >= p_start && q_start <= p_end); - DUK_ASSERT(q_end >= p_start && q_end <= p_end); - DUK_ASSERT(q_end >= q_start); - - DUK_DDD(DUK_DDDPRINT("trim: p_start=%p, p_end=%p, q_start=%p, q_end=%p", - (const void *) p_start, (const void *) p_end, - (const void *) q_start, (const void *) q_end)); - - if (q_start == p_start && q_end == p_end) { - DUK_DDD(DUK_DDDPRINT("nothing was trimmed: avoid interning (hashing etc)")); - return; - } - - duk_push_lstring(ctx, (const char *) q_start, (duk_size_t) (q_end - q_start)); - duk_replace(ctx, index); -} - -DUK_EXTERNAL duk_codepoint_t duk_char_code_at(duk_context *ctx, duk_idx_t index, duk_size_t char_offset) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_hstring *h; - duk_ucodepoint_t cp; - - DUK_ASSERT_CTX_VALID(ctx); - - h = duk_require_hstring(ctx, index); - DUK_ASSERT(h != NULL); - - DUK_ASSERT_DISABLE(char_offset >= 0); /* always true, arg is unsigned */ - if (char_offset >= DUK_HSTRING_GET_CHARLEN(h)) { - return 0; - } - - DUK_ASSERT(char_offset <= DUK_UINT_MAX); /* guaranteed by string limits */ - cp = duk_hstring_char_code_at_raw(thr, h, (duk_uint_t) char_offset); - return (duk_codepoint_t) cp; -} -#line 1 "duk_api_var.c" -/* - * Variable access - */ - -/* include removed: duk_internal.h */ - -DUK_EXTERNAL void duk_get_var(duk_context *ctx) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_activation *act; - duk_hstring *h_varname; - duk_small_int_t throw_flag = 1; /* always throw ReferenceError for unresolvable */ - - DUK_ASSERT_CTX_VALID(ctx); - - h_varname = duk_require_hstring(ctx, -1); /* XXX: tostring? */ - DUK_ASSERT(h_varname != NULL); - - act = duk_hthread_get_current_activation(thr); - if (act) { - (void) duk_js_getvar_activation(thr, act, h_varname, throw_flag); /* -> [ ... varname val this ] */ - } else { - /* Outside any activation -> look up from global. */ - DUK_ASSERT(thr->builtins[DUK_BIDX_GLOBAL_ENV] != NULL); - (void) duk_js_getvar_envrec(thr, thr->builtins[DUK_BIDX_GLOBAL_ENV], h_varname, throw_flag); - } - - /* [ ... varname val this ] (because throw_flag == 1, always resolved) */ - - duk_pop(ctx); - duk_remove(ctx, -2); - - /* [ ... val ] */ - - /* Return value would be pointless: because throw_flag==1, we always - * throw if the identifier doesn't resolve. - */ - return; -} - -DUK_EXTERNAL void duk_put_var(duk_context *ctx) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_activation *act; - duk_hstring *h_varname; - duk_tval *tv_val; - duk_small_int_t throw_flag; - - DUK_ASSERT_CTX_VALID(ctx); - - h_varname = duk_require_hstring(ctx, -2); /* XXX: tostring? */ - DUK_ASSERT(h_varname != NULL); - - tv_val = duk_require_tval(ctx, -1); - - throw_flag = duk_is_strict_call(ctx); - - act = duk_hthread_get_current_activation(thr); - if (act) { - duk_js_putvar_activation(thr, act, h_varname, tv_val, throw_flag); /* -> [ ... varname val this ] */ - } else { - /* Outside any activation -> put to global. */ - DUK_ASSERT(thr->builtins[DUK_BIDX_GLOBAL_ENV] != NULL); - duk_js_putvar_envrec(thr, thr->builtins[DUK_BIDX_GLOBAL_ENV], h_varname, tv_val, throw_flag); - } - - /* [ ... varname val ] */ - - duk_pop_2(ctx); - - /* [ ... ] */ - - return; -} - -DUK_EXTERNAL duk_bool_t duk_del_var(duk_context *ctx) { - DUK_ASSERT_CTX_VALID(ctx); - - DUK_ERROR_UNIMPLEMENTED_DEFMSG((duk_hthread *) ctx); - return 0; -} - -DUK_EXTERNAL duk_bool_t duk_has_var(duk_context *ctx) { - DUK_ASSERT_CTX_VALID(ctx); - - DUK_ERROR_UNIMPLEMENTED_DEFMSG((duk_hthread *) ctx); - return 0; -} -#line 1 "duk_bi_array.c" -/* - * Array built-ins - * - * Note that most Array built-ins are intentionally generic and work even - * when the 'this' binding is not an Array instance. To ensure this, - * Array algorithms do not assume "magical" Array behavior for the "length" - * property, for instance. - * - * XXX: the "Throw" flag should be set for (almost?) all [[Put]] and - * [[Delete]] operations, but it's currently false throughout. Go through - * all put/delete cases and check throw flag use. Need a new API primitive - * which allows throws flag to be specified. - * - * XXX: array lengths above 2G won't work reliably. There are many places - * where one needs a full signed 32-bit range ([-0xffffffff, 0xffffffff], - * i.e. -33- bits). Although array 'length' cannot be written to be outside - * the unsigned 32-bit range (E5.1 Section 15.4.5.1 throws a RangeError if so) - * some intermediate values may be above 0xffffffff and this may not be always - * correctly handled now (duk_uint32_t is not enough for all algorithms). - * - * For instance, push() can legitimately write entries beyond length 0xffffffff - * and cause a RangeError only at the end. To do this properly, the current - * push() implementation tracks the array index using a 'double' instead of a - * duk_uint32_t (which is somewhat awkward). See test-bi-array-push-maxlen.js. - * - * On using "put" vs. "def" prop - * ============================= - * - * Code below must be careful to use the appropriate primitive as it matters - * for compliance. When using "put" there may be inherited properties in - * Array.prototype which cause side effects when values are written. When - * using "define" there are no such side effects, and many test262 test cases - * check for this (for real world code, such side effects are very rare). - * Both "put" and "define" are used in the E5.1 specification; as a rule, - * "put" is used when modifying an existing array (or a non-array 'this' - * binding) and "define" for setting values into a fresh result array. - * - * Also note that Array instance 'length' should be writable, but not - * enumerable and definitely not configurable: even Duktape code internally - * assumes that an Array instance will always have a 'length' property. - * Preventing deletion of the property is critical. - */ - -/* include removed: duk_internal.h */ - -/* Perform an intermediate join when this many elements have been pushed - * on the value stack. - */ -#define DUK__ARRAY_MID_JOIN_LIMIT 4096 - -/* Shared entry code for many Array built-ins. Note that length is left - * on stack (it could be popped, but that's not necessary). - */ -DUK_LOCAL duk_uint32_t duk__push_this_obj_len_u32(duk_context *ctx) { - duk_uint32_t len; - - (void) duk_push_this_coercible_to_object(ctx); - duk_get_prop_stridx(ctx, -1, DUK_STRIDX_LENGTH); - len = duk_to_uint32(ctx, -1); - - /* -> [ ... ToObject(this) ToUint32(length) ] */ - return len; -} - -DUK_LOCAL duk_uint32_t duk__push_this_obj_len_u32_limited(duk_context *ctx) { - /* Range limited to [0, 0x7fffffff] range, i.e. range that can be - * represented with duk_int32_t. Use this when the method doesn't - * handle the full 32-bit unsigned range correctly. - */ - duk_uint32_t ret = duk__push_this_obj_len_u32(ctx); - if (DUK_UNLIKELY(ret >= 0x80000000UL)) { - DUK_ERROR_RANGE((duk_hthread *) ctx, DUK_STR_ARRAY_LENGTH_OVER_2G); - } - return ret; -} - -/* - * Constructor - */ - -DUK_INTERNAL duk_ret_t duk_bi_array_constructor(duk_context *ctx) { - duk_idx_t nargs; - duk_double_t d; - duk_uint32_t len; - duk_idx_t i; - - nargs = duk_get_top(ctx); - duk_push_array(ctx); - - if (nargs == 1 && duk_is_number(ctx, 0)) { - /* XXX: expensive check (also shared elsewhere - so add a shared internal API call?) */ - d = duk_get_number(ctx, 0); - len = duk_to_uint32(ctx, 0); - if (((duk_double_t) len) != d) { - return DUK_RET_RANGE_ERROR; - } - - /* XXX: if 'len' is low, may want to ensure array part is kept: - * the caller is likely to want a dense array. - */ - duk_push_u32(ctx, len); - duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_LENGTH, DUK_PROPDESC_FLAGS_W); /* [ ToUint32(len) array ToUint32(len) ] -> [ ToUint32(len) array ] */ - return 1; - } - - /* XXX: optimize by creating array into correct size directly, and - * operating on the array part directly; values can be memcpy()'d from - * value stack directly as long as refcounts are increased. - */ - for (i = 0; i < nargs; i++) { - duk_dup(ctx, i); - duk_xdef_prop_index_wec(ctx, -2, (duk_uarridx_t) i); - } - - duk_push_u32(ctx, (duk_uint32_t) nargs); - duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_LENGTH, DUK_PROPDESC_FLAGS_W); - return 1; -} - -/* - * isArray() - */ - -DUK_INTERNAL duk_ret_t duk_bi_array_constructor_is_array(duk_context *ctx) { - duk_hobject *h; - - h = duk_get_hobject_with_class(ctx, 0, DUK_HOBJECT_CLASS_ARRAY); - duk_push_boolean(ctx, (h != NULL)); - return 1; -} - -/* - * toString() - */ - -DUK_INTERNAL duk_ret_t duk_bi_array_prototype_to_string(duk_context *ctx) { - (void) duk_push_this_coercible_to_object(ctx); - duk_get_prop_stridx(ctx, -1, DUK_STRIDX_JOIN); - - /* [ ... this func ] */ - if (!duk_is_callable(ctx, -1)) { - /* Fall back to the initial (original) Object.toString(). We don't - * currently have pointers to the built-in functions, only the top - * level global objects (like "Array") so this is now done in a bit - * of a hacky manner. It would be cleaner to push the (original) - * function and use duk_call_method(). - */ - - /* XXX: 'this' will be ToObject() coerced twice, which is incorrect - * but should have no visible side effects. - */ - DUK_DDD(DUK_DDDPRINT("this.join is not callable, fall back to (original) Object.toString")); - duk_set_top(ctx, 0); - return duk_bi_object_prototype_to_string(ctx); /* has access to 'this' binding */ - } - - /* [ ... this func ] */ - - duk_insert(ctx, -2); - - /* [ ... func this ] */ - - DUK_DDD(DUK_DDDPRINT("calling: func=%!iT, this=%!iT", - (duk_tval *) duk_get_tval(ctx, -2), - (duk_tval *) duk_get_tval(ctx, -1))); - duk_call_method(ctx, 0); - - return 1; -} - -/* - * concat() - */ - -DUK_INTERNAL duk_ret_t duk_bi_array_prototype_concat(duk_context *ctx) { - duk_idx_t i, n; - duk_uarridx_t idx, idx_last; - duk_uarridx_t j, len; - duk_hobject *h; - - /* XXX: the insert here is a bit expensive if there are a lot of items. - * It could also be special cased in the outermost for loop quite easily - * (as the element is dup()'d anyway). - */ - - (void) duk_push_this_coercible_to_object(ctx); - duk_insert(ctx, 0); - n = duk_get_top(ctx); - duk_push_array(ctx); /* -> [ ToObject(this) item1 ... itemN arr ] */ - - /* NOTE: The Array special behaviors are NOT invoked by duk_xdef_prop_index() - * (which differs from the official algorithm). If no error is thrown, this - * doesn't matter as the length is updated at the end. However, if an error - * is thrown, the length will be unset. That shouldn't matter because the - * caller won't get a reference to the intermediate value. - */ - - idx = 0; - idx_last = 0; - for (i = 0; i < n; i++) { - DUK_ASSERT_TOP(ctx, n + 1); - - /* [ ToObject(this) item1 ... itemN arr ] */ - - duk_dup(ctx, i); - h = duk_get_hobject_with_class(ctx, -1, DUK_HOBJECT_CLASS_ARRAY); - if (!h) { - duk_xdef_prop_index_wec(ctx, -2, idx++); - idx_last = idx; - continue; - } - - /* [ ToObject(this) item1 ... itemN arr item(i) ] */ - - /* XXX: an array can have length higher than 32 bits; this is not handled - * correctly now. - */ - len = (duk_uarridx_t) duk_get_length(ctx, -1); - for (j = 0; j < len; j++) { - if (duk_get_prop_index(ctx, -1, j)) { - /* [ ToObject(this) item1 ... itemN arr item(i) item(i)[j] ] */ - duk_xdef_prop_index_wec(ctx, -3, idx++); - idx_last = idx; - } else { - idx++; - duk_pop(ctx); -#if defined(DUK_USE_NONSTD_ARRAY_CONCAT_TRAILER) - /* According to E5.1 Section 15.4.4.4 nonexistent trailing - * elements do not affect 'length' of the result. Test262 - * and other engines disagree, so update idx_last here too. - */ - idx_last = idx; -#else - /* Strict standard behavior, ignore trailing elements for - * result 'length'. - */ -#endif - } - } - duk_pop(ctx); - } - - /* The E5.1 Section 15.4.4.4 algorithm doesn't set the length explicitly - * in the end, but because we're operating with an internal value which - * is known to be an array, this should be equivalent. - */ - duk_push_uarridx(ctx, idx_last); - duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_LENGTH, DUK_PROPDESC_FLAGS_W); - - DUK_ASSERT_TOP(ctx, n + 1); - return 1; -} - -/* - * join(), toLocaleString() - * - * Note: checking valstack is necessary, but only in the per-element loop. - * - * Note: the trivial approach of pushing all the elements on the value stack - * and then calling duk_join() fails when the array contains a large number - * of elements. This problem can't be offloaded to duk_join() because the - * elements to join must be handled here and have special handling. Current - * approach is to do intermediate joins with very large number of elements. - * There is no fancy handling; the prefix gets re-joined multiple times. - */ - -DUK_INTERNAL duk_ret_t duk_bi_array_prototype_join_shared(duk_context *ctx) { - duk_uint32_t len, count; - duk_uint32_t idx; - duk_small_int_t to_locale_string = duk_get_current_magic(ctx); - duk_idx_t valstack_required; - - /* For join(), nargs is 1. For toLocaleString(), nargs is 0 and - * setting the top essentially pushes an undefined to the stack, - * thus defaulting to a comma separator. - */ - duk_set_top(ctx, 1); - if (duk_is_undefined(ctx, 0)) { - duk_pop(ctx); - duk_push_hstring_stridx(ctx, DUK_STRIDX_COMMA); - } else { - duk_to_string(ctx, 0); - } - - len = duk__push_this_obj_len_u32(ctx); - - /* [ sep ToObject(this) len ] */ - - DUK_DDD(DUK_DDDPRINT("sep=%!T, this=%!T, len=%lu", - (duk_tval *) duk_get_tval(ctx, 0), - (duk_tval *) duk_get_tval(ctx, 1), - (unsigned long) len)); - - /* The extra (+4) is tight. */ - valstack_required = (len >= DUK__ARRAY_MID_JOIN_LIMIT ? - DUK__ARRAY_MID_JOIN_LIMIT : len) + 4; - duk_require_stack(ctx, valstack_required); - - duk_dup(ctx, 0); - - /* [ sep ToObject(this) len sep ] */ - - count = 0; - idx = 0; - for (;;) { - if (count >= DUK__ARRAY_MID_JOIN_LIMIT || /* intermediate join to avoid valstack overflow */ - idx >= len) { /* end of loop (careful with len==0) */ - /* [ sep ToObject(this) len sep str0 ... str(count-1) ] */ - DUK_DDD(DUK_DDDPRINT("mid/final join, count=%ld, idx=%ld, len=%ld", - (long) count, (long) idx, (long) len)); - duk_join(ctx, (duk_idx_t) count); /* -> [ sep ToObject(this) len str ] */ - duk_dup(ctx, 0); /* -> [ sep ToObject(this) len str sep ] */ - duk_insert(ctx, -2); /* -> [ sep ToObject(this) len sep str ] */ - count = 1; - } - if (idx >= len) { - /* if true, the stack already contains the final result */ - break; - } - - duk_get_prop_index(ctx, 1, (duk_uarridx_t) idx); - if (duk_is_null_or_undefined(ctx, -1)) { - duk_pop(ctx); - duk_push_hstring_stridx(ctx, DUK_STRIDX_EMPTY_STRING); - } else { - if (to_locale_string) { - duk_to_object(ctx, -1); - duk_get_prop_stridx(ctx, -1, DUK_STRIDX_TO_LOCALE_STRING); - duk_insert(ctx, -2); /* -> [ ... toLocaleString ToObject(val) ] */ - duk_call_method(ctx, 0); - duk_to_string(ctx, -1); - } else { - duk_to_string(ctx, -1); - } - } - - count++; - idx++; - } - - /* [ sep ToObject(this) len sep result ] */ - - return 1; -} - -/* - * pop(), push() - */ - -DUK_INTERNAL duk_ret_t duk_bi_array_prototype_pop(duk_context *ctx) { - duk_uint32_t len; - duk_uint32_t idx; - - DUK_ASSERT_TOP(ctx, 0); - len = duk__push_this_obj_len_u32(ctx); - if (len == 0) { - duk_push_int(ctx, 0); - duk_put_prop_stridx(ctx, 0, DUK_STRIDX_LENGTH); - return 0; - } - idx = len - 1; - - duk_get_prop_index(ctx, 0, (duk_uarridx_t) idx); - duk_del_prop_index(ctx, 0, (duk_uarridx_t) idx); - duk_push_u32(ctx, idx); - duk_put_prop_stridx(ctx, 0, DUK_STRIDX_LENGTH); - return 1; -} - -DUK_INTERNAL duk_ret_t duk_bi_array_prototype_push(duk_context *ctx) { - /* Note: 'this' is not necessarily an Array object. The push() - * algorithm is supposed to work for other kinds of objects too, - * so the algorithm has e.g. an explicit update for the 'length' - * property which is normally "magical" in arrays. - */ - - duk_uint32_t len; - duk_idx_t i, n; - - n = duk_get_top(ctx); - len = duk__push_this_obj_len_u32(ctx); - - /* [ arg1 ... argN obj length ] */ - - /* Technically Array.prototype.push() can create an Array with length - * longer than 2^32-1, i.e. outside the 32-bit range. The final length - * is *not* wrapped to 32 bits in the specification. - * - * This implementation tracks length with a uint32 because it's much - * more practical. - * - * See: test-bi-array-push-maxlen.js. - */ - - if (len + (duk_uint32_t) n < len) { - DUK_D(DUK_DPRINT("Array.prototype.push() would go beyond 32-bit length, throw")); - return DUK_RET_RANGE_ERROR; - } - - for (i = 0; i < n; i++) { - duk_dup(ctx, i); - duk_put_prop_index(ctx, -3, len + i); - } - len += n; - - duk_push_u32(ctx, len); - duk_dup_top(ctx); - duk_put_prop_stridx(ctx, -4, DUK_STRIDX_LENGTH); - - /* [ arg1 ... argN obj length new_length ] */ - return 1; -} - -/* - * sort() - * - * Currently qsort with random pivot. This is now really, really slow, - * because there is no fast path for array parts. - * - * Signed indices are used because qsort() leaves and degenerate cases - * may use a negative offset. - */ - -DUK_LOCAL duk_small_int_t duk__array_sort_compare(duk_context *ctx, duk_int_t idx1, duk_int_t idx2) { - duk_bool_t have1, have2; - duk_bool_t undef1, undef2; - duk_small_int_t ret; - duk_idx_t idx_obj = 1; /* fixed offsets in valstack */ - duk_idx_t idx_fn = 0; - duk_hstring *h1, *h2; - - /* Fast exit if indices are identical. This is valid for a non-existent property, - * for an undefined value, and almost always for ToString() coerced comparison of - * arbitrary values (corner cases where this is not the case include e.g. a an - * object with varying ToString() coercion). - * - * The specification does not prohibit "caching" of values read from the array, so - * assuming equality for comparing an index with itself falls into the category of - * "caching". - * - * Also, compareFn may be inconsistent, so skipping a call to compareFn here may - * have an effect on the final result. The specification does not require any - * specific behavior for inconsistent compare functions, so again, this fast path - * is OK. - */ - - if (idx1 == idx2) { - DUK_DDD(DUK_DDDPRINT("duk__array_sort_compare: idx1=%ld, idx2=%ld -> indices identical, quick exit", - (long) idx1, (long) idx2)); - return 0; - } - - have1 = duk_get_prop_index(ctx, idx_obj, (duk_uarridx_t) idx1); - have2 = duk_get_prop_index(ctx, idx_obj, (duk_uarridx_t) idx2); - - DUK_DDD(DUK_DDDPRINT("duk__array_sort_compare: idx1=%ld, idx2=%ld, have1=%ld, have2=%ld, val1=%!T, val2=%!T", - (long) idx1, (long) idx2, (long) have1, (long) have2, - (duk_tval *) duk_get_tval(ctx, -2), (duk_tval *) duk_get_tval(ctx, -1))); - - if (have1) { - if (have2) { - ; - } else { - ret = -1; - goto pop_ret; - } - } else { - if (have2) { - ret = 1; - goto pop_ret; - } else { - ret = 0; - goto pop_ret; - } - } - - undef1 = duk_is_undefined(ctx, -2); - undef2 = duk_is_undefined(ctx, -1); - if (undef1) { - if (undef2) { - ret = 0; - goto pop_ret; - } else { - ret = 1; - goto pop_ret; - } - } else { - if (undef2) { - ret = -1; - goto pop_ret; - } else { - ; - } - } - - if (!duk_is_undefined(ctx, idx_fn)) { - duk_double_t d; - - /* no need to check callable; duk_call() will do that */ - duk_dup(ctx, idx_fn); /* -> [ ... x y fn ] */ - duk_insert(ctx, -3); /* -> [ ... fn x y ] */ - duk_call(ctx, 2); /* -> [ ... res ] */ - - /* The specification is a bit vague what to do if the return - * value is not a number. Other implementations seem to - * tolerate non-numbers but e.g. V8 won't apparently do a - * ToNumber(). - */ - - /* XXX: best behavior for real world compatibility? */ - - d = duk_to_number(ctx, -1); - if (d < 0.0) { - ret = -1; - } else if (d > 0.0) { - ret = 1; - } else { - ret = 0; - } - - duk_pop(ctx); - DUK_DDD(DUK_DDDPRINT("-> result %ld (from comparefn, after coercion)", (long) ret)); - return ret; - } - - /* string compare is the default (a bit oddly) */ - - h1 = duk_to_hstring(ctx, -2); - h2 = duk_to_hstring(ctx, -1); - DUK_ASSERT(h1 != NULL); - DUK_ASSERT(h2 != NULL); - - ret = duk_js_string_compare(h1, h2); /* retval is directly usable */ - goto pop_ret; - - pop_ret: - duk_pop_2(ctx); - DUK_DDD(DUK_DDDPRINT("-> result %ld", (long) ret)); - return ret; -} - -DUK_LOCAL void duk__array_sort_swap(duk_context *ctx, duk_int_t l, duk_int_t r) { - duk_bool_t have_l, have_r; - duk_idx_t idx_obj = 1; /* fixed offset in valstack */ - - if (l == r) { - return; - } - - /* swap elements; deal with non-existent elements correctly */ - have_l = duk_get_prop_index(ctx, idx_obj, (duk_uarridx_t) l); - have_r = duk_get_prop_index(ctx, idx_obj, (duk_uarridx_t) r); - - if (have_r) { - /* right exists, [[Put]] regardless whether or not left exists */ - duk_put_prop_index(ctx, idx_obj, (duk_uarridx_t) l); - } else { - duk_del_prop_index(ctx, idx_obj, (duk_uarridx_t) l); - duk_pop(ctx); - } - - if (have_l) { - duk_put_prop_index(ctx, idx_obj, (duk_uarridx_t) r); - } else { - duk_del_prop_index(ctx, idx_obj, (duk_uarridx_t) r); - duk_pop(ctx); - } -} - -#if defined(DUK_USE_DDDPRINT) -/* Debug print which visualizes the qsort partitioning process. */ -DUK_LOCAL void duk__debuglog_qsort_state(duk_context *ctx, duk_int_t lo, duk_int_t hi, duk_int_t pivot) { - char buf[4096]; - char *ptr = buf; - duk_int_t i, n; - n = (duk_int_t) duk_get_length(ctx, 1); - if (n > 4000) { - n = 4000; - } - *ptr++ = '['; - for (i = 0; i < n; i++) { - if (i == pivot) { - *ptr++ = '|'; - } else if (i == lo) { - *ptr++ = '<'; - } else if (i == hi) { - *ptr++ = '>'; - } else if (i >= lo && i <= hi) { - *ptr++ = '-'; - } else { - *ptr++ = ' '; - } - } - *ptr++ = ']'; - *ptr++ = '\0'; - - DUK_DDD(DUK_DDDPRINT("%s (lo=%ld, hi=%ld, pivot=%ld)", - (const char *) buf, (long) lo, (long) hi, (long) pivot)); -} -#endif - -DUK_LOCAL void duk__array_qsort(duk_context *ctx, duk_int_t lo, duk_int_t hi) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_int_t p, l, r; - - /* The lo/hi indices may be crossed and hi < 0 is possible at entry. */ - - DUK_DDD(DUK_DDDPRINT("duk__array_qsort: lo=%ld, hi=%ld, obj=%!T", - (long) lo, (long) hi, (duk_tval *) duk_get_tval(ctx, 1))); - - DUK_ASSERT_TOP(ctx, 3); - - /* In some cases it may be that lo > hi, or hi < 0; these - * degenerate cases happen e.g. for empty arrays, and in - * recursion leaves. - */ - - /* trivial cases */ - if (hi - lo < 1) { - DUK_DDD(DUK_DDDPRINT("degenerate case, return immediately")); - return; - } - DUK_ASSERT(hi > lo); - DUK_ASSERT(hi - lo + 1 >= 2); - - /* randomized pivot selection */ - p = lo + (duk_util_tinyrandom_get_bits(thr, 30) % (hi - lo + 1)); /* rnd in [lo,hi] */ - DUK_ASSERT(p >= lo && p <= hi); - DUK_DDD(DUK_DDDPRINT("lo=%ld, hi=%ld, chose pivot p=%ld", - (long) lo, (long) hi, (long) p)); - - /* move pivot out of the way */ - duk__array_sort_swap(ctx, p, lo); - p = lo; - DUK_DDD(DUK_DDDPRINT("pivot moved out of the way: %!T", (duk_tval *) duk_get_tval(ctx, 1))); - - l = lo + 1; - r = hi; - for (;;) { - /* find elements to swap */ - for (;;) { - DUK_DDD(DUK_DDDPRINT("left scan: l=%ld, r=%ld, p=%ld", - (long) l, (long) r, (long) p)); - if (l >= hi) { - break; - } - if (duk__array_sort_compare(ctx, l, p) >= 0) { /* !(l < p) */ - break; - } - l++; - } - for (;;) { - DUK_DDD(DUK_DDDPRINT("right scan: l=%ld, r=%ld, p=%ld", - (long) l, (long) r, (long) p)); - if (r <= lo) { - break; - } - if (duk__array_sort_compare(ctx, p, r) >= 0) { /* !(p < r) */ - break; - } - r--; - } - if (l >= r) { - goto done; - } - DUK_ASSERT(l < r); - - DUK_DDD(DUK_DDDPRINT("swap %ld and %ld", (long) l, (long) r)); - - duk__array_sort_swap(ctx, l, r); - - DUK_DDD(DUK_DDDPRINT("after swap: %!T", (duk_tval *) duk_get_tval(ctx, 1))); - l++; - r--; - } - done: - /* Note that 'l' and 'r' may cross, i.e. r < l */ - DUK_ASSERT(l >= lo && l <= hi); - DUK_ASSERT(r >= lo && r <= hi); - - /* XXX: there's no explicit recursion bound here now. For the average - * qsort recursion depth O(log n) that's not really necessary: e.g. for - * 2**32 recursion depth would be about 32 which is OK. However, qsort - * worst case recursion depth is O(n) which may be a problem. - */ - - /* move pivot to its final place */ - DUK_DDD(DUK_DDDPRINT("before final pivot swap: %!T", (duk_tval *) duk_get_tval(ctx, 1))); - duk__array_sort_swap(ctx, lo, r); - -#if defined(DUK_USE_DDDPRINT) - duk__debuglog_qsort_state(ctx, lo, hi, r); -#endif - - DUK_DDD(DUK_DDDPRINT("recurse: pivot=%ld, obj=%!T", (long) r, (duk_tval *) duk_get_tval(ctx, 1))); - duk__array_qsort(ctx, lo, r - 1); - duk__array_qsort(ctx, r + 1, hi); -} - -DUK_INTERNAL duk_ret_t duk_bi_array_prototype_sort(duk_context *ctx) { - duk_uint32_t len; - - /* XXX: len >= 0x80000000 won't work below because a signed type - * is needed by qsort. - */ - len = duk__push_this_obj_len_u32_limited(ctx); - - /* stack[0] = compareFn - * stack[1] = ToObject(this) - * stack[2] = ToUint32(length) - */ - - if (len > 0) { - /* avoid degenerate cases, so that (len - 1) won't underflow */ - duk__array_qsort(ctx, (duk_int_t) 0, (duk_int_t) (len - 1)); - } - - DUK_ASSERT_TOP(ctx, 3); - duk_pop(ctx); - return 1; /* return ToObject(this) */ -} - -/* - * splice() - */ - -/* XXX: this compiles to over 500 bytes now, even without special handling - * for an array part. Uses signed ints so does not handle full array range correctly. - */ - -/* XXX: can shift() / unshift() use the same helper? - * shift() is (close to?) <--> splice(0, 1) - * unshift is (close to?) <--> splice(0, 0, [items])? - */ - -DUK_INTERNAL duk_ret_t duk_bi_array_prototype_splice(duk_context *ctx) { - duk_idx_t nargs; - duk_uint32_t len; - duk_bool_t have_delcount; - duk_int_t item_count; - duk_int_t act_start; - duk_int_t del_count; - duk_int_t i, n; - - DUK_UNREF(have_delcount); - - nargs = duk_get_top(ctx); - if (nargs < 2) { - duk_set_top(ctx, 2); - nargs = 2; - have_delcount = 0; - } else { - have_delcount = 1; - } - - /* XXX: len >= 0x80000000 won't work below because we need to be - * able to represent -len. - */ - len = duk__push_this_obj_len_u32_limited(ctx); - - act_start = duk_to_int_clamped(ctx, 0, -((duk_int_t) len), (duk_int_t) len); - if (act_start < 0) { - act_start = len + act_start; - } - DUK_ASSERT(act_start >= 0 && act_start <= (duk_int_t) len); - -#ifdef DUK_USE_NONSTD_ARRAY_SPLICE_DELCOUNT - if (have_delcount) { -#endif - del_count = duk_to_int_clamped(ctx, 1, 0, len - act_start); -#ifdef DUK_USE_NONSTD_ARRAY_SPLICE_DELCOUNT - } else { - /* E5.1 standard behavior when deleteCount is not given would be - * to treat it just like if 'undefined' was given, which coerces - * ultimately to 0. Real world behavior is to splice to the end - * of array, see test-bi-array-proto-splice-no-delcount.js. - */ - del_count = len - act_start; - } -#endif - - DUK_ASSERT(nargs >= 2); - item_count = (duk_int_t) (nargs - 2); - - DUK_ASSERT(del_count >= 0 && del_count <= (duk_int_t) len - act_start); - DUK_ASSERT(del_count + act_start <= (duk_int_t) len); - - /* For now, restrict result array into 32-bit length range. */ - if (((duk_double_t) len) - ((duk_double_t) del_count) + ((duk_double_t) item_count) > (duk_double_t) DUK_UINT32_MAX) { - DUK_D(DUK_DPRINT("Array.prototype.splice() would go beyond 32-bit length, throw")); - return DUK_RET_RANGE_ERROR; - } - - duk_push_array(ctx); - - /* stack[0] = start - * stack[1] = deleteCount - * stack[2...nargs-1] = items - * stack[nargs] = ToObject(this) -3 - * stack[nargs+1] = ToUint32(length) -2 - * stack[nargs+2] = result array -1 - */ - - DUK_ASSERT_TOP(ctx, nargs + 3); - - /* Step 9: copy elements-to-be-deleted into the result array */ - - for (i = 0; i < del_count; i++) { - if (duk_get_prop_index(ctx, -3, (duk_uarridx_t) (act_start + i))) { - duk_xdef_prop_index_wec(ctx, -2, i); /* throw flag irrelevant (false in std alg) */ - } else { - duk_pop(ctx); - } - } - duk_push_u32(ctx, (duk_uint32_t) del_count); - duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_LENGTH, DUK_PROPDESC_FLAGS_W); - - /* Steps 12 and 13: reorganize elements to make room for itemCount elements */ - - if (item_count < del_count) { - /* [ A B C D E F G H ] rel_index = 2, del_count 3, item count 1 - * -> [ A B F G H ] (conceptual intermediate step) - * -> [ A B . F G H ] (placeholder marked) - * [ A B C F G H ] (actual result at this point, C will be replaced) - */ - - DUK_ASSERT_TOP(ctx, nargs + 3); - - n = len - del_count; - for (i = act_start; i < n; i++) { - if (duk_get_prop_index(ctx, -3, (duk_uarridx_t) (i + del_count))) { - duk_put_prop_index(ctx, -4, (duk_uarridx_t) (i + item_count)); - } else { - duk_pop(ctx); - duk_del_prop_index(ctx, -3, (duk_uarridx_t) (i + item_count)); - } - } - - DUK_ASSERT_TOP(ctx, nargs + 3); - - /* loop iterator init and limit changed from standard algorithm */ - n = len - del_count + item_count; - for (i = len - 1; i >= n; i--) { - duk_del_prop_index(ctx, -3, (duk_uarridx_t) i); - } - - DUK_ASSERT_TOP(ctx, nargs + 3); - } else if (item_count > del_count) { - /* [ A B C D E F G H ] rel_index = 2, del_count 3, item count 4 - * -> [ A B F G H ] (conceptual intermediate step) - * -> [ A B . . . . F G H ] (placeholder marked) - * [ A B C D E F F G H ] (actual result at this point) - */ - - DUK_ASSERT_TOP(ctx, nargs + 3); - - /* loop iterator init and limit changed from standard algorithm */ - for (i = len - del_count - 1; i >= act_start; i--) { - if (duk_get_prop_index(ctx, -3, (duk_uarridx_t) (i + del_count))) { - duk_put_prop_index(ctx, -4, (duk_uarridx_t) (i + item_count)); - } else { - duk_pop(ctx); - duk_del_prop_index(ctx, -3, (duk_uarridx_t) (i + item_count)); - } - } - - DUK_ASSERT_TOP(ctx, nargs + 3); - } else { - /* [ A B C D E F G H ] rel_index = 2, del_count 3, item count 3 - * -> [ A B F G H ] (conceptual intermediate step) - * -> [ A B . . . F G H ] (placeholder marked) - * [ A B C D E F G H ] (actual result at this point) - */ - } - DUK_ASSERT_TOP(ctx, nargs + 3); - - /* Step 15: insert itemCount elements into the hole made above */ - - for (i = 0; i < item_count; i++) { - duk_dup(ctx, i + 2); /* args start at index 2 */ - duk_put_prop_index(ctx, -4, (duk_uarridx_t) (act_start + i)); - } - - /* Step 16: update length; note that the final length may be above 32 bit range - * (but we checked above that this isn't the case here) - */ - - duk_push_u32(ctx, len - del_count + item_count); - duk_put_prop_stridx(ctx, -4, DUK_STRIDX_LENGTH); - - /* result array is already at the top of stack */ - DUK_ASSERT_TOP(ctx, nargs + 3); - return 1; -} - -/* - * reverse() - */ - -DUK_INTERNAL duk_ret_t duk_bi_array_prototype_reverse(duk_context *ctx) { - duk_uint32_t len; - duk_uint32_t middle; - duk_uint32_t lower, upper; - duk_bool_t have_lower, have_upper; - - len = duk__push_this_obj_len_u32(ctx); - middle = len / 2; - - /* If len <= 1, middle will be 0 and for-loop bails out - * immediately (0 < 0 -> false). - */ - - for (lower = 0; lower < middle; lower++) { - DUK_ASSERT(len >= 2); - DUK_ASSERT_TOP(ctx, 2); - - DUK_ASSERT(len >= lower + 1); - upper = len - lower - 1; - - have_lower = duk_get_prop_index(ctx, -2, (duk_uarridx_t) lower); - have_upper = duk_get_prop_index(ctx, -3, (duk_uarridx_t) upper); - - /* [ ToObject(this) ToUint32(length) lowerValue upperValue ] */ - - if (have_upper) { - duk_put_prop_index(ctx, -4, (duk_uarridx_t) lower); - } else { - duk_del_prop_index(ctx, -4, (duk_uarridx_t) lower); - duk_pop(ctx); - } - - if (have_lower) { - duk_put_prop_index(ctx, -3, (duk_uarridx_t) upper); - } else { - duk_del_prop_index(ctx, -3, (duk_uarridx_t) upper); - duk_pop(ctx); - } - - DUK_ASSERT_TOP(ctx, 2); - } - - DUK_ASSERT_TOP(ctx, 2); - duk_pop(ctx); /* -> [ ToObject(this) ] */ - return 1; -} - -/* - * slice() - */ - -DUK_INTERNAL duk_ret_t duk_bi_array_prototype_slice(duk_context *ctx) { - duk_uint32_t len; - duk_int_t start, end; - duk_int_t i; - duk_uarridx_t idx; - duk_uint32_t res_length = 0; - - /* XXX: len >= 0x80000000 won't work below because we need to be - * able to represent -len. - */ - len = duk__push_this_obj_len_u32_limited(ctx); - duk_push_array(ctx); - - /* stack[0] = start - * stack[1] = end - * stack[2] = ToObject(this) - * stack[3] = ToUint32(length) - * stack[4] = result array - */ - - start = duk_to_int_clamped(ctx, 0, -((duk_int_t) len), (duk_int_t) len); - if (start < 0) { - start = len + start; - } - /* XXX: could duk_is_undefined() provide defaulting undefined to 'len' - * (the upper limit)? - */ - if (duk_is_undefined(ctx, 1)) { - end = len; - } else { - end = duk_to_int_clamped(ctx, 1, -((duk_int_t) len), (duk_int_t) len); - if (end < 0) { - end = len + end; - } - } - DUK_ASSERT(start >= 0 && (duk_uint32_t) start <= len); - DUK_ASSERT(end >= 0 && (duk_uint32_t) end <= len); - - idx = 0; - for (i = start; i < end; i++) { - DUK_ASSERT_TOP(ctx, 5); - if (duk_get_prop_index(ctx, 2, (duk_uarridx_t) i)) { - duk_xdef_prop_index_wec(ctx, 4, idx); - res_length = idx + 1; - } else { - duk_pop(ctx); - } - idx++; - DUK_ASSERT_TOP(ctx, 5); - } - - duk_push_u32(ctx, res_length); - duk_xdef_prop_stridx(ctx, 4, DUK_STRIDX_LENGTH, DUK_PROPDESC_FLAGS_W); - - DUK_ASSERT_TOP(ctx, 5); - return 1; -} - -/* - * shift() - */ - -DUK_INTERNAL duk_ret_t duk_bi_array_prototype_shift(duk_context *ctx) { - duk_uint32_t len; - duk_uint32_t i; - - len = duk__push_this_obj_len_u32(ctx); - if (len == 0) { - duk_push_int(ctx, 0); - duk_put_prop_stridx(ctx, 0, DUK_STRIDX_LENGTH); - return 0; - } - - duk_get_prop_index(ctx, 0, 0); - - /* stack[0] = object (this) - * stack[1] = ToUint32(length) - * stack[2] = elem at index 0 (retval) - */ - - for (i = 1; i < len; i++) { - DUK_ASSERT_TOP(ctx, 3); - if (duk_get_prop_index(ctx, 0, (duk_uarridx_t) i)) { - /* fromPresent = true */ - duk_put_prop_index(ctx, 0, (duk_uarridx_t) (i - 1)); - } else { - /* fromPresent = false */ - duk_del_prop_index(ctx, 0, (duk_uarridx_t) (i - 1)); - duk_pop(ctx); - } - } - duk_del_prop_index(ctx, 0, (duk_uarridx_t) (len - 1)); - - duk_push_u32(ctx, (duk_uint32_t) (len - 1)); - duk_put_prop_stridx(ctx, 0, DUK_STRIDX_LENGTH); - - DUK_ASSERT_TOP(ctx, 3); - return 1; -} - -/* - * unshift() - */ - -DUK_INTERNAL duk_ret_t duk_bi_array_prototype_unshift(duk_context *ctx) { - duk_idx_t nargs; - duk_uint32_t len; - duk_uint32_t i; - - nargs = duk_get_top(ctx); - len = duk__push_this_obj_len_u32(ctx); - - /* stack[0...nargs-1] = unshift args (vararg) - * stack[nargs] = ToObject(this) - * stack[nargs+1] = ToUint32(length) - */ - - DUK_ASSERT_TOP(ctx, nargs + 2); - - /* Note: unshift() may operate on indices above unsigned 32-bit range - * and the final length may be >= 2**32. However, we restrict the - * final result to 32-bit range for practicality. - */ - - if (len + (duk_uint32_t) nargs < len) { - DUK_D(DUK_DPRINT("Array.prototype.unshift() would go beyond 32-bit length, throw")); - return DUK_RET_RANGE_ERROR; - } - - i = len; - while (i > 0) { - DUK_ASSERT_TOP(ctx, nargs + 2); - i--; - /* k+argCount-1; note that may be above 32-bit range */ - - if (duk_get_prop_index(ctx, -2, (duk_uarridx_t) i)) { - /* fromPresent = true */ - /* [ ... ToObject(this) ToUint32(length) val ] */ - duk_put_prop_index(ctx, -3, (duk_uarridx_t) (i + nargs)); /* -> [ ... ToObject(this) ToUint32(length) ] */ - } else { - /* fromPresent = false */ - /* [ ... ToObject(this) ToUint32(length) val ] */ - duk_pop(ctx); - duk_del_prop_index(ctx, -2, (duk_uarridx_t) (i + nargs)); /* -> [ ... ToObject(this) ToUint32(length) ] */ - } - DUK_ASSERT_TOP(ctx, nargs + 2); - } - - for (i = 0; i < (duk_uint32_t) nargs; i++) { - DUK_ASSERT_TOP(ctx, nargs + 2); - duk_dup(ctx, i); /* -> [ ... ToObject(this) ToUint32(length) arg[i] ] */ - duk_put_prop_index(ctx, -3, (duk_uarridx_t) i); - DUK_ASSERT_TOP(ctx, nargs + 2); - } - - DUK_ASSERT_TOP(ctx, nargs + 2); - duk_push_u32(ctx, len + nargs); - duk_dup_top(ctx); /* -> [ ... ToObject(this) ToUint32(length) final_len final_len ] */ - duk_put_prop_stridx(ctx, -4, DUK_STRIDX_LENGTH); - return 1; -} - -/* - * indexOf(), lastIndexOf() - */ - -DUK_INTERNAL duk_ret_t duk_bi_array_prototype_indexof_shared(duk_context *ctx) { - duk_idx_t nargs; - duk_int_t i, len; - duk_int_t from_index; - duk_small_int_t idx_step = duk_get_current_magic(ctx); /* idx_step is +1 for indexOf, -1 for lastIndexOf */ - - /* lastIndexOf() needs to be a vararg function because we must distinguish - * between an undefined fromIndex and a "not given" fromIndex; indexOf() is - * made vararg for symmetry although it doesn't strictly need to be. - */ - - nargs = duk_get_top(ctx); - duk_set_top(ctx, 2); - - /* XXX: must be able to represent -len */ - len = (duk_int_t) duk__push_this_obj_len_u32_limited(ctx); - if (len == 0) { - goto not_found; - } - - /* Index clamping is a bit tricky, we must ensure that we'll only iterate - * through elements that exist and that the specific requirements from E5.1 - * Sections 15.4.4.14 and 15.4.4.15 are fulfilled; especially: - * - * - indexOf: clamp to [-len,len], negative handling -> [0,len], - * if clamped result is len, for-loop bails out immediately - * - * - lastIndexOf: clamp to [-len-1, len-1], negative handling -> [-1, len-1], - * if clamped result is -1, for-loop bails out immediately - * - * If fromIndex is not given, ToInteger(undefined) = 0, which is correct - * for indexOf() but incorrect for lastIndexOf(). Hence special handling, - * and why lastIndexOf() needs to be a vararg function. - */ - - if (nargs >= 2) { - /* indexOf: clamp fromIndex to [-len, len] - * (if fromIndex == len, for-loop terminates directly) - * - * lastIndexOf: clamp fromIndex to [-len - 1, len - 1] - * (if clamped to -len-1 -> fromIndex becomes -1, terminates for-loop directly) - */ - from_index = duk_to_int_clamped(ctx, - 1, - (idx_step > 0 ? -len : -len - 1), - (idx_step > 0 ? len : len - 1)); - if (from_index < 0) { - /* for lastIndexOf, result may be -1 (mark immediate termination) */ - from_index = len + from_index; - } - } else { - /* for indexOf, ToInteger(undefined) would be 0, i.e. correct, but - * handle both indexOf and lastIndexOf specially here. - */ - if (idx_step > 0) { - from_index = 0; - } else { - from_index = len - 1; - } - } - - /* stack[0] = searchElement - * stack[1] = fromIndex - * stack[2] = object - * stack[3] = length (not needed, but not popped above) - */ - - for (i = from_index; i >= 0 && i < len; i += idx_step) { - DUK_ASSERT_TOP(ctx, 4); - - if (duk_get_prop_index(ctx, 2, (duk_uarridx_t) i)) { - DUK_ASSERT_TOP(ctx, 5); - if (duk_strict_equals(ctx, 0, 4)) { - duk_push_int(ctx, i); - return 1; - } - } - - duk_pop(ctx); - } - - not_found: - duk_push_int(ctx, -1); - return 1; -} - -/* - * every(), some(), forEach(), map(), filter() - */ - -#define DUK__ITER_EVERY 0 -#define DUK__ITER_SOME 1 -#define DUK__ITER_FOREACH 2 -#define DUK__ITER_MAP 3 -#define DUK__ITER_FILTER 4 - -/* XXX: This helper is a bit awkward because the handling for the different iteration - * callers is quite different. This now compiles to a bit less than 500 bytes, so with - * 5 callers the net result is about 100 bytes / caller. - */ - -DUK_INTERNAL duk_ret_t duk_bi_array_prototype_iter_shared(duk_context *ctx) { - duk_uint32_t len; - duk_uint32_t i; - duk_uarridx_t k; - duk_bool_t bval; - duk_small_int_t iter_type = duk_get_current_magic(ctx); - duk_uint32_t res_length = 0; - - /* each call this helper serves has nargs==2 */ - DUK_ASSERT_TOP(ctx, 2); - - len = duk__push_this_obj_len_u32(ctx); - duk_require_callable(ctx, 0); - /* if thisArg not supplied, behave as if undefined was supplied */ - - if (iter_type == DUK__ITER_MAP || iter_type == DUK__ITER_FILTER) { - duk_push_array(ctx); - } else { - duk_push_undefined(ctx); - } - - /* stack[0] = callback - * stack[1] = thisArg - * stack[2] = object - * stack[3] = ToUint32(length) (unused, but avoid unnecessary pop) - * stack[4] = result array (or undefined) - */ - - k = 0; /* result index for filter() */ - for (i = 0; i < len; i++) { - DUK_ASSERT_TOP(ctx, 5); - - if (!duk_get_prop_index(ctx, 2, (duk_uarridx_t) i)) { -#if defined(DUK_USE_NONSTD_ARRAY_MAP_TRAILER) - /* Real world behavior for map(): trailing non-existent - * elements don't invoke the user callback, but are still - * counted towards result 'length'. - */ - if (iter_type == DUK__ITER_MAP) { - res_length = i + 1; - } -#else - /* Standard behavior for map(): trailing non-existent - * elements don't invoke the user callback and are not - * counted towards result 'length'. - */ -#endif - duk_pop(ctx); - continue; - } - - /* The original value needs to be preserved for filter(), hence - * this funny order. We can't re-get the value because of side - * effects. - */ - - duk_dup(ctx, 0); - duk_dup(ctx, 1); - duk_dup(ctx, -3); - duk_push_u32(ctx, i); - duk_dup(ctx, 2); /* [ ... val callback thisArg val i obj ] */ - duk_call_method(ctx, 3); /* -> [ ... val retval ] */ - - switch (iter_type) { - case DUK__ITER_EVERY: - bval = duk_to_boolean(ctx, -1); - if (!bval) { - /* stack top contains 'false' */ - return 1; - } - break; - case DUK__ITER_SOME: - bval = duk_to_boolean(ctx, -1); - if (bval) { - /* stack top contains 'true' */ - return 1; - } - break; - case DUK__ITER_FOREACH: - /* nop */ - break; - case DUK__ITER_MAP: - duk_dup(ctx, -1); - duk_xdef_prop_index_wec(ctx, 4, (duk_uarridx_t) i); /* retval to result[i] */ - res_length = i + 1; - break; - case DUK__ITER_FILTER: - bval = duk_to_boolean(ctx, -1); - if (bval) { - duk_dup(ctx, -2); /* orig value */ - duk_xdef_prop_index_wec(ctx, 4, (duk_uarridx_t) k); - k++; - res_length = k; - } - break; - default: - DUK_UNREACHABLE(); - break; - } - duk_pop_2(ctx); - - DUK_ASSERT_TOP(ctx, 5); - } - - switch (iter_type) { - case DUK__ITER_EVERY: - duk_push_true(ctx); - break; - case DUK__ITER_SOME: - duk_push_false(ctx); - break; - case DUK__ITER_FOREACH: - duk_push_undefined(ctx); - break; - case DUK__ITER_MAP: - case DUK__ITER_FILTER: - DUK_ASSERT_TOP(ctx, 5); - DUK_ASSERT(duk_is_array(ctx, -1)); /* topmost element is the result array already */ - duk_push_u32(ctx, res_length); - duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_LENGTH, DUK_PROPDESC_FLAGS_W); - break; - default: - DUK_UNREACHABLE(); - break; - } - - return 1; -} - -/* - * reduce(), reduceRight() - */ - -DUK_INTERNAL duk_ret_t duk_bi_array_prototype_reduce_shared(duk_context *ctx) { - duk_idx_t nargs; - duk_bool_t have_acc; - duk_uint32_t i, len; - duk_small_int_t idx_step = duk_get_current_magic(ctx); /* idx_step is +1 for reduce, -1 for reduceRight */ - - /* We're a varargs function because we need to detect whether - * initialValue was given or not. - */ - nargs = duk_get_top(ctx); - DUK_DDD(DUK_DDDPRINT("nargs=%ld", (long) nargs)); - - duk_set_top(ctx, 2); - len = duk__push_this_obj_len_u32(ctx); - if (!duk_is_callable(ctx, 0)) { - goto type_error; - } - - /* stack[0] = callback fn - * stack[1] = initialValue - * stack[2] = object (coerced this) - * stack[3] = length (not needed, but not popped above) - * stack[4] = accumulator - */ - - have_acc = 0; - if (nargs >= 2) { - duk_dup(ctx, 1); - have_acc = 1; - } - DUK_DDD(DUK_DDDPRINT("have_acc=%ld, acc=%!T", - (long) have_acc, (duk_tval *) duk_get_tval(ctx, 3))); - - /* For len == 0, i is initialized to len - 1 which underflows. - * The condition (i < len) will then exit the for-loop on the - * first round which is correct. Similarly, loop termination - * happens by i underflowing. - */ - - for (i = (idx_step >= 0 ? 0 : len - 1); - i < len; /* i >= 0 would always be true */ - i += idx_step) { - DUK_DDD(DUK_DDDPRINT("i=%ld, len=%ld, have_acc=%ld, top=%ld, acc=%!T", - (long) i, (long) len, (long) have_acc, - (long) duk_get_top(ctx), - (duk_tval *) duk_get_tval(ctx, 4))); - - DUK_ASSERT((have_acc && duk_get_top(ctx) == 5) || - (!have_acc && duk_get_top(ctx) == 4)); - - if (!duk_has_prop_index(ctx, 2, (duk_uarridx_t) i)) { - continue; - } - - if (!have_acc) { - DUK_ASSERT_TOP(ctx, 4); - duk_get_prop_index(ctx, 2, (duk_uarridx_t) i); - have_acc = 1; - DUK_ASSERT_TOP(ctx, 5); - } else { - DUK_ASSERT_TOP(ctx, 5); - duk_dup(ctx, 0); - duk_dup(ctx, 4); - duk_get_prop_index(ctx, 2, (duk_uarridx_t) i); - duk_push_u32(ctx, i); - duk_dup(ctx, 2); - DUK_DDD(DUK_DDDPRINT("calling reduce function: func=%!T, prev=%!T, curr=%!T, idx=%!T, obj=%!T", - (duk_tval *) duk_get_tval(ctx, -5), (duk_tval *) duk_get_tval(ctx, -4), - (duk_tval *) duk_get_tval(ctx, -3), (duk_tval *) duk_get_tval(ctx, -2), - (duk_tval *) duk_get_tval(ctx, -1))); - duk_call(ctx, 4); - DUK_DDD(DUK_DDDPRINT("-> result: %!T", (duk_tval *) duk_get_tval(ctx, -1))); - duk_replace(ctx, 4); - DUK_ASSERT_TOP(ctx, 5); - } - } - - if (!have_acc) { - goto type_error; - } - - DUK_ASSERT_TOP(ctx, 5); - return 1; - - type_error: - return DUK_RET_TYPE_ERROR; -} - -#undef DUK__ARRAY_MID_JOIN_LIMIT - -#undef DUK__ITER_EVERY -#undef DUK__ITER_SOME -#undef DUK__ITER_FOREACH -#undef DUK__ITER_MAP -#undef DUK__ITER_FILTER -#line 1 "duk_bi_boolean.c" -/* - * Boolean built-ins - */ - -/* include removed: duk_internal.h */ - -/* Shared helper to provide toString() and valueOf(). Checks 'this', gets - * the primitive value to stack top, and optionally coerces with ToString(). - */ -DUK_INTERNAL duk_ret_t duk_bi_boolean_prototype_tostring_shared(duk_context *ctx) { - duk_tval *tv; - duk_hobject *h; - duk_small_int_t coerce_tostring = duk_get_current_magic(ctx); - - /* XXX: there is room to use a shared helper here, many built-ins - * check the 'this' type, and if it's an object, check its class, - * then get its internal value, etc. - */ - - duk_push_this(ctx); - tv = duk_get_tval(ctx, -1); - DUK_ASSERT(tv != NULL); - - if (DUK_TVAL_IS_BOOLEAN(tv)) { - goto type_ok; - } else if (DUK_TVAL_IS_OBJECT(tv)) { - h = DUK_TVAL_GET_OBJECT(tv); - DUK_ASSERT(h != NULL); - - if (DUK_HOBJECT_GET_CLASS_NUMBER(h) == DUK_HOBJECT_CLASS_BOOLEAN) { - duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_VALUE); - DUK_ASSERT(duk_is_boolean(ctx, -1)); - goto type_ok; - } - } - - return DUK_RET_TYPE_ERROR; - - type_ok: - if (coerce_tostring) { - duk_to_string(ctx, -1); - } - return 1; -} - -DUK_INTERNAL duk_ret_t duk_bi_boolean_constructor(duk_context *ctx) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_hobject *h_this; - - DUK_UNREF(thr); - - duk_to_boolean(ctx, 0); - - if (duk_is_constructor_call(ctx)) { - /* XXX: helper; rely on Boolean.prototype as being non-writable, non-configurable */ - duk_push_this(ctx); - h_this = duk_get_hobject(ctx, -1); - DUK_ASSERT(h_this != NULL); - DUK_ASSERT(DUK_HOBJECT_GET_PROTOTYPE(thr->heap, h_this) == thr->builtins[DUK_BIDX_BOOLEAN_PROTOTYPE]); - - DUK_HOBJECT_SET_CLASS_NUMBER(h_this, DUK_HOBJECT_CLASS_BOOLEAN); - - duk_dup(ctx, 0); /* -> [ val obj val ] */ - duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_INT_VALUE, DUK_PROPDESC_FLAGS_NONE); /* XXX: proper flags? */ - } /* unbalanced stack */ - - return 1; -} -#line 1 "duk_bi_buffer.c" -/* - * Duktape.Buffer, Node.js Buffer, and Khronos/ES6 TypedArray built-ins - */ - -/* include removed: duk_internal.h */ - -/* - * Misc helpers - */ - -#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) -/* Map DUK_HBUFFEROBJECT_ELEM_xxx to duk_hobject class number. - * Sync with duk_hbufferobject.h and duk_hobject.h. - */ -static const duk_uint8_t duk__buffer_class_from_elemtype[9] = { - DUK_HOBJECT_CLASS_UINT8ARRAY, - DUK_HOBJECT_CLASS_UINT8CLAMPEDARRAY, - DUK_HOBJECT_CLASS_INT8ARRAY, - DUK_HOBJECT_CLASS_UINT16ARRAY, - DUK_HOBJECT_CLASS_INT16ARRAY, - DUK_HOBJECT_CLASS_UINT32ARRAY, - DUK_HOBJECT_CLASS_INT32ARRAY, - DUK_HOBJECT_CLASS_FLOAT32ARRAY, - DUK_HOBJECT_CLASS_FLOAT64ARRAY -}; -#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ - -#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) -/* Map DUK_HBUFFEROBJECT_ELEM_xxx to prototype object built-in index. - * Sync with duk_hbufferobject.h. - */ -static const duk_uint8_t duk__buffer_proto_from_elemtype[9] = { - DUK_BIDX_UINT8ARRAY_PROTOTYPE, - DUK_BIDX_UINT8CLAMPEDARRAY_PROTOTYPE, - DUK_BIDX_INT8ARRAY_PROTOTYPE, - DUK_BIDX_UINT16ARRAY_PROTOTYPE, - DUK_BIDX_INT16ARRAY_PROTOTYPE, - DUK_BIDX_UINT32ARRAY_PROTOTYPE, - DUK_BIDX_INT32ARRAY_PROTOTYPE, - DUK_BIDX_FLOAT32ARRAY_PROTOTYPE, - DUK_BIDX_FLOAT64ARRAY_PROTOTYPE -}; -#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ - -#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) -/* Map DUK__FLX_xxx to byte size. - */ -static const duk_uint8_t duk__buffer_nbytes_from_fldtype[6] = { - 1, /* DUK__FLD_8BIT */ - 2, /* DUK__FLD_16BIT */ - 4, /* DUK__FLD_32BIT */ - 4, /* DUK__FLD_FLOAT */ - 8, /* DUK__FLD_DOUBLE */ - 0 /* DUK__FLD_VARINT; not relevant here */ -}; -#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ - -#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) -/* Bitfield for each DUK_HBUFFEROBJECT_ELEM_xxx indicating which element types - * are compatible with a blind byte copy for the TypedArray set() method (also - * used for TypedArray constructor). Array index is target buffer elem type, - * bitfield indicates compatible source types. The types must have same byte - * size and they must be coercion compatible. - */ -static duk_uint16_t duk__buffer_elemtype_copy_compatible[9] = { - /* xxx -> DUK_HBUFFEROBJECT_ELEM_UINT8 */ - (1U << DUK_HBUFFEROBJECT_ELEM_UINT8) | - (1U << DUK_HBUFFEROBJECT_ELEM_UINT8CLAMPED) | - (1U << DUK_HBUFFEROBJECT_ELEM_INT8), - - /* xxx -> DUK_HBUFFEROBJECT_ELEM_UINT8CLAMPED - * Note: INT8 is -not- copy compatible, e.g. -1 would coerce to 0x00. - */ - (1U << DUK_HBUFFEROBJECT_ELEM_UINT8) | - (1U << DUK_HBUFFEROBJECT_ELEM_UINT8CLAMPED), - - /* xxx -> DUK_HBUFFEROBJECT_ELEM_INT8 */ - (1U << DUK_HBUFFEROBJECT_ELEM_UINT8) | - (1U << DUK_HBUFFEROBJECT_ELEM_UINT8CLAMPED) | - (1U << DUK_HBUFFEROBJECT_ELEM_INT8), - - /* xxx -> DUK_HBUFFEROBJECT_ELEM_UINT16 */ - (1U << DUK_HBUFFEROBJECT_ELEM_UINT16) | - (1U << DUK_HBUFFEROBJECT_ELEM_INT16), - - /* xxx -> DUK_HBUFFEROBJECT_ELEM_INT16 */ - (1U << DUK_HBUFFEROBJECT_ELEM_UINT16) | - (1U << DUK_HBUFFEROBJECT_ELEM_INT16), - - /* xxx -> DUK_HBUFFEROBJECT_ELEM_UINT32 */ - (1U << DUK_HBUFFEROBJECT_ELEM_UINT32) | - (1U << DUK_HBUFFEROBJECT_ELEM_INT32), - - /* xxx -> DUK_HBUFFEROBJECT_ELEM_INT32 */ - (1U << DUK_HBUFFEROBJECT_ELEM_UINT32) | - (1U << DUK_HBUFFEROBJECT_ELEM_INT32), - - /* xxx -> DUK_HBUFFEROBJECT_ELEM_FLOAT32 */ - (1U << DUK_HBUFFEROBJECT_ELEM_FLOAT32), - - /* xxx -> DUK_HBUFFEROBJECT_ELEM_FLOAT64 */ - (1U << DUK_HBUFFEROBJECT_ELEM_FLOAT64) -}; -#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ - -#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) -/* Shared helper. */ -DUK_LOCAL duk_hbufferobject *duk__getrequire_bufobj_this(duk_context *ctx, duk_bool_t throw_flag) { - duk_hthread *thr; - duk_tval *tv; - duk_hbufferobject *h_this; - - DUK_ASSERT(ctx != NULL); - thr = (duk_hthread *) ctx; - - tv = duk_get_borrowed_this_tval(ctx); - DUK_ASSERT(tv != NULL); - if (DUK_TVAL_IS_OBJECT(tv)) { - h_this = (duk_hbufferobject *) DUK_TVAL_GET_OBJECT(tv); - DUK_ASSERT(h_this != NULL); - if (DUK_HOBJECT_IS_BUFFEROBJECT((duk_hobject *) h_this)) { - DUK_ASSERT_HBUFFEROBJECT_VALID(h_this); - return h_this; - } - } - - if (throw_flag) { - DUK_ERROR_TYPE(thr, DUK_STR_NOT_BUFFER); - } - return NULL; -} -#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ - -#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) -/* Check that 'this' is a duk_hbufferobject and return a pointer to it. */ -DUK_LOCAL duk_hbufferobject *duk__get_bufobj_this(duk_context *ctx) { - return duk__getrequire_bufobj_this(ctx, 0); -} -#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ - -#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) -/* Check that 'this' is a duk_hbufferobject and return a pointer to it - * (NULL if not). - */ -DUK_LOCAL duk_hbufferobject *duk__require_bufobj_this(duk_context *ctx) { - return duk__getrequire_bufobj_this(ctx, 1); -} -#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ - -#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) -/* Check that value is a duk_hbufferobject and return a pointer to it. */ -DUK_LOCAL duk_hbufferobject *duk__require_bufobj_value(duk_context *ctx, duk_idx_t index) { - duk_hthread *thr; - duk_tval *tv; - duk_hbufferobject *h_obj; - - thr = (duk_hthread *) ctx; - - /* Don't accept relative indices now. */ - DUK_ASSERT(index >= 0); - - tv = duk_require_tval(ctx, index); - DUK_ASSERT(tv != NULL); - if (DUK_TVAL_IS_OBJECT(tv)) { - h_obj = (duk_hbufferobject *) DUK_TVAL_GET_OBJECT(tv); - DUK_ASSERT(h_obj != NULL); - if (DUK_HOBJECT_IS_BUFFEROBJECT((duk_hobject *) h_obj)) { - DUK_ASSERT_HBUFFEROBJECT_VALID(h_obj); - return h_obj; - } - } - - DUK_ERROR_TYPE(thr, DUK_STR_NOT_BUFFER); - return NULL; /* not reachable */ -} -#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ - -DUK_LOCAL void duk__set_bufobj_buffer(duk_context *ctx, duk_hbufferobject *h_bufobj, duk_hbuffer *h_val) { - duk_hthread *thr; - - thr = (duk_hthread *) ctx; - DUK_UNREF(thr); - - DUK_ASSERT(ctx != NULL); - DUK_ASSERT(h_bufobj != NULL); - DUK_ASSERT(h_bufobj->buf == NULL); /* no need to decref */ - DUK_ASSERT(h_val != NULL); - DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj); - - h_bufobj->buf = h_val; - DUK_HBUFFER_INCREF(thr, h_val); - h_bufobj->length = (duk_uint_t) DUK_HBUFFER_GET_SIZE(h_val); - DUK_ASSERT(h_bufobj->shift == 0); - DUK_ASSERT(h_bufobj->elem_type == DUK_HBUFFEROBJECT_ELEM_UINT8); - - DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj); -} - -#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) -DUK_LOCAL duk_hbufferobject *duk__push_arraybuffer_with_length(duk_context *ctx, duk_uint_t len) { - duk_hbuffer *h_val; - duk_hbufferobject *h_bufobj; - - (void) duk_push_fixed_buffer(ctx, (duk_size_t) len); - h_val = (duk_hbuffer *) duk_get_hbuffer(ctx, -1); - DUK_ASSERT(h_val != NULL); - - h_bufobj = duk_push_bufferobject_raw(ctx, - DUK_HOBJECT_FLAG_EXTENSIBLE | - DUK_HOBJECT_FLAG_BUFFEROBJECT | - DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_ARRAYBUFFER), - DUK_BIDX_ARRAYBUFFER_PROTOTYPE); - DUK_ASSERT(h_bufobj != NULL); - - duk__set_bufobj_buffer(ctx, h_bufobj, h_val); - DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj); - - return h_bufobj; -} -#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ - -#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) -/* Shared offset/length coercion helper. */ -DUK_LOCAL void duk__resolve_offset_opt_length(duk_context *ctx, - duk_hbufferobject *h_bufarg, - duk_idx_t idx_offset, - duk_idx_t idx_length, - duk_uint_t *out_offset, - duk_uint_t *out_length, - duk_bool_t throw_flag) { - duk_hthread *thr; - duk_int_t offset_signed; - duk_int_t length_signed; - duk_uint_t offset; - duk_uint_t length; - - thr = (duk_hthread *) ctx; - DUK_UNREF(thr); - - offset_signed = duk_to_int(ctx, idx_offset); - if (offset_signed < 0) { - goto fail_range; - } - offset = (duk_uint_t) offset_signed; - if (offset > h_bufarg->length) { - goto fail_range; - } - DUK_ASSERT_DISABLE(offset >= 0); /* unsigned */ - DUK_ASSERT(offset <= h_bufarg->length); - - if (duk_is_undefined(ctx, idx_length)) { - DUK_ASSERT(h_bufarg->length >= offset); - length = h_bufarg->length - offset; /* >= 0 */ - } else { - length_signed = duk_to_int(ctx, idx_length); - if (length_signed < 0) { - goto fail_range; - } - length = (duk_uint_t) length_signed; - DUK_ASSERT(h_bufarg->length >= offset); - if (length > h_bufarg->length - offset) { - /* Unlike for negative arguments, some call sites - * want length to be clamped if it's positive. - */ - if (throw_flag) { - goto fail_range; - } else { - length = h_bufarg->length - offset; - } - } - } - DUK_ASSERT_DISABLE(length >= 0); /* unsigned */ - DUK_ASSERT(offset + length <= h_bufarg->length); - - *out_offset = offset; - *out_length = length; - return; - - fail_range: - DUK_ERROR_RANGE(thr, DUK_STR_INVALID_CALL_ARGS); -} -#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ - -#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) -/* Shared lenient buffer length clamping helper. No negative indices, no - * element/byte shifting. - */ -DUK_LOCAL void duk__clamp_startend_nonegidx_noshift(duk_context *ctx, - duk_hbufferobject *h_bufobj, - duk_idx_t idx_start, - duk_idx_t idx_end, - duk_int_t *out_start_offset, - duk_int_t *out_end_offset) { - duk_int_t buffer_length; - duk_int_t start_offset; - duk_int_t end_offset; - - DUK_ASSERT(out_start_offset != NULL); - DUK_ASSERT(out_end_offset != NULL); - - buffer_length = (duk_int_t) h_bufobj->length; - - /* undefined coerces to zero which is correct */ - start_offset = duk_to_int_clamped(ctx, idx_start, 0, buffer_length); - if (duk_is_undefined(ctx, idx_end)) { - end_offset = buffer_length; - } else { - end_offset = duk_to_int_clamped(ctx, idx_end, start_offset, buffer_length); - } - - DUK_ASSERT(start_offset >= 0); - DUK_ASSERT(start_offset <= buffer_length); - DUK_ASSERT(end_offset >= 0); - DUK_ASSERT(end_offset <= buffer_length); - DUK_ASSERT(start_offset <= end_offset); - - *out_start_offset = start_offset; - *out_end_offset = end_offset; -} -#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ - -#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) -/* Shared lenient buffer length clamping helper. Indices are treated as - * element indices (though output values are byte offsets) which only - * really matters for TypedArray views as other buffer object have a zero - * shift. Negative indices are counted from end of input slice; crossed - * indices are clamped to zero length; and final indices are clamped - * against input slice. Used for e.g. ArrayBuffer slice(). - */ -DUK_LOCAL void duk__clamp_startend_negidx_shifted(duk_context *ctx, - duk_hbufferobject *h_bufobj, - duk_idx_t idx_start, - duk_idx_t idx_end, - duk_int_t *out_start_offset, - duk_int_t *out_end_offset) { - duk_int_t buffer_length; - duk_int_t start_offset; - duk_int_t end_offset; - - DUK_ASSERT(out_start_offset != NULL); - DUK_ASSERT(out_end_offset != NULL); - - buffer_length = (duk_int_t) h_bufobj->length; - buffer_length >>= h_bufobj->shift; /* as elements */ - - /* Resolve start/end offset as element indices first; arguments - * at idx_start/idx_end are element offsets. Working with element - * indices first also avoids potential for wrapping. - */ - - start_offset = duk_to_int(ctx, idx_start); - if (start_offset < 0) { - start_offset = buffer_length + start_offset; - } - if (duk_is_undefined(ctx, idx_end)) { - end_offset = buffer_length; - } else { - end_offset = duk_to_int(ctx, idx_end); - if (end_offset < 0) { - end_offset = buffer_length + end_offset; - } - } - /* Note: start_offset/end_offset can still be < 0 here. */ - - if (start_offset < 0) { - start_offset = 0; - } else if (start_offset > buffer_length) { - start_offset = buffer_length; - } - if (end_offset < start_offset) { - end_offset = start_offset; - } else if (end_offset > buffer_length) { - end_offset = buffer_length; - } - DUK_ASSERT(start_offset >= 0); - DUK_ASSERT(start_offset <= buffer_length); - DUK_ASSERT(end_offset >= 0); - DUK_ASSERT(end_offset <= buffer_length); - DUK_ASSERT(start_offset <= end_offset); - - /* Convert indices to byte offsets. */ - start_offset <<= h_bufobj->shift; - end_offset <<= h_bufobj->shift; - - *out_start_offset = start_offset; - *out_end_offset = end_offset; -} -#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ - -/* - * Indexed read/write helpers (also used from outside this file) - */ - -DUK_INTERNAL void duk_hbufferobject_push_validated_read(duk_context *ctx, duk_hbufferobject *h_bufobj, duk_uint8_t *p, duk_small_uint_t elem_size) { - duk_double_union du; - - DUK_MEMCPY((void *) du.uc, (const void *) p, (size_t) elem_size); - - switch (h_bufobj->elem_type) { - case DUK_HBUFFEROBJECT_ELEM_UINT8: -#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) - case DUK_HBUFFEROBJECT_ELEM_UINT8CLAMPED: -#endif - duk_push_uint(ctx, (duk_uint_t) du.uc[0]); - break; -#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) - /* These are not needed when only Duktape.Buffer is supported. */ - case DUK_HBUFFEROBJECT_ELEM_INT8: - duk_push_int(ctx, (duk_int_t) (duk_int8_t) du.uc[0]); - break; - case DUK_HBUFFEROBJECT_ELEM_UINT16: - duk_push_uint(ctx, (duk_uint_t) du.us[0]); - break; - case DUK_HBUFFEROBJECT_ELEM_INT16: - duk_push_int(ctx, (duk_int_t) (duk_int16_t) du.us[0]); - break; - case DUK_HBUFFEROBJECT_ELEM_UINT32: - duk_push_uint(ctx, (duk_uint_t) du.ui[0]); - break; - case DUK_HBUFFEROBJECT_ELEM_INT32: - duk_push_int(ctx, (duk_int_t) (duk_int32_t) du.ui[0]); - break; - case DUK_HBUFFEROBJECT_ELEM_FLOAT32: - duk_push_number(ctx, (duk_double_t) du.f[0]); - break; - case DUK_HBUFFEROBJECT_ELEM_FLOAT64: - duk_push_number(ctx, (duk_double_t) du.d); - break; -#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ - default: - DUK_UNREACHABLE(); - } -} - -DUK_INTERNAL void duk_hbufferobject_validated_write(duk_context *ctx, duk_hbufferobject *h_bufobj, duk_uint8_t *p, duk_small_uint_t elem_size) { - duk_double_union du; - - /* NOTE! Caller must ensure that any side effects from the - * coercions below are safe. If that cannot be guaranteed - * (which is normally the case), caller must coerce the - * argument using duk_to_number() before any pointer - * validations; the result of duk_to_number() always coerces - * without side effects here. - */ - - switch (h_bufobj->elem_type) { - case DUK_HBUFFEROBJECT_ELEM_UINT8: - du.uc[0] = (duk_uint8_t) duk_to_uint32(ctx, -1); - break; -#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) - /* These are not needed when only Duktape.Buffer is supported. */ - case DUK_HBUFFEROBJECT_ELEM_UINT8CLAMPED: - du.uc[0] = (duk_uint8_t) duk_to_uint8clamped(ctx, -1); - break; - case DUK_HBUFFEROBJECT_ELEM_INT8: - du.uc[0] = (duk_uint8_t) duk_to_int32(ctx, -1); - break; - case DUK_HBUFFEROBJECT_ELEM_UINT16: - du.us[0] = (duk_uint16_t) duk_to_uint32(ctx, -1); - break; - case DUK_HBUFFEROBJECT_ELEM_INT16: - du.us[0] = (duk_uint16_t) duk_to_int32(ctx, -1); - break; - case DUK_HBUFFEROBJECT_ELEM_UINT32: - du.ui[0] = (duk_uint32_t) duk_to_uint32(ctx, -1); - break; - case DUK_HBUFFEROBJECT_ELEM_INT32: - du.ui[0] = (duk_uint32_t) duk_to_int32(ctx, -1); - break; - case DUK_HBUFFEROBJECT_ELEM_FLOAT32: - du.f[0] = (duk_float_t) duk_to_number(ctx, -1); - break; - case DUK_HBUFFEROBJECT_ELEM_FLOAT64: - du.d = (duk_double_t) duk_to_number(ctx, -1); - break; -#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ - default: - DUK_UNREACHABLE(); - } - - DUK_MEMCPY((void *) p, (const void *) du.uc, (size_t) elem_size); -} - -/* - * Duktape.Buffer: constructor - */ - -DUK_INTERNAL duk_ret_t duk_bi_buffer_constructor(duk_context *ctx) { - duk_hthread *thr; - duk_size_t buf_size; - duk_small_int_t buf_dynamic; - duk_uint8_t *buf_data; - const duk_uint8_t *src_data; - - thr = (duk_hthread *) ctx; - DUK_UNREF(thr); - - /* - * Constructor arguments are currently somewhat compatible with - * (keep it that way if possible): - * - * http://nodejs.org/api/buffer.html - * - * Note that the ToBuffer() coercion (duk_to_buffer()) does NOT match - * the constructor behavior. - */ - - buf_dynamic = duk_get_boolean(ctx, 1); /* default to false */ - - switch (duk_get_type(ctx, 0)) { - case DUK_TYPE_NUMBER: { - /* new buffer of specified size */ - buf_size = (duk_size_t) duk_to_int(ctx, 0); - (void) duk_push_buffer(ctx, buf_size, buf_dynamic); - break; - } - case DUK_TYPE_BUFFER: { - /* return input buffer, converted to a Duktape.Buffer object - * if called as a constructor (no change if called as a - * function). - */ - duk_set_top(ctx, 1); - break; - } - case DUK_TYPE_STRING: { - /* new buffer with string contents */ - src_data = (const duk_uint8_t *) duk_get_lstring(ctx, 0, &buf_size); - DUK_ASSERT(src_data != NULL); /* even for zero-length string */ - buf_data = (duk_uint8_t *) duk_push_buffer(ctx, buf_size, buf_dynamic); - DUK_MEMCPY((void *) buf_data, (const void *) src_data, (size_t) buf_size); - break; - } - case DUK_TYPE_OBJECT: { - /* For all duk_hbufferobjects, get the plain buffer inside - * without making a copy. This is compatible with Duktape 1.2 - * but means that a slice/view information is ignored and the - * full underlying buffer is returned. - * - * If called as a constructor, a new Duktape.Buffer object - * pointing to the same plain buffer is created below. - */ - duk_hbufferobject *h_bufobj; - h_bufobj = (duk_hbufferobject *) duk_get_hobject(ctx, 0); - DUK_ASSERT(h_bufobj != NULL); - if (!DUK_HOBJECT_IS_BUFFEROBJECT((duk_hobject *) h_bufobj)) { - return DUK_RET_TYPE_ERROR; - } - if (h_bufobj->buf == NULL) { - return DUK_RET_TYPE_ERROR; - } - duk_push_hbuffer(ctx, h_bufobj->buf); - break; - } - case DUK_TYPE_NONE: - default: { - return DUK_RET_TYPE_ERROR; - } - } - DUK_ASSERT(duk_is_buffer(ctx, -1)); - - /* stack is unbalanced, but: [ buf ] */ - - if (duk_is_constructor_call(ctx)) { - duk_hbufferobject *h_bufobj; - duk_hbuffer *h_val; - - h_val = duk_get_hbuffer(ctx, -1); - DUK_ASSERT(h_val != NULL); - - h_bufobj = duk_push_bufferobject_raw(ctx, - DUK_HOBJECT_FLAG_EXTENSIBLE | - DUK_HOBJECT_FLAG_BUFFEROBJECT | - DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_BUFFER), - DUK_BIDX_BUFFER_PROTOTYPE); - DUK_ASSERT(h_bufobj != NULL); - - duk__set_bufobj_buffer(ctx, h_bufobj, h_val); - - DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj); - } - /* Note: unbalanced stack on purpose */ - - return 1; -} - -/* - * Node.js Buffer: constructor - */ - -#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) -DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_constructor(duk_context *ctx) { - /* Internal class is Object: Object.prototype.toString.call(new Buffer(0)) - * prints "[object Object]". - */ - duk_int_t len; - duk_int_t i; - duk_hbuffer *h_buf; - duk_hbufferobject *h_bufobj; - duk_size_t buf_size; - - switch (duk_get_type(ctx, 0)) { - case DUK_TYPE_BUFFER: { - /* Custom behavior: plain buffer is used as internal buffer - * without making a copy (matches Duktape.Buffer). - */ - duk_set_top(ctx, 1); /* -> [ buffer ] */ - break; - } - case DUK_TYPE_NUMBER: { - len = duk_to_int_clamped(ctx, 0, 0, DUK_INT_MAX); - (void) duk_push_fixed_buffer(ctx, (duk_size_t) len); - break; - } - case DUK_TYPE_OBJECT: { - duk_uint8_t *buf; - - (void) duk_get_prop_string(ctx, 0, "length"); - len = duk_to_int_clamped(ctx, -1, 0, DUK_INT_MAX); - duk_pop(ctx); - buf = (duk_uint8_t *) duk_push_fixed_buffer(ctx, (duk_size_t) len); - for (i = 0; i < len; i++) { - /* XXX: fast path for array arguments? */ - duk_get_prop_index(ctx, 0, (duk_uarridx_t) i); - buf[i] = (duk_uint8_t) (duk_to_uint32(ctx, -1) & 0xffU); - duk_pop(ctx); - } - break; - } - case DUK_TYPE_STRING: { - /* ignore encoding for now */ - duk_dup(ctx, 0); - (void) duk_to_buffer(ctx, -1, &buf_size); - break; - } - default: - return DUK_RET_TYPE_ERROR; - } - - DUK_ASSERT(duk_is_buffer(ctx, -1)); - h_buf = duk_get_hbuffer(ctx, -1); - DUK_ASSERT(h_buf != NULL); - - h_bufobj = duk_push_bufferobject_raw(ctx, - DUK_HOBJECT_FLAG_EXTENSIBLE | - DUK_HOBJECT_FLAG_BUFFEROBJECT | - DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_BUFFER), - DUK_BIDX_NODEJS_BUFFER_PROTOTYPE); - DUK_ASSERT(h_bufobj != NULL); - - h_bufobj->buf = h_buf; - DUK_HBUFFER_INCREF(thr, h_buf); - DUK_ASSERT(h_bufobj->offset == 0); - h_bufobj->length = (duk_int_t) DUK_HBUFFER_GET_SIZE(h_buf); - DUK_ASSERT(h_bufobj->elem_type == DUK_HBUFFEROBJECT_ELEM_UINT8); - - DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj); - - return 1; -} -#else /* DUK_USE_BUFFEROBJECT_SUPPORT */ -DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_constructor(duk_context *ctx) { - DUK_UNREF(ctx); - return DUK_RET_UNSUPPORTED_ERROR; -} -#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ - -/* - * ArrayBuffer, DataView, and TypedArray constructors - */ - -#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) -DUK_INTERNAL duk_ret_t duk_bi_arraybuffer_constructor(duk_context *ctx) { - duk_hthread *thr; - duk_hbufferobject *h_bufobj; - duk_hbuffer *h_val; - - DUK_ASSERT_CTX_VALID(ctx); - thr = (duk_hthread *) ctx; - DUK_UNREF(thr); - - /* XXX: function flag to make this automatic? */ - if (!duk_is_constructor_call(ctx)) { - return DUK_RET_TYPE_ERROR; - } - - if (duk_is_buffer(ctx, 0)) { - /* Custom behavior: plain buffer is used as internal buffer - * without making a copy (matches Duktape.Buffer). - */ - - h_val = duk_get_hbuffer(ctx, 0); - DUK_ASSERT(h_val != NULL); - - /* XXX: accept any duk_hbufferobject type as an input also? */ - } else { - duk_int_t len; - len = duk_to_int(ctx, 0); - if (len < 0) { - goto fail_length; - } - (void) duk_push_fixed_buffer(ctx, (duk_size_t) len); - h_val = (duk_hbuffer *) duk_get_hbuffer(ctx, -1); - DUK_ASSERT(h_val != NULL); - -#if !defined(DUK_USE_ZERO_BUFFER_DATA) - /* Khronos/ES6 requires zeroing even when DUK_USE_ZERO_BUFFER_DATA - * is not set. - */ - DUK_ASSERT(!DUK_HBUFFER_HAS_DYNAMIC((duk_hbuffer *) h_val)); - DUK_MEMZERO((void *) DUK_HBUFFER_FIXED_GET_DATA_PTR(thr->heap, h_val), (duk_size_t) len); -#endif - } - - h_bufobj = duk_push_bufferobject_raw(ctx, - DUK_HOBJECT_FLAG_EXTENSIBLE | - DUK_HOBJECT_FLAG_BUFFEROBJECT | - DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_ARRAYBUFFER), - DUK_BIDX_ARRAYBUFFER_PROTOTYPE); - DUK_ASSERT(h_bufobj != NULL); - - duk__set_bufobj_buffer(ctx, h_bufobj, h_val); - DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj); - - return 1; - - fail_length: - return DUK_RET_RANGE_ERROR; -} -#else /* DUK_USE_BUFFEROBJECT_SUPPORT */ -DUK_INTERNAL duk_ret_t duk_bi_arraybuffer_constructor(duk_context *ctx) { - DUK_UNREF(ctx); - return DUK_RET_UNSUPPORTED_ERROR; -} -#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ - - -/* Format of magic, bits: - * 0...1: elem size shift (0-3) - * 2...5: elem type (DUK_HBUFFEROBJECT_ELEM_xxx) - */ - -#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) -DUK_INTERNAL duk_ret_t duk_bi_typedarray_constructor(duk_context *ctx) { - duk_hthread *thr; - duk_tval *tv; - duk_hobject *h_obj; - duk_hbufferobject *h_bufobj = NULL; - duk_hbufferobject *h_bufarr = NULL; - duk_hbufferobject *h_bufarg = NULL; - duk_hbuffer *h_val; - duk_small_uint_t magic; - duk_small_uint_t shift; - duk_small_uint_t elem_type; - duk_small_uint_t elem_size; - duk_small_uint_t class_num; - duk_small_uint_t proto_bidx; - duk_uint_t align_mask; - duk_uint_t elem_length; - duk_int_t elem_length_signed; - duk_uint_t byte_length; - duk_small_uint_t copy_mode; - - thr = (duk_hthread *) ctx; - DUK_UNREF(thr); - - /* XXX: function flag to make this automatic? */ - if (!duk_is_constructor_call(ctx)) { - return DUK_RET_TYPE_ERROR; - } - - /* We could fit built-in index into magic but that'd make the magic - * number dependent on built-in numbering (genbuiltins.py doesn't - * handle that yet). So map both class and prototype from the - * element type. - */ - magic = duk_get_current_magic(ctx); - shift = magic & 0x03; /* bits 0...1: shift */ - elem_type = (magic >> 2) & 0x0f; /* bits 2...5: type */ - elem_size = 1 << shift; - align_mask = elem_size - 1; - DUK_ASSERT(elem_type < sizeof(duk__buffer_proto_from_elemtype) / sizeof(duk_uint8_t)); - proto_bidx = duk__buffer_proto_from_elemtype[elem_type]; - DUK_ASSERT(proto_bidx < DUK_NUM_BUILTINS); - DUK_ASSERT(elem_type < sizeof(duk__buffer_class_from_elemtype) / sizeof(duk_uint8_t)); - class_num = duk__buffer_class_from_elemtype[elem_type]; - - DUK_DD(DUK_DDPRINT("typedarray constructor, magic=%d, shift=%d, elem_type=%d, " - "elem_size=%d, proto_bidx=%d, class_num=%d", - (int) magic, (int) shift, (int) elem_type, (int) elem_size, - (int) proto_bidx, (int) class_num)); - - /* Argument variants. When the argument is an ArrayBuffer a view to - * the same buffer is created; otherwise a new ArrayBuffer is always - * created. - */ - - tv = duk_get_tval(ctx, 0); - DUK_ASSERT(tv != NULL); /* arg count */ - if (DUK_TVAL_IS_OBJECT(tv)) { - h_obj = DUK_TVAL_GET_OBJECT(tv); - DUK_ASSERT(h_obj != NULL); - - if (DUK_HOBJECT_GET_CLASS_NUMBER(h_obj) == DUK_HOBJECT_CLASS_ARRAYBUFFER) { - /* ArrayBuffer: unlike any other argument variant, create - * a view into the existing buffer. - */ - - duk_int_t byte_offset_signed; - duk_uint_t byte_offset; - - h_bufarg = (duk_hbufferobject *) h_obj; - - byte_offset_signed = duk_to_int(ctx, 1); - if (byte_offset_signed < 0) { - goto fail_arguments; - } - byte_offset = (duk_uint_t) byte_offset_signed; - if (byte_offset > h_bufarg->length || - (byte_offset & align_mask) != 0) { - /* Must be >= 0 and multiple of element size. */ - goto fail_arguments; - } - if (duk_is_undefined(ctx, 2)) { - DUK_ASSERT(h_bufarg->length >= byte_offset); - byte_length = h_bufarg->length - byte_offset; - if ((byte_length & align_mask) != 0) { - /* Must be element size multiple from - * start offset to end of buffer. - */ - goto fail_arguments; - } - elem_length = (byte_length >> shift); - } else { - elem_length_signed = duk_to_int(ctx, 2); - if (elem_length_signed < 0) { - goto fail_arguments; - } - elem_length = (duk_uint_t) elem_length_signed; - byte_length = elem_length << shift; - if ((byte_length >> shift) != elem_length) { - /* Byte length would overflow. */ - /* XXX: easier check with less code? */ - goto fail_arguments; - } - DUK_ASSERT(h_bufarg->length >= byte_offset); - if (byte_length > h_bufarg->length - byte_offset) { - /* Not enough data. */ - goto fail_arguments; - } - } - DUK_UNREF(elem_length); - DUK_ASSERT_DISABLE(byte_offset >= 0); - DUK_ASSERT(byte_offset <= h_bufarg->length); - DUK_ASSERT_DISABLE(byte_length >= 0); - DUK_ASSERT(byte_offset + byte_length <= h_bufarg->length); - DUK_ASSERT((elem_length << shift) == byte_length); - - h_bufobj = duk_push_bufferobject_raw(ctx, - DUK_HOBJECT_FLAG_EXTENSIBLE | - DUK_HOBJECT_FLAG_BUFFEROBJECT | - DUK_HOBJECT_CLASS_AS_FLAGS(class_num), - proto_bidx); - h_val = h_bufarg->buf; - if (h_val == NULL) { - return DUK_RET_TYPE_ERROR; - } - h_bufobj->buf = h_val; - DUK_HBUFFER_INCREF(thr, h_val); - h_bufobj->offset = h_bufarg->offset + byte_offset; - h_bufobj->length = byte_length; - h_bufobj->shift = (duk_uint8_t) shift; - h_bufobj->elem_type = (duk_uint8_t) elem_type; - h_bufobj->is_view = 1; - DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj); - - /* Set .buffer to the argument ArrayBuffer. */ - duk_dup(ctx, 0); - duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_LC_BUFFER, DUK_PROPDESC_FLAGS_NONE); - duk_compact(ctx, -1); - return 1; - } else if (DUK_HOBJECT_IS_BUFFEROBJECT(h_obj)) { - /* TypedArray (or other non-ArrayBuffer duk_hbufferobject). - * Conceptually same behavior as for an Array-like argument, - * with a few fast paths. - */ - - h_bufarg = (duk_hbufferobject *) h_obj; - DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufarg); - elem_length_signed = (duk_int_t) (h_bufarg->length >> h_bufarg->shift); - if (h_bufarg->buf == NULL) { - return DUK_RET_TYPE_ERROR; - } - - /* Select copy mode. Must take into account element - * compatibility and validity of the underlying source - * buffer. - */ - - DUK_DDD(DUK_DDDPRINT("selecting copy mode for bufobj arg, " - "src byte_length=%ld, src shift=%d, " - "src/dst elem_length=%ld; " - "dst shift=%d -> dst byte_length=%ld", - (long) h_bufarg->length, (int) h_bufarg->shift, - (long) elem_length_signed, (int) shift, - (long) (elem_length_signed << shift))); - - copy_mode = 2; /* default is explicit index read/write copy */ - DUK_ASSERT(elem_type < sizeof(duk__buffer_elemtype_copy_compatible) / sizeof(duk_uint16_t)); - if (DUK_HBUFFEROBJECT_VALID_SLICE(h_bufarg)) { - if ((duk__buffer_elemtype_copy_compatible[elem_type] & (1 << h_bufarg->elem_type)) != 0) { - DUK_DDD(DUK_DDDPRINT("source/target are copy compatible, memcpy")); - DUK_ASSERT(shift == h_bufarg->shift); /* byte sizes will match */ - copy_mode = 0; - } else { - DUK_DDD(DUK_DDDPRINT("source/target not copy compatible but valid, fast copy")); - copy_mode = 1; - } - } - } else { - /* Array or Array-like */ - elem_length_signed = (duk_int_t) duk_get_length(ctx, 0); - copy_mode = 2; - } - } else if (DUK_TVAL_IS_BUFFER(tv)) { - /* Accept plain buffer values like array initializers - * (new in Duktape 1.4.0). - */ - duk_hbuffer *h_srcbuf; - h_srcbuf = DUK_TVAL_GET_BUFFER(tv); - elem_length_signed = (duk_int_t) DUK_HBUFFER_GET_SIZE(h_srcbuf); - copy_mode = 2; /* XXX: could add fast path for u8 compatible views */ - } else { - /* Non-object argument is simply int coerced, matches - * V8 behavior (except for "null", which we coerce to - * 0 but V8 TypeErrors). - */ - elem_length_signed = duk_to_int(ctx, 0); - copy_mode = 3; - } - if (elem_length_signed < 0) { - goto fail_arguments; - } - elem_length = (duk_uint_t) elem_length_signed; - byte_length = (duk_uint_t) (elem_length << shift); - if ((byte_length >> shift) != elem_length) { - /* Byte length would overflow. */ - /* XXX: easier check with less code? */ - goto fail_arguments; - } - - DUK_DDD(DUK_DDDPRINT("elem_length=%ld, byte_length=%ld", - (long) elem_length, (long) byte_length)); - - /* ArrayBuffer argument is handled specially above; the rest of the - * argument variants are handled by shared code below. - */ - - /* Push a new ArrayBuffer (becomes view .buffer) */ - h_bufarr = duk__push_arraybuffer_with_length(ctx, byte_length); - DUK_ASSERT(h_bufarr != NULL); - h_val = h_bufarr->buf; - DUK_ASSERT(h_val != NULL); - - /* Push the resulting view object and attach the ArrayBuffer. */ - h_bufobj = duk_push_bufferobject_raw(ctx, - DUK_HOBJECT_FLAG_EXTENSIBLE | - DUK_HOBJECT_FLAG_BUFFEROBJECT | - DUK_HOBJECT_CLASS_AS_FLAGS(class_num), - proto_bidx); - - h_bufobj->buf = h_val; - DUK_HBUFFER_INCREF(thr, h_val); - DUK_ASSERT(h_bufobj->offset == 0); - h_bufobj->length = byte_length; - h_bufobj->shift = (duk_uint8_t) shift; - h_bufobj->elem_type = (duk_uint8_t) elem_type; - h_bufobj->is_view = 1; - DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj); - - /* Set .buffer */ - duk_dup(ctx, -2); - duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_LC_BUFFER, DUK_PROPDESC_FLAGS_NONE); - duk_compact(ctx, -1); - - /* Copy values, the copy method depends on the arguments. - * - * Copy mode decision may depend on the validity of the underlying - * buffer of the source argument; there must be no harmful side effects - * from there to here for copy_mode to still be valid. - */ - DUK_DDD(DUK_DDDPRINT("copy mode: %d", (int) copy_mode)); - switch (copy_mode) { - case 0: { - /* Use byte copy. */ - - duk_uint8_t *p_src; - duk_uint8_t *p_dst; - - DUK_ASSERT(h_bufobj != NULL); - DUK_ASSERT(h_bufobj->buf != NULL); - DUK_ASSERT(DUK_HBUFFEROBJECT_VALID_SLICE(h_bufobj)); - DUK_ASSERT(h_bufarg != NULL); - DUK_ASSERT(h_bufarg->buf != NULL); - DUK_ASSERT(DUK_HBUFFEROBJECT_VALID_SLICE(h_bufarg)); - - p_dst = DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_bufobj); - p_src = DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_bufarg); - - DUK_DDD(DUK_DDDPRINT("using memcpy: p_src=%p, p_dst=%p, byte_length=%ld", - (void *) p_src, (void *) p_dst, (long) byte_length)); - - DUK_MEMCPY((void *) p_dst, (const void *) p_src, (size_t) byte_length); - break; - } - case 1: { - /* Copy values through direct validated reads and writes. */ - - duk_small_uint_t src_elem_size; - duk_small_uint_t dst_elem_size; - duk_uint8_t *p_src; - duk_uint8_t *p_src_end; - duk_uint8_t *p_dst; - - DUK_ASSERT(h_bufobj != NULL); - DUK_ASSERT(h_bufobj->buf != NULL); - DUK_ASSERT(DUK_HBUFFEROBJECT_VALID_SLICE(h_bufobj)); - DUK_ASSERT(h_bufarg != NULL); - DUK_ASSERT(h_bufarg->buf != NULL); - DUK_ASSERT(DUK_HBUFFEROBJECT_VALID_SLICE(h_bufarg)); - - src_elem_size = 1 << h_bufarg->shift; - dst_elem_size = elem_size; - - p_src = DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_bufarg); - p_dst = DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_bufobj); - p_src_end = p_src + h_bufarg->length; - - DUK_DDD(DUK_DDDPRINT("using fast copy: p_src=%p, p_src_end=%p, p_dst=%p, " - "src_elem_size=%d, dst_elem_size=%d", - (void *) p_src, (void *) p_src_end, (void *) p_dst, - (int) src_elem_size, (int) dst_elem_size)); - - while (p_src != p_src_end) { - DUK_DDD(DUK_DDDPRINT("fast path per element copy loop: " - "p_src=%p, p_src_end=%p, p_dst=%p", - (void *) p_src, (void *) p_src_end, (void *) p_dst)); - /* A validated read() is always a number, so it's write coercion - * is always side effect free an won't invalidate pointers etc. - */ - duk_hbufferobject_push_validated_read(ctx, h_bufarg, p_src, src_elem_size); - duk_hbufferobject_validated_write(ctx, h_bufobj, p_dst, dst_elem_size); - duk_pop(ctx); - p_src += src_elem_size; - p_dst += dst_elem_size; - } - break; - } - case 2: { - /* Copy values by index reads and writes. Let virtual - * property handling take care of coercion. - */ - duk_uint_t i; - - DUK_DDD(DUK_DDDPRINT("using slow copy")); - - for (i = 0; i < elem_length; i++) { - duk_get_prop_index(ctx, 0, (duk_uarridx_t) i); - duk_put_prop_index(ctx, -2, (duk_uarridx_t) i); - } - break; - } - default: - case 3: { - /* No copy, leave zero bytes in the buffer. There's no - * ambiguity with Float32/Float64 because zero bytes also - * represent 0.0. - */ -#if !defined(DUK_USE_ZERO_BUFFER_DATA) - /* Khronos/ES6 requires zeroing even when DUK_USE_ZERO_BUFFER_DATA - * is not set. - */ - DUK_ASSERT(!DUK_HBUFFER_HAS_DYNAMIC((duk_hbuffer *) h_val)); - DUK_MEMZERO((void *) DUK_HBUFFER_FIXED_GET_DATA_PTR(thr->heap, h_val), (duk_size_t) byte_length); -#endif - - DUK_DDD(DUK_DDDPRINT("using no copy")); - break; - } - } - - return 1; - - fail_arguments: - return DUK_RET_RANGE_ERROR; -} -#else /* DUK_USE_BUFFEROBJECT_SUPPORT */ -DUK_INTERNAL duk_ret_t duk_bi_typedarray_constructor(duk_context *ctx) { - DUK_UNREF(ctx); - return DUK_RET_UNSUPPORTED_ERROR; -} -#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ - -#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) -DUK_INTERNAL duk_ret_t duk_bi_dataview_constructor(duk_context *ctx) { - duk_hbufferobject *h_bufarg; - duk_hbufferobject *h_bufobj; - duk_hbuffer *h_val; - duk_uint_t offset; - duk_uint_t length; - - /* XXX: function flag to make this automatic? */ - if (!duk_is_constructor_call(ctx)) { - return DUK_RET_TYPE_ERROR; - } - - h_bufarg = duk__require_bufobj_value(ctx, 0); - DUK_ASSERT(h_bufarg != NULL); - - duk__resolve_offset_opt_length(ctx, h_bufarg, 1, 2, &offset, &length, 1 /*throw_flag*/); - DUK_ASSERT(offset <= h_bufarg->length); - DUK_ASSERT(offset + length <= h_bufarg->length); - - h_bufobj = duk_push_bufferobject_raw(ctx, - DUK_HOBJECT_FLAG_EXTENSIBLE | - DUK_HOBJECT_FLAG_BUFFEROBJECT | - DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_DATAVIEW), - DUK_BIDX_DATAVIEW_PROTOTYPE); - - h_val = h_bufarg->buf; - if (h_val == NULL) { - return DUK_RET_TYPE_ERROR; - } - h_bufobj->buf = h_val; - DUK_HBUFFER_INCREF(thr, h_val); - h_bufobj->offset = h_bufarg->offset + offset; - h_bufobj->length = length; - DUK_ASSERT(h_bufobj->shift == 0); - DUK_ASSERT(h_bufobj->elem_type == DUK_HBUFFEROBJECT_ELEM_UINT8); - h_bufobj->is_view = 1; - - /* The DataView .buffer property is ordinarily set to the argument - * which is an ArrayBuffer. We accept any duk_hbufferobject as - * an argument and .buffer will be set to the argument regardless - * of what it is. This may be a bit confusing if the argument - * is e.g. a DataView or another TypedArray view. - * - * XXX: Copy .buffer property from a DataView/TypedArray argument? - * Create a fresh ArrayBuffer for Duktape.Buffer and Node.js Buffer - * arguments? See: test-bug-dataview-buffer-prop.js. - */ - - duk_dup(ctx, 0); - duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_LC_BUFFER, DUK_PROPDESC_FLAGS_NONE); - duk_compact(ctx, -1); - - DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj); - return 1; -} -#else /* DUK_USE_BUFFEROBJECT_SUPPORT */ -DUK_INTERNAL duk_ret_t duk_bi_dataview_constructor(duk_context *ctx) { - DUK_UNREF(ctx); - return DUK_RET_UNSUPPORTED_ERROR; -} -#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ - -/* - * ArrayBuffer.isView() - */ - -#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) -DUK_INTERNAL duk_ret_t duk_bi_arraybuffer_isview(duk_context *ctx) { - duk_hobject *h_obj; - duk_bool_t ret = 0; - - h_obj = duk_get_hobject(ctx, 0); - if (h_obj != NULL && DUK_HOBJECT_IS_BUFFEROBJECT(h_obj)) { - ret = ((duk_hbufferobject *) h_obj)->is_view; - } - duk_push_boolean(ctx, ret); - return 1; -} -#else /* DUK_USE_BUFFEROBJECT_SUPPORT */ -DUK_INTERNAL duk_ret_t duk_bi_arraybuffer_isview(duk_context *ctx) { - DUK_UNREF(ctx); - return DUK_RET_UNSUPPORTED_ERROR; -} -#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ - -/* - * Node.js Buffer: toString([encoding], [start], [end]) - */ - -#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) -DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_tostring(duk_context *ctx) { - duk_hthread *thr; - duk_hbufferobject *h_this; - duk_int_t start_offset, end_offset; - duk_uint8_t *buf_slice; - duk_size_t slice_length; - - thr = (duk_hthread *) ctx; - DUK_UNREF(thr); - - h_this = duk__get_bufobj_this(ctx); - if (h_this == NULL) { - /* XXX: happens e.g. when evaluating: String(Buffer.prototype). */ - duk_push_string(ctx, "[object Object]"); - return 1; - } - DUK_ASSERT_HBUFFEROBJECT_VALID(h_this); - - /* ignore encoding for now */ - - duk__clamp_startend_nonegidx_noshift(ctx, h_this, 1 /*idx_start*/, 2 /*idx_end*/, &start_offset, &end_offset); - - slice_length = (duk_size_t) (end_offset - start_offset); - buf_slice = (duk_uint8_t *) duk_push_fixed_buffer(ctx, slice_length); - DUK_ASSERT(buf_slice != NULL); - - if (h_this->buf == NULL) { - goto type_error; - } - - if (DUK_HBUFFEROBJECT_VALID_BYTEOFFSET_EXCL(h_this, start_offset + slice_length)) { - DUK_MEMCPY((void *) buf_slice, - (const void *) (DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_this) + start_offset), - (size_t) slice_length); - } else { - /* not covered, return all zeroes */ - ; - } - - duk_to_string(ctx, -1); - return 1; - - type_error: - return DUK_RET_TYPE_ERROR; -} -#else /* DUK_USE_BUFFEROBJECT_SUPPORT */ -DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_tostring(duk_context *ctx) { - DUK_UNREF(ctx); - return DUK_RET_UNSUPPORTED_ERROR; -} -#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ - -/* - * Duktape.Buffer: toString(), valueOf() - */ - -#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) -DUK_INTERNAL duk_ret_t duk_bi_buffer_prototype_tostring_shared(duk_context *ctx) { - duk_hthread *thr; - duk_tval *tv; - duk_small_int_t to_string = duk_get_current_magic(ctx); - - thr = (duk_hthread *) ctx; - DUK_UNREF(thr); - - tv = duk_get_borrowed_this_tval(ctx); - DUK_ASSERT(tv != NULL); - - if (DUK_TVAL_IS_BUFFER(tv)) { - duk_hbuffer *h_buf; - h_buf = DUK_TVAL_GET_BUFFER(tv); - DUK_ASSERT(h_buf != NULL); - duk_push_hbuffer(ctx, h_buf); - } else if (DUK_TVAL_IS_OBJECT(tv)) { - duk_hobject *h; - duk_hbufferobject *h_bufobj; - - /* Accept any duk_hbufferobject, though we're only normally - * called for Duktape.Buffer values. - */ - h = DUK_TVAL_GET_OBJECT(tv); - DUK_ASSERT(h != NULL); - if (!DUK_HOBJECT_IS_BUFFEROBJECT(h)) { - DUK_DD(DUK_DDPRINT("toString/valueOf() called for a non-bufferobject object")); - goto type_error; - } - h_bufobj = (duk_hbufferobject *) h; - DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj); - - if (h_bufobj->buf == NULL) { - DUK_DD(DUK_DDPRINT("toString/valueOf() called for a bufferobject with NULL buf")); - goto type_error; - } - duk_push_hbuffer(ctx, h_bufobj->buf); - } else { - goto type_error; - } - - if (to_string) { - duk_to_string(ctx, -1); - } - return 1; - - type_error: - return DUK_RET_TYPE_ERROR; -} -#else /* DUK_USE_BUFFEROBJECT_SUPPORT */ -DUK_INTERNAL duk_ret_t duk_bi_buffer_prototype_tostring_shared(duk_context *ctx) { - DUK_UNREF(ctx); - return DUK_RET_UNSUPPORTED_ERROR; -} -#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ - -/* - * Node.js Buffer.prototype: toJSON() - */ - -#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) -DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_tojson(duk_context *ctx) { - duk_hthread *thr; - duk_hbufferobject *h_this; - duk_uint8_t *buf; - duk_uint_t i; - - thr = (duk_hthread *) ctx; - DUK_UNREF(thr); - h_this = duk__require_bufobj_this(ctx); - DUK_ASSERT(h_this != NULL); - - if (h_this->buf == NULL || !DUK_HBUFFEROBJECT_VALID_SLICE(h_this)) { - /* Serialize uncovered backing buffer as a null; doesn't - * really matter as long we're memory safe. - */ - duk_push_null(ctx); - return 1; - } - - duk_push_object(ctx); - duk_push_hstring_stridx(ctx, DUK_STRIDX_UC_BUFFER); - duk_put_prop_stridx(ctx, -2, DUK_STRIDX_TYPE); - - duk_push_array(ctx); - for (i = 0; i < h_this->length; i++) { - /* XXX: regetting the pointer may be overkill - we're writing - * to a side-effect free array here. - */ - DUK_ASSERT(h_this->buf != NULL); - buf = DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_this); - duk_push_uint(ctx, (duk_uint_t) buf[i]); - duk_put_prop_index(ctx, -2, (duk_idx_t) i); - } - duk_put_prop_stridx(ctx, -2, DUK_STRIDX_DATA); - - return 1; -} -#else /* DUK_USE_BUFFEROBJECT_SUPPORT */ -DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_tojson(duk_context *ctx) { - DUK_UNREF(ctx); - return DUK_RET_UNSUPPORTED_ERROR; -} -#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ - -/* - * Node.js Buffer.prototype.equals() - * Node.js Buffer.prototype.compare() - * Node.js Buffer.compare() - */ - -#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) -DUK_INTERNAL duk_ret_t duk_bi_buffer_compare_shared(duk_context *ctx) { - duk_hthread *thr; - duk_small_uint_t magic; - duk_hbufferobject *h_bufarg1; - duk_hbufferobject *h_bufarg2; - duk_small_int_t comp_res; - - thr = (duk_hthread *) ctx; - DUK_UNREF(thr); - - magic = duk_get_current_magic(ctx); - if (magic & 0x02) { - /* Static call style. */ - h_bufarg1 = duk__require_bufobj_value(ctx, 0); - h_bufarg2 = duk__require_bufobj_value(ctx, 1); - } else { - h_bufarg1 = duk__require_bufobj_this(ctx); - h_bufarg2 = duk__require_bufobj_value(ctx, 0); - } - DUK_ASSERT(h_bufarg1 != NULL); - DUK_ASSERT(h_bufarg2 != NULL); - - /* We want to compare the slice/view areas of the arguments. - * If either slice/view is invalid (underlying buffer is shorter) - * ensure equals() is false, but otherwise the only thing that - * matters is to be memory safe. - */ - - if (DUK_HBUFFEROBJECT_VALID_SLICE(h_bufarg1) && - DUK_HBUFFEROBJECT_VALID_SLICE(h_bufarg2)) { - comp_res = duk_js_data_compare((const duk_uint8_t *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h_bufarg1->buf) + h_bufarg1->offset, - (const duk_uint8_t *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h_bufarg2->buf) + h_bufarg2->offset, - (duk_size_t) h_bufarg1->length, - (duk_size_t) h_bufarg2->length); - } else { - comp_res = -1; /* either nonzero value is ok */ - } - - if (magic & 0x01) { - /* compare: similar to string comparison but for buffer data. */ - duk_push_int(ctx, comp_res); - } else { - /* equals */ - duk_push_boolean(ctx, (comp_res == 0)); - } - - return 1; -} -#else /* DUK_USE_BUFFEROBJECT_SUPPORT */ -DUK_INTERNAL duk_ret_t duk_bi_buffer_compare_shared(duk_context *ctx) { - DUK_UNREF(ctx); - return DUK_RET_UNSUPPORTED_ERROR; -} -#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ - -/* - * Node.js Buffer.prototype.fill() - */ - -#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) -DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_fill(duk_context *ctx) { - duk_hthread *thr; - duk_hbufferobject *h_this; - const duk_uint8_t *fill_str_ptr; - duk_size_t fill_str_len; - duk_uint8_t fill_value; - duk_int_t fill_offset; - duk_int_t fill_end; - duk_size_t fill_length; - duk_uint8_t *p; - - thr = (duk_hthread *) ctx; - DUK_UNREF(thr); - - h_this = duk__require_bufobj_this(ctx); - DUK_ASSERT(h_this != NULL); - if (h_this->buf == NULL) { - return DUK_RET_TYPE_ERROR; - } - - /* [ value offset end ] */ - - if (duk_is_string(ctx, 0)) { - fill_str_ptr = (const duk_uint8_t *) duk_get_lstring(ctx, 0, &fill_str_len); - DUK_ASSERT(fill_str_ptr != NULL); - } else { - fill_value = (duk_uint8_t) duk_to_uint32(ctx, 0); - fill_str_ptr = (const duk_uint8_t *) &fill_value; - fill_str_len = 1; - } - - /* Fill offset handling is more lenient than in Node.js. */ - - duk__clamp_startend_nonegidx_noshift(ctx, h_this, 1 /*idx_start*/, 2 /*idx_end*/, &fill_offset, &fill_end); - - DUK_DDD(DUK_DDDPRINT("fill: fill_value=%02x, fill_offset=%ld, fill_end=%ld, view length=%ld", - (unsigned int) fill_value, (long) fill_offset, (long) fill_end, (long) h_this->length)); - - DUK_ASSERT(fill_end - fill_offset >= 0); - DUK_ASSERT(h_this->buf != NULL); - - p = (DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_this) + fill_offset); - fill_length = (duk_size_t) (fill_end - fill_offset); - if (fill_str_len == 1) { - /* Handle single character fills as memset() even when - * the fill data comes from a one-char argument. - */ - DUK_MEMSET((void *) p, (int) fill_str_ptr[0], (size_t) fill_length); - } else if (fill_str_len > 1) { - duk_size_t i, n, t; - - for (i = 0, n = (fill_end - fill_offset), t = 0; i < n; i++) { - p[i] = fill_str_ptr[t++]; - if (t >= fill_str_len) { - t = 0; - } - } - } else { - DUK_DDD(DUK_DDDPRINT("zero size fill pattern, ignore silently")); - } - - /* Return the Buffer to allow chaining: b.fill(0x11).fill(0x22, 3, 5).toString() */ - duk_push_this(ctx); - return 1; -} -#else /* DUK_USE_BUFFEROBJECT_SUPPORT */ -DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_fill(duk_context *ctx) { - DUK_UNREF(ctx); - return DUK_RET_UNSUPPORTED_ERROR; -} -#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ - -/* - * Node.js Buffer.prototype.write(string, [offset], [length], [encoding]) - */ - -#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) -DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_write(duk_context *ctx) { - duk_hthread *thr; - duk_hbufferobject *h_this; - duk_uint_t offset; - duk_uint_t length; - const duk_uint8_t *str_data; - duk_size_t str_len; - - thr = (duk_hthread *) ctx; - DUK_UNREF(thr); - - h_this = duk__require_bufobj_this(ctx); - DUK_ASSERT(h_this != NULL); - - /* Argument must be a string, e.g. a buffer is not allowed. */ - str_data = (const duk_uint8_t *) duk_require_lstring(ctx, 0, &str_len); - - duk__resolve_offset_opt_length(ctx, h_this, 1, 2, &offset, &length, 0 /*throw_flag*/); - DUK_ASSERT(offset <= h_this->length); - DUK_ASSERT(offset + length <= h_this->length); - - /* XXX: encoding is ignored now. */ - - if (length > str_len) { - length = (duk_uint_t) str_len; - } - - if (DUK_HBUFFEROBJECT_VALID_SLICE(h_this)) { - /* Cannot overlap. */ - DUK_MEMCPY((void *) (DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_this) + offset), - (const void *) str_data, - (size_t) length); - } else { - DUK_DDD(DUK_DDDPRINT("write() target buffer is not covered, silent ignore")); - } - - duk_push_uint(ctx, length); - return 1; -} -#else /* DUK_USE_BUFFEROBJECT_SUPPORT */ -DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_write(duk_context *ctx) { - DUK_UNREF(ctx); - return DUK_RET_UNSUPPORTED_ERROR; -} -#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ - -/* - * Node.js Buffer.prototype.copy() - */ - -#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) -DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_copy(duk_context *ctx) { - duk_hthread *thr; - duk_hbufferobject *h_this; - duk_hbufferobject *h_bufarg; - duk_int_t source_length; - duk_int_t target_length; - duk_int_t target_start, source_start, source_end; - duk_uint_t target_ustart, source_ustart, source_uend; - duk_uint_t copy_size = 0; - - /* [ targetBuffer targetStart sourceStart sourceEnd ] */ - - thr = (duk_hthread *) ctx; - DUK_UNREF(thr); - - h_this = duk__require_bufobj_this(ctx); - h_bufarg = duk__require_bufobj_value(ctx, 0); - DUK_ASSERT(h_this != NULL); - DUK_ASSERT(h_bufarg != NULL); - source_length = (duk_int_t) h_this->length; - target_length = (duk_int_t) h_bufarg->length; - - target_start = duk_to_int(ctx, 1); - source_start = duk_to_int(ctx, 2); - if (duk_is_undefined(ctx, 3)) { - source_end = source_length; - } else { - source_end = duk_to_int(ctx, 3); - } - - DUK_DDD(DUK_DDDPRINT("checking copy args: target_start=%ld, target_length=%ld, " - "source_start=%ld, source_end=%ld, source_length=%ld", - (long) target_start, (long) h_bufarg->length, - (long) source_start, (long) source_end, (long) source_length)); - - /* This behavior mostly mimics Node.js now. */ - - if (source_start < 0 || source_end < 0 || target_start < 0) { - /* Negative offsets cause a RangeError. */ - goto fail_bounds; - } - source_ustart = (duk_uint_t) source_start; - source_uend = (duk_uint_t) source_end; - target_ustart = (duk_uint_t) target_start; - if (source_ustart >= source_uend || /* crossed offsets or zero size */ - source_ustart >= (duk_uint_t) source_length || /* source out-of-bounds (but positive) */ - target_ustart >= (duk_uint_t) target_length) { /* target out-of-bounds (but positive) */ - goto silent_ignore; - } - if (source_uend >= (duk_uint_t) source_length) { - /* Source end clamped silently to available length. */ - source_uend = source_length; - } - copy_size = source_uend - source_ustart; - if (target_ustart + copy_size > (duk_uint_t) target_length) { - /* Clamp to target's end if too long. - * - * NOTE: there's no overflow possibility in the comparison; - * both target_ustart and copy_size are >= 0 and based on - * values in duk_int_t range. Adding them as duk_uint_t - * values is then guaranteed not to overflow. - */ - DUK_ASSERT(target_ustart + copy_size >= target_ustart); /* no overflow */ - DUK_ASSERT(target_ustart + copy_size >= copy_size); /* no overflow */ - copy_size = (duk_uint_t) target_length - target_ustart; - } - - DUK_DDD(DUK_DDDPRINT("making copy: target_ustart=%lu source_ustart=%lu copy_size=%lu", - (unsigned long) target_ustart, (unsigned long) source_ustart, - (unsigned long) copy_size)); - - DUK_ASSERT(copy_size >= 1); - DUK_ASSERT(source_ustart <= (duk_uint_t) source_length); - DUK_ASSERT(source_ustart + copy_size <= (duk_uint_t) source_length); - DUK_ASSERT(target_ustart <= (duk_uint_t) target_length); - DUK_ASSERT(target_ustart + copy_size <= (duk_uint_t) target_length); - - /* Ensure copy is covered by underlying buffers. */ - DUK_ASSERT(h_bufarg->buf != NULL); /* length check */ - DUK_ASSERT(h_this->buf != NULL); /* length check */ - if (DUK_HBUFFEROBJECT_VALID_BYTEOFFSET_EXCL(h_bufarg, target_ustart + copy_size) && - DUK_HBUFFEROBJECT_VALID_BYTEOFFSET_EXCL(h_this, source_ustart + copy_size)) { - /* Must use memmove() because copy area may overlap (source and target - * buffer may be the same, or from different slices. - */ - DUK_MEMMOVE((void *) (DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_bufarg) + target_ustart), - (const void *) (DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_this) + source_ustart), - (size_t) copy_size); - } else { - DUK_DDD(DUK_DDDPRINT("buffer copy not covered by underlying buffer(s), ignoring")); - } - - silent_ignore: - /* Return value is like write(), number of bytes written. - * The return value matters because of code like: - * "off += buf.copy(...)". - */ - duk_push_uint(ctx, copy_size); - return 1; - - fail_bounds: - return DUK_RET_RANGE_ERROR; -} -#else /* DUK_USE_BUFFEROBJECT_SUPPORT */ -DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_copy(duk_context *ctx) { - DUK_UNREF(ctx); - return DUK_RET_UNSUPPORTED_ERROR; -} -#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ - -/* - * TypedArray.prototype.set() - * - * TypedArray set() is pretty interesting to implement because: - * - * - The source argument may be a plain array or a typedarray. If the - * source is a TypedArray, values are decoded and re-encoded into the - * target (not as a plain byte copy). This may happen even when the - * element byte size is the same, e.g. integer values may be re-encoded - * into floats. - * - * - Source and target may refer to the same underlying buffer, so that - * the set() operation may overlap. The specification requires that this - * must work as if a copy was made before the operation. Note that this - * is NOT a simple memmove() situation because the source and target - * byte sizes may be different -- e.g. a 4-byte source (Int8Array) may - * expand to a 16-byte target (Uint32Array) so that the target overlaps - * the source both from beginning and the end (unlike in typical memmove). - * - * - Even if 'buf' pointers of the source and target differ, there's no - * guarantee that their memory areas don't overlap. This may be the - * case with external buffers. - * - * Even so, it is nice to optimize for the common case: - * - * - Source and target separate buffers or non-overlapping. - * - * - Source and target have a compatible type so that a plain byte copy - * is possible. Note that while e.g. uint8 and int8 are compatible - * (coercion one way or another doesn't change the byte representation), - * e.g. int8 and uint8clamped are NOT compatible when writing int8 - * values into uint8clamped typedarray (-1 would clamp to 0 for instance). - * - * See test-bi-typedarray-proto-set.js. - */ - -#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) -DUK_INTERNAL duk_ret_t duk_bi_typedarray_set(duk_context *ctx) { - duk_hthread *thr; - duk_hbufferobject *h_this; - duk_hobject *h_obj; - duk_uarridx_t i, n; - duk_int_t offset_signed; - duk_uint_t offset_elems; - duk_uint_t offset_bytes; - - thr = (duk_hthread *) ctx; - DUK_UNREF(thr); - - h_this = duk__require_bufobj_this(ctx); - DUK_ASSERT(h_this != NULL); - DUK_ASSERT_HBUFFEROBJECT_VALID(h_this); - - if (h_this->buf == NULL) { - DUK_DDD(DUK_DDDPRINT("source neutered, skip copy")); - return 0; - } - - h_obj = duk_require_hobject(ctx, 0); - DUK_ASSERT(h_obj != NULL); - - /* XXX: V8 throws a TypeError for negative values. Would it - * be more useful to interpret negative offsets here from the - * end of the buffer too? - */ - offset_signed = duk_to_int(ctx, 1); - if (offset_signed < 0) { - return DUK_RET_TYPE_ERROR; - } - offset_elems = (duk_uint_t) offset_signed; - offset_bytes = offset_elems << h_this->shift; - if ((offset_bytes >> h_this->shift) != offset_elems) { - /* Byte length would overflow. */ - /* XXX: easier check with less code? */ - return DUK_RET_RANGE_ERROR; - } - if (offset_bytes > h_this->length) { - /* Equality may be OK but >length not. Checking - * this explicitly avoids some overflow cases - * below. - */ - return DUK_RET_RANGE_ERROR; - } - DUK_ASSERT(offset_bytes <= h_this->length); - - /* Fast path: source is a TypedArray (or any bufferobject). */ - - if (DUK_HOBJECT_IS_BUFFEROBJECT(h_obj)) { - duk_hbufferobject *h_bufarg; - duk_uint16_t comp_mask; - duk_small_int_t no_overlap = 0; - duk_uint_t src_length; - duk_uint_t dst_length; - duk_uint_t dst_length_elems; - duk_uint8_t *p_src_base; - duk_uint8_t *p_src_end; - duk_uint8_t *p_src; - duk_uint8_t *p_dst_base; - duk_uint8_t *p_dst; - duk_small_uint_t src_elem_size; - duk_small_uint_t dst_elem_size; - - h_bufarg = (duk_hbufferobject *) h_obj; - DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufarg); - - if (h_bufarg->buf == NULL) { - DUK_DDD(DUK_DDDPRINT("target neutered, skip copy")); - return 0; - } - - /* Nominal size check. */ - src_length = h_bufarg->length; /* bytes in source */ - dst_length_elems = (src_length >> h_bufarg->shift); /* elems in source and dest */ - dst_length = dst_length_elems << h_this->shift; /* bytes in dest */ - if ((dst_length >> h_this->shift) != dst_length_elems) { - /* Byte length would overflow. */ - /* XXX: easier check with less code? */ - return DUK_RET_RANGE_ERROR; - } - DUK_DDD(DUK_DDDPRINT("nominal size check: src_length=%ld, dst_length=%ld", - (long) src_length, (long) dst_length)); - DUK_ASSERT(offset_bytes <= h_this->length); - if (dst_length > h_this->length - offset_bytes) { - /* Overflow not an issue because subtraction is used on the right - * side and guaranteed to be >= 0. - */ - DUK_DDD(DUK_DDDPRINT("copy exceeds target buffer nominal length")); - return DUK_RET_RANGE_ERROR; - } - if (!DUK_HBUFFEROBJECT_VALID_BYTEOFFSET_EXCL(h_this, offset_bytes + dst_length)) { - DUK_DDD(DUK_DDDPRINT("copy not covered by underlying target buffer, ignore")); - return 0; - } - - p_src_base = DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_bufarg); - p_dst_base = DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_this) + offset_bytes; - - /* Check actual underlying buffers for validity and that they - * cover the copy. No side effects are allowed after the check - * so that the validity status doesn't change. - */ - if (!DUK_HBUFFEROBJECT_VALID_SLICE(h_this) || - !DUK_HBUFFEROBJECT_VALID_SLICE(h_bufarg)) { - /* The condition could be more narrow and check for the - * copy area only, but there's no need for fine grained - * behavior when the underlying buffer is misconfigured. - */ - DUK_DDD(DUK_DDDPRINT("source and/or target not covered by underlying buffer, skip copy")); - return 0; - } - - /* We want to do a straight memory copy if possible: this is - * an important operation because .set() is the TypedArray - * way to copy chunks of memory. However, because set() - * conceptually works in terms of elements, not all views are - * compatible with direct byte copying. - * - * If we do manage a direct copy, the "overlap issue" handled - * below can just be solved using memmove() because the source - * and destination element sizes are necessarily equal. - */ - - DUK_ASSERT(h_this->elem_type < sizeof(duk__buffer_elemtype_copy_compatible) / sizeof(duk_uint16_t)); - comp_mask = duk__buffer_elemtype_copy_compatible[h_this->elem_type]; - if (comp_mask & (1 << h_bufarg->elem_type)) { - DUK_ASSERT(src_length == dst_length); - - DUK_DDD(DUK_DDDPRINT("fast path: able to use memmove() because views are compatible")); - DUK_MEMMOVE((void *) p_dst_base, (const void *) p_src_base, (size_t) dst_length); - return 0; - } - DUK_DDD(DUK_DDDPRINT("fast path: views are not compatible with a byte copy, copy by item")); - - /* We want to avoid making a copy to process set() but that's - * not always possible: the source and the target may overlap - * and because element sizes are different, the overlap cannot - * always be handled with a memmove() or choosing the copy - * direction in a certain way. For example, if source type is - * uint8 and target type is uint32, the target area may exceed - * the source area from both ends! - * - * Note that because external buffers may point to the same - * memory areas, we must ultimately make this check using - * pointers. - * - * NOTE: careful with side effects: any side effect may cause - * a buffer resize (or external buffer pointer/length update)! - */ - - DUK_DDD(DUK_DDDPRINT("overlap check: p_src_base=%p, src_length=%ld, " - "p_dst_base=%p, dst_length=%ld", - (void *) p_src_base, (long) src_length, - (void *) p_dst_base, (long) dst_length)); - - if (p_src_base >= p_dst_base + dst_length || /* source starts after dest ends */ - p_src_base + src_length <= p_dst_base) { /* source ends before dest starts */ - no_overlap = 1; - } - - if (!no_overlap) { - /* There's overlap: the desired end result is that - * conceptually a copy is made to avoid "trampling" - * of source data by destination writes. We make - * an actual temporary copy to handle this case. - */ - duk_uint8_t *p_src_copy; - - DUK_DDD(DUK_DDDPRINT("there is overlap, make a copy of the source")); - p_src_copy = (duk_uint8_t *) duk_push_fixed_buffer(ctx, src_length); - DUK_ASSERT(p_src_copy != NULL); - DUK_MEMCPY((void *) p_src_copy, (const void *) p_src_base, (size_t) src_length); - - p_src_base = p_src_copy; /* use p_src_base from now on */ - } - /* Value stack intentionally mixed size here. */ - - DUK_DDD(DUK_DDDPRINT("after overlap check: p_src_base=%p, src_length=%ld, " - "p_dst_base=%p, dst_length=%ld, valstack top=%ld", - (void *) p_src_base, (long) src_length, - (void *) p_dst_base, (long) dst_length, - (long) duk_get_top(ctx))); - - /* Ready to make the copy. We must proceed element by element - * and must avoid any side effects that might cause the buffer - * validity check above to become invalid. - * - * Although we work through the value stack here, only plain - * numbers are handled which should be side effect safe. - */ - - src_elem_size = 1 << h_bufarg->shift; - dst_elem_size = 1 << h_this->shift; - p_src = p_src_base; - p_dst = p_dst_base; - p_src_end = p_src_base + src_length; - - while (p_src != p_src_end) { - DUK_DDD(DUK_DDDPRINT("fast path per element copy loop: " - "p_src=%p, p_src_end=%p, p_dst=%p", - (void *) p_src, (void *) p_src_end, (void *) p_dst)); - /* A validated read() is always a number, so it's write coercion - * is always side effect free an won't invalidate pointers etc. - */ - duk_hbufferobject_push_validated_read(ctx, h_bufarg, p_src, src_elem_size); - duk_hbufferobject_validated_write(ctx, h_this, p_dst, dst_elem_size); - duk_pop(ctx); - p_src += src_elem_size; - p_dst += dst_elem_size; - } - - return 0; - } else { - /* Slow path: quite slow, but we save space by using the property code - * to write coerce target values. We don't need to worry about overlap - * here because the source is not a TypedArray. - * - * We could use the bufferobject write coercion helper but since the - * property read may have arbitrary side effects, full validity checks - * would be needed for every element anyway. - */ - - n = (duk_uarridx_t) duk_get_length(ctx, 0); - DUK_ASSERT(offset_bytes <= h_this->length); - if ((n << h_this->shift) > h_this->length - offset_bytes) { - /* Overflow not an issue because subtraction is used on the right - * side and guaranteed to be >= 0. - */ - DUK_DDD(DUK_DDDPRINT("copy exceeds target buffer nominal length")); - return DUK_RET_RANGE_ERROR; - } - - /* There's no need to check for buffer validity status for the - * target here: the property access code will do that for each - * element. Moreover, if we did check the validity here, side - * effects from reading the source argument might invalidate - * the results anyway. - */ - - DUK_ASSERT_TOP(ctx, 2); - duk_push_this(ctx); - - for (i = 0; i < n; i++) { - duk_get_prop_index(ctx, 0, i); - duk_put_prop_index(ctx, 2, offset_elems + i); - } - } - - return 0; -} -#else /* DUK_USE_BUFFEROBJECT_SUPPORT */ -DUK_INTERNAL duk_ret_t duk_bi_typedarray_set(duk_context *ctx) { - DUK_UNREF(ctx); - return DUK_RET_UNSUPPORTED_ERROR; -} -#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ - -/* - * Node.js Buffer.prototype.slice([start], [end]) - * ArrayBuffer.prototype.slice(begin, [end]) - * TypedArray.prototype.slice(begin, [end]) - * - * The API calls are almost identical; negative indices are counted from end - * of buffer, and final indices are clamped (allowing crossed indices). Main - * differences: - * - * - Copy/view behavior; Node.js .slice() and TypedArray .subarray() create - * views, ArrayBuffer .slice() creates a copy - * - * - Resulting object has a different class and prototype depending on the - * call (or 'this' argument) - * - * - TypedArray .subarray() arguments are element indices, not byte offsets - */ - -#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) -DUK_INTERNAL duk_ret_t duk_bi_buffer_slice_shared(duk_context *ctx) { - duk_hthread *thr; - duk_small_int_t magic; - duk_small_uint_t res_class_num; - duk_hobject *res_proto; - duk_hbufferobject *h_this; - duk_hbufferobject *h_bufobj; - duk_hbuffer *h_val; - duk_int_t start_offset, end_offset; - duk_uint_t slice_length; - - thr = (duk_hthread *) ctx; - DUK_UNREF(thr); - - /* [ start end ] */ - - magic = duk_get_current_magic(ctx); - h_this = duk__require_bufobj_this(ctx); - - /* Slice offsets are element (not byte) offsets, which only matters - * for TypedArray views, Node.js Buffer and ArrayBuffer have shift - * zero so byte and element offsets are the same. Negative indices - * are counted from end of slice, crossed indices are allowed (and - * result in zero length result), and final values are clamped - * against the current slice. There's intentionally no check - * against the underlying buffer here. - */ - - duk__clamp_startend_negidx_shifted(ctx, h_this, 0 /*idx_start*/, 1 /*idx_end*/, &start_offset, &end_offset); - DUK_ASSERT(end_offset >= start_offset); - slice_length = (duk_uint_t) (end_offset - start_offset); - - /* The resulting buffer object gets the same class and prototype as - * the buffer in 'this', e.g. if the input is a Node.js Buffer the - * result is a Node.js Buffer; if the input is a Float32Array, the - * result is a Float32Array. - * - * For the class number this seems correct. The internal prototype - * is not so clear: if 'this' is a bufferobject with a non-standard - * prototype object, that value gets copied over into the result - * (instead of using the standard prototype for that object type). - */ - - res_class_num = DUK_HOBJECT_GET_CLASS_NUMBER((duk_hobject *) h_this); - h_bufobj = duk_push_bufferobject_raw(ctx, - DUK_HOBJECT_FLAG_EXTENSIBLE | - DUK_HOBJECT_FLAG_BUFFEROBJECT | - DUK_HOBJECT_CLASS_AS_FLAGS(res_class_num), - DUK_BIDX_OBJECT_PROTOTYPE); /* replaced */ - DUK_ASSERT(h_bufobj != NULL); - res_proto = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, (duk_hobject *) h_this); /* may be NULL */ - DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, (duk_hobject *) h_bufobj, res_proto); - - h_bufobj->length = slice_length; - h_bufobj->shift = h_this->shift; /* inherit */ - h_bufobj->elem_type = h_this->elem_type; /* inherit */ - h_bufobj->is_view = magic & 0x01; - DUK_ASSERT(h_bufobj->is_view == 0 || h_bufobj->is_view == 1); - - h_val = h_this->buf; - if (h_val == NULL) { - return DUK_RET_TYPE_ERROR; - } - - if (magic & 0x02) { - /* non-zero: make copy */ - duk_uint8_t *p_copy; - duk_size_t copy_length; - - p_copy = (duk_uint8_t *) duk_push_fixed_buffer(ctx, (duk_size_t) slice_length); - DUK_ASSERT(p_copy != NULL); - - /* Copy slice, respecting underlying buffer limits; remainder - * is left as zero. - */ - copy_length = DUK_HBUFFEROBJECT_CLAMP_BYTELENGTH(h_this, slice_length); - DUK_MEMCPY((void *) p_copy, - (const void *) (DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_this) + start_offset), - copy_length); - - h_val = duk_get_hbuffer(ctx, -1); - DUK_ASSERT(h_val != NULL); - - h_bufobj->buf = h_val; - DUK_HBUFFER_INCREF(thr, h_val); - DUK_ASSERT(h_bufobj->offset == 0); - - duk_pop(ctx); /* reachable so pop OK */ - } else { - h_bufobj->buf = h_val; - DUK_HBUFFER_INCREF(thr, h_val); - h_bufobj->offset = (duk_uint_t) (h_this->offset + start_offset); - - /* Copy the .buffer property, needed for TypedArray.prototype.subarray(). - * - * XXX: limit copy only for TypedArray classes specifically? - */ - - duk_push_this(ctx); - if (duk_get_prop_stridx(ctx, -1, DUK_STRIDX_LC_BUFFER)) { - duk_xdef_prop_stridx(ctx, -3, DUK_STRIDX_LC_BUFFER, DUK_PROPDESC_FLAGS_NONE); - duk_pop(ctx); - } else { - duk_pop_2(ctx); - } - } - /* unbalanced stack on purpose */ - - DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj); - return 1; -} -#else /* DUK_USE_BUFFEROBJECT_SUPPORT */ -DUK_INTERNAL duk_ret_t duk_bi_buffer_slice_shared(duk_context *ctx) { - DUK_UNREF(ctx); - return DUK_RET_UNSUPPORTED_ERROR; -} -#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ - -/* - * Node.js Buffer.isEncoding() - */ - -#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) -DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_is_encoding(duk_context *ctx) { - const char *encoding; - - /* only accept lowercase 'utf8' now. */ - - encoding = duk_to_string(ctx, 0); - DUK_ASSERT(duk_is_string(ctx, 0)); /* guaranteed by duk_to_string() */ - duk_push_boolean(ctx, DUK_STRCMP(encoding, "utf8") == 0); - return 1; -} -#else /* DUK_USE_BUFFEROBJECT_SUPPORT */ -DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_is_encoding(duk_context *ctx) { - DUK_UNREF(ctx); - return DUK_RET_UNSUPPORTED_ERROR; -} -#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ - -/* - * Node.js Buffer.isBuffer() - */ - -#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) -DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_is_buffer(duk_context *ctx) { - duk_hthread *thr; - duk_tval *tv; - duk_hobject *h; - duk_hobject *h_proto; - duk_bool_t ret = 0; - - thr = (duk_hthread *) ctx; - - DUK_ASSERT(duk_get_top(ctx) >= 1); /* nargs */ - tv = duk_get_tval(ctx, 0); - DUK_ASSERT(tv != NULL); - - if (DUK_TVAL_IS_OBJECT(tv)) { - h = DUK_TVAL_GET_OBJECT(tv); - DUK_ASSERT(h != NULL); - - h_proto = thr->builtins[DUK_BIDX_NODEJS_BUFFER_PROTOTYPE]; - DUK_ASSERT(h_proto != NULL); - - h = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, h); - if (h) { - ret = duk_hobject_prototype_chain_contains(thr, h, h_proto, 0 /*ignore_loop*/); - } - } - - duk_push_boolean(ctx, ret); - return 1; -} -#else /* DUK_USE_BUFFEROBJECT_SUPPORT */ -DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_is_buffer(duk_context *ctx) { - DUK_UNREF(ctx); - return DUK_RET_UNSUPPORTED_ERROR; -} -#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ - -/* - * Node.js Buffer.byteLength() - */ - -#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) -DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_byte_length(duk_context *ctx) { - const char *str; - duk_size_t len; - - /* At the moment Buffer() will just use the string bytes as - * is (ignoring encoding), so we return the string length here - * unconditionally. - */ - - str = duk_to_lstring(ctx, 0, &len); - DUK_UNREF(str); - duk_push_size_t(ctx, len); - return 1; -} -#else /* DUK_USE_BUFFEROBJECT_SUPPORT */ -DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_byte_length(duk_context *ctx) { - DUK_UNREF(ctx); - return DUK_RET_UNSUPPORTED_ERROR; -} -#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ - -/* - * Node.js Buffer.concat() - */ - -#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) -DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_concat(duk_context *ctx) { - duk_hthread *thr; - duk_hobject *h_arg; - duk_int_t total_length = 0; - duk_hbufferobject *h_bufobj; - duk_hbufferobject *h_bufres; - duk_hbuffer *h_val; - duk_uint_t i, n; - duk_uint8_t *p; - duk_size_t space_left; - duk_size_t copy_size; - - thr = (duk_hthread *) ctx; - DUK_UNREF(thr); - - /* Node.js accepts only actual Arrays. */ - h_arg = duk_require_hobject(ctx, 0); - if (DUK_HOBJECT_GET_CLASS_NUMBER(h_arg) != DUK_HOBJECT_CLASS_ARRAY) { - return DUK_RET_TYPE_ERROR; - } - - /* Compute result length and validate argument buffers. */ - n = (duk_uint_t) duk_get_length(ctx, 0); - for (i = 0; i < n; i++) { - /* Neutered checks not necessary here: neutered buffers have - * zero 'length' so we'll effectively skip them. - */ - DUK_ASSERT_TOP(ctx, 2); /* [ array totalLength ] */ - duk_get_prop_index(ctx, 0, (duk_uarridx_t) i); /* -> [ array totalLength buf ] */ - h_bufobj = duk__require_bufobj_value(ctx, 2); - DUK_ASSERT(h_bufobj != NULL); - total_length += h_bufobj->length; - duk_pop(ctx); - } - if (n == 1) { - /* For the case n==1 Node.js doesn't seem to type check - * the sole member but we do it before returning it. - * For this case only the original buffer object is - * returned (not a copy). - */ - duk_get_prop_index(ctx, 0, 0); - return 1; - } - - /* User totalLength overrides a computed length, but we'll check - * every copy in the copy loop. Note that duk_to_uint() can - * technically have arbitrary side effects so we need to recheck - * the buffers in the copy loop. - */ - if (!duk_is_undefined(ctx, 1) && n > 0) { - /* For n == 0, Node.js ignores totalLength argument and - * returns a zero length buffer. - */ - total_length = duk_to_int(ctx, 1); - } - if (total_length < 0) { - return DUK_RET_RANGE_ERROR; - } - - h_bufres = duk_push_bufferobject_raw(ctx, - DUK_HOBJECT_FLAG_EXTENSIBLE | - DUK_HOBJECT_FLAG_BUFFEROBJECT | - DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_BUFFER), - DUK_BIDX_NODEJS_BUFFER_PROTOTYPE); - DUK_ASSERT(h_bufres != NULL); - - p = (duk_uint8_t *) duk_push_fixed_buffer(ctx, total_length); - DUK_ASSERT(p != NULL); - space_left = total_length; - - for (i = 0; i < n; i++) { - DUK_ASSERT_TOP(ctx, 4); /* [ array totalLength bufres buf ] */ - - duk_get_prop_index(ctx, 0, (duk_uarridx_t) i); - h_bufobj = duk__require_bufobj_value(ctx, 4); - DUK_ASSERT(h_bufobj != NULL); - - copy_size = h_bufobj->length; - if (copy_size > space_left) { - copy_size = space_left; - } - - if (h_bufobj->buf != NULL && - DUK_HBUFFEROBJECT_VALID_SLICE(h_bufobj)) { - DUK_MEMCPY((void *) p, - (const void *) DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_bufobj), - copy_size); - } else { - /* Just skip, leaving zeroes in the result. */ - ; - } - p += copy_size; - space_left -= copy_size; - - duk_pop(ctx); - } - - h_val = duk_get_hbuffer(ctx, -1); - DUK_ASSERT(h_val != NULL); - - duk__set_bufobj_buffer(ctx, h_bufres, h_val); - DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufres); - - duk_pop(ctx); /* pop plain buffer, now reachable through h_bufres */ - - return 1; /* return h_bufres */ -} -#else /* DUK_USE_BUFFEROBJECT_SUPPORT */ -DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_concat(duk_context *ctx) { - DUK_UNREF(ctx); - return DUK_RET_UNSUPPORTED_ERROR; -} -#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ - -/* - * Shared readfield and writefield methods - * - * The readfield/writefield methods need support for endianness and field - * types. All offsets are byte based so no offset shifting is needed. - */ - -#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) -/* Format of magic, bits: - * 0...1: field type; 0=uint8, 1=uint16, 2=uint32, 3=float, 4=double, 5=unused, 6=unused, 7=unused - * 3: endianness: 0=little, 1=big - * 4: signed: 1=yes, 0=no - * 5: typedarray: 1=yes, 0=no - */ -#define DUK__FLD_8BIT 0 -#define DUK__FLD_16BIT 1 -#define DUK__FLD_32BIT 2 -#define DUK__FLD_FLOAT 3 -#define DUK__FLD_DOUBLE 4 -#define DUK__FLD_VARINT 5 -#define DUK__FLD_BIGENDIAN (1 << 3) -#define DUK__FLD_SIGNED (1 << 4) -#define DUK__FLD_TYPEDARRAY (1 << 5) - -/* XXX: split into separate functions for each field type? */ -DUK_INTERNAL duk_ret_t duk_bi_buffer_readfield(duk_context *ctx) { - duk_hthread *thr; - duk_small_int_t magic = (duk_small_int_t) duk_get_current_magic(ctx); - duk_small_int_t magic_ftype; - duk_small_int_t magic_bigendian; - duk_small_int_t magic_signed; - duk_small_int_t magic_typedarray; - duk_small_int_t endswap; - duk_hbufferobject *h_this; - duk_bool_t no_assert; - duk_int_t offset_signed; - duk_uint_t offset; - duk_uint_t buffer_length; - duk_uint_t check_length; - duk_uint8_t *buf; - duk_double_union du; - - thr = (duk_hthread *) ctx; - DUK_UNREF(thr); - - magic_ftype = magic & 0x0007; - magic_bigendian = magic & 0x0008; - magic_signed = magic & 0x0010; - magic_typedarray = magic & 0x0020; - - h_this = duk__require_bufobj_this(ctx); - DUK_ASSERT(h_this != NULL); - buffer_length = h_this->length; - - /* [ offset noAssert ], when ftype != DUK__FLD_VARINT */ - /* [ offset fieldByteLength noAssert ], when ftype == DUK__FLD_VARINT */ - /* [ offset littleEndian ], when DUK__FLD_TYPEDARRAY (regardless of ftype) */ - - /* Handle TypedArray vs. Node.js Buffer arg differences */ - if (magic_typedarray) { - no_assert = 0; -#if defined(DUK_USE_INTEGER_LE) - endswap = !duk_to_boolean(ctx, 1); /* 1=little endian */ -#else - endswap = duk_to_boolean(ctx, 1); /* 1=little endian */ -#endif - } else { - no_assert = duk_to_boolean(ctx, (magic_ftype == DUK__FLD_VARINT) ? 2 : 1); -#if defined(DUK_USE_INTEGER_LE) - endswap = magic_bigendian; -#else - endswap = !magic_bigendian; -#endif - } - - /* Offset is coerced first to signed integer range and then to unsigned. - * This ensures we can add a small byte length (1-8) to the offset in - * bound checks and not wrap. - */ - offset_signed = duk_to_int(ctx, 0); - offset = (duk_uint_t) offset_signed; - if (offset_signed < 0) { - goto fail_bounds; - } - - DUK_DDD(DUK_DDDPRINT("readfield, buffer_length=%ld, offset=%ld, no_assert=%d, " - "magic=%04x, magic_fieldtype=%d, magic_bigendian=%d, magic_signed=%d, " - "endswap=%d", - (long) buffer_length, (long) offset, (int) no_assert, - (unsigned int) magic, (int) magic_ftype, (int) (magic_bigendian >> 3), - (int) (magic_signed >> 4), (int) endswap)); - - /* Update 'buffer_length' to be the effective, safe limit which - * takes into account the underlying buffer. This value will be - * potentially invalidated by any side effect. - */ - check_length = DUK_HBUFFEROBJECT_CLAMP_BYTELENGTH(h_this, buffer_length); - DUK_DDD(DUK_DDDPRINT("buffer_length=%ld, check_length=%ld", - (long) buffer_length, (long) check_length)); - - if (h_this->buf) { - buf = DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_this); - } else { - /* Neutered. We could go into the switch-case safely with - * buf == NULL because check_length == 0. To avoid scanbuild - * warnings, fail directly instead. - */ - DUK_ASSERT(check_length == 0); - goto fail_neutered; - } - DUK_ASSERT(buf != NULL); - - switch (magic_ftype) { - case DUK__FLD_8BIT: { - duk_uint8_t tmp; - if (offset + 1U > check_length) { - goto fail_bounds; - } - tmp = buf[offset]; - if (magic_signed) { - duk_push_int(ctx, (duk_int_t) ((duk_int8_t) tmp)); - } else { - duk_push_uint(ctx, (duk_uint_t) tmp); - } - break; - } - case DUK__FLD_16BIT: { - duk_uint16_t tmp; - if (offset + 2U > check_length) { - goto fail_bounds; - } - DUK_MEMCPY((void *) du.uc, (const void *) (buf + offset), 2); - tmp = du.us[0]; - if (endswap) { - tmp = DUK_BSWAP16(tmp); - } - if (magic_signed) { - duk_push_int(ctx, (duk_int_t) ((duk_int16_t) tmp)); - } else { - duk_push_uint(ctx, (duk_uint_t) tmp); - } - break; - } - case DUK__FLD_32BIT: { - duk_uint32_t tmp; - if (offset + 4U > check_length) { - goto fail_bounds; - } - DUK_MEMCPY((void *) du.uc, (const void *) (buf + offset), 4); - tmp = du.ui[0]; - if (endswap) { - tmp = DUK_BSWAP32(tmp); - } - if (magic_signed) { - duk_push_int(ctx, (duk_int_t) ((duk_int32_t) tmp)); - } else { - duk_push_uint(ctx, (duk_uint_t) tmp); - } - break; - } - case DUK__FLD_FLOAT: { - duk_uint32_t tmp; - if (offset + 4U > check_length) { - goto fail_bounds; - } - DUK_MEMCPY((void *) du.uc, (const void *) (buf + offset), 4); - if (endswap) { - tmp = du.ui[0]; - tmp = DUK_BSWAP32(tmp); - du.ui[0] = tmp; - } - duk_push_number(ctx, (duk_double_t) du.f[0]); - break; - } - case DUK__FLD_DOUBLE: { - if (offset + 8U > check_length) { - goto fail_bounds; - } - DUK_MEMCPY((void *) du.uc, (const void *) (buf + offset), 8); - if (endswap) { - DUK_DBLUNION_BSWAP64(&du); - } - duk_push_number(ctx, (duk_double_t) du.d); - break; - } - case DUK__FLD_VARINT: { - /* Node.js Buffer variable width integer field. We don't really - * care about speed here, so aim for shortest algorithm. - */ - duk_int_t field_bytelen; - duk_int_t i, i_step, i_end; -#if defined(DUK_USE_64BIT_OPS) - duk_int64_t tmp; - duk_small_uint_t shift_tmp; -#else - duk_double_t tmp; - duk_small_int_t highbyte; -#endif - const duk_uint8_t *p; - - field_bytelen = duk_get_int(ctx, 1); /* avoid side effects! */ - if (field_bytelen < 1 || field_bytelen > 6) { - goto fail_field_length; - } - if (offset + (duk_uint_t) field_bytelen > check_length) { - goto fail_bounds; - } - p = (const duk_uint8_t *) (buf + offset); - - /* Slow gathering of value using either 64-bit arithmetic - * or IEEE doubles if 64-bit types not available. Handling - * of negative numbers is a bit non-obvious in both cases. - */ - - if (magic_bigendian) { - /* Gather in big endian */ - i = 0; - i_step = 1; - i_end = field_bytelen; /* one i_step over */ - } else { - /* Gather in little endian */ - i = field_bytelen - 1; - i_step = -1; - i_end = -1; /* one i_step over */ - } - -#if defined(DUK_USE_64BIT_OPS) - tmp = 0; - do { - DUK_ASSERT(i >= 0 && i < field_bytelen); - tmp = (tmp << 8) + (duk_int64_t) p[i]; - i += i_step; - } while (i != i_end); - - if (magic_signed) { - /* Shift to sign extend. */ - shift_tmp = 64 - (field_bytelen * 8); - tmp = (tmp << shift_tmp) >> shift_tmp; - } - - duk_push_i64(ctx, tmp); -#else - highbyte = p[i]; - if (magic_signed && (highbyte & 0x80) != 0) { - /* 0xff => 255 - 256 = -1; 0x80 => 128 - 256 = -128 */ - tmp = (duk_double_t) (highbyte - 256); - } else { - tmp = (duk_double_t) highbyte; - } - for (;;) { - i += i_step; - if (i == i_end) { - break; - } - DUK_ASSERT(i >= 0 && i < field_bytelen); - tmp = (tmp * 256.0) + (duk_double_t) p[i]; - } - - duk_push_number(ctx, tmp); -#endif - break; - } - default: { /* should never happen but default here */ - goto fail_bounds; - } - } - - return 1; - - fail_neutered: - fail_field_length: - fail_bounds: - if (no_assert) { - /* Node.js return value for noAssert out-of-bounds reads is - * usually (but not always) NaN. Return NaN consistently. - */ - duk_push_nan(ctx); - return 1; - } - - return DUK_RET_RANGE_ERROR; -} -#else /* DUK_USE_BUFFEROBJECT_SUPPORT */ -DUK_INTERNAL duk_ret_t duk_bi_buffer_readfield(duk_context *ctx) { - DUK_UNREF(ctx); - return DUK_RET_UNSUPPORTED_ERROR; -} -#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ - -#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) -/* XXX: split into separate functions for each field type? */ -DUK_INTERNAL duk_ret_t duk_bi_buffer_writefield(duk_context *ctx) { - duk_hthread *thr; - duk_small_int_t magic = (duk_small_int_t) duk_get_current_magic(ctx); - duk_small_int_t magic_ftype; - duk_small_int_t magic_bigendian; - duk_small_int_t magic_signed; - duk_small_int_t magic_typedarray; - duk_small_int_t endswap; - duk_hbufferobject *h_this; - duk_bool_t no_assert; - duk_int_t offset_signed; - duk_uint_t offset; - duk_uint_t buffer_length; - duk_uint_t check_length; - duk_uint8_t *buf; - duk_double_union du; - duk_int_t nbytes = 0; - - thr = (duk_hthread *) ctx; - DUK_UNREF(thr); - - magic_ftype = magic & 0x0007; - magic_bigendian = magic & 0x0008; - magic_signed = magic & 0x0010; - magic_typedarray = magic & 0x0020; - DUK_UNREF(magic_signed); - - h_this = duk__require_bufobj_this(ctx); - DUK_ASSERT(h_this != NULL); - buffer_length = h_this->length; - - /* [ value offset noAssert ], when ftype != DUK__FLD_VARINT */ - /* [ value offset fieldByteLength noAssert ], when ftype == DUK__FLD_VARINT */ - /* [ offset value littleEndian ], when DUK__FLD_TYPEDARRAY (regardless of ftype) */ - - /* Handle TypedArray vs. Node.js Buffer arg differences */ - if (magic_typedarray) { - no_assert = 0; -#if defined(DUK_USE_INTEGER_LE) - endswap = !duk_to_boolean(ctx, 2); /* 1=little endian */ -#else - endswap = duk_to_boolean(ctx, 2); /* 1=little endian */ -#endif - duk_swap(ctx, 0, 1); /* offset/value order different from Node.js */ - } else { - no_assert = duk_to_boolean(ctx, (magic_ftype == DUK__FLD_VARINT) ? 3 : 2); -#if defined(DUK_USE_INTEGER_LE) - endswap = magic_bigendian; -#else - endswap = !magic_bigendian; -#endif - } - - /* Offset is coerced first to signed integer range and then to unsigned. - * This ensures we can add a small byte length (1-8) to the offset in - * bound checks and not wrap. - */ - offset_signed = duk_to_int(ctx, 1); - offset = (duk_uint_t) offset_signed; - - /* We need 'nbytes' even for a failed offset; return value must be - * (offset + nbytes) even when write fails due to invalid offset. - */ - if (magic_ftype != DUK__FLD_VARINT) { - DUK_ASSERT(magic_ftype >= 0 && magic_ftype < (duk_small_int_t) (sizeof(duk__buffer_nbytes_from_fldtype) / sizeof(duk_uint8_t))); - nbytes = duk__buffer_nbytes_from_fldtype[magic_ftype]; - } else { - nbytes = duk_get_int(ctx, 2); - if (nbytes < 1 || nbytes > 6) { - goto fail_field_length; - } - } - DUK_ASSERT(nbytes >= 1 && nbytes <= 8); - - /* Now we can check offset validity. */ - if (offset_signed < 0) { - goto fail_bounds; - } - - DUK_DDD(DUK_DDDPRINT("writefield, value=%!T, buffer_length=%ld, offset=%ld, no_assert=%d, " - "magic=%04x, magic_fieldtype=%d, magic_bigendian=%d, magic_signed=%d, " - "endswap=%d", - duk_get_tval(ctx, 0), (long) buffer_length, (long) offset, (int) no_assert, - (unsigned int) magic, (int) magic_ftype, (int) (magic_bigendian >> 3), - (int) (magic_signed >> 4), (int) endswap)); - - /* Coerce value to a number before computing check_length, so that - * the field type specific coercion below can't have side effects - * that would invalidate check_length. - */ - duk_to_number(ctx, 0); - - /* Update 'buffer_length' to be the effective, safe limit which - * takes into account the underlying buffer. This value will be - * potentially invalidated by any side effect. - */ - check_length = DUK_HBUFFEROBJECT_CLAMP_BYTELENGTH(h_this, buffer_length); - DUK_DDD(DUK_DDDPRINT("buffer_length=%ld, check_length=%ld", - (long) buffer_length, (long) check_length)); - - if (h_this->buf) { - buf = DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_this); - } else { - /* Neutered. We could go into the switch-case safely with - * buf == NULL because check_length == 0. To avoid scanbuild - * warnings, fail directly instead. - */ - DUK_ASSERT(check_length == 0); - goto fail_neutered; - } - DUK_ASSERT(buf != NULL); - - switch (magic_ftype) { - case DUK__FLD_8BIT: { - if (offset + 1U > check_length) { - goto fail_bounds; - } - /* sign doesn't matter when writing */ - buf[offset] = (duk_uint8_t) duk_to_uint32(ctx, 0); - break; - } - case DUK__FLD_16BIT: { - duk_uint16_t tmp; - if (offset + 2U > check_length) { - goto fail_bounds; - } - tmp = (duk_uint16_t) duk_to_uint32(ctx, 0); - if (endswap) { - tmp = DUK_BSWAP16(tmp); - } - du.us[0] = tmp; - /* sign doesn't matter when writing */ - DUK_MEMCPY((void *) (buf + offset), (const void *) du.uc, 2); - break; - } - case DUK__FLD_32BIT: { - duk_uint32_t tmp; - if (offset + 4U > check_length) { - goto fail_bounds; - } - tmp = (duk_uint32_t) duk_to_uint32(ctx, 0); - if (endswap) { - tmp = DUK_BSWAP32(tmp); - } - du.ui[0] = tmp; - /* sign doesn't matter when writing */ - DUK_MEMCPY((void *) (buf + offset), (const void *) du.uc, 4); - break; - } - case DUK__FLD_FLOAT: { - duk_uint32_t tmp; - if (offset + 4U > check_length) { - goto fail_bounds; - } - du.f[0] = (duk_float_t) duk_to_number(ctx, 0); - if (endswap) { - tmp = du.ui[0]; - tmp = DUK_BSWAP32(tmp); - du.ui[0] = tmp; - } - /* sign doesn't matter when writing */ - DUK_MEMCPY((void *) (buf + offset), (const void *) du.uc, 4); - break; - } - case DUK__FLD_DOUBLE: { - if (offset + 8U > check_length) { - goto fail_bounds; - } - du.d = (duk_double_t) duk_to_number(ctx, 0); - if (endswap) { - DUK_DBLUNION_BSWAP64(&du); - } - /* sign doesn't matter when writing */ - DUK_MEMCPY((void *) (buf + offset), (const void *) du.uc, 8); - break; - } - case DUK__FLD_VARINT: { - /* Node.js Buffer variable width integer field. We don't really - * care about speed here, so aim for shortest algorithm. - */ - duk_int_t field_bytelen; - duk_int_t i, i_step, i_end; -#if defined(DUK_USE_64BIT_OPS) - duk_int64_t tmp; -#else - duk_double_t tmp; -#endif - duk_uint8_t *p; - - field_bytelen = (duk_int_t) nbytes; - if (offset + (duk_uint_t) field_bytelen > check_length) { - goto fail_bounds; - } - - /* Slow writing of value using either 64-bit arithmetic - * or IEEE doubles if 64-bit types not available. There's - * no special sign handling when writing varints. - */ - - if (magic_bigendian) { - /* Write in big endian */ - i = field_bytelen; /* one i_step added at top of loop */ - i_step = -1; - i_end = 0; - } else { - /* Write in little endian */ - i = -1; /* one i_step added at top of loop */ - i_step = 1; - i_end = field_bytelen - 1; - } - - /* XXX: The duk_to_number() cast followed by integer coercion - * is platform specific so NaN, +/- Infinity, and out-of-bounds - * values result in platform specific output now. - * See: test-bi-nodejs-buffer-proto-varint-special.js - */ - -#if defined(DUK_USE_64BIT_OPS) - tmp = (duk_int64_t) duk_to_number(ctx, 0); - p = (duk_uint8_t *) (buf + offset); - do { - i += i_step; - DUK_ASSERT(i >= 0 && i < field_bytelen); - p[i] = (duk_uint8_t) (tmp & 0xff); - tmp = tmp >> 8; /* unnecessary shift for last byte */ - } while (i != i_end); -#else - tmp = duk_to_number(ctx, 0); - p = (duk_uint8_t *) (buf + offset); - do { - i += i_step; - tmp = DUK_FLOOR(tmp); - DUK_ASSERT(i >= 0 && i < field_bytelen); - p[i] = (duk_uint8_t) (DUK_FMOD(tmp, 256.0)); - tmp = tmp / 256.0; /* unnecessary div for last byte */ - } while (i != i_end); -#endif - break; - } - default: { /* should never happen but default here */ - goto fail_bounds; - } - } - - /* Node.js Buffer: return offset + #bytes written (i.e. next - * write offset). - */ - if (magic_typedarray) { - /* For TypedArrays 'undefined' return value is specified - * by ES6 (matches V8). - */ - return 0; - } - duk_push_uint(ctx, offset + nbytes); - return 1; - - fail_neutered: - fail_field_length: - fail_bounds: - if (no_assert) { - /* Node.js return value for failed writes is offset + #bytes - * that would have been written. - */ - /* XXX: for negative input offsets, 'offset' will be a large - * positive value so the result here is confusing. - */ - if (magic_typedarray) { - return 0; - } - duk_push_uint(ctx, offset + nbytes); - return 1; - } - return DUK_RET_RANGE_ERROR; -} -#else /* DUK_USE_BUFFEROBJECT_SUPPORT */ -DUK_INTERNAL duk_ret_t duk_bi_buffer_writefield(duk_context *ctx) { - DUK_UNREF(ctx); - return DUK_RET_UNSUPPORTED_ERROR; -} -#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ - -#undef DUK__FLD_8BIT -#undef DUK__FLD_16BIT -#undef DUK__FLD_32BIT -#undef DUK__FLD_FLOAT -#undef DUK__FLD_DOUBLE -#undef DUK__FLD_VARINT -#undef DUK__FLD_BIGENDIAN -#undef DUK__FLD_SIGNED -#undef DUK__FLD_TYPEDARRAY -#line 1 "duk_bi_date.c" -/* - * Date built-ins - * - * Unlike most built-ins, Date has some platform dependencies for getting - * UTC time, converting between UTC and local time, and parsing and - * formatting time values. These are all abstracted behind DUK_USE_xxx - * config options. There are built-in platform specific providers for - * POSIX and Windows, but external providers can also be used. - * - * See doc/datetime.rst. - * - */ - -/* include removed: duk_internal.h */ - -/* - * Forward declarations - */ - -DUK_LOCAL_DECL duk_double_t duk__push_this_get_timeval_tzoffset(duk_context *ctx, duk_small_uint_t flags, duk_int_t *out_tzoffset); -DUK_LOCAL_DECL duk_double_t duk__push_this_get_timeval(duk_context *ctx, duk_small_uint_t flags); -DUK_LOCAL_DECL void duk__twodigit_year_fixup(duk_context *ctx, duk_idx_t idx_val); -DUK_LOCAL_DECL duk_ret_t duk__set_this_timeval_from_dparts(duk_context *ctx, duk_double_t *dparts, duk_small_uint_t flags); - -/* - * Other file level defines - */ - -/* Debug macro to print all parts and dparts (used manually because of debug level). */ -#define DUK__DPRINT_PARTS_AND_DPARTS(parts,dparts) do { \ - DUK_D(DUK_DPRINT("parts: %ld %ld %ld %ld %ld %ld %ld %ld, dparts: %lf %lf %lf %lf %lf %lf %lf %lf", \ - (long) (parts)[0], (long) (parts)[1], \ - (long) (parts)[2], (long) (parts)[3], \ - (long) (parts)[4], (long) (parts)[5], \ - (long) (parts)[6], (long) (parts)[7], \ - (double) (dparts)[0], (double) (dparts)[1], \ - (double) (dparts)[2], (double) (dparts)[3], \ - (double) (dparts)[4], (double) (dparts)[5], \ - (double) (dparts)[6], (double) (dparts)[7])); \ - } while (0) -#define DUK__DPRINT_PARTS(parts) do { \ - DUK_D(DUK_DPRINT("parts: %ld %ld %ld %ld %ld %ld %ld %ld", \ - (long) (parts)[0], (long) (parts)[1], \ - (long) (parts)[2], (long) (parts)[3], \ - (long) (parts)[4], (long) (parts)[5], \ - (long) (parts)[6], (long) (parts)[7])); \ - } while (0) -#define DUK__DPRINT_DPARTS(dparts) do { \ - DUK_D(DUK_DPRINT("dparts: %lf %lf %lf %lf %lf %lf %lf %lf", \ - (double) (dparts)[0], (double) (dparts)[1], \ - (double) (dparts)[2], (double) (dparts)[3], \ - (double) (dparts)[4], (double) (dparts)[5], \ - (double) (dparts)[6], (double) (dparts)[7])); \ - } while (0) - -/* Equivalent year for DST calculations outside [1970,2038[ range, see - * E5 Section 15.9.1.8. Equivalent year has the same leap-year-ness and - * starts with the same weekday on Jan 1. - * https://bugzilla.mozilla.org/show_bug.cgi?id=351066 - */ -#define DUK__YEAR(x) ((duk_uint8_t) ((x) - 1970)) -DUK_LOCAL duk_uint8_t duk__date_equivyear[14] = { -#if 1 - /* This is based on V8 EquivalentYear() algorithm (see src/genequivyear.py): - * http://code.google.com/p/v8/source/browse/trunk/src/date.h#146 - */ - - /* non-leap year: sunday, monday, ... */ - DUK__YEAR(2023), DUK__YEAR(2035), DUK__YEAR(2019), DUK__YEAR(2031), - DUK__YEAR(2015), DUK__YEAR(2027), DUK__YEAR(2011), - - /* leap year: sunday, monday, ... */ - DUK__YEAR(2012), DUK__YEAR(2024), DUK__YEAR(2008), DUK__YEAR(2020), - DUK__YEAR(2032), DUK__YEAR(2016), DUK__YEAR(2028) -#endif - -#if 0 - /* This is based on Rhino EquivalentYear() algorithm: - * https://github.com/mozilla/rhino/blob/f99cc11d616f0cdda2c42bde72b3484df6182947/src/org/mozilla/javascript/NativeDate.java - */ - - /* non-leap year: sunday, monday, ... */ - DUK__YEAR(1978), DUK__YEAR(1973), DUK__YEAR(1985), DUK__YEAR(1986), - DUK__YEAR(1981), DUK__YEAR(1971), DUK__YEAR(1977), - - /* leap year: sunday, monday, ... */ - DUK__YEAR(1984), DUK__YEAR(1996), DUK__YEAR(1980), DUK__YEAR(1992), - DUK__YEAR(1976), DUK__YEAR(1988), DUK__YEAR(1972) -#endif -}; -#undef DUK__YEAR - -/* - * ISO 8601 subset parser. - */ - -/* Parser part count. */ -#define DUK__NUM_ISO8601_PARSER_PARTS 9 - -/* Parser part indices. */ -#define DUK__PI_YEAR 0 -#define DUK__PI_MONTH 1 -#define DUK__PI_DAY 2 -#define DUK__PI_HOUR 3 -#define DUK__PI_MINUTE 4 -#define DUK__PI_SECOND 5 -#define DUK__PI_MILLISECOND 6 -#define DUK__PI_TZHOUR 7 -#define DUK__PI_TZMINUTE 8 - -/* Parser part masks. */ -#define DUK__PM_YEAR (1 << DUK__PI_YEAR) -#define DUK__PM_MONTH (1 << DUK__PI_MONTH) -#define DUK__PM_DAY (1 << DUK__PI_DAY) -#define DUK__PM_HOUR (1 << DUK__PI_HOUR) -#define DUK__PM_MINUTE (1 << DUK__PI_MINUTE) -#define DUK__PM_SECOND (1 << DUK__PI_SECOND) -#define DUK__PM_MILLISECOND (1 << DUK__PI_MILLISECOND) -#define DUK__PM_TZHOUR (1 << DUK__PI_TZHOUR) -#define DUK__PM_TZMINUTE (1 << DUK__PI_TZMINUTE) - -/* Parser separator indices. */ -#define DUK__SI_PLUS 0 -#define DUK__SI_MINUS 1 -#define DUK__SI_T 2 -#define DUK__SI_SPACE 3 -#define DUK__SI_COLON 4 -#define DUK__SI_PERIOD 5 -#define DUK__SI_Z 6 -#define DUK__SI_NUL 7 - -/* Parser separator masks. */ -#define DUK__SM_PLUS (1 << DUK__SI_PLUS) -#define DUK__SM_MINUS (1 << DUK__SI_MINUS) -#define DUK__SM_T (1 << DUK__SI_T) -#define DUK__SM_SPACE (1 << DUK__SI_SPACE) -#define DUK__SM_COLON (1 << DUK__SI_COLON) -#define DUK__SM_PERIOD (1 << DUK__SI_PERIOD) -#define DUK__SM_Z (1 << DUK__SI_Z) -#define DUK__SM_NUL (1 << DUK__SI_NUL) - -/* Rule control flags. */ -#define DUK__CF_NEG (1 << 0) /* continue matching, set neg_tzoffset flag */ -#define DUK__CF_ACCEPT (1 << 1) /* accept string */ -#define DUK__CF_ACCEPT_NUL (1 << 2) /* accept string if next char is NUL (otherwise reject) */ - -#define DUK__PACK_RULE(partmask,sepmask,nextpart,flags) \ - ((duk_uint32_t) (partmask) + \ - (((duk_uint32_t) (sepmask)) << 9) + \ - (((duk_uint32_t) (nextpart)) << 17) + \ - (((duk_uint32_t) (flags)) << 21)) - -#define DUK__UNPACK_RULE(rule,var_nextidx,var_flags) do { \ - (var_nextidx) = (duk_small_uint_t) (((rule) >> 17) & 0x0f); \ - (var_flags) = (duk_small_uint_t) ((rule) >> 21); \ - } while (0) - -#define DUK__RULE_MASK_PART_SEP 0x1ffffUL - -/* Matching separator index is used in the control table */ -DUK_LOCAL const duk_uint8_t duk__parse_iso8601_seps[] = { - DUK_ASC_PLUS /*0*/, DUK_ASC_MINUS /*1*/, DUK_ASC_UC_T /*2*/, DUK_ASC_SPACE /*3*/, - DUK_ASC_COLON /*4*/, DUK_ASC_PERIOD /*5*/, DUK_ASC_UC_Z /*6*/, DUK_ASC_NUL /*7*/ -}; - -/* Rule table: first matching rule is used to determine what to do next. */ -DUK_LOCAL const duk_uint32_t duk__parse_iso8601_control[] = { - DUK__PACK_RULE(DUK__PM_YEAR, DUK__SM_MINUS, DUK__PI_MONTH, 0), - DUK__PACK_RULE(DUK__PM_MONTH, DUK__SM_MINUS, DUK__PI_DAY, 0), - DUK__PACK_RULE(DUK__PM_YEAR | DUK__PM_MONTH | DUK__PM_DAY, DUK__SM_T | DUK__SM_SPACE, DUK__PI_HOUR, 0), - DUK__PACK_RULE(DUK__PM_HOUR, DUK__SM_COLON, DUK__PI_MINUTE, 0), - DUK__PACK_RULE(DUK__PM_MINUTE, DUK__SM_COLON, DUK__PI_SECOND, 0), - DUK__PACK_RULE(DUK__PM_SECOND, DUK__SM_PERIOD, DUK__PI_MILLISECOND, 0), - DUK__PACK_RULE(DUK__PM_TZHOUR, DUK__SM_COLON, DUK__PI_TZMINUTE, 0), - DUK__PACK_RULE(DUK__PM_YEAR | DUK__PM_MONTH | DUK__PM_DAY | DUK__PM_HOUR /*Note1*/ | DUK__PM_MINUTE | DUK__PM_SECOND | DUK__PM_MILLISECOND, DUK__SM_PLUS, DUK__PI_TZHOUR, 0), - DUK__PACK_RULE(DUK__PM_YEAR | DUK__PM_MONTH | DUK__PM_DAY | DUK__PM_HOUR /*Note1*/ | DUK__PM_MINUTE | DUK__PM_SECOND | DUK__PM_MILLISECOND, DUK__SM_MINUS, DUK__PI_TZHOUR, DUK__CF_NEG), - DUK__PACK_RULE(DUK__PM_YEAR | DUK__PM_MONTH | DUK__PM_DAY | DUK__PM_HOUR /*Note1*/ | DUK__PM_MINUTE | DUK__PM_SECOND | DUK__PM_MILLISECOND, DUK__SM_Z, 0, DUK__CF_ACCEPT_NUL), - DUK__PACK_RULE(DUK__PM_YEAR | DUK__PM_MONTH | DUK__PM_DAY | DUK__PM_HOUR /*Note1*/ | DUK__PM_MINUTE | DUK__PM_SECOND | DUK__PM_MILLISECOND | DUK__PM_TZHOUR /*Note2*/ | DUK__PM_TZMINUTE, DUK__SM_NUL, 0, DUK__CF_ACCEPT) - - /* Note1: the specification doesn't require matching a time form with - * just hours ("HH"), but we accept it here, e.g. "2012-01-02T12Z". - * - * Note2: the specification doesn't require matching a timezone offset - * with just hours ("HH"), but accept it here, e.g. "2012-01-02T03:04:05+02" - */ -}; - -DUK_LOCAL duk_bool_t duk__parse_string_iso8601_subset(duk_context *ctx, const char *str) { - duk_int_t parts[DUK__NUM_ISO8601_PARSER_PARTS]; - duk_double_t dparts[DUK_DATE_IDX_NUM_PARTS]; - duk_double_t d; - const duk_uint8_t *p; - duk_small_uint_t part_idx = 0; - duk_int_t accum = 0; - duk_small_uint_t ndigits = 0; - duk_bool_t neg_year = 0; - duk_bool_t neg_tzoffset = 0; - duk_uint_fast8_t ch; - duk_small_uint_t i; - - /* During parsing, month and day are one-based; set defaults here. */ - DUK_MEMZERO(parts, sizeof(parts)); - DUK_ASSERT(parts[DUK_DATE_IDX_YEAR] == 0); /* don't care value, year is mandatory */ - parts[DUK_DATE_IDX_MONTH] = 1; - parts[DUK_DATE_IDX_DAY] = 1; - - /* Special handling for year sign. */ - p = (const duk_uint8_t *) str; - ch = p[0]; - if (ch == DUK_ASC_PLUS) { - p++; - } else if (ch == DUK_ASC_MINUS) { - neg_year = 1; - p++; - } - - for (;;) { - ch = *p++; - DUK_DDD(DUK_DDDPRINT("parsing, part_idx=%ld, char=%ld ('%c')", - (long) part_idx, (long) ch, - (int) ((ch >= 0x20 && ch <= 0x7e) ? ch : DUK_ASC_QUESTION))); - - if (ch >= DUK_ASC_0 && ch <= DUK_ASC_9) { - if (ndigits >= 9) { - DUK_DDD(DUK_DDDPRINT("too many digits -> reject")); - goto reject; - } - if (part_idx == DUK__PI_MILLISECOND /*msec*/ && ndigits >= 3) { - /* ignore millisecond fractions after 3 */ - } else { - accum = accum * 10 + ((duk_int_t) ch) - ((duk_int_t) DUK_ASC_0) + 0x00; - ndigits++; - } - } else { - duk_uint_fast32_t match_val; - duk_small_int_t sep_idx; - - if (ndigits <= 0) { - goto reject; - } - if (part_idx == DUK__PI_MILLISECOND) { - /* complete the millisecond field */ - while (ndigits < 3) { - accum *= 10; - ndigits++; - } - } - parts[part_idx] = accum; - DUK_DDD(DUK_DDDPRINT("wrote part %ld -> value %ld", (long) part_idx, (long) accum)); - - accum = 0; - ndigits = 0; - - for (i = 0; i < (duk_small_uint_t) (sizeof(duk__parse_iso8601_seps) / sizeof(duk_uint8_t)); i++) { - if (duk__parse_iso8601_seps[i] == ch) { - break; - } - } - if (i == (duk_small_uint_t) (sizeof(duk__parse_iso8601_seps) / sizeof(duk_uint8_t))) { - DUK_DDD(DUK_DDDPRINT("separator character doesn't match -> reject")); - goto reject; - } - - sep_idx = i; - match_val = (1UL << part_idx) + (1UL << (sep_idx + 9)); /* match against rule part/sep bits */ - - for (i = 0; i < (duk_small_uint_t) (sizeof(duk__parse_iso8601_control) / sizeof(duk_uint32_t)); i++) { - duk_uint_fast32_t rule = duk__parse_iso8601_control[i]; - duk_small_uint_t nextpart; - duk_small_uint_t cflags; - - DUK_DDD(DUK_DDDPRINT("part_idx=%ld, sep_idx=%ld, match_val=0x%08lx, considering rule=0x%08lx", - (long) part_idx, (long) sep_idx, - (unsigned long) match_val, (unsigned long) rule)); - - if ((rule & match_val) != match_val) { - continue; - } - - DUK__UNPACK_RULE(rule, nextpart, cflags); - - DUK_DDD(DUK_DDDPRINT("rule match -> part_idx=%ld, sep_idx=%ld, match_val=0x%08lx, " - "rule=0x%08lx -> nextpart=%ld, cflags=0x%02lx", - (long) part_idx, (long) sep_idx, - (unsigned long) match_val, (unsigned long) rule, - (long) nextpart, (unsigned long) cflags)); - - if (cflags & DUK__CF_NEG) { - neg_tzoffset = 1; - } - - if (cflags & DUK__CF_ACCEPT) { - goto accept; - } - - if (cflags & DUK__CF_ACCEPT_NUL) { - DUK_ASSERT(*(p - 1) != (char) 0); - if (*p == DUK_ASC_NUL) { - goto accept; - } - goto reject; - } - - part_idx = nextpart; - break; - } /* rule match */ - - if (i == (duk_small_uint_t) (sizeof(duk__parse_iso8601_control) / sizeof(duk_uint32_t))) { - DUK_DDD(DUK_DDDPRINT("no rule matches -> reject")); - goto reject; - } - - if (ch == 0) { - /* This shouldn't be necessary, but check just in case - * to avoid any chance of overruns. - */ - DUK_DDD(DUK_DDDPRINT("NUL after rule matching (should not happen) -> reject")); - goto reject; - } - } /* if-digit-else-ctrl */ - } /* char loop */ - - /* We should never exit the loop above. */ - DUK_UNREACHABLE(); - - reject: - DUK_DDD(DUK_DDDPRINT("reject")); - return 0; - - accept: - DUK_DDD(DUK_DDDPRINT("accept")); - - /* Apply timezone offset to get the main parts in UTC */ - if (neg_year) { - parts[DUK__PI_YEAR] = -parts[DUK__PI_YEAR]; - } - if (neg_tzoffset) { - parts[DUK__PI_HOUR] += parts[DUK__PI_TZHOUR]; - parts[DUK__PI_MINUTE] += parts[DUK__PI_TZMINUTE]; - } else { - parts[DUK__PI_HOUR] -= parts[DUK__PI_TZHOUR]; - parts[DUK__PI_MINUTE] -= parts[DUK__PI_TZMINUTE]; - } - parts[DUK__PI_MONTH] -= 1; /* zero-based month */ - parts[DUK__PI_DAY] -= 1; /* zero-based day */ - - /* Use double parts, they tolerate unnormalized time. - * - * Note: DUK_DATE_IDX_WEEKDAY is initialized with a bogus value (DUK__PI_TZHOUR) - * on purpose. It won't be actually used by duk_bi_date_get_timeval_from_dparts(), - * but will make the value initialized just in case, and avoid any - * potential for Valgrind issues. - */ - for (i = 0; i < DUK_DATE_IDX_NUM_PARTS; i++) { - DUK_DDD(DUK_DDDPRINT("part[%ld] = %ld", (long) i, (long) parts[i])); - dparts[i] = parts[i]; - } - - d = duk_bi_date_get_timeval_from_dparts(dparts, 0 /*flags*/); - duk_push_number(ctx, d); - return 1; -} - -/* - * Date/time parsing helper. - * - * Parse a datetime string into a time value. We must first try to parse - * the input according to the standard format in E5.1 Section 15.9.1.15. - * If that fails, we can try to parse using custom parsing, which can - * either be platform neutral (custom code) or platform specific (using - * existing platform API calls). - * - * Note in particular that we must parse whatever toString(), toUTCString(), - * and toISOString() can produce; see E5.1 Section 15.9.4.2. - * - * Returns 1 to allow tail calling. - * - * There is much room for improvement here with respect to supporting - * alternative datetime formats. For instance, V8 parses '2012-01-01' as - * UTC and '2012/01/01' as local time. - */ - -DUK_LOCAL duk_ret_t duk__parse_string(duk_context *ctx, const char *str) { - /* XXX: there is a small risk here: because the ISO 8601 parser is - * very loose, it may end up parsing some datetime values which - * would be better parsed with a platform specific parser. - */ - - DUK_ASSERT(str != NULL); - DUK_DDD(DUK_DDDPRINT("parse datetime from string '%s'", (const char *) str)); - - if (duk__parse_string_iso8601_subset(ctx, str) != 0) { - return 1; - } - -#if defined(DUK_USE_DATE_PARSE_STRING) - /* Contract, either: - * - Push value on stack and return 1 - * - Don't push anything on stack and return 0 - */ - - if (DUK_USE_DATE_PARSE_STRING(ctx, str) != 0) { - return 1; - } -#else - /* No platform-specific parsing, this is not an error. */ -#endif - - duk_push_nan(ctx); - return 1; -} - -/* - * Calendar helpers - * - * Some helpers are used for getters and can operate on normalized values - * which can be represented with 32-bit signed integers. Other helpers are - * needed by setters and operate on un-normalized double values, must watch - * out for non-finite numbers etc. - */ - -DUK_LOCAL duk_uint8_t duk__days_in_month[12] = { - (duk_uint8_t) 31, (duk_uint8_t) 28, (duk_uint8_t) 31, (duk_uint8_t) 30, - (duk_uint8_t) 31, (duk_uint8_t) 30, (duk_uint8_t) 31, (duk_uint8_t) 31, - (duk_uint8_t) 30, (duk_uint8_t) 31, (duk_uint8_t) 30, (duk_uint8_t) 31 -}; - -/* Maximum iteration count for computing UTC-to-local time offset when - * creating an Ecmascript time value from local parts. - */ -#define DUK__LOCAL_TZOFFSET_MAXITER 4 - -/* Because 'day since epoch' can be negative and is used to compute weekday - * using a modulo operation, add this multiple of 7 to avoid negative values - * when year is below 1970 epoch. Ecmascript time values are restricted to - * +/- 100 million days from epoch, so this adder fits nicely into 32 bits. - * Round to a multiple of 7 (= floor(100000000 / 7) * 7) and add margin. - */ -#define DUK__WEEKDAY_MOD_ADDER (20000000 * 7) /* 0x08583b00 */ - -DUK_INTERNAL duk_bool_t duk_bi_date_is_leap_year(duk_int_t year) { - if ((year % 4) != 0) { - return 0; - } - if ((year % 100) != 0) { - return 1; - } - if ((year % 400) != 0) { - return 0; - } - return 1; -} - -DUK_INTERNAL duk_bool_t duk_bi_date_timeval_in_valid_range(duk_double_t x) { - return (x >= -DUK_DATE_MSEC_100M_DAYS && x <= DUK_DATE_MSEC_100M_DAYS); -} - -DUK_INTERNAL duk_bool_t duk_bi_date_timeval_in_leeway_range(duk_double_t x) { - return (x >= -DUK_DATE_MSEC_100M_DAYS_LEEWAY && x <= DUK_DATE_MSEC_100M_DAYS_LEEWAY); -} - -DUK_INTERNAL duk_bool_t duk_bi_date_year_in_valid_range(duk_double_t x) { - return (x >= DUK_DATE_MIN_ECMA_YEAR && x <= DUK_DATE_MAX_ECMA_YEAR); -} - -DUK_LOCAL duk_double_t duk__timeclip(duk_double_t x) { - if (!DUK_ISFINITE(x)) { - return DUK_DOUBLE_NAN; - } - - if (!duk_bi_date_timeval_in_valid_range(x)) { - return DUK_DOUBLE_NAN; - } - - x = duk_js_tointeger_number(x); - - /* Here we'd have the option to normalize -0 to +0. */ - return x; -} - -/* Integer division which floors also negative values correctly. */ -DUK_LOCAL duk_int_t duk__div_floor(duk_int_t a, duk_int_t b) { - DUK_ASSERT(b > 0); - if (a >= 0) { - return a / b; - } else { - /* e.g. a = -4, b = 5 --> -4 - 5 + 1 / 5 --> -8 / 5 --> -1 - * a = -5, b = 5 --> -5 - 5 + 1 / 5 --> -9 / 5 --> -1 - * a = -6, b = 5 --> -6 - 5 + 1 / 5 --> -10 / 5 --> -2 - */ - return (a - b + 1) / b; - } -} - -/* Compute day number of the first day of a given year. */ -DUK_LOCAL duk_int_t duk__day_from_year(duk_int_t year) { - /* Note: in integer arithmetic, (x / 4) is same as floor(x / 4) for non-negative - * values, but is incorrect for negative ones. - */ - return 365 * (year - 1970) - + duk__div_floor(year - 1969, 4) - - duk__div_floor(year - 1901, 100) - + duk__div_floor(year - 1601, 400); -} - -/* Given a day number, determine year and day-within-year. */ -DUK_LOCAL duk_int_t duk__year_from_day(duk_int_t day, duk_small_int_t *out_day_within_year) { - duk_int_t year; - duk_int_t diff_days; - - /* estimate year upwards (towards positive infinity), then back down; - * two iterations should be enough - */ - - if (day >= 0) { - year = 1970 + day / 365; - } else { - year = 1970 + day / 366; - } - - for (;;) { - diff_days = duk__day_from_year(year) - day; - DUK_DDD(DUK_DDDPRINT("year=%ld day=%ld, diff_days=%ld", (long) year, (long) day, (long) diff_days)); - if (diff_days <= 0) { - DUK_ASSERT(-diff_days < 366); /* fits into duk_small_int_t */ - *out_day_within_year = -diff_days; - DUK_DDD(DUK_DDDPRINT("--> year=%ld, day-within-year=%ld", - (long) year, (long) *out_day_within_year)); - DUK_ASSERT(*out_day_within_year >= 0); - DUK_ASSERT(*out_day_within_year < (duk_bi_date_is_leap_year(year) ? 366 : 365)); - return year; - } - - /* Note: this is very tricky; we must never 'overshoot' the - * correction downwards. - */ - year -= 1 + (diff_days - 1) / 366; /* conservative */ - } -} - -/* Given a (year, month, day-within-month) triple, compute day number. - * The input triple is un-normalized and may contain non-finite values. - */ -DUK_LOCAL duk_double_t duk__make_day(duk_double_t year, duk_double_t month, duk_double_t day) { - duk_int_t day_num; - duk_bool_t is_leap; - duk_small_int_t i, n; - - /* Assume that year, month, day are all coerced to whole numbers. - * They may also be NaN or infinity, in which case this function - * must return NaN or infinity to ensure time value becomes NaN. - * If 'day' is NaN, the final return will end up returning a NaN, - * so it doesn't need to be checked here. - */ - - if (!DUK_ISFINITE(year) || !DUK_ISFINITE(month)) { - return DUK_DOUBLE_NAN; - } - - year += DUK_FLOOR(month / 12.0); - - month = DUK_FMOD(month, 12.0); - if (month < 0.0) { - /* handle negative values */ - month += 12.0; - } - - /* The algorithm in E5.1 Section 15.9.1.12 normalizes month, but - * does not normalize the day-of-month (nor check whether or not - * it is finite) because it's not necessary for finding the day - * number which matches the (year,month) pair. - * - * We assume that duk__day_from_year() is exact here. - * - * Without an explicit infinity / NaN check in the beginning, - * day_num would be a bogus integer here. - * - * It's possible for 'year' to be out of integer range here. - * If so, we need to return NaN without integer overflow. - * This fixes test-bug-setyear-overflow.js. - */ - - if (!duk_bi_date_year_in_valid_range(year)) { - DUK_DD(DUK_DDPRINT("year not in ecmascript valid range, avoid integer overflow: %lf", (double) year)); - return DUK_DOUBLE_NAN; - } - day_num = duk__day_from_year((duk_int_t) year); - is_leap = duk_bi_date_is_leap_year((duk_int_t) year); - - n = (duk_small_int_t) month; - for (i = 0; i < n; i++) { - day_num += duk__days_in_month[i]; - if (i == 1 && is_leap) { - day_num++; - } - } - - /* If 'day' is NaN, returns NaN. */ - return (duk_double_t) day_num + day; -} - -/* Split time value into parts. The time value is assumed to be an internal - * one, i.e. finite, no fractions. Possible local time adjustment has already - * been applied when reading the time value. - */ -DUK_INTERNAL void duk_bi_date_timeval_to_parts(duk_double_t d, duk_int_t *parts, duk_double_t *dparts, duk_small_uint_t flags) { - duk_double_t d1, d2; - duk_int_t t1, t2; - duk_int_t day_since_epoch; - duk_int_t year; /* does not fit into 16 bits */ - duk_small_int_t day_in_year; - duk_small_int_t month; - duk_small_int_t day; - duk_small_int_t dim; - duk_int_t jan1_since_epoch; - duk_small_int_t jan1_weekday; - duk_int_t equiv_year; - duk_small_uint_t i; - duk_bool_t is_leap; - duk_small_int_t arridx; - - DUK_ASSERT(DUK_ISFINITE(d)); /* caller checks */ - DUK_ASSERT(DUK_FLOOR(d) == d); /* no fractions in internal time */ - - /* The timevalue must be in valid Ecmascript range, but since a local - * time offset can be applied, we need to allow a +/- 24h leeway to - * the value. In other words, although the UTC time is within the - * Ecmascript range, the local part values can be just outside of it. - */ - DUK_UNREF(duk_bi_date_timeval_in_leeway_range); - DUK_ASSERT(duk_bi_date_timeval_in_leeway_range(d)); - - /* these computations are guaranteed to be exact for the valid - * E5 time value range, assuming milliseconds without fractions. - */ - d1 = (duk_double_t) DUK_FMOD(d, (double) DUK_DATE_MSEC_DAY); - if (d1 < 0.0) { - /* deal with negative values */ - d1 += (duk_double_t) DUK_DATE_MSEC_DAY; - } - d2 = DUK_FLOOR((double) (d / (duk_double_t) DUK_DATE_MSEC_DAY)); - DUK_ASSERT(d2 * ((duk_double_t) DUK_DATE_MSEC_DAY) + d1 == d); - /* now expected to fit into a 32-bit integer */ - t1 = (duk_int_t) d1; - t2 = (duk_int_t) d2; - day_since_epoch = t2; - DUK_ASSERT((duk_double_t) t1 == d1); - DUK_ASSERT((duk_double_t) t2 == d2); - - /* t1 = milliseconds within day (fits 32 bit) - * t2 = day number from epoch (fits 32 bit, may be negative) - */ - - parts[DUK_DATE_IDX_MILLISECOND] = t1 % 1000; t1 /= 1000; - parts[DUK_DATE_IDX_SECOND] = t1 % 60; t1 /= 60; - parts[DUK_DATE_IDX_MINUTE] = t1 % 60; t1 /= 60; - parts[DUK_DATE_IDX_HOUR] = t1; - DUK_ASSERT(parts[DUK_DATE_IDX_MILLISECOND] >= 0 && parts[DUK_DATE_IDX_MILLISECOND] <= 999); - DUK_ASSERT(parts[DUK_DATE_IDX_SECOND] >= 0 && parts[DUK_DATE_IDX_SECOND] <= 59); - DUK_ASSERT(parts[DUK_DATE_IDX_MINUTE] >= 0 && parts[DUK_DATE_IDX_MINUTE] <= 59); - DUK_ASSERT(parts[DUK_DATE_IDX_HOUR] >= 0 && parts[DUK_DATE_IDX_HOUR] <= 23); - - DUK_DDD(DUK_DDDPRINT("d=%lf, d1=%lf, d2=%lf, t1=%ld, t2=%ld, parts: hour=%ld min=%ld sec=%ld msec=%ld", - (double) d, (double) d1, (double) d2, (long) t1, (long) t2, - (long) parts[DUK_DATE_IDX_HOUR], - (long) parts[DUK_DATE_IDX_MINUTE], - (long) parts[DUK_DATE_IDX_SECOND], - (long) parts[DUK_DATE_IDX_MILLISECOND])); - - /* This assert depends on the input parts representing time inside - * the Ecmascript range. - */ - DUK_ASSERT(t2 + DUK__WEEKDAY_MOD_ADDER >= 0); - parts[DUK_DATE_IDX_WEEKDAY] = (t2 + 4 + DUK__WEEKDAY_MOD_ADDER) % 7; /* E5.1 Section 15.9.1.6 */ - DUK_ASSERT(parts[DUK_DATE_IDX_WEEKDAY] >= 0 && parts[DUK_DATE_IDX_WEEKDAY] <= 6); - - year = duk__year_from_day(t2, &day_in_year); - day = day_in_year; - is_leap = duk_bi_date_is_leap_year(year); - for (month = 0; month < 12; month++) { - dim = duk__days_in_month[month]; - if (month == 1 && is_leap) { - dim++; - } - DUK_DDD(DUK_DDDPRINT("month=%ld, dim=%ld, day=%ld", - (long) month, (long) dim, (long) day)); - if (day < dim) { - break; - } - day -= dim; - } - DUK_DDD(DUK_DDDPRINT("final month=%ld", (long) month)); - DUK_ASSERT(month >= 0 && month <= 11); - DUK_ASSERT(day >= 0 && day <= 31); - - /* Equivalent year mapping, used to avoid DST trouble when platform - * may fail to provide reasonable DST answers for dates outside the - * ordinary range (e.g. 1970-2038). An equivalent year has the same - * leap-year-ness as the original year and begins on the same weekday - * (Jan 1). - * - * The year 2038 is avoided because there seem to be problems with it - * on some platforms. The year 1970 is also avoided as there were - * practical problems with it; an equivalent year is used for it too, - * which breaks some DST computations for 1970 right now, see e.g. - * test-bi-date-tzoffset-brute-fi.js. - */ - if ((flags & DUK_DATE_FLAG_EQUIVYEAR) && (year < 1971 || year > 2037)) { - DUK_ASSERT(is_leap == 0 || is_leap == 1); - - jan1_since_epoch = day_since_epoch - day_in_year; /* day number for Jan 1 since epoch */ - DUK_ASSERT(jan1_since_epoch + DUK__WEEKDAY_MOD_ADDER >= 0); - jan1_weekday = (jan1_since_epoch + 4 + DUK__WEEKDAY_MOD_ADDER) % 7; /* E5.1 Section 15.9.1.6 */ - DUK_ASSERT(jan1_weekday >= 0 && jan1_weekday <= 6); - arridx = jan1_weekday; - if (is_leap) { - arridx += 7; - } - DUK_ASSERT(arridx >= 0 && arridx < (duk_small_int_t) (sizeof(duk__date_equivyear) / sizeof(duk_uint8_t))); - - equiv_year = (duk_int_t) duk__date_equivyear[arridx] + 1970; - year = equiv_year; - DUK_DDD(DUK_DDDPRINT("equiv year mapping, year=%ld, day_in_year=%ld, day_since_epoch=%ld, " - "jan1_since_epoch=%ld, jan1_weekday=%ld -> equiv year %ld", - (long) year, (long) day_in_year, (long) day_since_epoch, - (long) jan1_since_epoch, (long) jan1_weekday, (long) equiv_year)); - } - - parts[DUK_DATE_IDX_YEAR] = year; - parts[DUK_DATE_IDX_MONTH] = month; - parts[DUK_DATE_IDX_DAY] = day; - - if (flags & DUK_DATE_FLAG_ONEBASED) { - parts[DUK_DATE_IDX_MONTH]++; /* zero-based -> one-based */ - parts[DUK_DATE_IDX_DAY]++; /* -""- */ - } - - if (dparts != NULL) { - for (i = 0; i < DUK_DATE_IDX_NUM_PARTS; i++) { - dparts[i] = (duk_double_t) parts[i]; - } - } -} - -/* Compute time value from (double) parts. The parts can be either UTC - * or local time; if local, they need to be (conceptually) converted into - * UTC time. The parts may represent valid or invalid time, and may be - * wildly out of range (but may cancel each other and still come out in - * the valid Date range). - */ -DUK_INTERNAL duk_double_t duk_bi_date_get_timeval_from_dparts(duk_double_t *dparts, duk_small_uint_t flags) { -#if defined(DUK_USE_PARANOID_DATE_COMPUTATION) - /* See comments below on MakeTime why these are volatile. */ - volatile duk_double_t tmp_time; - volatile duk_double_t tmp_day; - volatile duk_double_t d; -#else - duk_double_t tmp_time; - duk_double_t tmp_day; - duk_double_t d; -#endif - duk_small_uint_t i; - duk_int_t tzoff, tzoffprev1, tzoffprev2; - - /* Expects 'this' at top of stack on entry. */ - - /* Coerce all finite parts with ToInteger(). ToInteger() must not - * be called for NaN/Infinity because it will convert e.g. NaN to - * zero. If ToInteger() has already been called, this has no side - * effects and is idempotent. - * - * Don't read dparts[DUK_DATE_IDX_WEEKDAY]; it will cause Valgrind - * issues if the value is uninitialized. - */ - for (i = 0; i <= DUK_DATE_IDX_MILLISECOND; i++) { - /* SCANBUILD: scan-build complains here about assigned value - * being garbage or undefined. This is correct but operating - * on undefined values has no ill effect and is ignored by the - * caller in the case where this happens. - */ - d = dparts[i]; - if (DUK_ISFINITE(d)) { - dparts[i] = duk_js_tointeger_number(d); - } - } - - /* Use explicit steps in computation to try to ensure that - * computation happens with intermediate results coerced to - * double values (instead of using something more accurate). - * E.g. E5.1 Section 15.9.1.11 requires use of IEEE 754 - * rules (= Ecmascript '+' and '*' operators). - * - * Without 'volatile' even this approach fails on some platform - * and compiler combinations. For instance, gcc 4.8.1 on Ubuntu - * 64-bit, with -m32 and without -std=c99, test-bi-date-canceling.js - * would fail because of some optimizations when computing tmp_time - * (MakeTime below). Adding 'volatile' to tmp_time solved this - * particular problem (annoyingly, also adding debug prints or - * running the executable under valgrind hides it). - */ - - /* MakeTime */ - tmp_time = 0.0; - tmp_time += dparts[DUK_DATE_IDX_HOUR] * ((duk_double_t) DUK_DATE_MSEC_HOUR); - tmp_time += dparts[DUK_DATE_IDX_MINUTE] * ((duk_double_t) DUK_DATE_MSEC_MINUTE); - tmp_time += dparts[DUK_DATE_IDX_SECOND] * ((duk_double_t) DUK_DATE_MSEC_SECOND); - tmp_time += dparts[DUK_DATE_IDX_MILLISECOND]; - - /* MakeDay */ - tmp_day = duk__make_day(dparts[DUK_DATE_IDX_YEAR], dparts[DUK_DATE_IDX_MONTH], dparts[DUK_DATE_IDX_DAY]); - - /* MakeDate */ - d = tmp_day * ((duk_double_t) DUK_DATE_MSEC_DAY) + tmp_time; - - DUK_DDD(DUK_DDDPRINT("time=%lf day=%lf --> timeval=%lf", - (double) tmp_time, (double) tmp_day, (double) d)); - - /* Optional UTC conversion. */ - if (flags & DUK_DATE_FLAG_LOCALTIME) { - /* DUK_USE_DATE_GET_LOCAL_TZOFFSET() needs to be called with a - * time value computed from UTC parts. At this point we only - * have 'd' which is a time value computed from local parts, so - * it is off by the UTC-to-local time offset which we don't know - * yet. The current solution for computing the UTC-to-local - * time offset is to iterate a few times and detect a fixed - * point or a two-cycle loop (or a sanity iteration limit), - * see test-bi-date-local-parts.js and test-bi-date-tzoffset-basic-fi.js. - * - * E5.1 Section 15.9.1.9: - * UTC(t) = t - LocalTZA - DaylightSavingTA(t - LocalTZA) - * - * For NaN/inf, DUK_USE_DATE_GET_LOCAL_TZOFFSET() returns 0. - */ - -#if 0 - /* Old solution: don't iterate, incorrect */ - tzoff = DUK_USE_DATE_GET_LOCAL_TZOFFSET(d); - DUK_DDD(DUK_DDDPRINT("tzoffset w/o iteration, tzoff=%ld", (long) tzoff)); - d -= tzoff * 1000L; - DUK_UNREF(tzoffprev1); - DUK_UNREF(tzoffprev2); -#endif - - /* Iteration solution */ - tzoff = 0; - tzoffprev1 = 999999999L; /* invalid value which never matches */ - for (i = 0; i < DUK__LOCAL_TZOFFSET_MAXITER; i++) { - tzoffprev2 = tzoffprev1; - tzoffprev1 = tzoff; - tzoff = DUK_USE_DATE_GET_LOCAL_TZOFFSET(d - tzoff * 1000L); - DUK_DDD(DUK_DDDPRINT("tzoffset iteration, i=%d, tzoff=%ld, tzoffprev1=%ld tzoffprev2=%ld", - (int) i, (long) tzoff, (long) tzoffprev1, (long) tzoffprev2)); - if (tzoff == tzoffprev1) { - DUK_DDD(DUK_DDDPRINT("tzoffset iteration finished, i=%d, tzoff=%ld, tzoffprev1=%ld, tzoffprev2=%ld", - (int) i, (long) tzoff, (long) tzoffprev1, (long) tzoffprev2)); - break; - } else if (tzoff == tzoffprev2) { - /* Two value cycle, see e.g. test-bi-date-tzoffset-basic-fi.js. - * In these cases, favor a higher tzoffset to get a consistent - * result which is independent of iteration count. Not sure if - * this is a generically correct solution. - */ - DUK_DDD(DUK_DDDPRINT("tzoffset iteration two-value cycle, i=%d, tzoff=%ld, tzoffprev1=%ld, tzoffprev2=%ld", - (int) i, (long) tzoff, (long) tzoffprev1, (long) tzoffprev2)); - if (tzoffprev1 > tzoff) { - tzoff = tzoffprev1; - } - break; - } - } - DUK_DDD(DUK_DDDPRINT("tzoffset iteration, tzoff=%ld", (long) tzoff)); - d -= tzoff * 1000L; - } - - /* TimeClip(), which also handles Infinity -> NaN conversion */ - d = duk__timeclip(d); - - return d; -} - -/* - * API oriented helpers - */ - -/* Push 'this' binding, check that it is a Date object; then push the - * internal time value. At the end, stack is: [ ... this timeval ]. - * Returns the time value. Local time adjustment is done if requested. - */ -DUK_LOCAL duk_double_t duk__push_this_get_timeval_tzoffset(duk_context *ctx, duk_small_uint_t flags, duk_int_t *out_tzoffset) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_hobject *h; - duk_double_t d; - duk_int_t tzoffset = 0; - - duk_push_this(ctx); - h = duk_get_hobject(ctx, -1); /* XXX: getter with class check, useful in built-ins */ - if (h == NULL || DUK_HOBJECT_GET_CLASS_NUMBER(h) != DUK_HOBJECT_CLASS_DATE) { - DUK_ERROR_TYPE(thr, "expected Date"); - } - - duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_VALUE); - d = duk_to_number(ctx, -1); - duk_pop(ctx); - - if (DUK_ISNAN(d)) { - if (flags & DUK_DATE_FLAG_NAN_TO_ZERO) { - d = 0.0; - } - if (flags & DUK_DATE_FLAG_NAN_TO_RANGE_ERROR) { - DUK_ERROR_RANGE(thr, "Invalid Date"); - } - } - /* if no NaN handling flag, may still be NaN here, but not Inf */ - DUK_ASSERT(!DUK_ISINF(d)); - - if (flags & DUK_DATE_FLAG_LOCALTIME) { - /* Note: DST adjustment is determined using UTC time. - * If 'd' is NaN, tzoffset will be 0. - */ - tzoffset = DUK_USE_DATE_GET_LOCAL_TZOFFSET(d); /* seconds */ - d += tzoffset * 1000L; - } - if (out_tzoffset) { - *out_tzoffset = tzoffset; - } - - /* [ ... this ] */ - return d; -} - -DUK_LOCAL duk_double_t duk__push_this_get_timeval(duk_context *ctx, duk_small_uint_t flags) { - return duk__push_this_get_timeval_tzoffset(ctx, flags, NULL); -} - -/* Set timeval to 'this' from dparts, push the new time value onto the - * value stack and return 1 (caller can then tail call us). Expects - * the value stack to contain 'this' on the stack top. - */ -DUK_LOCAL duk_ret_t duk__set_this_timeval_from_dparts(duk_context *ctx, duk_double_t *dparts, duk_small_uint_t flags) { - duk_double_t d; - - /* [ ... this ] */ - - d = duk_bi_date_get_timeval_from_dparts(dparts, flags); - duk_push_number(ctx, d); /* -> [ ... this timeval_new ] */ - duk_dup_top(ctx); /* -> [ ... this timeval_new timeval_new ] */ - duk_put_prop_stridx(ctx, -3, DUK_STRIDX_INT_VALUE); - - /* stack top: new time value, return 1 to allow tail calls */ - return 1; -} - -/* 'out_buf' must be at least DUK_BI_DATE_ISO8601_BUFSIZE long. */ -DUK_LOCAL void duk__format_parts_iso8601(duk_int_t *parts, duk_int_t tzoffset, duk_small_uint_t flags, duk_uint8_t *out_buf) { - char yearstr[8]; /* "-123456\0" */ - char tzstr[8]; /* "+11:22\0" */ - char sep = (flags & DUK_DATE_FLAG_SEP_T) ? DUK_ASC_UC_T : DUK_ASC_SPACE; - - DUK_ASSERT(parts[DUK_DATE_IDX_MONTH] >= 1 && parts[DUK_DATE_IDX_MONTH] <= 12); - DUK_ASSERT(parts[DUK_DATE_IDX_DAY] >= 1 && parts[DUK_DATE_IDX_DAY] <= 31); - DUK_ASSERT(parts[DUK_DATE_IDX_YEAR] >= -999999 && parts[DUK_DATE_IDX_YEAR] <= 999999); - - /* Note: %06d for positive value, %07d for negative value to include - * sign and 6 digits. - */ - DUK_SNPRINTF(yearstr, - sizeof(yearstr), - (parts[DUK_DATE_IDX_YEAR] >= 0 && parts[DUK_DATE_IDX_YEAR] <= 9999) ? "%04ld" : - ((parts[DUK_DATE_IDX_YEAR] >= 0) ? "+%06ld" : "%07ld"), - (long) parts[DUK_DATE_IDX_YEAR]); - yearstr[sizeof(yearstr) - 1] = (char) 0; - - if (flags & DUK_DATE_FLAG_LOCALTIME) { - /* tzoffset seconds are dropped; 16 bits suffice for - * time offset in minutes - */ - if (tzoffset >= 0) { - duk_small_int_t tmp = tzoffset / 60; - DUK_SNPRINTF(tzstr, sizeof(tzstr), "+%02d:%02d", (int) (tmp / 60), (int) (tmp % 60)); - } else { - duk_small_int_t tmp = -tzoffset / 60; - DUK_SNPRINTF(tzstr, sizeof(tzstr), "-%02d:%02d", (int) (tmp / 60), (int) (tmp % 60)); - } - tzstr[sizeof(tzstr) - 1] = (char) 0; - } else { - tzstr[0] = DUK_ASC_UC_Z; - tzstr[1] = (char) 0; - } - - /* Unlike year, the other parts fit into 16 bits so %d format - * is portable. - */ - if ((flags & DUK_DATE_FLAG_TOSTRING_DATE) && (flags & DUK_DATE_FLAG_TOSTRING_TIME)) { - DUK_SPRINTF((char *) out_buf, "%s-%02d-%02d%c%02d:%02d:%02d.%03d%s", - (const char *) yearstr, (int) parts[DUK_DATE_IDX_MONTH], (int) parts[DUK_DATE_IDX_DAY], (int) sep, - (int) parts[DUK_DATE_IDX_HOUR], (int) parts[DUK_DATE_IDX_MINUTE], - (int) parts[DUK_DATE_IDX_SECOND], (int) parts[DUK_DATE_IDX_MILLISECOND], (const char *) tzstr); - } else if (flags & DUK_DATE_FLAG_TOSTRING_DATE) { - DUK_SPRINTF((char *) out_buf, "%s-%02d-%02d", - (const char *) yearstr, (int) parts[DUK_DATE_IDX_MONTH], (int) parts[DUK_DATE_IDX_DAY]); - } else { - DUK_ASSERT(flags & DUK_DATE_FLAG_TOSTRING_TIME); - DUK_SPRINTF((char *) out_buf, "%02d:%02d:%02d.%03d%s", - (int) parts[DUK_DATE_IDX_HOUR], (int) parts[DUK_DATE_IDX_MINUTE], - (int) parts[DUK_DATE_IDX_SECOND], (int) parts[DUK_DATE_IDX_MILLISECOND], - (const char *) tzstr); - } -} - -/* Helper for string conversion calls: check 'this' binding, get the - * internal time value, and format date and/or time in a few formats. - * Return value allows tail calls. - */ -DUK_LOCAL duk_ret_t duk__to_string_helper(duk_context *ctx, duk_small_uint_t flags) { - duk_double_t d; - duk_int_t parts[DUK_DATE_IDX_NUM_PARTS]; - duk_int_t tzoffset; /* seconds, doesn't fit into 16 bits */ - duk_bool_t rc; - duk_uint8_t buf[DUK_BI_DATE_ISO8601_BUFSIZE]; - - DUK_UNREF(rc); /* unreferenced with some options */ - - d = duk__push_this_get_timeval_tzoffset(ctx, flags, &tzoffset); - if (DUK_ISNAN(d)) { - duk_push_hstring_stridx(ctx, DUK_STRIDX_INVALID_DATE); - return 1; - } - DUK_ASSERT(DUK_ISFINITE(d)); - - /* formatters always get one-based month/day-of-month */ - duk_bi_date_timeval_to_parts(d, parts, NULL, DUK_DATE_FLAG_ONEBASED); - DUK_ASSERT(parts[DUK_DATE_IDX_MONTH] >= 1 && parts[DUK_DATE_IDX_MONTH] <= 12); - DUK_ASSERT(parts[DUK_DATE_IDX_DAY] >= 1 && parts[DUK_DATE_IDX_DAY] <= 31); - - if (flags & DUK_DATE_FLAG_TOSTRING_LOCALE) { - /* try locale specific formatter; if it refuses to format the - * string, fall back to an ISO 8601 formatted value in local - * time. - */ -#if defined(DUK_USE_DATE_FORMAT_STRING) - /* Contract, either: - * - Push string to value stack and return 1 - * - Don't push anything and return 0 - */ - - rc = DUK_USE_DATE_FORMAT_STRING(ctx, parts, tzoffset, flags); - if (rc != 0) { - return 1; - } -#else - /* No locale specific formatter; this is OK, we fall back - * to ISO 8601. - */ -#endif - } - - /* Different calling convention than above used because the helper - * is shared. - */ - duk__format_parts_iso8601(parts, tzoffset, flags, buf); - duk_push_string(ctx, (const char *) buf); - return 1; -} - -/* Helper for component getter calls: check 'this' binding, get the - * internal time value, split it into parts (either as UTC time or - * local time), push a specified component as a return value to the - * value stack and return 1 (caller can then tail call us). - */ -DUK_LOCAL duk_ret_t duk__get_part_helper(duk_context *ctx, duk_small_uint_t flags_and_idx) { - duk_double_t d; - duk_int_t parts[DUK_DATE_IDX_NUM_PARTS]; - duk_small_uint_t idx_part = (duk_small_uint_t) (flags_and_idx >> DUK_DATE_FLAG_VALUE_SHIFT); /* unpack args */ - - DUK_ASSERT_DISABLE(idx_part >= 0); /* unsigned */ - DUK_ASSERT(idx_part < DUK_DATE_IDX_NUM_PARTS); - - d = duk__push_this_get_timeval(ctx, flags_and_idx); - if (DUK_ISNAN(d)) { - duk_push_nan(ctx); - return 1; - } - DUK_ASSERT(DUK_ISFINITE(d)); - - duk_bi_date_timeval_to_parts(d, parts, NULL, flags_and_idx); /* no need to mask idx portion */ - - /* Setter APIs detect special year numbers (0...99) and apply a +1900 - * only in certain cases. The legacy getYear() getter applies -1900 - * unconditionally. - */ - duk_push_int(ctx, (flags_and_idx & DUK_DATE_FLAG_SUB1900) ? parts[idx_part] - 1900 : parts[idx_part]); - return 1; -} - -/* Helper for component setter calls: check 'this' binding, get the - * internal time value, split it into parts (either as UTC time or - * local time), modify one or more components as specified, recompute - * the time value, set it as the internal value. Finally, push the - * new time value as a return value to the value stack and return 1 - * (caller can then tail call us). - */ -DUK_LOCAL duk_ret_t duk__set_part_helper(duk_context *ctx, duk_small_uint_t flags_and_maxnargs) { - duk_double_t d; - duk_int_t parts[DUK_DATE_IDX_NUM_PARTS]; - duk_double_t dparts[DUK_DATE_IDX_NUM_PARTS]; - duk_idx_t nargs; - duk_small_uint_t maxnargs = (duk_small_uint_t) (flags_and_maxnargs >> DUK_DATE_FLAG_VALUE_SHIFT); /* unpack args */ - duk_small_uint_t idx_first, idx; - duk_small_uint_t i; - - nargs = duk_get_top(ctx); - d = duk__push_this_get_timeval(ctx, flags_and_maxnargs); - DUK_ASSERT(DUK_ISFINITE(d) || DUK_ISNAN(d)); - - if (DUK_ISFINITE(d)) { - duk_bi_date_timeval_to_parts(d, parts, dparts, flags_and_maxnargs); - } else { - /* NaN timevalue: we need to coerce the arguments, but - * the resulting internal timestamp needs to remain NaN. - * This works but is not pretty: parts and dparts will - * be partially uninitialized, but we only write to them. - */ - } - - /* - * Determining which datetime components to overwrite based on - * stack arguments is a bit complicated, but important to factor - * out from setters themselves for compactness. - * - * If DUK_DATE_FLAG_TIMESETTER, maxnargs indicates setter type: - * - * 1 -> millisecond - * 2 -> second, [millisecond] - * 3 -> minute, [second], [millisecond] - * 4 -> hour, [minute], [second], [millisecond] - * - * Else: - * - * 1 -> date - * 2 -> month, [date] - * 3 -> year, [month], [date] - * - * By comparing nargs and maxnargs (and flags) we know which - * components to override. We rely on part index ordering. - */ - - if (flags_and_maxnargs & DUK_DATE_FLAG_TIMESETTER) { - DUK_ASSERT(maxnargs >= 1 && maxnargs <= 4); - idx_first = DUK_DATE_IDX_MILLISECOND - (maxnargs - 1); - } else { - DUK_ASSERT(maxnargs >= 1 && maxnargs <= 3); - idx_first = DUK_DATE_IDX_DAY - (maxnargs - 1); - } - DUK_ASSERT_DISABLE(idx_first >= 0); /* unsigned */ - DUK_ASSERT(idx_first < DUK_DATE_IDX_NUM_PARTS); - - for (i = 0; i < maxnargs; i++) { - if ((duk_idx_t) i >= nargs) { - /* no argument given -> leave components untouched */ - break; - } - idx = idx_first + i; - DUK_ASSERT_DISABLE(idx >= 0); /* unsigned */ - DUK_ASSERT(idx < DUK_DATE_IDX_NUM_PARTS); - - if (idx == DUK_DATE_IDX_YEAR && (flags_and_maxnargs & DUK_DATE_FLAG_YEAR_FIXUP)) { - duk__twodigit_year_fixup(ctx, (duk_idx_t) i); - } - - dparts[idx] = duk_to_number(ctx, i); - - if (idx == DUK_DATE_IDX_DAY) { - /* Day-of-month is one-based in the API, but zero-based - * internally, so fix here. Note that month is zero-based - * both in the API and internally. - */ - /* SCANBUILD: complains about use of uninitialized values. - * The complaint is correct, but operating in undefined - * values here is intentional in some cases and the caller - * ignores the results. - */ - dparts[idx] -= 1.0; - } - } - - /* Leaves new timevalue on stack top and returns 1, which is correct - * for part setters. - */ - if (DUK_ISFINITE(d)) { - return duk__set_this_timeval_from_dparts(ctx, dparts, flags_and_maxnargs); - } else { - /* Internal timevalue is already NaN, so don't touch it. */ - duk_push_nan(ctx); - return 1; - } -} - -/* Apply ToNumber() to specified index; if ToInteger(val) in [0,99], add - * 1900 and replace value at idx_val. - */ -DUK_LOCAL void duk__twodigit_year_fixup(duk_context *ctx, duk_idx_t idx_val) { - duk_double_t d; - - /* XXX: idx_val would fit into 16 bits, but using duk_small_uint_t - * might not generate better code due to casting. - */ - - /* E5 Sections 15.9.3.1, B.2.4, B.2.5 */ - duk_to_number(ctx, idx_val); - if (duk_is_nan(ctx, idx_val)) { - return; - } - duk_dup(ctx, idx_val); - duk_to_int(ctx, -1); - d = duk_get_number(ctx, -1); /* get as double to handle huge numbers correctly */ - if (d >= 0.0 && d <= 99.0) { - d += 1900.0; - duk_push_number(ctx, d); - duk_replace(ctx, idx_val); - } - duk_pop(ctx); -} - -/* Set datetime parts from stack arguments, defaulting any missing values. - * Day-of-week is not set; it is not required when setting the time value. - */ -DUK_LOCAL void duk__set_parts_from_args(duk_context *ctx, duk_double_t *dparts, duk_idx_t nargs) { - duk_double_t d; - duk_small_uint_t i; - duk_small_uint_t idx; - - /* Causes a ToNumber() coercion, but doesn't break coercion order since - * year is coerced first anyway. - */ - duk__twodigit_year_fixup(ctx, 0); - - /* There are at most 7 args, but we use 8 here so that also - * DUK_DATE_IDX_WEEKDAY gets initialized (to zero) to avoid the potential - * for any Valgrind gripes later. - */ - for (i = 0; i < 8; i++) { - /* Note: rely on index ordering */ - idx = DUK_DATE_IDX_YEAR + i; - if ((duk_idx_t) i < nargs) { - d = duk_to_number(ctx, (duk_idx_t) i); - if (idx == DUK_DATE_IDX_DAY) { - /* Convert day from one-based to zero-based (internal). This may - * cause the day part to be negative, which is OK. - */ - d -= 1.0; - } - } else { - /* All components default to 0 except day-of-month which defaults - * to 1. However, because our internal day-of-month is zero-based, - * it also defaults to zero here. - */ - d = 0.0; - } - dparts[idx] = d; - } - - DUK_DDD(DUK_DDDPRINT("parts from args -> %lf %lf %lf %lf %lf %lf %lf %lf", - (double) dparts[0], (double) dparts[1], - (double) dparts[2], (double) dparts[3], - (double) dparts[4], (double) dparts[5], - (double) dparts[6], (double) dparts[7])); -} - -/* - * Helper to format a time value into caller buffer, used by logging. - * 'out_buf' must be at least DUK_BI_DATE_ISO8601_BUFSIZE long. - */ - -DUK_INTERNAL void duk_bi_date_format_timeval(duk_double_t timeval, duk_uint8_t *out_buf) { - duk_int_t parts[DUK_DATE_IDX_NUM_PARTS]; - - duk_bi_date_timeval_to_parts(timeval, - parts, - NULL, - DUK_DATE_FLAG_ONEBASED); - - duk__format_parts_iso8601(parts, - 0 /*tzoffset*/, - DUK_DATE_FLAG_TOSTRING_DATE | - DUK_DATE_FLAG_TOSTRING_TIME | - DUK_DATE_FLAG_SEP_T /*flags*/, - out_buf); -} - -/* - * Indirect magic value lookup for Date methods. - * - * Date methods don't put their control flags into the function magic value - * because they wouldn't fit into a LIGHTFUNC's magic field. Instead, the - * magic value is set to an index pointing to the array of control flags - * below. - * - * This must be kept in strict sync with genbuiltins.py! - */ - -static duk_uint16_t duk__date_magics[] = { - /* 0: toString */ - DUK_DATE_FLAG_TOSTRING_DATE + DUK_DATE_FLAG_TOSTRING_TIME + DUK_DATE_FLAG_LOCALTIME, - - /* 1: toDateString */ - DUK_DATE_FLAG_TOSTRING_DATE + DUK_DATE_FLAG_LOCALTIME, - - /* 2: toTimeString */ - DUK_DATE_FLAG_TOSTRING_TIME + DUK_DATE_FLAG_LOCALTIME, - - /* 3: toLocaleString */ - DUK_DATE_FLAG_TOSTRING_DATE + DUK_DATE_FLAG_TOSTRING_TIME + DUK_DATE_FLAG_TOSTRING_LOCALE + DUK_DATE_FLAG_LOCALTIME, - - /* 4: toLocaleDateString */ - DUK_DATE_FLAG_TOSTRING_DATE + DUK_DATE_FLAG_TOSTRING_LOCALE + DUK_DATE_FLAG_LOCALTIME, - - /* 5: toLocaleTimeString */ - DUK_DATE_FLAG_TOSTRING_TIME + DUK_DATE_FLAG_TOSTRING_LOCALE + DUK_DATE_FLAG_LOCALTIME, - - /* 6: toUTCString */ - DUK_DATE_FLAG_TOSTRING_DATE + DUK_DATE_FLAG_TOSTRING_TIME, - - /* 7: toISOString */ - DUK_DATE_FLAG_TOSTRING_DATE + DUK_DATE_FLAG_TOSTRING_TIME + DUK_DATE_FLAG_NAN_TO_RANGE_ERROR + DUK_DATE_FLAG_SEP_T, - - /* 8: getFullYear */ - DUK_DATE_FLAG_LOCALTIME + (DUK_DATE_IDX_YEAR << DUK_DATE_FLAG_VALUE_SHIFT), - - /* 9: getUTCFullYear */ - 0 + (DUK_DATE_IDX_YEAR << DUK_DATE_FLAG_VALUE_SHIFT), - - /* 10: getMonth */ - DUK_DATE_FLAG_LOCALTIME + (DUK_DATE_IDX_MONTH << DUK_DATE_FLAG_VALUE_SHIFT), - - /* 11: getUTCMonth */ - 0 + (DUK_DATE_IDX_MONTH << DUK_DATE_FLAG_VALUE_SHIFT), - - /* 12: getDate */ - DUK_DATE_FLAG_ONEBASED + DUK_DATE_FLAG_LOCALTIME + (DUK_DATE_IDX_DAY << DUK_DATE_FLAG_VALUE_SHIFT), - - /* 13: getUTCDate */ - DUK_DATE_FLAG_ONEBASED + (DUK_DATE_IDX_DAY << DUK_DATE_FLAG_VALUE_SHIFT), - - /* 14: getDay */ - DUK_DATE_FLAG_LOCALTIME + (DUK_DATE_IDX_WEEKDAY << DUK_DATE_FLAG_VALUE_SHIFT), - - /* 15: getUTCDay */ - 0 + (DUK_DATE_IDX_WEEKDAY << DUK_DATE_FLAG_VALUE_SHIFT), - - /* 16: getHours */ - DUK_DATE_FLAG_LOCALTIME + (DUK_DATE_IDX_HOUR << DUK_DATE_FLAG_VALUE_SHIFT), - - /* 17: getUTCHours */ - 0 + (DUK_DATE_IDX_HOUR << DUK_DATE_FLAG_VALUE_SHIFT), - - /* 18: getMinutes */ - DUK_DATE_FLAG_LOCALTIME + (DUK_DATE_IDX_MINUTE << DUK_DATE_FLAG_VALUE_SHIFT), - - /* 19: getUTCMinutes */ - 0 + (DUK_DATE_IDX_MINUTE << DUK_DATE_FLAG_VALUE_SHIFT), - - /* 20: getSeconds */ - DUK_DATE_FLAG_LOCALTIME + (DUK_DATE_IDX_SECOND << DUK_DATE_FLAG_VALUE_SHIFT), - - /* 21: getUTCSeconds */ - 0 + (DUK_DATE_IDX_SECOND << DUK_DATE_FLAG_VALUE_SHIFT), - - /* 22: getMilliseconds */ - DUK_DATE_FLAG_LOCALTIME + (DUK_DATE_IDX_MILLISECOND << DUK_DATE_FLAG_VALUE_SHIFT), - - /* 23: getUTCMilliseconds */ - 0 + (DUK_DATE_IDX_MILLISECOND << DUK_DATE_FLAG_VALUE_SHIFT), - - /* 24: setMilliseconds */ - DUK_DATE_FLAG_TIMESETTER + DUK_DATE_FLAG_LOCALTIME + (1 << DUK_DATE_FLAG_VALUE_SHIFT), - - /* 25: setUTCMilliseconds */ - DUK_DATE_FLAG_TIMESETTER + (1 << DUK_DATE_FLAG_VALUE_SHIFT), - - /* 26: setSeconds */ - DUK_DATE_FLAG_TIMESETTER + DUK_DATE_FLAG_LOCALTIME + (2 << DUK_DATE_FLAG_VALUE_SHIFT), - - /* 27: setUTCSeconds */ - DUK_DATE_FLAG_TIMESETTER + (2 << DUK_DATE_FLAG_VALUE_SHIFT), - - /* 28: setMinutes */ - DUK_DATE_FLAG_TIMESETTER + DUK_DATE_FLAG_LOCALTIME + (3 << DUK_DATE_FLAG_VALUE_SHIFT), - - /* 29: setUTCMinutes */ - DUK_DATE_FLAG_TIMESETTER + (3 << DUK_DATE_FLAG_VALUE_SHIFT), - - /* 30: setHours */ - DUK_DATE_FLAG_TIMESETTER + DUK_DATE_FLAG_LOCALTIME + (4 << DUK_DATE_FLAG_VALUE_SHIFT), - - /* 31: setUTCHours */ - DUK_DATE_FLAG_TIMESETTER + (4 << DUK_DATE_FLAG_VALUE_SHIFT), - - /* 32: setDate */ - DUK_DATE_FLAG_LOCALTIME + (1 << DUK_DATE_FLAG_VALUE_SHIFT), - - /* 33: setUTCDate */ - 0 + (1 << DUK_DATE_FLAG_VALUE_SHIFT), - - /* 34: setMonth */ - DUK_DATE_FLAG_LOCALTIME + (2 << DUK_DATE_FLAG_VALUE_SHIFT), - - /* 35: setUTCMonth */ - 0 + (2 << DUK_DATE_FLAG_VALUE_SHIFT), - - /* 36: setFullYear */ - DUK_DATE_FLAG_NAN_TO_ZERO + DUK_DATE_FLAG_LOCALTIME + (3 << DUK_DATE_FLAG_VALUE_SHIFT), - - /* 37: setUTCFullYear */ - DUK_DATE_FLAG_NAN_TO_ZERO + (3 << DUK_DATE_FLAG_VALUE_SHIFT), - - /* 38: getYear */ - DUK_DATE_FLAG_LOCALTIME + DUK_DATE_FLAG_SUB1900 + (DUK_DATE_IDX_YEAR << DUK_DATE_FLAG_VALUE_SHIFT), - - /* 39: setYear */ - DUK_DATE_FLAG_NAN_TO_ZERO + DUK_DATE_FLAG_YEAR_FIXUP + (3 << DUK_DATE_FLAG_VALUE_SHIFT), -}; - -DUK_LOCAL duk_small_uint_t duk__date_get_indirect_magic(duk_context *ctx) { - duk_small_int_t magicidx = (duk_small_uint_t) duk_get_current_magic(ctx); - DUK_ASSERT(magicidx >= 0 && magicidx < (duk_small_int_t) (sizeof(duk__date_magics) / sizeof(duk_uint16_t))); - return (duk_small_uint_t) duk__date_magics[magicidx]; -} - -/* - * Constructor calls - */ - -DUK_INTERNAL duk_ret_t duk_bi_date_constructor(duk_context *ctx) { - duk_idx_t nargs = duk_get_top(ctx); - duk_bool_t is_cons = duk_is_constructor_call(ctx); - duk_double_t dparts[DUK_DATE_IDX_NUM_PARTS]; - duk_double_t d; - - DUK_DDD(DUK_DDDPRINT("Date constructor, nargs=%ld, is_cons=%ld", (long) nargs, (long) is_cons)); - - duk_push_object_helper(ctx, - DUK_HOBJECT_FLAG_EXTENSIBLE | - DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_DATE), - DUK_BIDX_DATE_PROTOTYPE); - - /* Unlike most built-ins, the internal [[PrimitiveValue]] of a Date - * is mutable. - */ - - if (nargs == 0 || !is_cons) { - d = duk__timeclip(DUK_USE_DATE_GET_NOW(ctx)); - duk_push_number(ctx, d); - duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_INT_VALUE, DUK_PROPDESC_FLAGS_W); - if (!is_cons) { - /* called as a normal function: return new Date().toString() */ - duk_to_string(ctx, -1); - } - return 1; - } else if (nargs == 1) { - duk_to_primitive(ctx, 0, DUK_HINT_NONE); - if (duk_is_string(ctx, 0)) { - duk__parse_string(ctx, duk_to_string(ctx, 0)); - duk_replace(ctx, 0); /* may be NaN */ - } - d = duk__timeclip(duk_to_number(ctx, 0)); - duk_push_number(ctx, d); - duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_INT_VALUE, DUK_PROPDESC_FLAGS_W); - return 1; - } - - duk__set_parts_from_args(ctx, dparts, nargs); - - /* Parts are in local time, convert when setting. */ - - (void) duk__set_this_timeval_from_dparts(ctx, dparts, DUK_DATE_FLAG_LOCALTIME /*flags*/); /* -> [ ... this timeval ] */ - duk_pop(ctx); /* -> [ ... this ] */ - return 1; -} - -DUK_INTERNAL duk_ret_t duk_bi_date_constructor_parse(duk_context *ctx) { - return duk__parse_string(ctx, duk_to_string(ctx, 0)); -} - -DUK_INTERNAL duk_ret_t duk_bi_date_constructor_utc(duk_context *ctx) { - duk_idx_t nargs = duk_get_top(ctx); - duk_double_t dparts[DUK_DATE_IDX_NUM_PARTS]; - duk_double_t d; - - /* Behavior for nargs < 2 is implementation dependent: currently we'll - * set a NaN time value (matching V8 behavior) in this case. - */ - - if (nargs < 2) { - duk_push_nan(ctx); - } else { - duk__set_parts_from_args(ctx, dparts, nargs); - d = duk_bi_date_get_timeval_from_dparts(dparts, 0 /*flags*/); - duk_push_number(ctx, d); - } - return 1; -} - -DUK_INTERNAL duk_ret_t duk_bi_date_constructor_now(duk_context *ctx) { - duk_double_t d; - - d = DUK_USE_DATE_GET_NOW(ctx); - DUK_ASSERT(duk__timeclip(d) == d); /* TimeClip() should never be necessary */ - duk_push_number(ctx, d); - return 1; -} - -/* - * String/JSON conversions - * - * Human readable conversions are now basically ISO 8601 with a space - * (instead of 'T') as the date/time separator. This is a good baseline - * and is platform independent. - * - * A shared native helper to provide many conversions. Magic value contains - * a set of flags. The helper provides: - * - * toString() - * toDateString() - * toTimeString() - * toLocaleString() - * toLocaleDateString() - * toLocaleTimeString() - * toUTCString() - * toISOString() - * - * Notes: - * - * - Date.prototype.toGMTString() and Date.prototype.toUTCString() are - * required to be the same Ecmascript function object (!), so it is - * omitted from here. - * - * - Date.prototype.toUTCString(): E5.1 specification does not require a - * specific format, but result should be human readable. The - * specification suggests using ISO 8601 format with a space (instead - * of 'T') separator if a more human readable format is not available. - * - * - Date.prototype.toISOString(): unlike other conversion functions, - * toISOString() requires a RangeError for invalid date values. - */ - -DUK_INTERNAL duk_ret_t duk_bi_date_prototype_tostring_shared(duk_context *ctx) { - duk_small_uint_t flags = duk__date_get_indirect_magic(ctx); - return duk__to_string_helper(ctx, flags); -} - -DUK_INTERNAL duk_ret_t duk_bi_date_prototype_value_of(duk_context *ctx) { - /* This native function is also used for Date.prototype.getTime() - * as their behavior is identical. - */ - - duk_double_t d = duk__push_this_get_timeval(ctx, 0 /*flags*/); /* -> [ this ] */ - DUK_ASSERT(DUK_ISFINITE(d) || DUK_ISNAN(d)); - duk_push_number(ctx, d); - return 1; -} - -DUK_INTERNAL duk_ret_t duk_bi_date_prototype_to_json(duk_context *ctx) { - /* Note: toJSON() is a generic function which works even if 'this' - * is not a Date. The sole argument is ignored. - */ - - duk_push_this(ctx); - duk_to_object(ctx, -1); - - duk_dup_top(ctx); - duk_to_primitive(ctx, -1, DUK_HINT_NUMBER); - if (duk_is_number(ctx, -1)) { - duk_double_t d = duk_get_number(ctx, -1); - if (!DUK_ISFINITE(d)) { - duk_push_null(ctx); - return 1; - } - } - duk_pop(ctx); - - duk_get_prop_stridx(ctx, -1, DUK_STRIDX_TO_ISO_STRING); - duk_dup(ctx, -2); /* -> [ O toIsoString O ] */ - duk_call_method(ctx, 0); - return 1; -} - -/* - * Getters. - * - * Implementing getters is quite easy. The internal time value is either - * NaN, or represents milliseconds (without fractions) from Jan 1, 1970. - * The internal time value can be converted to integer parts, and each - * part will be normalized and will fit into a 32-bit signed integer. - * - * A shared native helper to provide all getters. Magic value contains - * a set of flags and also packs the date component index argument. The - * helper provides: - * - * getFullYear() - * getUTCFullYear() - * getMonth() - * getUTCMonth() - * getDate() - * getUTCDate() - * getDay() - * getUTCDay() - * getHours() - * getUTCHours() - * getMinutes() - * getUTCMinutes() - * getSeconds() - * getUTCSeconds() - * getMilliseconds() - * getUTCMilliseconds() - * getYear() - * - * Notes: - * - * - Date.prototype.getDate(): 'date' means day-of-month, and is - * zero-based in internal calculations but public API expects it to - * be one-based. - * - * - Date.prototype.getTime() and Date.prototype.valueOf() have identical - * behavior. They have separate function objects, but share the same C - * function (duk_bi_date_prototype_value_of). - */ - -DUK_INTERNAL duk_ret_t duk_bi_date_prototype_get_shared(duk_context *ctx) { - duk_small_uint_t flags_and_idx = duk__date_get_indirect_magic(ctx); - return duk__get_part_helper(ctx, flags_and_idx); -} - -DUK_INTERNAL duk_ret_t duk_bi_date_prototype_get_timezone_offset(duk_context *ctx) { - /* - * Return (t - LocalTime(t)) in minutes: - * - * t - LocalTime(t) = t - (t + LocalTZA + DaylightSavingTA(t)) - * = -(LocalTZA + DaylightSavingTA(t)) - * - * where DaylightSavingTA() is checked for time 't'. - * - * Note that the sign of the result is opposite to common usage, - * e.g. for EE(S)T which normally is +2h or +3h from UTC, this - * function returns -120 or -180. - * - */ - - duk_double_t d; - duk_int_t tzoffset; - - /* Note: DST adjustment is determined using UTC time. */ - d = duk__push_this_get_timeval(ctx, 0 /*flags*/); - DUK_ASSERT(DUK_ISFINITE(d) || DUK_ISNAN(d)); - if (DUK_ISNAN(d)) { - duk_push_nan(ctx); - } else { - DUK_ASSERT(DUK_ISFINITE(d)); - tzoffset = DUK_USE_DATE_GET_LOCAL_TZOFFSET(d); - duk_push_int(ctx, -tzoffset / 60); - } - return 1; -} - -/* - * Setters. - * - * Setters are a bit more complicated than getters. Component setters - * break down the current time value into its (normalized) component - * parts, replace one or more components with -unnormalized- new values, - * and the components are then converted back into a time value. As an - * example of using unnormalized values: - * - * var d = new Date(1234567890); - * - * is equivalent to: - * - * var d = new Date(0); - * d.setUTCMilliseconds(1234567890); - * - * A shared native helper to provide almost all setters. Magic value - * contains a set of flags and also packs the "maxnargs" argument. The - * helper provides: - * - * setMilliseconds() - * setUTCMilliseconds() - * setSeconds() - * setUTCSeconds() - * setMinutes() - * setUTCMinutes() - * setHours() - * setUTCHours() - * setDate() - * setUTCDate() - * setMonth() - * setUTCMonth() - * setFullYear() - * setUTCFullYear() - * setYear() - * - * Notes: - * - * - Date.prototype.setYear() (Section B addition): special year check - * is omitted. NaN / Infinity will just flow through and ultimately - * result in a NaN internal time value. - * - * - Date.prototype.setYear() does not have optional arguments for - * setting month and day-in-month (like setFullYear()), but we indicate - * 'maxnargs' to be 3 to get the year written to the correct component - * index in duk__set_part_helper(). The function has nargs == 1, so only - * the year will be set regardless of actual argument count. - */ - -DUK_INTERNAL duk_ret_t duk_bi_date_prototype_set_shared(duk_context *ctx) { - duk_small_uint_t flags_and_maxnargs = duk__date_get_indirect_magic(ctx); - return duk__set_part_helper(ctx, flags_and_maxnargs); -} - -DUK_INTERNAL duk_ret_t duk_bi_date_prototype_set_time(duk_context *ctx) { - duk_double_t d; - - (void) duk__push_this_get_timeval(ctx, 0 /*flags*/); /* -> [ timeval this ] */ - d = duk__timeclip(duk_to_number(ctx, 0)); - duk_push_number(ctx, d); - duk_dup_top(ctx); - duk_put_prop_stridx(ctx, -3, DUK_STRIDX_INT_VALUE); /* -> [ timeval this timeval ] */ - - return 1; -} -#line 1 "duk_bi_date_unix.c" -/* - * Unix-like Date providers - * - * Generally useful Unix / POSIX / ANSI Date providers. - */ - -/* include removed: duk_internal.h */ - -/* The necessary #includes are in place in duk_config.h. */ - -/* Buffer sizes for some UNIX calls. Larger than strictly necessary - * to avoid Valgrind errors. - */ -#define DUK__STRPTIME_BUF_SIZE 64 -#define DUK__STRFTIME_BUF_SIZE 64 - -#if defined(DUK_USE_DATE_NOW_GETTIMEOFDAY) -/* Get current Ecmascript time (= UNIX/Posix time, but in milliseconds). */ -DUK_INTERNAL duk_double_t duk_bi_date_get_now_gettimeofday(duk_context *ctx) { - duk_hthread *thr = (duk_hthread *) ctx; - struct timeval tv; - duk_double_t d; - - if (gettimeofday(&tv, NULL) != 0) { - DUK_ERROR_INTERNAL_DEFMSG(thr); - } - - d = ((duk_double_t) tv.tv_sec) * 1000.0 + - ((duk_double_t) (tv.tv_usec / 1000)); - DUK_ASSERT(DUK_FLOOR(d) == d); /* no fractions */ - - return d; -} -#endif /* DUK_USE_DATE_NOW_GETTIMEOFDAY */ - -#if defined(DUK_USE_DATE_NOW_TIME) -/* Not a very good provider: only full seconds are available. */ -DUK_INTERNAL duk_double_t duk_bi_date_get_now_time(duk_context *ctx) { - time_t t; - - DUK_UNREF(ctx); - t = time(NULL); - return ((duk_double_t) t) * 1000.0; -} -#endif /* DUK_USE_DATE_NOW_TIME */ - -#if defined(DUK_USE_DATE_TZO_GMTIME) || defined(DUK_USE_DATE_TZO_GMTIME_R) -/* Get local time offset (in seconds) for a certain (UTC) instant 'd'. */ -DUK_INTERNAL duk_int_t duk_bi_date_get_local_tzoffset_gmtime(duk_double_t d) { - time_t t, t1, t2; - duk_int_t parts[DUK_DATE_IDX_NUM_PARTS]; - duk_double_t dparts[DUK_DATE_IDX_NUM_PARTS]; - struct tm tms[2]; -#ifdef DUK_USE_DATE_TZO_GMTIME - struct tm *tm_ptr; -#endif - - /* For NaN/inf, the return value doesn't matter. */ - if (!DUK_ISFINITE(d)) { - return 0; - } - - /* If not within Ecmascript range, some integer time calculations - * won't work correctly (and some asserts will fail), so bail out - * if so. This fixes test-bug-date-insane-setyear.js. There is - * a +/- 24h leeway in this range check to avoid a test262 corner - * case documented in test-bug-date-timeval-edges.js. - */ - if (!duk_bi_date_timeval_in_leeway_range(d)) { - DUK_DD(DUK_DDPRINT("timeval not within valid range, skip tzoffset computation to avoid integer overflows")); - return 0; - } - - /* - * This is a bit tricky to implement portably. The result depends - * on the timestamp (specifically, DST depends on the timestamp). - * If e.g. UNIX APIs are used, they'll have portability issues with - * very small and very large years. - * - * Current approach: - * - * - Stay within portable UNIX limits by using equivalent year mapping. - * Avoid year 1970 and 2038 as some conversions start to fail, at - * least on some platforms. Avoiding 1970 means that there are - * currently DST discrepancies for 1970. - * - * - Create a UTC and local time breakdowns from 't'. Then create - * a time_t using gmtime() and localtime() and compute the time - * difference between the two. - * - * Equivalent year mapping (E5 Section 15.9.1.8): - * - * If the host environment provides functionality for determining - * daylight saving time, the implementation of ECMAScript is free - * to map the year in question to an equivalent year (same - * leap-year-ness and same starting week day for the year) for which - * the host environment provides daylight saving time information. - * The only restriction is that all equivalent years should produce - * the same result. - * - * This approach is quite reasonable but not entirely correct, e.g. - * the specification also states (E5 Section 15.9.1.8): - * - * The implementation of ECMAScript should not try to determine - * whether the exact time was subject to daylight saving time, but - * just whether daylight saving time would have been in effect if - * the _current daylight saving time algorithm_ had been used at the - * time. This avoids complications such as taking into account the - * years that the locale observed daylight saving time year round. - * - * Since we rely on the platform APIs for conversions between local - * time and UTC, we can't guarantee the above. Rather, if the platform - * has historical DST rules they will be applied. This seems to be the - * general preferred direction in Ecmascript standardization (or at least - * implementations) anyway, and even the equivalent year mapping should - * be disabled if the platform is known to handle DST properly for the - * full Ecmascript range. - * - * The following has useful discussion and links: - * - * https://bugzilla.mozilla.org/show_bug.cgi?id=351066 - */ - - duk_bi_date_timeval_to_parts(d, parts, dparts, DUK_DATE_FLAG_EQUIVYEAR /*flags*/); - DUK_ASSERT(parts[DUK_DATE_IDX_YEAR] >= 1970 && parts[DUK_DATE_IDX_YEAR] <= 2038); - - d = duk_bi_date_get_timeval_from_dparts(dparts, 0 /*flags*/); - DUK_ASSERT(d >= 0 && d < 2147483648.0 * 1000.0); /* unsigned 31-bit range */ - t = (time_t) (d / 1000.0); - DUK_DDD(DUK_DDDPRINT("timeval: %lf -> time_t %ld", (double) d, (long) t)); - - DUK_MEMZERO((void *) tms, sizeof(struct tm) * 2); - -#if defined(DUK_USE_DATE_TZO_GMTIME_R) - (void) gmtime_r(&t, &tms[0]); - (void) localtime_r(&t, &tms[1]); -#elif defined(DUK_USE_DATE_TZO_GMTIME) - tm_ptr = gmtime(&t); - DUK_MEMCPY((void *) &tms[0], tm_ptr, sizeof(struct tm)); - tm_ptr = localtime(&t); - DUK_MEMCPY((void *) &tms[1], tm_ptr, sizeof(struct tm)); -#else -#error internal error -#endif - DUK_DDD(DUK_DDDPRINT("gmtime result: tm={sec:%ld,min:%ld,hour:%ld,mday:%ld,mon:%ld,year:%ld," - "wday:%ld,yday:%ld,isdst:%ld}", - (long) tms[0].tm_sec, (long) tms[0].tm_min, (long) tms[0].tm_hour, - (long) tms[0].tm_mday, (long) tms[0].tm_mon, (long) tms[0].tm_year, - (long) tms[0].tm_wday, (long) tms[0].tm_yday, (long) tms[0].tm_isdst)); - DUK_DDD(DUK_DDDPRINT("localtime result: tm={sec:%ld,min:%ld,hour:%ld,mday:%ld,mon:%ld,year:%ld," - "wday:%ld,yday:%ld,isdst:%ld}", - (long) tms[1].tm_sec, (long) tms[1].tm_min, (long) tms[1].tm_hour, - (long) tms[1].tm_mday, (long) tms[1].tm_mon, (long) tms[1].tm_year, - (long) tms[1].tm_wday, (long) tms[1].tm_yday, (long) tms[1].tm_isdst)); - - /* tm_isdst is both an input and an output to mktime(), use 0 to - * avoid DST handling in mktime(): - * - https://github.com/svaarala/duktape/issues/406 - * - http://stackoverflow.com/questions/8558919/mktime-and-tm-isdst - */ - tms[0].tm_isdst = 0; - tms[1].tm_isdst = 0; - t1 = mktime(&tms[0]); /* UTC */ - t2 = mktime(&tms[1]); /* local */ - if (t1 == (time_t) -1 || t2 == (time_t) -1) { - /* This check used to be for (t < 0) but on some platforms - * time_t is unsigned and apparently the proper way to detect - * an mktime() error return is the cast above. See e.g.: - * http://pubs.opengroup.org/onlinepubs/009695299/functions/mktime.html - */ - goto error; - } - DUK_DDD(DUK_DDDPRINT("t1=%ld (utc), t2=%ld (local)", (long) t1, (long) t2)); - - /* Compute final offset in seconds, positive if local time ahead of - * UTC (returned value is UTC-to-local offset). - * - * difftime() returns a double, so coercion to int generates quite - * a lot of code. Direct subtraction is not portable, however. - * XXX: allow direct subtraction on known platforms. - */ -#if 0 - return (duk_int_t) (t2 - t1); -#endif - return (duk_int_t) difftime(t2, t1); - - error: - /* XXX: return something more useful, so that caller can throw? */ - DUK_D(DUK_DPRINT("mktime() failed, d=%lf", (double) d)); - return 0; -} -#endif /* DUK_USE_DATE_TZO_GMTIME */ - -#if defined(DUK_USE_DATE_PRS_STRPTIME) -DUK_INTERNAL duk_bool_t duk_bi_date_parse_string_strptime(duk_context *ctx, const char *str) { - struct tm tm; - time_t t; - char buf[DUK__STRPTIME_BUF_SIZE]; - - /* copy to buffer with spare to avoid Valgrind gripes from strptime */ - DUK_ASSERT(str != NULL); - DUK_MEMZERO(buf, sizeof(buf)); /* valgrind whine without this */ - DUK_SNPRINTF(buf, sizeof(buf), "%s", (const char *) str); - buf[sizeof(buf) - 1] = (char) 0; - - DUK_DDD(DUK_DDDPRINT("parsing: '%s'", (const char *) buf)); - - DUK_MEMZERO(&tm, sizeof(tm)); - if (strptime((const char *) buf, "%c", &tm) != NULL) { - DUK_DDD(DUK_DDDPRINT("before mktime: tm={sec:%ld,min:%ld,hour:%ld,mday:%ld,mon:%ld,year:%ld," - "wday:%ld,yday:%ld,isdst:%ld}", - (long) tm.tm_sec, (long) tm.tm_min, (long) tm.tm_hour, - (long) tm.tm_mday, (long) tm.tm_mon, (long) tm.tm_year, - (long) tm.tm_wday, (long) tm.tm_yday, (long) tm.tm_isdst)); - tm.tm_isdst = -1; /* negative: dst info not available */ - - t = mktime(&tm); - DUK_DDD(DUK_DDDPRINT("mktime() -> %ld", (long) t)); - if (t >= 0) { - duk_push_number(ctx, ((duk_double_t) t) * 1000.0); - return 1; - } - } - - return 0; -} -#endif /* DUK_USE_DATE_PRS_STRPTIME */ - -#if defined(DUK_USE_DATE_PRS_GETDATE) -DUK_INTERNAL duk_bool_t duk_bi_date_parse_string_getdate(duk_context *ctx, const char *str) { - struct tm tm; - duk_small_int_t rc; - time_t t; - - /* For this to work, DATEMSK must be set, so this is not very - * convenient for an embeddable interpreter. - */ - - DUK_MEMZERO(&tm, sizeof(struct tm)); - rc = (duk_small_int_t) getdate_r(str, &tm); - DUK_DDD(DUK_DDDPRINT("getdate_r() -> %ld", (long) rc)); - - if (rc == 0) { - t = mktime(&tm); - DUK_DDD(DUK_DDDPRINT("mktime() -> %ld", (long) t)); - if (t >= 0) { - duk_push_number(ctx, (duk_double_t) t); - return 1; - } - } - - return 0; -} -#endif /* DUK_USE_DATE_PRS_GETDATE */ - -#if defined(DUK_USE_DATE_FMT_STRFTIME) -DUK_INTERNAL duk_bool_t duk_bi_date_format_parts_strftime(duk_context *ctx, duk_int_t *parts, duk_int_t tzoffset, duk_small_uint_t flags) { - char buf[DUK__STRFTIME_BUF_SIZE]; - struct tm tm; - const char *fmt; - - DUK_UNREF(tzoffset); - - /* If the platform doesn't support the entire Ecmascript range, we need - * to return 0 so that the caller can fall back to the default formatter. - * - * For now, assume that if time_t is 8 bytes or more, the whole Ecmascript - * range is supported. For smaller time_t values (4 bytes in practice), - * assumes that the signed 32-bit range is supported. - * - * XXX: detect this more correctly per platform. The size of time_t is - * probably not an accurate guarantee of strftime() supporting or not - * supporting a large time range (the full Ecmascript range). - */ - if (sizeof(time_t) < 8 && - (parts[DUK_DATE_IDX_YEAR] < 1970 || parts[DUK_DATE_IDX_YEAR] > 2037)) { - /* be paranoid for 32-bit time values (even avoiding negative ones) */ - return 0; - } - - DUK_MEMZERO(&tm, sizeof(tm)); - tm.tm_sec = parts[DUK_DATE_IDX_SECOND]; - tm.tm_min = parts[DUK_DATE_IDX_MINUTE]; - tm.tm_hour = parts[DUK_DATE_IDX_HOUR]; - tm.tm_mday = parts[DUK_DATE_IDX_DAY]; /* already one-based */ - tm.tm_mon = parts[DUK_DATE_IDX_MONTH] - 1; /* one-based -> zero-based */ - tm.tm_year = parts[DUK_DATE_IDX_YEAR] - 1900; - tm.tm_wday = parts[DUK_DATE_IDX_WEEKDAY]; - tm.tm_isdst = 0; - - DUK_MEMZERO(buf, sizeof(buf)); - if ((flags & DUK_DATE_FLAG_TOSTRING_DATE) && (flags & DUK_DATE_FLAG_TOSTRING_TIME)) { - fmt = "%c"; - } else if (flags & DUK_DATE_FLAG_TOSTRING_DATE) { - fmt = "%x"; - } else { - DUK_ASSERT(flags & DUK_DATE_FLAG_TOSTRING_TIME); - fmt = "%X"; - } - (void) strftime(buf, sizeof(buf) - 1, fmt, &tm); - DUK_ASSERT(buf[sizeof(buf) - 1] == 0); - - duk_push_string(ctx, buf); - return 1; -} -#endif /* DUK_USE_DATE_FMT_STRFTIME */ - -#undef DUK__STRPTIME_BUF_SIZE -#undef DUK__STRFTIME_BUF_SIZE -#line 1 "duk_bi_date_windows.c" -/* - * Windows Date providers - * - * Platform specific links: - * - * - http://msdn.microsoft.com/en-us/library/windows/desktop/ms725473(v=vs.85).aspx - */ - -/* include removed: duk_internal.h */ - -/* The necessary #includes are in place in duk_config.h. */ - -#if defined(DUK_USE_DATE_NOW_WINDOWS) || defined(DUK_USE_DATE_TZO_WINDOWS) -/* Shared Windows helpers. */ -DUK_LOCAL void duk__convert_systime_to_ularge(const SYSTEMTIME *st, ULARGE_INTEGER *res) { - FILETIME ft; - if (SystemTimeToFileTime(st, &ft) == 0) { - DUK_D(DUK_DPRINT("SystemTimeToFileTime() failed, returning 0")); - res->QuadPart = 0; - } else { - res->LowPart = ft.dwLowDateTime; - res->HighPart = ft.dwHighDateTime; - } -} -DUK_LOCAL void duk__set_systime_jan1970(SYSTEMTIME *st) { - DUK_MEMZERO((void *) st, sizeof(*st)); - st->wYear = 1970; - st->wMonth = 1; - st->wDayOfWeek = 4; /* not sure whether or not needed; Thursday */ - st->wDay = 1; - DUK_ASSERT(st->wHour == 0); - DUK_ASSERT(st->wMinute == 0); - DUK_ASSERT(st->wSecond == 0); - DUK_ASSERT(st->wMilliseconds == 0); -} -#endif /* defined(DUK_USE_DATE_NOW_WINDOWS) || defined(DUK_USE_DATE_TZO_WINDOWS) */ - -#ifdef DUK_USE_DATE_NOW_WINDOWS -DUK_INTERNAL duk_double_t duk_bi_date_get_now_windows(duk_context *ctx) { - /* Suggested step-by-step method from documentation of RtlTimeToSecondsSince1970: - * http://msdn.microsoft.com/en-us/library/windows/desktop/ms724928(v=vs.85).aspx - */ - SYSTEMTIME st1, st2; - ULARGE_INTEGER tmp1, tmp2; - - DUK_UNREF(ctx); - - GetSystemTime(&st1); - duk__convert_systime_to_ularge((const SYSTEMTIME *) &st1, &tmp1); - - duk__set_systime_jan1970(&st2); - duk__convert_systime_to_ularge((const SYSTEMTIME *) &st2, &tmp2); - - /* Difference is in 100ns units, convert to milliseconds w/o fractions */ - return (duk_double_t) ((tmp1.QuadPart - tmp2.QuadPart) / 10000LL); -} -#endif /* DUK_USE_DATE_NOW_WINDOWS */ - - -#if defined(DUK_USE_DATE_TZO_WINDOWS) -DUK_INTERNAL_DECL duk_int_t duk_bi_date_get_local_tzoffset_windows(duk_double_t d) { - SYSTEMTIME st1; - SYSTEMTIME st2; - SYSTEMTIME st3; - ULARGE_INTEGER tmp1; - ULARGE_INTEGER tmp2; - ULARGE_INTEGER tmp3; - FILETIME ft1; - - /* XXX: handling of timestamps outside Windows supported range. - * How does Windows deal with dates before 1600? Does windows - * support all Ecmascript years (like -200000 and +200000)? - * Should equivalent year mapping be used here too? If so, use - * a shared helper (currently integrated into timeval-to-parts). - */ - - /* Use the approach described in "Remarks" of FileTimeToLocalFileTime: - * http://msdn.microsoft.com/en-us/library/windows/desktop/ms724277(v=vs.85).aspx - */ - - duk__set_systime_jan1970(&st1); - duk__convert_systime_to_ularge((const SYSTEMTIME *) &st1, &tmp1); - tmp2.QuadPart = (ULONGLONG) (d * 10000.0); /* millisec -> 100ns units since jan 1, 1970 */ - tmp2.QuadPart += tmp1.QuadPart; /* input 'd' in Windows UTC, 100ns units */ - - ft1.dwLowDateTime = tmp2.LowPart; - ft1.dwHighDateTime = tmp2.HighPart; - FileTimeToSystemTime((const FILETIME *) &ft1, &st2); - if (SystemTimeToTzSpecificLocalTime((LPTIME_ZONE_INFORMATION) NULL, &st2, &st3) == 0) { - DUK_D(DUK_DPRINT("SystemTimeToTzSpecificLocalTime() failed, return tzoffset 0")); - return 0; - } - duk__convert_systime_to_ularge((const SYSTEMTIME *) &st3, &tmp3); - - /* Positive if local time ahead of UTC. */ - return (duk_int_t) (((LONGLONG) tmp3.QuadPart - (LONGLONG) tmp2.QuadPart) / 10000000LL); /* seconds */ -} -#endif /* DUK_USE_DATE_TZO_WINDOWS */ -#line 1 "duk_bi_duktape.c" -/* - * Duktape built-ins - * - * Size optimization note: it might seem that vararg multipurpose functions - * like fin(), enc(), and dec() are not very size optimal, but using a single - * user-visible Ecmascript function saves a lot of run-time footprint; each - * Function instance takes >100 bytes. Using a shared native helper and a - * 'magic' value won't save much if there are multiple Function instances - * anyway. - */ - -/* include removed: duk_internal.h */ - -/* Raw helper to extract internal information / statistics about a value. - * The return values are version specific and must not expose anything - * that would lead to security issues (e.g. exposing compiled function - * 'data' buffer might be an issue). Currently only counts and sizes and - * such are given so there should not be a security impact. - */ -DUK_INTERNAL duk_ret_t duk_bi_duktape_object_info(duk_context *ctx) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_tval *tv; - duk_heaphdr *h; - duk_int_t i, n; - - DUK_UNREF(thr); - - /* result array */ - duk_push_array(ctx); /* -> [ val arr ] */ - - /* type tag (public) */ - duk_push_int(ctx, duk_get_type(ctx, 0)); - - /* address */ - tv = duk_get_tval(ctx, 0); - DUK_ASSERT(tv != NULL); /* because arg count is 1 */ - if (DUK_TVAL_IS_HEAP_ALLOCATED(tv)) { - h = DUK_TVAL_GET_HEAPHDR(tv); - duk_push_pointer(ctx, (void *) h); - } else { - /* internal type tag */ - duk_push_int(ctx, (duk_int_t) DUK_TVAL_GET_TAG(tv)); - goto done; - } - DUK_ASSERT(h != NULL); - - /* refcount */ -#ifdef DUK_USE_REFERENCE_COUNTING - duk_push_size_t(ctx, DUK_HEAPHDR_GET_REFCOUNT(h)); -#else - duk_push_undefined(ctx); -#endif - - /* heaphdr size and additional allocation size, followed by - * type specific stuff (with varying value count) - */ - switch ((duk_small_int_t) DUK_HEAPHDR_GET_TYPE(h)) { - case DUK_HTYPE_STRING: { - duk_hstring *h_str = (duk_hstring *) h; - duk_push_uint(ctx, (duk_uint_t) (sizeof(duk_hstring) + DUK_HSTRING_GET_BYTELEN(h_str) + 1)); - break; - } - case DUK_HTYPE_OBJECT: { - duk_hobject *h_obj = (duk_hobject *) h; - duk_small_uint_t hdr_size; - if (DUK_HOBJECT_IS_COMPILEDFUNCTION(h_obj)) { - hdr_size = (duk_small_uint_t) sizeof(duk_hcompiledfunction); - } else if (DUK_HOBJECT_IS_NATIVEFUNCTION(h_obj)) { - hdr_size = (duk_small_uint_t) sizeof(duk_hnativefunction); - } else if (DUK_HOBJECT_IS_THREAD(h_obj)) { - hdr_size = (duk_small_uint_t) sizeof(duk_hthread); - } else { - hdr_size = (duk_small_uint_t) sizeof(duk_hobject); - } - duk_push_uint(ctx, (duk_uint_t) hdr_size); - duk_push_uint(ctx, (duk_uint_t) DUK_HOBJECT_P_ALLOC_SIZE(h_obj)); - duk_push_uint(ctx, (duk_uint_t) DUK_HOBJECT_GET_ESIZE(h_obj)); - /* Note: e_next indicates the number of gc-reachable entries - * in the entry part, and also indicates the index where the - * next new property would be inserted. It does *not* indicate - * the number of non-NULL keys present in the object. That - * value could be counted separately but requires a pass through - * the key list. - */ - duk_push_uint(ctx, (duk_uint_t) DUK_HOBJECT_GET_ENEXT(h_obj)); - duk_push_uint(ctx, (duk_uint_t) DUK_HOBJECT_GET_ASIZE(h_obj)); - duk_push_uint(ctx, (duk_uint_t) DUK_HOBJECT_GET_HSIZE(h_obj)); - if (DUK_HOBJECT_IS_COMPILEDFUNCTION(h_obj)) { - duk_hbuffer *h_data = (duk_hbuffer *) DUK_HCOMPILEDFUNCTION_GET_DATA(thr->heap, (duk_hcompiledfunction *) h_obj); - if (h_data) { - duk_push_uint(ctx, (duk_uint_t) DUK_HBUFFER_GET_SIZE(h_data)); - } else { - duk_push_uint(ctx, 0); - } - } - break; - } - case DUK_HTYPE_BUFFER: { - duk_hbuffer *h_buf = (duk_hbuffer *) h; - if (DUK_HBUFFER_HAS_DYNAMIC(h_buf)) { - if (DUK_HBUFFER_HAS_EXTERNAL(h_buf)) { - duk_push_uint(ctx, (duk_uint_t) (sizeof(duk_hbuffer_external))); - } else { - /* When alloc_size == 0 the second allocation may not - * actually exist. - */ - duk_push_uint(ctx, (duk_uint_t) (sizeof(duk_hbuffer_dynamic))); - } - duk_push_uint(ctx, (duk_uint_t) (DUK_HBUFFER_GET_SIZE(h_buf))); - } else { - duk_push_uint(ctx, (duk_uint_t) (sizeof(duk_hbuffer_fixed) + DUK_HBUFFER_GET_SIZE(h_buf) + 1)); - } - break; - - } - } - - done: - /* set values into ret array */ - /* XXX: primitive to make array from valstack slice */ - n = duk_get_top(ctx); - for (i = 2; i < n; i++) { - duk_dup(ctx, i); - duk_put_prop_index(ctx, 1, i - 2); - } - duk_dup(ctx, 1); - return 1; -} - -DUK_INTERNAL duk_ret_t duk_bi_duktape_object_act(duk_context *ctx) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_activation *act; - duk_uint_fast32_t pc; - duk_uint_fast32_t line; - duk_int_t level; - - /* -1 = top callstack entry, callstack[callstack_top - 1] - * -callstack_top = bottom callstack entry, callstack[0] - */ - level = duk_to_int(ctx, 0); - if (level >= 0 || -level > (duk_int_t) thr->callstack_top) { - return 0; - } - DUK_ASSERT(level >= -((duk_int_t) thr->callstack_top) && level <= -1); - act = thr->callstack + thr->callstack_top + level; - - duk_push_object(ctx); - - duk_push_tval(ctx, &act->tv_func); - - /* Relevant PC is just before current one because PC is - * post-incremented. This should match what error augment - * code does. - */ - pc = duk_hthread_get_act_prev_pc(thr, act); - duk_push_uint(ctx, (duk_uint_t) pc); - -#if defined(DUK_USE_PC2LINE) - line = duk_hobject_pc2line_query(ctx, -2, pc); -#else - line = 0; -#endif - duk_push_uint(ctx, (duk_uint_t) line); - - /* Providing access to e.g. act->lex_env would be dangerous: these - * internal structures must never be accessible to the application. - * Duktape relies on them having consistent data, and this consistency - * is only asserted for, not checked for. - */ - - /* [ level obj func pc line ] */ - - /* XXX: version specific array format instead? */ - duk_xdef_prop_stridx_wec(ctx, -4, DUK_STRIDX_LINE_NUMBER); - duk_xdef_prop_stridx_wec(ctx, -3, DUK_STRIDX_PC); - duk_xdef_prop_stridx_wec(ctx, -2, DUK_STRIDX_LC_FUNCTION); - return 1; -} - -DUK_INTERNAL duk_ret_t duk_bi_duktape_object_gc(duk_context *ctx) { -#ifdef DUK_USE_MARK_AND_SWEEP - duk_hthread *thr = (duk_hthread *) ctx; - duk_small_uint_t flags; - duk_bool_t rc; - - flags = (duk_small_uint_t) duk_get_uint(ctx, 0); - rc = duk_heap_mark_and_sweep(thr->heap, flags); - - /* XXX: Not sure what the best return value would be in the API. - * Return a boolean for now. Note that rc == 0 is success (true). - */ - duk_push_boolean(ctx, !rc); - return 1; -#else - DUK_UNREF(ctx); - return 0; -#endif -} - -DUK_INTERNAL duk_ret_t duk_bi_duktape_object_fin(duk_context *ctx) { - (void) duk_require_hobject(ctx, 0); - if (duk_get_top(ctx) >= 2) { - /* Set: currently a finalizer is disabled by setting it to - * undefined; this does not remove the property at the moment. - * The value could be type checked to be either a function - * or something else; if something else, the property could - * be deleted. - */ - duk_set_top(ctx, 2); - (void) duk_put_prop_stridx(ctx, 0, DUK_STRIDX_INT_FINALIZER); - return 0; - } else { - /* Get. */ - DUK_ASSERT(duk_get_top(ctx) == 1); - duk_get_prop_stridx(ctx, 0, DUK_STRIDX_INT_FINALIZER); - return 1; - } -} - -DUK_INTERNAL duk_ret_t duk_bi_duktape_object_enc(duk_context *ctx) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_hstring *h_str; - - DUK_UNREF(thr); - - /* Vararg function: must be careful to check/require arguments. - * The JSON helpers accept invalid indices and treat them like - * non-existent optional parameters. - */ - - h_str = duk_require_hstring(ctx, 0); - duk_require_valid_index(ctx, 1); - - if (h_str == DUK_HTHREAD_STRING_HEX(thr)) { - duk_set_top(ctx, 2); - duk_hex_encode(ctx, 1); - DUK_ASSERT_TOP(ctx, 2); - } else if (h_str == DUK_HTHREAD_STRING_BASE64(thr)) { - duk_set_top(ctx, 2); - duk_base64_encode(ctx, 1); - DUK_ASSERT_TOP(ctx, 2); -#ifdef DUK_USE_JX - } else if (h_str == DUK_HTHREAD_STRING_JX(thr)) { - duk_bi_json_stringify_helper(ctx, - 1 /*idx_value*/, - 2 /*idx_replacer*/, - 3 /*idx_space*/, - DUK_JSON_FLAG_EXT_CUSTOM | - DUK_JSON_FLAG_ASCII_ONLY | - DUK_JSON_FLAG_AVOID_KEY_QUOTES /*flags*/); -#endif -#ifdef DUK_USE_JC - } else if (h_str == DUK_HTHREAD_STRING_JC(thr)) { - duk_bi_json_stringify_helper(ctx, - 1 /*idx_value*/, - 2 /*idx_replacer*/, - 3 /*idx_space*/, - DUK_JSON_FLAG_EXT_COMPATIBLE | - DUK_JSON_FLAG_ASCII_ONLY /*flags*/); -#endif - } else { - return DUK_RET_TYPE_ERROR; - } - return 1; -} - -DUK_INTERNAL duk_ret_t duk_bi_duktape_object_dec(duk_context *ctx) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_hstring *h_str; - - DUK_UNREF(thr); - - /* Vararg function: must be careful to check/require arguments. - * The JSON helpers accept invalid indices and treat them like - * non-existent optional parameters. - */ - - h_str = duk_require_hstring(ctx, 0); - duk_require_valid_index(ctx, 1); - - if (h_str == DUK_HTHREAD_STRING_HEX(thr)) { - duk_set_top(ctx, 2); - duk_hex_decode(ctx, 1); - DUK_ASSERT_TOP(ctx, 2); - } else if (h_str == DUK_HTHREAD_STRING_BASE64(thr)) { - duk_set_top(ctx, 2); - duk_base64_decode(ctx, 1); - DUK_ASSERT_TOP(ctx, 2); -#ifdef DUK_USE_JX - } else if (h_str == DUK_HTHREAD_STRING_JX(thr)) { - duk_bi_json_parse_helper(ctx, - 1 /*idx_value*/, - 2 /*idx_replacer*/, - DUK_JSON_FLAG_EXT_CUSTOM /*flags*/); -#endif -#ifdef DUK_USE_JC - } else if (h_str == DUK_HTHREAD_STRING_JC(thr)) { - duk_bi_json_parse_helper(ctx, - 1 /*idx_value*/, - 2 /*idx_replacer*/, - DUK_JSON_FLAG_EXT_COMPATIBLE /*flags*/); -#endif - } else { - return DUK_RET_TYPE_ERROR; - } - return 1; -} - -/* - * Compact an object - */ - -DUK_INTERNAL duk_ret_t duk_bi_duktape_object_compact(duk_context *ctx) { - DUK_ASSERT_TOP(ctx, 1); - duk_compact(ctx, 0); - return 1; /* return the argument object */ -} -#line 1 "duk_bi_error.c" -/* - * Error built-ins - */ - -/* include removed: duk_internal.h */ - -DUK_INTERNAL duk_ret_t duk_bi_error_constructor_shared(duk_context *ctx) { - /* Behavior for constructor and non-constructor call is - * the same except for augmenting the created error. When - * called as a constructor, the caller (duk_new()) will handle - * augmentation; when called as normal function, we need to do - * it here. - */ - - duk_hthread *thr = (duk_hthread *) ctx; - duk_small_int_t bidx_prototype = duk_get_current_magic(ctx); - - /* same for both error and each subclass like TypeError */ - duk_uint_t flags_and_class = DUK_HOBJECT_FLAG_EXTENSIBLE | - DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_ERROR); - - DUK_UNREF(thr); - - duk_push_object_helper(ctx, flags_and_class, bidx_prototype); - - /* If message is undefined, the own property 'message' is not set at - * all to save property space. An empty message is inherited anyway. - */ - if (!duk_is_undefined(ctx, 0)) { - duk_to_string(ctx, 0); - duk_dup(ctx, 0); /* [ message error message ] */ - duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_MESSAGE, DUK_PROPDESC_FLAGS_WC); - } - - /* Augment the error if called as a normal function. __FILE__ and __LINE__ - * are not desirable in this case. - */ - -#ifdef DUK_USE_AUGMENT_ERROR_CREATE - if (!duk_is_constructor_call(ctx)) { - duk_err_augment_error_create(thr, thr, NULL, 0, 1 /*noblame_fileline*/); - } -#endif - - return 1; -} - -DUK_INTERNAL duk_ret_t duk_bi_error_prototype_to_string(duk_context *ctx) { - /* XXX: optimize with more direct internal access */ - - duk_push_this(ctx); - (void) duk_require_hobject_or_lfunc_coerce(ctx, -1); - - /* [ ... this ] */ - - duk_get_prop_stridx(ctx, -1, DUK_STRIDX_NAME); - if (duk_is_undefined(ctx, -1)) { - duk_pop(ctx); - duk_push_string(ctx, "Error"); - } else { - duk_to_string(ctx, -1); - } - - /* [ ... this name ] */ - - /* XXX: Are steps 6 and 7 in E5 Section 15.11.4.4 duplicated by - * accident or are they actually needed? The first ToString() - * could conceivably return 'undefined'. - */ - duk_get_prop_stridx(ctx, -2, DUK_STRIDX_MESSAGE); - if (duk_is_undefined(ctx, -1)) { - duk_pop(ctx); - duk_push_string(ctx, ""); - } else { - duk_to_string(ctx, -1); - } - - /* [ ... this name message ] */ - - if (duk_get_length(ctx, -2) == 0) { - /* name is empty -> return message */ - return 1; - } - if (duk_get_length(ctx, -1) == 0) { - /* message is empty -> return name */ - duk_pop(ctx); - return 1; - } - duk_push_string(ctx, ": "); - duk_insert(ctx, -2); /* ... name ': ' message */ - duk_concat(ctx, 3); - - return 1; -} - -#if defined(DUK_USE_TRACEBACKS) - -/* - * Traceback handling - * - * The unified helper decodes the traceback and produces various requested - * outputs. It should be optimized for size, and may leave garbage on stack, - * only the topmost return value matters. For instance, traceback separator - * and decoded strings are pushed even when looking for filename only. - * - * NOTE: although _Tracedata is an internal property, user code can currently - * write to the array (or replace it with something other than an array). - * The code below must tolerate arbitrary _Tracedata. It can throw errors - * etc, but cannot cause a segfault or memory unsafe behavior. - */ - -/* constants arbitrary, chosen for small loads */ -#define DUK__OUTPUT_TYPE_TRACEBACK (-1) -#define DUK__OUTPUT_TYPE_FILENAME 0 -#define DUK__OUTPUT_TYPE_LINENUMBER 1 - -DUK_LOCAL duk_ret_t duk__error_getter_helper(duk_context *ctx, duk_small_int_t output_type) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_idx_t idx_td; - duk_small_int_t i; /* traceback depth fits into 16 bits */ - duk_small_int_t t; /* stack type fits into 16 bits */ - duk_small_int_t count_func = 0; /* traceback depth ensures fits into 16 bits */ - const char *str_tailcall = " tailcall"; - const char *str_strict = " strict"; - const char *str_construct = " construct"; - const char *str_prevyield = " preventsyield"; - const char *str_directeval = " directeval"; - const char *str_empty = ""; - - DUK_ASSERT_TOP(ctx, 0); /* fixed arg count */ - DUK_UNREF(thr); - - duk_push_this(ctx); - duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_TRACEDATA); - idx_td = duk_get_top_index(ctx); - - duk_push_hstring_stridx(ctx, DUK_STRIDX_NEWLINE_4SPACE); - duk_push_this(ctx); - - /* [ ... this tracedata sep this ] */ - - /* XXX: skip null filename? */ - - if (duk_check_type(ctx, idx_td, DUK_TYPE_OBJECT)) { - /* Current tracedata contains 2 entries per callstack entry. */ - for (i = 0; ; i += 2) { - duk_int_t pc; - duk_int_t line; - duk_int_t flags; - duk_double_t d; - const char *funcname; - const char *filename; - duk_hobject *h_func; - duk_hstring *h_name; - - duk_require_stack(ctx, 5); - duk_get_prop_index(ctx, idx_td, i); - duk_get_prop_index(ctx, idx_td, i + 1); - d = duk_to_number(ctx, -1); - pc = (duk_int_t) DUK_FMOD(d, DUK_DOUBLE_2TO32); - flags = (duk_int_t) DUK_FLOOR(d / DUK_DOUBLE_2TO32); - t = (duk_small_int_t) duk_get_type(ctx, -2); - - if (t == DUK_TYPE_OBJECT || t == DUK_TYPE_LIGHTFUNC) { - /* - * Ecmascript/native function call or lightfunc call - */ - - count_func++; - - /* [ ... v1(func) v2(pc+flags) ] */ - - h_func = duk_get_hobject(ctx, -2); /* NULL for lightfunc */ - - duk_get_prop_stridx(ctx, -2, DUK_STRIDX_NAME); - duk_get_prop_stridx(ctx, -3, DUK_STRIDX_FILE_NAME); - -#if defined(DUK_USE_PC2LINE) - line = duk_hobject_pc2line_query(ctx, -4, (duk_uint_fast32_t) pc); -#else - line = 0; -#endif - - /* [ ... v1 v2 name filename ] */ - - /* When looking for .fileName/.lineNumber, blame first - * function which has a .fileName. - */ - if (duk_is_string(ctx, -1)) { - if (output_type == DUK__OUTPUT_TYPE_FILENAME) { - return 1; - } else if (output_type == DUK__OUTPUT_TYPE_LINENUMBER) { - duk_push_int(ctx, line); - return 1; - } - } - - /* XXX: Change 'anon' handling here too, to use empty string for anonymous functions? */ - /* XXX: Could be improved by coercing to a readable duk_tval (especially string escaping) */ - h_name = duk_get_hstring(ctx, -2); /* may be NULL */ - funcname = (h_name == NULL || h_name == DUK_HTHREAD_STRING_EMPTY_STRING(thr)) ? - "[anon]" : (const char *) DUK_HSTRING_GET_DATA(h_name); - filename = duk_get_string(ctx, -1); - filename = filename ? filename : ""; - DUK_ASSERT(funcname != NULL); - DUK_ASSERT(filename != NULL); - - if (h_func == NULL) { - duk_push_sprintf(ctx, "at %s light%s%s%s%s%s", - (const char *) funcname, - (const char *) ((flags & DUK_ACT_FLAG_STRICT) ? str_strict : str_empty), - (const char *) ((flags & DUK_ACT_FLAG_TAILCALLED) ? str_tailcall : str_empty), - (const char *) ((flags & DUK_ACT_FLAG_CONSTRUCT) ? str_construct : str_empty), - (const char *) ((flags & DUK_ACT_FLAG_DIRECT_EVAL) ? str_directeval : str_empty), - (const char *) ((flags & DUK_ACT_FLAG_PREVENT_YIELD) ? str_prevyield : str_empty)); - } else if (DUK_HOBJECT_HAS_NATIVEFUNCTION(h_func)) { - duk_push_sprintf(ctx, "at %s (%s) native%s%s%s%s%s", - (const char *) funcname, - (const char *) filename, - (const char *) ((flags & DUK_ACT_FLAG_STRICT) ? str_strict : str_empty), - (const char *) ((flags & DUK_ACT_FLAG_TAILCALLED) ? str_tailcall : str_empty), - (const char *) ((flags & DUK_ACT_FLAG_CONSTRUCT) ? str_construct : str_empty), - (const char *) ((flags & DUK_ACT_FLAG_DIRECT_EVAL) ? str_directeval : str_empty), - (const char *) ((flags & DUK_ACT_FLAG_PREVENT_YIELD) ? str_prevyield : str_empty)); - } else { - duk_push_sprintf(ctx, "at %s (%s:%ld)%s%s%s%s%s", - (const char *) funcname, - (const char *) filename, - (long) line, - (const char *) ((flags & DUK_ACT_FLAG_STRICT) ? str_strict : str_empty), - (const char *) ((flags & DUK_ACT_FLAG_TAILCALLED) ? str_tailcall : str_empty), - (const char *) ((flags & DUK_ACT_FLAG_CONSTRUCT) ? str_construct : str_empty), - (const char *) ((flags & DUK_ACT_FLAG_DIRECT_EVAL) ? str_directeval : str_empty), - (const char *) ((flags & DUK_ACT_FLAG_PREVENT_YIELD) ? str_prevyield : str_empty)); - } - duk_replace(ctx, -5); /* [ ... v1 v2 name filename str ] -> [ ... str v2 name filename ] */ - duk_pop_n(ctx, 3); /* -> [ ... str ] */ - } else if (t == DUK_TYPE_STRING) { - /* - * __FILE__ / __LINE__ entry, here 'pc' is line number directly. - * Sometimes __FILE__ / __LINE__ is reported as the source for - * the error (fileName, lineNumber), sometimes not. - */ - - /* [ ... v1(filename) v2(line+flags) ] */ - - /* When looking for .fileName/.lineNumber, blame compilation - * or C call site unless flagged not to do so. - */ - if (!(flags & DUK_TB_FLAG_NOBLAME_FILELINE)) { - if (output_type == DUK__OUTPUT_TYPE_FILENAME) { - duk_pop(ctx); - return 1; - } else if (output_type == DUK__OUTPUT_TYPE_LINENUMBER) { - duk_push_int(ctx, pc); - return 1; - } - } - - duk_push_sprintf(ctx, "at [anon] (%s:%ld) internal", - (const char *) duk_get_string(ctx, -2), (long) pc); - duk_replace(ctx, -3); /* [ ... v1 v2 str ] -> [ ... str v2 ] */ - duk_pop(ctx); /* -> [ ... str ] */ - } else { - /* unknown, ignore */ - duk_pop_2(ctx); - break; - } - } - - if (count_func >= DUK_USE_TRACEBACK_DEPTH) { - /* Possibly truncated; there is no explicit truncation - * marker so this is the best we can do. - */ - - duk_push_hstring_stridx(ctx, DUK_STRIDX_BRACKETED_ELLIPSIS); - } - } - - /* [ ... this tracedata sep this str1 ... strN ] */ - - if (output_type != DUK__OUTPUT_TYPE_TRACEBACK) { - return 0; - } else { - /* The 'this' after 'sep' will get ToString() coerced by - * duk_join() automatically. We don't want to do that - * coercion when providing .fileName or .lineNumber (GH-254). - */ - duk_join(ctx, duk_get_top(ctx) - (idx_td + 2) /*count, not including sep*/); - return 1; - } -} - -/* XXX: Output type could be encoded into native function 'magic' value to - * save space. For setters the stridx could be encoded into 'magic'. - */ - -DUK_INTERNAL duk_ret_t duk_bi_error_prototype_stack_getter(duk_context *ctx) { - return duk__error_getter_helper(ctx, DUK__OUTPUT_TYPE_TRACEBACK); -} - -DUK_INTERNAL duk_ret_t duk_bi_error_prototype_filename_getter(duk_context *ctx) { - return duk__error_getter_helper(ctx, DUK__OUTPUT_TYPE_FILENAME); -} - -DUK_INTERNAL duk_ret_t duk_bi_error_prototype_linenumber_getter(duk_context *ctx) { - return duk__error_getter_helper(ctx, DUK__OUTPUT_TYPE_LINENUMBER); -} - -#undef DUK__OUTPUT_TYPE_TRACEBACK -#undef DUK__OUTPUT_TYPE_FILENAME -#undef DUK__OUTPUT_TYPE_LINENUMBER - -#else /* DUK_USE_TRACEBACKS */ - -/* - * Traceback handling when tracebacks disabled. - * - * The fileName / lineNumber stubs are now necessary because built-in - * data will include the accessor properties in Error.prototype. If those - * are removed for builds without tracebacks, these can also be removed. - * 'stack' should still be present and produce a ToString() equivalent: - * this is useful for user code which prints a stacktrace and expects to - * see something useful. A normal stacktrace also begins with a ToString() - * of the error so this makes sense. - */ - -DUK_INTERNAL duk_ret_t duk_bi_error_prototype_stack_getter(duk_context *ctx) { - /* XXX: remove this native function and map 'stack' accessor - * to the toString() implementation directly. - */ - return duk_bi_error_prototype_to_string(ctx); -} - -DUK_INTERNAL duk_ret_t duk_bi_error_prototype_filename_getter(duk_context *ctx) { - DUK_UNREF(ctx); - return 0; -} - -DUK_INTERNAL duk_ret_t duk_bi_error_prototype_linenumber_getter(duk_context *ctx) { - DUK_UNREF(ctx); - return 0; -} - -#endif /* DUK_USE_TRACEBACKS */ - -DUK_LOCAL duk_ret_t duk__error_setter_helper(duk_context *ctx, duk_small_uint_t stridx_key) { - /* Attempt to write 'stack', 'fileName', 'lineNumber' works as if - * user code called Object.defineProperty() to create an overriding - * own property. This allows user code to overwrite .fileName etc - * intuitively as e.g. "err.fileName = 'dummy'" as one might expect. - * See https://github.com/svaarala/duktape/issues/387. - */ - - DUK_ASSERT_TOP(ctx, 1); /* fixed arg count: value */ - - duk_push_this(ctx); - duk_push_hstring_stridx(ctx, (duk_small_int_t) stridx_key); - duk_dup(ctx, 0); - - /* [ ... obj key value ] */ - - DUK_DD(DUK_DDPRINT("error setter: %!T %!T %!T", - duk_get_tval(ctx, -3), duk_get_tval(ctx, -2), duk_get_tval(ctx, -1))); - - duk_def_prop(ctx, -3, DUK_DEFPROP_HAVE_VALUE | - DUK_DEFPROP_HAVE_WRITABLE | DUK_DEFPROP_WRITABLE | - DUK_DEFPROP_HAVE_ENUMERABLE | /*not enumerable*/ - DUK_DEFPROP_HAVE_CONFIGURABLE | DUK_DEFPROP_CONFIGURABLE); - return 0; -} - -DUK_INTERNAL duk_ret_t duk_bi_error_prototype_stack_setter(duk_context *ctx) { - return duk__error_setter_helper(ctx, DUK_STRIDX_STACK); -} - -DUK_INTERNAL duk_ret_t duk_bi_error_prototype_filename_setter(duk_context *ctx) { - return duk__error_setter_helper(ctx, DUK_STRIDX_FILE_NAME); -} - -DUK_INTERNAL duk_ret_t duk_bi_error_prototype_linenumber_setter(duk_context *ctx) { - return duk__error_setter_helper(ctx, DUK_STRIDX_LINE_NUMBER); -} -#line 1 "duk_bi_function.c" -/* - * Function built-ins - */ - -/* include removed: duk_internal.h */ - -DUK_INTERNAL duk_ret_t duk_bi_function_constructor(duk_context *ctx) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_hstring *h_sourcecode; - duk_idx_t nargs; - duk_idx_t i; - duk_small_uint_t comp_flags; - duk_hcompiledfunction *func; - duk_hobject *outer_lex_env; - duk_hobject *outer_var_env; - - /* normal and constructor calls have identical semantics */ - - nargs = duk_get_top(ctx); - for (i = 0; i < nargs; i++) { - duk_to_string(ctx, i); - } - - if (nargs == 0) { - duk_push_string(ctx, ""); - duk_push_string(ctx, ""); - } else if (nargs == 1) { - /* XXX: cover this with the generic >1 case? */ - duk_push_string(ctx, ""); - } else { - duk_insert(ctx, 0); /* [ arg1 ... argN-1 body] -> [body arg1 ... argN-1] */ - duk_push_string(ctx, ","); - duk_insert(ctx, 1); - duk_join(ctx, nargs - 1); - } - - /* [ body formals ], formals is comma separated list that needs to be parsed */ - - DUK_ASSERT_TOP(ctx, 2); - - /* XXX: this placeholder is not always correct, but use for now. - * It will fail in corner cases; see test-dev-func-cons-args.js. - */ - duk_push_string(ctx, "function("); - duk_dup(ctx, 1); - duk_push_string(ctx, "){"); - duk_dup(ctx, 0); - duk_push_string(ctx, "}"); - duk_concat(ctx, 5); - - /* [ body formals source ] */ - - DUK_ASSERT_TOP(ctx, 3); - - /* strictness is not inherited, intentional */ - comp_flags = DUK_JS_COMPILE_FLAG_FUNCEXPR; - - duk_push_hstring_stridx(ctx, DUK_STRIDX_COMPILE); /* XXX: copy from caller? */ /* XXX: ignored now */ - h_sourcecode = duk_require_hstring(ctx, -2); - duk_js_compile(thr, - (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h_sourcecode), - (duk_size_t) DUK_HSTRING_GET_BYTELEN(h_sourcecode), - comp_flags); - func = (duk_hcompiledfunction *) duk_get_hobject(ctx, -1); - DUK_ASSERT(func != NULL); - DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION((duk_hobject *) func)); - - /* [ body formals source template ] */ - - /* only outer_lex_env matters, as functions always get a new - * variable declaration environment. - */ - - outer_lex_env = thr->builtins[DUK_BIDX_GLOBAL_ENV]; - outer_var_env = thr->builtins[DUK_BIDX_GLOBAL_ENV]; - - duk_js_push_closure(thr, func, outer_var_env, outer_lex_env, 1 /*add_auto_proto*/); - - /* [ body formals source template closure ] */ - - return 1; -} - -DUK_INTERNAL duk_ret_t duk_bi_function_prototype(duk_context *ctx) { - /* ignore arguments, return undefined (E5 Section 15.3.4) */ - DUK_UNREF(ctx); - return 0; -} - -DUK_INTERNAL duk_ret_t duk_bi_function_prototype_to_string(duk_context *ctx) { - duk_tval *tv; - - /* - * E5 Section 15.3.4.2 places few requirements on the output of - * this function: - * - * - The result is an implementation dependent representation - * of the function; in particular - * - * - The result must follow the syntax of a FunctionDeclaration. - * In particular, the function must have a name (even in the - * case of an anonymous function or a function with an empty - * name). - * - * - Note in particular that the output does NOT need to compile - * into anything useful. - */ - - - /* XXX: faster internal way to get this */ - duk_push_this(ctx); - tv = duk_get_tval(ctx, -1); - DUK_ASSERT(tv != NULL); - - if (DUK_TVAL_IS_OBJECT(tv)) { - duk_hobject *obj = DUK_TVAL_GET_OBJECT(tv); - const char *func_name; - - /* Function name: missing/undefined is mapped to empty string, - * otherwise coerce to string. - */ - /* XXX: currently no handling for non-allowed identifier characters, - * e.g. a '{' in the function name. - */ - duk_get_prop_stridx(ctx, -1, DUK_STRIDX_NAME); - if (duk_is_undefined(ctx, -1)) { - func_name = ""; - } else { - func_name = duk_to_string(ctx, -1); - DUK_ASSERT(func_name != NULL); - } - - /* Indicate function type in the function body using a dummy - * directive. - */ - if (DUK_HOBJECT_HAS_COMPILEDFUNCTION(obj)) { - duk_push_sprintf(ctx, "function %s() {\"ecmascript\"}", (const char *) func_name); - } else if (DUK_HOBJECT_HAS_NATIVEFUNCTION(obj)) { - duk_push_sprintf(ctx, "function %s() {\"native\"}", (const char *) func_name); - } else if (DUK_HOBJECT_HAS_BOUND(obj)) { - duk_push_sprintf(ctx, "function %s() {\"bound\"}", (const char *) func_name); - } else { - goto type_error; - } - } else if (DUK_TVAL_IS_LIGHTFUNC(tv)) { - duk_push_lightfunc_tostring(ctx, tv); - } else { - goto type_error; - } - - return 1; - - type_error: - return DUK_RET_TYPE_ERROR; -} - -DUK_INTERNAL duk_ret_t duk_bi_function_prototype_apply(duk_context *ctx) { - duk_idx_t len; - duk_idx_t i; - - DUK_ASSERT_TOP(ctx, 2); /* not a vararg function */ - - duk_push_this(ctx); - if (!duk_is_callable(ctx, -1)) { - DUK_DDD(DUK_DDDPRINT("func is not callable")); - goto type_error; - } - duk_insert(ctx, 0); - DUK_ASSERT_TOP(ctx, 3); - - DUK_DDD(DUK_DDDPRINT("func=%!iT, thisArg=%!iT, argArray=%!iT", - (duk_tval *) duk_get_tval(ctx, 0), - (duk_tval *) duk_get_tval(ctx, 1), - (duk_tval *) duk_get_tval(ctx, 2))); - - /* [ func thisArg argArray ] */ - - if (duk_is_null_or_undefined(ctx, 2)) { - DUK_DDD(DUK_DDDPRINT("argArray is null/undefined, no args")); - len = 0; - } else if (!duk_is_object(ctx, 2)) { - goto type_error; - } else { - DUK_DDD(DUK_DDDPRINT("argArray is an object")); - - /* XXX: make this an internal helper */ - duk_get_prop_stridx(ctx, 2, DUK_STRIDX_LENGTH); - len = (duk_idx_t) duk_to_uint32(ctx, -1); /* ToUint32() coercion required */ - duk_pop(ctx); - - duk_require_stack(ctx, len); - - DUK_DDD(DUK_DDDPRINT("argArray length is %ld", (long) len)); - for (i = 0; i < len; i++) { - duk_get_prop_index(ctx, 2, i); - } - } - duk_remove(ctx, 2); - DUK_ASSERT_TOP(ctx, 2 + len); - - /* [ func thisArg arg1 ... argN ] */ - - DUK_DDD(DUK_DDDPRINT("apply, func=%!iT, thisArg=%!iT, len=%ld", - (duk_tval *) duk_get_tval(ctx, 0), - (duk_tval *) duk_get_tval(ctx, 1), - (long) len)); - duk_call_method(ctx, len); - return 1; - - type_error: - return DUK_RET_TYPE_ERROR; -} - -DUK_INTERNAL duk_ret_t duk_bi_function_prototype_call(duk_context *ctx) { - duk_idx_t nargs; - - /* Step 1 is not necessary because duk_call_method() will take - * care of it. - */ - - /* vararg function, thisArg needs special handling */ - nargs = duk_get_top(ctx); /* = 1 + arg count */ - if (nargs == 0) { - duk_push_undefined(ctx); - nargs++; - } - DUK_ASSERT(nargs >= 1); - - /* [ thisArg arg1 ... argN ] */ - - duk_push_this(ctx); /* 'func' in the algorithm */ - duk_insert(ctx, 0); - - /* [ func thisArg arg1 ... argN ] */ - - DUK_DDD(DUK_DDDPRINT("func=%!iT, thisArg=%!iT, argcount=%ld, top=%ld", - (duk_tval *) duk_get_tval(ctx, 0), - (duk_tval *) duk_get_tval(ctx, 1), - (long) (nargs - 1), - (long) duk_get_top(ctx))); - duk_call_method(ctx, nargs - 1); - return 1; -} - -/* XXX: the implementation now assumes "chained" bound functions, - * whereas "collapsed" bound functions (where there is ever only - * one bound function which directly points to a non-bound, final - * function) would require a "collapsing" implementation which - * merges argument lists etc here. - */ -DUK_INTERNAL duk_ret_t duk_bi_function_prototype_bind(duk_context *ctx) { - duk_hobject *h_bound; - duk_hobject *h_target; - duk_idx_t nargs; - duk_idx_t i; - - /* vararg function, careful arg handling (e.g. thisArg may not be present) */ - nargs = duk_get_top(ctx); /* = 1 + arg count */ - if (nargs == 0) { - duk_push_undefined(ctx); - nargs++; - } - DUK_ASSERT(nargs >= 1); - - duk_push_this(ctx); - if (!duk_is_callable(ctx, -1)) { - DUK_DDD(DUK_DDDPRINT("func is not callable")); - goto type_error; - } - - /* [ thisArg arg1 ... argN func ] (thisArg+args == nargs total) */ - DUK_ASSERT_TOP(ctx, nargs + 1); - - /* create bound function object */ - duk_push_object_helper(ctx, - DUK_HOBJECT_FLAG_EXTENSIBLE | - DUK_HOBJECT_FLAG_BOUND | - DUK_HOBJECT_FLAG_CONSTRUCTABLE | - DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_FUNCTION), - DUK_BIDX_FUNCTION_PROTOTYPE); - h_bound = duk_get_hobject(ctx, -1); - DUK_ASSERT(h_bound != NULL); - - /* [ thisArg arg1 ... argN func boundFunc ] */ - duk_dup(ctx, -2); /* func */ - duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_INT_TARGET, DUK_PROPDESC_FLAGS_NONE); - - duk_dup(ctx, 0); /* thisArg */ - duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_INT_THIS, DUK_PROPDESC_FLAGS_NONE); - - duk_push_array(ctx); - - /* [ thisArg arg1 ... argN func boundFunc argArray ] */ - - for (i = 0; i < nargs - 1; i++) { - duk_dup(ctx, 1 + i); - duk_put_prop_index(ctx, -2, i); - } - duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_INT_ARGS, DUK_PROPDESC_FLAGS_NONE); - - /* [ thisArg arg1 ... argN func boundFunc ] */ - - /* bound function 'length' property is interesting */ - h_target = duk_get_hobject(ctx, -2); - if (h_target == NULL || /* lightfunc */ - DUK_HOBJECT_GET_CLASS_NUMBER(h_target) == DUK_HOBJECT_CLASS_FUNCTION) { - /* For lightfuncs, simply read the virtual property. */ - duk_int_t tmp; - duk_get_prop_stridx(ctx, -2, DUK_STRIDX_LENGTH); - tmp = duk_to_int(ctx, -1) - (nargs - 1); /* step 15.a */ - duk_pop(ctx); - duk_push_int(ctx, (tmp < 0 ? 0 : tmp)); - } else { - duk_push_int(ctx, 0); - } - duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_LENGTH, DUK_PROPDESC_FLAGS_NONE); /* attrs in E5 Section 15.3.5.1 */ - - /* caller and arguments must use the same thrower, [[ThrowTypeError]] */ - duk_xdef_prop_stridx_thrower(ctx, -1, DUK_STRIDX_CALLER, DUK_PROPDESC_FLAGS_NONE); - duk_xdef_prop_stridx_thrower(ctx, -1, DUK_STRIDX_LC_ARGUMENTS, DUK_PROPDESC_FLAGS_NONE); - - /* these non-standard properties are copied for convenience */ - /* XXX: 'copy properties' API call? */ - duk_get_prop_stridx(ctx, -2, DUK_STRIDX_NAME); - duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_NAME, DUK_PROPDESC_FLAGS_WC); - duk_get_prop_stridx(ctx, -2, DUK_STRIDX_FILE_NAME); - duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_FILE_NAME, DUK_PROPDESC_FLAGS_WC); - - /* The 'strict' flag is copied to get the special [[Get]] of E5.1 - * Section 15.3.5.4 to apply when a 'caller' value is a strict bound - * function. Not sure if this is correct, because the specification - * is a bit ambiguous on this point but it would make sense. - */ - if (h_target == NULL) { - /* Lightfuncs are always strict. */ - DUK_HOBJECT_SET_STRICT(h_bound); - } else if (DUK_HOBJECT_HAS_STRICT(h_target)) { - DUK_HOBJECT_SET_STRICT(h_bound); - } - DUK_DDD(DUK_DDDPRINT("created bound function: %!iT", (duk_tval *) duk_get_tval(ctx, -1))); - - return 1; - - type_error: - return DUK_RET_TYPE_ERROR; -} -#line 1 "duk_bi_global.c" -/* - * Global object built-ins - */ - -/* include removed: duk_internal.h */ - -/* - * Encoding/decoding helpers - */ - -/* XXX: Could add fast path (for each transform callback) with direct byte - * lookups (no shifting) and no explicit check for x < 0x80 before table - * lookup. - */ - -/* Macros for creating and checking bitmasks for character encoding. - * Bit number is a bit counterintuitive, but minimizes code size. - */ -#define DUK__MKBITS(a,b,c,d,e,f,g,h) ((duk_uint8_t) ( \ - ((a) << 0) | ((b) << 1) | ((c) << 2) | ((d) << 3) | \ - ((e) << 4) | ((f) << 5) | ((g) << 6) | ((h) << 7) \ - )) -#define DUK__CHECK_BITMASK(table,cp) ((table)[(cp) >> 3] & (1 << ((cp) & 0x07))) - -/* E5.1 Section 15.1.3.3: uriReserved + uriUnescaped + '#' */ -DUK_LOCAL const duk_uint8_t duk__encode_uriunescaped_table[16] = { - DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x00-0x0f */ - DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x10-0x1f */ - DUK__MKBITS(0, 1, 0, 1, 1, 0, 1, 1), DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), /* 0x20-0x2f */ - DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 1, 0, 1, 0, 1), /* 0x30-0x3f */ - DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), /* 0x40-0x4f */ - DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 0, 0, 0, 0, 1), /* 0x50-0x5f */ - DUK__MKBITS(0, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), /* 0x60-0x6f */ - DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 0, 0, 0, 1, 0), /* 0x70-0x7f */ -}; - -/* E5.1 Section 15.1.3.4: uriUnescaped */ -DUK_LOCAL const duk_uint8_t duk__encode_uricomponent_unescaped_table[16] = { - DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x00-0x0f */ - DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x10-0x1f */ - DUK__MKBITS(0, 1, 0, 0, 0, 0, 0, 1), DUK__MKBITS(1, 1, 1, 0, 0, 1, 1, 0), /* 0x20-0x2f */ - DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 0, 0, 0, 0, 0, 0), /* 0x30-0x3f */ - DUK__MKBITS(0, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), /* 0x40-0x4f */ - DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 0, 0, 0, 0, 1), /* 0x50-0x5f */ - DUK__MKBITS(0, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), /* 0x60-0x6f */ - DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 0, 0, 0, 1, 0), /* 0x70-0x7f */ -}; - -/* E5.1 Section 15.1.3.1: uriReserved + '#' */ -DUK_LOCAL const duk_uint8_t duk__decode_uri_reserved_table[16] = { - DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x00-0x0f */ - DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x10-0x1f */ - DUK__MKBITS(0, 0, 0, 1, 1, 0, 1, 0), DUK__MKBITS(0, 0, 0, 1, 1, 0, 0, 1), /* 0x20-0x2f */ - DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 1, 1, 0, 1, 0, 1), /* 0x30-0x3f */ - DUK__MKBITS(1, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x40-0x4f */ - DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x50-0x5f */ - DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x60-0x6f */ - DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x70-0x7f */ -}; - -/* E5.1 Section 15.1.3.2: empty */ -DUK_LOCAL const duk_uint8_t duk__decode_uri_component_reserved_table[16] = { - DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x00-0x0f */ - DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x10-0x1f */ - DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x20-0x2f */ - DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x30-0x3f */ - DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x40-0x4f */ - DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x50-0x5f */ - DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x60-0x6f */ - DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x70-0x7f */ -}; - -#ifdef DUK_USE_SECTION_B -/* E5.1 Section B.2.2, step 7. */ -DUK_LOCAL const duk_uint8_t duk__escape_unescaped_table[16] = { - DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x00-0x0f */ - DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x10-0x1f */ - DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 1, 1, 0, 1, 1, 1), /* 0x20-0x2f */ - DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 0, 0, 0, 0, 0, 0), /* 0x30-0x3f */ - DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), /* 0x40-0x4f */ - DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 0, 0, 0, 0, 1), /* 0x50-0x5f */ - DUK__MKBITS(0, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), /* 0x60-0x6f */ - DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 0, 0, 0, 0, 0) /* 0x70-0x7f */ -}; -#endif /* DUK_USE_SECTION_B */ - -#undef DUK__MKBITS - -typedef struct { - duk_hthread *thr; - duk_hstring *h_str; - duk_bufwriter_ctx bw; - const duk_uint8_t *p; - const duk_uint8_t *p_start; - const duk_uint8_t *p_end; -} duk__transform_context; - -typedef void (*duk__transform_callback)(duk__transform_context *tfm_ctx, const void *udata, duk_codepoint_t cp); - -/* XXX: refactor and share with other code */ -DUK_LOCAL duk_small_int_t duk__decode_hex_escape(const duk_uint8_t *p, duk_small_int_t n) { - duk_small_int_t ch; - duk_small_int_t t = 0; - - while (n > 0) { - t = t * 16; - ch = (duk_small_int_t) duk_hex_dectab[*p++]; - if (DUK_LIKELY(ch >= 0)) { - t += ch; - } else { - return -1; - } - n--; - } - return t; -} - -DUK_LOCAL int duk__transform_helper(duk_context *ctx, duk__transform_callback callback, const void *udata) { - duk_hthread *thr = (duk_hthread *) ctx; - duk__transform_context tfm_ctx_alloc; - duk__transform_context *tfm_ctx = &tfm_ctx_alloc; - duk_codepoint_t cp; - - tfm_ctx->thr = thr; - - tfm_ctx->h_str = duk_to_hstring(ctx, 0); - DUK_ASSERT(tfm_ctx->h_str != NULL); - - DUK_BW_INIT_PUSHBUF(thr, &tfm_ctx->bw, DUK_HSTRING_GET_BYTELEN(tfm_ctx->h_str)); /* initial size guess */ - - tfm_ctx->p_start = DUK_HSTRING_GET_DATA(tfm_ctx->h_str); - tfm_ctx->p_end = tfm_ctx->p_start + DUK_HSTRING_GET_BYTELEN(tfm_ctx->h_str); - tfm_ctx->p = tfm_ctx->p_start; - - while (tfm_ctx->p < tfm_ctx->p_end) { - cp = (duk_codepoint_t) duk_unicode_decode_xutf8_checked(thr, &tfm_ctx->p, tfm_ctx->p_start, tfm_ctx->p_end); - callback(tfm_ctx, udata, cp); - } - - DUK_BW_COMPACT(thr, &tfm_ctx->bw); - - duk_to_string(ctx, -1); - return 1; -} - -DUK_LOCAL void duk__transform_callback_encode_uri(duk__transform_context *tfm_ctx, const void *udata, duk_codepoint_t cp) { - duk_uint8_t xutf8_buf[DUK_UNICODE_MAX_XUTF8_LENGTH]; - duk_small_int_t len; - duk_codepoint_t cp1, cp2; - duk_small_int_t i, t; - const duk_uint8_t *unescaped_table = (const duk_uint8_t *) udata; - - /* UTF-8 encoded bytes escaped as %xx%xx%xx... -> 3 * nbytes. - * Codepoint range is restricted so this is a slightly too large - * but doesn't matter. - */ - DUK_BW_ENSURE(tfm_ctx->thr, &tfm_ctx->bw, 3 * DUK_UNICODE_MAX_XUTF8_LENGTH); - - if (cp < 0) { - goto uri_error; - } else if ((cp < 0x80L) && DUK__CHECK_BITMASK(unescaped_table, cp)) { - DUK_BW_WRITE_RAW_U8(tfm_ctx->thr, &tfm_ctx->bw, (duk_uint8_t) cp); - return; - } else if (cp >= 0xdc00L && cp <= 0xdfffL) { - goto uri_error; - } else if (cp >= 0xd800L && cp <= 0xdbffL) { - /* Needs lookahead */ - if (duk_unicode_decode_xutf8(tfm_ctx->thr, &tfm_ctx->p, tfm_ctx->p_start, tfm_ctx->p_end, (duk_ucodepoint_t *) &cp2) == 0) { - goto uri_error; - } - if (!(cp2 >= 0xdc00L && cp2 <= 0xdfffL)) { - goto uri_error; - } - cp1 = cp; - cp = ((cp1 - 0xd800L) << 10) + (cp2 - 0xdc00L) + 0x10000L; - } else if (cp > 0x10ffffL) { - /* Although we can allow non-BMP characters (they'll decode - * back into surrogate pairs), we don't allow extended UTF-8 - * characters; they would encode to URIs which won't decode - * back because of strict UTF-8 checks in URI decoding. - * (However, we could just as well allow them here.) - */ - goto uri_error; - } else { - /* Non-BMP characters within valid UTF-8 range: encode as is. - * They'll decode back into surrogate pairs if the escaped - * output is decoded. - */ - ; - } - - len = duk_unicode_encode_xutf8((duk_ucodepoint_t) cp, xutf8_buf); - for (i = 0; i < len; i++) { - t = (int) xutf8_buf[i]; - DUK_BW_WRITE_RAW_U8_3(tfm_ctx->thr, - &tfm_ctx->bw, - DUK_ASC_PERCENT, - (duk_uint8_t) duk_uc_nybbles[t >> 4], - (duk_uint8_t) duk_uc_nybbles[t & 0x0f]); - } - - return; - - uri_error: - DUK_ERROR(tfm_ctx->thr, DUK_ERR_URI_ERROR, "invalid input"); -} - -DUK_LOCAL void duk__transform_callback_decode_uri(duk__transform_context *tfm_ctx, const void *udata, duk_codepoint_t cp) { - const duk_uint8_t *reserved_table = (const duk_uint8_t *) udata; - duk_small_uint_t utf8_blen; - duk_codepoint_t min_cp; - duk_small_int_t t; /* must be signed */ - duk_small_uint_t i; - - /* Maximum write size: XUTF8 path writes max DUK_UNICODE_MAX_XUTF8_LENGTH, - * percent escape path writes max two times CESU-8 encoded BMP length. - */ - DUK_BW_ENSURE(tfm_ctx->thr, - &tfm_ctx->bw, - (DUK_UNICODE_MAX_XUTF8_LENGTH >= 2 * DUK_UNICODE_MAX_CESU8_BMP_LENGTH ? - DUK_UNICODE_MAX_XUTF8_LENGTH : DUK_UNICODE_MAX_CESU8_BMP_LENGTH)); - - if (cp == (duk_codepoint_t) '%') { - const duk_uint8_t *p = tfm_ctx->p; - duk_size_t left = (duk_size_t) (tfm_ctx->p_end - p); /* bytes left */ - - DUK_DDD(DUK_DDDPRINT("percent encoding, left=%ld", (long) left)); - - if (left < 2) { - goto uri_error; - } - - t = duk__decode_hex_escape(p, 2); - DUK_DDD(DUK_DDDPRINT("first byte: %ld", (long) t)); - if (t < 0) { - goto uri_error; - } - - if (t < 0x80) { - if (DUK__CHECK_BITMASK(reserved_table, t)) { - /* decode '%xx' to '%xx' if decoded char in reserved set */ - DUK_ASSERT(tfm_ctx->p - 1 >= tfm_ctx->p_start); - DUK_BW_WRITE_RAW_U8_3(tfm_ctx->thr, - &tfm_ctx->bw, - DUK_ASC_PERCENT, - p[0], - p[1]); - } else { - DUK_BW_WRITE_RAW_U8(tfm_ctx->thr, &tfm_ctx->bw, (duk_uint8_t) t); - } - tfm_ctx->p += 2; - return; - } - - /* Decode UTF-8 codepoint from a sequence of hex escapes. The - * first byte of the sequence has been decoded to 't'. - * - * Note that UTF-8 validation must be strict according to the - * specification: E5.1 Section 15.1.3, decode algorithm step - * 4.d.vii.8. URIError from non-shortest encodings is also - * specifically noted in the spec. - */ - - DUK_ASSERT(t >= 0x80); - if (t < 0xc0) { - /* continuation byte */ - goto uri_error; - } else if (t < 0xe0) { - /* 110x xxxx; 2 bytes */ - utf8_blen = 2; - min_cp = 0x80L; - cp = t & 0x1f; - } else if (t < 0xf0) { - /* 1110 xxxx; 3 bytes */ - utf8_blen = 3; - min_cp = 0x800L; - cp = t & 0x0f; - } else if (t < 0xf8) { - /* 1111 0xxx; 4 bytes */ - utf8_blen = 4; - min_cp = 0x10000L; - cp = t & 0x07; - } else { - /* extended utf-8 not allowed for URIs */ - goto uri_error; - } - - if (left < utf8_blen * 3 - 1) { - /* '%xx%xx...%xx', p points to char after first '%' */ - goto uri_error; - } - - p += 3; - for (i = 1; i < utf8_blen; i++) { - /* p points to digit part ('%xy', p points to 'x') */ - t = duk__decode_hex_escape(p, 2); - DUK_DDD(DUK_DDDPRINT("i=%ld utf8_blen=%ld cp=%ld t=0x%02lx", - (long) i, (long) utf8_blen, (long) cp, (unsigned long) t)); - if (t < 0) { - goto uri_error; - } - if ((t & 0xc0) != 0x80) { - goto uri_error; - } - cp = (cp << 6) + (t & 0x3f); - p += 3; - } - p--; /* p overshoots */ - tfm_ctx->p = p; - - DUK_DDD(DUK_DDDPRINT("final cp=%ld, min_cp=%ld", (long) cp, (long) min_cp)); - - if (cp < min_cp || cp > 0x10ffffL || (cp >= 0xd800L && cp <= 0xdfffL)) { - goto uri_error; - } - - /* The E5.1 algorithm checks whether or not a decoded codepoint - * is below 0x80 and perhaps may be in the "reserved" set. - * This seems pointless because the single byte UTF-8 case is - * handled separately, and non-shortest encodings are rejected. - * So, 'cp' cannot be below 0x80 here, and thus cannot be in - * the reserved set. - */ - - /* utf-8 validation ensures these */ - DUK_ASSERT(cp >= 0x80L && cp <= 0x10ffffL); - - if (cp >= 0x10000L) { - cp -= 0x10000L; - DUK_ASSERT(cp < 0x100000L); - - DUK_BW_WRITE_RAW_XUTF8(tfm_ctx->thr, &tfm_ctx->bw, ((cp >> 10) + 0xd800L)); - DUK_BW_WRITE_RAW_XUTF8(tfm_ctx->thr, &tfm_ctx->bw, ((cp & 0x03ffUL) + 0xdc00L)); - } else { - DUK_BW_WRITE_RAW_XUTF8(tfm_ctx->thr, &tfm_ctx->bw, cp); - } - } else { - DUK_BW_WRITE_RAW_XUTF8(tfm_ctx->thr, &tfm_ctx->bw, cp); - } - return; - - uri_error: - DUK_ERROR(tfm_ctx->thr, DUK_ERR_URI_ERROR, "invalid input"); -} - -#ifdef DUK_USE_SECTION_B -DUK_LOCAL void duk__transform_callback_escape(duk__transform_context *tfm_ctx, const void *udata, duk_codepoint_t cp) { - DUK_UNREF(udata); - - DUK_BW_ENSURE(tfm_ctx->thr, &tfm_ctx->bw, 6); - - if (cp < 0) { - goto esc_error; - } else if ((cp < 0x80L) && DUK__CHECK_BITMASK(duk__escape_unescaped_table, cp)) { - DUK_BW_WRITE_RAW_U8(tfm_ctx->thr, &tfm_ctx->bw, (duk_uint8_t) cp); - } else if (cp < 0x100L) { - DUK_BW_WRITE_RAW_U8_3(tfm_ctx->thr, - &tfm_ctx->bw, - (duk_uint8_t) DUK_ASC_PERCENT, - (duk_uint8_t) duk_uc_nybbles[cp >> 4], - (duk_uint8_t) duk_uc_nybbles[cp & 0x0f]); - } else if (cp < 0x10000L) { - DUK_BW_WRITE_RAW_U8_6(tfm_ctx->thr, - &tfm_ctx->bw, - (duk_uint8_t) DUK_ASC_PERCENT, - (duk_uint8_t) DUK_ASC_LC_U, - (duk_uint8_t) duk_uc_nybbles[cp >> 12], - (duk_uint8_t) duk_uc_nybbles[(cp >> 8) & 0x0f], - (duk_uint8_t) duk_uc_nybbles[(cp >> 4) & 0x0f], - (duk_uint8_t) duk_uc_nybbles[cp & 0x0f]); - } else { - /* Characters outside BMP cannot be escape()'d. We could - * encode them as surrogate pairs (for codepoints inside - * valid UTF-8 range, but not extended UTF-8). Because - * escape() and unescape() are legacy functions, we don't. - */ - goto esc_error; - } - - return; - - esc_error: - DUK_ERROR_TYPE(tfm_ctx->thr, "invalid input"); -} - -DUK_LOCAL void duk__transform_callback_unescape(duk__transform_context *tfm_ctx, const void *udata, duk_codepoint_t cp) { - duk_small_int_t t; - - DUK_UNREF(udata); - - if (cp == (duk_codepoint_t) '%') { - const duk_uint8_t *p = tfm_ctx->p; - duk_size_t left = (duk_size_t) (tfm_ctx->p_end - p); /* bytes left */ - - if (left >= 5 && p[0] == 'u' && - ((t = duk__decode_hex_escape(p + 1, 4)) >= 0)) { - cp = (duk_codepoint_t) t; - tfm_ctx->p += 5; - } else if (left >= 2 && - ((t = duk__decode_hex_escape(p, 2)) >= 0)) { - cp = (duk_codepoint_t) t; - tfm_ctx->p += 2; - } - } - - DUK_BW_WRITE_ENSURE_XUTF8(tfm_ctx->thr, &tfm_ctx->bw, cp); -} -#endif /* DUK_USE_SECTION_B */ - -/* - * Eval - * - * Eval needs to handle both a "direct eval" and an "indirect eval". - * Direct eval handling needs access to the caller's activation so that its - * lexical environment can be accessed. A direct eval is only possible from - * Ecmascript code; an indirect eval call is possible also from C code. - * When an indirect eval call is made from C code, there may not be a - * calling activation at all which needs careful handling. - */ - -DUK_INTERNAL duk_ret_t duk_bi_global_object_eval(duk_context *ctx) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_hstring *h; - duk_activation *act_caller; - duk_activation *act_eval; - duk_activation *act; - duk_hcompiledfunction *func; - duk_hobject *outer_lex_env; - duk_hobject *outer_var_env; - duk_bool_t this_to_global = 1; - duk_small_uint_t comp_flags; - duk_int_t level = -2; - - DUK_ASSERT(duk_get_top(ctx) == 1 || duk_get_top(ctx) == 2); /* 2 when called by debugger */ - DUK_ASSERT(thr->callstack_top >= 1); /* at least this function exists */ - DUK_ASSERT(((thr->callstack + thr->callstack_top - 1)->flags & DUK_ACT_FLAG_DIRECT_EVAL) == 0 || /* indirect eval */ - (thr->callstack_top >= 2)); /* if direct eval, calling activation must exist */ - - /* - * callstack_top - 1 --> this function - * callstack_top - 2 --> caller (may not exist) - * - * If called directly from C, callstack_top might be 1. If calling - * activation doesn't exist, call must be indirect. - */ - - h = duk_get_hstring(ctx, 0); - if (!h) { - return 1; /* return arg as-is */ - } - -#if defined(DUK_USE_DEBUGGER_SUPPORT) - /* NOTE: level is used only by the debugger and should never be present - * for an Ecmascript eval(). - */ - DUK_ASSERT(level == -2); /* by default, use caller's environment */ - if (duk_get_top(ctx) >= 2 && duk_is_number(ctx, 1)) { - level = duk_get_int(ctx, 1); - } - DUK_ASSERT(level <= -2); /* This is guaranteed by debugger code. */ -#endif - - /* [ source ] */ - - comp_flags = DUK_JS_COMPILE_FLAG_EVAL; - act_eval = thr->callstack + thr->callstack_top - 1; /* this function */ - if (thr->callstack_top >= (duk_size_t) -level) { - /* Have a calling activation, check for direct eval (otherwise - * assume indirect eval. - */ - act_caller = thr->callstack + thr->callstack_top + level; /* caller */ - if ((act_caller->flags & DUK_ACT_FLAG_STRICT) && - (act_eval->flags & DUK_ACT_FLAG_DIRECT_EVAL)) { - /* Only direct eval inherits strictness from calling code - * (E5.1 Section 10.1.1). - */ - comp_flags |= DUK_JS_COMPILE_FLAG_STRICT; - } - } else { - DUK_ASSERT((act_eval->flags & DUK_ACT_FLAG_DIRECT_EVAL) == 0); - } - act_caller = NULL; /* avoid dereference after potential callstack realloc */ - act_eval = NULL; - - duk_push_hstring_stridx(ctx, DUK_STRIDX_INPUT); /* XXX: copy from caller? */ - duk_js_compile(thr, - (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h), - (duk_size_t) DUK_HSTRING_GET_BYTELEN(h), - comp_flags); - func = (duk_hcompiledfunction *) duk_get_hobject(ctx, -1); - DUK_ASSERT(func != NULL); - DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION((duk_hobject *) func)); - - /* [ source template ] */ - - /* E5 Section 10.4.2 */ - DUK_ASSERT(thr->callstack_top >= 1); - act = thr->callstack + thr->callstack_top - 1; /* this function */ - if (act->flags & DUK_ACT_FLAG_DIRECT_EVAL) { - DUK_ASSERT(thr->callstack_top >= 2); - act = thr->callstack + thr->callstack_top + level; /* caller */ - if (act->lex_env == NULL) { - DUK_ASSERT(act->var_env == NULL); - DUK_DDD(DUK_DDDPRINT("delayed environment initialization")); - - /* this may have side effects, so re-lookup act */ - duk_js_init_activation_environment_records_delayed(thr, act); - act = thr->callstack + thr->callstack_top + level; - } - DUK_ASSERT(act->lex_env != NULL); - DUK_ASSERT(act->var_env != NULL); - - this_to_global = 0; - - if (DUK_HOBJECT_HAS_STRICT((duk_hobject *) func)) { - duk_hobject *new_env; - duk_hobject *act_lex_env; - - DUK_DDD(DUK_DDDPRINT("direct eval call to a strict function -> " - "var_env and lex_env to a fresh env, " - "this_binding to caller's this_binding")); - - act = thr->callstack + thr->callstack_top + level; /* caller */ - act_lex_env = act->lex_env; - act = NULL; /* invalidated */ - - (void) duk_push_object_helper_proto(ctx, - DUK_HOBJECT_FLAG_EXTENSIBLE | - DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_DECENV), - act_lex_env); - new_env = duk_require_hobject(ctx, -1); - DUK_ASSERT(new_env != NULL); - DUK_DDD(DUK_DDDPRINT("new_env allocated: %!iO", - (duk_heaphdr *) new_env)); - - outer_lex_env = new_env; - outer_var_env = new_env; - - duk_insert(ctx, 0); /* stash to bottom of value stack to keep new_env reachable for duration of eval */ - - /* compiler's responsibility */ - DUK_ASSERT(DUK_HOBJECT_HAS_NEWENV((duk_hobject *) func)); - } else { - DUK_DDD(DUK_DDDPRINT("direct eval call to a non-strict function -> " - "var_env and lex_env to caller's envs, " - "this_binding to caller's this_binding")); - - outer_lex_env = act->lex_env; - outer_var_env = act->var_env; - - /* compiler's responsibility */ - DUK_ASSERT(!DUK_HOBJECT_HAS_NEWENV((duk_hobject *) func)); - } - } else { - DUK_DDD(DUK_DDDPRINT("indirect eval call -> var_env and lex_env to " - "global object, this_binding to global object")); - - this_to_global = 1; - outer_lex_env = thr->builtins[DUK_BIDX_GLOBAL_ENV]; - outer_var_env = thr->builtins[DUK_BIDX_GLOBAL_ENV]; - } - act = NULL; - - /* Eval code doesn't need an automatic .prototype object. */ - duk_js_push_closure(thr, func, outer_var_env, outer_lex_env, 0 /*add_auto_proto*/); - - /* [ source template closure ] */ - - if (this_to_global) { - DUK_ASSERT(thr->builtins[DUK_BIDX_GLOBAL] != NULL); - duk_push_hobject_bidx(ctx, DUK_BIDX_GLOBAL); - } else { - duk_tval *tv; - DUK_ASSERT(thr->callstack_top >= 2); - act = thr->callstack + thr->callstack_top + level; /* caller */ - tv = thr->valstack + act->idx_bottom - 1; /* this is just beneath bottom */ - DUK_ASSERT(tv >= thr->valstack); - duk_push_tval(ctx, tv); - } - - DUK_DDD(DUK_DDDPRINT("eval -> lex_env=%!iO, var_env=%!iO, this_binding=%!T", - (duk_heaphdr *) outer_lex_env, - (duk_heaphdr *) outer_var_env, - duk_get_tval(ctx, -1))); - - /* [ source template closure this ] */ - - duk_call_method(ctx, 0); - - /* [ source template result ] */ - - return 1; -} - -/* - * Parsing of ints and floats - */ - -DUK_INTERNAL duk_ret_t duk_bi_global_object_parse_int(duk_context *ctx) { - duk_int32_t radix; - duk_small_uint_t s2n_flags; - - DUK_ASSERT_TOP(ctx, 2); - duk_to_string(ctx, 0); - - radix = duk_to_int32(ctx, 1); - - s2n_flags = DUK_S2N_FLAG_TRIM_WHITE | - DUK_S2N_FLAG_ALLOW_GARBAGE | - DUK_S2N_FLAG_ALLOW_PLUS | - DUK_S2N_FLAG_ALLOW_MINUS | - DUK_S2N_FLAG_ALLOW_LEADING_ZERO | - DUK_S2N_FLAG_ALLOW_AUTO_HEX_INT; - - /* Specification stripPrefix maps to DUK_S2N_FLAG_ALLOW_AUTO_HEX_INT. - * - * Don't autodetect octals (from leading zeroes), require user code to - * provide an explicit radix 8 for parsing octal. See write-up from Mozilla: - * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt#ECMAScript_5_Removes_Octal_Interpretation - */ - - if (radix != 0) { - if (radix < 2 || radix > 36) { - goto ret_nan; - } - if (radix != 16) { - s2n_flags &= ~DUK_S2N_FLAG_ALLOW_AUTO_HEX_INT; - } - } else { - radix = 10; - } - - duk_dup(ctx, 0); - duk_numconv_parse(ctx, radix, s2n_flags); - return 1; - - ret_nan: - duk_push_nan(ctx); - return 1; -} - -DUK_INTERNAL duk_ret_t duk_bi_global_object_parse_float(duk_context *ctx) { - duk_small_uint_t s2n_flags; - duk_int32_t radix; - - DUK_ASSERT_TOP(ctx, 1); - duk_to_string(ctx, 0); - - radix = 10; - - /* XXX: check flags */ - s2n_flags = DUK_S2N_FLAG_TRIM_WHITE | - DUK_S2N_FLAG_ALLOW_EXP | - DUK_S2N_FLAG_ALLOW_GARBAGE | - DUK_S2N_FLAG_ALLOW_PLUS | - DUK_S2N_FLAG_ALLOW_MINUS | - DUK_S2N_FLAG_ALLOW_INF | - DUK_S2N_FLAG_ALLOW_FRAC | - DUK_S2N_FLAG_ALLOW_NAKED_FRAC | - DUK_S2N_FLAG_ALLOW_EMPTY_FRAC | - DUK_S2N_FLAG_ALLOW_LEADING_ZERO; - - duk_numconv_parse(ctx, radix, s2n_flags); - return 1; -} - -/* - * Number checkers - */ - -DUK_INTERNAL duk_ret_t duk_bi_global_object_is_nan(duk_context *ctx) { - duk_double_t d = duk_to_number(ctx, 0); - duk_push_boolean(ctx, DUK_ISNAN(d)); - return 1; -} - -DUK_INTERNAL duk_ret_t duk_bi_global_object_is_finite(duk_context *ctx) { - duk_double_t d = duk_to_number(ctx, 0); - duk_push_boolean(ctx, DUK_ISFINITE(d)); - return 1; -} - -/* - * URI handling - */ - -DUK_INTERNAL duk_ret_t duk_bi_global_object_decode_uri(duk_context *ctx) { - return duk__transform_helper(ctx, duk__transform_callback_decode_uri, (const void *) duk__decode_uri_reserved_table); -} - -DUK_INTERNAL duk_ret_t duk_bi_global_object_decode_uri_component(duk_context *ctx) { - return duk__transform_helper(ctx, duk__transform_callback_decode_uri, (const void *) duk__decode_uri_component_reserved_table); -} - -DUK_INTERNAL duk_ret_t duk_bi_global_object_encode_uri(duk_context *ctx) { - return duk__transform_helper(ctx, duk__transform_callback_encode_uri, (const void *) duk__encode_uriunescaped_table); -} - -DUK_INTERNAL duk_ret_t duk_bi_global_object_encode_uri_component(duk_context *ctx) { - return duk__transform_helper(ctx, duk__transform_callback_encode_uri, (const void *) duk__encode_uricomponent_unescaped_table); -} - -#ifdef DUK_USE_SECTION_B -DUK_INTERNAL duk_ret_t duk_bi_global_object_escape(duk_context *ctx) { - return duk__transform_helper(ctx, duk__transform_callback_escape, (const void *) NULL); -} - -DUK_INTERNAL duk_ret_t duk_bi_global_object_unescape(duk_context *ctx) { - return duk__transform_helper(ctx, duk__transform_callback_unescape, (const void *) NULL); -} -#else /* DUK_USE_SECTION_B */ -DUK_INTERNAL duk_ret_t duk_bi_global_object_escape(duk_context *ctx) { - DUK_UNREF(ctx); - return DUK_RET_UNSUPPORTED_ERROR; -} - -DUK_INTERNAL duk_ret_t duk_bi_global_object_unescape(duk_context *ctx) { - DUK_UNREF(ctx); - return DUK_RET_UNSUPPORTED_ERROR; -} -#endif /* DUK_USE_SECTION_B */ - -#if defined(DUK_USE_BROWSER_LIKE) && (defined(DUK_USE_FILE_IO) || defined(DUK_USE_DEBUGGER_SUPPORT)) -DUK_INTERNAL duk_ret_t duk_bi_global_object_print_helper(duk_context *ctx) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_int_t magic; - duk_idx_t nargs; - const duk_uint8_t *buf; - duk_size_t sz_buf; - const char nl = (const char) DUK_ASC_LF; -#ifndef DUK_USE_PREFER_SIZE - duk_uint8_t buf_stack[256]; -#endif -#ifdef DUK_USE_FILE_IO - duk_file *f_out; -#endif - - DUK_UNREF(thr); - - magic = duk_get_current_magic(ctx); - DUK_UNREF(magic); - - nargs = duk_get_top(ctx); - - /* If argument count is 1 and first argument is a buffer, write the buffer - * as raw data into the file without a newline; this allows exact control - * over stdout/stderr without an additional entrypoint (useful for now). - * - * Otherwise current print/alert semantics are to ToString() coerce - * arguments, join them with a single space, and append a newline. - */ - - if (nargs == 1 && duk_is_buffer(ctx, 0)) { - buf = (const duk_uint8_t *) duk_get_buffer(ctx, 0, &sz_buf); - DUK_ASSERT(buf != NULL); - } else if (nargs > 0) { -#ifdef DUK_USE_PREFER_SIZE - /* Compact but lots of churn. */ - duk_push_hstring_stridx(thr, DUK_STRIDX_SPACE); - duk_insert(ctx, 0); - duk_join(ctx, nargs); - duk_push_string(thr, "\n"); - duk_concat(ctx, 2); - buf = (const duk_uint8_t *) duk_get_lstring(ctx, -1, &sz_buf); - DUK_ASSERT(buf != NULL); -#else /* DUK_USE_PREFER_SIZE */ - /* Higher footprint, less churn. */ - duk_idx_t i; - duk_size_t sz_str; - const duk_uint8_t *p_str; - duk_uint8_t *p; - - sz_buf = (duk_size_t) nargs; /* spaces (nargs - 1) + newline */ - for (i = 0; i < nargs; i++) { - (void) duk_to_lstring(ctx, i, &sz_str); - sz_buf += sz_str; - } - - if (sz_buf <= sizeof(buf_stack)) { - p = (duk_uint8_t *) buf_stack; - } else { - p = (duk_uint8_t *) duk_push_fixed_buffer(ctx, sz_buf); - DUK_ASSERT(p != NULL); - } - - buf = (const duk_uint8_t *) p; - for (i = 0; i < nargs; i++) { - p_str = (const duk_uint8_t *) duk_get_lstring(ctx, i, &sz_str); - DUK_ASSERT(p_str != NULL); - DUK_MEMCPY((void *) p, (const void *) p_str, sz_str); - p += sz_str; - *p++ = (duk_uint8_t) (i == nargs - 1 ? DUK_ASC_LF : DUK_ASC_SPACE); - } - DUK_ASSERT((const duk_uint8_t *) p == buf + sz_buf); -#endif /* DUK_USE_PREFER_SIZE */ - } else { - buf = (const duk_uint8_t *) &nl; - sz_buf = 1; - } - - /* 'buf' contains the string to write, 'sz_buf' contains the length - * (which may be zero). - */ - DUK_ASSERT(buf != NULL); - - if (sz_buf == 0) { - return 0; - } - -#ifdef DUK_USE_FILE_IO - f_out = (magic ? DUK_STDERR : DUK_STDOUT); - DUK_FWRITE((const void *) buf, 1, (size_t) sz_buf, f_out); - DUK_FFLUSH(f_out); -#endif - -#if defined(DUK_USE_DEBUGGER_SUPPORT) && defined(DUK_USE_DEBUGGER_FWD_PRINTALERT) - if (DUK_HEAP_IS_DEBUGGER_ATTACHED(thr->heap)) { - duk_debug_write_notify(thr, magic ? DUK_DBG_CMD_ALERT : DUK_DBG_CMD_PRINT); - duk_debug_write_string(thr, (const char *) buf, sz_buf); - duk_debug_write_eom(thr); - } -#endif - return 0; -} -#elif defined(DUK_USE_BROWSER_LIKE) /* print provider */ -DUK_INTERNAL duk_ret_t duk_bi_global_object_print_helper(duk_context *ctx) { - DUK_UNREF(ctx); - return 0; -} -#else /* print provider */ -DUK_INTERNAL duk_ret_t duk_bi_global_object_print_helper(duk_context *ctx) { - DUK_UNREF(ctx); - return DUK_RET_UNSUPPORTED_ERROR; -} -#endif /* print provider */ - -/* - * CommonJS require() and modules support - */ - -#if defined(DUK_USE_COMMONJS_MODULES) -DUK_LOCAL void duk__bi_global_resolve_module_id(duk_context *ctx, const char *req_id, const char *mod_id) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_uint8_t buf[DUK_BI_COMMONJS_MODULE_ID_LIMIT]; - duk_uint8_t *p; - duk_uint8_t *q; - duk_uint8_t *q_last; /* last component */ - duk_int_t int_rc; - - DUK_ASSERT(req_id != NULL); - /* mod_id may be NULL */ - - /* - * A few notes on the algorithm: - * - * - Terms are not allowed to begin with a period unless the term - * is either '.' or '..'. This simplifies implementation (and - * is within CommonJS modules specification). - * - * - There are few output bound checks here. This is on purpose: - * the resolution input is length checked and the output is never - * longer than the input. The resolved output is written directly - * over the input because it's never longer than the input at any - * point in the algorithm. - * - * - Non-ASCII characters are processed as individual bytes and - * need no special treatment. However, U+0000 terminates the - * algorithm; this is not an issue because U+0000 is not a - * desirable term character anyway. - */ - - /* - * Set up the resolution input which is the requested ID directly - * (if absolute or no current module path) or with current module - * ID prepended (if relative and current module path exists). - * - * Suppose current module is 'foo/bar' and relative path is './quux'. - * The 'bar' component must be replaced so the initial input here is - * 'foo/bar/.././quux'. - */ - - if (mod_id != NULL && req_id[0] == '.') { - int_rc = DUK_SNPRINTF((char *) buf, sizeof(buf), "%s/../%s", mod_id, req_id); - } else { - int_rc = DUK_SNPRINTF((char *) buf, sizeof(buf), "%s", req_id); - } - if (int_rc >= (duk_int_t) sizeof(buf) || int_rc < 0) { - /* Potentially truncated, NUL not guaranteed in any case. - * The (int_rc < 0) case should not occur in practice. - */ - DUK_DD(DUK_DDPRINT("resolve error: temporary working module ID doesn't fit into resolve buffer")); - goto resolve_error; - } - DUK_ASSERT(DUK_STRLEN((const char *) buf) < sizeof(buf)); /* at most sizeof(buf) - 1 */ - - DUK_DDD(DUK_DDDPRINT("input module id: '%s'", (const char *) buf)); - - /* - * Resolution loop. At the top of the loop we're expecting a valid - * term: '.', '..', or a non-empty identifier not starting with a period. - */ - - p = buf; - q = buf; - for (;;) { - duk_uint_fast8_t c; - - /* Here 'p' always points to the start of a term. - * - * We can also unconditionally reset q_last here: if this is - * the last (non-empty) term q_last will have the right value - * on loop exit. - */ - - DUK_ASSERT(p >= q); /* output is never longer than input during resolution */ - - DUK_DDD(DUK_DDDPRINT("resolve loop top: p -> '%s', q=%p, buf=%p", - (const char *) p, (void *) q, (void *) buf)); - - q_last = q; - - c = *p++; - if (DUK_UNLIKELY(c == 0)) { - DUK_DD(DUK_DDPRINT("resolve error: requested ID must end with a non-empty term")); - goto resolve_error; - } else if (DUK_UNLIKELY(c == '.')) { - c = *p++; - if (c == '/') { - /* Term was '.' and is eaten entirely (including dup slashes). */ - goto eat_dup_slashes; - } - if (c == '.' && *p == '/') { - /* Term was '..', backtrack resolved name by one component. - * q[-1] = previous slash (or beyond start of buffer) - * q[-2] = last char of previous component (or beyond start of buffer) - */ - p++; /* eat (first) input slash */ - DUK_ASSERT(q >= buf); - if (q == buf) { - DUK_DD(DUK_DDPRINT("resolve error: term was '..' but nothing to backtrack")); - goto resolve_error; - } - DUK_ASSERT(*(q - 1) == '/'); - q--; /* backtrack to last output slash (dups already eliminated) */ - for (;;) { - /* Backtrack to previous slash or start of buffer. */ - DUK_ASSERT(q >= buf); - if (q == buf) { - break; - } - if (*(q - 1) == '/') { - break; - } - q--; - } - goto eat_dup_slashes; - } - DUK_DD(DUK_DDPRINT("resolve error: term begins with '.' but is not '.' or '..' (not allowed now)")); - goto resolve_error; - } else if (DUK_UNLIKELY(c == '/')) { - /* e.g. require('/foo'), empty terms not allowed */ - DUK_DD(DUK_DDPRINT("resolve error: empty term (not allowed now)")); - goto resolve_error; - } else { - for (;;) { - /* Copy term name until end or '/'. */ - *q++ = c; - c = *p++; - if (DUK_UNLIKELY(c == 0)) { - /* This was the last term, and q_last was - * updated to match this term at loop top. - */ - goto loop_done; - } else if (DUK_UNLIKELY(c == '/')) { - *q++ = '/'; - break; - } else { - /* write on next loop */ - } - } - } - - eat_dup_slashes: - for (;;) { - /* eat dup slashes */ - c = *p; - if (DUK_LIKELY(c != '/')) { - break; - } - p++; - } - } - loop_done: - /* Output #1: resolved absolute name */ - DUK_ASSERT(q >= buf); - duk_push_lstring(ctx, (const char *) buf, (size_t) (q - buf)); - - /* Output #2: last component name */ - DUK_ASSERT(q >= q_last); - DUK_ASSERT(q_last >= buf); - duk_push_lstring(ctx, (const char *) q_last, (size_t) (q - q_last)); - - DUK_DD(DUK_DDPRINT("after resolving module name: buf=%p, q_last=%p, q=%p", - (void *) buf, (void *) q_last, (void *) q)); - return; - - resolve_error: - DUK_ERROR_FMT1(thr, DUK_ERR_TYPE_ERROR, "cannot resolve module id: %s", (const char *) req_id); -} -#endif /* DUK_USE_COMMONJS_MODULES */ - -#if defined(DUK_USE_COMMONJS_MODULES) -/* Stack indices for better readability */ -#define DUK__IDX_REQUESTED_ID 0 /* Module id requested */ -#define DUK__IDX_REQUIRE 1 /* Current require() function */ -#define DUK__IDX_REQUIRE_ID 2 /* The base ID of the current require() function, resolution base */ -#define DUK__IDX_RESOLVED_ID 3 /* Resolved, normalized absolute module ID */ -#define DUK__IDX_LASTCOMP 4 /* Last component name in resolved path */ -#define DUK__IDX_DUKTAPE 5 /* Duktape object */ -#define DUK__IDX_MODLOADED 6 /* Duktape.modLoaded[] module cache */ -#define DUK__IDX_UNDEFINED 7 /* 'undefined', artifact of lookup */ -#define DUK__IDX_FRESH_REQUIRE 8 /* New require() function for module, updated resolution base */ -#define DUK__IDX_EXPORTS 9 /* Default exports table */ -#define DUK__IDX_MODULE 10 /* Module object containing module.exports, etc */ - -DUK_INTERNAL duk_ret_t duk_bi_global_object_require(duk_context *ctx) { - const char *str_req_id; /* requested identifier */ - const char *str_mod_id; /* require.id of current module */ - duk_int_t pcall_rc; - - /* NOTE: we try to minimize code size by avoiding unnecessary pops, - * so the stack looks a bit cluttered in this function. DUK_ASSERT_TOP() - * assertions are used to ensure stack configuration is correct at each - * step. - */ - - /* - * Resolve module identifier into canonical absolute form. - */ - - str_req_id = duk_require_string(ctx, DUK__IDX_REQUESTED_ID); - duk_push_current_function(ctx); - duk_get_prop_stridx(ctx, -1, DUK_STRIDX_ID); - str_mod_id = duk_get_string(ctx, DUK__IDX_REQUIRE_ID); /* ignore non-strings */ - DUK_DDD(DUK_DDDPRINT("resolve module id: requested=%!T, currentmodule=%!T", - duk_get_tval(ctx, DUK__IDX_REQUESTED_ID), - duk_get_tval(ctx, DUK__IDX_REQUIRE_ID))); - duk__bi_global_resolve_module_id(ctx, str_req_id, str_mod_id); - str_req_id = NULL; - str_mod_id = NULL; - DUK_DDD(DUK_DDDPRINT("resolved module id: requested=%!T, currentmodule=%!T, result=%!T, lastcomp=%!T", - duk_get_tval(ctx, DUK__IDX_REQUESTED_ID), - duk_get_tval(ctx, DUK__IDX_REQUIRE_ID), - duk_get_tval(ctx, DUK__IDX_RESOLVED_ID), - duk_get_tval(ctx, DUK__IDX_LASTCOMP))); - - /* [ requested_id require require.id resolved_id last_comp ] */ - DUK_ASSERT_TOP(ctx, DUK__IDX_LASTCOMP + 1); - - /* - * Cached module check. - * - * If module has been loaded or its loading has already begun without - * finishing, return the same cached value ('exports'). The value is - * registered when module load starts so that circular references can - * be supported to some extent. - */ - - duk_push_hobject_bidx(ctx, DUK_BIDX_DUKTAPE); - duk_get_prop_stridx(ctx, DUK__IDX_DUKTAPE, DUK_STRIDX_MOD_LOADED); /* Duktape.modLoaded */ - (void) duk_require_hobject(ctx, DUK__IDX_MODLOADED); - DUK_ASSERT_TOP(ctx, DUK__IDX_MODLOADED + 1); - - duk_dup(ctx, DUK__IDX_RESOLVED_ID); - if (duk_get_prop(ctx, DUK__IDX_MODLOADED)) { - /* [ requested_id require require.id resolved_id last_comp Duktape Duktape.modLoaded Duktape.modLoaded[id] ] */ - DUK_DD(DUK_DDPRINT("module already loaded: %!T", - duk_get_tval(ctx, DUK__IDX_RESOLVED_ID))); - duk_get_prop_stridx(ctx, -1, DUK_STRIDX_EXPORTS); /* return module.exports */ - return 1; - } - DUK_ASSERT_TOP(ctx, DUK__IDX_UNDEFINED + 1); - - /* [ requested_id require require.id resolved_id last_comp Duktape Duktape.modLoaded undefined ] */ - - /* - * Module not loaded (and loading not started previously). - * - * Create a new require() function with 'id' set to resolved ID - * of module being loaded. Also create 'exports' and 'module' - * tables but don't register exports to the loaded table yet. - * We don't want to do that unless the user module search callbacks - * succeeds in finding the module. - */ - - DUK_D(DUK_DPRINT("loading module %!T, resolution base %!T, requested ID %!T -> resolved ID %!T, last component %!T", - duk_get_tval(ctx, DUK__IDX_RESOLVED_ID), - duk_get_tval(ctx, DUK__IDX_REQUIRE_ID), - duk_get_tval(ctx, DUK__IDX_REQUESTED_ID), - duk_get_tval(ctx, DUK__IDX_RESOLVED_ID), - duk_get_tval(ctx, DUK__IDX_LASTCOMP))); - - /* Fresh require: require.id is left configurable (but not writable) - * so that is not easy to accidentally tweak it, but it can still be - * done with Object.defineProperty(). - * - * XXX: require.id could also be just made non-configurable, as there - * is no practical reason to touch it. - */ - duk_push_c_function(ctx, duk_bi_global_object_require, 1 /*nargs*/); - duk_push_hstring_stridx(ctx, DUK_STRIDX_REQUIRE); - duk_xdef_prop_stridx(ctx, DUK__IDX_FRESH_REQUIRE, DUK_STRIDX_NAME, DUK_PROPDESC_FLAGS_NONE); - duk_dup(ctx, DUK__IDX_RESOLVED_ID); - duk_xdef_prop_stridx(ctx, DUK__IDX_FRESH_REQUIRE, DUK_STRIDX_ID, DUK_PROPDESC_FLAGS_C); /* a fresh require() with require.id = resolved target module id */ - - /* Module table: - * - module.exports: initial exports table (may be replaced by user) - * - module.id is non-writable and non-configurable, as the CommonJS - * spec suggests this if possible - * - module.filename: not set, defaults to resolved ID if not explicitly - * set by modSearch() (note capitalization, not .fileName, matches Node.js) - * - module.name: not set, defaults to last component of resolved ID if - * not explicitly set by modSearch() - */ - duk_push_object(ctx); /* exports */ - duk_push_object(ctx); /* module */ - duk_dup(ctx, DUK__IDX_EXPORTS); - duk_xdef_prop_stridx(ctx, DUK__IDX_MODULE, DUK_STRIDX_EXPORTS, DUK_PROPDESC_FLAGS_WC); /* module.exports = exports */ - duk_dup(ctx, DUK__IDX_RESOLVED_ID); /* resolved id: require(id) must return this same module */ - duk_xdef_prop_stridx(ctx, DUK__IDX_MODULE, DUK_STRIDX_ID, DUK_PROPDESC_FLAGS_NONE); /* module.id = resolved_id */ - duk_compact(ctx, DUK__IDX_MODULE); /* module table remains registered to modLoaded, minimize its size */ - DUK_ASSERT_TOP(ctx, DUK__IDX_MODULE + 1); - - DUK_DD(DUK_DDPRINT("module table created: %!T", duk_get_tval(ctx, DUK__IDX_MODULE))); - - /* [ requested_id require require.id resolved_id last_comp Duktape Duktape.modLoaded undefined fresh_require exports module ] */ - - /* Register the module table early to modLoaded[] so that we can - * support circular references even in modSearch(). If an error - * is thrown, we'll delete the reference. - */ - duk_dup(ctx, DUK__IDX_RESOLVED_ID); - duk_dup(ctx, DUK__IDX_MODULE); - duk_put_prop(ctx, DUK__IDX_MODLOADED); /* Duktape.modLoaded[resolved_id] = module */ - - /* - * Call user provided module search function and build the wrapped - * module source code (if necessary). The module search function - * can be used to implement pure Ecmacsript, pure C, and mixed - * Ecmascript/C modules. - * - * The module search function can operate on the exports table directly - * (e.g. DLL code can register values to it). It can also return a - * string which is interpreted as module source code (if a non-string - * is returned the module is assumed to be a pure C one). If a module - * cannot be found, an error must be thrown by the user callback. - * - * Because Duktape.modLoaded[] already contains the module being - * loaded, circular references for C modules should also work - * (although expected to be quite rare). - */ - - duk_push_string(ctx, "(function(require,exports,module){"); - - /* Duktape.modSearch(resolved_id, fresh_require, exports, module). */ - duk_get_prop_stridx(ctx, DUK__IDX_DUKTAPE, DUK_STRIDX_MOD_SEARCH); /* Duktape.modSearch */ - duk_dup(ctx, DUK__IDX_RESOLVED_ID); - duk_dup(ctx, DUK__IDX_FRESH_REQUIRE); - duk_dup(ctx, DUK__IDX_EXPORTS); - duk_dup(ctx, DUK__IDX_MODULE); /* [ ... Duktape.modSearch resolved_id last_comp fresh_require exports module ] */ - pcall_rc = duk_pcall(ctx, 4 /*nargs*/); /* -> [ ... source ] */ - DUK_ASSERT_TOP(ctx, DUK__IDX_MODULE + 3); - - if (pcall_rc != DUK_EXEC_SUCCESS) { - /* Delete entry in Duktape.modLoaded[] and rethrow. */ - goto delete_rethrow; - } - - /* If user callback did not return source code, module loading - * is finished (user callback initialized exports table directly). - */ - if (!duk_is_string(ctx, -1)) { - /* User callback did not return source code, so module loading - * is finished: just update modLoaded with final module.exports - * and we're done. - */ - goto return_exports; - } - - /* Finish the wrapped module source. Force module.filename as the - * function .fileName so it gets set for functions defined within a - * module. This also ensures loggers created within the module get - * the module ID (or overridden filename) as their default logger name. - * (Note capitalization: .filename matches Node.js while .fileName is - * used elsewhere in Duktape.) - */ - duk_push_string(ctx, "})"); - duk_concat(ctx, 3); - if (!duk_get_prop_stridx(ctx, DUK__IDX_MODULE, DUK_STRIDX_FILENAME)) { - /* module.filename for .fileName, default to resolved ID if - * not present. - */ - duk_pop(ctx); - duk_dup(ctx, DUK__IDX_RESOLVED_ID); - } - duk_eval_raw(ctx, NULL, 0, DUK_COMPILE_EVAL); - - /* Module has now evaluated to a wrapped module function. Force its - * .name to match module.name (defaults to last component of resolved - * ID) so that it is shown in stack traces too. Note that we must not - * introduce an actual name binding into the function scope (which is - * usually the case with a named function) because it would affect the - * scope seen by the module and shadow accesses to globals of the same name. - * This is now done by compiling the function as anonymous and then forcing - * its .name without setting a "has name binding" flag. - */ - - duk_push_hstring_stridx(ctx, DUK_STRIDX_NAME); - if (!duk_get_prop_stridx(ctx, DUK__IDX_MODULE, DUK_STRIDX_NAME)) { - /* module.name for .name, default to last component if - * not present. - */ - duk_pop(ctx); - duk_dup(ctx, DUK__IDX_LASTCOMP); - } - duk_def_prop(ctx, -3, DUK_DEFPROP_HAVE_VALUE | DUK_DEFPROP_FORCE); - - /* - * Call the wrapped module function. - * - * Use a protected call so that we can update Duktape.modLoaded[resolved_id] - * even if the module throws an error. - */ - - /* [ requested_id require require.id resolved_id last_comp Duktape Duktape.modLoaded undefined fresh_require exports module mod_func ] */ - DUK_ASSERT_TOP(ctx, DUK__IDX_MODULE + 2); - - duk_dup(ctx, DUK__IDX_EXPORTS); /* exports (this binding) */ - duk_dup(ctx, DUK__IDX_FRESH_REQUIRE); /* fresh require (argument) */ - duk_get_prop_stridx(ctx, DUK__IDX_MODULE, DUK_STRIDX_EXPORTS); /* relookup exports from module.exports in case it was changed by modSearch */ - duk_dup(ctx, DUK__IDX_MODULE); /* module (argument) */ - DUK_ASSERT_TOP(ctx, DUK__IDX_MODULE + 6); - - /* [ requested_id require require.id resolved_id last_comp Duktape Duktape.modLoaded undefined fresh_require exports module mod_func exports fresh_require exports module ] */ - - pcall_rc = duk_pcall_method(ctx, 3 /*nargs*/); - if (pcall_rc != DUK_EXEC_SUCCESS) { - /* Module loading failed. Node.js will forget the module - * registration so that another require() will try to load - * the module again. Mimic that behavior. - */ - goto delete_rethrow; - } - - /* [ requested_id require require.id resolved_id last_comp Duktape Duktape.modLoaded undefined fresh_require exports module result(ignored) ] */ - DUK_ASSERT_TOP(ctx, DUK__IDX_MODULE + 2); - - /* fall through */ - - return_exports: - duk_get_prop_stridx(ctx, DUK__IDX_MODULE, DUK_STRIDX_EXPORTS); - duk_compact(ctx, -1); /* compact the exports table */ - return 1; /* return module.exports */ - - delete_rethrow: - duk_dup(ctx, DUK__IDX_RESOLVED_ID); - duk_del_prop(ctx, DUK__IDX_MODLOADED); /* delete Duktape.modLoaded[resolved_id] */ - duk_throw(ctx); /* rethrow original error */ - return 0; /* not reachable */ -} - -#undef DUK__IDX_REQUESTED_ID -#undef DUK__IDX_REQUIRE -#undef DUK__IDX_REQUIRE_ID -#undef DUK__IDX_RESOLVED_ID -#undef DUK__IDX_LASTCOMP -#undef DUK__IDX_DUKTAPE -#undef DUK__IDX_MODLOADED -#undef DUK__IDX_UNDEFINED -#undef DUK__IDX_FRESH_REQUIRE -#undef DUK__IDX_EXPORTS -#undef DUK__IDX_MODULE -#else -DUK_INTERNAL duk_ret_t duk_bi_global_object_require(duk_context *ctx) { - DUK_UNREF(ctx); - return DUK_RET_UNSUPPORTED_ERROR; -} -#endif /* DUK_USE_COMMONJS_MODULES */ -#line 1 "duk_bi_json.c" -/* - * JSON built-ins. - * - * See doc/json.rst. - * - * Codepoints are handled as duk_uint_fast32_t to ensure that the full - * unsigned 32-bit range is supported. This matters to e.g. JX. - * - * Input parsing doesn't do an explicit end-of-input check at all. This is - * safe: input string data is always NUL-terminated (0x00) and valid JSON - * inputs never contain plain NUL characters, so that as long as syntax checks - * are correct, we'll never read past the NUL. This approach reduces code size - * and improves parsing performance, but it's critical that syntax checks are - * indeed correct! - */ - -/* include removed: duk_internal.h */ - -/* - * Local defines and forward declarations. - */ - -#define DUK__JSON_DECSTR_BUFSIZE 128 -#define DUK__JSON_DECSTR_CHUNKSIZE 64 -#define DUK__JSON_ENCSTR_CHUNKSIZE 64 -#define DUK__JSON_STRINGIFY_BUFSIZE 128 -#define DUK__JSON_MAX_ESC_LEN 10 /* '\Udeadbeef' */ - -DUK_LOCAL_DECL void duk__dec_syntax_error(duk_json_dec_ctx *js_ctx); -DUK_LOCAL_DECL void duk__dec_eat_white(duk_json_dec_ctx *js_ctx); -DUK_LOCAL_DECL duk_uint8_t duk__dec_peek(duk_json_dec_ctx *js_ctx); -DUK_LOCAL_DECL duk_uint8_t duk__dec_get(duk_json_dec_ctx *js_ctx); -DUK_LOCAL_DECL duk_uint8_t duk__dec_get_nonwhite(duk_json_dec_ctx *js_ctx); -DUK_LOCAL_DECL duk_uint_fast32_t duk__dec_decode_hex_escape(duk_json_dec_ctx *js_ctx, duk_small_uint_t n); -DUK_LOCAL_DECL void duk__dec_req_stridx(duk_json_dec_ctx *js_ctx, duk_small_uint_t stridx); -DUK_LOCAL_DECL void duk__dec_string(duk_json_dec_ctx *js_ctx); -#ifdef DUK_USE_JX -DUK_LOCAL_DECL void duk__dec_plain_string(duk_json_dec_ctx *js_ctx); -DUK_LOCAL_DECL void duk__dec_pointer(duk_json_dec_ctx *js_ctx); -DUK_LOCAL_DECL void duk__dec_buffer(duk_json_dec_ctx *js_ctx); -#endif -DUK_LOCAL_DECL void duk__dec_number(duk_json_dec_ctx *js_ctx); -DUK_LOCAL_DECL void duk__dec_objarr_entry(duk_json_dec_ctx *js_ctx); -DUK_LOCAL_DECL void duk__dec_objarr_exit(duk_json_dec_ctx *js_ctx); -DUK_LOCAL_DECL void duk__dec_object(duk_json_dec_ctx *js_ctx); -DUK_LOCAL_DECL void duk__dec_array(duk_json_dec_ctx *js_ctx); -DUK_LOCAL_DECL void duk__dec_value(duk_json_dec_ctx *js_ctx); -DUK_LOCAL_DECL void duk__dec_reviver_walk(duk_json_dec_ctx *js_ctx); - -DUK_LOCAL_DECL void duk__emit_1(duk_json_enc_ctx *js_ctx, duk_uint_fast8_t ch); -DUK_LOCAL_DECL void duk__emit_2(duk_json_enc_ctx *js_ctx, duk_uint_fast8_t ch1, duk_uint_fast8_t ch2); -DUK_LOCAL_DECL void duk__unemit_1(duk_json_enc_ctx *js_ctx); -DUK_LOCAL_DECL void duk__emit_hstring(duk_json_enc_ctx *js_ctx, duk_hstring *h); -#if defined(DUK_USE_FASTINT) -DUK_LOCAL_DECL void duk__emit_cstring(duk_json_enc_ctx *js_ctx, const char *p); -#endif -DUK_LOCAL_DECL void duk__emit_stridx(duk_json_enc_ctx *js_ctx, duk_small_uint_t stridx); -DUK_LOCAL_DECL duk_uint8_t *duk__emit_esc_auto_fast(duk_json_enc_ctx *js_ctx, duk_uint_fast32_t cp, duk_uint8_t *q); -DUK_LOCAL_DECL void duk__enc_key_autoquote(duk_json_enc_ctx *js_ctx, duk_hstring *k); -DUK_LOCAL_DECL void duk__enc_quote_string(duk_json_enc_ctx *js_ctx, duk_hstring *h_str); -DUK_LOCAL_DECL void duk__enc_objarr_entry(duk_json_enc_ctx *js_ctx, duk_idx_t *entry_top); -DUK_LOCAL_DECL void duk__enc_objarr_exit(duk_json_enc_ctx *js_ctx, duk_idx_t *entry_top); -DUK_LOCAL_DECL void duk__enc_object(duk_json_enc_ctx *js_ctx); -DUK_LOCAL_DECL void duk__enc_array(duk_json_enc_ctx *js_ctx); -DUK_LOCAL_DECL duk_bool_t duk__enc_value(duk_json_enc_ctx *js_ctx, duk_idx_t idx_holder); -DUK_LOCAL_DECL duk_bool_t duk__enc_allow_into_proplist(duk_tval *tv); -DUK_LOCAL_DECL void duk__enc_double(duk_json_enc_ctx *js_ctx); -#if defined(DUK_USE_FASTINT) -DUK_LOCAL_DECL void duk__enc_fastint_tval(duk_json_enc_ctx *js_ctx, duk_tval *tv); -#endif -#if defined(DUK_USE_JX) || defined(DUK_USE_JC) -DUK_LOCAL_DECL void duk__enc_buffer(duk_json_enc_ctx *js_ctx, duk_hbuffer *h); -DUK_LOCAL_DECL void duk__enc_pointer(duk_json_enc_ctx *js_ctx, void *ptr); -DUK_LOCAL_DECL void duk__enc_bufferobject(duk_json_enc_ctx *js_ctx, duk_hbufferobject *h_bufobj); -#endif -DUK_LOCAL_DECL void duk__enc_newline_indent(duk_json_enc_ctx *js_ctx, duk_int_t depth); - -/* - * Helper tables - */ - -#if defined(DUK_USE_JSON_QUOTESTRING_FASTPATH) -DUK_LOCAL const duk_uint8_t duk__json_quotestr_lookup[256] = { - /* 0x00 ... 0x7f: as is - * 0x80: escape generically - * 0x81: slow path - * 0xa0 ... 0xff: backslash + one char - */ - - 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0xe2, 0xf4, 0xee, 0x80, 0xe6, 0xf2, 0x80, 0x80, - 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, - 0x20, 0x21, 0xa2, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, - 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, - 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, - 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0xdc, 0x5d, 0x5e, 0x5f, - 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, - 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x81, - 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, - 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, - 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, - 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, - 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, - 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, - 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, - 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81 -}; -#else /* DUK_USE_JSON_QUOTESTRING_FASTPATH */ -DUK_LOCAL const duk_uint8_t duk__json_quotestr_esc[14] = { - DUK_ASC_NUL, DUK_ASC_NUL, DUK_ASC_NUL, DUK_ASC_NUL, - DUK_ASC_NUL, DUK_ASC_NUL, DUK_ASC_NUL, DUK_ASC_NUL, - DUK_ASC_LC_B, DUK_ASC_LC_T, DUK_ASC_LC_N, DUK_ASC_NUL, - DUK_ASC_LC_F, DUK_ASC_LC_R -}; -#endif /* DUK_USE_JSON_QUOTESTRING_FASTPATH */ - -#if defined(DUK_USE_JSON_DECSTRING_FASTPATH) -DUK_LOCAL const duk_uint8_t duk__json_decstr_lookup[256] = { - /* 0x00: slow path - * other: as is - */ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x20, 0x21, 0x00, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, - 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, - 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, - 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x00, 0x5d, 0x5e, 0x5f, - 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, - 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, - 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, - 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, - 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, - 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, - 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, - 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, - 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, - 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff -}; -#endif /* DUK_USE_JSON_DECSTRING_FASTPATH */ - -#if defined(DUK_USE_JSON_EATWHITE_FASTPATH) -DUK_LOCAL const duk_uint8_t duk__json_eatwhite_lookup[256] = { - /* 0x00: finish (non-white) - * 0x01: continue - */ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 -}; -#endif /* DUK_USE_JSON_EATWHITE_FASTPATH */ - -#if defined(DUK_USE_JSON_DECNUMBER_FASTPATH) -DUK_LOCAL const duk_uint8_t duk__json_decnumber_lookup[256] = { - /* 0x00: finish (not part of number) - * 0x01: continue - */ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x01, 0x00, - 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 -}; -#endif /* DUK_USE_JSON_DECNUMBER_FASTPATH */ - -/* - * Parsing implementation. - * - * JSON lexer is now separate from duk_lexer.c because there are numerous - * small differences making it difficult to share the lexer. - * - * The parser here works with raw bytes directly; this works because all - * JSON delimiters are ASCII characters. Invalid xUTF-8 encoded values - * inside strings will be passed on without normalization; this is not a - * compliance concern because compliant inputs will always be valid - * CESU-8 encodings. - */ - -DUK_LOCAL void duk__dec_syntax_error(duk_json_dec_ctx *js_ctx) { - /* Shared handler to minimize parser size. Cause will be - * hidden, unfortunately, but we'll have an offset which - * is often quite enough. - */ - DUK_ERROR_FMT1(js_ctx->thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_FMT_INVALID_JSON, - (long) (js_ctx->p - js_ctx->p_start)); -} - -DUK_LOCAL void duk__dec_eat_white(duk_json_dec_ctx *js_ctx) { - const duk_uint8_t *p; - duk_uint8_t t; - - p = js_ctx->p; - for (;;) { - DUK_ASSERT(p <= js_ctx->p_end); - t = *p; - -#if defined(DUK_USE_JSON_EATWHITE_FASTPATH) - /* This fast path is pretty marginal in practice. - * XXX: candidate for removal. - */ - DUK_ASSERT(duk__json_eatwhite_lookup[0x00] == 0x00); /* end-of-input breaks */ - if (duk__json_eatwhite_lookup[t] == 0) { - break; - } -#else /* DUK_USE_JSON_EATWHITE_FASTPATH */ - if (!(t == 0x20 || t == 0x0a || t == 0x0d || t == 0x09)) { - /* NUL also comes here. Comparison order matters, 0x20 - * is most common whitespace. - */ - break; - } -#endif /* DUK_USE_JSON_EATWHITE_FASTPATH */ - p++; - } - js_ctx->p = p; -} - -DUK_LOCAL duk_uint8_t duk__dec_peek(duk_json_dec_ctx *js_ctx) { - DUK_ASSERT(js_ctx->p <= js_ctx->p_end); - return *js_ctx->p; -} - -DUK_LOCAL duk_uint8_t duk__dec_get(duk_json_dec_ctx *js_ctx) { - DUK_ASSERT(js_ctx->p <= js_ctx->p_end); - return *js_ctx->p++; -} - -DUK_LOCAL duk_uint8_t duk__dec_get_nonwhite(duk_json_dec_ctx *js_ctx) { - duk__dec_eat_white(js_ctx); - return duk__dec_get(js_ctx); -} - -/* For JX, expressing the whole unsigned 32-bit range matters. */ -DUK_LOCAL duk_uint_fast32_t duk__dec_decode_hex_escape(duk_json_dec_ctx *js_ctx, duk_small_uint_t n) { - duk_small_uint_t i; - duk_uint_fast32_t res = 0; - duk_uint8_t x; - duk_small_int_t t; - - for (i = 0; i < n; i++) { - /* XXX: share helper from lexer; duk_lexer.c / hexval(). */ - - x = duk__dec_get(js_ctx); - DUK_DDD(DUK_DDDPRINT("decode_hex_escape: i=%ld, n=%ld, res=%ld, x=%ld", - (long) i, (long) n, (long) res, (long) x)); - - /* x == 0x00 (EOF) causes syntax_error */ - DUK_ASSERT(duk_hex_dectab[0] == -1); - t = duk_hex_dectab[x & 0xff]; - if (DUK_LIKELY(t >= 0)) { - res = (res * 16) + t; - } else { - /* catches EOF and invalid digits */ - goto syntax_error; - } - } - - DUK_DDD(DUK_DDDPRINT("final hex decoded value: %ld", (long) res)); - return res; - - syntax_error: - duk__dec_syntax_error(js_ctx); - DUK_UNREACHABLE(); - return 0; -} - -DUK_LOCAL void duk__dec_req_stridx(duk_json_dec_ctx *js_ctx, duk_small_uint_t stridx) { - duk_hstring *h; - const duk_uint8_t *p; - duk_uint8_t x, y; - - /* First character has already been eaten and checked by the caller. - * We can scan until a NUL in stridx string because no built-in strings - * have internal NULs. - */ - - DUK_ASSERT_DISABLE(stridx >= 0); /* unsigned */ - DUK_ASSERT(stridx < DUK_HEAP_NUM_STRINGS); - h = DUK_HTHREAD_GET_STRING(js_ctx->thr, stridx); - DUK_ASSERT(h != NULL); - - p = (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h) + 1; - DUK_ASSERT(*(js_ctx->p - 1) == *(p - 1)); /* first character has been matched */ - - for (;;) { - x = *p; - if (x == 0) { - break; - } - y = duk__dec_get(js_ctx); - if (x != y) { - /* Catches EOF of JSON input. */ - goto syntax_error; - } - p++; - } - - return; - - syntax_error: - duk__dec_syntax_error(js_ctx); - DUK_UNREACHABLE(); -} - -DUK_LOCAL duk_small_int_t duk__dec_string_escape(duk_json_dec_ctx *js_ctx, duk_uint8_t **ext_p) { - duk_uint_fast32_t cp; - - /* EOF (-1) will be cast to an unsigned value first - * and then re-cast for the switch. In any case, it - * will match the default case (syntax error). - */ - cp = (duk_uint_fast32_t) duk__dec_get(js_ctx); - switch ((int) cp) { - case DUK_ASC_BACKSLASH: break; - case DUK_ASC_DOUBLEQUOTE: break; - case DUK_ASC_SLASH: break; - case DUK_ASC_LC_T: cp = 0x09; break; - case DUK_ASC_LC_N: cp = 0x0a; break; - case DUK_ASC_LC_R: cp = 0x0d; break; - case DUK_ASC_LC_F: cp = 0x0c; break; - case DUK_ASC_LC_B: cp = 0x08; break; - case DUK_ASC_LC_U: { - cp = duk__dec_decode_hex_escape(js_ctx, 4); - break; - } -#ifdef DUK_USE_JX - case DUK_ASC_UC_U: { - if (js_ctx->flag_ext_custom) { - cp = duk__dec_decode_hex_escape(js_ctx, 8); - } else { - return 1; /* syntax error */ - } - break; - } - case DUK_ASC_LC_X: { - if (js_ctx->flag_ext_custom) { - cp = duk__dec_decode_hex_escape(js_ctx, 2); - } else { - return 1; /* syntax error */ - } - break; - } -#endif /* DUK_USE_JX */ - default: - /* catches EOF (0x00) */ - return 1; /* syntax error */ - } - - DUK_RAW_WRITE_XUTF8(*ext_p, cp); - - return 0; -} - -DUK_LOCAL void duk__dec_string(duk_json_dec_ctx *js_ctx) { - duk_hthread *thr = js_ctx->thr; - duk_context *ctx = (duk_context *) thr; - duk_bufwriter_ctx bw_alloc; - duk_bufwriter_ctx *bw; - duk_uint8_t *q; - - /* '"' was eaten by caller */ - - /* Note that we currently parse -bytes-, not codepoints. - * All non-ASCII extended UTF-8 will encode to bytes >= 0x80, - * so they'll simply pass through (valid UTF-8 or not). - */ - - bw = &bw_alloc; - DUK_BW_INIT_PUSHBUF(js_ctx->thr, bw, DUK__JSON_DECSTR_BUFSIZE); - q = DUK_BW_GET_PTR(js_ctx->thr, bw); - -#if defined(DUK_USE_JSON_DECSTRING_FASTPATH) - for (;;) { - duk_small_uint_t safe; - duk_uint8_t b, x; - const duk_uint8_t *p; - - /* Select a safe loop count where no output checks are - * needed assuming we won't encounter escapes. Input - * bound checks are not necessary as a NUL (guaranteed) - * will cause a SyntaxError before we read out of bounds. - */ - - safe = DUK__JSON_DECSTR_CHUNKSIZE; - - /* Ensure space for 1:1 output plus one escape. */ - q = DUK_BW_ENSURE_RAW(js_ctx->thr, bw, safe + DUK_UNICODE_MAX_XUTF8_LENGTH, q); - - p = js_ctx->p; /* temp copy, write back for next loop */ - for (;;) { - if (safe == 0) { - js_ctx->p = p; - break; - } - safe--; - - /* End of input (NUL) goes through slow path and causes SyntaxError. */ - DUK_ASSERT(duk__json_decstr_lookup[0] == 0x00); - - b = *p++; - x = (duk_small_int_t) duk__json_decstr_lookup[b]; - if (DUK_LIKELY(x != 0)) { - /* Fast path, decode as is. */ - *q++ = b; - } else if (b == DUK_ASC_DOUBLEQUOTE) { - js_ctx->p = p; - goto found_quote; - } else if (b == DUK_ASC_BACKSLASH) { - /* We've ensured space for one escaped input; then - * bail out and recheck (this makes escape handling - * quite slow but it's uncommon). - */ - js_ctx->p = p; - if (duk__dec_string_escape(js_ctx, &q) != 0) { - goto syntax_error; - } - break; - } else { - js_ctx->p = p; - goto syntax_error; - } - } - } - found_quote: -#else /* DUK_USE_JSON_DECSTRING_FASTPATH */ - for (;;) { - duk_uint8_t x; - - q = DUK_BW_ENSURE_RAW(js_ctx->thr, bw, DUK_UNICODE_MAX_XUTF8_LENGTH, q); - - x = duk__dec_get(js_ctx); - - if (x == DUK_ASC_DOUBLEQUOTE) { - break; - } else if (x == DUK_ASC_BACKSLASH) { - if (duk__dec_string_escape(js_ctx, &q) != 0) { - goto syntax_error; - } - } else if (x < 0x20) { - /* catches EOF (NUL) */ - goto syntax_error; - } else { - *q++ = (duk_uint8_t) x; - } - } -#endif /* DUK_USE_JSON_DECSTRING_FASTPATH */ - - DUK_BW_SETPTR_AND_COMPACT(js_ctx->thr, bw, q); - duk_to_string(ctx, -1); - - /* [ ... str ] */ - - return; - - syntax_error: - duk__dec_syntax_error(js_ctx); - DUK_UNREACHABLE(); -} - -#ifdef DUK_USE_JX -/* Decode a plain string consisting entirely of identifier characters. - * Used to parse plain keys (e.g. "foo: 123"). - */ -DUK_LOCAL void duk__dec_plain_string(duk_json_dec_ctx *js_ctx) { - duk_hthread *thr = js_ctx->thr; - duk_context *ctx = (duk_context *) thr; - const duk_uint8_t *p; - duk_small_int_t x; - - /* Caller has already eaten the first char so backtrack one byte. */ - - js_ctx->p--; /* safe */ - p = js_ctx->p; - - /* Here again we parse bytes, and non-ASCII UTF-8 will cause end of - * parsing (which is correct except if there are non-shortest encodings). - * There is also no need to check explicitly for end of input buffer as - * the input is NUL padded and NUL will exit the parsing loop. - * - * Because no unescaping takes place, we can just scan to the end of the - * plain string and intern from the input buffer. - */ - - for (;;) { - x = *p; - - /* There is no need to check the first character specially here - * (i.e. reject digits): the caller only accepts valid initial - * characters and won't call us if the first character is a digit. - * This also ensures that the plain string won't be empty. - */ - - if (!duk_unicode_is_identifier_part((duk_codepoint_t) x)) { - break; - } - p++; - } - - duk_push_lstring(ctx, (const char *) js_ctx->p, (duk_size_t) (p - js_ctx->p)); - js_ctx->p = p; - - /* [ ... str ] */ -} -#endif /* DUK_USE_JX */ - -#ifdef DUK_USE_JX -DUK_LOCAL void duk__dec_pointer(duk_json_dec_ctx *js_ctx) { - duk_hthread *thr = js_ctx->thr; - duk_context *ctx = (duk_context *) thr; - const duk_uint8_t *p; - duk_small_int_t x; - void *voidptr; - - /* Caller has already eaten the first character ('(') which we don't need. */ - - p = js_ctx->p; - - for (;;) { - x = *p; - - /* Assume that the native representation never contains a closing - * parenthesis. - */ - - if (x == DUK_ASC_RPAREN) { - break; - } else if (x <= 0) { - /* NUL term or -1 (EOF), NUL check would suffice */ - goto syntax_error; - } - p++; - } - - /* There is no need to NUL delimit the sscanf() call: trailing garbage is - * ignored and there is always a NUL terminator which will force an error - * if no error is encountered before it. It's possible that the scan - * would scan further than between [js_ctx->p,p[ though and we'd advance - * by less than the scanned value. - * - * Because pointers are platform specific, a failure to scan a pointer - * results in a null pointer which is a better placeholder than a missing - * value or an error. - */ - - voidptr = NULL; - (void) DUK_SSCANF((const char *) js_ctx->p, DUK_STR_FMT_PTR, &voidptr); - duk_push_pointer(ctx, voidptr); - js_ctx->p = p + 1; /* skip ')' */ - - /* [ ... ptr ] */ - - return; - - syntax_error: - duk__dec_syntax_error(js_ctx); - DUK_UNREACHABLE(); -} -#endif /* DUK_USE_JX */ - -#ifdef DUK_USE_JX -DUK_LOCAL void duk__dec_buffer(duk_json_dec_ctx *js_ctx) { - duk_hthread *thr = js_ctx->thr; - duk_context *ctx = (duk_context *) thr; - const duk_uint8_t *p; - duk_uint8_t *buf; - duk_size_t src_len; - duk_small_int_t x; - - /* Caller has already eaten the first character ('|') which we don't need. */ - - p = js_ctx->p; - - /* XXX: Would be nice to share the fast path loop from duk_hex_decode() - * and avoid creating a temporary buffer. However, there are some - * differences which prevent trivial sharing: - * - * - Pipe char detection - * - EOF detection - * - Unknown length of input and output - * - * The best approach here would be a bufwriter and a reasonaly sized - * safe inner loop (e.g. 64 output bytes at a time). - */ - - for (;;) { - x = *p; - - /* This loop intentionally does not ensure characters are valid - * ([0-9a-fA-F]) because the hex decode call below will do that. - */ - if (x == DUK_ASC_PIPE) { - break; - } else if (x <= 0) { - /* NUL term or -1 (EOF), NUL check would suffice */ - goto syntax_error; - } - p++; - } - - src_len = (duk_size_t) (p - js_ctx->p); - buf = (duk_uint8_t *) duk_push_fixed_buffer(ctx, src_len); - DUK_ASSERT(buf != NULL); - DUK_MEMCPY((void *) buf, (const void *) js_ctx->p, src_len); - duk_hex_decode(ctx, -1); - - js_ctx->p = p + 1; /* skip '|' */ - - /* [ ... buf ] */ - - return; - - syntax_error: - duk__dec_syntax_error(js_ctx); - DUK_UNREACHABLE(); -} -#endif /* DUK_USE_JX */ - -/* Parse a number, other than NaN or +/- Infinity */ -DUK_LOCAL void duk__dec_number(duk_json_dec_ctx *js_ctx) { - duk_context *ctx = (duk_context *) js_ctx->thr; - const duk_uint8_t *p_start; - const duk_uint8_t *p; - duk_uint8_t x; - duk_small_uint_t s2n_flags; - - DUK_DDD(DUK_DDDPRINT("parse_number")); - - p_start = js_ctx->p; - - /* First pass parse is very lenient (e.g. allows '1.2.3') and extracts a - * string for strict number parsing. - */ - - p = js_ctx->p; - for (;;) { - x = *p; - - DUK_DDD(DUK_DDDPRINT("parse_number: p_start=%p, p=%p, p_end=%p, x=%ld", - (const void *) p_start, (const void *) p, - (const void *) js_ctx->p_end, (long) x)); - -#if defined(DUK_USE_JSON_DECNUMBER_FASTPATH) - /* This fast path is pretty marginal in practice. - * XXX: candidate for removal. - */ - DUK_ASSERT(duk__json_decnumber_lookup[0x00] == 0x00); /* end-of-input breaks */ - if (duk__json_decnumber_lookup[x] == 0) { - break; - } -#else /* DUK_USE_JSON_DECNUMBER_FASTPATH */ - if (!((x >= DUK_ASC_0 && x <= DUK_ASC_9) || - (x == DUK_ASC_PERIOD || x == DUK_ASC_LC_E || - x == DUK_ASC_UC_E || x == DUK_ASC_MINUS || x == DUK_ASC_PLUS))) { - /* Plus sign must be accepted for positive exponents - * (e.g. '1.5e+2'). This clause catches NULs. - */ - break; - } -#endif /* DUK_USE_JSON_DECNUMBER_FASTPATH */ - p++; /* safe, because matched (NUL causes a break) */ - } - js_ctx->p = p; - - DUK_ASSERT(js_ctx->p > p_start); - duk_push_lstring(ctx, (const char *) p_start, (duk_size_t) (p - p_start)); - - s2n_flags = DUK_S2N_FLAG_ALLOW_EXP | - DUK_S2N_FLAG_ALLOW_MINUS | /* but don't allow leading plus */ - DUK_S2N_FLAG_ALLOW_FRAC; - - DUK_DDD(DUK_DDDPRINT("parse_number: string before parsing: %!T", - (duk_tval *) duk_get_tval(ctx, -1))); - duk_numconv_parse(ctx, 10 /*radix*/, s2n_flags); - if (duk_is_nan(ctx, -1)) { - duk__dec_syntax_error(js_ctx); - } - DUK_ASSERT(duk_is_number(ctx, -1)); - DUK_DDD(DUK_DDDPRINT("parse_number: final number: %!T", - (duk_tval *) duk_get_tval(ctx, -1))); - - /* [ ... num ] */ -} - -DUK_LOCAL void duk__dec_objarr_entry(duk_json_dec_ctx *js_ctx) { - duk_context *ctx = (duk_context *) js_ctx->thr; - duk_require_stack(ctx, DUK_JSON_DEC_REQSTACK); - - /* c recursion check */ - - DUK_ASSERT(js_ctx->recursion_depth >= 0); - DUK_ASSERT(js_ctx->recursion_depth <= js_ctx->recursion_limit); - if (js_ctx->recursion_depth >= js_ctx->recursion_limit) { - DUK_ERROR_RANGE((duk_hthread *) ctx, DUK_STR_JSONDEC_RECLIMIT); - } - js_ctx->recursion_depth++; -} - -DUK_LOCAL void duk__dec_objarr_exit(duk_json_dec_ctx *js_ctx) { - /* c recursion check */ - - DUK_ASSERT(js_ctx->recursion_depth > 0); - DUK_ASSERT(js_ctx->recursion_depth <= js_ctx->recursion_limit); - js_ctx->recursion_depth--; -} - -DUK_LOCAL void duk__dec_object(duk_json_dec_ctx *js_ctx) { - duk_context *ctx = (duk_context *) js_ctx->thr; - duk_int_t key_count; /* XXX: a "first" flag would suffice */ - duk_uint8_t x; - - DUK_DDD(DUK_DDDPRINT("parse_object")); - - duk__dec_objarr_entry(js_ctx); - - duk_push_object(ctx); - - /* Initial '{' has been checked and eaten by caller. */ - - key_count = 0; - for (;;) { - x = duk__dec_get_nonwhite(js_ctx); - - DUK_DDD(DUK_DDDPRINT("parse_object: obj=%!T, x=%ld, key_count=%ld", - (duk_tval *) duk_get_tval(ctx, -1), - (long) x, (long) key_count)); - - /* handle comma and closing brace */ - - if (x == DUK_ASC_COMMA && key_count > 0) { - /* accept comma, expect new value */ - x = duk__dec_get_nonwhite(js_ctx); - } else if (x == DUK_ASC_RCURLY) { - /* eat closing brace */ - break; - } else if (key_count == 0) { - /* accept anything, expect first value (EOF will be - * caught by key parsing below. - */ - ; - } else { - /* catches EOF (NUL) and initial comma */ - goto syntax_error; - } - - /* parse key and value */ - - if (x == DUK_ASC_DOUBLEQUOTE) { - duk__dec_string(js_ctx); -#ifdef DUK_USE_JX - } else if (js_ctx->flag_ext_custom && - duk_unicode_is_identifier_start((duk_codepoint_t) x)) { - duk__dec_plain_string(js_ctx); -#endif - } else { - goto syntax_error; - } - - /* [ ... obj key ] */ - - x = duk__dec_get_nonwhite(js_ctx); - if (x != DUK_ASC_COLON) { - goto syntax_error; - } - - duk__dec_value(js_ctx); - - /* [ ... obj key val ] */ - - duk_xdef_prop_wec(ctx, -3); - - /* [ ... obj ] */ - - key_count++; - } - - /* [ ... obj ] */ - - DUK_DDD(DUK_DDDPRINT("parse_object: final object is %!T", - (duk_tval *) duk_get_tval(ctx, -1))); - - duk__dec_objarr_exit(js_ctx); - return; - - syntax_error: - duk__dec_syntax_error(js_ctx); - DUK_UNREACHABLE(); -} - -DUK_LOCAL void duk__dec_array(duk_json_dec_ctx *js_ctx) { - duk_context *ctx = (duk_context *) js_ctx->thr; - duk_uarridx_t arr_idx; - duk_uint8_t x; - - DUK_DDD(DUK_DDDPRINT("parse_array")); - - duk__dec_objarr_entry(js_ctx); - - duk_push_array(ctx); - - /* Initial '[' has been checked and eaten by caller. */ - - arr_idx = 0; - for (;;) { - x = duk__dec_get_nonwhite(js_ctx); - - DUK_DDD(DUK_DDDPRINT("parse_array: arr=%!T, x=%ld, arr_idx=%ld", - (duk_tval *) duk_get_tval(ctx, -1), - (long) x, (long) arr_idx)); - - /* handle comma and closing bracket */ - - if ((x == DUK_ASC_COMMA) && (arr_idx != 0)) { - /* accept comma, expect new value */ - ; - } else if (x == DUK_ASC_RBRACKET) { - /* eat closing bracket */ - break; - } else if (arr_idx == 0) { - /* accept anything, expect first value (EOF will be - * caught by duk__dec_value() below. - */ - js_ctx->p--; /* backtrack (safe) */ - } else { - /* catches EOF (NUL) and initial comma */ - goto syntax_error; - } - - /* parse value */ - - duk__dec_value(js_ctx); - - /* [ ... arr val ] */ - - duk_xdef_prop_index_wec(ctx, -2, arr_idx); - arr_idx++; - } - - /* Must set 'length' explicitly when using duk_xdef_prop_xxx() to - * set the values. - */ - - duk_set_length(ctx, -1, arr_idx); - - /* [ ... arr ] */ - - DUK_DDD(DUK_DDDPRINT("parse_array: final array is %!T", - (duk_tval *) duk_get_tval(ctx, -1))); - - duk__dec_objarr_exit(js_ctx); - return; - - syntax_error: - duk__dec_syntax_error(js_ctx); - DUK_UNREACHABLE(); -} - -DUK_LOCAL void duk__dec_value(duk_json_dec_ctx *js_ctx) { - duk_context *ctx = (duk_context *) js_ctx->thr; - duk_uint8_t x; - - x = duk__dec_get_nonwhite(js_ctx); - - DUK_DDD(DUK_DDDPRINT("parse_value: initial x=%ld", (long) x)); - - /* Note: duk__dec_req_stridx() backtracks one char */ - - if (x == DUK_ASC_DOUBLEQUOTE) { - duk__dec_string(js_ctx); - } else if ((x >= DUK_ASC_0 && x <= DUK_ASC_9) || (x == DUK_ASC_MINUS)) { -#ifdef DUK_USE_JX - if (js_ctx->flag_ext_custom && x == DUK_ASC_MINUS && duk__dec_peek(js_ctx) == DUK_ASC_UC_I) { - duk__dec_req_stridx(js_ctx, DUK_STRIDX_MINUS_INFINITY); /* "-Infinity", '-' has been eaten */ - duk_push_number(ctx, -DUK_DOUBLE_INFINITY); - } else { -#else - { /* unconditional block */ -#endif - /* We already ate 'x', so backup one byte. */ - js_ctx->p--; /* safe */ - duk__dec_number(js_ctx); - } - } else if (x == DUK_ASC_LC_T) { - duk__dec_req_stridx(js_ctx, DUK_STRIDX_TRUE); - duk_push_true(ctx); - } else if (x == DUK_ASC_LC_F) { - duk__dec_req_stridx(js_ctx, DUK_STRIDX_FALSE); - duk_push_false(ctx); - } else if (x == DUK_ASC_LC_N) { - duk__dec_req_stridx(js_ctx, DUK_STRIDX_LC_NULL); - duk_push_null(ctx); -#ifdef DUK_USE_JX - } else if (js_ctx->flag_ext_custom && x == DUK_ASC_LC_U) { - duk__dec_req_stridx(js_ctx, DUK_STRIDX_LC_UNDEFINED); - duk_push_undefined(ctx); - } else if (js_ctx->flag_ext_custom && x == DUK_ASC_UC_N) { - duk__dec_req_stridx(js_ctx, DUK_STRIDX_NAN); - duk_push_nan(ctx); - } else if (js_ctx->flag_ext_custom && x == DUK_ASC_UC_I) { - duk__dec_req_stridx(js_ctx, DUK_STRIDX_INFINITY); - duk_push_number(ctx, DUK_DOUBLE_INFINITY); - } else if (js_ctx->flag_ext_custom && x == DUK_ASC_LPAREN) { - duk__dec_pointer(js_ctx); - } else if (js_ctx->flag_ext_custom && x == DUK_ASC_PIPE) { - duk__dec_buffer(js_ctx); -#endif - } else if (x == DUK_ASC_LCURLY) { - duk__dec_object(js_ctx); - } else if (x == DUK_ASC_LBRACKET) { - duk__dec_array(js_ctx); - } else { - /* catches EOF (NUL) */ - goto syntax_error; - } - - duk__dec_eat_white(js_ctx); - - /* [ ... val ] */ - return; - - syntax_error: - duk__dec_syntax_error(js_ctx); - DUK_UNREACHABLE(); -} - -/* Recursive value reviver, implements the Walk() algorithm. No C recursion - * check is done here because the initial parsing step will already ensure - * there is a reasonable limit on C recursion depth and hence object depth. - */ -DUK_LOCAL void duk__dec_reviver_walk(duk_json_dec_ctx *js_ctx) { - duk_context *ctx = (duk_context *) js_ctx->thr; - duk_hobject *h; - duk_uarridx_t i, arr_len; - - DUK_DDD(DUK_DDDPRINT("walk: top=%ld, holder=%!T, name=%!T", - (long) duk_get_top(ctx), - (duk_tval *) duk_get_tval(ctx, -2), - (duk_tval *) duk_get_tval(ctx, -1))); - - duk_dup_top(ctx); - duk_get_prop(ctx, -3); /* -> [ ... holder name val ] */ - - h = duk_get_hobject(ctx, -1); - if (h != NULL) { - if (DUK_HOBJECT_GET_CLASS_NUMBER(h) == DUK_HOBJECT_CLASS_ARRAY) { - arr_len = (duk_uarridx_t) duk_get_length(ctx, -1); - for (i = 0; i < arr_len; i++) { - /* [ ... holder name val ] */ - - DUK_DDD(DUK_DDDPRINT("walk: array, top=%ld, i=%ld, arr_len=%ld, holder=%!T, name=%!T, val=%!T", - (long) duk_get_top(ctx), (long) i, (long) arr_len, - (duk_tval *) duk_get_tval(ctx, -3), (duk_tval *) duk_get_tval(ctx, -2), - (duk_tval *) duk_get_tval(ctx, -1))); - - /* XXX: push_uint_string / push_u32_string */ - duk_dup_top(ctx); - duk_push_uint(ctx, (duk_uint_t) i); - duk_to_string(ctx, -1); /* -> [ ... holder name val val ToString(i) ] */ - duk__dec_reviver_walk(js_ctx); /* -> [ ... holder name val new_elem ] */ - - if (duk_is_undefined(ctx, -1)) { - duk_pop(ctx); - duk_del_prop_index(ctx, -1, i); - } else { - /* XXX: duk_xdef_prop_index_wec() would be more appropriate - * here but it currently makes some assumptions that might - * not hold (e.g. that previous property is not an accessor). - */ - duk_put_prop_index(ctx, -2, i); - } - } - } else { - /* [ ... holder name val ] */ - duk_enum(ctx, -1, DUK_ENUM_OWN_PROPERTIES_ONLY /*flags*/); - while (duk_next(ctx, -1 /*enum_index*/, 0 /*get_value*/)) { - DUK_DDD(DUK_DDDPRINT("walk: object, top=%ld, holder=%!T, name=%!T, val=%!T, enum=%!iT, obj_key=%!T", - (long) duk_get_top(ctx), (duk_tval *) duk_get_tval(ctx, -5), - (duk_tval *) duk_get_tval(ctx, -4), (duk_tval *) duk_get_tval(ctx, -3), - (duk_tval *) duk_get_tval(ctx, -2), (duk_tval *) duk_get_tval(ctx, -1))); - - /* [ ... holder name val enum obj_key ] */ - duk_dup(ctx, -3); - duk_dup(ctx, -2); - - /* [ ... holder name val enum obj_key val obj_key ] */ - duk__dec_reviver_walk(js_ctx); - - /* [ ... holder name val enum obj_key new_elem ] */ - if (duk_is_undefined(ctx, -1)) { - duk_pop(ctx); - duk_del_prop(ctx, -3); - } else { - /* XXX: duk_xdef_prop_index_wec() would be more appropriate - * here but it currently makes some assumptions that might - * not hold (e.g. that previous property is not an accessor). - * - * Using duk_put_prop() works incorrectly with '__proto__' - * if the own property with that name has been deleted. This - * does not happen normally, but a clever reviver can trigger - * that, see complex reviver case in: test-bug-json-parse-__proto__.js. - */ - duk_put_prop(ctx, -4); - } - } - duk_pop(ctx); /* pop enum */ - } - } - - /* [ ... holder name val ] */ - - duk_dup(ctx, js_ctx->idx_reviver); - duk_insert(ctx, -4); /* -> [ ... reviver holder name val ] */ - duk_call_method(ctx, 2); /* -> [ ... res ] */ - - DUK_DDD(DUK_DDDPRINT("walk: top=%ld, result=%!T", - (long) duk_get_top(ctx), (duk_tval *) duk_get_tval(ctx, -1))); -} - -/* - * Stringify implementation. - */ - -#define DUK__EMIT_1(js_ctx,ch) duk__emit_1((js_ctx), (duk_uint_fast8_t) (ch)) -#define DUK__EMIT_2(js_ctx,ch1,ch2) duk__emit_2((js_ctx), (duk_uint_fast8_t) (ch1), (duk_uint_fast8_t) (ch2)) -#define DUK__EMIT_HSTR(js_ctx,h) duk__emit_hstring((js_ctx), (h)) -#if defined(DUK_USE_FASTINT) || defined(DUK_USE_JX) || defined(DUK_USE_JC) -#define DUK__EMIT_CSTR(js_ctx,p) duk__emit_cstring((js_ctx), (p)) -#endif -#define DUK__EMIT_STRIDX(js_ctx,i) duk__emit_stridx((js_ctx), (i)) -#define DUK__UNEMIT_1(js_ctx) duk__unemit_1((js_ctx)) - -DUK_LOCAL void duk__emit_1(duk_json_enc_ctx *js_ctx, duk_uint_fast8_t ch) { - DUK_BW_WRITE_ENSURE_U8(js_ctx->thr, &js_ctx->bw, ch); -} - -DUK_LOCAL void duk__emit_2(duk_json_enc_ctx *js_ctx, duk_uint_fast8_t ch1, duk_uint_fast8_t ch2) { - DUK_BW_WRITE_ENSURE_U8_2(js_ctx->thr, &js_ctx->bw, ch1, ch2); -} - -DUK_LOCAL void duk__emit_hstring(duk_json_enc_ctx *js_ctx, duk_hstring *h) { - DUK_BW_WRITE_ENSURE_HSTRING(js_ctx->thr, &js_ctx->bw, h); -} - -#if defined(DUK_USE_FASTINT) || defined(DUK_USE_JX) || defined(DUK_USE_JC) -DUK_LOCAL void duk__emit_cstring(duk_json_enc_ctx *js_ctx, const char *str) { - DUK_BW_WRITE_ENSURE_CSTRING(js_ctx->thr, &js_ctx->bw, str); -} -#endif - -DUK_LOCAL void duk__emit_stridx(duk_json_enc_ctx *js_ctx, duk_small_uint_t stridx) { - duk_hstring *h; - - DUK_ASSERT_DISABLE(stridx >= 0); /* unsigned */ - DUK_ASSERT(stridx < DUK_HEAP_NUM_STRINGS); - h = DUK_HTHREAD_GET_STRING(js_ctx->thr, stridx); - DUK_ASSERT(h != NULL); - - DUK_BW_WRITE_ENSURE_HSTRING(js_ctx->thr, &js_ctx->bw, h); -} - -DUK_LOCAL void duk__unemit_1(duk_json_enc_ctx *js_ctx) { - DUK_ASSERT(DUK_BW_GET_SIZE(js_ctx->thr, &js_ctx->bw) >= 1); - DUK_BW_ADD_PTR(js_ctx->thr, &js_ctx->bw, -1); -} - -#define DUK__MKESC(nybbles,esc1,esc2) \ - (((duk_uint_fast32_t) (nybbles)) << 16) | \ - (((duk_uint_fast32_t) (esc1)) << 8) | \ - ((duk_uint_fast32_t) (esc2)) - -DUK_LOCAL duk_uint8_t *duk__emit_esc_auto_fast(duk_json_enc_ctx *js_ctx, duk_uint_fast32_t cp, duk_uint8_t *q) { - duk_uint_fast32_t tmp; - duk_small_uint_t dig; - - DUK_UNREF(js_ctx); - - /* Caller ensures space for at least DUK__JSON_MAX_ESC_LEN. */ - - /* Select appropriate escape format automatically, and set 'tmp' to a - * value encoding both the escape format character and the nybble count: - * - * (nybble_count << 16) | (escape_char1) | (escape_char2) - */ - -#ifdef DUK_USE_JX - if (DUK_LIKELY(cp < 0x100UL)) { - if (DUK_UNLIKELY(js_ctx->flag_ext_custom)) { - tmp = DUK__MKESC(2, DUK_ASC_BACKSLASH, DUK_ASC_LC_X); - } else { - tmp = DUK__MKESC(4, DUK_ASC_BACKSLASH, DUK_ASC_LC_U); - } - } else -#endif - if (DUK_LIKELY(cp < 0x10000UL)) { - tmp = DUK__MKESC(4, DUK_ASC_BACKSLASH, DUK_ASC_LC_U); - } else { -#ifdef DUK_USE_JX - if (DUK_LIKELY(js_ctx->flag_ext_custom)) { - tmp = DUK__MKESC(8, DUK_ASC_BACKSLASH, DUK_ASC_UC_U); - } else -#endif - { - /* In compatible mode and standard JSON mode, output - * something useful for non-BMP characters. This won't - * roundtrip but will still be more or less readable and - * more useful than an error. - */ - tmp = DUK__MKESC(8, DUK_ASC_UC_U, DUK_ASC_PLUS); - } - } - - *q++ = (duk_uint8_t) ((tmp >> 8) & 0xff); - *q++ = (duk_uint8_t) (tmp & 0xff); - - tmp = tmp >> 16; - while (tmp > 0) { - tmp--; - dig = (duk_small_uint_t) ((cp >> (4 * tmp)) & 0x0f); - *q++ = duk_lc_digits[dig]; - } - - return q; -} - -DUK_LOCAL void duk__enc_key_autoquote(duk_json_enc_ctx *js_ctx, duk_hstring *k) { - const duk_int8_t *p, *p_start, *p_end; /* Note: intentionally signed. */ - duk_size_t k_len; - duk_codepoint_t cp; - - DUK_ASSERT(k != NULL); - - /* Accept ASCII strings which conform to identifier requirements - * as being emitted without key quotes. Since we only accept ASCII - * there's no need for actual decoding: 'p' is intentionally signed - * so that bytes >= 0x80 extend to negative values and are rejected - * as invalid identifier codepoints. - */ - - if (js_ctx->flag_avoid_key_quotes) { - k_len = DUK_HSTRING_GET_BYTELEN(k); - p_start = (const duk_int8_t *) DUK_HSTRING_GET_DATA(k); - p_end = p_start + k_len; - p = p_start; - - if (p == p_end) { - /* Zero length string is not accepted without quotes */ - goto quote_normally; - } - cp = (duk_codepoint_t) (*p++); - if (DUK_UNLIKELY(!duk_unicode_is_identifier_start(cp))) { - goto quote_normally; - } - while (p < p_end) { - cp = (duk_codepoint_t) (*p++); - if (DUK_UNLIKELY(!duk_unicode_is_identifier_part(cp))) { - goto quote_normally; - } - } - - /* This seems faster than emitting bytes one at a time and - * then potentially rewinding. - */ - DUK__EMIT_HSTR(js_ctx, k); - return; - } - - quote_normally: - duk__enc_quote_string(js_ctx, k); -} - -/* The Quote(value) operation: quote a string. - * - * Stack policy: [ ] -> [ ]. - */ - -DUK_LOCAL void duk__enc_quote_string(duk_json_enc_ctx *js_ctx, duk_hstring *h_str) { - duk_hthread *thr = js_ctx->thr; - const duk_uint8_t *p, *p_start, *p_end, *p_now, *p_tmp; - duk_uint8_t *q; - duk_ucodepoint_t cp; /* typed for duk_unicode_decode_xutf8() */ - - DUK_DDD(DUK_DDDPRINT("duk__enc_quote_string: h_str=%!O", (duk_heaphdr *) h_str)); - - DUK_ASSERT(h_str != NULL); - p_start = DUK_HSTRING_GET_DATA(h_str); - p_end = p_start + DUK_HSTRING_GET_BYTELEN(h_str); - p = p_start; - - DUK__EMIT_1(js_ctx, DUK_ASC_DOUBLEQUOTE); - - /* Encode string in small chunks, estimating the maximum expansion so that - * there's no need to ensure space while processing the chunk. - */ - - while (p < p_end) { - duk_size_t left, now, space; - - left = (duk_size_t) (p_end - p); - now = (left > DUK__JSON_ENCSTR_CHUNKSIZE ? - DUK__JSON_ENCSTR_CHUNKSIZE : left); - - /* Maximum expansion per input byte is 6: - * - invalid UTF-8 byte causes "\uXXXX" to be emitted (6/1 = 6). - * - 2-byte UTF-8 encodes as "\uXXXX" (6/2 = 3). - * - 4-byte UTF-8 encodes as "\Uxxxxxxxx" (10/4 = 2.5). - */ - space = now * 6; - q = DUK_BW_ENSURE_GETPTR(thr, &js_ctx->bw, space); - - p_now = p + now; - - while (p < p_now) { -#if defined(DUK_USE_JSON_QUOTESTRING_FASTPATH) - duk_uint8_t b; - - b = duk__json_quotestr_lookup[*p++]; - if (DUK_LIKELY(b < 0x80)) { - /* Most input bytes go through here. */ - *q++ = b; - } else if (b >= 0xa0) { - *q++ = DUK_ASC_BACKSLASH; - *q++ = (duk_uint8_t) (b - 0x80); - } else if (b == 0x80) { - cp = (duk_ucodepoint_t) (*(p - 1)); - q = duk__emit_esc_auto_fast(js_ctx, cp, q); - } else if (b == 0x7f && js_ctx->flag_ascii_only) { - /* 0x7F is special */ - DUK_ASSERT(b == 0x81); - cp = (duk_ucodepoint_t) 0x7f; - q = duk__emit_esc_auto_fast(js_ctx, cp, q); - } else { - DUK_ASSERT(b == 0x81); - p--; - - /* slow path is shared */ -#else /* DUK_USE_JSON_QUOTESTRING_FASTPATH */ - cp = *p; - - if (DUK_LIKELY(cp <= 0x7f)) { - /* ascii fast path: avoid decoding utf-8 */ - p++; - if (cp == 0x22 || cp == 0x5c) { - /* double quote or backslash */ - *q++ = DUK_ASC_BACKSLASH; - *q++ = (duk_uint8_t) cp; - } else if (cp < 0x20) { - duk_uint_fast8_t esc_char; - - /* This approach is a bit shorter than a straight - * if-else-ladder and also a bit faster. - */ - if (cp < (sizeof(duk__json_quotestr_esc) / sizeof(duk_uint8_t)) && - (esc_char = duk__json_quotestr_esc[cp]) != 0) { - *q++ = DUK_ASC_BACKSLASH; - *q++ = (duk_uint8_t) esc_char; - } else { - q = duk__emit_esc_auto_fast(js_ctx, cp, q); - } - } else if (cp == 0x7f && js_ctx->flag_ascii_only) { - q = duk__emit_esc_auto_fast(js_ctx, cp, q); - } else { - /* any other printable -> as is */ - *q++ = (duk_uint8_t) cp; - } - } else { - /* slow path is shared */ -#endif /* DUK_USE_JSON_QUOTESTRING_FASTPATH */ - - /* slow path decode */ - - /* If XUTF-8 decoding fails, treat the offending byte as a codepoint directly - * and go forward one byte. This is of course very lossy, but allows some kind - * of output to be produced even for internal strings which don't conform to - * XUTF-8. All standard Ecmascript strings are always CESU-8, so this behavior - * does not violate the Ecmascript specification. The behavior is applied to - * all modes, including Ecmascript standard JSON. Because the current XUTF-8 - * decoding is not very strict, this behavior only really affects initial bytes - * and truncated codepoints. - * - * Another alternative would be to scan forwards to start of next codepoint - * (or end of input) and emit just one replacement codepoint. - */ - - p_tmp = p; - if (!duk_unicode_decode_xutf8(thr, &p, p_start, p_end, &cp)) { - /* Decode failed. */ - cp = *p_tmp; - p = p_tmp + 1; - } - -#ifdef DUK_USE_NONSTD_JSON_ESC_U2028_U2029 - if (js_ctx->flag_ascii_only || cp == 0x2028 || cp == 0x2029) { -#else - if (js_ctx->flag_ascii_only) { -#endif - q = duk__emit_esc_auto_fast(js_ctx, cp, q); - } else { - /* as is */ - DUK_RAW_WRITE_XUTF8(q, cp); - } - } - } - - DUK_BW_SET_PTR(thr, &js_ctx->bw, q); - } - - DUK__EMIT_1(js_ctx, DUK_ASC_DOUBLEQUOTE); -} - -/* Encode a double (checked by caller) from stack top. Stack top may be - * replaced by serialized string but is not popped (caller does that). - */ -DUK_LOCAL void duk__enc_double(duk_json_enc_ctx *js_ctx) { - duk_hthread *thr; - duk_context *ctx; - duk_tval *tv; - duk_double_t d; - duk_small_int_t c; - duk_small_int_t s; - duk_small_uint_t stridx; - duk_small_uint_t n2s_flags; - duk_hstring *h_str; - - DUK_ASSERT(js_ctx != NULL); - thr = js_ctx->thr; - DUK_ASSERT(thr != NULL); - ctx = (duk_context *) thr; - - /* Caller must ensure 'tv' is indeed a double and not a fastint! */ - tv = DUK_GET_TVAL_NEGIDX(ctx, -1); - DUK_ASSERT(DUK_TVAL_IS_DOUBLE(tv)); - d = DUK_TVAL_GET_DOUBLE(tv); - - c = (duk_small_int_t) DUK_FPCLASSIFY(d); - s = (duk_small_int_t) DUK_SIGNBIT(d); - DUK_UNREF(s); - - if (DUK_LIKELY(!(c == DUK_FP_INFINITE || c == DUK_FP_NAN))) { - DUK_ASSERT(DUK_ISFINITE(d)); - -#if defined(DUK_USE_JX) || defined(DUK_USE_JC) - /* Negative zero needs special handling in JX/JC because - * it would otherwise serialize to '0', not '-0'. - */ - if (DUK_UNLIKELY(c == DUK_FP_ZERO && s != 0 && - (js_ctx->flag_ext_custom_or_compatible))) { - duk_push_hstring_stridx(ctx, DUK_STRIDX_MINUS_ZERO); /* '-0' */ - } else -#endif /* DUK_USE_JX || DUK_USE_JC */ - { - n2s_flags = 0; - /* [ ... number ] -> [ ... string ] */ - duk_numconv_stringify(ctx, 10 /*radix*/, 0 /*digits*/, n2s_flags); - } - h_str = duk_to_hstring(ctx, -1); - DUK_ASSERT(h_str != NULL); - DUK__EMIT_HSTR(js_ctx, h_str); - return; - } - -#if defined(DUK_USE_JX) || defined(DUK_USE_JC) - if (!(js_ctx->flags & (DUK_JSON_FLAG_EXT_CUSTOM | - DUK_JSON_FLAG_EXT_COMPATIBLE))) { - stridx = DUK_STRIDX_LC_NULL; - } else if (c == DUK_FP_NAN) { - stridx = js_ctx->stridx_custom_nan; - } else if (s == 0) { - stridx = js_ctx->stridx_custom_posinf; - } else { - stridx = js_ctx->stridx_custom_neginf; - } -#else - stridx = DUK_STRIDX_LC_NULL; -#endif - DUK__EMIT_STRIDX(js_ctx, stridx); -} - -#if defined(DUK_USE_FASTINT) -/* Encode a fastint from duk_tval ptr, no value stack effects. */ -DUK_LOCAL void duk__enc_fastint_tval(duk_json_enc_ctx *js_ctx, duk_tval *tv) { - duk_int64_t v; - - /* Fastint range is signed 48-bit so longest value is -2^47 = -140737488355328 - * (16 chars long), longest signed 64-bit value is -2^63 = -9223372036854775808 - * (20 chars long). Alloc space for 64-bit range to be safe. - */ - duk_uint8_t buf[20 + 1]; - - /* Caller must ensure 'tv' is indeed a fastint! */ - DUK_ASSERT(DUK_TVAL_IS_FASTINT(tv)); - v = DUK_TVAL_GET_FASTINT(tv); - - /* XXX: There are no format strings in duk_config.h yet, could add - * one for formatting duk_int64_t. For now, assumes "%lld" and that - * "long long" type exists. Could also rely on C99 directly but that - * won't work for older MSVC. - */ - DUK_SPRINTF((char *) buf, "%lld", (long long) v); - DUK__EMIT_CSTR(js_ctx, (const char *) buf); -} -#endif - -#if defined(DUK_USE_JX) || defined(DUK_USE_JC) -#if defined(DUK_USE_HEX_FASTPATH) -DUK_LOCAL duk_uint8_t *duk__enc_buffer_data_hex(const duk_uint8_t *src, duk_size_t src_len, duk_uint8_t *dst) { - duk_uint8_t *q; - duk_uint16_t *q16; - duk_small_uint_t x; - duk_size_t i, len_safe; -#if !defined(DUK_USE_UNALIGNED_ACCESSES_POSSIBLE) - duk_bool_t shift_dst; -#endif - - /* Unlike in duk_hex_encode() 'dst' is not necessarily aligned by 2. - * For platforms where unaligned accesses are not allowed, shift 'dst' - * ahead by 1 byte to get alignment and then DUK_MEMMOVE() the result - * in place. The faster encoding loop makes up the difference. - * There's always space for one extra byte because a terminator always - * follows the hex data and that's been accounted for by the caller. - */ - -#if defined(DUK_USE_UNALIGNED_ACCESSES_POSSIBLE) - q16 = (duk_uint16_t *) (void *) dst; -#else - shift_dst = (duk_bool_t) (((duk_size_t) dst) & 0x01U); - if (shift_dst) { - DUK_DD(DUK_DDPRINT("unaligned accesses not possible, dst not aligned -> step to dst + 1")); - q16 = (duk_uint16_t *) (void *) (dst + 1); - } else { - DUK_DD(DUK_DDPRINT("unaligned accesses not possible, dst is aligned")); - q16 = (duk_uint16_t *) (void *) dst; - } - DUK_ASSERT((((duk_size_t) q16) & 0x01U) == 0); -#endif - - len_safe = src_len & ~0x03U; - for (i = 0; i < len_safe; i += 4) { - q16[0] = duk_hex_enctab[src[i]]; - q16[1] = duk_hex_enctab[src[i + 1]]; - q16[2] = duk_hex_enctab[src[i + 2]]; - q16[3] = duk_hex_enctab[src[i + 3]]; - q16 += 4; - } - q = (duk_uint8_t *) q16; - -#if !defined(DUK_USE_UNALIGNED_ACCESSES_POSSIBLE) - if (shift_dst) { - q--; - DUK_MEMMOVE((void *) dst, (const void *) (dst + 1), 2 * len_safe); - DUK_ASSERT(dst + 2 * len_safe == q); - } -#endif - - for (; i < src_len; i++) { - x = src[i]; - *q++ = duk_lc_digits[x >> 4]; - *q++ = duk_lc_digits[x & 0x0f]; - } - - return q; -} -#else /* DUK_USE_HEX_FASTPATH */ -DUK_LOCAL duk_uint8_t *duk__enc_buffer_data_hex(const duk_uint8_t *src, duk_size_t src_len, duk_uint8_t *dst) { - const duk_uint8_t *p; - const duk_uint8_t *p_end; - duk_uint8_t *q; - duk_small_uint_t x; - - p = src; - p_end = src + src_len; - q = dst; - while (p != p_end) { - x = *p++; - *q++ = duk_lc_digits[x >> 4]; - *q++ = duk_lc_digits[x & 0x0f]; - } - - return q; -} -#endif /* DUK_USE_HEX_FASTPATH */ - -DUK_LOCAL void duk__enc_buffer_data(duk_json_enc_ctx *js_ctx, duk_uint8_t *buf_data, duk_size_t buf_len) { - duk_hthread *thr; - duk_uint8_t *q; - duk_size_t space; - - thr = js_ctx->thr; - - DUK_ASSERT(js_ctx->flag_ext_custom || js_ctx->flag_ext_compatible); /* caller checks */ - DUK_ASSERT(js_ctx->flag_ext_custom_or_compatible); - - /* Buffer values are encoded in (lowercase) hex to make the - * binary data readable. Base64 or similar would be more - * compact but less readable, and the point of JX/JC - * variants is to be as useful to a programmer as possible. - */ - - /* The #ifdef clutter here needs to handle the three cases: - * (1) JX+JC, (2) JX only, (3) JC only. - */ - - /* Note: space must cater for both JX and JC. */ - space = 9 + buf_len * 2 + 2; - DUK_ASSERT(DUK_HBUFFER_MAX_BYTELEN <= 0x7ffffffeUL); - DUK_ASSERT((space - 2) / 2 >= buf_len); /* overflow not possible, buffer limits */ - q = DUK_BW_ENSURE_GETPTR(thr, &js_ctx->bw, space); - -#if defined(DUK_USE_JX) && defined(DUK_USE_JC) - if (js_ctx->flag_ext_custom) -#endif -#if defined(DUK_USE_JX) - { - *q++ = DUK_ASC_PIPE; - q = duk__enc_buffer_data_hex(buf_data, buf_len, q); - *q++ = DUK_ASC_PIPE; - - } -#endif -#if defined(DUK_USE_JX) && defined(DUK_USE_JC) - else -#endif -#if defined(DUK_USE_JC) - { - DUK_ASSERT(js_ctx->flag_ext_compatible); - DUK_MEMCPY((void *) q, (const void *) "{\"_buf\":\"", 9); /* len: 9 */ - q += 9; - q = duk__enc_buffer_data_hex(buf_data, buf_len, q); - *q++ = DUK_ASC_DOUBLEQUOTE; - *q++ = DUK_ASC_RCURLY; - } -#endif - - DUK_BW_SET_PTR(thr, &js_ctx->bw, q); -} - -DUK_LOCAL void duk__enc_buffer(duk_json_enc_ctx *js_ctx, duk_hbuffer *h) { - duk__enc_buffer_data(js_ctx, - (duk_uint8_t *) DUK_HBUFFER_GET_DATA_PTR(js_ctx->thr->heap, h), - (duk_size_t) DUK_HBUFFER_GET_SIZE(h)); -} -#endif /* DUK_USE_JX || DUK_USE_JC */ - -#if defined(DUK_USE_JX) || defined(DUK_USE_JC) -DUK_LOCAL void duk__enc_pointer(duk_json_enc_ctx *js_ctx, void *ptr) { - char buf[64]; /* XXX: how to figure correct size? */ - const char *fmt; - - DUK_ASSERT(js_ctx->flag_ext_custom || js_ctx->flag_ext_compatible); /* caller checks */ - DUK_ASSERT(js_ctx->flag_ext_custom_or_compatible); - - DUK_MEMZERO(buf, sizeof(buf)); - - /* The #ifdef clutter here needs to handle the three cases: - * (1) JX+JC, (2) JX only, (3) JC only. - */ -#if defined(DUK_USE_JX) && defined(DUK_USE_JC) - if (js_ctx->flag_ext_custom) -#endif -#if defined(DUK_USE_JX) - { - fmt = ptr ? "(%p)" : "(null)"; - } -#endif -#if defined(DUK_USE_JX) && defined(DUK_USE_JC) - else -#endif -#if defined(DUK_USE_JC) - { - DUK_ASSERT(js_ctx->flag_ext_compatible); - fmt = ptr ? "{\"_ptr\":\"%p\"}" : "{\"_ptr\":\"null\"}"; - } -#endif - - /* When ptr == NULL, the format argument is unused. */ - DUK_SNPRINTF(buf, sizeof(buf) - 1, fmt, ptr); /* must not truncate */ - DUK__EMIT_CSTR(js_ctx, buf); -} -#endif /* DUK_USE_JX || DUK_USE_JC */ - -#if defined(DUK_USE_JX) || defined(DUK_USE_JC) -DUK_LOCAL void duk__enc_bufferobject(duk_json_enc_ctx *js_ctx, duk_hbufferobject *h_bufobj) { - DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj); - - if (h_bufobj->buf == NULL || !DUK_HBUFFEROBJECT_VALID_SLICE(h_bufobj)) { - DUK__EMIT_STRIDX(js_ctx, DUK_STRIDX_LC_NULL); - } else { - /* Handle both full and partial slice (as long as covered). */ - duk__enc_buffer_data(js_ctx, - (duk_uint8_t *) DUK_HBUFFEROBJECT_GET_SLICE_BASE(js_ctx->thr->heap, h_bufobj), - (duk_size_t) h_bufobj->length); - } -} -#endif /* DUK_USE_JX || DUK_USE_JC */ - -/* Indent helper. Calling code relies on js_ctx->recursion_depth also being - * directly related to indent depth. - */ -#if defined(DUK_USE_PREFER_SIZE) -DUK_LOCAL void duk__enc_newline_indent(duk_json_enc_ctx *js_ctx, duk_int_t depth) { - DUK_ASSERT(js_ctx->h_gap != NULL); - DUK_ASSERT(DUK_HSTRING_GET_BYTELEN(js_ctx->h_gap) > 0); /* caller guarantees */ - - DUK__EMIT_1(js_ctx, 0x0a); - while (depth-- > 0) { - DUK__EMIT_HSTR(js_ctx, js_ctx->h_gap); - } -} -#else /* DUK_USE_PREFER_SIZE */ -DUK_LOCAL void duk__enc_newline_indent(duk_json_enc_ctx *js_ctx, duk_int_t depth) { - const duk_uint8_t *gap_data; - duk_size_t gap_len; - duk_size_t avail_bytes; /* bytes of indent available for copying */ - duk_size_t need_bytes; /* bytes of indent still needed */ - duk_uint8_t *p_start; - duk_uint8_t *p; - - DUK_ASSERT(js_ctx->h_gap != NULL); - DUK_ASSERT(DUK_HSTRING_GET_BYTELEN(js_ctx->h_gap) > 0); /* caller guarantees */ - - DUK__EMIT_1(js_ctx, 0x0a); - if (DUK_UNLIKELY(depth == 0)) { - return; - } - - /* To handle deeper indents efficiently, make use of copies we've - * already emitted. In effect we can emit a sequence of 1, 2, 4, - * 8, etc copies, and then finish the last run. Byte counters - * avoid multiply with gap_len on every loop. - */ - - gap_data = (const duk_uint8_t *) DUK_HSTRING_GET_DATA(js_ctx->h_gap); - gap_len = (duk_size_t) DUK_HSTRING_GET_BYTELEN(js_ctx->h_gap); - DUK_ASSERT(gap_len > 0); - - need_bytes = gap_len * depth; - p = DUK_BW_ENSURE_GETPTR(js_ctx->thr, &js_ctx->bw, need_bytes); - p_start = p; - - DUK_MEMCPY((void *) p, (const void *) gap_data, (size_t) gap_len); - p += gap_len; - avail_bytes = gap_len; - DUK_ASSERT(need_bytes >= gap_len); - need_bytes -= gap_len; - - while (need_bytes >= avail_bytes) { - DUK_MEMCPY((void *) p, (const void *) p_start, (size_t) avail_bytes); - p += avail_bytes; - need_bytes -= avail_bytes; - avail_bytes <<= 1; - } - - DUK_ASSERT(need_bytes < avail_bytes); /* need_bytes may be zero */ - DUK_MEMCPY((void *) p, (const void *) p_start, (size_t) need_bytes); - p += need_bytes; - /*avail_bytes += need_bytes*/ - - DUK_BW_SET_PTR(js_ctx->thr, &js_ctx->bw, p); -} -#endif /* DUK_USE_PREFER_SIZE */ - -/* Shared entry handling for object/array serialization. */ -DUK_LOCAL void duk__enc_objarr_entry(duk_json_enc_ctx *js_ctx, duk_idx_t *entry_top) { - duk_context *ctx = (duk_context *) js_ctx->thr; - duk_hobject *h_target; - duk_uint_fast32_t i, n; - - *entry_top = duk_get_top(ctx); - - duk_require_stack(ctx, DUK_JSON_ENC_REQSTACK); - - /* Loop check using a hybrid approach: a fixed-size visited[] array - * with overflow in a loop check object. - */ - - h_target = duk_get_hobject(ctx, -1); /* object or array */ - DUK_ASSERT(h_target != NULL); - - n = js_ctx->recursion_depth; - if (DUK_UNLIKELY(n > DUK_JSON_ENC_LOOPARRAY)) { - n = DUK_JSON_ENC_LOOPARRAY; - } - for (i = 0; i < n; i++) { - if (DUK_UNLIKELY(js_ctx->visiting[i] == h_target)) { - DUK_DD(DUK_DDPRINT("slow path loop detect")); - DUK_ERROR_TYPE((duk_hthread *) ctx, DUK_STR_CYCLIC_INPUT); - } - } - if (js_ctx->recursion_depth < DUK_JSON_ENC_LOOPARRAY) { - js_ctx->visiting[js_ctx->recursion_depth] = h_target; - } else { - duk_push_sprintf(ctx, DUK_STR_FMT_PTR, (void *) h_target); - duk_dup_top(ctx); /* -> [ ... voidp voidp ] */ - if (duk_has_prop(ctx, js_ctx->idx_loop)) { - DUK_ERROR_TYPE((duk_hthread *) ctx, DUK_STR_CYCLIC_INPUT); - } - duk_push_true(ctx); /* -> [ ... voidp true ] */ - duk_put_prop(ctx, js_ctx->idx_loop); /* -> [ ... ] */ - } - - /* C recursion check. */ - - DUK_ASSERT(js_ctx->recursion_depth >= 0); - DUK_ASSERT(js_ctx->recursion_depth <= js_ctx->recursion_limit); - if (js_ctx->recursion_depth >= js_ctx->recursion_limit) { - DUK_ERROR_RANGE((duk_hthread *) ctx, DUK_STR_JSONENC_RECLIMIT); - } - js_ctx->recursion_depth++; - - DUK_DDD(DUK_DDDPRINT("shared entry finished: top=%ld, loop=%!T", - (long) duk_get_top(ctx), (duk_tval *) duk_get_tval(ctx, js_ctx->idx_loop))); -} - -/* Shared exit handling for object/array serialization. */ -DUK_LOCAL void duk__enc_objarr_exit(duk_json_enc_ctx *js_ctx, duk_idx_t *entry_top) { - duk_context *ctx = (duk_context *) js_ctx->thr; - duk_hobject *h_target; - - /* C recursion check. */ - - DUK_ASSERT(js_ctx->recursion_depth > 0); - DUK_ASSERT(js_ctx->recursion_depth <= js_ctx->recursion_limit); - js_ctx->recursion_depth--; - - /* Loop check. */ - - h_target = duk_get_hobject(ctx, *entry_top - 1); /* original target at entry_top - 1 */ - DUK_ASSERT(h_target != NULL); - - if (js_ctx->recursion_depth < DUK_JSON_ENC_LOOPARRAY) { - /* Previous entry was inside visited[], nothing to do. */ - } else { - duk_push_sprintf(ctx, DUK_STR_FMT_PTR, (void *) h_target); - duk_del_prop(ctx, js_ctx->idx_loop); /* -> [ ... ] */ - } - - /* Restore stack top after unbalanced code paths. */ - duk_set_top(ctx, *entry_top); - - DUK_DDD(DUK_DDDPRINT("shared entry finished: top=%ld, loop=%!T", - (long) duk_get_top(ctx), (duk_tval *) duk_get_tval(ctx, js_ctx->idx_loop))); -} - -/* The JO(value) operation: encode object. - * - * Stack policy: [ object ] -> [ object ]. - */ -DUK_LOCAL void duk__enc_object(duk_json_enc_ctx *js_ctx) { - duk_context *ctx = (duk_context *) js_ctx->thr; - duk_hstring *h_key; - duk_idx_t entry_top; - duk_idx_t idx_obj; - duk_idx_t idx_keys; - duk_bool_t emitted; - duk_uarridx_t arr_len, i; - duk_size_t prev_size; - - DUK_DDD(DUK_DDDPRINT("duk__enc_object: obj=%!T", (duk_tval *) duk_get_tval(ctx, -1))); - - duk__enc_objarr_entry(js_ctx, &entry_top); - - idx_obj = entry_top - 1; - - if (js_ctx->idx_proplist >= 0) { - idx_keys = js_ctx->idx_proplist; - } else { - /* XXX: would be nice to enumerate an object at specified index */ - duk_dup(ctx, idx_obj); - (void) duk_hobject_get_enumerated_keys(ctx, DUK_ENUM_OWN_PROPERTIES_ONLY /*flags*/); /* [ ... target ] -> [ ... target keys ] */ - idx_keys = duk_require_normalize_index(ctx, -1); - /* leave stack unbalanced on purpose */ - } - - DUK_DDD(DUK_DDDPRINT("idx_keys=%ld, h_keys=%!T", - (long) idx_keys, (duk_tval *) duk_get_tval(ctx, idx_keys))); - - /* Steps 8-10 have been merged to avoid a "partial" variable. */ - - DUK__EMIT_1(js_ctx, DUK_ASC_LCURLY); - - /* XXX: keys is an internal object with all keys to be processed - * in its (gapless) array part. Because nobody can touch the keys - * object, we could iterate its array part directly (keeping in mind - * that it can be reallocated). - */ - - arr_len = (duk_uarridx_t) duk_get_length(ctx, idx_keys); - emitted = 0; - for (i = 0; i < arr_len; i++) { - duk_get_prop_index(ctx, idx_keys, i); /* -> [ ... key ] */ - - DUK_DDD(DUK_DDDPRINT("object property loop: holder=%!T, key=%!T", - (duk_tval *) duk_get_tval(ctx, idx_obj), - (duk_tval *) duk_get_tval(ctx, -1))); - - h_key = duk_get_hstring(ctx, -1); - DUK_ASSERT(h_key != NULL); - - prev_size = DUK_BW_GET_SIZE(js_ctx->thr, &js_ctx->bw); - if (DUK_UNLIKELY(js_ctx->h_gap != NULL)) { - duk__enc_newline_indent(js_ctx, js_ctx->recursion_depth); - duk__enc_key_autoquote(js_ctx, h_key); - DUK__EMIT_2(js_ctx, DUK_ASC_COLON, DUK_ASC_SPACE); - } else { - duk__enc_key_autoquote(js_ctx, h_key); - DUK__EMIT_1(js_ctx, DUK_ASC_COLON); - } - - /* [ ... key ] */ - - if (DUK_UNLIKELY(duk__enc_value(js_ctx, idx_obj) == 0)) { - /* Value would yield 'undefined', so skip key altogether. - * Side effects have already happened. - */ - DUK_BW_SET_SIZE(js_ctx->thr, &js_ctx->bw, prev_size); - } else { - DUK__EMIT_1(js_ctx, DUK_ASC_COMMA); - emitted = 1; - } - - /* [ ... ] */ - } - - if (emitted) { - DUK_ASSERT(*((duk_uint8_t *) DUK_BW_GET_PTR(js_ctx->thr, &js_ctx->bw) - 1) == DUK_ASC_COMMA); - DUK__UNEMIT_1(js_ctx); /* eat trailing comma */ - if (DUK_UNLIKELY(js_ctx->h_gap != NULL)) { - DUK_ASSERT(js_ctx->recursion_depth >= 1); - duk__enc_newline_indent(js_ctx, js_ctx->recursion_depth - 1); - } - } - DUK__EMIT_1(js_ctx, DUK_ASC_RCURLY); - - duk__enc_objarr_exit(js_ctx, &entry_top); - - DUK_ASSERT_TOP(ctx, entry_top); -} - -/* The JA(value) operation: encode array. - * - * Stack policy: [ array ] -> [ array ]. - */ -DUK_LOCAL void duk__enc_array(duk_json_enc_ctx *js_ctx) { - duk_context *ctx = (duk_context *) js_ctx->thr; - duk_idx_t entry_top; - duk_idx_t idx_arr; - duk_bool_t emitted; - duk_uarridx_t i, arr_len; - - DUK_DDD(DUK_DDDPRINT("duk__enc_array: array=%!T", - (duk_tval *) duk_get_tval(ctx, -1))); - - duk__enc_objarr_entry(js_ctx, &entry_top); - - idx_arr = entry_top - 1; - - /* Steps 8-10 have been merged to avoid a "partial" variable. */ - - DUK__EMIT_1(js_ctx, DUK_ASC_LBRACKET); - - arr_len = (duk_uarridx_t) duk_get_length(ctx, idx_arr); - emitted = 0; - for (i = 0; i < arr_len; i++) { - DUK_DDD(DUK_DDDPRINT("array entry loop: array=%!T, index=%ld, arr_len=%ld", - (duk_tval *) duk_get_tval(ctx, idx_arr), - (long) i, (long) arr_len)); - - if (DUK_UNLIKELY(js_ctx->h_gap != NULL)) { - DUK_ASSERT(js_ctx->recursion_depth >= 1); - duk__enc_newline_indent(js_ctx, js_ctx->recursion_depth); - } - - /* XXX: duk_push_uint_string() */ - duk_push_uint(ctx, (duk_uint_t) i); - duk_to_string(ctx, -1); /* -> [ ... key ] */ - - /* [ ... key ] */ - - if (DUK_UNLIKELY(duk__enc_value(js_ctx, idx_arr) == 0)) { - /* Value would normally be omitted, replace with 'null'. */ - DUK__EMIT_STRIDX(js_ctx, DUK_STRIDX_LC_NULL); - } else { - ; - } - - /* [ ... ] */ - - DUK__EMIT_1(js_ctx, DUK_ASC_COMMA); - emitted = 1; - } - - if (emitted) { - DUK_ASSERT(*((duk_uint8_t *) DUK_BW_GET_PTR(js_ctx->thr, &js_ctx->bw) - 1) == DUK_ASC_COMMA); - DUK__UNEMIT_1(js_ctx); /* eat trailing comma */ - if (DUK_UNLIKELY(js_ctx->h_gap != NULL)) { - DUK_ASSERT(js_ctx->recursion_depth >= 1); - duk__enc_newline_indent(js_ctx, js_ctx->recursion_depth - 1); - } - } - DUK__EMIT_1(js_ctx, DUK_ASC_RBRACKET); - - duk__enc_objarr_exit(js_ctx, &entry_top); - - DUK_ASSERT_TOP(ctx, entry_top); -} - -/* The Str(key, holder) operation. - * - * Stack policy: [ ... key ] -> [ ... ] - */ -DUK_LOCAL duk_bool_t duk__enc_value(duk_json_enc_ctx *js_ctx, duk_idx_t idx_holder) { - duk_context *ctx = (duk_context *) js_ctx->thr; - duk_hthread *thr = (duk_hthread *) ctx; - duk_hobject *h; - duk_tval *tv; - duk_tval *tv_holder; - duk_tval *tv_key; - duk_small_int_t c; - - DUK_DDD(DUK_DDDPRINT("duk__enc_value: idx_holder=%ld, holder=%!T, key=%!T", - (long) idx_holder, (duk_tval *) duk_get_tval(ctx, idx_holder), - (duk_tval *) duk_get_tval(ctx, -1))); - - DUK_UNREF(thr); - - tv_holder = DUK_GET_TVAL_POSIDX(ctx, idx_holder); - DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv_holder)); - tv_key = DUK_GET_TVAL_NEGIDX(ctx, -1); - DUK_ASSERT(DUK_TVAL_IS_STRING(tv_key)); - (void) duk_hobject_getprop(thr, tv_holder, tv_key); - - /* -> [ ... key val ] */ - - DUK_DDD(DUK_DDDPRINT("value=%!T", (duk_tval *) duk_get_tval(ctx, -1))); - - h = duk_get_hobject_or_lfunc_coerce(ctx, -1); - if (h != NULL) { - duk_get_prop_stridx(ctx, -1, DUK_STRIDX_TO_JSON); - h = duk_get_hobject_or_lfunc_coerce(ctx, -1); /* toJSON() can also be a lightfunc */ - - if (h != NULL && DUK_HOBJECT_IS_CALLABLE(h)) { - DUK_DDD(DUK_DDDPRINT("value is object, has callable toJSON() -> call it")); - /* XXX: duk_dup_unvalidated(ctx, -2) etc. */ - duk_dup(ctx, -2); /* -> [ ... key val toJSON val ] */ - duk_dup(ctx, -4); /* -> [ ... key val toJSON val key ] */ - duk_call_method(ctx, 1); /* -> [ ... key val val' ] */ - duk_remove(ctx, -2); /* -> [ ... key val' ] */ - } else { - duk_pop(ctx); /* -> [ ... key val ] */ - } - } - - /* [ ... key val ] */ - - DUK_DDD(DUK_DDDPRINT("value=%!T", (duk_tval *) duk_get_tval(ctx, -1))); - - if (js_ctx->h_replacer) { - /* XXX: Here a "slice copy" would be useful. */ - DUK_DDD(DUK_DDDPRINT("replacer is set, call replacer")); - duk_push_hobject(ctx, js_ctx->h_replacer); /* -> [ ... key val replacer ] */ - duk_dup(ctx, idx_holder); /* -> [ ... key val replacer holder ] */ - duk_dup(ctx, -4); /* -> [ ... key val replacer holder key ] */ - duk_dup(ctx, -4); /* -> [ ... key val replacer holder key val ] */ - duk_call_method(ctx, 2); /* -> [ ... key val val' ] */ - duk_remove(ctx, -2); /* -> [ ... key val' ] */ - } - - /* [ ... key val ] */ - - DUK_DDD(DUK_DDDPRINT("value=%!T", (duk_tval *) duk_get_tval(ctx, -1))); - - tv = DUK_GET_TVAL_NEGIDX(ctx, -1); - if (DUK_TVAL_IS_OBJECT(tv)) { - h = DUK_TVAL_GET_OBJECT(tv); - DUK_ASSERT(h != NULL); - - if (DUK_HOBJECT_IS_BUFFEROBJECT(h)) { -#if defined(DUK_USE_JX) || defined(DUK_USE_JC) - duk_hbufferobject *h_bufobj; - h_bufobj = (duk_hbufferobject *) h; - DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj); - - /* Conceptually we'd extract the plain underlying buffer - * or its slice and then do a type mask check below to - * see if we should reject it. Do the mask check here - * instead to avoid making a copy of the buffer slice. - */ - - if (js_ctx->mask_for_undefined & DUK_TYPE_MASK_BUFFER) { - DUK_DDD(DUK_DDDPRINT("-> bufferobject (-> plain buffer) will result in undefined (type mask check)")); - goto pop2_undef; - } - DUK_DDD(DUK_DDDPRINT("-> bufferobject won't result in undefined, encode directly")); - duk__enc_bufferobject(js_ctx, h_bufobj); - goto pop2_emitted; -#else - DUK_DDD(DUK_DDDPRINT("no JX/JC support, bufferobject/buffer will always result in undefined")); - goto pop2_undef; -#endif - } else { - c = (duk_small_int_t) DUK_HOBJECT_GET_CLASS_NUMBER(h); - switch ((int) c) { - case DUK_HOBJECT_CLASS_NUMBER: { - DUK_DDD(DUK_DDDPRINT("value is a Number object -> coerce with ToNumber()")); - duk_to_number(ctx, -1); - /* The coercion potentially invokes user .valueOf() and .toString() - * but can't result in a function value because [[DefaultValue]] would - * reject such a result: test-dev-json-stringify-coercion-1.js. - */ - DUK_ASSERT(!duk_is_callable(ctx, -1)); - break; - } - case DUK_HOBJECT_CLASS_STRING: { - DUK_DDD(DUK_DDDPRINT("value is a String object -> coerce with ToString()")); - duk_to_string(ctx, -1); - /* Same coercion behavior as for Number. */ - DUK_ASSERT(!duk_is_callable(ctx, -1)); - break; - } -#if defined(DUK_USE_JX) || defined(DUK_USE_JC) - case DUK_HOBJECT_CLASS_POINTER: -#endif - case DUK_HOBJECT_CLASS_BOOLEAN: { - DUK_DDD(DUK_DDDPRINT("value is a Boolean/Buffer/Pointer object -> get internal value")); - duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_VALUE); - duk_remove(ctx, -2); - break; - } - default: { - /* Normal object which doesn't get automatically coerced to a - * primitive value. Functions are checked for specially. The - * primitive value coercions for Number, String, Pointer, and - * Boolean can't result in functions so suffices to check here. - */ - DUK_ASSERT(h != NULL); - if (DUK_HOBJECT_IS_CALLABLE(h)) { -#if defined(DUK_USE_JX) || defined(DUK_USE_JC) - if (js_ctx->flags & (DUK_JSON_FLAG_EXT_CUSTOM | - DUK_JSON_FLAG_EXT_COMPATIBLE)) { - /* We only get here when doing non-standard JSON encoding */ - DUK_DDD(DUK_DDDPRINT("-> function allowed, serialize to custom format")); - DUK_ASSERT(js_ctx->flag_ext_custom || js_ctx->flag_ext_compatible); - DUK__EMIT_STRIDX(js_ctx, js_ctx->stridx_custom_function); - goto pop2_emitted; - } else { - DUK_DDD(DUK_DDDPRINT("-> will result in undefined (function)")); - goto pop2_undef; - } -#else /* DUK_USE_JX || DUK_USE_JC */ - DUK_DDD(DUK_DDDPRINT("-> will result in undefined (function)")); - goto pop2_undef; -#endif /* DUK_USE_JX || DUK_USE_JC */ - } - } - } /* end switch */ - } - } - - /* [ ... key val ] */ - - DUK_DDD(DUK_DDDPRINT("value=%!T", (duk_tval *) duk_get_tval(ctx, -1))); - - if (duk_check_type_mask(ctx, -1, js_ctx->mask_for_undefined)) { - /* will result in undefined */ - DUK_DDD(DUK_DDDPRINT("-> will result in undefined (type mask check)")); - goto pop2_undef; - } - tv = DUK_GET_TVAL_NEGIDX(ctx, -1); - - switch (DUK_TVAL_GET_TAG(tv)) { -#if defined(DUK_USE_JX) || defined(DUK_USE_JC) - /* When JX/JC not in use, the type mask above will avoid this case if needed. */ - case DUK_TAG_UNDEFINED: { - DUK__EMIT_STRIDX(js_ctx, js_ctx->stridx_custom_undefined); - break; - } -#endif - case DUK_TAG_NULL: { - DUK__EMIT_STRIDX(js_ctx, DUK_STRIDX_LC_NULL); - break; - } - case DUK_TAG_BOOLEAN: { - DUK__EMIT_STRIDX(js_ctx, DUK_TVAL_GET_BOOLEAN(tv) ? - DUK_STRIDX_TRUE : DUK_STRIDX_FALSE); - break; - } -#if defined(DUK_USE_JX) || defined(DUK_USE_JC) - /* When JX/JC not in use, the type mask above will avoid this case if needed. */ - case DUK_TAG_POINTER: { - duk__enc_pointer(js_ctx, DUK_TVAL_GET_POINTER(tv)); - break; - } -#endif /* DUK_USE_JX || DUK_USE_JC */ - case DUK_TAG_STRING: { - duk_hstring *h = DUK_TVAL_GET_STRING(tv); - DUK_ASSERT(h != NULL); - - duk__enc_quote_string(js_ctx, h); - break; - } - case DUK_TAG_OBJECT: { - duk_hobject *h = DUK_TVAL_GET_OBJECT(tv); - DUK_ASSERT(h != NULL); - - /* Function values are handled completely above (including - * coercion results): - */ - DUK_ASSERT(!DUK_HOBJECT_IS_CALLABLE(h)); - - if (DUK_HOBJECT_GET_CLASS_NUMBER(h) == DUK_HOBJECT_CLASS_ARRAY) { - duk__enc_array(js_ctx); - } else { - duk__enc_object(js_ctx); - } - break; - } -#if defined(DUK_USE_JX) || defined(DUK_USE_JC) - /* When JX/JC not in use, the type mask above will avoid this case if needed. */ - case DUK_TAG_BUFFER: { - duk__enc_buffer(js_ctx, DUK_TVAL_GET_BUFFER(tv)); - break; - } -#endif /* DUK_USE_JX || DUK_USE_JC */ - case DUK_TAG_LIGHTFUNC: { -#if defined(DUK_USE_JX) || defined(DUK_USE_JC) - /* We only get here when doing non-standard JSON encoding */ - DUK_ASSERT(js_ctx->flag_ext_custom || js_ctx->flag_ext_compatible); - DUK__EMIT_STRIDX(js_ctx, js_ctx->stridx_custom_function); -#else - /* Standard JSON omits functions */ - DUK_UNREACHABLE(); -#endif - break; - } -#if defined(DUK_USE_FASTINT) - case DUK_TAG_FASTINT: - /* Number serialization has a significant impact relative to - * other fast path code, so careful fast path for fastints. - */ - duk__enc_fastint_tval(js_ctx, tv); - break; -#endif - default: { - /* number */ - DUK_ASSERT(!DUK_TVAL_IS_UNUSED(tv)); - DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv)); - /* XXX: A fast path for usual integers would be useful when - * fastint support is not enabled. - */ - duk__enc_double(js_ctx); - break; - } - } - - pop2_emitted: - duk_pop_2(ctx); /* [ ... key val ] -> [ ... ] */ - return 1; /* emitted */ - - pop2_undef: - duk_pop_2(ctx); /* [ ... key val ] -> [ ... ] */ - return 0; /* not emitted */ -} - -/* E5 Section 15.12.3, main algorithm, step 4.b.ii steps 1-4. */ -DUK_LOCAL duk_bool_t duk__enc_allow_into_proplist(duk_tval *tv) { - duk_hobject *h; - duk_small_int_t c; - - DUK_ASSERT(tv != NULL); - if (DUK_TVAL_IS_STRING(tv) || DUK_TVAL_IS_NUMBER(tv)) { - return 1; - } else if (DUK_TVAL_IS_OBJECT(tv)) { - h = DUK_TVAL_GET_OBJECT(tv); - DUK_ASSERT(h != NULL); - c = (duk_small_int_t) DUK_HOBJECT_GET_CLASS_NUMBER(h); - if (c == DUK_HOBJECT_CLASS_STRING || c == DUK_HOBJECT_CLASS_NUMBER) { - return 1; - } - } - - return 0; -} - -/* - * JSON.stringify() fast path - * - * Otherwise supports full JSON, JX, and JC features, but bails out on any - * possible side effect which might change the value being serialized. The - * fast path can take advantage of the fact that the value being serialized - * is unchanged so that we can walk directly through property tables etc. - */ - -#if defined(DUK_USE_JSON_STRINGIFY_FASTPATH) -DUK_LOCAL duk_bool_t duk__json_stringify_fast_value(duk_json_enc_ctx *js_ctx, duk_tval *tv) { - duk_uint_fast32_t i, n; - - DUK_DDD(DUK_DDDPRINT("stringify fast: %!T", tv)); - - DUK_ASSERT(js_ctx != NULL); - DUK_ASSERT(js_ctx->thr != NULL); - -#if 0 /* disabled for now */ - restart_match: -#endif - - DUK_ASSERT(tv != NULL); - - switch (DUK_TVAL_GET_TAG(tv)) { - case DUK_TAG_UNDEFINED: { -#if defined(DUK_USE_JX) || defined(DUK_USE_JC) - if (js_ctx->flag_ext_custom || js_ctx->flag_ext_compatible) { - DUK__EMIT_STRIDX(js_ctx, js_ctx->stridx_custom_undefined); - break; - } else { - goto emit_undefined; - } -#else - goto emit_undefined; -#endif - } - case DUK_TAG_NULL: { - DUK__EMIT_STRIDX(js_ctx, DUK_STRIDX_LC_NULL); - break; - } - case DUK_TAG_BOOLEAN: { - DUK__EMIT_STRIDX(js_ctx, DUK_TVAL_GET_BOOLEAN(tv) ? - DUK_STRIDX_TRUE : DUK_STRIDX_FALSE); - break; - } - case DUK_TAG_STRING: { - duk_hstring *h; - - h = DUK_TVAL_GET_STRING(tv); - DUK_ASSERT(h != NULL); - duk__enc_quote_string(js_ctx, h); - break; - } - case DUK_TAG_OBJECT: { - duk_hobject *obj; - duk_tval *tv_val; - duk_bool_t emitted = 0; - duk_uint32_t c_bit, c_all, c_array, c_unbox, c_undef, - c_func, c_bufobj, c_object; - - /* For objects JSON.stringify() only looks for own, enumerable - * properties which is nice for the fast path here. - * - * For arrays JSON.stringify() uses [[Get]] so it will actually - * inherit properties during serialization! This fast path - * supports gappy arrays as long as there's no actual inherited - * property (which might be a getter etc). - * - * Since recursion only happens for objects, we can have both - * recursion and loop checks here. We use a simple, depth-limited - * loop check in the fast path because the object-based tracking - * is very slow (when tested, it accounted for 50% of fast path - * execution time for input data with a lot of small objects!). - */ - - /* XXX: for real world code, could just ignore array inheritance - * and only look at array own properties. - */ - - /* We rely on a few object flag / class number relationships here, - * assert for them. - */ - - obj = DUK_TVAL_GET_OBJECT(tv); - DUK_ASSERT(obj != NULL); - DUK_ASSERT_HOBJECT_VALID(obj); - - /* Once recursion depth is increased, exit path must decrease - * it (though it's OK to abort the fast path). - */ - - DUK_ASSERT(js_ctx->recursion_depth >= 0); - DUK_ASSERT(js_ctx->recursion_depth <= js_ctx->recursion_limit); - if (js_ctx->recursion_depth >= js_ctx->recursion_limit) { - DUK_DD(DUK_DDPRINT("fast path recursion limit")); - DUK_ERROR_RANGE(js_ctx->thr, DUK_STR_JSONDEC_RECLIMIT); - } - - for (i = 0, n = (duk_uint_fast32_t) js_ctx->recursion_depth; i < n; i++) { - if (DUK_UNLIKELY(js_ctx->visiting[i] == obj)) { - DUK_DD(DUK_DDPRINT("fast path loop detect")); - DUK_ERROR_TYPE(js_ctx->thr, DUK_STR_CYCLIC_INPUT); - } - } - - /* Guaranteed by recursion_limit setup so we don't have to - * check twice. - */ - DUK_ASSERT(js_ctx->recursion_depth < DUK_JSON_ENC_LOOPARRAY); - js_ctx->visiting[js_ctx->recursion_depth] = obj; - js_ctx->recursion_depth++; - - /* If object has a .toJSON() property, we can't be certain - * that it wouldn't mutate any value arbitrarily, so bail - * out of the fast path. - * - * If an object is a Proxy we also can't avoid side effects - * so abandon. - */ - /* XXX: non-callable .toJSON() doesn't need to cause an abort - * but does at the moment, probably not worth fixing. - */ - if (duk_hobject_hasprop_raw(js_ctx->thr, obj, DUK_HTHREAD_STRING_TO_JSON(js_ctx->thr)) || - DUK_HOBJECT_HAS_EXOTIC_PROXYOBJ(obj)) { - DUK_DD(DUK_DDPRINT("object has a .toJSON property or object is a Proxy, abort fast path")); - goto abort_fastpath; - } - - /* We could use a switch-case for the class number but it turns out - * a small if-else ladder on class masks is better. The if-ladder - * should be in order of relevancy. - */ - - /* XXX: move masks to js_ctx? they don't change during one - * fast path invocation. - */ - DUK_ASSERT(DUK_HOBJECT_CLASS_MAX <= 31); -#if defined(DUK_USE_JX) || defined(DUK_USE_JC) - if (js_ctx->flag_ext_custom_or_compatible) { - c_all = DUK_HOBJECT_CMASK_ALL; - c_array = DUK_HOBJECT_CMASK_ARRAY; - c_unbox = DUK_HOBJECT_CMASK_NUMBER | - DUK_HOBJECT_CMASK_STRING | - DUK_HOBJECT_CMASK_BOOLEAN | - DUK_HOBJECT_CMASK_POINTER; - c_func = DUK_HOBJECT_CMASK_FUNCTION; - c_bufobj = DUK_HOBJECT_CMASK_ALL_BUFFEROBJECTS; - c_undef = 0; - c_object = c_all & ~(c_array | c_unbox | c_func | c_bufobj | c_undef); - } - else -#endif - { - c_all = DUK_HOBJECT_CMASK_ALL; - c_array = DUK_HOBJECT_CMASK_ARRAY; - c_unbox = DUK_HOBJECT_CMASK_NUMBER | - DUK_HOBJECT_CMASK_STRING | - DUK_HOBJECT_CMASK_BOOLEAN; - c_func = 0; - c_bufobj = 0; - c_undef = DUK_HOBJECT_CMASK_FUNCTION | - DUK_HOBJECT_CMASK_POINTER | - DUK_HOBJECT_CMASK_ALL_BUFFEROBJECTS; - c_object = c_all & ~(c_array | c_unbox | c_func | c_bufobj | c_undef); - } - - c_bit = DUK_HOBJECT_GET_CLASS_MASK(obj); - if (c_bit & c_object) { - /* All other object types. */ - DUK__EMIT_1(js_ctx, DUK_ASC_LCURLY); - - /* A non-Array object should not have an array part in practice. - * But since it is supported internally (and perhaps used at some - * point), check and abandon if that's the case. - */ - if (DUK_HOBJECT_HAS_ARRAY_PART(obj)) { - DUK_DD(DUK_DDPRINT("non-Array object has array part, abort fast path")); - goto abort_fastpath; - } - - for (i = 0; i < (duk_uint_fast32_t) DUK_HOBJECT_GET_ENEXT(obj); i++) { - duk_hstring *k; - duk_size_t prev_size; - - k = DUK_HOBJECT_E_GET_KEY(js_ctx->thr->heap, obj, i); - if (!k) { - continue; - } - if (!DUK_HOBJECT_E_SLOT_IS_ENUMERABLE(js_ctx->thr->heap, obj, i)) { - continue; - } - if (DUK_HOBJECT_E_SLOT_IS_ACCESSOR(js_ctx->thr->heap, obj, i)) { - /* Getter might have arbitrary side effects, - * so bail out. - */ - DUK_DD(DUK_DDPRINT("property is an accessor, abort fast path")); - goto abort_fastpath; - } - if (DUK_HSTRING_HAS_INTERNAL(k)) { - continue; - } - - tv_val = DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(js_ctx->thr->heap, obj, i); - - prev_size = DUK_BW_GET_SIZE(js_ctx->thr, &js_ctx->bw); - if (DUK_UNLIKELY(js_ctx->h_gap != NULL)) { - duk__enc_newline_indent(js_ctx, js_ctx->recursion_depth); - duk__enc_key_autoquote(js_ctx, k); - DUK__EMIT_2(js_ctx, DUK_ASC_COLON, DUK_ASC_SPACE); - } else { - duk__enc_key_autoquote(js_ctx, k); - DUK__EMIT_1(js_ctx, DUK_ASC_COLON); - } - - if (duk__json_stringify_fast_value(js_ctx, tv_val) == 0) { - DUK_DD(DUK_DDPRINT("prop value not supported, rewind key and colon")); - DUK_BW_SET_SIZE(js_ctx->thr, &js_ctx->bw, prev_size); - } else { - DUK__EMIT_1(js_ctx, DUK_ASC_COMMA); - emitted = 1; - } - } - - /* If any non-Array value had enumerable virtual own - * properties, they should be serialized here. Standard - * types don't. - */ - - if (emitted) { - DUK_ASSERT(*((duk_uint8_t *) DUK_BW_GET_PTR(js_ctx->thr, &js_ctx->bw) - 1) == DUK_ASC_COMMA); - DUK__UNEMIT_1(js_ctx); /* eat trailing comma */ - if (DUK_UNLIKELY(js_ctx->h_gap != NULL)) { - DUK_ASSERT(js_ctx->recursion_depth >= 1); - duk__enc_newline_indent(js_ctx, js_ctx->recursion_depth - 1); - } - } - DUK__EMIT_1(js_ctx, DUK_ASC_RCURLY); - } else if (c_bit & c_array) { - duk_uint_fast32_t arr_len; - duk_uint_fast32_t asize; - - DUK__EMIT_1(js_ctx, DUK_ASC_LBRACKET); - - /* Assume arrays are dense in the fast path. */ - if (!DUK_HOBJECT_HAS_ARRAY_PART(obj)) { - DUK_DD(DUK_DDPRINT("Array object is sparse, abort fast path")); - goto abort_fastpath; - } - - arr_len = (duk_uint_fast32_t) duk_hobject_get_length(js_ctx->thr, obj); - asize = (duk_uint_fast32_t) DUK_HOBJECT_GET_ASIZE(obj); - if (arr_len > asize) { - /* Array length is larger than 'asize'. This shouldn't - * happen in practice. Bail out just in case. - */ - DUK_DD(DUK_DDPRINT("arr_len > asize, abort fast path")); - goto abort_fastpath; - } - /* Array part may be larger than 'length'; if so, iterate - * only up to array 'length'. - */ - for (i = 0; i < arr_len; i++) { - DUK_ASSERT(i < (duk_uint_fast32_t) DUK_HOBJECT_GET_ASIZE(obj)); - - tv_val = DUK_HOBJECT_A_GET_VALUE_PTR(js_ctx->thr->heap, obj, i); - - if (DUK_UNLIKELY(js_ctx->h_gap != NULL)) { - duk__enc_newline_indent(js_ctx, js_ctx->recursion_depth); - } - - if (DUK_UNLIKELY(DUK_TVAL_IS_UNUSED(tv_val))) { - /* Gap in array; check for inherited property, - * bail out if one exists. This should be enough - * to support gappy arrays for all practical code. - */ - duk_hstring *h_tmp; - duk_bool_t has_inherited; - - /* XXX: refactor into an internal helper, pretty awkward */ - duk_push_uint((duk_context *) js_ctx->thr, (duk_uint_t) i); - h_tmp = duk_to_hstring((duk_context *) js_ctx->thr, -1); - DUK_ASSERT(h_tmp != NULL); - has_inherited = duk_hobject_hasprop_raw(js_ctx->thr, obj, h_tmp); - duk_pop((duk_context *) js_ctx->thr); - - if (has_inherited) { - DUK_D(DUK_DPRINT("gap in array, conflicting inherited property, abort fast path")); - goto abort_fastpath; - } - - /* Ordinary gap, undefined encodes to 'null' in - * standard JSON (and no JX/JC support here now). - */ - DUK_D(DUK_DPRINT("gap in array, no conflicting inherited property, remain on fast path")); - DUK__EMIT_STRIDX(js_ctx, DUK_STRIDX_LC_NULL); - } else { - if (duk__json_stringify_fast_value(js_ctx, tv_val) == 0) { - DUK__EMIT_STRIDX(js_ctx, DUK_STRIDX_LC_NULL); - } - } - DUK__EMIT_1(js_ctx, DUK_ASC_COMMA); - emitted = 1; - } - - if (emitted) { - DUK_ASSERT(*((duk_uint8_t *) DUK_BW_GET_PTR(js_ctx->thr, &js_ctx->bw) - 1) == DUK_ASC_COMMA); - DUK__UNEMIT_1(js_ctx); /* eat trailing comma */ - if (DUK_UNLIKELY(js_ctx->h_gap != NULL)) { - DUK_ASSERT(js_ctx->recursion_depth >= 1); - duk__enc_newline_indent(js_ctx, js_ctx->recursion_depth - 1); - } - } - DUK__EMIT_1(js_ctx, DUK_ASC_RBRACKET); - } else if (c_bit & c_unbox) { - /* Certain boxed types are required to go through - * automatic unboxing. Rely on internal value being - * sane (to avoid infinite recursion). - */ -#if 1 - /* The code below is incorrect if .toString() or .valueOf() have - * have been overridden. The correct approach would be to look up - * the method(s) and if they resolve to the built-in function we - * can safely bypass it and look up the internal value directly. - * Unimplemented for now, abort fast path for boxed values. - */ - goto abort_fastpath; -#else /* disabled */ - /* Disabled until fixed, see above. */ - duk_tval *tv_internal; - - DUK_DD(DUK_DDPRINT("auto unboxing in fast path")); - - tv_internal = duk_hobject_get_internal_value_tval_ptr(js_ctx->thr->heap, obj); - DUK_ASSERT(tv_internal != NULL); - DUK_ASSERT(DUK_TVAL_IS_STRING(tv_internal) || - DUK_TVAL_IS_NUMBER(tv_internal) || - DUK_TVAL_IS_BOOLEAN(tv_internal) || - DUK_TVAL_IS_POINTER(tv_internal)); - - tv = tv_internal; - DUK_ASSERT(js_ctx->recursion_depth > 0); - js_ctx->recursion_depth--; /* required to keep recursion depth correct */ - goto restart_match; -#endif /* disabled */ -#if defined(DUK_USE_JX) || defined(DUK_USE_JC) - } else if (c_bit & c_func) { - DUK__EMIT_STRIDX(js_ctx, js_ctx->stridx_custom_function); - } else if (c_bit & c_bufobj) { - duk__enc_bufferobject(js_ctx, (duk_hbufferobject *) obj); -#endif - } else { - DUK_ASSERT((c_bit & c_undef) != 0); - - /* Must decrease recursion depth before returning. */ - DUK_ASSERT(js_ctx->recursion_depth > 0); - DUK_ASSERT(js_ctx->recursion_depth <= js_ctx->recursion_limit); - js_ctx->recursion_depth--; - goto emit_undefined; - } - - DUK_ASSERT(js_ctx->recursion_depth > 0); - DUK_ASSERT(js_ctx->recursion_depth <= js_ctx->recursion_limit); - js_ctx->recursion_depth--; - break; - } - case DUK_TAG_BUFFER: { -#if defined(DUK_USE_JX) || defined(DUK_USE_JC) - if (js_ctx->flag_ext_custom_or_compatible) { - duk__enc_buffer(js_ctx, DUK_TVAL_GET_BUFFER(tv)); - break; - } else { - goto emit_undefined; - } -#else - goto emit_undefined; -#endif - } - case DUK_TAG_POINTER: { -#if defined(DUK_USE_JX) || defined(DUK_USE_JC) - if (js_ctx->flag_ext_custom_or_compatible) { - duk__enc_pointer(js_ctx, DUK_TVAL_GET_POINTER(tv)); - break; - } else { - goto emit_undefined; - } -#else - goto emit_undefined; -#endif - } - case DUK_TAG_LIGHTFUNC: { - /* A lightfunc might also inherit a .toJSON() so just bail out. */ - /* XXX: Could just lookup .toJSON() and continue in fast path, - * as it would almost never be defined. - */ - DUK_DD(DUK_DDPRINT("value is a lightfunc, abort fast path")); - goto abort_fastpath; - } -#if defined(DUK_USE_FASTINT) - case DUK_TAG_FASTINT: { - /* Number serialization has a significant impact relative to - * other fast path code, so careful fast path for fastints. - */ - duk__enc_fastint_tval(js_ctx, tv); - break; - } -#endif - default: { - /* XXX: A fast path for usual integers would be useful when - * fastint support is not enabled. - */ - DUK_ASSERT(!DUK_TVAL_IS_UNUSED(tv)); - DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv)); - - /* XXX: Stack discipline is annoying, could be changed in numconv. */ - duk_push_tval((duk_context *) js_ctx->thr, tv); - duk__enc_double(js_ctx); - duk_pop((duk_context *) js_ctx->thr); - -#if 0 - /* Could also rely on native sprintf(), but it will handle - * values like NaN, Infinity, -0, exponent notation etc in - * a JSON-incompatible way. - */ - duk_double_t d; - char buf[64]; - - DUK_ASSERT(DUK_TVAL_IS_DOUBLE(tv)); - d = DUK_TVAL_GET_DOUBLE(tv); - DUK_SPRINTF(buf, "%lg", d); - DUK__EMIT_CSTR(js_ctx, buf); -#endif - } - } - return 1; /* not undefined */ - - emit_undefined: - return 0; /* value was undefined/unsupported */ - - abort_fastpath: - /* Error message doesn't matter: the error is ignored anyway. */ - DUK_DD(DUK_DDPRINT("aborting fast path")); - DUK_ERROR_INTERNAL_DEFMSG(js_ctx->thr); - return 0; /* unreachable */ -} - -DUK_LOCAL duk_ret_t duk__json_stringify_fast(duk_context *ctx) { - duk_json_enc_ctx *js_ctx; - duk_tval *tv; - - DUK_ASSERT(ctx != NULL); - tv = DUK_GET_TVAL_NEGIDX(ctx, -2); - DUK_ASSERT(DUK_TVAL_IS_POINTER(tv)); - js_ctx = (duk_json_enc_ctx *) DUK_TVAL_GET_POINTER(tv); - DUK_ASSERT(js_ctx != NULL); - - tv = DUK_GET_TVAL_NEGIDX(ctx, -1); - if (duk__json_stringify_fast_value(js_ctx, tv) == 0) { - DUK_DD(DUK_DDPRINT("top level value not supported, fail fast path")); - return DUK_RET_ERROR; /* error message doesn't matter, ignored anyway */ - } - - return 0; -} -#endif /* DUK_USE_JSON_STRINGIFY_FASTPATH */ - -/* - * Top level wrappers - */ - -DUK_INTERNAL -void duk_bi_json_parse_helper(duk_context *ctx, - duk_idx_t idx_value, - duk_idx_t idx_reviver, - duk_small_uint_t flags) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_json_dec_ctx js_ctx_alloc; - duk_json_dec_ctx *js_ctx = &js_ctx_alloc; - duk_hstring *h_text; -#ifdef DUK_USE_ASSERTIONS - duk_idx_t entry_top = duk_get_top(ctx); -#endif - - /* negative top-relative indices not allowed now */ - DUK_ASSERT(idx_value == DUK_INVALID_INDEX || idx_value >= 0); - DUK_ASSERT(idx_reviver == DUK_INVALID_INDEX || idx_reviver >= 0); - - DUK_DDD(DUK_DDDPRINT("JSON parse start: text=%!T, reviver=%!T, flags=0x%08lx, stack_top=%ld", - (duk_tval *) duk_get_tval(ctx, idx_value), - (duk_tval *) duk_get_tval(ctx, idx_reviver), - (unsigned long) flags, - (long) duk_get_top(ctx))); - - DUK_MEMZERO(&js_ctx_alloc, sizeof(js_ctx_alloc)); - js_ctx->thr = thr; -#ifdef DUK_USE_EXPLICIT_NULL_INIT - /* nothing now */ -#endif - js_ctx->recursion_limit = DUK_USE_JSON_DEC_RECLIMIT; - DUK_ASSERT(js_ctx->recursion_depth == 0); - - /* Flag handling currently assumes that flags are consistent. This is OK - * because the call sites are now strictly controlled. - */ - - js_ctx->flags = flags; -#if defined(DUK_USE_JX) - js_ctx->flag_ext_custom = flags & DUK_JSON_FLAG_EXT_CUSTOM; -#endif -#if defined(DUK_USE_JC) - js_ctx->flag_ext_compatible = flags & DUK_JSON_FLAG_EXT_COMPATIBLE; -#endif -#if defined(DUK_USE_JX) || defined(DUK_USE_JC) - js_ctx->flag_ext_custom_or_compatible = flags & (DUK_JSON_FLAG_EXT_CUSTOM | DUK_JSON_FLAG_EXT_COMPATIBLE); -#endif - - h_text = duk_to_hstring(ctx, idx_value); /* coerce in-place */ - DUK_ASSERT(h_text != NULL); - - /* JSON parsing code is allowed to read [p_start,p_end]: p_end is - * valid and points to the string NUL terminator (which is always - * guaranteed for duk_hstrings. - */ - js_ctx->p_start = (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h_text); - js_ctx->p = js_ctx->p_start; - js_ctx->p_end = ((const duk_uint8_t *) DUK_HSTRING_GET_DATA(h_text)) + - DUK_HSTRING_GET_BYTELEN(h_text); - DUK_ASSERT(*(js_ctx->p_end) == 0x00); - - duk__dec_value(js_ctx); /* -> [ ... value ] */ - - /* Trailing whitespace has been eaten by duk__dec_value(), so if - * we're not at end of input here, it's a SyntaxError. - */ - - if (js_ctx->p != js_ctx->p_end) { - duk__dec_syntax_error(js_ctx); - } - - if (duk_is_callable(ctx, idx_reviver)) { - DUK_DDD(DUK_DDDPRINT("applying reviver: %!T", - (duk_tval *) duk_get_tval(ctx, idx_reviver))); - - js_ctx->idx_reviver = idx_reviver; - - duk_push_object(ctx); - duk_dup(ctx, -2); /* -> [ ... val root val ] */ - duk_put_prop_stridx(ctx, -2, DUK_STRIDX_EMPTY_STRING); /* default attrs ok */ - duk_push_hstring_stridx(ctx, DUK_STRIDX_EMPTY_STRING); /* -> [ ... val root "" ] */ - - DUK_DDD(DUK_DDDPRINT("start reviver walk, root=%!T, name=%!T", - (duk_tval *) duk_get_tval(ctx, -2), - (duk_tval *) duk_get_tval(ctx, -1))); - - duk__dec_reviver_walk(js_ctx); /* [ ... val root "" ] -> [ ... val val' ] */ - duk_remove(ctx, -2); /* -> [ ... val' ] */ - } else { - DUK_DDD(DUK_DDDPRINT("reviver does not exist or is not callable: %!T", - (duk_tval *) duk_get_tval(ctx, idx_reviver))); - } - - /* Final result is at stack top. */ - - DUK_DDD(DUK_DDDPRINT("JSON parse end: text=%!T, reviver=%!T, flags=0x%08lx, result=%!T, stack_top=%ld", - (duk_tval *) duk_get_tval(ctx, idx_value), - (duk_tval *) duk_get_tval(ctx, idx_reviver), - (unsigned long) flags, - (duk_tval *) duk_get_tval(ctx, -1), - (long) duk_get_top(ctx))); - - DUK_ASSERT(duk_get_top(ctx) == entry_top + 1); -} - -DUK_INTERNAL -void duk_bi_json_stringify_helper(duk_context *ctx, - duk_idx_t idx_value, - duk_idx_t idx_replacer, - duk_idx_t idx_space, - duk_small_uint_t flags) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_json_enc_ctx js_ctx_alloc; - duk_json_enc_ctx *js_ctx = &js_ctx_alloc; - duk_hobject *h; - duk_idx_t idx_holder; - duk_idx_t entry_top; - - /* negative top-relative indices not allowed now */ - DUK_ASSERT(idx_value == DUK_INVALID_INDEX || idx_value >= 0); - DUK_ASSERT(idx_replacer == DUK_INVALID_INDEX || idx_replacer >= 0); - DUK_ASSERT(idx_space == DUK_INVALID_INDEX || idx_space >= 0); - - DUK_DDD(DUK_DDDPRINT("JSON stringify start: value=%!T, replacer=%!T, space=%!T, flags=0x%08lx, stack_top=%ld", - (duk_tval *) duk_get_tval(ctx, idx_value), - (duk_tval *) duk_get_tval(ctx, idx_replacer), - (duk_tval *) duk_get_tval(ctx, idx_space), - (unsigned long) flags, - (long) duk_get_top(ctx))); - - entry_top = duk_get_top(ctx); - - /* - * Context init - */ - - DUK_MEMZERO(&js_ctx_alloc, sizeof(js_ctx_alloc)); - js_ctx->thr = thr; -#ifdef DUK_USE_EXPLICIT_NULL_INIT - js_ctx->h_replacer = NULL; - js_ctx->h_gap = NULL; -#endif - js_ctx->idx_proplist = -1; - - /* Flag handling currently assumes that flags are consistent. This is OK - * because the call sites are now strictly controlled. - */ - - js_ctx->flags = flags; - js_ctx->flag_ascii_only = flags & DUK_JSON_FLAG_ASCII_ONLY; - js_ctx->flag_avoid_key_quotes = flags & DUK_JSON_FLAG_AVOID_KEY_QUOTES; -#ifdef DUK_USE_JX - js_ctx->flag_ext_custom = flags & DUK_JSON_FLAG_EXT_CUSTOM; -#endif -#ifdef DUK_USE_JC - js_ctx->flag_ext_compatible = flags & DUK_JSON_FLAG_EXT_COMPATIBLE; -#endif -#if defined(DUK_USE_JX) || defined(DUK_USE_JC) - js_ctx->flag_ext_custom_or_compatible = flags & (DUK_JSON_FLAG_EXT_CUSTOM | DUK_JSON_FLAG_EXT_COMPATIBLE); -#endif - - /* The #ifdef clutter here handles the JX/JC enable/disable - * combinations properly. - */ -#if defined(DUK_USE_JX) || defined(DUK_USE_JC) -#if defined(DUK_USE_JX) - if (flags & DUK_JSON_FLAG_EXT_CUSTOM) { - js_ctx->stridx_custom_undefined = DUK_STRIDX_LC_UNDEFINED; - js_ctx->stridx_custom_nan = DUK_STRIDX_NAN; - js_ctx->stridx_custom_neginf = DUK_STRIDX_MINUS_INFINITY; - js_ctx->stridx_custom_posinf = DUK_STRIDX_INFINITY; - js_ctx->stridx_custom_function = - (flags & DUK_JSON_FLAG_AVOID_KEY_QUOTES) ? - DUK_STRIDX_JSON_EXT_FUNCTION2 : - DUK_STRIDX_JSON_EXT_FUNCTION1; - } -#endif /* DUK_USE_JX */ -#if defined(DUK_USE_JX) && defined(DUK_USE_JC) - else -#endif /* DUK_USE_JX && DUK_USE_JC */ -#if defined(DUK_USE_JC) - if (js_ctx->flags & DUK_JSON_FLAG_EXT_COMPATIBLE) { - js_ctx->stridx_custom_undefined = DUK_STRIDX_JSON_EXT_UNDEFINED; - js_ctx->stridx_custom_nan = DUK_STRIDX_JSON_EXT_NAN; - js_ctx->stridx_custom_neginf = DUK_STRIDX_JSON_EXT_NEGINF; - js_ctx->stridx_custom_posinf = DUK_STRIDX_JSON_EXT_POSINF; - js_ctx->stridx_custom_function = DUK_STRIDX_JSON_EXT_FUNCTION1; - } -#endif /* DUK_USE_JC */ -#endif /* DUK_USE_JX || DUK_USE_JC */ - -#if defined(DUK_USE_JX) || defined(DUK_USE_JC) - if (js_ctx->flags & (DUK_JSON_FLAG_EXT_CUSTOM | - DUK_JSON_FLAG_EXT_COMPATIBLE)) { - DUK_ASSERT(js_ctx->mask_for_undefined == 0); /* already zero */ - } - else -#endif /* DUK_USE_JX || DUK_USE_JC */ - { - js_ctx->mask_for_undefined = DUK_TYPE_MASK_UNDEFINED | - DUK_TYPE_MASK_POINTER | - DUK_TYPE_MASK_BUFFER | - DUK_TYPE_MASK_LIGHTFUNC; - } - - DUK_BW_INIT_PUSHBUF(thr, &js_ctx->bw, DUK__JSON_STRINGIFY_BUFSIZE); - - js_ctx->idx_loop = duk_push_object_internal(ctx); - DUK_ASSERT(js_ctx->idx_loop >= 0); - - /* [ ... buf loop ] */ - - /* - * Process replacer/proplist (2nd argument to JSON.stringify) - */ - - h = duk_get_hobject(ctx, idx_replacer); - if (h != NULL) { - if (DUK_HOBJECT_IS_CALLABLE(h)) { - js_ctx->h_replacer = h; - } else if (DUK_HOBJECT_GET_CLASS_NUMBER(h) == DUK_HOBJECT_CLASS_ARRAY) { - /* Here the specification requires correct array index enumeration - * which is a bit tricky for sparse arrays (it is handled by the - * enum setup code). We now enumerate ancestors too, although the - * specification is not very clear on whether that is required. - */ - - duk_uarridx_t plist_idx = 0; - duk_small_uint_t enum_flags; - - js_ctx->idx_proplist = duk_push_array(ctx); /* XXX: array internal? */ - - enum_flags = DUK_ENUM_ARRAY_INDICES_ONLY | - DUK_ENUM_SORT_ARRAY_INDICES; /* expensive flag */ - duk_enum(ctx, idx_replacer, enum_flags); - while (duk_next(ctx, -1 /*enum_index*/, 1 /*get_value*/)) { - /* [ ... proplist enum_obj key val ] */ - if (duk__enc_allow_into_proplist(duk_get_tval(ctx, -1))) { - /* XXX: duplicates should be eliminated here */ - DUK_DDD(DUK_DDDPRINT("proplist enum: key=%!T, val=%!T --> accept", - (duk_tval *) duk_get_tval(ctx, -2), - (duk_tval *) duk_get_tval(ctx, -1))); - duk_to_string(ctx, -1); /* extra coercion of strings is OK */ - duk_put_prop_index(ctx, -4, plist_idx); /* -> [ ... proplist enum_obj key ] */ - plist_idx++; - duk_pop(ctx); - } else { - DUK_DDD(DUK_DDDPRINT("proplist enum: key=%!T, val=%!T --> reject", - (duk_tval *) duk_get_tval(ctx, -2), - (duk_tval *) duk_get_tval(ctx, -1))); - duk_pop_2(ctx); - } - } - duk_pop(ctx); /* pop enum */ - - /* [ ... proplist ] */ - } - } - - /* [ ... buf loop (proplist) ] */ - - /* - * Process space (3rd argument to JSON.stringify) - */ - - h = duk_get_hobject(ctx, idx_space); - if (h != NULL) { - int c = DUK_HOBJECT_GET_CLASS_NUMBER(h); - if (c == DUK_HOBJECT_CLASS_NUMBER) { - duk_to_number(ctx, idx_space); - } else if (c == DUK_HOBJECT_CLASS_STRING) { - duk_to_string(ctx, idx_space); - } - } - - if (duk_is_number(ctx, idx_space)) { - duk_small_int_t nspace; - /* spaces[] must be static to allow initializer with old compilers like BCC */ - static const char spaces[10] = { - DUK_ASC_SPACE, DUK_ASC_SPACE, DUK_ASC_SPACE, DUK_ASC_SPACE, - DUK_ASC_SPACE, DUK_ASC_SPACE, DUK_ASC_SPACE, DUK_ASC_SPACE, - DUK_ASC_SPACE, DUK_ASC_SPACE - }; /* XXX: helper */ - - /* ToInteger() coercion; NaN -> 0, infinities are clamped to 0 and 10 */ - nspace = (duk_small_int_t) duk_to_int_clamped(ctx, idx_space, 0 /*minval*/, 10 /*maxval*/); - DUK_ASSERT(nspace >= 0 && nspace <= 10); - - duk_push_lstring(ctx, spaces, (duk_size_t) nspace); - js_ctx->h_gap = duk_get_hstring(ctx, -1); - DUK_ASSERT(js_ctx->h_gap != NULL); - } else if (duk_is_string(ctx, idx_space)) { - /* XXX: substring in-place at idx_place? */ - duk_dup(ctx, idx_space); - duk_substring(ctx, -1, 0, 10); /* clamp to 10 chars */ - js_ctx->h_gap = duk_get_hstring(ctx, -1); - DUK_ASSERT(js_ctx->h_gap != NULL); - } else { - /* nop */ - } - - if (js_ctx->h_gap != NULL) { - /* if gap is empty, behave as if not given at all */ - if (DUK_HSTRING_GET_CHARLEN(js_ctx->h_gap) == 0) { - js_ctx->h_gap = NULL; - } - } - - /* [ ... buf loop (proplist) (gap) ] */ - - /* - * Fast path: assume no mutation, iterate object property tables - * directly; bail out if that assumption doesn't hold. - */ - -#if defined(DUK_USE_JSON_STRINGIFY_FASTPATH) - if (js_ctx->h_replacer == NULL && /* replacer is a mutation risk */ - js_ctx->idx_proplist == -1) { /* proplist is very rare */ - duk_int_t pcall_rc; -#ifdef DUK_USE_MARK_AND_SWEEP - duk_small_uint_t prev_mark_and_sweep_base_flags; -#endif - - DUK_DD(DUK_DDPRINT("try JSON.stringify() fast path")); - - /* Use recursion_limit to ensure we don't overwrite js_ctx->visiting[] - * array so we don't need two counter checks in the fast path. The - * slow path has a much larger recursion limit which we'll use if - * necessary. - */ - DUK_ASSERT(DUK_USE_JSON_ENC_RECLIMIT >= DUK_JSON_ENC_LOOPARRAY); - js_ctx->recursion_limit = DUK_JSON_ENC_LOOPARRAY; - DUK_ASSERT(js_ctx->recursion_depth == 0); - - /* Execute the fast path in a protected call. If any error is thrown, - * fall back to the slow path. This includes e.g. recursion limit - * because the fast path has a smaller recursion limit (and simpler, - * limited loop detection). - */ - - duk_push_pointer(ctx, (void *) js_ctx); - duk_dup(ctx, idx_value); - -#if defined(DUK_USE_MARK_AND_SWEEP) - /* Must prevent finalizers which may have arbitrary side effects. */ - prev_mark_and_sweep_base_flags = thr->heap->mark_and_sweep_base_flags; - thr->heap->mark_and_sweep_base_flags |= - DUK_MS_FLAG_NO_FINALIZERS | /* avoid attempts to add/remove object keys */ - DUK_MS_FLAG_NO_OBJECT_COMPACTION; /* avoid attempt to compact any objects */ -#endif - - pcall_rc = duk_safe_call(ctx, duk__json_stringify_fast, 2 /*nargs*/, 0 /*nret*/); - -#if defined(DUK_USE_MARK_AND_SWEEP) - thr->heap->mark_and_sweep_base_flags = prev_mark_and_sweep_base_flags; -#endif - if (pcall_rc == DUK_EXEC_SUCCESS) { - DUK_DD(DUK_DDPRINT("fast path successful")); - DUK_BW_PUSH_AS_STRING(thr, &js_ctx->bw); - goto replace_finished; - } - - /* We come here for actual aborts (like encountering .toJSON()) - * but also for recursion/loop errors. Bufwriter size can be - * kept because we'll probably need at least as much as we've - * allocated so far. - */ - DUK_D(DUK_DPRINT("fast path failed, serialize using slow path instead")); - DUK_BW_RESET_SIZE(thr, &js_ctx->bw); - js_ctx->recursion_depth = 0; - } -#endif - - /* - * Create wrapper object and serialize - */ - - idx_holder = duk_push_object(ctx); - duk_dup(ctx, idx_value); - duk_put_prop_stridx(ctx, -2, DUK_STRIDX_EMPTY_STRING); - - DUK_DDD(DUK_DDDPRINT("before: flags=0x%08lx, loop=%!T, replacer=%!O, " - "proplist=%!T, gap=%!O, holder=%!T", - (unsigned long) js_ctx->flags, - (duk_tval *) duk_get_tval(ctx, js_ctx->idx_loop), - (duk_heaphdr *) js_ctx->h_replacer, - (duk_tval *) (js_ctx->idx_proplist >= 0 ? duk_get_tval(ctx, js_ctx->idx_proplist) : NULL), - (duk_heaphdr *) js_ctx->h_gap, - (duk_tval *) duk_get_tval(ctx, -1))); - - /* serialize the wrapper with empty string key */ - - duk_push_hstring_stridx(ctx, DUK_STRIDX_EMPTY_STRING); - - /* [ ... buf loop (proplist) (gap) holder "" ] */ - - js_ctx->recursion_limit = DUK_USE_JSON_ENC_RECLIMIT; - DUK_ASSERT(js_ctx->recursion_depth == 0); - - if (DUK_UNLIKELY(duk__enc_value(js_ctx, idx_holder) == 0)) { /* [ ... holder key ] -> [ ... holder ] */ - /* Result is undefined. */ - duk_push_undefined(ctx); - } else { - /* Convert buffer to result string. */ - DUK_BW_PUSH_AS_STRING(thr, &js_ctx->bw); - } - - DUK_DDD(DUK_DDDPRINT("after: flags=0x%08lx, loop=%!T, replacer=%!O, " - "proplist=%!T, gap=%!O, holder=%!T", - (unsigned long) js_ctx->flags, - (duk_tval *) duk_get_tval(ctx, js_ctx->idx_loop), - (duk_heaphdr *) js_ctx->h_replacer, - (duk_tval *) (js_ctx->idx_proplist >= 0 ? duk_get_tval(ctx, js_ctx->idx_proplist) : NULL), - (duk_heaphdr *) js_ctx->h_gap, - (duk_tval *) duk_get_tval(ctx, idx_holder))); - - /* The stack has a variable shape here, so force it to the - * desired one explicitly. - */ - -#if defined(DUK_USE_JSON_STRINGIFY_FASTPATH) - replace_finished: -#endif - duk_replace(ctx, entry_top); - duk_set_top(ctx, entry_top + 1); - - DUK_DDD(DUK_DDDPRINT("JSON stringify end: value=%!T, replacer=%!T, space=%!T, " - "flags=0x%08lx, result=%!T, stack_top=%ld", - (duk_tval *) duk_get_tval(ctx, idx_value), - (duk_tval *) duk_get_tval(ctx, idx_replacer), - (duk_tval *) duk_get_tval(ctx, idx_space), - (unsigned long) flags, - (duk_tval *) duk_get_tval(ctx, -1), - (long) duk_get_top(ctx))); - - DUK_ASSERT(duk_get_top(ctx) == entry_top + 1); -} - -/* - * Entry points - */ - -DUK_INTERNAL duk_ret_t duk_bi_json_object_parse(duk_context *ctx) { - duk_bi_json_parse_helper(ctx, - 0 /*idx_value*/, - 1 /*idx_replacer*/, - 0 /*flags*/); - return 1; -} - -DUK_INTERNAL duk_ret_t duk_bi_json_object_stringify(duk_context *ctx) { - duk_bi_json_stringify_helper(ctx, - 0 /*idx_value*/, - 1 /*idx_replacer*/, - 2 /*idx_space*/, - 0 /*flags*/); - return 1; -} - -#undef DUK__JSON_DECSTR_BUFSIZE -#undef DUK__JSON_DECSTR_CHUNKSIZE -#undef DUK__JSON_ENCSTR_CHUNKSIZE -#undef DUK__JSON_STRINGIFY_BUFSIZE -#undef DUK__JSON_MAX_ESC_LEN -#line 1 "duk_bi_logger.c" -/* - * Logging support - */ - -/* include removed: duk_internal.h */ - -/* 3-letter log level strings */ -DUK_LOCAL const duk_uint8_t duk__log_level_strings[] = { - (duk_uint8_t) DUK_ASC_UC_T, (duk_uint8_t) DUK_ASC_UC_R, (duk_uint8_t) DUK_ASC_UC_C, - (duk_uint8_t) DUK_ASC_UC_D, (duk_uint8_t) DUK_ASC_UC_B, (duk_uint8_t) DUK_ASC_UC_G, - (duk_uint8_t) DUK_ASC_UC_I, (duk_uint8_t) DUK_ASC_UC_N, (duk_uint8_t) DUK_ASC_UC_F, - (duk_uint8_t) DUK_ASC_UC_W, (duk_uint8_t) DUK_ASC_UC_R, (duk_uint8_t) DUK_ASC_UC_N, - (duk_uint8_t) DUK_ASC_UC_E, (duk_uint8_t) DUK_ASC_UC_R, (duk_uint8_t) DUK_ASC_UC_R, - (duk_uint8_t) DUK_ASC_UC_F, (duk_uint8_t) DUK_ASC_UC_T, (duk_uint8_t) DUK_ASC_UC_L -}; - -/* Constructor */ -DUK_INTERNAL duk_ret_t duk_bi_logger_constructor(duk_context *ctx) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_idx_t nargs; - - /* Calling as a non-constructor is not meaningful. */ - if (!duk_is_constructor_call(ctx)) { - return DUK_RET_TYPE_ERROR; - } - - nargs = duk_get_top(ctx); - duk_set_top(ctx, 1); - - duk_push_this(ctx); - - /* [ name this ] */ - - if (nargs == 0) { - /* Automatic defaulting of logger name from caller. This would - * work poorly with tail calls, but constructor calls are currently - * never tail calls, so tail calls are not an issue now. - */ - - if (thr->callstack_top >= 2) { - duk_activation *act_caller = thr->callstack + thr->callstack_top - 2; - duk_hobject *func_caller; - - func_caller = DUK_ACT_GET_FUNC(act_caller); - if (func_caller) { - /* Stripping the filename might be a good idea - * ("/foo/bar/quux.js" -> logger name "quux"), - * but now used verbatim. - */ - duk_push_hobject(ctx, func_caller); - duk_get_prop_stridx(ctx, -1, DUK_STRIDX_FILE_NAME); - duk_replace(ctx, 0); - } - } - } - /* the stack is unbalanced here on purpose; we only rely on the - * initial two values: [ name this ]. - */ - - if (duk_is_string(ctx, 0)) { - duk_dup(ctx, 0); - duk_put_prop_stridx(ctx, 1, DUK_STRIDX_LC_N); - } else { - /* don't set 'n' at all, inherited value is used as name */ - } - - duk_compact(ctx, 1); - - return 0; /* keep default instance */ -} - -/* Default function to format objects. Tries to use toLogString() but falls - * back to toString(). Any errors are propagated out without catching. - */ -DUK_INTERNAL duk_ret_t duk_bi_logger_prototype_fmt(duk_context *ctx) { - if (duk_get_prop_stridx(ctx, 0, DUK_STRIDX_TO_LOG_STRING)) { - /* [ arg toLogString ] */ - - duk_dup(ctx, 0); - duk_call_method(ctx, 0); - - /* [ arg result ] */ - return 1; - } - - /* [ arg undefined ] */ - duk_pop(ctx); - duk_to_string(ctx, 0); - return 1; -} - -/* Default function to write a formatted log line. Writes to stderr, - * appending a newline to the log line. - * - * The argument is a buffer whose visible size contains the log message. - * This function should avoid coercing the buffer to a string to avoid - * string table traffic. - */ -DUK_INTERNAL duk_ret_t duk_bi_logger_prototype_raw(duk_context *ctx) { - const char *data; - duk_size_t data_len; - - DUK_UNREF(ctx); - DUK_UNREF(data); - DUK_UNREF(data_len); - -#ifdef DUK_USE_FILE_IO - data = (const char *) duk_require_buffer(ctx, 0, &data_len); - DUK_FWRITE((const void *) data, 1, data_len, DUK_STDERR); - DUK_FPUTC((int) '\n', DUK_STDERR); - DUK_FFLUSH(DUK_STDERR); -#else - /* nop */ -#endif - return 0; -} - -/* Log frontend shared helper, magic value indicates log level. Provides - * frontend functions: trace(), debug(), info(), warn(), error(), fatal(). - * This needs to have small footprint, reasonable performance, minimal - * memory churn, etc. - */ -DUK_INTERNAL duk_ret_t duk_bi_logger_prototype_log_shared(duk_context *ctx) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_double_t now; - duk_small_int_t entry_lev = duk_get_current_magic(ctx); - duk_small_int_t logger_lev; - duk_int_t nargs; - duk_int_t i; - duk_size_t tot_len; - const duk_uint8_t *arg_str; - duk_size_t arg_len; - duk_uint8_t *buf, *p; - const duk_uint8_t *q; - duk_uint8_t date_buf[DUK_BI_DATE_ISO8601_BUFSIZE]; - duk_size_t date_len; - duk_small_int_t rc; - - DUK_ASSERT(entry_lev >= 0 && entry_lev <= 5); - DUK_UNREF(thr); - - /* XXX: sanitize to printable (and maybe ASCII) */ - /* XXX: better multiline */ - - /* - * Logger arguments are: - * - * magic: log level (0-5) - * this: logger - * stack: plain log args - * - * We want to minimize memory churn so a two-pass approach - * is used: first pass formats arguments and computes final - * string length, second pass copies strings either into a - * pre-allocated and reused buffer (short messages) or into a - * newly allocated fixed buffer. If the backend function plays - * nice, it won't coerce the buffer to a string (and thus - * intern it). - */ - - nargs = duk_get_top(ctx); - - /* [ arg1 ... argN this ] */ - - /* - * Log level check - */ - - duk_push_this(ctx); - - duk_get_prop_stridx(ctx, -1, DUK_STRIDX_LC_L); - logger_lev = (duk_small_int_t) duk_get_int(ctx, -1); - if (entry_lev < logger_lev) { - return 0; - } - /* log level could be popped but that's not necessary */ - - now = DUK_USE_DATE_GET_NOW(ctx); - duk_bi_date_format_timeval(now, date_buf); - date_len = DUK_STRLEN((const char *) date_buf); - - duk_get_prop_stridx(ctx, -2, DUK_STRIDX_LC_N); - duk_to_string(ctx, -1); - DUK_ASSERT(duk_is_string(ctx, -1)); - - /* [ arg1 ... argN this loggerLevel loggerName ] */ - - /* - * Pass 1 - */ - - /* Line format: