Commit 51c00e9f authored by Timothy Gu's avatar Timothy Gu Committed by Commit Bot

[torque] Add CastOrDefault and consistently use Is<Callable>

Remove other variants for the same functionality such as IsCallable(),
TaggedIsCallable(), and !TaggedIsSmi() && IsCallable().

Change-Id: I33bcdf7699c1adf2330b3c11f482f7bbfcd927b3
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2107515
Commit-Queue: Timothy Gu <timothygu@chromium.org>
Reviewed-by: 's avatarTobias Tebbi <tebbi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#66973}
parent ec4ccade
......@@ -33,7 +33,7 @@ namespace array {
mapping = false;
} else {
// a. If IsCallable(mapfn) is false, throw a TypeError exception.
if (!TaggedIsCallable(mapfn)) deferred {
if (!Is<Callable>(mapfn)) deferred {
ThrowTypeError(MessageTemplate::kCalledNonCallable, mapfn);
}
// b. Let mapping be true.
......
......@@ -2,7 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
extern macro IsCallable(HeapObject): bool;
extern macro IsConstructor(HeapObject): bool;
extern macro IsFeedbackVector(HeapObject): bool;
extern macro IsJSArray(HeapObject): bool;
......@@ -19,7 +18,6 @@ extern macro IsNumberDictionary(HeapObject): bool;
extern macro IsContext(HeapObject): bool;
extern macro IsNativeContext(HeapObject): bool;
extern macro IsJSReceiver(HeapObject): bool;
extern macro TaggedIsCallable(Object): bool;
extern macro IsHeapNumber(HeapObject): bool;
extern macro IsBigInt(HeapObject): bool;
extern macro IsFixedArray(HeapObject): bool;
......@@ -740,3 +738,8 @@ UnsafeCast<RegExpMatchInfo>(implicit context: Context)(o: Object):
assert(Is<FixedArray>(o));
return %RawDownCast<RegExpMatchInfo>(o);
}
macro CastOrDefault<T: type, Arg: type, Default: type>(
implicit context: Context)(x: Arg, default: Default): T|Default {
return Cast<T>(x) otherwise return default;
}
......@@ -345,8 +345,8 @@ namespace promise {
const promise = Construct(promiseConstructor, executor);
capability.promise = promise;
if (!TaggedIsCallable(capability.resolve) ||
!TaggedIsCallable(capability.reject)) {
if (!Is<Callable>(capability.resolve) ||
!Is<Callable>(capability.reject)) {
ThrowTypeError(MessageTemplate::kPromiseNonCallable);
}
return capability;
......
......@@ -53,7 +53,7 @@ namespace promise {
}
// 2. If IsCallable(executor) is false, throw a TypeError exception.
if (!TaggedIsCallable(executor)) {
if (!Is<Callable>(executor)) {
ThrowTypeError(MessageTemplate::kResolverNotAFunction, executor);
}
......
......@@ -188,17 +188,18 @@ namespace promise {
// slots to onFinally.
let thenFinally: JSAny;
let catchFinally: JSAny;
if (!TaggedIsSmi(onFinally) &&
IsCallable(UnsafeCast<HeapObject>(onFinally))) {
typeswitch (onFinally) {
case (onFinally: Callable): {
const pair = CreatePromiseFinallyFunctions(
nativeContext, UnsafeCast<Callable>(onFinally), constructor);
nativeContext, onFinally, constructor);
thenFinally = pair.then_finally;
catchFinally = pair.catch_finally;
} else
deferred {
}
case (JSAny): deferred {
thenFinally = onFinally;
catchFinally = onFinally;
}
}
// 7. Return ? Invoke(promise, "then", « thenFinally, catchFinally »).
return UnsafeCast<JSAny>(
......
......@@ -165,11 +165,8 @@ namespace promise {
}
// 11. If IsCallable(thenAction) is false, then
if (TaggedIsSmi(then)) {
return FulfillPromise(promise, resolution);
}
if (!IsCallable(UnsafeCast<HeapObject>(then))) {
if (!Is<Callable>(then)) {
// a. Return FulfillPromise(promise, resolution).
return FulfillPromise(promise, resolution);
}
goto Enqueue;
......
......@@ -60,15 +60,11 @@ namespace promise {
// 3. If IsCallable(onFulfilled) is false, then
// a. Set onFulfilled to undefined.
const onFulfilled = TaggedIsCallable(onFulfilled) ?
UnsafeCast<Callable>(onFulfilled) :
Undefined;
const onFulfilled = CastOrDefault<Callable>(onFulfilled, Undefined);
// 4. If IsCallable(onRejected) is false, then
// a. Set onRejected to undefined.
const onRejected = TaggedIsCallable(onRejected) ?
UnsafeCast<Callable>(onRejected) :
Undefined;
const onRejected = CastOrDefault<Callable>(onRejected, Undefined);
// 5. Return PerformPromiseThen(promise, onFulfilled, onRejected,
// resultCapability).
......
......@@ -131,7 +131,7 @@ namespace string {
// 5. Let functionalReplace be IsCallable(replaceValue).
let replaceValueArg = replaceValue;
const functionalReplace = TaggedIsCallable(replaceValue);
const functionalReplace = Is<Callable>(replaceValue);
// 6. If functionalReplace is false, then
if (!functionalReplace) {
......
......@@ -32,7 +32,7 @@ namespace typed_array {
// 4. Else, let mapping be false.
const mapping: bool = arguments.length > 1;
const mapfnObj: JSAny = mapping ? arguments[1] : Undefined;
if (mapping && !TaggedIsCallable(mapfnObj)) deferred {
if (mapping && !Is<Callable>(mapfnObj)) deferred {
ThrowTypeError(MessageTemplate::kCalledNonCallable, mapfnObj);
}
......
......@@ -92,7 +92,7 @@ namespace typed_array {
// 1. If comparefn is not undefined and IsCallable(comparefn) is false,
// throw a TypeError exception.
const comparefnObj: JSAny = arguments.length > 0 ? arguments[0] : Undefined;
if (comparefnObj != Undefined && !TaggedIsCallable(comparefnObj)) {
if (comparefnObj != Undefined && !Is<Callable>(comparefnObj)) {
ThrowTypeError(MessageTemplate::kBadSortComparisonFunction, comparefnObj);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment