Skip to content

Commit 8811ef5

Browse files
committed
Fix String.protype issues with primitives
1 parent c804f96 commit 8811ef5

File tree

2 files changed

+16
-39
lines changed

2 files changed

+16
-39
lines changed

builtin_string.go

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ func (r *Runtime) stringproto_localeCompare(call FunctionCall) Value {
381381
func (r *Runtime) stringproto_match(call FunctionCall) Value {
382382
r.checkObjectCoercible(call.This)
383383
regexp := call.Argument(0)
384-
if regexp != _undefined && regexp != _null {
384+
if _, ok := regexp.(*Object); ok {
385385
if matcher := toMethod(r.getV(regexp, SymMatch)); matcher != nil {
386386
return matcher(FunctionCall{
387387
This: regexp,
@@ -412,14 +412,12 @@ func (r *Runtime) stringproto_match(call FunctionCall) Value {
412412
func (r *Runtime) stringproto_matchAll(call FunctionCall) Value {
413413
r.checkObjectCoercible(call.This)
414414
regexp := call.Argument(0)
415-
if regexp != _undefined && regexp != _null {
415+
if o, ok := regexp.(*Object); ok {
416416
if isRegexp(regexp) {
417-
if o, ok := regexp.(*Object); ok {
418-
flags := nilSafe(o.self.getStr("flags", nil))
419-
r.checkObjectCoercible(flags)
420-
if !strings.Contains(flags.toString().String(), "g") {
421-
panic(r.NewTypeError("RegExp doesn't have global flag set"))
422-
}
417+
flags := nilSafe(o.self.getStr("flags", nil))
418+
r.checkObjectCoercible(flags)
419+
if !strings.Contains(flags.toString().String(), "g") {
420+
panic(r.NewTypeError("RegExp doesn't have global flag set"))
423421
}
424422
}
425423
if matcher := toMethod(r.getV(regexp, SymMatchAll)); matcher != nil {
@@ -664,7 +662,7 @@ func (r *Runtime) stringproto_replace(call FunctionCall) Value {
664662
r.checkObjectCoercible(call.This)
665663
searchValue := call.Argument(0)
666664
replaceValue := call.Argument(1)
667-
if searchValue != _undefined && searchValue != _null {
665+
if _, ok := searchValue.(*Object); ok {
668666
if replacer := toMethod(r.getV(searchValue, SymReplace)); replacer != nil {
669667
return replacer(FunctionCall{
670668
This: searchValue,
@@ -689,14 +687,12 @@ func (r *Runtime) stringproto_replaceAll(call FunctionCall) Value {
689687
r.checkObjectCoercible(call.This)
690688
searchValue := call.Argument(0)
691689
replaceValue := call.Argument(1)
692-
if searchValue != _undefined && searchValue != _null {
690+
if o, ok := searchValue.(*Object); ok {
693691
if isRegexp(searchValue) {
694-
if o, ok := searchValue.(*Object); ok {
695-
flags := nilSafe(o.self.getStr("flags", nil))
696-
r.checkObjectCoercible(flags)
697-
if !strings.Contains(flags.toString().String(), "g") {
698-
panic(r.NewTypeError("String.prototype.replaceAll called with a non-global RegExp argument"))
699-
}
692+
flags := nilSafe(o.self.getStr("flags", nil))
693+
r.checkObjectCoercible(flags)
694+
if !strings.Contains(flags.toString().String(), "g") {
695+
panic(r.NewTypeError("String.prototype.replaceAll called with a non-global RegExp argument"))
700696
}
701697
}
702698
if replacer := toMethod(r.getV(searchValue, SymReplace)); replacer != nil {
@@ -726,7 +722,7 @@ func (r *Runtime) stringproto_replaceAll(call FunctionCall) Value {
726722
func (r *Runtime) stringproto_search(call FunctionCall) Value {
727723
r.checkObjectCoercible(call.This)
728724
regexp := call.Argument(0)
729-
if regexp != _undefined && regexp != _null {
725+
if _, ok := regexp.(*Object); ok {
730726
if searcher := toMethod(r.getV(regexp, SymSearch)); searcher != nil {
731727
return searcher(FunctionCall{
732728
This: regexp,
@@ -799,7 +795,7 @@ func (r *Runtime) stringproto_split(call FunctionCall) Value {
799795
r.checkObjectCoercible(call.This)
800796
separatorValue := call.Argument(0)
801797
limitValue := call.Argument(1)
802-
if separatorValue != _undefined && separatorValue != _null {
798+
if _, ok := separatorValue.(*Object); ok {
803799
if splitter := toMethod(r.getV(separatorValue, SymSplit)); splitter != nil {
804800
return splitter(FunctionCall{
805801
This: separatorValue,

tc39_test.go

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -218,27 +218,8 @@ var (
218218
"test/built-ins/TypedArray/prototype/with/value-throw-completion.js": true,
219219
"test/built-ins/TypedArray/prototype/slice/speciesctor-return-same-buffer-with-offset.js": true,
220220

221-
// String prototype (cstm-* and regexp)
222-
"test/built-ins/String/prototype/split/cstm-split-on-string-primitive.js": true,
223-
"test/built-ins/String/prototype/split/cstm-split-on-number-primitive.js": true,
224-
"test/built-ins/String/prototype/split/cstm-split-on-boolean-primitive.js": true,
225-
"test/built-ins/String/prototype/split/cstm-split-on-bigint-primitive.js": true,
226-
"test/built-ins/String/prototype/search/cstm-search-on-string-primitive.js": true,
227-
"test/built-ins/String/prototype/search/cstm-search-on-number-primitive.js": true,
228-
"test/built-ins/String/prototype/search/cstm-search-on-boolean-primitive.js": true,
229-
"test/built-ins/String/prototype/search/cstm-search-on-bigint-primitive.js": true,
230-
"test/built-ins/String/prototype/replaceAll/cstm-replaceall-on-number-primitive.js": true,
231-
"test/built-ins/String/prototype/replaceAll/cstm-replaceall-on-bigint-primitive.js": true,
232-
"test/built-ins/String/prototype/replace/cstm-replace-on-string-primitive.js": true,
233-
"test/built-ins/String/prototype/replace/cstm-replace-on-number-primitive.js": true,
234-
"test/built-ins/String/prototype/replaceAll/cstm-replaceall-on-string-primitive.js": true,
235-
"test/built-ins/String/prototype/replace/cstm-replace-on-boolean-primitive.js": true,
236-
"test/built-ins/String/prototype/replaceAll/cstm-replaceall-on-boolean-primitive.js": true,
237-
"test/built-ins/String/prototype/replace/regexp-capture-by-index.js": true,
238-
"test/built-ins/String/prototype/replace/cstm-replace-on-bigint-primitive.js": true,
239-
"test/built-ins/String/prototype/matchAll/cstm-matchall-on-number-primitive.js": true,
240-
"test/built-ins/String/prototype/matchAll/cstm-matchall-on-bigint-primitive.js": true,
241-
"test/built-ins/String/prototype/matchAll/cstm-matchall-on-string-primitive.js": true,
221+
// String prototype regexp-capture-by-index (regexp named groups)
222+
"test/built-ins/String/prototype/replace/regexp-capture-by-index.js": true,
242223

243224
// RegExp CharacterClassEscapes
244225
"test/built-ins/RegExp/CharacterClassEscapes/character-class-word-class-escape-positive-cases.js": true,

0 commit comments

Comments
 (0)