OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 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 1327 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1338 " b = function() { \n" | 1338 " b = function() { \n" |
1339 " 01; \n" | 1339 " 01; \n" |
1340 " }; \n" | 1340 " }; \n" |
1341 "}; \n"; | 1341 "}; \n"; |
1342 v8::Script::Compile(v8::String::NewFromUtf8(CcTest::isolate(), script)); | 1342 v8::Script::Compile(v8::String::NewFromUtf8(CcTest::isolate(), script)); |
1343 CHECK(try_catch.HasCaught()); | 1343 CHECK(try_catch.HasCaught()); |
1344 v8::String::Utf8Value exception(try_catch.Exception()); | 1344 v8::String::Utf8Value exception(try_catch.Exception()); |
1345 CHECK_EQ("SyntaxError: Octal literals are not allowed in strict mode.", | 1345 CHECK_EQ("SyntaxError: Octal literals are not allowed in strict mode.", |
1346 *exception); | 1346 *exception); |
1347 } | 1347 } |
| 1348 |
| 1349 |
| 1350 void VerifyPreParseAndParseNoError(v8::Handle<v8::String> source) { |
| 1351 v8::ScriptData* preparse = v8::ScriptData::PreCompile(source); |
| 1352 CHECK(!preparse->HasError()); |
| 1353 |
| 1354 v8::TryCatch try_catch; |
| 1355 v8::Script::Compile(source); |
| 1356 CHECK(!try_catch.HasCaught()); |
| 1357 } |
| 1358 |
| 1359 |
| 1360 void VerifyPreParseAndParseErrorMessages(v8::Handle<v8::String> source, |
| 1361 int error_location_beg, |
| 1362 int error_location_end, |
| 1363 const char* preparse_error_message, |
| 1364 const char* parse_error_message) { |
| 1365 v8::ScriptData* preparse = v8::ScriptData::PreCompile(source); |
| 1366 CHECK(preparse->HasError()); |
| 1367 i::ScriptDataImpl* pre_impl = |
| 1368 reinterpret_cast<i::ScriptDataImpl*>(preparse); |
| 1369 i::Scanner::Location error_location = pre_impl->MessageLocation(); |
| 1370 const char* message = pre_impl->BuildMessage(); |
| 1371 CHECK_EQ(0, strcmp(preparse_error_message, message)); |
| 1372 CHECK_EQ(error_location_beg, error_location.beg_pos); |
| 1373 CHECK_EQ(error_location_end, error_location.end_pos); |
| 1374 |
| 1375 v8::TryCatch try_catch; |
| 1376 v8::Script::Compile(source); |
| 1377 CHECK(try_catch.HasCaught()); |
| 1378 v8::String::Utf8Value exception(try_catch.Exception()); |
| 1379 CHECK_EQ(parse_error_message, *exception); |
| 1380 CHECK_EQ(error_location_beg, try_catch.Message()->GetStartPosition()); |
| 1381 CHECK_EQ(error_location_end, try_catch.Message()->GetEndPosition()); |
| 1382 } |
| 1383 |
| 1384 |
| 1385 TEST(ErrorsEvalAndArguments) { |
| 1386 // Tests that both preparsing and parsing produce the right kind of errors for |
| 1387 // using "eval" and "arguments" as identifiers. Without the strict mode, it's |
| 1388 // ok to use "eval" or "arguments" as identifiers. With the strict mode, it |
| 1389 // isn't. |
| 1390 v8::Isolate* isolate = CcTest::isolate(); |
| 1391 v8::HandleScope handles(isolate); |
| 1392 v8::Local<v8::Context> context = v8::Context::New(isolate); |
| 1393 v8::Context::Scope context_scope(context); |
| 1394 |
| 1395 const char* use_strict_prefix = "\"use strict\";\n"; |
| 1396 int prefix_length = i::StrLength(use_strict_prefix); |
| 1397 |
| 1398 const char* strict_var_name_preparse = "strict_var_name"; |
| 1399 const char* strict_var_name_parse = |
| 1400 "SyntaxError: Variable name may not be eval or arguments in strict mode"; |
| 1401 |
| 1402 const char* strict_catch_variable_preparse = "strict_catch_variable"; |
| 1403 const char* strict_catch_variable_parse = |
| 1404 "SyntaxError: Catch variable may not be eval or arguments in strict mode"; |
| 1405 |
| 1406 const char* strict_function_name_preparse = "strict_function_name"; |
| 1407 const char* strict_function_name_parse = |
| 1408 "SyntaxError: Function name may not be eval or arguments in strict mode"; |
| 1409 |
| 1410 const char* strict_param_name_preparse = "strict_param_name"; |
| 1411 const char* strict_param_name_parse = |
| 1412 "SyntaxError: Parameter name eval or arguments is not allowed in strict " |
| 1413 "mode"; |
| 1414 |
| 1415 const char* strict_lhs_assignment_preparse = "strict_lhs_assignment"; |
| 1416 const char* strict_lhs_assignment_parse = |
| 1417 "SyntaxError: Assignment to eval or arguments is not allowed in strict " |
| 1418 "mode"; |
| 1419 |
| 1420 const char* strict_lhs_prefix_preparse = "strict_lhs_prefix"; |
| 1421 const char* strict_lhs_prefix_parse = |
| 1422 "SyntaxError: Prefix increment/decrement may not have eval or arguments " |
| 1423 "operand in strict mode"; |
| 1424 |
| 1425 const char* strict_lhs_postfix_preparse = "strict_lhs_postfix"; |
| 1426 const char* strict_lhs_postfix_parse = |
| 1427 "SyntaxError: Postfix increment/decrement may not have eval or arguments " |
| 1428 "operand in strict mode"; |
| 1429 |
| 1430 struct TestCase { |
| 1431 const char* source; |
| 1432 int error_location_beg; |
| 1433 int error_location_end; |
| 1434 const char* preparse_error_message; |
| 1435 const char* parse_error_message; |
| 1436 } test_cases[] = { |
| 1437 {"var eval = 42;", 4, 8, strict_var_name_preparse, strict_var_name_parse}, |
| 1438 {"var arguments = 42;", 4, 13, strict_var_name_preparse, |
| 1439 strict_var_name_parse}, |
| 1440 {"var foo, eval;", 9, 13, strict_var_name_preparse, strict_var_name_parse}, |
| 1441 {"var foo, arguments;", 9, 18, strict_var_name_preparse, |
| 1442 strict_var_name_parse}, |
| 1443 {"try { } catch (eval) { }", 15, 19, strict_catch_variable_preparse, |
| 1444 strict_catch_variable_parse}, |
| 1445 {"try { } catch (arguments) { }", 15, 24, strict_catch_variable_preparse, |
| 1446 strict_catch_variable_parse}, |
| 1447 {"function eval() { }", 9, 13, strict_function_name_preparse, |
| 1448 strict_function_name_parse}, |
| 1449 {"function arguments() { }", 9, 18, strict_function_name_preparse, |
| 1450 strict_function_name_parse}, |
| 1451 {"function foo(eval) { }", 13, 17, strict_param_name_preparse, |
| 1452 strict_param_name_parse}, |
| 1453 {"function foo(arguments) { }", 13, 22, strict_param_name_preparse, |
| 1454 strict_param_name_parse}, |
| 1455 {"function foo(bar, eval) { }", 18, 22, strict_param_name_preparse, |
| 1456 strict_param_name_parse}, |
| 1457 {"function foo(bar, arguments) { }", 18, 27, strict_param_name_preparse, |
| 1458 strict_param_name_parse}, |
| 1459 {"eval = 1;", 0, 4, strict_lhs_assignment_preparse, |
| 1460 strict_lhs_assignment_parse}, |
| 1461 {"arguments = 1;", 0, 9, strict_lhs_assignment_preparse, |
| 1462 strict_lhs_assignment_parse}, |
| 1463 {"++eval;", 2, 6, strict_lhs_prefix_preparse, strict_lhs_prefix_parse}, |
| 1464 {"++arguments;", 2, 11, strict_lhs_prefix_preparse, |
| 1465 strict_lhs_prefix_parse}, |
| 1466 {"eval++;", 0, 4, strict_lhs_postfix_preparse, strict_lhs_postfix_parse}, |
| 1467 {"arguments++;", 0, 9, strict_lhs_postfix_preparse, |
| 1468 strict_lhs_postfix_parse}, |
| 1469 {NULL, 0, 0, NULL, NULL} |
| 1470 }; |
| 1471 |
| 1472 for (int i = 0; test_cases[i].source; ++i) { |
| 1473 v8::Handle<v8::String> source = |
| 1474 v8::String::NewFromUtf8(isolate, test_cases[i].source); |
| 1475 |
| 1476 VerifyPreParseAndParseNoError(source); |
| 1477 |
| 1478 v8::Handle<v8::String> strict_source = v8::String::Concat( |
| 1479 v8::String::NewFromUtf8(isolate, use_strict_prefix), |
| 1480 v8::String::NewFromUtf8(isolate, test_cases[i].source)); |
| 1481 |
| 1482 VerifyPreParseAndParseErrorMessages( |
| 1483 strict_source, |
| 1484 test_cases[i].error_location_beg + prefix_length, |
| 1485 test_cases[i].error_location_end + prefix_length, |
| 1486 test_cases[i].preparse_error_message, |
| 1487 test_cases[i].parse_error_message); |
| 1488 } |
| 1489 } |
OLD | NEW |