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

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

Issue 1154233003: Introduce v8::Object::CreateDataProperty (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: updates 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 13269 matching lines...) Expand 10 before | Expand all | Expand 10 after
13280 CHECK_EQ(1, force_set_set_count); 13280 CHECK_EQ(1, force_set_set_count);
13281 CHECK_EQ(5, force_set_get_count); 13281 CHECK_EQ(5, force_set_get_count);
13282 // The interceptor should also work for other properties 13282 // The interceptor should also work for other properties
13283 CHECK_EQ(3, global->Get(v8::String::NewFromUtf8(isolate, "b")) 13283 CHECK_EQ(3, global->Get(v8::String::NewFromUtf8(isolate, "b"))
13284 ->Int32Value()); 13284 ->Int32Value());
13285 CHECK_EQ(1, force_set_set_count); 13285 CHECK_EQ(1, force_set_set_count);
13286 CHECK_EQ(6, force_set_get_count); 13286 CHECK_EQ(6, force_set_get_count);
13287 } 13287 }
13288 13288
13289 13289
13290 TEST(CreateDataProperty) {
13291 LocalContext env;
13292 v8::Isolate* isolate = env->GetIsolate();
13293 v8::HandleScope handle_scope(isolate);
13294
13295 CompileRun(
13296 "var a = {};"
13297 "var b = [];"
13298 "Object.defineProperty(a, 'foo', {value: 23});"
13299 "Object.defineProperty(a, 'bar', {value: 23, configurable: true});");
13300
13301 v8::Local<v8::Object> obj =
13302 v8::Local<v8::Object>::Cast(env->Global()->Get(v8_str("a")));
13303 v8::Local<v8::Array> arr =
13304 v8::Local<v8::Array>::Cast(env->Global()->Get(v8_str("b")));
13305 {
13306 // Can't change a non-configurable properties.
13307 v8::TryCatch try_catch(isolate);
13308 CHECK(!obj->CreateDataProperty(env.local(), v8_str("foo"),
13309 v8::Integer::New(isolate, 42)).FromJust());
13310 CHECK(!try_catch.HasCaught());
13311 CHECK(obj->CreateDataProperty(env.local(), v8_str("bar"),
13312 v8::Integer::New(isolate, 42)).FromJust());
13313 CHECK(!try_catch.HasCaught());
13314 v8::Local<v8::Value> val =
13315 obj->Get(env.local(), v8_str("bar")).ToLocalChecked();
13316 CHECK(val->IsNumber());
13317 CHECK_EQ(42.0, val->NumberValue(env.local()).FromJust());
13318 }
13319
13320 {
13321 // Set a regular property.
13322 v8::TryCatch try_catch(isolate);
13323 CHECK(obj->CreateDataProperty(env.local(), v8_str("blub"),
13324 v8::Integer::New(isolate, 42)).FromJust());
13325 CHECK(!try_catch.HasCaught());
13326 v8::Local<v8::Value> val =
13327 obj->Get(env.local(), v8_str("blub")).ToLocalChecked();
13328 CHECK(val->IsNumber());
13329 CHECK_EQ(42.0, val->NumberValue(env.local()).FromJust());
13330 }
13331
13332 {
13333 // Set an indexed property.
13334 v8::TryCatch try_catch(isolate);
13335 CHECK(obj->CreateDataProperty(env.local(), v8_str("1"),
13336 v8::Integer::New(isolate, 42)).FromJust());
13337 CHECK(!try_catch.HasCaught());
13338 v8::Local<v8::Value> val = obj->Get(env.local(), 1).ToLocalChecked();
13339 CHECK(val->IsNumber());
13340 CHECK_EQ(42.0, val->NumberValue(env.local()).FromJust());
13341 }
13342
13343 {
13344 // Special cases for arrays.
13345 v8::TryCatch try_catch(isolate);
13346 CHECK(!arr->CreateDataProperty(env.local(), v8_str("length"),
13347 v8::Integer::New(isolate, 1)).FromJust());
13348 CHECK(!try_catch.HasCaught());
13349 }
13350 {
13351 // Special cases for arrays: index exceeds the array's length
13352 v8::TryCatch try_catch(isolate);
13353 CHECK(arr->CreateDataProperty(env.local(), 1, v8::Integer::New(isolate, 23))
13354 .FromJust());
13355 CHECK(!try_catch.HasCaught());
13356 CHECK_EQ(2, arr->Length());
13357 v8::Local<v8::Value> val = arr->Get(env.local(), 1).ToLocalChecked();
13358 CHECK(val->IsNumber());
13359 CHECK_EQ(23.0, val->NumberValue(env.local()).FromJust());
13360
13361 // Set an existing entry.
13362 CHECK(arr->CreateDataProperty(env.local(), 0, v8::Integer::New(isolate, 42))
13363 .FromJust());
13364 CHECK(!try_catch.HasCaught());
13365 val = arr->Get(env.local(), 0).ToLocalChecked();
13366 CHECK(val->IsNumber());
13367 CHECK_EQ(42.0, val->NumberValue(env.local()).FromJust());
13368 }
13369
13370 CompileRun("Object.freeze(a);");
13371 {
13372 // Can't change non-extensible objects.
13373 v8::TryCatch try_catch(isolate);
13374 CHECK(!obj->CreateDataProperty(env.local(), v8_str("baz"),
13375 v8::Integer::New(isolate, 42)).FromJust());
13376 CHECK(!try_catch.HasCaught());
13377 }
13378
13379 v8::Local<v8::ObjectTemplate> templ = v8::ObjectTemplate::New(isolate);
13380 templ->SetAccessCheckCallbacks(AccessAlwaysBlocked, NULL);
13381 v8::Local<v8::Object> access_checked =
13382 templ->NewInstance(env.local()).ToLocalChecked();
13383 {
13384 v8::TryCatch try_catch(isolate);
13385 CHECK(access_checked->CreateDataProperty(env.local(), v8_str("foo"),
13386 v8::Integer::New(isolate, 42))
13387 .IsNothing());
13388 CHECK(try_catch.HasCaught());
13389 }
13390 }
13391
13392
13290 static v8::Local<Context> calling_context0; 13393 static v8::Local<Context> calling_context0;
13291 static v8::Local<Context> calling_context1; 13394 static v8::Local<Context> calling_context1;
13292 static v8::Local<Context> calling_context2; 13395 static v8::Local<Context> calling_context2;
13293 13396
13294 13397
13295 // Check that the call to the callback is initiated in 13398 // Check that the call to the callback is initiated in
13296 // calling_context2, the directly calling context is calling_context1 13399 // calling_context2, the directly calling context is calling_context1
13297 // and the callback itself is in calling_context0. 13400 // and the callback itself is in calling_context0.
13298 static void GetCallingContextCallback( 13401 static void GetCallingContextCallback(
13299 const v8::FunctionCallbackInfo<v8::Value>& args) { 13402 const v8::FunctionCallbackInfo<v8::Value>& args) {
(...skipping 7775 matching lines...) Expand 10 before | Expand all | Expand 10 after
21075 // add the testExtraShouldReturnFive export 21178 // add the testExtraShouldReturnFive export
21076 v8::Local<v8::Object> exports = env->GetExtrasExportsObject(); 21179 v8::Local<v8::Object> exports = env->GetExtrasExportsObject();
21077 21180
21078 auto func = 21181 auto func =
21079 exports->Get(v8_str("testExtraShouldReturnFive")).As<v8::Function>(); 21182 exports->Get(v8_str("testExtraShouldReturnFive")).As<v8::Function>();
21080 auto undefined = v8::Undefined(isolate); 21183 auto undefined = v8::Undefined(isolate);
21081 auto result = func->Call(undefined, 0, {}).As<v8::Number>(); 21184 auto result = func->Call(undefined, 0, {}).As<v8::Number>();
21082 21185
21083 CHECK(result->Value() == 5.0); 21186 CHECK(result->Value() == 5.0);
21084 } 21187 }
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