forked from svaarala/duktape
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathduk_bi_function.c
More file actions
338 lines (272 loc) · 9.95 KB
/
Copy pathduk_bi_function.c
File metadata and controls
338 lines (272 loc) · 9.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
* Function built-ins
*/
#include "duk_internal.h"
DUK_INTERNAL duk_ret_t duk_bi_function_constructor(duk_context *ctx) {
duk_hthread *thr = (duk_hthread *) ctx;
duk_hstring *h_sourcecode;
duk_idx_t nargs;
duk_idx_t i;
duk_small_uint_t comp_flags;
duk_hcompiledfunction *func;
duk_hobject *outer_lex_env;
duk_hobject *outer_var_env;
/* normal and constructor calls have identical semantics */
nargs = duk_get_top(ctx);
for (i = 0; i < nargs; i++) {
duk_to_string(ctx, i);
}
if (nargs == 0) {
duk_push_string(ctx, "");
duk_push_string(ctx, "");
} else if (nargs == 1) {
/* XXX: cover this with the generic >1 case? */
duk_push_string(ctx, "");
} else {
duk_insert(ctx, 0); /* [ arg1 ... argN-1 body] -> [body arg1 ... argN-1] */
duk_push_string(ctx, ",");
duk_insert(ctx, 1);
duk_join(ctx, nargs - 1);
}
/* [ body formals ], formals is comma separated list that needs to be parsed */
DUK_ASSERT_TOP(ctx, 2);
/* XXX: this placeholder is not always correct, but use for now.
* It will fail in corner cases; see test-dev-func-cons-args.js.
*/
duk_push_string(ctx, "function(");
duk_dup(ctx, 1);
duk_push_string(ctx, "){");
duk_dup(ctx, 0);
duk_push_string(ctx, "}");
duk_concat(ctx, 5);
/* [ body formals source ] */
DUK_ASSERT_TOP(ctx, 3);
/* strictness is not inherited, intentional */
comp_flags = DUK_JS_COMPILE_FLAG_FUNCEXPR;
duk_push_hstring_stridx(ctx, DUK_STRIDX_COMPILE); /* XXX: copy from caller? */ /* XXX: ignored now */
h_sourcecode = duk_require_hstring(ctx, -2);
duk_js_compile(thr,
(const duk_uint8_t *) DUK_HSTRING_GET_DATA(h_sourcecode),
(duk_size_t) DUK_HSTRING_GET_BYTELEN(h_sourcecode),
comp_flags);
func = (duk_hcompiledfunction *) duk_get_hobject(ctx, -1);
DUK_ASSERT(func != NULL);
DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION((duk_hobject *) func));
/* [ body formals source template ] */
/* only outer_lex_env matters, as functions always get a new
* variable declaration environment.
*/
outer_lex_env = thr->builtins[DUK_BIDX_GLOBAL_ENV];
outer_var_env = thr->builtins[DUK_BIDX_GLOBAL_ENV];
duk_js_push_closure(thr, func, outer_var_env, outer_lex_env);
/* [ body formals source template closure ] */
return 1;
}
DUK_INTERNAL duk_ret_t duk_bi_function_prototype(duk_context *ctx) {
/* ignore arguments, return undefined (E5 Section 15.3.4) */
DUK_UNREF(ctx);
return 0;
}
DUK_INTERNAL duk_ret_t duk_bi_function_prototype_to_string(duk_context *ctx) {
duk_tval *tv;
/*
* E5 Section 15.3.4.2 places few requirements on the output of
* this function:
*
* - The result is an implementation dependent representation
* of the function; in particular
*
* - The result must follow the syntax of a FunctionDeclaration.
* In particular, the function must have a name (even in the
* case of an anonymous function or a function with an empty
* name).
*
* - Note in particular that the output does NOT need to compile
* into anything useful.
*/
/* XXX: faster internal way to get this */
duk_push_this(ctx);
tv = duk_get_tval(ctx, -1);
DUK_ASSERT(tv != NULL);
if (DUK_TVAL_IS_OBJECT(tv)) {
duk_hobject *obj = DUK_TVAL_GET_OBJECT(tv);
const char *func_name = DUK_STR_ANON;
/* XXX: rework, it would be nice to avoid C formatting functions to
* ensure there are no Unicode issues.
*/
duk_get_prop_stridx(ctx, -1, DUK_STRIDX_NAME);
if (!duk_is_undefined(ctx, -1)) {
func_name = duk_to_string(ctx, -1);
DUK_ASSERT(func_name != NULL);
if (func_name[0] == (char) 0) {
func_name = DUK_STR_ANON;
}
}
if (DUK_HOBJECT_HAS_COMPILEDFUNCTION(obj)) {
/* XXX: actual source, if available */
duk_push_sprintf(ctx, "function %s() {/* source code */}", (const char *) func_name);
} else if (DUK_HOBJECT_HAS_NATIVEFUNCTION(obj)) {
duk_push_sprintf(ctx, "function %s() {/* native code */}", (const char *) func_name);
} else if (DUK_HOBJECT_HAS_BOUND(obj)) {
duk_push_sprintf(ctx, "function %s() {/* bound */}", (const char *) func_name);
} else {
goto type_error;
}
} else {
goto type_error;
}
return 1;
type_error:
return DUK_RET_TYPE_ERROR;
}
DUK_INTERNAL duk_ret_t duk_bi_function_prototype_apply(duk_context *ctx) {
duk_idx_t len;
duk_idx_t i;
DUK_ASSERT_TOP(ctx, 2); /* not a vararg function */
duk_push_this(ctx);
if (!duk_is_callable(ctx, -1)) {
DUK_DDD(DUK_DDDPRINT("func is not callable"));
goto type_error;
}
duk_insert(ctx, 0);
DUK_ASSERT_TOP(ctx, 3);
DUK_DDD(DUK_DDDPRINT("func=%!iT, thisArg=%!iT, argArray=%!iT",
(duk_tval *) duk_get_tval(ctx, 0),
(duk_tval *) duk_get_tval(ctx, 1),
(duk_tval *) duk_get_tval(ctx, 2)));
/* [ func thisArg argArray ] */
if (duk_is_null_or_undefined(ctx, 2)) {
DUK_DDD(DUK_DDDPRINT("argArray is null/undefined, no args"));
len = 0;
} else if (!duk_is_object(ctx, 2)) {
goto type_error;
} else {
DUK_DDD(DUK_DDDPRINT("argArray is an object"));
/* XXX: make this an internal helper */
duk_get_prop_stridx(ctx, 2, DUK_STRIDX_LENGTH);
len = (duk_idx_t) duk_to_uint32(ctx, -1); /* ToUint32() coercion required */
duk_pop(ctx);
duk_require_stack(ctx, len);
DUK_DDD(DUK_DDDPRINT("argArray length is %ld", (long) len));
for (i = 0; i < len; i++) {
duk_get_prop_index(ctx, 2, i);
}
}
duk_remove(ctx, 2);
DUK_ASSERT_TOP(ctx, 2 + len);
/* [ func thisArg arg1 ... argN ] */
DUK_DDD(DUK_DDDPRINT("apply, func=%!iT, thisArg=%!iT, len=%ld",
(duk_tval *) duk_get_tval(ctx, 0),
(duk_tval *) duk_get_tval(ctx, 1),
(long) len));
duk_call_method(ctx, len);
return 1;
type_error:
return DUK_RET_TYPE_ERROR;
}
DUK_INTERNAL duk_ret_t duk_bi_function_prototype_call(duk_context *ctx) {
duk_idx_t nargs;
/* Step 1 is not necessary because duk_call_method() will take
* care of it.
*/
/* vararg function, thisArg needs special handling */
nargs = duk_get_top(ctx); /* = 1 + arg count */
if (nargs == 0) {
duk_push_undefined(ctx);
nargs++;
}
DUK_ASSERT(nargs >= 1);
/* [ thisArg arg1 ... argN ] */
duk_push_this(ctx); /* 'func' in the algorithm */
duk_insert(ctx, 0);
/* [ func thisArg arg1 ... argN ] */
DUK_DDD(DUK_DDDPRINT("func=%!iT, thisArg=%!iT, argcount=%ld, top=%ld",
(duk_tval *) duk_get_tval(ctx, 0),
(duk_tval *) duk_get_tval(ctx, 1),
(long) (nargs - 1),
(long) duk_get_top(ctx)));
duk_call_method(ctx, nargs - 1);
return 1;
}
/* XXX: the implementation now assumes "chained" bound functions,
* whereas "collapsed" bound functions (where there is ever only
* one bound function which directly points to a non-bound, final
* function) would require a "collapsing" implementation which
* merges argument lists etc here.
*/
DUK_INTERNAL duk_ret_t duk_bi_function_prototype_bind(duk_context *ctx) {
duk_hobject *h_bound;
duk_hobject *h_target;
duk_idx_t nargs;
duk_idx_t i;
/* vararg function, careful arg handling (e.g. thisArg may not be present) */
nargs = duk_get_top(ctx); /* = 1 + arg count */
if (nargs == 0) {
duk_push_undefined(ctx);
nargs++;
}
DUK_ASSERT(nargs >= 1);
duk_push_this(ctx);
if (!duk_is_callable(ctx, -1)) {
DUK_DDD(DUK_DDDPRINT("func is not callable"));
goto type_error;
}
/* [ thisArg arg1 ... argN func ] (thisArg+args == nargs total) */
DUK_ASSERT_TOP(ctx, nargs + 1);
/* create bound function object */
duk_push_object_helper(ctx,
DUK_HOBJECT_FLAG_EXTENSIBLE |
DUK_HOBJECT_FLAG_BOUND |
DUK_HOBJECT_FLAG_CONSTRUCTABLE |
DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_FUNCTION),
DUK_BIDX_FUNCTION_PROTOTYPE);
h_bound = duk_get_hobject(ctx, -1);
DUK_ASSERT(h_bound != NULL);
/* [ thisArg arg1 ... argN func boundFunc ] */
duk_dup(ctx, -2); /* func */
duk_def_prop_stridx(ctx, -2, DUK_STRIDX_INT_TARGET, DUK_PROPDESC_FLAGS_NONE);
duk_dup(ctx, 0); /* thisArg */
duk_def_prop_stridx(ctx, -2, DUK_STRIDX_INT_THIS, DUK_PROPDESC_FLAGS_NONE);
duk_push_array(ctx);
/* [ thisArg arg1 ... argN func boundFunc argArray ] */
for (i = 0; i < nargs - 1; i++) {
duk_dup(ctx, 1 + i);
duk_put_prop_index(ctx, -2, i);
}
duk_def_prop_stridx(ctx, -2, DUK_STRIDX_INT_ARGS, DUK_PROPDESC_FLAGS_NONE);
/* [ thisArg arg1 ... argN func boundFunc ] */
/* bound function 'length' property is interesting */
h_target = duk_get_hobject(ctx, -2);
DUK_ASSERT(h_target != NULL);
if (DUK_HOBJECT_GET_CLASS_NUMBER(h_target) == DUK_HOBJECT_CLASS_FUNCTION) {
duk_int_t tmp;
duk_get_prop_stridx(ctx, -2, DUK_STRIDX_LENGTH);
tmp = duk_to_int(ctx, -1) - (nargs - 1); /* step 15.a */
duk_pop(ctx);
duk_push_int(ctx, (tmp < 0 ? 0 : tmp));
} else {
duk_push_int(ctx, 0);
}
duk_def_prop_stridx(ctx, -2, DUK_STRIDX_LENGTH, DUK_PROPDESC_FLAGS_NONE); /* attrs in E5 Section 15.3.5.1 */
/* caller and arguments must use the same thrower, [[ThrowTypeError]] */
duk_def_prop_stridx_thrower(ctx, -1, DUK_STRIDX_CALLER, DUK_PROPDESC_FLAGS_NONE);
duk_def_prop_stridx_thrower(ctx, -1, DUK_STRIDX_LC_ARGUMENTS, DUK_PROPDESC_FLAGS_NONE);
/* these non-standard properties are copied for convenience */
/* XXX: 'copy properties' API call? */
duk_get_prop_stridx(ctx, -2, DUK_STRIDX_NAME);
duk_def_prop_stridx(ctx, -2, DUK_STRIDX_NAME, DUK_PROPDESC_FLAGS_WC);
duk_get_prop_stridx(ctx, -2, DUK_STRIDX_FILE_NAME);
duk_def_prop_stridx(ctx, -2, DUK_STRIDX_FILE_NAME, DUK_PROPDESC_FLAGS_WC);
/* The 'strict' flag is copied to get the special [[Get]] of E5.1
* Section 15.3.5.4 to apply when a 'caller' value is a strict bound
* function. Not sure if this is correct, because the specification
* is a bit ambiguous on this point but it would make sense.
*/
if (DUK_HOBJECT_HAS_STRICT(h_target)) {
DUK_HOBJECT_SET_STRICT(h_bound);
}
DUK_DDD(DUK_DDDPRINT("created bound function: %!iT", (duk_tval *) duk_get_tval(ctx, -1)));
return 1;
type_error:
return DUK_RET_TYPE_ERROR;
}