set executable bit on promote-build.sh (r=cpeyer)
[tamarin-stm.git] / extensions / JavaGlue.h
blob815589b5d06e59bf8cf4f114249b287918ac67d3
1 /* -*- Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*- */
2 /* vi: set ts=4 sw=4 expandtab: (add to ~/.vimrc: set modeline modelines=5) */
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
14 * License.
16 * The Original Code is [Open Source Virtual Machine.].
18 * The Initial Developer of the Original Code is
19 * Adobe System Incorporated.
20 * Portions created by the Initial Developer are Copyright (C) 1993-2006
21 * the Initial Developer. All Rights Reserved.
23 * Contributor(s):
24 * Adobe AS3 Team
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * 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 #ifndef _JAVA_GLUE_H_
42 #define _JAVA_GLUE_H_
44 #ifdef AVMPLUS_WITH_JNI
46 #define NO_JNI_STDIO
47 #include <javaVM/jni.h>
49 /**
50 * Pointers to the routines that we will be calling. We bind
51 * dynamically since we don't want to get a can't find dll error
52 * if we failed to have a vm on this machine.
54 typedef
55 _JNI_IMPORT_OR_EXPORT_ jint (JNICALL
56 *_JNI_GetDefaultJavaVMInitArgs) (void *args);
58 typedef
59 _JNI_IMPORT_OR_EXPORT_ jint (JNICALL
60 *_JNI_CreateJavaVM) (JavaVM **pvm, void **penv, void *args);
62 #define JNI_NO_LIB (-8) /* shared library not found */
63 #define JNI_BAD_VER (-9) /* vm version is less than what is required */
64 #define JNI_VM_FAIL (-10) /* vm creation failure */
65 #define JNI_INIT_FAIL (-11) /* our initialization did not occur correctly */
67 namespace avmplus
69 class JObject;
70 class JClass;
72 class JObjectClass: public ClassClosure
74 public:
75 static JObjectClass* cc; //@todo hack to remove
77 JObjectClass(VTable *cvtable);
78 ScriptObject* createInstance(VTable *ivtable, ScriptObject *prototype);
80 /**
81 * Implementations of AS JObject.xxx() methods
83 JObject* create(String* name, Atom* argv, int argc);
84 JObject* createArray(JObject* arrayType, int size, ArrayObject* arr);
85 ArrayObject* toArray(JObject* jobj);
86 String* constructorSignature(String* name, Atom* argv, int argc);
87 String* methodSignature(JObject* jobj, String* name, Atom* argv, int argc);
88 String* fieldSignature(JObject* jobj, String* name);
90 // JObject that has a java class tied to it but no java instance object
91 JObject* createJObjectShell(JClass* clazz);
93 // helper method for finding the JClass associated with a particular java class(array)
94 JClass* forName(String* name);
95 JClass* forType(jstring type);
97 static void throwException(Java* j, Toplevel* top, int errorId, jthrowable jthrow, String* arg1=0, String* arg2=0);
99 private:
100 Java* jvm();
102 DECLARE_SLOTS_JObjectClass;
105 class JObject : public ScriptObject
107 public:
108 JObject(VTable *vtable, ScriptObject *proto);
109 ~JObject();
111 ScriptObject* createInstance(VTable *ivtable, ScriptObject *prototype);
113 Atom getAtomProperty(Atom name) const;
114 bool hasMultinameProperty(const Multiname* name) const;
115 Atom callProperty(const Multiname* name, int argc, Atom* argv);
116 void setMultinameProperty(const Multiname* name, Atom value);
118 // internal
119 void setClass(JClass* clazz) { this->jclass = clazz; }
120 void setObject(jobject obj) { this->obj = obj; }
121 bool indexFrom(AvmCore* core, Atom a, int* index) const;
123 JClass* getClass() { return jclass; }
124 jobject getObject() { return obj; }
126 String* javaObjectToString() const;
128 // AS exposed functions
130 private:
131 jobject obj; /* underlying java object */
132 DWB(JClass*) jclass; /* class of this object */
134 DECLARE_SLOTS_JObject;
137 // non-AS exposed wrapper around a jvm Class object
138 class JClass : public MMgc::GCObject
140 public:
141 JClass(Java* vm, String* name, jclass cref);
143 jobject callConstructor(JObjectClass* jobj, int argc, Atom* argv);
144 jarray createArray(JObjectClass* jobj, int size, ArrayObject* arr);
145 Atom callMethod(JObject* jobj, String* name, int argc, Atom* argv);
146 Atom getField(JObject* jobj, String* name);
147 bool setField(JObject* jobj, String* name, Atom val);
148 bool hasField(JObject* jobj, String* nm);
149 Atom getArrayElement(JObject* jobj, int index);
150 void setArrayElement(JObject* jobj, int index, Atom val);
151 const char* boxArgs(AvmCore* core, Toplevel* toplevel, const char* descriptor, int argc, Atom* argv, jvalue* jargs);
153 jobject methodFor(String* name, const char* argvDesc, int argc, char* methodDesc);
154 jobject constructorFor(const char* argvDesc, int argc, char* constrDesc);
155 jobject fieldFor(String* name);
156 int methodDescriptor(char* desc, jobject method);
157 int constructorDescriptor(char* desc, jobject constr);
158 int argsDescriptor(AvmCore* core, char* desc, int argc, Atom* argv);
159 char* descriptorVerbose(char* desc, jclass cls);
160 char descriptorChar(jclass cls);
161 bool stringsEqual(jstring s1, String* s2);
162 bool isStatic(jint modifiers);
164 static const char* compareDescriptors(const char* base, const char* desc1, const char* desc2);
165 static int argumentCount(const char* d);
166 static int conversionCost(const char* from, const char* to);
168 // conversions methods
169 const char* jvalueToAtom(AvmCore* core, Toplevel* toplevel, jvalue& val, const char* type, Atom& a);
170 const char* atomTojvalue(AvmCore* core, Toplevel* toplevel, Atom a, const char* type, jvalue& val);
171 void atomTojlong(AvmCore* core, Atom a, jlong& val);
173 static bool isJObject(AvmCore* core, Atom a);
174 static String* jlongToString(AvmCore* core, jlong& val);
176 Java* jvm() { return vm; }
177 String* name() { return nm; }
178 jclass classRef() { return cref; }
180 private:
181 DWB(Java*) vm;
182 DRCWB(String*) nm; /* name used for class lookup */
183 jclass cref; /* class */
186 // auto class determination
187 #define BEGIN_DEFINE_JCLASS() static const int _classRef_0_= __LINE__ + 1
188 #define DEFINE_JCLASS(c) static const int _classRef_##c = __LINE__ - _classRef_0_; jclass c() const { return classRefTable[_classRef_##c]; }
189 #define END_DEFINE_JCLASS() jclass classRefTable[__LINE__-_classRef_0_]
190 #define INIT_JCLASS(c) classRefTable[_classRef_##c] = jni->FindClass( replace(dst, #c) );
192 // instances of Class objects
193 #define BEGIN_DEFINE_PRIMITIVE_TYPE() static const int _primitiveRef_0_= __LINE__ + 1
194 #define DEFINE_PRIMITIVE_TYPE(c) static const int _primitiveRef_##c = __LINE__ - _primitiveRef_0_; jclass c##_class() const { return primitiveRefTable[_primitiveRef_##c]; }
195 #define END_DEFINE_PRIMITIVE_TYPE() jclass primitiveRefTable[__LINE__-_primitiveRef_0_]
196 #define INIT_PRIMITIVE_TYPE(c) primitiveRefTable[_primitiveRef_##c] = (jclass)jni->GetStaticObjectField( c(), jni->GetStaticFieldID( c(), "TYPE", "Ljava/lang/Class;") );
198 // auto method id determination
199 #define BEGIN_DEFINE_JMETHODID() static const int _methodId_0_= __LINE__ + 1
200 #define DEFINE_JMETHODID(c, m, s) static const int _methodId_##c##m = __LINE__ - _methodId_0_; jmethodID c##_##m() const { return methodIdTable[_methodId_##c##m]; }
201 #define END_DEFINE_JMETHODID() jmethodID methodIdTable[__LINE__-_methodId_0_]
202 #define INIT_JMETHODID(c, m, s) methodIdTable[_methodId_##c##m] = jni->GetMethodID( c(), #m, s);
203 #define INIT_SJMETHODID(c, m, s) methodIdTable[_methodId_##c##m] = jni->GetStaticMethodID( c(), #m, s);
204 #define DEFINE_SJMETHODID DEFINE_JMETHODID
207 * The container class for a Java Vm
209 class Java
211 public:
212 Java();
213 ~Java();
215 String* newString(AvmCore* core, jstring jstr);
217 static char* startup_options; /* set by the shell */
219 /* returns jni error code or JNI_OK */
220 int startupJVM(AvmCore* core);
222 JavaVM* jvm; /* denotes a Java VM */
223 JNIEnv* jni; /* pointer to native method interface */
224 bool bound;
226 _JNI_GetDefaultJavaVMInitArgs JNI_GetDefaultJavaVMInitArgs_;
227 _JNI_CreateJavaVM JNI_CreateJavaVM_;
229 bool bindLibrary(AvmCore* core);
231 // java.lang.reflect.Modifier consts
232 int modifier_STATIC;
233 int modifier_FINAL;
235 BEGIN_DEFINE_JCLASS();
236 DEFINE_JCLASS(java_lang_Byte);
237 DEFINE_JCLASS(java_lang_Boolean);
238 DEFINE_JCLASS(java_lang_Class);
239 DEFINE_JCLASS(java_lang_Character);
240 DEFINE_JCLASS(java_lang_Double);
241 DEFINE_JCLASS(java_lang_Float);
242 DEFINE_JCLASS(java_lang_Integer);
243 DEFINE_JCLASS(java_lang_Long);
244 DEFINE_JCLASS(java_lang_Object);
245 DEFINE_JCLASS(java_lang_Short);
246 DEFINE_JCLASS(java_lang_String);
247 DEFINE_JCLASS(java_lang_Void);
248 DEFINE_JCLASS(java_lang_reflect_Constructor);
249 DEFINE_JCLASS(java_lang_reflect_Field);
250 DEFINE_JCLASS(java_lang_reflect_Method);
251 DEFINE_JCLASS(java_lang_reflect_Modifier);
252 END_DEFINE_JCLASS();
254 BEGIN_DEFINE_JMETHODID();
255 DEFINE_JMETHODID(java_lang_Object, equals, "(Ljava/lang/Object;)Z");
256 DEFINE_JMETHODID(java_lang_Object, toString, "()Ljava/lang/String;");
257 DEFINE_JMETHODID(java_lang_Object, getClass, "()Ljava/lang/Class;");
258 DEFINE_JMETHODID(java_lang_Class, getName, "()Ljava/lang/String;");
259 DEFINE_JMETHODID(java_lang_Class, getCanonicalName, "()Ljava/lang/String;");
260 DEFINE_JMETHODID(java_lang_Class, getMethods, "()[Ljava/lang/reflect/Method;");
261 DEFINE_JMETHODID(java_lang_Class, getFields, "()[Ljava/lang/reflect/Field;");
262 DEFINE_JMETHODID(java_lang_Class, getField, "(Ljava/lang/String;)Ljava/lang/reflect/Field;");
263 DEFINE_JMETHODID(java_lang_Class, getConstructors, "()[Ljava/lang/reflect/Constructors;");
264 DEFINE_JMETHODID(java_lang_Class, isPrimitive, "()Z");
265 DEFINE_JMETHODID(java_lang_Class, isArray, "()Z");
266 DEFINE_SJMETHODID(java_lang_Class, forName, "(Ljava/lang/String;)Ljava/lang/Class;");
267 DEFINE_JMETHODID(java_lang_reflect_Method, getName, "()Ljava/lang/String;");
268 DEFINE_JMETHODID(java_lang_reflect_Method, getReturnType, "()Ljava/lang/Class;");
269 DEFINE_JMETHODID(java_lang_reflect_Method, getParameterTypes, "()[Ljava/lang/Class;");
270 DEFINE_JMETHODID(java_lang_reflect_Method, getModifiers, "()I");
271 DEFINE_JMETHODID(java_lang_reflect_Field, getName, "()Ljava/lang/String;");
272 DEFINE_JMETHODID(java_lang_reflect_Field, getType, "()Ljava/lang/Class;");
273 DEFINE_JMETHODID(java_lang_reflect_Field, getModifiers, "()I");
274 DEFINE_JMETHODID(java_lang_reflect_Constructor, getParameterTypes, "()[Ljava/lang/Class;");
275 END_DEFINE_JMETHODID();
277 BEGIN_DEFINE_PRIMITIVE_TYPE();
278 DEFINE_PRIMITIVE_TYPE(java_lang_Byte);
279 DEFINE_PRIMITIVE_TYPE(java_lang_Boolean);
280 DEFINE_PRIMITIVE_TYPE(java_lang_Class);
281 DEFINE_PRIMITIVE_TYPE(java_lang_Character);
282 DEFINE_PRIMITIVE_TYPE(java_lang_Double);
283 DEFINE_PRIMITIVE_TYPE(java_lang_Float);
284 DEFINE_PRIMITIVE_TYPE(java_lang_Integer);
285 DEFINE_PRIMITIVE_TYPE(java_lang_Long);
286 DEFINE_PRIMITIVE_TYPE(java_lang_Object);
287 DEFINE_PRIMITIVE_TYPE(java_lang_Short);
288 DEFINE_PRIMITIVE_TYPE(java_lang_Void);
289 END_DEFINE_PRIMITIVE_TYPE();
291 static char* replace(char* dst, const char* src, char what='_', char with='/');
295 #else /* !AVMPLUS_WITH_JNI */
296 namespace avmplus
298 class JObject;
299 class JClass;
301 class JObjectClass: public ClassClosure
303 public:
304 JObjectClass(VTable *cvtable) : ClassClosure(cvtable) { }
305 ArrayObject* toArray(JObject* ) { return 0; }
306 String* fieldSignature(JObject* , String* ) { return 0; }
307 String* methodSignature(JObject* , String* , Atom* , int ) { return 0; }
308 JObject* create(String* , Atom* , int ) { return 0; }
309 JObject* createArray(JObject* , int , ArrayObject* ) { return 0; }
310 String* constructorSignature(String* , Atom* , int ) { return 0; }
312 DECLARE_SLOTS_JObjectClass;
315 class JObject : public ScriptObject
317 public:
318 JObject(VTable *vtable, ScriptObject *proto) : ScriptObject(vtable, proto) { }
319 String* javaObjectToString() const { return 0; }
321 DECLARE_SLOTS_JObject;
324 #endif /* AVMPLUS_WITH_JNI */
326 #endif /* _JAVA_GLUE_H_ */