summaryrefslogtreecommitdiffstats
path: root/WebCore/bridge/jni/v8/JavaInstanceV8.cpp
diff options
context:
space:
mode:
authorSteve Block <steveblock@google.com>2010-01-21 15:36:39 +0000
committerSteve Block <steveblock@google.com>2010-01-21 18:21:54 +0000
commit40c8c4abfc2d689e4aa21c5dfab61ce45e45b38b (patch)
treee67974ff50fbafbdfbd4149ef08886c273a5deaf /WebCore/bridge/jni/v8/JavaInstanceV8.cpp
parentbd0a8b8ddfa1230848d33782c0e0f04f739dd550 (diff)
downloadexternal_webkit-40c8c4abfc2d689e4aa21c5dfab61ce45e45b38b.zip
external_webkit-40c8c4abfc2d689e4aa21c5dfab61ce45e45b38b.tar.gz
external_webkit-40c8c4abfc2d689e4aa21c5dfab61ce45e45b38b.tar.bz2
Move V8 jni_instance to bridge/jni/v8/JavaInstanceV8 and fix style
This is the V8 equivalent of http://trac.webkit.org/changeset/53412 It is being upstreamed to webkit.org in https://bugs.webkit.org/show_bug.cgi?id=33951 Change-Id: Id22d59b7a5e0be72d594d454bb41c58760d824ff
Diffstat (limited to 'WebCore/bridge/jni/v8/JavaInstanceV8.cpp')
-rw-r--r--WebCore/bridge/jni/v8/JavaInstanceV8.cpp168
1 files changed, 168 insertions, 0 deletions
diff --git a/WebCore/bridge/jni/v8/JavaInstanceV8.cpp b/WebCore/bridge/jni/v8/JavaInstanceV8.cpp
new file mode 100644
index 0000000..13d4984
--- /dev/null
+++ b/WebCore/bridge/jni/v8/JavaInstanceV8.cpp
@@ -0,0 +1,168 @@
+/*
+ * Copyright 2010, The Android Open Source Project
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "JavaInstanceV8.h"
+
+#include "JNIUtility.h"
+#include "JavaClassV8.h"
+#include "jni_runtime.h"
+
+#include <assert.h>
+#include <utils/Log.h>
+
+#define LOG_TAG "v8binding"
+
+using namespace JSC::Bindings;
+
+JavaInstance::JavaInstance(jobject instance)
+{
+ m_instance = new JObjectWrapper(instance);
+ m_class = 0;
+}
+
+JavaInstance::~JavaInstance()
+{
+ m_instance = 0;
+ delete m_class;
+}
+
+JavaClass* JavaInstance::getClass() const
+{
+ if (!m_class) {
+ m_class = new JavaClass(javaInstance());
+ }
+ return m_class;
+}
+
+bool JavaInstance::invokeMethod(const char* methodName, const NPVariant* args, int count, NPVariant* resultValue)
+{
+ VOID_TO_NPVARIANT(*resultValue);
+
+ MethodList methodList = getClass()->methodsNamed(methodName);
+
+ size_t numMethods = methodList.size();
+
+ // Try to find a good match for the overloaded method. The
+ // fundamental problem is that JavaScript doesn have the
+ // notion of method overloading and Java does. We could
+ // get a bit more sophisticated and attempt to does some
+ // type checking as we as checking the number of parameters.
+ JavaMethod* aMethod;
+ JavaMethod* method = 0;
+ for (size_t methodIndex = 0; methodIndex < numMethods; methodIndex++) {
+ aMethod = methodList[methodIndex];
+ if (aMethod->numParameters() == count) {
+ method = aMethod;
+ break;
+ }
+ }
+ if (!method) {
+ LOGW("unable to find an appropiate method\n");
+ return false;
+ }
+
+ const JavaMethod* jMethod = static_cast<const JavaMethod*>(method);
+
+ jvalue* jArgs = 0;
+ if (count > 0)
+ jArgs = (jvalue*)malloc (count * sizeof(jvalue));
+
+ for (int i = 0; i < count; i++) {
+ JavaParameter* aParameter = jMethod->parameterAt(i);
+ jArgs[i] = convertNPVariantToJValue(args[i], aParameter->getJNIType(), aParameter->type());
+ }
+
+ jvalue result;
+
+ // The following code can be conditionally removed once we have a Tiger update that
+ // contains the new Java plugin. It is needed for builds prior to Tiger.
+ {
+ jobject obj = javaInstance();
+ switch (jMethod->JNIReturnType()) {
+ case void_type:
+ callJNIMethodIDA<void>(obj, jMethod->methodID(obj), jArgs);
+ break;
+ case object_type:
+ result.l = callJNIMethodIDA<jobject>(obj, jMethod->methodID(obj), jArgs);
+ break;
+ case boolean_type:
+ result.z = callJNIMethodIDA<jboolean>(obj, jMethod->methodID(obj), jArgs);
+ break;
+ case byte_type:
+ result.b = callJNIMethodIDA<jbyte>(obj, jMethod->methodID(obj), jArgs);
+ break;
+ case char_type:
+ result.c = callJNIMethodIDA<jchar>(obj, jMethod->methodID(obj), jArgs);
+ break;
+ case short_type:
+ result.s = callJNIMethodIDA<jshort>(obj, jMethod->methodID(obj), jArgs);
+ break;
+ case int_type:
+ result.i = callJNIMethodIDA<jint>(obj, jMethod->methodID(obj), jArgs);
+ break;
+
+ case long_type:
+ result.j = callJNIMethodIDA<jlong>(obj, jMethod->methodID(obj), jArgs);
+ break;
+ case float_type:
+ result.f = callJNIMethodIDA<jfloat>(obj, jMethod->methodID(obj), jArgs);
+ break;
+ case double_type:
+ result.d = callJNIMethodIDA<jdouble>(obj, jMethod->methodID(obj), jArgs);
+ break;
+ case invalid_type:
+ default:
+ break;
+ }
+ }
+
+ convertJValueToNPVariant(result, jMethod->JNIReturnType(), jMethod->returnType(), resultValue);
+ free(jArgs);
+
+ return true;
+}
+
+JObjectWrapper::JObjectWrapper(jobject instance)
+ : m_refCount(0)
+{
+ assert(instance);
+
+ // Cache the JNIEnv used to get the global ref for this java instanace.
+ // It'll be used to delete the reference.
+ m_env = getJNIEnv();
+
+ m_instance = m_env->NewGlobalRef(instance);
+
+ LOGV("new global ref %p for %p\n", m_instance, instance);
+
+ if (!m_instance)
+ fprintf (stderr, "%s: could not get GlobalRef for %p\n", __PRETTY_FUNCTION__, instance);
+}
+
+JObjectWrapper::~JObjectWrapper() {
+ LOGV("deleting global ref %p\n", m_instance);
+ m_env->DeleteGlobalRef(m_instance);
+}