| 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*, v8::Handle<v8::ObjectTemplate> i
nstanceTemplate, v8::Handle<v8::ObjectTemplate> prototype, const AttributeConfig
uration*, size_t attributeCount); |
| 72 |
| 73 static void installAttribute(v8::Isolate*, v8::Handle<v8::ObjectTemplate> in
stanceTemplate, v8::Handle<v8::ObjectTemplate> prototype, const AttributeConfigu
ration&); |
| 74 |
| 75 static void installAttribute(v8::Isolate*, v8::Handle<v8::Object> instanceTe
mplate, v8::Handle<v8::Object> prototype, const AttributeConfiguration&); |
| 76 |
| 71 // AccessorConfiguration translates into calls to SetAccessorProperty() | 77 // AccessorConfiguration translates into calls to SetAccessorProperty() |
| 72 // on prototype ObjectTemplate. | 78 // on prototype ObjectTemplate. |
| 73 struct AccessorConfiguration { | 79 struct AccessorConfiguration { |
| 74 const char* const name; | 80 const char* const name; |
| 75 v8::FunctionCallback getter; | 81 v8::FunctionCallback getter; |
| 76 v8::FunctionCallback setter; | 82 v8::FunctionCallback setter; |
| 77 v8::FunctionCallback getterForMainWorld; | 83 v8::FunctionCallback getterForMainWorld; |
| 78 v8::FunctionCallback setterForMainWorld; | 84 v8::FunctionCallback setterForMainWorld; |
| 79 const WrapperTypeInfo* data; | 85 const WrapperTypeInfo* data; |
| 80 v8::AccessControl settings; | 86 v8::AccessControl settings; |
| 81 v8::PropertyAttribute attribute; | 87 v8::PropertyAttribute attribute; |
| 82 ExposeConfiguration exposeConfiguration; | 88 ExposeConfiguration exposeConfiguration; |
| 83 }; | 89 }; |
| 84 | 90 |
| 85 static void installAttributes(v8::Isolate*, v8::Handle<v8::ObjectTemplate>,
v8::Handle<v8::ObjectTemplate>, const AttributeConfiguration*, size_t attributeC
ount); | 91 static void installAccessors(v8::Isolate*, v8::Handle<v8::ObjectTemplate> pr
ototype, v8::Handle<v8::Signature>, const AccessorConfiguration*, size_t accesso
rCount); |
| 86 | 92 |
| 87 template<class ObjectOrTemplate> | 93 static void installAccessor(v8::Isolate*, v8::Handle<v8::ObjectTemplate> pro
totype, v8::Handle<v8::Signature>, const AccessorConfiguration&); |
| 88 static inline void installAttribute(v8::Handle<ObjectOrTemplate> instanceTem
plate, v8::Handle<ObjectOrTemplate> prototype, const AttributeConfiguration& att
ribute, v8::Isolate* isolate) | |
| 89 { | |
| 90 DOMWrapperWorld& world = DOMWrapperWorld::current(isolate); | |
| 91 if (attribute.exposeConfiguration == OnlyExposedToPrivateScript && !worl
d.isPrivateScriptIsolatedWorld()) | |
| 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 } | |
| 109 | 94 |
| 110 enum ConstantType { | 95 enum ConstantType { |
| 111 ConstantTypeShort, | 96 ConstantTypeShort, |
| 112 ConstantTypeLong, | 97 ConstantTypeLong, |
| 113 ConstantTypeUnsignedShort, | 98 ConstantTypeUnsignedShort, |
| 114 ConstantTypeUnsignedLong, | 99 ConstantTypeUnsignedLong, |
| 115 ConstantTypeFloat, | 100 ConstantTypeFloat, |
| 116 ConstantTypeDouble, | 101 ConstantTypeDouble, |
| 117 ConstantTypeString | 102 ConstantTypeString |
| 118 }; | 103 }; |
| 119 | 104 |
| 120 // ConstantConfiguration translates into calls to Set() for setting up an | 105 // ConstantConfiguration translates into calls to Set() for setting up an |
| 121 // object's constants. It sets the constant on both the FunctionTemplate and | 106 // object's constants. It sets the constant on both the FunctionTemplate and |
| 122 // the ObjectTemplate. PropertyAttributes is always ReadOnly. | 107 // the ObjectTemplate. PropertyAttributes is always ReadOnly. |
| 123 struct ConstantConfiguration { | 108 struct ConstantConfiguration { |
| 124 const char* const name; | 109 const char* const name; |
| 125 int ivalue; | 110 int ivalue; |
| 126 double dvalue; | 111 double dvalue; |
| 127 const char* const svalue; | 112 const char* const svalue; |
| 128 ConstantType type; | 113 ConstantType type; |
| 129 }; | 114 }; |
| 130 | 115 |
| 131 static void installConstants(v8::Isolate*, v8::Handle<v8::FunctionTemplate>,
v8::Handle<v8::ObjectTemplate>, const ConstantConfiguration*, size_t constantCo
unt); | 116 // Constant installation |
| 132 static void installConstant(v8::Isolate*, v8::Handle<v8::FunctionTemplate>,
v8::Handle<v8::ObjectTemplate>, const char* name, v8::AccessorGetterCallback); | 117 // |
| 118 // installConstants and installConstant are used for simple constants. They |
| 119 // install constants using v8::Template::Set(), which results in a property |
| 120 // that is much faster to access from scripts. |
| 121 // installConstantWithGetter is used when some C++ code needs to be executed |
| 122 // when the constant is accessed, e.g. to handle deprecation or measuring |
| 123 // usage. The property appears the same to scripts, but is slower to access. |
| 124 static void installConstants(v8::Isolate*, v8::Handle<v8::FunctionTemplate>
functionDescriptor, v8::Handle<v8::ObjectTemplate> prototype, const ConstantConf
iguration*, size_t constantCount); |
| 125 |
| 126 static void installConstant(v8::Isolate*, v8::Handle<v8::FunctionTemplate> f
unctionDescriptor, v8::Handle<v8::ObjectTemplate> prototype, const ConstantConfi
guration&); |
| 127 |
| 128 static void installConstantWithGetter(v8::Isolate*, v8::Handle<v8::FunctionT
emplate> functionDescriptor, v8::Handle<v8::ObjectTemplate> prototype, const cha
r* name, v8::AccessorGetterCallback); |
| 133 | 129 |
| 134 // MethodConfiguration translates into calls to Set() for setting up an | 130 // MethodConfiguration translates into calls to Set() for setting up an |
| 135 // object's callbacks. It sets the method on both the FunctionTemplate or | 131 // object's callbacks. It sets the method on both the FunctionTemplate or |
| 136 // the ObjectTemplate. | 132 // the ObjectTemplate. |
| 137 struct MethodConfiguration { | 133 struct MethodConfiguration { |
| 138 v8::Local<v8::Name> methodName(v8::Isolate* isolate) const { return v8At
omicString(isolate, name); } | 134 v8::Local<v8::Name> methodName(v8::Isolate* isolate) const { return v8At
omicString(isolate, name); } |
| 139 v8::FunctionCallback callbackForWorld(const DOMWrapperWorld& world) cons
t | 135 v8::FunctionCallback callbackForWorld(const DOMWrapperWorld& world) cons
t |
| 140 { | 136 { |
| 141 return world.isMainWorld() && callbackForMainWorld ? callbackForMain
World : callback; | 137 return world.isMainWorld() && callbackForMainWorld ? callbackForMain
World : callback; |
| 142 } | 138 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 155 return callback; | 151 return callback; |
| 156 } | 152 } |
| 157 | 153 |
| 158 v8::Local<v8::Symbol> (*getSymbol)(v8::Isolate*); | 154 v8::Local<v8::Symbol> (*getSymbol)(v8::Isolate*); |
| 159 v8::FunctionCallback callback; | 155 v8::FunctionCallback callback; |
| 160 // SymbolKeyedMethodConfiguration doesn't support per-world bindings. | 156 // SymbolKeyedMethodConfiguration doesn't support per-world bindings. |
| 161 int length; | 157 int length; |
| 162 ExposeConfiguration exposeConfiguration; | 158 ExposeConfiguration exposeConfiguration; |
| 163 }; | 159 }; |
| 164 | 160 |
| 165 static void installMethods(v8::Isolate*, v8::Handle<v8::ObjectTemplate>, v8:
:Handle<v8::Signature>, v8::PropertyAttribute, const MethodConfiguration*, size_
t callbackCount); | 161 static void installMethods(v8::Isolate*, v8::Handle<v8::ObjectTemplate> prot
otype, v8::Handle<v8::Signature>, v8::PropertyAttribute, const MethodConfigurati
on*, size_t callbackCount); |
| 166 | 162 |
| 167 template <class ObjectOrTemplate, class Configuration> | 163 static void installMethod(v8::Isolate*, v8::Handle<v8::FunctionTemplate>, v8
::Handle<v8::Signature>, v8::PropertyAttribute, const MethodConfiguration&); |
| 168 static void installMethod(v8::Handle<ObjectOrTemplate> objectOrTemplate, v8:
:Handle<v8::Signature> signature, v8::PropertyAttribute attribute, const Configu
ration& callback, v8::Isolate* isolate) | |
| 169 { | |
| 170 DOMWrapperWorld& world = DOMWrapperWorld::current(isolate); | |
| 171 if (callback.exposeConfiguration == OnlyExposedToPrivateScript && !world
.isPrivateScriptIsolatedWorld()) | |
| 172 return; | |
| 173 | 164 |
| 174 v8::Local<v8::FunctionTemplate> functionTemplate = functionTemplateForCa
llback(isolate, signature, callback.callbackForWorld(world), callback.length); | 165 static void installMethod(v8::Isolate*, v8::Handle<v8::ObjectTemplate>, v8::
Handle<v8::Signature>, v8::PropertyAttribute, const MethodConfiguration&); |
| 175 setMethod(objectOrTemplate, callback.methodName(isolate), functionTempla
te, attribute); | |
| 176 } | |
| 177 | 166 |
| 178 static void installAccessors(v8::Isolate*, v8::Handle<v8::ObjectTemplate>, v
8::Handle<v8::Signature>, const AccessorConfiguration*, size_t accessorCount); | 167 static void installMethod(v8::Isolate*, v8::Handle<v8::ObjectTemplate>, v8::
Handle<v8::Signature>, v8::PropertyAttribute, const SymbolKeyedMethodConfigurati
on&); |
| 179 | 168 |
| 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, | 169 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, | 170 const AttributeConfiguration*, size_t attributeCount, |
| 182 const AccessorConfiguration*, size_t accessorCount, | 171 const AccessorConfiguration*, size_t accessorCount, |
| 183 const MethodConfiguration*, size_t callbackCount); | 172 const MethodConfiguration*, size_t callbackCount); |
| 184 | 173 |
| 185 static v8::Handle<v8::FunctionTemplate> domClassTemplate(v8::Isolate*, Wrapp
erTypeInfo*, void (*)(v8::Local<v8::FunctionTemplate>, v8::Isolate*)); | 174 static v8::Handle<v8::FunctionTemplate> domClassTemplate(v8::Isolate*, Wrapp
erTypeInfo*, void (*)(v8::Local<v8::FunctionTemplate>, v8::Isolate*)); |
| 186 | |
| 187 private: | |
| 188 static void setMethod(v8::Handle<v8::Object> target, v8::Handle<v8::Name> na
me, v8::Handle<v8::FunctionTemplate> functionTemplate, v8::PropertyAttribute att
ribute) | |
| 189 { | |
| 190 target->Set(name, functionTemplate->GetFunction()); | |
| 191 } | |
| 192 static void setMethod(v8::Handle<v8::FunctionTemplate> target, v8::Handle<v8
::Name> name, v8::Handle<v8::FunctionTemplate> functionTemplate, v8::PropertyAtt
ribute attribute) | |
| 193 { | |
| 194 target->Set(name, functionTemplate, attribute); | |
| 195 } | |
| 196 static void setMethod(v8::Handle<v8::ObjectTemplate> target, v8::Handle<v8::
Name> name, v8::Handle<v8::FunctionTemplate> functionTemplate, v8::PropertyAttri
bute attribute) | |
| 197 { | |
| 198 target->Set(name, functionTemplate, attribute); | |
| 199 } | |
| 200 | |
| 201 static v8::Handle<v8::FunctionTemplate> functionTemplateForCallback(v8::Isol
ate*, v8::Handle<v8::Signature>, v8::FunctionCallback, int length); | |
| 202 }; | 175 }; |
| 203 | 176 |
| 204 } // namespace blink | 177 } // namespace blink |
| 205 | 178 |
| 206 #endif // V8DOMConfiguration_h | 179 #endif // V8DOMConfiguration_h |
| OLD | NEW |