Skip to content

Commit

Permalink
Optimized qjs_to_bytes().
Browse files Browse the repository at this point in the history
Doing JS_IsString() check first before a heavy-weight call to
JS_GetTypedArrayBuffer() which throws an exception when argument is not
a typed array.
  • Loading branch information
xeioex committed Oct 8, 2024
1 parent 3a4349e commit 39a2d4b
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/qjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);

Expand All @@ -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;
Expand Down

0 comments on commit 39a2d4b

Please sign in to comment.