1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
16 * The Original Code is Mozilla Communicator client code, released
19 * The Initial Developer of the Original Code is
20 * Netscape Communications Corporation.
21 * Portions created by the Initial Developer are Copyright (C) 1998
22 * the Initial Developer. All Rights Reserved.
26 * Alternatively, the contents of this file may be used under the terms of
27 * either of the GNU General Public License Version 2 or later (the "GPL"),
28 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
41 * This file is part of the Java-vendor-neutral implementation of LiveConnect
43 * It contains the code for reading and writing elements of a Java array.
46 #include "jsj_private.h" /* LiveConnect internals */
49 * Read the Java value at a given index into a Java array and convert it
50 * to a JS value. The array_component_signature describes the type of
51 * the resulting Java value, which can be a primitive type or an object type.
52 * More specifically it can be an array type in the case of multidimensional
56 jsj_GetJavaArrayElement(JSContext
*cx
, JNIEnv
*jEnv
, jarray java_array
, jsize index
,
57 JavaSignature
*array_component_signature
,
61 JavaSignatureChar component_type
;
64 #define GET_ELEMENT_FROM_PRIMITIVE_JAVA_ARRAY(Type,member) \
65 (*jEnv)->Get##Type##ArrayRegion(jEnv, java_array, index, 1, \
66 &java_value.member); \
67 if ((*jEnv)->ExceptionOccurred(jEnv)) { \
68 jsj_ReportJavaError(cx, jEnv, "Error reading element of " \
69 "Java primitive array"); \
73 component_type
= array_component_signature
->type
;
74 switch(component_type
) {
75 case JAVA_SIGNATURE_BYTE
:
76 GET_ELEMENT_FROM_PRIMITIVE_JAVA_ARRAY(Byte
,b
);
79 case JAVA_SIGNATURE_CHAR
:
80 GET_ELEMENT_FROM_PRIMITIVE_JAVA_ARRAY(Char
,c
);
83 case JAVA_SIGNATURE_SHORT
:
84 GET_ELEMENT_FROM_PRIMITIVE_JAVA_ARRAY(Short
,s
);
87 case JAVA_SIGNATURE_INT
:
88 GET_ELEMENT_FROM_PRIMITIVE_JAVA_ARRAY(Int
,i
);
91 case JAVA_SIGNATURE_BOOLEAN
:
92 GET_ELEMENT_FROM_PRIMITIVE_JAVA_ARRAY(Boolean
,z
);
95 case JAVA_SIGNATURE_LONG
:
96 GET_ELEMENT_FROM_PRIMITIVE_JAVA_ARRAY(Long
,j
);
99 case JAVA_SIGNATURE_FLOAT
:
100 GET_ELEMENT_FROM_PRIMITIVE_JAVA_ARRAY(Float
,f
);
103 case JAVA_SIGNATURE_DOUBLE
:
104 GET_ELEMENT_FROM_PRIMITIVE_JAVA_ARRAY(Double
,d
);
107 /* Non-primitive (reference) type */
109 JS_ASSERT(component_type
>= JAVA_SIGNATURE_ARRAY
);
110 java_value
.l
= (*jEnv
)->GetObjectArrayElement(jEnv
, java_array
, index
);
111 if ((*jEnv
)->ExceptionOccurred(jEnv
)) {
112 jsj_ReportJavaError(cx
, jEnv
, "Error reading Java object array");
115 success
= jsj_ConvertJavaObjectToJSValue(cx
, jEnv
, java_value
.l
, vp
);
116 (*jEnv
)->DeleteLocalRef(jEnv
, java_value
.l
);
119 #undef GET_ELEMENT_FROM_PRIMITIVE_JAVA_ARRAY
120 case JAVA_SIGNATURE_UNKNOWN
:
121 case JAVA_SIGNATURE_VOID
:
122 JS_ASSERT(0); /* Unknown java type signature */
126 return jsj_ConvertJavaValueToJSValue(cx
, jEnv
, array_component_signature
, &java_value
, vp
);
130 jsj_SetJavaArrayElement(JSContext
*cx
, JNIEnv
*jEnv
, jarray java_array
, jsize index
,
131 JavaSignature
*array_component_signature
,
136 JavaSignatureChar component_type
;
139 if (!jsj_ConvertJSValueToJavaValue(cx
, jEnv
, js_val
, array_component_signature
,
140 &dummy_cost
, &java_value
, &is_local_ref
))
143 #define SET_ELEMENT_FROM_PRIMITIVE_JAVA_ARRAY(Type,member) \
144 (*jEnv)->Set##Type##ArrayRegion(jEnv, java_array, index, 1, \
145 &java_value.member); \
146 if ((*jEnv)->ExceptionOccurred(jEnv)) { \
147 jsj_ReportJavaError(cx, jEnv, "Error assigning to element of " \
148 "Java primitive array"); \
152 component_type
= array_component_signature
->type
;
153 switch(component_type
) {
154 case JAVA_SIGNATURE_BYTE
:
155 SET_ELEMENT_FROM_PRIMITIVE_JAVA_ARRAY(Byte
,b
);
158 case JAVA_SIGNATURE_CHAR
:
159 SET_ELEMENT_FROM_PRIMITIVE_JAVA_ARRAY(Char
,c
);
162 case JAVA_SIGNATURE_SHORT
:
163 SET_ELEMENT_FROM_PRIMITIVE_JAVA_ARRAY(Short
,s
);
166 case JAVA_SIGNATURE_INT
:
167 SET_ELEMENT_FROM_PRIMITIVE_JAVA_ARRAY(Int
,i
);
170 case JAVA_SIGNATURE_BOOLEAN
:
171 SET_ELEMENT_FROM_PRIMITIVE_JAVA_ARRAY(Boolean
,z
);
174 case JAVA_SIGNATURE_LONG
:
175 SET_ELEMENT_FROM_PRIMITIVE_JAVA_ARRAY(Long
,j
);
178 case JAVA_SIGNATURE_FLOAT
:
179 SET_ELEMENT_FROM_PRIMITIVE_JAVA_ARRAY(Float
,f
);
182 case JAVA_SIGNATURE_DOUBLE
:
183 SET_ELEMENT_FROM_PRIMITIVE_JAVA_ARRAY(Double
,d
);
186 /* Non-primitive (reference) type */
188 JS_ASSERT(IS_REFERENCE_TYPE(component_type
));
189 (*jEnv
)->SetObjectArrayElement(jEnv
, java_array
, index
, java_value
.l
);
191 (*jEnv
)->DeleteLocalRef(jEnv
, java_value
.l
);
192 if ((*jEnv
)->ExceptionOccurred(jEnv
)) {
193 jsj_ReportJavaError(cx
, jEnv
, "Error assigning to Java object array");
198 #undef SET_ELEMENT_FROM_PRIMITIVE_JAVA_ARRAY
199 case JAVA_SIGNATURE_UNKNOWN
:
200 case JAVA_SIGNATURE_VOID
:
201 JS_ASSERT(0); /* Unknown java type signature */