~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/js/src/liveconnect/jsj_array.c

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 
2
 *
 
3
 * ***** BEGIN LICENSE BLOCK *****
 
4
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 
5
 *
 
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/
 
10
 *
 
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
 
14
 * License.
 
15
 *
 
16
 * The Original Code is Mozilla Communicator client code, released
 
17
 * March 31, 1998.
 
18
 *
 
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.
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
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.
 
37
 *
 
38
 * ***** END LICENSE BLOCK ***** */
 
39
 
 
40
/*
 
41
 * This file is part of the Java-vendor-neutral implementation of LiveConnect
 
42
 *
 
43
 * It contains the code for reading and writing elements of a Java array.
 
44
 */
 
45
 
 
46
#include "jsj_private.h"      /* LiveConnect internals */
 
47
 
 
48
/*
 
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
 
53
 * arrays.
 
54
 */
 
55
JSBool
 
56
jsj_GetJavaArrayElement(JSContext *cx, JNIEnv *jEnv, jarray java_array, jsize index,
 
57
                        JavaSignature *array_component_signature,
 
58
                        jsval *vp)
 
59
{
 
60
    jvalue java_value;
 
61
    JavaSignatureChar component_type;
 
62
    JSBool success;
 
63
 
 
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");               \
 
70
        return JS_FALSE;                                                     \
 
71
    }
 
72
 
 
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);
 
77
        break;
 
78
 
 
79
    case JAVA_SIGNATURE_CHAR:
 
80
        GET_ELEMENT_FROM_PRIMITIVE_JAVA_ARRAY(Char,c);
 
81
        break;
 
82
 
 
83
    case JAVA_SIGNATURE_SHORT:
 
84
        GET_ELEMENT_FROM_PRIMITIVE_JAVA_ARRAY(Short,s);
 
85
        break;
 
86
 
 
87
    case JAVA_SIGNATURE_INT:
 
88
        GET_ELEMENT_FROM_PRIMITIVE_JAVA_ARRAY(Int,i);
 
89
        break;
 
90
 
 
91
    case JAVA_SIGNATURE_BOOLEAN:
 
92
        GET_ELEMENT_FROM_PRIMITIVE_JAVA_ARRAY(Boolean,z);
 
93
        break;
 
94
 
 
95
    case JAVA_SIGNATURE_LONG:
 
96
        GET_ELEMENT_FROM_PRIMITIVE_JAVA_ARRAY(Long,j);
 
97
        break;
 
98
  
 
99
    case JAVA_SIGNATURE_FLOAT:
 
100
        GET_ELEMENT_FROM_PRIMITIVE_JAVA_ARRAY(Float,f);
 
101
        break;
 
102
 
 
103
    case JAVA_SIGNATURE_DOUBLE:
 
104
        GET_ELEMENT_FROM_PRIMITIVE_JAVA_ARRAY(Double,d);
 
105
        break;
 
106
 
 
107
    /* Non-primitive (reference) type */
 
108
    default:
 
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");
 
113
            return JS_FALSE;
 
114
        }
 
115
        success = jsj_ConvertJavaObjectToJSValue(cx, jEnv, java_value.l, vp);
 
116
        (*jEnv)->DeleteLocalRef(jEnv, java_value.l);
 
117
        return success;
 
118
 
 
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 */
 
123
        return JS_FALSE;
 
124
    }
 
125
 
 
126
    return jsj_ConvertJavaValueToJSValue(cx, jEnv, array_component_signature, &java_value, vp);
 
127
}
 
128
 
 
129
JSBool
 
130
jsj_SetJavaArrayElement(JSContext *cx, JNIEnv *jEnv, jarray java_array, jsize index,
 
131
                        JavaSignature *array_component_signature,
 
132
                        jsval js_val)
 
133
{
 
134
    int dummy_cost;
 
135
    jvalue java_value;
 
136
    JavaSignatureChar component_type;
 
137
    JSBool is_local_ref;
 
138
 
 
139
    if (!jsj_ConvertJSValueToJavaValue(cx, jEnv, js_val, array_component_signature,
 
140
                                       &dummy_cost, &java_value, &is_local_ref))
 
141
        return JS_FALSE;
 
142
 
 
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");               \
 
149
        return JS_FALSE;                                                     \
 
150
    }
 
151
 
 
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);
 
156
        break;
 
157
 
 
158
    case JAVA_SIGNATURE_CHAR:
 
159
        SET_ELEMENT_FROM_PRIMITIVE_JAVA_ARRAY(Char,c);
 
160
        break;
 
161
 
 
162
    case JAVA_SIGNATURE_SHORT:
 
163
        SET_ELEMENT_FROM_PRIMITIVE_JAVA_ARRAY(Short,s);
 
164
        break;
 
165
 
 
166
    case JAVA_SIGNATURE_INT:
 
167
        SET_ELEMENT_FROM_PRIMITIVE_JAVA_ARRAY(Int,i);
 
168
        break;
 
169
 
 
170
    case JAVA_SIGNATURE_BOOLEAN:
 
171
        SET_ELEMENT_FROM_PRIMITIVE_JAVA_ARRAY(Boolean,z);
 
172
        break;
 
173
 
 
174
    case JAVA_SIGNATURE_LONG:
 
175
        SET_ELEMENT_FROM_PRIMITIVE_JAVA_ARRAY(Long,j);
 
176
        break;
 
177
  
 
178
    case JAVA_SIGNATURE_FLOAT:
 
179
        SET_ELEMENT_FROM_PRIMITIVE_JAVA_ARRAY(Float,f);
 
180
        break;
 
181
 
 
182
    case JAVA_SIGNATURE_DOUBLE:
 
183
        SET_ELEMENT_FROM_PRIMITIVE_JAVA_ARRAY(Double,d);
 
184
        break;
 
185
 
 
186
    /* Non-primitive (reference) type */
 
187
    default:
 
188
        JS_ASSERT(IS_REFERENCE_TYPE(component_type));
 
189
        (*jEnv)->SetObjectArrayElement(jEnv, java_array, index, java_value.l);
 
190
        if (is_local_ref)                                                           \
 
191
            (*jEnv)->DeleteLocalRef(jEnv, java_value.l);
 
192
        if ((*jEnv)->ExceptionOccurred(jEnv)) {
 
193
            jsj_ReportJavaError(cx, jEnv, "Error assigning to Java object array");
 
194
            return JS_FALSE;
 
195
        }
 
196
        break;
 
197
 
 
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 */
 
202
        return JS_FALSE;
 
203
    }
 
204
 
 
205
    return JS_TRUE;
 
206
}
 
207