From 39a2d4bf212346d1487e4d27383453cafefa17ea Mon Sep 17 00:00:00 2001 From: Dmitry Volyntsev Date: Mon, 7 Oct 2024 22:46:40 -0700 Subject: [PATCH] Optimized qjs_to_bytes(). 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 | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/qjs.c b/src/qjs.c index e76535690..3d378fcc2 100644 --- 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;