]> git.kaiwu.me - njs.git/commitdiff
Optimized ngx_qjs_string().
authorDmitry Volyntsev <xeioex@nginx.com>
Tue, 8 Oct 2024 05:41:24 +0000 (22:41 -0700)
committerDmitry Volyntsev <xeioexception@gmail.com>
Tue, 8 Oct 2024 23:03:08 +0000 (16:03 -0700)
Doing JS_IsString() check first before a heavy-weight call to
JS_GetTypedArrayBuffer() which throws an exception when argument is not
a typed array.

nginx/ngx_js.c

index 5fe3dc847fd18b7abd2386037fa9e3e59a876a92..98e48dc2ca159aea38218e62d01eb5fb587196c6 100644 (file)
@@ -1472,6 +1472,10 @@ ngx_qjs_string(ngx_engine_t *e, JSValueConst val, ngx_str_t *dst)
 
     cx = e->u.qjs.ctx;
 
+    if (JS_IsString(val)) {
+        goto string;
+    }
+
     buffer = JS_GetTypedArrayBuffer(cx, val, &byte_offset, &byte_length, NULL);
     if (!JS_IsException(buffer)) {
         start = JS_GetArrayBuffer(cx, &dst->len, buffer);
@@ -1492,6 +1496,8 @@ ngx_qjs_string(ngx_engine_t *e, JSValueConst val, ngx_str_t *dst)
         }
     }
 
+string:
+
     str = JS_ToCString(cx, val);
     if (str == NULL) {
         return NGX_ERROR;