source: webkit/trunk/JavaScriptCore/API/JSObjectRef.cpp@ 15469

Last change on this file since 15469 was 15469, checked in by ggaren, 19 years ago

Reviewed by Maciej.


  • Added names to functions.


  • Removed GetPrivate/SetPrivate from callbackFunctions and callbackConstructors. The private data idiom is that a JS object stores its native implementation as private data. For functions and constructors, the native implementation is nothing more than the callback they already store, so supporting private data, too, confuses the idiom. If you *really* want, you can still create a custom function with private data.
  • API/JSCallbackConstructor.cpp:
  • API/JSCallbackConstructor.h:
  • API/JSCallbackFunction.cpp: (KJS::JSCallbackFunction::JSCallbackFunction):
  • API/JSCallbackFunction.h:
  • API/JSCallbackObject.cpp: (KJS::JSCallbackObject::staticFunctionGetter):
  • API/JSObjectRef.cpp: (JSObjectMakeFunction): (JSObjectMakeFunctionWithBody): (JSObjectGetPrivate): (JSObjectSetPrivate):
  • API/JSObjectRef.h:
  • API/minidom.c: (main):
  • API/testapi.c: (main):
File size: 10.4 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 "JSValueRef.h"
29#include "JSObjectRef.h"
30#include "JSCallbackConstructor.h"
31#include "JSCallbackFunction.h"
32#include "JSCallbackObject.h"
33#include "JSClassRef.h"
34
35#include "identifier.h"
36#include "function.h"
37#include "nodes.h"
38#include "internal.h"
39#include "object.h"
40#include "PropertyNameArray.h"
41
42using namespace KJS;
43
44JSClassRef JSClassCreate(JSClassDefinition* definition)
45{
46 JSClassRef jsClass = new __JSClass(definition);
47 return JSClassRetain(jsClass);
48}
49
50JSClassRef JSClassRetain(JSClassRef jsClass)
51{
52 ++jsClass->refCount;
53 return jsClass;
54}
55
56void JSClassRelease(JSClassRef jsClass)
57{
58 if (--jsClass->refCount == 0)
59 delete jsClass;
60}
61
62JSObjectRef JSObjectMake(JSContextRef context, JSClassRef jsClass, JSValueRef prototype)
63{
64 JSLock lock;
65
66 ExecState* exec = toJS(context);
67 JSValue* jsPrototype = toJS(prototype);
68
69 if (!prototype)
70 jsPrototype = exec->lexicalInterpreter()->builtinObjectPrototype();
71
72 if (!jsClass)
73 return toRef(new JSObject(jsPrototype)); // slightly more efficient
74 else
75 return toRef(new JSCallbackObject(context, jsClass, jsPrototype));
76}
77
78JSObjectRef JSObjectMakeFunction(JSContextRef context, JSStringRef name, JSObjectCallAsFunctionCallback callAsFunction)
79{
80 JSLock lock;
81 ExecState* exec = toJS(context);
82 Identifier nameID = name ? Identifier(toJS(name)) : Identifier("anonymous");
83
84 return toRef(new JSCallbackFunction(exec, callAsFunction, nameID));
85}
86
87JSObjectRef JSObjectMakeConstructor(JSContextRef context, JSObjectCallAsConstructorCallback callAsConstructor)
88{
89 JSLock lock;
90 ExecState* exec = toJS(context);
91 return toRef(new JSCallbackConstructor(exec, callAsConstructor));
92}
93
94JSObjectRef JSObjectMakeFunctionWithBody(JSContextRef context, JSStringRef name, unsigned parameterCount, const JSStringRef parameterNames[], JSStringRef body, JSStringRef sourceURL, int startingLineNumber, JSValueRef* exception)
95{
96 JSLock lock;
97
98 ExecState* exec = toJS(context);
99 UString::Rep* bodyRep = toJS(body);
100 UString::Rep* sourceURLRep = sourceURL ? toJS(sourceURL) : &UString::Rep::null;
101
102 Identifier nameID = name ? Identifier(toJS(name)) : Identifier("anonymous");
103
104 List args;
105 for (unsigned i = 0; i < parameterCount; i++)
106 args.append(jsString(UString(toJS(parameterNames[i]))));
107 args.append(jsString(UString(bodyRep)));
108
109 JSObject* result = exec->dynamicInterpreter()->builtinFunction()->construct(exec, args, nameID, UString(sourceURLRep), startingLineNumber);
110 if (exec->hadException()) {
111 if (exception)
112 *exception = toRef(exec->exception());
113 exec->clearException();
114 result = 0;
115 }
116 return toRef(result);
117}
118
119JSValueRef JSObjectGetPrototype(JSObjectRef object)
120{
121 JSObject* jsObject = toJS(object);
122 return toRef(jsObject->prototype());
123}
124
125void JSObjectSetPrototype(JSObjectRef object, JSValueRef value)
126{
127 JSObject* jsObject = toJS(object);
128 JSValue* jsValue = toJS(value);
129
130 jsObject->setPrototype(jsValue);
131}
132
133bool JSObjectHasProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName)
134{
135 JSLock lock;
136 ExecState* exec = toJS(context);
137 JSObject* jsObject = toJS(object);
138 UString::Rep* nameRep = toJS(propertyName);
139
140 return jsObject->hasProperty(exec, Identifier(nameRep));
141}
142
143JSValueRef JSObjectGetProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
144{
145 JSLock lock;
146 ExecState* exec = toJS(context);
147 JSObject* jsObject = toJS(object);
148 UString::Rep* nameRep = toJS(propertyName);
149
150 JSValue* jsValue = jsObject->get(exec, Identifier(nameRep));
151 if (jsValue->isUndefined())
152 jsValue = 0;
153 if (exec->hadException()) {
154 if (exception)
155 *exception = toRef(exec->exception());
156 exec->clearException();
157 }
158 return toRef(jsValue);
159}
160
161void JSObjectSetProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef value, JSPropertyAttributes attributes, JSValueRef* exception)
162{
163 JSLock lock;
164 ExecState* exec = toJS(context);
165 JSObject* jsObject = toJS(object);
166 UString::Rep* nameRep = toJS(propertyName);
167 JSValue* jsValue = toJS(value);
168
169 jsObject->put(exec, Identifier(nameRep), jsValue, attributes);
170 if (exec->hadException()) {
171 if (exception)
172 *exception = toRef(exec->exception());
173 exec->clearException();
174 }
175}
176
177JSValueRef JSObjectGetPropertyAtIndex(JSContextRef context, JSObjectRef object, unsigned propertyIndex)
178{
179 JSLock lock;
180 ExecState* exec = toJS(context);
181 JSObject* jsObject = toJS(object);
182
183 JSValue* jsValue = jsObject->get(exec, propertyIndex);
184 if (jsValue->isUndefined())
185 return 0;
186 return toRef(jsValue);
187}
188
189
190void JSObjectSetPropertyAtIndex(JSContextRef context, JSObjectRef object, unsigned propertyIndex, JSValueRef value)
191{
192 JSLock lock;
193 ExecState* exec = toJS(context);
194 JSObject* jsObject = toJS(object);
195 JSValue* jsValue = toJS(value);
196
197 jsObject->put(exec, propertyIndex, jsValue);
198}
199
200bool JSObjectDeleteProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
201{
202 JSLock lock;
203 ExecState* exec = toJS(context);
204 JSObject* jsObject = toJS(object);
205 UString::Rep* nameRep = toJS(propertyName);
206
207 bool result = jsObject->deleteProperty(exec, Identifier(nameRep));
208 if (exec->hadException()) {
209 if (exception)
210 *exception = toRef(exec->exception());
211 exec->clearException();
212 }
213 return result;
214}
215
216void* JSObjectGetPrivate(JSObjectRef object)
217{
218 JSObject* jsObject = toJS(object);
219
220 if (jsObject->inherits(&JSCallbackObject::info))
221 return static_cast<JSCallbackObject*>(jsObject)->getPrivate();
222
223 return 0;
224}
225
226bool JSObjectSetPrivate(JSObjectRef object, void* data)
227{
228 JSObject* jsObject = toJS(object);
229
230 if (jsObject->inherits(&JSCallbackObject::info)) {
231 static_cast<JSCallbackObject*>(jsObject)->setPrivate(data);
232 return true;
233 }
234
235 return false;
236}
237
238bool JSObjectIsFunction(JSObjectRef object)
239{
240 JSObject* jsObject = toJS(object);
241 return jsObject->implementsCall();
242}
243
244JSValueRef JSObjectCallAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
245{
246 JSLock lock;
247 ExecState* exec = toJS(context);
248 JSObject* jsObject = toJS(object);
249 JSObject* jsThisObject = toJS(thisObject);
250
251 if (!jsThisObject)
252 jsThisObject = exec->dynamicInterpreter()->globalObject();
253
254 List argList;
255 for (size_t i = 0; i < argumentCount; i++)
256 argList.append(toJS(arguments[i]));
257
258 JSValueRef result = toRef(jsObject->call(exec, jsThisObject, argList)); // returns NULL if object->implementsCall() is false
259 if (exec->hadException()) {
260 if (exception)
261 *exception = toRef(exec->exception());
262 exec->clearException();
263 result = 0;
264 }
265 return result;
266}
267
268bool JSObjectIsConstructor(JSObjectRef object)
269{
270 JSObject* jsObject = toJS(object);
271 return jsObject->implementsConstruct();
272}
273
274JSObjectRef JSObjectCallAsConstructor(JSContextRef context, JSObjectRef object, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
275{
276 JSLock lock;
277 ExecState* exec = toJS(context);
278 JSObject* jsObject = toJS(object);
279
280 List argList;
281 for (size_t i = 0; i < argumentCount; i++)
282 argList.append(toJS(arguments[i]));
283
284 JSObjectRef result = toRef(jsObject->construct(exec, argList)); // returns NULL if object->implementsCall() is false
285 if (exec->hadException()) {
286 if (exception)
287 *exception = toRef(exec->exception());
288 exec->clearException();
289 result = 0;
290 }
291 return result;
292}
293
294struct __JSPropertyNameArray
295{
296 __JSPropertyNameArray() : refCount(0)
297 {
298 }
299
300 unsigned refCount;
301 PropertyNameArray array;
302};
303
304JSPropertyNameArrayRef JSObjectCopyPropertyNames(JSContextRef context, JSObjectRef object)
305{
306 JSLock lock;
307 JSObject* jsObject = toJS(object);
308 ExecState* exec = toJS(context);
309
310 JSPropertyNameArrayRef propertyNames = new __JSPropertyNameArray();
311 jsObject->getPropertyNames(exec, propertyNames->array);
312
313 return JSPropertyNameArrayRetain(propertyNames);
314}
315
316JSPropertyNameArrayRef JSPropertyNameArrayRetain(JSPropertyNameArrayRef array)
317{
318 ++array->refCount;
319 return array;
320}
321
322void JSPropertyNameArrayRelease(JSPropertyNameArrayRef array)
323{
324 if (--array->refCount == 0)
325 delete array;
326}
327
328size_t JSPropertyNameArrayGetCount(JSPropertyNameArrayRef array)
329{
330 return array->array.size();
331}
332
333JSStringRef JSPropertyNameArrayGetNameAtIndex(JSPropertyNameArrayRef array, size_t index)
334{
335 return toRef(array->array[index].ustring().rep());
336}
337
338void JSPropertyNameAccumulatorAddName(JSPropertyNameAccumulatorRef array, JSStringRef propertyName)
339{
340 JSLock lock;
341 PropertyNameArray* propertyNames = toJS(array);
342 UString::Rep* rep = toJS(propertyName);
343
344 propertyNames->add(Identifier(rep));
345}
Note: See TracBrowser for help on using the repository browser.