1 /* 2 * Copyright (C) 2003, 2008, 2009 Apple Inc. All rights reserved. 3 * Copyright 2010, The Android Open Source Project 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY 15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR 18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #ifndef BridgeJSC_h 28 #define BridgeJSC_h 29 30 #if USE(JSC) 31 32 #include <runtime/JSString.h> 33 #include <wtf/HashMap.h> 34 #include <wtf/RefCounted.h> 35 #include <wtf/Vector.h> 36 37 namespace JSC { 38 39 class ArgList; 40 class Identifier; 41 class JSGlobalObject; 42 class PropertyNameArray; 43 class RuntimeObjectImp; 44 45 namespace Bindings { 46 47 class Instance; 48 class Method; 49 class RootObject; 50 51 typedef Vector<Method*> MethodList; 52 53 class Field { 54 public: 55 virtual JSValue valueFromInstance(ExecState*, const Instance*) const = 0; 56 virtual void setValueToInstance(ExecState*, const Instance*, JSValue) const = 0; 57 ~Field()58 virtual ~Field() { } 59 }; 60 61 class Class : public Noncopyable { 62 public: 63 virtual MethodList methodsNamed(const Identifier&, Instance*) const = 0; 64 virtual Field* fieldNamed(const Identifier&, Instance*) const = 0; fallbackObject(ExecState *,Instance *,const Identifier &)65 virtual JSValue fallbackObject(ExecState*, Instance*, const Identifier&) { return jsUndefined(); } 66 ~Class()67 virtual ~Class() { } 68 }; 69 70 typedef void (*KJSDidExecuteFunctionPtr)(ExecState*, JSObject* rootObject); 71 72 class Instance : public RefCounted<Instance> { 73 public: 74 Instance(PassRefPtr<RootObject>); 75 76 static void setDidExecuteFunction(KJSDidExecuteFunctionPtr func); 77 static KJSDidExecuteFunctionPtr didExecuteFunction(); 78 79 // These functions are called before and after the main entry points into 80 // the native implementations. They can be used to establish and cleanup 81 // any needed state. 82 void begin(); 83 void end(); 84 85 virtual Class* getClass() const = 0; 86 RuntimeObjectImp* createRuntimeObject(ExecState*); 87 void willInvalidateRuntimeObject(); 88 void willDestroyRuntimeObject(); 89 90 // Returns false if the value was not set successfully. setValueOfUndefinedField(ExecState *,const Identifier &,JSValue)91 virtual bool setValueOfUndefinedField(ExecState*, const Identifier&, JSValue) { return false; } 92 93 virtual JSValue invokeMethod(ExecState*, const MethodList&, const ArgList& args) = 0; 94 supportsInvokeDefaultMethod()95 virtual bool supportsInvokeDefaultMethod() const { return false; } invokeDefaultMethod(ExecState *,const ArgList &)96 virtual JSValue invokeDefaultMethod(ExecState*, const ArgList&) { return jsUndefined(); } 97 supportsConstruct()98 virtual bool supportsConstruct() const { return false; } invokeConstruct(ExecState *,const ArgList &)99 virtual JSValue invokeConstruct(ExecState*, const ArgList&) { return JSValue(); } 100 getPropertyNames(ExecState *,PropertyNameArray &)101 virtual void getPropertyNames(ExecState*, PropertyNameArray&) { } 102 103 virtual JSValue defaultValue(ExecState*, PreferredPrimitiveType) const = 0; 104 105 virtual JSValue valueOf(ExecState* exec) const = 0; 106 107 RootObject* rootObject() const; 108 109 virtual ~Instance(); 110 getOwnPropertySlot(JSObject *,ExecState *,const Identifier &,PropertySlot &)111 virtual bool getOwnPropertySlot(JSObject*, ExecState*, const Identifier&, PropertySlot&) { return false; } getOwnPropertyDescriptor(JSObject *,ExecState *,const Identifier &,PropertyDescriptor &)112 virtual bool getOwnPropertyDescriptor(JSObject*, ExecState*, const Identifier&, PropertyDescriptor&) { return false; } put(JSObject *,ExecState *,const Identifier &,JSValue,PutPropertySlot &)113 virtual void put(JSObject*, ExecState*, const Identifier&, JSValue, PutPropertySlot&) { } 114 115 protected: virtualBegin()116 virtual void virtualBegin() { } virtualEnd()117 virtual void virtualEnd() { } 118 virtual RuntimeObjectImp* newRuntimeObject(ExecState*); 119 120 RefPtr<RootObject> m_rootObject; 121 122 private: 123 RuntimeObjectImp* m_runtimeObject; 124 }; 125 126 class Array : public Noncopyable { 127 public: 128 Array(PassRefPtr<RootObject>); 129 virtual ~Array(); 130 131 virtual void setValueAt(ExecState*, unsigned index, JSValue) const = 0; 132 virtual JSValue valueAt(ExecState*, unsigned index) const = 0; 133 virtual unsigned int getLength() const = 0; 134 135 protected: 136 RefPtr<RootObject> m_rootObject; 137 }; 138 139 const char* signatureForParameters(const ArgList&); 140 141 typedef HashMap<RefPtr<UString::Rep>, MethodList*> MethodListMap; 142 typedef HashMap<RefPtr<UString::Rep>, Method*> MethodMap; 143 typedef HashMap<RefPtr<UString::Rep>, Field*> FieldMap; 144 145 } // namespace Bindings 146 147 } // namespace JSC 148 149 #endif // USE(JSC) 150 151 #endif 152