Back to index

lightning-sunbird  0.9+nobinonly
jsj_array.c
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is Mozilla Communicator client code, released
00017  * March 31, 1998.
00018  *
00019  * The Initial Developer of the Original Code is
00020  * Netscape Communications Corporation.
00021  * Portions created by the Initial Developer are Copyright (C) 1998
00022  * the Initial Developer. All Rights Reserved.
00023  *
00024  * Contributor(s):
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039  
00040 /*
00041  * This file is part of the Java-vendor-neutral implementation of LiveConnect
00042  *
00043  * It contains the code for reading and writing elements of a Java array.
00044  */
00045 
00046 #include "jsj_private.h"      /* LiveConnect internals */
00047 
00048 /*
00049  * Read the Java value at a given index into a Java array and convert it
00050  * to a JS value.  The array_component_signature describes the type of
00051  * the resulting Java value, which can be a primitive type or an object type.
00052  * More specifically it can be an array type in the case of multidimensional
00053  * arrays.
00054  */
00055 JSBool
00056 jsj_GetJavaArrayElement(JSContext *cx, JNIEnv *jEnv, jarray java_array, jsize index,
00057                         JavaSignature *array_component_signature,
00058                         jsval *vp)
00059 {
00060     jvalue java_value;
00061     JavaSignatureChar component_type;
00062     JSBool success;
00063 
00064 #define GET_ELEMENT_FROM_PRIMITIVE_JAVA_ARRAY(Type,member)                   \
00065     (*jEnv)->Get##Type##ArrayRegion(jEnv, java_array, index, 1,              \
00066                                     &java_value.member);                     \
00067     if ((*jEnv)->ExceptionOccurred(jEnv)) {                                  \
00068         jsj_ReportJavaError(cx, jEnv, "Error reading element of "            \
00069                                       "Java primitive array");               \
00070         return JS_FALSE;                                                     \
00071     }
00072 
00073     component_type = array_component_signature->type;
00074     switch(component_type) {
00075     case JAVA_SIGNATURE_BYTE:
00076         GET_ELEMENT_FROM_PRIMITIVE_JAVA_ARRAY(Byte,b);
00077         break;
00078 
00079     case JAVA_SIGNATURE_CHAR:
00080         GET_ELEMENT_FROM_PRIMITIVE_JAVA_ARRAY(Char,c);
00081         break;
00082 
00083     case JAVA_SIGNATURE_SHORT:
00084         GET_ELEMENT_FROM_PRIMITIVE_JAVA_ARRAY(Short,s);
00085         break;
00086 
00087     case JAVA_SIGNATURE_INT:
00088         GET_ELEMENT_FROM_PRIMITIVE_JAVA_ARRAY(Int,i);
00089         break;
00090 
00091     case JAVA_SIGNATURE_BOOLEAN:
00092         GET_ELEMENT_FROM_PRIMITIVE_JAVA_ARRAY(Boolean,z);
00093         break;
00094 
00095     case JAVA_SIGNATURE_LONG:
00096         GET_ELEMENT_FROM_PRIMITIVE_JAVA_ARRAY(Long,j);
00097         break;
00098   
00099     case JAVA_SIGNATURE_FLOAT:
00100         GET_ELEMENT_FROM_PRIMITIVE_JAVA_ARRAY(Float,f);
00101         break;
00102 
00103     case JAVA_SIGNATURE_DOUBLE:
00104         GET_ELEMENT_FROM_PRIMITIVE_JAVA_ARRAY(Double,d);
00105         break;
00106 
00107     /* Non-primitive (reference) type */
00108     default:
00109         JS_ASSERT(component_type >= JAVA_SIGNATURE_ARRAY);
00110         java_value.l = (*jEnv)->GetObjectArrayElement(jEnv, java_array, index);
00111         if ((*jEnv)->ExceptionOccurred(jEnv)) {
00112             jsj_ReportJavaError(cx, jEnv, "Error reading Java object array");
00113             return JS_FALSE;
00114         }
00115         success = jsj_ConvertJavaObjectToJSValue(cx, jEnv, java_value.l, vp);
00116         (*jEnv)->DeleteLocalRef(jEnv, java_value.l);
00117         return success;
00118 
00119 #undef GET_ELEMENT_FROM_PRIMITIVE_JAVA_ARRAY
00120     case JAVA_SIGNATURE_UNKNOWN:
00121     case JAVA_SIGNATURE_VOID:
00122         JS_ASSERT(0);        /* Unknown java type signature */
00123         return JS_FALSE;
00124     }
00125 
00126     return jsj_ConvertJavaValueToJSValue(cx, jEnv, array_component_signature, &java_value, vp);
00127 }
00128 
00129 JSBool
00130 jsj_SetJavaArrayElement(JSContext *cx, JNIEnv *jEnv, jarray java_array, jsize index,
00131                         JavaSignature *array_component_signature,
00132                         jsval js_val)
00133 {
00134     int dummy_cost;
00135     jvalue java_value;
00136     JavaSignatureChar component_type;
00137     JSBool is_local_ref;
00138 
00139     if (!jsj_ConvertJSValueToJavaValue(cx, jEnv, js_val, array_component_signature,
00140                                        &dummy_cost, &java_value, &is_local_ref))
00141         return JS_FALSE;
00142 
00143 #define SET_ELEMENT_FROM_PRIMITIVE_JAVA_ARRAY(Type,member)                   \
00144     (*jEnv)->Set##Type##ArrayRegion(jEnv, java_array, index, 1,              \
00145                                     &java_value.member);                     \
00146     if ((*jEnv)->ExceptionOccurred(jEnv)) {                                  \
00147         jsj_ReportJavaError(cx, jEnv, "Error assigning to element of "       \
00148                                       "Java primitive array");               \
00149         return JS_FALSE;                                                     \
00150     }
00151 
00152     component_type = array_component_signature->type;
00153     switch(component_type) {
00154     case JAVA_SIGNATURE_BYTE:
00155         SET_ELEMENT_FROM_PRIMITIVE_JAVA_ARRAY(Byte,b);
00156         break;
00157 
00158     case JAVA_SIGNATURE_CHAR:
00159         SET_ELEMENT_FROM_PRIMITIVE_JAVA_ARRAY(Char,c);
00160         break;
00161 
00162     case JAVA_SIGNATURE_SHORT:
00163         SET_ELEMENT_FROM_PRIMITIVE_JAVA_ARRAY(Short,s);
00164         break;
00165 
00166     case JAVA_SIGNATURE_INT:
00167         SET_ELEMENT_FROM_PRIMITIVE_JAVA_ARRAY(Int,i);
00168         break;
00169 
00170     case JAVA_SIGNATURE_BOOLEAN:
00171         SET_ELEMENT_FROM_PRIMITIVE_JAVA_ARRAY(Boolean,z);
00172         break;
00173 
00174     case JAVA_SIGNATURE_LONG:
00175         SET_ELEMENT_FROM_PRIMITIVE_JAVA_ARRAY(Long,j);
00176         break;
00177   
00178     case JAVA_SIGNATURE_FLOAT:
00179         SET_ELEMENT_FROM_PRIMITIVE_JAVA_ARRAY(Float,f);
00180         break;
00181 
00182     case JAVA_SIGNATURE_DOUBLE:
00183         SET_ELEMENT_FROM_PRIMITIVE_JAVA_ARRAY(Double,d);
00184         break;
00185 
00186     /* Non-primitive (reference) type */
00187     default:
00188         JS_ASSERT(IS_REFERENCE_TYPE(component_type));
00189         (*jEnv)->SetObjectArrayElement(jEnv, java_array, index, java_value.l);
00190         if (is_local_ref)                                                           \
00191             (*jEnv)->DeleteLocalRef(jEnv, java_value.l);
00192         if ((*jEnv)->ExceptionOccurred(jEnv)) {
00193             jsj_ReportJavaError(cx, jEnv, "Error assigning to Java object array");
00194             return JS_FALSE;
00195         }
00196         break;
00197 
00198 #undef SET_ELEMENT_FROM_PRIMITIVE_JAVA_ARRAY
00199     case JAVA_SIGNATURE_UNKNOWN:
00200     case JAVA_SIGNATURE_VOID:
00201         JS_ASSERT(0);        /* Unknown java type signature */
00202         return JS_FALSE;
00203     }
00204 
00205     return JS_TRUE;
00206 }
00207