forked from svaarala/duktape
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-dev-global-object-proxy.c
More file actions
53 lines (45 loc) · 1.14 KB
/
test-dev-global-object-proxy.c
File metadata and controls
53 lines (45 loc) · 1.14 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
/*
* Demonstrate replacing global object with a Proxy that returns dummy
* values for non-existent properties.
*/
/*===
*** test_1 (duk_safe_call)
GET Math
[object Math]
GET nonExistent
replaced
HAS print
GET print
HAS Math
GET Math
object
HAS print
GET print
HAS nonExistent
GET nonExistent
string
final top: 0
==> rc=0, result='undefined'
===*/
static duk_ret_t test_1(duk_context *ctx, void *udata) {
(void) udata;
duk_eval_string(ctx,
"new Proxy(this, {\n"
" get: function (targ, key, recv) { print('GET', key); if (key in targ) { return targ[key]; } else { return 'replaced'; } },\n"
" has: function (targ, key) { print('HAS', key); return true; }\n"
"})");
duk_set_global_object(ctx);
duk_get_global_string(ctx, "Math");
printf("%s\n", duk_safe_to_string(ctx, -1));
duk_pop(ctx);
duk_get_global_string(ctx, "nonExistent");
printf("%s\n", duk_safe_to_string(ctx, -1));
duk_pop(ctx);
duk_eval_string_noresult(ctx, "print(typeof Math);");
duk_eval_string_noresult(ctx, "print(typeof nonExistent);");
printf("final top: %ld\n", (long) duk_get_top(ctx));
return 0;
}
void test(duk_context *ctx) {
TEST_SAFE_CALL(test_1);
}