2 * Copyright (C) 2005, 2009 Apple Computer, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
30 #include "JSValueWrapper.h"
32 #include <JavaScriptCore/JSArray.h>
33 #include <JavaScriptCore/PropertyNameArray.h>
36 JSValueWrapper::JSValueWrapper(JSValue inValue)
37 : fValue(getThreadGlobalExecState()->globalData(), inValue)
41 JSValueWrapper::~JSValueWrapper()
45 JSValue JSValueWrapper::GetValue()
50 void JSValueWrapper::GetJSObectCallBacks(JSObjectCallBacks& callBacks)
52 callBacks.dispose = (JSObjectDisposeProcPtr)JSValueWrapper::JSObjectDispose;
53 callBacks.equal = (JSObjectEqualProcPtr)0;
54 callBacks.copyPropertyNames = (JSObjectCopyPropertyNamesProcPtr)JSValueWrapper::JSObjectCopyPropertyNames;
55 callBacks.copyCFValue = (JSObjectCopyCFValueProcPtr)JSValueWrapper::JSObjectCopyCFValue;
56 callBacks.copyProperty = (JSObjectCopyPropertyProcPtr)JSValueWrapper::JSObjectCopyProperty;
57 callBacks.setProperty = (JSObjectSetPropertyProcPtr)JSValueWrapper::JSObjectSetProperty;
58 callBacks.callFunction = (JSObjectCallFunctionProcPtr)JSValueWrapper::JSObjectCallFunction;
61 void JSValueWrapper::JSObjectDispose(void *data)
63 JSValueWrapper* ptr = (JSValueWrapper*)data;
68 CFArrayRef JSValueWrapper::JSObjectCopyPropertyNames(void *data)
72 CFMutableArrayRef result = 0;
73 JSValueWrapper* ptr = (JSValueWrapper*)data;
76 ExecState* exec = getThreadGlobalExecState();
77 JSObject* object = ptr->GetValue().toObject(exec);
78 PropertyNameArray propNames(exec);
79 object->getPropertyNames(exec, propNames);
80 PropertyNameArray::const_iterator iterator = propNames.begin();
82 while (iterator != propNames.end()) {
83 Identifier name = *iterator;
84 CFStringRef nameStr = IdentifierToCFString(name);
88 result = CFArrayCreateMutable(0, 0, &kCFTypeArrayCallBacks);
90 if (result && nameStr)
92 CFArrayAppendValue(result, nameStr);
94 ReleaseCFType(nameStr);
103 JSObjectRef JSValueWrapper::JSObjectCopyProperty(void *data, CFStringRef propertyName)
105 JSGlueAPIEntry entry;
107 JSObjectRef result = 0;
108 JSValueWrapper* ptr = (JSValueWrapper*)data;
111 ExecState* exec = getThreadGlobalExecState();
112 JSValue propValue = ptr->GetValue().toObject(exec)->get(exec, CFStringToIdentifier(propertyName, exec));
113 JSValueWrapper* wrapperValue = new JSValueWrapper(propValue);
115 JSObjectCallBacks callBacks;
116 GetJSObectCallBacks(callBacks);
117 result = JSObjectCreateInternal(wrapperValue, &callBacks, JSValueWrapper::JSObjectMark, kJSUserObjectDataTypeJSValueWrapper);
127 void JSValueWrapper::JSObjectSetProperty(void *data, CFStringRef propertyName, JSObjectRef jsValue)
129 JSGlueAPIEntry entry;
131 JSValueWrapper* ptr = (JSValueWrapper*)data;
134 ExecState* exec = getThreadGlobalExecState();
135 JSValue value = JSObjectKJSValue((JSUserObject*)jsValue);
136 JSObject *objValue = ptr->GetValue().toObject(exec);
137 PutPropertySlot slot;
138 objValue->put(exec, CFStringToIdentifier(propertyName, exec), value, slot);
142 JSObjectRef JSValueWrapper::JSObjectCallFunction(void *data, JSObjectRef thisObj, CFArrayRef args)
144 JSGlueAPIEntry entry;
146 JSObjectRef result = 0;
147 JSValueWrapper* ptr = (JSValueWrapper*)data;
150 ExecState* exec = getThreadGlobalExecState();
152 JSValue value = JSObjectKJSValue((JSUserObject*)thisObj);
153 JSObject* ksjThisObj = value.toObject(exec);
154 JSObject* objValue = ptr->GetValue().toObject(exec);
156 MarkedArgumentBuffer listArgs;
157 CFIndex argCount = args ? CFArrayGetCount(args) : 0;
158 for (CFIndex i = 0; i < argCount; i++)
160 JSObjectRef jsArg = (JSObjectRef)CFArrayGetValueAtIndex(args, i);
161 JSValue kgsArg = JSObjectKJSValue((JSUserObject*)jsArg);
162 listArgs.append(kgsArg);
166 CallType callType = objValue->getCallData(callData);
167 if (callType == CallTypeNone)
169 JSValue resultValue = call(exec, objValue, callType, callData, ksjThisObj, listArgs);
170 JSValueWrapper* wrapperValue = new JSValueWrapper(resultValue);
171 JSObjectCallBacks callBacks;
172 GetJSObectCallBacks(callBacks);
173 result = JSObjectCreate(wrapperValue, &callBacks);
182 CFTypeRef JSValueWrapper::JSObjectCopyCFValue(void *data)
184 JSGlueAPIEntry entry;
186 CFTypeRef result = 0;
187 JSValueWrapper* ptr = (JSValueWrapper*)data;
190 result = KJSValueToCFType(ptr->GetValue(), getThreadGlobalExecState());
195 void JSValueWrapper::JSObjectMark(void *data)