2 * Internal API calls which have (stack and other) semantics similar
6 #ifndef DUK_API_INTERNAL_H_INCLUDED
7 #define DUK_API_INTERNAL_H_INCLUDED
9 /* duk_push_sprintf constants */
10 #define DUK_PUSH_SPRINTF_INITIAL_SIZE 256L
11 #define DUK_PUSH_SPRINTF_SANITY_LIMIT (1L * 1024L * 1024L * 1024L)
13 /* Flag ORed to err_code to indicate __FILE__ / __LINE__ is not
14 * blamed as source of error for error fileName / lineNumber.
16 #define DUK_ERRCODE_FLAG_NOBLAME_FILELINE (1L << 24)
18 /* Valstack resize flags */
19 #define DUK_VSRESIZE_FLAG_SHRINK (1 << 0)
20 #define DUK_VSRESIZE_FLAG_COMPACT (1 << 1)
21 #define DUK_VSRESIZE_FLAG_THROW (1 << 2)
23 /* Current convention is to use duk_size_t for value stack sizes and global indices,
24 * and duk_idx_t for local frame indices.
27 duk_bool_t
duk_valstack_resize_raw(duk_context
*ctx
,
28 duk_size_t min_new_size
,
29 duk_small_uint_t flags
);
31 #if defined(DUK_USE_VERBOSE_ERRORS) && defined(DUK_USE_PARANOID_ERRORS)
32 DUK_INTERNAL_DECL
const char *duk_get_type_name(duk_context
*ctx
, duk_idx_t index
);
35 DUK_INTERNAL_DECL duk_tval
*duk_get_tval(duk_context
*ctx
, duk_idx_t index
);
36 DUK_INTERNAL_DECL duk_tval
*duk_require_tval(duk_context
*ctx
, duk_idx_t index
);
37 DUK_INTERNAL_DECL
void duk_push_tval(duk_context
*ctx
, duk_tval
*tv
);
39 /* Push the current 'this' binding; throw TypeError if binding is not object
40 * coercible (CheckObjectCoercible).
42 DUK_INTERNAL_DECL
void duk_push_this_check_object_coercible(duk_context
*ctx
);
44 /* duk_push_this() + CheckObjectCoercible() + duk_to_object() */
45 DUK_INTERNAL_DECL duk_hobject
*duk_push_this_coercible_to_object(duk_context
*ctx
);
47 /* duk_push_this() + CheckObjectCoercible() + duk_to_string() */
48 DUK_INTERNAL_DECL duk_hstring
*duk_push_this_coercible_to_string(duk_context
*ctx
);
50 /* Get a borrowed duk_tval pointer to the current 'this' binding. Caller must
51 * make sure there's an active callstack entry. Note that the returned pointer
52 * is unstable with regards to side effects.
54 DUK_INTERNAL_DECL duk_tval
*duk_get_borrowed_this_tval(duk_context
*ctx
);
56 /* XXX: add fastint support? */
57 #define duk_push_u64(ctx,val) \
58 duk_push_number((ctx), (duk_double_t) (val))
59 #define duk_push_i64(ctx,val) \
60 duk_push_number((ctx), (duk_double_t) (val))
62 /* duk_push_(u)int() is guaranteed to support at least (un)signed 32-bit range */
63 #define duk_push_u32(ctx,val) \
64 duk_push_uint((ctx), (duk_uint_t) (val))
65 #define duk_push_i32(ctx,val) \
66 duk_push_int((ctx), (duk_int_t) (val))
68 /* sometimes stack and array indices need to go on the stack */
69 #define duk_push_idx(ctx,val) \
70 duk_push_int((ctx), (duk_int_t) (val))
71 #define duk_push_uarridx(ctx,val) \
72 duk_push_uint((ctx), (duk_uint_t) (val))
73 #define duk_push_size_t(ctx,val) \
74 duk_push_uint((ctx), (duk_uint_t) (val)) /* XXX: assumed to fit for now */
76 DUK_INTERNAL_DECL duk_hstring
*duk_get_hstring(duk_context
*ctx
, duk_idx_t index
);
77 DUK_INTERNAL_DECL duk_hobject
*duk_get_hobject(duk_context
*ctx
, duk_idx_t index
);
78 DUK_INTERNAL_DECL duk_hbuffer
*duk_get_hbuffer(duk_context
*ctx
, duk_idx_t index
);
79 DUK_INTERNAL_DECL duk_hthread
*duk_get_hthread(duk_context
*ctx
, duk_idx_t index
);
80 DUK_INTERNAL_DECL duk_hcompiledfunction
*duk_get_hcompiledfunction(duk_context
*ctx
, duk_idx_t index
);
81 DUK_INTERNAL_DECL duk_hnativefunction
*duk_get_hnativefunction(duk_context
*ctx
, duk_idx_t index
);
83 DUK_INTERNAL_DECL duk_hobject
*duk_get_hobject_with_class(duk_context
*ctx
, duk_idx_t index
, duk_small_uint_t classnum
);
85 #if 0 /* This would be pointless: unexpected type and lightfunc would both return NULL */
86 DUK_INTERNAL_DECL duk_hobject
*duk_get_hobject_or_lfunc(duk_context
*ctx
, duk_idx_t index
);
88 DUK_INTERNAL_DECL duk_hobject
*duk_get_hobject_or_lfunc_coerce(duk_context
*ctx
, duk_idx_t index
);
91 DUK_INTERNAL_DECL
void *duk_get_voidptr(duk_context
*ctx
, duk_idx_t index
);
94 DUK_INTERNAL_DECL duk_hstring
*duk_to_hstring(duk_context
*ctx
, duk_idx_t index
);
95 #if defined(DUK_USE_DEBUGGER_SUPPORT) /* only needed by debugger for now */
96 DUK_INTERNAL_DECL duk_hstring
*duk_safe_to_hstring(duk_context
*ctx
, duk_idx_t index
);
98 DUK_INTERNAL_DECL
void duk_to_object_class_string_top(duk_context
*ctx
);
99 #if !defined(DUK_USE_PARANOID_ERRORS)
100 DUK_INTERNAL_DECL
void duk_push_hobject_class_string(duk_context
*ctx
, duk_hobject
*h
);
103 DUK_INTERNAL_DECL duk_int_t
duk_to_int_clamped_raw(duk_context
*ctx
, duk_idx_t index
, duk_int_t minval
, duk_int_t maxval
, duk_bool_t
*out_clamped
); /* out_clamped=NULL, RangeError if outside range */
104 DUK_INTERNAL_DECL duk_int_t
duk_to_int_clamped(duk_context
*ctx
, duk_idx_t index
, duk_int_t minval
, duk_int_t maxval
);
105 DUK_INTERNAL_DECL duk_int_t
duk_to_int_check_range(duk_context
*ctx
, duk_idx_t index
, duk_int_t minval
, duk_int_t maxval
);
106 #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
107 DUK_INTERNAL_DECL duk_uint8_t
duk_to_uint8clamped(duk_context
*ctx
, duk_idx_t index
);
110 DUK_INTERNAL_DECL duk_hstring
*duk_require_hstring(duk_context
*ctx
, duk_idx_t index
);
111 DUK_INTERNAL_DECL duk_hobject
*duk_require_hobject(duk_context
*ctx
, duk_idx_t index
);
112 DUK_INTERNAL_DECL duk_hbuffer
*duk_require_hbuffer(duk_context
*ctx
, duk_idx_t index
);
113 DUK_INTERNAL_DECL duk_hthread
*duk_require_hthread(duk_context
*ctx
, duk_idx_t index
);
114 DUK_INTERNAL_DECL duk_hcompiledfunction
*duk_require_hcompiledfunction(duk_context
*ctx
, duk_idx_t index
);
115 DUK_INTERNAL_DECL duk_hnativefunction
*duk_require_hnativefunction(duk_context
*ctx
, duk_idx_t index
);
117 DUK_INTERNAL_DECL duk_hobject
*duk_require_hobject_with_class(duk_context
*ctx
, duk_idx_t index
, duk_small_uint_t classnum
);
119 DUK_INTERNAL_DECL duk_hobject
*duk_require_hobject_or_lfunc(duk_context
*ctx
, duk_idx_t index
);
120 DUK_INTERNAL_DECL duk_hobject
*duk_require_hobject_or_lfunc_coerce(duk_context
*ctx
, duk_idx_t index
);
122 DUK_INTERNAL_DECL
void duk_push_hstring(duk_context
*ctx
, duk_hstring
*h
);
123 DUK_INTERNAL_DECL
void duk_push_hstring_stridx(duk_context
*ctx
, duk_small_int_t stridx
);
124 DUK_INTERNAL_DECL
void duk_push_hobject(duk_context
*ctx
, duk_hobject
*h
);
125 DUK_INTERNAL_DECL
void duk_push_hbuffer(duk_context
*ctx
, duk_hbuffer
*h
);
126 #define duk_push_hthread(ctx,h) \
127 duk_push_hobject((ctx), (duk_hobject *) (h))
128 #define duk_push_hcompiledfunction(ctx,h) \
129 duk_push_hobject((ctx), (duk_hobject *) (h))
130 #define duk_push_hnativefunction(ctx,h) \
131 duk_push_hobject((ctx), (duk_hobject *) (h))
132 DUK_INTERNAL_DECL
void duk_push_hobject_bidx(duk_context
*ctx
, duk_small_int_t builtin_idx
);
133 DUK_INTERNAL_DECL duk_idx_t
duk_push_object_helper(duk_context
*ctx
, duk_uint_t hobject_flags_and_class
, duk_small_int_t prototype_bidx
);
134 DUK_INTERNAL_DECL duk_idx_t
duk_push_object_helper_proto(duk_context
*ctx
, duk_uint_t hobject_flags_and_class
, duk_hobject
*proto
);
135 DUK_INTERNAL_DECL duk_idx_t
duk_push_object_internal(duk_context
*ctx
);
136 DUK_INTERNAL_DECL duk_idx_t
duk_push_compiledfunction(duk_context
*ctx
);
137 DUK_INTERNAL_DECL
void duk_push_c_function_noexotic(duk_context
*ctx
, duk_c_function func
, duk_int_t nargs
);
138 DUK_INTERNAL_DECL
void duk_push_c_function_noconstruct_noexotic(duk_context
*ctx
, duk_c_function func
, duk_int_t nargs
);
140 DUK_INTERNAL_DECL
void duk_push_string_funcptr(duk_context
*ctx
, duk_uint8_t
*ptr
, duk_size_t sz
);
141 DUK_INTERNAL_DECL
void duk_push_lightfunc_name(duk_context
*ctx
, duk_tval
*tv
);
142 DUK_INTERNAL_DECL
void duk_push_lightfunc_tostring(duk_context
*ctx
, duk_tval
*tv
);
143 DUK_INTERNAL_DECL duk_hbufferobject
*duk_push_bufferobject_raw(duk_context
*ctx
, duk_uint_t hobject_flags_and_class
, duk_small_int_t prototype_bidx
);
145 #if !defined(DUK_USE_PARANOID_ERRORS)
146 DUK_INTERNAL_DECL
const char *duk_push_string_readable(duk_context
*ctx
, duk_idx_t index
);
147 DUK_INTERNAL_DECL
const char *duk_push_string_tval_readable(duk_context
*ctx
, duk_tval
*tv
);
150 DUK_INTERNAL_DECL duk_bool_t
duk_get_prop_stridx(duk_context
*ctx
, duk_idx_t obj_index
, duk_small_int_t stridx
); /* [] -> [val] */
151 DUK_INTERNAL_DECL duk_bool_t
duk_put_prop_stridx(duk_context
*ctx
, duk_idx_t obj_index
, duk_small_int_t stridx
); /* [val] -> [] */
152 DUK_INTERNAL_DECL duk_bool_t
duk_del_prop_stridx(duk_context
*ctx
, duk_idx_t obj_index
, duk_small_int_t stridx
); /* [] -> [] */
153 DUK_INTERNAL_DECL duk_bool_t
duk_has_prop_stridx(duk_context
*ctx
, duk_idx_t obj_index
, duk_small_int_t stridx
); /* [] -> [] */
155 DUK_INTERNAL_DECL duk_bool_t
duk_get_prop_stridx_boolean(duk_context
*ctx
, duk_idx_t obj_index
, duk_small_int_t stridx
, duk_bool_t
*out_has_prop
); /* [] -> [] */
157 DUK_INTERNAL_DECL
void duk_xdef_prop(duk_context
*ctx
, duk_idx_t obj_index
, duk_small_uint_t desc_flags
); /* [key val] -> [] */
158 DUK_INTERNAL_DECL
void duk_xdef_prop_index(duk_context
*ctx
, duk_idx_t obj_index
, duk_uarridx_t arr_index
, duk_small_uint_t desc_flags
); /* [val] -> [] */
159 DUK_INTERNAL_DECL
void duk_xdef_prop_stridx(duk_context
*ctx
, duk_idx_t obj_index
, duk_small_int_t stridx
, duk_small_uint_t desc_flags
); /* [val] -> [] */
160 DUK_INTERNAL_DECL
void duk_xdef_prop_stridx_builtin(duk_context
*ctx
, duk_idx_t obj_index
, duk_small_int_t stridx
, duk_small_int_t builtin_idx
, duk_small_uint_t desc_flags
); /* [] -> [] */
161 DUK_INTERNAL_DECL
void duk_xdef_prop_stridx_thrower(duk_context
*ctx
, duk_idx_t obj_index
, duk_small_int_t stridx
, duk_small_uint_t desc_flags
); /* [] -> [] */
163 /* These are macros for now, but could be separate functions to reduce code
164 * footprint (check call site count before refactoring).
166 #define duk_xdef_prop_wec(ctx,obj_index) \
167 duk_xdef_prop((ctx), (obj_index), DUK_PROPDESC_FLAGS_WEC)
168 #define duk_xdef_prop_index_wec(ctx,obj_index,arr_index) \
169 duk_xdef_prop_index((ctx), (obj_index), (arr_index), DUK_PROPDESC_FLAGS_WEC)
170 #define duk_xdef_prop_stridx_wec(ctx,obj_index,stridx) \
171 duk_xdef_prop_stridx((ctx), (obj_index), (stridx), DUK_PROPDESC_FLAGS_WEC)
173 /* Set object 'length'. */
174 DUK_INTERNAL_DECL
void duk_set_length(duk_context
*ctx
, duk_idx_t index
, duk_size_t length
);
176 /* Raw internal valstack access macros: access is unsafe so call site
177 * must have a guarantee that the index is valid. When that is the case,
178 * using these macro results in faster and smaller code than duk_get_tval().
179 * Both 'ctx' and 'idx' are evaluted multiple times, but only for asserts.
181 #define DUK_ASSERT_VALID_NEGIDX(ctx,idx) \
182 (DUK_ASSERT_EXPR((idx) < 0), DUK_ASSERT_EXPR(duk_is_valid_index((ctx), (idx))))
183 #define DUK_ASSERT_VALID_POSIDX(ctx,idx) \
184 (DUK_ASSERT_EXPR((idx) >= 0), DUK_ASSERT_EXPR(duk_is_valid_index((ctx), (idx))))
185 #define DUK_GET_TVAL_NEGIDX(ctx,idx) \
186 (DUK_ASSERT_VALID_NEGIDX((ctx),(idx)), ((duk_hthread *) (ctx))->valstack_top + (idx))
187 #define DUK_GET_TVAL_POSIDX(ctx,idx) \
188 (DUK_ASSERT_VALID_POSIDX((ctx),(idx)), ((duk_hthread *) (ctx))->valstack_bottom + (idx))
189 #define DUK_GET_HOBJECT_NEGIDX(ctx,idx) \
190 (DUK_ASSERT_VALID_NEGIDX((ctx),(idx)), DUK_TVAL_GET_OBJECT(((duk_hthread *) (ctx))->valstack_top + (idx)))
191 #define DUK_GET_HOBJECT_POSIDX(ctx,idx) \
192 (DUK_ASSERT_VALID_POSIDX((ctx),(idx)), DUK_TVAL_GET_OBJECT(((duk_hthread *) (ctx))->valstack_bottom + (idx)))
194 #endif /* DUK_API_INTERNAL_H_INCLUDED */