blob: 00eaed8137e1540de1b00541d9003b5aa5fe4afc [file] [log] [blame]
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001/*
2* Copyright (C) 2006, 2007, 2008, 2009 Google Inc. All rights reserved.
3*
4* Redistribution and use in source and binary forms, with or without
5* modification, are permitted provided that the following conditions are
6* met:
7*
8* * Redistributions of source code must retain the above copyright
9* notice, this list of conditions and the following disclaimer.
10* * Redistributions in binary form must reproduce the above
11* copyright notice, this list of conditions and the following disclaimer
12* in the documentation and/or other materials provided with the
13* distribution.
14* * Neither the name of Google Inc. nor the names of its
15* contributors may be used to endorse or promote products derived from
16* this software without specific prior written permission.
17*
18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29*/
30
31#include "config.h"
Ben Murdoche69819b2013-07-17 14:56:49 +010032
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010033#include "bindings/v8/V8NPObject.h"
34
35#include "V8HTMLAppletElement.h"
36#include "V8HTMLEmbedElement.h"
37#include "V8HTMLObjectElement.h"
38#include "bindings/v8/NPV8Object.h"
Torne (Richard Coles)5267f702013-06-11 10:57:24 +010039#include "bindings/v8/UnsafePersistent.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010040#include "bindings/v8/V8Binding.h"
41#include "bindings/v8/V8NPUtils.h"
42#include "bindings/v8/V8ObjectConstructor.h"
43#include "bindings/v8/npruntime_impl.h"
44#include "bindings/v8/npruntime_priv.h"
45#include "core/html/HTMLPlugInElement.h"
46#include "wtf/OwnArrayPtr.h"
47
48namespace WebCore {
49
50enum InvokeFunctionType {
51 InvokeMethod = 1,
52 InvokeConstruct = 2,
53 InvokeDefault = 3
54};
55
56struct IdentifierRep {
57 int number() const { return m_isString ? 0 : m_value.m_number; }
58 const char* string() const { return m_isString ? m_value.m_string : 0; }
59
60 union {
61 const char* m_string;
62 int m_number;
63 } m_value;
64 bool m_isString;
65};
66
67// FIXME: need comments.
68// Params: holder could be HTMLEmbedElement or NPObject
Torne (Richard Coles)5267f702013-06-11 10:57:24 +010069static void npObjectInvokeImpl(const v8::FunctionCallbackInfo<v8::Value>& args, InvokeFunctionType functionId)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010070{
71 NPObject* npObject;
72
73 WrapperWorldType currentWorldType = worldType(args.GetIsolate());
74 // These three types are subtypes of HTMLPlugInElement.
75 if (V8HTMLAppletElement::HasInstance(args.Holder(), args.GetIsolate(), currentWorldType) || V8HTMLEmbedElement::HasInstance(args.Holder(), args.GetIsolate(), currentWorldType)
76 || V8HTMLObjectElement::HasInstance(args.Holder(), args.GetIsolate(), currentWorldType)) {
77 // The holder object is a subtype of HTMLPlugInElement.
78 HTMLPlugInElement* element;
79 if (V8HTMLAppletElement::HasInstance(args.Holder(), args.GetIsolate(), currentWorldType))
80 element = V8HTMLAppletElement::toNative(args.Holder());
81 else if (V8HTMLEmbedElement::HasInstance(args.Holder(), args.GetIsolate(), currentWorldType))
82 element = V8HTMLEmbedElement::toNative(args.Holder());
83 else
84 element = V8HTMLObjectElement::toNative(args.Holder());
85 ScriptInstance scriptInstance = element->getInstance();
Torne (Richard Coles)93ac45c2013-05-29 14:40:20 +010086 if (scriptInstance) {
87 v8::Isolate* isolate = v8::Isolate::GetCurrent();
88 v8::HandleScope handleScope(isolate);
89 npObject = v8ObjectToNPObject(scriptInstance->newLocal(isolate));
90 } else
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010091 npObject = 0;
92 } else {
93 // The holder object is not a subtype of HTMLPlugInElement, it must be an NPObject which has three
94 // internal fields.
Torne (Richard Coles)5267f702013-06-11 10:57:24 +010095 if (args.Holder()->InternalFieldCount() != npObjectInternalFieldCount) {
96 throwError(v8ReferenceError, "NPMethod called on non-NPObject", args.GetIsolate());
97 return;
98 }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010099
100 npObject = v8ObjectToNPObject(args.Holder());
101 }
102
103 // Verify that our wrapper wasn't using a NPObject which has already been deleted.
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100104 if (!npObject || !_NPN_IsAlive(npObject)) {
105 throwError(v8ReferenceError, "NPObject deleted", args.GetIsolate());
106 return;
107 }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100108
109 // Wrap up parameters.
110 int numArgs = args.Length();
111 OwnArrayPtr<NPVariant> npArgs = adoptArrayPtr(new NPVariant[numArgs]);
Ben Murdoche69819b2013-07-17 14:56:49 +0100112
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100113 for (int i = 0; i < numArgs; i++)
Torne (Richard Coles)9bbd2f52013-09-19 22:37:05 +0100114 convertV8ObjectToNPVariant(args[i], npObject, &npArgs[i], args.GetIsolate());
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100115
116 NPVariant result;
117 VOID_TO_NPVARIANT(result);
118
119 bool retval = true;
120 switch (functionId) {
121 case InvokeMethod:
122 if (npObject->_class->invoke) {
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100123 v8::Handle<v8::String> functionName = v8::Handle<v8::String>::Cast(args.Data());
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100124 NPIdentifier identifier = getStringIdentifier(functionName);
125 retval = npObject->_class->invoke(npObject, identifier, npArgs.get(), numArgs, &result);
126 }
127 break;
128 case InvokeConstruct:
129 if (npObject->_class->construct)
130 retval = npObject->_class->construct(npObject, npArgs.get(), numArgs, &result);
131 break;
132 case InvokeDefault:
133 if (npObject->_class->invokeDefault)
134 retval = npObject->_class->invokeDefault(npObject, npArgs.get(), numArgs, &result);
135 break;
136 default:
137 break;
138 }
139
140 if (!retval)
141 throwError(v8GeneralError, "Error calling method on NPObject.", args.GetIsolate());
142
143 for (int i = 0; i < numArgs; i++)
144 _NPN_ReleaseVariantValue(&npArgs[i]);
145
146 // Unwrap return values.
147 v8::Handle<v8::Value> returnValue;
148 if (_NPN_IsAlive(npObject))
Ben Murdoche69819b2013-07-17 14:56:49 +0100149 returnValue = convertNPVariantToV8Object(&result, npObject, args.GetIsolate());
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100150 _NPN_ReleaseVariantValue(&result);
151
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100152 v8SetReturnValue(args, returnValue);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100153}
154
155
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100156void npObjectMethodHandler(const v8::FunctionCallbackInfo<v8::Value>& args)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100157{
158 return npObjectInvokeImpl(args, InvokeMethod);
159}
160
161
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100162void npObjectInvokeDefaultHandler(const v8::FunctionCallbackInfo<v8::Value>& args)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100163{
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100164 if (args.IsConstructCall()) {
165 npObjectInvokeImpl(args, InvokeConstruct);
166 return;
167 }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100168
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100169 npObjectInvokeImpl(args, InvokeDefault);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100170}
171
172class V8NPTemplateMap {
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100173public:
174 // NPIdentifier is PrivateIdentifier*.
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100175 typedef HashMap<PrivateIdentifier*, UnsafePersistent<v8::FunctionTemplate> > MapType;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100176
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100177 UnsafePersistent<v8::FunctionTemplate> get(PrivateIdentifier* key)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100178 {
179 return m_map.get(key);
180 }
181
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100182 void set(PrivateIdentifier* key, v8::Handle<v8::FunctionTemplate> handle)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100183 {
184 ASSERT(!m_map.contains(key));
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100185 v8::Persistent<v8::FunctionTemplate> wrapper(m_isolate, handle);
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100186 wrapper.MakeWeak(key, &makeWeakCallback);
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100187 m_map.set(key, UnsafePersistent<v8::FunctionTemplate>(wrapper));
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100188 }
189
190 static V8NPTemplateMap& sharedInstance(v8::Isolate* isolate)
191 {
192 DEFINE_STATIC_LOCAL(V8NPTemplateMap, map, (isolate));
193 ASSERT(isolate == map.m_isolate);
194 return map;
195 }
196
197private:
198 explicit V8NPTemplateMap(v8::Isolate* isolate)
199 : m_isolate(isolate)
200 {
201 }
202
203 void dispose(PrivateIdentifier* key)
204 {
205 MapType::iterator it = m_map.find(key);
206 ASSERT(it != m_map.end());
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100207 it->value.dispose();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100208 m_map.remove(it);
209 }
210
Torne (Richard Coles)93ac45c2013-05-29 14:40:20 +0100211 static void makeWeakCallback(v8::Isolate* isolate, v8::Persistent<v8::FunctionTemplate>*, PrivateIdentifier* key)
212 {
213 V8NPTemplateMap::sharedInstance(isolate).dispose(key);
214 }
215
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100216 MapType m_map;
217 v8::Isolate* m_isolate;
218};
219
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100220static v8::Handle<v8::Value> npObjectGetProperty(v8::Local<v8::Object> self, NPIdentifier identifier, v8::Local<v8::Value> key, v8::Isolate* isolate)
221{
222 NPObject* npObject = v8ObjectToNPObject(self);
223
224 // Verify that our wrapper wasn't using a NPObject which
225 // has already been deleted.
226 if (!npObject || !_NPN_IsAlive(npObject))
227 return throwError(v8ReferenceError, "NPObject deleted", isolate);
228
Ben Murdoche69819b2013-07-17 14:56:49 +0100229
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100230 if (npObject->_class->hasProperty && npObject->_class->getProperty && npObject->_class->hasProperty(npObject, identifier)) {
231 if (!_NPN_IsAlive(npObject))
232 return throwError(v8ReferenceError, "NPObject deleted", isolate);
233
234 NPVariant result;
235 VOID_TO_NPVARIANT(result);
236 if (!npObject->_class->getProperty(npObject, identifier, &result))
237 return v8Undefined();
238
239 v8::Handle<v8::Value> returnValue;
240 if (_NPN_IsAlive(npObject))
Ben Murdoche69819b2013-07-17 14:56:49 +0100241 returnValue = convertNPVariantToV8Object(&result, npObject, isolate);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100242 _NPN_ReleaseVariantValue(&result);
243 return returnValue;
244
245 }
246
247 if (!_NPN_IsAlive(npObject))
248 return throwError(v8ReferenceError, "NPObject deleted", isolate);
249
250 if (key->IsString() && npObject->_class->hasMethod && npObject->_class->hasMethod(npObject, identifier)) {
251 if (!_NPN_IsAlive(npObject))
252 return throwError(v8ReferenceError, "NPObject deleted", isolate);
253
254 PrivateIdentifier* id = static_cast<PrivateIdentifier*>(identifier);
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100255 UnsafePersistent<v8::FunctionTemplate> functionTemplate = V8NPTemplateMap::sharedInstance(isolate).get(id);
256 // FunctionTemplate caches function for each context.
257 v8::Local<v8::Function> v8Function;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100258 // Cache templates using identifier as the key.
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100259 if (functionTemplate.isEmpty()) {
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100260 // Create a new template.
261 v8::Local<v8::FunctionTemplate> temp = v8::FunctionTemplate::New();
262 temp->SetCallHandler(npObjectMethodHandler, key);
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100263 V8NPTemplateMap::sharedInstance(isolate).set(id, temp);
264 v8Function = temp->GetFunction();
265 } else {
266 v8Function = functionTemplate.newLocal(isolate)->GetFunction();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100267 }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100268 v8Function->SetName(v8::Handle<v8::String>::Cast(key));
269 return v8Function;
270 }
271
272 return v8Undefined();
273}
274
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100275void npObjectNamedPropertyGetter(v8::Local<v8::String> name, const v8::PropertyCallbackInfo<v8::Value>& info)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100276{
277 NPIdentifier identifier = getStringIdentifier(name);
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100278 v8SetReturnValue(info, npObjectGetProperty(info.Holder(), identifier, name, info.GetIsolate()));
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100279}
280
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100281void npObjectIndexedPropertyGetter(uint32_t index, const v8::PropertyCallbackInfo<v8::Value>& info)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100282{
283 NPIdentifier identifier = _NPN_GetIntIdentifier(index);
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100284 v8SetReturnValue(info, npObjectGetProperty(info.Holder(), identifier, v8::Number::New(info.GetIsolate(), index), info.GetIsolate()));
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100285}
286
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100287void npObjectGetNamedProperty(v8::Local<v8::Object> self, v8::Local<v8::String> name, const v8::PropertyCallbackInfo<v8::Value>& info)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100288{
289 NPIdentifier identifier = getStringIdentifier(name);
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100290 v8SetReturnValue(info, npObjectGetProperty(self, identifier, name, info.GetIsolate()));
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100291}
292
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100293void npObjectGetIndexedProperty(v8::Local<v8::Object> self, uint32_t index, const v8::PropertyCallbackInfo<v8::Value>& info)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100294{
295 NPIdentifier identifier = _NPN_GetIntIdentifier(index);
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100296 v8SetReturnValue(info, npObjectGetProperty(self, identifier, v8::Number::New(info.GetIsolate(), index), info.GetIsolate()));
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100297}
298
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100299void npObjectQueryProperty(v8::Local<v8::String> name, const v8::PropertyCallbackInfo<v8::Integer>& info)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100300{
301 NPIdentifier identifier = getStringIdentifier(name);
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100302 if (npObjectGetProperty(info.Holder(), identifier, name, info.GetIsolate()).IsEmpty())
303 return;
304 v8SetReturnValueInt(info, 0);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100305}
306
307static v8::Handle<v8::Value> npObjectSetProperty(v8::Local<v8::Object> self, NPIdentifier identifier, v8::Local<v8::Value> value, v8::Isolate* isolate)
308{
309 NPObject* npObject = v8ObjectToNPObject(self);
310
311 // Verify that our wrapper wasn't using a NPObject which has already been deleted.
312 if (!npObject || !_NPN_IsAlive(npObject)) {
313 throwError(v8ReferenceError, "NPObject deleted", isolate);
314 return value; // Intercepted, but an exception was thrown.
315 }
316
317 if (npObject->_class->hasProperty && npObject->_class->setProperty && npObject->_class->hasProperty(npObject, identifier)) {
318 if (!_NPN_IsAlive(npObject))
319 return throwError(v8ReferenceError, "NPObject deleted", isolate);
320
321 NPVariant npValue;
322 VOID_TO_NPVARIANT(npValue);
Torne (Richard Coles)9bbd2f52013-09-19 22:37:05 +0100323 convertV8ObjectToNPVariant(value, npObject, &npValue, isolate);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100324 bool success = npObject->_class->setProperty(npObject, identifier, &npValue);
325 _NPN_ReleaseVariantValue(&npValue);
326 if (success)
327 return value; // Intercept the call.
328 }
329 return v8Undefined();
330}
331
332
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100333void npObjectNamedPropertySetter(v8::Local<v8::String> name, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<v8::Value>& info)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100334{
335 NPIdentifier identifier = getStringIdentifier(name);
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100336 v8SetReturnValue(info, npObjectSetProperty(info.Holder(), identifier, value, info.GetIsolate()));
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100337}
338
339
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100340void npObjectIndexedPropertySetter(uint32_t index, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<v8::Value>& info)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100341{
342 NPIdentifier identifier = _NPN_GetIntIdentifier(index);
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100343 v8SetReturnValue(info, npObjectSetProperty(info.Holder(), identifier, value, info.GetIsolate()));
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100344}
345
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100346void npObjectSetNamedProperty(v8::Local<v8::Object> self, v8::Local<v8::String> name, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<v8::Value>& info)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100347{
348 NPIdentifier identifier = getStringIdentifier(name);
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100349 v8SetReturnValue(info, npObjectSetProperty(self, identifier, value, info.GetIsolate()));
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100350}
351
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100352void npObjectSetIndexedProperty(v8::Local<v8::Object> self, uint32_t index, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<v8::Value>& info)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100353{
354 NPIdentifier identifier = _NPN_GetIntIdentifier(index);
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100355 v8SetReturnValue(info, npObjectSetProperty(self, identifier, value, info.GetIsolate()));
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100356}
357
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100358void npObjectPropertyEnumerator(const v8::PropertyCallbackInfo<v8::Array>& info, bool namedProperty)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100359{
360 NPObject* npObject = v8ObjectToNPObject(info.Holder());
361
362 // Verify that our wrapper wasn't using a NPObject which
363 // has already been deleted.
364 if (!npObject || !_NPN_IsAlive(npObject))
365 throwError(v8ReferenceError, "NPObject deleted", info.GetIsolate());
366
367 if (NP_CLASS_STRUCT_VERSION_HAS_ENUM(npObject->_class) && npObject->_class->enumerate) {
368 uint32_t count;
369 NPIdentifier* identifiers;
370 if (npObject->_class->enumerate(npObject, &identifiers, &count)) {
371 v8::Handle<v8::Array> properties = v8::Array::New(count);
372 for (uint32_t i = 0; i < count; ++i) {
373 IdentifierRep* identifier = static_cast<IdentifierRep*>(identifiers[i]);
374 if (namedProperty)
Ben Murdoch591b9582013-07-10 11:41:44 +0100375 properties->Set(v8::Integer::New(i, info.GetIsolate()), v8::String::NewSymbol(identifier->string()));
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100376 else
Ben Murdoch591b9582013-07-10 11:41:44 +0100377 properties->Set(v8::Integer::New(i, info.GetIsolate()), v8::Integer::New(identifier->number(), info.GetIsolate()));
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100378 }
379
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100380 v8SetReturnValue(info, properties);
381 return;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100382 }
383 }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100384}
385
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100386void npObjectNamedPropertyEnumerator(const v8::PropertyCallbackInfo<v8::Array>& info)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100387{
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100388 npObjectPropertyEnumerator(info, true);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100389}
390
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100391void npObjectIndexedPropertyEnumerator(const v8::PropertyCallbackInfo<v8::Array>& info)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100392{
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100393 npObjectPropertyEnumerator(info, false);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100394}
395
396static DOMWrapperMap<NPObject>& staticNPObjectMap()
397{
398 DEFINE_STATIC_LOCAL(DOMWrapperMap<NPObject>, npObjectMap, (v8::Isolate::GetCurrent()));
399 return npObjectMap;
400}
401
402template<>
Torne (Richard Coles)93ac45c2013-05-29 14:40:20 +0100403inline void DOMWrapperMap<NPObject>::makeWeakCallback(v8::Isolate* isolate, v8::Persistent<v8::Object>* wrapper, DOMWrapperMap<NPObject>*)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100404{
Torne (Richard Coles)93ac45c2013-05-29 14:40:20 +0100405 NPObject* npObject = static_cast<NPObject*>(toNative(*wrapper));
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100406
407 ASSERT(npObject);
Torne (Richard Coles)e1f1df52013-08-23 16:39:30 +0100408 ASSERT(staticNPObjectMap().containsKeyAndValue(npObject, *wrapper));
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100409
410 // Must remove from our map before calling _NPN_ReleaseObject(). _NPN_ReleaseObject can
411 // call forgetV8ObjectForNPObject, which uses the table as well.
Torne (Richard Coles)93ac45c2013-05-29 14:40:20 +0100412 staticNPObjectMap().removeAndDispose(npObject);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100413
414 if (_NPN_IsAlive(npObject))
415 _NPN_ReleaseObject(npObject);
416}
417
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100418v8::Local<v8::Object> createV8ObjectForNPObject(NPObject* object, NPObject* root, v8::Isolate* isolate)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100419{
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100420 static v8::Eternal<v8::FunctionTemplate> npObjectDesc;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100421
422 ASSERT(v8::Context::InContext());
423
424 // If this is a v8 object, just return it.
Ben Murdoch591b9582013-07-10 11:41:44 +0100425 V8NPObject* v8NPObject = npObjectToV8NPObject(object);
426 if (v8NPObject)
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100427 return v8::Local<v8::Object>::New(isolate, v8NPObject->v8Object);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100428
429 // If we've already wrapped this object, just return it.
Torne (Richard Coles)e1f1df52013-08-23 16:39:30 +0100430 v8::Handle<v8::Object> wrapper = staticNPObjectMap().newLocal(object, isolate);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100431 if (!wrapper.IsEmpty())
Torne (Richard Coles)e1f1df52013-08-23 16:39:30 +0100432 return wrapper;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100433
434 // FIXME: we should create a Wrapper type as a subclass of JSObject. It has two internal fields, field 0 is the wrapped
435 // pointer, and field 1 is the type. There should be an api function that returns unused type id. The same Wrapper type
436 // can be used by DOM bindings.
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100437 if (npObjectDesc.IsEmpty()) {
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100438 v8::Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New();
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100439 templ->InstanceTemplate()->SetInternalFieldCount(npObjectInternalFieldCount);
440 templ->InstanceTemplate()->SetNamedPropertyHandler(npObjectNamedPropertyGetter, npObjectNamedPropertySetter, npObjectQueryProperty, 0, npObjectNamedPropertyEnumerator);
441 templ->InstanceTemplate()->SetIndexedPropertyHandler(npObjectIndexedPropertyGetter, npObjectIndexedPropertySetter, 0, 0, npObjectIndexedPropertyEnumerator);
442 templ->InstanceTemplate()->SetCallAsFunctionHandler(npObjectInvokeDefaultHandler);
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100443 npObjectDesc.Set(isolate, templ);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100444 }
445
446 // FIXME: Move staticNPObjectMap() to DOMDataStore.
447 // Use V8DOMWrapper::createWrapper() and
448 // V8DOMWrapper::associateObjectWithWrapper()
449 // to create a wrapper object.
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100450 v8::Handle<v8::Function> v8Function = npObjectDesc.Get(isolate)->GetFunction();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100451 v8::Local<v8::Object> value = V8ObjectConstructor::newInstance(v8Function);
452 if (value.IsEmpty())
453 return value;
454
455 V8DOMWrapper::setNativeInfo(value, npObjectTypeInfo(), object);
456
457 // KJS retains the object as part of its wrapper (see Bindings::CInstance).
458 _NPN_RetainObject(object);
Ben Murdoche69819b2013-07-17 14:56:49 +0100459 _NPN_RegisterObject(object, root);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100460
461 WrapperConfiguration configuration = buildWrapperConfiguration(object, WrapperConfiguration::Dependent);
462 staticNPObjectMap().set(object, value, configuration);
463 ASSERT(V8DOMWrapper::maybeDOMWrapper(value));
464 return value;
465}
466
467void forgetV8ObjectForNPObject(NPObject* object)
468{
Torne (Richard Coles)93ac45c2013-05-29 14:40:20 +0100469 v8::Isolate* isolate = v8::Isolate::GetCurrent();
470 v8::HandleScope scope(isolate);
Torne (Richard Coles)e1f1df52013-08-23 16:39:30 +0100471 v8::Handle<v8::Object> wrapper = staticNPObjectMap().newLocal(object, isolate);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100472 if (!wrapper.IsEmpty()) {
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100473 V8DOMWrapper::clearNativeInfo(wrapper, npObjectTypeInfo());
Torne (Richard Coles)93ac45c2013-05-29 14:40:20 +0100474 staticNPObjectMap().removeAndDispose(object);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100475 _NPN_ReleaseObject(object);
476 }
477}
478
479} // namespace WebCore