1 | // -*- mode: c++; c-basic-offset: 4 -*-
|
---|
2 | /*
|
---|
3 | * Copyright (C) 2006 Apple Computer, 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 "APICast.h"
|
---|
28 | #include "JSCallbackObject.h"
|
---|
29 | #include "JSValueRef.h"
|
---|
30 |
|
---|
31 | #include <kjs/JSType.h>
|
---|
32 | #include <kjs/internal.h>
|
---|
33 | #include <kjs/operations.h>
|
---|
34 | #include <kjs/protect.h>
|
---|
35 | #include <kjs/ustring.h>
|
---|
36 | #include <kjs/value.h>
|
---|
37 |
|
---|
38 | #include <wtf/Assertions.h>
|
---|
39 |
|
---|
40 | #include <algorithm> // for std::min
|
---|
41 |
|
---|
42 | JSType JSValueGetType(JSValueRef value)
|
---|
43 | {
|
---|
44 | KJS::JSValue* jsValue = toJS(value);
|
---|
45 | switch (jsValue->type()) {
|
---|
46 | case KJS::UndefinedType:
|
---|
47 | return kJSTypeUndefined;
|
---|
48 | case KJS::NullType:
|
---|
49 | return kJSTypeNull;
|
---|
50 | case KJS::BooleanType:
|
---|
51 | return kJSTypeBoolean;
|
---|
52 | case KJS::NumberType:
|
---|
53 | return kJSTypeNumber;
|
---|
54 | case KJS::StringType:
|
---|
55 | return kJSTypeString;
|
---|
56 | case KJS::ObjectType:
|
---|
57 | return kJSTypeObject;
|
---|
58 | default:
|
---|
59 | ASSERT(!"JSValueGetType: unknown type code.\n");
|
---|
60 | return kJSTypeUndefined;
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | using namespace KJS; // placed here to avoid conflict between KJS::JSType and JSType, above.
|
---|
65 |
|
---|
66 | bool JSValueIsUndefined(JSValueRef value)
|
---|
67 | {
|
---|
68 | JSValue* jsValue = toJS(value);
|
---|
69 | return jsValue->isUndefined();
|
---|
70 | }
|
---|
71 |
|
---|
72 | bool JSValueIsNull(JSValueRef value)
|
---|
73 | {
|
---|
74 | JSValue* jsValue = toJS(value);
|
---|
75 | return jsValue->isNull();
|
---|
76 | }
|
---|
77 |
|
---|
78 | bool JSValueIsBoolean(JSValueRef value)
|
---|
79 | {
|
---|
80 | JSValue* jsValue = toJS(value);
|
---|
81 | return jsValue->isBoolean();
|
---|
82 | }
|
---|
83 |
|
---|
84 | bool JSValueIsNumber(JSValueRef value)
|
---|
85 | {
|
---|
86 | JSValue* jsValue = toJS(value);
|
---|
87 | return jsValue->isNumber();
|
---|
88 | }
|
---|
89 |
|
---|
90 | bool JSValueIsString(JSValueRef value)
|
---|
91 | {
|
---|
92 | JSValue* jsValue = toJS(value);
|
---|
93 | return jsValue->isString();
|
---|
94 | }
|
---|
95 |
|
---|
96 | bool JSValueIsObject(JSValueRef value)
|
---|
97 | {
|
---|
98 | JSValue* jsValue = toJS(value);
|
---|
99 | return jsValue->isObject();
|
---|
100 | }
|
---|
101 |
|
---|
102 | bool JSValueIsObjectOfClass(JSValueRef value, JSClassRef jsClass)
|
---|
103 | {
|
---|
104 | JSValue* jsValue = toJS(value);
|
---|
105 |
|
---|
106 | if (JSObject* o = jsValue->getObject())
|
---|
107 | if (o->inherits(&JSCallbackObject::info))
|
---|
108 | return static_cast<JSCallbackObject*>(o)->inherits(jsClass);
|
---|
109 | return false;
|
---|
110 | }
|
---|
111 |
|
---|
112 | bool JSValueIsEqual(JSContextRef context, JSValueRef a, JSValueRef b, JSValueRef* exception)
|
---|
113 | {
|
---|
114 | JSLock lock;
|
---|
115 | ExecState* exec = toJS(context);
|
---|
116 | JSValue* jsA = toJS(a);
|
---|
117 | JSValue* jsB = toJS(b);
|
---|
118 |
|
---|
119 | bool result = equal(exec, jsA, jsB); // false if an exception is thrown
|
---|
120 | if (exec->hadException()) {
|
---|
121 | if (exception)
|
---|
122 | *exception = toRef(exec->exception());
|
---|
123 | exec->clearException();
|
---|
124 | }
|
---|
125 | return result;
|
---|
126 | }
|
---|
127 |
|
---|
128 | bool JSValueIsStrictEqual(JSContextRef context, JSValueRef a, JSValueRef b)
|
---|
129 | {
|
---|
130 | JSLock lock;
|
---|
131 | ExecState* exec = toJS(context);
|
---|
132 | JSValue* jsA = toJS(a);
|
---|
133 | JSValue* jsB = toJS(b);
|
---|
134 |
|
---|
135 | bool result = strictEqual(exec, jsA, jsB); // can't throw because it doesn't perform value conversion
|
---|
136 | ASSERT(!exec->hadException());
|
---|
137 | return result;
|
---|
138 | }
|
---|
139 |
|
---|
140 | bool JSValueIsInstanceOfConstructor(JSContextRef context, JSValueRef value, JSObjectRef constructor)
|
---|
141 | {
|
---|
142 | ExecState* exec = toJS(context);
|
---|
143 | JSValue* jsValue = toJS(value);
|
---|
144 | JSObject* jsConstructor = toJS(constructor);
|
---|
145 | if (!jsConstructor->implementsHasInstance())
|
---|
146 | return false;
|
---|
147 | bool result = jsConstructor->hasInstance(exec, jsValue);
|
---|
148 | if (exec->hadException())
|
---|
149 | exec->clearException();
|
---|
150 | return result;
|
---|
151 | }
|
---|
152 |
|
---|
153 | JSValueRef JSValueMakeUndefined()
|
---|
154 | {
|
---|
155 | return toRef(jsUndefined());
|
---|
156 | }
|
---|
157 |
|
---|
158 | JSValueRef JSValueMakeNull()
|
---|
159 | {
|
---|
160 | return toRef(jsNull());
|
---|
161 | }
|
---|
162 |
|
---|
163 | JSValueRef JSValueMakeBoolean(bool value)
|
---|
164 | {
|
---|
165 | return toRef(jsBoolean(value));
|
---|
166 | }
|
---|
167 |
|
---|
168 | JSValueRef JSValueMakeNumber(double value)
|
---|
169 | {
|
---|
170 | JSLock lock;
|
---|
171 | return toRef(jsNumber(value));
|
---|
172 | }
|
---|
173 |
|
---|
174 | bool JSValueToBoolean(JSContextRef context, JSValueRef value, JSValueRef* exception)
|
---|
175 | {
|
---|
176 | JSLock lock;
|
---|
177 | ExecState* exec = toJS(context);
|
---|
178 | JSValue* jsValue = toJS(value);
|
---|
179 |
|
---|
180 | bool boolean = jsValue->toBoolean(exec);
|
---|
181 | if (exec->hadException()) {
|
---|
182 | if (exception)
|
---|
183 | *exception = toRef(exec->exception());
|
---|
184 | exec->clearException();
|
---|
185 | boolean = false;
|
---|
186 | }
|
---|
187 | return boolean;
|
---|
188 | }
|
---|
189 |
|
---|
190 | double JSValueToNumber(JSContextRef context, JSValueRef value, JSValueRef* exception)
|
---|
191 | {
|
---|
192 | JSLock lock;
|
---|
193 | JSValue* jsValue = toJS(value);
|
---|
194 | ExecState* exec = toJS(context);
|
---|
195 |
|
---|
196 | double number = jsValue->toNumber(exec);
|
---|
197 | if (exec->hadException()) {
|
---|
198 | if (exception)
|
---|
199 | *exception = toRef(exec->exception());
|
---|
200 | exec->clearException();
|
---|
201 | number = NaN;
|
---|
202 | }
|
---|
203 | return number;
|
---|
204 | }
|
---|
205 |
|
---|
206 | JSStringRef JSValueToStringCopy(JSContextRef context, JSValueRef value, JSValueRef* exception)
|
---|
207 | {
|
---|
208 | JSLock lock;
|
---|
209 | JSValue* jsValue = toJS(value);
|
---|
210 | ExecState* exec = toJS(context);
|
---|
211 |
|
---|
212 | JSStringRef stringRef = toRef(jsValue->toString(exec).rep()->ref());
|
---|
213 | if (exec->hadException()) {
|
---|
214 | if (exception)
|
---|
215 | *exception = toRef(exec->exception());
|
---|
216 | exec->clearException();
|
---|
217 | stringRef = 0;
|
---|
218 | }
|
---|
219 | return stringRef;
|
---|
220 | }
|
---|
221 |
|
---|
222 | JSObjectRef JSValueToObject(JSContextRef context, JSValueRef value, JSValueRef* exception)
|
---|
223 | {
|
---|
224 | JSLock lock;
|
---|
225 | ExecState* exec = toJS(context);
|
---|
226 | JSValue* jsValue = toJS(value);
|
---|
227 |
|
---|
228 | JSObjectRef objectRef = toRef(jsValue->toObject(exec));
|
---|
229 | if (exec->hadException()) {
|
---|
230 | if (exception)
|
---|
231 | *exception = toRef(exec->exception());
|
---|
232 | exec->clearException();
|
---|
233 | objectRef = 0;
|
---|
234 | }
|
---|
235 | return objectRef;
|
---|
236 | }
|
---|
237 |
|
---|
238 | void JSValueProtect(JSValueRef value)
|
---|
239 | {
|
---|
240 | JSLock lock;
|
---|
241 | JSValue* jsValue = toJS(value);
|
---|
242 | gcProtect(jsValue);
|
---|
243 | }
|
---|
244 |
|
---|
245 | void JSValueUnprotect(JSValueRef value)
|
---|
246 | {
|
---|
247 | JSLock lock;
|
---|
248 | JSValue* jsValue = toJS(value);
|
---|
249 | gcUnprotect(jsValue);
|
---|
250 | }
|
---|