1// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
5#ifndef RUNTIME_BIN_DARTDEV_ISOLATE_H_
6#define RUNTIME_BIN_DARTDEV_ISOLATE_H_
7
8#if !defined(DART_PRECOMPILED_RUNTIME)
9
10#include <memory>
11
12#include "bin/thread.h"
13#include "include/dart_api.h"
14#include "include/dart_native_api.h"
15#include "platform/globals.h"
16#include "platform/utils.h"
17
18namespace dart {
19namespace bin {
20
21class CommandLineOptions;
22
23class DartDevIsolate {
24 public:
25 // Note: keep in sync with pkg/dartdev/lib/vm_interop_handler.dart
26 typedef enum {
27 DartDev_Result_Unknown = -1,
28 DartDev_Result_Run = 1,
29 DartDev_Result_Exit = 2,
30 } DartDev_Result;
31
32 // Returns true if there does not exist a file at |script_uri| or the URI is
33 // not an HTTP resource.
34 static bool ShouldParseCommand(const char* script_uri);
35
36 static void set_should_run_dart_dev(bool enable) {
37 should_run_dart_dev_ = enable;
38 }
39
40 static bool should_run_dart_dev() { return should_run_dart_dev_; }
41
42 // Starts a DartDev instance in a new isolate and runs it to completion.
43 //
44 // Returns true if the VM should run the result in `script`, in which case
45 // `script` and `dart_options` will have been repopulated with the correct
46 // values.
47 static DartDev_Result RunDartDev(
48 Dart_IsolateGroupCreateCallback create_isolate,
49 const char* packages_file,
50 char** script,
51 CommandLineOptions* dart_options);
52
53 protected:
54 class DartDevRunner {
55 public:
56 DartDevRunner() {}
57
58 void Run(Dart_IsolateGroupCreateCallback create_isolate,
59 const char* packages_file,
60 char** script,
61 CommandLineOptions* dart_options);
62
63 DartDev_Result result() const { return result_; }
64
65 private:
66 static void DartDevResultCallback(Dart_Port dest_port_id,
67 Dart_CObject* message);
68 static void RunCallback(uword arg);
69 static void ProcessError(const char* msg, int32_t exit_code);
70
71 static DartDev_Result result_;
72 static char** script_;
73 static std::unique_ptr<char*[], void (*)(char**)> argv_;
74 static intptr_t argc_;
75
76 Dart_IsolateGroupCreateCallback create_isolate_;
77 CommandLineOptions* dart_options_;
78 const char* packages_file_;
79 static Monitor* monitor_;
80
81 DISALLOW_ALLOCATION();
82 };
83
84 private:
85 // Attempts to find the DartDev snapshot. If the snapshot cannot be found,
86 // the VM will shutdown.
87 static Utils::CStringUniquePtr TryResolveDartDevSnapshotPath();
88
89 static DartDevRunner runner_;
90 static bool should_run_dart_dev_;
91
92 DISALLOW_ALLOCATION();
93 DISALLOW_IMPLICIT_CONSTRUCTORS(DartDevIsolate);
94};
95
96} // namespace bin
97} // namespace dart
98
99#endif // !defined(DART_PRECOMPILED_RUNTIME)
100#endif // RUNTIME_BIN_DARTDEV_ISOLATE_H_
101