Skip to content

Commit

Permalink
Using native QuickJS way to declare symbol props.
Browse files Browse the repository at this point in the history
  • Loading branch information
xeioex committed Sep 14, 2024
1 parent 27f70ec commit 1e04d34
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 14 deletions.
31 changes: 24 additions & 7 deletions nginx/ngx_http_js_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,8 @@ static njs_int_t ngx_http_js_server(njs_vm_t *vm, ngx_http_request_t *r,
njs_value_t *retval);

#if (NJS_HAVE_QUICKJS)
static JSValue ngx_http_qjs_ext_to_string_tag(JSContext *cx,
JSValueConst this_val);
static JSValue ngx_http_qjs_ext_args(JSContext *cx, JSValueConst this_val);
static JSValue ngx_http_qjs_ext_done(JSContext *cx, JSValueConst this_val,
int argc, JSValueConst *argv);
Expand All @@ -292,6 +294,8 @@ static JSValue ngx_http_qjs_ext_internal_redirect(JSContext *cx,
JSValueConst this_val, int argc, JSValueConst *argv);
static JSValue ngx_http_qjs_ext_log(JSContext *cx, JSValueConst this_val,
int argc, JSValueConst *argv, int level);
static JSValue ngx_http_qjs_ext_periodic_to_string_tag(JSContext *cx,
JSValueConst this_val);
static JSValue ngx_http_qjs_ext_periodic_variables(JSContext *cx,
JSValueConst this_val, int type);
static JSValue ngx_http_qjs_ext_parent(JSContext *cx, JSValueConst this_val);
Expand Down Expand Up @@ -1030,6 +1034,8 @@ static ngx_http_js_entry_t ngx_http_methods[] = {
#if (NJS_HAVE_QUICKJS)

static const JSCFunctionListEntry ngx_http_qjs_ext_request[] = {
JS_CGETSET_DEF("[Symbol.toStringTag]", ngx_http_qjs_ext_to_string_tag,
NULL),
JS_CGETSET_DEF("args", ngx_http_qjs_ext_args, NULL),
JS_CFUNC_DEF("done", 0, ngx_http_qjs_ext_done),
JS_CFUNC_MAGIC_DEF("error", 1, ngx_http_qjs_ext_log, NGX_LOG_ERR),
Expand Down Expand Up @@ -1074,6 +1080,8 @@ static const JSCFunctionListEntry ngx_http_qjs_ext_request[] = {


static const JSCFunctionListEntry ngx_http_qjs_ext_periodic[] = {
JS_CGETSET_DEF("[Symbol.toStringTag]",
ngx_http_qjs_ext_periodic_to_string_tag, NULL),
JS_CGETSET_MAGIC_DEF("rawVariables", ngx_http_qjs_ext_periodic_variables,
NULL, NGX_JS_BUFFER),
JS_CGETSET_MAGIC_DEF("variables", ngx_http_qjs_ext_periodic_variables,
Expand Down Expand Up @@ -4757,6 +4765,14 @@ ngx_http_qjs_query_string_decode(njs_chb_t *chain, const u_char *start,
}


static JSValue
ngx_http_qjs_ext_to_string_tag(JSContext *cx,
JSValueConst this_val)
{
return JS_NewString(cx, "Request");
}


static JSValue
ngx_http_qjs_ext_args(JSContext *cx, JSValueConst this_val)
{
Expand Down Expand Up @@ -5105,6 +5121,14 @@ ngx_http_qjs_ext_log(JSContext *cx, JSValueConst this_val, int argc,
}


static JSValue
ngx_http_qjs_ext_periodic_to_string_tag(JSContext *cx,
JSValueConst this_val)
{
return JS_NewString(cx, "PeriodicSession");
}


static JSValue
ngx_http_qjs_ext_periodic_variables(JSContext *cx,
JSValueConst this_val, int type)
Expand Down Expand Up @@ -7378,7 +7402,6 @@ static JSValue
ngx_http_qjs_request_make(JSContext *cx, ngx_int_t proto_id,
ngx_http_request_t *r)
{
int ret;
JSValue request;
ngx_http_qjs_request_t *req;

Expand All @@ -7387,12 +7410,6 @@ ngx_http_qjs_request_make(JSContext *cx, ngx_int_t proto_id,
return JS_EXCEPTION;
}

ret = qjs_set_to_string_tag(cx, request, "Request");
if (ret == -1) {
JS_FreeValue(cx, request);
return JS_EXCEPTION;
}

req = js_malloc(cx, sizeof(ngx_http_qjs_request_t));
if (req == NULL) {
return JS_ThrowOutOfMemory(cx);
Expand Down
30 changes: 23 additions & 7 deletions nginx/ngx_stream_js_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ static njs_int_t ngx_stream_js_periodic_variables(njs_vm_t *vm,

#if (NJS_HAVE_QUICKJS)

static JSValue ngx_stream_qjs_ext_to_string_tag(JSContext *cx,
JSValueConst this_val);
static JSValue ngx_stream_qjs_ext_done(JSContext *cx, JSValueConst this_val,
int argc, JSValueConst *argv, int magic);
static JSValue ngx_stream_qjs_ext_log(JSContext *cx, JSValueConst this_val,
Expand All @@ -153,6 +155,8 @@ static JSValue ngx_stream_qjs_ext_on(JSContext *cx, JSValueConst this_val,
int argc, JSValueConst *argv);
static JSValue ngx_stream_qjs_ext_off(JSContext *cx, JSValueConst this_val,
int argc, JSValueConst *argv);
static JSValue ngx_stream_qjs_ext_periodic_to_string_tag(JSContext *cx,
JSValueConst this_val);
static JSValue ngx_stream_qjs_ext_periodic_variables(JSContext *cx,
JSValueConst this_val, int type);
static JSValue ngx_stream_qjs_ext_remote_address(JSContext *cx,
Expand Down Expand Up @@ -757,6 +761,8 @@ njs_module_t *njs_stream_js_addon_modules[] = {
#if (NJS_HAVE_QUICKJS)

static const JSCFunctionListEntry ngx_stream_qjs_ext_session[] = {
JS_CGETSET_DEF("[Symbol.toStringTag]", ngx_stream_qjs_ext_to_string_tag,
NULL),
JS_CFUNC_MAGIC_DEF("allow", 1, ngx_stream_qjs_ext_done, NGX_OK),
JS_CFUNC_MAGIC_DEF("decline", 1, ngx_stream_qjs_ext_done, -NGX_DECLINED),
JS_CFUNC_MAGIC_DEF("deny", 1, ngx_stream_qjs_ext_done, -NGX_DONE),
Expand All @@ -783,6 +789,8 @@ static const JSCFunctionListEntry ngx_stream_qjs_ext_session[] = {


static const JSCFunctionListEntry ngx_stream_qjs_ext_periodic[] = {
JS_CGETSET_DEF("[Symbol.toStringTag]",
ngx_stream_qjs_ext_periodic_to_string_tag, NULL),
JS_CGETSET_MAGIC_DEF("rawVariables", ngx_stream_qjs_ext_periodic_variables,
NULL, NGX_JS_BUFFER),
JS_CGETSET_MAGIC_DEF("variables", ngx_stream_qjs_ext_periodic_variables,
Expand Down Expand Up @@ -1963,6 +1971,13 @@ ngx_engine_njs_clone(ngx_js_ctx_t *ctx, ngx_js_loc_conf_t *cf,

#if (NJS_HAVE_QUICKJS)

static JSValue
ngx_stream_qjs_ext_to_string_tag(JSContext *cx, JSValueConst this_val)
{
return JS_NewString(cx, "Stream Session");
}


static JSValue
ngx_stream_qjs_ext_done(JSContext *cx, JSValueConst this_val, int argc,
JSValueConst *argv, int magic)
Expand Down Expand Up @@ -2180,6 +2195,14 @@ ngx_stream_qjs_ext_off(JSContext *cx, JSValueConst this_val, int argc,
}


static JSValue
ngx_stream_qjs_ext_periodic_to_string_tag(JSContext *cx,
JSValueConst this_val)
{
return JS_NewString(cx, "PeriodicSession");
}


static JSValue
ngx_stream_qjs_ext_periodic_variables(JSContext *cx,
JSValueConst this_val, int type)
Expand Down Expand Up @@ -2691,7 +2714,6 @@ static JSValue
ngx_stream_qjs_session_make(JSContext *cx, ngx_int_t proto_id,
ngx_stream_session_t *s)
{
int ret;
JSValue session;
ngx_uint_t i;
ngx_stream_qjs_session_t *ses;
Expand All @@ -2701,12 +2723,6 @@ ngx_stream_qjs_session_make(JSContext *cx, ngx_int_t proto_id,
return JS_EXCEPTION;
}

ret = qjs_set_to_string_tag(cx, session, "Stream Session");
if (ret == -1) {
JS_FreeValue(cx, session);
return JS_EXCEPTION;
}

ses = js_malloc(cx, sizeof(ngx_stream_qjs_session_t));
if (ses == NULL) {
return JS_ThrowOutOfMemory(cx);
Expand Down

0 comments on commit 1e04d34

Please sign in to comment.