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

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

Rubber-stamped by Maciej.

Eliminate JSLock (it was already disabled, removing the stub implementaion and all
call sites now).

  • Property svn:eol-style set to native
File size: 12.5 KB
Line 
1// -*- mode: c++; c-basic-offset: 4 -*-
2/*
3 * Copyright (C) 2006, 2007, 2008 Apple 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 "config.h"
28#include "JSObjectRef.h"
29
30#include "APICast.h"
31#include "FunctionConstructor.h"
32#include "JSCallbackConstructor.h"
33#include "JSCallbackFunction.h"
34#include "JSCallbackObject.h"
35#include "JSClassRef.h"
36#include "JSFunction.h"
37#include "JSGlobalObject.h"
38#include "JSObject.h"
39#include "JSString.h"
40#include "JSValueRef.h"
41#include "ObjectPrototype.h"
42#include "PropertyNameArray.h"
43#include "identifier.h"
44#include <wtf/Platform.h>
45
46using namespace KJS;
47
48JSClassRef JSClassCreate(const JSClassDefinition* definition)
49{
50 RefPtr<OpaqueJSClass> jsClass = (definition->attributes & kJSClassAttributeNoAutomaticPrototype)
51 ? OpaqueJSClass::createNoAutomaticPrototype(definition)
52 : OpaqueJSClass::create(definition);
53
54 return jsClass.release().releaseRef();
55}
56
57JSClassRef JSClassRetain(JSClassRef jsClass)
58{
59 jsClass->ref();
60 return jsClass;
61}
62
63void JSClassRelease(JSClassRef jsClass)
64{
65 jsClass->deref();
66}
67
68JSObjectRef JSObjectMake(JSContextRef ctx, JSClassRef jsClass, void* data)
69{
70 ExecState* exec = toJS(ctx);
71 exec->globalData().heap->registerThread();
72
73 if (!jsClass)
74 return toRef(new (exec) JSObject(exec->lexicalGlobalObject()->objectPrototype())); // slightly more efficient
75
76 JSValue* jsPrototype = jsClass->prototype(exec);
77 if (!jsPrototype)
78 jsPrototype = exec->lexicalGlobalObject()->objectPrototype();
79
80 return toRef(new (exec) JSCallbackObject<JSObject>(exec, jsClass, jsPrototype, data));
81}
82
83JSObjectRef JSObjectMakeFunctionWithCallback(JSContextRef ctx, JSStringRef name, JSObjectCallAsFunctionCallback callAsFunction)
84{
85 ExecState* exec = toJS(ctx);
86 exec->globalData().heap->registerThread();
87
88 Identifier nameID = name ? Identifier(exec, toJS(name)) : Identifier(exec, "anonymous");
89
90 return toRef(new (exec) JSCallbackFunction(exec, callAsFunction, nameID));
91}
92
93JSObjectRef JSObjectMakeConstructor(JSContextRef ctx, JSClassRef jsClass, JSObjectCallAsConstructorCallback callAsConstructor)
94{
95 ExecState* exec = toJS(ctx);
96 exec->globalData().heap->registerThread();
97
98 JSValue* jsPrototype = jsClass
99 ? jsClass->prototype(exec)
100 : exec->dynamicGlobalObject()->objectPrototype();
101
102 JSCallbackConstructor* constructor = new (exec) JSCallbackConstructor(exec, jsClass, callAsConstructor);
103 constructor->putDirect(exec->propertyNames().prototype, jsPrototype, DontEnum | DontDelete | ReadOnly);
104 return toRef(constructor);
105}
106
107JSObjectRef JSObjectMakeFunction(JSContextRef ctx, JSStringRef name, unsigned parameterCount, const JSStringRef parameterNames[], JSStringRef body, JSStringRef sourceURL, int startingLineNumber, JSValueRef* exception)
108{
109 ExecState* exec = toJS(ctx);
110 exec->globalData().heap->registerThread();
111
112 UString::Rep* bodyRep = toJS(body);
113 UString::Rep* sourceURLRep = sourceURL ? toJS(sourceURL) : &UString::Rep::null;
114
115 Identifier nameID = name ? Identifier(exec, toJS(name)) : Identifier(exec, "anonymous");
116
117 ArgList args;
118 for (unsigned i = 0; i < parameterCount; i++)
119 args.append(jsString(exec, UString(toJS(parameterNames[i]))));
120 args.append(jsString(exec, UString(bodyRep)));
121
122 JSObject* result = constructFunction(exec, args, nameID, UString(sourceURLRep), startingLineNumber);
123 if (exec->hadException()) {
124 if (exception)
125 *exception = toRef(exec->exception());
126 exec->clearException();
127 result = 0;
128 }
129 return toRef(result);
130}
131
132JSValueRef JSObjectGetPrototype(JSContextRef, JSObjectRef object)
133{
134 JSObject* jsObject = toJS(object);
135 return toRef(jsObject->prototype());
136}
137
138void JSObjectSetPrototype(JSContextRef, JSObjectRef object, JSValueRef value)
139{
140 JSObject* jsObject = toJS(object);
141 JSValue* jsValue = toJS(value);
142
143 jsObject->setPrototype(jsValue);
144}
145
146bool JSObjectHasProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName)
147{
148 ExecState* exec = toJS(ctx);
149 exec->globalData().heap->registerThread();
150
151 JSObject* jsObject = toJS(object);
152 UString::Rep* nameRep = toJS(propertyName);
153
154 return jsObject->hasProperty(exec, Identifier(exec, nameRep));
155}
156
157JSValueRef JSObjectGetProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
158{
159 ExecState* exec = toJS(ctx);
160 exec->globalData().heap->registerThread();
161
162 JSObject* jsObject = toJS(object);
163 UString::Rep* nameRep = toJS(propertyName);
164
165 JSValue* jsValue = jsObject->get(exec, Identifier(exec, nameRep));
166 if (exec->hadException()) {
167 if (exception)
168 *exception = toRef(exec->exception());
169 exec->clearException();
170 }
171 return toRef(jsValue);
172}
173
174void JSObjectSetProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef value, JSPropertyAttributes attributes, JSValueRef* exception)
175{
176 ExecState* exec = toJS(ctx);
177 exec->globalData().heap->registerThread();
178
179 JSObject* jsObject = toJS(object);
180 Identifier name(exec, toJS(propertyName));
181 JSValue* jsValue = toJS(value);
182
183 if (attributes && !jsObject->hasProperty(exec, name))
184 jsObject->putWithAttributes(exec, name, jsValue, attributes);
185 else
186 jsObject->put(exec, name, jsValue);
187
188 if (exec->hadException()) {
189 if (exception)
190 *exception = toRef(exec->exception());
191 exec->clearException();
192 }
193}
194
195JSValueRef JSObjectGetPropertyAtIndex(JSContextRef ctx, JSObjectRef object, unsigned propertyIndex, JSValueRef* exception)
196{
197 ExecState* exec = toJS(ctx);
198 exec->globalData().heap->registerThread();
199
200 JSObject* jsObject = toJS(object);
201
202 JSValue* jsValue = jsObject->get(exec, propertyIndex);
203 if (exec->hadException()) {
204 if (exception)
205 *exception = toRef(exec->exception());
206 exec->clearException();
207 }
208 return toRef(jsValue);
209}
210
211
212void JSObjectSetPropertyAtIndex(JSContextRef ctx, JSObjectRef object, unsigned propertyIndex, JSValueRef value, JSValueRef* exception)
213{
214 ExecState* exec = toJS(ctx);
215 exec->globalData().heap->registerThread();
216
217 JSObject* jsObject = toJS(object);
218 JSValue* jsValue = toJS(value);
219
220 jsObject->put(exec, propertyIndex, jsValue);
221 if (exec->hadException()) {
222 if (exception)
223 *exception = toRef(exec->exception());
224 exec->clearException();
225 }
226}
227
228bool JSObjectDeleteProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
229{
230 ExecState* exec = toJS(ctx);
231 exec->globalData().heap->registerThread();
232
233 JSObject* jsObject = toJS(object);
234 UString::Rep* nameRep = toJS(propertyName);
235
236 bool result = jsObject->deleteProperty(exec, Identifier(exec, nameRep));
237 if (exec->hadException()) {
238 if (exception)
239 *exception = toRef(exec->exception());
240 exec->clearException();
241 }
242 return result;
243}
244
245void* JSObjectGetPrivate(JSObjectRef object)
246{
247 JSObject* jsObject = toJS(object);
248
249 if (jsObject->inherits(&JSCallbackObject<JSGlobalObject>::info))
250 return static_cast<JSCallbackObject<JSGlobalObject>*>(jsObject)->getPrivate();
251 else if (jsObject->inherits(&JSCallbackObject<JSObject>::info))
252 return static_cast<JSCallbackObject<JSObject>*>(jsObject)->getPrivate();
253
254 return 0;
255}
256
257bool JSObjectSetPrivate(JSObjectRef object, void* data)
258{
259 JSObject* jsObject = toJS(object);
260
261 if (jsObject->inherits(&JSCallbackObject<JSGlobalObject>::info)) {
262 static_cast<JSCallbackObject<JSGlobalObject>*>(jsObject)->setPrivate(data);
263 return true;
264 } else if (jsObject->inherits(&JSCallbackObject<JSObject>::info)) {
265 static_cast<JSCallbackObject<JSObject>*>(jsObject)->setPrivate(data);
266 return true;
267 }
268
269 return false;
270}
271
272bool JSObjectIsFunction(JSContextRef, JSObjectRef object)
273{
274 CallData callData;
275 return toJS(object)->getCallData(callData) != CallTypeNone;
276}
277
278JSValueRef JSObjectCallAsFunction(JSContextRef ctx, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
279{
280 ExecState* exec = toJS(ctx);
281 exec->globalData().heap->registerThread();
282
283 JSObject* jsObject = toJS(object);
284 JSObject* jsThisObject = toJS(thisObject);
285
286 if (!jsThisObject)
287 jsThisObject = exec->globalThisValue();
288
289 ArgList argList;
290 for (size_t i = 0; i < argumentCount; i++)
291 argList.append(toJS(arguments[i]));
292
293 CallData callData;
294 CallType callType = jsObject->getCallData(callData);
295 if (callType == CallTypeNone)
296 return 0;
297
298 JSValueRef result = toRef(call(exec, jsObject, callType, callData, jsThisObject, argList));
299 if (exec->hadException()) {
300 if (exception)
301 *exception = toRef(exec->exception());
302 exec->clearException();
303 result = 0;
304 }
305 return result;
306}
307
308bool JSObjectIsConstructor(JSContextRef, JSObjectRef object)
309{
310 JSObject* jsObject = toJS(object);
311 ConstructData constructData;
312 return jsObject->getConstructData(constructData) != ConstructTypeNone;
313}
314
315JSObjectRef JSObjectCallAsConstructor(JSContextRef ctx, JSObjectRef object, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
316{
317 ExecState* exec = toJS(ctx);
318 exec->globalData().heap->registerThread();
319
320 JSObject* jsObject = toJS(object);
321
322 ConstructData constructData;
323 ConstructType constructType = jsObject->getConstructData(constructData);
324 if (constructType == ConstructTypeNone)
325 return 0;
326
327 ArgList argList;
328 for (size_t i = 0; i < argumentCount; i++)
329 argList.append(toJS(arguments[i]));
330 JSObjectRef result = toRef(construct(exec, jsObject, constructType, constructData, argList));
331 if (exec->hadException()) {
332 if (exception)
333 *exception = toRef(exec->exception());
334 exec->clearException();
335 result = 0;
336 }
337 return result;
338}
339
340struct OpaqueJSPropertyNameArray
341{
342 OpaqueJSPropertyNameArray(JSGlobalData* globalData) : refCount(0), array(globalData)
343 {
344 }
345
346 unsigned refCount;
347 PropertyNameArray array;
348};
349
350JSPropertyNameArrayRef JSObjectCopyPropertyNames(JSContextRef ctx, JSObjectRef object)
351{
352 JSObject* jsObject = toJS(object);
353 ExecState* exec = toJS(ctx);
354 exec->globalData().heap->registerThread();
355
356 JSPropertyNameArrayRef propertyNames = new OpaqueJSPropertyNameArray(&exec->globalData());
357 jsObject->getPropertyNames(exec, propertyNames->array);
358
359 return JSPropertyNameArrayRetain(propertyNames);
360}
361
362JSPropertyNameArrayRef JSPropertyNameArrayRetain(JSPropertyNameArrayRef array)
363{
364 ++array->refCount;
365 return array;
366}
367
368void JSPropertyNameArrayRelease(JSPropertyNameArrayRef array)
369{
370 if (--array->refCount == 0)
371 delete array;
372}
373
374size_t JSPropertyNameArrayGetCount(JSPropertyNameArrayRef array)
375{
376 return array->array.size();
377}
378
379JSStringRef JSPropertyNameArrayGetNameAtIndex(JSPropertyNameArrayRef array, size_t index)
380{
381 return toRef(array->array[static_cast<unsigned>(index)].ustring().rep());
382}
383
384void JSPropertyNameAccumulatorAddName(JSPropertyNameAccumulatorRef array, JSStringRef propertyName)
385{
386 PropertyNameArray* propertyNames = toJS(array);
387 UString::Rep* rep = toJS(propertyName);
388
389 propertyNames->globalData()->heap->registerThread();
390
391 propertyNames->add(Identifier(propertyNames->globalData(), rep));
392}
Note: See TracBrowser for help on using the repository browser.