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

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

Issue 22903012: js accessor creation on Template (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 7 years, 4 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 | Annotate | Revision Log
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 8210 matching lines...) Expand 10 before | Expand all | Expand 10 after
8221 8221
8222 static bool IndexedAccessBlocker(Local<v8::Object> global, 8222 static bool IndexedAccessBlocker(Local<v8::Object> global,
8223 uint32_t key, 8223 uint32_t key,
8224 v8::AccessType type, 8224 v8::AccessType type,
8225 Local<Value> data) { 8225 Local<Value> data) {
8226 return Context::GetCurrent()->Global()->Equals(global) || 8226 return Context::GetCurrent()->Global()->Equals(global) ||
8227 allowed_access_type[type]; 8227 allowed_access_type[type];
8228 } 8228 }
8229 8229
8230 8230
8231 static int g_echo_value = -1; 8231 static int g_echo_value_1 = -1;
8232 static int g_echo_value_2 = -1;
8233
8234
8232 static void EchoGetter( 8235 static void EchoGetter(
8233 Local<String> name, 8236 Local<String> name,
8234 const v8::PropertyCallbackInfo<v8::Value>& info) { 8237 const v8::PropertyCallbackInfo<v8::Value>& info) {
8235 info.GetReturnValue().Set(v8_num(g_echo_value)); 8238 info.GetReturnValue().Set(v8_num(g_echo_value_1));
8236 } 8239 }
8237 8240
8238 8241
8242 static void EchoGetter(const v8::FunctionCallbackInfo<v8::Value>& info) {
8243 info.GetReturnValue().Set(v8_num(g_echo_value_2));
8244 }
8245
8246
8239 static void EchoSetter(Local<String> name, 8247 static void EchoSetter(Local<String> name,
8240 Local<Value> value, 8248 Local<Value> value,
8241 const v8::PropertyCallbackInfo<void>&) { 8249 const v8::PropertyCallbackInfo<void>&) {
8242 if (value->IsNumber()) 8250 if (value->IsNumber())
8243 g_echo_value = value->Int32Value(); 8251 g_echo_value_1 = value->Int32Value();
8244 } 8252 }
8245 8253
8246 8254
8255 static void EchoSetter(const v8::FunctionCallbackInfo<v8::Value>& info) {
8256 v8::Handle<v8::Value> value = info[0];
8257 if (value->IsNumber())
8258 g_echo_value_2 = value->Int32Value();
8259 }
8260
8261
8247 static void UnreachableGetter( 8262 static void UnreachableGetter(
8248 Local<String> name, 8263 Local<String> name,
8249 const v8::PropertyCallbackInfo<v8::Value>& info) { 8264 const v8::PropertyCallbackInfo<v8::Value>& info) {
8250 CHECK(false); // This function should not be called.. 8265 CHECK(false); // This function should not be called..
8251 } 8266 }
8252 8267
8253 8268
8254 static void UnreachableSetter(Local<String>, 8269 static void UnreachableSetter(Local<String>,
8255 Local<Value>, 8270 Local<Value>,
8256 const v8::PropertyCallbackInfo<void>&) { 8271 const v8::PropertyCallbackInfo<void>&) {
8257 CHECK(false); // This function should nto be called. 8272 CHECK(false); // This function should nto be called.
8258 } 8273 }
8259 8274
8260 8275
8276 static void UnreachableFunction(
8277 const v8::FunctionCallbackInfo<v8::Value>& info) {
8278 CHECK(false); // This function should not be called..
8279 }
8280
8281
8261 TEST(AccessControl) { 8282 TEST(AccessControl) {
8262 v8::Isolate* isolate = v8::Isolate::GetCurrent(); 8283 v8::Isolate* isolate = v8::Isolate::GetCurrent();
8263 v8::HandleScope handle_scope(isolate); 8284 v8::HandleScope handle_scope(isolate);
8264 v8::Handle<v8::ObjectTemplate> global_template = v8::ObjectTemplate::New(); 8285 v8::Handle<v8::ObjectTemplate> global_template = v8::ObjectTemplate::New();
8265 8286
8266 global_template->SetAccessCheckCallbacks(NamedAccessBlocker, 8287 global_template->SetAccessCheckCallbacks(NamedAccessBlocker,
8267 IndexedAccessBlocker); 8288 IndexedAccessBlocker);
8268 8289
8269 // Add an accessor accessible by cross-domain JS code. 8290 // Add an accessor accessible by cross-domain JS code.
8270 global_template->SetAccessor( 8291 global_template->SetAccessor(
8271 v8_str("accessible_prop"), 8292 v8_str("accessible_prop"),
8272 EchoGetter, EchoSetter, 8293 EchoGetter, EchoSetter,
8273 v8::Handle<Value>(), 8294 v8::Handle<Value>(),
8274 v8::AccessControl(v8::ALL_CAN_READ | v8::ALL_CAN_WRITE)); 8295 v8::AccessControl(v8::ALL_CAN_READ | v8::ALL_CAN_WRITE));
8275 8296
8297 global_template->SetJsAccessor(
8298 isolate,
8299 v8_str("accessible_js_prop"),
8300 v8::FunctionTemplate::New(EchoGetter),
8301 v8::FunctionTemplate::New(EchoSetter),
8302 v8::AccessControl(v8::ALL_CAN_READ | v8::ALL_CAN_WRITE));
8303
8276 // Add an accessor that is not accessible by cross-domain JS code. 8304 // Add an accessor that is not accessible by cross-domain JS code.
8277 global_template->SetAccessor(v8_str("blocked_prop"), 8305 global_template->SetAccessor(v8_str("blocked_prop"),
8278 UnreachableGetter, UnreachableSetter, 8306 UnreachableGetter, UnreachableSetter,
8279 v8::Handle<Value>(), 8307 v8::Handle<Value>(),
8280 v8::DEFAULT); 8308 v8::DEFAULT);
8281 8309
8310 global_template->SetJsAccessor(
8311 isolate,
8312 v8_str("blocked_js_prop"),
8313 v8::FunctionTemplate::New(UnreachableFunction),
8314 v8::FunctionTemplate::New(UnreachableFunction),
8315 v8::DEFAULT);
8316
8282 // Create an environment 8317 // Create an environment
8283 v8::Local<Context> context0 = Context::New(isolate, NULL, global_template); 8318 v8::Local<Context> context0 = Context::New(isolate, NULL, global_template);
8284 context0->Enter(); 8319 context0->Enter();
8285 8320
8286 v8::Handle<v8::Object> global0 = context0->Global(); 8321 v8::Handle<v8::Object> global0 = context0->Global();
8287 8322
8288 // Define a property with JS getter and setter. 8323 // Define a property with JS getter and setter.
8289 CompileRun( 8324 CompileRun(
8290 "function getter() { return 'getter'; };\n" 8325 "function getter() { return 'getter'; };\n"
8291 "function setter() { return 'setter'; }\n" 8326 "function setter() { return 'setter'; }\n"
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
8464 allowed_access_type[v8::ACCESS_SET] = false; 8499 allowed_access_type[v8::ACCESS_SET] = false;
8465 allowed_access_type[v8::ACCESS_GET] = false; 8500 allowed_access_type[v8::ACCESS_GET] = false;
8466 allowed_access_type[v8::ACCESS_HAS] = false; 8501 allowed_access_type[v8::ACCESS_HAS] = false;
8467 8502
8468 v8::Handle<Value> value; 8503 v8::Handle<Value> value;
8469 8504
8470 // Access accessible property 8505 // Access accessible property
8471 value = CompileRun("other.accessible_prop = 3"); 8506 value = CompileRun("other.accessible_prop = 3");
8472 CHECK(value->IsNumber()); 8507 CHECK(value->IsNumber());
8473 CHECK_EQ(3, value->Int32Value()); 8508 CHECK_EQ(3, value->Int32Value());
8474 CHECK_EQ(3, g_echo_value); 8509 CHECK_EQ(3, g_echo_value_1);
8510
8511 // Access accessible js property
8512 value = CompileRun("other.accessible_js_prop = 3");
8513 CHECK(value->IsNumber());
8514 CHECK_EQ(3, value->Int32Value());
8515 CHECK_EQ(3, g_echo_value_2);
8475 8516
8476 value = CompileRun("other.accessible_prop"); 8517 value = CompileRun("other.accessible_prop");
8477 CHECK(value->IsNumber()); 8518 CHECK(value->IsNumber());
8478 CHECK_EQ(3, value->Int32Value()); 8519 CHECK_EQ(3, value->Int32Value());
8479 8520
8521 value = CompileRun("other.accessible_js_prop");
8522 CHECK(value->IsNumber());
8523 CHECK_EQ(3, value->Int32Value());
8524
8480 value = CompileRun( 8525 value = CompileRun(
8481 "Object.getOwnPropertyDescriptor(other, 'accessible_prop').value"); 8526 "Object.getOwnPropertyDescriptor(other, 'accessible_prop').value");
8482 CHECK(value->IsNumber()); 8527 CHECK(value->IsNumber());
8483 CHECK_EQ(3, value->Int32Value()); 8528 CHECK_EQ(3, value->Int32Value());
8484 8529
8530 // TODO(dcarney): not sure why this isn't working
8531 value = CompileRun(
8532 "Object.getOwnPropertyDescriptor(other, 'accessible_js_prop').get()");
8533 CHECK(value->IsNumber());
8534 CHECK_EQ(3, value->Int32Value());
8535
8485 value = CompileRun("propertyIsEnumerable.call(other, 'accessible_prop')"); 8536 value = CompileRun("propertyIsEnumerable.call(other, 'accessible_prop')");
8486 CHECK(value->IsTrue()); 8537 CHECK(value->IsTrue());
8487 8538
8539 value = CompileRun("propertyIsEnumerable.call(other, 'accessible_js_prop')");
8540 CHECK(value->IsTrue());
8541
8488 // Enumeration doesn't enumerate accessors from inaccessible objects in 8542 // Enumeration doesn't enumerate accessors from inaccessible objects in
8489 // the prototype chain even if the accessors are in themselves accessible. 8543 // the prototype chain even if the accessors are in themselves accessible.
8490 value = 8544 value =
8491 CompileRun("(function(){var obj = {'__proto__':other};" 8545 CompileRun("(function(){var obj = {'__proto__':other};"
8492 "for (var p in obj)" 8546 "for (var p in obj)"
8493 " if (p == 'accessible_prop' || p == 'blocked_prop') {" 8547 " if (p == 'accessible_prop' ||"
8548 " p == 'accessible_js_prop' ||"
8549 " p == 'blocked_js_prop' ||"
8550 " p == 'blocked_js_prop') {"
8494 " return false;" 8551 " return false;"
8495 " }" 8552 " }"
8496 "return true;})()"); 8553 "return true;})()");
8497 CHECK(value->IsTrue()); 8554 CHECK(value->IsTrue());
8498 8555
8499 context1->Exit(); 8556 context1->Exit();
8500 context0->Exit(); 8557 context0->Exit();
8501 } 8558 }
8502 8559
8503 8560
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
8555 CompileRun("Object.freeze(other)"); 8612 CompileRun("Object.freeze(other)");
8556 ExpectTrue("Object.isExtensible(other)"); 8613 ExpectTrue("Object.isExtensible(other)");
8557 8614
8558 CompileRun("Object.seal(other)"); 8615 CompileRun("Object.seal(other)");
8559 ExpectTrue("Object.isExtensible(other)"); 8616 ExpectTrue("Object.isExtensible(other)");
8560 8617
8561 // Regression test for issue 1250. 8618 // Regression test for issue 1250.
8562 // Make sure that we can set the accessible accessors value using normal 8619 // Make sure that we can set the accessible accessors value using normal
8563 // assignment. 8620 // assignment.
8564 CompileRun("other.accessible_prop = 42"); 8621 CompileRun("other.accessible_prop = 42");
8565 CHECK_EQ(42, g_echo_value); 8622 CHECK_EQ(42, g_echo_value_1);
8566 8623
8567 v8::Handle<Value> value; 8624 v8::Handle<Value> value;
8568 // We follow Safari in ignoring assignments to host object accessors. 8625 // We follow Safari in ignoring assignments to host object accessors.
8569 CompileRun("Object.defineProperty(other, 'accessible_prop', {value: -1})"); 8626 CompileRun("Object.defineProperty(other, 'accessible_prop', {value: -1})");
8570 value = CompileRun("other.accessible_prop == 42"); 8627 value = CompileRun("other.accessible_prop == 42");
8571 CHECK(value->IsTrue()); 8628 CHECK(value->IsTrue());
8572 } 8629 }
8573 8630
8574 8631
8575 static bool GetOwnPropertyNamesNamedBlocker(Local<v8::Object> global, 8632 static bool GetOwnPropertyNamesNamedBlocker(Local<v8::Object> global,
(...skipping 11591 matching lines...) Expand 10 before | Expand all | Expand 10 after
20167 CheckCorrectThrow("%HasProperty(other, 'x')"); 20224 CheckCorrectThrow("%HasProperty(other, 'x')");
20168 CheckCorrectThrow("%HasElement(other, 1)"); 20225 CheckCorrectThrow("%HasElement(other, 1)");
20169 CheckCorrectThrow("%IsPropertyEnumerable(other, 'x')"); 20226 CheckCorrectThrow("%IsPropertyEnumerable(other, 'x')");
20170 CheckCorrectThrow("%GetPropertyNames(other)"); 20227 CheckCorrectThrow("%GetPropertyNames(other)");
20171 CheckCorrectThrow("%GetLocalPropertyNames(other, true)"); 20228 CheckCorrectThrow("%GetLocalPropertyNames(other, true)");
20172 CheckCorrectThrow("%DefineOrRedefineAccessorProperty(" 20229 CheckCorrectThrow("%DefineOrRedefineAccessorProperty("
20173 "other, 'x', null, null, 1)"); 20230 "other, 'x', null, null, 1)");
20174 } 20231 }
20175 20232
20176 #endif // WIN32 20233 #endif // WIN32
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698