Skip to content

Commit

Permalink
Revert "Eliminate support for Dart 1 (flutter#5504)" (flutter#5505)
Browse files Browse the repository at this point in the history
Broke runtime_unittests, which are still running directly from source.

This reverts commit 0ea93c3.
  • Loading branch information
cbracken authored Jun 12, 2018
1 parent 0ea93c3 commit be02d0c
Show file tree
Hide file tree
Showing 12 changed files with 244 additions and 39 deletions.
2 changes: 2 additions & 0 deletions common/settings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace blink {
std::string Settings::ToString() const {
std::stringstream stream;
stream << "Settings: " << std::endl;
stream << "script_snapshot_path: " << script_snapshot_path << std::endl;
stream << "vm_snapshot_data_path: " << vm_snapshot_data_path << std::endl;
stream << "vm_snapshot_instr_path: " << vm_snapshot_instr_path << std::endl;
stream << "isolate_snapshot_data_path: " << isolate_snapshot_data_path
Expand All @@ -31,6 +32,7 @@ std::string Settings::ToString() const {
stream << "trace_startup: " << trace_startup << std::endl;
stream << "endless_trace_buffer: " << endless_trace_buffer << std::endl;
stream << "enable_dart_profiling: " << enable_dart_profiling << std::endl;
stream << "dart_non_checked_mode: " << dart_non_checked_mode << std::endl;
stream << "enable_observatory: " << enable_observatory << std::endl;
stream << "observatory_port: " << observatory_port << std::endl;
stream << "ipv6: " << ipv6 << std::endl;
Expand Down
2 changes: 2 additions & 0 deletions common/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ using TaskObserverRemove = std::function<void(intptr_t /* key */)>;

struct Settings {
// VM settings
std::string script_snapshot_path;
std::string platform_kernel_path;

std::string vm_snapshot_data_path;
Expand All @@ -46,6 +47,7 @@ struct Settings {
bool trace_startup = false;
bool endless_trace_buffer = false;
bool enable_dart_profiling = false;
bool dart_non_checked_mode = false;
// Used as the script URI in debug messages. Does not affect how the Dart code
// is executed.
std::string advisory_script_uri = "main.dart";
Expand Down
33 changes: 30 additions & 3 deletions runtime/dart_isolate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,18 @@ bool DartIsolate::PrepareForRunningFromPrecompiledCode() {
return true;
}

static bool LoadScriptSnapshot(std::shared_ptr<const fml::Mapping> mapping,
bool last_piece) {
FXL_CHECK(last_piece) << "Script snapshots cannot be divided";
if (tonic::LogIfError(Dart_LoadScriptFromSnapshot(mapping->GetMapping(),
mapping->GetSize()))) {
return false;
}
return true;
}

static bool LoadKernelSnapshot(std::shared_ptr<const fml::Mapping> mapping,
bool last_piece) {
FXL_DCHECK(Dart_IsKernel(mapping->GetMapping(), mapping->GetSize())) << "Only kernel snapshots are supported";
Dart_Handle library =
Dart_LoadLibraryFromKernel(mapping->GetMapping(), mapping->GetSize());
if (tonic::LogIfError(library)) {
Expand All @@ -298,6 +307,16 @@ static bool LoadKernelSnapshot(std::shared_ptr<const fml::Mapping> mapping,
return true;
}

static bool LoadSnapshot(std::shared_ptr<const fml::Mapping> mapping,
bool last_piece) {
if (Dart_IsKernel(mapping->GetMapping(), mapping->GetSize())) {
return LoadKernelSnapshot(std::move(mapping), last_piece);
} else {
return LoadScriptSnapshot(std::move(mapping), last_piece);
}
return false;
}

FXL_WARN_UNUSED_RESULT
bool DartIsolate::PrepareForRunningFromSnapshot(
std::shared_ptr<const fml::Mapping> mapping,
Expand All @@ -321,7 +340,7 @@ bool DartIsolate::PrepareForRunningFromSnapshot(
return false;
}

if (!LoadKernelSnapshot(mapping, last_piece)) {
if (!LoadSnapshot(mapping, last_piece)) {
return false;
}

Expand Down Expand Up @@ -547,12 +566,20 @@ Dart_Isolate DartIsolate::DartCreateAndStartServiceIsolate(
// thread.
service_isolate->ResetWeakPtrFactory();

const bool isolate_snapshot_is_dart_2 = Dart_IsDart2Snapshot(
vm->GetIsolateSnapshot()->GetData()->GetSnapshotPointer());
const bool is_preview_dart2 =
(vm->GetPlatformKernel().GetSize() > 0) || isolate_snapshot_is_dart_2;
const bool running_from_sources =
!DartVM::IsRunningPrecompiledCode() && !is_preview_dart2;

tonic::DartState::Scope scope(service_isolate);
if (!DartServiceIsolate::Startup(
settings.ipv6 ? "::1" : "127.0.0.1", // server IP address
settings.observatory_port, // server observatory port
tonic::DartState::HandleLibraryTag, // embedder library tag handler
false, // disable websocket origin check
running_from_sources, // running from source code
false, // disable websocket origin check
error // error (out)
)) {
// Error is populated by call to startup.
Expand Down
98 changes: 89 additions & 9 deletions runtime/dart_service_isolate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
}

#define kLibrarySourceNamePrefix "/vmservice"
static const char* kServiceIsolateScript = "vmservice_io.dart";

namespace flutter {
namespace runtime {
Expand Down Expand Up @@ -83,6 +84,7 @@ void DartServiceIsolate::Shutdown(Dart_NativeArguments args) {
bool DartServiceIsolate::Startup(std::string server_ip,
intptr_t server_port,
Dart_LibraryTagHandler embedder_tag_handler,
bool running_from_sources,
bool disable_origin_check,
char** error) {
Dart_Isolate isolate = Dart_CurrentIsolate();
Expand All @@ -106,14 +108,31 @@ bool DartServiceIsolate::Startup(std::string server_ip,
&flutter::runtime::__flutter_embedded_service_isolate_resources_[0]);
}

// Set the root library for the isolate.
Dart_Handle uri = Dart_NewStringFromCString("dart:vmservice_io");
Dart_Handle library = Dart_LookupLibrary(uri);
SHUTDOWN_ON_ERROR(library);
Dart_Handle result = Dart_SetRootLibrary(library);
SHUTDOWN_ON_ERROR(result);
result = Dart_SetNativeResolver(library, GetNativeFunction, GetSymbol);
SHUTDOWN_ON_ERROR(result);
Dart_Handle result;

if (running_from_sources) {
// Use our own library tag handler when loading service isolate sources.
Dart_SetLibraryTagHandler(DartServiceIsolate::LibraryTagHandler);
// Load main script.
Dart_Handle library = LoadScript(kServiceIsolateScript);
FXL_DCHECK(library != Dart_Null());
SHUTDOWN_ON_ERROR(library);
// Setup native entry resolution.
result = Dart_SetNativeResolver(library, GetNativeFunction, GetSymbol);

SHUTDOWN_ON_ERROR(result);
// Finalize loading.
result = Dart_FinalizeLoading(false);
SHUTDOWN_ON_ERROR(result);
} else {
Dart_Handle uri = Dart_NewStringFromCString("dart:vmservice_io");
Dart_Handle library = Dart_LookupLibrary(uri);
SHUTDOWN_ON_ERROR(library);
result = Dart_SetRootLibrary(library);
SHUTDOWN_ON_ERROR(result);
result = Dart_SetNativeResolver(library, GetNativeFunction, GetSymbol);
SHUTDOWN_ON_ERROR(result);
}

// Make runnable.
Dart_ExitScope();
Expand All @@ -127,7 +146,7 @@ bool DartServiceIsolate::Startup(std::string server_ip,
Dart_EnterIsolate(isolate);
Dart_EnterScope();

library = Dart_RootLibrary();
Dart_Handle library = Dart_RootLibrary();
SHUTDOWN_ON_ERROR(library);

// Set the HTTP server's ip.
Expand Down Expand Up @@ -155,6 +174,30 @@ bool DartServiceIsolate::Startup(std::string server_ip,
return true;
}

Dart_Handle DartServiceIsolate::GetSource(const char* name) {
const intptr_t kBufferSize = 512;
char buffer[kBufferSize];
snprintf(&buffer[0], kBufferSize - 1, "%s/%s", kLibrarySourceNamePrefix,
name);
const char* vmservice_source = NULL;
int r = g_resources->ResourceLookup(buffer, &vmservice_source);
FXL_DCHECK(r != EmbedderResources::kNoSuchInstance);
return Dart_NewStringFromCString(vmservice_source);
}

Dart_Handle DartServiceIsolate::LoadScript(const char* name) {
Dart_Handle url = Dart_NewStringFromCString("dart:vmservice_io");
Dart_Handle source = GetSource(name);
return Dart_LoadScript(url, Dart_Null(), source, 0, 0);
}

Dart_Handle DartServiceIsolate::LoadSource(Dart_Handle library,
const char* name) {
Dart_Handle url = Dart_NewStringFromCString(name);
Dart_Handle source = GetSource(name);
return Dart_LoadSource(library, url, Dart_Null(), source, 0, 0);
}

Dart_Handle DartServiceIsolate::LoadResource(Dart_Handle library,
const char* resource_name) {
// Prepare for invoke call.
Expand Down Expand Up @@ -205,4 +248,41 @@ Dart_Handle DartServiceIsolate::LoadResources(Dart_Handle library) {
return result;
}

Dart_Handle DartServiceIsolate::LibraryTagHandler(Dart_LibraryTag tag,
Dart_Handle library,
Dart_Handle url) {
if (!Dart_IsLibrary(library)) {
return Dart_NewApiError("not a library");
}
if (!Dart_IsString(url)) {
return Dart_NewApiError("url is not a string");
}
const char* url_string = NULL;
Dart_Handle result = Dart_StringToCString(url, &url_string);
if (Dart_IsError(result)) {
return result;
}
Dart_Handle library_url = Dart_LibraryUrl(library);
const char* library_url_string = NULL;
result = Dart_StringToCString(library_url, &library_url_string);
if (Dart_IsError(result)) {
return result;
}
if (tag == Dart_kImportTag) {
// Embedder handles all requests for external libraries.
return g_embedder_tag_handler(tag, library, url);
}
FXL_DCHECK((tag == Dart_kSourceTag) || (tag == Dart_kCanonicalizeUrl));
if (tag == Dart_kCanonicalizeUrl) {
// url is already canonicalized.
return url;
}
// Get source from builtin resources.
Dart_Handle source = GetSource(url_string);
if (Dart_IsError(source)) {
return source;
}
return Dart_LoadSource(library, url, Dart_Null(), source, 0, 0);
}

} // namespace blink
9 changes: 9 additions & 0 deletions runtime/dart_service_isolate.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class DartServiceIsolate {
static bool Startup(std::string server_ip,
intptr_t server_port,
Dart_LibraryTagHandler embedder_tag_handler,
bool running_from_sources,
bool disable_origin_check,
char** error);

Expand All @@ -27,6 +28,14 @@ class DartServiceIsolate {
static void NotifyServerState(Dart_NativeArguments args);
static void Shutdown(Dart_NativeArguments args);

// Script loading.
static Dart_Handle GetSource(const char* name);
static Dart_Handle LoadScript(const char* name);
static Dart_Handle LoadSource(Dart_Handle library, const char* name);
static Dart_Handle LibraryTagHandler(Dart_LibraryTag tag,
Dart_Handle library,
Dart_Handle url);

// Observatory resource loading.
static Dart_Handle LoadResources(Dart_Handle library);
static Dart_Handle LoadResource(Dart_Handle library, const char* name);
Expand Down
42 changes: 33 additions & 9 deletions runtime/dart_vm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ static const char* kDartAssertArgs[] = {
// clang-format on
};

static const char* kDartCheckedModeArgs[] = {
// clang-format off
"--enable_type_checks",
"--error_on_bad_type",
"--error_on_bad_override",
// clang-format on
};

static const char* kDartStrongModeArgs[] = {
// clang-format off
"--strong",
Expand Down Expand Up @@ -316,18 +324,18 @@ DartVM::DartVM(const Settings& settings,
arraysize(kDartPrecompilationArgs));
}

// Enable asserts if we are not running precompiled code. We run non-
// Enable checked mode if we are not running precompiled code. We run non-
// precompiled code only in the debug product mode.
bool enable_asserts = true;
bool use_checked_mode = !settings.dart_non_checked_mode;

#if FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DYNAMIC_PROFILE || \
FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DYNAMIC_RELEASE
enable_asserts = false;
use_checked_mode = false;
#endif

#if !OS_FUCHSIA
if (IsRunningPrecompiledCode()) {
enable_asserts = false;
use_checked_mode = false;
}
#endif // !OS_FUCHSIA

Expand All @@ -338,12 +346,29 @@ DartVM::DartVM(const Settings& settings,
arraysize(kDartWriteProtectCodeArgs));
#endif

// Require Dart 2.
FML_DCHECK(platform_kernel_mapping_->GetSize() > 0);
const bool isolate_snapshot_is_dart_2 =
Dart_IsDart2Snapshot(isolate_snapshot_->GetData()->GetSnapshotPointer());

PushBackAll(&args, kDartStrongModeArgs, arraysize(kDartStrongModeArgs));
if (enable_asserts) {
const bool is_preview_dart2 =
(platform_kernel_mapping_->GetSize() > 0) || isolate_snapshot_is_dart_2;

FXL_DLOG(INFO) << "Dart 2 " << (is_preview_dart2 ? "is" : "is NOT")
<< " enabled. Platform kernel: "
<< static_cast<bool>(platform_kernel_mapping_->GetSize() > 0)
<< " Isolate Snapshot is Dart 2: "
<< isolate_snapshot_is_dart_2;

if (is_preview_dart2) {
PushBackAll(&args, kDartStrongModeArgs, arraysize(kDartStrongModeArgs));
if (use_checked_mode) {
PushBackAll(&args, kDartAssertArgs, arraysize(kDartAssertArgs));
}
} else if (use_checked_mode) {
FXL_DLOG(INFO) << "Checked mode is ON";
PushBackAll(&args, kDartAssertArgs, arraysize(kDartAssertArgs));
PushBackAll(&args, kDartCheckedModeArgs, arraysize(kDartCheckedModeArgs));
} else {
FXL_DLOG(INFO) << "Is not Dart 2 and Checked mode is OFF";
}

if (settings.start_paused) {
Expand All @@ -365,7 +390,6 @@ DartVM::DartVM(const Settings& settings,
PushBackAll(&args, kDartFuchsiaTraceArgs, arraysize(kDartFuchsiaTraceArgs));
#endif

// Add VM dart_flags last to allow user overrides.
for (size_t i = 0; i < settings.dart_flags.size(); i++)
args.push_back(settings.dart_flags[i].c_str());

Expand Down
11 changes: 11 additions & 0 deletions shell/common/isolate_configuration.cc
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,17 @@ std::unique_ptr<IsolateConfiguration> IsolateConfiguration::InferFromSettings(
}
}

// Running from script snapshot.
{
// TODO(engine): Add AssetManager::GetAsMapping or such to avoid the copy.
std::vector<uint8_t> script_snapshot;
if (asset_manager && asset_manager->GetAsBuffer(
settings.script_snapshot_path, &script_snapshot)) {
return CreateForSnapshot(
std::make_unique<fml::DataMapping>(std::move(script_snapshot)));
}
}

// Running from kernel divided into several pieces (for sharing).
{
// TODO(fuchsia): Add AssetManager::GetAsMapping or such to avoid the copy.
Expand Down
7 changes: 7 additions & 0 deletions shell/common/switches.cc
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ blink::Settings SettingsFromCommandLine(const fxl::CommandLine& command_line) {
}
}

// Checked mode overrides.
settings.dart_non_checked_mode =
command_line.HasOption(FlagForSwitch(Switch::DartNonCheckedMode));

settings.ipv6 = command_line.HasOption(FlagForSwitch(Switch::IPv6));

settings.start_paused =
Expand Down Expand Up @@ -157,6 +161,9 @@ blink::Settings SettingsFromCommandLine(const fxl::CommandLine& command_line) {
command_line.GetOptionValue(FlagForSwitch(Switch::FlutterAssetsDir),
&settings.assets_path);

command_line.GetOptionValue(FlagForSwitch(Switch::Snapshot),
&settings.script_snapshot_path);

command_line.GetOptionValue(FlagForSwitch(Switch::MainDartFile),
&settings.main_dart_file_path);

Expand Down
Loading

0 comments on commit be02d0c

Please sign in to comment.