-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TaskAttribution: Refactor V8 interface (yield API 2/n)
Refactor the interface between V8 and task attribution to support inheritance in the yield() API. In a future dependent CL, the task state object will include self.scheduler state and be set for postTask() tasks. In this CL: - The wrapper class is renamed ScriptWrappableTaskState and is composed of a task attribution ID rather than being one - Attribution scopes store the previous object rather than ID. Note: there's a slight perf improvement here since we don't need to create a new object when restoring the previous state. - The integration with V8 continuation preserved embedder data is refactored into ScriptWrappableTaskState static methods, which will also be used by DOMScheduler and related classes. Rather than making the adapter virtual for testing, the relevant methods are virtual, with the default delegating to the new static methods. Bug: 979020 Change-Id: I9e9bf2e239e79ac0fd7c416667b44143c34c15a9 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4117451 Commit-Queue: Scott Haseley <shaseley@chromium.org> Reviewed-by: Yoav Weiss <yoavweiss@chromium.org> Cr-Commit-Position: refs/heads/main@{#1105848}
- Loading branch information
Showing
11 changed files
with
201 additions
and
164 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 0 additions & 27 deletions
27
third_party/blink/renderer/modules/scheduler/script_wrappable_task_attribution_id.h
This file was deleted.
Oops, something went wrong.
10 changes: 0 additions & 10 deletions
10
third_party/blink/renderer/modules/scheduler/script_wrappable_task_attribution_id.idl
This file was deleted.
Oops, something went wrong.
77 changes: 77 additions & 0 deletions
77
third_party/blink/renderer/modules/scheduler/script_wrappable_task_state.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Copyright 2023 The Chromium Authors | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "third_party/blink/renderer/modules/scheduler/script_wrappable_task_state.h" | ||
|
||
#include "third_party/blink/renderer/bindings/core/v8/native_value_traits_impl.h" | ||
#include "third_party/blink/renderer/bindings/core/v8/to_v8_traits.h" | ||
#include "third_party/blink/renderer/modules/scheduler/script_wrappable_task_state.h" | ||
#include "third_party/blink/renderer/platform/bindings/exception_state.h" | ||
#include "third_party/blink/renderer/platform/bindings/script_forbidden_scope.h" | ||
#include "third_party/blink/renderer/platform/bindings/script_state.h" | ||
|
||
namespace blink { | ||
|
||
ScriptWrappableTaskState::ScriptWrappableTaskState( | ||
scheduler::TaskAttributionId id) | ||
: task_attribution_id_(id) {} | ||
|
||
// static | ||
ScriptWrappableTaskState* ScriptWrappableTaskState::GetCurrent( | ||
ScriptState* script_state) { | ||
DCHECK(script_state); | ||
if (!script_state->ContextIsValid()) { | ||
return nullptr; | ||
} | ||
|
||
ScriptState::Scope scope(script_state); | ||
v8::Local<v8::Context> context = script_state->GetContext(); | ||
DCHECK(!context.IsEmpty()); | ||
v8::Local<v8::Value> v8_value = | ||
context->GetContinuationPreservedEmbedderData(); | ||
if (v8_value->IsNullOrUndefined()) { | ||
return nullptr; | ||
} | ||
v8::Isolate* isolate = script_state->GetIsolate(); | ||
DCHECK(isolate); | ||
if (isolate->IsExecutionTerminating()) { | ||
return nullptr; | ||
} | ||
// If not empty, the value must be a `ScriptWrappableTaskState`. | ||
NonThrowableExceptionState exception_state; | ||
ScriptWrappableTaskState* task_state = | ||
NativeValueTraits<ScriptWrappableTaskState>::NativeValue( | ||
isolate, v8_value, exception_state); | ||
DCHECK(task_state); | ||
return task_state; | ||
} | ||
|
||
// static | ||
void ScriptWrappableTaskState::SetCurrent( | ||
ScriptState* script_state, | ||
ScriptWrappableTaskState* task_state) { | ||
DCHECK(script_state); | ||
if (!script_state->ContextIsValid()) { | ||
return; | ||
} | ||
CHECK(!ScriptForbiddenScope::IsScriptForbidden()); | ||
ScriptState::Scope scope(script_state); | ||
v8::Isolate* isolate = script_state->GetIsolate(); | ||
DCHECK(isolate); | ||
if (isolate->IsExecutionTerminating()) { | ||
return; | ||
} | ||
v8::Local<v8::Context> context = script_state->GetContext(); | ||
DCHECK(!context.IsEmpty()); | ||
|
||
if (task_state) { | ||
context->SetContinuationPreservedEmbedderData( | ||
ToV8Traits<ScriptWrappableTaskState>::ToV8(script_state, task_state) | ||
.ToLocalChecked()); | ||
} else { | ||
context->SetContinuationPreservedEmbedderData(v8::Local<v8::Value>()); | ||
} | ||
} | ||
|
||
} // namespace blink |
43 changes: 43 additions & 0 deletions
43
third_party/blink/renderer/modules/scheduler/script_wrappable_task_state.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright 2023 The Chromium Authors | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_SCHEDULER_SCRIPT_WRAPPABLE_TASK_STATE_H_ | ||
#define THIRD_PARTY_BLINK_RENDERER_MODULES_SCHEDULER_SCRIPT_WRAPPABLE_TASK_STATE_H_ | ||
|
||
#include "third_party/blink/public/common/scheduler/task_attribution_id.h" | ||
#include "third_party/blink/renderer/modules/modules_export.h" | ||
#include "third_party/blink/renderer/platform/bindings/script_state.h" | ||
#include "third_party/blink/renderer/platform/bindings/script_wrappable.h" | ||
#include "third_party/blink/renderer/platform/heap/member.h" | ||
|
||
namespace blink { | ||
|
||
// The scheduler uses `ScriptWrappableTaskState` objects to store continuation | ||
// preserved embedder data, which is data stored on V8 promise reactions at | ||
// creation time and restored at run time. | ||
class MODULES_EXPORT ScriptWrappableTaskState final : public ScriptWrappable { | ||
DEFINE_WRAPPERTYPEINFO(); | ||
|
||
public: | ||
// Get the `ScriptWrappableTaskState` currently stored as continuation | ||
// preserved embedder data. | ||
static ScriptWrappableTaskState* GetCurrent(ScriptState*); | ||
|
||
// Set the given `ScriptWrappableTaskState` as the current continuation | ||
// preserved embedder data. | ||
static void SetCurrent(ScriptState*, ScriptWrappableTaskState*); | ||
|
||
explicit ScriptWrappableTaskState(scheduler::TaskAttributionId id); | ||
|
||
scheduler::TaskAttributionId GetTaskAttributionId() const { | ||
return task_attribution_id_; | ||
} | ||
|
||
private: | ||
const scheduler::TaskAttributionId task_attribution_id_; | ||
}; | ||
|
||
} // namespace blink | ||
|
||
#endif // THIRD_PARTY_BLINK_RENDERER_MODULES_SCHEDULER_SCRIPT_WRAPPABLE_TASK_STATE_H_ |
8 changes: 8 additions & 0 deletions
8
third_party/blink/renderer/modules/scheduler/script_wrappable_task_state.idl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// Copyright 2023 The Chromium Authors | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
// This interface is not web-exposed. It is used to generate a ScriptWrappable | ||
// object used to store task state as continuation embedder data. | ||
interface ScriptWrappableTaskState { | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.