2 * Copyright (C) 2006, 2007 Apple 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
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.
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.
27 #include "JSValueRef.h"
29 #include <wtf/Platform.h>
31 #include "JSCallbackObject.h"
33 #include <runtime/JSGlobalObject.h>
34 #include <runtime/JSString.h>
35 #include <kjs/operations.h>
36 #include <kjs/protect.h>
37 #include <kjs/ustring.h>
38 #include <runtime/JSValue.h>
40 #include <wtf/Assertions.h>
42 #include <algorithm> // for std::min
44 JSType JSValueGetType(JSContextRef, JSValueRef value)
46 JSC::JSValue* jsValue = toJS(value);
47 if (jsValue->isUndefined())
48 return kJSTypeUndefined;
49 if (jsValue->isNull())
51 if (jsValue->isBoolean())
52 return kJSTypeBoolean;
53 if (jsValue->isNumber())
55 if (jsValue->isString())
57 ASSERT(jsValue->isObject());
61 using namespace JSC; // placed here to avoid conflict between JSC::JSType and JSType, above.
63 bool JSValueIsUndefined(JSContextRef, JSValueRef value)
65 JSValue* jsValue = toJS(value);
66 return jsValue->isUndefined();
69 bool JSValueIsNull(JSContextRef, JSValueRef value)
71 JSValue* jsValue = toJS(value);
72 return jsValue->isNull();
75 bool JSValueIsBoolean(JSContextRef, JSValueRef value)
77 JSValue* jsValue = toJS(value);
78 return jsValue->isBoolean();
81 bool JSValueIsNumber(JSContextRef, JSValueRef value)
83 JSValue* jsValue = toJS(value);
84 return jsValue->isNumber();
87 bool JSValueIsString(JSContextRef, JSValueRef value)
89 JSValue* jsValue = toJS(value);
90 return jsValue->isString();
93 bool JSValueIsObject(JSContextRef, JSValueRef value)
95 JSValue* jsValue = toJS(value);
96 return jsValue->isObject();
99 bool JSValueIsObjectOfClass(JSContextRef, JSValueRef value, JSClassRef jsClass)
101 JSValue* jsValue = toJS(value);
103 if (JSObject* o = jsValue->getObject()) {
104 if (o->inherits(&JSCallbackObject<JSGlobalObject>::info))
105 return static_cast<JSCallbackObject<JSGlobalObject>*>(o)->inherits(jsClass);
106 else if (o->inherits(&JSCallbackObject<JSObject>::info))
107 return static_cast<JSCallbackObject<JSObject>*>(o)->inherits(jsClass);
112 bool JSValueIsEqual(JSContextRef ctx, JSValueRef a, JSValueRef b, JSValueRef* exception)
114 ExecState* exec = toJS(ctx);
115 exec->globalData().heap.registerThread();
118 JSValue* jsA = toJS(a);
119 JSValue* jsB = toJS(b);
121 bool result = equal(exec, jsA, jsB); // false if an exception is thrown
122 if (exec->hadException()) {
124 *exception = toRef(exec->exception());
125 exec->clearException();
130 bool JSValueIsStrictEqual(JSContextRef, JSValueRef a, JSValueRef b)
132 JSValue* jsA = toJS(a);
133 JSValue* jsB = toJS(b);
135 bool result = strictEqual(jsA, jsB);
139 bool JSValueIsInstanceOfConstructor(JSContextRef ctx, JSValueRef value, JSObjectRef constructor, JSValueRef* exception)
141 ExecState* exec = toJS(ctx);
142 exec->globalData().heap.registerThread();
145 JSValue* jsValue = toJS(value);
146 JSObject* jsConstructor = toJS(constructor);
147 if (!jsConstructor->structureID()->typeInfo().implementsHasInstance())
149 bool result = jsConstructor->hasInstance(exec, jsValue, jsConstructor->get(exec, exec->propertyNames().prototype)); // false if an exception is thrown
150 if (exec->hadException()) {
152 *exception = toRef(exec->exception());
153 exec->clearException();
158 JSValueRef JSValueMakeUndefined(JSContextRef)
160 return toRef(jsUndefined());
163 JSValueRef JSValueMakeNull(JSContextRef)
165 return toRef(jsNull());
168 JSValueRef JSValueMakeBoolean(JSContextRef, bool value)
170 return toRef(jsBoolean(value));
173 JSValueRef JSValueMakeNumber(JSContextRef ctx, double value)
175 ExecState* exec = toJS(ctx);
176 exec->globalData().heap.registerThread();
179 return toRef(jsNumber(exec, value));
182 JSValueRef JSValueMakeString(JSContextRef ctx, JSStringRef string)
184 ExecState* exec = toJS(ctx);
185 exec->globalData().heap.registerThread();
188 return toRef(jsString(exec, string->ustring()));
191 bool JSValueToBoolean(JSContextRef ctx, JSValueRef value)
193 ExecState* exec = toJS(ctx);
194 JSValue* jsValue = toJS(value);
195 return jsValue->toBoolean(exec);
198 double JSValueToNumber(JSContextRef ctx, JSValueRef value, JSValueRef* exception)
200 ExecState* exec = toJS(ctx);
201 exec->globalData().heap.registerThread();
204 JSValue* jsValue = toJS(value);
206 double number = jsValue->toNumber(exec);
207 if (exec->hadException()) {
209 *exception = toRef(exec->exception());
210 exec->clearException();
216 JSStringRef JSValueToStringCopy(JSContextRef ctx, JSValueRef value, JSValueRef* exception)
218 ExecState* exec = toJS(ctx);
219 exec->globalData().heap.registerThread();
222 JSValue* jsValue = toJS(value);
224 RefPtr<OpaqueJSString> stringRef(OpaqueJSString::create(jsValue->toString(exec)));
225 if (exec->hadException()) {
227 *exception = toRef(exec->exception());
228 exec->clearException();
231 return stringRef.release().releaseRef();
234 JSObjectRef JSValueToObject(JSContextRef ctx, JSValueRef value, JSValueRef* exception)
236 ExecState* exec = toJS(ctx);
237 exec->globalData().heap.registerThread();
240 JSValue* jsValue = toJS(value);
242 JSObjectRef objectRef = toRef(jsValue->toObject(exec));
243 if (exec->hadException()) {
245 *exception = toRef(exec->exception());
246 exec->clearException();
252 void JSValueProtect(JSContextRef ctx, JSValueRef value)
254 ExecState* exec = toJS(ctx);
255 exec->globalData().heap.registerThread();
258 JSValue* jsValue = toJS(value);
262 void JSValueUnprotect(JSContextRef ctx, JSValueRef value)
264 ExecState* exec = toJS(ctx);
265 exec->globalData().heap.registerThread();
268 JSValue* jsValue = toJS(value);
269 gcUnprotect(jsValue);