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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
/*
* Copyright (C) NGINX, Inc.
*/
#include <nxt_main.h>
struct nxt_js_s {
uint32_t index;
njs_vm_t *vm;
};
struct nxt_js_conf_s {
nxt_mp_t *pool;
njs_vm_t *vm;
nxt_array_t *funcs;
};
nxt_js_conf_t *
nxt_js_conf_new(nxt_mp_t *mp)
{
njs_vm_opt_t opts;
nxt_js_conf_t *jcf;
jcf = nxt_mp_zget(mp, sizeof(nxt_js_conf_t));
if (nxt_slow_path(jcf == NULL)) {
return NULL;
}
jcf->pool = mp;
njs_vm_opt_init(&opts);
jcf->vm = njs_vm_create(&opts);
if (nxt_slow_path(jcf->vm == NULL)) {
return NULL;
}
jcf->funcs = nxt_array_create(mp, 4, sizeof(nxt_str_t));
if (nxt_slow_path(jcf->funcs == NULL)) {
return NULL;
}
return jcf;
}
nxt_js_t *
nxt_js_add_tpl(nxt_js_conf_t *jcf, nxt_str_t *str, nxt_bool_t strz)
{
size_t size;
u_char *p, *start;
nxt_js_t *js;
nxt_str_t *func;
static nxt_str_t func_str = nxt_string("function() {"
" return ");
/*
* Appending a terminating null character if strz is true.
*/
static nxt_str_t strz_str = nxt_string(" + '\\x00'");
size = func_str.length + str->length + 1;
if (strz) {
size += strz_str.length;
}
start = nxt_mp_nget(jcf->pool, size);
if (nxt_slow_path(start == NULL)) {
return NULL;
}
p = start;
p = nxt_cpymem(p, func_str.start, func_str.length);
p = nxt_cpymem(p, str->start, str->length);
if (strz) {
p = nxt_cpymem(p, strz_str.start, strz_str.length);
}
*p++ = '}';
js = nxt_mp_get(jcf->pool, sizeof(nxt_js_t));
if (nxt_slow_path(js == NULL)) {
return NULL;
}
js->vm = jcf->vm;
func = nxt_array_add(jcf->funcs);
if (nxt_slow_path(func == NULL)) {
return NULL;
}
func->start = start;
func->length = p - start;
js->index = jcf->funcs->nelts - 1;
return js;
}
nxt_int_t
nxt_js_compile(nxt_js_conf_t *jcf)
{
size_t size;
u_char *p, *start;
njs_int_t ret;
nxt_str_t *func;
nxt_uint_t i;
size = 2;
func = jcf->funcs->elts;
for (i = 0; i < jcf->funcs->nelts; i++) {
size += func[i].length + 1;
}
start = nxt_mp_nget(jcf->pool, size);
if (nxt_slow_path(start == NULL)) {
return NXT_ERROR;
}
p = start;
*p++ = '[';
func = jcf->funcs->elts;
for (i = 0; i < jcf->funcs->nelts; i++) {
p = nxt_cpymem(p, func[i].start, func[i].length);
*p++ = ',';
}
*p++ = ']';
ret = njs_vm_compile(jcf->vm, &start, p);
return (ret == NJS_OK) ? NXT_OK : NXT_ERROR;
}
nxt_int_t
nxt_js_test(nxt_js_conf_t *jcf, nxt_str_t *str, u_char *error)
{
u_char *start;
nxt_str_t err;
njs_int_t ret;
njs_str_t res;
start = nxt_mp_nget(jcf->pool, str->length);
if (nxt_slow_path(start == NULL)) {
return NXT_ERROR;
}
nxt_memcpy(start, str->start, str->length);
ret = njs_vm_compile(jcf->vm, &start, start + str->length);
if (nxt_slow_path(ret != NJS_OK)) {
(void) njs_vm_retval_string(jcf->vm, &res);
err.start = res.start;
err.length = res.length;
nxt_sprintf(error, error + NXT_MAX_ERROR_STR, "\"%V\"%Z", &err);
return NXT_ERROR;
}
return NXT_OK;
}
nxt_int_t
nxt_js_call(nxt_task_t *task, nxt_js_cache_t *cache, nxt_js_t *js,
nxt_str_t *str, void *ctx)
{
njs_vm_t *vm;
njs_int_t rc, ret;
njs_str_t res;
njs_value_t *array, *value;
njs_function_t *func;
njs_opaque_value_t opaque_value;
vm = cache->vm;
if (vm == NULL) {
vm = njs_vm_clone(js->vm, ctx);
if (nxt_slow_path(vm == NULL)) {
return NXT_ERROR;
}
ret = njs_vm_start(vm);
if (ret != NJS_OK) {
return NXT_ERROR;
}
array = njs_vm_retval(vm);
cache->vm = vm;
cache->array = *array;
}
value = njs_vm_array_prop(vm, &cache->array, js->index, &opaque_value);
func = njs_value_function(value);
ret = njs_vm_call(vm, func, NULL, 0);
rc = njs_vm_retval_string(vm, &res);
if (rc != NJS_OK) {
return NXT_ERROR;
}
if (ret != NJS_OK) {
nxt_alert(task, "js exception: %V", &res);
return NXT_ERROR;
}
str->length = res.length;
str->start = res.start;
return NXT_OK;
}
|