diff --git a/builtin_regexp.go b/builtin_regexp.go index 88f0322..470b519 100644 --- a/builtin_regexp.go +++ b/builtin_regexp.go @@ -18,7 +18,10 @@ func builtinRegExp(call FunctionCall) Value { } func builtinNewRegExp(self *_object, _ Value, argumentList []Value) Value { - return toValue(self.runtime.newRegExp(valueOfArrayIndex(argumentList, 0), valueOfArrayIndex(argumentList, 1))) + return toValue(self.runtime.newRegExp( + valueOfArrayIndex(argumentList, 0), + valueOfArrayIndex(argumentList, 1), + )) } func builtinRegExp_toString(call FunctionCall) Value { diff --git a/global.go b/global.go index 7b65c96..582302a 100644 --- a/global.go +++ b/global.go @@ -774,6 +774,12 @@ func (runtime *_runtime) newNumber(value Value) *_object { } func (runtime *_runtime) newRegExp(patternValue Value, flagsValue Value) *_object { + if object := patternValue._object(); object != nil { + if object.class == "RegExp" && flagsValue.IsDefined() { + panic(newTypeError("Cannot supply flags when constructing one RegExp from another")) + } + } + pattern := "" if patternValue.IsDefined() { pattern = toString(patternValue) @@ -782,6 +788,7 @@ func (runtime *_runtime) newRegExp(patternValue Value, flagsValue Value) *_objec if flagsValue.IsDefined() { flags = toString(flagsValue) } + return runtime._newRegExp(pattern, flags) } diff --git a/regexp_test.go b/regexp_test.go index 46e05a7..cf57d95 100644 --- a/regexp_test.go +++ b/regexp_test.go @@ -131,6 +131,10 @@ func TestRegExpCopying(t *testing.T) { abc.indicator = 1; [ abc.indicator, def.indicator ]; `, "1,1") + + test(`raise: + RegExp(new RegExp("\d"), "1"); + `, "TypeError: Cannot supply flags when constructing one RegExp from another") } func TestRegExp_multiline(t *testing.T) {