/+junk/c_sdl_joypad_ducktape

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/%2Bjunk/c_sdl_joypad_ducktape

« back to all changes in this revision

Viewing changes to src/duktape.h

  • Committer: Gustav Hartvigsson
  • Date: 2014-06-15 17:02:39 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20140615170239-4xkvyjbrxfl760zz
* Started work on porting to Ducktape from MozJs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Duktape public API for Duktape 0.10.0.
 
3
 *  See the API reference for documentation on call semantics.
 
4
 *
 
5
 *  Git commit 78c73a152c35749ff5fe237209c353503ea6534d (v0.9.0-664-g78c73a1).
 
6
 *
 
7
 *  See Duktape AUTHORS.txt and LICENSE.txt for copyright and
 
8
 *  licensing information.
 
9
 */
 
10
 
 
11
#ifndef DUKTAPE_H_INCLUDED
 
12
#define DUKTAPE_H_INCLUDED
 
13
 
 
14
#ifdef __cplusplus
 
15
extern "C" {
 
16
#endif
 
17
 
 
18
/*
 
19
 *  Feature detection needed by this public header
 
20
 *
 
21
 *  DUK_API_NORETURN: macro for declaring a 'noreturn' function.
 
22
 *  Unfortunately the noreturn declaration may appear in various
 
23
 *  places of a function declaration, so the solution is to wrap
 
24
 *  the entire declaration inside the macro.  Compiler support
 
25
 *  for using a noreturn declaration on function pointers varies;
 
26
 *  this macro must only be used for actual function declarations.
 
27
 *
 
28
 *  http://gcc.gnu.org/onlinedocs/gcc-4.3.2//gcc/Function-Attributes.html
 
29
 *  http://clang.llvm.org/docs/LanguageExtensions.html
 
30
 */
 
31
 
 
32
#if defined(__GNUC__)
 
33
#if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__)
 
34
/* Convenience, e.g. gcc 4.5.1 == 40501; http://stackoverflow.com/questions/6031819/emulating-gccs-builtin-unreachable */
 
35
#define DUK_API_GCC_VERSION  (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
 
36
#else
 
37
#error cannot figure out gcc version
 
38
#endif
 
39
#endif
 
40
 
 
41
#if (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) || \
 
42
    (defined(__cplusplus) && (__cplusplus >= 201103L) && defined(__GNUC__))
 
43
#define DUK_API_VARIADIC_MACROS
 
44
#else
 
45
#undef DUK_API_VARIADIC_MACROS
 
46
#endif
 
47
 
 
48
#if defined(DUK_API_GCC_VERSION) && (DUK_API_GCC_VERSION >= 20500)
 
49
/* since gcc-2.5 */
 
50
#define DUK_API_NORETURN(decl)  decl __attribute__((noreturn))
 
51
#elif defined(__clang__)
 
52
/* syntax same as gcc */
 
53
#define DUK_API_NORETURN(decl)  decl __attribute__((noreturn))
 
54
#elif defined(_MSC_VER)
 
55
/* http://msdn.microsoft.com/en-us/library/aa235362(VS.60).aspx */
 
56
#define DUK_API_NORETURN(decl)  __declspec(noreturn) decl
 
57
#else
 
58
/* Don't know how to declare a noreturn function, so don't do it; this
 
59
 * may cause some spurious compilation warnings (e.g. "variable used
 
60
 * uninitialized").
 
61
 */
 
62
#define DUK_API_NORETURN(decl)  decl
 
63
#endif
 
64
 
 
65
/*
 
66
 *  Includes
 
67
 *
 
68
 *  Keep the set of includes minimal to serve tihs header only to avoid
 
69
 *  portability issues.  For instance, we can't rely on any C99 here.
 
70
 */
 
71
 
 
72
#include <limits.h>  /* INT_MIN, INT_MAX */
 
73
#include <stdarg.h>  /* va_list, etc */
 
74
#include <stdlib.h>  /* size_t (defined by stdlib.h and stddef.h) */
 
75
#include <stddef.h>
 
76
 
 
77
/*
 
78
 *  Typedefs; avoid all dependencies on internal types
 
79
 *
 
80
 *  (duk_context *) currently maps directly to internal type (duk_hthread *).
 
81
 *  Currently only primitive typedefs have a '_t' suffix.
 
82
 */
 
83
 
 
84
/* FIXME: proper detection */
 
85
typedef int duk_idx_t;
 
86
typedef int duk_ret_t;
 
87
typedef int duk_bool_t;
 
88
typedef size_t duk_size_t;
 
89
 
 
90
struct duk_memory_functions;
 
91
struct duk_function_list_entry;
 
92
struct duk_number_list_entry;
 
93
 
 
94
typedef void duk_context;
 
95
typedef struct duk_memory_functions duk_memory_functions;
 
96
typedef struct duk_function_list_entry duk_function_list_entry;
 
97
typedef struct duk_number_list_entry duk_number_list_entry;
 
98
 
 
99
typedef duk_ret_t (*duk_c_function)(duk_context *ctx);
 
100
typedef void *(*duk_alloc_function) (void *udata, duk_size_t size);
 
101
typedef void *(*duk_realloc_function) (void *udata, void *ptr, duk_size_t size);
 
102
typedef void (*duk_free_function) (void *udata, void *ptr);
 
103
typedef void (*duk_fatal_function) (duk_context *ctx, int code, const char *msg);
 
104
typedef void (*duk_decode_char_function) (void *udata, int codepoint);
 
105
typedef int (*duk_map_char_function) (void *udata, int codepoint);
 
106
typedef int (*duk_safe_call_function) (duk_context *ctx);
 
107
 
 
108
struct duk_memory_functions {
 
109
        duk_alloc_function alloc;
 
110
        duk_realloc_function realloc;
 
111
        duk_free_function free;
 
112
        void *udata;
 
113
};
 
114
 
 
115
struct duk_function_list_entry {
 
116
        const char *key;
 
117
        duk_c_function value;
 
118
        int nargs;
 
119
};
 
120
 
 
121
struct duk_number_list_entry {
 
122
        const char *key;
 
123
        double value;
 
124
};
 
125
 
 
126
/*
 
127
 *  Constants
 
128
 */
 
129
 
 
130
/* Duktape version, (major * 10000) + (minor * 100) + patch.  Allows C code
 
131
 * to #ifdef against Duktape API version.  The same value is also available
 
132
 * to Ecmascript code in Duktape.version.  Unofficial development snapshots
 
133
 * have 99 for patch level (e.g. 0.10.99 would be a development version
 
134
 * after 0.10.0 but before the next official release).
 
135
 */
 
136
#define DUK_VERSION                       1000L
 
137
 
 
138
/* Used to represent invalid index; if caller uses this without checking,
 
139
 * this index will map to a non-existent stack entry.  Also used in some
 
140
 * API calls as a marker to denote "no value".
 
141
 */
 
142
#define DUK_INVALID_INDEX                 INT_MIN 
 
143
 
 
144
/* Indicates that a native function does not have a fixed number of args,
 
145
 * and the argument stack should not be capped/extended at all.
 
146
 */
 
147
#define DUK_VARARGS                       (-1)
 
148
 
 
149
/* Number of value stack entries (in addition to actual call arguments)
 
150
 * guaranteed to be allocated on entry to a Duktape/C function.
 
151
 */
 
152
#define DUK_API_ENTRY_STACK               64
 
153
 
 
154
/* Value types, used by e.g. duk_get_type() */
 
155
#define DUK_TYPE_NONE                     0    /* no value, e.g. invalid index */
 
156
#define DUK_TYPE_UNDEFINED                1    /* Ecmascript undefined */
 
157
#define DUK_TYPE_NULL                     2    /* Ecmascript null */
 
158
#define DUK_TYPE_BOOLEAN                  3    /* Ecmascript boolean: 0 or 1 */
 
159
#define DUK_TYPE_NUMBER                   4    /* Ecmascript number: double */
 
160
#define DUK_TYPE_STRING                   5    /* Ecmascript string: CESU-8 / extended UTF-8 encoded */
 
161
#define DUK_TYPE_OBJECT                   6    /* Ecmascript object: includes objects, arrays, functions, threads */
 
162
#define DUK_TYPE_BUFFER                   7    /* fixed or dynamic, garbage collected byte buffer */
 
163
#define DUK_TYPE_POINTER                  8    /* raw void pointer */
 
164
 
 
165
/* Value mask types, used by e.g. duk_get_type_mask() */
 
166
#define DUK_TYPE_MASK_NONE                (1 << DUK_TYPE_NONE)
 
167
#define DUK_TYPE_MASK_UNDEFINED           (1 << DUK_TYPE_UNDEFINED)
 
168
#define DUK_TYPE_MASK_NULL                (1 << DUK_TYPE_NULL)
 
169
#define DUK_TYPE_MASK_BOOLEAN             (1 << DUK_TYPE_BOOLEAN)
 
170
#define DUK_TYPE_MASK_NUMBER              (1 << DUK_TYPE_NUMBER)
 
171
#define DUK_TYPE_MASK_STRING              (1 << DUK_TYPE_STRING)
 
172
#define DUK_TYPE_MASK_OBJECT              (1 << DUK_TYPE_OBJECT)
 
173
#define DUK_TYPE_MASK_BUFFER              (1 << DUK_TYPE_BUFFER)
 
174
#define DUK_TYPE_MASK_POINTER             (1 << DUK_TYPE_POINTER)
 
175
 
 
176
/* Coercion hints */
 
177
#define DUK_HINT_NONE                     0    /* prefer number, unless input is a Date, in which
 
178
                                                * case prefer string (E5 Section 8.12.8)
 
179
                                                */
 
180
#define DUK_HINT_STRING                   1    /* prefer string */
 
181
#define DUK_HINT_NUMBER                   2    /* prefer number */
 
182
 
 
183
/* Enumeration flags for duk_enum() */
 
184
#define DUK_ENUM_INCLUDE_NONENUMERABLE    (1 << 0)    /* enumerate non-numerable properties in addition to enumerable */
 
185
#define DUK_ENUM_INCLUDE_INTERNAL         (1 << 1)    /* enumerate internal properties (regardless of enumerability) */
 
186
#define DUK_ENUM_OWN_PROPERTIES_ONLY      (1 << 2)    /* don't walk prototype chain, only check own properties */
 
187
#define DUK_ENUM_ARRAY_INDICES_ONLY       (1 << 3)    /* only enumerate array indices */
 
188
#define DUK_ENUM_SORT_ARRAY_INDICES       (1 << 4)    /* sort array indices, use with DUK_ENUM_ARRAY_INDICES_ONLY */
 
189
 
 
190
/* Compilation flags for duk_compile() and duk_eval() */
 
191
#define DUK_COMPILE_EVAL                  (1 << 0)    /* compile eval code (instead of program) */
 
192
#define DUK_COMPILE_FUNCTION              (1 << 1)    /* compile function code (instead of program) */
 
193
#define DUK_COMPILE_STRICT                (1 << 2)    /* use strict (outer) context for program, eval, or function */
 
194
#define DUK_COMPILE_SAFE                  (1 << 3)    /* (internal) catch compilation errors */
 
195
#define DUK_COMPILE_NORESULT              (1 << 4)    /* (internal) omit eval result */
 
196
 
 
197
/* Flags for duk_push_thread_raw() */
 
198
#define DUK_THREAD_NEW_GLOBAL_ENV         (1 << 0)    /* create a new global environment */
 
199
 
 
200
/* Duktape specific error codes */
 
201
#define DUK_ERR_UNIMPLEMENTED_ERROR       50   /* UnimplementedError */
 
202
#define DUK_ERR_UNSUPPORTED_ERROR         51   /* UnsupportedError */
 
203
#define DUK_ERR_INTERNAL_ERROR            52   /* InternalError */
 
204
#define DUK_ERR_ALLOC_ERROR               53   /* AllocError */
 
205
#define DUK_ERR_ASSERTION_ERROR           54   /* AssertionError */
 
206
#define DUK_ERR_API_ERROR                 55   /* APIError */
 
207
#define DUK_ERR_UNCAUGHT_ERROR            56   /* UncaughtError */
 
208
 
 
209
/* Ecmascript E5 specification error codes */
 
210
#define DUK_ERR_ERROR                     100  /* Error */
 
211
#define DUK_ERR_EVAL_ERROR                101  /* EvalError */
 
212
#define DUK_ERR_RANGE_ERROR               102  /* RangeError */
 
213
#define DUK_ERR_REFERENCE_ERROR           103  /* ReferenceError */
 
214
#define DUK_ERR_SYNTAX_ERROR              104  /* SyntaxError */
 
215
#define DUK_ERR_TYPE_ERROR                105  /* TypeError */
 
216
#define DUK_ERR_URI_ERROR                 106  /* URIError */
 
217
 
 
218
/* Return codes for C functions (shortcut for throwing an error) */
 
219
#define DUK_RET_UNIMPLEMENTED_ERROR       (-DUK_ERR_UNIMPLEMENTED_ERROR)
 
220
#define DUK_RET_UNSUPPORTED_ERROR         (-DUK_ERR_UNSUPPORTED_ERROR)
 
221
#define DUK_RET_INTERNAL_ERROR            (-DUK_ERR_INTERNAL_ERROR)
 
222
#define DUK_RET_ALLOC_ERROR               (-DUK_ERR_ALLOC_ERROR)
 
223
#define DUK_RET_ASSERTION_ERROR           (-DUK_ERR_ASSERTION_ERROR)
 
224
#define DUK_RET_API_ERROR                 (-DUK_ERR_API_ERROR)
 
225
#define DUK_RET_UNCAUGHT_ERROR            (-DUK_ERR_UNCAUGHT_ERROR)
 
226
#define DUK_RET_ERROR                     (-DUK_ERR_ERROR)
 
227
#define DUK_RET_EVAL_ERROR                (-DUK_ERR_EVAL_ERROR)
 
228
#define DUK_RET_RANGE_ERROR               (-DUK_ERR_RANGE_ERROR)
 
229
#define DUK_RET_REFERENCE_ERROR           (-DUK_ERR_REFERENCE_ERROR)
 
230
#define DUK_RET_SYNTAX_ERROR              (-DUK_ERR_SYNTAX_ERROR)
 
231
#define DUK_RET_TYPE_ERROR                (-DUK_ERR_TYPE_ERROR)
 
232
#define DUK_RET_URI_ERROR                 (-DUK_ERR_URI_ERROR)
 
233
 
 
234
/* Return codes for protected calls (duk_safe_call(), duk_pcall()). */
 
235
#define DUK_EXEC_SUCCESS                  0
 
236
#define DUK_EXEC_ERROR                    1
 
237
 
 
238
/* Log levels */
 
239
#define  DUK_LOG_TRACE                    0
 
240
#define  DUK_LOG_DEBUG                    1
 
241
#define  DUK_LOG_INFO                     2
 
242
#define  DUK_LOG_WARN                     3
 
243
#define  DUK_LOG_ERROR                    4
 
244
#define  DUK_LOG_FATAL                    5
 
245
 
 
246
/*
 
247
 *  If no variadic macros, __FILE__ and __LINE__ are passed through globals
 
248
 *  which is ugly and not thread safe.
 
249
 */
 
250
 
 
251
#ifndef DUK_API_VARIADIC_MACROS
 
252
extern const char *duk_api_global_filename;
 
253
extern int duk_api_global_line;
 
254
#endif
 
255
 
 
256
/*
 
257
 *  Context management
 
258
 */
 
259
 
 
260
duk_context *duk_create_heap(duk_alloc_function alloc_func,
 
261
                             duk_realloc_function realloc_func,
 
262
                             duk_free_function free_func,
 
263
                             void *alloc_udata,
 
264
                             duk_fatal_function fatal_handler);
 
265
void duk_destroy_heap(duk_context *ctx);
 
266
 
 
267
#define duk_create_heap_default()  (duk_create_heap(NULL, NULL, NULL, NULL, NULL))
 
268
 
 
269
/*
 
270
 *  Memory management
 
271
 *
 
272
 *  Raw functions have no side effects (cannot trigger GC).
 
273
 */
 
274
 
 
275
void *duk_alloc_raw(duk_context *ctx, duk_size_t size);
 
276
void duk_free_raw(duk_context *ctx, void *ptr);
 
277
void *duk_realloc_raw(duk_context *ctx, void *ptr, duk_size_t size);
 
278
void *duk_alloc(duk_context *ctx, duk_size_t size);
 
279
void duk_free(duk_context *ctx, void *ptr);
 
280
void *duk_realloc(duk_context *ctx, void *ptr, duk_size_t size);
 
281
void duk_get_memory_functions(duk_context *ctx, duk_memory_functions *out_funcs);
 
282
void duk_gc(duk_context *ctx, int flags);
 
283
 
 
284
/*
 
285
 *  Error handling
 
286
 */
 
287
 
 
288
DUK_API_NORETURN(void duk_throw(duk_context *ctx));
 
289
 
 
290
DUK_API_NORETURN(void duk_error_raw(duk_context *ctx, int err_code, const char *filename, int line, const char *fmt, ...));
 
291
#ifdef DUK_API_VARIADIC_MACROS
 
292
#define duk_error(ctx,err_code,...)  \
 
293
        duk_error_raw((ctx),(err_code),__FILE__,__LINE__,__VA_ARGS__)
 
294
#else
 
295
DUK_API_NORETURN(void duk_error_stash(duk_context *ctx, int err_code, const char *fmt, ...));
 
296
#define duk_error  \
 
297
        duk_api_global_filename = __FILE__, \
 
298
        duk_api_global_line = __LINE__, \
 
299
        duk_error_stash  /* arguments follow */
 
300
#endif
 
301
 
 
302
DUK_API_NORETURN(void duk_fatal(duk_context *ctx, int err_code, const char *err_msg));
 
303
 
 
304
/*
 
305
 *  Other state related functions
 
306
 */
 
307
 
 
308
int duk_is_strict_call(duk_context *ctx);
 
309
int duk_is_constructor_call(duk_context *ctx);
 
310
int duk_get_magic(duk_context *ctx);
 
311
 
 
312
/*
 
313
 *  Stack management
 
314
 */
 
315
 
 
316
int duk_normalize_index(duk_context *ctx, int index);
 
317
int duk_require_normalize_index(duk_context *ctx, int index);
 
318
int duk_is_valid_index(duk_context *ctx, int index);
 
319
void duk_require_valid_index(duk_context *ctx, int index);
 
320
 
 
321
int duk_get_top(duk_context *ctx);
 
322
void duk_set_top(duk_context *ctx, int index);
 
323
int duk_get_top_index(duk_context *ctx);
 
324
int duk_require_top_index(duk_context *ctx);
 
325
 
 
326
int duk_check_stack(duk_context *ctx, unsigned int extra);
 
327
void duk_require_stack(duk_context *ctx, unsigned int extra);
 
328
int duk_check_stack_top(duk_context *ctx, unsigned int top);
 
329
void duk_require_stack_top(duk_context *ctx, unsigned int top);
 
330
 
 
331
/*
 
332
 *  Stack manipulation (other than push/pop)
 
333
 */
 
334
 
 
335
void duk_swap(duk_context *ctx, int index1, int index2);
 
336
void duk_swap_top(duk_context *ctx, int index);
 
337
void duk_dup(duk_context *ctx, int from_index);
 
338
void duk_dup_top(duk_context *ctx);
 
339
void duk_insert(duk_context *ctx, int to_index);
 
340
void duk_replace(duk_context *ctx, int to_index);
 
341
void duk_remove(duk_context *ctx, int index);
 
342
void duk_xmove(duk_context *from_ctx, duk_context *to_ctx, unsigned int count);  /* FIXME: undocumented */
 
343
 
 
344
/*
 
345
 *  Push operations
 
346
 *
 
347
 *  Push functions return the absolute (relative to bottom of frame)
 
348
 *  position of the pushed value for convenience.
 
349
 *
 
350
 *  Note: duk_dup() is technically a push.
 
351
 */
 
352
 
 
353
void duk_push_undefined(duk_context *ctx);
 
354
void duk_push_null(duk_context *ctx);
 
355
void duk_push_boolean(duk_context *ctx, int val);
 
356
void duk_push_true(duk_context *ctx);
 
357
void duk_push_false(duk_context *ctx);
 
358
void duk_push_number(duk_context *ctx, double val);
 
359
void duk_push_nan(duk_context *ctx);
 
360
void duk_push_int(duk_context *ctx, int val);
 
361
const char *duk_push_string(duk_context *ctx, const char *str);
 
362
const char *duk_push_string_file(duk_context *ctx, const char *path);
 
363
const char *duk_push_lstring(duk_context *ctx, const char *str, duk_size_t len);
 
364
void duk_push_pointer(duk_context *ctx, void *p);
 
365
const char *duk_push_sprintf(duk_context *ctx, const char *fmt, ...);
 
366
const char *duk_push_vsprintf(duk_context *ctx, const char *fmt, va_list ap);
 
367
 
 
368
void duk_push_this(duk_context *ctx);
 
369
void duk_push_current_function(duk_context *ctx);
 
370
void duk_push_current_thread(duk_context *ctx);
 
371
void duk_push_global_object(duk_context *ctx);
 
372
void duk_push_heap_stash(duk_context *ctx);
 
373
void duk_push_global_stash(duk_context *ctx);
 
374
void duk_push_thread_stash(duk_context *ctx, duk_context *target_ctx);
 
375
 
 
376
int duk_push_object(duk_context *ctx);
 
377
int duk_push_array(duk_context *ctx);
 
378
int duk_push_c_function(duk_context *ctx, duk_c_function func, int nargs);
 
379
int duk_push_thread_raw(duk_context *ctx, int flags);
 
380
 
 
381
#define duk_push_thread(ctx) \
 
382
        duk_push_thread_raw((ctx), 0 /*flags*/)
 
383
 
 
384
#define duk_push_thread_new_globalenv(ctx) \
 
385
        duk_push_thread_raw((ctx), DUK_THREAD_NEW_GLOBAL_ENV /*flags*/)
 
386
 
 
387
int duk_push_error_object_raw(duk_context *ctx, int err_code, const char *filename, int line, const char *fmt, ...);
 
388
#ifdef DUK_API_VARIADIC_MACROS
 
389
#define duk_push_error_object(ctx,err_code,...)  \
 
390
        duk_push_error_object_raw((ctx),(err_code),__FILE__,__LINE__,__VA_ARGS__)
 
391
#else
 
392
int duk_push_error_object_stash(duk_context *ctx, int err_code, const char *fmt, ...);
 
393
#define duk_push_error_object  \
 
394
        duk_api_global_filename = __FILE__, \
 
395
        duk_api_global_line = __LINE__, \
 
396
        duk_push_error_object_stash  /* arguments follow */
 
397
#endif
 
398
 
 
399
void *duk_push_buffer(duk_context *ctx, duk_size_t size, int dynamic);
 
400
void *duk_push_fixed_buffer(duk_context *ctx, duk_size_t size);
 
401
void *duk_push_dynamic_buffer(duk_context *ctx, duk_size_t size);
 
402
 
 
403
/*
 
404
 *  Pop operations
 
405
 */
 
406
 
 
407
void duk_pop(duk_context *ctx);
 
408
void duk_pop_n(duk_context *ctx, unsigned int count);
 
409
void duk_pop_2(duk_context *ctx);
 
410
void duk_pop_3(duk_context *ctx);
 
411
 
 
412
/*
 
413
 *  Type checks
 
414
 *
 
415
 *  duk_is_none(), which would indicate whether index it outside of stack,
 
416
 *  is not needed; duk_is_valid_index() gives the same information.
 
417
 */
 
418
 
 
419
int duk_get_type(duk_context *ctx, int index);
 
420
int duk_check_type(duk_context *ctx, int index, int type);
 
421
int duk_get_type_mask(duk_context *ctx, int index);
 
422
int duk_check_type_mask(duk_context *ctx, int index, int mask);
 
423
 
 
424
int duk_is_undefined(duk_context *ctx, int index);
 
425
int duk_is_null(duk_context *ctx, int index);
 
426
int duk_is_null_or_undefined(duk_context *ctx, int index);
 
427
int duk_is_boolean(duk_context *ctx, int index);
 
428
int duk_is_number(duk_context *ctx, int index);
 
429
int duk_is_nan(duk_context *ctx, int index);
 
430
int duk_is_string(duk_context *ctx, int index);
 
431
int duk_is_object(duk_context *ctx, int index);
 
432
int duk_is_buffer(duk_context *ctx, int index);
 
433
int duk_is_pointer(duk_context *ctx, int index);
 
434
 
 
435
int duk_is_array(duk_context *ctx, int index);
 
436
int duk_is_function(duk_context *ctx, int index);
 
437
int duk_is_c_function(duk_context *ctx, int index);
 
438
int duk_is_ecmascript_function(duk_context *ctx, int index);
 
439
int duk_is_bound_function(duk_context *ctx, int index);
 
440
int duk_is_thread(duk_context *ctx, int index);
 
441
 
 
442
int duk_is_callable(duk_context *ctx, int index);
 
443
int duk_is_dynamic(duk_context *ctx, int index);
 
444
int duk_is_fixed(duk_context *ctx, int index);
 
445
 
 
446
int duk_is_primitive(duk_context *ctx, int index);
 
447
int duk_is_object_coercible(duk_context *ctx, int index);
 
448
 
 
449
/*
 
450
 *  Get operations: no coercion, returns default value for invalid
 
451
 *  indices and invalid value types.
 
452
 *
 
453
 *  duk_get_undefined() and duk_get_null() would be pointless and
 
454
 *  are not included.
 
455
 */
 
456
 
 
457
int duk_get_boolean(duk_context *ctx, int index);
 
458
double duk_get_number(duk_context *ctx, int index);
 
459
int duk_get_int(duk_context *ctx, int index);
 
460
const char *duk_get_string(duk_context *ctx, int index);
 
461
const char *duk_get_lstring(duk_context *ctx, int index, duk_size_t *out_len);
 
462
void *duk_get_buffer(duk_context *ctx, int index, duk_size_t *out_size);
 
463
void *duk_get_pointer(duk_context *ctx, int index);
 
464
duk_c_function duk_get_c_function(duk_context *ctx, int index);
 
465
duk_context *duk_get_context(duk_context *ctx, int index);
 
466
duk_size_t duk_get_length(duk_context *ctx, int index);
 
467
 
 
468
/*
 
469
 *  Require operations: no coercion, throw error if index or type
 
470
 *  is incorrect.  No defaulting.
 
471
 */
 
472
 
 
473
void duk_require_undefined(duk_context *ctx, int index);
 
474
void duk_require_null(duk_context *ctx, int index);
 
475
int duk_require_boolean(duk_context *ctx, int index);
 
476
double duk_require_number(duk_context *ctx, int index);
 
477
int duk_require_int(duk_context *ctx, int index);
 
478
const char *duk_require_string(duk_context *ctx, int index);
 
479
const char *duk_require_lstring(duk_context *ctx, int index, duk_size_t *out_len);
 
480
void *duk_require_buffer(duk_context *ctx, int index, duk_size_t *out_size);
 
481
void *duk_require_pointer(duk_context *ctx, int index);
 
482
duk_c_function duk_require_c_function(duk_context *ctx, int index);
 
483
duk_context *duk_require_context(duk_context *ctx, int index);
 
484
 
 
485
/*
 
486
 *  Coercion operations: in-place coercion, return coerced value where
 
487
 *  applicable.  If index is invalid, throw error.  Some coercions may
 
488
 *  throw an expected error (e.g. from a toString() or valueOf() call)
 
489
 *  or an internal error (e.g. from out of memory).
 
490
 */
 
491
 
 
492
void duk_to_undefined(duk_context *ctx, int index);
 
493
void duk_to_null(duk_context *ctx, int index);
 
494
int duk_to_boolean(duk_context *ctx, int index);
 
495
double duk_to_number(duk_context *ctx, int index);
 
496
int duk_to_int(duk_context *ctx, int index);
 
497
int duk_to_int32(duk_context *ctx, int index);
 
498
unsigned int duk_to_uint32(duk_context *ctx, int index);
 
499
unsigned int duk_to_uint16(duk_context *ctx, int index);
 
500
const char *duk_to_string(duk_context *ctx, int index);
 
501
const char *duk_to_lstring(duk_context *ctx, int index, duk_size_t *out_len);
 
502
void *duk_to_buffer(duk_context *ctx, int index, duk_size_t *out_size);
 
503
void *duk_to_pointer(duk_context *ctx, int index);
 
504
void duk_to_object(duk_context *ctx, int index);
 
505
void duk_to_defaultvalue(duk_context *ctx, int index, int hint);
 
506
void duk_to_primitive(duk_context *ctx, int index, int hint);
 
507
 
 
508
/* safe variants of a few coercion operations */
 
509
const char *duk_safe_to_lstring(duk_context *ctx, int index, duk_size_t *out_len);
 
510
#define duk_safe_to_string(ctx,index) \
 
511
        duk_safe_to_lstring((ctx), (index), NULL)
 
512
 
 
513
/*
 
514
 *  Misc conversion
 
515
 */
 
516
 
 
517
const char *duk_base64_encode(duk_context *ctx, int index);
 
518
void duk_base64_decode(duk_context *ctx, int index);
 
519
const char *duk_hex_encode(duk_context *ctx, int index);
 
520
void duk_hex_decode(duk_context *ctx, int index);
 
521
const char *duk_json_encode(duk_context *ctx, int index);
 
522
void duk_json_decode(duk_context *ctx, int index);
 
523
 
 
524
/*
 
525
 *  Buffer
 
526
 */
 
527
 
 
528
void *duk_resize_buffer(duk_context *ctx, int index, duk_size_t new_size);
 
529
void duk_to_fixed_buffer(duk_context *ctx, int index);
 
530
 
 
531
/*
 
532
 *  Property access
 
533
 *
 
534
 *  The basic function assumes key is on stack.  The _string variant takes
 
535
 *  a C string as a property name, while the _index variant takes an array
 
536
 *  index as a property name (e.g. 123 is equivalent to the key "123").
 
537
 */
 
538
 
 
539
int duk_get_prop(duk_context *ctx, int obj_index);
 
540
int duk_get_prop_string(duk_context *ctx, int obj_index, const char *key);
 
541
int duk_get_prop_index(duk_context *ctx, int obj_index, unsigned int arr_index);
 
542
int duk_put_prop(duk_context *ctx, int obj_index);
 
543
int duk_put_prop_string(duk_context *ctx, int obj_index, const char *key);
 
544
int duk_put_prop_index(duk_context *ctx, int obj_index, unsigned int arr_index);
 
545
int duk_del_prop(duk_context *ctx, int obj_index);
 
546
int duk_del_prop_string(duk_context *ctx, int obj_index, const char *key);
 
547
int duk_del_prop_index(duk_context *ctx, int obj_index, unsigned int arr_index);
 
548
int duk_has_prop(duk_context *ctx, int obj_index);
 
549
int duk_has_prop_string(duk_context *ctx, int obj_index, const char *key);
 
550
int duk_has_prop_index(duk_context *ctx, int obj_index, unsigned int arr_index);
 
551
 
 
552
/*
 
553
 *  Module helpers: put multiple function or constant properties
 
554
 */
 
555
 
 
556
void duk_put_function_list(duk_context *ctx, int obj_index, const duk_function_list_entry *funcs);
 
557
void duk_put_number_list(duk_context *ctx, int obj_index, const duk_number_list_entry *numbers);
 
558
 
 
559
/*
 
560
 *  Variable access
 
561
 */
 
562
 
 
563
/* FIXME: incomplete, not usable now */
 
564
void duk_get_var(duk_context *ctx);
 
565
void duk_put_var(duk_context *ctx);
 
566
duk_bool_t duk_del_var(duk_context *ctx);
 
567
duk_bool_t duk_has_var(duk_context *ctx);
 
568
 
 
569
/*
 
570
 *  Object operations
 
571
 */
 
572
 
 
573
void duk_compact(duk_context *ctx, int obj_index);
 
574
void duk_enum(duk_context *ctx, int obj_index, int enum_flags);
 
575
int duk_next(duk_context *ctx, int enum_index, int get_value);
 
576
 
 
577
/*
 
578
 *  String manipulation
 
579
 */
 
580
 
 
581
void duk_concat(duk_context *ctx, unsigned int count);
 
582
void duk_join(duk_context *ctx, unsigned int count);
 
583
void duk_decode_string(duk_context *ctx, int index, duk_decode_char_function callback, void *udata);
 
584
void duk_map_string(duk_context *ctx, int index, duk_map_char_function callback, void *udata);
 
585
void duk_substring(duk_context *ctx, int index, duk_size_t start_offset, duk_size_t end_offset);
 
586
void duk_trim(duk_context *ctx, int index);
 
587
int duk_char_code_at(duk_context *ctx, int index, duk_size_t char_offset);
 
588
 
 
589
/*
 
590
 *  Ecmascript operators
 
591
 */
 
592
 
 
593
int duk_equals(duk_context *ctx, int index1, int index2);
 
594
int duk_strict_equals(duk_context *ctx, int index1, int index2);
 
595
 
 
596
/*
 
597
 *  Function (method) calls
 
598
 */
 
599
 
 
600
void duk_call(duk_context *ctx, int nargs);
 
601
void duk_call_method(duk_context *ctx, int nargs);
 
602
void duk_call_prop(duk_context *ctx, int obj_index, int nargs);
 
603
int duk_pcall(duk_context *ctx, int nargs);
 
604
int duk_pcall_method(duk_context *ctx, int nargs);
 
605
int duk_pcall_prop(duk_context *ctx, int obj_index, int nargs);
 
606
void duk_new(duk_context *ctx, int nargs);
 
607
int duk_safe_call(duk_context *ctx, duk_safe_call_function func, int nargs, int nrets);
 
608
 
 
609
/*
 
610
 *  Thread management
 
611
 */
 
612
 
 
613
/* There are currently no native functions to yield/resume, due to the internal
 
614
 * limitations on coroutine handling.  These will be added later.
 
615
 */
 
616
 
 
617
/*
 
618
 *  Compilation and evaluation
 
619
 */
 
620
 
 
621
int duk_eval_raw(duk_context *ctx, int flags);
 
622
int duk_compile_raw(duk_context *ctx, int flags);
 
623
 
 
624
#define duk_eval(ctx)  \
 
625
        ((void) duk_push_string((ctx), __FILE__), \
 
626
         (void) duk_eval_raw((ctx), DUK_COMPILE_EVAL))
 
627
 
 
628
#define duk_eval_noresult(ctx)  \
 
629
        ((void) duk_push_string((ctx), __FILE__), \
 
630
         (void) duk_eval_raw((ctx), DUK_COMPILE_EVAL | DUK_COMPILE_NORESULT))
 
631
 
 
632
#define duk_peval(ctx)  \
 
633
        ((void) duk_push_string((ctx), __FILE__), \
 
634
         duk_eval_raw((ctx), DUK_COMPILE_EVAL | DUK_COMPILE_SAFE))
 
635
 
 
636
#define duk_peval_noresult(ctx)  \
 
637
        ((void) duk_push_string((ctx), __FILE__), \
 
638
         duk_eval_raw((ctx), DUK_COMPILE_EVAL | DUK_COMPILE_SAFE | DUK_COMPILE_NORESULT))
 
639
 
 
640
#define duk_compile(ctx,flags)  \
 
641
        ((void) duk_compile_raw((ctx), (flags)))
 
642
 
 
643
#define duk_pcompile(ctx,flags)  \
 
644
        (duk_compile_raw((ctx), (flags) | DUK_COMPILE_SAFE))
 
645
 
 
646
#define duk_eval_string(ctx,src)  \
 
647
        ((void) duk_push_string((ctx), (src)), \
 
648
         (void) duk_push_string((ctx), __FILE__), \
 
649
         (void) duk_eval_raw((ctx), DUK_COMPILE_EVAL))
 
650
 
 
651
#define duk_eval_string_noresult(ctx,src)  \
 
652
        ((void) duk_push_string((ctx), (src)), \
 
653
         (void) duk_push_string((ctx), __FILE__), \
 
654
         (void) duk_eval_raw((ctx), DUK_COMPILE_EVAL | DUK_COMPILE_NORESULT))
 
655
 
 
656
#define duk_peval_string(ctx,src)  \
 
657
        ((void) duk_push_string((ctx), (src)), \
 
658
         (void) duk_push_string((ctx), __FILE__), \
 
659
         duk_eval_raw((ctx), DUK_COMPILE_EVAL | DUK_COMPILE_SAFE))
 
660
 
 
661
#define duk_peval_string_noresult(ctx,src)  \
 
662
        ((void) duk_push_string((ctx), (src)), \
 
663
         (void) duk_push_string((ctx), __FILE__), \
 
664
         duk_eval_raw((ctx), DUK_COMPILE_EVAL | DUK_COMPILE_SAFE | DUK_COMPILE_NORESULT))
 
665
 
 
666
#define duk_compile_string(ctx,flags,src)  \
 
667
        ((void) duk_push_string((ctx), (src)), \
 
668
         (void) duk_push_string((ctx), __FILE__), \
 
669
         (void) duk_compile_raw((ctx), (flags)))
 
670
 
 
671
#define duk_pcompile_string(ctx,flags,src)  \
 
672
        ((void) duk_push_string((ctx), (src)), \
 
673
         (void) duk_push_string((ctx), __FILE__), \
 
674
         duk_compile_raw((ctx), (flags) | DUK_COMPILE_SAFE))
 
675
 
 
676
#define duk_eval_file(ctx,path)  \
 
677
        ((void) duk_push_string_file((ctx), (path)), \
 
678
         (void) duk_push_string((ctx), (path)), \
 
679
         (void) duk_eval_raw((ctx), DUK_COMPILE_EVAL))
 
680
 
 
681
#define duk_eval_file_noresult(ctx,path)  \
 
682
        ((void) duk_push_string_file((ctx), (path)), \
 
683
         (void) duk_push_string((ctx), (path)), \
 
684
         (void) duk_eval_raw((ctx), DUK_COMPILE_EVAL | DUK_COMPILE_NORESULT))
 
685
 
 
686
#define duk_peval_file(ctx,path)  \
 
687
        ((void) duk_push_string_file((ctx), (path)), \
 
688
         (void) duk_push_string((ctx), (path)), \
 
689
         duk_eval_raw((ctx), DUK_COMPILE_EVAL | DUK_COMPILE_SAFE))
 
690
 
 
691
#define duk_peval_file_noresult(ctx,path)  \
 
692
        ((void) duk_push_string_file((ctx), (path)), \
 
693
         (void) duk_push_string((ctx), (path)), \
 
694
         duk_eval_raw((ctx), DUK_COMPILE_EVAL | DUK_COMPILE_SAFE | DUK_COMPILE_NORESULT))
 
695
 
 
696
#define duk_compile_file(ctx,flags,path)  \
 
697
        ((void) duk_push_string_file((ctx), (path)), \
 
698
         (void) duk_push_string((ctx), (path)), \
 
699
         (void) duk_compile_raw((ctx), (flags)))
 
700
 
 
701
#define duk_pcompile_file(ctx,flags,path)  \
 
702
        ((void) duk_push_string_file((ctx), (path)), \
 
703
         (void) duk_push_string((ctx), (path)), \
 
704
         duk_compile_raw((ctx), (flags) | DUK_COMPILE_SAFE))
 
705
 
 
706
/*
 
707
 *  Logging
 
708
 */
 
709
 
 
710
void duk_log(duk_context *ctx, int level, const char *fmt, ...);
 
711
 
 
712
#ifdef __cplusplus
 
713
}
 
714
#endif
 
715
 
 
716
#endif  /* DUKTAPE_H_INCLUDED */
 
717