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