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

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

Reviewed by Geoff Garen.

Eliminate per-thread JavaScript global data instance support and make arbitrary
global data/global object combinations possible.

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