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>
34 #include <JavaScriptCore/StrongInlines.h>
37 JSValueWrapper::JSValueWrapper(JSValue inValue)
38 : fValue(getThreadGlobalExecState()->globalData(), inValue)
42 JSValueWrapper::~JSValueWrapper()
46 JSValue JSValueWrapper::GetValue()
51 void JSValueWrapper::GetJSObectCallBacks(JSObjectCallBacks& callBacks)
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;
62 void JSValueWrapper::JSObjectDispose(void *data)
64 JSValueWrapper* ptr = (JSValueWrapper*)data;
69 CFArrayRef JSValueWrapper::JSObjectCopyPropertyNames(void *data)
73 CFMutableArrayRef result = 0;
74 JSValueWrapper* ptr = (JSValueWrapper*)data;
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();
83 while (iterator != propNames.end()) {
84 Identifier name = *iterator;
85 CFStringRef nameStr = IdentifierToCFString(name);
89 result = CFArrayCreateMutable(0, 0, &kCFTypeArrayCallBacks);
91 if (result && nameStr)
93 CFArrayAppendValue(result, nameStr);
95 ReleaseCFType(nameStr);
104 JSObjectRef JSValueWrapper::JSObjectCopyProperty(void *data, CFStringRef propertyName)
106 JSGlueAPIEntry entry;
108 JSObjectRef result = 0;
109 JSValueWrapper* ptr = (JSValueWrapper*)data;
112 ExecState* exec = getThreadGlobalExecState();
113 JSValue propValue = ptr->GetValue().toObject(exec)->get(exec, CFStringToIdentifier(propertyName, exec));
114 JSValueWrapper* wrapperValue = new JSValueWrapper(propValue);
116 JSObjectCallBacks callBacks;
117 GetJSObectCallBacks(callBacks);
118 result = JSObjectCreateInternal(wrapperValue, &callBacks, JSValueWrapper::JSObjectMark, kJSUserObjectDataTypeJSValueWrapper);
128 void JSValueWrapper::JSObjectSetProperty(void *data, CFStringRef propertyName, JSObjectRef jsValue)
130 JSGlueAPIEntry entry;
132 JSValueWrapper* ptr = (JSValueWrapper*)data;
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);
143 JSObjectRef JSValueWrapper::JSObjectCallFunction(void *data, JSObjectRef thisObj, CFArrayRef args)
145 JSGlueAPIEntry entry;
147 JSObjectRef result = 0;
148 JSValueWrapper* ptr = (JSValueWrapper*)data;
151 ExecState* exec = getThreadGlobalExecState();
153 JSValue value = JSObjectKJSValue((JSUserObject*)thisObj);
154 JSObject* ksjThisObj = value.toObject(exec);
155 JSObject* objValue = ptr->GetValue().toObject(exec);
157 MarkedArgumentBuffer listArgs;
158 CFIndex argCount = args ? CFArrayGetCount(args) : 0;
159 for (CFIndex i = 0; i < argCount; i++)
161 JSObjectRef jsArg = (JSObjectRef)CFArrayGetValueAtIndex(args, i);
162 JSValue kgsArg = JSObjectKJSValue((JSUserObject*)jsArg);
163 listArgs.append(kgsArg);
167 CallType callType = objValue->methodTable()->getCallData(objValue, callData);
168 if (callType == CallTypeNone)
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);
183 CFTypeRef JSValueWrapper::JSObjectCopyCFValue(void *data)
185 JSGlueAPIEntry entry;
187 CFTypeRef result = 0;
188 JSValueWrapper* ptr = (JSValueWrapper*)data;
191 result = KJSValueToCFType(ptr->GetValue(), getThreadGlobalExecState());
196 void JSValueWrapper::JSObjectMark(void *data)