From: Dmitry Volyntsev Date: Tue, 8 Oct 2024 05:46:40 +0000 (-0700) Subject: Optimized qjs_to_bytes(). X-Git-Tag: 0.8.7~15 X-Git-Url: http://www.kaiwu.me/postgresql/commit/static/gitweb.js?a=commitdiff_plain;h=39a2d4bf212346d1487e4d27383453cafefa17ea;p=njs.git 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. --- diff --git a/src/qjs.c b/src/qjs.c index e7653569..3d378fcc 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;