NJS: variable access support

This commit introduces the 'vars' JavaScript object to NJS,
enabling direct access to native variables such as $uri and $arg_foo.
The syntax is `${vars.var_name}` or `${'vars[var_name]'}`.

For example:
    {
        "action": {
            "share": "`/www/html${vars.uri}`"
        }
    }
This commit is contained in:
Zhidao HONG
2024-01-31 14:51:21 +08:00
parent 63ad4deb8a
commit 33c6c4d4c0
3 changed files with 58 additions and 2 deletions

View File

@@ -240,7 +240,7 @@ nxt_js_add_tpl(nxt_js_conf_t *jcf, nxt_str_t *str, nxt_bool_t strz)
nxt_str_t *func;
static nxt_str_t func_str = nxt_string("function(uri, host, remoteAddr, "
"args, headers, cookies) {"
"args, headers, cookies, vars) {"
" return ");
/*
@@ -391,7 +391,7 @@ nxt_js_call(nxt_task_t *task, nxt_js_conf_t *jcf, nxt_js_cache_t *cache,
njs_uint_t i, n;
njs_value_t *value;
njs_function_t *func;
njs_opaque_value_t retval, opaque_value, arguments[6];
njs_opaque_value_t retval, opaque_value, arguments[7];
static const njs_str_t js_args[] = {
njs_str("uri"),
@@ -400,6 +400,7 @@ nxt_js_call(nxt_task_t *task, nxt_js_conf_t *jcf, nxt_js_cache_t *cache,
njs_str("args"),
njs_str("headers"),
njs_str("cookies"),
njs_str("vars"),
};
vm = cache->vm;