From: ager@chromium.org Date: Fri, 16 Jan 2009 09:42:08 +0000 (+0000) Subject: Change the handling of catch blocks to use context extension objects X-Git-Tag: upstream/4.7.83~24793 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=47d12982369edf623ea007e3862d123c4a241949;p=platform%2Fupstream%2Fv8.git Change the handling of catch blocks to use context extension objects instead of normal JSObjects. This ensures that __proto__ and accessors on the Object prototype do not interfere with catch scopes. Also, it fixes the bug that catch variables were not DontDelete (issue 74). Next step is to create special lookup routines for context extension objects and remove the special handling of context extension objects from the general javascript object lookup routines. Review URL: http://codereview.chromium.org/18143 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@1091 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- diff --git a/src/ast.h b/src/ast.h index 427fe7a..360a054 100644 --- a/src/ast.h +++ b/src/ast.h @@ -77,6 +77,7 @@ namespace v8 { namespace internal { V(RegExpLiteral) \ V(ObjectLiteral) \ V(ArrayLiteral) \ + V(CatchExtensionObject) \ V(Assignment) \ V(Throw) \ V(Property) \ @@ -722,6 +723,26 @@ class ArrayLiteral: public Expression { }; +// Node for constructing a context extension object for a catch block. +// The catch context extension object has one property, the catch +// variable, which should be DontDelete. +class CatchExtensionObject: public Expression { + public: + CatchExtensionObject(Literal* key, VariableProxy* value) + : key_(key), value_(value) { + } + + virtual void Accept(AstVisitor* v); + + Literal* key() const { return key_; } + VariableProxy* value() const { return value_; } + + private: + Literal* key_; + VariableProxy* value_; +}; + + class VariableProxy: public Expression { public: virtual void Accept(AstVisitor* v); diff --git a/src/codegen-arm.cc b/src/codegen-arm.cc index c51610d..c06e848 100644 --- a/src/codegen-arm.cc +++ b/src/codegen-arm.cc @@ -2235,6 +2235,17 @@ void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) { } +void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) { + // Call runtime routine to allocate the catch extension object and + // assign the exception value to the catch variable. + Comment cmnt(masm_, "[CatchExtensionObject "); + Load(node->key()); + Load(node->value()); + __ CallRuntime(Runtime::kCreateCatchExtensionObject, 2); + frame_->Push(r0); +} + + void CodeGenerator::VisitAssignment(Assignment* node) { Comment cmnt(masm_, "[ Assignment"); CodeForStatement(node); diff --git a/src/codegen-ia32.cc b/src/codegen-ia32.cc index 119e41f..a9cc800 100644 --- a/src/codegen-ia32.cc +++ b/src/codegen-ia32.cc @@ -2505,7 +2505,7 @@ void CodeGenerator::VisitObjectLiteral(ObjectLiteral* node) { for (int i = 0; i < node->properties()->length(); i++) { - ObjectLiteral::Property* property = node->properties()->at(i); + ObjectLiteral::Property* property = node->properties()->at(i); switch (property->kind()) { case ObjectLiteral::Property::CONSTANT: break; case ObjectLiteral::Property::COMPUTED: { @@ -2607,6 +2607,17 @@ void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) { } +void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) { + // Call runtime routine to allocate the catch extension object and + // assign the exception value to the catch variable. + Comment cmnt(masm_, "[CatchExtensionObject "); + Load(node->key()); + Load(node->value()); + __ CallRuntime(Runtime::kCreateCatchExtensionObject, 2); + frame_->Push(eax); +} + + bool CodeGenerator::IsInlineSmi(Literal* literal) { if (literal == NULL || !literal->handle()->IsSmi()) return false; int int_value = Smi::cast(*literal->handle())->value(); diff --git a/src/parser.cc b/src/parser.cc index 60861fc..7f1bff2 100644 --- a/src/parser.cc +++ b/src/parser.cc @@ -2064,32 +2064,6 @@ Statement* Parser::ParseThrowStatement(bool* ok) { } -Expression* Parser::MakeCatchContext(Handle id, VariableProxy* value) { - ZoneListWrapper properties = - factory()->NewList(1); - Literal* key = NEW(Literal(id)); - ObjectLiteral::Property* property = NEW(ObjectLiteral::Property(key, value)); - properties.Add(property); - - // This must be called always, even during pre-parsing! - // (Computation of literal index must happen before pre-parse bailout.) - int literal_index = temp_scope_->NextMaterializedLiteralIndex(); - if (is_pre_parsing_) { - return NULL; - } - - // Construct the expression for calling Runtime::CreateObjectLiteral - // with the literal array as argument. - Handle constant_properties = Factory::empty_fixed_array(); - ZoneList* arguments = new ZoneList(1); - arguments->Add(new Literal(constant_properties)); - - return new ObjectLiteral(constant_properties, - properties.elements(), - literal_index); -} - - TryStatement* Parser::ParseTryStatement(bool* ok) { // TryStatement :: // 'try' Block Catch @@ -2141,7 +2115,8 @@ TryStatement* Parser::ParseTryStatement(bool* ok) { // Allocate a temporary for holding the finally state while // executing the finally block. catch_var = top_scope_->NewTemporary(Factory::catch_var_symbol()); - Expression* obj = MakeCatchContext(name, catch_var); + Literal* name_literal = NEW(Literal(name)); + Expression* obj = NEW(CatchExtensionObject(name_literal, catch_var)); { Target target(this, &catch_collector); catch_block = WithHelper(obj, NULL, true, CHECK_OK); } @@ -3103,10 +3078,6 @@ Expression* Parser::ParseObjectLiteral(bool* ok) { constant_properties->set(position++, *literal->handle()); } - // Construct the expression for calling Runtime::CreateObjectLiteral - // with the literal array as argument. - ZoneList* arguments = new ZoneList(1); - arguments->Add(new Literal(constant_properties)); return new ObjectLiteral(constant_properties, properties.elements(), literal_index); diff --git a/src/prettyprinter.cc b/src/prettyprinter.cc index a914383..c3bf531 100644 --- a/src/prettyprinter.cc +++ b/src/prettyprinter.cc @@ -285,6 +285,15 @@ void PrettyPrinter::VisitArrayLiteral(ArrayLiteral* node) { } +void PrettyPrinter::VisitCatchExtensionObject(CatchExtensionObject* node) { + Print("{ "); + Visit(node->key()); + Print(": "); + Visit(node->value()); + Print(" }"); +} + + void PrettyPrinter::VisitSlot(Slot* node) { switch (node->type()) { case Slot::PARAMETER: @@ -955,6 +964,13 @@ void AstPrinter::VisitArrayLiteral(ArrayLiteral* node) { } +void AstPrinter::VisitCatchExtensionObject(CatchExtensionObject* node) { + IndentedScope indent("CatchExtensionObject"); + PrintIndentedVisit("KEY", node->key()); + PrintIndentedVisit("VALUE", node->value()); +} + + void AstPrinter::VisitSlot(Slot* node) { PrintIndented("SLOT "); switch (node->type()) { diff --git a/src/rewriter.cc b/src/rewriter.cc index 956d3cb..4468066 100644 --- a/src/rewriter.cc +++ b/src/rewriter.cc @@ -243,6 +243,12 @@ void AstOptimizer::VisitObjectLiteral(ObjectLiteral* node) { } +void AstOptimizer::VisitCatchExtensionObject(CatchExtensionObject* node) { + Visit(node->key()); + Visit(node->value()); +} + + void AstOptimizer::VisitAssignment(Assignment* node) { switch (node->op()) { case Token::INIT_VAR: @@ -684,6 +690,12 @@ void Processor::VisitObjectLiteral(ObjectLiteral* node) { } +void Processor::VisitCatchExtensionObject(CatchExtensionObject* node) { + USE(node); + UNREACHABLE(); +} + + void Processor::VisitAssignment(Assignment* node) { USE(node); UNREACHABLE(); diff --git a/src/runtime.cc b/src/runtime.cc index 25afe28..36fae9d 100644 --- a/src/runtime.cc +++ b/src/runtime.cc @@ -215,6 +215,23 @@ static Object* Runtime_CreateArrayLiteral(Arguments args) { } +static Object* Runtime_CreateCatchExtensionObject(Arguments args) { + ASSERT(args.length() == 2); + CONVERT_CHECKED(String, key, args[0]); + Object* value = args[1]; + // Create a catch context extension object. + JSFunction* constructor = + Top::context()->global_context()->context_extension_function(); + Object* object = Heap::AllocateJSObject(constructor); + if (object->IsFailure()) return object; + // Assign the exception value to the catch variable and make sure + // that the catch variable is DontDelete. + value = JSObject::cast(object)->SetProperty(key, value, DONT_DELETE); + if (value->IsFailure()) return value; + return object; +} + + static Object* Runtime_ClassOf(Arguments args) { NoHandleAllocation ha; ASSERT(args.length() == 1); diff --git a/src/runtime.h b/src/runtime.h index cb8d40b..ebe70a4 100644 --- a/src/runtime.h +++ b/src/runtime.h @@ -253,6 +253,9 @@ namespace v8 { namespace internal { F(CreateObjectLiteralBoilerplate, 3) \ F(CloneObjectLiteralBoilerplate, 1) \ \ + /* Catch context extension objects */ \ + F(CreateCatchExtensionObject, 2) \ + \ /* Statements */ \ F(NewClosure, 2) \ F(NewObject, 1) \ diff --git a/src/usage-analyzer.cc b/src/usage-analyzer.cc index ad26cac..13176f7 100644 --- a/src/usage-analyzer.cc +++ b/src/usage-analyzer.cc @@ -69,6 +69,7 @@ class UsageComputer: public AstVisitor { void VisitRegExpLiteral(RegExpLiteral* node); void VisitObjectLiteral(ObjectLiteral* node); void VisitArrayLiteral(ArrayLiteral* node); + void VisitCatchExtensionObject(CatchExtensionObject* node); void VisitAssignment(Assignment* node); void VisitThrow(Throw* node); void VisitProperty(Property* node); @@ -288,6 +289,11 @@ void UsageComputer::VisitArrayLiteral(ArrayLiteral* node) { } +void UsageComputer::VisitCatchExtensionObject(CatchExtensionObject* node) { + Read(node->value()); +} + + void UsageComputer::VisitAssignment(Assignment* node) { if (node->op() != Token::ASSIGN) Read(node->target()); diff --git a/test/cctest/test-debug.cc b/test/cctest/test-debug.cc index 6c8c28f..816972b 100644 --- a/test/cctest/test-debug.cc +++ b/test/cctest/test-debug.cc @@ -2454,7 +2454,7 @@ TEST(StepWithException) { ChangeBreakOnException(false, true); step_action = StepIn; break_point_hit_count = 0; - expected_step_sequence = "ddedd"; + expected_step_sequence = "dded"; d->Call(env->Global(), 0, NULL); CHECK_EQ(strlen(expected_step_sequence), break_point_hit_count); @@ -2462,7 +2462,7 @@ TEST(StepWithException) { ChangeBreakOnException(true, true); step_action = StepIn; break_point_hit_count = 0; - expected_step_sequence = "ddeedd"; + expected_step_sequence = "ddeed"; d->Call(env->Global(), 0, NULL); CHECK_EQ(strlen(expected_step_sequence), break_point_hit_count); @@ -2472,7 +2472,7 @@ TEST(StepWithException) { ChangeBreakOnException(false, true); step_action = StepIn; break_point_hit_count = 0; - expected_step_sequence = "ffghff"; + expected_step_sequence = "ffghf"; f->Call(env->Global(), 0, NULL); CHECK_EQ(strlen(expected_step_sequence), break_point_hit_count); @@ -2480,7 +2480,7 @@ TEST(StepWithException) { ChangeBreakOnException(true, true); step_action = StepIn; break_point_hit_count = 0; - expected_step_sequence = "ffghhff"; + expected_step_sequence = "ffghhf"; f->Call(env->Global(), 0, NULL); CHECK_EQ(strlen(expected_step_sequence), break_point_hit_count); diff --git a/test/mjsunit/regress/regress-74.js b/test/mjsunit/regress/regress-74.js new file mode 100644 index 0000000..f22b33c --- /dev/null +++ b/test/mjsunit/regress/regress-74.js @@ -0,0 +1,41 @@ +// 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 the variable introduced by catch blocks is DontDelete. +// See http://code.google.com/p/v8/issues/detail?id=74 + +function test() { + try { + throw 42; + } catch(e) { + assertFalse(delete e, "deleting catch variable"); + assertEquals(42, e); + } +} + +test(); + diff --git a/test/mjsunit/try-catch-extension-object.js b/test/mjsunit/try-catch-extension-object.js new file mode 100644 index 0000000..efab821 --- /dev/null +++ b/test/mjsunit/try-catch-extension-object.js @@ -0,0 +1,58 @@ +// 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. + +// Try catch scopes should be implemented with special extension +// objects so that __proto__ accessors and accessor setters in the +// Object prototype have no effect. + +var setterCalled = false; +Object.prototype.__defineSetter__("x", function() { setterCalled = true; }); + +function runTest(test) { + setterCalled = false; + test(); +} + +function testProto() { + try { + throw 42; + } catch(__proto__) { + assertEquals(42, __proto__); + } +} + +function testAccessorSetter() { + try { + throw 42; + } catch(x) { + assertFalse(setterCalled); + assertEquals(42, x); + } +} + +runTest(testProto); +runTest(testAccessorSetter);