source: webkit/trunk/JavaScriptCore/API/JSValueRef.cpp@ 51801

Last change on this file since 51801 was 51801, checked in by [email protected], 15 years ago

https://bugs.webkit.org/show_bug.cgi?id=32184
Handle out-of-memory conditions with JSC Ropes with a JS exception, rather than crashing.
Switch from using fastMalloc to tryFastMalloc, pass an ExecState to record the exception on.

Reviewed by Oliver Hunt.

JavaScriptCore:

  • API/JSCallbackObjectFunctions.h:

(JSC::::toString):

  • API/JSValueRef.cpp:

(JSValueIsStrictEqual):

(JSC::BytecodeGenerator::emitEqualityOp):

  • debugger/DebuggerCallFrame.cpp:

(JSC::DebuggerCallFrame::functionName):
(JSC::DebuggerCallFrame::calculatedFunctionName):

  • interpreter/Interpreter.cpp:

(JSC::Interpreter::callEval):
(JSC::Interpreter::privateExecute):

  • jit/JITStubs.cpp:

(JSC::DEFINE_STUB_FUNCTION):

  • profiler/ProfileGenerator.cpp:

(JSC::ProfileGenerator::addParentForConsoleStart):

  • profiler/Profiler.cpp:

(JSC::Profiler::willExecute):
(JSC::Profiler::didExecute):
(JSC::Profiler::createCallIdentifier):
(JSC::createCallIdentifierFromFunctionImp):

  • profiler/Profiler.h:
  • runtime/ArrayPrototype.cpp:

(JSC::arrayProtoFuncIndexOf):
(JSC::arrayProtoFuncLastIndexOf):

  • runtime/DateConstructor.cpp:

(JSC::constructDate):

  • runtime/FunctionPrototype.cpp:

(JSC::functionProtoFuncToString):

  • runtime/InternalFunction.cpp:

(JSC::InternalFunction::name):
(JSC::InternalFunction::displayName):
(JSC::InternalFunction::calculatedDisplayName):

  • runtime/InternalFunction.h:
  • runtime/JSCell.cpp:

(JSC::JSCell::getString):

  • runtime/JSCell.h:

(JSC::JSValue::getString):

  • runtime/JSONObject.cpp:

(JSC::gap):
(JSC::Stringifier::Stringifier):
(JSC::Stringifier::appendStringifiedValue):

  • runtime/JSObject.cpp:

(JSC::JSObject::putDirectFunction):
(JSC::JSObject::putDirectFunctionWithoutTransition):
(JSC::JSObject::defineOwnProperty):

  • runtime/JSObject.h:
  • runtime/JSPropertyNameIterator.cpp:

(JSC::JSPropertyNameIterator::get):

  • runtime/JSString.cpp:

(JSC::JSString::Rope::~Rope):
(JSC::JSString::resolveRope):
(JSC::JSString::getPrimitiveNumber):
(JSC::JSString::toNumber):
(JSC::JSString::toString):
(JSC::JSString::toThisString):
(JSC::JSString::getStringPropertyDescriptor):

  • runtime/JSString.h:

(JSC::JSString::Rope::createOrNull):
(JSC::JSString::Rope::operator new):
(JSC::JSString::value):
(JSC::JSString::tryGetValue):
(JSC::JSString::getIndex):
(JSC::JSString::getStringPropertySlot):
(JSC::JSValue::toString):

  • runtime/JSValue.h:
  • runtime/NativeErrorConstructor.cpp:

(JSC::NativeErrorConstructor::NativeErrorConstructor):

  • runtime/Operations.cpp:

(JSC::JSValue::strictEqualSlowCase):

  • runtime/Operations.h:

(JSC::JSValue::equalSlowCaseInline):
(JSC::JSValue::strictEqualSlowCaseInline):
(JSC::JSValue::strictEqual):
(JSC::jsLess):
(JSC::jsLessEq):
(JSC::jsAdd):
(JSC::concatenateStrings):

  • runtime/PropertyDescriptor.cpp:

(JSC::PropertyDescriptor::equalTo):

  • runtime/PropertyDescriptor.h:
  • runtime/StringPrototype.cpp:

(JSC::stringProtoFuncReplace):
(JSC::stringProtoFuncToLowerCase):
(JSC::stringProtoFuncToUpperCase):

WebCore:

  • bindings/ScriptControllerBase.cpp:

(WebCore::ScriptController::executeIfJavaScriptURL):

  • bindings/js/JSCanvasRenderingContext2DCustom.cpp:

(WebCore::toHTMLCanvasStyle):
(WebCore::JSCanvasRenderingContext2D::setFillColor):
(WebCore::JSCanvasRenderingContext2D::setStrokeColor):
(WebCore::JSCanvasRenderingContext2D::setShadow):

  • bindings/js/ScriptCallStack.cpp:

(WebCore::ScriptCallStack::ScriptCallStack):
(WebCore::ScriptCallStack::initialize):

  • bindings/js/ScriptValue.cpp:

(WebCore::ScriptValue::getString):

  • bindings/js/ScriptValue.h:
  • bindings/js/SerializedScriptValue.cpp:

(WebCore::SerializingTreeWalker::convertIfTerminal):

  • bindings/objc/WebScriptObject.mm:

(+[WebScriptObject _convertValueToObjcValue:originRootObject:rootObject:]):

  • page/Console.cpp:

(WebCore::Console::addMessage):

WebKit/mac:

  • WebView/WebView.mm:

(aeDescFromJSValue):

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