| OLD | NEW |
| 1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "test/cctest/cctest.h" | 5 #include "test/cctest/cctest.h" |
| 6 | 6 |
| 7 #include "include/v8-experimental.h" | 7 #include "include/v8-experimental.h" |
| 8 #include "include/v8.h" | 8 #include "include/v8.h" |
| 9 #include "src/api.h" | 9 #include "src/api.h" |
| 10 #include "src/objects-inl.h" | 10 #include "src/objects-inl.h" |
| (...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 242 // Reset hidden property. | 242 // Reset hidden property. |
| 243 CHECK(obj->SetPrivate(env.local(), priv, v8::Integer::New(isolate, 789)) | 243 CHECK(obj->SetPrivate(env.local(), priv, v8::Integer::New(isolate, 789)) |
| 244 .FromJust()); | 244 .FromJust()); |
| 245 | 245 |
| 246 // Test non-global access in Crankshaft. | 246 // Test non-global access in Crankshaft. |
| 247 CompileRun("%OptimizeFunctionOnNextCall(g);"); | 247 CompileRun("%OptimizeFunctionOnNextCall(g);"); |
| 248 | 248 |
| 249 ExpectInt32("g()", 789); | 249 ExpectInt32("g()", 789); |
| 250 } | 250 } |
| 251 | 251 |
| 252 TEST(CachedAccessorOnGlobalObject) { |
| 253 i::FLAG_allow_natives_syntax = true; |
| 254 LocalContext env; |
| 255 v8::Isolate* isolate = env->GetIsolate(); |
| 256 v8::HandleScope scope(isolate); |
| 257 |
| 258 v8::Local<v8::FunctionTemplate> templ = |
| 259 v8::FunctionTemplate::New(CcTest::isolate()); |
| 260 v8::Local<v8::ObjectTemplate> object_template = templ->InstanceTemplate(); |
| 261 v8::Local<v8::Private> priv = |
| 262 v8::Private::ForApi(isolate, v8_str("Foo#draft")); |
| 263 |
| 264 object_template->SetAccessorProperty( |
| 265 v8_str("draft"), |
| 266 v8::FunctionTemplate::NewWithCache(isolate, UnreachableCallback, priv, |
| 267 v8::Local<v8::Value>())); |
| 268 |
| 269 v8::Local<v8::Context> ctx = |
| 270 v8::Context::New(CcTest::isolate(), nullptr, object_template); |
| 271 v8::Local<v8::Object> obj = ctx->Global(); |
| 272 |
| 273 // Install the private property on the instance. |
| 274 CHECK(obj->SetPrivate(isolate->GetCurrentContext(), priv, |
| 275 v8::Undefined(isolate)) |
| 276 .FromJust()); |
| 277 |
| 278 { |
| 279 v8::Context::Scope context_scope(ctx); |
| 280 |
| 281 // Access surrogate accessor. |
| 282 ExpectUndefined("draft"); |
| 283 |
| 284 // Set hidden property. |
| 285 CHECK(obj->SetPrivate(env.local(), priv, v8::Integer::New(isolate, 123)) |
| 286 .FromJust()); |
| 287 |
| 288 // Test ICs. |
| 289 CompileRun( |
| 290 "function f() {" |
| 291 " var x;" |
| 292 " for (var i = 0; i < 100; i++) {" |
| 293 " x = draft;" |
| 294 " }" |
| 295 " return x;" |
| 296 "}"); |
| 297 |
| 298 ExpectInt32("f()", 123); |
| 299 |
| 300 // Reset hidden property. |
| 301 CHECK(obj->SetPrivate(env.local(), priv, v8::Integer::New(isolate, 456)) |
| 302 .FromJust()); |
| 303 |
| 304 // Test Crankshaft. |
| 305 CompileRun("%OptimizeFunctionOnNextCall(f);"); |
| 306 |
| 307 ExpectInt32("f()", 456); |
| 308 |
| 309 CHECK(obj->SetPrivate(env.local(), priv, v8::Integer::New(isolate, 456)) |
| 310 .FromJust()); |
| 311 // Test non-global ICs. |
| 312 CompileRun( |
| 313 "var x = this;" |
| 314 "function g() {" |
| 315 " var r = 0;" |
| 316 " for (var i = 0; i < 100; i++) {" |
| 317 " r = x.draft;" |
| 318 " }" |
| 319 " return r;" |
| 320 "}"); |
| 321 |
| 322 ExpectInt32("g()", 456); |
| 323 |
| 324 // Reset hidden property. |
| 325 CHECK(obj->SetPrivate(env.local(), priv, v8::Integer::New(isolate, 789)) |
| 326 .FromJust()); |
| 327 |
| 328 // Test non-global access in Crankshaft. |
| 329 CompileRun("%OptimizeFunctionOnNextCall(g);"); |
| 330 |
| 331 ExpectInt32("g()", 789); |
| 332 } |
| 333 } |
| 334 |
| 252 namespace { | 335 namespace { |
| 253 | 336 |
| 254 static void Setter(v8::Local<v8::String> name, v8::Local<v8::Value> value, | 337 static void Setter(v8::Local<v8::String> name, v8::Local<v8::Value> value, |
| 255 const v8::PropertyCallbackInfo<void>& info) {} | 338 const v8::PropertyCallbackInfo<void>& info) {} |
| 256 } | 339 } |
| 257 | 340 |
| 258 // Re-declaration of non-configurable accessors should throw. | 341 // Re-declaration of non-configurable accessors should throw. |
| 259 TEST(RedeclareAccessor) { | 342 TEST(RedeclareAccessor) { |
| 260 v8::HandleScope scope(CcTest::isolate()); | 343 v8::HandleScope scope(CcTest::isolate()); |
| 261 LocalContext env; | 344 LocalContext env; |
| 262 | 345 |
| 263 v8::Local<v8::FunctionTemplate> templ = | 346 v8::Local<v8::FunctionTemplate> templ = |
| 264 v8::FunctionTemplate::New(CcTest::isolate()); | 347 v8::FunctionTemplate::New(CcTest::isolate()); |
| 265 | 348 |
| 266 v8::Local<v8::ObjectTemplate> object_template = templ->InstanceTemplate(); | 349 v8::Local<v8::ObjectTemplate> object_template = templ->InstanceTemplate(); |
| 267 object_template->SetAccessor( | 350 object_template->SetAccessor( |
| 268 v8_str("foo"), NULL, Setter, v8::Local<v8::Value>(), | 351 v8_str("foo"), NULL, Setter, v8::Local<v8::Value>(), |
| 269 v8::AccessControl::DEFAULT, v8::PropertyAttribute::DontDelete); | 352 v8::AccessControl::DEFAULT, v8::PropertyAttribute::DontDelete); |
| 270 | 353 |
| 271 v8::Local<v8::Context> ctx = | 354 v8::Local<v8::Context> ctx = |
| 272 v8::Context::New(CcTest::isolate(), nullptr, object_template); | 355 v8::Context::New(CcTest::isolate(), nullptr, object_template); |
| 273 | 356 |
| 274 // Declare function. | 357 // Declare function. |
| 275 v8::Local<v8::String> code = v8_str("function foo() {};"); | 358 v8::Local<v8::String> code = v8_str("function foo() {};"); |
| 276 | 359 |
| 277 v8::TryCatch try_catch(CcTest::isolate()); | 360 v8::TryCatch try_catch(CcTest::isolate()); |
| 278 v8::Script::Compile(ctx, code).ToLocalChecked()->Run(ctx).IsEmpty(); | 361 v8::Script::Compile(ctx, code).ToLocalChecked()->Run(ctx).IsEmpty(); |
| 279 CHECK(try_catch.HasCaught()); | 362 CHECK(try_catch.HasCaught()); |
| 280 } | 363 } |
| OLD | NEW |