]> git.kaiwu.me - njs.git/commitdiff
Optimized qjs_to_bytes().
authorDmitry Volyntsev <xeioex@nginx.com>
Tue, 8 Oct 2024 05:46:40 +0000 (22:46 -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.

src/qjs.c

index e765356905ad768473bd3eccb98a24910037170d..3d378fcc27963d22f60e07b860f5c6b85d18b168 100644 (file)
--- a/src/qjs.c
+++ b/src/qjs.c
@@ -175,6 +175,10 @@ qjs_to_bytes(JSContext *ctx, qjs_bytes_t *bytes, JSValueConst value)
     size_t   byte_offset, byte_length;
     JSValue  val;
 
+    if (JS_IsString(value)) {
+        goto string;
+    }
+
     val = JS_GetTypedArrayBuffer(ctx, value, &byte_offset, &byte_length, NULL);
     if (!JS_IsException(val)) {
         bytes->start = JS_GetArrayBuffer(ctx, &bytes->length, val);
@@ -195,8 +199,6 @@ qjs_to_bytes(JSContext *ctx, qjs_bytes_t *bytes, JSValueConst value)
         return 0;
     }
 
-    bytes->tag = JS_TAG_STRING;
-
     if (!JS_IsString(value)) {
         val = JS_ToString(ctx, value);
 
@@ -209,6 +211,9 @@ qjs_to_bytes(JSContext *ctx, qjs_bytes_t *bytes, JSValueConst value)
         }
     }
 
+string:
+
+    bytes->tag = JS_TAG_STRING;
     bytes->start = (u_char *) JS_ToCStringLen(ctx, &bytes->length, value);
 
     return (bytes->start != NULL) ? 0 : -1;