FreeWRL / FreeX3D 4.3.0
duktape.h
1/*
2 * Duktape public API for Duktape 2.0.0.
3 *
4 * See the API reference for documentation on call semantics.
5 * The exposed API is inside the DUK_API_PUBLIC_H_INCLUDED
6 * include guard. Other parts of the header are Duktape
7 * internal and related to platform/compiler/feature detection.
8 *
9 * Git commit external (external).
10 * Git branch external.
11 *
12 * See Duktape AUTHORS.rst and LICENSE.txt for copyright and
13 * licensing information.
14 */
15
16/* LICENSE.txt */
17/*
18 * ===============
19 * Duktape license
20 * ===============
21 *
22 * (http://opensource.org/licenses/MIT)
23 *
24 * Copyright (c) 2013-2017 by Duktape authors (see AUTHORS.rst)
25 *
26 * Permission is hereby granted, free of charge, to any person obtaining a copy
27 * of this software and associated documentation files (the "Software"), to deal
28 * in the Software without restriction, including without limitation the rights
29 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
30 * copies of the Software, and to permit persons to whom the Software is
31 * furnished to do so, subject to the following conditions:
32 *
33 * The above copyright notice and this permission notice shall be included in
34 * all copies or substantial portions of the Software.
35 *
36 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
37 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
38 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
39 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
40 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
41 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
42 * THE SOFTWARE.
43 */
44
45/* AUTHORS.rst */
46/*
47 * ===============
48 * Duktape authors
49 * ===============
50 *
51 * Copyright
52 * =========
53 *
54 * Duktape copyrights are held by its authors. Each author has a copyright
55 * to their contribution, and agrees to irrevocably license the contribution
56 * under the Duktape ``LICENSE.txt``.
57 *
58 * Authors
59 * =======
60 *
61 * Please include an e-mail address, a link to your GitHub profile, or something
62 * similar to allow your contribution to be identified accurately.
63 *
64 * The following people have contributed code, website contents, or Wiki contents,
65 * and agreed to irrevocably license their contributions under the Duktape
66 * ``LICENSE.txt`` (in order of appearance):
67 *
68 * * Sami Vaarala <sami.vaarala@iki.fi>
69 * * Niki Dobrev
70 * * Andreas \u00d6man <andreas@lonelycoder.com>
71 * * L\u00e1szl\u00f3 Lang\u00f3 <llango.u-szeged@partner.samsung.com>
72 * * Legimet <legimet.calc@gmail.com>
73 * * Karl Skomski <karl@skomski.com>
74 * * Bruce Pascoe <fatcerberus1@gmail.com>
75 * * Ren\u00e9 Hollander <rene@rene8888.at>
76 * * Julien Hamaide (https://github.com/crazyjul)
77 * * Sebastian G\u00f6tte (https://github.com/jaseg)
78 * * Tomasz Magulski (https://github.com/magul)
79 * * \D. Bohdan (https://github.com/dbohdan)
80 * * Ond\u0159ej Jirman (https://github.com/megous)
81 * * Sa\u00fal Ibarra Corretg\u00e9 <saghul@gmail.com>
82 * * Jeremy HU <huxingyi@msn.com>
83 * * Ole Andr\u00e9 Vadla Ravn\u00e5s (https://github.com/oleavr)
84 * * Harold Brenes (https://github.com/harold-b)
85 * * Oliver Crow (https://github.com/ocrow)
86 * * Jakub Ch\u0142api\u0144ski (https://github.com/jchlapinski)
87 * * Brett Vickers (https://github.com/beevik)
88 * * Dominik Okwieka (https://github.com/okitec)
89 * * Remko Tron\u00e7on (https://el-tramo.be)
90 *
91 * Other contributions
92 * ===================
93 *
94 * The following people have contributed something other than code (e.g. reported
95 * bugs, provided ideas, etc; roughly in order of appearance):
96 *
97 * * Greg Burns
98 * * Anthony Rabine
99 * * Carlos Costa
100 * * Aur\u00e9lien Bouilland
101 * * Preet Desai (Pris Matic)
102 * * judofyr (http://www.reddit.com/user/judofyr)
103 * * Jason Woofenden
104 * * Micha\u0142 Przyby\u015b
105 * * Anthony Howe
106 * * Conrad Pankoff
107 * * Jim Schimpf
108 * * Rajaran Gaunker (https://github.com/zimbabao)
109 * * Andreas \u00d6man
110 * * Doug Sanden
111 * * Josh Engebretson (https://github.com/JoshEngebretson)
112 * * Remo Eichenberger (https://github.com/remoe)
113 * * Mamod Mehyar (https://github.com/mamod)
114 * * David Demelier (https://github.com/markand)
115 * * Tim Caswell (https://github.com/creationix)
116 * * Mitchell Blank Jr (https://github.com/mitchblank)
117 * * https://github.com/yushli
118 * * Seo Sanghyeon (https://github.com/sanxiyn)
119 * * Han ChoongWoo (https://github.com/tunz)
120 * * Joshua Peek (https://github.com/josh)
121 * * Bruce E. Pascoe (https://github.com/fatcerberus)
122 * * https://github.com/Kelledin
123 * * https://github.com/sstruchtrup
124 * * Michael Drake (https://github.com/tlsa)
125 * * https://github.com/chris-y
126 * * Laurent Zubiaur (https://github.com/lzubiaur)
127 * * Neil Kolban (https://github.com/nkolban)
128 *
129 * If you are accidentally missing from this list, send me an e-mail
130 * (``sami.vaarala@iki.fi``) and I'll fix the omission.
131 */
132
133#if !defined(DUKTAPE_H_INCLUDED)
134#define DUKTAPE_H_INCLUDED
135
136#define DUK_SINGLE_FILE
137
138/* External duk_config.h provides platform/compiler/OS dependent
139 * typedefs and macros, and DUK_USE_xxx config options so that
140 * the rest of Duktape doesn't need to do any feature detection.
141 */
142#include "duk_config.h"
143
144/*
145 * BEGIN PUBLIC API
146 */
147
148#if !defined(DUK_API_PUBLIC_H_INCLUDED)
149#define DUK_API_PUBLIC_H_INCLUDED
150
151/*
152 * Avoid C++ name mangling
153 */
154
155#if defined(__cplusplus)
156extern "C" {
157#endif
158
159/*
160 * Some defines forwarded from feature detection
161 */
162
163#undef DUK_API_VARIADIC_MACROS
164#if defined(DUK_USE_VARIADIC_MACROS)
165#define DUK_API_VARIADIC_MACROS
166#endif
167
168#define DUK_API_NORETURN(decl) DUK_NORETURN(decl)
169
170/*
171 * Public API specific typedefs
172 *
173 * Many types are wrapped by Duktape for portability to rare platforms
174 * where e.g. 'int' is a 16-bit type. See practical typing discussion
175 * in Duktape web documentation.
176 */
177
178struct duk_thread_state;
183
184/* duk_context is now defined in duk_config.h because it may also be
185 * referenced there by prototypes.
186 */
192
193typedef duk_ret_t (*duk_c_function)(duk_context *ctx);
194typedef void *(*duk_alloc_function) (void *udata, duk_size_t size);
195typedef void *(*duk_realloc_function) (void *udata, void *ptr, duk_size_t size);
196typedef void (*duk_free_function) (void *udata, void *ptr);
197typedef void (*duk_fatal_function) (void *udata, const char *msg);
198typedef void (*duk_decode_char_function) (void *udata, duk_codepoint_t codepoint);
199typedef duk_codepoint_t (*duk_map_char_function) (void *udata, duk_codepoint_t codepoint);
200typedef duk_ret_t (*duk_safe_call_function) (duk_context *ctx, void *udata);
201typedef duk_size_t (*duk_debug_read_function) (void *udata, char *buffer, duk_size_t length);
202typedef duk_size_t (*duk_debug_write_function) (void *udata, const char *buffer, duk_size_t length);
203typedef duk_size_t (*duk_debug_peek_function) (void *udata);
204typedef void (*duk_debug_read_flush_function) (void *udata);
205typedef void (*duk_debug_write_flush_function) (void *udata);
206typedef duk_idx_t (*duk_debug_request_function) (duk_context *ctx, void *udata, duk_idx_t nvalues);
207typedef void (*duk_debug_detached_function) (duk_context *ctx, void *udata);
208
210 /* XXX: Enough space to hold internal suspend/resume structure.
211 * This is rather awkward and to be fixed when the internal
212 * structure is visible for the public API header.
213 */
214 char data[128];
215};
216
218 duk_alloc_function alloc_func;
219 duk_realloc_function realloc_func;
220 duk_free_function free_func;
221 void *udata;
222};
223
225 const char *key;
226 duk_c_function value;
227 duk_idx_t nargs;
228};
229
231 const char *key;
232 duk_double_t value;
233};
234
236 duk_double_t year; /* year, e.g. 2016, Ecmascript year range */
237 duk_double_t month; /* month: 1-12 */
238 duk_double_t day; /* day: 1-31 */
239 duk_double_t hours; /* hour: 0-59 */
240 duk_double_t minutes; /* minute: 0-59 */
241 duk_double_t seconds; /* second: 0-59 (in POSIX time no leap second) */
242 duk_double_t milliseconds; /* may contain sub-millisecond fractions */
243 duk_double_t weekday; /* weekday: 0-6, 0=Sunday, 1=Monday, ..., 6=Saturday */
244};
245
246/*
247 * Constants
248 */
249
250/* Duktape version, (major * 10000) + (minor * 100) + patch. Allows C code
251 * to #if (DUK_VERSION >= NNN) against Duktape API version. The same value
252 * is also available to Ecmascript code in Duktape.version. Unofficial
253 * development snapshots have 99 for patch level (e.g. 0.10.99 would be a
254 * development version after 0.10.0 but before the next official release).
255 */
256#define DUK_VERSION 20000L
257
258/* Git commit, describe, and branch for Duktape build. Useful for
259 * non-official snapshot builds so that application code can easily log
260 * which Duktape snapshot was used. Not available in the Ecmascript
261 * environment.
262 */
263#define DUK_GIT_COMMIT "external"
264#define DUK_GIT_DESCRIBE "external"
265#define DUK_GIT_BRANCH "external"
266
267/* Duktape debug protocol version used by this build. */
268#define DUK_DEBUG_PROTOCOL_VERSION 2
269
270/* Used to represent invalid index; if caller uses this without checking,
271 * this index will map to a non-existent stack entry. Also used in some
272 * API calls as a marker to denote "no value".
273 */
274#define DUK_INVALID_INDEX DUK_IDX_MIN
275
276/* Indicates that a native function does not have a fixed number of args,
277 * and the argument stack should not be capped/extended at all.
278 */
279#define DUK_VARARGS ((duk_int_t) (-1))
280
281/* Number of value stack entries (in addition to actual call arguments)
282 * guaranteed to be allocated on entry to a Duktape/C function.
283 */
284#define DUK_API_ENTRY_STACK 64
285
286/* Value types, used by e.g. duk_get_type() */
287#define DUK_TYPE_MIN 0
288#define DUK_TYPE_NONE 0 /* no value, e.g. invalid index */
289#define DUK_TYPE_UNDEFINED 1 /* Ecmascript undefined */
290#define DUK_TYPE_NULL 2 /* Ecmascript null */
291#define DUK_TYPE_BOOLEAN 3 /* Ecmascript boolean: 0 or 1 */
292#define DUK_TYPE_NUMBER 4 /* Ecmascript number: double */
293#define DUK_TYPE_STRING 5 /* Ecmascript string: CESU-8 / extended UTF-8 encoded */
294#define DUK_TYPE_OBJECT 6 /* Ecmascript object: includes objects, arrays, functions, threads */
295#define DUK_TYPE_BUFFER 7 /* fixed or dynamic, garbage collected byte buffer */
296#define DUK_TYPE_POINTER 8 /* raw void pointer */
297#define DUK_TYPE_LIGHTFUNC 9 /* lightweight function pointer */
298#define DUK_TYPE_MAX 9
299
300/* Value mask types, used by e.g. duk_get_type_mask() */
301#define DUK_TYPE_MASK_NONE (1 << DUK_TYPE_NONE)
302#define DUK_TYPE_MASK_UNDEFINED (1 << DUK_TYPE_UNDEFINED)
303#define DUK_TYPE_MASK_NULL (1 << DUK_TYPE_NULL)
304#define DUK_TYPE_MASK_BOOLEAN (1 << DUK_TYPE_BOOLEAN)
305#define DUK_TYPE_MASK_NUMBER (1 << DUK_TYPE_NUMBER)
306#define DUK_TYPE_MASK_STRING (1 << DUK_TYPE_STRING)
307#define DUK_TYPE_MASK_OBJECT (1 << DUK_TYPE_OBJECT)
308#define DUK_TYPE_MASK_BUFFER (1 << DUK_TYPE_BUFFER)
309#define DUK_TYPE_MASK_POINTER (1 << DUK_TYPE_POINTER)
310#define DUK_TYPE_MASK_LIGHTFUNC (1 << DUK_TYPE_LIGHTFUNC)
311#define DUK_TYPE_MASK_THROW (1 << 10) /* internal flag value: throw if mask doesn't match */
312#define DUK_TYPE_MASK_PROMOTE (1 << 11) /* internal flag value: promote to object if mask matches */
313
314/* Coercion hints */
315#define DUK_HINT_NONE 0 /* prefer number, unless input is a Date, in which
316 * case prefer string (E5 Section 8.12.8)
317 */
318#define DUK_HINT_STRING 1 /* prefer string */
319#define DUK_HINT_NUMBER 2 /* prefer number */
320
321/* Enumeration flags for duk_enum() */
322#define DUK_ENUM_INCLUDE_NONENUMERABLE (1 << 0) /* enumerate non-numerable properties in addition to enumerable */
323#define DUK_ENUM_INCLUDE_HIDDEN (1 << 1) /* enumerate hidden symbols too (in Duktape 1.x called internal properties) */
324#define DUK_ENUM_INCLUDE_SYMBOLS (1 << 2) /* enumerate symbols */
325#define DUK_ENUM_EXCLUDE_STRINGS (1 << 3) /* exclude strings */
326#define DUK_ENUM_OWN_PROPERTIES_ONLY (1 << 4) /* don't walk prototype chain, only check own properties */
327#define DUK_ENUM_ARRAY_INDICES_ONLY (1 << 5) /* only enumerate array indices */
328#define DUK_ENUM_SORT_ARRAY_INDICES (1 << 6) /* sort array indices (applied to full enumeration result, including inherited array indices) */
329#define DUK_ENUM_NO_PROXY_BEHAVIOR (1 << 7) /* enumerate a proxy object itself without invoking proxy behavior */
330
331/* Compilation flags for duk_compile() and duk_eval() */
332/* DUK_COMPILE_xxx bits 0-2 are reserved for an internal 'nargs' argument.
333 */
334#define DUK_COMPILE_EVAL (1 << 3) /* compile eval code (instead of global code) */
335#define DUK_COMPILE_FUNCTION (1 << 4) /* compile function code (instead of global code) */
336#define DUK_COMPILE_STRICT (1 << 5) /* use strict (outer) context for global, eval, or function code */
337#define DUK_COMPILE_SAFE (1 << 6) /* (internal) catch compilation errors */
338#define DUK_COMPILE_NORESULT (1 << 7) /* (internal) omit eval result */
339#define DUK_COMPILE_NOSOURCE (1 << 8) /* (internal) no source string on stack */
340#define DUK_COMPILE_STRLEN (1 << 9) /* (internal) take strlen() of src_buffer (avoids double evaluation in macro) */
341#define DUK_COMPILE_NOFILENAME (1 << 10) /* (internal) no filename on stack */
342
343/* Flags for duk_def_prop() and its variants */
344#define DUK_DEFPROP_WRITABLE (1 << 0) /* set writable (effective if DUK_DEFPROP_HAVE_WRITABLE set) */
345#define DUK_DEFPROP_ENUMERABLE (1 << 1) /* set enumerable (effective if DUK_DEFPROP_HAVE_ENUMERABLE set) */
346#define DUK_DEFPROP_CONFIGURABLE (1 << 2) /* set configurable (effective if DUK_DEFPROP_HAVE_CONFIGURABLE set) */
347#define DUK_DEFPROP_HAVE_WRITABLE (1 << 3) /* set/clear writable */
348#define DUK_DEFPROP_HAVE_ENUMERABLE (1 << 4) /* set/clear enumerable */
349#define DUK_DEFPROP_HAVE_CONFIGURABLE (1 << 5) /* set/clear configurable */
350#define DUK_DEFPROP_HAVE_VALUE (1 << 6) /* set value (given on value stack) */
351#define DUK_DEFPROP_HAVE_GETTER (1 << 7) /* set getter (given on value stack) */
352#define DUK_DEFPROP_HAVE_SETTER (1 << 8) /* set setter (given on value stack) */
353#define DUK_DEFPROP_FORCE (1 << 9) /* force change if possible, may still fail for e.g. virtual properties */
354#define DUK_DEFPROP_SET_WRITABLE (DUK_DEFPROP_HAVE_WRITABLE | DUK_DEFPROP_WRITABLE)
355#define DUK_DEFPROP_CLEAR_WRITABLE DUK_DEFPROP_HAVE_WRITABLE
356#define DUK_DEFPROP_SET_ENUMERABLE (DUK_DEFPROP_HAVE_ENUMERABLE | DUK_DEFPROP_ENUMERABLE)
357#define DUK_DEFPROP_CLEAR_ENUMERABLE DUK_DEFPROP_HAVE_ENUMERABLE
358#define DUK_DEFPROP_SET_CONFIGURABLE (DUK_DEFPROP_HAVE_CONFIGURABLE | DUK_DEFPROP_CONFIGURABLE)
359#define DUK_DEFPROP_CLEAR_CONFIGURABLE DUK_DEFPROP_HAVE_CONFIGURABLE
360
361/* Flags for duk_push_thread_raw() */
362#define DUK_THREAD_NEW_GLOBAL_ENV (1 << 0) /* create a new global environment */
363
364/* Flags for duk_gc() */
365#define DUK_GC_COMPACT (1 << 0) /* compact heap objects */
366
367/* Error codes (must be 8 bits at most, see duk_error.h) */
368#define DUK_ERR_NONE 0 /* no error (e.g. from duk_get_error_code()) */
369#define DUK_ERR_ERROR 1 /* Error */
370#define DUK_ERR_EVAL_ERROR 2 /* EvalError */
371#define DUK_ERR_RANGE_ERROR 3 /* RangeError */
372#define DUK_ERR_REFERENCE_ERROR 4 /* ReferenceError */
373#define DUK_ERR_SYNTAX_ERROR 5 /* SyntaxError */
374#define DUK_ERR_TYPE_ERROR 6 /* TypeError */
375#define DUK_ERR_URI_ERROR 7 /* URIError */
376
377/* Return codes for C functions (shortcut for throwing an error) */
378#define DUK_RET_ERROR (-DUK_ERR_ERROR)
379#define DUK_RET_EVAL_ERROR (-DUK_ERR_EVAL_ERROR)
380#define DUK_RET_RANGE_ERROR (-DUK_ERR_RANGE_ERROR)
381#define DUK_RET_REFERENCE_ERROR (-DUK_ERR_REFERENCE_ERROR)
382#define DUK_RET_SYNTAX_ERROR (-DUK_ERR_SYNTAX_ERROR)
383#define DUK_RET_TYPE_ERROR (-DUK_ERR_TYPE_ERROR)
384#define DUK_RET_URI_ERROR (-DUK_ERR_URI_ERROR)
385
386/* Return codes for protected calls (duk_safe_call(), duk_pcall()) */
387#define DUK_EXEC_SUCCESS 0
388#define DUK_EXEC_ERROR 1
389
390/* Debug levels for DUK_USE_DEBUG_WRITE(). */
391#define DUK_LEVEL_DEBUG 0
392#define DUK_LEVEL_DDEBUG 1
393#define DUK_LEVEL_DDDEBUG 2
394
395/*
396 * If no variadic macros, __FILE__ and __LINE__ are passed through globals
397 * which is ugly and not thread safe.
398 */
399
400#if !defined(DUK_API_VARIADIC_MACROS)
401DUK_EXTERNAL_DECL const char *duk_api_global_filename;
402DUK_EXTERNAL_DECL duk_int_t duk_api_global_line;
403#endif
404
405/*
406 * Context management
407 */
408
409DUK_EXTERNAL_DECL
410duk_context *duk_create_heap(duk_alloc_function alloc_func,
411 duk_realloc_function realloc_func,
412 duk_free_function free_func,
413 void *heap_udata,
414 duk_fatal_function fatal_handler);
415DUK_EXTERNAL_DECL void duk_destroy_heap(duk_context *ctx);
416
417DUK_EXTERNAL_DECL void duk_suspend(duk_context *ctx, duk_thread_state *state);
418DUK_EXTERNAL_DECL void duk_resume(duk_context *ctx, const duk_thread_state *state);
419
420#define duk_create_heap_default() \
421 duk_create_heap(NULL, NULL, NULL, NULL, NULL)
422
423/*
424 * Memory management
425 *
426 * Raw functions have no side effects (cannot trigger GC).
427 */
428
429DUK_EXTERNAL_DECL void *duk_alloc_raw(duk_context *ctx, duk_size_t size);
430DUK_EXTERNAL_DECL void duk_free_raw(duk_context *ctx, void *ptr);
431DUK_EXTERNAL_DECL void *duk_realloc_raw(duk_context *ctx, void *ptr, duk_size_t size);
432DUK_EXTERNAL_DECL void *duk_alloc(duk_context *ctx, duk_size_t size);
433DUK_EXTERNAL_DECL void duk_free(duk_context *ctx, void *ptr);
434DUK_EXTERNAL_DECL void *duk_realloc(duk_context *ctx, void *ptr, duk_size_t size);
435DUK_EXTERNAL_DECL void duk_get_memory_functions(duk_context *ctx, duk_memory_functions *out_funcs);
436DUK_EXTERNAL_DECL void duk_gc(duk_context *ctx, duk_uint_t flags);
437
438/*
439 * Error handling
440 */
441
442DUK_API_NORETURN(DUK_EXTERNAL_DECL void duk_throw_raw(duk_context *ctx));
443#define duk_throw(ctx) \
444 (duk_throw_raw((ctx)), (duk_ret_t) 0)
445DUK_API_NORETURN(DUK_EXTERNAL_DECL void duk_fatal_raw(duk_context *ctx, const char *err_msg));
446#define duk_fatal(ctx,err_msg) \
447 (duk_fatal_raw((ctx), (err_msg)), (duk_ret_t) 0)
448
449#if defined(DUK_API_VARIADIC_MACROS)
450DUK_API_NORETURN(DUK_EXTERNAL_DECL void duk_error_raw(duk_context *ctx, duk_errcode_t err_code, const char *filename, duk_int_t line, const char *fmt, ...));
451#define duk_error(ctx,err_code,...) \
452 (duk_error_raw((ctx), (duk_errcode_t) (err_code), (const char *) (DUK_FILE_MACRO), (duk_int_t) (DUK_LINE_MACRO), __VA_ARGS__), (duk_ret_t) 0)
453#define duk_generic_error(ctx,...) \
454 (duk_error_raw((ctx), (duk_errcode_t) DUK_ERR_ERROR, (const char *) (DUK_FILE_MACRO), (duk_int_t) (DUK_LINE_MACRO), __VA_ARGS__), (duk_ret_t) 0)
455#define duk_eval_error(ctx,...) \
456 (duk_error_raw((ctx), (duk_errcode_t) DUK_ERR_EVAL_ERROR, (const char *) (DUK_FILE_MACRO), (duk_int_t) (DUK_LINE_MACRO), __VA_ARGS__), (duk_ret_t) 0)
457#define duk_range_error(ctx,...) \
458 (duk_error_raw((ctx), (duk_errcode_t) DUK_ERR_RANGE_ERROR, (const char *) (DUK_FILE_MACRO), (duk_int_t) (DUK_LINE_MACRO), __VA_ARGS__), (duk_ret_t) 0)
459#define duk_reference_error(ctx,...) \
460 (duk_error_raw((ctx), (duk_errcode_t) DUK_ERR_REFERENCE_ERROR, (const char *) (DUK_FILE_MACRO), (duk_int_t) (DUK_LINE_MACRO), __VA_ARGS__), (duk_ret_t) 0)
461#define duk_syntax_error(ctx,...) \
462 (duk_error_raw((ctx), (duk_errcode_t) DUK_ERR_SYNTAX_ERROR, (const char *) (DUK_FILE_MACRO), (duk_int_t) (DUK_LINE_MACRO), __VA_ARGS__), (duk_ret_t) 0)
463#define duk_type_error(ctx,...) \
464 (duk_error_raw((ctx), (duk_errcode_t) DUK_ERR_TYPE_ERROR, (const char *) (DUK_FILE_MACRO), (duk_int_t) (DUK_LINE_MACRO), __VA_ARGS__), (duk_ret_t) 0)
465#define duk_uri_error(ctx,...) \
466 (duk_error_raw((ctx), (duk_errcode_t) DUK_ERR_URI_ERROR, (const char *) (DUK_FILE_MACRO), (duk_int_t) (DUK_LINE_MACRO), __VA_ARGS__), (duk_ret_t) 0)
467#else /* DUK_API_VARIADIC_MACROS */
468/* For legacy compilers without variadic macros a macro hack is used to allow
469 * variable arguments. While the macro allows "return duk_error(...)", it
470 * will fail with e.g. "(void) duk_error(...)". The calls are noreturn but
471 * with a return value to allow the "return duk_error(...)" idiom. This may
472 * cause some compiler warnings, but without noreturn the generated code is
473 * often worse. The same approach as with variadic macros (using
474 * "(duk_error(...), 0)") won't work due to the macro hack structure.
475 */
476DUK_API_NORETURN(DUK_EXTERNAL_DECL duk_ret_t duk_error_stash(duk_context *ctx, duk_errcode_t err_code, const char *fmt, ...));
477DUK_API_NORETURN(DUK_EXTERNAL_DECL duk_ret_t duk_generic_error_stash(duk_context *ctx, const char *fmt, ...));
478DUK_API_NORETURN(DUK_EXTERNAL_DECL duk_ret_t duk_eval_error_stash(duk_context *ctx, const char *fmt, ...));
479DUK_API_NORETURN(DUK_EXTERNAL_DECL duk_ret_t duk_range_error_stash(duk_context *ctx, const char *fmt, ...));
480DUK_API_NORETURN(DUK_EXTERNAL_DECL duk_ret_t duk_reference_error_stash(duk_context *ctx, const char *fmt, ...));
481DUK_API_NORETURN(DUK_EXTERNAL_DECL duk_ret_t duk_syntax_error_stash(duk_context *ctx, const char *fmt, ...));
482DUK_API_NORETURN(DUK_EXTERNAL_DECL duk_ret_t duk_type_error_stash(duk_context *ctx, const char *fmt, ...));
483DUK_API_NORETURN(DUK_EXTERNAL_DECL duk_ret_t duk_uri_error_stash(duk_context *ctx, const char *fmt, ...));
484#define duk_error \
485 (duk_api_global_filename = (const char *) (DUK_FILE_MACRO), \
486 duk_api_global_line = (duk_int_t) (DUK_LINE_MACRO), \
487 duk_error_stash) /* last value is func pointer, arguments follow in parens */
488#define duk_generic_error \
489 (duk_api_global_filename = (const char *) (DUK_FILE_MACRO), \
490 duk_api_global_line = (duk_int_t) (DUK_LINE_MACRO), \
491 duk_generic_error_stash)
492#define duk_eval_error \
493 (duk_api_global_filename = (const char *) (DUK_FILE_MACRO), \
494 duk_api_global_line = (duk_int_t) (DUK_LINE_MACRO), \
495 duk_eval_error_stash)
496#define duk_range_error \
497 (duk_api_global_filename = (const char *) (DUK_FILE_MACRO), \
498 duk_api_global_line = (duk_int_t) (DUK_LINE_MACRO), \
499 duk_range_error_stash)
500#define duk_reference_error \
501 (duk_api_global_filename = (const char *) (DUK_FILE_MACRO), \
502 duk_api_global_line = (duk_int_t) (DUK_LINE_MACRO), \
503 duk_reference_error_stash)
504#define duk_syntax_error \
505 (duk_api_global_filename = (const char *) (DUK_FILE_MACRO), \
506 duk_api_global_line = (duk_int_t) (DUK_LINE_MACRO), \
507 duk_syntax_error_stash)
508#define duk_type_error \
509 (duk_api_global_filename = (const char *) (DUK_FILE_MACRO), \
510 duk_api_global_line = (duk_int_t) (DUK_LINE_MACRO), \
511 duk_type_error_stash)
512#define duk_uri_error \
513 (duk_api_global_filename = (const char *) (DUK_FILE_MACRO), \
514 duk_api_global_line = (duk_int_t) (DUK_LINE_MACRO), \
515 duk_uri_error_stash)
516#endif /* DUK_API_VARIADIC_MACROS */
517
518DUK_API_NORETURN(DUK_EXTERNAL_DECL 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));
519#define duk_error_va(ctx,err_code,fmt,ap) \
520 (duk_error_va_raw((ctx), (duk_errcode_t) (err_code), (const char *) (DUK_FILE_MACRO), (duk_int_t) (DUK_LINE_MACRO), (fmt), (ap)), (duk_ret_t) 0)
521#define duk_generic_error_va(ctx,fmt,ap) \
522 (duk_error_va_raw((ctx), (duk_errcode_t) DUK_ERR_ERROR, (const char *) (DUK_FILE_MACRO), (duk_int_t) (DUK_LINE_MACRO), (fmt), (ap)), (duk_ret_t) 0)
523#define duk_eval_error_va(ctx,fmt,ap) \
524 (duk_error_va_raw((ctx), (duk_errcode_t) DUK_ERR_EVAL_ERROR, (const char *) (DUK_FILE_MACRO), (duk_int_t) (DUK_LINE_MACRO), (fmt), (ap)), (duk_ret_t) 0)
525#define duk_range_error_va(ctx,fmt,ap) \
526 (duk_error_va_raw((ctx), (duk_errcode_t) DUK_ERR_RANGE_ERROR, (const char *) (DUK_FILE_MACRO), (duk_int_t) (DUK_LINE_MACRO), (fmt), (ap)), (duk_ret_t) 0)
527#define duk_reference_error_va(ctx,fmt,ap) \
528 (duk_error_va_raw((ctx), (duk_errcode_t) DUK_ERR_REFERENCE_ERROR, (const char *) (DUK_FILE_MACRO), (duk_int_t) (DUK_LINE_MACRO), (fmt), (ap)), (duk_ret_t) 0)
529#define duk_syntax_error_va(ctx,fmt,ap) \
530 (duk_error_va_raw((ctx), (duk_errcode_t) DUK_ERR_SYNTAX_ERROR, (const char *) (DUK_FILE_MACRO), (duk_int_t) (DUK_LINE_MACRO), (fmt), (ap)), (duk_ret_t) 0)
531#define duk_type_error_va(ctx,fmt,ap) \
532 (duk_error_va_raw((ctx), (duk_errcode_t) DUK_ERR_TYPE_ERROR, (const char *) (DUK_FILE_MACRO), (duk_int_t) (DUK_LINE_MACRO), (fmt), (ap)), (duk_ret_t) 0)
533#define duk_uri_error_va(ctx,fmt,ap) \
534 (duk_error_va_raw((ctx), (duk_errcode_t) DUK_ERR_URI_ERROR, (const char *) (DUK_FILE_MACRO), (duk_int_t) (DUK_LINE_MACRO), (fmt), (ap)), (duk_ret_t) 0)
535
536/*
537 * Other state related functions
538 */
539
540DUK_EXTERNAL_DECL duk_bool_t duk_is_strict_call(duk_context *ctx);
541DUK_EXTERNAL_DECL duk_bool_t duk_is_constructor_call(duk_context *ctx);
542
543/*
544 * Stack management
545 */
546
547DUK_EXTERNAL_DECL duk_idx_t duk_normalize_index(duk_context *ctx, duk_idx_t idx);
548DUK_EXTERNAL_DECL duk_idx_t duk_require_normalize_index(duk_context *ctx, duk_idx_t idx);
549DUK_EXTERNAL_DECL duk_bool_t duk_is_valid_index(duk_context *ctx, duk_idx_t idx);
550DUK_EXTERNAL_DECL void duk_require_valid_index(duk_context *ctx, duk_idx_t idx);
551
552DUK_EXTERNAL_DECL duk_idx_t duk_get_top(duk_context *ctx);
553DUK_EXTERNAL_DECL void duk_set_top(duk_context *ctx, duk_idx_t idx);
554DUK_EXTERNAL_DECL duk_idx_t duk_get_top_index(duk_context *ctx);
555DUK_EXTERNAL_DECL duk_idx_t duk_require_top_index(duk_context *ctx);
556
557/* Although extra/top could be an unsigned type here, using a signed type
558 * makes the API more robust to calling code calculation errors or corner
559 * cases (where caller might occasionally come up with negative values).
560 * Negative values are treated as zero, which is better than casting them
561 * to a large unsigned number. (This principle is used elsewhere in the
562 * API too.)
563 */
564DUK_EXTERNAL_DECL duk_bool_t duk_check_stack(duk_context *ctx, duk_idx_t extra);
565DUK_EXTERNAL_DECL void duk_require_stack(duk_context *ctx, duk_idx_t extra);
566DUK_EXTERNAL_DECL duk_bool_t duk_check_stack_top(duk_context *ctx, duk_idx_t top);
567DUK_EXTERNAL_DECL void duk_require_stack_top(duk_context *ctx, duk_idx_t top);
568
569/*
570 * Stack manipulation (other than push/pop)
571 */
572
573DUK_EXTERNAL_DECL void duk_swap(duk_context *ctx, duk_idx_t idx1, duk_idx_t idx2);
574DUK_EXTERNAL_DECL void duk_swap_top(duk_context *ctx, duk_idx_t idx);
575DUK_EXTERNAL_DECL void duk_dup(duk_context *ctx, duk_idx_t from_idx);
576DUK_EXTERNAL_DECL void duk_dup_top(duk_context *ctx);
577DUK_EXTERNAL_DECL void duk_insert(duk_context *ctx, duk_idx_t to_idx);
578DUK_EXTERNAL_DECL void duk_replace(duk_context *ctx, duk_idx_t to_idx);
579DUK_EXTERNAL_DECL void duk_copy(duk_context *ctx, duk_idx_t from_idx, duk_idx_t to_idx);
580DUK_EXTERNAL_DECL void duk_remove(duk_context *ctx, duk_idx_t idx);
581DUK_EXTERNAL_DECL void duk_xcopymove_raw(duk_context *to_ctx, duk_context *from_ctx, duk_idx_t count, duk_bool_t is_copy);
582
583#define duk_xmove_top(to_ctx,from_ctx,count) \
584 duk_xcopymove_raw((to_ctx), (from_ctx), (count), 0 /*is_copy*/)
585#define duk_xcopy_top(to_ctx,from_ctx,count) \
586 duk_xcopymove_raw((to_ctx), (from_ctx), (count), 1 /*is_copy*/)
587
588/*
589 * Push operations
590 *
591 * Push functions return the absolute (relative to bottom of frame)
592 * position of the pushed value for convenience.
593 *
594 * Note: duk_dup() is technically a push.
595 */
596
597DUK_EXTERNAL_DECL void duk_push_undefined(duk_context *ctx);
598DUK_EXTERNAL_DECL void duk_push_null(duk_context *ctx);
599DUK_EXTERNAL_DECL void duk_push_boolean(duk_context *ctx, duk_bool_t val);
600DUK_EXTERNAL_DECL void duk_push_true(duk_context *ctx);
601DUK_EXTERNAL_DECL void duk_push_false(duk_context *ctx);
602DUK_EXTERNAL_DECL void duk_push_number(duk_context *ctx, duk_double_t val);
603DUK_EXTERNAL_DECL void duk_push_nan(duk_context *ctx);
604DUK_EXTERNAL_DECL void duk_push_int(duk_context *ctx, duk_int_t val);
605DUK_EXTERNAL_DECL void duk_push_uint(duk_context *ctx, duk_uint_t val);
606DUK_EXTERNAL_DECL const char *duk_push_string(duk_context *ctx, const char *str);
607DUK_EXTERNAL_DECL const char *duk_push_lstring(duk_context *ctx, const char *str, duk_size_t len);
608DUK_EXTERNAL_DECL void duk_push_pointer(duk_context *ctx, void *p);
609DUK_EXTERNAL_DECL const char *duk_push_sprintf(duk_context *ctx, const char *fmt, ...);
610DUK_EXTERNAL_DECL const char *duk_push_vsprintf(duk_context *ctx, const char *fmt, va_list ap);
611
612DUK_EXTERNAL_DECL void duk_push_this(duk_context *ctx);
613DUK_EXTERNAL_DECL void duk_push_current_function(duk_context *ctx);
614DUK_EXTERNAL_DECL void duk_push_current_thread(duk_context *ctx);
615DUK_EXTERNAL_DECL void duk_push_global_object(duk_context *ctx);
616DUK_EXTERNAL_DECL void duk_push_heap_stash(duk_context *ctx);
617DUK_EXTERNAL_DECL void duk_push_global_stash(duk_context *ctx);
618DUK_EXTERNAL_DECL void duk_push_thread_stash(duk_context *ctx, duk_context *target_ctx);
619
620DUK_EXTERNAL_DECL duk_idx_t duk_push_object(duk_context *ctx);
621DUK_EXTERNAL_DECL duk_idx_t duk_push_bare_object(duk_context *ctx);
622DUK_EXTERNAL_DECL duk_idx_t duk_push_array(duk_context *ctx);
623DUK_EXTERNAL_DECL duk_idx_t duk_push_c_function(duk_context *ctx, duk_c_function func, duk_idx_t nargs);
624DUK_EXTERNAL_DECL 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);
625DUK_EXTERNAL_DECL duk_idx_t duk_push_thread_raw(duk_context *ctx, duk_uint_t flags);
626
627#define duk_push_thread(ctx) \
628 duk_push_thread_raw((ctx), 0 /*flags*/)
629
630#define duk_push_thread_new_globalenv(ctx) \
631 duk_push_thread_raw((ctx), DUK_THREAD_NEW_GLOBAL_ENV /*flags*/)
632
633DUK_EXTERNAL_DECL 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, ...);
634
635#if defined(DUK_API_VARIADIC_MACROS)
636#define duk_push_error_object(ctx,err_code,...) \
637 duk_push_error_object_raw((ctx), (err_code), (const char *) (DUK_FILE_MACRO), (duk_int_t) (DUK_LINE_MACRO), __VA_ARGS__)
638#else
639DUK_EXTERNAL_DECL duk_idx_t duk_push_error_object_stash(duk_context *ctx, duk_errcode_t err_code, const char *fmt, ...);
640/* Note: parentheses are required so that the comma expression works in assignments. */
641#define duk_push_error_object \
642 (duk_api_global_filename = (const char *) (DUK_FILE_MACRO), \
643 duk_api_global_line = (duk_int_t) (DUK_LINE_MACRO), \
644 duk_push_error_object_stash) /* last value is func pointer, arguments follow in parens */
645#endif
646
647DUK_EXTERNAL_DECL 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);
648#define duk_push_error_object_va(ctx,err_code,fmt,ap) \
649 duk_push_error_object_va_raw((ctx), (err_code), (const char *) (DUK_FILE_MACRO), (duk_int_t) (DUK_LINE_MACRO), (fmt), (ap))
650
651#define DUK_BUF_FLAG_DYNAMIC (1 << 0) /* internal flag: dynamic buffer */
652#define DUK_BUF_FLAG_EXTERNAL (1 << 1) /* internal flag: external buffer */
653#define DUK_BUF_FLAG_NOZERO (1 << 2) /* internal flag: don't zero allocated buffer */
654
655DUK_EXTERNAL_DECL void *duk_push_buffer_raw(duk_context *ctx, duk_size_t size, duk_small_uint_t flags);
656
657#define duk_push_buffer(ctx,size,dynamic) \
658 duk_push_buffer_raw((ctx), (size), (dynamic) ? DUK_BUF_FLAG_DYNAMIC : 0)
659#define duk_push_fixed_buffer(ctx,size) \
660 duk_push_buffer_raw((ctx), (size), 0 /*flags*/)
661#define duk_push_dynamic_buffer(ctx,size) \
662 duk_push_buffer_raw((ctx), (size), DUK_BUF_FLAG_DYNAMIC /*flags*/)
663#define duk_push_external_buffer(ctx) \
664 ((void) duk_push_buffer_raw((ctx), 0, DUK_BUF_FLAG_DYNAMIC | DUK_BUF_FLAG_EXTERNAL))
665
666#define DUK_BUFOBJ_CREATE_ARRBUF (1 << 4) /* internal flag: create backing ArrayBuffer; keep in one byte */
667#define DUK_BUFOBJ_ARRAYBUFFER 0
668#define DUK_BUFOBJ_NODEJS_BUFFER (1 | DUK_BUFOBJ_CREATE_ARRBUF)
669#define DUK_BUFOBJ_DATAVIEW (2 | DUK_BUFOBJ_CREATE_ARRBUF)
670#define DUK_BUFOBJ_INT8ARRAY (3 | DUK_BUFOBJ_CREATE_ARRBUF)
671#define DUK_BUFOBJ_UINT8ARRAY (4 | DUK_BUFOBJ_CREATE_ARRBUF)
672#define DUK_BUFOBJ_UINT8CLAMPEDARRAY (5 | DUK_BUFOBJ_CREATE_ARRBUF)
673#define DUK_BUFOBJ_INT16ARRAY (6 | DUK_BUFOBJ_CREATE_ARRBUF)
674#define DUK_BUFOBJ_UINT16ARRAY (7 | DUK_BUFOBJ_CREATE_ARRBUF)
675#define DUK_BUFOBJ_INT32ARRAY (8 | DUK_BUFOBJ_CREATE_ARRBUF)
676#define DUK_BUFOBJ_UINT32ARRAY (9 | DUK_BUFOBJ_CREATE_ARRBUF)
677#define DUK_BUFOBJ_FLOAT32ARRAY (10 | DUK_BUFOBJ_CREATE_ARRBUF)
678#define DUK_BUFOBJ_FLOAT64ARRAY (11 | DUK_BUFOBJ_CREATE_ARRBUF)
679
680DUK_EXTERNAL_DECL 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);
681
682DUK_EXTERNAL_DECL duk_idx_t duk_push_heapptr(duk_context *ctx, void *ptr);
683
684/*
685 * Pop operations
686 */
687
688DUK_EXTERNAL_DECL void duk_pop(duk_context *ctx);
689DUK_EXTERNAL_DECL void duk_pop_n(duk_context *ctx, duk_idx_t count);
690DUK_EXTERNAL_DECL void duk_pop_2(duk_context *ctx);
691DUK_EXTERNAL_DECL void duk_pop_3(duk_context *ctx);
692
693/*
694 * Type checks
695 *
696 * duk_is_none(), which would indicate whether index it outside of stack,
697 * is not needed; duk_is_valid_index() gives the same information.
698 */
699
700DUK_EXTERNAL_DECL duk_int_t duk_get_type(duk_context *ctx, duk_idx_t idx);
701DUK_EXTERNAL_DECL duk_bool_t duk_check_type(duk_context *ctx, duk_idx_t idx, duk_int_t type);
702DUK_EXTERNAL_DECL duk_uint_t duk_get_type_mask(duk_context *ctx, duk_idx_t idx);
703DUK_EXTERNAL_DECL duk_bool_t duk_check_type_mask(duk_context *ctx, duk_idx_t idx, duk_uint_t mask);
704
705DUK_EXTERNAL_DECL duk_bool_t duk_is_undefined(duk_context *ctx, duk_idx_t idx);
706DUK_EXTERNAL_DECL duk_bool_t duk_is_null(duk_context *ctx, duk_idx_t idx);
707#define duk_is_null_or_undefined(ctx, idx) \
708 ((duk_get_type_mask((ctx), (idx)) & (DUK_TYPE_MASK_NULL | DUK_TYPE_MASK_UNDEFINED)) ? 1 : 0)
709
710DUK_EXTERNAL_DECL duk_bool_t duk_is_boolean(duk_context *ctx, duk_idx_t idx);
711DUK_EXTERNAL_DECL duk_bool_t duk_is_number(duk_context *ctx, duk_idx_t idx);
712DUK_EXTERNAL_DECL duk_bool_t duk_is_nan(duk_context *ctx, duk_idx_t idx);
713DUK_EXTERNAL_DECL duk_bool_t duk_is_string(duk_context *ctx, duk_idx_t idx);
714DUK_EXTERNAL_DECL duk_bool_t duk_is_object(duk_context *ctx, duk_idx_t idx);
715DUK_EXTERNAL_DECL duk_bool_t duk_is_buffer(duk_context *ctx, duk_idx_t idx);
716DUK_EXTERNAL_DECL duk_bool_t duk_is_buffer_data(duk_context *ctx, duk_idx_t idx);
717DUK_EXTERNAL_DECL duk_bool_t duk_is_pointer(duk_context *ctx, duk_idx_t idx);
718DUK_EXTERNAL_DECL duk_bool_t duk_is_lightfunc(duk_context *ctx, duk_idx_t idx);
719
720DUK_EXTERNAL_DECL duk_bool_t duk_is_symbol(duk_context *ctx, duk_idx_t idx);
721DUK_EXTERNAL_DECL duk_bool_t duk_is_array(duk_context *ctx, duk_idx_t idx);
722DUK_EXTERNAL_DECL duk_bool_t duk_is_function(duk_context *ctx, duk_idx_t idx);
723DUK_EXTERNAL_DECL duk_bool_t duk_is_c_function(duk_context *ctx, duk_idx_t idx);
724DUK_EXTERNAL_DECL duk_bool_t duk_is_ecmascript_function(duk_context *ctx, duk_idx_t idx);
725DUK_EXTERNAL_DECL duk_bool_t duk_is_bound_function(duk_context *ctx, duk_idx_t idx);
726DUK_EXTERNAL_DECL duk_bool_t duk_is_thread(duk_context *ctx, duk_idx_t idx);
727
728#define duk_is_callable(ctx,idx) \
729 duk_is_function((ctx), (idx))
730DUK_EXTERNAL_DECL duk_bool_t duk_is_dynamic_buffer(duk_context *ctx, duk_idx_t idx);
731DUK_EXTERNAL_DECL duk_bool_t duk_is_fixed_buffer(duk_context *ctx, duk_idx_t idx);
732DUK_EXTERNAL_DECL duk_bool_t duk_is_external_buffer(duk_context *ctx, duk_idx_t idx);
733
734/* Buffers and lightfuncs are not considered primitive because they mimic
735 * objects and e.g. duk_to_primitive() will coerce them instead of returning
736 * them as is. Symbols are represented as strings internally.
737 */
738#define duk_is_primitive(ctx,idx) \
739 duk_check_type_mask((ctx), (idx), DUK_TYPE_MASK_UNDEFINED | \
740 DUK_TYPE_MASK_NULL | \
741 DUK_TYPE_MASK_BOOLEAN | \
742 DUK_TYPE_MASK_NUMBER | \
743 DUK_TYPE_MASK_STRING | \
744 DUK_TYPE_MASK_POINTER)
745
746/* Symbols are object coercible, covered by DUK_TYPE_MASK_STRING. */
747#define duk_is_object_coercible(ctx,idx) \
748 duk_check_type_mask((ctx), (idx), DUK_TYPE_MASK_BOOLEAN | \
749 DUK_TYPE_MASK_NUMBER | \
750 DUK_TYPE_MASK_STRING | \
751 DUK_TYPE_MASK_OBJECT | \
752 DUK_TYPE_MASK_BUFFER | \
753 DUK_TYPE_MASK_POINTER | \
754 DUK_TYPE_MASK_LIGHTFUNC)
755
756DUK_EXTERNAL_DECL duk_errcode_t duk_get_error_code(duk_context *ctx, duk_idx_t idx);
757#define duk_is_error(ctx,idx) \
758 (duk_get_error_code((ctx), (idx)) != 0)
759#define duk_is_eval_error(ctx,idx) \
760 (duk_get_error_code((ctx), (idx)) == DUK_ERR_EVAL_ERROR)
761#define duk_is_range_error(ctx,idx) \
762 (duk_get_error_code((ctx), (idx)) == DUK_ERR_RANGE_ERROR)
763#define duk_is_reference_error(ctx,idx) \
764 (duk_get_error_code((ctx), (idx)) == DUK_ERR_REFERENCE_ERROR)
765#define duk_is_syntax_error(ctx,idx) \
766 (duk_get_error_code((ctx), (idx)) == DUK_ERR_SYNTAX_ERROR)
767#define duk_is_type_error(ctx,idx) \
768 (duk_get_error_code((ctx), (idx)) == DUK_ERR_TYPE_ERROR)
769#define duk_is_uri_error(ctx,idx) \
770 (duk_get_error_code((ctx), (idx)) == DUK_ERR_URI_ERROR)
771
772/*
773 * Get operations: no coercion, returns default value for invalid
774 * indices and invalid value types.
775 *
776 * duk_get_undefined() and duk_get_null() would be pointless and
777 * are not included.
778 */
779
780DUK_EXTERNAL_DECL duk_bool_t duk_get_boolean(duk_context *ctx, duk_idx_t idx);
781DUK_EXTERNAL_DECL duk_double_t duk_get_number(duk_context *ctx, duk_idx_t idx);
782DUK_EXTERNAL_DECL duk_int_t duk_get_int(duk_context *ctx, duk_idx_t idx);
783DUK_EXTERNAL_DECL duk_uint_t duk_get_uint(duk_context *ctx, duk_idx_t idx);
784DUK_EXTERNAL_DECL const char *duk_get_string(duk_context *ctx, duk_idx_t idx);
785DUK_EXTERNAL_DECL const char *duk_get_lstring(duk_context *ctx, duk_idx_t idx, duk_size_t *out_len);
786DUK_EXTERNAL_DECL void *duk_get_buffer(duk_context *ctx, duk_idx_t idx, duk_size_t *out_size);
787DUK_EXTERNAL_DECL void *duk_get_buffer_data(duk_context *ctx, duk_idx_t idx, duk_size_t *out_size);
788DUK_EXTERNAL_DECL void *duk_get_pointer(duk_context *ctx, duk_idx_t idx);
789DUK_EXTERNAL_DECL duk_c_function duk_get_c_function(duk_context *ctx, duk_idx_t idx);
790DUK_EXTERNAL_DECL duk_context *duk_get_context(duk_context *ctx, duk_idx_t idx);
791DUK_EXTERNAL_DECL void *duk_get_heapptr(duk_context *ctx, duk_idx_t idx);
792DUK_EXTERNAL_DECL duk_size_t duk_get_length(duk_context *ctx, duk_idx_t idx);
793DUK_EXTERNAL_DECL void duk_set_length(duk_context *ctx, duk_idx_t idx, duk_size_t len);
794
795/*
796 * Require operations: no coercion, throw error if index or type
797 * is incorrect. No defaulting.
798 */
799
800#define duk_require_type_mask(ctx,idx,mask) \
801 ((void) duk_check_type_mask((ctx), (idx), (mask) | DUK_TYPE_MASK_THROW))
802
803DUK_EXTERNAL_DECL void duk_require_undefined(duk_context *ctx, duk_idx_t idx);
804DUK_EXTERNAL_DECL void duk_require_null(duk_context *ctx, duk_idx_t idx);
805DUK_EXTERNAL_DECL duk_bool_t duk_require_boolean(duk_context *ctx, duk_idx_t idx);
806DUK_EXTERNAL_DECL duk_double_t duk_require_number(duk_context *ctx, duk_idx_t idx);
807DUK_EXTERNAL_DECL duk_int_t duk_require_int(duk_context *ctx, duk_idx_t idx);
808DUK_EXTERNAL_DECL duk_uint_t duk_require_uint(duk_context *ctx, duk_idx_t idx);
809DUK_EXTERNAL_DECL const char *duk_require_string(duk_context *ctx, duk_idx_t idx);
810DUK_EXTERNAL_DECL const char *duk_require_lstring(duk_context *ctx, duk_idx_t idx, duk_size_t *out_len);
811DUK_EXTERNAL_DECL void *duk_require_buffer(duk_context *ctx, duk_idx_t idx, duk_size_t *out_size);
812DUK_EXTERNAL_DECL void *duk_require_buffer_data(duk_context *ctx, duk_idx_t idx, duk_size_t *out_size);
813DUK_EXTERNAL_DECL void *duk_require_pointer(duk_context *ctx, duk_idx_t idx);
814DUK_EXTERNAL_DECL duk_c_function duk_require_c_function(duk_context *ctx, duk_idx_t idx);
815DUK_EXTERNAL_DECL duk_context *duk_require_context(duk_context *ctx, duk_idx_t idx);
816DUK_EXTERNAL_DECL void duk_require_function(duk_context *ctx, duk_idx_t idx);
817#define duk_require_callable(ctx,idx) \
818 duk_require_function((ctx), (idx))
819DUK_EXTERNAL_DECL void *duk_require_heapptr(duk_context *ctx, duk_idx_t idx);
820
821/* Symbols are object coercible and covered by DUK_TYPE_MASK_STRING. */
822#define duk_require_object_coercible(ctx,idx) \
823 ((void) duk_check_type_mask((ctx), (idx), DUK_TYPE_MASK_BOOLEAN | \
824 DUK_TYPE_MASK_NUMBER | \
825 DUK_TYPE_MASK_STRING | \
826 DUK_TYPE_MASK_OBJECT | \
827 DUK_TYPE_MASK_BUFFER | \
828 DUK_TYPE_MASK_POINTER | \
829 DUK_TYPE_MASK_LIGHTFUNC | \
830 DUK_TYPE_MASK_THROW))
831
832/*
833 * Coercion operations: in-place coercion, return coerced value where
834 * applicable. If index is invalid, throw error. Some coercions may
835 * throw an expected error (e.g. from a toString() or valueOf() call)
836 * or an internal error (e.g. from out of memory).
837 */
838
839DUK_EXTERNAL_DECL void duk_to_undefined(duk_context *ctx, duk_idx_t idx);
840DUK_EXTERNAL_DECL void duk_to_null(duk_context *ctx, duk_idx_t idx);
841DUK_EXTERNAL_DECL duk_bool_t duk_to_boolean(duk_context *ctx, duk_idx_t idx);
842DUK_EXTERNAL_DECL duk_double_t duk_to_number(duk_context *ctx, duk_idx_t idx);
843DUK_EXTERNAL_DECL duk_int_t duk_to_int(duk_context *ctx, duk_idx_t idx);
844DUK_EXTERNAL_DECL duk_uint_t duk_to_uint(duk_context *ctx, duk_idx_t idx);
845DUK_EXTERNAL_DECL duk_int32_t duk_to_int32(duk_context *ctx, duk_idx_t idx);
846DUK_EXTERNAL_DECL duk_uint32_t duk_to_uint32(duk_context *ctx, duk_idx_t idx);
847DUK_EXTERNAL_DECL duk_uint16_t duk_to_uint16(duk_context *ctx, duk_idx_t idx);
848DUK_EXTERNAL_DECL const char *duk_to_string(duk_context *ctx, duk_idx_t idx);
849DUK_EXTERNAL_DECL const char *duk_to_lstring(duk_context *ctx, duk_idx_t idx, duk_size_t *out_len);
850DUK_EXTERNAL_DECL void *duk_to_buffer_raw(duk_context *ctx, duk_idx_t idx, duk_size_t *out_size, duk_uint_t flags);
851DUK_EXTERNAL_DECL void *duk_to_pointer(duk_context *ctx, duk_idx_t idx);
852DUK_EXTERNAL_DECL void duk_to_object(duk_context *ctx, duk_idx_t idx);
853DUK_EXTERNAL_DECL void duk_to_primitive(duk_context *ctx, duk_idx_t idx, duk_int_t hint);
854
855#define DUK_BUF_MODE_FIXED 0 /* internal: request fixed buffer result */
856#define DUK_BUF_MODE_DYNAMIC 1 /* internal: request dynamic buffer result */
857#define DUK_BUF_MODE_DONTCARE 2 /* internal: don't care about fixed/dynamic nature */
858
859#define duk_to_buffer(ctx,idx,out_size) \
860 duk_to_buffer_raw((ctx), (idx), (out_size), DUK_BUF_MODE_DONTCARE)
861#define duk_to_fixed_buffer(ctx,idx,out_size) \
862 duk_to_buffer_raw((ctx), (idx), (out_size), DUK_BUF_MODE_FIXED)
863#define duk_to_dynamic_buffer(ctx,idx,out_size) \
864 duk_to_buffer_raw((ctx), (idx), (out_size), DUK_BUF_MODE_DYNAMIC)
865
866/* safe variants of a few coercion operations */
867DUK_EXTERNAL_DECL const char *duk_safe_to_lstring(duk_context *ctx, duk_idx_t idx, duk_size_t *out_len);
868#define duk_safe_to_string(ctx,idx) \
869 duk_safe_to_lstring((ctx), (idx), NULL)
870
871/*
872 * Misc conversion
873 */
874
875DUK_EXTERNAL_DECL const char *duk_base64_encode(duk_context *ctx, duk_idx_t idx);
876DUK_EXTERNAL_DECL void duk_base64_decode(duk_context *ctx, duk_idx_t idx);
877DUK_EXTERNAL_DECL const char *duk_hex_encode(duk_context *ctx, duk_idx_t idx);
878DUK_EXTERNAL_DECL void duk_hex_decode(duk_context *ctx, duk_idx_t idx);
879DUK_EXTERNAL_DECL const char *duk_json_encode(duk_context *ctx, duk_idx_t idx);
880DUK_EXTERNAL_DECL void duk_json_decode(duk_context *ctx, duk_idx_t idx);
881
882DUK_EXTERNAL_DECL const char *duk_buffer_to_string(duk_context *ctx, duk_idx_t idx);
883
884/*
885 * Buffer
886 */
887
888DUK_EXTERNAL_DECL void *duk_resize_buffer(duk_context *ctx, duk_idx_t idx, duk_size_t new_size);
889DUK_EXTERNAL_DECL void *duk_steal_buffer(duk_context *ctx, duk_idx_t idx, duk_size_t *out_size);
890DUK_EXTERNAL_DECL void duk_config_buffer(duk_context *ctx, duk_idx_t idx, void *ptr, duk_size_t len);
891
892/*
893 * Property access
894 *
895 * The basic function assumes key is on stack. The _string variant takes
896 * a C string as a property name, while the _index variant takes an array
897 * index as a property name (e.g. 123 is equivalent to the key "123").
898 */
899
900DUK_EXTERNAL_DECL duk_bool_t duk_get_prop(duk_context *ctx, duk_idx_t obj_idx);
901DUK_EXTERNAL_DECL duk_bool_t duk_get_prop_string(duk_context *ctx, duk_idx_t obj_idx, const char *key);
902DUK_EXTERNAL_DECL duk_bool_t duk_get_prop_lstring(duk_context *ctx, duk_idx_t obj_idx, const char *key, duk_size_t key_len);
903DUK_EXTERNAL_DECL duk_bool_t duk_get_prop_index(duk_context *ctx, duk_idx_t obj_idx, duk_uarridx_t arr_idx);
904DUK_EXTERNAL_DECL duk_bool_t duk_put_prop(duk_context *ctx, duk_idx_t obj_idx);
905DUK_EXTERNAL_DECL duk_bool_t duk_put_prop_string(duk_context *ctx, duk_idx_t obj_idx, const char *key);
906DUK_EXTERNAL_DECL duk_bool_t duk_put_prop_lstring(duk_context *ctx, duk_idx_t obj_idx, const char *key, duk_size_t key_len);
907DUK_EXTERNAL_DECL duk_bool_t duk_put_prop_index(duk_context *ctx, duk_idx_t obj_idx, duk_uarridx_t arr_idx);
908DUK_EXTERNAL_DECL duk_bool_t duk_del_prop(duk_context *ctx, duk_idx_t obj_idx);
909DUK_EXTERNAL_DECL duk_bool_t duk_del_prop_string(duk_context *ctx, duk_idx_t obj_idx, const char *key);
910DUK_EXTERNAL_DECL duk_bool_t duk_del_prop_lstring(duk_context *ctx, duk_idx_t obj_idx, const char *key, duk_size_t key_len);
911DUK_EXTERNAL_DECL duk_bool_t duk_del_prop_index(duk_context *ctx, duk_idx_t obj_idx, duk_uarridx_t arr_idx);
912DUK_EXTERNAL_DECL duk_bool_t duk_has_prop(duk_context *ctx, duk_idx_t obj_idx);
913DUK_EXTERNAL_DECL duk_bool_t duk_has_prop_string(duk_context *ctx, duk_idx_t obj_idx, const char *key);
914DUK_EXTERNAL_DECL duk_bool_t duk_has_prop_lstring(duk_context *ctx, duk_idx_t obj_idx, const char *key, duk_size_t key_len);
915DUK_EXTERNAL_DECL duk_bool_t duk_has_prop_index(duk_context *ctx, duk_idx_t obj_idx, duk_uarridx_t arr_idx);
916
917DUK_EXTERNAL_DECL void duk_get_prop_desc(duk_context *ctx, duk_idx_t obj_idx, duk_uint_t flags);
918DUK_EXTERNAL_DECL void duk_def_prop(duk_context *ctx, duk_idx_t obj_idx, duk_uint_t flags);
919
920DUK_EXTERNAL_DECL duk_bool_t duk_get_global_string(duk_context *ctx, const char *key);
921DUK_EXTERNAL_DECL duk_bool_t duk_get_global_lstring(duk_context *ctx, const char *key, duk_size_t key_len);
922DUK_EXTERNAL_DECL duk_bool_t duk_put_global_string(duk_context *ctx, const char *key);
923DUK_EXTERNAL_DECL duk_bool_t duk_put_global_lstring(duk_context *ctx, const char *key, duk_size_t key_len);
924
925/*
926 * Inspection
927 */
928
929DUK_EXTERNAL_DECL void duk_inspect_value(duk_context *ctx, duk_idx_t idx);
930DUK_EXTERNAL_DECL void duk_inspect_callstack_entry(duk_context *ctx, duk_int_t level);
931
932/*
933 * Object prototype
934 */
935
936DUK_EXTERNAL_DECL void duk_get_prototype(duk_context *ctx, duk_idx_t idx);
937DUK_EXTERNAL_DECL void duk_set_prototype(duk_context *ctx, duk_idx_t idx);
938
939/*
940 * Object finalizer
941 */
942
943DUK_EXTERNAL_DECL void duk_get_finalizer(duk_context *ctx, duk_idx_t idx);
944DUK_EXTERNAL_DECL void duk_set_finalizer(duk_context *ctx, duk_idx_t idx);
945
946/*
947 * Global object
948 */
949
950DUK_EXTERNAL_DECL void duk_set_global_object(duk_context *ctx);
951
952/*
953 * Duktape/C function magic value
954 */
955
956DUK_EXTERNAL_DECL duk_int_t duk_get_magic(duk_context *ctx, duk_idx_t idx);
957DUK_EXTERNAL_DECL void duk_set_magic(duk_context *ctx, duk_idx_t idx, duk_int_t magic);
958DUK_EXTERNAL_DECL duk_int_t duk_get_current_magic(duk_context *ctx);
959
960/*
961 * Module helpers: put multiple function or constant properties
962 */
963
964DUK_EXTERNAL_DECL void duk_put_function_list(duk_context *ctx, duk_idx_t obj_idx, const duk_function_list_entry *funcs);
965DUK_EXTERNAL_DECL void duk_put_number_list(duk_context *ctx, duk_idx_t obj_idx, const duk_number_list_entry *numbers);
966
967/*
968 * Object operations
969 */
970
971DUK_EXTERNAL_DECL void duk_compact(duk_context *ctx, duk_idx_t obj_idx);
972DUK_EXTERNAL_DECL void duk_enum(duk_context *ctx, duk_idx_t obj_idx, duk_uint_t enum_flags);
973DUK_EXTERNAL_DECL duk_bool_t duk_next(duk_context *ctx, duk_idx_t enum_idx, duk_bool_t get_value);
974
975/*
976 * String manipulation
977 */
978
979DUK_EXTERNAL_DECL void duk_concat(duk_context *ctx, duk_idx_t count);
980DUK_EXTERNAL_DECL void duk_join(duk_context *ctx, duk_idx_t count);
981DUK_EXTERNAL_DECL void duk_decode_string(duk_context *ctx, duk_idx_t idx, duk_decode_char_function callback, void *udata);
982DUK_EXTERNAL_DECL void duk_map_string(duk_context *ctx, duk_idx_t idx, duk_map_char_function callback, void *udata);
983DUK_EXTERNAL_DECL void duk_substring(duk_context *ctx, duk_idx_t idx, duk_size_t start_char_offset, duk_size_t end_char_offset);
984DUK_EXTERNAL_DECL void duk_trim(duk_context *ctx, duk_idx_t idx);
985DUK_EXTERNAL_DECL duk_codepoint_t duk_char_code_at(duk_context *ctx, duk_idx_t idx, duk_size_t char_offset);
986
987/*
988 * Ecmascript operators
989 */
990
991DUK_EXTERNAL_DECL duk_bool_t duk_equals(duk_context *ctx, duk_idx_t idx1, duk_idx_t idx2);
992DUK_EXTERNAL_DECL duk_bool_t duk_strict_equals(duk_context *ctx, duk_idx_t idx1, duk_idx_t idx2);
993DUK_EXTERNAL_DECL duk_bool_t duk_samevalue(duk_context *ctx, duk_idx_t idx1, duk_idx_t idx2);
994DUK_EXTERNAL_DECL duk_bool_t duk_instanceof(duk_context *ctx, duk_idx_t idx1, duk_idx_t idx2);
995
996/*
997 * Function (method) calls
998 */
999
1000DUK_EXTERNAL_DECL void duk_call(duk_context *ctx, duk_idx_t nargs);
1001DUK_EXTERNAL_DECL void duk_call_method(duk_context *ctx, duk_idx_t nargs);
1002DUK_EXTERNAL_DECL void duk_call_prop(duk_context *ctx, duk_idx_t obj_idx, duk_idx_t nargs);
1003DUK_EXTERNAL_DECL duk_int_t duk_pcall(duk_context *ctx, duk_idx_t nargs);
1004DUK_EXTERNAL_DECL duk_int_t duk_pcall_method(duk_context *ctx, duk_idx_t nargs);
1005DUK_EXTERNAL_DECL duk_int_t duk_pcall_prop(duk_context *ctx, duk_idx_t obj_idx, duk_idx_t nargs);
1006DUK_EXTERNAL_DECL void duk_new(duk_context *ctx, duk_idx_t nargs);
1007DUK_EXTERNAL_DECL duk_int_t duk_pnew(duk_context *ctx, duk_idx_t nargs);
1008DUK_EXTERNAL_DECL duk_int_t duk_safe_call(duk_context *ctx, duk_safe_call_function func, void *udata, duk_idx_t nargs, duk_idx_t nrets);
1009
1010/*
1011 * Thread management
1012 */
1013
1014/* There are currently no native functions to yield/resume, due to the internal
1015 * limitations on coroutine handling. These will be added later.
1016 */
1017
1018/*
1019 * Compilation and evaluation
1020 */
1021
1022DUK_EXTERNAL_DECL duk_int_t duk_eval_raw(duk_context *ctx, const char *src_buffer, duk_size_t src_length, duk_uint_t flags);
1023DUK_EXTERNAL_DECL duk_int_t duk_compile_raw(duk_context *ctx, const char *src_buffer, duk_size_t src_length, duk_uint_t flags);
1024
1025/* plain */
1026#define duk_eval(ctx) \
1027 ((void) duk_eval_raw((ctx), NULL, 0, 1 /*args*/ | DUK_COMPILE_EVAL | DUK_COMPILE_NOFILENAME))
1028
1029#define duk_eval_noresult(ctx) \
1030 ((void) duk_eval_raw((ctx), NULL, 0, 1 /*args*/ | DUK_COMPILE_EVAL | DUK_COMPILE_NORESULT | DUK_COMPILE_NOFILENAME))
1031
1032#define duk_peval(ctx) \
1033 (duk_eval_raw((ctx), NULL, 0, 1 /*args*/ | DUK_COMPILE_EVAL | DUK_COMPILE_SAFE | DUK_COMPILE_NOFILENAME))
1034
1035#define duk_peval_noresult(ctx) \
1036 (duk_eval_raw((ctx), NULL, 0, 1 /*args*/ | DUK_COMPILE_EVAL | DUK_COMPILE_SAFE | DUK_COMPILE_NORESULT | DUK_COMPILE_NOFILENAME))
1037
1038#define duk_compile(ctx,flags) \
1039 ((void) duk_compile_raw((ctx), NULL, 0, 2 /*args*/ | (flags)))
1040
1041#define duk_pcompile(ctx,flags) \
1042 (duk_compile_raw((ctx), NULL, 0, 2 /*args*/ | (flags) | DUK_COMPILE_SAFE))
1043
1044/* string */
1045#define duk_eval_string(ctx,src) \
1046 ((void) duk_eval_raw((ctx), (src), 0, 0 /*args*/ | DUK_COMPILE_EVAL | DUK_COMPILE_NOSOURCE | DUK_COMPILE_STRLEN | DUK_COMPILE_NOFILENAME))
1047
1048#define duk_eval_string_noresult(ctx,src) \
1049 ((void) duk_eval_raw((ctx), (src), 0, 0 /*args*/ | DUK_COMPILE_EVAL | DUK_COMPILE_NOSOURCE | DUK_COMPILE_STRLEN | DUK_COMPILE_NORESULT | DUK_COMPILE_NOFILENAME))
1050
1051#define duk_peval_string(ctx,src) \
1052 (duk_eval_raw((ctx), (src), 0, 0 /*args*/ | DUK_COMPILE_EVAL | DUK_COMPILE_SAFE | DUK_COMPILE_NOSOURCE | DUK_COMPILE_STRLEN | DUK_COMPILE_NOFILENAME))
1053
1054#define duk_peval_string_noresult(ctx,src) \
1055 (duk_eval_raw((ctx), (src), 0, 0 /*args*/ | DUK_COMPILE_EVAL | DUK_COMPILE_SAFE | DUK_COMPILE_NOSOURCE | DUK_COMPILE_STRLEN | DUK_COMPILE_NORESULT | DUK_COMPILE_NOFILENAME))
1056
1057#define duk_compile_string(ctx,flags,src) \
1058 ((void) duk_compile_raw((ctx), (src), 0, 0 /*args*/ | (flags) | DUK_COMPILE_NOSOURCE | DUK_COMPILE_STRLEN | DUK_COMPILE_NOFILENAME))
1059
1060#define duk_compile_string_filename(ctx,flags,src) \
1061 ((void) duk_compile_raw((ctx), (src), 0, 1 /*args*/ | (flags) | DUK_COMPILE_NOSOURCE | DUK_COMPILE_STRLEN))
1062
1063#define duk_pcompile_string(ctx,flags,src) \
1064 (duk_compile_raw((ctx), (src), 0, 0 /*args*/ | (flags) | DUK_COMPILE_SAFE | DUK_COMPILE_NOSOURCE | DUK_COMPILE_STRLEN | DUK_COMPILE_NOFILENAME))
1065
1066#define duk_pcompile_string_filename(ctx,flags,src) \
1067 (duk_compile_raw((ctx), (src), 0, 1 /*args*/ | (flags) | DUK_COMPILE_SAFE | DUK_COMPILE_NOSOURCE | DUK_COMPILE_STRLEN))
1068
1069/* lstring */
1070#define duk_eval_lstring(ctx,buf,len) \
1071 ((void) duk_eval_raw((ctx), buf, len, 0 /*args*/ | DUK_COMPILE_EVAL | DUK_COMPILE_NOSOURCE | DUK_COMPILE_NOFILENAME))
1072
1073#define duk_eval_lstring_noresult(ctx,buf,len) \
1074 ((void) duk_eval_raw((ctx), buf, len, 0 /*args*/ | DUK_COMPILE_EVAL | DUK_COMPILE_NOSOURCE | DUK_COMPILE_NORESULT | DUK_COMPILE_NOFILENAME))
1075
1076#define duk_peval_lstring(ctx,buf,len) \
1077 (duk_eval_raw((ctx), buf, len, 0 /*args*/ | DUK_COMPILE_EVAL | DUK_COMPILE_NOSOURCE | DUK_COMPILE_SAFE | DUK_COMPILE_NOFILENAME))
1078
1079#define duk_peval_lstring_noresult(ctx,buf,len) \
1080 (duk_eval_raw((ctx), buf, len, 0 /*args*/ | DUK_COMPILE_EVAL | DUK_COMPILE_SAFE | DUK_COMPILE_NOSOURCE | DUK_COMPILE_NORESULT | DUK_COMPILE_NOFILENAME))
1081
1082#define duk_compile_lstring(ctx,flags,buf,len) \
1083 ((void) duk_compile_raw((ctx), buf, len, 0 /*args*/ | (flags) | DUK_COMPILE_NOSOURCE | DUK_COMPILE_NOFILENAME))
1084
1085#define duk_compile_lstring_filename(ctx,flags,buf,len) \
1086 ((void) duk_compile_raw((ctx), buf, len, 1 /*args*/ | (flags) | DUK_COMPILE_NOSOURCE))
1087
1088#define duk_pcompile_lstring(ctx,flags,buf,len) \
1089 (duk_compile_raw((ctx), buf, len, 0 /*args*/ | (flags) | DUK_COMPILE_SAFE | DUK_COMPILE_NOSOURCE | DUK_COMPILE_NOFILENAME))
1090
1091#define duk_pcompile_lstring_filename(ctx,flags,buf,len) \
1092 (duk_compile_raw((ctx), buf, len, 1 /*args*/ | (flags) | DUK_COMPILE_SAFE | DUK_COMPILE_NOSOURCE))
1093
1094/*
1095 * Bytecode load/dump
1096 */
1097
1098DUK_EXTERNAL_DECL void duk_dump_function(duk_context *ctx);
1099DUK_EXTERNAL_DECL void duk_load_function(duk_context *ctx);
1100
1101/*
1102 * Debugging
1103 */
1104
1105DUK_EXTERNAL_DECL void duk_push_context_dump(duk_context *ctx);
1106
1107/*
1108 * Debugger (debug protocol)
1109 */
1110
1111DUK_EXTERNAL_DECL void duk_debugger_attach(duk_context *ctx,
1112 duk_debug_read_function read_cb,
1113 duk_debug_write_function write_cb,
1114 duk_debug_peek_function peek_cb,
1115 duk_debug_read_flush_function read_flush_cb,
1116 duk_debug_write_flush_function write_flush_cb,
1117 duk_debug_request_function request_cb,
1118 duk_debug_detached_function detached_cb,
1119 void *udata);
1120DUK_EXTERNAL_DECL void duk_debugger_detach(duk_context *ctx);
1121DUK_EXTERNAL_DECL void duk_debugger_cooperate(duk_context *ctx);
1122DUK_EXTERNAL_DECL duk_bool_t duk_debugger_notify(duk_context *ctx, duk_idx_t nvalues);
1123DUK_EXTERNAL_DECL void duk_debugger_pause(duk_context *ctx);
1124
1125/*
1126 * Time handling
1127 */
1128
1129DUK_EXTERNAL_DECL duk_double_t duk_get_now(duk_context *ctx);
1130DUK_EXTERNAL_DECL void duk_time_to_components(duk_context *ctx, duk_double_t timeval, duk_time_components *comp);
1131DUK_EXTERNAL_DECL duk_double_t duk_components_to_time(duk_context *ctx, duk_time_components *comp);
1132
1133/*
1134 * Date provider related constants
1135 *
1136 * NOTE: These are "semi public" - you should only use these if you write
1137 * your own platform specific Date provider, see doc/datetime.rst.
1138 */
1139
1140/* Millisecond count constants. */
1141#define DUK_DATE_MSEC_SECOND 1000L
1142#define DUK_DATE_MSEC_MINUTE (60L * 1000L)
1143#define DUK_DATE_MSEC_HOUR (60L * 60L * 1000L)
1144#define DUK_DATE_MSEC_DAY (24L * 60L * 60L * 1000L)
1145
1146/* Ecmascript date range is 100 million days from Epoch:
1147 * > 100e6 * 24 * 60 * 60 * 1000 // 100M days in millisecs
1148 * 8640000000000000
1149 * (= 8.64e15)
1150 */
1151#define DUK_DATE_MSEC_100M_DAYS (8.64e15)
1152#define DUK_DATE_MSEC_100M_DAYS_LEEWAY (8.64e15 + 24 * 3600e3)
1153
1154/* Ecmascript year range:
1155 * > new Date(100e6 * 24 * 3600e3).toISOString()
1156 * '+275760-09-13T00:00:00.000Z'
1157 * > new Date(-100e6 * 24 * 3600e3).toISOString()
1158 * '-271821-04-20T00:00:00.000Z'
1159 */
1160#define DUK_DATE_MIN_ECMA_YEAR (-271821L)
1161#define DUK_DATE_MAX_ECMA_YEAR 275760L
1162
1163/* Part indices for internal breakdowns. Part order from DUK_DATE_IDX_YEAR
1164 * to DUK_DATE_IDX_MILLISECOND matches argument ordering of Ecmascript API
1165 * calls (like Date constructor call). Some functions in duk_bi_date.c
1166 * depend on the specific ordering, so change with care. 16 bits are not
1167 * enough for all parts (year, specifically).
1168 *
1169 * Must be in-sync with genbuiltins.py.
1170 */
1171#define DUK_DATE_IDX_YEAR 0 /* year */
1172#define DUK_DATE_IDX_MONTH 1 /* month: 0 to 11 */
1173#define DUK_DATE_IDX_DAY 2 /* day within month: 0 to 30 */
1174#define DUK_DATE_IDX_HOUR 3
1175#define DUK_DATE_IDX_MINUTE 4
1176#define DUK_DATE_IDX_SECOND 5
1177#define DUK_DATE_IDX_MILLISECOND 6
1178#define DUK_DATE_IDX_WEEKDAY 7 /* weekday: 0 to 6, 0=sunday, 1=monday, etc */
1179#define DUK_DATE_IDX_NUM_PARTS 8
1180
1181/* Internal API call flags, used for various functions in duk_bi_date.c.
1182 * Certain flags are used by only certain functions, but since the flags
1183 * don't overlap, a single flags value can be passed around to multiple
1184 * functions.
1185 *
1186 * The unused top bits of the flags field are also used to pass values
1187 * to helpers (duk__get_part_helper() and duk__set_part_helper()).
1188 *
1189 * Must be in-sync with genbuiltins.py.
1190 */
1191
1192/* NOTE: when writing a Date provider you only need a few specific
1193 * flags from here, the rest are internal. Avoid using anything you
1194 * don't need.
1195 */
1196
1197#define DUK_DATE_FLAG_NAN_TO_ZERO (1 << 0) /* timeval breakdown: internal time value NaN -> zero */
1198#define DUK_DATE_FLAG_NAN_TO_RANGE_ERROR (1 << 1) /* timeval breakdown: internal time value NaN -> RangeError (toISOString) */
1199#define DUK_DATE_FLAG_ONEBASED (1 << 2) /* timeval breakdown: convert month and day-of-month parts to one-based (default is zero-based) */
1200#define DUK_DATE_FLAG_EQUIVYEAR (1 << 3) /* timeval breakdown: replace year with equivalent year in the [1971,2037] range for DST calculations */
1201#define DUK_DATE_FLAG_LOCALTIME (1 << 4) /* convert time value to local time */
1202#define DUK_DATE_FLAG_SUB1900 (1 << 5) /* getter: subtract 1900 from year when getting year part */
1203#define DUK_DATE_FLAG_TOSTRING_DATE (1 << 6) /* include date part in string conversion result */
1204#define DUK_DATE_FLAG_TOSTRING_TIME (1 << 7) /* include time part in string conversion result */
1205#define DUK_DATE_FLAG_TOSTRING_LOCALE (1 << 8) /* use locale specific formatting if available */
1206#define DUK_DATE_FLAG_TIMESETTER (1 << 9) /* setter: call is a time setter (affects hour, min, sec, ms); otherwise date setter (affects year, month, day-in-month) */
1207#define DUK_DATE_FLAG_YEAR_FIXUP (1 << 10) /* setter: perform 2-digit year fixup (00...99 -> 1900...1999) */
1208#define DUK_DATE_FLAG_SEP_T (1 << 11) /* string conversion: use 'T' instead of ' ' as a separator */
1209#define DUK_DATE_FLAG_VALUE_SHIFT 12 /* additional values begin at bit 12 */
1210
1211/*
1212 * ROM pointer compression
1213 */
1214
1215/* Support array for ROM pointer compression. Only declared when ROM
1216 * pointer compression is active.
1217 */
1218#if defined(DUK_USE_ROM_OBJECTS) && defined(DUK_USE_HEAPPTR16)
1219DUK_EXTERNAL_DECL const void * const duk_rom_compressed_pointers[];
1220#endif
1221
1222/*
1223 * C++ name mangling
1224 */
1225
1226#if defined(__cplusplus)
1227/* end 'extern "C"' wrapper */
1228}
1229#endif
1230
1231#endif /* DUK_API_PUBLIC_H_INCLUDED */
1232
1233/*
1234 * END PUBLIC API
1235 */
1236
1237/*
1238 * Union to access IEEE double memory representation, indexes for double
1239 * memory representation, and some macros for double manipulation.
1240 *
1241 * Also used by packed duk_tval. Use a union for bit manipulation to
1242 * minimize aliasing issues in practice. The C99 standard does not
1243 * guarantee that this should work, but it's a very widely supported
1244 * practice for low level manipulation.
1245 *
1246 * IEEE double format summary:
1247 *
1248 * seeeeeee eeeeffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff
1249 * A B C D E F G H
1250 *
1251 * s sign bit
1252 * eee... exponent field
1253 * fff... fraction
1254 *
1255 * See http://en.wikipedia.org/wiki/Double_precision_floating-point_format.
1256 *
1257 * NaNs are represented as exponent 0x7ff and mantissa != 0. The NaN is a
1258 * signaling NaN when the highest bit of the mantissa is zero, and a quiet
1259 * NaN when the highest bit is set.
1260 *
1261 * At least three memory layouts are relevant here:
1262 *
1263 * A B C D E F G H Big endian (e.g. 68k) DUK_USE_DOUBLE_BE
1264 * H G F E D C B A Little endian (e.g. x86) DUK_USE_DOUBLE_LE
1265 * D C B A H G F E Mixed/cross endian (e.g. ARM) DUK_USE_DOUBLE_ME
1266 *
1267 * ARM is a special case: ARM double values are in mixed/cross endian
1268 * format while ARM duk_uint64_t values are in standard little endian
1269 * format (H G F E D C B A). When a double is read as a duk_uint64_t
1270 * from memory, the register will contain the (logical) value
1271 * E F G H A B C D. This requires some special handling below.
1272 *
1273 * Indexes of various types (8-bit, 16-bit, 32-bit) in memory relative to
1274 * the logical (big endian) order:
1275 *
1276 * byte order duk_uint8_t duk_uint16_t duk_uint32_t
1277 * BE 01234567 0123 01
1278 * LE 76543210 3210 10
1279 * ME (ARM) 32107654 1032 01
1280 *
1281 * Some processors may alter NaN values in a floating point load+store.
1282 * For instance, on X86 a FLD + FSTP may convert a signaling NaN to a
1283 * quiet one. This is catastrophic when NaN space is used in packed
1284 * duk_tval values. See: misc/clang_aliasing.c.
1285 */
1286
1287#if !defined(DUK_DBLUNION_H_INCLUDED)
1288#define DUK_DBLUNION_H_INCLUDED
1289
1290/*
1291 * Union for accessing double parts, also serves as packed duk_tval
1293
1294union duk_double_union {
1295 double d;
1296 float f[2];
1297#if defined(DUK_USE_64BIT_OPS)
1298 duk_uint64_t ull[1];
1299#endif
1300 duk_uint32_t ui[2];
1301 duk_uint16_t us[4];
1302 duk_uint8_t uc[8];
1303#if defined(DUK_USE_PACKED_TVAL)
1304 void *vp[2]; /* used by packed duk_tval, assumes sizeof(void *) == 4 */
1305#endif
1306};
1307
1309
1310/*
1311 * Indexes of various types with respect to big endian (logical) layout
1312 */
1313
1314#if defined(DUK_USE_DOUBLE_LE)
1315#if defined(DUK_USE_64BIT_OPS)
1316#define DUK_DBL_IDX_ULL0 0
1317#endif
1318#define DUK_DBL_IDX_UI0 1
1319#define DUK_DBL_IDX_UI1 0
1320#define DUK_DBL_IDX_US0 3
1321#define DUK_DBL_IDX_US1 2
1322#define DUK_DBL_IDX_US2 1
1323#define DUK_DBL_IDX_US3 0
1324#define DUK_DBL_IDX_UC0 7
1325#define DUK_DBL_IDX_UC1 6
1326#define DUK_DBL_IDX_UC2 5
1327#define DUK_DBL_IDX_UC3 4
1328#define DUK_DBL_IDX_UC4 3
1329#define DUK_DBL_IDX_UC5 2
1330#define DUK_DBL_IDX_UC6 1
1331#define DUK_DBL_IDX_UC7 0
1332#define DUK_DBL_IDX_VP0 DUK_DBL_IDX_UI0 /* packed tval */
1333#define DUK_DBL_IDX_VP1 DUK_DBL_IDX_UI1 /* packed tval */
1334#elif defined(DUK_USE_DOUBLE_BE)
1335#if defined(DUK_USE_64BIT_OPS)
1336#define DUK_DBL_IDX_ULL0 0
1337#endif
1338#define DUK_DBL_IDX_UI0 0
1339#define DUK_DBL_IDX_UI1 1
1340#define DUK_DBL_IDX_US0 0
1341#define DUK_DBL_IDX_US1 1
1342#define DUK_DBL_IDX_US2 2
1343#define DUK_DBL_IDX_US3 3
1344#define DUK_DBL_IDX_UC0 0
1345#define DUK_DBL_IDX_UC1 1
1346#define DUK_DBL_IDX_UC2 2
1347#define DUK_DBL_IDX_UC3 3
1348#define DUK_DBL_IDX_UC4 4
1349#define DUK_DBL_IDX_UC5 5
1350#define DUK_DBL_IDX_UC6 6
1351#define DUK_DBL_IDX_UC7 7
1352#define DUK_DBL_IDX_VP0 DUK_DBL_IDX_UI0 /* packed tval */
1353#define DUK_DBL_IDX_VP1 DUK_DBL_IDX_UI1 /* packed tval */
1354#elif defined(DUK_USE_DOUBLE_ME)
1355#if defined(DUK_USE_64BIT_OPS)
1356#define DUK_DBL_IDX_ULL0 0 /* not directly applicable, byte order differs from a double */
1357#endif
1358#define DUK_DBL_IDX_UI0 0
1359#define DUK_DBL_IDX_UI1 1
1360#define DUK_DBL_IDX_US0 1
1361#define DUK_DBL_IDX_US1 0
1362#define DUK_DBL_IDX_US2 3
1363#define DUK_DBL_IDX_US3 2
1364#define DUK_DBL_IDX_UC0 3
1365#define DUK_DBL_IDX_UC1 2
1366#define DUK_DBL_IDX_UC2 1
1367#define DUK_DBL_IDX_UC3 0
1368#define DUK_DBL_IDX_UC4 7
1369#define DUK_DBL_IDX_UC5 6
1370#define DUK_DBL_IDX_UC6 5
1371#define DUK_DBL_IDX_UC7 4
1372#define DUK_DBL_IDX_VP0 DUK_DBL_IDX_UI0 /* packed tval */
1373#define DUK_DBL_IDX_VP1 DUK_DBL_IDX_UI1 /* packed tval */
1374#else
1375#error internal error
1376#endif
1377
1378/*
1379 * Helper macros for reading/writing memory representation parts, used
1380 * by duk_numconv.c and duk_tval.h.
1381 */
1382
1383#define DUK_DBLUNION_SET_DOUBLE(u,v) do { \
1384 (u)->d = (v); \
1385 } while (0)
1386
1387#define DUK_DBLUNION_SET_HIGH32(u,v) do { \
1388 (u)->ui[DUK_DBL_IDX_UI0] = (duk_uint32_t) (v); \
1389 } while (0)
1390
1391#if defined(DUK_USE_64BIT_OPS)
1392#if defined(DUK_USE_DOUBLE_ME)
1393#define DUK_DBLUNION_SET_HIGH32_ZERO_LOW32(u,v) do { \
1394 (u)->ull[DUK_DBL_IDX_ULL0] = (duk_uint64_t) (v); \
1395 } while (0)
1396#else
1397#define DUK_DBLUNION_SET_HIGH32_ZERO_LOW32(u,v) do { \
1398 (u)->ull[DUK_DBL_IDX_ULL0] = ((duk_uint64_t) (v)) << 32; \
1399 } while (0)
1400#endif
1401#else /* DUK_USE_64BIT_OPS */
1402#define DUK_DBLUNION_SET_HIGH32_ZERO_LOW32(u,v) do { \
1403 (u)->ui[DUK_DBL_IDX_UI0] = (duk_uint32_t) (v); \
1404 (u)->ui[DUK_DBL_IDX_UI1] = (duk_uint32_t) 0; \
1405 } while (0)
1406#endif /* DUK_USE_64BIT_OPS */
1407
1408#define DUK_DBLUNION_SET_LOW32(u,v) do { \
1409 (u)->ui[DUK_DBL_IDX_UI1] = (duk_uint32_t) (v); \
1410 } while (0)
1411
1412#define DUK_DBLUNION_GET_DOUBLE(u) ((u)->d)
1413#define DUK_DBLUNION_GET_HIGH32(u) ((u)->ui[DUK_DBL_IDX_UI0])
1414#define DUK_DBLUNION_GET_LOW32(u) ((u)->ui[DUK_DBL_IDX_UI1])
1415
1416#if defined(DUK_USE_64BIT_OPS)
1417#if defined(DUK_USE_DOUBLE_ME)
1418#define DUK_DBLUNION_SET_UINT64(u,v) do { \
1419 (u)->ui[DUK_DBL_IDX_UI0] = (duk_uint32_t) ((v) >> 32); \
1420 (u)->ui[DUK_DBL_IDX_UI1] = (duk_uint32_t) (v); \
1421 } while (0)
1422#define DUK_DBLUNION_GET_UINT64(u) \
1423 ((((duk_uint64_t) (u)->ui[DUK_DBL_IDX_UI0]) << 32) | \
1424 ((duk_uint64_t) (u)->ui[DUK_DBL_IDX_UI1]))
1425#else
1426#define DUK_DBLUNION_SET_UINT64(u,v) do { \
1427 (u)->ull[DUK_DBL_IDX_ULL0] = (duk_uint64_t) (v); \
1428 } while (0)
1429#define DUK_DBLUNION_GET_UINT64(u) ((u)->ull[DUK_DBL_IDX_ULL0])
1430#endif
1431#define DUK_DBLUNION_SET_INT64(u,v) DUK_DBLUNION_SET_UINT64((u), (duk_uint64_t) (v))
1432#define DUK_DBLUNION_GET_INT64(u) ((duk_int64_t) DUK_DBLUNION_GET_UINT64((u)))
1433#endif /* DUK_USE_64BIT_OPS */
1434
1435/*
1436 * Double NaN manipulation macros related to NaN normalization needed when
1437 * using the packed duk_tval representation. NaN normalization is necessary
1438 * to keep double values compatible with the duk_tval format.
1439 *
1440 * When packed duk_tval is used, the NaN space is used to store pointers
1441 * and other tagged values in addition to NaNs. Actual NaNs are normalized
1442 * to a specific quiet NaN. The macros below are used by the implementation
1443 * to check and normalize NaN values when they might be created. The macros
1444 * are essentially NOPs when the non-packed duk_tval representation is used.
1445 *
1446 * A FULL check is exact and checks all bits. A NOTFULL check is used by
1447 * the packed duk_tval and works correctly for all NaNs except those that
1448 * begin with 0x7ff0. Since the 'normalized NaN' values used with packed
1449 * duk_tval begin with 0x7ff8, the partial check is reliable when packed
1450 * duk_tval is used. The 0x7ff8 prefix means the normalized NaN will be a
1451 * quiet NaN regardless of its remaining lower bits.
1452 *
1453 * The ME variant below is specifically for ARM byte order, which has the
1454 * feature that while doubles have a mixed byte order (32107654), unsigned
1455 * long long values has a little endian byte order (76543210). When writing
1456 * a logical double value through a ULL pointer, the 32-bit words need to be
1457 * swapped; hence the #if defined()s below for ULL writes with DUK_USE_DOUBLE_ME.
1458 * This is not full ARM support but suffices for some environments.
1459 */
1460
1461#if defined(DUK_USE_64BIT_OPS)
1462#if defined(DUK_USE_DOUBLE_ME)
1463/* Macros for 64-bit ops + mixed endian doubles. */
1464#define DUK__DBLUNION_SET_NAN_FULL(u) do { \
1465 (u)->ull[DUK_DBL_IDX_ULL0] = 0x000000007ff80000ULL; \
1466 } while (0)
1467#define DUK__DBLUNION_IS_NAN_FULL(u) \
1468 ((((u)->ull[DUK_DBL_IDX_ULL0] & 0x000000007ff00000ULL) == 0x000000007ff00000ULL) && \
1469 ((((u)->ull[DUK_DBL_IDX_ULL0]) & 0xffffffff000fffffULL) != 0))
1470#define DUK__DBLUNION_IS_NORMALIZED_NAN_FULL(u) \
1471 ((u)->ull[DUK_DBL_IDX_ULL0] == 0x000000007ff80000ULL)
1472#define DUK__DBLUNION_IS_ANYINF(u) \
1473 (((u)->ull[DUK_DBL_IDX_ULL0] & 0xffffffff7fffffffULL) == 0x000000007ff00000ULL)
1474#define DUK__DBLUNION_IS_POSINF(u) \
1475 ((u)->ull[DUK_DBL_IDX_ULL0] == 0x000000007ff00000ULL)
1476#define DUK__DBLUNION_IS_NEGINF(u) \
1477 ((u)->ull[DUK_DBL_IDX_ULL0] == 0x00000000fff00000ULL)
1478#define DUK__DBLUNION_IS_ANYZERO(u) \
1479 (((u)->ull[DUK_DBL_IDX_ULL0] & 0xffffffff7fffffffULL) == 0x0000000000000000ULL)
1480#define DUK__DBLUNION_IS_POSZERO(u) \
1481 ((u)->ull[DUK_DBL_IDX_ULL0] == 0x0000000000000000ULL)
1482#define DUK__DBLUNION_IS_NEGZERO(u) \
1483 ((u)->ull[DUK_DBL_IDX_ULL0] == 0x0000000080000000ULL)
1484#else
1485/* Macros for 64-bit ops + big/little endian doubles. */
1486#define DUK__DBLUNION_SET_NAN_FULL(u) do { \
1487 (u)->ull[DUK_DBL_IDX_ULL0] = 0x7ff8000000000000ULL; \
1488 } while (0)
1489#define DUK__DBLUNION_IS_NAN_FULL(u) \
1490 ((((u)->ull[DUK_DBL_IDX_ULL0] & 0x7ff0000000000000ULL) == 0x7ff0000000000000UL) && \
1491 ((((u)->ull[DUK_DBL_IDX_ULL0]) & 0x000fffffffffffffULL) != 0))
1492#define DUK__DBLUNION_IS_NORMALIZED_NAN_FULL(u) \
1493 ((u)->ull[DUK_DBL_IDX_ULL0] == 0x7ff8000000000000ULL)
1494#define DUK__DBLUNION_IS_ANYINF(u) \
1495 (((u)->ull[DUK_DBL_IDX_ULL0] & 0x7fffffffffffffffULL) == 0x7ff0000000000000ULL)
1496#define DUK__DBLUNION_IS_POSINF(u) \
1497 ((u)->ull[DUK_DBL_IDX_ULL0] == 0x7ff0000000000000ULL)
1498#define DUK__DBLUNION_IS_NEGINF(u) \
1499 ((u)->ull[DUK_DBL_IDX_ULL0] == 0xfff0000000000000ULL)
1500#define DUK__DBLUNION_IS_ANYZERO(u) \
1501 (((u)->ull[DUK_DBL_IDX_ULL0] & 0x7fffffffffffffffULL) == 0x0000000000000000ULL)
1502#define DUK__DBLUNION_IS_POSZERO(u) \
1503 ((u)->ull[DUK_DBL_IDX_ULL0] == 0x0000000000000000ULL)
1504#define DUK__DBLUNION_IS_NEGZERO(u) \
1505 ((u)->ull[DUK_DBL_IDX_ULL0] == 0x8000000000000000ULL)
1506#endif
1507#else /* DUK_USE_64BIT_OPS */
1508/* Macros for no 64-bit ops, any endianness. */
1509#define DUK__DBLUNION_SET_NAN_FULL(u) do { \
1510 (u)->ui[DUK_DBL_IDX_UI0] = (duk_uint32_t) 0x7ff80000UL; \
1511 (u)->ui[DUK_DBL_IDX_UI1] = (duk_uint32_t) 0x00000000UL; \
1512 } while (0)
1513#define DUK__DBLUNION_IS_NAN_FULL(u) \
1514 ((((u)->ui[DUK_DBL_IDX_UI0] & 0x7ff00000UL) == 0x7ff00000UL) && \
1515 (((u)->ui[DUK_DBL_IDX_UI0] & 0x000fffffUL) != 0 || \
1516 (u)->ui[DUK_DBL_IDX_UI1] != 0))
1517#define DUK__DBLUNION_IS_NORMALIZED_NAN_FULL(u) \
1518 (((u)->ui[DUK_DBL_IDX_UI0] == 0x7ff80000UL) && \
1519 ((u)->ui[DUK_DBL_IDX_UI1] == 0x00000000UL))
1520#define DUK__DBLUNION_IS_ANYINF(u) \
1521 ((((u)->ui[DUK_DBL_IDX_UI0] & 0x7fffffffUL) == 0x7ff00000UL) && \
1522 ((u)->ui[DUK_DBL_IDX_UI1] == 0x00000000UL))
1523#define DUK__DBLUNION_IS_POSINF(u) \
1524 (((u)->ui[DUK_DBL_IDX_UI0] == 0x7ff00000UL) && \
1525 ((u)->ui[DUK_DBL_IDX_UI1] == 0x00000000UL))
1526#define DUK__DBLUNION_IS_NEGINF(u) \
1527 (((u)->ui[DUK_DBL_IDX_UI0] == 0xfff00000UL) && \
1528 ((u)->ui[DUK_DBL_IDX_UI1] == 0x00000000UL))
1529#define DUK__DBLUNION_IS_ANYZERO(u) \
1530 ((((u)->ui[DUK_DBL_IDX_UI0] & 0x7fffffffUL) == 0x00000000UL) && \
1531 ((u)->ui[DUK_DBL_IDX_UI1] == 0x00000000UL))
1532#define DUK__DBLUNION_IS_POSZERO(u) \
1533 (((u)->ui[DUK_DBL_IDX_UI0] == 0x00000000UL) && \
1534 ((u)->ui[DUK_DBL_IDX_UI1] == 0x00000000UL))
1535#define DUK__DBLUNION_IS_NEGZERO(u) \
1536 (((u)->ui[DUK_DBL_IDX_UI0] == 0x80000000UL) && \
1537 ((u)->ui[DUK_DBL_IDX_UI1] == 0x00000000UL))
1538#endif /* DUK_USE_64BIT_OPS */
1539
1540#define DUK__DBLUNION_SET_NAN_NOTFULL(u) do { \
1541 (u)->us[DUK_DBL_IDX_US0] = 0x7ff8UL; \
1542 } while (0)
1543
1544#define DUK__DBLUNION_IS_NAN_NOTFULL(u) \
1545 /* E == 0x7ff, topmost four bits of F != 0 => assume NaN */ \
1546 ((((u)->us[DUK_DBL_IDX_US0] & 0x7ff0UL) == 0x7ff0UL) && \
1547 (((u)->us[DUK_DBL_IDX_US0] & 0x000fUL) != 0x0000UL))
1548
1549#define DUK__DBLUNION_IS_NORMALIZED_NAN_NOTFULL(u) \
1550 /* E == 0x7ff, F == 8 => normalized NaN */ \
1551 ((u)->us[DUK_DBL_IDX_US0] == 0x7ff8UL)
1552
1553#define DUK__DBLUNION_NORMALIZE_NAN_CHECK_FULL(u) do { \
1554 if (DUK__DBLUNION_IS_NAN_FULL((u))) { \
1555 DUK__DBLUNION_SET_NAN_FULL((u)); \
1556 } \
1557 } while (0)
1558
1559#define DUK__DBLUNION_NORMALIZE_NAN_CHECK_NOTFULL(u) do { \
1560 if (DUK__DBLUNION_IS_NAN_NOTFULL((u))) { \
1561 DUK__DBLUNION_SET_NAN_NOTFULL((u)); \
1562 } \
1563 } while (0)
1564
1565/* Concrete macros for NaN handling used by the implementation internals.
1566 * Chosen so that they match the duk_tval representation: with a packed
1567 * duk_tval, ensure NaNs are properly normalized; with a non-packed duk_tval
1568 * these are essentially NOPs.
1569 */
1570
1571#if defined(DUK_USE_PACKED_TVAL)
1572#if defined(DUK_USE_FULL_TVAL)
1573#define DUK_DBLUNION_NORMALIZE_NAN_CHECK(u) DUK__DBLUNION_NORMALIZE_NAN_CHECK_FULL((u))
1574#define DUK_DBLUNION_IS_NAN(u) DUK__DBLUNION_IS_NAN_FULL((u))
1575#define DUK_DBLUNION_IS_NORMALIZED_NAN(u) DUK__DBLUNION_IS_NORMALIZED_NAN_FULL((u))
1576#define DUK_DBLUNION_SET_NAN(d) DUK__DBLUNION_SET_NAN_FULL((d))
1577#else
1578#define DUK_DBLUNION_NORMALIZE_NAN_CHECK(u) DUK__DBLUNION_NORMALIZE_NAN_CHECK_NOTFULL((u))
1579#define DUK_DBLUNION_IS_NAN(u) DUK__DBLUNION_IS_NAN_NOTFULL((u))
1580#define DUK_DBLUNION_IS_NORMALIZED_NAN(u) DUK__DBLUNION_IS_NORMALIZED_NAN_NOTFULL((u))
1581#define DUK_DBLUNION_SET_NAN(d) DUK__DBLUNION_SET_NAN_NOTFULL((d))
1582#endif
1583#define DUK_DBLUNION_IS_NORMALIZED(u) \
1584 (!DUK_DBLUNION_IS_NAN((u)) || /* either not a NaN */ \
1585 DUK_DBLUNION_IS_NORMALIZED_NAN((u))) /* or is a normalized NaN */
1586#else /* DUK_USE_PACKED_TVAL */
1587#define DUK_DBLUNION_NORMALIZE_NAN_CHECK(u) /* nop: no need to normalize */
1588#define DUK_DBLUNION_IS_NAN(u) DUK__DBLUNION_IS_NAN_FULL((u)) /* (DUK_ISNAN((u)->d)) */
1589#define DUK_DBLUNION_IS_NORMALIZED_NAN(u) DUK__DBLUNION_IS_NAN_FULL((u)) /* (DUK_ISNAN((u)->d)) */
1590#define DUK_DBLUNION_IS_NORMALIZED(u) 1 /* all doubles are considered normalized */
1591#define DUK_DBLUNION_SET_NAN(u) do { \
1592 /* in non-packed representation we don't care about which NaN is used */ \
1593 (u)->d = DUK_DOUBLE_NAN; \
1594 } while (0)
1595#endif /* DUK_USE_PACKED_TVAL */
1596
1597#define DUK_DBLUNION_IS_ANYINF(u) DUK__DBLUNION_IS_ANYINF((u))
1598#define DUK_DBLUNION_IS_POSINF(u) DUK__DBLUNION_IS_POSINF((u))
1599#define DUK_DBLUNION_IS_NEGINF(u) DUK__DBLUNION_IS_NEGINF((u))
1600
1601#define DUK_DBLUNION_IS_ANYZERO(u) DUK__DBLUNION_IS_ANYZERO((u))
1602#define DUK_DBLUNION_IS_POSZERO(u) DUK__DBLUNION_IS_POSZERO((u))
1603#define DUK_DBLUNION_IS_NEGZERO(u) DUK__DBLUNION_IS_NEGZERO((u))
1604
1605/* XXX: native 64-bit byteswaps when available */
1606
1607/* 64-bit byteswap, same operation independent of target endianness. */
1608#define DUK_DBLUNION_BSWAP64(u) do { \
1609 duk_uint32_t duk__bswaptmp1, duk__bswaptmp2; \
1610 duk__bswaptmp1 = (u)->ui[0]; \
1611 duk__bswaptmp2 = (u)->ui[1]; \
1612 duk__bswaptmp1 = DUK_BSWAP32(duk__bswaptmp1); \
1613 duk__bswaptmp2 = DUK_BSWAP32(duk__bswaptmp2); \
1614 (u)->ui[0] = duk__bswaptmp2; \
1615 (u)->ui[1] = duk__bswaptmp1; \
1616 } while (0)
1617
1618/* Byteswap an IEEE double in the duk_double_union from host to network
1619 * order. For a big endian target this is a no-op.
1620 */
1621#if defined(DUK_USE_DOUBLE_LE)
1622#define DUK_DBLUNION_DOUBLE_HTON(u) do { \
1623 duk_uint32_t duk__bswaptmp1, duk__bswaptmp2; \
1624 duk__bswaptmp1 = (u)->ui[0]; \
1625 duk__bswaptmp2 = (u)->ui[1]; \
1626 duk__bswaptmp1 = DUK_BSWAP32(duk__bswaptmp1); \
1627 duk__bswaptmp2 = DUK_BSWAP32(duk__bswaptmp2); \
1628 (u)->ui[0] = duk__bswaptmp2; \
1629 (u)->ui[1] = duk__bswaptmp1; \
1630 } while (0)
1631#elif defined(DUK_USE_DOUBLE_ME)
1632#define DUK_DBLUNION_DOUBLE_HTON(u) do { \
1633 duk_uint32_t duk__bswaptmp1, duk__bswaptmp2; \
1634 duk__bswaptmp1 = (u)->ui[0]; \
1635 duk__bswaptmp2 = (u)->ui[1]; \
1636 duk__bswaptmp1 = DUK_BSWAP32(duk__bswaptmp1); \
1637 duk__bswaptmp2 = DUK_BSWAP32(duk__bswaptmp2); \
1638 (u)->ui[0] = duk__bswaptmp1; \
1639 (u)->ui[1] = duk__bswaptmp2; \
1640 } while (0)
1641#elif defined(DUK_USE_DOUBLE_BE)
1642#define DUK_DBLUNION_DOUBLE_HTON(u) do { } while (0)
1643#else
1644#error internal error, double endianness insane
1645#endif
1646
1647/* Reverse operation is the same. */
1648#define DUK_DBLUNION_DOUBLE_NTOH(u) DUK_DBLUNION_DOUBLE_HTON((u))
1649
1650/* Some sign bit helpers. */
1651#if defined(DUK_USE_64BIT_OPS)
1652#define DUK_DBLUNION_HAS_SIGNBIT(u) (((u)->ull[DUK_DBL_IDX_ULL0] & 0x8000000000000000ULL) != 0)
1653#define DUK_DBLUNION_GET_SIGNBIT(u) (((u)->ull[DUK_DBL_IDX_ULL0] >> 63U))
1654#else
1655#define DUK_DBLUNION_HAS_SIGNBIT(u) (((u)->ui[DUK_DBL_IDX_UI0] & 0x80000000UL) != 0)
1656#define DUK_DBLUNION_GET_SIGNBIT(u) (((u)->ui[DUK_DBL_IDX_UI0] >> 31U))
1657#endif
1658
1659#endif /* DUK_DBLUNION_H_INCLUDED */
1660
1661#endif /* DUKTAPE_H_INCLUDED */
Definition duktape.h:224
Definition duktape.h:230
Definition Viewer.h:139