]>
Commit | Line | Data |
---|---|---|
b37bf2e1 | 1 | /* |
9dae56ea | 2 | * Copyright (C) 2006, 2008 Apple Inc. All rights reserved. |
b37bf2e1 A |
3 | * Copyright (C) 2007 Eric Seidel <eric@webkit.org> |
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 | * | |
81345200 | 14 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
b37bf2e1 A |
15 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
81345200 | 17 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
b37bf2e1 A |
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 | ||
b37bf2e1 | 27 | #include "APICast.h" |
9dae56ea | 28 | #include "Error.h" |
14957cd0 | 29 | #include "ExceptionHelpers.h" |
b37bf2e1 A |
30 | #include "JSCallbackFunction.h" |
31 | #include "JSClassRef.h" | |
14957cd0 | 32 | #include "JSFunction.h" |
b37bf2e1 | 33 | #include "JSGlobalObject.h" |
9dae56ea A |
34 | #include "JSLock.h" |
35 | #include "JSObjectRef.h" | |
36 | #include "JSString.h" | |
b37bf2e1 | 37 | #include "JSStringRef.h" |
9dae56ea | 38 | #include "OpaqueJSString.h" |
b37bf2e1 | 39 | #include "PropertyNameArray.h" |
b37bf2e1 A |
40 | #include <wtf/Vector.h> |
41 | ||
9dae56ea A |
42 | namespace JSC { |
43 | ||
6fe7ccc8 A |
44 | template <class Parent> |
45 | inline JSCallbackObject<Parent>* JSCallbackObject<Parent>::asCallbackObject(JSValue value) | |
9dae56ea | 46 | { |
81345200 | 47 | ASSERT(asObject(value)->inherits(info())); |
6fe7ccc8 | 48 | return jsCast<JSCallbackObject*>(asObject(value)); |
9dae56ea | 49 | } |
b37bf2e1 | 50 | |
81345200 A |
51 | template <class Parent> |
52 | inline JSCallbackObject<Parent>* JSCallbackObject<Parent>::asCallbackObject(EncodedJSValue value) | |
53 | { | |
54 | ASSERT(asObject(JSValue::decode(value))->inherits(info())); | |
55 | return jsCast<JSCallbackObject*>(asObject(JSValue::decode(value))); | |
56 | } | |
57 | ||
6fe7ccc8 A |
58 | template <class Parent> |
59 | JSCallbackObject<Parent>::JSCallbackObject(ExecState* exec, Structure* structure, JSClassRef jsClass, void* data) | |
93a37866 | 60 | : Parent(exec->vm(), structure) |
ed1e77d3 | 61 | , m_callbackObjectData(std::make_unique<JSCallbackObjectData>(data, jsClass)) |
b37bf2e1 | 62 | { |
b37bf2e1 A |
63 | } |
64 | ||
ed1e77d3 A |
65 | extern const GlobalObjectMethodTable javaScriptCoreAPIGlobalObjectMethodTable; |
66 | ||
9dae56ea A |
67 | // Global object constructor. |
68 | // FIXME: Move this into a separate JSGlobalCallbackObject class derived from this one. | |
6fe7ccc8 | 69 | template <class Parent> |
93a37866 | 70 | JSCallbackObject<Parent>::JSCallbackObject(VM& vm, JSClassRef jsClass, Structure* structure) |
ed1e77d3 A |
71 | : Parent(vm, structure, &javaScriptCoreAPIGlobalObjectMethodTable) |
72 | , m_callbackObjectData(std::make_unique<JSCallbackObjectData>(nullptr, jsClass)) | |
b37bf2e1 | 73 | { |
b37bf2e1 A |
74 | } |
75 | ||
ed1e77d3 A |
76 | template <class Parent> |
77 | JSCallbackObject<Parent>::~JSCallbackObject() | |
78 | { | |
79 | JSObjectRef thisRef = toRef(static_cast<JSObject*>(this)); | |
80 | for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass) { | |
81 | if (JSObjectFinalizeCallback finalize = jsClass->finalize) | |
82 | finalize(thisRef); | |
83 | } | |
84 | } | |
85 | ||
6fe7ccc8 A |
86 | template <class Parent> |
87 | void JSCallbackObject<Parent>::finishCreation(ExecState* exec) | |
88 | { | |
93a37866 | 89 | Base::finishCreation(exec->vm()); |
81345200 | 90 | ASSERT(Parent::inherits(info())); |
6fe7ccc8 A |
91 | init(exec); |
92 | } | |
93 | ||
94 | // This is just for Global object, so we can assume that Base::finishCreation is JSGlobalObject::finishCreation. | |
95 | template <class Parent> | |
93a37866 | 96 | void JSCallbackObject<Parent>::finishCreation(VM& vm) |
6fe7ccc8 | 97 | { |
81345200 | 98 | ASSERT(Parent::inherits(info())); |
6fe7ccc8 | 99 | ASSERT(Parent::isGlobalObject()); |
93a37866 | 100 | Base::finishCreation(vm); |
6fe7ccc8 A |
101 | init(jsCast<JSGlobalObject*>(this)->globalExec()); |
102 | } | |
103 | ||
104 | template <class Parent> | |
105 | void JSCallbackObject<Parent>::init(ExecState* exec) | |
b37bf2e1 A |
106 | { |
107 | ASSERT(exec); | |
108 | ||
109 | Vector<JSObjectInitializeCallback, 16> initRoutines; | |
9dae56ea | 110 | JSClassRef jsClass = classRef(); |
b37bf2e1 A |
111 | do { |
112 | if (JSObjectInitializeCallback initialize = jsClass->initialize) | |
113 | initRoutines.append(initialize); | |
114 | } while ((jsClass = jsClass->parentClass)); | |
115 | ||
116 | // initialize from base to derived | |
117 | for (int i = static_cast<int>(initRoutines.size()) - 1; i >= 0; i--) { | |
81345200 | 118 | JSLock::DropAllLocks dropAllLocks(exec); |
b37bf2e1 A |
119 | JSObjectInitializeCallback initialize = initRoutines[i]; |
120 | initialize(toRef(exec), toRef(this)); | |
121 | } | |
b37bf2e1 A |
122 | } |
123 | ||
6fe7ccc8 | 124 | template <class Parent> |
93a37866 | 125 | String JSCallbackObject<Parent>::className(const JSObject* object) |
b37bf2e1 | 126 | { |
6fe7ccc8 | 127 | const JSCallbackObject* thisObject = jsCast<const JSCallbackObject*>(object); |
93a37866 | 128 | String thisClassName = thisObject->classRef()->className(); |
ba379fdc | 129 | if (!thisClassName.isEmpty()) |
9dae56ea | 130 | return thisClassName; |
b37bf2e1 | 131 | |
6fe7ccc8 | 132 | return Parent::className(object); |
b37bf2e1 A |
133 | } |
134 | ||
6fe7ccc8 | 135 | template <class Parent> |
81345200 | 136 | bool JSCallbackObject<Parent>::getOwnPropertySlot(JSObject* object, ExecState* exec, PropertyName propertyName, PropertySlot& slot) |
b37bf2e1 | 137 | { |
81345200 | 138 | JSCallbackObject* thisObject = jsCast<JSCallbackObject*>(object); |
b37bf2e1 | 139 | JSContextRef ctx = toRef(exec); |
6fe7ccc8 | 140 | JSObjectRef thisRef = toRef(thisObject); |
9dae56ea | 141 | RefPtr<OpaqueJSString> propertyNameRef; |
b37bf2e1 | 142 | |
81345200 | 143 | if (StringImpl* name = propertyName.uid()) { |
93a37866 A |
144 | for (JSClassRef jsClass = thisObject->classRef(); jsClass; jsClass = jsClass->parentClass) { |
145 | // optional optimization to bypass getProperty in cases when we only need to know if the property exists | |
146 | if (JSObjectHasPropertyCallback hasProperty = jsClass->hasProperty) { | |
147 | if (!propertyNameRef) | |
148 | propertyNameRef = OpaqueJSString::create(name); | |
81345200 | 149 | JSLock::DropAllLocks dropAllLocks(exec); |
93a37866 | 150 | if (hasProperty(ctx, thisRef, propertyNameRef.get())) { |
81345200 | 151 | slot.setCustom(thisObject, ReadOnly | DontEnum, callbackGetter); |
93a37866 A |
152 | return true; |
153 | } | |
154 | } else if (JSObjectGetPropertyCallback getProperty = jsClass->getProperty) { | |
155 | if (!propertyNameRef) | |
156 | propertyNameRef = OpaqueJSString::create(name); | |
157 | JSValueRef exception = 0; | |
158 | JSValueRef value; | |
159 | { | |
81345200 | 160 | JSLock::DropAllLocks dropAllLocks(exec); |
93a37866 A |
161 | value = getProperty(ctx, thisRef, propertyNameRef.get(), &exception); |
162 | } | |
163 | if (exception) { | |
81345200 A |
164 | exec->vm().throwException(exec, toJS(exec, exception)); |
165 | slot.setValue(thisObject, ReadOnly | DontEnum, jsUndefined()); | |
93a37866 A |
166 | return true; |
167 | } | |
14957cd0 | 168 | if (value) { |
81345200 | 169 | slot.setValue(thisObject, ReadOnly | DontEnum, toJS(exec, value)); |
14957cd0 A |
170 | return true; |
171 | } | |
b37bf2e1 | 172 | } |
93a37866 A |
173 | |
174 | if (OpaqueJSClassStaticValuesTable* staticValues = jsClass->staticValues(exec)) { | |
175 | if (staticValues->contains(name)) { | |
176 | JSValue value = thisObject->getStaticValue(exec, propertyName); | |
177 | if (value) { | |
81345200 | 178 | slot.setValue(thisObject, ReadOnly | DontEnum, value); |
93a37866 A |
179 | return true; |
180 | } | |
181 | } | |
182 | } | |
183 | ||
184 | if (OpaqueJSClassStaticFunctionsTable* staticFunctions = jsClass->staticFunctions(exec)) { | |
185 | if (staticFunctions->contains(name)) { | |
81345200 | 186 | slot.setCustom(thisObject, ReadOnly | DontEnum, staticFunctionGetter); |
93a37866 A |
187 | return true; |
188 | } | |
b37bf2e1 A |
189 | } |
190 | } | |
191 | } | |
93a37866 | 192 | |
6fe7ccc8 A |
193 | return Parent::getOwnPropertySlot(thisObject, exec, propertyName, slot); |
194 | } | |
195 | ||
93a37866 | 196 | template <class Parent> |
81345200 | 197 | bool JSCallbackObject<Parent>::getOwnPropertySlotByIndex(JSObject* object, ExecState* exec, unsigned propertyName, PropertySlot& slot) |
93a37866 | 198 | { |
81345200 | 199 | return object->methodTable()->getOwnPropertySlot(object, exec, Identifier::from(exec, propertyName), slot); |
93a37866 A |
200 | } |
201 | ||
6fe7ccc8 A |
202 | template <class Parent> |
203 | JSValue JSCallbackObject<Parent>::defaultValue(const JSObject* object, ExecState* exec, PreferredPrimitiveType hint) | |
204 | { | |
205 | const JSCallbackObject* thisObject = jsCast<const JSCallbackObject*>(object); | |
206 | JSContextRef ctx = toRef(exec); | |
207 | JSObjectRef thisRef = toRef(thisObject); | |
208 | ::JSType jsHint = hint == PreferString ? kJSTypeString : kJSTypeNumber; | |
209 | ||
210 | for (JSClassRef jsClass = thisObject->classRef(); jsClass; jsClass = jsClass->parentClass) { | |
211 | if (JSObjectConvertToTypeCallback convertToType = jsClass->convertToType) { | |
212 | JSValueRef exception = 0; | |
213 | JSValueRef result = convertToType(ctx, thisRef, jsHint, &exception); | |
214 | if (exception) { | |
81345200 | 215 | exec->vm().throwException(exec, toJS(exec, exception)); |
6fe7ccc8 A |
216 | return jsUndefined(); |
217 | } | |
218 | if (result) | |
219 | return toJS(exec, result); | |
220 | } | |
221 | } | |
222 | ||
223 | return Parent::defaultValue(object, exec, hint); | |
b37bf2e1 A |
224 | } |
225 | ||
6fe7ccc8 | 226 | template <class Parent> |
93a37866 | 227 | void JSCallbackObject<Parent>::put(JSCell* cell, ExecState* exec, PropertyName propertyName, JSValue value, PutPropertySlot& slot) |
b37bf2e1 | 228 | { |
6fe7ccc8 | 229 | JSCallbackObject* thisObject = jsCast<JSCallbackObject*>(cell); |
b37bf2e1 | 230 | JSContextRef ctx = toRef(exec); |
6fe7ccc8 | 231 | JSObjectRef thisRef = toRef(thisObject); |
9dae56ea | 232 | RefPtr<OpaqueJSString> propertyNameRef; |
ba379fdc | 233 | JSValueRef valueRef = toRef(exec, value); |
b37bf2e1 | 234 | |
81345200 | 235 | if (StringImpl* name = propertyName.uid()) { |
93a37866 A |
236 | for (JSClassRef jsClass = thisObject->classRef(); jsClass; jsClass = jsClass->parentClass) { |
237 | if (JSObjectSetPropertyCallback setProperty = jsClass->setProperty) { | |
238 | if (!propertyNameRef) | |
239 | propertyNameRef = OpaqueJSString::create(name); | |
240 | JSValueRef exception = 0; | |
241 | bool result; | |
242 | { | |
81345200 | 243 | JSLock::DropAllLocks dropAllLocks(exec); |
93a37866 A |
244 | result = setProperty(ctx, thisRef, propertyNameRef.get(), valueRef, &exception); |
245 | } | |
246 | if (exception) | |
81345200 | 247 | exec->vm().throwException(exec, toJS(exec, exception)); |
93a37866 A |
248 | if (result || exception) |
249 | return; | |
250 | } | |
251 | ||
252 | if (OpaqueJSClassStaticValuesTable* staticValues = jsClass->staticValues(exec)) { | |
253 | if (StaticValueEntry* entry = staticValues->get(name)) { | |
254 | if (entry->attributes & kJSPropertyAttributeReadOnly) | |
255 | return; | |
256 | if (JSObjectSetPropertyCallback setProperty = entry->setProperty) { | |
93a37866 A |
257 | JSValueRef exception = 0; |
258 | bool result; | |
259 | { | |
81345200 A |
260 | JSLock::DropAllLocks dropAllLocks(exec); |
261 | result = setProperty(ctx, thisRef, entry->propertyNameRef.get(), valueRef, &exception); | |
93a37866 A |
262 | } |
263 | if (exception) | |
81345200 | 264 | exec->vm().throwException(exec, toJS(exec, exception)); |
93a37866 A |
265 | if (result || exception) |
266 | return; | |
267 | } | |
268 | } | |
269 | } | |
270 | ||
271 | if (OpaqueJSClassStaticFunctionsTable* staticFunctions = jsClass->staticFunctions(exec)) { | |
272 | if (StaticFunctionEntry* entry = staticFunctions->get(name)) { | |
ed1e77d3 A |
273 | PropertySlot getSlot(thisObject); |
274 | if (Parent::getOwnPropertySlot(thisObject, exec, propertyName, getSlot)) | |
275 | return Parent::put(thisObject, exec, propertyName, value, slot); | |
93a37866 A |
276 | if (entry->attributes & kJSPropertyAttributeReadOnly) |
277 | return; | |
278 | thisObject->JSCallbackObject<Parent>::putDirect(exec->vm(), propertyName, value); // put as override property | |
279 | return; | |
280 | } | |
281 | } | |
282 | } | |
283 | } | |
284 | ||
285 | return Parent::put(thisObject, exec, propertyName, value, slot); | |
286 | } | |
287 | ||
288 | template <class Parent> | |
289 | void JSCallbackObject<Parent>::putByIndex(JSCell* cell, ExecState* exec, unsigned propertyIndex, JSValue value, bool shouldThrow) | |
290 | { | |
291 | JSCallbackObject* thisObject = jsCast<JSCallbackObject*>(cell); | |
292 | JSContextRef ctx = toRef(exec); | |
293 | JSObjectRef thisRef = toRef(thisObject); | |
294 | RefPtr<OpaqueJSString> propertyNameRef; | |
295 | JSValueRef valueRef = toRef(exec, value); | |
81345200 | 296 | Identifier propertyName = Identifier::from(exec, propertyIndex); |
93a37866 | 297 | |
6fe7ccc8 | 298 | for (JSClassRef jsClass = thisObject->classRef(); jsClass; jsClass = jsClass->parentClass) { |
b37bf2e1 | 299 | if (JSObjectSetPropertyCallback setProperty = jsClass->setProperty) { |
9dae56ea | 300 | if (!propertyNameRef) |
93a37866 | 301 | propertyNameRef = OpaqueJSString::create(propertyName.impl()); |
ba379fdc A |
302 | JSValueRef exception = 0; |
303 | bool result; | |
304 | { | |
81345200 | 305 | JSLock::DropAllLocks dropAllLocks(exec); |
ba379fdc A |
306 | result = setProperty(ctx, thisRef, propertyNameRef.get(), valueRef, &exception); |
307 | } | |
f9bf01c6 | 308 | if (exception) |
81345200 | 309 | exec->vm().throwException(exec, toJS(exec, exception)); |
ba379fdc | 310 | if (result || exception) |
b37bf2e1 A |
311 | return; |
312 | } | |
93a37866 | 313 | |
9dae56ea | 314 | if (OpaqueJSClassStaticValuesTable* staticValues = jsClass->staticValues(exec)) { |
14957cd0 | 315 | if (StaticValueEntry* entry = staticValues->get(propertyName.impl())) { |
b37bf2e1 A |
316 | if (entry->attributes & kJSPropertyAttributeReadOnly) |
317 | return; | |
318 | if (JSObjectSetPropertyCallback setProperty = entry->setProperty) { | |
ba379fdc A |
319 | JSValueRef exception = 0; |
320 | bool result; | |
321 | { | |
81345200 A |
322 | JSLock::DropAllLocks dropAllLocks(exec); |
323 | result = setProperty(ctx, thisRef, entry->propertyNameRef.get(), valueRef, &exception); | |
ba379fdc | 324 | } |
f9bf01c6 | 325 | if (exception) |
81345200 | 326 | exec->vm().throwException(exec, toJS(exec, exception)); |
ba379fdc | 327 | if (result || exception) |
b37bf2e1 | 328 | return; |
14957cd0 | 329 | } |
b37bf2e1 A |
330 | } |
331 | } | |
93a37866 | 332 | |
9dae56ea | 333 | if (OpaqueJSClassStaticFunctionsTable* staticFunctions = jsClass->staticFunctions(exec)) { |
14957cd0 | 334 | if (StaticFunctionEntry* entry = staticFunctions->get(propertyName.impl())) { |
b37bf2e1 A |
335 | if (entry->attributes & kJSPropertyAttributeReadOnly) |
336 | return; | |
93a37866 | 337 | break; |
b37bf2e1 A |
338 | } |
339 | } | |
340 | } | |
93a37866 A |
341 | |
342 | return Parent::putByIndex(thisObject, exec, propertyIndex, value, shouldThrow); | |
b37bf2e1 A |
343 | } |
344 | ||
6fe7ccc8 | 345 | template <class Parent> |
93a37866 | 346 | bool JSCallbackObject<Parent>::deleteProperty(JSCell* cell, ExecState* exec, PropertyName propertyName) |
b37bf2e1 | 347 | { |
6fe7ccc8 | 348 | JSCallbackObject* thisObject = jsCast<JSCallbackObject*>(cell); |
b37bf2e1 | 349 | JSContextRef ctx = toRef(exec); |
6fe7ccc8 | 350 | JSObjectRef thisRef = toRef(thisObject); |
9dae56ea | 351 | RefPtr<OpaqueJSString> propertyNameRef; |
b37bf2e1 | 352 | |
81345200 | 353 | if (StringImpl* name = propertyName.uid()) { |
93a37866 A |
354 | for (JSClassRef jsClass = thisObject->classRef(); jsClass; jsClass = jsClass->parentClass) { |
355 | if (JSObjectDeletePropertyCallback deleteProperty = jsClass->deleteProperty) { | |
356 | if (!propertyNameRef) | |
357 | propertyNameRef = OpaqueJSString::create(name); | |
358 | JSValueRef exception = 0; | |
359 | bool result; | |
360 | { | |
81345200 | 361 | JSLock::DropAllLocks dropAllLocks(exec); |
93a37866 A |
362 | result = deleteProperty(ctx, thisRef, propertyNameRef.get(), &exception); |
363 | } | |
364 | if (exception) | |
81345200 | 365 | exec->vm().throwException(exec, toJS(exec, exception)); |
93a37866 A |
366 | if (result || exception) |
367 | return true; | |
ba379fdc | 368 | } |
93a37866 A |
369 | |
370 | if (OpaqueJSClassStaticValuesTable* staticValues = jsClass->staticValues(exec)) { | |
371 | if (StaticValueEntry* entry = staticValues->get(name)) { | |
372 | if (entry->attributes & kJSPropertyAttributeDontDelete) | |
373 | return false; | |
374 | return true; | |
375 | } | |
b37bf2e1 | 376 | } |
93a37866 A |
377 | |
378 | if (OpaqueJSClassStaticFunctionsTable* staticFunctions = jsClass->staticFunctions(exec)) { | |
379 | if (StaticFunctionEntry* entry = staticFunctions->get(name)) { | |
380 | if (entry->attributes & kJSPropertyAttributeDontDelete) | |
381 | return false; | |
382 | return true; | |
383 | } | |
b37bf2e1 A |
384 | } |
385 | } | |
386 | } | |
93a37866 | 387 | |
6fe7ccc8 | 388 | return Parent::deleteProperty(thisObject, exec, propertyName); |
b37bf2e1 A |
389 | } |
390 | ||
6fe7ccc8 A |
391 | template <class Parent> |
392 | bool JSCallbackObject<Parent>::deletePropertyByIndex(JSCell* cell, ExecState* exec, unsigned propertyName) | |
b37bf2e1 | 393 | { |
6fe7ccc8 A |
394 | JSCallbackObject* thisObject = jsCast<JSCallbackObject*>(cell); |
395 | return thisObject->methodTable()->deleteProperty(thisObject, exec, Identifier::from(exec, propertyName)); | |
b37bf2e1 A |
396 | } |
397 | ||
6fe7ccc8 A |
398 | template <class Parent> |
399 | ConstructType JSCallbackObject<Parent>::getConstructData(JSCell* cell, ConstructData& constructData) | |
b37bf2e1 | 400 | { |
6fe7ccc8 A |
401 | JSCallbackObject* thisObject = jsCast<JSCallbackObject*>(cell); |
402 | for (JSClassRef jsClass = thisObject->classRef(); jsClass; jsClass = jsClass->parentClass) { | |
9dae56ea A |
403 | if (jsClass->callAsConstructor) { |
404 | constructData.native.function = construct; | |
405 | return ConstructTypeHost; | |
406 | } | |
407 | } | |
408 | return ConstructTypeNone; | |
b37bf2e1 A |
409 | } |
410 | ||
6fe7ccc8 A |
411 | template <class Parent> |
412 | EncodedJSValue JSCallbackObject<Parent>::construct(ExecState* exec) | |
b37bf2e1 | 413 | { |
14957cd0 | 414 | JSObject* constructor = exec->callee(); |
b37bf2e1 | 415 | JSContextRef execRef = toRef(exec); |
9dae56ea | 416 | JSObjectRef constructorRef = toRef(constructor); |
b37bf2e1 | 417 | |
6fe7ccc8 | 418 | for (JSClassRef jsClass = jsCast<JSCallbackObject<Parent>*>(constructor)->classRef(); jsClass; jsClass = jsClass->parentClass) { |
b37bf2e1 | 419 | if (JSObjectCallAsConstructorCallback callAsConstructor = jsClass->callAsConstructor) { |
93a37866 A |
420 | size_t argumentCount = exec->argumentCount(); |
421 | Vector<JSValueRef, 16> arguments; | |
422 | arguments.reserveInitialCapacity(argumentCount); | |
423 | for (size_t i = 0; i < argumentCount; ++i) | |
81345200 | 424 | arguments.uncheckedAppend(toRef(exec, exec->uncheckedArgument(i))); |
ba379fdc A |
425 | JSValueRef exception = 0; |
426 | JSObject* result; | |
427 | { | |
81345200 | 428 | JSLock::DropAllLocks dropAllLocks(exec); |
ba379fdc A |
429 | result = toJS(callAsConstructor(execRef, constructorRef, argumentCount, arguments.data(), &exception)); |
430 | } | |
f9bf01c6 | 431 | if (exception) |
81345200 | 432 | exec->vm().throwException(exec, toJS(exec, exception)); |
14957cd0 | 433 | return JSValue::encode(result); |
b37bf2e1 A |
434 | } |
435 | } | |
436 | ||
93a37866 | 437 | RELEASE_ASSERT_NOT_REACHED(); // getConstructData should prevent us from reaching here |
14957cd0 | 438 | return JSValue::encode(JSValue()); |
b37bf2e1 A |
439 | } |
440 | ||
6fe7ccc8 | 441 | template <class Parent> |
93a37866 | 442 | bool JSCallbackObject<Parent>::customHasInstance(JSObject* object, ExecState* exec, JSValue value) |
b37bf2e1 | 443 | { |
6fe7ccc8 | 444 | JSCallbackObject* thisObject = jsCast<JSCallbackObject*>(object); |
b37bf2e1 | 445 | JSContextRef execRef = toRef(exec); |
6fe7ccc8 | 446 | JSObjectRef thisRef = toRef(thisObject); |
b37bf2e1 | 447 | |
6fe7ccc8 | 448 | for (JSClassRef jsClass = thisObject->classRef(); jsClass; jsClass = jsClass->parentClass) { |
b37bf2e1 | 449 | if (JSObjectHasInstanceCallback hasInstance = jsClass->hasInstance) { |
ba379fdc A |
450 | JSValueRef valueRef = toRef(exec, value); |
451 | JSValueRef exception = 0; | |
452 | bool result; | |
453 | { | |
81345200 | 454 | JSLock::DropAllLocks dropAllLocks(exec); |
ba379fdc A |
455 | result = hasInstance(execRef, thisRef, valueRef, &exception); |
456 | } | |
f9bf01c6 | 457 | if (exception) |
81345200 | 458 | exec->vm().throwException(exec, toJS(exec, exception)); |
ba379fdc | 459 | return result; |
b37bf2e1 | 460 | } |
9dae56ea A |
461 | } |
462 | return false; | |
b37bf2e1 A |
463 | } |
464 | ||
6fe7ccc8 A |
465 | template <class Parent> |
466 | CallType JSCallbackObject<Parent>::getCallData(JSCell* cell, CallData& callData) | |
b37bf2e1 | 467 | { |
6fe7ccc8 A |
468 | JSCallbackObject* thisObject = jsCast<JSCallbackObject*>(cell); |
469 | for (JSClassRef jsClass = thisObject->classRef(); jsClass; jsClass = jsClass->parentClass) { | |
9dae56ea A |
470 | if (jsClass->callAsFunction) { |
471 | callData.native.function = call; | |
472 | return CallTypeHost; | |
473 | } | |
474 | } | |
475 | return CallTypeNone; | |
b37bf2e1 A |
476 | } |
477 | ||
6fe7ccc8 A |
478 | template <class Parent> |
479 | EncodedJSValue JSCallbackObject<Parent>::call(ExecState* exec) | |
b37bf2e1 A |
480 | { |
481 | JSContextRef execRef = toRef(exec); | |
14957cd0 | 482 | JSObjectRef functionRef = toRef(exec->callee()); |
81345200 | 483 | JSObjectRef thisObjRef = toRef(jsCast<JSObject*>(exec->thisValue().toThis(exec, NotStrictMode))); |
b37bf2e1 | 484 | |
6fe7ccc8 | 485 | for (JSClassRef jsClass = jsCast<JSCallbackObject<Parent>*>(toJS(functionRef))->classRef(); jsClass; jsClass = jsClass->parentClass) { |
b37bf2e1 | 486 | if (JSObjectCallAsFunctionCallback callAsFunction = jsClass->callAsFunction) { |
93a37866 A |
487 | size_t argumentCount = exec->argumentCount(); |
488 | Vector<JSValueRef, 16> arguments; | |
489 | arguments.reserveInitialCapacity(argumentCount); | |
490 | for (size_t i = 0; i < argumentCount; ++i) | |
81345200 | 491 | arguments.uncheckedAppend(toRef(exec, exec->uncheckedArgument(i))); |
ba379fdc A |
492 | JSValueRef exception = 0; |
493 | JSValue result; | |
494 | { | |
81345200 | 495 | JSLock::DropAllLocks dropAllLocks(exec); |
ba379fdc A |
496 | result = toJS(exec, callAsFunction(execRef, functionRef, thisObjRef, argumentCount, arguments.data(), &exception)); |
497 | } | |
f9bf01c6 | 498 | if (exception) |
81345200 | 499 | exec->vm().throwException(exec, toJS(exec, exception)); |
14957cd0 | 500 | return JSValue::encode(result); |
b37bf2e1 A |
501 | } |
502 | } | |
503 | ||
93a37866 | 504 | RELEASE_ASSERT_NOT_REACHED(); // getCallData should prevent us from reaching here |
14957cd0 | 505 | return JSValue::encode(JSValue()); |
b37bf2e1 A |
506 | } |
507 | ||
6fe7ccc8 | 508 | template <class Parent> |
93a37866 | 509 | void JSCallbackObject<Parent>::getOwnNonIndexPropertyNames(JSObject* object, ExecState* exec, PropertyNameArray& propertyNames, EnumerationMode mode) |
b37bf2e1 | 510 | { |
6fe7ccc8 | 511 | JSCallbackObject* thisObject = jsCast<JSCallbackObject*>(object); |
b37bf2e1 | 512 | JSContextRef execRef = toRef(exec); |
6fe7ccc8 | 513 | JSObjectRef thisRef = toRef(thisObject); |
b37bf2e1 | 514 | |
6fe7ccc8 | 515 | for (JSClassRef jsClass = thisObject->classRef(); jsClass; jsClass = jsClass->parentClass) { |
b37bf2e1 | 516 | if (JSObjectGetPropertyNamesCallback getPropertyNames = jsClass->getPropertyNames) { |
81345200 | 517 | JSLock::DropAllLocks dropAllLocks(exec); |
b37bf2e1 A |
518 | getPropertyNames(execRef, thisRef, toRef(&propertyNames)); |
519 | } | |
520 | ||
9dae56ea A |
521 | if (OpaqueJSClassStaticValuesTable* staticValues = jsClass->staticValues(exec)) { |
522 | typedef OpaqueJSClassStaticValuesTable::const_iterator iterator; | |
b37bf2e1 A |
523 | iterator end = staticValues->end(); |
524 | for (iterator it = staticValues->begin(); it != end; ++it) { | |
93a37866 A |
525 | StringImpl* name = it->key.get(); |
526 | StaticValueEntry* entry = it->value.get(); | |
ed1e77d3 A |
527 | if (entry->getProperty && (!(entry->attributes & kJSPropertyAttributeDontEnum) || mode.includeDontEnumProperties())) { |
528 | ASSERT(!name->isSymbol()); | |
529 | propertyNames.add(Identifier::fromString(exec, String(name))); | |
530 | } | |
b37bf2e1 A |
531 | } |
532 | } | |
533 | ||
9dae56ea A |
534 | if (OpaqueJSClassStaticFunctionsTable* staticFunctions = jsClass->staticFunctions(exec)) { |
535 | typedef OpaqueJSClassStaticFunctionsTable::const_iterator iterator; | |
b37bf2e1 A |
536 | iterator end = staticFunctions->end(); |
537 | for (iterator it = staticFunctions->begin(); it != end; ++it) { | |
93a37866 A |
538 | StringImpl* name = it->key.get(); |
539 | StaticFunctionEntry* entry = it->value.get(); | |
ed1e77d3 A |
540 | if (!(entry->attributes & kJSPropertyAttributeDontEnum) || mode.includeDontEnumProperties()) { |
541 | ASSERT(!name->isSymbol()); | |
542 | propertyNames.add(Identifier::fromString(exec, String(name))); | |
543 | } | |
b37bf2e1 A |
544 | } |
545 | } | |
546 | } | |
547 | ||
93a37866 | 548 | Parent::getOwnNonIndexPropertyNames(thisObject, exec, propertyNames, mode); |
b37bf2e1 A |
549 | } |
550 | ||
6fe7ccc8 A |
551 | template <class Parent> |
552 | void JSCallbackObject<Parent>::setPrivate(void* data) | |
b37bf2e1 | 553 | { |
9dae56ea | 554 | m_callbackObjectData->privateData = data; |
b37bf2e1 A |
555 | } |
556 | ||
6fe7ccc8 A |
557 | template <class Parent> |
558 | void* JSCallbackObject<Parent>::getPrivate() | |
b37bf2e1 | 559 | { |
9dae56ea | 560 | return m_callbackObjectData->privateData; |
b37bf2e1 A |
561 | } |
562 | ||
6fe7ccc8 A |
563 | template <class Parent> |
564 | bool JSCallbackObject<Parent>::inherits(JSClassRef c) const | |
b37bf2e1 | 565 | { |
93a37866 | 566 | for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass) { |
b37bf2e1 A |
567 | if (jsClass == c) |
568 | return true; | |
93a37866 | 569 | } |
b37bf2e1 A |
570 | return false; |
571 | } | |
572 | ||
6fe7ccc8 | 573 | template <class Parent> |
93a37866 | 574 | JSValue JSCallbackObject<Parent>::getStaticValue(ExecState* exec, PropertyName propertyName) |
b37bf2e1 | 575 | { |
14957cd0 | 576 | JSObjectRef thisRef = toRef(this); |
b37bf2e1 | 577 | |
81345200 | 578 | if (StringImpl* name = propertyName.uid()) { |
93a37866 A |
579 | for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass) { |
580 | if (OpaqueJSClassStaticValuesTable* staticValues = jsClass->staticValues(exec)) { | |
581 | if (StaticValueEntry* entry = staticValues->get(name)) { | |
582 | if (JSObjectGetPropertyCallback getProperty = entry->getProperty) { | |
93a37866 A |
583 | JSValueRef exception = 0; |
584 | JSValueRef value; | |
585 | { | |
81345200 A |
586 | JSLock::DropAllLocks dropAllLocks(exec); |
587 | value = getProperty(toRef(exec), thisRef, entry->propertyNameRef.get(), &exception); | |
93a37866 A |
588 | } |
589 | if (exception) { | |
81345200 | 590 | exec->vm().throwException(exec, toJS(exec, exception)); |
93a37866 A |
591 | return jsUndefined(); |
592 | } | |
593 | if (value) | |
594 | return toJS(exec, value); | |
ba379fdc | 595 | } |
b37bf2e1 | 596 | } |
93a37866 A |
597 | } |
598 | } | |
599 | } | |
f9bf01c6 | 600 | |
14957cd0 | 601 | return JSValue(); |
b37bf2e1 A |
602 | } |
603 | ||
6fe7ccc8 | 604 | template <class Parent> |
81345200 | 605 | EncodedJSValue JSCallbackObject<Parent>::staticFunctionGetter(ExecState* exec, JSObject* slotParent, EncodedJSValue, PropertyName propertyName) |
b37bf2e1 | 606 | { |
6fe7ccc8 | 607 | JSCallbackObject* thisObj = asCallbackObject(slotParent); |
b37bf2e1 A |
608 | |
609 | // Check for cached or override property. | |
9dae56ea | 610 | PropertySlot slot2(thisObj); |
6fe7ccc8 | 611 | if (Parent::getOwnPropertySlot(thisObj, exec, propertyName, slot2)) |
81345200 A |
612 | return JSValue::encode(slot2.getValue(exec, propertyName)); |
613 | ||
614 | if (StringImpl* name = propertyName.uid()) { | |
93a37866 A |
615 | for (JSClassRef jsClass = thisObj->classRef(); jsClass; jsClass = jsClass->parentClass) { |
616 | if (OpaqueJSClassStaticFunctionsTable* staticFunctions = jsClass->staticFunctions(exec)) { | |
617 | if (StaticFunctionEntry* entry = staticFunctions->get(name)) { | |
618 | if (JSObjectCallAsFunctionCallback callAsFunction = entry->callAsFunction) { | |
81345200 A |
619 | VM& vm = exec->vm(); |
620 | JSObject* o = JSCallbackFunction::create(vm, thisObj->globalObject(), callAsFunction, name); | |
621 | thisObj->putDirect(vm, propertyName, o, entry->attributes); | |
622 | return JSValue::encode(o); | |
93a37866 | 623 | } |
b37bf2e1 A |
624 | } |
625 | } | |
626 | } | |
627 | } | |
93a37866 | 628 | |
81345200 | 629 | return JSValue::encode(exec->vm().throwException(exec, createReferenceError(exec, ASCIILiteral("Static function property defined with NULL callAsFunction callback.")))); |
b37bf2e1 A |
630 | } |
631 | ||
6fe7ccc8 | 632 | template <class Parent> |
81345200 | 633 | EncodedJSValue JSCallbackObject<Parent>::callbackGetter(ExecState* exec, JSObject* slotParent, EncodedJSValue, PropertyName propertyName) |
b37bf2e1 | 634 | { |
6fe7ccc8 | 635 | JSCallbackObject* thisObj = asCallbackObject(slotParent); |
b37bf2e1 A |
636 | |
637 | JSObjectRef thisRef = toRef(thisObj); | |
9dae56ea | 638 | RefPtr<OpaqueJSString> propertyNameRef; |
b37bf2e1 | 639 | |
81345200 | 640 | if (StringImpl* name = propertyName.uid()) { |
93a37866 A |
641 | for (JSClassRef jsClass = thisObj->classRef(); jsClass; jsClass = jsClass->parentClass) { |
642 | if (JSObjectGetPropertyCallback getProperty = jsClass->getProperty) { | |
643 | if (!propertyNameRef) | |
644 | propertyNameRef = OpaqueJSString::create(name); | |
645 | JSValueRef exception = 0; | |
646 | JSValueRef value; | |
647 | { | |
81345200 | 648 | JSLock::DropAllLocks dropAllLocks(exec); |
93a37866 A |
649 | value = getProperty(toRef(exec), thisRef, propertyNameRef.get(), &exception); |
650 | } | |
651 | if (exception) { | |
81345200 A |
652 | exec->vm().throwException(exec, toJS(exec, exception)); |
653 | return JSValue::encode(jsUndefined()); | |
93a37866 A |
654 | } |
655 | if (value) | |
81345200 | 656 | return JSValue::encode(toJS(exec, value)); |
f9bf01c6 | 657 | } |
b37bf2e1 | 658 | } |
93a37866 A |
659 | } |
660 | ||
81345200 | 661 | return JSValue::encode(exec->vm().throwException(exec, createReferenceError(exec, ASCIILiteral("hasProperty callback returned true for a property that doesn't exist.")))); |
b37bf2e1 A |
662 | } |
663 | ||
9dae56ea | 664 | } // namespace JSC |