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