tizen beta release
[profile/ivi/webkit-efl.git] / Source / JavaScriptGlue / JSValueWrapper.cpp
1 /*
2  * Copyright (C) 2005, 2009 Apple Computer, 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  *
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  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14  *     its contributors may be used to endorse or promote products derived
15  *     from this software without specific prior written permission. 
16  *
17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include "config.h"
30 #include "JSValueWrapper.h"
31 #include "JSRun.h"
32 #include <JavaScriptCore/JSArray.h>
33 #include <JavaScriptCore/PropertyNameArray.h>
34 #include <JavaScriptCore/StrongInlines.h>
35 #include <pthread.h>
36
37 JSValueWrapper::JSValueWrapper(JSValue inValue)
38     : fValue(getThreadGlobalExecState()->globalData(), inValue)
39 {
40 }
41
42 JSValueWrapper::~JSValueWrapper()
43 {
44 }
45
46 JSValue JSValueWrapper::GetValue()
47 {
48     return fValue.get();
49 }
50
51 void JSValueWrapper::GetJSObectCallBacks(JSObjectCallBacks& callBacks)
52 {
53     callBacks.dispose = (JSObjectDisposeProcPtr)JSValueWrapper::JSObjectDispose;
54     callBacks.equal = (JSObjectEqualProcPtr)0;
55     callBacks.copyPropertyNames = (JSObjectCopyPropertyNamesProcPtr)JSValueWrapper::JSObjectCopyPropertyNames;
56     callBacks.copyCFValue = (JSObjectCopyCFValueProcPtr)JSValueWrapper::JSObjectCopyCFValue;
57     callBacks.copyProperty = (JSObjectCopyPropertyProcPtr)JSValueWrapper::JSObjectCopyProperty;
58     callBacks.setProperty = (JSObjectSetPropertyProcPtr)JSValueWrapper::JSObjectSetProperty;
59     callBacks.callFunction = (JSObjectCallFunctionProcPtr)JSValueWrapper::JSObjectCallFunction;
60 }
61
62 void JSValueWrapper::JSObjectDispose(void *data)
63 {
64     JSValueWrapper* ptr = (JSValueWrapper*)data;
65     delete ptr;
66 }
67
68
69 CFArrayRef JSValueWrapper::JSObjectCopyPropertyNames(void *data)
70 {
71     JSGlueAPIEntry entry;
72
73     CFMutableArrayRef result = 0;
74     JSValueWrapper* ptr = (JSValueWrapper*)data;
75     if (ptr)
76     {
77         ExecState* exec = getThreadGlobalExecState();
78         JSObject* object = ptr->GetValue().toObject(exec);
79         PropertyNameArray propNames(exec);
80         object->methodTable()->getPropertyNames(object, exec, propNames, ExcludeDontEnumProperties);
81         PropertyNameArray::const_iterator iterator = propNames.begin();
82
83         while (iterator != propNames.end()) {
84             Identifier name = *iterator;
85             CFStringRef nameStr = IdentifierToCFString(name);
86
87             if (!result)
88             {
89                 result = CFArrayCreateMutable(0, 0, &kCFTypeArrayCallBacks);
90             }
91             if (result && nameStr)
92             {
93                 CFArrayAppendValue(result, nameStr);
94             }
95             ReleaseCFType(nameStr);
96             iterator++;
97         }
98
99     }
100     return result;
101 }
102
103
104 JSObjectRef JSValueWrapper::JSObjectCopyProperty(void *data, CFStringRef propertyName)
105 {
106     JSGlueAPIEntry entry;
107
108     JSObjectRef result = 0;
109     JSValueWrapper* ptr = (JSValueWrapper*)data;
110     if (ptr)
111     {
112         ExecState* exec = getThreadGlobalExecState();
113         JSValue propValue = ptr->GetValue().toObject(exec)->get(exec, CFStringToIdentifier(propertyName, exec));
114         JSValueWrapper* wrapperValue = new JSValueWrapper(propValue);
115
116         JSObjectCallBacks callBacks;
117         GetJSObectCallBacks(callBacks);
118         result = JSObjectCreateInternal(wrapperValue, &callBacks, JSValueWrapper::JSObjectMark, kJSUserObjectDataTypeJSValueWrapper);
119
120         if (!result)
121         {
122             delete wrapperValue;
123         }
124     }
125     return result;
126 }
127
128 void JSValueWrapper::JSObjectSetProperty(void *data, CFStringRef propertyName, JSObjectRef jsValue)
129 {
130     JSGlueAPIEntry entry;
131
132     JSValueWrapper* ptr = (JSValueWrapper*)data;
133     if (ptr)
134     {
135         ExecState* exec = getThreadGlobalExecState();
136         JSValue value = JSObjectKJSValue((JSUserObject*)jsValue);
137         JSObject *objValue = ptr->GetValue().toObject(exec);
138         PutPropertySlot slot;
139         objValue->methodTable()->put(objValue, exec, CFStringToIdentifier(propertyName, exec), value, slot);
140     }
141 }
142
143 JSObjectRef JSValueWrapper::JSObjectCallFunction(void *data, JSObjectRef thisObj, CFArrayRef args)
144 {
145     JSGlueAPIEntry entry;
146
147     JSObjectRef result = 0;
148     JSValueWrapper* ptr = (JSValueWrapper*)data;
149     if (ptr)
150     {
151         ExecState* exec = getThreadGlobalExecState();
152
153         JSValue value = JSObjectKJSValue((JSUserObject*)thisObj);
154         JSObject* ksjThisObj = value.toObject(exec);
155         JSObject* objValue = ptr->GetValue().toObject(exec);
156
157         MarkedArgumentBuffer listArgs;
158         CFIndex argCount = args ? CFArrayGetCount(args) : 0;
159         for (CFIndex i = 0; i < argCount; i++)
160         {
161             JSObjectRef jsArg = (JSObjectRef)CFArrayGetValueAtIndex(args, i);
162             JSValue kgsArg = JSObjectKJSValue((JSUserObject*)jsArg);
163             listArgs.append(kgsArg);
164         }
165
166         CallData callData;
167         CallType callType = objValue->methodTable()->getCallData(objValue, callData);
168         if (callType == CallTypeNone)
169             return 0;
170         JSValue  resultValue = call(exec, objValue, callType, callData, ksjThisObj, listArgs);
171         JSValueWrapper* wrapperValue = new JSValueWrapper(resultValue);
172         JSObjectCallBacks callBacks;
173         GetJSObectCallBacks(callBacks);
174         result = JSObjectCreate(wrapperValue, &callBacks);
175         if (!result)
176         {
177             delete wrapperValue;
178         }
179     }
180     return result;
181 }
182
183 CFTypeRef JSValueWrapper::JSObjectCopyCFValue(void *data)
184 {
185     JSGlueAPIEntry entry;
186
187     CFTypeRef result = 0;
188     JSValueWrapper* ptr = (JSValueWrapper*)data;
189     if (ptr)
190     {
191         result = KJSValueToCFType(ptr->GetValue(), getThreadGlobalExecState());
192     }
193     return result;
194 }
195
196 void JSValueWrapper::JSObjectMark(void *data)
197 {
198 }