summaryrefslogtreecommitdiffhomepage
path: root/src/nxt_tstr.c
diff options
context:
space:
mode:
authorZhidao HONG <z.hong@f5.com>2022-11-20 23:16:51 +0800
committerZhidao HONG <z.hong@f5.com>2022-11-20 23:16:51 +0800
commit4d6d146e920667a8afeacd355e4fb6a94387066e (patch)
treed25e70f93a4f91960434c4d631072e9936f80e36 /src/nxt_tstr.c
parent4735931ace321752c387dae04c8b217ef22897ee (diff)
downloadunit-4d6d146e920667a8afeacd355e4fb6a94387066e.tar.gz
unit-4d6d146e920667a8afeacd355e4fb6a94387066e.tar.bz2
Basic njs support.
Diffstat (limited to 'src/nxt_tstr.c')
-rw-r--r--src/nxt_tstr.c128
1 files changed, 111 insertions, 17 deletions
diff --git a/src/nxt_tstr.c b/src/nxt_tstr.c
index dff61952..fd01797c 100644
--- a/src/nxt_tstr.c
+++ b/src/nxt_tstr.c
@@ -9,12 +9,22 @@
typedef enum {
NXT_TSTR_CONST = 0,
NXT_TSTR_VAR,
+#if (NXT_HAVE_NJS)
+ NXT_TSTR_JS,
+#endif
} nxt_tstr_type_t;
struct nxt_tstr_s {
nxt_str_t str;
- nxt_var_t *var;
+
+ union {
+ nxt_var_t *var;
+#if (NXT_HAVE_NJS)
+ nxt_js_t *js;
+#endif
+ } u;
+
nxt_tstr_flags_t flags;
nxt_tstr_type_t type;
};
@@ -24,7 +34,7 @@ struct nxt_tstr_query_s {
nxt_mp_t *pool;
nxt_tstr_state_t *state;
- nxt_var_cache_t *cache;
+ nxt_tstr_cache_t *cache;
nxt_uint_t waiting;
nxt_uint_t failed; /* 1 bit */
@@ -37,8 +47,12 @@ struct nxt_tstr_query_s {
};
+#define nxt_tstr_is_js(str) \
+ nxt_strchr_start(str, '`')
+
+
nxt_tstr_state_t *
-nxt_tstr_state_new(nxt_mp_t *mp)
+nxt_tstr_state_new(nxt_mp_t *mp, nxt_bool_t test)
{
nxt_tstr_state_t *state;
@@ -48,12 +62,20 @@ nxt_tstr_state_new(nxt_mp_t *mp)
}
state->pool = mp;
+ state->test = test;
state->var_fields = nxt_array_create(mp, 4, sizeof(nxt_var_field_t));
if (nxt_slow_path(state->var_fields == NULL)) {
return NULL;
}
+#if (NXT_HAVE_NJS)
+ state->jcf = nxt_js_conf_new(mp);
+ if (nxt_slow_path(state->jcf == NULL)) {
+ return NULL;
+ }
+#endif
+
return state;
}
@@ -88,18 +110,38 @@ nxt_tstr_compile(nxt_tstr_state_t *state, nxt_str_t *str,
tstr->flags = flags;
- p = nxt_memchr(str->start, '$', str->length);
+ if (nxt_tstr_is_js(str)) {
+
+#if (NXT_HAVE_NJS)
+
+ nxt_str_t tpl;
- if (p != NULL) {
- tstr->type = NXT_TSTR_VAR;
+ tstr->type = NXT_TSTR_JS;
- tstr->var = nxt_var_compile(&tstr->str, state->pool, state->var_fields);
- if (nxt_slow_path(tstr->var == NULL)) {
+ nxt_tstr_str(tstr, &tpl);
+
+ tstr->u.js = nxt_js_add_tpl(state->jcf, &tpl, strz);
+ if (nxt_slow_path(tstr->u.js == NULL)) {
return NULL;
}
+#endif
+
} else {
- tstr->type = NXT_TSTR_CONST;
+ p = memchr(str->start, '$', str->length);
+
+ if (p != NULL) {
+ tstr->type = NXT_TSTR_VAR;
+
+ tstr->u.var = nxt_var_compile(&tstr->str, state->pool,
+ state->var_fields);
+ if (nxt_slow_path(tstr->u.var == NULL)) {
+ return NULL;
+ }
+
+ } else {
+ tstr->type = NXT_TSTR_CONST;
+ }
}
return tstr;
@@ -109,7 +151,46 @@ nxt_tstr_compile(nxt_tstr_state_t *state, nxt_str_t *str,
nxt_int_t
nxt_tstr_test(nxt_tstr_state_t *state, nxt_str_t *str, u_char *error)
{
- return nxt_var_test(str, state->var_fields, error);
+ u_char *p;
+
+ if (nxt_tstr_is_js(str)) {
+#if (NXT_HAVE_NJS)
+ return nxt_js_test(state->jcf, str, error);
+
+#else
+ nxt_sprintf(error, error + NXT_MAX_ERROR_STR,
+ "Unit is built without support of njs: "
+ "\"--njs\" ./configure option is missing.");
+ return NXT_ERROR;
+#endif
+
+ } else {
+ p = memchr(str->start, '$', str->length);
+
+ if (p != NULL) {
+ return nxt_var_test(str, state->var_fields, error);
+ }
+ }
+
+ return NXT_OK;
+}
+
+
+nxt_int_t
+nxt_tstr_state_done(nxt_tstr_state_t *state, u_char *error)
+{
+#if (NXT_HAVE_NJS)
+ if (!state->test) {
+ nxt_int_t ret;
+
+ ret = nxt_js_compile(state->jcf);
+ if (nxt_slow_path(ret != NXT_OK)) {
+ return NXT_ERROR;
+ }
+ }
+#endif
+
+ return NXT_OK;
}
@@ -133,7 +214,7 @@ nxt_tstr_str(nxt_tstr_t *tstr, nxt_str_t *str)
nxt_int_t
nxt_tstr_query_init(nxt_tstr_query_t **query_p, nxt_tstr_state_t *state,
- nxt_var_cache_t *cache, void *ctx, nxt_mp_t *mp)
+ nxt_tstr_cache_t *cache, void *ctx, nxt_mp_t *mp)
{
nxt_tstr_query_t *query;
@@ -172,11 +253,24 @@ nxt_tstr_query(nxt_task_t *task, nxt_tstr_query_t *query, nxt_tstr_t *tstr,
return;
}
- ret = nxt_var_interpreter(task, query->cache, tstr->var, val, query->ctx,
- tstr->flags & NXT_TSTR_LOGGING);
- if (nxt_slow_path(ret != NXT_OK)) {
- query->failed = 1;
- return;
+ if (tstr->type == NXT_TSTR_VAR) {
+ ret = nxt_var_interpreter(task, &query->cache->var, tstr->u.var, val,
+ query->ctx, tstr->flags & NXT_TSTR_LOGGING);
+
+ if (nxt_slow_path(ret != NXT_OK)) {
+ query->failed = 1;
+ return;
+ }
+
+ } else {
+#if (NXT_HAVE_NJS)
+ ret = nxt_js_call(task, &query->cache->js, tstr->u.js, val, query->ctx);
+
+ if (nxt_slow_path(ret != NXT_OK)) {
+ query->failed = 1;
+ return;
+ }
+#endif
}
if (tstr->flags & NXT_TSTR_STRZ) {
@@ -188,7 +282,7 @@ nxt_tstr_query(nxt_task_t *task, nxt_tstr_query_t *query, nxt_tstr_t *tstr,
nxt_tstr_str(tstr, &str);
- nxt_debug(task, "tstr: \"%V\" -> \"%V\"", &str, val);
+ nxt_debug(task, "tstr query: \"%V\", result: \"%V\"", &str, val);
#endif
}