@@ -191,76 +191,65 @@ class MockArrayBufferAllocator : public ArrayBufferAllocatorBase {
191
191
}
192
192
};
193
193
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 .
197
197
class 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);
259
242
};
260
243
261
244
262
245
v8::Platform* g_platform = NULL ;
263
246
247
+ v8::Platform* GetDefaultPlatform () {
248
+ return i::FLAG_verify_predictable
249
+ ? static_cast <PredictablePlatform*>(g_platform)->platform ()
250
+ : g_platform;
251
+ }
252
+
264
253
static Local<Value> Throw (Isolate* isolate, const char * message) {
265
254
return isolate->ThrowException (
266
255
String::NewFromUtf8 (isolate, message, NewStringType::kNormal )
@@ -1389,8 +1378,6 @@ void Shell::Quit(const v8::FunctionCallbackInfo<v8::Value>& args) {
1389
1378
const_cast <v8::FunctionCallbackInfo<v8::Value>*>(&args));
1390
1379
}
1391
1380
1392
- // Note that both WaitUntilDone and NotifyDone are no-op when
1393
- // --verify-predictable. See comment in Shell::EnsureEventLoopInitialized.
1394
1381
void Shell::WaitUntilDone (const v8::FunctionCallbackInfo<v8::Value>& args) {
1395
1382
SetWaitUntilDone (args.GetIsolate (), true );
1396
1383
}
@@ -2767,13 +2754,8 @@ void Shell::CollectGarbage(Isolate* isolate) {
2767
2754
}
2768
2755
2769
2756
void 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 );
2777
2759
}
2778
2760
2779
2761
void Shell::SetWaitUntilDone (Isolate* isolate, bool value) {
@@ -2792,29 +2774,32 @@ bool Shell::IsWaitUntilDone(Isolate* isolate) {
2792
2774
}
2793
2775
2794
2776
void Shell::CompleteMessageLoop (Isolate* isolate) {
2795
- // See comment in EnsureEventLoopInitialized.
2796
- if (i::FLAG_verify_predictable) return ;
2777
+ Platform* platform = GetDefaultPlatform ();
2797
2778
while (v8::platform::PumpMessageLoop (
2798
- g_platform , isolate,
2779
+ platform , isolate,
2799
2780
Shell::IsWaitUntilDone (isolate)
2800
2781
? platform::MessageLoopBehavior::kWaitForWork
2801
2782
: platform::MessageLoopBehavior::kDoNotWait )) {
2802
2783
isolate->RunMicrotasks ();
2803
2784
}
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
+ }
2806
2789
}
2807
2790
2808
2791
void Shell::EmptyMessageQueues (Isolate* isolate) {
2809
- if (i::FLAG_verify_predictable) return ;
2792
+ Platform* platform = GetDefaultPlatform () ;
2810
2793
// Pump the message loop until it is empty.
2811
2794
while (v8::platform::PumpMessageLoop (
2812
- g_platform , isolate, platform::MessageLoopBehavior::kDoNotWait )) {
2795
+ platform , isolate, platform::MessageLoopBehavior::kDoNotWait )) {
2813
2796
isolate->RunMicrotasks ();
2814
2797
}
2815
2798
// 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
+ }
2818
2803
}
2819
2804
2820
2805
class Serializer : public ValueSerializer ::Delegate {
@@ -3067,29 +3052,29 @@ int Shell::Main(int argc, char* argv[]) {
3067
3052
if (!SetOptions (argc, argv)) return 1 ;
3068
3053
v8::V8::InitializeICUDefaultLocation (argv[0 ], options.icu_data_file );
3069
3054
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
+
3070
3066
platform::tracing::TracingController* tracing_controller = nullptr ;
3071
- if (options.trace_enabled ) {
3067
+ if (options.trace_enabled && !i::FLAG_verify_predictable ) {
3072
3068
trace_file.open (" v8_trace.json" );
3073
3069
tracing_controller = new platform::tracing::TracingController ();
3074
3070
platform::tracing::TraceBuffer* trace_buffer =
3075
3071
platform::tracing::TraceBuffer::CreateTraceBufferRingBuffer (
3076
3072
platform::tracing::TraceBuffer::kRingBufferChunks ,
3077
3073
platform::tracing::TraceWriter::CreateJSONTraceWriter (trace_file));
3078
3074
tracing_controller->Initialize (trace_buffer);
3075
+ platform::SetTracingController (g_platform, tracing_controller);
3079
3076
}
3080
3077
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
-
3093
3078
v8::V8::InitializePlatform (g_platform);
3094
3079
v8::V8::Initialize ();
3095
3080
if (options.natives_blob || options.snapshot_blob ) {
@@ -3136,6 +3121,7 @@ int Shell::Main(int argc, char* argv[]) {
3136
3121
}
3137
3122
3138
3123
Isolate* isolate = Isolate::New (create_params);
3124
+
3139
3125
D8Console console (isolate);
3140
3126
{
3141
3127
Isolate::Scope scope (isolate);
@@ -3205,9 +3191,6 @@ int Shell::Main(int argc, char* argv[]) {
3205
3191
V8::Dispose ();
3206
3192
V8::ShutdownPlatform ();
3207
3193
delete g_platform;
3208
- if (i::FLAG_verify_predictable) {
3209
- delete tracing_controller;
3210
- }
3211
3194
3212
3195
return result;
3213
3196
}
0 commit comments