OLD | NEW |
---|---|
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 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
61 v8::AccessorSetterCallback setter; | 61 v8::AccessorSetterCallback setter; |
62 v8::AccessorGetterCallback getterForMainWorld; | 62 v8::AccessorGetterCallback getterForMainWorld; |
63 v8::AccessorSetterCallback setterForMainWorld; | 63 v8::AccessorSetterCallback setterForMainWorld; |
64 const WrapperTypeInfo* data; | 64 const WrapperTypeInfo* data; |
65 v8::AccessControl settings; | 65 v8::AccessControl settings; |
66 v8::PropertyAttribute attribute; | 66 v8::PropertyAttribute attribute; |
67 ExposeConfiguration exposeConfiguration; | 67 ExposeConfiguration exposeConfiguration; |
68 InstanceOrPrototypeConfiguration instanceOrPrototypeConfiguration; | 68 InstanceOrPrototypeConfiguration instanceOrPrototypeConfiguration; |
69 }; | 69 }; |
70 | 70 |
71 static void installAttributes(v8::Isolate* isolate, v8::Handle<v8::ObjectTem plate> instanceTemplate, v8::Handle<v8::ObjectTemplate> prototype, const Attribu teConfiguration* attributes, size_t attributeCount) | |
haraken
2015/02/16 13:23:20
Can we uninline all of these methods? I don't thin
Yuki
2015/02/16 15:02:05
Done.
| |
72 { | |
73 const DOMWrapperWorld& world = DOMWrapperWorld::current(isolate); | |
74 for (size_t i = 0; i < attributeCount; ++i) | |
75 installAttributeInternal(isolate, instanceTemplate, prototype, attri butes[i], world); | |
76 } | |
77 | |
78 static void installAttribute(v8::Isolate* isolate, v8::Handle<v8::ObjectTemp late> instanceTemplate, v8::Handle<v8::ObjectTemplate> prototype, const Attribut eConfiguration& attribute) | |
79 { | |
80 const DOMWrapperWorld& world = DOMWrapperWorld::current(isolate); | |
81 installAttributeInternal(isolate, instanceTemplate, prototype, attribute , world); | |
82 } | |
83 | |
84 static void installAttribute(v8::Isolate* isolate, v8::Handle<v8::Object> in stanceTemplate, v8::Handle<v8::Object> prototype, const AttributeConfiguration& attribute) | |
85 { | |
86 const DOMWrapperWorld& world = DOMWrapperWorld::current(isolate); | |
87 installAttributeInternal(isolate, instanceTemplate, prototype, attribute , world); | |
88 } | |
89 | |
71 // AccessorConfiguration translates into calls to SetAccessorProperty() | 90 // AccessorConfiguration translates into calls to SetAccessorProperty() |
72 // on prototype ObjectTemplate. | 91 // on prototype ObjectTemplate. |
73 struct AccessorConfiguration { | 92 struct AccessorConfiguration { |
74 const char* const name; | 93 const char* const name; |
75 v8::FunctionCallback getter; | 94 v8::FunctionCallback getter; |
76 v8::FunctionCallback setter; | 95 v8::FunctionCallback setter; |
77 v8::FunctionCallback getterForMainWorld; | 96 v8::FunctionCallback getterForMainWorld; |
78 v8::FunctionCallback setterForMainWorld; | 97 v8::FunctionCallback setterForMainWorld; |
79 const WrapperTypeInfo* data; | 98 const WrapperTypeInfo* data; |
80 v8::AccessControl settings; | 99 v8::AccessControl settings; |
81 v8::PropertyAttribute attribute; | 100 v8::PropertyAttribute attribute; |
82 ExposeConfiguration exposeConfiguration; | 101 ExposeConfiguration exposeConfiguration; |
83 }; | 102 }; |
84 | 103 |
85 static void installAttributes(v8::Isolate*, v8::Handle<v8::ObjectTemplate>, v8::Handle<v8::ObjectTemplate>, const AttributeConfiguration*, size_t attributeC ount); | 104 static void installAccessors(v8::Isolate* isolate, v8::Handle<v8::ObjectTemp late> prototype, v8::Handle<v8::Signature> signature, const AccessorConfiguratio n* accessors, size_t accessorCount) |
105 { | |
106 const DOMWrapperWorld& world = DOMWrapperWorld::current(isolate); | |
107 for (size_t i = 0; i < accessorCount; ++i) | |
108 installAccessorInternal(isolate, prototype, signature, accessors[i], world); | |
109 } | |
86 | 110 |
87 template<class ObjectOrTemplate> | 111 static void installAccessor(v8::Isolate* isolate, v8::Handle<v8::ObjectTempl ate> prototype, v8::Handle<v8::Signature> signature, const AccessorConfiguration & accessor) |
88 static inline void installAttribute(v8::Handle<ObjectOrTemplate> instanceTem plate, v8::Handle<ObjectOrTemplate> prototype, const AttributeConfiguration& att ribute, v8::Isolate* isolate) | |
89 { | 112 { |
90 DOMWrapperWorld& world = DOMWrapperWorld::current(isolate); | 113 const DOMWrapperWorld& world = DOMWrapperWorld::current(isolate); |
91 if (attribute.exposeConfiguration == OnlyExposedToPrivateScript && !worl d.isPrivateScriptIsolatedWorld()) | 114 installAccessorInternal(isolate, prototype, signature, accessor, world); |
92 return; | |
93 | |
94 v8::AccessorGetterCallback getter = attribute.getter; | |
95 v8::AccessorSetterCallback setter = attribute.setter; | |
96 if (world.isMainWorld()) { | |
97 if (attribute.getterForMainWorld) | |
98 getter = attribute.getterForMainWorld; | |
99 if (attribute.setterForMainWorld) | |
100 setter = attribute.setterForMainWorld; | |
101 } | |
102 (attribute.instanceOrPrototypeConfiguration == OnPrototype ? prototype : instanceTemplate)->SetAccessor(v8AtomicString(isolate, attribute.name), | |
103 getter, | |
104 setter, | |
105 v8::External::New(isolate, const_cast<WrapperTypeInfo*>(attribute.da ta)), | |
106 attribute.settings, | |
107 attribute.attribute); | |
108 } | 115 } |
109 | 116 |
110 enum ConstantType { | 117 enum ConstantType { |
111 ConstantTypeShort, | 118 ConstantTypeShort, |
112 ConstantTypeLong, | 119 ConstantTypeLong, |
113 ConstantTypeUnsignedShort, | 120 ConstantTypeUnsignedShort, |
114 ConstantTypeUnsignedLong, | 121 ConstantTypeUnsignedLong, |
115 ConstantTypeFloat, | 122 ConstantTypeFloat, |
116 ConstantTypeDouble, | 123 ConstantTypeDouble, |
117 ConstantTypeString | 124 ConstantTypeString |
118 }; | 125 }; |
119 | 126 |
120 // ConstantConfiguration translates into calls to Set() for setting up an | 127 // ConstantConfiguration translates into calls to Set() for setting up an |
121 // object's constants. It sets the constant on both the FunctionTemplate and | 128 // object's constants. It sets the constant on both the FunctionTemplate and |
122 // the ObjectTemplate. PropertyAttributes is always ReadOnly. | 129 // the ObjectTemplate. PropertyAttributes is always ReadOnly. |
123 struct ConstantConfiguration { | 130 struct ConstantConfiguration { |
124 const char* const name; | 131 const char* const name; |
125 int ivalue; | 132 int ivalue; |
126 double dvalue; | 133 double dvalue; |
127 const char* const svalue; | 134 const char* const svalue; |
128 ConstantType type; | 135 ConstantType type; |
129 }; | 136 }; |
130 | 137 |
131 static void installConstants(v8::Isolate*, v8::Handle<v8::FunctionTemplate>, v8::Handle<v8::ObjectTemplate>, const ConstantConfiguration*, size_t constantCo unt); | 138 static void installConstants(v8::Isolate* isolate, v8::Handle<v8::FunctionTe mplate> functionDescriptor, v8::Handle<v8::ObjectTemplate> prototype, const Cons tantConfiguration* constants, size_t constantCount) |
132 static void installConstant(v8::Isolate*, v8::Handle<v8::FunctionTemplate>, v8::Handle<v8::ObjectTemplate>, const char* name, v8::AccessorGetterCallback); | 139 { |
140 for (size_t i = 0; i < constantCount; ++i) | |
141 installConstantInternal(isolate, functionDescriptor, prototype, cons tants[i]); | |
142 } | |
143 | |
144 static void installConstant(v8::Isolate* isolate, v8::Handle<v8::FunctionTem plate> functionDescriptor, v8::Handle<v8::ObjectTemplate> prototype, const Const antConfiguration& constant) | |
145 { | |
146 installConstantInternal(isolate, functionDescriptor, prototype, constant ); | |
147 } | |
148 | |
149 static void installConstantWithGetter(v8::Isolate* isolate, v8::Handle<v8::F unctionTemplate> functionDescriptor, v8::Handle<v8::ObjectTemplate> prototype, c onst char* name, v8::AccessorGetterCallback getter) | |
150 { | |
151 v8::Handle<v8::String> constantName = v8AtomicString(isolate, name); | |
152 v8::PropertyAttribute attributes = | |
153 static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete); | |
154 functionDescriptor->SetNativeDataProperty(constantName, getter, 0, v8::H andle<v8::Value>(), attributes); | |
155 prototype->SetNativeDataProperty(constantName, getter, 0, v8::Handle<v8: :Value>(), attributes); | |
156 } | |
133 | 157 |
134 // MethodConfiguration translates into calls to Set() for setting up an | 158 // MethodConfiguration translates into calls to Set() for setting up an |
135 // object's callbacks. It sets the method on both the FunctionTemplate or | 159 // object's callbacks. It sets the method on both the FunctionTemplate or |
136 // the ObjectTemplate. | 160 // the ObjectTemplate. |
137 struct MethodConfiguration { | 161 struct MethodConfiguration { |
138 v8::Local<v8::Name> methodName(v8::Isolate* isolate) const { return v8At omicString(isolate, name); } | 162 v8::Local<v8::Name> methodName(v8::Isolate* isolate) const { return v8At omicString(isolate, name); } |
139 v8::FunctionCallback callbackForWorld(const DOMWrapperWorld& world) cons t | 163 v8::FunctionCallback callbackForWorld(const DOMWrapperWorld& world) cons t |
140 { | 164 { |
141 return world.isMainWorld() && callbackForMainWorld ? callbackForMain World : callback; | 165 return world.isMainWorld() && callbackForMainWorld ? callbackForMain World : callback; |
142 } | 166 } |
(...skipping 12 matching lines...) Expand all Loading... | |
155 return callback; | 179 return callback; |
156 } | 180 } |
157 | 181 |
158 v8::Local<v8::Symbol> (*getSymbol)(v8::Isolate*); | 182 v8::Local<v8::Symbol> (*getSymbol)(v8::Isolate*); |
159 v8::FunctionCallback callback; | 183 v8::FunctionCallback callback; |
160 // SymbolKeyedMethodConfiguration doesn't support per-world bindings. | 184 // SymbolKeyedMethodConfiguration doesn't support per-world bindings. |
161 int length; | 185 int length; |
162 ExposeConfiguration exposeConfiguration; | 186 ExposeConfiguration exposeConfiguration; |
163 }; | 187 }; |
164 | 188 |
165 static void installMethods(v8::Isolate*, v8::Handle<v8::ObjectTemplate>, v8: :Handle<v8::Signature>, v8::PropertyAttribute, const MethodConfiguration*, size_ t callbackCount); | 189 static void installMethods(v8::Isolate* isolate, v8::Handle<v8::ObjectTempla te> prototype, v8::Handle<v8::Signature> signature, v8::PropertyAttribute attrib ute, const MethodConfiguration* callbacks, size_t callbackCount) |
190 { | |
191 const DOMWrapperWorld& world = DOMWrapperWorld::current(isolate); | |
192 for (size_t i = 0; i < callbackCount; ++i) | |
193 installMethodInternal(isolate, prototype, signature, attribute, call backs[i], world); | |
194 } | |
166 | 195 |
167 template <class ObjectOrTemplate, class Configuration> | 196 template <class ObjectOrTemplate, class Configuration> |
168 static void installMethod(v8::Handle<ObjectOrTemplate> objectOrTemplate, v8: :Handle<v8::Signature> signature, v8::PropertyAttribute attribute, const Configu ration& callback, v8::Isolate* isolate) | 197 static void installMethod(v8::Isolate* isolate, v8::Handle<ObjectOrTemplate> objectOrTemplate, v8::Handle<v8::Signature> signature, v8::PropertyAttribute at tribute, const Configuration& callback) |
169 { | 198 { |
170 DOMWrapperWorld& world = DOMWrapperWorld::current(isolate); | 199 const DOMWrapperWorld& world = DOMWrapperWorld::current(isolate); |
171 if (callback.exposeConfiguration == OnlyExposedToPrivateScript && !world .isPrivateScriptIsolatedWorld()) | 200 installMethodInternal(isolate, objectOrTemplate, signature, attribute, c allback, world); |
172 return; | |
173 | |
174 v8::Local<v8::FunctionTemplate> functionTemplate = functionTemplateForCa llback(isolate, signature, callback.callbackForWorld(world), callback.length); | |
175 setMethod(objectOrTemplate, callback.methodName(isolate), functionTempla te, attribute); | |
176 } | 201 } |
177 | 202 |
178 static void installAccessors(v8::Isolate*, v8::Handle<v8::ObjectTemplate>, v 8::Handle<v8::Signature>, const AccessorConfiguration*, size_t accessorCount); | |
179 | |
180 static v8::Local<v8::Signature> installDOMClassTemplate(v8::Isolate*, v8::Lo cal<v8::FunctionTemplate>, const char* interfaceName, v8::Handle<v8::FunctionTem plate> parentClass, size_t fieldCount, | 203 static v8::Local<v8::Signature> installDOMClassTemplate(v8::Isolate*, v8::Lo cal<v8::FunctionTemplate>, const char* interfaceName, v8::Handle<v8::FunctionTem plate> parentClass, size_t fieldCount, |
181 const AttributeConfiguration*, size_t attributeCount, | 204 const AttributeConfiguration*, size_t attributeCount, |
182 const AccessorConfiguration*, size_t accessorCount, | 205 const AccessorConfiguration*, size_t accessorCount, |
183 const MethodConfiguration*, size_t callbackCount); | 206 const MethodConfiguration*, size_t callbackCount); |
184 | 207 |
185 static v8::Handle<v8::FunctionTemplate> domClassTemplate(v8::Isolate*, Wrapp erTypeInfo*, void (*)(v8::Local<v8::FunctionTemplate>, v8::Isolate*)); | 208 static v8::Handle<v8::FunctionTemplate> domClassTemplate(v8::Isolate*, Wrapp erTypeInfo*, void (*)(v8::Local<v8::FunctionTemplate>, v8::Isolate*)); |
186 | 209 |
187 private: | 210 private: |
211 template<class ObjectOrTemplate> | |
212 static void installAttributeInternal(v8::Isolate*, v8::Handle<ObjectOrTempla te> instanceTemplate, v8::Handle<ObjectOrTemplate> prototype, const AttributeCon figuration&, const DOMWrapperWorld&); | |
213 | |
214 static void installAccessorInternal(v8::Isolate*, v8::Handle<v8::ObjectTempl ate> prototype, v8::Handle<v8::Signature>, const AccessorConfiguration&, const D OMWrapperWorld&); | |
215 | |
216 static void installConstantInternal(v8::Isolate*, v8::Handle<v8::FunctionTem plate> functionDescriptor, v8::Handle<v8::ObjectTemplate> prototype, const Const antConfiguration&); | |
217 | |
218 template<class ObjectOrTemplate, class Configuration> | |
219 static void installMethodInternal(v8::Isolate* isolate, v8::Handle<ObjectOrT emplate> objectOrTemplate, v8::Handle<v8::Signature> signature, v8::PropertyAttr ibute attribute, const Configuration& callback, const DOMWrapperWorld& world) | |
220 { | |
221 if (callback.exposeConfiguration == OnlyExposedToPrivateScript | |
222 && !world.isPrivateScriptIsolatedWorld()) | |
223 return; | |
224 | |
225 v8::Local<v8::FunctionTemplate> functionTemplate = v8::FunctionTemplate: :New(isolate, callback.callbackForWorld(world), v8Undefined(), signature, callba ck.length); | |
226 functionTemplate->RemovePrototype(); | |
227 setMethod(objectOrTemplate, callback.methodName(isolate), functionTempla te, attribute); | |
228 } | |
229 | |
188 static void setMethod(v8::Handle<v8::Object> target, v8::Handle<v8::Name> na me, v8::Handle<v8::FunctionTemplate> functionTemplate, v8::PropertyAttribute att ribute) | 230 static void setMethod(v8::Handle<v8::Object> target, v8::Handle<v8::Name> na me, v8::Handle<v8::FunctionTemplate> functionTemplate, v8::PropertyAttribute att ribute) |
189 { | 231 { |
190 target->Set(name, functionTemplate->GetFunction()); | 232 target->Set(name, functionTemplate->GetFunction()); |
191 } | 233 } |
192 static void setMethod(v8::Handle<v8::FunctionTemplate> target, v8::Handle<v8 ::Name> name, v8::Handle<v8::FunctionTemplate> functionTemplate, v8::PropertyAtt ribute attribute) | 234 static void setMethod(v8::Handle<v8::FunctionTemplate> target, v8::Handle<v8 ::Name> name, v8::Handle<v8::FunctionTemplate> functionTemplate, v8::PropertyAtt ribute attribute) |
193 { | 235 { |
194 target->Set(name, functionTemplate, attribute); | 236 target->Set(name, functionTemplate, attribute); |
195 } | 237 } |
196 static void setMethod(v8::Handle<v8::ObjectTemplate> target, v8::Handle<v8:: Name> name, v8::Handle<v8::FunctionTemplate> functionTemplate, v8::PropertyAttri bute attribute) | 238 static void setMethod(v8::Handle<v8::ObjectTemplate> target, v8::Handle<v8:: Name> name, v8::Handle<v8::FunctionTemplate> functionTemplate, v8::PropertyAttri bute attribute) |
197 { | 239 { |
198 target->Set(name, functionTemplate, attribute); | 240 target->Set(name, functionTemplate, attribute); |
199 } | 241 } |
200 | |
201 static v8::Handle<v8::FunctionTemplate> functionTemplateForCallback(v8::Isol ate*, v8::Handle<v8::Signature>, v8::FunctionCallback, int length); | |
202 }; | 242 }; |
203 | 243 |
204 } // namespace blink | 244 } // namespace blink |
205 | 245 |
206 #endif // V8DOMConfiguration_h | 246 #endif // V8DOMConfiguration_h |
OLD | NEW |