static ngx_int_t ngx_http_js_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data) { ngx_str_t *fname = (ngx_str_t *) data; ngx_int_t rc; nxt_str_t name, value, exception; njs_function_t *func; ngx_http_js_ctx_t *ctx; rc = ngx_http_js_init_vm(r); if (rc == NGX_ERROR) { return NGX_ERROR; } if (rc == NGX_DECLINED) { v->not_found = 1; return NGX_OK; } ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "http js variable call \"%V\"", fname); ctx = ngx_http_get_module_ctx(r, ngx_http_js_module); name.start = fname->data; name.length = fname->len; func = njs_vm_function(ctx->vm, &name); if (func == NULL) { ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "js function \"%V\" not found", fname); v->not_found = 1; return NGX_OK; } if (njs_vm_call(ctx->vm, func, ctx->args, 2) != NJS_OK) { njs_vm_exception(ctx->vm, &exception); ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "js exception: %*s", exception.length, exception.start); v->not_found = 1; return NGX_OK; } if (njs_vm_retval(ctx->vm, &value) != NJS_OK) { return NGX_ERROR; } v->len = value.length; v->valid = 1; v->no_cacheable = 0; v->not_found = 0; v->data = value.start; return NGX_OK; }
static ngx_int_t ngx_http_js_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data) { njs_vm_t *vm = (njs_vm_t *) data; nxt_str_t value; njs_vm_t *nvm; ngx_pool_cleanup_t *cln; nxt_mem_cache_pool_t *mcp; ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "http js variable handler"); mcp = ngx_http_js_create_mem_cache_pool(); if (mcp == NULL) { return NGX_ERROR; } cln = ngx_pool_cleanup_add(r->pool, 0); if (cln == NULL) { return NGX_ERROR; } cln->handler = ngx_http_js_cleanup_mem_cache_pool; cln->data = mcp; nvm = njs_vm_clone(vm, mcp, (void **) &r); if (nvm == NULL) { return NGX_ERROR; } if (njs_vm_run(nvm) == NJS_OK) { if (njs_vm_retval(nvm, &value) != NJS_OK) { return NGX_ERROR; } v->len = value.len; v->valid = 1; v->no_cacheable = 0; v->not_found = 0; v->data = value.data; } else { njs_vm_exception(nvm, &value); ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "js exception: %*s", value.len, value.data); v->not_found = 1; } ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "http js variable done"); return NGX_OK; }