• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2003 Apple Computer, Inc.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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.
24  */
25 
26 #ifndef _JNI_RUNTIME_H_
27 #define _JNI_RUNTIME_H_
28 
29 #if ENABLE(MAC_JAVA_BRIDGE)
30 
31 #include <jni_utility.h>
32 #include <jni_instance.h>
33 #include <runtime/JSLock.h>
34 
35 
36 namespace JSC
37 {
38 
39 namespace Bindings
40 {
41 
42 typedef const char* RuntimeType;
43 
44 class JavaString
45 {
46 public:
JavaString()47     JavaString()
48     {
49         JSLock lock(SilenceAssertionsOnly);
50         _rep = UString().rep();
51     }
52 
_commonInit(JNIEnv * e,jstring s)53     void _commonInit (JNIEnv *e, jstring s)
54     {
55         int _size = e->GetStringLength (s);
56         const jchar *uc = getUCharactersFromJStringInEnv (e, s);
57         {
58             JSLock lock(SilenceAssertionsOnly);
59             _rep = UString(reinterpret_cast<const UChar*>(uc), _size).rep();
60         }
61         releaseUCharactersForJStringInEnv (e, s, uc);
62     }
63 
JavaString(JNIEnv * e,jstring s)64     JavaString (JNIEnv *e, jstring s) {
65         _commonInit (e, s);
66     }
67 
JavaString(jstring s)68     JavaString (jstring s) {
69         _commonInit (getJNIEnv(), s);
70     }
71 
~JavaString()72     ~JavaString()
73     {
74         JSLock lock(SilenceAssertionsOnly);
75         _rep = 0;
76     }
77 
UTF8String()78     const char *UTF8String() const {
79         if (_utf8String.c_str() == 0) {
80             JSLock lock(SilenceAssertionsOnly);
81             _utf8String = UString(_rep).UTF8String();
82         }
83         return _utf8String.c_str();
84     }
uchars()85     const jchar *uchars() const { return (const jchar *)_rep->data(); }
length()86     int length() const { return _rep->size(); }
UString()87     operator UString() const { return UString(_rep); }
88 
89 private:
90     RefPtr<UString::Rep> _rep;
91     mutable CString _utf8String;
92 };
93 
94 class JavaParameter
95 {
96 public:
JavaParameter()97     JavaParameter () : _JNIType(invalid_type) {};
98     JavaParameter (JNIEnv *env, jstring type);
~JavaParameter()99     virtual ~JavaParameter() { }
100 
type()101     RuntimeType type() const { return _type.UTF8String(); }
getJNIType()102     JNIType getJNIType() const { return _JNIType; }
103 
104 private:
105     JavaString _type;
106     JNIType _JNIType;
107 };
108 
109 
110 class JavaField : public Field
111 {
112 public:
113     JavaField (JNIEnv *env, jobject aField);
114 
115     virtual JSValue valueFromInstance(ExecState *exec, const Instance *instance) const;
116     virtual void setValueToInstance(ExecState *exec, const Instance *instance, JSValue aValue) const;
117 
name()118     UString::Rep* name() const { return ((UString)_name).rep(); }
type()119     virtual RuntimeType type() const { return _type.UTF8String(); }
120 
getJNIType()121     JNIType getJNIType() const { return _JNIType; }
122 
123 private:
124     void dispatchSetValueToInstance(ExecState *exec, const JavaInstance *instance, jvalue javaValue, const char *name, const char *sig) const;
125     jvalue dispatchValueFromInstance(ExecState *exec, const JavaInstance *instance, const char *name, const char *sig, JNIType returnType) const;
126 
127     JavaString _name;
128     JavaString _type;
129     JNIType _JNIType;
130     RefPtr<JObjectWrapper> _field;
131 };
132 
133 
134 class JavaMethod : public Method
135 {
136 public:
137     JavaMethod(JNIEnv* env, jobject aMethod);
138     ~JavaMethod();
139 
name()140     UString::Rep* name() const { return ((UString)_name).rep(); }
returnType()141     RuntimeType returnType() const { return _returnType.UTF8String(); };
parameterAt(int i)142     JavaParameter* parameterAt(int i) const { return &_parameters[i]; };
numParameters()143     int numParameters() const { return _numParameters; };
144 
145     const char *signature() const;
146     JNIType JNIReturnType() const;
147 
148     jmethodID methodID (jobject obj) const;
149 
isStatic()150     bool isStatic() const { return _isStatic; }
151 
152 private:
153     JavaParameter* _parameters;
154     int _numParameters;
155     JavaString _name;
156     mutable char* _signature;
157     JavaString _returnType;
158     JNIType _JNIReturnType;
159     mutable jmethodID _methodID;
160     bool _isStatic;
161 };
162 
163 class JavaArray : public Array
164 {
165 public:
166     JavaArray(jobject array, const char* type, PassRefPtr<RootObject>);
167     virtual ~JavaArray();
168 
169     RootObject* rootObject() const;
170 
171     virtual void setValueAt(ExecState *exec, unsigned int index, JSValue aValue) const;
172     virtual JSValue valueAt(ExecState *exec, unsigned int index) const;
173     virtual unsigned int getLength() const;
174 
javaArray()175     jobject javaArray() const { return _array->_instance; }
176 
177     static JSValue convertJObjectToArray (ExecState* exec, jobject anObject, const char* type, PassRefPtr<RootObject>);
178 
179 private:
180     RefPtr<JObjectWrapper> _array;
181     unsigned int _length;
182     const char *_type;
183 };
184 
185 } // namespace Bindings
186 
187 } // namespace JSC
188 
189 #endif // ENABLE(MAC_JAVA_BRIDGE)
190 
191 #endif // _JNI_RUNTIME_H_
192