Deleted
Added
nxt_js.c (2247:baa6b9879267) | nxt_js.c (2248:67f848571b9f) |
---|---|
1 2/* 3 * Copyright (C) NGINX, Inc. 4 */ 5 6#include <nxt_main.h> 7 8 9struct nxt_js_s { 10 uint32_t index; 11 njs_vm_t *vm; 12}; 13 14 15struct nxt_js_conf_s { 16 nxt_mp_t *pool; 17 njs_vm_t *vm; | 1 2/* 3 * Copyright (C) NGINX, Inc. 4 */ 5 6#include <nxt_main.h> 7 8 9struct nxt_js_s { 10 uint32_t index; 11 njs_vm_t *vm; 12}; 13 14 15struct nxt_js_conf_s { 16 nxt_mp_t *pool; 17 njs_vm_t *vm; |
18 njs_uint_t protos; 19 njs_external_t *proto; |
|
18 nxt_array_t *funcs; 19}; 20 21 | 20 nxt_array_t *funcs; 21}; 22 23 |
24njs_int_t nxt_js_proto_id; 25 26 |
|
22nxt_js_conf_t * 23nxt_js_conf_new(nxt_mp_t *mp) 24{ 25 njs_vm_opt_t opts; 26 nxt_js_conf_t *jcf; 27 28 jcf = nxt_mp_zget(mp, sizeof(nxt_js_conf_t)); 29 if (nxt_slow_path(jcf == NULL)) { --- 13 unchanged lines hidden (view full) --- 43 if (nxt_slow_path(jcf->funcs == NULL)) { 44 return NULL; 45 } 46 47 return jcf; 48} 49 50 | 27nxt_js_conf_t * 28nxt_js_conf_new(nxt_mp_t *mp) 29{ 30 njs_vm_opt_t opts; 31 nxt_js_conf_t *jcf; 32 33 jcf = nxt_mp_zget(mp, sizeof(nxt_js_conf_t)); 34 if (nxt_slow_path(jcf == NULL)) { --- 13 unchanged lines hidden (view full) --- 48 if (nxt_slow_path(jcf->funcs == NULL)) { 49 return NULL; 50 } 51 52 return jcf; 53} 54 55 |
56void 57nxt_js_set_proto(nxt_js_conf_t *jcf, njs_external_t *proto, njs_uint_t n) 58{ 59 jcf->protos = n; 60 jcf->proto = proto; 61} 62 63 |
|
51nxt_js_t * 52nxt_js_add_tpl(nxt_js_conf_t *jcf, nxt_str_t *str, nxt_bool_t strz) 53{ 54 size_t size; 55 u_char *p, *start; 56 nxt_js_t *js; 57 nxt_str_t *func; 58 | 64nxt_js_t * 65nxt_js_add_tpl(nxt_js_conf_t *jcf, nxt_str_t *str, nxt_bool_t strz) 66{ 67 size_t size; 68 u_char *p, *start; 69 nxt_js_t *js; 70 nxt_str_t *func; 71 |
59 static nxt_str_t func_str = nxt_string("function() {" | 72 static nxt_str_t func_str = nxt_string("function(uri, host, remoteAddr, " 73 "args, headers, cookies) {" |
60 " return "); 61 62 /* 63 * Appending a terminating null character if strz is true. 64 */ 65 static nxt_str_t strz_str = nxt_string(" + '\\x00'"); 66 67 size = func_str.length + str->length + 1; --- 67 unchanged lines hidden (view full) --- 135 136 for (i = 0; i < jcf->funcs->nelts; i++) { 137 p = nxt_cpymem(p, func[i].start, func[i].length); 138 *p++ = ','; 139 } 140 141 *p++ = ']'; 142 | 74 " return "); 75 76 /* 77 * Appending a terminating null character if strz is true. 78 */ 79 static nxt_str_t strz_str = nxt_string(" + '\\x00'"); 80 81 size = func_str.length + str->length + 1; --- 67 unchanged lines hidden (view full) --- 149 150 for (i = 0; i < jcf->funcs->nelts; i++) { 151 p = nxt_cpymem(p, func[i].start, func[i].length); 152 *p++ = ','; 153 } 154 155 *p++ = ']'; 156 |
157 nxt_js_proto_id = njs_vm_external_prototype(jcf->vm, jcf->proto, 158 jcf->protos); 159 if (nxt_slow_path(nxt_js_proto_id < 0)) { 160 return NXT_ERROR; 161 } 162 |
|
143 ret = njs_vm_compile(jcf->vm, &start, p); 144 145 return (ret == NJS_OK) ? NXT_OK : NXT_ERROR; 146} 147 148 149nxt_int_t 150nxt_js_test(nxt_js_conf_t *jcf, nxt_str_t *str, u_char *error) --- 31 unchanged lines hidden (view full) --- 182nxt_js_call(nxt_task_t *task, nxt_js_cache_t *cache, nxt_js_t *js, 183 nxt_str_t *str, void *ctx) 184{ 185 njs_vm_t *vm; 186 njs_int_t rc, ret; 187 njs_str_t res; 188 njs_value_t *array, *value; 189 njs_function_t *func; | 163 ret = njs_vm_compile(jcf->vm, &start, p); 164 165 return (ret == NJS_OK) ? NXT_OK : NXT_ERROR; 166} 167 168 169nxt_int_t 170nxt_js_test(nxt_js_conf_t *jcf, nxt_str_t *str, u_char *error) --- 31 unchanged lines hidden (view full) --- 202nxt_js_call(nxt_task_t *task, nxt_js_cache_t *cache, nxt_js_t *js, 203 nxt_str_t *str, void *ctx) 204{ 205 njs_vm_t *vm; 206 njs_int_t rc, ret; 207 njs_str_t res; 208 njs_value_t *array, *value; 209 njs_function_t *func; |
190 njs_opaque_value_t opaque_value; | 210 njs_opaque_value_t opaque_value, arguments[6]; |
191 | 211 |
212 static const njs_str_t uri_str = njs_str("uri"); 213 static const njs_str_t host_str = njs_str("host"); 214 static const njs_str_t remote_addr_str = njs_str("remoteAddr"); 215 static const njs_str_t args_str = njs_str("args"); 216 static const njs_str_t headers_str = njs_str("headers"); 217 static const njs_str_t cookies_str = njs_str("cookies"); 218 |
|
192 vm = cache->vm; 193 194 if (vm == NULL) { 195 vm = njs_vm_clone(js->vm, ctx); 196 if (nxt_slow_path(vm == NULL)) { 197 return NXT_ERROR; 198 } 199 --- 6 unchanged lines hidden (view full) --- 206 207 cache->vm = vm; 208 cache->array = *array; 209 } 210 211 value = njs_vm_array_prop(vm, &cache->array, js->index, &opaque_value); 212 func = njs_value_function(value); 213 | 219 vm = cache->vm; 220 221 if (vm == NULL) { 222 vm = njs_vm_clone(js->vm, ctx); 223 if (nxt_slow_path(vm == NULL)) { 224 return NXT_ERROR; 225 } 226 --- 6 unchanged lines hidden (view full) --- 233 234 cache->vm = vm; 235 cache->array = *array; 236 } 237 238 value = njs_vm_array_prop(vm, &cache->array, js->index, &opaque_value); 239 func = njs_value_function(value); 240 |
214 ret = njs_vm_call(vm, func, NULL, 0); | 241 ret = njs_vm_external_create(vm, njs_value_arg(&opaque_value), 242 nxt_js_proto_id, ctx, 0); 243 if (nxt_slow_path(ret != NJS_OK)) { 244 return NXT_ERROR; 245 } |
215 | 246 |
247 value = njs_vm_object_prop(vm, njs_value_arg(&opaque_value), &uri_str, 248 &arguments[0]); 249 if (nxt_slow_path(value == NULL)) { 250 return NXT_ERROR; 251 } 252 253 value = njs_vm_object_prop(vm, njs_value_arg(&opaque_value), &host_str, 254 &arguments[1]); 255 if (nxt_slow_path(value == NULL)) { 256 return NXT_ERROR; 257 } 258 259 value = njs_vm_object_prop(vm, njs_value_arg(&opaque_value), 260 &remote_addr_str, &arguments[2]); 261 if (nxt_slow_path(value == NULL)) { 262 return NXT_ERROR; 263 } 264 265 value = njs_vm_object_prop(vm, njs_value_arg(&opaque_value), &args_str, 266 &arguments[3]); 267 if (nxt_slow_path(value == NULL)) { 268 return NXT_ERROR; 269 } 270 271 value = njs_vm_object_prop(vm, njs_value_arg(&opaque_value), &headers_str, 272 &arguments[4]); 273 if (nxt_slow_path(value == NULL)) { 274 return NXT_ERROR; 275 } 276 277 value = njs_vm_object_prop(vm, njs_value_arg(&opaque_value), &cookies_str, 278 &arguments[5]); 279 if (nxt_slow_path(value == NULL)) { 280 return NXT_ERROR; 281 } 282 283 ret = njs_vm_call(vm, func, njs_value_arg(&arguments), 6); 284 |
|
216 rc = njs_vm_retval_string(vm, &res); 217 if (rc != NJS_OK) { 218 return NXT_ERROR; 219 } 220 221 if (ret != NJS_OK) { 222 nxt_alert(task, "js exception: %V", &res); 223 return NXT_ERROR; 224 } 225 226 str->length = res.length; 227 str->start = res.start; 228 229 return NXT_OK; 230} | 285 rc = njs_vm_retval_string(vm, &res); 286 if (rc != NJS_OK) { 287 return NXT_ERROR; 288 } 289 290 if (ret != NJS_OK) { 291 nxt_alert(task, "js exception: %V", &res); 292 return NXT_ERROR; 293 } 294 295 str->length = res.length; 296 str->start = res.start; 297 298 return NXT_OK; 299} |