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