forked from svaarala/duktape
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-dev-lightfunc-bound.c
More file actions
99 lines (86 loc) · 2.44 KB
/
test-dev-lightfunc-bound.c
File metadata and controls
99 lines (86 loc) · 2.44 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
/*
* Lightfunc cannot be bound, but can appear in a bound function chain as
* the final non-bound function. This may happen for both constuctor and
* non-constructor call.
*/
/*===
*** test_normal_call (duk_safe_call)
lightfunc called, constructor call: 0
argument 1: 123
argument 2: 234
return value: dummy
lightfunc called, constructor call: 0
argument 1: 1001
argument 2: 2002
return value: dummy
final top: 1
==> rc=0, result='undefined'
*** test_constructor_call (duk_safe_call)
lightfunc called, constructor call: 1
argument 1: 123
argument 2: 234
return value: [object Object]
lightfunc called, constructor call: 1
argument 1: 1001
argument 2: 2002
return value: [object Object]
final top: 1
==> rc=0, result='undefined'
===*/
static duk_ret_t my_lightfunc(duk_context *ctx) {
printf("lightfunc called, constructor call: %d\n", (int) duk_is_constructor_call(ctx));
printf("argument 1: %s\n", duk_to_string(ctx, 0));
printf("argument 2: %s\n", duk_to_string(ctx, 1));
duk_push_string(ctx, "dummy");
return 1;
}
static duk_ret_t test_normal_call(duk_context *ctx, void *udata) {
(void) udata;
duk_push_c_lightfunc(ctx, my_lightfunc, 2, 0, 0);
/* Call directly. */
duk_dup(ctx, -1);
duk_push_uint(ctx, 123);
duk_push_uint(ctx, 234);
duk_call(ctx, 2);
printf("return value: %s\n", duk_safe_to_string(ctx, -1));
duk_pop(ctx);
/* Call via bound function. */
duk_eval_string(ctx,
"(function (v) {\n"
" return v.bind(null, 1001, 2002);\n"
"})");
duk_dup(ctx, -2);
duk_call(ctx, 1); /* -> [ lfunc bound ] */
duk_call(ctx, 0);
printf("return value: %s\n", duk_safe_to_string(ctx, -1));
duk_pop(ctx);
printf("final top: %ld\n", (long) duk_get_top(ctx));
return 0;
}
static duk_ret_t test_constructor_call(duk_context *ctx, void *udata) {
(void) udata;
duk_push_c_lightfunc(ctx, my_lightfunc, 2, 0, 0);
/* Call directly. */
duk_dup(ctx, -1);
duk_push_uint(ctx, 123);
duk_push_uint(ctx, 234);
duk_new(ctx, 2);
printf("return value: %s\n", duk_safe_to_string(ctx, -1));
duk_pop(ctx);
/* Call via bound function. */
duk_eval_string(ctx,
"(function (v) {\n"
" return v.bind(null, 1001, 2002);\n"
"})");
duk_dup(ctx, -2);
duk_call(ctx, 1); /* -> [ lfunc bound ] */
duk_new(ctx, 0);
printf("return value: %s\n", duk_safe_to_string(ctx, -1));
duk_pop(ctx);
printf("final top: %ld\n", (long) duk_get_top(ctx));
return 0;
}
void test(duk_context *ctx) {
TEST_SAFE_CALL(test_normal_call);
TEST_SAFE_CALL(test_constructor_call);
}