NPObject* npCreateV8ScriptObject(NPP npp, v8::Handle<v8::Object> object, DOMWindow* root, v8::Isolate* isolate) { // Check to see if this object is already wrapped. if (object->InternalFieldCount() == npObjectInternalFieldCount) { const WrapperTypeInfo* typeInfo = static_cast<const WrapperTypeInfo*>(object->GetAlignedPointerFromInternalField(v8DOMWrapperTypeIndex)); if (typeInfo == npObjectTypeInfo()) { NPObject* returnValue = v8ObjectToNPObject(object); _NPN_RetainObject(returnValue); return returnValue; } } V8NPObjectVector* objectVector = 0; if (V8PerContextData* perContextData = V8PerContextData::from(object->CreationContext())) { int v8ObjectHash = object->GetIdentityHash(); ASSERT(v8ObjectHash); V8NPObjectMap* v8NPObjectMap = perContextData->v8NPObjectMap(); V8NPObjectMap::iterator iter = v8NPObjectMap->find(v8ObjectHash); if (iter != v8NPObjectMap->end()) { V8NPObjectVector& objects = iter->value; for (size_t index = 0; index < objects.size(); ++index) { V8NPObject* v8npObject = objects.at(index); if (v8npObject->v8Object == object && v8npObject->rootObject == root) { _NPN_RetainObject(&v8npObject->object); return reinterpret_cast<NPObject*>(v8npObject); } } } else { iter = v8NPObjectMap->set(v8ObjectHash, V8NPObjectVector()).iterator; } objectVector = &iter->value; } V8NPObject* v8npObject = reinterpret_cast<V8NPObject*>(_NPN_CreateObject(npp, &V8NPObjectClass)); // This is uninitialized memory, we need to clear it so that // Persistent::Reset won't try to Dispose anything bogus. v8npObject->v8Object.Clear(); v8npObject->v8Object.Reset(isolate, object); v8npObject->rootObject = root; if (objectVector) objectVector->append(v8npObject); return reinterpret_cast<NPObject*>(v8npObject); }
NPObject* npCreateV8ScriptObject(NPP npp, v8::Handle<v8::Object> object, DOMWindow* root) { // Check to see if this object is already wrapped. if (object->InternalFieldCount() == npObjectInternalFieldCount) { WrapperTypeInfo* typeInfo = static_cast<WrapperTypeInfo*>(object->GetAlignedPointerFromInternalField(v8DOMWrapperTypeIndex)); if (typeInfo == npObjectTypeInfo()) { NPObject* returnValue = v8ObjectToNPObject(object); _NPN_RetainObject(returnValue); return returnValue; } } V8NPObjectVector* objectVector = 0; if (V8PerContextData* perContextData = V8PerContextData::from(object->CreationContext())) { int v8ObjectHash = object->GetIdentityHash(); ASSERT(v8ObjectHash); V8NPObjectMap* v8NPObjectMap = perContextData->v8NPObjectMap(); V8NPObjectMap::iterator iter = v8NPObjectMap->find(v8ObjectHash); if (iter != v8NPObjectMap->end()) { V8NPObjectVector& objects = iter->value; for (size_t index = 0; index < objects.size(); ++index) { V8NPObject* v8npObject = objects.at(index); if (v8npObject->v8Object == object && v8npObject->rootObject == root) { _NPN_RetainObject(&v8npObject->object); return reinterpret_cast<NPObject*>(v8npObject); } } } else { iter = v8NPObjectMap->set(v8ObjectHash, V8NPObjectVector()).iterator; } objectVector = &iter->value; } V8NPObject* v8npObject = reinterpret_cast<V8NPObject*>(_NPN_CreateObject(npp, &V8NPObjectClass)); v8npObject->v8Object = v8::Persistent<v8::Object>::New(v8::Isolate::GetCurrent(), object); v8npObject->rootObject = root; if (objectVector) objectVector->append(v8npObject); return reinterpret_cast<NPObject*>(v8npObject); }
// Variant value must be released with NPReleaseVariantValue() void convertValueToNPVariant(ExecState *exec, JSValue *value, NPVariant *result) { JSLock lock; JSType type = value->type(); VOID_TO_NPVARIANT(*result); if (type == StringType) { UString ustring = value->toString(exec); CString cstring = ustring.UTF8String(); NPString string = { (const NPUTF8 *)cstring.c_str(), static_cast<uint32_t>(cstring.size()) }; NPN_InitializeVariantWithStringCopy(result, &string); } else if (type == NumberType) { DOUBLE_TO_NPVARIANT(value->toNumber(exec), *result); } else if (type == BooleanType) { BOOLEAN_TO_NPVARIANT(value->toBoolean(exec), *result); } else if (type == UnspecifiedType) { VOID_TO_NPVARIANT(*result); } else if (type == NullType) { NULL_TO_NPVARIANT(*result); } else if (type == ObjectType) { JSObject* object = static_cast<JSObject*>(value); if (object->classInfo() == &RuntimeObjectImp::info) { RuntimeObjectImp* imp = static_cast<RuntimeObjectImp *>(value); CInstance* instance = static_cast<CInstance*>(imp->getInternalInstance()); if (instance) { NPObject* obj = instance->getObject(); _NPN_RetainObject(obj); OBJECT_TO_NPVARIANT(obj, *result); } } else { Interpreter* originInterpreter = exec->dynamicInterpreter(); RootObject* originRootObject = findRootObject(originInterpreter); Interpreter* interpreter = 0; if (originInterpreter->isGlobalObject(value)) { interpreter = originInterpreter->interpreterForGlobalObject(value); } if (!interpreter) interpreter = originInterpreter; RootObject* rootObject = findRootObject(interpreter); if (rootObject) { NPObject* npObject = _NPN_CreateScriptObject(0, object, originRootObject, rootObject); OBJECT_TO_NPVARIANT(npObject, *result); } } } }
v8::Local<v8::Object> createV8ObjectForNPObject(NPObject* object, NPObject* root) { static v8::Persistent<v8::FunctionTemplate> npObjectDesc; ASSERT(v8::Context::InContext()); v8::Isolate* isolate = v8::Isolate::GetCurrent(); // If this is a v8 object, just return it. V8NPObject* v8NPObject = npObjectToV8NPObject(object); if (v8NPObject) return v8::Local<v8::Object>::New(isolate, v8NPObject->v8Object); // If we've already wrapped this object, just return it. v8::Handle<v8::Object> wrapper = staticNPObjectMap().get(object); if (!wrapper.IsEmpty()) return v8::Local<v8::Object>::New(isolate, wrapper); // FIXME: we should create a Wrapper type as a subclass of JSObject. It has two internal fields, field 0 is the wrapped // pointer, and field 1 is the type. There should be an api function that returns unused type id. The same Wrapper type // can be used by DOM bindings. if (npObjectDesc.IsEmpty()) { v8::Handle<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(); templ->InstanceTemplate()->SetInternalFieldCount(npObjectInternalFieldCount); templ->InstanceTemplate()->SetNamedPropertyHandler(npObjectNamedPropertyGetter, npObjectNamedPropertySetter, npObjectQueryProperty, 0, npObjectNamedPropertyEnumerator); templ->InstanceTemplate()->SetIndexedPropertyHandler(npObjectIndexedPropertyGetter, npObjectIndexedPropertySetter, 0, 0, npObjectIndexedPropertyEnumerator); templ->InstanceTemplate()->SetCallAsFunctionHandler(npObjectInvokeDefaultHandler); npObjectDesc.Reset(isolate, templ); } // FIXME: Move staticNPObjectMap() to DOMDataStore. // Use V8DOMWrapper::createWrapper() and // V8DOMWrapper::associateObjectWithWrapper() // to create a wrapper object. v8::Handle<v8::Function> v8Function = v8::Local<v8::FunctionTemplate>::New(isolate, npObjectDesc)->GetFunction(); v8::Local<v8::Object> value = V8ObjectConstructor::newInstance(v8Function); if (value.IsEmpty()) return value; V8DOMWrapper::setNativeInfo(value, npObjectTypeInfo(), object); // KJS retains the object as part of its wrapper (see Bindings::CInstance). _NPN_RetainObject(object); _NPN_RegisterObject(object, root); WrapperConfiguration configuration = buildWrapperConfiguration(object, WrapperConfiguration::Dependent); staticNPObjectMap().set(object, value, configuration); ASSERT(V8DOMWrapper::maybeDOMWrapper(value)); return value; }
// Variant value must be released with NPReleaseVariantValue() void convertValueToNPVariant(ExecState* exec, JSValue value, NPVariant* result) { JSLock lock(SilenceAssertionsOnly); VOID_TO_NPVARIANT(*result); if (value.isString()) { UString ustring = value.toString(exec); CString cstring = ustring.utf8(); NPString string = { (const NPUTF8*)cstring.data(), static_cast<uint32_t>(cstring.length()) }; NPN_InitializeVariantWithStringCopy(result, &string); } else if (value.isNumber()) { DOUBLE_TO_NPVARIANT(value.toNumber(exec), *result); } else if (value.isBoolean()) { BOOLEAN_TO_NPVARIANT(value.toBoolean(exec), *result); } else if (value.isNull()) { NULL_TO_NPVARIANT(*result); } else if (value.isObject()) { JSObject* object = asObject(value); if (object->classInfo() == &CRuntimeObject::s_info) { CRuntimeObject* runtimeObject = static_cast<CRuntimeObject*>(object); CInstance* instance = runtimeObject->getInternalCInstance(); if (instance) { NPObject* obj = instance->getObject(); _NPN_RetainObject(obj); OBJECT_TO_NPVARIANT(obj, *result); } } else { #ifdef ANDROID RootObject* rootObject = findRootObject(exec->dynamicGlobalObject()); if (!rootObject) rootObject = findRootObject(exec->lexicalGlobalObject()); #else JSGlobalObject* globalObject = exec->dynamicGlobalObject(); RootObject* rootObject = findRootObject(globalObject); #endif if (rootObject) { NPObject* npObject = _NPN_CreateScriptObject(0, object, rootObject); OBJECT_TO_NPVARIANT(npObject, *result); } } } }
NPObject* npCreateV8ScriptObject(NPP npp, v8::Handle<v8::Object> object, DOMWindow* root) { // Check to see if this object is already wrapped. if (object->InternalFieldCount() == npObjectInternalFieldCount) { WrapperTypeInfo* typeInfo = static_cast<WrapperTypeInfo*>(object->GetPointerFromInternalField(v8DOMWrapperTypeIndex)); if (typeInfo == npObjectTypeInfo()) { NPObject* returnValue = v8ObjectToNPObject(object); _NPN_RetainObject(returnValue); return returnValue; } } V8NPObject* v8npObject = reinterpret_cast<V8NPObject*>(_NPN_CreateObject(npp, &V8NPObjectClass)); v8npObject->v8Object = v8::Persistent<v8::Object>::New(object); #ifndef NDEBUG V8GCController::registerGlobalHandle(NPOBJECT, v8npObject, v8npObject->v8Object); #endif v8npObject->rootObject = root; return reinterpret_cast<NPObject*>(v8npObject); }
NPObject* PepperPluginImpl::scriptableObject(){ // Call through the plugin to get its instance object. The plugin should pass // us a reference which we release in destroy(). if (instance_object_.type == PP_VARTYPE_UNDEFINED) instance_object_ = instance_->GetInstanceObject(); // GetInstanceObject talked to the plugin which may have removed the instance // from the DOM, in which case instance_ would be NULL now. if (!instance_) return NULL; scoped_refptr<NPObjectVar> object(NPObjectVar::FromPPVar(instance_object_)); // If there's an InstanceObject, tell the Instance's MessageChannel to pass // any non-postMessage calls to it. if (object) { instance_->message_channel().SetPassthroughObject(object->np_object()); } NPObject* message_channel_np_object(instance_->message_channel().np_object()); // The object is expected to be retained before it is returned. _NPN_RetainObject(message_channel_np_object); return message_channel_np_object; }
// Variant value must be released with NPReleaseVariantValue() void convertValueToNPVariant(ExecState* exec, JSValuePtr value, NPVariant* result) { JSLock lock(false); VOID_TO_NPVARIANT(*result); if (value.isString()) { UString ustring = value.toString(exec); CString cstring = ustring.UTF8String(); NPString string = { (const NPUTF8*)cstring.c_str(), static_cast<uint32_t>(cstring.size()) }; NPN_InitializeVariantWithStringCopy(result, &string); } else if (value.isNumber()) { DOUBLE_TO_NPVARIANT(value.toNumber(exec), *result); } else if (value.isBoolean()) { BOOLEAN_TO_NPVARIANT(value.toBoolean(exec), *result); } else if (value.isNull()) { NULL_TO_NPVARIANT(*result); } else if (value.isObject()) { JSObject* object = asObject(value); if (object->classInfo() == &RuntimeObjectImp::s_info) { RuntimeObjectImp* imp = static_cast<RuntimeObjectImp*>(object); CInstance* instance = static_cast<CInstance*>(imp->getInternalInstance()); if (instance) { NPObject* obj = instance->getObject(); _NPN_RetainObject(obj); OBJECT_TO_NPVARIANT(obj, *result); } } else { JSGlobalObject* globalObject = exec->dynamicGlobalObject(); RootObject* rootObject = findRootObject(globalObject); if (rootObject) { NPObject* npObject = _NPN_CreateScriptObject(0, object, rootObject); OBJECT_TO_NPVARIANT(npObject, *result); } } } }
NPObject* WebBindings::retainObject(NPObject* object) { return _NPN_RetainObject(object); }
CInstance::CInstance(NPObject* o) { _object = _NPN_RetainObject(o); _class = 0; setRootObject(0); }
CInstance::CInstance(NPObject* o, PassRefPtr<RootObject> rootObject) : Instance(rootObject) { _object = _NPN_RetainObject(o); _class = 0; }
void NPN_InitializeVariantWithObject (NPVariant *variant, NPObject *value) { variant->type = NPVariantType_Object; variant->value.objectValue = _NPN_RetainObject (value); }
NPObject *NPN_RetainObject(NPObject *obj) { return _NPN_RetainObject(obj); }