Commit 14654f84 authored by tzik's avatar tzik Committed by Commit Bot

Ensure Isolate::GetEnteredOrMicrotaskContext returns a NativeContext

Unlike other v8::Context getter on v8::Isolate, the v8::Context returned
by GetEnteredOrMicrotaskContext may be non-NativeContext. However,
Blink implicitly assumes all v8::Context is backed by NativeContexts.

This CL updates GetEnteredOrMicrotaskContext() to check the resulting
Context is NativeContext, and updates callers of EnterMicrotaskContext
to use NativeContext.

Change-Id: Ifae528f4ce8feb067f4ad1a43330dc55f4a8ed79
Reviewed-on: https://chromium-review.googlesource.com/c/1301653Reviewed-by: 's avatarAdam Klein <adamk@chromium.org>
Commit-Queue: Taiju Tsuiki <tzik@chromium.org>
Cr-Commit-Position: refs/heads/master@{#57259}
parent 65464058
......@@ -8011,6 +8011,7 @@ v8::Local<v8::Context> Isolate::GetEnteredOrMicrotaskContext() {
last = isolate->handle_scope_implementer()->LastEnteredContext();
}
if (last.is_null()) return Local<Context>();
DCHECK(last->IsNativeContext());
return Utils::ToLocal(i::Handle<i::Context>::cast(last));
}
......
......@@ -761,13 +761,15 @@ void InternalBuiltinsAssembler::SetCurrentContext(TNode<Context> context) {
}
void InternalBuiltinsAssembler::EnterMicrotaskContext(
TNode<Context> microtask_context) {
TNode<Context> native_context) {
CSA_ASSERT(this, IsNativeContext(native_context));
auto ref = ExternalReference::handle_scope_implementer_address(isolate());
Node* const hsi = Load(MachineType::Pointer(), ExternalConstant(ref));
StoreNoWriteBarrier(
MachineType::PointerRepresentation(), hsi,
IntPtrConstant(HandleScopeImplementerOffsets::kMicrotaskContext),
BitcastTaggedToWord(microtask_context));
BitcastTaggedToWord(native_context));
// Load mirrored std::vector length from
// HandleScopeImplementer::entered_contexts_count_
......@@ -971,7 +973,7 @@ TF_BUILTIN(RunMicrotasks, InternalBuiltinsAssembler) {
TNode<Context> native_context = LoadNativeContext(microtask_context);
CSA_ASSERT(this, IsNativeContext(native_context));
EnterMicrotaskContext(microtask_context);
EnterMicrotaskContext(native_context);
SetCurrentContext(native_context);
TNode<JSReceiver> callable = LoadObjectField<JSReceiver>(
......@@ -1016,7 +1018,7 @@ TF_BUILTIN(RunMicrotasks, InternalBuiltinsAssembler) {
microtask, PromiseResolveThenableJobTask::kContextOffset);
TNode<Context> native_context = LoadNativeContext(microtask_context);
CSA_ASSERT(this, IsNativeContext(native_context));
EnterMicrotaskContext(microtask_context);
EnterMicrotaskContext(native_context);
SetCurrentContext(native_context);
Node* const promise_to_resolve = LoadObjectField(
......@@ -1042,7 +1044,7 @@ TF_BUILTIN(RunMicrotasks, InternalBuiltinsAssembler) {
microtask, PromiseReactionJobTask::kContextOffset);
TNode<Context> native_context = LoadNativeContext(microtask_context);
CSA_ASSERT(this, IsNativeContext(native_context));
EnterMicrotaskContext(microtask_context);
EnterMicrotaskContext(native_context);
SetCurrentContext(native_context);
Node* const argument =
......@@ -1077,7 +1079,7 @@ TF_BUILTIN(RunMicrotasks, InternalBuiltinsAssembler) {
microtask, PromiseReactionJobTask::kContextOffset);
TNode<Context> native_context = LoadNativeContext(microtask_context);
CSA_ASSERT(this, IsNativeContext(native_context));
EnterMicrotaskContext(microtask_context);
EnterMicrotaskContext(native_context);
SetCurrentContext(native_context);
Node* const argument =
......
......@@ -28929,3 +28929,34 @@ TEST(TestGetEmbeddedCodeRange) {
CHECK_EQ(0, builtins_range.length_in_bytes);
}
}
TEST(MicrotaskContextShouldBeNativeContext) {
LocalContext env;
v8::Isolate* isolate = env->GetIsolate();
v8::HandleScope scope(isolate);
auto callback = [](const v8::FunctionCallbackInfo<v8::Value>& info) {
v8::Isolate* isolate = info.GetIsolate();
v8::HandleScope scope(isolate);
i::Handle<i::Context> context =
v8::Utils::OpenHandle(*isolate->GetEnteredOrMicrotaskContext());
CHECK(context->IsNativeContext());
info.GetReturnValue().SetUndefined();
};
Local<v8::FunctionTemplate> desc = v8::FunctionTemplate::New(isolate);
desc->InstanceTemplate()->SetCallAsFunctionHandler(callback);
Local<v8::Object> obj = desc->GetFunction(env.local())
.ToLocalChecked()
->NewInstance(env.local())
.ToLocalChecked();
CHECK(env->Global()->Set(env.local(), v8_str("callback"), obj).FromJust());
CompileRun(
"with({}){(async ()=>{"
" await 42;"
"})().then(callback);}");
isolate->RunMicrotasks();
}
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