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

Side by Side Diff: Source/bindings/core/v8/V8DOMConfiguration.cpp

Issue 922233002: bindings: Makes runtime-enabled attributes on prototype chains compilable. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Addressed a review comment. Created 5 years, 10 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 /* 1 /*
2 * Copyright (C) 2012 Google Inc. All rights reserved. 2 * Copyright (C) 2012 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 7 *
8 * 1. Redistributions of source code must retain the above copyright 8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright 10 * 2. Redistributions in binary form must reproduce the above copyright
(...skipping 16 matching lines...) Expand all
27 */ 27 */
28 28
29 #include "config.h" 29 #include "config.h"
30 #include "bindings/core/v8/V8DOMConfiguration.h" 30 #include "bindings/core/v8/V8DOMConfiguration.h"
31 31
32 #include "bindings/core/v8/V8ObjectConstructor.h" 32 #include "bindings/core/v8/V8ObjectConstructor.h"
33 #include "platform/TraceEvent.h" 33 #include "platform/TraceEvent.h"
34 34
35 namespace blink { 35 namespace blink {
36 36
37 void V8DOMConfiguration::installAttributes(v8::Isolate* isolate, v8::Handle<v8:: ObjectTemplate> instanceTemplate, v8::Handle<v8::ObjectTemplate> prototype, cons t AttributeConfiguration* attributes, size_t attributeCount)
38 {
39 for (size_t i = 0; i < attributeCount; ++i)
40 installAttribute(instanceTemplate, prototype, attributes[i], isolate);
41 }
42
43 void V8DOMConfiguration::installAccessors(v8::Isolate* isolate, v8::Handle<v8::O bjectTemplate> prototype, v8::Handle<v8::Signature> signature, const AccessorCon figuration* accessors, size_t accessorCount)
44 {
45 DOMWrapperWorld& world = DOMWrapperWorld::current(isolate);
46 for (size_t i = 0; i < accessorCount; ++i) {
47 if (accessors[i].exposeConfiguration == OnlyExposedToPrivateScript && !w orld.isPrivateScriptIsolatedWorld())
48 continue;
49
50 v8::FunctionCallback getterCallback = accessors[i].getter;
51 v8::FunctionCallback setterCallback = accessors[i].setter;
52 if (world.isMainWorld()) {
53 if (accessors[i].getterForMainWorld)
54 getterCallback = accessors[i].getterForMainWorld;
55 if (accessors[i].setterForMainWorld)
56 setterCallback = accessors[i].setterForMainWorld;
57 }
58
59 v8::Local<v8::FunctionTemplate> getter;
60 if (getterCallback) {
61 getter = v8::FunctionTemplate::New(isolate, getterCallback, v8::Exte rnal::New(isolate, const_cast<WrapperTypeInfo*>(accessors[i].data)), signature, 0);
62 getter->RemovePrototype();
63 }
64 v8::Local<v8::FunctionTemplate> setter;
65 if (setterCallback) {
66 setter = v8::FunctionTemplate::New(isolate, setterCallback, v8::Exte rnal::New(isolate, const_cast<WrapperTypeInfo*>(accessors[i].data)), signature, 1);
67 setter->RemovePrototype();
68 }
69 prototype->SetAccessorProperty(v8AtomicString(isolate, accessors[i].name ), getter, setter, accessors[i].attribute, accessors[i].settings);
70 }
71 }
72
73 // Constant installation
haraken 2015/02/16 13:23:20 Can we keep this comment?
Yuki 2015/02/16 15:02:05 Done.
74 //
75 // installConstants() is be used for simple constants. It installs constants
76 // using v8::Template::Set(), which results in a property that is much faster to
77 // access from scripts.
78 // installConstant() is used when some C++ code needs to be executed when the
79 // constant is accessed, e.g. to handle deprecation or measuring usage. The
80 // property appears the same to scripts, but is slower to access.
81
82 void V8DOMConfiguration::installConstants(v8::Isolate* isolate, v8::Handle<v8::F unctionTemplate> functionDescriptor, v8::Handle<v8::ObjectTemplate> prototype, c onst ConstantConfiguration* constants, size_t constantCount)
83 {
84 for (size_t i = 0; i < constantCount; ++i) {
85 const ConstantConfiguration* constant = &constants[i];
86 v8::Handle<v8::String> constantName = v8AtomicString(isolate, constant-> name);
87 switch (constant->type) {
88 case ConstantTypeShort:
89 case ConstantTypeLong:
90 case ConstantTypeUnsignedShort:
91 functionDescriptor->Set(constantName, v8::Integer::New(isolate, cons tant->ivalue), static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete) );
92 prototype->Set(constantName, v8::Integer::New(isolate, constant->iva lue), static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete));
93 break;
94 case ConstantTypeUnsignedLong:
95 functionDescriptor->Set(constantName, v8::Integer::NewFromUnsigned(i solate, constant->ivalue), static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8: :DontDelete));
96 prototype->Set(constantName, v8::Integer::NewFromUnsigned(isolate, c onstant->ivalue), static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDele te));
97 break;
98 case ConstantTypeFloat:
99 case ConstantTypeDouble:
100 functionDescriptor->Set(constantName, v8::Number::New(isolate, const ant->dvalue), static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete)) ;
101 prototype->Set(constantName, v8::Number::New(isolate, constant->dval ue), static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete));
102 break;
103 case ConstantTypeString:
104 functionDescriptor->Set(constantName, v8::String::NewFromUtf8(isolat e, constant->svalue), static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::Dont Delete));
105 prototype->Set(constantName, v8::String::NewFromUtf8(isolate, consta nt->svalue), static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete));
106 break;
107 default:
108 ASSERT_NOT_REACHED();
109 }
110 }
111 }
112
113 void V8DOMConfiguration::installConstant(v8::Isolate* isolate, v8::Handle<v8::Fu nctionTemplate> functionDescriptor, v8::Handle<v8::ObjectTemplate> prototype, co nst char* name, v8::AccessorGetterCallback getter)
114 {
115 v8::Handle<v8::String> constantName = v8AtomicString(isolate, name);
116 functionDescriptor->SetNativeDataProperty(constantName, getter, 0, v8::Handl e<v8::Value>(), static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete ));
117 prototype->SetNativeDataProperty(constantName, getter, 0, v8::Handle<v8::Val ue>(), static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete));
118 }
119
120 void V8DOMConfiguration::installMethods(v8::Isolate* isolate, v8::Handle<v8::Obj ectTemplate> prototype, v8::Handle<v8::Signature> signature, v8::PropertyAttribu te attributes, const MethodConfiguration* callbacks, size_t callbackCount)
121 {
122 for (size_t i = 0; i < callbackCount; ++i)
123 installMethod(prototype, signature, attributes, callbacks[i], isolate);
124 }
125
126 v8::Handle<v8::FunctionTemplate> V8DOMConfiguration::functionTemplateForCallback (v8::Isolate* isolate, v8::Handle<v8::Signature> signature, v8::FunctionCallback callback, int length)
127 {
128 v8::Local<v8::FunctionTemplate> functionTemplate = v8::FunctionTemplate::New (isolate, callback, v8Undefined(), signature, length);
129 functionTemplate->RemovePrototype();
130 return functionTemplate;
131 }
132
133 v8::Local<v8::Signature> V8DOMConfiguration::installDOMClassTemplate(v8::Isolate * isolate, v8::Local<v8::FunctionTemplate> functionDescriptor, const char* inter faceName, v8::Handle<v8::FunctionTemplate> parentClass, size_t fieldCount, 37 v8::Local<v8::Signature> V8DOMConfiguration::installDOMClassTemplate(v8::Isolate * isolate, v8::Local<v8::FunctionTemplate> functionDescriptor, const char* inter faceName, v8::Handle<v8::FunctionTemplate> parentClass, size_t fieldCount,
134 const AttributeConfiguration* attributes, size_t attributeCount, 38 const AttributeConfiguration* attributes, size_t attributeCount,
135 const AccessorConfiguration* accessors, size_t accessorCount, 39 const AccessorConfiguration* accessors, size_t accessorCount,
136 const MethodConfiguration* callbacks, size_t callbackCount) 40 const MethodConfiguration* callbacks, size_t callbackCount)
137 { 41 {
138 functionDescriptor->SetClassName(v8AtomicString(isolate, interfaceName)); 42 functionDescriptor->SetClassName(v8AtomicString(isolate, interfaceName));
139 v8::Local<v8::ObjectTemplate> instanceTemplate = functionDescriptor->Instanc eTemplate(); 43 v8::Local<v8::ObjectTemplate> instanceTemplate = functionDescriptor->Instanc eTemplate();
140 instanceTemplate->SetInternalFieldCount(fieldCount); 44 instanceTemplate->SetInternalFieldCount(fieldCount);
141 if (!parentClass.IsEmpty()) { 45 if (!parentClass.IsEmpty()) {
142 functionDescriptor->Inherit(parentClass); 46 functionDescriptor->Inherit(parentClass);
(...skipping 21 matching lines...) Expand all
164 if (!result.IsEmpty()) 68 if (!result.IsEmpty())
165 return result; 69 return result;
166 70
167 TRACE_EVENT_SCOPED_SAMPLING_STATE("blink", "BuildDOMTemplate"); 71 TRACE_EVENT_SCOPED_SAMPLING_STATE("blink", "BuildDOMTemplate");
168 result = v8::FunctionTemplate::New(isolate, V8ObjectConstructor::isValidCons tructorMode); 72 result = v8::FunctionTemplate::New(isolate, V8ObjectConstructor::isValidCons tructorMode);
169 configureDOMClassTemplate(result, isolate); 73 configureDOMClassTemplate(result, isolate);
170 data->setDOMTemplate(wrapperTypeInfo, result); 74 data->setDOMTemplate(wrapperTypeInfo, result);
171 return result; 75 return result;
172 } 76 }
173 77
78 template<class ObjectOrTemplate>
79 void V8DOMConfiguration::installAttributeInternal(v8::Isolate* isolate, v8::Hand le<ObjectOrTemplate> instanceTemplate, v8::Handle<ObjectOrTemplate> prototype, c onst AttributeConfiguration& attribute, const DOMWrapperWorld& world)
80 {
81 if (attribute.exposeConfiguration == OnlyExposedToPrivateScript
82 && !world.isPrivateScriptIsolatedWorld())
83 return;
84
85 v8::AccessorGetterCallback getter = attribute.getter;
86 v8::AccessorSetterCallback setter = attribute.setter;
87 if (world.isMainWorld()) {
88 if (attribute.getterForMainWorld)
89 getter = attribute.getterForMainWorld;
90 if (attribute.setterForMainWorld)
91 setter = attribute.setterForMainWorld;
92 }
93 v8::Handle<ObjectOrTemplate> target =
94 attribute.instanceOrPrototypeConfiguration == OnPrototype ?
95 prototype :
96 instanceTemplate;
97 target->SetAccessor(
98 v8AtomicString(isolate, attribute.name),
99 getter,
100 setter,
101 v8::External::New(isolate, const_cast<WrapperTypeInfo*>(attribute.data)) ,
102 attribute.settings,
103 attribute.attribute);
104 }
105
106 template
107 void V8DOMConfiguration::installAttributeInternal<v8::ObjectTemplate>(v8::Isolat e*, v8::Handle<v8::ObjectTemplate> instanceTemplate, v8::Handle<v8::ObjectTempla te> prototype, const AttributeConfiguration&, const DOMWrapperWorld&);
108 template
109 void V8DOMConfiguration::installAttributeInternal<v8::Object>(v8::Isolate*, v8:: Handle<v8::Object> instanceTemplate, v8::Handle<v8::Object> prototype, const Att ributeConfiguration&, const DOMWrapperWorld&);
haraken 2015/02/16 13:23:20 Not related to your CL, I'm wondering why we need
Yuki 2015/02/16 15:02:04 ObjectTemplate version is used for the most cases.
110
111 void V8DOMConfiguration::installAccessorInternal(v8::Isolate* isolate, v8::Handl e<v8::ObjectTemplate> prototype, v8::Handle<v8::Signature> signature, const Acce ssorConfiguration& accessor, const DOMWrapperWorld& world)
112 {
113 if (accessor.exposeConfiguration == OnlyExposedToPrivateScript
114 && !world.isPrivateScriptIsolatedWorld())
115 return;
116
117 v8::FunctionCallback getterCallback = accessor.getter;
118 v8::FunctionCallback setterCallback = accessor.setter;
119 if (world.isMainWorld()) {
120 if (accessor.getterForMainWorld)
121 getterCallback = accessor.getterForMainWorld;
122 if (accessor.setterForMainWorld)
123 setterCallback = accessor.setterForMainWorld;
124 }
125 v8::Local<v8::FunctionTemplate> getter;
126 if (getterCallback) {
127 getter = v8::FunctionTemplate::New(isolate, getterCallback, v8::External ::New(isolate, const_cast<WrapperTypeInfo*>(accessor.data)), signature, 0);
128 getter->RemovePrototype();
129 }
130 v8::Local<v8::FunctionTemplate> setter;
131 if (setterCallback) {
132 setter = v8::FunctionTemplate::New(isolate, setterCallback, v8::External ::New(isolate, const_cast<WrapperTypeInfo*>(accessor.data)), signature, 1);
133 setter->RemovePrototype();
134 }
135 prototype->SetAccessorProperty(
136 v8AtomicString(isolate, accessor.name),
137 getter,
138 setter,
139 accessor.attribute,
140 accessor.settings);
141 }
142
143 void V8DOMConfiguration::installConstantInternal(v8::Isolate* isolate, v8::Handl e<v8::FunctionTemplate> functionDescriptor, v8::Handle<v8::ObjectTemplate> proto type, const ConstantConfiguration& constant)
144 {
145 v8::Handle<v8::String> constantName = v8AtomicString(isolate, constant.name) ;
146 v8::PropertyAttribute attributes =
147 static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete);
148 v8::Handle<v8::Primitive> value;
149 switch (constant.type) {
150 case ConstantTypeShort:
151 case ConstantTypeLong:
152 case ConstantTypeUnsignedShort:
153 value = v8::Integer::New(isolate, constant.ivalue);
154 break;
155 case ConstantTypeUnsignedLong:
156 value = v8::Integer::NewFromUnsigned(isolate, constant.ivalue);
157 break;
158 case ConstantTypeFloat:
159 case ConstantTypeDouble:
160 value = v8::Number::New(isolate, constant.dvalue);
161 break;
162 case ConstantTypeString:
163 value = v8::String::NewFromUtf8(isolate, constant.svalue);
164 break;
165 default:
166 ASSERT_NOT_REACHED();
167 }
168 functionDescriptor->Set(constantName, value, attributes);
169 prototype->Set(constantName, value, attributes);
170 }
171
174 } // namespace blink 172 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698