Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / bindings / core / v8 / NPV8Object.cpp
1 /*
2  * Copyright (C) 2004, 2006 Apple Computer, Inc.  All rights reserved.
3  * Copyright (C) 2007, 2008, 2009 Google, Inc.  All rights reserved.
4  * Copyright (C) 2014 Opera Software ASA. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
16  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #include "config.h"
29 #include "bindings/core/v8/NPV8Object.h"
30
31 #include "bindings/core/v8/ScriptController.h"
32 #include "bindings/core/v8/ScriptSourceCode.h"
33 #include "bindings/core/v8/V8Binding.h"
34 #include "bindings/core/v8/V8GCController.h"
35 #include "bindings/core/v8/V8NPUtils.h"
36 #include "bindings/core/v8/V8ObjectConstructor.h"
37 #include "bindings/core/v8/V8ScriptRunner.h"
38 #include "bindings/core/v8/WrapperTypeInfo.h"
39 #include "bindings/core/v8/npruntime_impl.h"
40 #include "bindings/core/v8/npruntime_priv.h"
41 #include "core/frame/LocalDOMWindow.h"
42 #include "core/frame/LocalFrame.h"
43 #include "platform/UserGestureIndicator.h"
44 #include "wtf/OwnPtr.h"
45 #include "wtf/StringExtras.h"
46 #include "wtf/text/WTFString.h"
47 #include <stdio.h>
48
49 using namespace blink;
50
51 namespace {
52
53 WrapperPersistentNode* createPersistentHandle(ScriptWrappableBase* internalPointer)
54 {
55     ASSERT_NOT_REACHED();
56     return 0;
57 }
58
59 } // namespace
60
61 namespace blink {
62
63 const WrapperTypeInfo* npObjectTypeInfo()
64 {
65     static const WrapperTypeInfo typeInfo = { gin::kEmbedderBlink, 0, 0, 0, createPersistentHandle, 0, 0, 0, 0, 0, 0, WrapperTypeInfo::WrapperTypeObjectPrototype, WrapperTypeInfo::ObjectClassId, WrapperTypeInfo::Dependent, WrapperTypeInfo::RefCountedObject };
66     return &typeInfo;
67 }
68
69 // FIXME: Comments on why use malloc and free.
70 static NPObject* allocV8NPObject(NPP, NPClass*)
71 {
72     return static_cast<NPObject*>(malloc(sizeof(V8NPObject)));
73 }
74
75 static void freeV8NPObject(NPObject* npObject)
76 {
77     V8NPObject* v8NpObject = reinterpret_cast<V8NPObject*>(npObject);
78     disposeUnderlyingV8Object(npObject, v8::Isolate::GetCurrent());
79     free(v8NpObject);
80 }
81
82 static NPClass V8NPObjectClass = {
83     NP_CLASS_STRUCT_VERSION,
84     allocV8NPObject,
85     freeV8NPObject,
86     0, 0, 0, 0, 0, 0, 0, 0, 0, 0
87 };
88
89 static ScriptState* mainWorldScriptState(v8::Isolate* isolate, NPP npp, NPObject* npObject)
90 {
91     ASSERT(npObject->_class == &V8NPObjectClass);
92     V8NPObject* object = reinterpret_cast<V8NPObject*>(npObject);
93     LocalDOMWindow* window = object->rootObject;
94     if (!window || !window->isCurrentlyDisplayedInFrame())
95         return 0;
96     v8::HandleScope handleScope(isolate);
97     v8::Handle<v8::Context> context = toV8Context(object->rootObject->frame(), DOMWrapperWorld::mainWorld());
98     return ScriptState::from(context);
99 }
100
101 static PassOwnPtr<v8::Handle<v8::Value>[]> createValueListFromVariantArgs(const NPVariant* arguments, uint32_t argumentCount, NPObject* owner, v8::Isolate* isolate)
102 {
103     OwnPtr<v8::Handle<v8::Value>[]> argv = adoptArrayPtr(new v8::Handle<v8::Value>[argumentCount]);
104     for (uint32_t index = 0; index < argumentCount; index++) {
105         const NPVariant* arg = &arguments[index];
106         argv[index] = convertNPVariantToV8Object(arg, owner, isolate);
107     }
108     return argv.release();
109 }
110
111 // Create an identifier (null terminated utf8 char*) from the NPIdentifier.
112 static v8::Local<v8::String> npIdentifierToV8Identifier(NPIdentifier name, v8::Isolate* isolate)
113 {
114     PrivateIdentifier* identifier = static_cast<PrivateIdentifier*>(name);
115     if (identifier->isString)
116         return v8AtomicString(isolate, static_cast<const char*>(identifier->value.string));
117
118     char buffer[32];
119     snprintf(buffer, sizeof(buffer), "%d", identifier->value.number);
120     return v8AtomicString(isolate, buffer);
121 }
122
123 NPObject* v8ObjectToNPObject(v8::Handle<v8::Object> object)
124 {
125     return reinterpret_cast<NPObject*>(toScriptWrappableBase(object));
126 }
127
128 bool isWrappedNPObject(v8::Handle<v8::Object> object)
129 {
130     if (object->InternalFieldCount() == npObjectInternalFieldCount) {
131         const WrapperTypeInfo* typeInfo = static_cast<const WrapperTypeInfo*>(object->GetAlignedPointerFromInternalField(v8DOMWrapperTypeIndex));
132         return typeInfo == npObjectTypeInfo();
133     }
134     return false;
135 }
136
137 NPObject* npCreateV8ScriptObject(NPP npp, v8::Handle<v8::Object> object, LocalDOMWindow* root, v8::Isolate* isolate)
138 {
139     // Check to see if this object is already wrapped.
140     if (isWrappedNPObject(object)) {
141         NPObject* returnValue = v8ObjectToNPObject(object);
142         _NPN_RetainObject(returnValue);
143         return returnValue;
144     }
145
146     V8NPObjectVector* objectVector = 0;
147     if (V8PerContextData* perContextData = V8PerContextData::from(object->CreationContext())) {
148         int v8ObjectHash = object->GetIdentityHash();
149         ASSERT(v8ObjectHash);
150         V8NPObjectMap* v8NPObjectMap = perContextData->v8NPObjectMap();
151         V8NPObjectMap::iterator iter = v8NPObjectMap->find(v8ObjectHash);
152         if (iter != v8NPObjectMap->end()) {
153             V8NPObjectVector& objects = iter->value;
154             for (size_t index = 0; index < objects.size(); ++index) {
155                 V8NPObject* v8npObject = objects.at(index);
156                 if (v8npObject->v8Object == object && v8npObject->rootObject == root) {
157                     _NPN_RetainObject(&v8npObject->object);
158                     return reinterpret_cast<NPObject*>(v8npObject);
159                 }
160             }
161             objectVector = &iter->value;
162         } else {
163             objectVector = &v8NPObjectMap->set(v8ObjectHash, V8NPObjectVector()).storedValue->value;
164         }
165     }
166
167     V8NPObject* v8npObject = reinterpret_cast<V8NPObject*>(_NPN_CreateObject(npp, &V8NPObjectClass));
168     // This is uninitialized memory, we need to clear it so that
169     // Persistent::Reset won't try to Dispose anything bogus.
170     new (&v8npObject->v8Object) v8::Persistent<v8::Object>();
171     v8npObject->v8Object.Reset(isolate, object);
172     v8npObject->rootObject = root;
173
174     if (objectVector)
175         objectVector->append(v8npObject);
176
177     return reinterpret_cast<NPObject*>(v8npObject);
178 }
179
180 V8NPObject* npObjectToV8NPObject(NPObject* npObject)
181 {
182     if (npObject->_class != &V8NPObjectClass)
183         return 0;
184     V8NPObject* v8NpObject = reinterpret_cast<V8NPObject*>(npObject);
185     if (v8NpObject->v8Object.IsEmpty())
186         return 0;
187     return v8NpObject;
188 }
189
190 ScriptWrappableBase* npObjectToScriptWrappableBase(NPObject* npObject)
191 {
192     return reinterpret_cast<ScriptWrappableBase*>(npObject);
193 }
194
195 void disposeUnderlyingV8Object(NPObject* npObject, v8::Isolate* isolate)
196 {
197     ASSERT(npObject);
198     V8NPObject* v8NpObject = npObjectToV8NPObject(npObject);
199     if (!v8NpObject)
200         return;
201     v8::HandleScope scope(isolate);
202     v8::Handle<v8::Object> v8Object = v8::Local<v8::Object>::New(isolate, v8NpObject->v8Object);
203     ASSERT(!v8Object->CreationContext().IsEmpty());
204     if (V8PerContextData* perContextData = V8PerContextData::from(v8Object->CreationContext())) {
205         V8NPObjectMap* v8NPObjectMap = perContextData->v8NPObjectMap();
206         int v8ObjectHash = v8Object->GetIdentityHash();
207         ASSERT(v8ObjectHash);
208         V8NPObjectMap::iterator iter = v8NPObjectMap->find(v8ObjectHash);
209         if (iter != v8NPObjectMap->end()) {
210             V8NPObjectVector& objects = iter->value;
211             for (size_t index = 0; index < objects.size(); ++index) {
212                 if (objects.at(index) == v8NpObject) {
213                     objects.remove(index);
214                     break;
215                 }
216             }
217             if (objects.isEmpty())
218                 v8NPObjectMap->remove(v8ObjectHash);
219         }
220     }
221     v8NpObject->v8Object.Reset();
222     v8NpObject->rootObject = 0;
223 }
224
225 } // namespace blink
226
227 bool _NPN_Invoke(NPP npp, NPObject* npObject, NPIdentifier methodName, const NPVariant* arguments, uint32_t argumentCount, NPVariant* result)
228 {
229     ScriptForbiddenScope::AllowSuperUnsafeScript thisShouldBeRemoved;
230
231     if (!npObject)
232         return false;
233
234     v8::Isolate* isolate = v8::Isolate::GetCurrent();
235
236     V8NPObject* v8NpObject = npObjectToV8NPObject(npObject);
237     if (!v8NpObject) {
238         if (npObject->_class->invoke)
239             return npObject->_class->invoke(npObject, methodName, arguments, argumentCount, result);
240
241         VOID_TO_NPVARIANT(*result);
242         return true;
243     }
244
245     PrivateIdentifier* identifier = static_cast<PrivateIdentifier*>(methodName);
246     if (!identifier->isString)
247         return false;
248
249     if (!strcmp(identifier->value.string, "eval")) {
250         if (argumentCount != 1)
251             return false;
252         if (arguments[0].type != NPVariantType_String)
253             return false;
254         return _NPN_Evaluate(npp, npObject, const_cast<NPString*>(&arguments[0].value.stringValue), result);
255     }
256
257     // FIXME: should use the plugin's owner frame as the security context.
258     ScriptState* scriptState = mainWorldScriptState(isolate, npp, npObject);
259     if (!scriptState)
260         return false;
261
262     ScriptState::Scope scope(scriptState);
263     ExceptionCatcher exceptionCatcher;
264
265     v8::Handle<v8::Object> v8Object = v8::Local<v8::Object>::New(isolate, v8NpObject->v8Object);
266     v8::Handle<v8::Value> functionObject = v8Object->Get(v8AtomicString(isolate, identifier->value.string));
267     if (functionObject.IsEmpty() || functionObject->IsNull()) {
268         NULL_TO_NPVARIANT(*result);
269         return false;
270     }
271     if (functionObject->IsUndefined()) {
272         VOID_TO_NPVARIANT(*result);
273         return false;
274     }
275
276     LocalFrame* frame = v8NpObject->rootObject->frame();
277     ASSERT(frame);
278
279     // Call the function object.
280     v8::Handle<v8::Function> function = v8::Handle<v8::Function>::Cast(functionObject);
281     OwnPtr<v8::Handle<v8::Value>[]> argv = createValueListFromVariantArgs(arguments, argumentCount, npObject, isolate);
282     v8::Local<v8::Value> resultObject = frame->script().callFunction(function, v8Object, argumentCount, argv.get());
283
284     // If we had an error, return false.  The spec is a little unclear here, but says "Returns true if the method was
285     // successfully invoked".  If we get an error return value, was that successfully invoked?
286     if (resultObject.IsEmpty())
287         return false;
288
289     convertV8ObjectToNPVariant(resultObject, npObject, result, isolate);
290     return true;
291 }
292
293 // FIXME: Fix it same as _NPN_Invoke (HandleScope and such).
294 bool _NPN_InvokeDefault(NPP npp, NPObject* npObject, const NPVariant* arguments, uint32_t argumentCount, NPVariant* result)
295 {
296     ScriptForbiddenScope::AllowSuperUnsafeScript thisShouldBeRemoved;
297
298     if (!npObject)
299         return false;
300
301     v8::Isolate* isolate = v8::Isolate::GetCurrent();
302
303     V8NPObject* v8NpObject = npObjectToV8NPObject(npObject);
304     if (!v8NpObject) {
305         if (npObject->_class->invokeDefault)
306             return npObject->_class->invokeDefault(npObject, arguments, argumentCount, result);
307
308         VOID_TO_NPVARIANT(*result);
309         return true;
310     }
311
312     VOID_TO_NPVARIANT(*result);
313
314     ScriptState* scriptState = mainWorldScriptState(isolate, npp, npObject);
315     if (!scriptState)
316         return false;
317
318     ScriptState::Scope scope(scriptState);
319     ExceptionCatcher exceptionCatcher;
320
321     // Lookup the function object and call it.
322     v8::Local<v8::Object> functionObject = v8::Local<v8::Object>::New(isolate, v8NpObject->v8Object);
323     if (!functionObject->IsFunction())
324         return false;
325
326     v8::Local<v8::Value> resultObject;
327     v8::Handle<v8::Function> function = v8::Local<v8::Function>::Cast(functionObject);
328     if (!function->IsNull()) {
329         LocalFrame* frame = v8NpObject->rootObject->frame();
330         ASSERT(frame);
331
332         OwnPtr<v8::Handle<v8::Value>[]> argv = createValueListFromVariantArgs(arguments, argumentCount, npObject, isolate);
333         resultObject = frame->script().callFunction(function, functionObject, argumentCount, argv.get());
334     }
335     // If we had an error, return false.  The spec is a little unclear here, but says "Returns true if the method was
336     // successfully invoked".  If we get an error return value, was that successfully invoked?
337     if (resultObject.IsEmpty())
338         return false;
339
340     convertV8ObjectToNPVariant(resultObject, npObject, result, isolate);
341     return true;
342 }
343
344 bool _NPN_Evaluate(NPP npp, NPObject* npObject, NPString* npScript, NPVariant* result)
345 {
346     // FIXME: Give the embedder a way to control this.
347     bool popupsAllowed = false;
348     return _NPN_EvaluateHelper(npp, popupsAllowed, npObject, npScript, result);
349 }
350
351 bool _NPN_EvaluateHelper(NPP npp, bool popupsAllowed, NPObject* npObject, NPString* npScript, NPVariant* result)
352 {
353     ScriptForbiddenScope::AllowSuperUnsafeScript thisShouldBeRemoved;
354
355     VOID_TO_NPVARIANT(*result);
356     if (!npObject)
357         return false;
358
359     V8NPObject* v8NpObject = npObjectToV8NPObject(npObject);
360     if (!v8NpObject)
361         return false;
362
363     v8::Isolate* isolate = v8::Isolate::GetCurrent();
364     ScriptState* scriptState = mainWorldScriptState(isolate, npp, npObject);
365     if (!scriptState)
366         return false;
367
368     ScriptState::Scope scope(scriptState);
369     ExceptionCatcher exceptionCatcher;
370
371     // FIXME: Is this branch still needed after switching to using UserGestureIndicator?
372     String filename;
373     if (!popupsAllowed)
374         filename = "npscript";
375
376     LocalFrame* frame = v8NpObject->rootObject->frame();
377     ASSERT(frame);
378
379     String script = String::fromUTF8(npScript->UTF8Characters, npScript->UTF8Length);
380
381     UserGestureIndicator gestureIndicator(popupsAllowed ? DefinitelyProcessingNewUserGesture : PossiblyProcessingUserGesture);
382     v8::Local<v8::Value> v8result = frame->script().executeScriptAndReturnValue(scriptState->context(), ScriptSourceCode(script, KURL(ParsedURLString, filename)));
383
384     if (v8result.IsEmpty())
385         return false;
386
387     if (_NPN_IsAlive(npObject))
388         convertV8ObjectToNPVariant(v8result, npObject, result, isolate);
389     return true;
390 }
391
392 bool _NPN_GetProperty(NPP npp, NPObject* npObject, NPIdentifier propertyName, NPVariant* result)
393 {
394     if (!npObject)
395         return false;
396
397     if (V8NPObject* object = npObjectToV8NPObject(npObject)) {
398         v8::Isolate* isolate = v8::Isolate::GetCurrent();
399         ScriptState* scriptState = mainWorldScriptState(isolate, npp, npObject);
400         if (!scriptState)
401             return false;
402
403         ScriptState::Scope scope(scriptState);
404         ExceptionCatcher exceptionCatcher;
405
406         v8::Handle<v8::Object> obj = v8::Local<v8::Object>::New(isolate, object->v8Object);
407         v8::Local<v8::Value> v8result = obj->Get(npIdentifierToV8Identifier(propertyName, isolate));
408
409         if (v8result.IsEmpty())
410             return false;
411
412         convertV8ObjectToNPVariant(v8result, npObject, result, isolate);
413         return true;
414     }
415
416     if (npObject->_class->hasProperty && npObject->_class->getProperty) {
417         if (npObject->_class->hasProperty(npObject, propertyName))
418             return npObject->_class->getProperty(npObject, propertyName, result);
419     }
420
421     VOID_TO_NPVARIANT(*result);
422     return false;
423 }
424
425 bool _NPN_SetProperty(NPP npp, NPObject* npObject, NPIdentifier propertyName, const NPVariant* value)
426 {
427     if (!npObject)
428         return false;
429
430     if (V8NPObject* object = npObjectToV8NPObject(npObject)) {
431         v8::Isolate* isolate = v8::Isolate::GetCurrent();
432         ScriptState* scriptState = mainWorldScriptState(isolate, npp, npObject);
433         if (!scriptState)
434             return false;
435
436         ScriptState::Scope scope(scriptState);
437         ExceptionCatcher exceptionCatcher;
438
439         v8::Handle<v8::Object> obj = v8::Local<v8::Object>::New(isolate, object->v8Object);
440         obj->Set(npIdentifierToV8Identifier(propertyName, isolate), convertNPVariantToV8Object(value, object->rootObject->frame()->script().windowScriptNPObject(), isolate));
441         return true;
442     }
443
444     if (npObject->_class->setProperty)
445         return npObject->_class->setProperty(npObject, propertyName, value);
446
447     return false;
448 }
449
450 bool _NPN_RemoveProperty(NPP npp, NPObject* npObject, NPIdentifier propertyName)
451 {
452     if (!npObject)
453         return false;
454
455     V8NPObject* object = npObjectToV8NPObject(npObject);
456     if (!object)
457         return false;
458
459     v8::Isolate* isolate = v8::Isolate::GetCurrent();
460     ScriptState* scriptState = mainWorldScriptState(isolate, npp, npObject);
461     if (!scriptState)
462         return false;
463     ScriptState::Scope scope(scriptState);
464     ExceptionCatcher exceptionCatcher;
465
466     v8::Handle<v8::Object> obj = v8::Local<v8::Object>::New(isolate, object->v8Object);
467     // FIXME: Verify that setting to undefined is right.
468     obj->Set(npIdentifierToV8Identifier(propertyName, isolate), v8::Undefined(isolate));
469     return true;
470 }
471
472 bool _NPN_HasProperty(NPP npp, NPObject* npObject, NPIdentifier propertyName)
473 {
474     if (!npObject)
475         return false;
476
477     if (V8NPObject* object = npObjectToV8NPObject(npObject)) {
478         v8::Isolate* isolate = v8::Isolate::GetCurrent();
479         ScriptState* scriptState = mainWorldScriptState(isolate, npp, npObject);
480         if (!scriptState)
481             return false;
482         ScriptState::Scope scope(scriptState);
483         ExceptionCatcher exceptionCatcher;
484
485         v8::Handle<v8::Object> obj = v8::Local<v8::Object>::New(isolate, object->v8Object);
486         return obj->Has(npIdentifierToV8Identifier(propertyName, isolate));
487     }
488
489     if (npObject->_class->hasProperty)
490         return npObject->_class->hasProperty(npObject, propertyName);
491     return false;
492 }
493
494 bool _NPN_HasMethod(NPP npp, NPObject* npObject, NPIdentifier methodName)
495 {
496     if (!npObject)
497         return false;
498
499     if (V8NPObject* object = npObjectToV8NPObject(npObject)) {
500         v8::Isolate* isolate = v8::Isolate::GetCurrent();
501         ScriptState* scriptState = mainWorldScriptState(isolate, npp, npObject);
502         if (!scriptState)
503             return false;
504         ScriptState::Scope scope(scriptState);
505         ExceptionCatcher exceptionCatcher;
506
507         v8::Handle<v8::Object> obj = v8::Local<v8::Object>::New(isolate, object->v8Object);
508         v8::Handle<v8::Value> prop = obj->Get(npIdentifierToV8Identifier(methodName, isolate));
509         return prop->IsFunction();
510     }
511
512     if (npObject->_class->hasMethod)
513         return npObject->_class->hasMethod(npObject, methodName);
514     return false;
515 }
516
517 void _NPN_SetException(NPObject* npObject, const NPUTF8 *message)
518 {
519     if (!npObject || !npObjectToV8NPObject(npObject)) {
520         // We won't be able to find a proper scope for this exception, so just throw it.
521         // This is consistent with JSC, which throws a global exception all the time.
522         V8ThrowException::throwGeneralError(message, v8::Isolate::GetCurrent());
523         return;
524     }
525
526     v8::Isolate* isolate = v8::Isolate::GetCurrent();
527     ScriptState* scriptState = mainWorldScriptState(isolate, 0, npObject);
528     if (!scriptState)
529         return;
530
531     ScriptState::Scope scope(scriptState);
532     ExceptionCatcher exceptionCatcher;
533
534     V8ThrowException::throwGeneralError(message, isolate);
535 }
536
537 bool _NPN_Enumerate(NPP npp, NPObject* npObject, NPIdentifier** identifier, uint32_t* count)
538 {
539     if (!npObject)
540         return false;
541
542     if (V8NPObject* object = npObjectToV8NPObject(npObject)) {
543         v8::Isolate* isolate = v8::Isolate::GetCurrent();
544         ScriptState* scriptState = mainWorldScriptState(isolate, npp, npObject);
545         if (!scriptState)
546             return false;
547         ScriptState::Scope scope(scriptState);
548         ExceptionCatcher exceptionCatcher;
549
550         v8::Handle<v8::Object> obj = v8::Local<v8::Object>::New(isolate, object->v8Object);
551
552         // FIXME: http://b/issue?id=1210340: Use a v8::Object::Keys() method when it exists, instead of evaluating javascript.
553
554         // FIXME: Figure out how to cache this helper function.  Run a helper function that collects the properties
555         // on the object into an array.
556         const char enumeratorCode[] =
557             "(function (obj) {"
558             "  var props = [];"
559             "  for (var prop in obj) {"
560             "    props[props.length] = prop;"
561             "  }"
562             "  return props;"
563             "});";
564         v8::Handle<v8::String> source = v8AtomicString(isolate, enumeratorCode);
565         v8::Handle<v8::Value> result = V8ScriptRunner::compileAndRunInternalScript(source, isolate);
566         ASSERT(!result.IsEmpty());
567         ASSERT(result->IsFunction());
568         v8::Handle<v8::Function> enumerator = v8::Handle<v8::Function>::Cast(result);
569         v8::Handle<v8::Value> argv[] = { obj };
570         v8::Local<v8::Value> propsObj = V8ScriptRunner::callInternalFunction(enumerator, v8::Handle<v8::Object>::Cast(result), WTF_ARRAY_LENGTH(argv), argv, isolate);
571         if (propsObj.IsEmpty())
572             return false;
573
574         // Convert the results into an array of NPIdentifiers.
575         v8::Handle<v8::Array> props = v8::Handle<v8::Array>::Cast(propsObj);
576         *count = props->Length();
577         *identifier = static_cast<NPIdentifier*>(calloc(*count, sizeof(NPIdentifier)));
578         for (uint32_t i = 0; i < *count; ++i) {
579             v8::Local<v8::Value> name = props->Get(v8::Integer::New(isolate, i));
580             (*identifier)[i] = getStringIdentifier(v8::Local<v8::String>::Cast(name));
581         }
582         return true;
583     }
584
585     if (NP_CLASS_STRUCT_VERSION_HAS_ENUM(npObject->_class) && npObject->_class->enumerate)
586        return npObject->_class->enumerate(npObject, identifier, count);
587
588     return false;
589 }
590
591 bool _NPN_Construct(NPP npp, NPObject* npObject, const NPVariant* arguments, uint32_t argumentCount, NPVariant* result)
592 {
593     if (!npObject)
594         return false;
595
596     v8::Isolate* isolate = v8::Isolate::GetCurrent();
597
598     if (V8NPObject* object = npObjectToV8NPObject(npObject)) {
599         ScriptState* scriptState = mainWorldScriptState(isolate, npp, npObject);
600         if (!scriptState)
601             return false;
602         ScriptState::Scope scope(scriptState);
603         ExceptionCatcher exceptionCatcher;
604
605         // Lookup the constructor function.
606         v8::Handle<v8::Object> ctorObj = v8::Local<v8::Object>::New(isolate, object->v8Object);
607         if (!ctorObj->IsFunction())
608             return false;
609
610         // Call the constructor.
611         v8::Local<v8::Value> resultObject;
612         v8::Handle<v8::Function> ctor = v8::Handle<v8::Function>::Cast(ctorObj);
613         if (!ctor->IsNull()) {
614             LocalFrame* frame = object->rootObject->frame();
615             ASSERT(frame);
616             OwnPtr<v8::Handle<v8::Value>[]> argv = createValueListFromVariantArgs(arguments, argumentCount, npObject, isolate);
617             resultObject = V8ObjectConstructor::newInstanceInDocument(isolate, ctor, argumentCount, argv.get(), frame ? frame->document() : 0);
618         }
619
620         if (resultObject.IsEmpty())
621             return false;
622
623         convertV8ObjectToNPVariant(resultObject, npObject, result, isolate);
624         return true;
625     }
626
627     if (NP_CLASS_STRUCT_VERSION_HAS_CTOR(npObject->_class) && npObject->_class->construct)
628         return npObject->_class->construct(npObject, arguments, argumentCount, result);
629
630     return false;
631 }