mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
Allow dynamic resolution of Dart isolate and instructions snapshot
This commit is contained in:
parent
1e98421ca1
commit
eb4326331d
@ -116,11 +116,11 @@ action("generate_snapshot_file") {
|
||||
]
|
||||
inputs = [
|
||||
"//dart/runtime/tools/create_snapshot_file.py",
|
||||
"snapshot.cc.tmpl",
|
||||
"snapshot.c.tmpl",
|
||||
"$target_gen_dir/vm_isolate_snapshot.bin",
|
||||
"$target_gen_dir/isolate_snapshot.bin",
|
||||
]
|
||||
output = "$target_gen_dir/snapshot.cc"
|
||||
output = "$target_gen_dir/snapshot.c"
|
||||
outputs = [
|
||||
output,
|
||||
]
|
||||
@ -132,7 +132,7 @@ action("generate_snapshot_file") {
|
||||
"--input_bin",
|
||||
rebase_path("$target_gen_dir/isolate_snapshot.bin"),
|
||||
"--input_cc",
|
||||
rebase_path("snapshot.cc.tmpl"),
|
||||
rebase_path("snapshot.c.tmpl"),
|
||||
"--output",
|
||||
rebase_path(output),
|
||||
]
|
||||
@ -171,7 +171,7 @@ action("generate_updater_snapshot_file") {
|
||||
|
||||
source_set("snapshot_cc") {
|
||||
sources = [
|
||||
"$target_gen_dir/snapshot.cc",
|
||||
"$target_gen_dir/snapshot.c",
|
||||
]
|
||||
|
||||
deps = [
|
||||
|
||||
@ -137,14 +137,14 @@ template("dart_precompile") {
|
||||
]
|
||||
}
|
||||
|
||||
snapshot_cc = "$target_gen_dir/" + target_name + "_precompiled_snapshot.cc"
|
||||
snapshot_cc_gen_target_name = target_name + "_snapshot_cc"
|
||||
action(snapshot_cc_gen_target_name) {
|
||||
snapshot_c = "$target_gen_dir/" + target_name + "_precompiled_snapshot.c"
|
||||
snapshot_c_gen_target_name = target_name + "_snapshot_c"
|
||||
action(snapshot_c_gen_target_name) {
|
||||
deps = [
|
||||
":$instructions_gen_target_name",
|
||||
]
|
||||
|
||||
template_file = "//sky/engine/bindings/snapshot.cc.tmpl"
|
||||
template_file = "//sky/engine/bindings/snapshot.c.tmpl"
|
||||
|
||||
inputs = [
|
||||
template_file,
|
||||
@ -153,7 +153,7 @@ template("dart_precompile") {
|
||||
]
|
||||
|
||||
outputs = [
|
||||
snapshot_cc,
|
||||
snapshot_c,
|
||||
]
|
||||
|
||||
script = "//dart/runtime/tools/create_snapshot_file.py"
|
||||
@ -165,19 +165,19 @@ template("dart_precompile") {
|
||||
"--input_cc",
|
||||
rebase_path(template_file, root_build_dir),
|
||||
"--output",
|
||||
rebase_path(snapshot_cc),
|
||||
rebase_path(snapshot_c),
|
||||
]
|
||||
}
|
||||
|
||||
source_set(target_name) {
|
||||
sources = [
|
||||
assembly_path,
|
||||
snapshot_cc,
|
||||
snapshot_c,
|
||||
]
|
||||
|
||||
deps = [
|
||||
":$instructions_gen_target_name",
|
||||
":$snapshot_cc_gen_target_name",
|
||||
":$snapshot_c_gen_target_name",
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@ -6,22 +6,16 @@
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
namespace blink {
|
||||
|
||||
// The string on the next line will be filled in with the contents of the
|
||||
// generated snapshot binary file for the vm isolate.
|
||||
// This string forms the content of the dart vm isolate snapshot which
|
||||
// is loaded into the vm isolate.
|
||||
static const uint8_t vm_isolate_snapshot_buffer_[]
|
||||
__attribute__((aligned(8))) = { % s};
|
||||
const uint8_t* kDartVmIsolateSnapshotBuffer = vm_isolate_snapshot_buffer_;
|
||||
const uint8_t kDartVmIsolateSnapshotBuffer[]
|
||||
__attribute__((visibility("default"), aligned(8), used)) = { %s };
|
||||
|
||||
// The string on the next line will be filled in with the contents of the
|
||||
// generated snapshot binary file for a regular dart isolate.
|
||||
// This string forms the content of a regular dart isolate snapshot which
|
||||
// is loaded into an isolate when it is created.
|
||||
static const uint8_t isolate_snapshot_buffer_[]
|
||||
__attribute__((aligned(8))) = { % s};
|
||||
const uint8_t* kDartIsolateSnapshotBuffer = isolate_snapshot_buffer_;
|
||||
|
||||
} // namespace blink
|
||||
const uint8_t kDartIsolateSnapshotBuffer[]
|
||||
__attribute__((visibility("default"), aligned(8), used)) = { %s };
|
||||
@ -137,11 +137,12 @@ void DartController::RunFromLibrary(const String& name,
|
||||
}
|
||||
|
||||
void DartController::CreateIsolateFor(std::unique_ptr<DOMDartState> state) {
|
||||
CHECK(kDartIsolateSnapshotBuffer);
|
||||
char* error = nullptr;
|
||||
dom_dart_state_ = std::move(state);
|
||||
Dart_Isolate isolate = Dart_CreateIsolate(
|
||||
dom_dart_state_->url().utf8().data(), "main", kDartIsolateSnapshotBuffer,
|
||||
dom_dart_state_->url().utf8().data(), "main",
|
||||
reinterpret_cast<uint8_t*>(
|
||||
DartSymbolLookup(kDartIsolateSnapshotBufferName)),
|
||||
nullptr, static_cast<DartState*>(dom_dart_state_.get()), &error);
|
||||
Dart_SetMessageNotifyCallback(MessageNotifyCallback);
|
||||
CHECK(isolate) << error;
|
||||
|
||||
@ -48,17 +48,12 @@ void EnsureHandleWatcherStarted() {
|
||||
// during shutdown.
|
||||
Dart_Handle mojo_core_lib = Dart_LookupLibrary(ToDart("dart:mojo.internal"));
|
||||
CHECK(!LogIfError((mojo_core_lib)));
|
||||
Dart_Handle handle_watcher_type = Dart_GetType(
|
||||
mojo_core_lib,
|
||||
Dart_NewStringFromCString("MojoHandleWatcher"),
|
||||
0,
|
||||
nullptr);
|
||||
Dart_Handle handle_watcher_type =
|
||||
Dart_GetType(mojo_core_lib,
|
||||
Dart_NewStringFromCString("MojoHandleWatcher"), 0, nullptr);
|
||||
CHECK(!LogIfError(handle_watcher_type));
|
||||
CHECK(!LogIfError(Dart_Invoke(
|
||||
handle_watcher_type,
|
||||
Dart_NewStringFromCString("_start"),
|
||||
0,
|
||||
nullptr)));
|
||||
handle_watcher_type, Dart_NewStringFromCString("_start"), 0, nullptr)));
|
||||
|
||||
// RunLoop until the handle watcher isolate is spun-up.
|
||||
CHECK(!LogIfError(Dart_RunLoop()));
|
||||
@ -87,15 +82,15 @@ static const char* kDartArgs[] = {
|
||||
#endif
|
||||
};
|
||||
|
||||
static const char* kDartPrecompilationArgs[] {
|
||||
"--precompilation",
|
||||
static const char* kDartPrecompilationArgs[]{
|
||||
"--precompilation",
|
||||
};
|
||||
|
||||
static const char* kDartCheckedModeArgs[] = {
|
||||
"--enable_asserts",
|
||||
"--enable_type_checks",
|
||||
"--error_on_bad_type",
|
||||
"--error_on_bad_override",
|
||||
"--enable_asserts",
|
||||
"--enable_type_checks",
|
||||
"--error_on_bad_type",
|
||||
"--error_on_bad_override",
|
||||
};
|
||||
|
||||
void UnhandledExceptionCallback(Dart_Handle error) {
|
||||
@ -108,17 +103,12 @@ void IsolateShutdownCallback(void* callback_data) {
|
||||
|
||||
bool IsServiceIsolateURL(const char* url_name) {
|
||||
return url_name != nullptr &&
|
||||
String(url_name) == DART_VM_SERVICE_ISOLATE_NAME;
|
||||
String(url_name) == DART_VM_SERVICE_ISOLATE_NAME;
|
||||
}
|
||||
|
||||
static const uint8_t* PrecompiledInstructionsSymbolIfPresent() {
|
||||
dlerror(); // clear previous errors on thread
|
||||
void* sym = nullptr;
|
||||
#ifdef RTLD_SELF
|
||||
static const char kInstructionsSnapshotSymbolName[] = "kInstructionsSnapshot";
|
||||
sym = dlsym(RTLD_SELF, kInstructionsSnapshotSymbolName);
|
||||
#endif
|
||||
return (dlerror() != nullptr) ? nullptr : reinterpret_cast<uint8_t * >(sym);
|
||||
return reinterpret_cast<uint8_t*>(
|
||||
DartSymbolLookup(kInstructionsSnapshotName));
|
||||
}
|
||||
|
||||
static bool IsRunningPrecompiledCode() {
|
||||
@ -136,11 +126,11 @@ Dart_Isolate IsolateCreateCallback(const char* script_uri,
|
||||
void* callback_data,
|
||||
char** error) {
|
||||
if (IsServiceIsolateURL(script_uri)) {
|
||||
CHECK(kDartIsolateSnapshotBuffer);
|
||||
DartState* dart_state = new DartState();
|
||||
Dart_Isolate isolate =
|
||||
Dart_CreateIsolate(script_uri, "main", kDartIsolateSnapshotBuffer,
|
||||
nullptr, nullptr, error);
|
||||
Dart_Isolate isolate = Dart_CreateIsolate(
|
||||
script_uri, "main", reinterpret_cast<const uint8_t*>(DartSymbolLookup(
|
||||
kDartIsolateSnapshotBufferName)),
|
||||
nullptr, nullptr, error);
|
||||
CHECK(isolate) << error;
|
||||
dart_state->SetIsolate(isolate);
|
||||
CHECK(Dart_IsServiceIsolate(isolate));
|
||||
@ -157,9 +147,8 @@ Dart_Isolate IsolateCreateCallback(const char* script_uri,
|
||||
if (RuntimeEnabledFeatures::observatoryEnabled()) {
|
||||
std::string ip = "127.0.0.1";
|
||||
const intptr_t port = 8181;
|
||||
const bool service_isolate_booted =
|
||||
DartServiceIsolate::Startup(ip, port, DartLibraryTagHandler,
|
||||
IsRunningPrecompiledCode(), error);
|
||||
const bool service_isolate_booted = DartServiceIsolate::Startup(
|
||||
ip, port, DartLibraryTagHandler, IsRunningPrecompiledCode(), error);
|
||||
CHECK(service_isolate_booted) << error;
|
||||
}
|
||||
}
|
||||
@ -168,12 +157,12 @@ Dart_Isolate IsolateCreateCallback(const char* script_uri,
|
||||
}
|
||||
|
||||
// Create & start the handle watcher isolate
|
||||
CHECK(kDartIsolateSnapshotBuffer);
|
||||
// TODO(abarth): Who deletes this DartState instance?
|
||||
DartState* dart_state = new DartState();
|
||||
Dart_Isolate isolate =
|
||||
Dart_CreateIsolate("sky:handle_watcher", "", kDartIsolateSnapshotBuffer,
|
||||
nullptr, dart_state, error);
|
||||
Dart_Isolate isolate = Dart_CreateIsolate(
|
||||
"sky:handle_watcher", "", reinterpret_cast<uint8_t*>(DartSymbolLookup(
|
||||
kDartIsolateSnapshotBufferName)),
|
||||
nullptr, dart_state, error);
|
||||
CHECK(isolate) << error;
|
||||
dart_state->SetIsolate(isolate);
|
||||
|
||||
@ -195,7 +184,47 @@ Dart_Isolate IsolateCreateCallback(const char* script_uri,
|
||||
return isolate;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace
|
||||
|
||||
const char* kDartVmIsolateSnapshotBufferName = "kDartVmIsolateSnapshotBuffer";
|
||||
const char* kDartIsolateSnapshotBufferName = "kDartIsolateSnapshotBuffer";
|
||||
const char* kInstructionsSnapshotName = "kInstructionsSnapshot";
|
||||
|
||||
const char* kDartApplicationLibraryPath =
|
||||
"FlutterApplication.framework/FlutterApplication";
|
||||
|
||||
static void* DartLookupSymbolInLibrary(const char* symbol_name,
|
||||
const char* library) {
|
||||
if (symbol_name == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
dlerror(); // clear previous errors on thread
|
||||
void* library_handle = dlopen(library, RTLD_NOW);
|
||||
if (dlerror() != nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
void* sym = dlsym(library_handle, symbol_name);
|
||||
return dlerror() != nullptr ? nullptr : sym;
|
||||
}
|
||||
|
||||
void* DartSymbolLookup(const char* symbol_name) {
|
||||
if (symbol_name == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// First the application library is checked for the valid symbols. This
|
||||
// library may not necessarily exist. If it does exist, it is loaded and the
|
||||
// symbols resolved. Once the application library is loaded, there is
|
||||
// currently no provision to unload the same.
|
||||
void* symbol =
|
||||
DartLookupSymbolInLibrary(symbol_name, kDartApplicationLibraryPath);
|
||||
if (symbol != nullptr) {
|
||||
return symbol;
|
||||
}
|
||||
|
||||
// Check inside the default library
|
||||
return DartLookupSymbolInLibrary(symbol_name, nullptr);
|
||||
}
|
||||
|
||||
void InitDartVM() {
|
||||
dart::bin::BootstrapDartIo();
|
||||
@ -217,21 +246,21 @@ void InitDartVM() {
|
||||
CHECK(Dart_SetVMFlags(args.size(), args.data()));
|
||||
// This should be called before calling Dart_Initialize.
|
||||
DartDebugger::InitDebugger();
|
||||
CHECK(Dart_Initialize(
|
||||
kDartVmIsolateSnapshotBuffer,
|
||||
PrecompiledInstructionsSymbolIfPresent(),
|
||||
IsolateCreateCallback,
|
||||
nullptr, // Isolate interrupt callback.
|
||||
UnhandledExceptionCallback, IsolateShutdownCallback,
|
||||
// File IO callbacks.
|
||||
nullptr, nullptr, nullptr, nullptr,
|
||||
// Entroy source
|
||||
nullptr,
|
||||
// VM service assets archive
|
||||
nullptr) == nullptr);
|
||||
CHECK(Dart_Initialize(reinterpret_cast<uint8_t*>(
|
||||
DartSymbolLookup(kDartVmIsolateSnapshotBufferName)),
|
||||
PrecompiledInstructionsSymbolIfPresent(),
|
||||
IsolateCreateCallback,
|
||||
nullptr, // Isolate interrupt callback.
|
||||
UnhandledExceptionCallback, IsolateShutdownCallback,
|
||||
// File IO callbacks.
|
||||
nullptr, nullptr, nullptr, nullptr,
|
||||
// Entroy source
|
||||
nullptr,
|
||||
// VM service assets archive
|
||||
nullptr) == nullptr);
|
||||
// Wait for load port- ensures handle watcher and service isolates are
|
||||
// running.
|
||||
Dart_ServiceWaitForLoadPort();
|
||||
}
|
||||
|
||||
} // namespace blink
|
||||
} // namespace blink
|
||||
|
||||
@ -9,8 +9,11 @@
|
||||
|
||||
namespace blink {
|
||||
|
||||
extern const uint8_t* kDartVmIsolateSnapshotBuffer;
|
||||
extern const uint8_t* kDartIsolateSnapshotBuffer;
|
||||
extern const char* kDartVmIsolateSnapshotBufferName;
|
||||
extern const char* kDartIsolateSnapshotBufferName;
|
||||
extern const char* kInstructionsSnapshotName;
|
||||
|
||||
void* DartSymbolLookup(const char* symbol_name);
|
||||
|
||||
void InitDartVM();
|
||||
Dart_Handle DartLibraryTagHandler(Dart_LibraryTag tag,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user