diff --git a/builtin_regexp.go b/builtin_regexp.go index f9533f7..88f0322 100644 --- a/builtin_regexp.go +++ b/builtin_regexp.go @@ -7,7 +7,14 @@ import ( // RegExp func builtinRegExp(call FunctionCall) Value { - return toValue(call.runtime.newRegExp(call.Argument(0), call.Argument(1))) + pattern := call.Argument(0) + flags := call.Argument(1) + if object := pattern._object(); object != nil { + if object.class == "RegExp" && flags.IsUndefined() { + return pattern + } + } + return toValue(call.runtime.newRegExp(pattern, flags)) } func builtinNewRegExp(self *_object, _ Value, argumentList []Value) Value { diff --git a/regexp_test.go b/regexp_test.go index 26c11b1..46e05a7 100644 --- a/regexp_test.go +++ b/regexp_test.go @@ -121,6 +121,18 @@ func TestRegExp_zaacbbbcac(t *testing.T) { } } +func TestRegExpCopying(t *testing.T) { + Terst(t) + + test := runTest() + test(` + abc = /xyzzy/i; + def = RegExp(abc); + abc.indicator = 1; + [ abc.indicator, def.indicator ]; + `, "1,1") +} + func TestRegExp_multiline(t *testing.T) { Terst(t)