Walking into the Dart virtual machine: In-depth understanding of efficient and excellent operating mechanism

Dart virtual machine overview

The Dart virtual machine is one of the main tools for running Dart code. The Flutter framework uses Dart as a development language, so the Dart virtual machine plays a very important role in Flutter.

The Dart virtual machine is an interpreter capable of interpreting and executing Dart code. Dart code is compiled into bytecode in a virtual machine, and then converted into machine code and executed by an interpreter. This process allows Dart code to run on a variety of different platforms without platform-specific compilation.

The Dart virtual machine has many functions, including:

  • Just-in-time compilation (JIT): The Dart virtual machine can compile Dart code at runtime, thereby improving execution efficiency.
  • Precompilation (AOT): The Dart virtual machine can precompile Dart code into local machine code to improve execution efficiency and reduce startup time.
  • Memory management: The Dart virtual machine uses a garbage collector to manage memory, helping developers avoid memory leaks and other memory-related problems.
  • Asynchronous programming: The Dart virtual machine has built-in asynchronous programming tools such as Future and Stream, making it easy for developers to perform asynchronous programming.
  • Plug-in system: The Dart virtual machine supports a plug-in system, allowing developers to easily integrate code from other languages ​​into Dart applications.

In Flutter, the Dart virtual machine is used to run Dart code and supports hot reloading, allowing developers to quickly iterate and debug code. In addition, the Dart virtual machine is also used to run native plugins in Flutter applications.

The creation process of Dart virtual machine

The Dart virtual machine is an interpreter that can compile Dart code into bytecode, and convert it into machine code and execute it through the interpreter. In a Flutter application, the Dart virtual machine is created and run by the Flutter engine.

The following is the creation process of the Dart virtual machine:

  1. First, the Flutter engine will create a new Isolate (independent Dart execution environment), and load the Dart code into the Isolate.
  2. Next, the Flutter engine will start the Dart virtual machine and create a Dart virtual machine instance for the Isolate.
  3. The Dart virtual machine instance initializes a Dart runtime environment and creates an Isolate object in the environment, which is used to manage and control the current execution environment.
  4. The Dart virtual machine instance will allocate some resources for the Isolate, including stack space, heap memory, and file descriptors.
  5. The Dart virtual machine instance will compile the Dart code into bytecode and load it into the Isolate.
  6. Then, the Dart virtual machine instance creates a Dart interpreter that interprets and executes the Dart code in the Isolate.
  7. Finally, the Dart virtual machine instance will start the Isolate to start executing Dart code.

Dart virtual machine code implementation

Parses command line arguments and environment variables. The Dart virtual machine accepts many command line parameters and environment variables, which are used to specify running parameters, load libraries, set log levels, and more.

复制代码dart::bin::MainIsolateStartupData* isolate_data = new dart::bin::MainIsolateStartupData(&error);
if (isolate_data == NULL) {
  OS::PrintErr("Error allocating startup data\n");
  return kErrorExit;
}
// Parse command-line arguments.
error = DartUtils::ParseCommandLineArguments(argc, argv, isolate_data);
if (!error.IsEmpty()) {
  OS::PrintErr("%s\n", error.ToCString());
  delete isolate_data;
  return kErrorExit;
}

Initialize the garbage collector. The Dart virtual machine uses a garbage collector called "marking", and its initialization process allocates a certain amount of memory for the virtual machine, including storage space, stack space, and heap space.

复制代码Dart_EnterScope();
Dart_Handle result_code = Dart_Init(isolate_data->script_name(),
                                    isolate_data->resolved_script_url(),
                                    isolate_data->create_group_);
if (Dart_IsError(result_code)) {
  error = DartUtils::GetStringValue(result_code);
} else {
  ASSERT(result_code == Dart_Null());
}
Dart_ExitScope();

Load the dart:core library. The dart:core library is the core library in the Dart virtual machine, which includes functions such as basic data types, control flow statements, and exception handling.

复制代码Dart_Handle core_lib = Dart_LookupLibrary(DartUtils::NewString("dart:core"));
Dart_Handle class_double = Dart_GetClass(core_lib, DartUtils::NewString("double"));
if (Dart_IsError(class_double)) {
  error = DartUtils::GetStringValue(class_double);
}

Load a user-specified library. Users can load their own Dart libraries by passing command line parameters or using import statements, and the Dart virtual machine needs to load these libraries into the virtual machine when it starts.

复制代码// Load the specified application script into a 'Library' object.
Dart_Handle root_library =
    DartUtils::LoadScript(isolate_data->script_name(),
                          isolate_data->resolved_script_url(),
                          isolate_data->source_directory());
if (Dart_IsError(root_library)) {
  error = DartUtils::GetStringValue(root_library);
}

Create a Dart standalone runtime environment. The Dart virtual machine uses Dart_Isolate as the basis for internal threads. Dart_Isolate is an execution unit in Dart that contains all states in Dart, including program counters, heaps, stacks, local variables, etc.

复制代码// Create a new isolate from the application script.
Dart_IsolateFlags flags;
strcpy(flags.name, "");
flags.package_root = NULL;
flags.packages_file = NULL;
Dart_Isolate isolate =
    Dart_CreateIsolate(isolate_data->script_name(), "", isolate_snapshot_buffer,
                       NULL, &

The Dart virtual machine is an important technical part in flutter, except for the small technical introduction of dart in this article. You also need to learn a lot of core technology points; you can be regarded as a senior flutter. Here it is recommended to refer to the "Flutter Manual" Click to view all technical categories.

Summarize

The Dart virtual machine is a strongly typed scripting language virtual machine that supports JIT compilation and AOT compilation. It can run on a variety of platforms, including PCs, mobile devices, and embedded devices. The Dart virtual machine has a large number of built-in standard libraries and tools, including Dart SDK, Dart Analyzer, Dartfmt, etc., which are convenient for developers to develop and maintain. The Dart virtual machine supports the use of various IDEs, including Flutter, IntelliJ IDEA, etc., and provides powerful code editing and debugging functions, greatly improving development efficiency.

The design architecture of the Dart virtual machine has good scalability and customizability, supports mechanisms such as Dart's Native extension, and can load and unload modules at runtime for dynamic expansion. The Dart virtual machine also supports some advanced language features, such as asynchronous programming and generators, allowing developers to write more efficient programs with asynchronous logic. At the same time, the Dart virtual machine also supports features such as code optimization and precompilation to improve program performance and operating efficiency.

In short, the Dart virtual machine is a strongly typed scripting language virtual machine with many excellent features. It is closely integrated with ecosystems such as Flutter and provides a comprehensive and efficient solution for mobile application development.

Guess you like

Origin blog.csdn.net/m0_62167422/article/details/130352599