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 { ...@@ -33,7 +33,7 @@ namespace array {
mapping = false; mapping = false;
} else { } else {
// a. If IsCallable(mapfn) is false, throw a TypeError exception. // a. If IsCallable(mapfn) is false, throw a TypeError exception.
if (!TaggedIsCallable(mapfn)) deferred { if (!Is<Callable>(mapfn)) deferred {
ThrowTypeError(MessageTemplate::kCalledNonCallable, mapfn); ThrowTypeError(MessageTemplate::kCalledNonCallable, mapfn);
} }
// b. Let mapping be true. // b. Let mapping be true.
......
...@@ -2,7 +2,6 @@ ...@@ -2,7 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
extern macro IsCallable(HeapObject): bool;
extern macro IsConstructor(HeapObject): bool; extern macro IsConstructor(HeapObject): bool;
extern macro IsFeedbackVector(HeapObject): bool; extern macro IsFeedbackVector(HeapObject): bool;
extern macro IsJSArray(HeapObject): bool; extern macro IsJSArray(HeapObject): bool;
...@@ -19,7 +18,6 @@ extern macro IsNumberDictionary(HeapObject): bool; ...@@ -19,7 +18,6 @@ extern macro IsNumberDictionary(HeapObject): bool;
extern macro IsContext(HeapObject): bool; extern macro IsContext(HeapObject): bool;
extern macro IsNativeContext(HeapObject): bool; extern macro IsNativeContext(HeapObject): bool;
extern macro IsJSReceiver(HeapObject): bool; extern macro IsJSReceiver(HeapObject): bool;
extern macro TaggedIsCallable(Object): bool;
extern macro IsHeapNumber(HeapObject): bool; extern macro IsHeapNumber(HeapObject): bool;
extern macro IsBigInt(HeapObject): bool; extern macro IsBigInt(HeapObject): bool;
extern macro IsFixedArray(HeapObject): bool; extern macro IsFixedArray(HeapObject): bool;
...@@ -740,3 +738,8 @@ UnsafeCast<RegExpMatchInfo>(implicit context: Context)(o: Object): ...@@ -740,3 +738,8 @@ UnsafeCast<RegExpMatchInfo>(implicit context: Context)(o: Object):
assert(Is<FixedArray>(o)); assert(Is<FixedArray>(o));
return %RawDownCast<RegExpMatchInfo>(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 { ...@@ -345,8 +345,8 @@ namespace promise {
const promise = Construct(promiseConstructor, executor); const promise = Construct(promiseConstructor, executor);
capability.promise = promise; capability.promise = promise;
if (!TaggedIsCallable(capability.resolve) || if (!Is<Callable>(capability.resolve) ||
!TaggedIsCallable(capability.reject)) { !Is<Callable>(capability.reject)) {
ThrowTypeError(MessageTemplate::kPromiseNonCallable); ThrowTypeError(MessageTemplate::kPromiseNonCallable);
} }
return capability; return capability;
......
...@@ -53,7 +53,7 @@ namespace promise { ...@@ -53,7 +53,7 @@ namespace promise {
} }
// 2. If IsCallable(executor) is false, throw a TypeError exception. // 2. If IsCallable(executor) is false, throw a TypeError exception.
if (!TaggedIsCallable(executor)) { if (!Is<Callable>(executor)) {
ThrowTypeError(MessageTemplate::kResolverNotAFunction, executor); ThrowTypeError(MessageTemplate::kResolverNotAFunction, executor);
} }
......
...@@ -188,17 +188,18 @@ namespace promise { ...@@ -188,17 +188,18 @@ namespace promise {
// slots to onFinally. // slots to onFinally.
let thenFinally: JSAny; let thenFinally: JSAny;
let catchFinally: JSAny; let catchFinally: JSAny;
if (!TaggedIsSmi(onFinally) && typeswitch (onFinally) {
IsCallable(UnsafeCast<HeapObject>(onFinally))) { case (onFinally: Callable): {
const pair = CreatePromiseFinallyFunctions( const pair = CreatePromiseFinallyFunctions(
nativeContext, UnsafeCast<Callable>(onFinally), constructor); nativeContext, onFinally, constructor);
thenFinally = pair.then_finally; thenFinally = pair.then_finally;
catchFinally = pair.catch_finally; catchFinally = pair.catch_finally;
} else }
deferred { case (JSAny): deferred {
thenFinally = onFinally; thenFinally = onFinally;
catchFinally = onFinally; catchFinally = onFinally;
} }
}
// 7. Return ? Invoke(promise, "then", « thenFinally, catchFinally »). // 7. Return ? Invoke(promise, "then", « thenFinally, catchFinally »).
return UnsafeCast<JSAny>( return UnsafeCast<JSAny>(
......
...@@ -165,11 +165,8 @@ namespace promise { ...@@ -165,11 +165,8 @@ namespace promise {
} }
// 11. If IsCallable(thenAction) is false, then // 11. If IsCallable(thenAction) is false, then
if (TaggedIsSmi(then)) { if (!Is<Callable>(then)) {
return FulfillPromise(promise, resolution); // a. Return FulfillPromise(promise, resolution).
}
if (!IsCallable(UnsafeCast<HeapObject>(then))) {
return FulfillPromise(promise, resolution); return FulfillPromise(promise, resolution);
} }
goto Enqueue; goto Enqueue;
......
...@@ -60,15 +60,11 @@ namespace promise { ...@@ -60,15 +60,11 @@ namespace promise {
// 3. If IsCallable(onFulfilled) is false, then // 3. If IsCallable(onFulfilled) is false, then
// a. Set onFulfilled to undefined. // a. Set onFulfilled to undefined.
const onFulfilled = TaggedIsCallable(onFulfilled) ? const onFulfilled = CastOrDefault<Callable>(onFulfilled, Undefined);
UnsafeCast<Callable>(onFulfilled) :
Undefined;
// 4. If IsCallable(onRejected) is false, then // 4. If IsCallable(onRejected) is false, then
// a. Set onRejected to undefined. // a. Set onRejected to undefined.
const onRejected = TaggedIsCallable(onRejected) ? const onRejected = CastOrDefault<Callable>(onRejected, Undefined);
UnsafeCast<Callable>(onRejected) :
Undefined;
// 5. Return PerformPromiseThen(promise, onFulfilled, onRejected, // 5. Return PerformPromiseThen(promise, onFulfilled, onRejected,
// resultCapability). // resultCapability).
......
...@@ -131,7 +131,7 @@ namespace string { ...@@ -131,7 +131,7 @@ namespace string {
// 5. Let functionalReplace be IsCallable(replaceValue). // 5. Let functionalReplace be IsCallable(replaceValue).
let replaceValueArg = replaceValue; let replaceValueArg = replaceValue;
const functionalReplace = TaggedIsCallable(replaceValue); const functionalReplace = Is<Callable>(replaceValue);
// 6. If functionalReplace is false, then // 6. If functionalReplace is false, then
if (!functionalReplace) { if (!functionalReplace) {
......
...@@ -32,7 +32,7 @@ namespace typed_array { ...@@ -32,7 +32,7 @@ namespace typed_array {
// 4. Else, let mapping be false. // 4. Else, let mapping be false.
const mapping: bool = arguments.length > 1; const mapping: bool = arguments.length > 1;
const mapfnObj: JSAny = mapping ? arguments[1] : Undefined; const mapfnObj: JSAny = mapping ? arguments[1] : Undefined;
if (mapping && !TaggedIsCallable(mapfnObj)) deferred { if (mapping && !Is<Callable>(mapfnObj)) deferred {
ThrowTypeError(MessageTemplate::kCalledNonCallable, mapfnObj); ThrowTypeError(MessageTemplate::kCalledNonCallable, mapfnObj);
} }
......
...@@ -92,7 +92,7 @@ namespace typed_array { ...@@ -92,7 +92,7 @@ namespace typed_array {
// 1. If comparefn is not undefined and IsCallable(comparefn) is false, // 1. If comparefn is not undefined and IsCallable(comparefn) is false,
// throw a TypeError exception. // throw a TypeError exception.
const comparefnObj: JSAny = arguments.length > 0 ? arguments[0] : Undefined; const comparefnObj: JSAny = arguments.length > 0 ? arguments[0] : Undefined;
if (comparefnObj != Undefined && !TaggedIsCallable(comparefnObj)) { if (comparefnObj != Undefined && !Is<Callable>(comparefnObj)) {
ThrowTypeError(MessageTemplate::kBadSortComparisonFunction, 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