Skip to content

Commit 4727e40

Browse files
authored
Retrieve RegExp 'g' flag in spec conformant way (#92)
1 parent b56cbb1 commit 4727e40

File tree

2 files changed

+27
-20
lines changed

2 files changed

+27
-20
lines changed

quickjs.c

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40489,24 +40489,31 @@ static JSValue js_regexp_Symbol_match(JSContext *ctx, JSValueConst this_val,
4048940489
{
4049040490
// [Symbol.match](str)
4049140491
JSValueConst rx = this_val;
40492-
JSValue A, S, result, matchStr;
40492+
JSValue A, S, flags, result, matchStr;
4049340493
int global, n, fullUnicode, isEmpty;
4049440494
JSString *p;
4049540495

4049640496
if (!JS_IsObject(rx))
4049740497
return JS_ThrowTypeErrorNotAnObject(ctx);
4049840498

4049940499
A = JS_UNDEFINED;
40500+
flags = JS_UNDEFINED;
4050040501
result = JS_UNDEFINED;
4050140502
matchStr = JS_UNDEFINED;
4050240503
S = JS_ToString(ctx, argv[0]);
4050340504
if (JS_IsException(S))
4050440505
goto exception;
4050540506

40506-
global = JS_ToBoolFree(ctx, JS_GetProperty(ctx, rx, JS_ATOM_global));
40507-
if (global < 0)
40507+
flags = JS_GetProperty(ctx, rx, JS_ATOM_flags);
40508+
if (JS_IsException(flags))
40509+
goto exception;
40510+
flags = JS_ToStringFree(ctx, flags);
40511+
if (JS_IsException(flags))
4050840512
goto exception;
40513+
p = JS_VALUE_GET_STRING(flags);
4050940514

40515+
// TODO(bnoordhuis) query 'u' flag the same way?
40516+
global = (-1 != string_indexof_char(p, 'g', 0));
4051040517
if (!global) {
4051140518
A = JS_RegExpExec(ctx, rx, S);
4051240519
} else {
@@ -40550,12 +40557,14 @@ static JSValue js_regexp_Symbol_match(JSContext *ctx, JSValueConst this_val,
4055040557
}
4055140558
}
4055240559
JS_FreeValue(ctx, result);
40560+
JS_FreeValue(ctx, flags);
4055340561
JS_FreeValue(ctx, S);
4055440562
return A;
4055540563

4055640564
exception:
4055740565
JS_FreeValue(ctx, A);
4055840566
JS_FreeValue(ctx, result);
40567+
JS_FreeValue(ctx, flags);
4055940568
JS_FreeValue(ctx, S);
4056040569
return JS_EXCEPTION;
4056140570
}
@@ -40798,8 +40807,8 @@ static JSValue js_regexp_Symbol_replace(JSContext *ctx, JSValueConst this_val,
4079840807
// [Symbol.replace](str, rep)
4079940808
JSValueConst rx = this_val, rep = argv[1];
4080040809
JSValueConst args[6];
40801-
JSValue str, rep_val, matched, tab, rep_str, namedCaptures, res;
40802-
JSString *sp, *rp;
40810+
JSValue flags, str, rep_val, matched, tab, rep_str, namedCaptures, res;
40811+
JSString *p, *sp, *rp;
4080340812
StringBuffer b_s, *b = &b_s;
4080440813
ValueBuffer v_b, *results = &v_b;
4080540814
int nextSourcePosition, n, j, functionalReplace, is_global, fullUnicode;
@@ -40815,6 +40824,7 @@ static JSValue js_regexp_Symbol_replace(JSContext *ctx, JSValueConst this_val,
4081540824
rep_val = JS_UNDEFINED;
4081640825
matched = JS_UNDEFINED;
4081740826
tab = JS_UNDEFINED;
40827+
flags = JS_UNDEFINED;
4081840828
rep_str = JS_UNDEFINED;
4081940829
namedCaptures = JS_UNDEFINED;
4082040830

@@ -40831,10 +40841,18 @@ static JSValue js_regexp_Symbol_replace(JSContext *ctx, JSValueConst this_val,
4083140841
goto exception;
4083240842
rp = JS_VALUE_GET_STRING(rep_val);
4083340843
}
40834-
fullUnicode = 0;
40835-
is_global = JS_ToBoolFree(ctx, JS_GetProperty(ctx, rx, JS_ATOM_global));
40836-
if (is_global < 0)
40844+
40845+
flags = JS_GetProperty(ctx, rx, JS_ATOM_flags);
40846+
if (JS_IsException(flags))
40847+
goto exception;
40848+
flags = JS_ToStringFree(ctx, flags);
40849+
if (JS_IsException(flags))
4083740850
goto exception;
40851+
p = JS_VALUE_GET_STRING(flags);
40852+
40853+
// TODO(bnoordhuis) query 'u' flag the same way?
40854+
fullUnicode = 0;
40855+
is_global = (-1 != string_indexof_char(p, 'g', 0));
4083840856
if (is_global) {
4083940857
fullUnicode = JS_ToBoolFree(ctx, JS_GetProperty(ctx, rx, JS_ATOM_unicode));
4084040858
if (fullUnicode < 0)
@@ -40968,6 +40986,7 @@ static JSValue js_regexp_Symbol_replace(JSContext *ctx, JSValueConst this_val,
4096840986
value_buffer_free(results);
4096940987
JS_FreeValue(ctx, rep_val);
4097040988
JS_FreeValue(ctx, matched);
40989+
JS_FreeValue(ctx, flags);
4097140990
JS_FreeValue(ctx, tab);
4097240991
JS_FreeValue(ctx, rep_str);
4097340992
JS_FreeValue(ctx, namedCaptures);

test262_errors.txt

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,6 @@ test262/test/built-ins/Function/internals/Construct/derived-this-uninitialized-r
3737
test262/test/built-ins/Function/internals/Construct/derived-this-uninitialized-realm.js:20: strict mode: Test262Error: Expected a ReferenceError but got a different error constructor with the same name
3838
test262/test/built-ins/RegExp/lookahead-quantifier-match-groups.js:27: Test262Error: Expected [a, abc] and [a, undefined] to have the same contents. ? quantifier
3939
test262/test/built-ins/RegExp/lookahead-quantifier-match-groups.js:27: strict mode: Test262Error: Expected [a, abc] and [a, undefined] to have the same contents. ? quantifier
40-
test262/test/built-ins/RegExp/prototype/Symbol.match/flags-tostring-error.js:22: Test262Error: Expected a CustomError but got a Test262Error
41-
test262/test/built-ins/RegExp/prototype/Symbol.match/flags-tostring-error.js:22: strict mode: Test262Error: Expected a CustomError but got a Test262Error
42-
test262/test/built-ins/RegExp/prototype/Symbol.match/get-flags-err.js:23: Test262Error: Expected a CustomError but got a Test262Error
43-
test262/test/built-ins/RegExp/prototype/Symbol.match/get-flags-err.js:23: strict mode: Test262Error: Expected a CustomError but got a Test262Error
44-
test262/test/built-ins/RegExp/prototype/Symbol.match/get-unicode-error.js:22: Test262Error: Expected a Test262Error to be thrown but no exception was thrown at all
45-
test262/test/built-ins/RegExp/prototype/Symbol.match/get-unicode-error.js:22: strict mode: Test262Error: Expected a Test262Error to be thrown but no exception was thrown at all
46-
test262/test/built-ins/RegExp/prototype/Symbol.replace/flags-tostring-error.js:26: Test262Error: Expected a CustomError but got a Test262Error
47-
test262/test/built-ins/RegExp/prototype/Symbol.replace/flags-tostring-error.js:26: strict mode: Test262Error: Expected a CustomError but got a Test262Error
48-
test262/test/built-ins/RegExp/prototype/Symbol.replace/get-flags-err.js:27: Test262Error: Expected a CustomError but got a Test262Error
49-
test262/test/built-ins/RegExp/prototype/Symbol.replace/get-flags-err.js:27: strict mode: Test262Error: Expected a CustomError but got a Test262Error
50-
test262/test/built-ins/RegExp/prototype/Symbol.replace/get-unicode-error.js:26: Test262Error: Expected a Test262Error to be thrown but no exception was thrown at all
51-
test262/test/built-ins/RegExp/prototype/Symbol.replace/get-unicode-error.js:26: strict mode: Test262Error: Expected a Test262Error to be thrown but no exception was thrown at all
5240
test262/test/built-ins/String/prototype/localeCompare/15.5.4.9_CE.js:62: Test262Error: String.prototype.localeCompare considers ö (\u006f\u0308) ≠ ö (\u00f6).
5341
test262/test/built-ins/String/prototype/localeCompare/15.5.4.9_CE.js:62: strict mode: Test262Error: String.prototype.localeCompare considers ö (\u006f\u0308) ≠ ö (\u00f6).
5442
test262/test/built-ins/TypedArray/prototype/set/array-arg-targetbuffer-detached-on-get-src-value-no-throw.js:30: TypeError: out-of-bound numeric index (Testing with Float64Array.)

0 commit comments

Comments
 (0)