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