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

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

Issue 1428793002: Reland v8::Private and related APIs (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: updates Created 5 years, 1 month 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
OLDNEW
1 // Copyright 2015 the V8 project authors. All rights reserved. 1 // Copyright 2015 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <stdlib.h> 5 #include <stdlib.h>
6 6
7 #include "test/cctest/test-api.h" 7 #include "test/cctest/test-api.h"
8 8
9 #include "include/v8-util.h" 9 #include "include/v8-util.h"
10 #include "src/api.h" 10 #include "src/api.h"
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
102 const v8::PropertyCallbackInfo<v8::Value>& 102 const v8::PropertyCallbackInfo<v8::Value>&
103 info) { // Intercept names that start with 'interceptor_'. 103 info) { // Intercept names that start with 'interceptor_'.
104 String::Utf8Value utf8(name); 104 String::Utf8Value utf8(name);
105 char* name_str = *utf8; 105 char* name_str = *utf8;
106 char prefix[] = "interceptor_"; 106 char prefix[] = "interceptor_";
107 int i; 107 int i;
108 for (i = 0; name_str[i] && prefix[i]; ++i) { 108 for (i = 0; name_str[i] && prefix[i]; ++i) {
109 if (name_str[i] != prefix[i]) return; 109 if (name_str[i] != prefix[i]) return;
110 } 110 }
111 Handle<Object> self = Handle<Object>::Cast(info.This()); 111 Handle<Object> self = Handle<Object>::Cast(info.This());
112 info.GetReturnValue().Set(self->GetHiddenValue(v8_str(name_str + i))); 112 info.GetReturnValue().Set(
113 self->GetPrivate(
114 info.GetIsolate()->GetCurrentContext(),
115 v8::Private::ForApi(info.GetIsolate(), v8_str(name_str + i)))
116 .ToLocalChecked());
113 } 117 }
114 118
115 119
116 void StringInterceptorSetter(Local<String> name, Local<Value> value, 120 void StringInterceptorSetter(Local<String> name, Local<Value> value,
117 const v8::PropertyCallbackInfo<v8::Value>& info) { 121 const v8::PropertyCallbackInfo<v8::Value>& info) {
118 // Intercept accesses that set certain integer values, for which the name does 122 // Intercept accesses that set certain integer values, for which the name does
119 // not start with 'accessor_'. 123 // not start with 'accessor_'.
120 String::Utf8Value utf8(name); 124 String::Utf8Value utf8(name);
121 char* name_str = *utf8; 125 char* name_str = *utf8;
122 char prefix[] = "accessor_"; 126 char prefix[] = "accessor_";
123 int i; 127 int i;
124 for (i = 0; name_str[i] && prefix[i]; ++i) { 128 for (i = 0; name_str[i] && prefix[i]; ++i) {
125 if (name_str[i] != prefix[i]) break; 129 if (name_str[i] != prefix[i]) break;
126 } 130 }
127 if (!prefix[i]) return; 131 if (!prefix[i]) return;
128 132
129 if (value->IsInt32() && value->Int32Value() < 10000) { 133 if (value->IsInt32() && value->Int32Value() < 10000) {
130 Handle<Object> self = Handle<Object>::Cast(info.This()); 134 Handle<Object> self = Handle<Object>::Cast(info.This());
131 self->SetHiddenValue(name, value); 135 Handle<Context> context = info.GetIsolate()->GetCurrentContext();
136 Handle<v8::Private> symbol = v8::Private::ForApi(info.GetIsolate(), name);
137 self->SetPrivate(context, symbol, value).FromJust();
132 info.GetReturnValue().Set(value); 138 info.GetReturnValue().Set(value);
133 } 139 }
134 } 140 }
135 141
136 void InterceptorGetter(Local<Name> generic_name, 142 void InterceptorGetter(Local<Name> generic_name,
137 const v8::PropertyCallbackInfo<v8::Value>& info) { 143 const v8::PropertyCallbackInfo<v8::Value>& info) {
138 if (generic_name->IsSymbol()) return; 144 if (generic_name->IsSymbol()) return;
139 StringInterceptorGetter(Local<String>::Cast(generic_name), info); 145 StringInterceptorGetter(Local<String>::Cast(generic_name), info);
140 } 146 }
141 147
(...skipping 1165 matching lines...) Expand 10 before | Expand all | Expand 10 after
1307 } 1313 }
1308 1314
1309 1315
1310 THREADED_TEST(HiddenPropertiesWithInterceptors) { 1316 THREADED_TEST(HiddenPropertiesWithInterceptors) {
1311 LocalContext context; 1317 LocalContext context;
1312 v8::Isolate* isolate = context->GetIsolate(); 1318 v8::Isolate* isolate = context->GetIsolate();
1313 v8::HandleScope scope(isolate); 1319 v8::HandleScope scope(isolate);
1314 1320
1315 interceptor_for_hidden_properties_called = false; 1321 interceptor_for_hidden_properties_called = false;
1316 1322
1317 v8::Local<v8::String> key = v8_str("api-test::hidden-key"); 1323 v8::Local<v8::Private> key =
1324 v8::Private::New(isolate, v8_str("api-test::hidden-key"));
1318 1325
1319 // Associate an interceptor with an object and start setting hidden values. 1326 // Associate an interceptor with an object and start setting hidden values.
1320 Local<v8::FunctionTemplate> fun_templ = v8::FunctionTemplate::New(isolate); 1327 Local<v8::FunctionTemplate> fun_templ = v8::FunctionTemplate::New(isolate);
1321 Local<v8::ObjectTemplate> instance_templ = fun_templ->InstanceTemplate(); 1328 Local<v8::ObjectTemplate> instance_templ = fun_templ->InstanceTemplate();
1322 instance_templ->SetHandler( 1329 instance_templ->SetHandler(
1323 v8::NamedPropertyHandlerConfiguration(InterceptorForHiddenProperties)); 1330 v8::NamedPropertyHandlerConfiguration(InterceptorForHiddenProperties));
1324 Local<v8::Function> function = fun_templ->GetFunction(); 1331 Local<v8::Function> function = fun_templ->GetFunction();
1325 Local<v8::Object> obj = function->NewInstance(); 1332 Local<v8::Object> obj = function->NewInstance();
1326 CHECK(obj->SetHiddenValue(key, v8::Integer::New(isolate, 2302))); 1333 CHECK(obj->SetPrivate(context.local(), key, v8::Integer::New(isolate, 2302))
1327 CHECK_EQ(2302, obj->GetHiddenValue(key)->Int32Value()); 1334 .FromJust());
1335 CHECK_EQ(
1336 2302,
1337 obj->GetPrivate(context.local(), key).ToLocalChecked()->Int32Value());
1328 CHECK(!interceptor_for_hidden_properties_called); 1338 CHECK(!interceptor_for_hidden_properties_called);
1329 } 1339 }
1330 1340
1331 1341
1332 static void XPropertyGetter(Local<Name> property, 1342 static void XPropertyGetter(Local<Name> property,
1333 const v8::PropertyCallbackInfo<v8::Value>& info) { 1343 const v8::PropertyCallbackInfo<v8::Value>& info) {
1334 ApiTestFuzzer::Fuzz(); 1344 ApiTestFuzzer::Fuzz();
1335 CHECK(info.Data()->IsUndefined()); 1345 CHECK(info.Data()->IsUndefined());
1336 info.GetReturnValue().Set(property); 1346 info.GetReturnValue().Set(property);
1337 } 1347 }
(...skipping 2033 matching lines...) Expand 10 before | Expand all | Expand 10 after
3371 "var obj = intercepted_1;" 3381 "var obj = intercepted_1;"
3372 "obj.x = 4;" 3382 "obj.x = 4;"
3373 "eval('obj.x');" 3383 "eval('obj.x');"
3374 "eval('obj.x');" 3384 "eval('obj.x');"
3375 "eval('obj.x');" 3385 "eval('obj.x');"
3376 "obj = intercepted_2;" 3386 "obj = intercepted_2;"
3377 "obj.x = 9;" 3387 "obj.x = 9;"
3378 "eval('obj.x');", 3388 "eval('obj.x');",
3379 9); 3389 9);
3380 } 3390 }
OLDNEW
« include/v8.h ('K') | « test/cctest/test-api.cc ('k') | test/cctest/test-debug.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698