Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(58)

Side by Side Diff: test/cctest/test-api.cc

Issue 1167473002: Also expose DefineOwnProperty (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/v8natives.js ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 13411 matching lines...) Expand 10 before | Expand all | Expand 10 after
13422 { 13422 {
13423 v8::TryCatch try_catch(isolate); 13423 v8::TryCatch try_catch(isolate);
13424 CHECK(access_checked->CreateDataProperty(env.local(), v8_str("foo"), 13424 CHECK(access_checked->CreateDataProperty(env.local(), v8_str("foo"),
13425 v8::Integer::New(isolate, 42)) 13425 v8::Integer::New(isolate, 42))
13426 .IsNothing()); 13426 .IsNothing());
13427 CHECK(try_catch.HasCaught()); 13427 CHECK(try_catch.HasCaught());
13428 } 13428 }
13429 } 13429 }
13430 13430
13431 13431
13432 TEST(DefineOwnProperty) {
13433 LocalContext env;
13434 v8::Isolate* isolate = env->GetIsolate();
13435 v8::HandleScope handle_scope(isolate);
13436
13437 CompileRun(
13438 "var a = {};"
13439 "var b = [];"
13440 "Object.defineProperty(a, 'foo', {value: 23});"
13441 "Object.defineProperty(a, 'bar', {value: 23, configurable: true});");
13442
13443 v8::Local<v8::Object> obj =
13444 v8::Local<v8::Object>::Cast(env->Global()->Get(v8_str("a")));
13445 v8::Local<v8::Array> arr =
13446 v8::Local<v8::Array>::Cast(env->Global()->Get(v8_str("b")));
13447 {
13448 // Can't change a non-configurable properties.
13449 v8::TryCatch try_catch(isolate);
13450 CHECK(!obj->DefineOwnProperty(env.local(), v8_str("foo"),
13451 v8::Integer::New(isolate, 42)).FromJust());
13452 CHECK(!try_catch.HasCaught());
13453 CHECK(obj->DefineOwnProperty(env.local(), v8_str("bar"),
13454 v8::Integer::New(isolate, 42)).FromJust());
13455 CHECK(!try_catch.HasCaught());
13456 v8::Local<v8::Value> val =
13457 obj->Get(env.local(), v8_str("bar")).ToLocalChecked();
13458 CHECK(val->IsNumber());
13459 CHECK_EQ(42.0, val->NumberValue(env.local()).FromJust());
13460 }
13461
13462 {
13463 // Set a regular property.
13464 v8::TryCatch try_catch(isolate);
13465 CHECK(obj->DefineOwnProperty(env.local(), v8_str("blub"),
13466 v8::Integer::New(isolate, 42)).FromJust());
13467 CHECK(!try_catch.HasCaught());
13468 v8::Local<v8::Value> val =
13469 obj->Get(env.local(), v8_str("blub")).ToLocalChecked();
13470 CHECK(val->IsNumber());
13471 CHECK_EQ(42.0, val->NumberValue(env.local()).FromJust());
13472 }
13473
13474 {
13475 // Set an indexed property.
13476 v8::TryCatch try_catch(isolate);
13477 CHECK(obj->DefineOwnProperty(env.local(), v8_str("1"),
13478 v8::Integer::New(isolate, 42)).FromJust());
13479 CHECK(!try_catch.HasCaught());
13480 v8::Local<v8::Value> val = obj->Get(env.local(), 1).ToLocalChecked();
13481 CHECK(val->IsNumber());
13482 CHECK_EQ(42.0, val->NumberValue(env.local()).FromJust());
13483 }
13484
13485 {
13486 // Special cases for arrays.
13487 v8::TryCatch try_catch(isolate);
13488 CHECK(!arr->DefineOwnProperty(env.local(), v8_str("length"),
13489 v8::Integer::New(isolate, 1)).FromJust());
13490 CHECK(!try_catch.HasCaught());
13491 }
13492 {
13493 // Special cases for arrays: index exceeds the array's length
13494 v8::TryCatch try_catch(isolate);
13495 CHECK(arr->DefineOwnProperty(env.local(), v8_str("1"),
13496 v8::Integer::New(isolate, 23)).FromJust());
13497 CHECK(!try_catch.HasCaught());
13498 CHECK_EQ(2, arr->Length());
13499 v8::Local<v8::Value> val = arr->Get(env.local(), 1).ToLocalChecked();
13500 CHECK(val->IsNumber());
13501 CHECK_EQ(23.0, val->NumberValue(env.local()).FromJust());
13502
13503 // Set an existing entry.
13504 CHECK(arr->DefineOwnProperty(env.local(), v8_str("0"),
13505 v8::Integer::New(isolate, 42)).FromJust());
13506 CHECK(!try_catch.HasCaught());
13507 val = arr->Get(env.local(), 0).ToLocalChecked();
13508 CHECK(val->IsNumber());
13509 CHECK_EQ(42.0, val->NumberValue(env.local()).FromJust());
13510 }
13511
13512 {
13513 // Set a non-writable property.
13514 v8::TryCatch try_catch(isolate);
13515 CHECK(obj->DefineOwnProperty(env.local(), v8_str("lala"),
13516 v8::Integer::New(isolate, 42),
13517 v8::ReadOnly).FromJust());
13518 CHECK(!try_catch.HasCaught());
13519 v8::Local<v8::Value> val =
13520 obj->Get(env.local(), v8_str("lala")).ToLocalChecked();
13521 CHECK(val->IsNumber());
13522 CHECK_EQ(42.0, val->NumberValue(env.local()).FromJust());
13523 CHECK_EQ(v8::ReadOnly, obj->GetPropertyAttributes(
13524 env.local(), v8_str("lala")).FromJust());
13525 CHECK(!try_catch.HasCaught());
13526 }
13527
13528 CompileRun("Object.freeze(a);");
13529 {
13530 // Can't change non-extensible objects.
13531 v8::TryCatch try_catch(isolate);
13532 CHECK(!obj->DefineOwnProperty(env.local(), v8_str("baz"),
13533 v8::Integer::New(isolate, 42)).FromJust());
13534 CHECK(!try_catch.HasCaught());
13535 }
13536
13537 v8::Local<v8::ObjectTemplate> templ = v8::ObjectTemplate::New(isolate);
13538 templ->SetAccessCheckCallbacks(AccessAlwaysBlocked, NULL);
13539 v8::Local<v8::Object> access_checked =
13540 templ->NewInstance(env.local()).ToLocalChecked();
13541 {
13542 v8::TryCatch try_catch(isolate);
13543 CHECK(access_checked->DefineOwnProperty(env.local(), v8_str("foo"),
13544 v8::Integer::New(isolate, 42))
13545 .IsNothing());
13546 CHECK(try_catch.HasCaught());
13547 }
13548 }
13549
13550
13432 static v8::Local<Context> calling_context0; 13551 static v8::Local<Context> calling_context0;
13433 static v8::Local<Context> calling_context1; 13552 static v8::Local<Context> calling_context1;
13434 static v8::Local<Context> calling_context2; 13553 static v8::Local<Context> calling_context2;
13435 13554
13436 13555
13437 // Check that the call to the callback is initiated in 13556 // Check that the call to the callback is initiated in
13438 // calling_context2, the directly calling context is calling_context1 13557 // calling_context2, the directly calling context is calling_context1
13439 // and the callback itself is in calling_context0. 13558 // and the callback itself is in calling_context0.
13440 static void GetCallingContextCallback( 13559 static void GetCallingContextCallback(
13441 const v8::FunctionCallbackInfo<v8::Value>& args) { 13560 const v8::FunctionCallbackInfo<v8::Value>& args) {
(...skipping 7832 matching lines...) Expand 10 before | Expand all | Expand 10 after
21274 CHECK_EQ(2, set->Size()); 21393 CHECK_EQ(2, set->Size());
21275 21394
21276 v8::Local<v8::Array> keys = set->AsArray(); 21395 v8::Local<v8::Array> keys = set->AsArray();
21277 CHECK_EQ(2, keys->Length()); 21396 CHECK_EQ(2, keys->Length());
21278 CHECK_EQ(1, keys->Get(0).As<v8::Int32>()->Value()); 21397 CHECK_EQ(1, keys->Get(0).As<v8::Int32>()->Value());
21279 CHECK_EQ(2, keys->Get(1).As<v8::Int32>()->Value()); 21398 CHECK_EQ(2, keys->Get(1).As<v8::Int32>()->Value());
21280 21399
21281 set = v8::Set::FromArray(env.local(), keys).ToLocalChecked(); 21400 set = v8::Set::FromArray(env.local(), keys).ToLocalChecked();
21282 CHECK_EQ(2, set->Size()); 21401 CHECK_EQ(2, set->Size());
21283 } 21402 }
OLDNEW
« no previous file with comments | « src/v8natives.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698