1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874
|
/*
* Copyright (C) 2006-2020 Apple Inc. All rights reserved.
* Copyright (C) 2008 Kelvin W Sherlock (ksherlock@gmail.com)
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "JSObjectRef.h"
#include "JSObjectRefPrivate.h"
#include "APICast.h"
#include "APIUtils.h"
#include "DateConstructor.h"
#include "FunctionConstructor.h"
#include "Identifier.h"
#include "InitializeThreading.h"
#include "JSAPIWrapperObject.h"
#include "JSArray.h"
#include "JSCInlines.h"
#include "JSCallbackConstructor.h"
#include "JSCallbackFunction.h"
#include "JSCallbackObject.h"
#include "JSClassRef.h"
#include "JSPromise.h"
#include "JSString.h"
#include "ObjectConstructor.h"
#include "ObjectPrototype.h"
#include "PropertyNameArray.h"
#include "ProxyObject.h"
#include "RegExpConstructor.h"
#if ENABLE(REMOTE_INSPECTOR)
#include "JSGlobalObjectInspectorController.h"
#endif
using namespace JSC;
JSClassRef JSClassCreate(const JSClassDefinition* definition)
{
JSC::initialize();
auto jsClass = (definition->attributes & kJSClassAttributeNoAutomaticPrototype)
? OpaqueJSClass::createNoAutomaticPrototype(definition)
: OpaqueJSClass::create(definition);
return &jsClass.leakRef();
}
JSClassRef JSClassRetain(JSClassRef jsClass)
{
jsClass->ref();
return jsClass;
}
void JSClassRelease(JSClassRef jsClass)
{
jsClass->deref();
}
JSObjectRef JSObjectMake(JSContextRef ctx, JSClassRef jsClass, void* data)
{
if (!ctx) {
ASSERT_NOT_REACHED();
return nullptr;
}
JSGlobalObject* globalObject = toJS(ctx);
VM& vm = globalObject->vm();
JSLockHolder locker(vm);
if (!jsClass)
return toRef(constructEmptyObject(globalObject));
JSCallbackObject<JSNonFinalObject>* object = JSCallbackObject<JSNonFinalObject>::create(globalObject, globalObject->callbackObjectStructure(), jsClass, data);
if (JSObject* prototype = jsClass->prototype(globalObject))
object->setPrototypeDirect(vm, prototype);
return toRef(object);
}
JSObjectRef JSObjectMakeFunctionWithCallback(JSContextRef ctx, JSStringRef name, JSObjectCallAsFunctionCallback callAsFunction)
{
if (!ctx) {
ASSERT_NOT_REACHED();
return nullptr;
}
JSGlobalObject* globalObject = toJS(ctx);
VM& vm = globalObject->vm();
JSLockHolder locker(vm);
return toRef(JSCallbackFunction::create(vm, globalObject, callAsFunction, name ? name->string() : "anonymous"_s));
}
JSObjectRef JSObjectMakeConstructor(JSContextRef ctx, JSClassRef jsClass, JSObjectCallAsConstructorCallback callAsConstructor)
{
if (!ctx) {
ASSERT_NOT_REACHED();
return nullptr;
}
JSGlobalObject* globalObject = toJS(ctx);
VM& vm = globalObject->vm();
JSLockHolder locker(vm);
JSValue jsPrototype = jsClass ? jsClass->prototype(globalObject) : nullptr;
if (!jsPrototype)
jsPrototype = globalObject->objectPrototype();
JSCallbackConstructor* constructor = JSCallbackConstructor::create(globalObject, globalObject->callbackConstructorStructure(), jsClass, callAsConstructor);
constructor->putDirect(vm, vm.propertyNames->prototype, jsPrototype, PropertyAttribute::DontEnum | PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly);
return toRef(constructor);
}
JSObjectRef JSObjectMakeFunction(JSContextRef ctx, JSStringRef name, unsigned parameterCount, const JSStringRef parameterNames[], JSStringRef body, JSStringRef sourceURLString, int startingLineNumber, JSValueRef* exception)
{
if (!ctx) {
ASSERT_NOT_REACHED();
return nullptr;
}
JSGlobalObject* globalObject = toJS(ctx);
VM& vm = globalObject->vm();
JSLockHolder locker(vm);
auto scope = DECLARE_CATCH_SCOPE(vm);
startingLineNumber = std::max(1, startingLineNumber);
Identifier nameID = name ? name->identifier(&vm) : Identifier::fromString(vm, "anonymous"_s);
MarkedArgumentBuffer args;
for (unsigned i = 0; i < parameterCount; i++)
args.append(jsString(vm, parameterNames[i]->string()));
args.append(jsString(vm, body->string()));
if (UNLIKELY(args.hasOverflowed())) {
auto throwScope = DECLARE_THROW_SCOPE(vm);
throwOutOfMemoryError(globalObject, throwScope);
handleExceptionIfNeeded(scope, ctx, exception);
return nullptr;
}
auto sourceURL = sourceURLString ? URL({ }, sourceURLString->string()) : URL();
JSObject* result = constructFunction(globalObject, args, nameID, SourceOrigin { sourceURL }, sourceURL.string(), TextPosition(OrdinalNumber::fromOneBasedInt(startingLineNumber), OrdinalNumber()));
if (handleExceptionIfNeeded(scope, ctx, exception) == ExceptionStatus::DidThrow)
result = nullptr;
return toRef(result);
}
JSObjectRef JSObjectMakeArray(JSContextRef ctx, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
{
if (!ctx) {
ASSERT_NOT_REACHED();
return nullptr;
}
JSGlobalObject* globalObject = toJS(ctx);
VM& vm = globalObject->vm();
JSLockHolder locker(vm);
auto scope = DECLARE_CATCH_SCOPE(vm);
JSObject* result;
if (argumentCount) {
MarkedArgumentBuffer argList;
for (size_t i = 0; i < argumentCount; ++i)
argList.append(toJS(globalObject, arguments[i]));
if (UNLIKELY(argList.hasOverflowed())) {
auto throwScope = DECLARE_THROW_SCOPE(vm);
throwOutOfMemoryError(globalObject, throwScope);
handleExceptionIfNeeded(scope, ctx, exception);
return nullptr;
}
result = constructArray(globalObject, static_cast<ArrayAllocationProfile*>(nullptr), argList);
} else
result = constructEmptyArray(globalObject, nullptr);
if (handleExceptionIfNeeded(scope, ctx, exception) == ExceptionStatus::DidThrow)
result = nullptr;
return toRef(result);
}
JSObjectRef JSObjectMakeDate(JSContextRef ctx, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
{
if (!ctx) {
ASSERT_NOT_REACHED();
return nullptr;
}
JSGlobalObject* globalObject = toJS(ctx);
VM& vm = globalObject->vm();
JSLockHolder locker(vm);
auto scope = DECLARE_CATCH_SCOPE(vm);
MarkedArgumentBuffer argList;
for (size_t i = 0; i < argumentCount; ++i)
argList.append(toJS(globalObject, arguments[i]));
if (UNLIKELY(argList.hasOverflowed())) {
auto throwScope = DECLARE_THROW_SCOPE(vm);
throwOutOfMemoryError(globalObject, throwScope);
handleExceptionIfNeeded(scope, ctx, exception);
return nullptr;
}
JSObject* result = constructDate(globalObject, JSValue(), argList);
if (handleExceptionIfNeeded(scope, ctx, exception) == ExceptionStatus::DidThrow)
result = nullptr;
return toRef(result);
}
JSObjectRef JSObjectMakeError(JSContextRef ctx, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
{
if (!ctx) {
ASSERT_NOT_REACHED();
return nullptr;
}
JSGlobalObject* globalObject = toJS(ctx);
VM& vm = globalObject->vm();
JSLockHolder locker(vm);
auto scope = DECLARE_CATCH_SCOPE(vm);
JSValue message = argumentCount ? toJS(globalObject, arguments[0]) : jsUndefined();
JSValue options = argumentCount > 1 ? toJS(globalObject, arguments[1]) : jsUndefined();
Structure* errorStructure = globalObject->errorStructure();
JSObject* result = ErrorInstance::create(globalObject, errorStructure, message, options);
if (handleExceptionIfNeeded(scope, ctx, exception) == ExceptionStatus::DidThrow)
result = nullptr;
return toRef(result);
}
JSObjectRef JSObjectMakeRegExp(JSContextRef ctx, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
{
if (!ctx) {
ASSERT_NOT_REACHED();
return nullptr;
}
JSGlobalObject* globalObject = toJS(ctx);
VM& vm = globalObject->vm();
JSLockHolder locker(vm);
auto scope = DECLARE_CATCH_SCOPE(vm);
MarkedArgumentBuffer argList;
for (size_t i = 0; i < argumentCount; ++i)
argList.append(toJS(globalObject, arguments[i]));
if (UNLIKELY(argList.hasOverflowed())) {
auto throwScope = DECLARE_THROW_SCOPE(vm);
throwOutOfMemoryError(globalObject, throwScope);
handleExceptionIfNeeded(scope, ctx, exception);
return nullptr;
}
JSObject* result = constructRegExp(globalObject, argList);
if (handleExceptionIfNeeded(scope, ctx, exception) == ExceptionStatus::DidThrow)
result = nullptr;
return toRef(result);
}
JSObjectRef JSObjectMakeDeferredPromise(JSContextRef ctx, JSObjectRef* resolve, JSObjectRef* reject, JSValueRef* exception)
{
if (!ctx) {
ASSERT_NOT_REACHED();
return nullptr;
}
JSGlobalObject* globalObject = toJS(ctx);
VM& vm = globalObject->vm();
JSLockHolder locker(globalObject);
auto scope = DECLARE_CATCH_SCOPE(vm);
JSPromise::DeferredData data = JSPromise::createDeferredData(globalObject, globalObject->promiseConstructor());
if (handleExceptionIfNeeded(scope, ctx, exception) == ExceptionStatus::DidThrow)
return nullptr;
if (resolve)
*resolve = toRef(data.resolve);
if (reject)
*reject = toRef(data.reject);
return toRef(data.promise);
}
JSValueRef JSObjectGetPrototype(JSContextRef ctx, JSObjectRef object)
{
if (!ctx) {
ASSERT_NOT_REACHED();
return nullptr;
}
JSGlobalObject* globalObject = toJS(ctx);
JSLockHolder locker(globalObject);
JSObject* jsObject = toJS(object);
return toRef(globalObject, jsObject->getPrototypeDirect());
}
void JSObjectSetPrototype(JSContextRef ctx, JSObjectRef object, JSValueRef value)
{
if (!ctx) {
ASSERT_NOT_REACHED();
return;
}
JSGlobalObject* globalObject = toJS(ctx);
VM& vm = globalObject->vm();
JSLockHolder locker(vm);
auto scope = DECLARE_CATCH_SCOPE(vm);
JSObject* jsObject = toJS(object);
JSValue jsValue = toJS(globalObject, value);
jsObject->setPrototype(vm, globalObject, jsValue.isObject() ? jsValue : jsNull());
handleExceptionIfNeeded(scope, ctx, nullptr);
}
bool JSObjectHasProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName)
{
if (!ctx) {
ASSERT_NOT_REACHED();
return false;
}
JSGlobalObject* globalObject = toJS(ctx);
VM& vm = globalObject->vm();
JSLockHolder locker(vm);
JSObject* jsObject = toJS(object);
return jsObject->hasProperty(globalObject, propertyName->identifier(&vm));
}
JSValueRef JSObjectGetProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
{
if (!ctx || !object) {
ASSERT_NOT_REACHED();
return nullptr;
}
JSGlobalObject* globalObject = toJS(ctx);
VM& vm = globalObject->vm();
JSLockHolder locker(vm);
auto scope = DECLARE_CATCH_SCOPE(vm);
JSObject* jsObject = toJS(object);
JSValue jsValue = jsObject->get(globalObject, propertyName->identifier(&vm));
if (handleExceptionIfNeeded(scope, ctx, exception) == ExceptionStatus::DidThrow)
return nullptr;
return toRef(globalObject, jsValue);
}
void JSObjectSetProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef value, JSPropertyAttributes attributes, JSValueRef* exception)
{
if (!ctx) {
ASSERT_NOT_REACHED();
return;
}
JSGlobalObject* globalObject = toJS(ctx);
VM& vm = globalObject->vm();
JSLockHolder locker(vm);
auto scope = DECLARE_CATCH_SCOPE(vm);
JSObject* jsObject = toJS(object);
Identifier name(propertyName->identifier(&vm));
JSValue jsValue = toJS(globalObject, value);
bool doesNotHaveProperty = attributes && !jsObject->hasProperty(globalObject, name);
if (LIKELY(!scope.exception())) {
if (doesNotHaveProperty) {
PropertyDescriptor desc(jsValue, attributes);
jsObject->methodTable()->defineOwnProperty(jsObject, globalObject, name, desc, false);
} else {
PutPropertySlot slot(jsObject);
jsObject->methodTable()->put(jsObject, globalObject, name, jsValue, slot);
}
}
handleExceptionIfNeeded(scope, ctx, exception);
}
bool JSObjectHasPropertyForKey(JSContextRef ctx, JSObjectRef object, JSValueRef key, JSValueRef* exception)
{
if (!ctx) {
ASSERT_NOT_REACHED();
return false;
}
JSGlobalObject* globalObject = toJS(ctx);
VM& vm = globalObject->vm();
JSLockHolder locker(vm);
auto scope = DECLARE_CATCH_SCOPE(vm);
JSObject* jsObject = toJS(object);
Identifier ident = toJS(globalObject, key).toPropertyKey(globalObject);
if (handleExceptionIfNeeded(scope, ctx, exception) == ExceptionStatus::DidThrow)
return false;
bool result = jsObject->hasProperty(globalObject, ident);
if (handleExceptionIfNeeded(scope, ctx, exception) == ExceptionStatus::DidThrow)
return false;
return result;
}
JSValueRef JSObjectGetPropertyForKey(JSContextRef ctx, JSObjectRef object, JSValueRef key, JSValueRef* exception)
{
if (!ctx) {
ASSERT_NOT_REACHED();
return nullptr;
}
JSGlobalObject* globalObject = toJS(ctx);
VM& vm = globalObject->vm();
JSLockHolder locker(vm);
auto scope = DECLARE_CATCH_SCOPE(vm);
JSObject* jsObject = toJS(object);
Identifier ident = toJS(globalObject, key).toPropertyKey(globalObject);
if (handleExceptionIfNeeded(scope, ctx, exception) == ExceptionStatus::DidThrow)
return nullptr;
JSValue jsValue = jsObject->get(globalObject, ident);
if (handleExceptionIfNeeded(scope, ctx, exception) == ExceptionStatus::DidThrow)
return nullptr;
return toRef(globalObject, jsValue);
}
void JSObjectSetPropertyForKey(JSContextRef ctx, JSObjectRef object, JSValueRef key, JSValueRef value, JSPropertyAttributes attributes, JSValueRef* exception)
{
if (!ctx) {
ASSERT_NOT_REACHED();
return;
}
JSGlobalObject* globalObject = toJS(ctx);
VM& vm = globalObject->vm();
JSLockHolder locker(vm);
auto scope = DECLARE_CATCH_SCOPE(vm);
JSObject* jsObject = toJS(object);
JSValue jsValue = toJS(globalObject, value);
Identifier ident = toJS(globalObject, key).toPropertyKey(globalObject);
if (handleExceptionIfNeeded(scope, ctx, exception) == ExceptionStatus::DidThrow)
return;
bool doesNotHaveProperty = attributes && !jsObject->hasProperty(globalObject, ident);
if (LIKELY(!scope.exception())) {
if (doesNotHaveProperty) {
PropertyDescriptor desc(jsValue, attributes);
jsObject->methodTable()->defineOwnProperty(jsObject, globalObject, ident, desc, false);
} else {
PutPropertySlot slot(jsObject);
jsObject->methodTable()->put(jsObject, globalObject, ident, jsValue, slot);
}
}
handleExceptionIfNeeded(scope, ctx, exception);
}
bool JSObjectDeletePropertyForKey(JSContextRef ctx, JSObjectRef object, JSValueRef key, JSValueRef* exception)
{
if (!ctx) {
ASSERT_NOT_REACHED();
return false;
}
JSGlobalObject* globalObject = toJS(ctx);
VM& vm = globalObject->vm();
JSLockHolder locker(vm);
auto scope = DECLARE_CATCH_SCOPE(vm);
JSObject* jsObject = toJS(object);
Identifier ident = toJS(globalObject, key).toPropertyKey(globalObject);
if (handleExceptionIfNeeded(scope, ctx, exception) == ExceptionStatus::DidThrow)
return false;
bool result = JSCell::deleteProperty(jsObject, globalObject, ident);
if (handleExceptionIfNeeded(scope, ctx, exception) == ExceptionStatus::DidThrow)
return false;
return result;
}
JSValueRef JSObjectGetPropertyAtIndex(JSContextRef ctx, JSObjectRef object, unsigned propertyIndex, JSValueRef* exception)
{
if (!ctx) {
ASSERT_NOT_REACHED();
return nullptr;
}
JSGlobalObject* globalObject = toJS(ctx);
VM& vm = globalObject->vm();
JSLockHolder locker(vm);
auto scope = DECLARE_CATCH_SCOPE(vm);
JSObject* jsObject = toJS(object);
JSValue jsValue = jsObject->get(globalObject, propertyIndex);
if (handleExceptionIfNeeded(scope, ctx, exception) == ExceptionStatus::DidThrow)
return nullptr;
return toRef(globalObject, jsValue);
}
void JSObjectSetPropertyAtIndex(JSContextRef ctx, JSObjectRef object, unsigned propertyIndex, JSValueRef value, JSValueRef* exception)
{
if (!ctx) {
ASSERT_NOT_REACHED();
return;
}
JSGlobalObject* globalObject = toJS(ctx);
VM& vm = globalObject->vm();
JSLockHolder locker(vm);
auto scope = DECLARE_CATCH_SCOPE(vm);
JSObject* jsObject = toJS(object);
JSValue jsValue = toJS(globalObject, value);
jsObject->methodTable()->putByIndex(jsObject, globalObject, propertyIndex, jsValue, false);
handleExceptionIfNeeded(scope, ctx, exception);
}
bool JSObjectDeleteProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
{
if (!ctx) {
ASSERT_NOT_REACHED();
return false;
}
JSGlobalObject* globalObject = toJS(ctx);
VM& vm = globalObject->vm();
JSLockHolder locker(vm);
auto scope = DECLARE_CATCH_SCOPE(vm);
JSObject* jsObject = toJS(object);
bool result = JSCell::deleteProperty(jsObject, globalObject, propertyName->identifier(&vm));
if (handleExceptionIfNeeded(scope, ctx, exception) == ExceptionStatus::DidThrow)
return false;
return result;
}
// API objects have private properties, which may get accessed during destruction. This
// helper lets us get the ClassInfo of an API object from a function that may get called
// during destruction.
static const ClassInfo* classInfoPrivate(JSObject* jsObject)
{
VM& vm = jsObject->vm();
if (vm.currentlyDestructingCallbackObject != jsObject)
return jsObject->classInfo();
return vm.currentlyDestructingCallbackObjectClassInfo;
}
void* JSObjectGetPrivate(JSObjectRef object)
{
JSObject* jsObject = uncheckedToJS(object);
const ClassInfo* classInfo = classInfoPrivate(jsObject);
// Get wrapped object if proxied
if (classInfo->isSubClassOf(JSProxy::info())) {
jsObject = static_cast<JSProxy*>(jsObject)->target();
classInfo = jsObject->classInfo();
}
if (classInfo->isSubClassOf(JSCallbackObject<JSGlobalObject>::info()))
return static_cast<JSCallbackObject<JSGlobalObject>*>(jsObject)->getPrivate();
if (classInfo->isSubClassOf(JSCallbackObject<JSNonFinalObject>::info()))
return static_cast<JSCallbackObject<JSNonFinalObject>*>(jsObject)->getPrivate();
#if JSC_OBJC_API_ENABLED
if (classInfo->isSubClassOf(JSCallbackObject<JSAPIWrapperObject>::info()))
return static_cast<JSCallbackObject<JSAPIWrapperObject>*>(jsObject)->getPrivate();
#endif
return nullptr;
}
bool JSObjectSetPrivate(JSObjectRef object, void* data)
{
JSObject* jsObject = uncheckedToJS(object);
const ClassInfo* classInfo = classInfoPrivate(jsObject);
// Get wrapped object if proxied
if (classInfo->isSubClassOf(JSProxy::info())) {
jsObject = static_cast<JSProxy*>(jsObject)->target();
classInfo = jsObject->classInfo();
}
if (classInfo->isSubClassOf(JSCallbackObject<JSGlobalObject>::info())) {
static_cast<JSCallbackObject<JSGlobalObject>*>(jsObject)->setPrivate(data);
return true;
}
if (classInfo->isSubClassOf(JSCallbackObject<JSNonFinalObject>::info())) {
static_cast<JSCallbackObject<JSNonFinalObject>*>(jsObject)->setPrivate(data);
return true;
}
#if JSC_OBJC_API_ENABLED
if (classInfo->isSubClassOf(JSCallbackObject<JSAPIWrapperObject>::info())) {
static_cast<JSCallbackObject<JSAPIWrapperObject>*>(jsObject)->setPrivate(data);
return true;
}
#endif
return false;
}
JSValueRef JSObjectGetPrivateProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName)
{
JSGlobalObject* globalObject = toJS(ctx);
VM& vm = globalObject->vm();
JSLockHolder locker(vm);
JSObject* jsObject = toJS(object);
JSValue result;
Identifier name(propertyName->identifier(&vm));
// Get wrapped object if proxied
if (jsObject->inherits<JSProxy>())
jsObject = jsCast<JSProxy*>(jsObject)->target();
if (jsObject->inherits<JSCallbackObject<JSGlobalObject>>())
result = jsCast<JSCallbackObject<JSGlobalObject>*>(jsObject)->getPrivateProperty(name);
else if (jsObject->inherits<JSCallbackObject<JSNonFinalObject>>())
result = jsCast<JSCallbackObject<JSNonFinalObject>*>(jsObject)->getPrivateProperty(name);
#if JSC_OBJC_API_ENABLED
else if (jsObject->inherits<JSCallbackObject<JSAPIWrapperObject>>())
result = jsCast<JSCallbackObject<JSAPIWrapperObject>*>(jsObject)->getPrivateProperty(name);
#endif
return toRef(globalObject, result);
}
bool JSObjectSetPrivateProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef value)
{
JSGlobalObject* globalObject = toJS(ctx);
VM& vm = globalObject->vm();
JSLockHolder locker(vm);
JSObject* jsObject = toJS(object);
JSValue jsValue = value ? toJS(globalObject, value) : JSValue();
Identifier name(propertyName->identifier(&vm));
// Get wrapped object if proxied
if (jsObject->inherits<JSProxy>())
jsObject = jsCast<JSProxy*>(jsObject)->target();
if (jsObject->inherits<JSCallbackObject<JSGlobalObject>>()) {
jsCast<JSCallbackObject<JSGlobalObject>*>(jsObject)->setPrivateProperty(vm, name, jsValue);
return true;
}
if (jsObject->inherits<JSCallbackObject<JSNonFinalObject>>()) {
jsCast<JSCallbackObject<JSNonFinalObject>*>(jsObject)->setPrivateProperty(vm, name, jsValue);
return true;
}
#if JSC_OBJC_API_ENABLED
if (jsObject->inherits<JSCallbackObject<JSAPIWrapperObject>>()) {
jsCast<JSCallbackObject<JSAPIWrapperObject>*>(jsObject)->setPrivateProperty(vm, name, jsValue);
return true;
}
#endif
return false;
}
bool JSObjectDeletePrivateProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName)
{
JSGlobalObject* globalObject = toJS(ctx);
VM& vm = globalObject->vm();
JSLockHolder locker(vm);
JSObject* jsObject = toJS(object);
Identifier name(propertyName->identifier(&vm));
// Get wrapped object if proxied
if (jsObject->inherits<JSProxy>())
jsObject = jsCast<JSProxy*>(jsObject)->target();
if (jsObject->inherits<JSCallbackObject<JSGlobalObject>>()) {
jsCast<JSCallbackObject<JSGlobalObject>*>(jsObject)->deletePrivateProperty(name);
return true;
}
if (jsObject->inherits<JSCallbackObject<JSNonFinalObject>>()) {
jsCast<JSCallbackObject<JSNonFinalObject>*>(jsObject)->deletePrivateProperty(name);
return true;
}
#if JSC_OBJC_API_ENABLED
if (jsObject->inherits<JSCallbackObject<JSAPIWrapperObject>>()) {
jsCast<JSCallbackObject<JSAPIWrapperObject>*>(jsObject)->deletePrivateProperty(name);
return true;
}
#endif
return false;
}
bool JSObjectIsFunction(JSContextRef ctx, JSObjectRef object)
{
if (!object)
return false;
JSGlobalObject* globalObject = toJS(ctx);
VM& vm = globalObject->vm();
JSLockHolder locker(vm);
JSCell* cell = toJS(object);
return cell->isCallable();
}
JSValueRef JSObjectCallAsFunction(JSContextRef ctx, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
{
JSGlobalObject* globalObject = toJS(ctx);
VM& vm = globalObject->vm();
JSLockHolder locker(vm);
auto scope = DECLARE_CATCH_SCOPE(vm);
if (!object)
return nullptr;
JSObject* jsObject = toJS(object);
JSObject* jsThisObject = toJS(thisObject);
if (!jsThisObject)
jsThisObject = globalObject->globalThis();
MarkedArgumentBuffer argList;
for (size_t i = 0; i < argumentCount; i++)
argList.append(toJS(globalObject, arguments[i]));
if (UNLIKELY(argList.hasOverflowed())) {
auto throwScope = DECLARE_THROW_SCOPE(vm);
throwOutOfMemoryError(globalObject, throwScope);
handleExceptionIfNeeded(scope, ctx, exception);
return nullptr;
}
auto callData = JSC::getCallData(jsObject);
if (callData.type == CallData::Type::None)
return nullptr;
JSValueRef result = toRef(globalObject, profiledCall(globalObject, ProfilingReason::API, jsObject, callData, jsThisObject, argList));
if (handleExceptionIfNeeded(scope, ctx, exception) == ExceptionStatus::DidThrow)
result = nullptr;
return result;
}
bool JSObjectIsConstructor(JSContextRef ctx, JSObjectRef object)
{
JSGlobalObject* globalObject = toJS(ctx);
VM& vm = globalObject->vm();
JSLockHolder locker(vm);
if (!object)
return false;
return toJS(object)->isConstructor();
}
JSObjectRef JSObjectCallAsConstructor(JSContextRef ctx, JSObjectRef object, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
{
JSGlobalObject* globalObject = toJS(ctx);
VM& vm = globalObject->vm();
JSLockHolder locker(vm);
auto scope = DECLARE_CATCH_SCOPE(vm);
if (!object)
return nullptr;
JSObject* jsObject = toJS(object);
auto constructData = JSC::getConstructData(jsObject);
if (constructData.type == CallData::Type::None)
return nullptr;
MarkedArgumentBuffer argList;
for (size_t i = 0; i < argumentCount; i++)
argList.append(toJS(globalObject, arguments[i]));
if (UNLIKELY(argList.hasOverflowed())) {
auto throwScope = DECLARE_THROW_SCOPE(vm);
throwOutOfMemoryError(globalObject, throwScope);
handleExceptionIfNeeded(scope, ctx, exception);
return nullptr;
}
JSObjectRef result = toRef(profiledConstruct(globalObject, ProfilingReason::API, jsObject, constructData, argList));
if (handleExceptionIfNeeded(scope, ctx, exception) == ExceptionStatus::DidThrow)
result = nullptr;
return result;
}
struct OpaqueJSPropertyNameArray {
WTF_MAKE_FAST_ALLOCATED;
public:
// FIXME: Why not inherit from RefCounted?
OpaqueJSPropertyNameArray(VM* vm)
: refCount(0)
, vm(vm)
{
}
unsigned refCount;
VM* vm;
Vector<Ref<OpaqueJSString>> array;
};
JSPropertyNameArrayRef JSObjectCopyPropertyNames(JSContextRef ctx, JSObjectRef object)
{
if (!ctx) {
ASSERT_NOT_REACHED();
return nullptr;
}
JSGlobalObject* globalObject = toJS(ctx);
JSLockHolder locker(globalObject);
VM& vm = globalObject->vm();
JSObject* jsObject = toJS(object);
JSPropertyNameArrayRef propertyNames = new OpaqueJSPropertyNameArray(&vm);
PropertyNameArray array(vm, PropertyNameMode::Strings, PrivateSymbolMode::Exclude);
jsObject->getPropertyNames(globalObject, array, DontEnumPropertiesMode::Exclude);
size_t size = array.size();
propertyNames->array.reserveInitialCapacity(size);
for (size_t i = 0; i < size; ++i)
propertyNames->array.uncheckedAppend(OpaqueJSString::tryCreate(array[i].string()).releaseNonNull());
return JSPropertyNameArrayRetain(propertyNames);
}
JSPropertyNameArrayRef JSPropertyNameArrayRetain(JSPropertyNameArrayRef array)
{
++array->refCount;
return array;
}
void JSPropertyNameArrayRelease(JSPropertyNameArrayRef array)
{
if (--array->refCount == 0) {
JSLockHolder locker(array->vm);
delete array;
}
}
size_t JSPropertyNameArrayGetCount(JSPropertyNameArrayRef array)
{
return array->array.size();
}
JSStringRef JSPropertyNameArrayGetNameAtIndex(JSPropertyNameArrayRef array, size_t index)
{
return array->array[static_cast<unsigned>(index)].ptr();
}
void JSPropertyNameAccumulatorAddName(JSPropertyNameAccumulatorRef array, JSStringRef propertyName)
{
PropertyNameArray* propertyNames = toJS(array);
VM& vm = propertyNames->vm();
JSLockHolder locker(vm);
propertyNames->add(propertyName->identifier(&vm));
}
JSObjectRef JSObjectGetProxyTarget(JSObjectRef objectRef)
{
JSObject* object = toJS(objectRef);
if (!object)
return nullptr;
VM& vm = object->vm();
JSLockHolder locker(vm);
JSObject* result = nullptr;
if (JSProxy* proxy = jsDynamicCast<JSProxy*>(object))
result = proxy->target();
else if (ProxyObject* proxy = jsDynamicCast<ProxyObject*>(object))
result = proxy->target();
return toRef(result);
}
JSGlobalContextRef JSObjectGetGlobalContext(JSObjectRef objectRef)
{
JSObject* object = toJS(objectRef);
if (!object)
return nullptr;
return reinterpret_cast<JSGlobalContextRef>(object->globalObject());
}
|