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