@@ -191,76 +191,65 @@ class MockArrayBufferAllocator : public ArrayBufferAllocatorBase {
191191 }
192192};
193193
194-
195- // Predictable v8::Platform implementation. All background and foreground
196- // tasks are run immediately, delayed tasks are not executed at all .
194+ // Predictable v8::Platform implementation. Background tasks and idle tasks are
195+ // disallowed, and the time reported by {MonotonicallyIncreasingTime} is
196+ // deterministic .
197197class PredictablePlatform : public Platform {
198- public:
199- PredictablePlatform () {}
200-
201- void CallOnBackgroundThread (Task* task,
202- ExpectedRuntime expected_runtime) override {
203- task->Run ();
204- delete task;
205- }
206-
207- void CallOnForegroundThread (v8::Isolate* isolate, Task* task) override {
208- task->Run ();
209- delete task;
210- }
211-
212- void CallDelayedOnForegroundThread (v8::Isolate* isolate, Task* task,
213- double delay_in_seconds) override {
214- delete task;
215- }
216-
217- void CallIdleOnForegroundThread (v8::Isolate* isolate,
218- IdleTask* task) override {
219- UNREACHABLE ();
220- }
221-
222- bool IdleTasksEnabled (v8::Isolate* isolate) override { return false ; }
223-
224- double MonotonicallyIncreasingTime () override {
225- return synthetic_time_in_sec_ += 0.00001 ;
226- }
227-
228- v8::TracingController* GetTracingController () override {
229- return platform_->GetTracingController ();
230- }
231-
232- using Platform::AddTraceEvent;
233- uint64_t AddTraceEvent (char phase, const uint8_t * categoryEnabledFlag,
234- const char * name, const char * scope, uint64_t id,
235- uint64_t bind_id, int numArgs, const char ** argNames,
236- const uint8_t * argTypes, const uint64_t * argValues,
237- unsigned int flags) override {
238- return 0 ;
239- }
240-
241- void UpdateTraceEventDuration (const uint8_t * categoryEnabledFlag,
242- const char * name, uint64_t handle) override {}
243-
244- const uint8_t * GetCategoryGroupEnabled (const char * name) override {
245- static uint8_t no = 0 ;
246- return &no;
247- }
248-
249- const char * GetCategoryGroupName (
250- const uint8_t * categoryEnabledFlag) override {
251- static const char * dummy = " dummy" ;
252- return dummy;
253- }
254-
255- private:
256- double synthetic_time_in_sec_ = 0.0 ;
257-
258- DISALLOW_COPY_AND_ASSIGN (PredictablePlatform);
198+ public:
199+ explicit PredictablePlatform (std::unique_ptr<Platform> platform)
200+ : platform_(std::move(platform)) {
201+ DCHECK_NOT_NULL (platform_);
202+ }
203+
204+ void CallOnBackgroundThread (Task* task,
205+ ExpectedRuntime expected_runtime) override {
206+ // It's not defined when background tasks are being executed, so we can just
207+ // execute them right away.
208+ task->Run ();
209+ delete task;
210+ }
211+
212+ void CallOnForegroundThread (v8::Isolate* isolate, Task* task) override {
213+ platform_->CallOnForegroundThread (isolate, task);
214+ }
215+
216+ void CallDelayedOnForegroundThread (v8::Isolate* isolate, Task* task,
217+ double delay_in_seconds) override {
218+ platform_->CallDelayedOnForegroundThread (isolate, task, delay_in_seconds);
219+ }
220+
221+ void CallIdleOnForegroundThread (Isolate* isolate, IdleTask* task) override {
222+ UNREACHABLE ();
223+ }
224+
225+ bool IdleTasksEnabled (Isolate* isolate) override { return false ; }
226+
227+ double MonotonicallyIncreasingTime () override {
228+ return synthetic_time_in_sec_ += 0.00001 ;
229+ }
230+
231+ v8::TracingController* GetTracingController () override {
232+ return platform_->GetTracingController ();
233+ }
234+
235+ Platform* platform () const { return platform_.get (); }
236+
237+ private:
238+ double synthetic_time_in_sec_ = 0.0 ;
239+ std::unique_ptr<Platform> platform_;
240+
241+ DISALLOW_COPY_AND_ASSIGN (PredictablePlatform);
259242};
260243
261244
262245v8::Platform* g_platform = NULL ;
263246
247+ v8::Platform* GetDefaultPlatform () {
248+ return i::FLAG_verify_predictable
249+ ? static_cast <PredictablePlatform*>(g_platform)->platform ()
250+ : g_platform;
251+ }
252+
264253static Local<Value> Throw (Isolate* isolate, const char * message) {
265254 return isolate->ThrowException (
266255 String::NewFromUtf8 (isolate, message, NewStringType::kNormal )
@@ -1389,8 +1378,6 @@ void Shell::Quit(const v8::FunctionCallbackInfo<v8::Value>& args) {
13891378 const_cast <v8::FunctionCallbackInfo<v8::Value>*>(&args));
13901379}
13911380
1392- // Note that both WaitUntilDone and NotifyDone are no-op when
1393- // --verify-predictable. See comment in Shell::EnsureEventLoopInitialized.
13941381void Shell::WaitUntilDone (const v8::FunctionCallbackInfo<v8::Value>& args) {
13951382 SetWaitUntilDone (args.GetIsolate (), true );
13961383}
@@ -2767,13 +2754,8 @@ void Shell::CollectGarbage(Isolate* isolate) {
27672754}
27682755
27692756void Shell::EnsureEventLoopInitialized (Isolate* isolate) {
2770- // When using PredictablePlatform (i.e. FLAG_verify_predictable),
2771- // we don't need event loop support, because tasks are completed
2772- // immediately - both background and foreground ones.
2773- if (!i::FLAG_verify_predictable) {
2774- v8::platform::EnsureEventLoopInitialized (g_platform, isolate);
2775- SetWaitUntilDone (isolate, false );
2776- }
2757+ v8::platform::EnsureEventLoopInitialized (GetDefaultPlatform (), isolate);
2758+ SetWaitUntilDone (isolate, false );
27772759}
27782760
27792761void Shell::SetWaitUntilDone (Isolate* isolate, bool value) {
@@ -2792,29 +2774,32 @@ bool Shell::IsWaitUntilDone(Isolate* isolate) {
27922774}
27932775
27942776void Shell::CompleteMessageLoop (Isolate* isolate) {
2795- // See comment in EnsureEventLoopInitialized.
2796- if (i::FLAG_verify_predictable) return ;
2777+ Platform* platform = GetDefaultPlatform ();
27972778 while (v8::platform::PumpMessageLoop (
2798- g_platform , isolate,
2779+ platform , isolate,
27992780 Shell::IsWaitUntilDone (isolate)
28002781 ? platform::MessageLoopBehavior::kWaitForWork
28012782 : platform::MessageLoopBehavior::kDoNotWait )) {
28022783 isolate->RunMicrotasks ();
28032784 }
2804- v8::platform::RunIdleTasks (g_platform, isolate,
2805- 50.0 / base::Time::kMillisecondsPerSecond );
2785+ if (platform->IdleTasksEnabled (isolate)) {
2786+ v8::platform::RunIdleTasks (platform, isolate,
2787+ 50.0 / base::Time::kMillisecondsPerSecond );
2788+ }
28062789}
28072790
28082791void Shell::EmptyMessageQueues (Isolate* isolate) {
2809- if (i::FLAG_verify_predictable) return ;
2792+ Platform* platform = GetDefaultPlatform () ;
28102793 // Pump the message loop until it is empty.
28112794 while (v8::platform::PumpMessageLoop (
2812- g_platform , isolate, platform::MessageLoopBehavior::kDoNotWait )) {
2795+ platform , isolate, platform::MessageLoopBehavior::kDoNotWait )) {
28132796 isolate->RunMicrotasks ();
28142797 }
28152798 // Run the idle tasks.
2816- v8::platform::RunIdleTasks (g_platform, isolate,
2817- 50.0 / base::Time::kMillisecondsPerSecond );
2799+ if (platform->IdleTasksEnabled (isolate)) {
2800+ v8::platform::RunIdleTasks (platform, isolate,
2801+ 50.0 / base::Time::kMillisecondsPerSecond );
2802+ }
28182803}
28192804
28202805class Serializer : public ValueSerializer ::Delegate {
@@ -3067,29 +3052,29 @@ int Shell::Main(int argc, char* argv[]) {
30673052 if (!SetOptions (argc, argv)) return 1 ;
30683053 v8::V8::InitializeICUDefaultLocation (argv[0 ], options.icu_data_file );
30693054
3055+ v8::platform::InProcessStackDumping in_process_stack_dumping =
3056+ options.disable_in_process_stack_traces
3057+ ? v8::platform::InProcessStackDumping::kDisabled
3058+ : v8::platform::InProcessStackDumping::kEnabled ;
3059+
3060+ g_platform = v8::platform::CreateDefaultPlatform (
3061+ 0 , v8::platform::IdleTaskSupport::kEnabled , in_process_stack_dumping);
3062+ if (i::FLAG_verify_predictable) {
3063+ g_platform = new PredictablePlatform (std::unique_ptr<Platform>(g_platform));
3064+ }
3065+
30703066 platform::tracing::TracingController* tracing_controller = nullptr ;
3071- if (options.trace_enabled ) {
3067+ if (options.trace_enabled && !i::FLAG_verify_predictable ) {
30723068 trace_file.open (" v8_trace.json" );
30733069 tracing_controller = new platform::tracing::TracingController ();
30743070 platform::tracing::TraceBuffer* trace_buffer =
30753071 platform::tracing::TraceBuffer::CreateTraceBufferRingBuffer (
30763072 platform::tracing::TraceBuffer::kRingBufferChunks ,
30773073 platform::tracing::TraceWriter::CreateJSONTraceWriter (trace_file));
30783074 tracing_controller->Initialize (trace_buffer);
3075+ platform::SetTracingController (g_platform, tracing_controller);
30793076 }
30803077
3081- v8::platform::InProcessStackDumping in_process_stack_dumping =
3082- options.disable_in_process_stack_traces
3083- ? v8::platform::InProcessStackDumping::kDisabled
3084- : v8::platform::InProcessStackDumping::kEnabled ;
3085-
3086- g_platform = i::FLAG_verify_predictable
3087- ? new PredictablePlatform ()
3088- : v8::platform::CreateDefaultPlatform (
3089- 0 , v8::platform::IdleTaskSupport::kEnabled ,
3090- in_process_stack_dumping,
3091- tracing_controller);
3092-
30933078 v8::V8::InitializePlatform (g_platform);
30943079 v8::V8::Initialize ();
30953080 if (options.natives_blob || options.snapshot_blob ) {
@@ -3136,6 +3121,7 @@ int Shell::Main(int argc, char* argv[]) {
31363121 }
31373122
31383123 Isolate* isolate = Isolate::New (create_params);
3124+
31393125 D8Console console (isolate);
31403126 {
31413127 Isolate::Scope scope (isolate);
@@ -3205,9 +3191,6 @@ int Shell::Main(int argc, char* argv[]) {
32053191 V8::Dispose ();
32063192 V8::ShutdownPlatform ();
32073193 delete g_platform;
3208- if (i::FLAG_verify_predictable) {
3209- delete tracing_controller;
3210- }
32113194
32123195 return result;
32133196}
0 commit comments