Skip to content
File

Blob: src/workerd/server/v8-platform-impl.h

cpp83 lines
1// Copyright (c) 2017-2022 Cloudflare, Inc.
2// Licensed under the Apache 2.0 license found in the LICENSE file or at:
3// https://opensource.org/licenses/Apache-2.0
4 
5#pragma once
6 
7#include <v8-platform.h>
8 
9#include <kj/common.h>
10 
11namespace workerd::server {
12 
13// Workerd-specific implementation of v8::Platform.
14//
15// We customize the CurrentClockTimeMillis() virtual method in order to control the value
16// returned by `Date.now()`.
17//
18// Everything else gets passed through to the wrapped v8::Platform implementation (presumably
19// from `jsg::defaultPlatform()`).
20class WorkerdPlatform final: public v8::Platform {
21 public:
22 // This takes a reference to its wrapped platform because otherwise we would have to destroy a
23 // kj::Own in our noexcept destructor (feasible but ugly).
24 explicit WorkerdPlatform(v8::Platform& inner): inner(inner) {}
25 
26 ~WorkerdPlatform() noexcept {}
27 
28 // =====================================================================================
29 // v8::Platform API
30 
31 v8::PageAllocator* GetPageAllocator() noexcept override {
32 return inner.GetPageAllocator();
33 }
34 
35 int NumberOfWorkerThreads() noexcept override {
36 return inner.NumberOfWorkerThreads();
37 }
38 
39 std::shared_ptr<v8::TaskRunner> GetForegroundTaskRunner(
40 v8::Isolate* isolate, v8::TaskPriority priority) noexcept override {
41 return inner.GetForegroundTaskRunner(isolate, priority);
42 }
43 
44 void PostTaskOnWorkerThreadImpl(v8::TaskPriority priority,
45 std::unique_ptr<v8::Task> task,
46 const v8::SourceLocation& location) override {
47 inner.PostTaskOnWorkerThreadImpl(priority, kj::mv(task), location);
48 }
49 
50 void PostDelayedTaskOnWorkerThreadImpl(v8::TaskPriority priority,
51 std::unique_ptr<v8::Task> task,
52 double delay_in_seconds,
53 const v8::SourceLocation& location) override {
54 inner.PostDelayedTaskOnWorkerThreadImpl(priority, kj::mv(task), delay_in_seconds, location);
55 }
56 
57 bool IdleTasksEnabled(v8::Isolate* isolate) noexcept override {
58 return inner.IdleTasksEnabled(isolate);
59 }
60 
61 std::unique_ptr<v8::JobHandle> CreateJobImpl(v8::TaskPriority priority,
62 std::unique_ptr<v8::JobTask> job_task,
63 const v8::SourceLocation& location) override {
64 return inner.CreateJobImpl(priority, kj::mv(job_task), location);
65 }
66 
67 double MonotonicallyIncreasingTime() noexcept override {
68 return inner.MonotonicallyIncreasingTime();
69 }
70 
71 // Overridden to return KJ time
72 double CurrentClockTimeMillis() noexcept override;
73 
74 v8::TracingController* GetTracingController() noexcept override {
75 return inner.GetTracingController();
76 }
77 
78 private:
79 v8::Platform& inner;
80};
81 
82} // namespace workerd::server