Rubber stamped by hyatt.
[WebKit-https.git] / JavaScriptGlue / JSValueWrapper.cpp
1 //
2 // JSValueWrapper.cpp
3 //
4 #include "JSValueWrapper.h"
5
6 JSValueWrapper::JSValueWrapper(const Value& inValue, ExecState *inExec) 
7         : fValue(inValue), fExec(inExec) 
8
9 }
10
11 JSValueWrapper::~JSValueWrapper()
12
13 }
14
15 Value& JSValueWrapper::GetValue() 
16
17         return fValue; 
18 }
19 ExecState* JSValueWrapper::GetExecState() const 
20 {
21         return fExec; 
22 }
23         
24
25 void JSValueWrapper::GetJSObectCallBacks(JSObjectCallBacks& callBacks)
26 {
27         callBacks.dispose = (JSObjectDisposeProcPtr)JSValueWrapper::JSObjectDispose;
28         callBacks.equal = (JSObjectEqualProcPtr)NULL;
29         callBacks.copyPropertyNames = (JSObjectCopyPropertyNamesProcPtr)JSValueWrapper::JSObjectCopyPropertyNames;
30         callBacks.copyCFValue = (JSObjectCopyCFValueProcPtr)JSValueWrapper::JSObjectCopyCFValue;
31         callBacks.copyProperty = (JSObjectCopyPropertyProcPtr)JSValueWrapper::JSObjectCopyProperty;
32         callBacks.setProperty = (JSObjectSetPropertyProcPtr)JSValueWrapper::JSObjectSetProperty;
33         callBacks.callFunction = (JSObjectCallFunctionProcPtr)JSValueWrapper::JSObjectCallFunction;
34 }
35                         
36 void JSValueWrapper::JSObjectDispose(void* data)
37 {
38         JSValueWrapper* ptr = (JSValueWrapper*)data;
39         delete ptr;
40 }
41         
42
43 CFArrayRef JSValueWrapper::JSObjectCopyPropertyNames(void* data)
44 {
45         CFMutableArrayRef result = NULL;
46         JSValueWrapper* ptr = (JSValueWrapper*)data;
47         if (ptr)
48         {
49                 ExecState* exec = ptr->GetExecState();
50 #if JAG_PINK_OR_LATER
51                 Object object = ptr->GetValue().toObject(exec);
52                 ReferenceList list = object.propList(exec, false);
53                 ReferenceListIterator iterator = list.begin();
54 #else
55                 Object object = ptr->GetValue().imp()->toObject(exec);
56                 List list = object.propList(exec, false);
57                 ListIterator iterator = list.begin();
58 #endif
59
60
61                 while (iterator != list.end()) {
62 #if JAG_PINK_OR_LATER
63                         Identifier name = iterator->getPropertyName(exec);
64                         CFStringRef nameStr = IdentifierToCFString(name);
65 #else
66                         UString name = iterator->getPropertyName(exec);
67                         CFStringRef nameStr = UStringToCFString(name);
68 #endif
69                         if (!result)
70                         {
71                                 result = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
72                         }
73                         if (result && nameStr)
74                         {
75                                 CFArrayAppendValue(result, nameStr);
76                         }
77                         ReleaseCFType(nameStr);
78                         iterator++;
79                 }
80
81         }
82         return result;
83 }
84
85
86 JSObjectRef JSValueWrapper::JSObjectCopyProperty(void* data, CFStringRef propertyName)
87 {
88         JSObjectRef result = NULL;
89         JSValueWrapper* ptr = (JSValueWrapper*)data;
90         if (ptr)
91         {
92                 ExecState* exec = ptr->GetExecState();
93 #if JAG_PINK_OR_LATER
94                 Value propValue = ptr->GetValue().toObject(exec).get(exec, CFStringToIdentifier(propertyName));
95 #else
96                 Value propValue = ptr->GetValue().imp()->toObject(exec).get(exec, CFStringToUString(propertyName));
97 #endif
98                 JSValueWrapper* wrapperValue = new JSValueWrapper(propValue, exec);
99
100                 JSObjectCallBacks callBacks;
101                 GetJSObectCallBacks(callBacks);
102                 result = JSObjectCreateInternal(wrapperValue, &callBacks, JSValueWrapper::JSObjectMark, kJSUserObjectDataTypeJSValueWrapper);
103
104                 if (!result)
105                 {
106                         delete wrapperValue;
107                 }
108         }
109         return result;
110 }
111
112 void JSValueWrapper::JSObjectSetProperty(void* data, CFStringRef propertyName, JSObjectRef jsValue)
113 {
114         JSValueWrapper* ptr = (JSValueWrapper*)data;
115         if (ptr)
116         {
117                 ExecState* exec = ptr->GetExecState();  
118                 Value value = JSObjectKJSValue((JSUserObject*)jsValue);
119 #if JAG_PINK_OR_LATER
120                 Object objValue = ptr->GetValue().toObject(exec);
121                 objValue.put(exec, CFStringToIdentifier(propertyName), value);
122 #else
123                 Object objValue = ptr->GetValue().imp()->toObject(exec);
124                 objValue.put(exec, CFStringToUString(propertyName), value);
125 #endif
126         }
127 }
128
129 JSObjectRef JSValueWrapper::JSObjectCallFunction(void* data, JSObjectRef thisObj, CFArrayRef args)
130 {
131         JSObjectRef result = NULL;
132         JSValueWrapper* ptr = (JSValueWrapper*)data;
133         if (ptr)
134         {
135                 ExecState* exec = ptr->GetExecState();  
136         
137                 Value value = JSObjectKJSValue((JSUserObject*)thisObj);
138 #if JAG_PINK_OR_LATER
139                 Object ksjThisObj = value.toObject(exec);
140                 Object objValue = ptr->GetValue().toObject(exec);
141 #else
142                 Object ksjThisObj = value.imp()->toObject(exec);
143                 Object objValue = ptr->GetValue().imp()->toObject(exec);
144 #endif
145
146                 List listArgs;
147                 CFIndex argCount = args ? CFArrayGetCount(args) : 0;
148                 for (CFIndex i = 0; i < argCount; i++)
149                 {
150                         JSObjectRef jsArg = (JSObjectRef)CFArrayGetValueAtIndex(args, i);
151                         Value kgsArg = JSObjectKJSValue((JSUserObject*)jsArg);
152                         listArgs.append(kgsArg);
153                 }
154
155                 Value resultValue = objValue.call(exec, ksjThisObj, listArgs);
156                 JSValueWrapper* wrapperValue = new JSValueWrapper(resultValue, ptr->GetExecState());
157                 JSObjectCallBacks callBacks;
158                 GetJSObectCallBacks(callBacks);
159                 result = JSObjectCreate(wrapperValue, &callBacks);
160                 if (!result)
161                 {
162                         delete wrapperValue;
163                 }
164         }
165         return result;
166 }
167
168 CFTypeRef JSValueWrapper::JSObjectCopyCFValue(void* data)
169 {
170         CFTypeRef result = NULL;
171         JSValueWrapper* ptr = (JSValueWrapper*)data;
172         if (ptr)
173         {
174                 result = KJSValueToCFType(ptr->fValue, ptr->fExec);
175         }
176         return result;
177 }
178
179 void JSValueWrapper::JSObjectMark(void* data)
180 {
181         JSValueWrapper* ptr = (JSValueWrapper*)data;
182         if (ptr)
183         {
184                 ptr->fValue.imp()->mark();
185         }
186 }