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
|
/*
* Copyright (C) 2017-2019 Apple Inc. All rights reserved.
*
* 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. AND ITS CONTRIBUTORS ``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 ITS 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 "APICast.h"
#include "JSGlobalObjectInlines.h"
#include "MarkedJSValueRefArray.h"
#include <JavaScriptCore/JSContextRefPrivate.h>
#include <JavaScriptCore/JSObjectRefPrivate.h>
#include <JavaScriptCore/JavaScript.h>
#include <wtf/DataLog.h>
#include <wtf/Expected.h>
#include <wtf/Noncopyable.h>
#include <wtf/NumberOfCores.h>
#include <wtf/Vector.h>
#include <wtf/text/StringCommon.h>
#if PLATFORM(COCOA)
#include <wtf/cocoa/RuntimeApplicationChecksCocoa.h>
#endif
extern "C" void configureJSCForTesting();
extern "C" int testCAPIViaCpp(const char* filter);
extern "C" void JSSynchronousGarbageCollectForDebugging(JSContextRef);
class APIString {
WTF_MAKE_NONCOPYABLE(APIString);
public:
APIString(const char* string)
: m_string(JSStringCreateWithUTF8CString(string))
{
}
APIString(const String& string)
: APIString(string.utf8().data())
{
}
~APIString()
{
JSStringRelease(m_string);
}
operator JSStringRef() { return m_string; }
private:
JSStringRef m_string;
};
class APIContext {
WTF_MAKE_NONCOPYABLE(APIContext);
public:
APIContext()
: m_context(JSGlobalContextCreate(nullptr))
{
APIString print("print");
JSObjectRef printFunction = JSObjectMakeFunctionWithCallback(m_context, print, [] (JSContextRef ctx, JSObjectRef, JSObjectRef, size_t argumentCount, const JSValueRef arguments[], JSValueRef*) {
JSC::JSGlobalObject* globalObject = toJS(ctx);
for (unsigned i = 0; i < argumentCount; i++)
dataLog(toJS(globalObject, arguments[i]));
dataLogLn();
return JSValueMakeUndefined(ctx);
});
JSObjectSetProperty(m_context, JSContextGetGlobalObject(m_context), print, printFunction, kJSPropertyAttributeNone, nullptr);
}
~APIContext()
{
JSGlobalContextRelease(m_context);
}
operator JSGlobalContextRef() { return m_context; }
operator JSC::JSGlobalObject*() { return toJS(m_context); }
private:
JSGlobalContextRef m_context;
};
template<typename T>
class APIVector : protected Vector<T> {
using Base = Vector<T>;
public:
APIVector(APIContext& context)
: Base()
, m_context(context)
{
}
~APIVector()
{
for (auto& value : *this)
JSValueUnprotect(m_context, value);
}
using Vector<T>::operator[];
using Vector<T>::size;
using Vector<T>::begin;
using Vector<T>::end;
using typename Vector<T>::iterator;
void append(T value)
{
JSValueProtect(m_context, value);
Base::append(WTFMove(value));
}
private:
APIContext& m_context;
};
class TestAPI {
public:
int run(const char* filter);
void basicSymbol();
void symbolsTypeof();
void symbolsDescription();
void symbolsGetPropertyForKey();
void symbolsSetPropertyForKey();
void symbolsHasPropertyForKey();
void symbolsDeletePropertyForKey();
void promiseResolveTrue();
void promiseRejectTrue();
void promiseUnhandledRejection();
void promiseUnhandledRejectionFromUnhandledRejectionCallback();
void promiseEarlyHandledRejections();
void promiseDrainDoesNotEatExceptions();
void topCallFrameAccess();
void markedJSValueArrayAndGC();
void classDefinitionWithJSSubclass();
void proxyReturnedWithJSSubclassing();
void testJSObjectSetOnGlobalObjectSubclassDefinition();
int failed() const { return m_failed; }
private:
template<typename... Strings>
bool check(bool condition, Strings... message);
template<typename JSFunctor, typename APIFunctor>
void checkJSAndAPIMatch(const JSFunctor&, const APIFunctor&, const char* description);
// Helper methods.
using ScriptResult = Expected<JSValueRef, JSValueRef>;
ScriptResult evaluateScript(const char* script, JSObjectRef thisObject = nullptr);
template<typename... ArgumentTypes>
ScriptResult callFunction(const char* functionSource, ArgumentTypes... arguments);
template<typename... ArgumentTypes>
bool functionReturnsTrue(const char* functionSource, ArgumentTypes... arguments);
bool scriptResultIs(ScriptResult, JSValueRef);
// Ways to make sets of interesting things.
APIVector<JSObjectRef> interestingObjects();
APIVector<JSValueRef> interestingKeys();
int m_failed { 0 };
APIContext context;
};
TestAPI::ScriptResult TestAPI::evaluateScript(const char* script, JSObjectRef thisObject)
{
APIString scriptAPIString(script);
JSValueRef exception = nullptr;
JSValueRef result = JSEvaluateScript(context, scriptAPIString, thisObject, nullptr, 0, &exception);
if (exception)
return Unexpected<JSValueRef>(exception);
return ScriptResult(result);
}
template<typename... ArgumentTypes>
TestAPI::ScriptResult TestAPI::callFunction(const char* functionSource, ArgumentTypes... arguments)
{
JSValueRef function;
{
ScriptResult functionResult = evaluateScript(functionSource);
if (!functionResult)
return functionResult;
function = functionResult.value();
}
JSValueRef exception = nullptr;
if (JSObjectRef functionObject = JSValueToObject(context, function, &exception)) {
JSValueRef args[sizeof...(arguments)] { arguments... };
JSValueRef result = JSObjectCallAsFunction(context, functionObject, functionObject, sizeof...(arguments), args, &exception);
if (!exception)
return ScriptResult(result);
}
RELEASE_ASSERT(exception);
return Unexpected<JSValueRef>(exception);
}
#if COMPILER(MSVC)
template<>
TestAPI::ScriptResult TestAPI::callFunction(const char* functionSource)
{
JSValueRef function;
{
ScriptResult functionResult = evaluateScript(functionSource);
if (!functionResult)
return functionResult;
function = functionResult.value();
}
JSValueRef exception = nullptr;
if (JSObjectRef functionObject = JSValueToObject(context, function, &exception)) {
JSValueRef result = JSObjectCallAsFunction(context, functionObject, functionObject, 0, nullptr, &exception);
if (!exception)
return ScriptResult(result);
}
RELEASE_ASSERT(exception);
return Unexpected<JSValueRef>(exception);
}
#endif
template<typename... ArgumentTypes>
bool TestAPI::functionReturnsTrue(const char* functionSource, ArgumentTypes... arguments)
{
JSValueRef trueValue = JSValueMakeBoolean(context, true);
ScriptResult result = callFunction(functionSource, arguments...);
if (!result)
return false;
return JSValueIsStrictEqual(context, trueValue, result.value());
}
bool TestAPI::scriptResultIs(ScriptResult result, JSValueRef value)
{
if (!result)
return false;
return JSValueIsStrictEqual(context, result.value(), value);
}
template<typename... Strings>
bool TestAPI::check(bool condition, Strings... messages)
{
if (!condition) {
dataLogLn(messages..., ": FAILED");
m_failed++;
} else
dataLogLn(messages..., ": PASSED");
return condition;
}
template<typename JSFunctor, typename APIFunctor>
void TestAPI::checkJSAndAPIMatch(const JSFunctor& jsFunctor, const APIFunctor& apiFunctor, const char* description)
{
JSValueRef exception = nullptr;
JSValueRef result = apiFunctor(&exception);
ScriptResult jsResult = jsFunctor();
if (!jsResult) {
check(exception, "JS and API calls should both throw an exception while ", description);
check(functionReturnsTrue("(function(a, b) { return a.constructor === b.constructor; })", exception, jsResult.error()), "JS and API calls should both throw the same exception while ", description);
} else {
check(!exception, "JS and API calls should both not throw an exception while ", description);
check(JSValueIsStrictEqual(context, result, jsResult.value()), "JS result and API calls should return the same value while ", description);
}
}
APIVector<JSObjectRef> TestAPI::interestingObjects()
{
APIVector<JSObjectRef> result(context);
JSObjectRef array = JSValueToObject(context, evaluateScript(
"[{}, [], { [Symbol.iterator]: 1 }, new Date(), new String('str'), new Map(), new Set(), new WeakMap(), new WeakSet(), new Error(), new Number(42), new Boolean(), { get length() { throw new Error(); } }];").value(), nullptr);
APIString lengthString("length");
unsigned length = JSValueToNumber(context, JSObjectGetProperty(context, array, lengthString, nullptr), nullptr);
for (unsigned i = 0; i < length; i++) {
JSObjectRef object = JSValueToObject(context, JSObjectGetPropertyAtIndex(context, array, i, nullptr), nullptr);
ASSERT(object);
result.append(object);
}
return result;
}
APIVector<JSValueRef> TestAPI::interestingKeys()
{
APIVector<JSValueRef> result(context);
JSObjectRef array = JSValueToObject(context, evaluateScript("[{}, [], 1, Symbol.iterator, 'length']").value(), nullptr);
APIString lengthString("length");
unsigned length = JSValueToNumber(context, JSObjectGetProperty(context, array, lengthString, nullptr), nullptr);
for (unsigned i = 0; i < length; i++) {
JSValueRef value = JSObjectGetPropertyAtIndex(context, array, i, nullptr);
ASSERT(value);
result.append(value);
}
return result;
}
static const char* isSymbolFunction = "(function isSymbol(symbol) { return typeof(symbol) === 'symbol'; })";
static const char* getSymbolDescription = "(function getSymbolDescription(symbol) { return symbol.description; })";
static const char* getFunction = "(function get(object, key) { return object[key]; })";
static const char* setFunction = "(function set(object, key, value) { object[key] = value; })";
void TestAPI::basicSymbol()
{
// Can't call Symbol as a constructor since it's not subclassable.
auto result = evaluateScript("Symbol('dope');");
check(JSValueGetType(context, result.value()) == kJSTypeSymbol, "dope get type is a symbol");
check(JSValueIsSymbol(context, result.value()), "dope is a symbol");
}
void TestAPI::symbolsTypeof()
{
{
JSValueRef symbol = JSValueMakeSymbol(context, nullptr);
check(functionReturnsTrue(isSymbolFunction, symbol), "JSValueMakeSymbol makes a symbol value");
}
{
APIString description("dope");
JSValueRef symbol = JSValueMakeSymbol(context, description);
check(functionReturnsTrue(isSymbolFunction, symbol), "JSValueMakeSymbol makes a symbol value");
}
}
void TestAPI::symbolsDescription()
{
{
JSValueRef symbol = JSValueMakeSymbol(context, nullptr);
auto result = callFunction(getSymbolDescription, symbol);
check(JSValueIsStrictEqual(context, result.value(), JSValueMakeUndefined(context)), "JSValueMakeSymbol with nullptr description produces a symbol value without description");
}
{
APIString description("dope");
JSValueRef symbol = JSValueMakeSymbol(context, description);
auto result = callFunction(getSymbolDescription, symbol);
check(JSValueIsStrictEqual(context, result.value(), JSValueMakeString(context, description)), "JSValueMakeSymbol with description string produces a symbol value with description");
}
}
void TestAPI::symbolsGetPropertyForKey()
{
auto objects = interestingObjects();
auto keys = interestingKeys();
for (auto& object : objects) {
dataLogLn("\nnext object: ", toJS(context, object));
for (auto& key : keys) {
dataLogLn("Using key: ", toJS(context, key));
checkJSAndAPIMatch(
[&] {
return callFunction(getFunction, object, key);
}, [&] (JSValueRef* exception) {
return JSObjectGetPropertyForKey(context, object, key, exception);
}, "checking get property keys");
}
}
}
void TestAPI::symbolsSetPropertyForKey()
{
auto jsObjects = interestingObjects();
auto apiObjects = interestingObjects();
auto keys = interestingKeys();
JSValueRef theAnswer = JSValueMakeNumber(context, 42);
for (size_t i = 0; i < jsObjects.size(); i++) {
for (auto& key : keys) {
JSObjectRef jsObject = jsObjects[i];
JSObjectRef apiObject = apiObjects[i];
checkJSAndAPIMatch(
[&] {
return callFunction(setFunction, jsObject, key, theAnswer);
} , [&] (JSValueRef* exception) {
JSObjectSetPropertyForKey(context, apiObject, key, theAnswer, kJSPropertyAttributeNone, exception);
return JSValueMakeUndefined(context);
}, "setting property keys to the answer");
// Check get is the same on API object.
checkJSAndAPIMatch(
[&] {
return callFunction(getFunction, apiObject, key);
}, [&] (JSValueRef* exception) {
return JSObjectGetPropertyForKey(context, apiObject, key, exception);
}, "getting property keys from API objects");
// Check get is the same on respective objects.
checkJSAndAPIMatch(
[&] {
return callFunction(getFunction, jsObject, key);
}, [&] (JSValueRef* exception) {
return JSObjectGetPropertyForKey(context, apiObject, key, exception);
}, "getting property keys from respective objects");
}
}
}
void TestAPI::symbolsHasPropertyForKey()
{
const char* hasFunction = "(function has(object, key) { return key in object; })";
auto objects = interestingObjects();
auto keys = interestingKeys();
JSValueRef theAnswer = JSValueMakeNumber(context, 42);
for (auto& object : objects) {
dataLogLn("\nNext object: ", toJS(context, object));
for (auto& key : keys) {
dataLogLn("Using key: ", toJS(context, key));
checkJSAndAPIMatch(
[&] {
return callFunction(hasFunction, object, key);
}, [&] (JSValueRef* exception) {
return JSValueMakeBoolean(context, JSObjectHasPropertyForKey(context, object, key, exception));
}, "checking has property keys unset");
check(!!callFunction(setFunction, object, key, theAnswer), "set property to the answer");
checkJSAndAPIMatch(
[&] {
return callFunction(hasFunction, object, key);
}, [&] (JSValueRef* exception) {
return JSValueMakeBoolean(context, JSObjectHasPropertyForKey(context, object, key, exception));
}, "checking has property keys set");
}
}
}
void TestAPI::symbolsDeletePropertyForKey()
{
const char* deleteFunction = "(function del(object, key) { return delete object[key]; })";
auto objects = interestingObjects();
auto keys = interestingKeys();
JSValueRef theAnswer = JSValueMakeNumber(context, 42);
for (auto& object : objects) {
dataLogLn("\nNext object: ", toJS(context, object));
for (auto& key : keys) {
dataLogLn("Using key: ", toJS(context, key));
checkJSAndAPIMatch(
[&] {
return callFunction(deleteFunction, object, key);
}, [&] (JSValueRef* exception) {
return JSValueMakeBoolean(context, JSObjectDeletePropertyForKey(context, object, key, exception));
}, "checking has property keys unset");
check(!!callFunction(setFunction, object, key, theAnswer), "set property to the answer");
checkJSAndAPIMatch(
[&] {
return callFunction(deleteFunction, object, key);
}, [&] (JSValueRef* exception) {
return JSValueMakeBoolean(context, JSObjectDeletePropertyForKey(context, object, key, exception));
}, "checking has property keys set");
}
}
}
void TestAPI::promiseResolveTrue()
{
JSObjectRef resolve;
JSObjectRef reject;
JSValueRef exception = nullptr;
JSObjectRef promise = JSObjectMakeDeferredPromise(context, &resolve, &reject, &exception);
check(!exception, "No exception should be thrown creating a deferred promise");
// Ugh, we don't have any C API that takes blocks... so we do this hack to capture the runner.
static TestAPI* tester = this;
static bool passedTrueCalled = false;
APIString trueString("passedTrue");
auto passedTrue = [](JSContextRef ctx, JSObjectRef, JSObjectRef, size_t argumentCount, const JSValueRef arguments[], JSValueRef*) -> JSValueRef {
tester->check(argumentCount && JSValueIsStrictEqual(ctx, arguments[0], JSValueMakeBoolean(ctx, true)), "function should have been called back with true");
passedTrueCalled = true;
return JSValueMakeUndefined(ctx);
};
APIString thenString("then");
JSValueRef thenFunction = JSObjectGetProperty(context, promise, thenString, &exception);
check(!exception && thenFunction && JSValueIsObject(context, thenFunction), "Promise should have a then object property");
JSValueRef passedTrueFunction = JSObjectMakeFunctionWithCallback(context, trueString, passedTrue);
JSObjectCallAsFunction(context, const_cast<JSObjectRef>(thenFunction), promise, 1, &passedTrueFunction, &exception);
check(!exception, "No exception should be thrown setting up callback");
auto trueValue = JSValueMakeBoolean(context, true);
JSObjectCallAsFunction(context, resolve, resolve, 1, &trueValue, &exception);
check(!exception, "No exception should be thrown resolving promise");
check(passedTrueCalled, "then response function should have been called.");
}
void TestAPI::promiseRejectTrue()
{
JSObjectRef resolve;
JSObjectRef reject;
JSValueRef exception = nullptr;
JSObjectRef promise = JSObjectMakeDeferredPromise(context, &resolve, &reject, &exception);
check(!exception, "No exception should be thrown creating a deferred promise");
// Ugh, we don't have any C API that takes blocks... so we do this hack to capture the runner.
static TestAPI* tester = this;
static bool passedTrueCalled = false;
APIString trueString("passedTrue");
auto passedTrue = [](JSContextRef ctx, JSObjectRef, JSObjectRef, size_t argumentCount, const JSValueRef arguments[], JSValueRef*) -> JSValueRef {
tester->check(argumentCount && JSValueIsStrictEqual(ctx, arguments[0], JSValueMakeBoolean(ctx, true)), "function should have been called back with true");
passedTrueCalled = true;
return JSValueMakeUndefined(ctx);
};
APIString catchString("catch");
JSValueRef catchFunction = JSObjectGetProperty(context, promise, catchString, &exception);
check(!exception && catchFunction && JSValueIsObject(context, catchFunction), "Promise should have a catch object property");
JSValueRef passedTrueFunction = JSObjectMakeFunctionWithCallback(context, trueString, passedTrue);
JSObjectCallAsFunction(context, const_cast<JSObjectRef>(catchFunction), promise, 1, &passedTrueFunction, &exception);
check(!exception, "No exception should be thrown setting up callback");
auto trueValue = JSValueMakeBoolean(context, true);
JSObjectCallAsFunction(context, reject, reject, 1, &trueValue, &exception);
check(!exception, "No exception should be thrown rejecting promise");
check(passedTrueCalled, "catch response function should have been called.");
}
void TestAPI::promiseUnhandledRejection()
{
JSObjectRef reject = nullptr;
JSValueRef exception = nullptr;
static auto promise = JSObjectMakeDeferredPromise(context, nullptr, &reject, &exception);
check(!exception, "creating a (reject-only) deferred promise should not throw");
static auto reason = JSValueMakeString(context, APIString("reason"));
static TestAPI* tester = this;
static bool callbackCalled = false;
auto callback = [](JSContextRef ctx, JSObjectRef, JSObjectRef, size_t argumentCount, const JSValueRef arguments[], JSValueRef*) -> JSValueRef {
if (callbackCalled)
return JSValueMakeUndefined(ctx);
tester->check(argumentCount && JSValueIsStrictEqual(ctx, arguments[0], promise), "callback should receive rejected promise as first argument");
tester->check(argumentCount > 1 && JSValueIsStrictEqual(ctx, arguments[1], reason), "callback should receive rejection reason as second argument");
tester->check(argumentCount == 2, "callback should not receive a third argument");
callbackCalled = true;
return JSValueMakeUndefined(ctx);
};
auto callbackFunction = JSObjectMakeFunctionWithCallback(context, APIString("callback"), callback);
JSGlobalContextSetUnhandledRejectionCallback(context, callbackFunction, &exception);
check(!exception, "setting unhandled rejection callback should not throw");
JSObjectCallAsFunction(context, reject, reject, 1, &reason, &exception);
check(!exception && callbackCalled, "unhandled rejection callback should be called upon unhandled rejection");
}
void TestAPI::promiseUnhandledRejectionFromUnhandledRejectionCallback()
{
static JSObjectRef reject;
static JSValueRef exception = nullptr;
JSObjectMakeDeferredPromise(context, nullptr, &reject, &exception);
check(!exception, "creating a (reject-only) deferred promise should not throw");
static auto callbackCallCount = 0;
auto callback = [](JSContextRef ctx, JSObjectRef, JSObjectRef, size_t, const JSValueRef[], JSValueRef*) -> JSValueRef {
if (!callbackCallCount)
JSObjectCallAsFunction(ctx, reject, reject, 0, nullptr, &exception);
callbackCallCount++;
return JSValueMakeUndefined(ctx);
};
auto callbackFunction = JSObjectMakeFunctionWithCallback(context, APIString("callback"), callback);
JSGlobalContextSetUnhandledRejectionCallback(context, callbackFunction, &exception);
check(!exception, "setting unhandled rejection callback should not throw");
callFunction("(function () { Promise.reject(); })");
check(!exception && callbackCallCount == 2, "unhandled rejection from unhandled rejection callback should also trigger the callback");
}
void TestAPI::promiseEarlyHandledRejections()
{
JSValueRef exception = nullptr;
static bool callbackCalled = false;
auto callback = [](JSContextRef ctx, JSObjectRef, JSObjectRef, size_t, const JSValueRef[], JSValueRef*) -> JSValueRef {
callbackCalled = true;
return JSValueMakeUndefined(ctx);
};
auto callbackFunction = JSObjectMakeFunctionWithCallback(context, APIString("callback"), callback);
JSGlobalContextSetUnhandledRejectionCallback(context, callbackFunction, &exception);
check(!exception, "setting unhandled rejection callback should not throw");
callFunction("(function () { const p = Promise.reject(); p.catch(() => {}); })");
check(!callbackCalled, "unhandled rejection callback should not be called for synchronous early-handled rejection");
callFunction("(function () { const p = Promise.reject(); Promise.resolve().then(() => { p.catch(() => {}); }); })");
check(!callbackCalled, "unhandled rejection callback should not be called for asynchronous early-handled rejection");
}
void TestAPI::promiseDrainDoesNotEatExceptions()
{
#if PLATFORM(COCOA)
bool useLegacyDrain = !linkedOnOrAfterSDKWithBehavior(SDKAlignedBehavior::DoesNotDrainTheMicrotaskQueueWhenCallingObjC);
if (useLegacyDrain)
return;
#endif
ScriptResult result = callFunction("(function() { Promise.resolve().then(() => { throw 2; }); throw 1; })");
check(!result, "function should throw an error");
check(JSValueIsNumber(context, result.error()) && JSValueToNumber(context, result.error(), nullptr) == 1, "exception payload should have been 1");
}
void TestAPI::topCallFrameAccess()
{
{
JSObjectRef function = JSValueToObject(context, evaluateScript("(function () { })").value(), nullptr);
APIString argumentsString("arguments");
auto arguments = JSObjectGetProperty(context, function, argumentsString, nullptr);
check(JSValueIsNull(context, arguments), "vm.topCallFrame access from C++ world should use nullptr internally for arguments");
}
{
JSObjectRef arguments = JSValueToObject(context, evaluateScript("(function ok(v) { return ok.arguments; })(42)").value(), nullptr);
check(!JSValueIsNull(context, arguments), "vm.topCallFrame is materialized and we found the caller function's arguments");
}
{
JSObjectRef function = JSValueToObject(context, evaluateScript("(function () { })").value(), nullptr);
APIString callerString("caller");
auto caller = JSObjectGetProperty(context, function, callerString, nullptr);
check(JSValueIsNull(context, caller), "vm.topCallFrame access from C++ world should use nullptr internally for caller");
}
{
JSObjectRef caller = JSValueToObject(context, evaluateScript("(function () { return (function ok(v) { return ok.caller; })(42); })()").value(), nullptr);
check(!JSValueIsNull(context, caller), "vm.topCallFrame is materialized and we found the caller function's caller");
}
{
JSObjectRef caller = JSValueToObject(context, evaluateScript("(function ok(v) { return ok.caller; })(42)").value(), nullptr);
check(JSValueIsNull(context, caller), "vm.topCallFrame is materialized and we found the caller function's caller, but the caller is global code");
}
}
void TestAPI::markedJSValueArrayAndGC()
{
auto testMarkedJSValueArray = [&] (unsigned count) {
auto* globalObject = toJS(context);
JSC::JSLockHolder locker(globalObject->vm());
JSC::MarkedJSValueRefArray values(context, count);
for (unsigned index = 0; index < count; ++index) {
JSValueRef string = JSValueMakeString(context, APIString(makeString("Prefix"_s, index)));
values[index] = string;
}
JSSynchronousGarbageCollectForDebugging(context);
bool ok = true;
for (unsigned index = 0; index < count; ++index) {
JSValueRef string = JSValueMakeString(context, APIString(makeString("Prefix"_s, index)));
if (!JSValueIsStrictEqual(context, values[index], string))
ok = false;
}
check(ok, "Held JSString should be alive and correct.");
};
testMarkedJSValueArray(4);
testMarkedJSValueArray(1000);
}
void TestAPI::classDefinitionWithJSSubclass()
{
const static JSClassDefinition definition = kJSClassDefinitionEmpty;
static JSClassRef jsClass = JSClassCreate(&definition);
auto constructor = [] (JSContextRef ctx, JSObjectRef, size_t, const JSValueRef*, JSValueRef*) -> JSObjectRef {
return JSObjectMake(ctx, jsClass, nullptr);
};
JSObjectRef Superclass = JSObjectMakeConstructor(context, jsClass, constructor);
ScriptResult result = callFunction("(function (Superclass) { class Subclass extends Superclass { method() { return 'value'; } }; return new Subclass(); })", Superclass);
check(!!result, "creating a subclass should not throw.");
check(JSValueIsObject(context, result.value()), "result of construction should have been an object.");
JSObjectRef subclass = const_cast<JSObjectRef>(result.value());
check(JSObjectHasProperty(context, subclass, APIString("method")), "subclass should have derived classes functions.");
check(functionReturnsTrue("(function (subclass, Superclass) { return subclass instanceof Superclass; })", subclass, Superclass), "JS subclass should instanceof the Superclass");
JSClassRelease(jsClass);
}
void TestAPI::proxyReturnedWithJSSubclassing()
{
const static JSClassDefinition definition = kJSClassDefinitionEmpty;
static JSClassRef jsClass = JSClassCreate(&definition);
static TestAPI& test = *this;
auto constructor = [] (JSContextRef ctx, JSObjectRef, size_t, const JSValueRef*, JSValueRef*) -> JSObjectRef {
ScriptResult result = test.callFunction("(function (object) { return new Proxy(object, { getPrototypeOf: () => { globalThis.triggeredProxy = true; return object.__proto__; }}); })", JSObjectMake(ctx, jsClass, nullptr));
test.check(!!result, "creating a proxy should not throw");
test.check(JSValueIsObject(ctx, result.value()), "result of proxy creation should have been an object.");
return const_cast<JSObjectRef>(result.value());
};
JSObjectRef Superclass = JSObjectMakeConstructor(context, jsClass, constructor);
ScriptResult result = callFunction("(function (Superclass) { class Subclass extends Superclass { method() { return 'value'; } }; return new Subclass(); })", Superclass);
check(!!result, "creating a subclass should not throw.");
check(JSValueIsObject(context, result.value()), "result of construction should have been an object.");
JSObjectRef subclass = const_cast<JSObjectRef>(result.value());
check(scriptResultIs(evaluateScript("globalThis.triggeredProxy"), JSValueMakeUndefined(context)), "creating a subclass should not have triggered the proxy");
check(functionReturnsTrue("(function (subclass, Superclass) { return subclass.__proto__ == Superclass.prototype; })", subclass, Superclass), "proxy's prototype should match Superclass.prototype");
}
void TestAPI::testJSObjectSetOnGlobalObjectSubclassDefinition()
{
JSClassDefinition globalClassDef = kJSClassDefinitionEmpty;
globalClassDef.className = "CustomGlobalClass";
JSClassRef globalClassRef = JSClassCreate(&globalClassDef);
JSContextRef context = JSGlobalContextCreate(globalClassRef);
JSObjectRef newObject = JSObjectMake(context, nullptr, nullptr);
JSObjectRef globalObject = JSContextGetGlobalObject(context);
APIString propertyName("myObject");
JSObjectSetProperty(context, globalObject, propertyName, newObject, 0, nullptr);
check(JSEvaluateScript(context, propertyName, globalObject, nullptr, 1, nullptr) == newObject, "Setting a property on a custom global object should set the property");
}
void configureJSCForTesting()
{
JSC::Config::configureForTesting();
}
#define RUN(test) do { \
if (!shouldRun(#test)) \
break; \
tasks.append( \
createSharedTask<void(TestAPI&)>( \
[&] (TestAPI& tester) { \
tester.test; \
dataLog(#test ": OK!\n"); \
})); \
} while (false)
int testCAPIViaCpp(const char* filter)
{
dataLogLn("Starting C-API tests in C++");
Deque<RefPtr<SharedTask<void(TestAPI&)>>> tasks;
auto shouldRun = [&] (const char* testName) -> bool {
return !filter || WTF::findIgnoringASCIICaseWithoutLength(testName, filter) != WTF::notFound;
};
RUN(topCallFrameAccess());
RUN(basicSymbol());
RUN(symbolsTypeof());
RUN(symbolsDescription());
RUN(symbolsGetPropertyForKey());
RUN(symbolsSetPropertyForKey());
RUN(symbolsHasPropertyForKey());
RUN(symbolsDeletePropertyForKey());
RUN(promiseResolveTrue());
RUN(promiseRejectTrue());
RUN(promiseUnhandledRejection());
RUN(promiseUnhandledRejectionFromUnhandledRejectionCallback());
RUN(promiseDrainDoesNotEatExceptions());
RUN(promiseEarlyHandledRejections());
RUN(markedJSValueArrayAndGC());
RUN(classDefinitionWithJSSubclass());
RUN(proxyReturnedWithJSSubclassing());
RUN(testJSObjectSetOnGlobalObjectSubclassDefinition());
if (tasks.isEmpty())
return 0;
Lock lock;
static Atomic<int> failed { 0 };
Vector<Ref<Thread>> threads;
for (unsigned i = filter ? 1 : WTF::numberOfProcessorCores(); i--;) {
threads.append(Thread::create(
"Testapi via C++ thread",
[&] () {
TestAPI tester;
for (;;) {
RefPtr<SharedTask<void(TestAPI&)>> task;
{
Locker locker { lock };
if (tasks.isEmpty())
break;
task = tasks.takeFirst();
}
task->run(tester);
}
failed.exchangeAdd(tester.failed());
}));
}
for (auto& thread : threads)
thread->waitForCompletion();
dataLogLn("C-API tests in C++ had ", failed.load(), " failures");
return failed.load();
}
|