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 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
271 LocalContext context3(NULL, Handle<ObjectTemplate>(), global_proxy); | 271 LocalContext context3(NULL, Handle<ObjectTemplate>(), global_proxy); |
272 CompileRun( | 272 CompileRun( |
273 "var records3 = [];" | 273 "var records3 = [];" |
274 "Object.observe(this, function(r) { [].push.apply(records3, r) });" | 274 "Object.observe(this, function(r) { [].push.apply(records3, r) });" |
275 "this.qux = 'context3';"); | 275 "this.qux = 'context3';"); |
276 CHECK_EQ(1, CompileRun("records3.length")->Int32Value()); | 276 CHECK_EQ(1, CompileRun("records3.length")->Int32Value()); |
277 CHECK(global_proxy->StrictEquals(CompileRun("records3[0].object"))); | 277 CHECK(global_proxy->StrictEquals(CompileRun("records3[0].object"))); |
278 } | 278 } |
279 CHECK_EQ(3, CompileRun("records.length")->Int32Value()); | 279 CHECK_EQ(3, CompileRun("records.length")->Int32Value()); |
280 } | 280 } |
| 281 |
| 282 struct RecordExpectation { |
| 283 Handle<Value> object; |
| 284 const char* type; |
| 285 const char* name; |
| 286 Handle<Value> old_value; |
| 287 }; |
| 288 |
| 289 // TODO(adamk): Use this helper elsewhere in this file. |
| 290 static void ExpectRecords(Handle<Value> records, |
| 291 const RecordExpectation expectations[], |
| 292 int num) { |
| 293 CHECK(records->IsArray()); |
| 294 Handle<Array> recordArray = records.As<Array>(); |
| 295 CHECK_EQ(num, static_cast<int>(recordArray->Length())); |
| 296 for (int i = 0; i < num; ++i) { |
| 297 Handle<Value> record = recordArray->Get(i); |
| 298 CHECK(record->IsObject()); |
| 299 Handle<Object> recordObj = record.As<Object>(); |
| 300 CHECK(expectations[i].object->StrictEquals( |
| 301 recordObj->Get(String::New("object")))); |
| 302 CHECK(String::New(expectations[i].type)->Equals( |
| 303 recordObj->Get(String::New("type")))); |
| 304 CHECK(String::New(expectations[i].name)->Equals( |
| 305 recordObj->Get(String::New("name")))); |
| 306 if (!expectations[i].old_value.IsEmpty()) { |
| 307 CHECK(expectations[i].old_value->Equals( |
| 308 recordObj->Get(String::New("oldValue")))); |
| 309 } |
| 310 } |
| 311 } |
| 312 |
| 313 TEST(APITestBasicMutation) { |
| 314 HarmonyIsolate isolate; |
| 315 HandleScope scope; |
| 316 LocalContext context; |
| 317 Handle<Object> obj = Handle<Object>::Cast(CompileRun( |
| 318 "var records = [];" |
| 319 "var obj = {};" |
| 320 "function observer(r) { [].push.apply(records, r); };" |
| 321 "Object.observe(obj, observer);" |
| 322 "obj")); |
| 323 obj->Set(String::New("foo"), Number::New(1)); |
| 324 obj->Set(1, Number::New(2)); |
| 325 // ForceSet should work just as well as Set |
| 326 obj->ForceSet(String::New("foo"), Number::New(3)); |
| 327 obj->ForceSet(Number::New(1), Number::New(4)); |
| 328 // Setting an indexed element via the property setting method |
| 329 obj->Set(Number::New(1), Number::New(5)); |
| 330 // Setting with a non-String, non-uint32 key |
| 331 obj->Set(Number::New(1.1), Number::New(6), DontDelete); |
| 332 obj->Delete(String::New("foo")); |
| 333 obj->Delete(1); |
| 334 obj->ForceDelete(Number::New(1.1)); |
| 335 |
| 336 // Force delivery |
| 337 // TODO(adamk): Should the above set methods trigger delivery themselves? |
| 338 CompileRun("void 0"); |
| 339 CHECK_EQ(9, CompileRun("records.length")->Int32Value()); |
| 340 const RecordExpectation expected_records[] = { |
| 341 { obj, "new", "foo", Handle<Value>() }, |
| 342 { obj, "new", "1", Handle<Value>() }, |
| 343 { obj, "updated", "foo", Number::New(1) }, |
| 344 { obj, "updated", "1", Number::New(2) }, |
| 345 { obj, "updated", "1", Number::New(4) }, |
| 346 { obj, "new", "1.1", Handle<Value>() }, |
| 347 { obj, "deleted", "foo", Number::New(3) }, |
| 348 { obj, "deleted", "1", Number::New(5) }, |
| 349 { obj, "deleted", "1.1", Number::New(6) } |
| 350 }; |
| 351 ExpectRecords(CompileRun("records"), |
| 352 expected_records, |
| 353 ARRAY_SIZE(expected_records)); |
| 354 } |
OLD | NEW |