]>
Commit | Line | Data |
---|---|---|
b37bf2e1 | 1 | /* |
9dae56ea A |
2 | * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved. |
3 | * Copyright (C) 2008 Kelvin W Sherlock (ksherlock@gmail.com) | |
b37bf2e1 A |
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" | |
4e4e5a6f | 29 | #include "JSObjectRefPrivate.h" |
b37bf2e1 | 30 | |
b37bf2e1 | 31 | #include "APICast.h" |
f9bf01c6 | 32 | #include "CodeBlock.h" |
9dae56ea A |
33 | #include "DateConstructor.h" |
34 | #include "ErrorConstructor.h" | |
35 | #include "FunctionConstructor.h" | |
36 | #include "Identifier.h" | |
37 | #include "InitializeThreading.h" | |
38 | #include "JSArray.h" | |
b37bf2e1 A |
39 | #include "JSCallbackConstructor.h" |
40 | #include "JSCallbackFunction.h" | |
41 | #include "JSCallbackObject.h" | |
42 | #include "JSClassRef.h" | |
9dae56ea | 43 | #include "JSFunction.h" |
b37bf2e1 | 44 | #include "JSGlobalObject.h" |
9dae56ea A |
45 | #include "JSObject.h" |
46 | #include "JSRetainPtr.h" | |
47 | #include "JSString.h" | |
48 | #include "JSValueRef.h" | |
49 | #include "ObjectPrototype.h" | |
b37bf2e1 | 50 | #include "PropertyNameArray.h" |
9dae56ea | 51 | #include "RegExpConstructor.h" |
b37bf2e1 | 52 | |
9dae56ea | 53 | using namespace JSC; |
b37bf2e1 A |
54 | |
55 | JSClassRef JSClassCreate(const JSClassDefinition* definition) | |
56 | { | |
9dae56ea A |
57 | initializeThreading(); |
58 | RefPtr<OpaqueJSClass> jsClass = (definition->attributes & kJSClassAttributeNoAutomaticPrototype) | |
b37bf2e1 A |
59 | ? OpaqueJSClass::createNoAutomaticPrototype(definition) |
60 | : OpaqueJSClass::create(definition); | |
61 | ||
9dae56ea | 62 | return jsClass.release().releaseRef(); |
b37bf2e1 A |
63 | } |
64 | ||
65 | JSClassRef JSClassRetain(JSClassRef jsClass) | |
66 | { | |
b37bf2e1 A |
67 | jsClass->ref(); |
68 | return jsClass; | |
69 | } | |
70 | ||
71 | void JSClassRelease(JSClassRef jsClass) | |
72 | { | |
b37bf2e1 A |
73 | jsClass->deref(); |
74 | } | |
75 | ||
76 | JSObjectRef JSObjectMake(JSContextRef ctx, JSClassRef jsClass, void* data) | |
77 | { | |
b37bf2e1 | 78 | ExecState* exec = toJS(ctx); |
f9bf01c6 | 79 | APIEntryShim entryShim(exec); |
b37bf2e1 A |
80 | |
81 | if (!jsClass) | |
9dae56ea | 82 | return toRef(new (exec) JSObject(exec->lexicalGlobalObject()->emptyObjectStructure())); // slightly more efficient |
b37bf2e1 | 83 | |
9dae56ea A |
84 | JSCallbackObject<JSObject>* object = new (exec) JSCallbackObject<JSObject>(exec, exec->lexicalGlobalObject()->callbackObjectStructure(), jsClass, data); |
85 | if (JSObject* prototype = jsClass->prototype(exec)) | |
86 | object->setPrototype(prototype); | |
b37bf2e1 | 87 | |
9dae56ea | 88 | return toRef(object); |
b37bf2e1 A |
89 | } |
90 | ||
91 | JSObjectRef JSObjectMakeFunctionWithCallback(JSContextRef ctx, JSStringRef name, JSObjectCallAsFunctionCallback callAsFunction) | |
92 | { | |
b37bf2e1 | 93 | ExecState* exec = toJS(ctx); |
f9bf01c6 | 94 | APIEntryShim entryShim(exec); |
9dae56ea A |
95 | |
96 | Identifier nameID = name ? name->identifier(&exec->globalData()) : Identifier(exec, "anonymous"); | |
b37bf2e1 | 97 | |
9dae56ea | 98 | return toRef(new (exec) JSCallbackFunction(exec, callAsFunction, nameID)); |
b37bf2e1 A |
99 | } |
100 | ||
101 | JSObjectRef JSObjectMakeConstructor(JSContextRef ctx, JSClassRef jsClass, JSObjectCallAsConstructorCallback callAsConstructor) | |
102 | { | |
b37bf2e1 | 103 | ExecState* exec = toJS(ctx); |
f9bf01c6 | 104 | APIEntryShim entryShim(exec); |
9dae56ea | 105 | |
ba379fdc A |
106 | JSValue jsPrototype = jsClass ? jsClass->prototype(exec) : 0; |
107 | if (!jsPrototype) | |
108 | jsPrototype = exec->lexicalGlobalObject()->objectPrototype(); | |
109 | ||
9dae56ea | 110 | JSCallbackConstructor* constructor = new (exec) JSCallbackConstructor(exec->lexicalGlobalObject()->callbackConstructorStructure(), jsClass, callAsConstructor); |
b37bf2e1 A |
111 | constructor->putDirect(exec->propertyNames().prototype, jsPrototype, DontEnum | DontDelete | ReadOnly); |
112 | return toRef(constructor); | |
113 | } | |
114 | ||
115 | JSObjectRef JSObjectMakeFunction(JSContextRef ctx, JSStringRef name, unsigned parameterCount, const JSStringRef parameterNames[], JSStringRef body, JSStringRef sourceURL, int startingLineNumber, JSValueRef* exception) | |
116 | { | |
b37bf2e1 | 117 | ExecState* exec = toJS(ctx); |
f9bf01c6 | 118 | APIEntryShim entryShim(exec); |
9dae56ea A |
119 | |
120 | Identifier nameID = name ? name->identifier(&exec->globalData()) : Identifier(exec, "anonymous"); | |
b37bf2e1 | 121 | |
ba379fdc | 122 | MarkedArgumentBuffer args; |
b37bf2e1 | 123 | for (unsigned i = 0; i < parameterCount; i++) |
9dae56ea A |
124 | args.append(jsString(exec, parameterNames[i]->ustring())); |
125 | args.append(jsString(exec, body->ustring())); | |
126 | ||
127 | JSObject* result = constructFunction(exec, args, nameID, sourceURL->ustring(), startingLineNumber); | |
128 | if (exec->hadException()) { | |
129 | if (exception) | |
ba379fdc | 130 | *exception = toRef(exec, exec->exception()); |
9dae56ea A |
131 | exec->clearException(); |
132 | result = 0; | |
133 | } | |
134 | return toRef(result); | |
135 | } | |
136 | ||
137 | JSObjectRef JSObjectMakeArray(JSContextRef ctx, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) | |
138 | { | |
139 | ExecState* exec = toJS(ctx); | |
f9bf01c6 | 140 | APIEntryShim entryShim(exec); |
9dae56ea A |
141 | |
142 | JSObject* result; | |
143 | if (argumentCount) { | |
ba379fdc | 144 | MarkedArgumentBuffer argList; |
9dae56ea | 145 | for (size_t i = 0; i < argumentCount; ++i) |
ba379fdc | 146 | argList.append(toJS(exec, arguments[i])); |
9dae56ea A |
147 | |
148 | result = constructArray(exec, argList); | |
149 | } else | |
150 | result = constructEmptyArray(exec); | |
b37bf2e1 | 151 | |
b37bf2e1 A |
152 | if (exec->hadException()) { |
153 | if (exception) | |
ba379fdc | 154 | *exception = toRef(exec, exec->exception()); |
b37bf2e1 A |
155 | exec->clearException(); |
156 | result = 0; | |
157 | } | |
9dae56ea A |
158 | |
159 | return toRef(result); | |
160 | } | |
161 | ||
162 | JSObjectRef JSObjectMakeDate(JSContextRef ctx, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) | |
163 | { | |
164 | ExecState* exec = toJS(ctx); | |
f9bf01c6 | 165 | APIEntryShim entryShim(exec); |
9dae56ea | 166 | |
ba379fdc | 167 | MarkedArgumentBuffer argList; |
9dae56ea | 168 | for (size_t i = 0; i < argumentCount; ++i) |
ba379fdc | 169 | argList.append(toJS(exec, arguments[i])); |
9dae56ea A |
170 | |
171 | JSObject* result = constructDate(exec, argList); | |
172 | if (exec->hadException()) { | |
173 | if (exception) | |
ba379fdc | 174 | *exception = toRef(exec, exec->exception()); |
9dae56ea A |
175 | exec->clearException(); |
176 | result = 0; | |
177 | } | |
178 | ||
179 | return toRef(result); | |
180 | } | |
181 | ||
182 | JSObjectRef JSObjectMakeError(JSContextRef ctx, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) | |
183 | { | |
184 | ExecState* exec = toJS(ctx); | |
f9bf01c6 | 185 | APIEntryShim entryShim(exec); |
9dae56ea | 186 | |
ba379fdc | 187 | MarkedArgumentBuffer argList; |
9dae56ea | 188 | for (size_t i = 0; i < argumentCount; ++i) |
ba379fdc | 189 | argList.append(toJS(exec, arguments[i])); |
9dae56ea A |
190 | |
191 | JSObject* result = constructError(exec, argList); | |
192 | if (exec->hadException()) { | |
193 | if (exception) | |
ba379fdc | 194 | *exception = toRef(exec, exec->exception()); |
9dae56ea A |
195 | exec->clearException(); |
196 | result = 0; | |
197 | } | |
198 | ||
199 | return toRef(result); | |
200 | } | |
201 | ||
202 | JSObjectRef JSObjectMakeRegExp(JSContextRef ctx, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) | |
203 | { | |
204 | ExecState* exec = toJS(ctx); | |
f9bf01c6 | 205 | APIEntryShim entryShim(exec); |
9dae56ea | 206 | |
ba379fdc | 207 | MarkedArgumentBuffer argList; |
9dae56ea | 208 | for (size_t i = 0; i < argumentCount; ++i) |
ba379fdc | 209 | argList.append(toJS(exec, arguments[i])); |
9dae56ea A |
210 | |
211 | JSObject* result = constructRegExp(exec, argList); | |
212 | if (exec->hadException()) { | |
213 | if (exception) | |
ba379fdc | 214 | *exception = toRef(exec, exec->exception()); |
9dae56ea A |
215 | exec->clearException(); |
216 | result = 0; | |
217 | } | |
218 | ||
b37bf2e1 A |
219 | return toRef(result); |
220 | } | |
221 | ||
ba379fdc | 222 | JSValueRef JSObjectGetPrototype(JSContextRef ctx, JSObjectRef object) |
b37bf2e1 | 223 | { |
ba379fdc | 224 | ExecState* exec = toJS(ctx); |
f9bf01c6 | 225 | APIEntryShim entryShim(exec); |
ba379fdc | 226 | |
b37bf2e1 | 227 | JSObject* jsObject = toJS(object); |
ba379fdc | 228 | return toRef(exec, jsObject->prototype()); |
b37bf2e1 A |
229 | } |
230 | ||
ba379fdc | 231 | void JSObjectSetPrototype(JSContextRef ctx, JSObjectRef object, JSValueRef value) |
b37bf2e1 | 232 | { |
ba379fdc | 233 | ExecState* exec = toJS(ctx); |
f9bf01c6 | 234 | APIEntryShim entryShim(exec); |
ba379fdc | 235 | |
b37bf2e1 | 236 | JSObject* jsObject = toJS(object); |
ba379fdc | 237 | JSValue jsValue = toJS(exec, value); |
b37bf2e1 | 238 | |
9dae56ea | 239 | jsObject->setPrototype(jsValue.isObject() ? jsValue : jsNull()); |
b37bf2e1 A |
240 | } |
241 | ||
242 | bool JSObjectHasProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName) | |
243 | { | |
b37bf2e1 | 244 | ExecState* exec = toJS(ctx); |
f9bf01c6 | 245 | APIEntryShim entryShim(exec); |
9dae56ea | 246 | |
b37bf2e1 | 247 | JSObject* jsObject = toJS(object); |
b37bf2e1 | 248 | |
9dae56ea | 249 | return jsObject->hasProperty(exec, propertyName->identifier(&exec->globalData())); |
b37bf2e1 A |
250 | } |
251 | ||
252 | JSValueRef JSObjectGetProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception) | |
253 | { | |
b37bf2e1 | 254 | ExecState* exec = toJS(ctx); |
f9bf01c6 | 255 | APIEntryShim entryShim(exec); |
9dae56ea | 256 | |
b37bf2e1 | 257 | JSObject* jsObject = toJS(object); |
b37bf2e1 | 258 | |
ba379fdc | 259 | JSValue jsValue = jsObject->get(exec, propertyName->identifier(&exec->globalData())); |
b37bf2e1 A |
260 | if (exec->hadException()) { |
261 | if (exception) | |
ba379fdc | 262 | *exception = toRef(exec, exec->exception()); |
b37bf2e1 A |
263 | exec->clearException(); |
264 | } | |
ba379fdc | 265 | return toRef(exec, jsValue); |
b37bf2e1 A |
266 | } |
267 | ||
268 | void JSObjectSetProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef value, JSPropertyAttributes attributes, JSValueRef* exception) | |
269 | { | |
b37bf2e1 | 270 | ExecState* exec = toJS(ctx); |
f9bf01c6 | 271 | APIEntryShim entryShim(exec); |
9dae56ea | 272 | |
b37bf2e1 | 273 | JSObject* jsObject = toJS(object); |
9dae56ea | 274 | Identifier name(propertyName->identifier(&exec->globalData())); |
ba379fdc | 275 | JSValue jsValue = toJS(exec, value); |
9dae56ea A |
276 | |
277 | if (attributes && !jsObject->hasProperty(exec, name)) | |
278 | jsObject->putWithAttributes(exec, name, jsValue, attributes); | |
279 | else { | |
280 | PutPropertySlot slot; | |
281 | jsObject->put(exec, name, jsValue, slot); | |
282 | } | |
283 | ||
b37bf2e1 A |
284 | if (exec->hadException()) { |
285 | if (exception) | |
ba379fdc | 286 | *exception = toRef(exec, exec->exception()); |
b37bf2e1 A |
287 | exec->clearException(); |
288 | } | |
289 | } | |
290 | ||
291 | JSValueRef JSObjectGetPropertyAtIndex(JSContextRef ctx, JSObjectRef object, unsigned propertyIndex, JSValueRef* exception) | |
292 | { | |
b37bf2e1 | 293 | ExecState* exec = toJS(ctx); |
f9bf01c6 | 294 | APIEntryShim entryShim(exec); |
9dae56ea | 295 | |
b37bf2e1 A |
296 | JSObject* jsObject = toJS(object); |
297 | ||
ba379fdc | 298 | JSValue jsValue = jsObject->get(exec, propertyIndex); |
b37bf2e1 A |
299 | if (exec->hadException()) { |
300 | if (exception) | |
ba379fdc | 301 | *exception = toRef(exec, exec->exception()); |
b37bf2e1 A |
302 | exec->clearException(); |
303 | } | |
ba379fdc | 304 | return toRef(exec, jsValue); |
b37bf2e1 A |
305 | } |
306 | ||
307 | ||
308 | void JSObjectSetPropertyAtIndex(JSContextRef ctx, JSObjectRef object, unsigned propertyIndex, JSValueRef value, JSValueRef* exception) | |
309 | { | |
b37bf2e1 | 310 | ExecState* exec = toJS(ctx); |
f9bf01c6 | 311 | APIEntryShim entryShim(exec); |
9dae56ea | 312 | |
b37bf2e1 | 313 | JSObject* jsObject = toJS(object); |
ba379fdc | 314 | JSValue jsValue = toJS(exec, value); |
b37bf2e1 A |
315 | |
316 | jsObject->put(exec, propertyIndex, jsValue); | |
317 | if (exec->hadException()) { | |
318 | if (exception) | |
ba379fdc | 319 | *exception = toRef(exec, exec->exception()); |
b37bf2e1 A |
320 | exec->clearException(); |
321 | } | |
322 | } | |
323 | ||
324 | bool JSObjectDeleteProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception) | |
325 | { | |
b37bf2e1 | 326 | ExecState* exec = toJS(ctx); |
f9bf01c6 | 327 | APIEntryShim entryShim(exec); |
9dae56ea | 328 | |
b37bf2e1 | 329 | JSObject* jsObject = toJS(object); |
b37bf2e1 | 330 | |
9dae56ea | 331 | bool result = jsObject->deleteProperty(exec, propertyName->identifier(&exec->globalData())); |
b37bf2e1 A |
332 | if (exec->hadException()) { |
333 | if (exception) | |
ba379fdc | 334 | *exception = toRef(exec, exec->exception()); |
b37bf2e1 A |
335 | exec->clearException(); |
336 | } | |
337 | return result; | |
338 | } | |
339 | ||
340 | void* JSObjectGetPrivate(JSObjectRef object) | |
341 | { | |
342 | JSObject* jsObject = toJS(object); | |
343 | ||
344 | if (jsObject->inherits(&JSCallbackObject<JSGlobalObject>::info)) | |
345 | return static_cast<JSCallbackObject<JSGlobalObject>*>(jsObject)->getPrivate(); | |
346 | else if (jsObject->inherits(&JSCallbackObject<JSObject>::info)) | |
347 | return static_cast<JSCallbackObject<JSObject>*>(jsObject)->getPrivate(); | |
348 | ||
349 | return 0; | |
350 | } | |
351 | ||
352 | bool JSObjectSetPrivate(JSObjectRef object, void* data) | |
353 | { | |
354 | JSObject* jsObject = toJS(object); | |
355 | ||
356 | if (jsObject->inherits(&JSCallbackObject<JSGlobalObject>::info)) { | |
357 | static_cast<JSCallbackObject<JSGlobalObject>*>(jsObject)->setPrivate(data); | |
358 | return true; | |
359 | } else if (jsObject->inherits(&JSCallbackObject<JSObject>::info)) { | |
360 | static_cast<JSCallbackObject<JSObject>*>(jsObject)->setPrivate(data); | |
361 | return true; | |
362 | } | |
363 | ||
364 | return false; | |
365 | } | |
366 | ||
4e4e5a6f A |
367 | JSValueRef JSObjectGetPrivateProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName) |
368 | { | |
369 | ExecState* exec = toJS(ctx); | |
370 | APIEntryShim entryShim(exec); | |
371 | JSObject* jsObject = toJS(object); | |
372 | JSValue result; | |
373 | Identifier name(propertyName->identifier(&exec->globalData())); | |
374 | if (jsObject->inherits(&JSCallbackObject<JSGlobalObject>::info)) | |
375 | result = static_cast<JSCallbackObject<JSGlobalObject>*>(jsObject)->getPrivateProperty(name); | |
376 | else if (jsObject->inherits(&JSCallbackObject<JSObject>::info)) | |
377 | result = static_cast<JSCallbackObject<JSObject>*>(jsObject)->getPrivateProperty(name); | |
378 | return toRef(exec, result); | |
379 | } | |
380 | ||
381 | bool JSObjectSetPrivateProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef value) | |
382 | { | |
383 | ExecState* exec = toJS(ctx); | |
384 | APIEntryShim entryShim(exec); | |
385 | JSObject* jsObject = toJS(object); | |
386 | JSValue jsValue = toJS(exec, value); | |
387 | Identifier name(propertyName->identifier(&exec->globalData())); | |
388 | if (jsObject->inherits(&JSCallbackObject<JSGlobalObject>::info)) { | |
389 | static_cast<JSCallbackObject<JSGlobalObject>*>(jsObject)->setPrivateProperty(name, jsValue); | |
390 | return true; | |
391 | } | |
392 | if (jsObject->inherits(&JSCallbackObject<JSObject>::info)) { | |
393 | static_cast<JSCallbackObject<JSObject>*>(jsObject)->setPrivateProperty(name, jsValue); | |
394 | return true; | |
395 | } | |
396 | return false; | |
397 | } | |
398 | ||
399 | bool JSObjectDeletePrivateProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName) | |
400 | { | |
401 | ExecState* exec = toJS(ctx); | |
402 | APIEntryShim entryShim(exec); | |
403 | JSObject* jsObject = toJS(object); | |
404 | Identifier name(propertyName->identifier(&exec->globalData())); | |
405 | if (jsObject->inherits(&JSCallbackObject<JSGlobalObject>::info)) { | |
406 | static_cast<JSCallbackObject<JSGlobalObject>*>(jsObject)->deletePrivateProperty(name); | |
407 | return true; | |
408 | } | |
409 | if (jsObject->inherits(&JSCallbackObject<JSObject>::info)) { | |
410 | static_cast<JSCallbackObject<JSObject>*>(jsObject)->deletePrivateProperty(name); | |
411 | return true; | |
412 | } | |
413 | return false; | |
414 | } | |
415 | ||
b37bf2e1 A |
416 | bool JSObjectIsFunction(JSContextRef, JSObjectRef object) |
417 | { | |
9dae56ea A |
418 | CallData callData; |
419 | return toJS(object)->getCallData(callData) != CallTypeNone; | |
b37bf2e1 A |
420 | } |
421 | ||
422 | JSValueRef JSObjectCallAsFunction(JSContextRef ctx, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) | |
423 | { | |
b37bf2e1 | 424 | ExecState* exec = toJS(ctx); |
f9bf01c6 | 425 | APIEntryShim entryShim(exec); |
9dae56ea | 426 | |
b37bf2e1 A |
427 | JSObject* jsObject = toJS(object); |
428 | JSObject* jsThisObject = toJS(thisObject); | |
429 | ||
430 | if (!jsThisObject) | |
9dae56ea A |
431 | jsThisObject = exec->globalThisValue(); |
432 | ||
ba379fdc | 433 | MarkedArgumentBuffer argList; |
b37bf2e1 | 434 | for (size_t i = 0; i < argumentCount; i++) |
ba379fdc | 435 | argList.append(toJS(exec, arguments[i])); |
b37bf2e1 | 436 | |
9dae56ea A |
437 | CallData callData; |
438 | CallType callType = jsObject->getCallData(callData); | |
439 | if (callType == CallTypeNone) | |
440 | return 0; | |
441 | ||
ba379fdc | 442 | JSValueRef result = toRef(exec, call(exec, jsObject, callType, callData, jsThisObject, argList)); |
b37bf2e1 A |
443 | if (exec->hadException()) { |
444 | if (exception) | |
ba379fdc | 445 | *exception = toRef(exec, exec->exception()); |
b37bf2e1 A |
446 | exec->clearException(); |
447 | result = 0; | |
448 | } | |
449 | return result; | |
450 | } | |
451 | ||
452 | bool JSObjectIsConstructor(JSContextRef, JSObjectRef object) | |
453 | { | |
454 | JSObject* jsObject = toJS(object); | |
9dae56ea A |
455 | ConstructData constructData; |
456 | return jsObject->getConstructData(constructData) != ConstructTypeNone; | |
b37bf2e1 A |
457 | } |
458 | ||
459 | JSObjectRef JSObjectCallAsConstructor(JSContextRef ctx, JSObjectRef object, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) | |
460 | { | |
b37bf2e1 | 461 | ExecState* exec = toJS(ctx); |
f9bf01c6 | 462 | APIEntryShim entryShim(exec); |
9dae56ea | 463 | |
b37bf2e1 | 464 | JSObject* jsObject = toJS(object); |
9dae56ea A |
465 | |
466 | ConstructData constructData; | |
467 | ConstructType constructType = jsObject->getConstructData(constructData); | |
468 | if (constructType == ConstructTypeNone) | |
469 | return 0; | |
470 | ||
ba379fdc | 471 | MarkedArgumentBuffer argList; |
b37bf2e1 | 472 | for (size_t i = 0; i < argumentCount; i++) |
ba379fdc | 473 | argList.append(toJS(exec, arguments[i])); |
9dae56ea | 474 | JSObjectRef result = toRef(construct(exec, jsObject, constructType, constructData, argList)); |
b37bf2e1 A |
475 | if (exec->hadException()) { |
476 | if (exception) | |
ba379fdc | 477 | *exception = toRef(exec, exec->exception()); |
b37bf2e1 A |
478 | exec->clearException(); |
479 | result = 0; | |
480 | } | |
481 | return result; | |
482 | } | |
483 | ||
f9bf01c6 | 484 | struct OpaqueJSPropertyNameArray : FastAllocBase { |
9dae56ea A |
485 | OpaqueJSPropertyNameArray(JSGlobalData* globalData) |
486 | : refCount(0) | |
487 | , globalData(globalData) | |
b37bf2e1 A |
488 | { |
489 | } | |
490 | ||
491 | unsigned refCount; | |
9dae56ea A |
492 | JSGlobalData* globalData; |
493 | Vector<JSRetainPtr<JSStringRef> > array; | |
b37bf2e1 A |
494 | }; |
495 | ||
496 | JSPropertyNameArrayRef JSObjectCopyPropertyNames(JSContextRef ctx, JSObjectRef object) | |
497 | { | |
b37bf2e1 A |
498 | JSObject* jsObject = toJS(object); |
499 | ExecState* exec = toJS(ctx); | |
f9bf01c6 | 500 | APIEntryShim entryShim(exec); |
9dae56ea A |
501 | |
502 | JSGlobalData* globalData = &exec->globalData(); | |
503 | ||
504 | JSPropertyNameArrayRef propertyNames = new OpaqueJSPropertyNameArray(globalData); | |
505 | PropertyNameArray array(globalData); | |
506 | jsObject->getPropertyNames(exec, array); | |
507 | ||
508 | size_t size = array.size(); | |
509 | propertyNames->array.reserveInitialCapacity(size); | |
510 | for (size_t i = 0; i < size; ++i) | |
511 | propertyNames->array.append(JSRetainPtr<JSStringRef>(Adopt, OpaqueJSString::create(array[i].ustring()).releaseRef())); | |
b37bf2e1 A |
512 | |
513 | return JSPropertyNameArrayRetain(propertyNames); | |
514 | } | |
515 | ||
516 | JSPropertyNameArrayRef JSPropertyNameArrayRetain(JSPropertyNameArrayRef array) | |
517 | { | |
b37bf2e1 A |
518 | ++array->refCount; |
519 | return array; | |
520 | } | |
521 | ||
522 | void JSPropertyNameArrayRelease(JSPropertyNameArrayRef array) | |
523 | { | |
9dae56ea | 524 | if (--array->refCount == 0) { |
f9bf01c6 | 525 | APIEntryShim entryShim(array->globalData, false); |
b37bf2e1 | 526 | delete array; |
9dae56ea | 527 | } |
b37bf2e1 A |
528 | } |
529 | ||
530 | size_t JSPropertyNameArrayGetCount(JSPropertyNameArrayRef array) | |
531 | { | |
532 | return array->array.size(); | |
533 | } | |
534 | ||
535 | JSStringRef JSPropertyNameArrayGetNameAtIndex(JSPropertyNameArrayRef array, size_t index) | |
536 | { | |
9dae56ea | 537 | return array->array[static_cast<unsigned>(index)].get(); |
b37bf2e1 A |
538 | } |
539 | ||
540 | void JSPropertyNameAccumulatorAddName(JSPropertyNameAccumulatorRef array, JSStringRef propertyName) | |
541 | { | |
b37bf2e1 | 542 | PropertyNameArray* propertyNames = toJS(array); |
f9bf01c6 | 543 | APIEntryShim entryShim(propertyNames->globalData()); |
9dae56ea | 544 | propertyNames->add(propertyName->identifier(propertyNames->globalData())); |
b37bf2e1 | 545 | } |