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

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

Approved by Maciej, RS by Beth.


JSObjectMakeFunction -> JSObjectMakeFunctionWithCallback
JSObjectMakeFunctionWithBody -> JSObjectMakeFunction


because the latter is more common, and more fundamental, than the former.

  • API/APICast.h: (toJS):
  • API/JSBase.h:
  • API/JSCallbackObject.cpp: (KJS::JSCallbackObject::getOwnPropertySlot): (KJS::JSCallbackObject::put): (KJS::JSCallbackObject::deleteProperty): (KJS::JSCallbackObject::getPropertyNames): (KJS::JSCallbackObject::staticValueGetter): (KJS::JSCallbackObject::staticFunctionGetter):
  • API/JSClassRef.cpp: (OpaqueJSClass::OpaqueJSClass): (OpaqueJSClass::~OpaqueJSClass):
  • API/JSClassRef.h:
  • API/JSObjectRef.cpp: (JSClassCreate): (JSObjectMakeFunctionWithCallback): (JSObjectMakeFunction): (OpaqueJSPropertyNameArray::OpaqueJSPropertyNameArray): (JSObjectCopyPropertyNames):
  • API/JSObjectRef.h:
  • API/minidom.c: (main):
  • API/testapi.c: (main):
  • ChangeLog:
  • JavaScriptCore.exp:
