forked from svaarala/duktape
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-c-constructor.c
More file actions
33 lines (27 loc) · 1 KB
/
test-c-constructor.c
File metadata and controls
33 lines (27 loc) · 1 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
/*===
*** test_1 (duk_safe_call)
inherited value
top at end: 0
==> rc=0, result='undefined'
===*/
static duk_ret_t my_constructor(duk_context *ctx) {
return 0;
}
static duk_ret_t test_1(duk_context *ctx, void *udata) {
(void) udata;
duk_push_global_object(ctx);
duk_push_c_function(ctx, my_constructor, 0); /* constructor (function) */
duk_push_object(ctx); /* prototype object -> [ global cons proto ] */
duk_push_string(ctx, "inherited value");
duk_put_prop_string(ctx, -2, "inherited"); /* set proto.inherited = "inherited value" */
duk_put_prop_string(ctx, -2, "prototype"); /* set cons.prototype = proto; stack -> [ global cons ] */
duk_put_prop_string(ctx, -2, "MyConstructor"); /* set global.MyConstructor = cons; stack -> [ global ] */
duk_pop(ctx);
duk_eval_string(ctx, "var obj = new MyConstructor(); print(obj.inherited);");
duk_pop(ctx);
printf("top at end: %ld\n", (long) duk_get_top(ctx));
return 0;
}
void test(duk_context *ctx) {
TEST_SAFE_CALL(test_1);
}