1/*
2 * Copyright (C) 2004, 2008 Apple 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 KJS_BINDINGS_OBJC_RUNTIME_H
27#define KJS_BINDINGS_OBJC_RUNTIME_H
28
29#include "BridgeJSC.h"
30#include "objc_header.h"
31#include <runtime/JSGlobalObject.h>
32#include <runtime/JSObjectWithGlobalObject.h>
33#include <wtf/RetainPtr.h>
34
35namespace JSC {
36namespace Bindings {
37
38ClassStructPtr webScriptObjectClass();
39ClassStructPtr webUndefinedClass();
40
41class ObjcInstance;
42
43class ObjcField : public Field {
44public:
45    ObjcField(IvarStructPtr);
46    ObjcField(CFStringRef name);
47
48    virtual JSValue valueFromInstance(ExecState*, const Instance*) const;
49    virtual void setValueToInstance(ExecState*, const Instance*, JSValue) const;
50
51private:
52    IvarStructPtr _ivar;
53    RetainPtr<CFStringRef> _name;
54};
55
56class ObjcMethod : public Method {
57public:
58    ObjcMethod() : _objcClass(0), _selector(0), _javaScriptName(0) {}
59    ObjcMethod(ClassStructPtr aClass, SEL _selector);
60
61    virtual int numParameters() const;
62
63    NSMethodSignature *getMethodSignature() const;
64
65    bool isFallbackMethod() const { return _selector == @selector(invokeUndefinedMethodFromWebScript:withArguments:); }
66    void setJavaScriptName(CFStringRef n) { _javaScriptName = n; }
67    CFStringRef javaScriptName() const { return _javaScriptName.get(); }
68
69    SEL selector() const { return _selector; }
70
71private:
72    ClassStructPtr _objcClass;
73    SEL _selector;
74    RetainPtr<CFStringRef> _javaScriptName;
75};
76
77class ObjcArray : public Array {
78public:
79    ObjcArray(ObjectStructPtr, PassRefPtr<RootObject>);
80
81    virtual void setValueAt(ExecState *exec, unsigned int index, JSValue aValue) const;
82    virtual JSValue valueAt(ExecState *exec, unsigned int index) const;
83    virtual unsigned int getLength() const;
84
85    ObjectStructPtr getObjcArray() const { return _array.get(); }
86
87    static JSValue convertObjcArrayToArray(ExecState *exec, ObjectStructPtr anObject);
88
89private:
90    RetainPtr<ObjectStructPtr> _array;
91};
92
93class ObjcFallbackObjectImp : public JSObjectWithGlobalObject {
94public:
95    ObjcFallbackObjectImp(ExecState*, JSGlobalObject*, ObjcInstance*, const Identifier& propertyName);
96
97    static const ClassInfo s_info;
98
99    const Identifier& propertyName() const { return _item; }
100
101    static ObjectPrototype* createPrototype(ExecState*, JSGlobalObject* globalObject)
102    {
103        return globalObject->objectPrototype();
104    }
105
106    static Structure* createStructure(JSGlobalData& globalData, JSValue prototype)
107    {
108        return Structure::create(globalData, prototype, TypeInfo(ObjectType, StructureFlags), AnonymousSlotCount, &s_info);
109    }
110
111private:
112    static const unsigned StructureFlags = OverridesGetOwnPropertySlot | JSObject::StructureFlags;
113    virtual bool getOwnPropertySlot(ExecState*, const Identifier&, PropertySlot&);
114    virtual bool getOwnPropertyDescriptor(ExecState*, const Identifier&, PropertyDescriptor&);
115    virtual void put(ExecState*, const Identifier& propertyName, JSValue, PutPropertySlot&);
116    virtual CallType getCallData(CallData&);
117    virtual bool deleteProperty(ExecState*, const Identifier& propertyName);
118    virtual JSValue defaultValue(ExecState*, PreferredPrimitiveType) const;
119
120    virtual bool toBoolean(ExecState*) const;
121
122    RefPtr<ObjcInstance> _instance;
123    Identifier _item;
124};
125
126} // namespace Bindings
127} // namespace JSC
128
129#endif
130