File size: 10.6 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 OpaqueJSClass(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 JSObjectMakeFunctionWithCallback(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 JSObjectMakeFunction(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 (exec->hadException()) {
152 if (exception)
153 *exception = toRef(exec->exception());
154 exec->clearException();
155 }
156 return toRef(jsValue);
157}
158
159void JSObjectSetProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef value, JSPropertyAttributes attributes, JSValueRef* exception)
160{
161 JSLock lock;
162 ExecState* exec = toJS(context);
163 JSObject* jsObject = toJS(object);
164 UString::Rep* nameRep = toJS(propertyName);
165 JSValue* jsValue = toJS(value);
166
167 jsObject->put(exec, Identifier(nameRep), jsValue, attributes);
168 if (exec->hadException()) {
169 if (exception)
170 *exception = toRef(exec->exception());
171 exec->clearException();
172 }
173}
174
175JSValueRef JSObjectGetPropertyAtIndex(JSContextRef context, JSObjectRef object, unsigned propertyIndex, JSValueRef* exception)
176{
177 JSLock lock;
178 ExecState* exec = toJS(context);
179 JSObject* jsObject = toJS(object);
180
181 JSValue* jsValue = jsObject->get(exec, propertyIndex);
182 if (exec->hadException()) {
183 if (exception)
184 *exception = toRef(exec->exception());
185 exec->clearException();
186 }
187 return toRef(jsValue);
188}
189
190
191void JSObjectSetPropertyAtIndex(JSContextRef context, JSObjectRef object, unsigned propertyIndex, JSValueRef value, JSValueRef* exception)
192{
193 JSLock lock;
194 ExecState* exec = toJS(context);
195 JSObject* jsObject = toJS(object);
196 JSValue* jsValue = toJS(value);
197
198 jsObject->put(exec, propertyIndex, jsValue);
199 if (exec->hadException()) {
200 if (exception)
201 *exception = toRef(exec->exception());
202 exec->clearException();
203 }
204}
205
206bool JSObjectDeleteProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
207{
208 JSLock lock;
209 ExecState* exec = toJS(context);
210 JSObject* jsObject = toJS(object);
211 UString::Rep* nameRep = toJS(propertyName);
212
213 bool result = jsObject->deleteProperty(exec, Identifier(nameRep));
214 if (exec->hadException()) {
215 if (exception)
216 *exception = toRef(exec->exception());
217 exec->clearException();
218 }
219 return result;
220}
221
222void* JSObjectGetPrivate(JSObjectRef object)
223{
224 JSObject* jsObject = toJS(object);
225
226 if (jsObject->inherits(&JSCallbackObject::info))
227 return static_cast<JSCallbackObject*>(jsObject)->getPrivate();
228
229 return 0;
230}
231
232bool JSObjectSetPrivate(JSObjectRef object, void* data)
233{
234 JSObject* jsObject = toJS(object);
235
236 if (jsObject->inherits(&JSCallbackObject::info)) {
237 static_cast<JSCallbackObject*>(jsObject)->setPrivate(data);
238 return true;
239 }
240
241 return false;
242}
243
244bool JSObjectIsFunction(JSObjectRef object)
245{
246 JSObject* jsObject = toJS(object);
247 return jsObject->implementsCall();
248}
249
250JSValueRef JSObjectCallAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
251{
252 JSLock lock;
253 ExecState* exec = toJS(context);
254 JSObject* jsObject = toJS(object);
255 JSObject* jsThisObject = toJS(thisObject);
256
257 if (!jsThisObject)
258 jsThisObject = exec->dynamicInterpreter()->globalObject();
259
260 List argList;
261 for (size_t i = 0; i < argumentCount; i++)
262 argList.append(toJS(arguments[i]));
263
264 JSValueRef result = toRef(jsObject->call(exec, jsThisObject, argList)); // returns NULL if object->implementsCall() is false
265 if (exec->hadException()) {
266 if (exception)
267 *exception = toRef(exec->exception());
268 exec->clearException();
269 result = 0;
270 }
271 return result;
272}
273
274bool JSObjectIsConstructor(JSObjectRef object)
275{
276 JSObject* jsObject = toJS(object);
277 return jsObject->implementsConstruct();
278}
279
280JSObjectRef JSObjectCallAsConstructor(JSContextRef context, JSObjectRef object, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
281{
282 JSLock lock;
283 ExecState* exec = toJS(context);
284 JSObject* jsObject = toJS(object);
285
286 List argList;
287 for (size_t i = 0; i < argumentCount; i++)
288 argList.append(toJS(arguments[i]));
289
290 JSObjectRef result = toRef(jsObject->construct(exec, argList)); // returns NULL if object->implementsCall() is false
291 if (exec->hadException()) {
292 if (exception)
293 *exception = toRef(exec->exception());
294 exec->clearException();
295 result = 0;
296 }
297 return result;
298}
299
300struct OpaqueJSPropertyNameArray
301{
302 OpaqueJSPropertyNameArray() : refCount(0)
303 {
304 }
305
306 unsigned refCount;
307 PropertyNameArray array;
308};
309
310JSPropertyNameArrayRef JSObjectCopyPropertyNames(JSContextRef context, JSObjectRef object)
311{
312 JSLock lock;
313 JSObject* jsObject = toJS(object);
314 ExecState* exec = toJS(context);
315
316 JSPropertyNameArrayRef propertyNames = new OpaqueJSPropertyNameArray();
317 jsObject->getPropertyNames(exec, propertyNames->array);
318
319 return JSPropertyNameArrayRetain(propertyNames);
320}
321
322JSPropertyNameArrayRef JSPropertyNameArrayRetain(JSPropertyNameArrayRef array)
323{
324 ++array->refCount;
325 return array;
326}
327
328void JSPropertyNameArrayRelease(JSPropertyNameArrayRef array)
329{
330 if (--array->refCount == 0)
331 delete array;
332}
333
334size_t JSPropertyNameArrayGetCount(JSPropertyNameArrayRef array)
335{
336 return array->array.size();
337}
338
339JSStringRef JSPropertyNameArrayGetNameAtIndex(JSPropertyNameArrayRef array, size_t index)
340{
341 return toRef(array->array[index].ustring().rep());
342}
343
344void JSPropertyNameAccumulatorAddName(JSPropertyNameAccumulatorRef array, JSStringRef propertyName)
345{
346 JSLock lock;
347 PropertyNameArray* propertyNames = toJS(array);
348 UString::Rep* rep = toJS(propertyName);
349
350 propertyNames->add(Identifier(rep));
351}
Note: See TracBrowser for help on using the repository browser.