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