Commit 5d0d354e authored by feng@chromium.org's avatar feng@chromium.org

Fix issue http://code.google.com/p/v8/issues/detail?id=32

Allows numberical strings as array index and make a call. e.g.,
callbacks['0']();

Added more test case for regexp (disabled by default, requires --call_regexp)
and call_as_function object created by API.

Review URL: http://codereview.chromium.org/1604

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@214 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 7adf58ff
...@@ -250,6 +250,26 @@ void KeyedStoreIC::Clear(Address address, Code* target) { ...@@ -250,6 +250,26 @@ void KeyedStoreIC::Clear(Address address, Code* target) {
} }
Object* CallIC::TryCallAsFunction(Object* object) {
HandleScope scope;
Handle<Object> target(object);
Handle<Object> delegate = Execution::GetFunctionDelegate(target);
if (delegate->IsJSFunction()) {
// Patch the receiver and use the delegate as the function to
// invoke. This is used for invoking objects as if they were
// functions.
const int argc = this->target()->arguments_count();
StackFrameLocator locator;
JavaScriptFrame* frame = locator.FindJavaScriptFrame(0);
int index = frame->ComputeExpressionsCount() - (argc + 1);
frame->SetExpression(index, *target);
}
return *delegate;
}
Object* CallIC::LoadFunction(State state, Object* CallIC::LoadFunction(State state,
Handle<Object> object, Handle<Object> object,
Handle<String> name) { Handle<String> name) {
...@@ -259,12 +279,26 @@ Object* CallIC::LoadFunction(State state, ...@@ -259,12 +279,26 @@ Object* CallIC::LoadFunction(State state,
return TypeError("non_object_property_call", object, name); return TypeError("non_object_property_call", object, name);
} }
Object* result = Heap::the_hole_value();
// Check if the name is trivially convertible to an index and get
// the element if so.
uint32_t index;
if (name->AsArrayIndex(&index)) {
result = object->GetElement(index);
if (result->IsJSFunction()) return result;
// Try to find a suitable function delegate for the object at hand.
result = TryCallAsFunction(result);
if (result->IsJSFunction()) return result;
// Otherwise, it will fail in the lookup step.
}
// Lookup the property in the object. // Lookup the property in the object.
LookupResult lookup; LookupResult lookup;
object->Lookup(*name, &lookup); object->Lookup(*name, &lookup);
Object* result = Heap::the_hole_value();
if (!lookup.IsValid()) { if (!lookup.IsValid()) {
// If the object does not have the requested property, check which // If the object does not have the requested property, check which
// exception we need to throw. // exception we need to throw.
...@@ -328,23 +362,9 @@ Object* CallIC::LoadFunction(State state, ...@@ -328,23 +362,9 @@ Object* CallIC::LoadFunction(State state,
} }
// Try to find a suitable function delegate for the object at hand. // Try to find a suitable function delegate for the object at hand.
HandleScope scope; result = TryCallAsFunction(result);
Handle<Object> target(result); return result->IsJSFunction() ?
Handle<Object> delegate = Execution::GetFunctionDelegate(target); result : TypeError("property_not_function", object, name);
if (delegate->IsJSFunction()) {
// Patch the receiver and use the delegate as the function to
// invoke. This is used for invoking objects as if they were
// functions.
const int argc = this->target()->arguments_count();
StackFrameLocator locator;
JavaScriptFrame* frame = locator.FindJavaScriptFrame(0);
int index = frame->ComputeExpressionsCount() - (argc + 1);
frame->SetExpression(index, *target);
return *delegate;
} else {
return TypeError("property_not_function", object, name);
}
} }
......
...@@ -183,6 +183,11 @@ class CallIC: public IC { ...@@ -183,6 +183,11 @@ class CallIC: public IC {
Handle<Object> object, Handle<Object> object,
Handle<String> name); Handle<String> name);
// Returns a JSFunction if the object can be called as a function,
// and patches the stack to be ready for the call.
// Otherwise, it returns the undefined value.
Object* TryCallAsFunction(Object* object);
static void Clear(Address address, Code* target); static void Clear(Address address, Code* target);
friend class IC; friend class IC;
}; };
......
...@@ -3959,6 +3959,11 @@ THREADED_TEST(CallAsFunction) { ...@@ -3959,6 +3959,11 @@ THREADED_TEST(CallAsFunction) {
CHECK(!try_catch.HasCaught()); CHECK(!try_catch.HasCaught());
CHECK_EQ(49, value->Int32Value()); CHECK_EQ(49, value->Int32Value());
// test special case of call as function
value = Script::Compile(v8_str("[obj]['0'](45)"))->Run();
CHECK(!try_catch.HasCaught());
CHECK_EQ(45, value->Int32Value());
value = Script::Compile(v8_str("obj.call = Function.prototype.call;" value = Script::Compile(v8_str("obj.call = Function.prototype.call;"
"obj.call(null, 87)"))->Run(); "obj.call(null, 87)"))->Run();
CHECK(!try_catch.HasCaught()); CHECK(!try_catch.HasCaught());
......
// Copyright 2008 Google Inc. 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.
// Flags: --call_regexp
var callbacks = [ function() {return 'foo'}, "nonobject", /abc/ ];
assertEquals('foo', callbacks['0']());
assertThrows("callbacks['1']()");
assertEquals('abc', callbacks['2']("abcdefg"));
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment