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

Last change on this file since 16089 was 15481, checked in by mjs, 19 years ago

Reviewed by Geoff.


  • add a JSContextRef parameter to all JSValueRef, JSObjectRef, and JSContextRef operations; except JSObject{Get,Set}PrivateData which can be assumed to be simple pure accessors.


Also renamed the parameter "context" to "ctx" because it makes the code read better with this pervasive
but usually uninteresting parameter.

  • API/JSBase.cpp: (JSEvaluateScript): (JSCheckScriptSyntax): (JSGarbageCollect):
  • API/JSBase.h:
  • API/JSCallbackObject.cpp: (KJS::JSCallbackObject::JSCallbackObject): (KJS::JSCallbackObject::init): (KJS::JSCallbackObject::getOwnPropertySlot): (KJS::JSCallbackObject::put): (KJS::JSCallbackObject::deleteProperty): (KJS::JSCallbackObject::toNumber): (KJS::JSCallbackObject::toString):
  • API/JSContextRef.cpp: (JSGlobalContextCreate): (JSGlobalContextRetain): (JSGlobalContextRelease): (JSContextGetGlobalObject):
  • API/JSContextRef.h:
  • API/JSNode.c: (JSNodePrototype_appendChild): (JSNodePrototype_removeChild): (JSNodePrototype_replaceChild): (JSNode_getNodeType): (JSNode_getFirstChild): (JSNode_prototype):
  • API/JSNodeList.c: (JSNodeListPrototype_item): (JSNodeList_length): (JSNodeList_getProperty): (JSNodeList_prototype):
  • API/JSObjectRef.cpp: (JSObjectMake): (JSObjectMakeFunctionWithCallback): (JSObjectMakeConstructor): (JSObjectMakeFunction): (JSObjectGetPrototype): (JSObjectSetPrototype): (JSObjectHasProperty): (JSObjectGetProperty): (JSObjectSetProperty): (JSObjectGetPropertyAtIndex): (JSObjectSetPropertyAtIndex): (JSObjectDeleteProperty): (JSObjectIsFunction): (JSObjectCallAsFunction): (JSObjectIsConstructor): (JSObjectCallAsConstructor): (JSObjectCopyPropertyNames):
  • API/JSObjectRef.h:
  • API/JSStringRef.cpp:
  • API/JSValueRef.cpp: (JSValueGetType): (JSValueIsUndefined): (JSValueIsNull): (JSValueIsBoolean): (JSValueIsNumber): (JSValueIsString): (JSValueIsObject): (JSValueIsObjectOfClass): (JSValueIsEqual): (JSValueIsStrictEqual): (JSValueIsInstanceOfConstructor): (JSValueMakeUndefined): (JSValueMakeNull): (JSValueMakeBoolean): (JSValueMakeNumber): (JSValueMakeString): (JSValueToBoolean): (JSValueToNumber): (JSValueToStringCopy): (JSValueToObject): (JSValueProtect): (JSValueUnprotect):
  • API/JSValueRef.h:
  • API/minidom.c: (print):
  • API/testapi.c: (MyObject_getProperty): (MyObject_deleteProperty): (MyObject_callAsFunction): (MyObject_callAsConstructor): (MyObject_convertToType): (print_callAsFunction): (main):
