forked from svaarala/duktape
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-compact.c
More file actions
30 lines (26 loc) · 996 Bytes
/
test-compact.c
File metadata and controls
30 lines (26 loc) · 996 Bytes
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
/*===
{"meaningOfLife":42}
{"meaningOfLife":42,"foo":"bar"}
{"meaningOfLife":42,"foo":"bar"}
===*/
void test(duk_context *ctx) {
duk_push_object(ctx); /* [ ... obj ] */
duk_push_int(ctx, 42); /* [ ... obj 42 ] */
duk_put_prop_string(ctx, -2, "meaningOfLife"); /* [ ... obj ] */
/* compaction has no external impact */
duk_compact(ctx, -1); /* [ ... obj ] */
duk_dup_top(ctx);
printf("%s\n", duk_json_encode(ctx, -1));
duk_pop(ctx);
/* compaction doesn't prevent new properties from being added */
duk_push_string(ctx, "bar"); /* [ ... obj "bar" ] */
duk_put_prop_string(ctx, -2, "foo"); /* [ ... obj ] */
duk_dup_top(ctx);
printf("%s\n", duk_json_encode(ctx, -1));
duk_pop(ctx);
/* compaction can be done multiple times */
duk_compact(ctx, -1); /* [ ... obj ] */
duk_dup_top(ctx);
printf("%s\n", duk_json_encode(ctx, -1));
duk_pop(ctx);
}