JSValue *RuntimeMethod::callAsFunction(ExecState *exec, JSObject *thisObj, const List &args) { if (_methodList->isEmpty()) return jsUndefined(); RuntimeObjectImp *imp = 0; if (thisObj->classInfo() == &KJS::RuntimeObjectImp::info) { imp = static_cast<RuntimeObjectImp*>(thisObj); } else { // If thisObj is the DOM object for a plugin, get the corresponding // runtime object from the DOM object. JSValue* value = thisObj->get(exec, "__apple_runtime_object"); if (value->isObject(&KJS::RuntimeObjectImp::info)) imp = static_cast<RuntimeObjectImp*>(value); } if (!imp) return throwError(exec, TypeError); Instance *instance = imp->getInternalInstance(); if (!instance) return RuntimeObjectImp::throwInvalidAccessError(exec); instance->begin(); JSValue *aValue = instance->invokeMethod(exec, *_methodList, args); instance->end(); return aValue; }
static JSValue JSC_HOST_CALL callRuntimeMethod(ExecState* exec, JSObject* function, JSValue thisValue, const ArgList& args) { RuntimeMethod* method = static_cast<RuntimeMethod*>(function); if (method->methods()->isEmpty()) return jsUndefined(); RuntimeObjectImp* imp; if (thisValue.isObject(&RuntimeObjectImp::s_info)) { imp = static_cast<RuntimeObjectImp*>(asObject(thisValue)); } else { // If thisObj is the DOM object for a plugin, get the corresponding // runtime object from the DOM object. JSValue value = thisValue.get(exec, Identifier(exec, "__apple_runtime_object")); if (value.isObject(&RuntimeObjectImp::s_info)) imp = static_cast<RuntimeObjectImp*>(asObject(value)); else return throwError(exec, TypeError); } RefPtr<Instance> instance = imp->getInternalInstance(); if (!instance) return RuntimeObjectImp::throwInvalidAccessError(exec); instance->begin(); JSValue result = instance->invokeMethod(exec, *method->methods(), args); instance->end(); return result; }
// 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); } } } }
// 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); } } } }
jvalue convertValueToJValue(ExecState* exec, JSValue value, JNIType jniType, const char* javaClassName) { JSLock lock(SilenceAssertionsOnly); jvalue result; switch (jniType) { case array_type: case object_type: { result.l = (jobject)0; // First see if we have a Java instance. if (value.isObject()) { JSObject* objectImp = asObject(value); if (objectImp->classInfo() == &RuntimeObjectImp::s_info) { RuntimeObjectImp* imp = static_cast<RuntimeObjectImp*>(objectImp); JavaInstance* instance = static_cast<JavaInstance*>(imp->getInternalInstance()); if (instance) result.l = instance->javaInstance(); } else if (objectImp->classInfo() == &RuntimeArray::s_info) { // Input is a JavaScript Array that was originally created from a Java Array RuntimeArray* imp = static_cast<RuntimeArray*>(objectImp); JavaArray* array = static_cast<JavaArray*>(imp->getConcreteArray()); result.l = array->javaArray(); } else if (objectImp->classInfo() == &JSArray::info) { // Input is a Javascript Array. We need to create it to a Java Array. result.l = convertArrayInstanceToJavaArray(exec, asArray(value), javaClassName); } } // Now convert value to a string if the target type is a java.lang.string, and we're not // converting from a Null. if (!result.l && !strcmp(javaClassName, "java.lang.String")) { #ifdef CONVERT_NULL_TO_EMPTY_STRING if (value->isNull()) { JNIEnv* env = getJNIEnv(); jchar buf[2]; jobject javaString = env->functions->NewString(env, buf, 0); result.l = javaString; } else #else if (!value.isNull()) #endif { UString stringValue = value.toString(exec); JNIEnv* env = getJNIEnv(); jobject javaString = env->functions->NewString(env, (const jchar *)stringValue.data(), stringValue.size()); result.l = javaString; } } else if (!result.l) // ANDROID memset(&result, 0, sizeof(jvalue)); // Handle it the same as a void case } break; case boolean_type: { result.z = (jboolean)value.toNumber(exec); } break; case byte_type: { result.b = (jbyte)value.toNumber(exec); } break; case char_type: { result.c = (jchar)value.toNumber(exec); } break; case short_type: { result.s = (jshort)value.toNumber(exec); } break; case int_type: { result.i = (jint)value.toNumber(exec); } break; case long_type: { result.j = (jlong)value.toNumber(exec); } break; case float_type: { result.f = (jfloat)value.toNumber(exec); } break; case double_type: { result.d = (jdouble)value.toNumber(exec); } break; break; case invalid_type: default: case void_type: { // ANDROID memset(&result, 0, sizeof(jvalue)); } break; } return result; }