Home | History | Annotate | Download | only in jsc
      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 "Bridge.h"
     33 #include <runtime/JSString.h>
     34 #include <wtf/HashMap.h>
     35 #include <wtf/RefCounted.h>
     36 #include <wtf/Vector.h>
     37 
     38 namespace JSC  {
     39 
     40 class ArgList;
     41 class Identifier;
     42 class JSGlobalObject;
     43 class PropertyNameArray;
     44 class RuntimeMethod;
     45 
     46 namespace Bindings {
     47 
     48 class Instance;
     49 class Method;
     50 class RootObject;
     51 class RuntimeObject;
     52 
     53 typedef Vector<Method*> MethodList;
     54 
     55 class Field {
     56 public:
     57     virtual JSValue valueFromInstance(ExecState*, const Instance*) const = 0;
     58     virtual void setValueToInstance(ExecState*, const Instance*, JSValue) const = 0;
     59 
     60     virtual ~Field() { }
     61 };
     62 
     63 class Class {
     64     WTF_MAKE_NONCOPYABLE(Class); WTF_MAKE_FAST_ALLOCATED;
     65 public:
     66     Class() { }
     67     virtual MethodList methodsNamed(const Identifier&, Instance*) const = 0;
     68     virtual Field* fieldNamed(const Identifier&, Instance*) const = 0;
     69     virtual JSValue fallbackObject(ExecState*, Instance*, const Identifier&) { return jsUndefined(); }
     70 
     71     virtual ~Class() { }
     72 };
     73 
     74 typedef void (*KJSDidExecuteFunctionPtr)(ExecState*, JSObject* rootObject);
     75 
     76 class Instance : public RefCounted<Instance> {
     77 public:
     78     Instance(PassRefPtr<RootObject>);
     79 
     80     static void setDidExecuteFunction(KJSDidExecuteFunctionPtr func);
     81     static KJSDidExecuteFunctionPtr didExecuteFunction();
     82 
     83     // These functions are called before and after the main entry points into
     84     // the native implementations.  They can be used to establish and cleanup
     85     // any needed state.
     86     void begin();
     87     void end();
     88 
     89     virtual Class* getClass() const = 0;
     90     JSObject* createRuntimeObject(ExecState*);
     91     void willInvalidateRuntimeObject();
     92 
     93     // Returns false if the value was not set successfully.
     94     virtual bool setValueOfUndefinedField(ExecState*, const Identifier&, JSValue) { return false; }
     95 
     96     virtual JSValue getMethod(ExecState* exec, const Identifier& propertyName) = 0;
     97     virtual JSValue invokeMethod(ExecState*, RuntimeMethod* method) = 0;
     98 
     99     virtual bool supportsInvokeDefaultMethod() const { return false; }
    100     virtual JSValue invokeDefaultMethod(ExecState*) { return jsUndefined(); }
    101 
    102     virtual bool supportsConstruct() const { return false; }
    103     virtual JSValue invokeConstruct(ExecState*, const ArgList&) { return JSValue(); }
    104 
    105     virtual void getPropertyNames(ExecState*, PropertyNameArray&) { }
    106 
    107     virtual JSValue defaultValue(ExecState*, PreferredPrimitiveType) const = 0;
    108 
    109     virtual JSValue valueOf(ExecState* exec) const = 0;
    110 
    111     RootObject* rootObject() const;
    112 
    113     virtual ~Instance();
    114 
    115     virtual bool getOwnPropertySlot(JSObject*, ExecState*, const Identifier&, PropertySlot&) { return false; }
    116     virtual bool getOwnPropertyDescriptor(JSObject*, ExecState*, const Identifier&, PropertyDescriptor&) { return false; }
    117     virtual void put(JSObject*, ExecState*, const Identifier&, JSValue, PutPropertySlot&) { }
    118 
    119 protected:
    120     virtual void virtualBegin() { }
    121     virtual void virtualEnd() { }
    122     virtual RuntimeObject* newRuntimeObject(ExecState*);
    123 
    124     RefPtr<RootObject> m_rootObject;
    125 
    126 private:
    127     Weak<RuntimeObject> m_runtimeObject;
    128 };
    129 
    130 class Array {
    131     WTF_MAKE_NONCOPYABLE(Array);
    132 public:
    133     Array(PassRefPtr<RootObject>);
    134     virtual ~Array();
    135 
    136     virtual void setValueAt(ExecState*, unsigned index, JSValue) const = 0;
    137     virtual JSValue valueAt(ExecState*, unsigned index) const = 0;
    138     virtual unsigned int getLength() const = 0;
    139 
    140 protected:
    141     RefPtr<RootObject> m_rootObject;
    142 };
    143 
    144 const char* signatureForParameters(const ArgList&);
    145 
    146 typedef HashMap<RefPtr<StringImpl>, MethodList*> MethodListMap;
    147 typedef HashMap<RefPtr<StringImpl>, Method*> MethodMap;
    148 typedef HashMap<RefPtr<StringImpl>, Field*> FieldMap;
    149 
    150 } // namespace Bindings
    151 
    152 } // namespace JSC
    153 
    154 #endif // USE(JSC)
    155 
    156 #endif
    157