]>
Commit | Line | Data |
---|---|---|
b37bf2e1 A |
1 | /* |
2 | * Copyright (C) 2006, 2007 Apple 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 | * 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. | |
12 | * | |
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. | |
24 | */ | |
25 | ||
26 | #include "config.h" | |
27 | #include "JSValueRef.h" | |
28 | ||
b37bf2e1 | 29 | #include "APICast.h" |
f9bf01c6 | 30 | #include "APIShims.h" |
b37bf2e1 A |
31 | #include "JSCallbackObject.h" |
32 | ||
9dae56ea | 33 | #include <runtime/JSGlobalObject.h> |
4e4e5a6f | 34 | #include <runtime/JSONObject.h> |
9dae56ea | 35 | #include <runtime/JSString.h> |
4e4e5a6f | 36 | #include <runtime/LiteralParser.h> |
9dae56ea A |
37 | #include <runtime/Operations.h> |
38 | #include <runtime/Protect.h> | |
39 | #include <runtime/UString.h> | |
40 | #include <runtime/JSValue.h> | |
b37bf2e1 A |
41 | |
42 | #include <wtf/Assertions.h> | |
4e4e5a6f | 43 | #include <wtf/text/StringHash.h> |
b37bf2e1 A |
44 | |
45 | #include <algorithm> // for std::min | |
46 | ||
f9bf01c6 A |
47 | using namespace JSC; |
48 | ||
49 | ::JSType JSValueGetType(JSContextRef ctx, JSValueRef value) | |
b37bf2e1 | 50 | { |
f9bf01c6 A |
51 | ExecState* exec = toJS(ctx); |
52 | APIEntryShim entryShim(exec); | |
ba379fdc | 53 | |
f9bf01c6 | 54 | JSValue jsValue = toJS(exec, value); |
ba379fdc | 55 | |
9dae56ea A |
56 | if (jsValue.isUndefined()) |
57 | return kJSTypeUndefined; | |
58 | if (jsValue.isNull()) | |
59 | return kJSTypeNull; | |
60 | if (jsValue.isBoolean()) | |
61 | return kJSTypeBoolean; | |
62 | if (jsValue.isNumber()) | |
63 | return kJSTypeNumber; | |
64 | if (jsValue.isString()) | |
65 | return kJSTypeString; | |
66 | ASSERT(jsValue.isObject()); | |
67 | return kJSTypeObject; | |
b37bf2e1 A |
68 | } |
69 | ||
ba379fdc | 70 | bool JSValueIsUndefined(JSContextRef ctx, JSValueRef value) |
b37bf2e1 | 71 | { |
ba379fdc | 72 | ExecState* exec = toJS(ctx); |
f9bf01c6 | 73 | APIEntryShim entryShim(exec); |
ba379fdc A |
74 | |
75 | JSValue jsValue = toJS(exec, value); | |
9dae56ea | 76 | return jsValue.isUndefined(); |
b37bf2e1 A |
77 | } |
78 | ||
ba379fdc | 79 | bool JSValueIsNull(JSContextRef ctx, JSValueRef value) |
b37bf2e1 | 80 | { |
ba379fdc | 81 | ExecState* exec = toJS(ctx); |
f9bf01c6 | 82 | APIEntryShim entryShim(exec); |
ba379fdc A |
83 | |
84 | JSValue jsValue = toJS(exec, value); | |
9dae56ea | 85 | return jsValue.isNull(); |
b37bf2e1 A |
86 | } |
87 | ||
ba379fdc | 88 | bool JSValueIsBoolean(JSContextRef ctx, JSValueRef value) |
b37bf2e1 | 89 | { |
ba379fdc | 90 | ExecState* exec = toJS(ctx); |
f9bf01c6 | 91 | APIEntryShim entryShim(exec); |
ba379fdc A |
92 | |
93 | JSValue jsValue = toJS(exec, value); | |
9dae56ea | 94 | return jsValue.isBoolean(); |
b37bf2e1 A |
95 | } |
96 | ||
ba379fdc | 97 | bool JSValueIsNumber(JSContextRef ctx, JSValueRef value) |
b37bf2e1 | 98 | { |
ba379fdc | 99 | ExecState* exec = toJS(ctx); |
f9bf01c6 | 100 | APIEntryShim entryShim(exec); |
ba379fdc A |
101 | |
102 | JSValue jsValue = toJS(exec, value); | |
9dae56ea | 103 | return jsValue.isNumber(); |
b37bf2e1 A |
104 | } |
105 | ||
ba379fdc | 106 | bool JSValueIsString(JSContextRef ctx, JSValueRef value) |
b37bf2e1 | 107 | { |
ba379fdc | 108 | ExecState* exec = toJS(ctx); |
f9bf01c6 | 109 | APIEntryShim entryShim(exec); |
ba379fdc A |
110 | |
111 | JSValue jsValue = toJS(exec, value); | |
9dae56ea | 112 | return jsValue.isString(); |
b37bf2e1 A |
113 | } |
114 | ||
ba379fdc | 115 | bool JSValueIsObject(JSContextRef ctx, JSValueRef value) |
b37bf2e1 | 116 | { |
ba379fdc | 117 | ExecState* exec = toJS(ctx); |
f9bf01c6 | 118 | APIEntryShim entryShim(exec); |
ba379fdc A |
119 | |
120 | JSValue jsValue = toJS(exec, value); | |
9dae56ea | 121 | return jsValue.isObject(); |
b37bf2e1 A |
122 | } |
123 | ||
ba379fdc | 124 | bool JSValueIsObjectOfClass(JSContextRef ctx, JSValueRef value, JSClassRef jsClass) |
b37bf2e1 | 125 | { |
ba379fdc | 126 | ExecState* exec = toJS(ctx); |
f9bf01c6 | 127 | APIEntryShim entryShim(exec); |
ba379fdc A |
128 | |
129 | JSValue jsValue = toJS(exec, value); | |
b37bf2e1 | 130 | |
9dae56ea | 131 | if (JSObject* o = jsValue.getObject()) { |
b37bf2e1 A |
132 | if (o->inherits(&JSCallbackObject<JSGlobalObject>::info)) |
133 | return static_cast<JSCallbackObject<JSGlobalObject>*>(o)->inherits(jsClass); | |
134 | else if (o->inherits(&JSCallbackObject<JSObject>::info)) | |
135 | return static_cast<JSCallbackObject<JSObject>*>(o)->inherits(jsClass); | |
136 | } | |
137 | return false; | |
138 | } | |
139 | ||
140 | bool JSValueIsEqual(JSContextRef ctx, JSValueRef a, JSValueRef b, JSValueRef* exception) | |
141 | { | |
b37bf2e1 | 142 | ExecState* exec = toJS(ctx); |
f9bf01c6 | 143 | APIEntryShim entryShim(exec); |
9dae56ea | 144 | |
ba379fdc A |
145 | JSValue jsA = toJS(exec, a); |
146 | JSValue jsB = toJS(exec, b); | |
b37bf2e1 | 147 | |
ba379fdc | 148 | bool result = JSValue::equal(exec, jsA, jsB); // false if an exception is thrown |
b37bf2e1 A |
149 | if (exec->hadException()) { |
150 | if (exception) | |
ba379fdc | 151 | *exception = toRef(exec, exec->exception()); |
b37bf2e1 A |
152 | exec->clearException(); |
153 | } | |
154 | return result; | |
155 | } | |
156 | ||
ba379fdc | 157 | bool JSValueIsStrictEqual(JSContextRef ctx, JSValueRef a, JSValueRef b) |
b37bf2e1 | 158 | { |
ba379fdc | 159 | ExecState* exec = toJS(ctx); |
f9bf01c6 | 160 | APIEntryShim entryShim(exec); |
ba379fdc A |
161 | |
162 | JSValue jsA = toJS(exec, a); | |
163 | JSValue jsB = toJS(exec, b); | |
164 | ||
f9bf01c6 | 165 | return JSValue::strictEqual(exec, jsA, jsB); |
b37bf2e1 A |
166 | } |
167 | ||
168 | bool JSValueIsInstanceOfConstructor(JSContextRef ctx, JSValueRef value, JSObjectRef constructor, JSValueRef* exception) | |
169 | { | |
b37bf2e1 | 170 | ExecState* exec = toJS(ctx); |
f9bf01c6 | 171 | APIEntryShim entryShim(exec); |
9dae56ea | 172 | |
ba379fdc A |
173 | JSValue jsValue = toJS(exec, value); |
174 | ||
b37bf2e1 | 175 | JSObject* jsConstructor = toJS(constructor); |
9dae56ea | 176 | if (!jsConstructor->structure()->typeInfo().implementsHasInstance()) |
b37bf2e1 | 177 | return false; |
9dae56ea | 178 | bool result = jsConstructor->hasInstance(exec, jsValue, jsConstructor->get(exec, exec->propertyNames().prototype)); // false if an exception is thrown |
b37bf2e1 A |
179 | if (exec->hadException()) { |
180 | if (exception) | |
ba379fdc | 181 | *exception = toRef(exec, exec->exception()); |
b37bf2e1 A |
182 | exec->clearException(); |
183 | } | |
184 | return result; | |
185 | } | |
186 | ||
ba379fdc | 187 | JSValueRef JSValueMakeUndefined(JSContextRef ctx) |
b37bf2e1 | 188 | { |
ba379fdc | 189 | ExecState* exec = toJS(ctx); |
f9bf01c6 | 190 | APIEntryShim entryShim(exec); |
ba379fdc A |
191 | |
192 | return toRef(exec, jsUndefined()); | |
b37bf2e1 A |
193 | } |
194 | ||
ba379fdc | 195 | JSValueRef JSValueMakeNull(JSContextRef ctx) |
b37bf2e1 | 196 | { |
ba379fdc | 197 | ExecState* exec = toJS(ctx); |
f9bf01c6 | 198 | APIEntryShim entryShim(exec); |
ba379fdc A |
199 | |
200 | return toRef(exec, jsNull()); | |
b37bf2e1 A |
201 | } |
202 | ||
ba379fdc | 203 | JSValueRef JSValueMakeBoolean(JSContextRef ctx, bool value) |
b37bf2e1 | 204 | { |
ba379fdc | 205 | ExecState* exec = toJS(ctx); |
f9bf01c6 | 206 | APIEntryShim entryShim(exec); |
ba379fdc A |
207 | |
208 | return toRef(exec, jsBoolean(value)); | |
b37bf2e1 A |
209 | } |
210 | ||
9dae56ea | 211 | JSValueRef JSValueMakeNumber(JSContextRef ctx, double value) |
b37bf2e1 | 212 | { |
9dae56ea | 213 | ExecState* exec = toJS(ctx); |
f9bf01c6 | 214 | APIEntryShim entryShim(exec); |
9dae56ea | 215 | |
4e4e5a6f A |
216 | // Our JSValue representation relies on a standard bit pattern for NaN. NaNs |
217 | // generated internally to JavaScriptCore naturally have that representation, | |
218 | // but an external NaN might not. | |
219 | if (isnan(value)) | |
220 | value = NaN; | |
221 | ||
ba379fdc | 222 | return toRef(exec, jsNumber(exec, value)); |
b37bf2e1 A |
223 | } |
224 | ||
9dae56ea | 225 | JSValueRef JSValueMakeString(JSContextRef ctx, JSStringRef string) |
b37bf2e1 | 226 | { |
9dae56ea | 227 | ExecState* exec = toJS(ctx); |
f9bf01c6 | 228 | APIEntryShim entryShim(exec); |
9dae56ea | 229 | |
ba379fdc | 230 | return toRef(exec, jsString(exec, string->ustring())); |
b37bf2e1 A |
231 | } |
232 | ||
4e4e5a6f A |
233 | JSValueRef JSValueMakeFromJSONString(JSContextRef ctx, JSStringRef string) |
234 | { | |
235 | ExecState* exec = toJS(ctx); | |
236 | APIEntryShim entryShim(exec); | |
237 | LiteralParser parser(exec, string->ustring(), LiteralParser::StrictJSON); | |
238 | return toRef(exec, parser.tryLiteralParse()); | |
239 | } | |
240 | ||
241 | JSStringRef JSValueCreateJSONString(JSContextRef ctx, JSValueRef apiValue, unsigned indent, JSValueRef* exception) | |
242 | { | |
243 | ExecState* exec = toJS(ctx); | |
244 | APIEntryShim entryShim(exec); | |
245 | JSValue value = toJS(exec, apiValue); | |
246 | UString result = JSONStringify(exec, value, indent); | |
247 | if (exception) | |
248 | *exception = 0; | |
249 | if (exec->hadException()) { | |
250 | if (exception) | |
251 | *exception = toRef(exec, exec->exception()); | |
252 | exec->clearException(); | |
253 | return 0; | |
254 | } | |
255 | return OpaqueJSString::create(result).releaseRef(); | |
256 | } | |
257 | ||
b37bf2e1 A |
258 | bool JSValueToBoolean(JSContextRef ctx, JSValueRef value) |
259 | { | |
260 | ExecState* exec = toJS(ctx); | |
f9bf01c6 | 261 | APIEntryShim entryShim(exec); |
ba379fdc A |
262 | |
263 | JSValue jsValue = toJS(exec, value); | |
9dae56ea | 264 | return jsValue.toBoolean(exec); |
b37bf2e1 A |
265 | } |
266 | ||
267 | double JSValueToNumber(JSContextRef ctx, JSValueRef value, JSValueRef* exception) | |
268 | { | |
b37bf2e1 | 269 | ExecState* exec = toJS(ctx); |
f9bf01c6 | 270 | APIEntryShim entryShim(exec); |
b37bf2e1 | 271 | |
ba379fdc | 272 | JSValue jsValue = toJS(exec, value); |
9dae56ea A |
273 | |
274 | double number = jsValue.toNumber(exec); | |
b37bf2e1 A |
275 | if (exec->hadException()) { |
276 | if (exception) | |
ba379fdc | 277 | *exception = toRef(exec, exec->exception()); |
b37bf2e1 A |
278 | exec->clearException(); |
279 | number = NaN; | |
280 | } | |
281 | return number; | |
282 | } | |
283 | ||
284 | JSStringRef JSValueToStringCopy(JSContextRef ctx, JSValueRef value, JSValueRef* exception) | |
285 | { | |
b37bf2e1 | 286 | ExecState* exec = toJS(ctx); |
f9bf01c6 | 287 | APIEntryShim entryShim(exec); |
9dae56ea | 288 | |
ba379fdc | 289 | JSValue jsValue = toJS(exec, value); |
b37bf2e1 | 290 | |
9dae56ea | 291 | RefPtr<OpaqueJSString> stringRef(OpaqueJSString::create(jsValue.toString(exec))); |
b37bf2e1 A |
292 | if (exec->hadException()) { |
293 | if (exception) | |
ba379fdc | 294 | *exception = toRef(exec, exec->exception()); |
b37bf2e1 | 295 | exec->clearException(); |
9dae56ea | 296 | stringRef.clear(); |
b37bf2e1 | 297 | } |
9dae56ea | 298 | return stringRef.release().releaseRef(); |
b37bf2e1 A |
299 | } |
300 | ||
301 | JSObjectRef JSValueToObject(JSContextRef ctx, JSValueRef value, JSValueRef* exception) | |
302 | { | |
b37bf2e1 | 303 | ExecState* exec = toJS(ctx); |
f9bf01c6 | 304 | APIEntryShim entryShim(exec); |
9dae56ea | 305 | |
ba379fdc | 306 | JSValue jsValue = toJS(exec, value); |
b37bf2e1 | 307 | |
9dae56ea | 308 | JSObjectRef objectRef = toRef(jsValue.toObject(exec)); |
b37bf2e1 A |
309 | if (exec->hadException()) { |
310 | if (exception) | |
ba379fdc | 311 | *exception = toRef(exec, exec->exception()); |
b37bf2e1 A |
312 | exec->clearException(); |
313 | objectRef = 0; | |
314 | } | |
315 | return objectRef; | |
316 | } | |
317 | ||
9dae56ea | 318 | void JSValueProtect(JSContextRef ctx, JSValueRef value) |
b37bf2e1 | 319 | { |
9dae56ea | 320 | ExecState* exec = toJS(ctx); |
f9bf01c6 | 321 | APIEntryShim entryShim(exec); |
9dae56ea | 322 | |
f9bf01c6 | 323 | JSValue jsValue = toJSForGC(exec, value); |
b37bf2e1 A |
324 | gcProtect(jsValue); |
325 | } | |
326 | ||
9dae56ea | 327 | void JSValueUnprotect(JSContextRef ctx, JSValueRef value) |
b37bf2e1 | 328 | { |
9dae56ea | 329 | ExecState* exec = toJS(ctx); |
f9bf01c6 | 330 | APIEntryShim entryShim(exec); |
9dae56ea | 331 | |
f9bf01c6 | 332 | JSValue jsValue = toJSForGC(exec, value); |
b37bf2e1 A |
333 | gcUnprotect(jsValue); |
334 | } |