forked from svaarala/duktape
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathduk_api_var.c
86 lines (63 loc) · 2.22 KB
/
duk_api_var.c
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
/*
* Variable access
*/
#include "duk_internal.h"
DUK_EXTERNAL void duk_get_var(duk_context *ctx) {
duk_hthread *thr = (duk_hthread *) ctx;
duk_activation *act;
duk_hstring *h_varname;
duk_small_int_t throw_flag = 1; /* always throw ReferenceError for unresolvable */
DUK_ASSERT_CTX_VALID(ctx);
h_varname = duk_require_hstring(ctx, -1); /* XXX: tostring? */
DUK_ASSERT(h_varname != NULL);
act = duk_hthread_get_current_activation(thr);
if (act) {
(void) duk_js_getvar_activation(thr, act, h_varname, throw_flag); /* -> [ ... varname val this ] */
} else {
/* Outside any activation -> look up from global. */
DUK_ASSERT(thr->builtins[DUK_BIDX_GLOBAL_ENV] != NULL);
(void) duk_js_getvar_envrec(thr, thr->builtins[DUK_BIDX_GLOBAL_ENV], h_varname, throw_flag);
}
/* [ ... varname val this ] (because throw_flag == 1, always resolved) */
duk_pop(ctx);
duk_remove(ctx, -2);
/* [ ... val ] */
/* Return value would be pointless: because throw_flag==1, we always
* throw if the identifier doesn't resolve.
*/
return;
}
DUK_EXTERNAL void duk_put_var(duk_context *ctx) {
duk_hthread *thr = (duk_hthread *) ctx;
duk_activation *act;
duk_hstring *h_varname;
duk_tval *tv_val;
duk_small_int_t throw_flag;
DUK_ASSERT_CTX_VALID(ctx);
h_varname = duk_require_hstring(ctx, -2); /* XXX: tostring? */
DUK_ASSERT(h_varname != NULL);
tv_val = duk_require_tval(ctx, -1);
throw_flag = duk_is_strict_call(ctx);
act = duk_hthread_get_current_activation(thr);
if (act) {
duk_js_putvar_activation(thr, act, h_varname, tv_val, throw_flag); /* -> [ ... varname val this ] */
} else {
/* Outside any activation -> put to global. */
DUK_ASSERT(thr->builtins[DUK_BIDX_GLOBAL_ENV] != NULL);
duk_js_putvar_envrec(thr, thr->builtins[DUK_BIDX_GLOBAL_ENV], h_varname, tv_val, throw_flag);
}
/* [ ... varname val ] */
duk_pop_2(ctx);
/* [ ... ] */
return;
}
DUK_EXTERNAL duk_bool_t duk_del_var(duk_context *ctx) {
DUK_ASSERT_CTX_VALID(ctx);
DUK_ERROR((duk_hthread *) ctx, DUK_ERR_UNIMPLEMENTED_ERROR, DUK_STR_UNIMPLEMENTED);
return 0;
}
DUK_EXTERNAL duk_bool_t duk_has_var(duk_context *ctx) {
DUK_ASSERT_CTX_VALID(ctx);
DUK_ERROR((duk_hthread *) ctx, DUK_ERR_UNIMPLEMENTED_ERROR, DUK_STR_UNIMPLEMENTED);
return 0;
}