[v8] wrapSlow methods should ref underlying object before creating wrapper
[WebKit-https.git] / Source / WebCore / bindings / scripts / test / V8 / V8TestNamedConstructor.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 "V8TestNamedConstructor.h"
23
24 #include "ExceptionCode.h"
25 #include "RuntimeEnabledFeatures.h"
26 #include "V8Binding.h"
27 #include "V8BindingMacros.h"
28 #include "V8BindingState.h"
29 #include "V8DOMWrapper.h"
30 #include "V8IsolatedContext.h"
31 #include "V8Proxy.h"
32 #include <wtf/UnusedParam.h>
33
34 namespace WebCore {
35
36 WrapperTypeInfo V8TestNamedConstructor::info = { V8TestNamedConstructor::GetTemplate, V8TestNamedConstructor::derefObject, V8TestNamedConstructor::toActiveDOMObject, 0 };
37
38 namespace TestNamedConstructorInternal {
39
40 template <typename T> void V8_USE(T) { }
41
42 } // namespace TestNamedConstructorInternal
43
44 WrapperTypeInfo V8TestNamedConstructorConstructor::info = { V8TestNamedConstructorConstructor::GetTemplate, V8TestNamedConstructor::derefObject, V8TestNamedConstructor::toActiveDOMObject, 0 };
45
46 static v8::Handle<v8::Value> V8TestNamedConstructorConstructorCallback(const v8::Arguments& args)
47 {
48     INC_STATS("DOM.TestNamedConstructor.Constructor");
49
50     if (!args.IsConstructCall())
51         return throwError("DOM object constructor cannot be called as a function.", V8Proxy::TypeError);
52
53     if (ConstructorMode::current() == ConstructorMode::WrapExistingObject)
54         return args.Holder();
55
56     Frame* frame = V8Proxy::retrieveFrameForCurrentContext();
57     if (!frame)
58         return throwError("TestNamedConstructor constructor associated frame is unavailable", V8Proxy::ReferenceError);
59
60     Document* document = frame->document();
61
62     // Make sure the document is added to the DOM Node map. Otherwise, the TestNamedConstructor instance
63     // may end up being the only node in the map and get garbage-collected prematurely.
64     toV8(document);
65
66     if (args.Length() < 1)
67         return throwError("Not enough arguments", V8Proxy::TypeError);
68
69     ExceptionCode ec = 0;
70     STRING_TO_V8PARAMETER_EXCEPTION_BLOCK(V8Parameter<>, str1, MAYBE_MISSING_PARAMETER(args, 0, DefaultIsUndefined));
71     STRING_TO_V8PARAMETER_EXCEPTION_BLOCK(V8Parameter<>, str2, MAYBE_MISSING_PARAMETER(args, 1, DefaultIsUndefined));
72     STRING_TO_V8PARAMETER_EXCEPTION_BLOCK(V8Parameter<>, str3, MAYBE_MISSING_PARAMETER(args, 2, DefaultIsNullString));
73
74     RefPtr<TestNamedConstructor> impl = TestNamedConstructor::createForJSConstructor(document, str1, str2, str3, ec);
75     v8::Handle<v8::Object> wrapper = args.Holder();
76     if (ec)
77         goto fail;
78
79     V8DOMWrapper::setDOMWrapper(wrapper, &V8TestNamedConstructorConstructor::info, impl.get());
80     impl->ref();
81     V8DOMWrapper::setJSWrapperForActiveDOMObject(impl.get(), v8::Persistent<v8::Object>::New(wrapper));
82     return args.Holder();
83   fail:
84     return throwError(ec);
85 }
86
87 v8::Persistent<v8::FunctionTemplate> V8TestNamedConstructorConstructor::GetTemplate()
88 {
89     static v8::Persistent<v8::FunctionTemplate> cachedTemplate;
90     if (!cachedTemplate.IsEmpty())
91         return cachedTemplate;
92
93     v8::HandleScope scope;
94     v8::Local<v8::FunctionTemplate> result = v8::FunctionTemplate::New(V8TestNamedConstructorConstructorCallback);
95
96     v8::Local<v8::ObjectTemplate> instance = result->InstanceTemplate();
97     instance->SetInternalFieldCount(V8TestNamedConstructor::internalFieldCount);
98     result->SetClassName(v8::String::New("TestNamedConstructor"));
99     result->Inherit(V8TestNamedConstructor::GetTemplate());
100
101     cachedTemplate = v8::Persistent<v8::FunctionTemplate>::New(result);
102     return cachedTemplate;
103 }
104
105 static v8::Persistent<v8::FunctionTemplate> ConfigureV8TestNamedConstructorTemplate(v8::Persistent<v8::FunctionTemplate> desc)
106 {
107     desc->ReadOnlyPrototype();
108
109     v8::Local<v8::Signature> defaultSignature;
110     defaultSignature = configureTemplate(desc, "TestNamedConstructor", v8::Persistent<v8::FunctionTemplate>(), V8TestNamedConstructor::internalFieldCount,
111         0, 0,
112         0, 0);
113     UNUSED_PARAM(defaultSignature); // In some cases, it will not be used.
114     
115
116     // Custom toString template
117     desc->Set(getToStringName(), getToStringTemplate());
118     return desc;
119 }
120
121 v8::Persistent<v8::FunctionTemplate> V8TestNamedConstructor::GetRawTemplate()
122 {
123     V8BindingPerIsolateData* data = V8BindingPerIsolateData::current();
124     V8BindingPerIsolateData::TemplateMap::iterator result = data->rawTemplateMap().find(&info);
125     if (result != data->rawTemplateMap().end())
126         return result->second;
127
128     v8::HandleScope handleScope;
129     v8::Persistent<v8::FunctionTemplate> templ = createRawTemplate();
130     data->rawTemplateMap().add(&info, templ);
131     return templ;
132 }
133
134 v8::Persistent<v8::FunctionTemplate> V8TestNamedConstructor::GetTemplate()
135 {
136     V8BindingPerIsolateData* data = V8BindingPerIsolateData::current();
137     V8BindingPerIsolateData::TemplateMap::iterator result = data->templateMap().find(&info);
138     if (result != data->templateMap().end())
139         return result->second;
140
141     v8::HandleScope handleScope;
142     v8::Persistent<v8::FunctionTemplate> templ =
143         ConfigureV8TestNamedConstructorTemplate(GetRawTemplate());
144     data->templateMap().add(&info, templ);
145     return templ;
146 }
147
148 bool V8TestNamedConstructor::HasInstance(v8::Handle<v8::Value> value)
149 {
150     return GetRawTemplate()->HasInstance(value);
151 }
152
153 ActiveDOMObject* V8TestNamedConstructor::toActiveDOMObject(v8::Handle<v8::Object> object)
154 {
155     return toNative(object);
156 }      
157
158 v8::Handle<v8::Object> V8TestNamedConstructor::wrapSlow(PassRefPtr<TestNamedConstructor> impl)
159 {
160     v8::Handle<v8::Object> wrapper;
161     V8Proxy* proxy = 0;
162     wrapper = V8DOMWrapper::instantiateV8Object(proxy, &info, impl.get());
163     if (UNLIKELY(wrapper.IsEmpty()))
164         return wrapper;
165
166     v8::Persistent<v8::Object> wrapperHandle = v8::Persistent<v8::Object>::New(wrapper);
167
168     if (!hasDependentLifetime)
169         wrapperHandle.MarkIndependent();
170     getActiveDOMObjectMap().set(impl.leakRef(), wrapperHandle);
171     return wrapper;
172 }
173
174 void V8TestNamedConstructor::derefObject(void* object)
175 {
176     static_cast<TestNamedConstructor*>(object)->deref();
177 }
178
179 } // namespace WebCore