]> git.saurik.com Git - apple/javascriptcore.git/blob - bindings/jni/jni_jsobject.h
d559fa19d5ef4d5c2129c3ce7e4f4407ce80f7db
[apple/javascriptcore.git] / bindings / jni / jni_jsobject.h
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 JAVASCRIPTCORE_BINDINGS_JNI_JSOBJECT_H
27 #define JAVASCRIPTCORE_BINDINGS_JNI_JSOBJECT_H
28
29 #if ENABLE(JAVA_BINDINGS)
30
31 #if PLATFORM(MAC)
32 #include <CoreFoundation/CoreFoundation.h>
33 #endif
34
35 #include <JavaVM/jni.h>
36 #include <wtf/RefPtr.h>
37
38 #define jlong_to_ptr(a) ((void*)(uintptr_t)(a))
39 #define jlong_to_impptr(a) (static_cast<KJS::JSObject*>(((void*)(uintptr_t)(a))))
40 #define ptr_to_jlong(a) ((jlong)(uintptr_t)(a))
41
42 namespace KJS {
43
44 class List;
45 class JSObject;
46 class JSValue;
47
48 namespace Bindings {
49
50 class RootObject;
51
52 enum JSObjectCallType {
53 CreateNative,
54 Call,
55 Eval,
56 GetMember,
57 SetMember,
58 RemoveMember,
59 GetSlot,
60 SetSlot,
61 ToString,
62 Finalize
63 };
64
65 struct JSObjectCallContext
66 {
67 JSObjectCallType type;
68 jlong nativeHandle;
69 jstring string;
70 jobjectArray args;
71 jint index;
72 jobject value;
73 CFRunLoopRef originatingLoop;
74 jvalue result;
75 };
76
77 class JavaJSObject
78 {
79 public:
80 JavaJSObject(jlong nativeHandle);
81
82 static jlong createNative(jlong nativeHandle);
83 jobject call(jstring methodName, jobjectArray args) const;
84 jobject eval(jstring script) const;
85 jobject getMember(jstring memberName) const;
86 void setMember(jstring memberName, jobject value) const;
87 void removeMember(jstring memberName) const;
88 jobject getSlot(jint index) const;
89 void setSlot(jint index, jobject value) const;
90 jstring toString() const;
91 void finalize() const;
92
93 static jvalue invoke(JSObjectCallContext*);
94
95 jobject convertValueToJObject(JSValue*) const;
96 JSValue* convertJObjectToValue(jobject) const;
97 void getListFromJArray(jobjectArray, List&) const;
98
99 RootObject* rootObject() const;
100
101 private:
102 RefPtr<RootObject> _rootObject;
103 JSObject* _imp;
104 };
105
106
107 } // namespace Bindings
108
109 } // namespace KJS
110
111 extern "C" {
112
113 // The Java VM calls these functions to handle calls to methods in Java's JSObject class.
114 jlong KJS_JSCreateNativeJSObject(JNIEnv*, jclass, jstring jurl, jlong nativeHandle, jboolean ctx);
115 void KJS_JSObject_JSFinalize(JNIEnv*, jclass, jlong nativeJSObject);
116 jobject KJS_JSObject_JSObjectCall(JNIEnv*, jclass, jlong nativeJSObject, jstring jurl, jstring methodName, jobjectArray args, jboolean ctx);
117 jobject KJS_JSObject_JSObjectEval(JNIEnv*, jclass, jlong nativeJSObject, jstring jurl, jstring jscript, jboolean ctx);
118 jobject KJS_JSObject_JSObjectGetMember(JNIEnv*, jclass, jlong nativeJSObject, jstring jurl, jstring jname, jboolean ctx);
119 void KJS_JSObject_JSObjectSetMember(JNIEnv*, jclass, jlong nativeJSObject, jstring jurl, jstring jname, jobject value, jboolean ctx);
120 void KJS_JSObject_JSObjectRemoveMember(JNIEnv*, jclass, jlong nativeJSObject, jstring jurl, jstring jname, jboolean ctx);
121 jobject KJS_JSObject_JSObjectGetSlot(JNIEnv*, jclass, jlong nativeJSObject, jstring jurl, jint jindex, jboolean ctx);
122 void KJS_JSObject_JSObjectSetSlot(JNIEnv*, jclass, jlong nativeJSObject, jstring jurl, jint jindex, jobject value, jboolean ctx);
123 jstring KJS_JSObject_JSObjectToString(JNIEnv*, jclass, jlong nativeJSObject);
124
125 }
126
127 #endif // ENABLE(JAVA_BINDINGS)
128
129 #endif // JAVASCRIPTCORE_BINDINGS_JNI_JSOBJECT_H