comparison extern/duktape/duktape.h @ 0:1158cffe5a5e

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