@@ -192,72 +192,61 @@ class MockArrayBufferAllocator : public ArrayBufferAllocatorBase {
192192 }
193193};
194194
195-
196- // Predictable v8::Platform implementation. All background and foreground
197- // tasks are run immediately, delayed tasks are not executed at all .
195+ // Predictable v8::Platform implementation. Background tasks and idle tasks are
196+ // disallowed, and the time reported by {MonotonicallyIncreasingTime} is
197+ // deterministic .
198198class PredictablePlatform : public Platform {
199199 public:
200- PredictablePlatform () {}
200+ explicit PredictablePlatform (std::unique_ptr<Platform> platform)
201+ : platform_(std::move(platform)) {
202+ DCHECK_NOT_NULL (platform_);
203+ }
201204
202205 void CallOnBackgroundThread (Task* task,
203206 ExpectedRuntime expected_runtime) override {
207+ // It's not defined when background tasks are being executed, so we can just
208+ // execute them right away.
204209 task->Run ();
205210 delete task;
206211 }
207212
208213 void CallOnForegroundThread (v8::Isolate* isolate, Task* task) override {
209- task->Run ();
210- delete task;
214+ platform_->CallOnForegroundThread (isolate, task);
211215 }
212216
213217 void CallDelayedOnForegroundThread (v8::Isolate* isolate, Task* task,
214218 double delay_in_seconds) override {
215- delete task;
219+ platform_-> CallDelayedOnForegroundThread (isolate, task, delay_in_seconds) ;
216220 }
217221
218- void CallIdleOnForegroundThread (v8::Isolate* isolate,
219- IdleTask* task) override {
222+ void CallIdleOnForegroundThread (Isolate* isolate, IdleTask* task) override {
220223 UNREACHABLE ();
221224 }
222225
223- bool IdleTasksEnabled (v8:: Isolate* isolate) override { return false ; }
226+ bool IdleTasksEnabled (Isolate* isolate) override { return false ; }
224227
225228 double MonotonicallyIncreasingTime () override {
226229 return synthetic_time_in_sec_ += 0.00001 ;
227230 }
228231
229- using Platform::AddTraceEvent;
230- uint64_t AddTraceEvent (char phase, const uint8_t * categoryEnabledFlag,
231- const char * name, const char * scope, uint64_t id,
232- uint64_t bind_id, int numArgs, const char ** argNames,
233- const uint8_t * argTypes, const uint64_t * argValues,
234- unsigned int flags) override {
235- return 0 ;
236- }
237-
238- void UpdateTraceEventDuration (const uint8_t * categoryEnabledFlag,
239- const char * name, uint64_t handle) override {}
240-
241- const uint8_t * GetCategoryGroupEnabled (const char * name) override {
242- static uint8_t no = 0 ;
243- return &no;
244- }
245-
246- const char * GetCategoryGroupName (
247- const uint8_t * categoryEnabledFlag) override {
248- static const char * dummy = " dummy" ;
249- return dummy;
250- }
232+ Platform* platform () const { return platform_.get (); }
251233
252234 private:
253235 double synthetic_time_in_sec_ = 0.0 ;
236+ std::unique_ptr<Platform> platform_;
254237
255238 DISALLOW_COPY_AND_ASSIGN (PredictablePlatform);
256239};
257240
258241
259242v8::Platform* g_platform = NULL ;
260243
244+ v8::Platform* GetDefaultPlatform () {
245+ return i::FLAG_verify_predictable
246+ ? static_cast <PredictablePlatform*>(g_platform)->platform ()
247+ : g_platform;
248+ }
249+
261250static Local<Value> Throw (Isolate* isolate, const char * message) {
262251 return isolate->ThrowException (
263252 String::NewFromUtf8 (isolate, message, NewStringType::kNormal )
@@ -1386,8 +1375,6 @@ void Shell::Quit(const v8::FunctionCallbackInfo<v8::Value>& args) {
13861375 const_cast <v8::FunctionCallbackInfo<v8::Value>*>(&args));
13871376}
13881377
1389- // Note that both WaitUntilDone and NotifyDone are no-op when
1390- // --verify-predictable. See comment in Shell::EnsureEventLoopInitialized.
13911378void Shell::WaitUntilDone (const v8::FunctionCallbackInfo<v8::Value>& args) {
13921379 SetWaitUntilDone (args.GetIsolate (), true );
13931380}
@@ -2764,13 +2751,8 @@ void Shell::CollectGarbage(Isolate* isolate) {
27642751}
27652752
27662753void Shell::EnsureEventLoopInitialized (Isolate* isolate) {
2767- // When using PredictablePlatform (i.e. FLAG_verify_predictable),
2768- // we don't need event loop support, because tasks are completed
2769- // immediately - both background and foreground ones.
2770- if (!i::FLAG_verify_predictable) {
2771- v8::platform::EnsureEventLoopInitialized (g_platform, isolate);
2772- SetWaitUntilDone (isolate, false );
2773- }
2754+ v8::platform::EnsureEventLoopInitialized (GetDefaultPlatform (), isolate);
2755+ SetWaitUntilDone (isolate, false );
27742756}
27752757
27762758void Shell::SetWaitUntilDone (Isolate* isolate, bool value) {
@@ -2789,29 +2771,32 @@ bool Shell::IsWaitUntilDone(Isolate* isolate) {
27892771}
27902772
27912773void Shell::CompleteMessageLoop (Isolate* isolate) {
2792- // See comment in EnsureEventLoopInitialized.
2793- if (i::FLAG_verify_predictable) return ;
2774+ Platform* platform = GetDefaultPlatform ();
27942775 while (v8::platform::PumpMessageLoop (
2795- g_platform , isolate,
2776+ platform , isolate,
27962777 Shell::IsWaitUntilDone (isolate)
27972778 ? platform::MessageLoopBehavior::kWaitForWork
27982779 : platform::MessageLoopBehavior::kDoNotWait )) {
27992780 isolate->RunMicrotasks ();
28002781 }
2801- v8::platform::RunIdleTasks (g_platform, isolate,
2802- 50.0 / base::Time::kMillisecondsPerSecond );
2782+ if (platform->IdleTasksEnabled (isolate)) {
2783+ v8::platform::RunIdleTasks (platform, isolate,
2784+ 50.0 / base::Time::kMillisecondsPerSecond );
2785+ }
28032786}
28042787
28052788void Shell::EmptyMessageQueues (Isolate* isolate) {
2806- if (i::FLAG_verify_predictable) return ;
2789+ Platform* platform = GetDefaultPlatform () ;
28072790 // Pump the message loop until it is empty.
28082791 while (v8::platform::PumpMessageLoop (
2809- g_platform , isolate, platform::MessageLoopBehavior::kDoNotWait )) {
2792+ platform , isolate, platform::MessageLoopBehavior::kDoNotWait )) {
28102793 isolate->RunMicrotasks ();
28112794 }
28122795 // Run the idle tasks.
2813- v8::platform::RunIdleTasks (g_platform, isolate,
2814- 50.0 / base::Time::kMillisecondsPerSecond );
2796+ if (platform->IdleTasksEnabled (isolate)) {
2797+ v8::platform::RunIdleTasks (platform, isolate,
2798+ 50.0 / base::Time::kMillisecondsPerSecond );
2799+ }
28152800}
28162801
28172802class Serializer : public ValueSerializer ::Delegate {
@@ -3069,24 +3054,22 @@ int Shell::Main(int argc, char* argv[]) {
30693054 ? v8::platform::InProcessStackDumping::kDisabled
30703055 : v8::platform::InProcessStackDumping::kEnabled ;
30713056
3072- g_platform = i::FLAG_verify_predictable
3073- ? new PredictablePlatform ()
3074- : v8::platform::CreateDefaultPlatform (
3075- 0 , v8::platform::IdleTaskSupport:: kEnabled ,
3076- in_process_stack_dumping);
3057+ g_platform = v8::platform::CreateDefaultPlatform (
3058+ 0 , v8::platform::IdleTaskSupport:: kEnabled , in_process_stack_dumping);
3059+ if (i::FLAG_verify_predictable) {
3060+ g_platform = new PredictablePlatform (std::unique_ptr<Platform>(g_platform));
3061+ }
30773062
30783063 platform::tracing::TracingController* tracing_controller;
3079- if (options.trace_enabled ) {
3064+ if (options.trace_enabled && !i::FLAG_verify_predictable ) {
30803065 trace_file.open (" v8_trace.json" );
30813066 tracing_controller = new platform::tracing::TracingController ();
30823067 platform::tracing::TraceBuffer* trace_buffer =
30833068 platform::tracing::TraceBuffer::CreateTraceBufferRingBuffer (
30843069 platform::tracing::TraceBuffer::kRingBufferChunks ,
30853070 platform::tracing::TraceWriter::CreateJSONTraceWriter (trace_file));
30863071 tracing_controller->Initialize (trace_buffer);
3087- if (!i::FLAG_verify_predictable) {
3088- platform::SetTracingController (g_platform, tracing_controller);
3089- }
3072+ platform::SetTracingController (g_platform, tracing_controller);
30903073 }
30913074
30923075 v8::V8::InitializePlatform (g_platform);
@@ -3135,6 +3118,7 @@ int Shell::Main(int argc, char* argv[]) {
31353118 }
31363119
31373120 Isolate* isolate = Isolate::New (create_params);
3121+
31383122 D8Console console (isolate);
31393123 {
31403124 Isolate::Scope scope (isolate);
@@ -3204,9 +3188,6 @@ int Shell::Main(int argc, char* argv[]) {
32043188 V8::Dispose ();
32053189 V8::ShutdownPlatform ();
32063190 delete g_platform;
3207- if (i::FLAG_verify_predictable) {
3208- delete tracing_controller;
3209- }
32103191
32113192 return result;
32123193}
0 commit comments