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