[V8] Get rid of function-level static FunctionTemplates in generated bindings code
[WebKit-https.git] / Source / WebCore / bindings / scripts / test / V8 / V8TestActiveDOMObject.cpp
1 /*
2     This file is part of the WebKit open source project.
3     This file has been generated by generate-bindings.pl. DO NOT MODIFY!
4
5     This library is free software; you can redistribute it and/or
6     modify it under the terms of the GNU Library General Public
7     License as published by the Free Software Foundation; either
8     version 2 of the License, or (at your option) any later version.
9
10     This library is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13     Library General Public License for more details.
14
15     You should have received a copy of the GNU Library General Public License
16     along with this library; see the file COPYING.LIB.  If not, write to
17     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18     Boston, MA 02111-1307, USA.
19 */
20
21 #include "config.h"
22 #include "V8TestActiveDOMObject.h"
23
24 #include "BindingState.h"
25 #include "ContextFeatures.h"
26 #include "ExceptionCode.h"
27 #include "Frame.h"
28 #include "RuntimeEnabledFeatures.h"
29 #include "V8Binding.h"
30 #include "V8DOMWrapper.h"
31 #include "V8Node.h"
32 #include <wtf/UnusedParam.h>
33
34 #if ENABLE(BINDING_INTEGRITY)
35 #if defined(OS_WIN)
36 #pragma warning(disable: 4483)
37 extern "C" { extern void (*const __identifier("??_7TestActiveDOMObject@WebCore@@6B@")[])(); }
38 #else
39 extern "C" { extern void* _ZTVN7WebCore19TestActiveDOMObjectE[]; }
40 #endif
41 #endif // ENABLE(BINDING_INTEGRITY)
42
43 namespace WebCore {
44
45 #if ENABLE(BINDING_INTEGRITY)
46 // This checks if a DOM object that is about to be wrapped is valid.
47 // Specifically, it checks that a vtable of the DOM object is equal to
48 // a vtable of an expected class.
49 // Due to a dangling pointer, the DOM object you are wrapping might be
50 // already freed or realloced. If freed, the check will fail because
51 // a free list pointer should be stored at the head of the DOM object.
52 // If realloced, the check will fail because the vtable of the DOM object
53 // differs from the expected vtable (unless the same class of DOM object
54 // is realloced on the slot).
55 inline void checkTypeOrDieTrying(TestActiveDOMObject* object)
56 {
57     void* actualVTablePointer = *(reinterpret_cast<void**>(object));
58 #if defined(OS_WIN)
59     void* expectedVTablePointer = reinterpret_cast<void*>(__identifier("??_7TestActiveDOMObject@WebCore@@6B@"));
60 #else
61     void* expectedVTablePointer = &_ZTVN7WebCore19TestActiveDOMObjectE[2];
62 #endif
63     if (actualVTablePointer != expectedVTablePointer)
64         CRASH();
65 }
66 #endif // ENABLE(BINDING_INTEGRITY)
67
68 WrapperTypeInfo V8TestActiveDOMObject::info = { V8TestActiveDOMObject::GetTemplate, V8TestActiveDOMObject::derefObject, 0, 0, 0, V8TestActiveDOMObject::installPerContextPrototypeProperties, 0, WrapperTypeObjectPrototype };
69
70 namespace TestActiveDOMObjectV8Internal {
71
72 template <typename T> void V8_USE(T) { }
73
74 static v8::Handle<v8::Value> excitingAttrAttrGetter(v8::Local<v8::String> name, const v8::AccessorInfo& info)
75 {
76     TestActiveDOMObject* imp = V8TestActiveDOMObject::toNative(info.Holder());
77     return v8Integer(imp->excitingAttr(), info.GetIsolate());
78 }
79
80 static v8::Handle<v8::Value> excitingAttrAttrGetterCallback(v8::Local<v8::String> name, const v8::AccessorInfo& info)
81 {
82     return TestActiveDOMObjectV8Internal::excitingAttrAttrGetter(name, info);
83 }
84
85 bool indexedSecurityCheck(v8::Local<v8::Object> host, uint32_t index, v8::AccessType type, v8::Local<v8::Value>)
86 {
87     TestActiveDOMObject* imp =  V8TestActiveDOMObject::toNative(host);
88     return BindingSecurity::shouldAllowAccessToFrame(BindingState::instance(), imp->frame(), DoNotReportSecurityError);
89 }
90
91 bool namedSecurityCheck(v8::Local<v8::Object> host, v8::Local<v8::Value> key, v8::AccessType type, v8::Local<v8::Value>)
92 {
93     TestActiveDOMObject* imp =  V8TestActiveDOMObject::toNative(host);
94     return BindingSecurity::shouldAllowAccessToFrame(BindingState::instance(), imp->frame(), DoNotReportSecurityError);
95 }
96
97 static v8::Handle<v8::Value> excitingFunctionMethod(const v8::Arguments& args)
98 {
99     if (args.Length() < 1)
100         return throwNotEnoughArgumentsError(args.GetIsolate());
101     TestActiveDOMObject* imp = V8TestActiveDOMObject::toNative(args.Holder());
102     if (!BindingSecurity::shouldAllowAccessToFrame(BindingState::instance(), imp->frame()))
103         return v8Undefined();
104     V8TRYCATCH(Node*, nextChild, V8Node::HasInstance(args[0], args.GetIsolate()) ? V8Node::toNative(v8::Handle<v8::Object>::Cast(args[0])) : 0);
105     imp->excitingFunction(nextChild);
106     return v8Undefined();
107 }
108
109 static v8::Handle<v8::Value> excitingFunctionMethodCallback(const v8::Arguments& args)
110 {
111     return TestActiveDOMObjectV8Internal::excitingFunctionMethod(args);
112 }
113
114 static v8::Handle<v8::Value> postMessageMethod(const v8::Arguments& args)
115 {
116     if (args.Length() < 1)
117         return throwNotEnoughArgumentsError(args.GetIsolate());
118     TestActiveDOMObject* imp = V8TestActiveDOMObject::toNative(args.Holder());
119     V8TRYCATCH_FOR_V8STRINGRESOURCE(V8StringResource<>, message, args[0]);
120     imp->postMessage(message);
121     return v8Undefined();
122 }
123
124 static v8::Handle<v8::Value> postMessageMethodCallback(const v8::Arguments& args)
125 {
126     return TestActiveDOMObjectV8Internal::postMessageMethod(args);
127 }
128
129 static v8::Handle<v8::Value> postMessageAttrGetter(v8::Local<v8::String> name, const v8::AccessorInfo& info)
130 {
131     // This is only for getting a unique pointer which we can pass to privateTemplate.
132     static String privateTemplateUniqueKey = "postMessagePrivateTemplate";
133     WrapperWorldType currentWorldType = worldType(info.GetIsolate());
134     v8::Persistent<v8::FunctionTemplate> privateTemplate = V8PerIsolateData::from(info.GetIsolate())->privateTemplate(currentWorldType, &privateTemplateUniqueKey, TestActiveDOMObjectV8Internal::postMessageMethodCallback, v8Undefined(), v8::Signature::New(V8TestActiveDOMObject::GetRawTemplate(info.GetIsolate())));
135
136     v8::Handle<v8::Object> holder = info.This()->FindInstanceInPrototypeChain(V8TestActiveDOMObject::GetTemplate(info.GetIsolate(), currentWorldType));
137     if (holder.IsEmpty()) {
138         // can only reach here by 'object.__proto__.func', and it should passed
139         // domain security check already
140         return privateTemplate->GetFunction();
141     }
142     TestActiveDOMObject* imp = V8TestActiveDOMObject::toNative(holder);
143     if (!BindingSecurity::shouldAllowAccessToFrame(BindingState::instance(), imp->frame(), DoNotReportSecurityError)) {
144         static String sharedTemplateUniqueKey = "postMessageSharedTemplate";
145         v8::Persistent<v8::FunctionTemplate> sharedTemplate = V8PerIsolateData::from(info.GetIsolate())->privateTemplate(currentWorldType, &sharedTemplateUniqueKey, TestActiveDOMObjectV8Internal::postMessageMethodCallback, v8Undefined(), v8::Signature::New(V8TestActiveDOMObject::GetRawTemplate(info.GetIsolate())));
146         return sharedTemplate->GetFunction();
147     }
148
149     v8::Local<v8::Value> hiddenValue = info.This()->GetHiddenValue(name);
150     if (!hiddenValue.IsEmpty())
151         return hiddenValue;
152
153     return privateTemplate->GetFunction();
154 }
155
156 static v8::Handle<v8::Value> postMessageAttrGetterCallback(v8::Local<v8::String> name, const v8::AccessorInfo& info)
157 {
158     return TestActiveDOMObjectV8Internal::postMessageAttrGetter(name, info);
159 }
160
161 static void TestActiveDOMObjectDomainSafeFunctionSetter(v8::Local<v8::String> name, v8::Local<v8::Value> value, const v8::AccessorInfo& info)
162 {
163     v8::Handle<v8::Object> holder = info.This()->FindInstanceInPrototypeChain(V8TestActiveDOMObject::GetTemplate(info.GetIsolate(), worldType(info.GetIsolate())));
164     if (holder.IsEmpty())
165         return;
166     TestActiveDOMObject* imp = V8TestActiveDOMObject::toNative(holder);
167     if (!BindingSecurity::shouldAllowAccessToFrame(BindingState::instance(), imp->frame()))
168         return;
169
170     info.This()->SetHiddenValue(name, value);
171 }
172
173 } // namespace TestActiveDOMObjectV8Internal
174
175 static const V8DOMConfiguration::BatchedAttribute V8TestActiveDOMObjectAttrs[] = {
176     // Attribute 'excitingAttr' (Type: 'readonly attribute' ExtAttr: '')
177     {"excitingAttr", TestActiveDOMObjectV8Internal::excitingAttrAttrGetterCallback, 0, 0 /* no data */, static_cast<v8::AccessControl>(v8::DEFAULT), static_cast<v8::PropertyAttribute>(v8::None), 0 /* on instance */},
178 };
179
180 static v8::Persistent<v8::FunctionTemplate> ConfigureV8TestActiveDOMObjectTemplate(v8::Persistent<v8::FunctionTemplate> desc, v8::Isolate* isolate, WrapperWorldType worldType)
181 {
182     desc->ReadOnlyPrototype();
183
184     v8::Local<v8::Signature> defaultSignature;
185     defaultSignature = V8DOMConfiguration::configureTemplate(desc, "TestActiveDOMObject", v8::Persistent<v8::FunctionTemplate>(), V8TestActiveDOMObject::internalFieldCount,
186         V8TestActiveDOMObjectAttrs, WTF_ARRAY_LENGTH(V8TestActiveDOMObjectAttrs),
187         0, 0, isolate);
188     UNUSED_PARAM(defaultSignature); // In some cases, it will not be used.
189     v8::Local<v8::ObjectTemplate> instance = desc->InstanceTemplate();
190     v8::Local<v8::ObjectTemplate> proto = desc->PrototypeTemplate();
191     UNUSED_PARAM(instance); // In some cases, it will not be used.
192     UNUSED_PARAM(proto); // In some cases, it will not be used.
193     instance->SetAccessCheckCallbacks(TestActiveDOMObjectV8Internal::namedSecurityCheck, TestActiveDOMObjectV8Internal::indexedSecurityCheck, v8::External::New(&V8TestActiveDOMObject::info));
194
195     // Custom Signature 'excitingFunction'
196     const int excitingFunctionArgc = 1;
197     v8::Handle<v8::FunctionTemplate> excitingFunctionArgv[excitingFunctionArgc] = { V8Node::GetRawTemplate(isolate) };
198     v8::Handle<v8::Signature> excitingFunctionSignature = v8::Signature::New(desc, excitingFunctionArgc, excitingFunctionArgv);
199     proto->Set(v8::String::NewSymbol("excitingFunction"), v8::FunctionTemplate::New(TestActiveDOMObjectV8Internal::excitingFunctionMethodCallback, v8Undefined(), excitingFunctionSignature));
200
201     // Function 'postMessage' (ExtAttr: 'DoNotCheckSecurity')
202     proto->SetAccessor(v8::String::NewSymbol("postMessage"), TestActiveDOMObjectV8Internal::postMessageAttrGetterCallback, TestActiveDOMObjectV8Internal::TestActiveDOMObjectDomainSafeFunctionSetter, v8Undefined(), v8::ALL_CAN_READ, static_cast<v8::PropertyAttribute>(v8::DontDelete));
203
204     // Custom toString template
205     desc->Set(v8::String::NewSymbol("toString"), V8PerIsolateData::current()->toStringTemplate());
206     return desc;
207 }
208
209 v8::Persistent<v8::FunctionTemplate> V8TestActiveDOMObject::GetRawTemplate(v8::Isolate* isolate)
210 {
211     V8PerIsolateData* data = V8PerIsolateData::from(isolate);
212     V8PerIsolateData::TemplateMap::iterator result = data->rawTemplateMap().find(&info);
213     if (result != data->rawTemplateMap().end())
214         return result->value;
215
216     v8::HandleScope handleScope;
217     v8::Persistent<v8::FunctionTemplate> templ = createRawTemplate(isolate);
218     data->rawTemplateMap().add(&info, templ);
219     return templ;
220 }
221
222 v8::Persistent<v8::FunctionTemplate> V8TestActiveDOMObject::GetTemplate(v8::Isolate* isolate, WrapperWorldType worldType)
223 {
224     V8PerIsolateData* data = V8PerIsolateData::from(isolate);
225     V8PerIsolateData::TemplateMap::iterator result = data->templateMap().find(&info);
226     if (result != data->templateMap().end())
227         return result->value;
228
229     v8::HandleScope handleScope;
230     v8::Persistent<v8::FunctionTemplate> templ =
231         ConfigureV8TestActiveDOMObjectTemplate(GetRawTemplate(isolate), isolate, worldType);
232     data->templateMap().add(&info, templ);
233     return templ;
234 }
235
236 bool V8TestActiveDOMObject::HasInstance(v8::Handle<v8::Value> value, v8::Isolate* isolate)
237 {
238     return GetRawTemplate(isolate)->HasInstance(value);
239 }
240
241
242 v8::Handle<v8::Object> V8TestActiveDOMObject::createWrapper(PassRefPtr<TestActiveDOMObject> impl, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
243 {
244     ASSERT(impl.get());
245     ASSERT(DOMDataStore::getWrapper(impl.get(), isolate).IsEmpty());
246
247 #if ENABLE(BINDING_INTEGRITY)
248     checkTypeOrDieTrying(impl.get());
249 #endif
250
251     v8::Handle<v8::Object> wrapper = V8DOMWrapper::createWrapper(creationContext, &info, impl.get(), isolate);
252     if (UNLIKELY(wrapper.IsEmpty()))
253         return wrapper;
254
255     installPerContextProperties(wrapper, impl.get(), isolate);
256     V8DOMWrapper::associateObjectWithWrapper(impl, &info, wrapper, isolate, hasDependentLifetime ? WrapperConfiguration::Dependent : WrapperConfiguration::Independent);
257     return wrapper;
258 }
259 void V8TestActiveDOMObject::derefObject(void* object)
260 {
261     static_cast<TestActiveDOMObject*>(object)->deref();
262 }
263
264 } // namespace WebCore