1 | // -*- mode: c++; c-basic-offset: 4 -*-
|
---|
2 | /*
|
---|
3 | * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
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 | *
|
---|
14 | * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
|
---|
15 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
---|
17 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
|
---|
18 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
---|
19 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
---|
20 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
---|
21 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
---|
22 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
24 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
25 | */
|
---|
26 |
|
---|
27 | #include "config.h"
|
---|
28 | #include "JSValueRef.h"
|
---|
29 |
|
---|
30 | #include <wtf/Platform.h>
|
---|
31 | #include "APICast.h"
|
---|
32 | #include "JSCallbackObject.h"
|
---|
33 |
|
---|
34 | #include <kjs/JSType.h>
|
---|
35 | #include <kjs/JSGlobalObject.h>
|
---|
36 | #include <kjs/internal.h>
|
---|
37 | #include <kjs/operations.h>
|
---|
38 | #include <kjs/protect.h>
|
---|
39 | #include <kjs/ustring.h>
|
---|
40 | #include <kjs/value.h>
|
---|
41 |
|
---|
42 | #include <wtf/Assertions.h>
|
---|
43 |
|
---|
44 | #include <algorithm> // for std::min
|
---|
45 |
|
---|
46 | JSType JSValueGetType(JSContextRef, JSValueRef value)
|
---|
47 | {
|
---|
48 | KJS::JSValue* jsValue = toJS(value);
|
---|
49 | switch (jsValue->type()) {
|
---|
50 | case KJS::UndefinedType:
|
---|
51 | return kJSTypeUndefined;
|
---|
52 | case KJS::NullType:
|
---|
53 | return kJSTypeNull;
|
---|
54 | case KJS::BooleanType:
|
---|
55 | return kJSTypeBoolean;
|
---|
56 | case KJS::NumberType:
|
---|
57 | return kJSTypeNumber;
|
---|
58 | case KJS::StringType:
|
---|
59 | return kJSTypeString;
|
---|
60 | case KJS::ObjectType:
|
---|
61 | return kJSTypeObject;
|
---|
62 | default:
|
---|
63 | ASSERT(!"JSValueGetType: unknown type code.\n");
|
---|
64 | return kJSTypeUndefined;
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | using namespace KJS; // placed here to avoid conflict between KJS::JSType and JSType, above.
|
---|
69 |
|
---|
70 | bool JSValueIsUndefined(JSContextRef, JSValueRef value)
|
---|
71 | {
|
---|
72 | JSValue* jsValue = toJS(value);
|
---|
73 | return jsValue->isUndefined();
|
---|
74 | }
|
---|
75 |
|
---|
76 | bool JSValueIsNull(JSContextRef, JSValueRef value)
|
---|
77 | {
|
---|
78 | JSValue* jsValue = toJS(value);
|
---|
79 | return jsValue->isNull();
|
---|
80 | }
|
---|
81 |
|
---|
82 | bool JSValueIsBoolean(JSContextRef, JSValueRef value)
|
---|
83 | {
|
---|
84 | JSValue* jsValue = toJS(value);
|
---|
85 | return jsValue->isBoolean();
|
---|
86 | }
|
---|
87 |
|
---|
88 | bool JSValueIsNumber(JSContextRef, JSValueRef value)
|
---|
89 | {
|
---|
90 | JSValue* jsValue = toJS(value);
|
---|
91 | return jsValue->isNumber();
|
---|
92 | }
|
---|
93 |
|
---|
94 | bool JSValueIsString(JSContextRef, JSValueRef value)
|
---|
95 | {
|
---|
96 | JSValue* jsValue = toJS(value);
|
---|
97 | return jsValue->isString();
|
---|
98 | }
|
---|
99 |
|
---|
100 | bool JSValueIsObject(JSContextRef, JSValueRef value)
|
---|
101 | {
|
---|
102 | JSValue* jsValue = toJS(value);
|
---|
103 | return jsValue->isObject();
|
---|
104 | }
|
---|
105 |
|
---|
106 | bool JSValueIsObjectOfClass(JSContextRef, JSValueRef value, JSClassRef jsClass)
|
---|
107 | {
|
---|
108 | JSValue* jsValue = toJS(value);
|
---|
109 |
|
---|
110 | if (JSObject* o = jsValue->getObject()) {
|
---|
111 | if (o->inherits(&JSCallbackObject<JSGlobalObject>::info))
|
---|
112 | return static_cast<JSCallbackObject<JSGlobalObject>*>(o)->inherits(jsClass);
|
---|
113 | else if (o->inherits(&JSCallbackObject<JSObject>::info))
|
---|
114 | return static_cast<JSCallbackObject<JSObject>*>(o)->inherits(jsClass);
|
---|
115 | }
|
---|
116 | return false;
|
---|
117 | }
|
---|
118 |
|
---|
119 | bool JSValueIsEqual(JSContextRef ctx, JSValueRef a, JSValueRef b, JSValueRef* exception)
|
---|
120 | {
|
---|
121 | JSLock lock;
|
---|
122 | ExecState* exec = toJS(ctx);
|
---|
123 | JSValue* jsA = toJS(a);
|
---|
124 | JSValue* jsB = toJS(b);
|
---|
125 |
|
---|
126 | bool result = equal(exec, jsA, jsB); // false if an exception is thrown
|
---|
127 | if (exec->hadException()) {
|
---|
128 | if (exception)
|
---|
129 | *exception = toRef(exec->exception());
|
---|
130 | exec->clearException();
|
---|
131 | }
|
---|
132 | return result;
|
---|
133 | }
|
---|
134 |
|
---|
135 | bool JSValueIsStrictEqual(JSContextRef ctx, JSValueRef a, JSValueRef b)
|
---|
136 | {
|
---|
137 | UNUSED_PARAM(ctx);
|
---|
138 |
|
---|
139 | JSLock lock;
|
---|
140 | JSValue* jsA = toJS(a);
|
---|
141 | JSValue* jsB = toJS(b);
|
---|
142 |
|
---|
143 | bool result = strictEqual(jsA, jsB);
|
---|
144 | return result;
|
---|
145 | }
|
---|
146 |
|
---|
147 | bool JSValueIsInstanceOfConstructor(JSContextRef ctx, JSValueRef value, JSObjectRef constructor, JSValueRef* exception)
|
---|
148 | {
|
---|
149 | JSLock lock;
|
---|
150 | ExecState* exec = toJS(ctx);
|
---|
151 | JSValue* jsValue = toJS(value);
|
---|
152 | JSObject* jsConstructor = toJS(constructor);
|
---|
153 | if (!jsConstructor->implementsHasInstance())
|
---|
154 | return false;
|
---|
155 | bool result = jsConstructor->hasInstance(exec, jsValue); // false if an exception is thrown
|
---|
156 | if (exec->hadException()) {
|
---|
157 | if (exception)
|
---|
158 | *exception = toRef(exec->exception());
|
---|
159 | exec->clearException();
|
---|
160 | }
|
---|
161 | return result;
|
---|
162 | }
|
---|
163 |
|
---|
164 | JSValueRef JSValueMakeUndefined(JSContextRef)
|
---|
165 | {
|
---|
166 | return toRef(jsUndefined());
|
---|
167 | }
|
---|
168 |
|
---|
169 | JSValueRef JSValueMakeNull(JSContextRef)
|
---|
170 | {
|
---|
171 | return toRef(jsNull());
|
---|
172 | }
|
---|
173 |
|
---|
174 | JSValueRef JSValueMakeBoolean(JSContextRef, bool value)
|
---|
175 | {
|
---|
176 | return toRef(jsBoolean(value));
|
---|
177 | }
|
---|
178 |
|
---|
179 | JSValueRef JSValueMakeNumber(JSContextRef, double value)
|
---|
180 | {
|
---|
181 | JSLock lock;
|
---|
182 | return toRef(jsNumber(value));
|
---|
183 | }
|
---|
184 |
|
---|
185 | JSValueRef JSValueMakeString(JSContextRef, JSStringRef string)
|
---|
186 | {
|
---|
187 | JSLock lock;
|
---|
188 | UString::Rep* rep = toJS(string);
|
---|
189 | return toRef(jsString(UString(rep)));
|
---|
190 | }
|
---|
191 |
|
---|
192 | bool JSValueToBoolean(JSContextRef ctx, JSValueRef value)
|
---|
193 | {
|
---|
194 | ExecState* exec = toJS(ctx);
|
---|
195 | JSValue* jsValue = toJS(value);
|
---|
196 | return jsValue->toBoolean(exec);
|
---|
197 | }
|
---|
198 |
|
---|
199 | double JSValueToNumber(JSContextRef ctx, JSValueRef value, JSValueRef* exception)
|
---|
200 | {
|
---|
201 | JSLock lock;
|
---|
202 | JSValue* jsValue = toJS(value);
|
---|
203 | ExecState* exec = toJS(ctx);
|
---|
204 |
|
---|
205 | double number = jsValue->toNumber(exec);
|
---|
206 | if (exec->hadException()) {
|
---|
207 | if (exception)
|
---|
208 | *exception = toRef(exec->exception());
|
---|
209 | exec->clearException();
|
---|
210 | number = NaN;
|
---|
211 | }
|
---|
212 | return number;
|
---|
213 | }
|
---|
214 |
|
---|
215 | JSStringRef JSValueToStringCopy(JSContextRef ctx, JSValueRef value, JSValueRef* exception)
|
---|
216 | {
|
---|
217 | JSLock lock;
|
---|
218 | JSValue* jsValue = toJS(value);
|
---|
219 | ExecState* exec = toJS(ctx);
|
---|
220 |
|
---|
221 | JSStringRef stringRef = toRef(jsValue->toString(exec).rep()->ref());
|
---|
222 | if (exec->hadException()) {
|
---|
223 | if (exception)
|
---|
224 | *exception = toRef(exec->exception());
|
---|
225 | exec->clearException();
|
---|
226 | stringRef = 0;
|
---|
227 | }
|
---|
228 | return stringRef;
|
---|
229 | }
|
---|
230 |
|
---|
231 | JSObjectRef JSValueToObject(JSContextRef ctx, JSValueRef value, JSValueRef* exception)
|
---|
232 | {
|
---|
233 | JSLock lock;
|
---|
234 | ExecState* exec = toJS(ctx);
|
---|
235 | JSValue* jsValue = toJS(value);
|
---|
236 |
|
---|
237 | JSObjectRef objectRef = toRef(jsValue->toObject(exec));
|
---|
238 | if (exec->hadException()) {
|
---|
239 | if (exception)
|
---|
240 | *exception = toRef(exec->exception());
|
---|
241 | exec->clearException();
|
---|
242 | objectRef = 0;
|
---|
243 | }
|
---|
244 | return objectRef;
|
---|
245 | }
|
---|
246 |
|
---|
247 | void JSValueProtect(JSContextRef, JSValueRef value)
|
---|
248 | {
|
---|
249 | JSLock lock;
|
---|
250 | JSValue* jsValue = toJS(value);
|
---|
251 | gcProtect(jsValue);
|
---|
252 | }
|
---|
253 |
|
---|
254 | void JSValueUnprotect(JSContextRef, JSValueRef value)
|
---|
255 | {
|
---|
256 | JSLock lock;
|
---|
257 | JSValue* jsValue = toJS(value);
|
---|
258 | gcUnprotect(jsValue);
|
---|
259 | }
|
---|