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(JAVA_BRIDGE)
     30 
     31 #include "JNIUtility.h"
     32 #include <runtime/JSValue.h>
     33 #include <wtf/RefPtr.h>
     34 
     35 #define jlong_to_ptr(a) ((void*)(uintptr_t)(a))
     36 #define jlong_to_impptr(a) (static_cast<JSC::JSObject*>(((void*)(uintptr_t)(a))))
     37 #define ptr_to_jlong(a) ((jlong)(uintptr_t)(a))
     38 
     39 #if PLATFORM(MAC)
     40 
     41 namespace JSC {
     42 
     43 class ArgList;
     44 class ExecState;
     45 class JSObject;
     46 class MarkedArgumentBuffer;
     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(ExecState*, jobject) const;
     97     void getListFromJArray(ExecState*, jobjectArray, MarkedArgumentBuffer&) const;
     98 
     99     RootObject* rootObject() const;
    100 
    101     // Must be called from the thread that will be used to access JavaScript.
    102     static void initializeJNIThreading();
    103 private:
    104     RefPtr<RootObject> _rootObject;
    105     JSObject* _imp;
    106 };
    107 
    108 
    109 } // namespace Bindings
    110 
    111 } // namespace JSC
    112 
    113 extern "C" {
    114 
    115 // The Java VM calls these functions to handle calls to methods in Java's JSObject class.
    116 jlong KJS_JSCreateNativeJSObject(JNIEnv*, jclass, jstring jurl, jlong nativeHandle, jboolean ctx);
    117 void KJS_JSObject_JSFinalize(JNIEnv*, jclass, jlong nativeJSObject);
    118 jobject KJS_JSObject_JSObjectCall(JNIEnv*, jclass, jlong nativeJSObject, jstring jurl, jstring methodName, jobjectArray args, jboolean ctx);
    119 jobject KJS_JSObject_JSObjectEval(JNIEnv*, jclass, jlong nativeJSObject, jstring jurl, jstring jscript, jboolean ctx);
    120 jobject KJS_JSObject_JSObjectGetMember(JNIEnv*, jclass, jlong nativeJSObject, jstring jurl, jstring jname, jboolean ctx);
    121 void KJS_JSObject_JSObjectSetMember(JNIEnv*, jclass, jlong nativeJSObject, jstring jurl, jstring jname, jobject value, jboolean ctx);
    122 void KJS_JSObject_JSObjectRemoveMember(JNIEnv*, jclass, jlong nativeJSObject, jstring jurl, jstring jname, jboolean ctx);
    123 jobject KJS_JSObject_JSObjectGetSlot(JNIEnv*, jclass, jlong nativeJSObject, jstring jurl, jint jindex, jboolean ctx);
    124 void KJS_JSObject_JSObjectSetSlot(JNIEnv*, jclass, jlong nativeJSObject, jstring jurl, jint jindex, jobject value, jboolean ctx);
    125 jstring KJS_JSObject_JSObjectToString(JNIEnv*, jclass, jlong nativeJSObject);
    126 
    127 }
    128 
    129 #endif // PLATFORM(MAC)
    130 
    131 #endif // ENABLE(JAVA_BRIDGE)
    132 
    133 #endif // JAVASCRIPTCORE_BINDINGS_JNI_JSOBJECT_H
    134