From 7a80be473db4120db2ab899c7f6914fc8ee9c33d Mon Sep 17 00:00:00 2001 From: "ager@chromium.org" Date: Mon, 13 Jul 2009 23:41:17 +0000 Subject: [PATCH] Firefox and Safari both allow calling regular expression objects as functions (as an alias for calling the exec method). For compatibility make call_regexp the default and remove the flag. Review URL: http://codereview.chromium.org/155453 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@2451 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- src/arm/stub-cache-arm.cc | 2 -- src/execution.cc | 21 +++++++-------- src/flag-definitions.h | 3 --- test/mjsunit/regexp-call-as-function.js | 36 +++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 17 deletions(-) create mode 100644 test/mjsunit/regexp-call-as-function.js diff --git a/src/arm/stub-cache-arm.cc b/src/arm/stub-cache-arm.cc index 6d9ace84cf..b2aa6c8399 100644 --- a/src/arm/stub-cache-arm.cc +++ b/src/arm/stub-cache-arm.cc @@ -1121,8 +1121,6 @@ Object* LoadStubCompiler::CompileLoadGlobal(JSObject* object, } -// TODO(1224671): IC stubs for keyed loads have not been implemented -// for ARM. Object* KeyedLoadStubCompiler::CompileLoadField(String* name, JSObject* receiver, JSObject* holder, diff --git a/src/execution.cc b/src/execution.cc index adc18725e4..40a9b4f9a5 100644 --- a/src/execution.cc +++ b/src/execution.cc @@ -164,19 +164,16 @@ Handle Execution::GetFunctionDelegate(Handle object) { // If you return a function from here, it will be called when an // attempt is made to call the given object as a function. - // The regular expression code here is really meant more as an - // example than anything else. KJS does not support calling regular - // expressions as functions, but SpiderMonkey does. - if (FLAG_call_regexp) { - bool is_regexp = - object->IsHeapObject() && - (HeapObject::cast(*object)->map()->constructor() == - *Top::regexp_function()); + // Regular expressions can be called as functions in both Firefox + // and Safari so we allow it too. + bool is_regexp = + object->IsHeapObject() && + (HeapObject::cast(*object)->map()->constructor() == + *Top::regexp_function()); - if (is_regexp) { - Handle exec = Factory::exec_symbol(); - return Handle(object->GetProperty(*exec)); - } + if (is_regexp) { + Handle exec = Factory::exec_symbol(); + return Handle(object->GetProperty(*exec)); } // Objects created through the API can have an instance-call handler diff --git a/src/flag-definitions.h b/src/flag-definitions.h index 4e7829b2c0..b0770b0028 100644 --- a/src/flag-definitions.h +++ b/src/flag-definitions.h @@ -144,9 +144,6 @@ DEFINE_bool(debugger_auto_break, false, "automatically set the debug break flag when debugger commands are " "in the queue (experimental)") -// execution.cc -DEFINE_bool(call_regexp, false, "allow calls to RegExp objects") - // frames.cc DEFINE_int(max_stack_trace_source_length, 300, "maximum length of function source code printed in a stack trace.") diff --git a/test/mjsunit/regexp-call-as-function.js b/test/mjsunit/regexp-call-as-function.js new file mode 100644 index 0000000000..4cbe7f94f3 --- /dev/null +++ b/test/mjsunit/regexp-call-as-function.js @@ -0,0 +1,36 @@ +// Copyright 2009 the V8 project authors. All rights reserved. +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following +// disclaimer in the documentation and/or other materials provided +// with the distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived +// from this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +// Test that regular expressions can be called as functions. Calling +// a regular expression as a function corresponds to calling it's exec +// method. + +var regexp = /a(b)(c)/; +var subject = "xyzabcde"; +var expected = 'abc,b,c'; +assertEquals(expected, String(regexp.exec(subject))); +assertEquals(expected, String(regexp(subject)));