]>
git.saurik.com Git - apple/javascriptcore.git/blob - bindings/jni/jni_utility.cpp
2 * Copyright (C) 2003 Apple Computer, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #if ENABLE(JAVA_BINDINGS)
30 #include "jni_utility.h"
33 #include "jni_runtime.h"
34 #include "runtime_array.h"
35 #include "runtime_object.h"
42 static jint
KJS_GetCreatedJavaVMs(JavaVM
** vmBuf
, jsize bufLen
, jsize
* nVMs
)
44 static void* javaVMFramework
= 0;
46 javaVMFramework
= dlopen("/System/Library/Frameworks/JavaVM.framework/JavaVM", RTLD_LAZY
);
50 static jint(*functionPointer
)(JavaVM
**, jsize
, jsize
*) = 0;
52 functionPointer
= (jint(*)(JavaVM
**, jsize
, jsize
*))dlsym(javaVMFramework
, "JNI_GetCreatedJavaVMs");
55 return functionPointer(vmBuf
, bufLen
, nVMs
);
58 static JavaVM
*jvm
= 0;
60 // Provide the ability for an outside component to specify the JavaVM to use
61 // If the jvm value is set, the getJavaVM function below will just return.
62 // In getJNIEnv(), if AttachCurrentThread is called to a VM that is already
63 // attached, the result is a no-op.
64 void setJavaVM(JavaVM
*javaVM
)
79 // Assumes JVM is already running ..., one per process
80 jniError
= KJS_GetCreatedJavaVMs(jvmArray
, bufLen
, &nJVMs
);
81 if ( jniError
== JNI_OK
&& nJVMs
> 0 ) {
85 fprintf(stderr
, "%s: JNI_GetCreatedJavaVMs failed, returned %ld\n", __PRETTY_FUNCTION__
, (long)jniError
);
98 jniError
= (getJavaVM())->AttachCurrentThread(&u
.dummy
, NULL
);
99 if (jniError
== JNI_OK
)
102 fprintf(stderr
, "%s: AttachCurrentThread failed, returned %ld\n", __PRETTY_FUNCTION__
, (long)jniError
);
106 static jvalue
callJNIMethod (JNIType type
, jobject obj
, const char *name
, const char *sig
, va_list args
)
108 JavaVM
*jvm
= getJavaVM();
109 JNIEnv
*env
= getJNIEnv();
112 bzero (&result
, sizeof(jvalue
));
113 if ( obj
!= NULL
&& jvm
!= NULL
&& env
!= NULL
) {
114 jclass cls
= env
->GetObjectClass(obj
);
116 jmethodID mid
= env
->GetMethodID(cls
, name
, sig
);
121 env
->functions
->CallVoidMethodV(env
, obj
, mid
, args
);
125 result
.l
= env
->functions
->CallObjectMethodV(env
, obj
, mid
, args
);
128 result
.z
= env
->functions
->CallBooleanMethodV(env
, obj
, mid
, args
);
131 result
.b
= env
->functions
->CallByteMethodV(env
, obj
, mid
, args
);
134 result
.c
= env
->functions
->CallCharMethodV(env
, obj
, mid
, args
);
137 result
.s
= env
->functions
->CallShortMethodV(env
, obj
, mid
, args
);
140 result
.i
= env
->functions
->CallIntMethodV(env
, obj
, mid
, args
);
143 result
.j
= env
->functions
->CallLongMethodV(env
, obj
, mid
, args
);
146 result
.f
= env
->functions
->CallFloatMethodV(env
, obj
, mid
, args
);
149 result
.d
= env
->functions
->CallDoubleMethodV(env
, obj
, mid
, args
);
152 fprintf(stderr
, "%s: invalid function type (%d)\n", __PRETTY_FUNCTION__
, (int)type
);
157 fprintf(stderr
, "%s: Could not find method: %s for %p\n", __PRETTY_FUNCTION__
, name
, obj
);
158 env
->ExceptionDescribe();
159 env
->ExceptionClear();
160 fprintf (stderr
, "\n");
163 env
->DeleteLocalRef(cls
);
166 fprintf(stderr
, "%s: Could not find class for %p\n", __PRETTY_FUNCTION__
, obj
);
173 static jvalue
callJNIStaticMethod (JNIType type
, jclass cls
, const char *name
, const char *sig
, va_list args
)
175 JavaVM
*jvm
= getJavaVM();
176 JNIEnv
*env
= getJNIEnv();
179 bzero (&result
, sizeof(jvalue
));
180 if ( cls
!= NULL
&& jvm
!= NULL
&& env
!= NULL
) {
181 jmethodID mid
= env
->GetStaticMethodID(cls
, name
, sig
);
186 env
->functions
->CallStaticVoidMethodV(env
, cls
, mid
, args
);
190 result
.l
= env
->functions
->CallStaticObjectMethodV(env
, cls
, mid
, args
);
193 result
.z
= env
->functions
->CallStaticBooleanMethodV(env
, cls
, mid
, args
);
196 result
.b
= env
->functions
->CallStaticByteMethodV(env
, cls
, mid
, args
);
199 result
.c
= env
->functions
->CallStaticCharMethodV(env
, cls
, mid
, args
);
202 result
.s
= env
->functions
->CallStaticShortMethodV(env
, cls
, mid
, args
);
205 result
.i
= env
->functions
->CallStaticIntMethodV(env
, cls
, mid
, args
);
208 result
.j
= env
->functions
->CallStaticLongMethodV(env
, cls
, mid
, args
);
211 result
.f
= env
->functions
->CallStaticFloatMethodV(env
, cls
, mid
, args
);
214 result
.d
= env
->functions
->CallStaticDoubleMethodV(env
, cls
, mid
, args
);
217 fprintf(stderr
, "%s: invalid function type (%d)\n", __PRETTY_FUNCTION__
, (int)type
);
222 fprintf(stderr
, "%s: Could not find method: %s for %p\n", __PRETTY_FUNCTION__
, name
, cls
);
223 env
->ExceptionDescribe();
224 env
->ExceptionClear();
225 fprintf (stderr
, "\n");
232 static jvalue
callJNIMethodIDA (JNIType type
, jobject obj
, jmethodID mid
, jvalue
*args
)
234 JNIEnv
*env
= getJNIEnv();
237 bzero (&result
, sizeof(jvalue
));
238 if ( obj
!= NULL
&& mid
!= NULL
)
242 env
->functions
->CallVoidMethodA(env
, obj
, mid
, args
);
246 result
.l
= env
->functions
->CallObjectMethodA(env
, obj
, mid
, args
);
249 result
.z
= env
->functions
->CallBooleanMethodA(env
, obj
, mid
, args
);
252 result
.b
= env
->functions
->CallByteMethodA(env
, obj
, mid
, args
);
255 result
.c
= env
->functions
->CallCharMethodA(env
, obj
, mid
, args
);
258 result
.s
= env
->functions
->CallShortMethodA(env
, obj
, mid
, args
);
261 result
.i
= env
->functions
->CallIntMethodA(env
, obj
, mid
, args
);
264 result
.j
= env
->functions
->CallLongMethodA(env
, obj
, mid
, args
);
267 result
.f
= env
->functions
->CallFloatMethodA(env
, obj
, mid
, args
);
270 result
.d
= env
->functions
->CallDoubleMethodA(env
, obj
, mid
, args
);
273 fprintf(stderr
, "%s: invalid function type (%d)\n", __PRETTY_FUNCTION__
, (int)type
);
280 static jvalue
callJNIMethodA (JNIType type
, jobject obj
, const char *name
, const char *sig
, jvalue
*args
)
282 JavaVM
*jvm
= getJavaVM();
283 JNIEnv
*env
= getJNIEnv();
286 bzero (&result
, sizeof(jvalue
));
287 if ( obj
!= NULL
&& jvm
!= NULL
&& env
!= NULL
) {
288 jclass cls
= env
->GetObjectClass(obj
);
290 jmethodID mid
= env
->GetMethodID(cls
, name
, sig
);
292 result
= callJNIMethodIDA (type
, obj
, mid
, args
);
295 fprintf(stderr
, "%s: Could not find method: %s\n", __PRETTY_FUNCTION__
, name
);
296 env
->ExceptionDescribe();
297 env
->ExceptionClear();
298 fprintf (stderr
, "\n");
301 env
->DeleteLocalRef(cls
);
304 fprintf(stderr
, "%s: Could not find class for object\n", __PRETTY_FUNCTION__
);
311 jmethodID
getMethodID (jobject obj
, const char *name
, const char *sig
)
313 JNIEnv
*env
= getJNIEnv();
317 jclass cls
= env
->GetObjectClass(obj
);
319 mid
= env
->GetMethodID(cls
, name
, sig
);
321 env
->ExceptionClear();
322 mid
= env
->GetStaticMethodID(cls
, name
, sig
);
324 env
->ExceptionClear();
328 env
->DeleteLocalRef(cls
);
334 #define CALL_JNI_METHOD(function_type,obj,name,sig) \
336 va_start (args, sig);\
338 jvalue result = callJNIMethod(function_type, obj, name, sig, args);\
342 #define CALL_JNI_STATIC_METHOD(function_type,cls,name,sig) \
344 va_start (args, sig);\
346 jvalue result = callJNIStaticMethod(function_type, cls, name, sig, args);\
350 void callJNIVoidMethod (jobject obj
, const char *name
, const char *sig
, ... )
352 CALL_JNI_METHOD (void_type
, obj
, name
, sig
);
355 jobject
callJNIObjectMethod (jobject obj
, const char *name
, const char *sig
, ... )
357 CALL_JNI_METHOD (object_type
, obj
, name
, sig
);
361 jboolean
callJNIBooleanMethod( jobject obj
, const char *name
, const char *sig
, ... )
363 CALL_JNI_METHOD (boolean_type
, obj
, name
, sig
);
367 jboolean
callJNIStaticBooleanMethod (jclass cls
, const char *name
, const char *sig
, ... )
369 CALL_JNI_STATIC_METHOD (boolean_type
, cls
, name
, sig
);
373 jbyte
callJNIByteMethod( jobject obj
, const char *name
, const char *sig
, ... )
375 CALL_JNI_METHOD (byte_type
, obj
, name
, sig
);
379 jchar
callJNICharMethod (jobject obj
, const char *name
, const char *sig
, ... )
381 CALL_JNI_METHOD (char_type
, obj
, name
, sig
);
385 jshort
callJNIShortMethod (jobject obj
, const char *name
, const char *sig
, ... )
387 CALL_JNI_METHOD (short_type
, obj
, name
, sig
);
391 jint
callJNIIntMethod (jobject obj
, const char *name
, const char *sig
, ... )
393 CALL_JNI_METHOD (int_type
, obj
, name
, sig
);
397 jlong
callJNILongMethod (jobject obj
, const char *name
, const char *sig
, ... )
399 CALL_JNI_METHOD (long_type
, obj
, name
, sig
);
403 jfloat
callJNIFloatMethod (jobject obj
, const char *name
, const char *sig
, ... )
405 CALL_JNI_METHOD (float_type
, obj
, name
, sig
);
409 jdouble
callJNIDoubleMethod (jobject obj
, const char *name
, const char *sig
, ... )
411 CALL_JNI_METHOD (double_type
, obj
, name
, sig
);
415 void callJNIVoidMethodA (jobject obj
, const char *name
, const char *sig
, jvalue
*args
)
417 jvalue result
= callJNIMethodA (void_type
, obj
, name
, sig
, args
);
420 jobject
callJNIObjectMethodA (jobject obj
, const char *name
, const char *sig
, jvalue
*args
)
422 jvalue result
= callJNIMethodA (object_type
, obj
, name
, sig
, args
);
426 jbyte
callJNIByteMethodA ( jobject obj
, const char *name
, const char *sig
, jvalue
*args
)
428 jvalue result
= callJNIMethodA (byte_type
, obj
, name
, sig
, args
);
432 jchar
callJNICharMethodA (jobject obj
, const char *name
, const char *sig
, jvalue
*args
)
434 jvalue result
= callJNIMethodA (char_type
, obj
, name
, sig
, args
);
438 jshort
callJNIShortMethodA (jobject obj
, const char *name
, const char *sig
, jvalue
*args
)
440 jvalue result
= callJNIMethodA (short_type
, obj
, name
, sig
, args
);
444 jint
callJNIIntMethodA (jobject obj
, const char *name
, const char *sig
, jvalue
*args
)
446 jvalue result
= callJNIMethodA (int_type
, obj
, name
, sig
, args
);
450 jlong
callJNILongMethodA (jobject obj
, const char *name
, const char *sig
, jvalue
*args
)
452 jvalue result
= callJNIMethodA (long_type
, obj
, name
, sig
, args
);
456 jfloat
callJNIFloatMethodA (jobject obj
, const char *name
, const char *sig
, jvalue
*args
)
458 jvalue result
= callJNIMethodA (float_type
, obj
, name
, sig
, args
);
462 jdouble
callJNIDoubleMethodA (jobject obj
, const char *name
, const char *sig
, jvalue
*args
)
464 jvalue result
= callJNIMethodA (double_type
, obj
, name
, sig
, args
);
468 jboolean
callJNIBooleanMethodA (jobject obj
, const char *name
, const char *sig
, jvalue
*args
)
470 jvalue result
= callJNIMethodA (boolean_type
, obj
, name
, sig
, args
);
474 void callJNIVoidMethodIDA (jobject obj
, jmethodID methodID
, jvalue
*args
)
476 jvalue result
= callJNIMethodIDA (void_type
, obj
, methodID
, args
);
479 jobject
callJNIObjectMethodIDA (jobject obj
, jmethodID methodID
, jvalue
*args
)
481 jvalue result
= callJNIMethodIDA (object_type
, obj
, methodID
, args
);
485 jbyte
callJNIByteMethodIDA ( jobject obj
, jmethodID methodID
, jvalue
*args
)
487 jvalue result
= callJNIMethodIDA (byte_type
, obj
, methodID
, args
);
491 jchar
callJNICharMethodIDA (jobject obj
, jmethodID methodID
, jvalue
*args
)
493 jvalue result
= callJNIMethodIDA (char_type
, obj
, methodID
, args
);
497 jshort
callJNIShortMethodIDA (jobject obj
, jmethodID methodID
, jvalue
*args
)
499 jvalue result
= callJNIMethodIDA (short_type
, obj
, methodID
, args
);
503 jint
callJNIIntMethodIDA (jobject obj
, jmethodID methodID
, jvalue
*args
)
505 jvalue result
= callJNIMethodIDA (int_type
, obj
, methodID
, args
);
509 jlong
callJNILongMethodIDA (jobject obj
, jmethodID methodID
, jvalue
*args
)
511 jvalue result
= callJNIMethodIDA (long_type
, obj
, methodID
, args
);
515 jfloat
callJNIFloatMethodIDA (jobject obj
, jmethodID methodID
, jvalue
*args
)
517 jvalue result
= callJNIMethodIDA (float_type
, obj
, methodID
, args
);
521 jdouble
callJNIDoubleMethodIDA (jobject obj
, jmethodID methodID
, jvalue
*args
)
523 jvalue result
= callJNIMethodIDA (double_type
, obj
, methodID
, args
);
527 jboolean
callJNIBooleanMethodIDA (jobject obj
, jmethodID methodID
, jvalue
*args
)
529 jvalue result
= callJNIMethodIDA (boolean_type
, obj
, methodID
, args
);
533 const char *getCharactersFromJString (jstring aJString
)
535 return getCharactersFromJStringInEnv (getJNIEnv(), aJString
);
538 void releaseCharactersForJString (jstring aJString
, const char *s
)
540 releaseCharactersForJStringInEnv (getJNIEnv(), aJString
, s
);
543 const char *getCharactersFromJStringInEnv (JNIEnv
*env
, jstring aJString
)
546 const char *s
= env
->GetStringUTFChars((jstring
)aJString
, &isCopy
);
548 env
->ExceptionDescribe();
549 env
->ExceptionClear();
550 fprintf (stderr
, "\n");
555 void releaseCharactersForJStringInEnv (JNIEnv
*env
, jstring aJString
, const char *s
)
557 env
->ReleaseStringUTFChars (aJString
, s
);
560 const jchar
*getUCharactersFromJStringInEnv (JNIEnv
*env
, jstring aJString
)
563 const jchar
*s
= env
->GetStringChars((jstring
)aJString
, &isCopy
);
565 env
->ExceptionDescribe();
566 env
->ExceptionClear();
567 fprintf (stderr
, "\n");
572 void releaseUCharactersForJStringInEnv (JNIEnv
*env
, jstring aJString
, const jchar
*s
)
574 env
->ReleaseStringChars (aJString
, s
);
577 JNIType
JNITypeFromClassName(const char *name
)
581 if (strcmp("byte",name
) == 0)
583 else if (strcmp("short",name
) == 0)
585 else if (strcmp("int",name
) == 0)
587 else if (strcmp("long",name
) == 0)
589 else if (strcmp("float",name
) == 0)
591 else if (strcmp("double",name
) == 0)
593 else if (strcmp("char",name
) == 0)
595 else if (strcmp("boolean",name
) == 0)
597 else if (strcmp("void",name
) == 0)
599 else if ('[' == name
[0])
607 const char *signatureFromPrimitiveType(JNIType type
)
650 JNIType
JNITypeFromPrimitiveType(char type
)
692 jvalue
getJNIField( jobject obj
, JNIType type
, const char *name
, const char *signature
)
694 JavaVM
*jvm
= getJavaVM();
695 JNIEnv
*env
= getJNIEnv();
698 bzero (&result
, sizeof(jvalue
));
699 if ( obj
!= NULL
&& jvm
!= NULL
&& env
!= NULL
) {
700 jclass cls
= env
->GetObjectClass(obj
);
702 jfieldID field
= env
->GetFieldID(cls
, name
, signature
);
703 if ( field
!= NULL
) {
707 result
.l
= env
->functions
->GetObjectField(env
, obj
, field
);
710 result
.z
= env
->functions
->GetBooleanField(env
, obj
, field
);
713 result
.b
= env
->functions
->GetByteField(env
, obj
, field
);
716 result
.c
= env
->functions
->GetCharField(env
, obj
, field
);
719 result
.s
= env
->functions
->GetShortField(env
, obj
, field
);
722 result
.i
= env
->functions
->GetIntField(env
, obj
, field
);
725 result
.j
= env
->functions
->GetLongField(env
, obj
, field
);
728 result
.f
= env
->functions
->GetFloatField(env
, obj
, field
);
731 result
.d
= env
->functions
->GetDoubleField(env
, obj
, field
);
734 fprintf(stderr
, "%s: invalid field type (%d)\n", __PRETTY_FUNCTION__
, (int)type
);
739 fprintf(stderr
, "%s: Could not find field: %s\n", __PRETTY_FUNCTION__
, name
);
740 env
->ExceptionDescribe();
741 env
->ExceptionClear();
742 fprintf (stderr
, "\n");
745 env
->DeleteLocalRef(cls
);
748 fprintf(stderr
, "%s: Could not find class for object\n", __PRETTY_FUNCTION__
);
755 static jobject
convertArrayInstanceToJavaArray(ExecState
*exec
, JSValue
*value
, const char *javaClassName
) {
757 ASSERT(JSLock::lockCount() > 0);
759 JNIEnv
*env
= getJNIEnv();
760 // As JS Arrays can contain a mixture of objects, assume we can convert to
761 // the requested Java Array type requested, unless the array type is some object array
762 // other than a string.
763 ArrayInstance
*jsArray
= static_cast<ArrayInstance
*>(value
);
764 unsigned length
= jsArray
->getLength();
765 jobjectArray jarray
= 0;
767 // Build the correct array type
768 switch (JNITypeFromPrimitiveType(javaClassName
[1])) {
770 // Only support string object types
771 if (0 == strcmp("[Ljava.lang.String;", javaClassName
)) {
772 jarray
= (jobjectArray
)env
->NewObjectArray(length
,
773 env
->FindClass("java/lang/String"),
774 env
->NewStringUTF(""));
775 for(unsigned i
= 0; i
< length
; i
++) {
776 JSValue
* item
= jsArray
->getItem(i
);
777 UString stringValue
= item
->toString(exec
);
778 env
->SetObjectArrayElement(jarray
,i
,
779 env
->functions
->NewString(env
, (const jchar
*)stringValue
.data(), stringValue
.size()));
786 jarray
= (jobjectArray
)env
->NewBooleanArray(length
);
787 for(unsigned i
= 0; i
< length
; i
++) {
788 JSValue
* item
= jsArray
->getItem(i
);
789 jboolean value
= (jboolean
)item
->toNumber(exec
);
790 env
->SetBooleanArrayRegion((jbooleanArray
)jarray
, (jsize
)i
, (jsize
)1, &value
);
796 jarray
= (jobjectArray
)env
->NewByteArray(length
);
797 for(unsigned i
= 0; i
< length
; i
++) {
798 JSValue
* item
= jsArray
->getItem(i
);
799 jbyte value
= (jbyte
)item
->toNumber(exec
);
800 env
->SetByteArrayRegion((jbyteArray
)jarray
, (jsize
)i
, (jsize
)1, &value
);
806 jarray
= (jobjectArray
)env
->NewCharArray(length
);
807 for(unsigned i
= 0; i
< length
; i
++) {
808 JSValue
* item
= jsArray
->getItem(i
);
809 UString stringValue
= item
->toString(exec
);
811 if (stringValue
.size() > 0)
812 value
= ((const jchar
*)stringValue
.data())[0];
813 env
->SetCharArrayRegion((jcharArray
)jarray
, (jsize
)i
, (jsize
)1, &value
);
819 jarray
= (jobjectArray
)env
->NewShortArray(length
);
820 for(unsigned i
= 0; i
< length
; i
++) {
821 JSValue
* item
= jsArray
->getItem(i
);
822 jshort value
= (jshort
)item
->toNumber(exec
);
823 env
->SetShortArrayRegion((jshortArray
)jarray
, (jsize
)i
, (jsize
)1, &value
);
829 jarray
= (jobjectArray
)env
->NewIntArray(length
);
830 for(unsigned i
= 0; i
< length
; i
++) {
831 JSValue
* item
= jsArray
->getItem(i
);
832 jint value
= (jint
)item
->toNumber(exec
);
833 env
->SetIntArrayRegion((jintArray
)jarray
, (jsize
)i
, (jsize
)1, &value
);
839 jarray
= (jobjectArray
)env
->NewLongArray(length
);
840 for(unsigned i
= 0; i
< length
; i
++) {
841 JSValue
* item
= jsArray
->getItem(i
);
842 jlong value
= (jlong
)item
->toNumber(exec
);
843 env
->SetLongArrayRegion((jlongArray
)jarray
, (jsize
)i
, (jsize
)1, &value
);
849 jarray
= (jobjectArray
)env
->NewFloatArray(length
);
850 for(unsigned i
= 0; i
< length
; i
++) {
851 JSValue
* item
= jsArray
->getItem(i
);
852 jfloat value
= (jfloat
)item
->toNumber(exec
);
853 env
->SetFloatArrayRegion((jfloatArray
)jarray
, (jsize
)i
, (jsize
)1, &value
);
859 jarray
= (jobjectArray
)env
->NewDoubleArray(length
);
860 for(unsigned i
= 0; i
< length
; i
++) {
861 JSValue
* item
= jsArray
->getItem(i
);
862 jdouble value
= (jdouble
)item
->toNumber(exec
);
863 env
->SetDoubleArrayRegion((jdoubleArray
)jarray
, (jsize
)i
, (jsize
)1, &value
);
868 case array_type
: // don't handle embedded arrays
869 case void_type
: // Don't expect arrays of void objects
870 case invalid_type
: // Array of unknown objects
874 // if it was not one of the cases handled, then null is returned
879 jvalue
convertValueToJValue (ExecState
*exec
, JSValue
*value
, JNIType _JNIType
, const char *javaClassName
)
888 result
.l
= (jobject
)0;
890 // First see if we have a Java instance.
891 if (value
->isObject()){
892 JSObject
*objectImp
= static_cast<JSObject
*>(value
);
893 if (objectImp
->classInfo() == &RuntimeObjectImp::info
) {
894 RuntimeObjectImp
*imp
= static_cast<RuntimeObjectImp
*>(value
);
895 JavaInstance
*instance
= static_cast<JavaInstance
*>(imp
->getInternalInstance());
897 result
.l
= instance
->javaInstance();
899 else if (objectImp
->classInfo() == &RuntimeArray::info
) {
900 // Input is a JavaScript Array that was originally created from a Java Array
901 RuntimeArray
*imp
= static_cast<RuntimeArray
*>(value
);
902 JavaArray
*array
= static_cast<JavaArray
*>(imp
->getConcreteArray());
903 result
.l
= array
->javaArray();
905 else if (objectImp
->classInfo() == &ArrayInstance::info
) {
906 // Input is a Javascript Array. We need to create it to a Java Array.
907 result
.l
= convertArrayInstanceToJavaArray(exec
, value
, javaClassName
);
911 // Now convert value to a string if the target type is a java.lang.string, and we're not
912 // converting from a Null.
913 if (result
.l
== 0 && strcmp(javaClassName
, "java.lang.String") == 0) {
914 #ifdef CONVERT_NULL_TO_EMPTY_STRING
915 if (value
->isNull()) {
916 JNIEnv
*env
= getJNIEnv();
918 jobject javaString
= env
->functions
->NewString (env
, buf
, 0);
919 result
.l
= javaString
;
923 if (!value
->isNull())
926 UString stringValue
= value
->toString(exec
);
927 JNIEnv
*env
= getJNIEnv();
928 jobject javaString
= env
->functions
->NewString (env
, (const jchar
*)stringValue
.data(), stringValue
.size());
929 result
.l
= javaString
;
931 } else if (result
.l
== 0)
932 bzero (&result
, sizeof(jvalue
)); // Handle it the same as a void case
937 result
.z
= (jboolean
)value
->toNumber(exec
);
942 result
.b
= (jbyte
)value
->toNumber(exec
);
947 result
.c
= (jchar
)value
->toNumber(exec
);
952 result
.s
= (jshort
)value
->toNumber(exec
);
957 result
.i
= (jint
)value
->toNumber(exec
);
962 result
.j
= (jlong
)value
->toNumber(exec
);
967 result
.f
= (jfloat
)value
->toNumber(exec
);
972 result
.d
= (jdouble
)value
->toNumber(exec
);
981 bzero (&result
, sizeof(jvalue
));
988 } // end of namespace Bindings
990 } // end of namespace KJS
992 #endif // ENABLE(JAVA_BINDINGS)