Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2006-2009 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2009 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 13 matching lines...) Expand all Loading... | |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | 27 |
| 28 #include <stdlib.h> | 28 #include <stdlib.h> |
| 29 | 29 |
| 30 #include "v8.h" | 30 #include "v8.h" |
| 31 | 31 |
| 32 #include "token.h" | 32 #include "token.h" |
| 33 #include "scanner.h" | 33 #include "scanner.h" |
| 34 #include "parser.h" | |
| 34 #include "utils.h" | 35 #include "utils.h" |
| 35 #include "execution.h" | 36 #include "execution.h" |
| 36 | 37 |
| 37 #include "cctest.h" | 38 #include "cctest.h" |
| 38 | 39 |
| 39 namespace i = ::v8::internal; | 40 namespace i = ::v8::internal; |
| 40 | 41 |
| 41 TEST(KeywordMatcher) { | 42 TEST(KeywordMatcher) { |
| 42 struct KeywordToken { | 43 struct KeywordToken { |
| 43 const char* keyword; | 44 const char* keyword; |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 141 "--> is eol-comment\nvar y = 37;\n", | 142 "--> is eol-comment\nvar y = 37;\n", |
| 142 "\n --> is eol-comment\nvar y = 37;\n", | 143 "\n --> is eol-comment\nvar y = 37;\n", |
| 143 "/* precomment */ --> is eol-comment\nvar y = 37;\n", | 144 "/* precomment */ --> is eol-comment\nvar y = 37;\n", |
| 144 "\n/* precomment */ --> is eol-comment\nvar y = 37;\n", | 145 "\n/* precomment */ --> is eol-comment\nvar y = 37;\n", |
| 145 // After first real token. | 146 // After first real token. |
| 146 "var x = 42;\n--> is eol-comment\nvar y = 37;\n", | 147 "var x = 42;\n--> is eol-comment\nvar y = 37;\n", |
| 147 "var x = 42;\n/* precomment */ --> is eol-comment\nvar y = 37;\n", | 148 "var x = 42;\n/* precomment */ --> is eol-comment\nvar y = 37;\n", |
| 148 NULL | 149 NULL |
| 149 }; | 150 }; |
| 150 | 151 |
| 151 // Parser needs a stack limit. | 152 // Parser/Scanner needs a stack limit. |
| 152 int marker; | 153 int marker; |
| 153 i::StackGuard::SetStackLimit( | 154 i::StackGuard::SetStackLimit( |
| 154 reinterpret_cast<uintptr_t>(&marker) - 128 * 1024); | 155 reinterpret_cast<uintptr_t>(&marker) - 128 * 1024); |
| 155 | 156 |
| 156 for (int i = 0; tests[i]; i++) { | 157 for (int i = 0; tests[i]; i++) { |
| 157 v8::ScriptData* data = | 158 v8::ScriptData* data = |
| 158 v8::ScriptData::PreCompile(tests[i], strlen(tests[i])); | 159 v8::ScriptData::PreCompile(tests[i], strlen(tests[i])); |
| 159 CHECK(data != NULL && !data->HasError()); | 160 CHECK(data != NULL && !data->HasError()); |
| 160 delete data; | 161 delete data; |
| 161 } | 162 } |
| 162 } | 163 } |
| 164 | |
| 165 | |
| 166 class ScriptResource : public v8::String::ExternalAsciiStringResource { | |
| 167 public: | |
| 168 ScriptResource(const char* data, size_t length) | |
| 169 : data_(data), length_(length) { } | |
| 170 | |
| 171 const char* data() const { return data_; } | |
| 172 size_t length() const { return length_; } | |
| 173 | |
| 174 private: | |
| 175 const char* data_; | |
| 176 size_t length_; | |
| 177 }; | |
| 178 | |
| 179 | |
| 180 TEST(Preparsing) { | |
| 181 v8::HandleScope handles; | |
| 182 v8::Persistent<v8::Context> context = v8::Context::New(); | |
| 183 v8::Context::Scope context_scope(context); | |
| 184 int marker; | |
| 185 i::StackGuard::SetStackLimit( | |
| 186 reinterpret_cast<uintptr_t>(&marker) - 128 * 1024); | |
| 187 | |
| 188 // Source containing functions that might be lazily compiled and all types | |
| 189 // of symbols (string, propertyName, regexp). | |
| 190 const char* source = | |
| 191 "var x = 42;" | |
| 192 "function foo(a) { return function nolazy(b) { return a + b; } }" | |
| 193 "function bar(a) { if (a) return function lazy(b) { return b; } }" | |
| 194 "var z = {'string': 'string literal', bareword: 'propertyName', " | |
| 195 " 42: 'number literal', for: 'keyword as propertyName', " | |
| 196 " f\\u006fr: 'keyword propertyname with escape'};" | |
| 197 "var v = /RegExp Literal/;" | |
| 198 "var w = /RegExp Literal\\u0020With Escape/gin;" | |
| 199 "var y = { get getter() { return 42; }, " | |
| 200 " set setter(v) { this.value = v; }};"; | |
| 201 int source_length = strlen(source); | |
| 202 const char* error_source = "var x = y z;"; | |
| 203 int error_source_length = strlen(error_source); | |
| 204 | |
| 205 // Parser needs a handle scope. | |
|
Mads Ager (chromium)
2010/09/07 12:44:53
This comment doesn't seem to fit here?
| |
| 206 | |
| 207 v8::ScriptData* preparse = | |
| 208 v8::ScriptData::PreCompile(source, source_length); | |
| 209 CHECK(!preparse->HasError()); | |
| 210 bool lazy_flag = i::FLAG_lazy; | |
| 211 { | |
| 212 i::FLAG_lazy = true; | |
| 213 ScriptResource* resource = new ScriptResource(source, source_length); | |
| 214 v8::Local<v8::String> script_source = v8::String::NewExternal(resource); | |
| 215 v8::Script::Compile(script_source, NULL, preparse); | |
| 216 } | |
| 217 | |
| 218 { | |
| 219 i::FLAG_lazy = false; | |
| 220 | |
| 221 ScriptResource* resource = new ScriptResource(source, source_length); | |
| 222 v8::Local<v8::String> script_source = v8::String::NewExternal(resource); | |
| 223 v8::Script::New(script_source, NULL, preparse, v8::Local<v8::String>()); | |
| 224 } | |
| 225 delete preparse; | |
| 226 i::FLAG_lazy = lazy_flag; | |
| 227 | |
| 228 // Syntax error. | |
| 229 v8::ScriptData* error_preparse = | |
| 230 v8::ScriptData::PreCompile(error_source, error_source_length); | |
| 231 CHECK(error_preparse->HasError()); | |
| 232 i::ScriptDataImpl *pre_impl = | |
| 233 reinterpret_cast<i::ScriptDataImpl*>(error_preparse); | |
| 234 i::Scanner::Location error_location = | |
| 235 pre_impl->MessageLocation(); | |
| 236 // Error is at "z" in source, location 10..11. | |
| 237 CHECK_EQ(10, error_location.beg_pos); | |
| 238 CHECK_EQ(11, error_location.end_pos); | |
| 239 // Should not crash. | |
| 240 const char* message = pre_impl->BuildMessage(); | |
| 241 i::Vector<const char*> args = pre_impl->BuildArgs(); | |
| 242 CHECK_GT(strlen(message), 0); | |
| 243 } | |
| OLD | NEW |