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