Commit 57760b82 authored by lrn@chromium.org's avatar lrn@chromium.org

Make preparser detect duplicate parameters and object literal properties.

This is a fix and reapply of r8516 with some comments addressed and more
tests added.
The difference from r8516 is that canonicalization of number literals is
no performed using the same methods as in v8, to avoid false positives/negatives
when detecting duplicates.

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

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@8541 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 1fff7803
......@@ -232,8 +232,13 @@ PREPARSER_SOURCES = {
'all': Split("""
allocation.cc
bignum.cc
bignum-dtoa.cc
cached-powers.cc
conversions.cc
diy-fp.cc
dtoa.cc
fast-dtoa.cc
fixed-dtoa.cc
hashmap.cc
preparse-data.cc
preparser.cc
......
// Copyright 2010 the V8 project authors. All rights reserved.
// 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:
......@@ -27,7 +27,10 @@
#include <math.h>
#include "v8.h"
#include "../include/v8stdint.h"
#include "checks.h"
#include "utils.h"
#include "bignum-dtoa.h"
#include "bignum.h"
......
......@@ -26,6 +26,7 @@
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <stdarg.h>
#include <math.h>
#include <limits.h>
#include "conversions-inl.h"
......@@ -390,7 +391,7 @@ char* DoubleToRadixCString(double value, int radix) {
int integer_pos = kBufferSize - 2;
do {
integer_buffer[integer_pos--] =
chars[static_cast<int>(modulo(integer_part, radix))];
chars[static_cast<int>(fmod(integer_part, radix))];
integer_part /= radix;
} while (integer_part >= 1.0);
// Sanity check.
......@@ -430,24 +431,4 @@ char* DoubleToRadixCString(double value, int radix) {
return builder.Finalize();
}
static Mutex* dtoa_lock_one = OS::CreateMutex();
static Mutex* dtoa_lock_zero = OS::CreateMutex();
} } // namespace v8::internal
extern "C" {
void ACQUIRE_DTOA_LOCK(int n) {
ASSERT(n == 0 || n == 1);
(n == 0 ? v8::internal::dtoa_lock_zero : v8::internal::dtoa_lock_one)->Lock();
}
void FREE_DTOA_LOCK(int n) {
ASSERT(n == 0 || n == 1);
(n == 0 ? v8::internal::dtoa_lock_zero : v8::internal::dtoa_lock_one)->
Unlock();
}
}
......@@ -127,6 +127,8 @@ double StringToDouble(UnicodeCache* unicode_cache,
int flags,
double empty_string_val = 0);
const int kDoubleToCStringMinBufferSize = 100;
// Converts a double to a string value according to ECMA-262 9.8.1.
// The buffer should be large enough for any floating point number.
// 100 characters is enough.
......
// Copyright 2010 the V8 project authors. All rights reserved.
// 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:
......@@ -27,7 +27,10 @@
#include <math.h>
#include "v8.h"
#include "../include/v8stdint.h"
#include "checks.h"
#include "utils.h"
#include "dtoa.h"
#include "bignum-dtoa.h"
......
// Copyright 2010 the V8 project authors. All rights reserved.
// 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:
......@@ -25,7 +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 "v8.h"
#include "../include/v8stdint.h"
#include "checks.h"
#include "utils.h"
#include "fast-dtoa.h"
......
// Copyright 2010 the V8 project authors. All rights reserved.
// 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:
......@@ -27,7 +27,9 @@
#include <math.h>
#include "v8.h"
#include "../include/v8stdint.h"
#include "checks.h"
#include "utils.h"
#include "double.h"
#include "fixed-dtoa.h"
......
......@@ -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"
......
This diff is collapsed.
......@@ -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,57 @@ namespace preparser {
namespace i = v8::internal;
class DuplicateFinder {
public:
explicit DuplicateFinder(i::UnicodeCache* constants)
: unicode_constants_(constants),
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);
// Size of buffer. Sufficient for using it to call DoubleToCString in
// from conversions.h.
static const int kBufferSize = 100;
i::UnicodeCache* unicode_constants_;
// Backing store used to store strings used as hashmap keys.
i::SequenceCollector<unsigned char> backing_store_;
i::HashMap* map_;
// Buffer used for string->number->canonical string conversions.
char number_buffer_[kBufferSize];
};
class PreParser {
public:
enum PreParseResult {
......@@ -67,6 +120,45 @@ class PreParser {
}
private:
// Used to detect duplicates in object literals. Each of the values
// kGetterProperty, kSetterProperty and kValueProperty represents
// a type of object literal property. When parsing a property, its
// type value is stored in the DuplicateFinder for the property name.
// Values are chosen so that having intersection bits means the there is
// an incompatibility.
// I.e., you can add a getter to a property that already has a setter, since
// kGetterProperty and kSetterProperty doesn't intersect, but not if it
// already has a getter or a value. Adding the getter to an existing
// setter will store the value (kGetterProperty | kSetterProperty), which
// is incompatible with adding any further properties.
enum PropertyType {
kNone = 0,
// Bit patterns representing different object literal property types.
kGetterProperty = 1,
kSetterProperty = 2,
kValueProperty = 7,
// Helper constants.
kValueFlag = 4
};
// Checks the type of conflict based on values coming from PropertyType.
bool HasConflict(int type1, int type2) { return (type1 & type2) != 0; }
bool IsDataDataConflict(int type1, int type2) {
return ((type1 & type2) & kValueFlag) != 0;
}
bool IsDataAccessorConflict(int type1, int type2) {
return ((type1 ^ type2) & kValueFlag) != 0;
}
bool IsAccessorAccessorConflict(int type1, int type2) {
return ((type1 | type2) & kValueFlag) == 0;
}
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 +456,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,
......
......@@ -349,6 +349,8 @@ class Scanner {
return next_.literal_chars->length();
}
UnicodeCache* unicode_cache() { return unicode_cache_; }
static const int kCharacterLookaheadBufferSize = 1;
protected:
......
......@@ -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);
......
# 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 }
""");
DuplicateParameterTest("escapes", """
function foo(a, \u0061) { $selfstrict }
""");
DuplicateParameterTest("long-names", """
function foo(arglebargleglopglyfarglebargleglopglyfarglebargleglopglyfa,
arglebargleglopglyfarglebargleglopglyfarglebargleglopglyfa) {
$selfstrict
}
""");
# 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, allow_strict = True):
replacement = {"id1": propa, "id2": propb, "name": name}
# Tests same test in both strict and non-strict context.
def StrictTest(name, source, replacement, expectation):
if (allow_strict):
Template("strict-" + name,
"\"use strict\";\n" + source)(replacement, expectation)
Template(name, source)(replacement, expectation)
# This one only fails in non-strict context.
if (allow_strict):
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, allow_strict = True):
PropertyTest(name + "-1", propa, propb, allow_strict)
PropertyTest(name + "-2", propb, propa, allow_strict)
def TestSame(name, prop, allow_strict = True):
PropertyTest(name, prop, prop, allow_strict)
#-----------------------------------------------------------------------
# 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")
TestSame("Oct40", "040", False);
# 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("oct-int", "040", "32", False) # Octals disallowed in strict mode.
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("overflow-inf-exact", "1.797693134862315808e+308", "Infinity")
TestBothWays("non-overflow-inf-exact", "1.797693134862315807e+308",
"1.7976931348623157e+308")
TestBothWays("underflow-0", "1e-2000", "0")
TestBothWays("underflow-0-exact", "2.4703282292062E-324", "0")
TestBothWays("non-underflow-0-exact", "2.4703282292063E-324", "5e-324")
TestBothWays("precission-loss-high", "9007199254740992", "9007199254740993")
TestBothWays("precission-loss-low", "1.9999999999999998", "1.9999999999999997")
TestBothWays("non-canonical-literal-int", "1.0", "1")
TestBothWays("non-canonical-literal-frac", "1.50", "1.5")
TestBothWays("rounding-down", "1.12512512512512452", "1.1251251251251244")
TestBothWays("rounding-up", "1.12512512512512453", "1.1251251251251246")
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"')
TestBothWays("non-canonical-literal-int-str", "1.0", '"1"')
TestBothWays("non-canonical-literal-frac-str", "1.50", '"1.5"')
......@@ -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,7 +141,7 @@ 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)
return result
......
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