Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(287)

Side by Side Diff: src/parser.cc

Issue 7309002: Fix a bug in with and catch context allocation. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 9 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/parser.h ('k') | src/scopes.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2011 the V8 project authors. All rights reserved. 1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 394 matching lines...) Expand 10 before | Expand all | Expand 10 after
405 } 405 }
406 406
407 407
408 Scope* Parser::NewScope(Scope* parent, Scope::Type type, bool inside_with) { 408 Scope* Parser::NewScope(Scope* parent, Scope::Type type, bool inside_with) {
409 Scope* result = new(zone()) Scope(parent, type); 409 Scope* result = new(zone()) Scope(parent, type);
410 result->Initialize(inside_with); 410 result->Initialize(inside_with);
411 return result; 411 return result;
412 } 412 }
413 413
414 414
415 Scope* Parser::DeclarationScope() {
416 Scope* scope = top_scope_;
417 while (scope->is_catch_scope()) {
418 scope = scope->outer_scope();
419 }
420 return scope;
421 }
422
423 // ---------------------------------------------------------------------------- 415 // ----------------------------------------------------------------------------
424 // Target is a support class to facilitate manipulation of the 416 // Target is a support class to facilitate manipulation of the
425 // Parser's target_stack_ (the stack of potential 'break' and 417 // Parser's target_stack_ (the stack of potential 'break' and
426 // 'continue' statement targets). Upon construction, a new target is 418 // 'continue' statement targets). Upon construction, a new target is
427 // added; it is removed upon destruction. 419 // added; it is removed upon destruction.
428 420
429 class Target BASE_EMBEDDED { 421 class Target BASE_EMBEDDED {
430 public: 422 public:
431 Target(Target** variable, AstNode* node) 423 Target(Target** variable, AstNode* node)
432 : variable_(variable), node_(node), previous_(*variable) { 424 : variable_(variable), node_(node), previous_(*variable) {
(...skipping 870 matching lines...) Expand 10 before | Expand all | Expand 10 after
1303 // is always the function scope. 1295 // is always the function scope.
1304 1296
1305 // If a function scope exists, then we can statically declare this 1297 // If a function scope exists, then we can statically declare this
1306 // variable and also set its mode. In any case, a Declaration node 1298 // variable and also set its mode. In any case, a Declaration node
1307 // will be added to the scope so that the declaration can be added 1299 // will be added to the scope so that the declaration can be added
1308 // to the corresponding activation frame at runtime if necessary. 1300 // to the corresponding activation frame at runtime if necessary.
1309 // For instance declarations inside an eval scope need to be added 1301 // For instance declarations inside an eval scope need to be added
1310 // to the calling function context. 1302 // to the calling function context.
1311 // Similarly, strict mode eval scope does not leak variable declarations to 1303 // Similarly, strict mode eval scope does not leak variable declarations to
1312 // the caller's scope so we declare all locals, too. 1304 // the caller's scope so we declare all locals, too.
1313 Scope* declaration_scope = DeclarationScope(); 1305 Scope* declaration_scope = top_scope_->DeclarationScope();
1314 if (declaration_scope->is_function_scope() || 1306 if (declaration_scope->is_function_scope() ||
1315 declaration_scope->is_strict_mode_eval_scope()) { 1307 declaration_scope->is_strict_mode_eval_scope()) {
1316 // Declare the variable in the function scope. 1308 // Declare the variable in the function scope.
1317 var = declaration_scope->LocalLookup(name); 1309 var = declaration_scope->LocalLookup(name);
1318 if (var == NULL) { 1310 if (var == NULL) {
1319 // Declare the name. 1311 // Declare the name.
1320 var = declaration_scope->DeclareLocal(name, mode); 1312 var = declaration_scope->DeclareLocal(name, mode);
1321 } else { 1313 } else {
1322 // The name was declared before; check for conflicting 1314 // The name was declared before; check for conflicting
1323 // re-declarations. If the previous declaration was a const or the 1315 // re-declarations. If the previous declaration was a const or the
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
1414 Expect(Token::COMMA, CHECK_OK); 1406 Expect(Token::COMMA, CHECK_OK);
1415 } 1407 }
1416 } 1408 }
1417 Expect(Token::RPAREN, CHECK_OK); 1409 Expect(Token::RPAREN, CHECK_OK);
1418 Expect(Token::SEMICOLON, CHECK_OK); 1410 Expect(Token::SEMICOLON, CHECK_OK);
1419 1411
1420 // Make sure that the function containing the native declaration 1412 // Make sure that the function containing the native declaration
1421 // isn't lazily compiled. The extension structures are only 1413 // isn't lazily compiled. The extension structures are only
1422 // accessible while parsing the first time not when reparsing 1414 // accessible while parsing the first time not when reparsing
1423 // because of lazy compilation. 1415 // because of lazy compilation.
1424 DeclarationScope()->ForceEagerCompilation(); 1416 top_scope_->DeclarationScope()->ForceEagerCompilation();
1425 1417
1426 // Compute the function template for the native function. 1418 // Compute the function template for the native function.
1427 v8::Handle<v8::FunctionTemplate> fun_template = 1419 v8::Handle<v8::FunctionTemplate> fun_template =
1428 extension_->GetNativeFunction(v8::Utils::ToLocal(name)); 1420 extension_->GetNativeFunction(v8::Utils::ToLocal(name));
1429 ASSERT(!fun_template.IsEmpty()); 1421 ASSERT(!fun_template.IsEmpty());
1430 1422
1431 // Instantiate the function and create a shared function info from it. 1423 // Instantiate the function and create a shared function info from it.
1432 Handle<JSFunction> fun = Utils::OpenHandle(*fun_template->GetFunction()); 1424 Handle<JSFunction> fun = Utils::OpenHandle(*fun_template->GetFunction());
1433 const int literals = fun->NumberOfLiterals(); 1425 const int literals = fun->NumberOfLiterals();
1434 Handle<Code> code = Handle<Code>(fun->shared()->code()); 1426 Handle<Code> code = Handle<Code>(fun->shared()->code());
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
1518 // to initialize it properly. This mechanism is used for the parsing 1510 // to initialize it properly. This mechanism is used for the parsing
1519 // of 'for-in' loops. 1511 // of 'for-in' loops.
1520 Block* Parser::ParseVariableDeclarations(bool accept_IN, 1512 Block* Parser::ParseVariableDeclarations(bool accept_IN,
1521 Handle<String>* out, 1513 Handle<String>* out,
1522 bool* ok) { 1514 bool* ok) {
1523 // VariableDeclarations :: 1515 // VariableDeclarations ::
1524 // ('var' | 'const') (Identifier ('=' AssignmentExpression)?)+[','] 1516 // ('var' | 'const') (Identifier ('=' AssignmentExpression)?)+[',']
1525 1517
1526 Variable::Mode mode = Variable::VAR; 1518 Variable::Mode mode = Variable::VAR;
1527 bool is_const = false; 1519 bool is_const = false;
1528 Scope* declaration_scope = DeclarationScope(); 1520 Scope* declaration_scope = top_scope_->DeclarationScope();
1529 if (peek() == Token::VAR) { 1521 if (peek() == Token::VAR) {
1530 Consume(Token::VAR); 1522 Consume(Token::VAR);
1531 } else if (peek() == Token::CONST) { 1523 } else if (peek() == Token::CONST) {
1532 Consume(Token::CONST); 1524 Consume(Token::CONST);
1533 if (declaration_scope->is_strict_mode()) { 1525 if (declaration_scope->is_strict_mode()) {
1534 ReportMessage("strict_const", Vector<const char*>::empty()); 1526 ReportMessage("strict_const", Vector<const char*>::empty());
1535 *ok = false; 1527 *ok = false;
1536 return NULL; 1528 return NULL;
1537 } 1529 }
1538 mode = Variable::CONST; 1530 mode = Variable::CONST;
(...skipping 369 matching lines...) Expand 10 before | Expand all | Expand 10 after
1908 // Consume the return token. It is necessary to do the before 1900 // Consume the return token. It is necessary to do the before
1909 // reporting any errors on it, because of the way errors are 1901 // reporting any errors on it, because of the way errors are
1910 // reported (underlining). 1902 // reported (underlining).
1911 Expect(Token::RETURN, CHECK_OK); 1903 Expect(Token::RETURN, CHECK_OK);
1912 1904
1913 // An ECMAScript program is considered syntactically incorrect if it 1905 // An ECMAScript program is considered syntactically incorrect if it
1914 // contains a return statement that is not within the body of a 1906 // contains a return statement that is not within the body of a
1915 // function. See ECMA-262, section 12.9, page 67. 1907 // function. See ECMA-262, section 12.9, page 67.
1916 // 1908 //
1917 // To be consistent with KJS we report the syntax error at runtime. 1909 // To be consistent with KJS we report the syntax error at runtime.
1918 Scope* declaration_scope = DeclarationScope(); 1910 Scope* declaration_scope = top_scope_->DeclarationScope();
1919 if (declaration_scope->is_global_scope() || 1911 if (declaration_scope->is_global_scope() ||
1920 declaration_scope->is_eval_scope()) { 1912 declaration_scope->is_eval_scope()) {
1921 Handle<String> type = isolate()->factory()->illegal_return_symbol(); 1913 Handle<String> type = isolate()->factory()->illegal_return_symbol();
1922 Expression* throw_error = NewThrowSyntaxError(type, Handle<Object>::null()); 1914 Expression* throw_error = NewThrowSyntaxError(type, Handle<Object>::null());
1923 return new(zone()) ExpressionStatement(throw_error); 1915 return new(zone()) ExpressionStatement(throw_error);
1924 } 1916 }
1925 1917
1926 Token::Value tok = peek(); 1918 Token::Value tok = peek();
1927 if (scanner().HasAnyLineTerminatorBeforeNext() || 1919 if (scanner().HasAnyLineTerminatorBeforeNext() ||
1928 tok == Token::SEMICOLON || 1920 tok == Token::SEMICOLON ||
1929 tok == Token::RBRACE || 1921 tok == Token::RBRACE ||
1930 tok == Token::EOS) { 1922 tok == Token::EOS) {
1931 ExpectSemicolon(CHECK_OK); 1923 ExpectSemicolon(CHECK_OK);
1932 return new(zone()) ReturnStatement(GetLiteralUndefined()); 1924 return new(zone()) ReturnStatement(GetLiteralUndefined());
1933 } 1925 }
1934 1926
1935 Expression* expr = ParseExpression(true, CHECK_OK); 1927 Expression* expr = ParseExpression(true, CHECK_OK);
1936 ExpectSemicolon(CHECK_OK); 1928 ExpectSemicolon(CHECK_OK);
1937 return new(zone()) ReturnStatement(expr); 1929 return new(zone()) ReturnStatement(expr);
1938 } 1930 }
1939 1931
1940 1932
1941 Block* Parser::WithHelper(Expression* obj, ZoneStringList* labels, bool* ok) { 1933 Block* Parser::WithHelper(Expression* obj, ZoneStringList* labels, bool* ok) {
1942 // Parse the statement and collect escaping labels. 1934 // Parse the statement and collect escaping labels.
1943 TargetCollector collector; 1935 TargetCollector collector;
1944 Statement* stat; 1936 Statement* stat;
1945 { Target target(&this->target_stack_, &collector); 1937 { Target target(&this->target_stack_, &collector);
1946 with_nesting_level_++; 1938 with_nesting_level_++;
1947 DeclarationScope()->RecordWithStatement(); 1939 top_scope_->DeclarationScope()->RecordWithStatement();
1948 stat = ParseStatement(labels, CHECK_OK); 1940 stat = ParseStatement(labels, CHECK_OK);
1949 with_nesting_level_--; 1941 with_nesting_level_--;
1950 } 1942 }
1951 // Create resulting block with two statements. 1943 // Create resulting block with two statements.
1952 // 1: Evaluate the with expression. 1944 // 1: Evaluate the with expression.
1953 // 2: The try-finally block evaluating the body. 1945 // 2: The try-finally block evaluating the body.
1954 Block* result = new(zone()) Block(NULL, 2, false); 1946 Block* result = new(zone()) Block(NULL, 2, false);
1955 1947
1956 if (result != NULL) { 1948 if (result != NULL) {
1957 result->AddStatement(new(zone()) EnterWithContextStatement(obj)); 1949 result->AddStatement(new(zone()) EnterWithContextStatement(obj));
(...skipping 1837 matching lines...) Expand 10 before | Expand all | Expand 10 after
3795 // CallRuntime :: 3787 // CallRuntime ::
3796 // '%' Identifier Arguments 3788 // '%' Identifier Arguments
3797 3789
3798 Expect(Token::MOD, CHECK_OK); 3790 Expect(Token::MOD, CHECK_OK);
3799 Handle<String> name = ParseIdentifier(CHECK_OK); 3791 Handle<String> name = ParseIdentifier(CHECK_OK);
3800 ZoneList<Expression*>* args = ParseArguments(CHECK_OK); 3792 ZoneList<Expression*>* args = ParseArguments(CHECK_OK);
3801 3793
3802 if (extension_ != NULL) { 3794 if (extension_ != NULL) {
3803 // The extension structures are only accessible while parsing the 3795 // The extension structures are only accessible while parsing the
3804 // very first time not when reparsing because of lazy compilation. 3796 // very first time not when reparsing because of lazy compilation.
3805 DeclarationScope()->ForceEagerCompilation(); 3797 top_scope_->DeclarationScope()->ForceEagerCompilation();
3806 } 3798 }
3807 3799
3808 const Runtime::Function* function = Runtime::FunctionForSymbol(name); 3800 const Runtime::Function* function = Runtime::FunctionForSymbol(name);
3809 3801
3810 // Check for built-in IS_VAR macro. 3802 // Check for built-in IS_VAR macro.
3811 if (function != NULL && 3803 if (function != NULL &&
3812 function->intrinsic_type == Runtime::RUNTIME && 3804 function->intrinsic_type == Runtime::RUNTIME &&
3813 function->function_id == Runtime::kIS_VAR) { 3805 function->function_id == Runtime::kIS_VAR) {
3814 // %IS_VAR(x) evaluates to x if x is a variable, 3806 // %IS_VAR(x) evaluates to x if x is a variable,
3815 // leads to a parse error otherwise. Could be implemented as an 3807 // leads to a parse error otherwise. Could be implemented as an
(...skipping 1259 matching lines...) Expand 10 before | Expand all | Expand 10 after
5075 info->is_global(), 5067 info->is_global(),
5076 info->StrictMode()); 5068 info->StrictMode());
5077 } 5069 }
5078 } 5070 }
5079 5071
5080 info->SetFunction(result); 5072 info->SetFunction(result);
5081 return (result != NULL); 5073 return (result != NULL);
5082 } 5074 }
5083 5075
5084 } } // namespace v8::internal 5076 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/parser.h ('k') | src/scopes.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698