OLD | NEW |
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 Loading... |
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)).IsNothing()); |
| 13348 CHECK(try_catch.HasCaught()); |
| 13349 } |
| 13350 { |
| 13351 // Special cases for arrays. |
| 13352 v8::TryCatch try_catch(isolate); |
| 13353 CHECK(arr->CreateDataProperty(env.local(), 0, v8::Integer::New(isolate, 42)) |
| 13354 .FromJust()); |
| 13355 CHECK(!try_catch.HasCaught()); |
| 13356 CHECK_EQ(1, arr->Length()); |
| 13357 v8::Local<v8::Value> val = arr->Get(env.local(), 0).ToLocalChecked(); |
| 13358 CHECK(val->IsNumber()); |
| 13359 CHECK_EQ(42.0, val->NumberValue(env.local()).FromJust()); |
| 13360 CHECK(arr->CreateDataProperty(env.local(), 1, v8::Integer::New(isolate, 23)) |
| 13361 .FromJust()); |
| 13362 CHECK(!try_catch.HasCaught()); |
| 13363 CHECK_EQ(2, arr->Length()); |
| 13364 val = arr->Get(env.local(), 1).ToLocalChecked(); |
| 13365 CHECK(val->IsNumber()); |
| 13366 CHECK_EQ(23.0, val->NumberValue(env.local()).FromJust()); |
| 13367 } |
| 13368 |
| 13369 CompileRun("Object.freeze(a);"); |
| 13370 { |
| 13371 // Can't change non-extensible objects. |
| 13372 v8::TryCatch try_catch(isolate); |
| 13373 CHECK(!obj->CreateDataProperty(env.local(), v8_str("baz"), |
| 13374 v8::Integer::New(isolate, 42)).FromJust()); |
| 13375 CHECK(!try_catch.HasCaught()); |
| 13376 } |
| 13377 } |
| 13378 |
| 13379 |
13290 static v8::Local<Context> calling_context0; | 13380 static v8::Local<Context> calling_context0; |
13291 static v8::Local<Context> calling_context1; | 13381 static v8::Local<Context> calling_context1; |
13292 static v8::Local<Context> calling_context2; | 13382 static v8::Local<Context> calling_context2; |
13293 | 13383 |
13294 | 13384 |
13295 // Check that the call to the callback is initiated in | 13385 // Check that the call to the callback is initiated in |
13296 // calling_context2, the directly calling context is calling_context1 | 13386 // calling_context2, the directly calling context is calling_context1 |
13297 // and the callback itself is in calling_context0. | 13387 // and the callback itself is in calling_context0. |
13298 static void GetCallingContextCallback( | 13388 static void GetCallingContextCallback( |
13299 const v8::FunctionCallbackInfo<v8::Value>& args) { | 13389 const v8::FunctionCallbackInfo<v8::Value>& args) { |
(...skipping 7775 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
21075 // add the testExtraShouldReturnFive export | 21165 // add the testExtraShouldReturnFive export |
21076 v8::Local<v8::Object> exports = env->GetExtrasExportsObject(); | 21166 v8::Local<v8::Object> exports = env->GetExtrasExportsObject(); |
21077 | 21167 |
21078 auto func = | 21168 auto func = |
21079 exports->Get(v8_str("testExtraShouldReturnFive")).As<v8::Function>(); | 21169 exports->Get(v8_str("testExtraShouldReturnFive")).As<v8::Function>(); |
21080 auto undefined = v8::Undefined(isolate); | 21170 auto undefined = v8::Undefined(isolate); |
21081 auto result = func->Call(undefined, 0, {}).As<v8::Number>(); | 21171 auto result = func->Call(undefined, 0, {}).As<v8::Number>(); |
21082 | 21172 |
21083 CHECK(result->Value() == 5.0); | 21173 CHECK(result->Value() == 5.0); |
21084 } | 21174 } |
OLD | NEW |