Skip to content

Commit 1eb9608

Browse files
authored
Fix regexp split with zero-length capture group (#566)
The expected result of `"ab".split(/(c)*/)[1]` is `undefined` but was in fact `"undefined"` due to unintentional stringification. Fixes: #565
1 parent e156452 commit 1eb9608

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

quickjs.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43835,9 +43835,14 @@ static JSValue js_regexp_Symbol_split(JSContext *ctx, JSValue this_val,
4383543835
if (js_get_length64(ctx, &numberOfCaptures, z))
4383643836
goto exception;
4383743837
for(i = 1; i < numberOfCaptures; i++) {
43838-
sub = JS_ToStringFree(ctx, JS_GetPropertyInt64(ctx, z, i));
43838+
sub = JS_GetPropertyInt64(ctx, z, i);
4383943839
if (JS_IsException(sub))
4384043840
goto exception;
43841+
if (!JS_IsUndefined(sub)) {
43842+
sub = JS_ToStringFree(ctx, sub);
43843+
if (JS_IsException(sub))
43844+
goto exception;
43845+
}
4384143846
if (JS_DefinePropertyValueInt64(ctx, A, lengthA++, sub, JS_PROP_C_W_E | JS_PROP_THROW) < 0)
4384243847
goto exception;
4384343848
if (lengthA == lim)

tests/test_builtin.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,8 @@ function test_regexp()
783783
assert(a, ["a", undefined]);
784784
a = /(?:|[\w])+([0-9])/.exec("123a23");
785785
assert(a, ["123a23", "3"]);
786+
a = "ab".split(/(c)*/);
787+
assert(a, ["a", undefined, "b"]);
786788
}
787789

788790
function test_symbol()

0 commit comments

Comments
 (0)