Commit 4400755a authored by Tobias Tebbi's avatar Tobias Tebbi Committed by Commit Bot

[torque] make map field const

As an escape hatch, add UnsafeConstCast() to still mutate the map
field where necessary.

Drive-by change: Refactor NewPromiseReactionJobTask to avoid unsafe
allocation and map mutations.

Bug: v8:7793
Change-Id: I90e06340c1cf048059b544f1c0a6f730f75d200c
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2096675
Commit-Queue: Tobias Tebbi <tebbi@chromium.org>
Reviewed-by: 's avatarNico Hartmann <nicohartmann@chromium.org>
Cr-Commit-Position: refs/heads/master@{#66766}
parent a9606996
...@@ -940,6 +940,10 @@ macro UnsafeCast<A : type extends Object>(implicit context: Context)(o: Object): ...@@ -940,6 +940,10 @@ macro UnsafeCast<A : type extends Object>(implicit context: Context)(o: Object):
return %RawDownCast<A>(o); return %RawDownCast<A>(o);
} }
macro UnsafeConstCast<T: type>(r: const &T):&T {
return %RawDownCast<&T>(r);
}
extern macro FixedArrayMapConstant(): Map; extern macro FixedArrayMapConstant(): Map;
extern macro FixedDoubleArrayMapConstant(): Map; extern macro FixedDoubleArrayMapConstant(): Map;
extern macro FixedCOWArrayMapConstant(): Map; extern macro FixedCOWArrayMapConstant(): Map;
......
...@@ -28,11 +28,6 @@ void PromiseBuiltinsAssembler::ZeroOutEmbedderOffsets( ...@@ -28,11 +28,6 @@ void PromiseBuiltinsAssembler::ZeroOutEmbedderOffsets(
} }
} }
TNode<HeapObject> PromiseBuiltinsAssembler::AllocatePromiseReactionJobTask(
TNode<Context> context) {
return Allocate(PromiseReactionJobTask::kSizeOfAllPromiseReactionJobTasks);
}
TNode<HeapObject> PromiseBuiltinsAssembler::AllocateJSPromise( TNode<HeapObject> PromiseBuiltinsAssembler::AllocateJSPromise(
TNode<Context> context) { TNode<Context> context) {
return Allocate(JSPromise::kSizeWithEmbedderFields); return Allocate(JSPromise::kSizeWithEmbedderFields);
......
...@@ -20,8 +20,6 @@ class V8_EXPORT_PRIVATE PromiseBuiltinsAssembler : public CodeStubAssembler { ...@@ -20,8 +20,6 @@ class V8_EXPORT_PRIVATE PromiseBuiltinsAssembler : public CodeStubAssembler {
void ZeroOutEmbedderOffsets(TNode<JSPromise> promise); void ZeroOutEmbedderOffsets(TNode<JSPromise> promise);
TNode<HeapObject> AllocateJSPromise(TNode<Context> context); TNode<HeapObject> AllocateJSPromise(TNode<Context> context);
TNode<HeapObject> AllocatePromiseReactionJobTask(TNode<Context> context);
}; };
} // namespace internal } // namespace internal
......
...@@ -115,7 +115,8 @@ namespace promise { ...@@ -115,7 +115,8 @@ namespace promise {
kPromiseReactionSize == kPromiseReactionSize ==
kPromiseReactionJobTaskSizeOfAllPromiseReactionJobTasks); kPromiseReactionJobTaskSizeOfAllPromiseReactionJobTasks);
if constexpr (reactionType == kPromiseReactionFulfill) { if constexpr (reactionType == kPromiseReactionFulfill) {
promiseReaction.map = PromiseFulfillReactionJobTaskMapConstant(); * UnsafeConstCast(& promiseReaction.map) =
PromiseFulfillReactionJobTaskMapConstant();
const promiseReactionJobTask = const promiseReactionJobTask =
UnsafeCast<PromiseFulfillReactionJobTask>(promiseReaction); UnsafeCast<PromiseFulfillReactionJobTask>(promiseReaction);
promiseReactionJobTask.argument = argument; promiseReactionJobTask.argument = argument;
...@@ -129,7 +130,8 @@ namespace promise { ...@@ -129,7 +130,8 @@ namespace promise {
kPromiseReactionJobTaskPromiseOrCapabilityOffset); kPromiseReactionJobTaskPromiseOrCapabilityOffset);
} else { } else {
StaticAssert(reactionType == kPromiseReactionReject); StaticAssert(reactionType == kPromiseReactionReject);
promiseReaction.map = PromiseRejectReactionJobTaskMapConstant(); * UnsafeConstCast(& promiseReaction.map) =
PromiseRejectReactionJobTaskMapConstant();
const promiseReactionJobTask = const promiseReactionJobTask =
UnsafeCast<PromiseRejectReactionJobTask>(promiseReaction); UnsafeCast<PromiseRejectReactionJobTask>(promiseReaction);
promiseReactionJobTask.argument = argument; promiseReactionJobTask.argument = argument;
...@@ -437,28 +439,25 @@ namespace promise { ...@@ -437,28 +439,25 @@ namespace promise {
onFulfilled, onRejected); onFulfilled, onRejected);
promise.reactions_or_result = reaction; promise.reactions_or_result = reaction;
} else { } else {
let map: Map; const reactionsOrResult = promise.reactions_or_result;
let handler: Callable|Undefined = Undefined; let microtask: PromiseReactionJobTask;
let handlerContext: Context; let handlerContext: Context;
if (promise.Status() == PromiseState::kFulfilled) { if (promise.Status() == PromiseState::kFulfilled) {
map = PromiseFulfillReactionJobTaskMapConstant();
handler = onFulfilled;
handlerContext = ExtractHandlerContext(onFulfilled, onRejected); handlerContext = ExtractHandlerContext(onFulfilled, onRejected);
microtask = NewPromiseFulfillReactionJobTask(
handlerContext, reactionsOrResult, onFulfilled,
resultPromiseOrCapability);
} else } else
deferred { deferred {
assert(promise.Status() == PromiseState::kRejected); assert(promise.Status() == PromiseState::kRejected);
map = PromiseRejectReactionJobTaskMapConstant();
handler = onRejected;
handlerContext = ExtractHandlerContext(onRejected, onFulfilled); handlerContext = ExtractHandlerContext(onRejected, onFulfilled);
microtask = NewPromiseRejectReactionJobTask(
handlerContext, reactionsOrResult, onRejected,
resultPromiseOrCapability);
if (!promise.HasHandler()) { if (!promise.HasHandler()) {
runtime::PromiseRevokeReject(promise); runtime::PromiseRevokeReject(promise);
} }
} }
const reactionsOrResult = promise.reactions_or_result;
const microtask = NewPromiseReactionJobTask(
map, handlerContext, reactionsOrResult, handler,
resultPromiseOrCapability);
EnqueueMicrotask(handlerContext, microtask); EnqueueMicrotask(handlerContext, microtask);
} }
promise.SetHasHandler(); promise.SetHasHandler();
......
...@@ -16,10 +16,6 @@ namespace promise_internal { ...@@ -16,10 +16,6 @@ namespace promise_internal {
void; void;
extern macro PromiseBuiltinsAssembler::AllocateJSPromise(Context): HeapObject; extern macro PromiseBuiltinsAssembler::AllocateJSPromise(Context): HeapObject;
extern macro PromiseBuiltinsAssembler::AllocatePromiseReactionJobTask(
Context): HeapObject;
} }
namespace promise { namespace promise {
...@@ -45,7 +41,7 @@ namespace promise { ...@@ -45,7 +41,7 @@ namespace promise {
assert(IsFunctionWithPrototypeSlotMap(promiseFun.map)); assert(IsFunctionWithPrototypeSlotMap(promiseFun.map));
const promiseMap = UnsafeCast<Map>(promiseFun.prototype_or_initial_map); const promiseMap = UnsafeCast<Map>(promiseFun.prototype_or_initial_map);
const promiseHeapObject = promise_internal::AllocateJSPromise(context); const promiseHeapObject = promise_internal::AllocateJSPromise(context);
promiseHeapObject.map = promiseMap; * UnsafeConstCast(& promiseHeapObject.map) = promiseMap;
const promise = UnsafeCast<JSPromise>(promiseHeapObject); const promise = UnsafeCast<JSPromise>(promiseHeapObject);
promise.properties_or_hash = kEmptyFixedArray; promise.properties_or_hash = kEmptyFixedArray;
promise.elements = kEmptyFixedArray; promise.elements = kEmptyFixedArray;
...@@ -54,24 +50,36 @@ namespace promise { ...@@ -54,24 +50,36 @@ namespace promise {
return promise; return promise;
} }
macro NewPromiseReactionJobTask(implicit context: Context)( macro NewPromiseFulfillReactionJobTask(implicit context: Context)(
map: Map, handlerContext: Context, argument: Object, handlerContext: Context, argument: Object, handler: Callable|Undefined,
handler: Callable|Undefined, promiseOrCapability: JSPromise|PromiseCapability|
Undefined): PromiseFulfillReactionJobTask {
const nativeContext = LoadNativeContext(handlerContext);
return new PromiseFulfillReactionJobTask{
map: PromiseFulfillReactionJobTaskMapConstant(),
argument,
context: handlerContext,
handler,
promise_or_capability: promiseOrCapability,
continuation_preserved_embedder_data: nativeContext
[NativeContextSlot::CONTINUATION_PRESERVED_EMBEDDER_DATA_INDEX]
};
}
macro NewPromiseRejectReactionJobTask(implicit context: Context)(
handlerContext: Context, argument: Object, handler: Callable|Undefined,
promiseOrCapability: JSPromise|PromiseCapability| promiseOrCapability: JSPromise|PromiseCapability|
Undefined): PromiseReactionJobTask { Undefined): PromiseRejectReactionJobTask {
const nativeContext = LoadNativeContext(handlerContext); const nativeContext = LoadNativeContext(handlerContext);
const taskHeapObject = return new PromiseRejectReactionJobTask{
promise_internal::AllocatePromiseReactionJobTask(context); map: PromiseRejectReactionJobTaskMapConstant(),
taskHeapObject.map = map; argument,
const jobTask = UnsafeCast<PromiseReactionJobTask>(taskHeapObject); context: handlerContext,
jobTask.argument = argument; handler,
jobTask.context = handlerContext; promise_or_capability: promiseOrCapability,
jobTask.handler = handler; continuation_preserved_embedder_data: nativeContext
jobTask.promise_or_capability = promiseOrCapability; [NativeContextSlot::CONTINUATION_PRESERVED_EMBEDDER_DATA_INDEX]
jobTask.continuation_preserved_embedder_data = };
nativeContext[NativeContextSlot::
CONTINUATION_PRESERVED_EMBEDDER_DATA_INDEX];
return jobTask;
} }
// These allocate and initialize a promise with pending state and // These allocate and initialize a promise with pending state and
......
...@@ -4,5 +4,5 @@ ...@@ -4,5 +4,5 @@
@abstract @abstract
extern class HeapObject extends StrongTagged { extern class HeapObject extends StrongTagged {
map: Map; const map: Map;
} }
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