File size: 7.0 KB
Line 
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
42JSType JSValueGetType(JSContextRef, 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
64using namespace KJS; // placed here to avoid conflict between KJS::JSType and JSType, above.
65
66bool JSValueIsUndefined(JSContextRef, JSValueRef value)
67{
68 JSValue* jsValue = toJS(value);
69 return jsValue->isUndefined();
70}
71
72bool JSValueIsNull(JSContextRef, JSValueRef value)
73{
74 JSValue* jsValue = toJS(value);
75 return jsValue->isNull();
76}
77
78bool JSValueIsBoolean(JSContextRef, JSValueRef value)
79{
80 JSValue* jsValue = toJS(value);
81 return jsValue->isBoolean();
82}
83
84bool JSValueIsNumber(JSContextRef, JSValueRef value)
85{
86 JSValue* jsValue = toJS(value);
87 return jsValue->isNumber();
88}
89
90bool JSValueIsString(JSContextRef, JSValueRef value)
91{
92 JSValue* jsValue = toJS(value);
93 return jsValue->isString();
94}
95
96bool JSValueIsObject(JSContextRef, JSValueRef value)
97{
98 JSValue* jsValue = toJS(value);
99 return jsValue->isObject();
100}
101
102bool JSValueIsObjectOfClass(JSContextRef, 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
112bool JSValueIsEqual(JSContextRef ctx, JSValueRef a, JSValueRef b, JSValueRef* exception)
113{
114 JSLock lock;
115 ExecState* exec = toJS(ctx);
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
128bool JSValueIsStrictEqual(JSContextRef ctx, JSValueRef a, JSValueRef b)
129{
130 JSLock lock;
131 ExecState* exec = toJS(ctx);
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
140bool JSValueIsInstanceOfConstructor(JSContextRef ctx, JSValueRef value, JSObjectRef constructor, JSValueRef* exception)
141{
142 ExecState* exec = toJS(ctx);
143 JSValue* jsValue = toJS(value);
144 JSObject* jsConstructor = toJS(constructor);
145 if (!jsConstructor->implementsHasInstance())
146 return false;
147 bool result = jsConstructor->hasInstance(exec, jsValue); // false if an exception is thrown
148 if (exec->hadException()) {
149 if (exception)
150 *exception = toRef(exec->exception());
151 exec->clearException();
152 }
153 return result;
154}
155
156JSValueRef JSValueMakeUndefined(JSContextRef)
157{
158 return toRef(jsUndefined());
159}
160
161JSValueRef JSValueMakeNull(JSContextRef)
162{
163 return toRef(jsNull());
164}
165
166JSValueRef JSValueMakeBoolean(JSContextRef, bool value)
167{
168 return toRef(jsBoolean(value));
169}
170
171JSValueRef JSValueMakeNumber(JSContextRef, double value)
172{
173 JSLock lock;
174 return toRef(jsNumber(value));
175}
176
177JSValueRef JSValueMakeString(JSContextRef, JSStringRef string)
178{
179 JSLock lock;
180 UString::Rep* rep = toJS(string);
181 return toRef(jsString(UString(rep)));
182}
183
184bool JSValueToBoolean(JSContextRef ctx, JSValueRef value)
185{
186 ExecState* exec = toJS(ctx);
187 JSValue* jsValue = toJS(value);
188 return jsValue->toBoolean(exec);
189}
190
191double JSValueToNumber(JSContextRef ctx, JSValueRef value, JSValueRef* exception)
192{
193 JSLock lock;
194 JSValue* jsValue = toJS(value);
195 ExecState* exec = toJS(ctx);
196
197 double number = jsValue->toNumber(exec);
198 if (exec->hadException()) {
199 if (exception)
200 *exception = toRef(exec->exception());
201 exec->clearException();
202 number = NaN;
203 }
204 return number;
205}
206
207JSStringRef JSValueToStringCopy(JSContextRef ctx, JSValueRef value, JSValueRef* exception)
208{
209 JSLock lock;
210 JSValue* jsValue = toJS(value);
211 ExecState* exec = toJS(ctx);
212
213 JSStringRef stringRef = toRef(jsValue->toString(exec).rep()->ref());
214 if (exec->hadException()) {
215 if (exception)
216 *exception = toRef(exec->exception());
217 exec->clearException();
218 stringRef = 0;
219 }
220 return stringRef;
221}
222
223JSObjectRef JSValueToObject(JSContextRef ctx, JSValueRef value, JSValueRef* exception)
224{
225 JSLock lock;
226 ExecState* exec = toJS(ctx);
227 JSValue* jsValue = toJS(value);
228
229 JSObjectRef objectRef = toRef(jsValue->toObject(exec));
230 if (exec->hadException()) {
231 if (exception)
232 *exception = toRef(exec->exception());
233 exec->clearException();
234 objectRef = 0;
235 }
236 return objectRef;
237}
238
239void JSValueProtect(JSContextRef, JSValueRef value)
240{
241 JSLock lock;
242 JSValue* jsValue = toJS(value);
243 gcProtect(jsValue);
244}
245
246void JSValueUnprotect(JSContextRef, JSValueRef value)
247{
248 JSLock lock;
249 JSValue* jsValue = toJS(value);
250 gcUnprotect(jsValue);
251}
Note: See TracBrowser for help on using the repository browser.