Support for function calls on an arbitrary expression that returns

a function in the top-level compiler.

e.g.

function f() { return (function() { return true; }) }
f()()


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

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@3196 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent b710d66f
...@@ -723,8 +723,13 @@ void FastCodeGenerator::EmitCallWithStub(Call* expr) { ...@@ -723,8 +723,13 @@ void FastCodeGenerator::EmitCallWithStub(Call* expr) {
void FastCodeGenerator::VisitCall(Call* expr) { void FastCodeGenerator::VisitCall(Call* expr) {
Expression* fun = expr->expression(); Expression* fun = expr->expression();
Variable* var = fun->AsVariableProxy()->AsVariable();
if (fun->AsProperty() != NULL) { if (var != NULL &&
var->is_possibly_eval()) {
// Call to eval.
UNREACHABLE();
} else if (fun->AsProperty() != NULL) {
// Call on a property. // Call on a property.
Property* prop = fun->AsProperty(); Property* prop = fun->AsProperty();
Literal* key = prop->key()->AsLiteral(); Literal* key = prop->key()->AsLiteral();
...@@ -755,9 +760,8 @@ void FastCodeGenerator::VisitCall(Call* expr) { ...@@ -755,9 +760,8 @@ void FastCodeGenerator::VisitCall(Call* expr) {
__ str(r1, MemOperand(sp)); __ str(r1, MemOperand(sp));
EmitCallWithStub(expr); EmitCallWithStub(expr);
} }
} else if (fun->AsVariableProxy()->AsVariable() != NULL) { } else if (var != NULL) {
// Call on a global variable // Call on a global variable
Variable* var = fun->AsVariableProxy()->AsVariable();
ASSERT(var != NULL && !var->is_this() && var->is_global()); ASSERT(var != NULL && !var->is_this() && var->is_global());
ASSERT(!var->is_possibly_eval()); ASSERT(!var->is_possibly_eval());
__ mov(r1, Operand(var->name())); __ mov(r1, Operand(var->name()));
...@@ -765,10 +769,19 @@ void FastCodeGenerator::VisitCall(Call* expr) { ...@@ -765,10 +769,19 @@ void FastCodeGenerator::VisitCall(Call* expr) {
__ ldr(r0, CodeGenerator::GlobalObject()); __ ldr(r0, CodeGenerator::GlobalObject());
__ stm(db_w, sp, r1.bit() | r0.bit()); __ stm(db_w, sp, r1.bit() | r0.bit());
EmitCallWithIC(expr, RelocInfo::CODE_TARGET_CONTEXT); EmitCallWithIC(expr, RelocInfo::CODE_TARGET_CONTEXT);
} else { } else if (var != NULL && var->slot() != NULL &&
// Calls we cannot handle right now. var->slot()->type() == Slot::LOOKUP) {
// Should bailout in the CodeGenSelector. // Call inside a with-statement
UNREACHABLE(); UNREACHABLE();
} else {
// Call with an arbitrary function expression.
Visit(expr->expression());
// Load global receiver object.
__ ldr(r1, CodeGenerator::GlobalObject());
__ ldr(r1, FieldMemOperand(r1, GlobalObject::kGlobalReceiverOffset));
__ push(r1);
// Emit function call.
EmitCallWithStub(expr);
} }
} }
......
...@@ -791,6 +791,9 @@ void CodeGenSelector::VisitCall(Call* expr) { ...@@ -791,6 +791,9 @@ void CodeGenSelector::VisitCall(Call* expr) {
// Check for supported calls // Check for supported calls
if (var != NULL && var->is_possibly_eval()) { if (var != NULL && var->is_possibly_eval()) {
// ----------------------------------
// JavaScript example: 'eval(arg)' // eval is not known to be shadowed
// ----------------------------------
BAILOUT("Call to a function named 'eval'"); BAILOUT("Call to a function named 'eval'");
} else if (var != NULL && !var->is_this() && var->is_global()) { } else if (var != NULL && !var->is_this() && var->is_global()) {
// ---------------------------------- // ----------------------------------
...@@ -811,8 +814,17 @@ void CodeGenSelector::VisitCall(Call* expr) { ...@@ -811,8 +814,17 @@ void CodeGenSelector::VisitCall(Call* expr) {
ProcessExpression(prop->key(), Expression::kValue); ProcessExpression(prop->key(), Expression::kValue);
CHECK_BAILOUT; CHECK_BAILOUT;
} }
} else if (var != NULL && var->slot() != NULL &&
var->slot()->type() == Slot::LOOKUP) {
// ----------------------------------
// JavaScript example: 'with (obj) foo(1, 2, 3)' // foo is in obj
// ----------------------------------
BAILOUT("Call inside a with-statement");
} else { } else {
BAILOUT("Unsupported call to a function"); // ----------------------------------
// JavaScript example: 'foo(1, 2, 3)' // foo is any expression, not global
// ----------------------------------
ProcessExpression(fun, Expression::kValue);
} }
// Check all arguments to the call. (Relies on TEMP meaning STACK.) // Check all arguments to the call. (Relies on TEMP meaning STACK.)
for (int i = 0; i < args->length(); i++) { for (int i = 0; i < args->length(); i++) {
......
...@@ -745,8 +745,13 @@ void FastCodeGenerator::EmitCallWithStub(Call* expr) { ...@@ -745,8 +745,13 @@ void FastCodeGenerator::EmitCallWithStub(Call* expr) {
void FastCodeGenerator::VisitCall(Call* expr) { void FastCodeGenerator::VisitCall(Call* expr) {
Expression* fun = expr->expression(); Expression* fun = expr->expression();
Variable* var = fun->AsVariableProxy()->AsVariable();
if (fun->AsProperty() != NULL) { if (var != NULL &&
var->is_possibly_eval()) {
// Call to eval.
UNREACHABLE();
} else if (fun->AsProperty() != NULL) {
// Call on a property. // Call on a property.
Property* prop = fun->AsProperty(); Property* prop = fun->AsProperty();
Literal* key = prop->key()->AsLiteral(); Literal* key = prop->key()->AsLiteral();
...@@ -782,19 +787,28 @@ void FastCodeGenerator::VisitCall(Call* expr) { ...@@ -782,19 +787,28 @@ void FastCodeGenerator::VisitCall(Call* expr) {
} }
EmitCallWithStub(expr); EmitCallWithStub(expr);
} }
} else if (fun->AsVariableProxy()->AsVariable() != NULL) { } else if (var != NULL) {
// Call on a global variable // Call on a global variable
Variable* var = fun->AsVariableProxy()->AsVariable(); ASSERT(var != NULL);
ASSERT(var != NULL && !var->is_this() && var->is_global()); ASSERT(!var->is_this());
ASSERT(var->is_global());
ASSERT(!var->is_possibly_eval()); ASSERT(!var->is_possibly_eval());
__ push(Immediate(var->name())); __ push(Immediate(var->name()));
// Push global object (receiver). // Push global object (receiver).
__ push(CodeGenerator::GlobalObject()); __ push(CodeGenerator::GlobalObject());
EmitCallWithIC(expr, RelocInfo::CODE_TARGET_CONTEXT); EmitCallWithIC(expr, RelocInfo::CODE_TARGET_CONTEXT);
} else { } else if (var != NULL && var->slot() != NULL &&
// Calls we cannot handle right now. var->slot()->type() == Slot::LOOKUP) {
// Should bailout in the CodeGenSelector. // Call inside a with-statement
UNREACHABLE(); UNREACHABLE();
} else {
// Call with an arbitrary function expression.
Visit(expr->expression());
// Load global receiver object.
__ mov(ebx, CodeGenerator::GlobalObject());
__ push(FieldOperand(ebx, GlobalObject::kGlobalReceiverOffset));
// Emit function call.
EmitCallWithStub(expr);
} }
} }
......
...@@ -760,8 +760,13 @@ void FastCodeGenerator::EmitCallWithStub(Call* expr) { ...@@ -760,8 +760,13 @@ void FastCodeGenerator::EmitCallWithStub(Call* expr) {
void FastCodeGenerator::VisitCall(Call* expr) { void FastCodeGenerator::VisitCall(Call* expr) {
Expression* fun = expr->expression(); Expression* fun = expr->expression();
Variable* var = fun->AsVariableProxy()->AsVariable();
if (fun->AsProperty() != NULL) { if (var != NULL &&
var->is_possibly_eval()) {
// Call to eval.
UNREACHABLE();
} else if (fun->AsProperty() != NULL) {
// Call on a property. // Call on a property.
Property* prop = fun->AsProperty(); Property* prop = fun->AsProperty();
Literal* key = prop->key()->AsLiteral(); Literal* key = prop->key()->AsLiteral();
...@@ -797,19 +802,26 @@ void FastCodeGenerator::VisitCall(Call* expr) { ...@@ -797,19 +802,26 @@ void FastCodeGenerator::VisitCall(Call* expr) {
} }
EmitCallWithStub(expr); EmitCallWithStub(expr);
} }
} else if (fun->AsVariableProxy()->AsVariable() != NULL) { } else if (var != NULL) {
// Call on a global variable // Call on a global variable
Variable* var = fun->AsVariableProxy()->AsVariable();
ASSERT(var != NULL && !var->is_this() && var->is_global()); ASSERT(var != NULL && !var->is_this() && var->is_global());
ASSERT(!var->is_possibly_eval()); ASSERT(!var->is_possibly_eval());
__ Push(var->name()); __ Push(var->name());
// Push global object (receiver). // Push global object (receiver).
__ push(CodeGenerator::GlobalObject()); __ push(CodeGenerator::GlobalObject());
EmitCallWithIC(expr, RelocInfo::CODE_TARGET_CONTEXT); EmitCallWithIC(expr, RelocInfo::CODE_TARGET_CONTEXT);
} else { } else if (var != NULL && var->slot() != NULL &&
// Calls we cannot handle right now. var->slot()->type() == Slot::LOOKUP) {
// Should bailout in the CodeGenSelector. // Call inside a with-statement
UNREACHABLE(); UNREACHABLE();
} else {
// Call with an arbitrary function expression.
Visit(expr->expression());
// Load global receiver object.
__ movq(rbx, CodeGenerator::GlobalObject());
__ push(FieldOperand(rbx, GlobalObject::kGlobalReceiverOffset));
// Emit function call.
EmitCallWithStub(expr);
} }
} }
......
...@@ -46,3 +46,7 @@ c = "x"; ...@@ -46,3 +46,7 @@ c = "x";
a = b[c](10); a = b[c](10);
assertEquals(10, a); assertEquals(10, a);
// Call on a function expression
function g() { return f; }
a = g()(8);
assertEquals(8, a);
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