Add duplicate parameter detection to preparser.

Add tests for duplicate properties of object initialisers to preparser.

TEST=preparser

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

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@8517 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
This commit is contained in:
lrn@chromium.org 2011-07-01 13:46:52 +00:00
parent 8448d09d3b
commit b6779044c9
7 changed files with 530 additions and 48 deletions

View File

@ -32,6 +32,7 @@
#include "allocation.h"
#include "utils.h"
#include "list.h"
#include "hashmap.h"
#include "scanner-base.h"
#include "preparse-data-format.h"
#include "preparse-data.h"

View File

@ -25,6 +25,9 @@
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <errno.h>
#include <math.h>
#include "../include/v8stdint.h"
#include "unicode.h"
#include "globals.h"
@ -32,6 +35,7 @@
#include "allocation.h"
#include "utils.h"
#include "list.h"
#include "hashmap.h"
#include "scanner-base.h"
#include "preparse-data-format.h"
@ -68,27 +72,22 @@ void PreParser::ReportUnexpectedToken(i::Token::Value token) {
// Four of the tokens are treated specially
switch (token) {
case i::Token::EOS:
return ReportMessageAt(source_location.beg_pos, source_location.end_pos,
"unexpected_eos", NULL);
return ReportMessageAt(source_location, "unexpected_eos", NULL);
case i::Token::NUMBER:
return ReportMessageAt(source_location.beg_pos, source_location.end_pos,
"unexpected_token_number", NULL);
return ReportMessageAt(source_location, "unexpected_token_number", NULL);
case i::Token::STRING:
return ReportMessageAt(source_location.beg_pos, source_location.end_pos,
"unexpected_token_string", NULL);
return ReportMessageAt(source_location, "unexpected_token_string", NULL);
case i::Token::IDENTIFIER:
return ReportMessageAt(source_location.beg_pos, source_location.end_pos,
return ReportMessageAt(source_location,
"unexpected_token_identifier", NULL);
case i::Token::FUTURE_RESERVED_WORD:
return ReportMessageAt(source_location.beg_pos, source_location.end_pos,
"unexpected_reserved", NULL);
return ReportMessageAt(source_location, "unexpected_reserved", NULL);
case i::Token::FUTURE_STRICT_RESERVED_WORD:
return ReportMessageAt(source_location.beg_pos, source_location.end_pos,
return ReportMessageAt(source_location,
"unexpected_strict_reserved", NULL);
default:
const char* name = i::Token::String(token);
ReportMessageAt(source_location.beg_pos, source_location.end_pos,
"unexpected_token", name);
ReportMessageAt(source_location, "unexpected_token", name);
}
}
@ -98,7 +97,7 @@ void PreParser::ReportUnexpectedToken(i::Token::Value token) {
void PreParser::CheckOctalLiteral(int beg_pos, int end_pos, bool* ok) {
i::Scanner::Location octal = scanner_->octal_position();
if (beg_pos <= octal.beg_pos && octal.end_pos <= end_pos) {
ReportMessageAt(octal.beg_pos, octal.end_pos, "strict_octal_literal", NULL);
ReportMessageAt(octal, "strict_octal_literal", NULL);
scanner_->clear_octal_position();
*ok = false;
}
@ -241,7 +240,7 @@ PreParser::Statement PreParser::ParseFunctionDeclaration(bool* ok) {
if (identifier.IsFutureStrictReserved()) {
type = "strict_reserved_word";
}
ReportMessageAt(location.beg_pos, location.end_pos, type, NULL);
ReportMessageAt(location, type, NULL);
*ok = false;
}
return Statement::FunctionDeclaration();
@ -298,8 +297,7 @@ PreParser::Statement PreParser::ParseVariableDeclarations(bool accept_IN,
} else if (peek() == i::Token::CONST) {
if (strict_mode()) {
i::Scanner::Location location = scanner_->peek_location();
ReportMessageAt(location.beg_pos, location.end_pos,
"strict_const", NULL);
ReportMessageAt(location, "strict_const", NULL);
*ok = false;
return Statement::Default();
}
@ -448,8 +446,7 @@ PreParser::Statement PreParser::ParseWithStatement(bool* ok) {
Expect(i::Token::WITH, CHECK_OK);
if (strict_mode()) {
i::Scanner::Location location = scanner_->location();
ReportMessageAt(location.beg_pos, location.end_pos,
"strict_mode_with", NULL);
ReportMessageAt(location, "strict_mode_with", NULL);
*ok = false;
return Statement::Default();
}
@ -584,8 +581,7 @@ PreParser::Statement PreParser::ParseThrowStatement(bool* ok) {
Expect(i::Token::THROW, CHECK_OK);
if (scanner_->HasAnyLineTerminatorBeforeNext()) {
i::JavaScriptScanner::Location pos = scanner_->location();
ReportMessageAt(pos.beg_pos, pos.end_pos,
"newline_after_throw", NULL);
ReportMessageAt(pos, "newline_after_throw", NULL);
*ok = false;
return Statement::Default();
}
@ -997,8 +993,7 @@ PreParser::Expression PreParser::ParsePrimaryExpression(bool* ok) {
if (strict_mode()) {
Next();
i::Scanner::Location location = scanner_->location();
ReportMessageAt(location.beg_pos, location.end_pos,
"strict_reserved_word", NULL);
ReportMessageAt(location, "strict_reserved_word", NULL);
*ok = false;
return Expression::Default();
}
@ -1079,6 +1074,39 @@ PreParser::Expression PreParser::ParseArrayLiteral(bool* ok) {
return Expression::Default();
}
void PreParser::CheckDuplicate(DuplicateFinder* finder,
i::Token::Value property,
int type,
bool* ok) {
int old_value;
if (property == i::Token::NUMBER) {
old_value = finder->AddNumber(scanner_->literal_ascii_string(), type);
} else if (scanner_->is_literal_ascii()) {
old_value = finder->AddAsciiSymbol(scanner_->literal_ascii_string(),
type);
} else {
old_value = finder->AddUC16Symbol(scanner_->literal_uc16_string(), type);
}
int intersect = old_value & type;
if (intersect != 0) {
if ((intersect & kValueFlag) != 0) {
// Both are data properties.
if (!strict_mode()) return;
ReportMessageAt(scanner_->location(),
"strict_duplicate_property", NULL);
} else if (((old_value ^ type) & kValueFlag) != 0) {
// Both a data and an accessor property with the same name.
ReportMessageAt(scanner_->location(),
"accessor_data_property", NULL);
} else {
// Both accessors of the same type.
ReportMessageAt(scanner_->location(),
"accessor_get_set", NULL);
}
*ok = false;
}
}
PreParser::Expression PreParser::ParseObjectLiteral(bool* ok) {
// ObjectLiteral ::
@ -1088,6 +1116,7 @@ PreParser::Expression PreParser::ParseObjectLiteral(bool* ok) {
// )*[','] '}'
Expect(i::Token::LBRACE, CHECK_OK);
DuplicateFinder duplicate_finder;
while (peek() != i::Token::RBRACE) {
i::Token::Value next = peek();
switch (next) {
@ -1112,24 +1141,30 @@ PreParser::Expression PreParser::ParseObjectLiteral(bool* ok) {
if (!is_keyword) {
LogSymbol();
}
int type = is_getter ? kGetterProperty : kSetterProperty;
CheckDuplicate(&duplicate_finder, name, type, CHECK_OK);
ParseFunctionLiteral(CHECK_OK);
if (peek() != i::Token::RBRACE) {
Expect(i::Token::COMMA, CHECK_OK);
}
continue; // restart the while
}
CheckDuplicate(&duplicate_finder, next, kValueProperty, CHECK_OK);
break;
}
case i::Token::STRING:
Consume(next);
CheckDuplicate(&duplicate_finder, next, kValueProperty, CHECK_OK);
GetStringSymbol();
break;
case i::Token::NUMBER:
Consume(next);
CheckDuplicate(&duplicate_finder, next, kValueProperty, CHECK_OK);
break;
default:
if (i::Token::IsKeyword(next)) {
Consume(next);
CheckDuplicate(&duplicate_finder, next, kValueProperty, CHECK_OK);
} else {
// Unexpected token.
*ok = false;
@ -1154,9 +1189,7 @@ PreParser::Expression PreParser::ParseRegExpLiteral(bool seen_equal,
bool* ok) {
if (!scanner_->ScanRegExpPattern(seen_equal)) {
Next();
i::JavaScriptScanner::Location location = scanner_->location();
ReportMessageAt(location.beg_pos, location.end_pos,
"unterminated_regexp", NULL);
ReportMessageAt(scanner_->location(), "unterminated_regexp", NULL);
*ok = false;
return Expression::Default();
}
@ -1165,9 +1198,7 @@ PreParser::Expression PreParser::ParseRegExpLiteral(bool seen_equal,
if (!scanner_->ScanRegExpFlags()) {
Next();
i::JavaScriptScanner::Location location = scanner_->location();
ReportMessageAt(location.beg_pos, location.end_pos,
"invalid_regexp_flags", NULL);
ReportMessageAt(scanner_->location(), "invalid_regexp_flags", NULL);
*ok = false;
return Expression::Default();
}
@ -1212,6 +1243,7 @@ PreParser::Expression PreParser::ParseFunctionLiteral(bool* ok) {
Expect(i::Token::LPAREN, CHECK_OK);
int start_position = scanner_->location().beg_pos;
bool done = (peek() == i::Token::RPAREN);
DuplicateFinder duplicate_finder;
while (!done) {
Identifier id = ParseIdentifier(CHECK_OK);
if (!id.IsValidStrictVariable()) {
@ -1220,6 +1252,20 @@ PreParser::Expression PreParser::ParseFunctionLiteral(bool* ok) {
id,
CHECK_OK);
}
int prev_value;
if (scanner_->is_literal_ascii()) {
prev_value =
duplicate_finder.AddAsciiSymbol(scanner_->literal_ascii_string(), 1);
} else {
prev_value =
duplicate_finder.AddUC16Symbol(scanner_->literal_uc16_string(), 1);
}
if (prev_value != 0) {
SetStrictModeViolation(scanner_->location(),
"strict_param_dupe",
CHECK_OK);
}
done = (peek() == i::Token::RPAREN);
if (!done) {
Expect(i::Token::COMMA, CHECK_OK);
@ -1371,13 +1417,18 @@ void PreParser::SetStrictModeViolation(i::Scanner::Location location,
const char* type,
bool* ok) {
if (strict_mode()) {
ReportMessageAt(location.beg_pos, location.end_pos, type, NULL);
ReportMessageAt(location, type, NULL);
*ok = false;
return;
}
// Delay report in case this later turns out to be strict code
// (i.e., for function names and parameters prior to a "use strict"
// directive).
// It's safe to overwrite an existing violation.
// It's either from a function that turned out to be non-strict,
// or it's in the current function (and we just need to report
// one error), or it's in a unclosed nesting function that wasn't
// strict (otherwise we would already be in strict mode).
strict_mode_violation_location_ = location;
strict_mode_violation_type_ = type;
}
@ -1389,11 +1440,9 @@ void PreParser::CheckDelayedStrictModeViolation(int beg_pos,
i::Scanner::Location location = strict_mode_violation_location_;
if (location.IsValid() &&
location.beg_pos > beg_pos && location.end_pos < end_pos) {
ReportMessageAt(location.beg_pos, location.end_pos,
strict_mode_violation_type_, NULL);
ReportMessageAt(location, strict_mode_violation_type_, NULL);
*ok = false;
}
strict_mode_violation_location_ = i::Scanner::Location::invalid();
}
@ -1408,7 +1457,7 @@ void PreParser::StrictModeIdentifierViolation(i::Scanner::Location location,
type = "strict_reserved_word";
}
if (strict_mode()) {
ReportMessageAt(location.beg_pos, location.end_pos, type, NULL);
ReportMessageAt(location, type, NULL);
*ok = false;
return;
}
@ -1460,4 +1509,141 @@ bool PreParser::peek_any_identifier() {
next == i::Token::FUTURE_RESERVED_WORD ||
next == i::Token::FUTURE_STRICT_RESERVED_WORD;
}
int DuplicateFinder::AddAsciiSymbol(i::Vector<const char> key, int value) {
return AddSymbol(i::Vector<const byte>::cast(key), true, value);
}
int DuplicateFinder::AddUC16Symbol(i::Vector<const uint16_t> key, int value) {
return AddSymbol(i::Vector<const byte>::cast(key), false, value);
}
int DuplicateFinder::AddSymbol(i::Vector<const byte> key,
bool is_ascii,
int value) {
uint32_t hash = Hash(key, is_ascii);
byte* encoding = BackupKey(key, is_ascii);
i::HashMap::Entry* entry = map_->Lookup(encoding, hash, true);
int old_value = static_cast<int>(reinterpret_cast<intptr_t>(entry->value));
entry->value =
reinterpret_cast<void*>(static_cast<intptr_t>(value | old_value));
return old_value;
}
int DuplicateFinder::AddNumber(i::Vector<const char> key, int value) {
// Quick check for already being in canonical form.
if (IsNumberCanonical(key)) {
return AddAsciiSymbol(key, value);
}
// TODO(lrn): Use correct string->number->string conversions that
// generate the same string as v8's ToString(ToNumber(literal)).
// Current solution doesn't handle octal, and probably doesn't
// generate the same string in edge cases.
double double_value = 0.0;
double_value = strtod(key.start(), NULL);
int length;
if (errno == ERANGE && double_value == HUGE_VAL) {
// Overflow.
// Negative overflow (-HUGE_VAL) cannit happen as number literals
// don't have signs.
// Underflow is handled by the default code, since it returns 0.
length = 8; // strlen("Infinity");
memcpy(number_buffer_, "Infinity", length);
} else {
length = snprintf(number_buffer_, kBufferSize, "%g", double_value);
}
return AddAsciiSymbol(i::Vector<const char>(number_buffer_, length), value);
}
bool DuplicateFinder::IsNumberCanonical(i::Vector<const char> number) {
// Test for a safe approximation of number literals that are already
// in canonical form: max 15 digits, no leading zeroes, except an
// integer part that is a single zero, and no trailing zeros below
// the decimal point.
int pos = 0;
int length = number.length();
if (number.length() > 15) return false;
if (number[pos] == '0') {
pos++;
} else {
while (pos < length &&
static_cast<unsigned>(number[pos] - '0') <= ('9' - '0')) pos++;
}
if (length == pos) return true;
if (number[pos] != '.') return false;
pos++;
bool invalid_last_digit = true;
while (pos < length) {
byte digit = number[pos] - '0';
if (digit > '9' - '0') return false;
invalid_last_digit = (digit == 0);
pos++;
}
return !invalid_last_digit;
}
uint32_t DuplicateFinder::Hash(i::Vector<const byte> key, bool is_ascii) {
// Primitive hash function, almost identical to the one used
// for strings (except that it's seeded by the length and ASCII-ness).
int length = key.length();
uint32_t hash = (length << 1) | (is_ascii ? 1 : 0) ;
for (int i = 0; i < length; i++) {
uint32_t c = key[i];
hash = (hash + c) * 1025;
hash ^= (hash >> 6);
}
return hash;
}
bool DuplicateFinder::Match(void* first, void* second) {
// Decode lengths.
// Length + ASCII-bit is encoded as base 128, most significant heptet first,
// with a 8th bit being non-zero while there are more heptets.
// The value encodes the number of bytes following, and whether the original
// was ASCII.
byte* s1 = reinterpret_cast<byte*>(first);
byte* s2 = reinterpret_cast<byte*>(second);
uint32_t length_ascii_field = 0;
byte c1;
do {
c1 = *s1;
if (c1 != *s2) return false;
length_ascii_field = (length_ascii_field << 7) | (c1 & 0x7f);
s1++;
s2++;
} while ((c1 & 0x80) != 0);
int length = static_cast<int>(length_ascii_field >> 1);
return memcmp(s1, s2, length) == 0;
}
byte* DuplicateFinder::BackupKey(i::Vector<const byte> bytes,
bool is_ascii) {
uint32_t ascii_length = (bytes.length() << 1) | (is_ascii ? 1 : 0);
backing_store_.StartSequence();
// Emit ascii_length as base-128 encoded number, with the 7th bit set
// on the byte of every heptet except the last, least significant, one.
if (ascii_length >= (1 << 7)) {
if (ascii_length >= (1 << 14)) {
if (ascii_length >= (1 << 21)) {
if (ascii_length >= (1 << 28)) {
backing_store_.Add(static_cast<byte>((ascii_length >> 28) | 0x80));
}
backing_store_.Add(static_cast<byte>((ascii_length >> 21) | 0x80u));
}
backing_store_.Add(static_cast<byte>((ascii_length >> 14) | 0x80u));
}
backing_store_.Add(static_cast<byte>((ascii_length >> 7) | 0x80u));
}
backing_store_.Add(static_cast<byte>(ascii_length & 0x7f));
backing_store_.AddBlock(bytes);
return backing_store_.EndSequence().start();
}
} } // v8::preparser

View File

@ -31,6 +31,8 @@
namespace v8 {
namespace preparser {
typedef uint8_t byte;
// Preparsing checks a JavaScript program and emits preparse-data that helps
// a later parsing to be faster.
// See preparse-data-format.h for the data format.
@ -46,6 +48,51 @@ namespace preparser {
namespace i = v8::internal;
class DuplicateFinder {
public:
DuplicateFinder()
: backing_store_(16),
map_(new i::HashMap(&Match)) { }
~DuplicateFinder() {
delete map_;
}
int AddAsciiSymbol(i::Vector<const char> key, int value);
int AddUC16Symbol(i::Vector<const uint16_t> key, int value);
// Add a a number literal by converting it (if necessary)
// to the string that ToString(ToNumber(literal)) would generate.
// and then adding that string with AddAsciiSymbol.
// This string is the actual value used as key in an object literal,
// and the one that must be different from the other keys.
int AddNumber(i::Vector<const char> key, int value);
private:
int AddSymbol(i::Vector<const byte> key, bool is_ascii, int value);
// Backs up the key and its length in the backing store.
// The backup is stored with a base 127 encoding of the
// length (plus a bit saying whether the string is ASCII),
// followed by the bytes of the key.
byte* BackupKey(i::Vector<const byte> key, bool is_ascii);
// Compare two encoded keys (both pointing into the backing store)
// for having the same base-127 encoded lengths and ASCII-ness,
// and then having the same 'length' bytes following.
static bool Match(void* first, void* second);
// Creates a hash from a sequence of bytes.
static uint32_t Hash(i::Vector<const byte> key, bool is_ascii);
// Checks whether a string containing a JS number is its canonical
// form.
static bool IsNumberCanonical(i::Vector<const char> key);
static const int kBufferSize = 100;
// Buffer used for string->number->canonical string conversions.
char number_buffer_[kBufferSize];
// Backing store used to store strings used as hashmap keys.
i::SequenceCollector<unsigned char> backing_store_;
i::HashMap* map_;
};
class PreParser {
public:
enum PreParseResult {
@ -67,6 +114,22 @@ class PreParser {
}
private:
// Used to detect duplicates in object literals.
enum PropertyType {
kNone = 0,
// Bit patterns representing different object literal property types.
kGetterProperty = 1,
kSetterProperty = 2,
kValueProperty = 7,
// Helper constants.
kValueFlag = 4
};
void CheckDuplicate(DuplicateFinder* finder,
i::Token::Value property,
int type,
bool* ok);
// These types form an algebra over syntactic categories that is just
// rich enough to let us recognize and propagate the constructs that
// are either being counted in the preparser data, or is important
@ -364,6 +427,11 @@ class PreParser {
// Report syntax error
void ReportUnexpectedToken(i::Token::Value token);
void ReportMessageAt(i::Scanner::Location location,
const char* type,
const char* name_opt) {
log_->LogMessage(location.beg_pos, location.end_pos, type, name_opt);
}
void ReportMessageAt(int start_pos,
int end_pos,
const char* type,

View File

@ -496,9 +496,6 @@ class Collector {
public:
explicit Collector(int initial_capacity = kMinCapacity)
: index_(0), size_(0) {
if (initial_capacity < kMinCapacity) {
initial_capacity = kMinCapacity;
}
current_chunk_ = Vector<T>::New(initial_capacity);
}
@ -600,13 +597,21 @@ class Collector {
// Creates a new current chunk, and stores the old chunk in the chunks_ list.
void Grow(int min_capacity) {
ASSERT(growth_factor > 1);
int growth = current_chunk_.length() * (growth_factor - 1);
if (growth > max_growth) {
growth = max_growth;
}
int new_capacity = current_chunk_.length() + growth;
if (new_capacity < min_capacity) {
new_capacity = min_capacity + growth;
int new_capacity;
int current_length = current_chunk_.length();
if (current_length < kMinCapacity) {
// The collector started out as empty.
new_capacity = min_capacity * growth_factor;
if (new_capacity < kMinCapacity) new_capacity = kMinCapacity;
} else {
int growth = current_length * (growth_factor - 1);
if (growth > max_growth) {
growth = max_growth;
}
new_capacity = current_length + growth;
if (new_capacity < min_capacity) {
new_capacity = min_capacity + growth;
}
}
Vector<T> new_chunk = Vector<T>::New(new_capacity);
int new_index = PrepareGrow(new_chunk);

View File

@ -0,0 +1,80 @@
# Copyright 2011 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.
# Templatated tests with duplicate parameter names.
# ----------------------------------------------------------------------
# Constants and utility functions
# A template that performs the same strict-mode test in different
# scopes (global scope, function scope, and nested function scope),
# and in non-strict mode too.
def DuplicateParameterTest(name, source):
expectation = "strict_param_dupe"
non_selfstrict = {"selfstrict":"", "id":"selfnormal"}
Template(name, '"use strict";\n' + source)(non_selfstrict, expectation)
Template(name + '-infunc',
'function foo() {\n "use strict";\n' + source +'\n}\n')(
non_selfstrict, expectation)
Template(name + '-infunc2',
'function foo() {\n "use strict";\n function bar() {\n' +
source +'\n }\n}\n')(non_selfstrict, expectation)
selfstrict = {"selfstrict": "\"use strict\";", "id": "selfstrict"}
nestedstrict = {"selfstrict": "function bar(){\"use strict\";}",
"id": "nestedstrict"}
selfstrictnestedclean = {"selfstrict": """
"use strict";
function bar(){}
""", "id": "selfstrictnestedclean"}
selftest = Template(name + '-$id', source)
selftest(selfstrict, expectation)
selftest(selfstrictnestedclean, expectation)
selftest(nestedstrict, None)
selftest(non_selfstrict, None)
# ----------------------------------------------------------------------
# Test templates
DuplicateParameterTest("dups", """
function foo(a, a) { $selfstrict }
""");
DuplicateParameterTest("dups-apart", """
function foo(a, b, c, d, e, f, g, h, i, j, k, l, m, n, a) { $selfstrict }
""");
DuplicateParameterTest("dups-escaped", """
function foo(\u0061, b, c, d, e, f, g, h, i, j, k, l, m, n, a) { $selfstrict }
""");
DuplicateParameterTest("triples", """
function foo(a, b, c, d, e, f, g, h, a, i, j, k, l, m, n, a) { $selfstrict }
""");

View File

@ -0,0 +1,144 @@
# Copyright 2011 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.
# Tests of duplicate properties in object literals.
# ----------------------------------------------------------------------
# Utility functions to generate a number of tests for each property
# name pair.
def PropertyTest(name, propa, propb):
replacement = {"id1": propa, "id2": propb, "name": name}
def StrictTest(name, source, replacement, expectation):
Template("strict-" + name,
"\"use strict\";\n" + source)(replacement, expectation)
Template(name, source)(replacement, expectation)
Template("strict-$name-data-data", """
"use strict";
var o = {$id1: 42, $id2: 42};
""")(replacement, "strict_duplicate_property")
Template("$name-data-data", """
var o = {$id1: 42, $id2: 42};
""")(replacement, None)
StrictTest("$name-data-get", """
var o = {$id1: 42, get $id2(){}};
""", replacement, "accessor_data_property")
StrictTest("$name-data-set", """
var o = {$id1: 42, set $id2(v){}};
""", replacement, "accessor_data_property")
StrictTest("$name-get-data", """
var o = {get $id1(){}, $id2: 42};
""", replacement, "accessor_data_property")
StrictTest("$name-set-data", """
var o = {set $id1(v){}, $id2: 42};
""", replacement, "accessor_data_property")
StrictTest("$name-get-get", """
var o = {get $id1(){}, get $id2(){}};
""", replacement, "accessor_get_set")
StrictTest("$name-set-set", """
var o = {set $id1(v){}, set $id2(v){}};
""", replacement, "accessor_get_set")
StrictTest("$name-nested-get", """
var o = {get $id1(){}, o: {get $id2(){} } };
""", replacement, None)
StrictTest("$name-nested-set", """
var o = {set $id1(){}, o: {set $id2(){} } };
""", replacement, None)
def TestBothWays(name, propa, propb):
PropertyTest(name + "-1", propa, propb)
PropertyTest(name + "-2", propb, propa)
def TestSame(name, prop):
PropertyTest(name, prop, prop)
#-----------------------------------------------------------------------
# Simple identifier property
TestSame("a", "a")
# Get/set identifiers
TestSame("get-id", "get")
TestSame("set-id", "set")
# Number properties
TestSame("0", "0")
TestSame("0.1", "0.1")
TestSame("1.0", "1.0")
TestSame("42.33", "42.33")
TestSame("2^32-2", "4294967294")
TestSame("2^32", "4294967296")
TestSame("2^53", "9007199254740992")
TestSame("Hex20", "0x20")
TestSame("exp10", "1e10")
TestSame("exp20", "1e20")
# String properties
TestSame("str-a", '"a"')
TestSame("str-0", '"0"')
TestSame("str-42", '"42"')
TestSame("str-empty", '""')
# Keywords
TestSame("if", "if")
TestSame("case", "case")
# Future reserved keywords
TestSame("public", "public")
TestSame("class", "class")
# Test that numbers are converted to string correctly.
TestBothWays("hex-int", "0x20", "32")
TestBothWays("dec-int", "32.00", "32")
TestBothWays("dec-underflow-int",
"32.00000000000000000000000000000000000000001", "32")
TestBothWays("exp-int", "3.2e1", "32")
TestBothWays("exp-int", "3200e-2", "32")
TestBothWays("overflow-inf", "1e2000", "Infinity")
TestBothWays("underflow-0", "1e-2000", "0")
TestBothWays("precission-loss-high", "9007199254740992", "9007199254740993")
TestBothWays("precission-loss-low", "1.9999999999999998", "1.9999999999999997")
TestBothWays("hex-int-str", "0x20", '"32"')
TestBothWays("dec-int-str", "32.00", '"32"')
TestBothWays("exp-int-str", "3.2e1", '"32"')
TestBothWays("overflow-inf-str", "1e2000", '"Infinity"')
TestBothWays("underflow-0-str", "1e-2000", '"0"')

View File

@ -98,7 +98,6 @@ class PreparserTestConfiguration(test.TestConfiguration):
def ParsePythonTestTemplates(self, result, filename,
executable, current_path, mode):
pathname = join(self.root, filename + ".pyt")
source = open(pathname).read();
def Test(name, source, expectation):
throws = None
if (expectation is not None):
@ -118,8 +117,7 @@ class PreparserTestConfiguration(test.TestConfiguration):
testsource = testsource.replace("$"+key, replacement[key]);
Test(testname, testsource, expectation)
return MkTest
eval(compile(source, pathname, "exec"),
{"Test": Test, "Template": Template}, {})
execfile(pathname, {"Test": Test, "Template": Template})
def ListTests(self, current_path, path, mode, variant_flags):
executable = join('obj', 'preparser', mode, 'preparser')
@ -143,9 +141,9 @@ class PreparserTestConfiguration(test.TestConfiguration):
filenames.sort()
for file in filenames:
# Each file as a python source file to be executed in a specially
# perparsed environment (defining the Template and Test functions)
# created environment (defining the Template and Test functions)
self.ParsePythonTestTemplates(result, file,
executable, current_path, mode)
executable, current_path, mode)
return result
def GetTestStatus(self, sections, defs):