提交 0c03d1c5 编写于 作者: A Adam Barth

Rework how we lookup native functions from Dart

This patch changes how we lookup native functions from Dart to be more
extensible. This patch paves the way for removing the dart:ui_internals
library.
上级 b7a66619
......@@ -7,20 +7,20 @@ import("//sky/engine/core/core.gni")
source_set("bindings") {
sources = [
"builtin.cc",
"builtin.h",
"dart_natives.cc",
"dart_natives.h",
"dart_ui.cc",
"dart_ui.h",
"dart_callback.cc",
"dart_callback.h",
"dart_mojo_internal.cc",
"dart_mojo_internal.h",
"dart_runtime_hooks.cc",
"dart_runtime_hooks.h",
"dart_ui.cc",
"dart_ui.h",
"exception_messages.cc",
"exception_messages.h",
"exception_state.cc",
"exception_state.h",
"exception_state_placeholder.cc",
"exception_state_placeholder.h",
"exception_state.cc",
"exception_state.h",
"mojo_natives.cc",
"mojo_natives.h",
"nullable.h",
......@@ -32,7 +32,6 @@ source_set("bindings") {
deps = [
"//base",
"//dart/runtime/bin:embedded_dart_io",
"//dart/runtime:libdart",
"//mojo/public/c/system",
"//mojo/public/cpp/system",
......
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "sky/engine/bindings/builtin.h"
#include "base/logging.h"
#include "bin/io_natives.h"
#include "dart/runtime/include/dart_api.h"
#include "sky/engine/bindings/dart_natives.h"
#include "sky/engine/bindings/dart_ui.h"
#include "sky/engine/bindings/mojo_natives.h"
#include "sky/engine/tonic/dart_builtin.h"
namespace blink {
namespace {
struct LibraryDescriptor {
const char* url;
bool has_natives;
Dart_NativeEntrySymbol native_symbol;
Dart_NativeEntryResolver native_resolver;
};
const LibraryDescriptor kBuiltinLibraries[] = {
/* { url_, has_natives_, native_symbol_, native_resolver_ } */
{"dart:ui", true, DartUI::NativeSymbol, DartUI::NativeLookup},
{"dart:mojo.internal", true, MojoNativeSymbol, MojoNativeLookup},
{"dart:io", true, dart::bin::IONativeSymbol, dart::bin::IONativeLookup },
};
} // namespace
void Builtin::SetNativeResolver(BuiltinLibraryId id) {
static_assert(arraysize(kBuiltinLibraries) == kInvalidLibrary,
"Unexpected number of builtin libraries");
DCHECK_GE(id, kUILibrary);
DCHECK_LT(id, kInvalidLibrary);
if (kBuiltinLibraries[id].has_natives) {
Dart_Handle library = DartBuiltin::LookupLibrary(kBuiltinLibraries[id].url);
// Setup the native resolver for built in library functions.
DART_CHECK_VALID(
Dart_SetNativeResolver(library,
kBuiltinLibraries[id].native_resolver,
kBuiltinLibraries[id].native_symbol));
}
}
Dart_Handle Builtin::LoadAndCheckLibrary(BuiltinLibraryId id) {
static_assert(arraysize(kBuiltinLibraries) == kInvalidLibrary,
"Unexpected number of builtin libraries");
DCHECK_GE(id, kUILibrary);
DCHECK_LT(id, kInvalidLibrary);
Dart_Handle library = DartBuiltin::LookupLibrary(kBuiltinLibraries[id].url);
DART_CHECK_VALID(library);
return library;
}
} // namespace blink
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef SKY_ENGINE_BINDINGS_BUILTIN_H_
#define SKY_ENGINE_BINDINGS_BUILTIN_H_
#include "base/macros.h"
#include "dart/runtime/include/dart_api.h"
namespace blink {
class Builtin {
public:
// Note: Changes to this enum should be accompanied with changes to
// the builtin_libraries_ array in builtin.cc.
enum BuiltinLibraryId {
kUILibrary,
kMojoInternalLibrary,
kIOLibrary,
kInvalidLibrary,
};
static void SetNativeResolver(BuiltinLibraryId id);
static Dart_Handle LoadAndCheckLibrary(BuiltinLibraryId id);
private:
DISALLOW_IMPLICIT_CONSTRUCTORS(Builtin);
};
} // namespace blink
#endif // SKY_ENGINE_BINDINGS_BUILTIN_H_
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "sky/engine/bindings/dart_mojo_internal.h"
#include "dart/runtime/include/dart_api.h"
#include "sky/engine/bindings/mojo_natives.h"
#include "sky/engine/tonic/dart_converter.h"
namespace blink {
void DartMojoInternal::InitForIsolate() {
DART_CHECK_VALID(Dart_SetNativeResolver(
Dart_LookupLibrary(ToDart("dart:mojo.internal")),
MojoNativeLookup,
MojoNativeSymbol));
}
} // namespace blink
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef SKY_ENGINE_BINDINGS_DART_MOJO_INTERNAL_H_
#define SKY_ENGINE_BINDINGS_DART_MOJO_INTERNAL_H_
#include "base/macros.h"
namespace blink {
class DartMojoInternal {
public:
static void InitForIsolate();
private:
DISALLOW_IMPLICIT_CONSTRUCTORS(DartMojoInternal);
};
} // namespace blink
#endif // SKY_ENGINE_BINDINGS_DART_MOJO_INTERNAL_H_
......@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "sky/engine/bindings/dart_natives.h"
#include "sky/engine/bindings/dart_runtime_hooks.h"
#include <stdio.h>
#include <stdlib.h>
......@@ -13,15 +13,14 @@
#include "base/macros.h"
#include "base/time/time.h"
#include "dart/runtime/include/dart_api.h"
#include "sky/engine/bindings/builtin.h"
#include "sky/engine/core/dom/Microtask.h"
#include "sky/engine/core/script/dom_dart_state.h"
#include "sky/engine/tonic/dart_api_scope.h"
#include "sky/engine/tonic/dart_builtin.h"
#include "sky/engine/tonic/dart_converter.h"
#include "sky/engine/tonic/dart_error.h"
#include "sky/engine/tonic/dart_invoke.h"
#include "sky/engine/tonic/dart_isolate_scope.h"
#include "sky/engine/tonic/dart_library_natives.h"
#include "sky/engine/tonic/dart_state.h"
#include "sky/engine/tonic/dart_timer_heap.h"
#include "sky/engine/tonic/dart_value.h"
......@@ -41,7 +40,7 @@ extern void syslog(int, const char *, ...);
namespace blink {
#define REGISTER_FUNCTION(name, count) \
{ "" #name, name, count },
{ "" #name, name, count, true },
#define DECLARE_FUNCTION(name, count) \
extern void name(Dart_NativeArguments args);
......@@ -56,41 +55,10 @@ namespace blink {
BUILTIN_NATIVE_LIST(DECLARE_FUNCTION);
static struct NativeEntries {
const char* name;
Dart_NativeFunction function;
int argument_count;
} BuiltinEntries[] = {BUILTIN_NATIVE_LIST(REGISTER_FUNCTION)};
Dart_NativeFunction DartNatives::NativeLookup(Dart_Handle name,
int argument_count,
bool* auto_setup_scope) {
const char* function_name = nullptr;
Dart_Handle result = Dart_StringToCString(name, &function_name);
DART_CHECK_VALID(result);
DCHECK(function_name != nullptr);
DCHECK(auto_setup_scope != nullptr);
*auto_setup_scope = true;
size_t num_entries = arraysize(BuiltinEntries);
for (size_t i = 0; i < num_entries; i++) {
const struct NativeEntries& entry = BuiltinEntries[i];
if (!strcmp(function_name, entry.name) &&
(entry.argument_count == argument_count)) {
return entry.function;
}
}
return nullptr;
}
const uint8_t* DartNatives::NativeSymbol(Dart_NativeFunction native_function) {
size_t num_entries = arraysize(BuiltinEntries);
for (size_t i = 0; i < num_entries; i++) {
const struct NativeEntries& entry = BuiltinEntries[i];
if (entry.function == native_function) {
return reinterpret_cast<const uint8_t*>(entry.name);
}
}
return nullptr;
void DartRuntimeHooks::RegisterNatives(DartLibraryNatives* natives) {
natives->Register({
BUILTIN_NATIVE_LIST(REGISTER_FUNCTION)
});
}
static Dart_Handle GetClosure(Dart_Handle builtin_library, const char* name) {
......@@ -101,65 +69,64 @@ static Dart_Handle GetClosure(Dart_Handle builtin_library, const char* name) {
}
static void InitDartInternal(Dart_Handle builtin_library,
DartNatives::IsolateType isolate_type) {
DartRuntimeHooks::IsolateType isolate_type) {
Dart_Handle print = GetClosure(builtin_library, "_getPrintClosure");
Dart_Handle timer = GetClosure(builtin_library, "_getCreateTimerClosure");
Dart_Handle internal_library = DartBuiltin::LookupLibrary("dart:_internal");
Dart_Handle internal_library = Dart_LookupLibrary(ToDart("dart:_internal"));
DART_CHECK_VALID(Dart_SetField(
internal_library, ToDart("_printClosure"), print));
if (isolate_type == DartNatives::MainIsolate) {
if (isolate_type == DartRuntimeHooks::MainIsolate) {
Dart_Handle vm_hooks_name = ToDart("VMLibraryHooks");
Dart_Handle vm_hooks = Dart_GetClass(internal_library, vm_hooks_name);
DART_CHECK_VALID(vm_hooks);
Dart_Handle timer_name = ToDart("timerFactory");
DART_CHECK_VALID(Dart_SetField(vm_hooks, timer_name, timer));
} else {
CHECK(isolate_type == DartNatives::DartIOIsolate);
Dart_Handle io_lib = DartBuiltin::LookupLibrary("dart:io");
CHECK(isolate_type == DartRuntimeHooks::DartIOIsolate);
Dart_Handle io_lib = Dart_LookupLibrary(ToDart("dart:io"));
DART_CHECK_VALID(io_lib);
Dart_Handle setup_hooks = Dart_NewStringFromCString("_setupHooks");
DART_CHECK_VALID(Dart_Invoke(io_lib, setup_hooks, 0, NULL));
Dart_Handle isolate_lib = DartBuiltin::LookupLibrary("dart:isolate");
Dart_Handle isolate_lib = Dart_LookupLibrary(ToDart("dart:isolate"));
DART_CHECK_VALID(isolate_lib);
DART_CHECK_VALID(Dart_Invoke(isolate_lib, setup_hooks, 0, NULL));
}
}
static void InitDartCore(Dart_Handle builtin,
DartNatives::IsolateType isolate_type) {
static void InitDartCore(Dart_Handle builtin) {
Dart_Handle get_base_url = GetClosure(builtin, "_getGetBaseURLClosure");
Dart_Handle core_library = DartBuiltin::LookupLibrary("dart:core");
Dart_Handle core_library = Dart_LookupLibrary(ToDart("dart:core"));
DART_CHECK_VALID(Dart_SetField(core_library,
ToDart("_uriBaseClosure"), get_base_url));
}
static void InitDartAsync(Dart_Handle builtin_library,
DartNatives::IsolateType isolate_type) {
DartRuntimeHooks::IsolateType isolate_type) {
Dart_Handle schedule_microtask;
if (isolate_type == DartNatives::MainIsolate) {
if (isolate_type == DartRuntimeHooks::MainIsolate) {
schedule_microtask =
GetClosure(builtin_library, "_getScheduleMicrotaskClosure");
} else {
CHECK(isolate_type == DartNatives::DartIOIsolate);
Dart_Handle isolate_lib = DartBuiltin::LookupLibrary("dart:isolate");
CHECK(isolate_type == DartRuntimeHooks::DartIOIsolate);
Dart_Handle isolate_lib = Dart_LookupLibrary(ToDart("dart:isolate"));
Dart_Handle method_name =
Dart_NewStringFromCString("_getIsolateScheduleImmediateClosure");
schedule_microtask = Dart_Invoke(isolate_lib, method_name, 0, NULL);
}
Dart_Handle async_library = DartBuiltin::LookupLibrary("dart:async");
Dart_Handle async_library = Dart_LookupLibrary(ToDart("dart:async"));
Dart_Handle set_schedule_microtask = ToDart("_setScheduleImmediateClosure");
DART_CHECK_VALID(Dart_Invoke(async_library, set_schedule_microtask, 1,
&schedule_microtask));
}
void DartNatives::Init(IsolateType isolate_type) {
Dart_Handle builtin = Builtin::LoadAndCheckLibrary(Builtin::kUILibrary);
void DartRuntimeHooks::Install(IsolateType isolate_type) {
Dart_Handle builtin = Dart_LookupLibrary(ToDart("dart:ui"));
DART_CHECK_VALID(builtin);
InitDartInternal(builtin, isolate_type);
InitDartCore(builtin, isolate_type);
InitDartCore(builtin);
InitDartAsync(builtin, isolate_type);
}
......
......@@ -2,32 +2,29 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef SKY_ENGINE_BINDINGS_DART_NATIVES_H_
#define SKY_ENGINE_BINDINGS_DART_NATIVES_H_
#ifndef SKY_ENGINE_BINDINGS_DART_RUNTIME_HOOKS_H_
#define SKY_ENGINE_BINDINGS_DART_RUNTIME_HOOKS_H_
#include "base/macros.h"
#include "dart/runtime/include/dart_api.h"
#include "sky/engine/tonic/dart_library_natives.h"
namespace blink {
class DartNatives {
class DartRuntimeHooks {
public:
enum IsolateType {
MainIsolate,
DartIOIsolate,
};
static Dart_NativeFunction NativeLookup(Dart_Handle name,
int argument_count,
bool* auto_setup_scope);
static const uint8_t* NativeSymbol(Dart_NativeFunction native_function);
static void Init(IsolateType isolate_type);
static void Install(IsolateType isolate_type);
static void RegisterNatives(DartLibraryNatives* natives);
private:
DISALLOW_IMPLICIT_CONSTRUCTORS(DartNatives);
DISALLOW_IMPLICIT_CONSTRUCTORS(DartRuntimeHooks);
};
} // namespace blink
#endif // SKY_ENGINE_BINDINGS_DART_NATIVES_H_
#endif // SKY_ENGINE_BINDINGS_DART_RUNTIME_HOOKS_H_
......@@ -5,45 +5,41 @@
#include "sky/engine/bindings/dart_ui.h"
#include "gen/sky/bindings/DartGlobal.h"
#include "sky/engine/bindings/builtin.h"
#include "sky/engine/bindings/dart_natives.h"
#include "sky/engine/core/script/dom_dart_state.h"
#include "sky/engine/core/view/View.h"
#include "sky/engine/bindings/dart_runtime_hooks.h"
#include "sky/engine/tonic/dart_converter.h"
#include "sky/engine/tonic/dart_error.h"
namespace blink {
namespace {
DartUI::DartUI(DOMDartState* dart_state) {
library_.Set(dart_state, Builtin::LoadAndCheckLibrary(Builtin::kUILibrary));
}
DartUI::~DartUI() {
}
static DartLibraryNatives* g_natives;
Dart_NativeFunction DartUI::NativeLookup(Dart_Handle name,
Dart_NativeFunction GetNativeFunction(Dart_Handle name,
int argument_count,
bool* auto_setup_scope) {
if (auto result = DartNatives::NativeLookup(name, argument_count, auto_setup_scope))
if (auto result = g_natives->GetNativeFunction(name,
argument_count,
auto_setup_scope))
return result;
return skySnapshotResolver(name, argument_count, auto_setup_scope);
}
const uint8_t* DartUI::NativeSymbol(Dart_NativeFunction native_function) {
if (auto result = DartNatives::NativeSymbol(native_function))
const uint8_t* GetSymbol(Dart_NativeFunction native_function) {
if (auto result = g_natives->GetSymbol(native_function))
return result;
return skySnapshotSymbolizer(native_function);
}
void DartUI::InstallView(View* view) {
CHECK(!LogIfError(
Dart_SetField(library_.value(), ToDart("view"), ToDart(view))));
}
} // namespace
void DartUI::InitForIsolate() {
if (!g_natives) {
g_natives = new DartLibraryNatives();
DartRuntimeHooks::RegisterNatives(g_natives);
}
Dart_Handle DartUI::GetClassByName(const char* class_name) {
Dart_Handle name_handle = ToDart(class_name);
Dart_Handle class_handle = Dart_GetType(library_.value(), name_handle, 0, nullptr);
DCHECK(!Dart_IsError(class_handle)) << class_name << ": " << Dart_GetError(class_handle);
return class_handle;
DART_CHECK_VALID(Dart_SetNativeResolver(
Dart_LookupLibrary(ToDart("dart:ui")), GetNativeFunction, GetSymbol));
}
} // namespace blink
......@@ -6,33 +6,15 @@
#define SKY_ENGINE_BINDINGS_DART_UI_H_
#include "base/macros.h"
#include "dart/runtime/include/dart_api.h"
#include "sky/engine/tonic/dart_class_provider.h"
#include "sky/engine/tonic/dart_persistent_value.h"
namespace blink {
class DOMDartState;
class View;
class DartUI : public DartClassProvider {
class DartUI {
public:
explicit DartUI(DOMDartState* dart_state);
~DartUI();
static Dart_NativeFunction NativeLookup(Dart_Handle name,
int argument_count,
bool* auto_setup_scope);
static const uint8_t* NativeSymbol(Dart_NativeFunction native_function);
void InstallView(View* view);
// DartClassProvider:
Dart_Handle GetClassByName(const char* class_name) override;
static void InitForIsolate();
private:
DartPersistentValue library_;
DISALLOW_COPY_AND_ASSIGN(DartUI);
DISALLOW_IMPLICIT_CONSTRUCTORS(DartUI);
};
} // namespace blink
......
......@@ -14,9 +14,7 @@
#include "dart/runtime/include/dart_api.h"
#include "mojo/public/c/system/core.h"
#include "mojo/public/cpp/system/core.h"
#include "sky/engine/bindings/builtin.h"
#include "sky/engine/tonic/dart_converter.h"
#include "sky/engine/tonic/dart_builtin.h"
namespace blink {
......
......@@ -5,7 +5,6 @@
#include "sky/engine/core/painting/CanvasColor.h"
#include "sky/engine/core/script/dom_dart_state.h"
#include "sky/engine/tonic/dart_builtin.h"
#include "sky/engine/tonic/dart_error.h"
#include "sky/engine/tonic/dart_value.h"
......
......@@ -11,13 +11,14 @@
#include "dart/runtime/include/dart_tools_api.h"
#include "mojo/data_pipe_utils/data_pipe_utils.h"
#include "mojo/public/cpp/system/data_pipe.h"
#include "sky/engine/bindings/builtin.h"
#include "sky/engine/bindings/dart_natives.h"
#include "sky/engine/bindings/dart_mojo_internal.h"
#include "sky/engine/bindings/dart_runtime_hooks.h"
#include "sky/engine/bindings/dart_ui.h"
#include "sky/engine/core/script/dart_debugger.h"
#include "sky/engine/core/script/dart_init.h"
#include "sky/engine/core/script/dart_service_isolate.h"
#include "sky/engine/core/script/dom_dart_state.h"
#include "sky/engine/core/view/View.h"
#include "sky/engine/public/platform/Platform.h"
#include "sky/engine/tonic/dart_api_scope.h"
#include "sky/engine/tonic/dart_class_library.h"
......@@ -25,11 +26,13 @@
#include "sky/engine/tonic/dart_error.h"
#include "sky/engine/tonic/dart_gc_controller.h"
#include "sky/engine/tonic/dart_invoke.h"
#include "sky/engine/tonic/dart_io.h"
#include "sky/engine/tonic/dart_isolate_scope.h"
#include "sky/engine/tonic/dart_library_loader.h"
#include "sky/engine/tonic/dart_snapshot_loader.h"
#include "sky/engine/tonic/dart_state.h"
#include "sky/engine/tonic/dart_wrappable.h"
#include "sky/engine/wtf/MakeUnique.h"
namespace blink {
namespace {
......@@ -48,7 +51,7 @@ void CallHandleMessage(base::WeakPtr<DartState> dart_state) {
return;
DartIsolateScope scope(dart_state->isolate());
DartApiScope api_scope;
DartApiScope dart_api_scope;
LogIfError(Dart_HandleMessage());
}
......@@ -148,15 +151,14 @@ void DartController::CreateIsolateFor(PassOwnPtr<DOMDartState> state) {
CHECK(!LogIfError(Dart_SetLibraryTagHandler(DartLibraryTagHandler)));
{
DartApiScope apiScope;
Builtin::SetNativeResolver(Builtin::kUILibrary);
Builtin::SetNativeResolver(Builtin::kMojoInternalLibrary);
Builtin::SetNativeResolver(Builtin::kIOLibrary);
DartNatives::Init(DartNatives::MainIsolate);
DartApiScope dart_api_scope;
DartIO::InitForIsolate();
DartUI::InitForIsolate();
DartMojoInternal::InitForIsolate();
DartRuntimeHooks::Install(DartRuntimeHooks::MainIsolate);
dart_ui_ = adoptPtr(new DartUI(dart_state()));
dart_state()->class_library().set_provider(dart_ui_.get());
dart_state()->class_library().set_provider(
WTF::MakeUnique<DartClassProvider>(dart_state(), "dart:ui"));
EnsureHandleWatcherStarted();
}
......@@ -167,7 +169,8 @@ void DartController::InstallView(View* view) {
DartIsolateScope isolate_scope(dart_state()->isolate());
DartApiScope dart_api_scope;
dart_ui_->InstallView(view);
Dart_Handle library = Dart_LookupLibrary(ToDart("dart:ui"));
CHECK(!LogIfError(Dart_SetField(library, ToDart("view"), ToDart(view))));
}
static void DartController_DartStreamConsumer(
......
......@@ -13,8 +13,8 @@
#include "dart/runtime/bin/embedded_dart_io.h"
#include "dart/runtime/include/dart_mirrors_api.h"
#include "gen/sky/platform/RuntimeEnabledFeatures.h"
#include "sky/engine/bindings/builtin.h"
#include "sky/engine/bindings/dart_natives.h"
#include "sky/engine/bindings/dart_mojo_internal.h"
#include "sky/engine/bindings/dart_runtime_hooks.h"
#include "sky/engine/bindings/dart_ui.h"
#include "sky/engine/core/script/dart_debugger.h"
#include "sky/engine/core/script/dart_service_isolate.h"
......@@ -25,6 +25,7 @@
#include "sky/engine/tonic/dart_error.h"
#include "sky/engine/tonic/dart_gc_controller.h"
#include "sky/engine/tonic/dart_invoke.h"
#include "sky/engine/tonic/dart_io.h"
#include "sky/engine/tonic/dart_isolate_scope.h"
#include "sky/engine/tonic/dart_library_loader.h"
#include "sky/engine/tonic/dart_snapshot_loader.h"
......@@ -46,8 +47,7 @@ void EnsureHandleWatcherStarted() {
// TODO(dart): Call Dart_Cleanup (ensure the handle watcher isolate is closed)
// during shutdown.
Dart_Handle mojo_core_lib =
Builtin::LoadAndCheckLibrary(Builtin::kMojoInternalLibrary);
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,
......@@ -141,11 +141,11 @@ Dart_Isolate IsolateCreateCallback(const char* script_uri,
CHECK(Dart_IsServiceIsolate(isolate));
CHECK(!LogIfError(Dart_SetLibraryTagHandler(DartLibraryTagHandler)));
{
DartApiScope apiScope;
Builtin::SetNativeResolver(Builtin::kUILibrary);
Builtin::SetNativeResolver(Builtin::kMojoInternalLibrary);
Builtin::SetNativeResolver(Builtin::kIOLibrary);
DartNatives::Init(DartNatives::DartIOIsolate);
DartApiScope dart_api_scope;
DartIO::InitForIsolate();
DartUI::InitForIsolate();
DartMojoInternal::InitForIsolate();
DartRuntimeHooks::Install(DartRuntimeHooks::DartIOIsolate);
// Start the handle watcher from the service isolate so it isn't available
// for debugging or general Observatory interaction.
EnsureHandleWatcherStarted();
......@@ -175,10 +175,10 @@ Dart_Isolate IsolateCreateCallback(const char* script_uri,
CHECK(!LogIfError(Dart_SetLibraryTagHandler(DartLibraryTagHandler)));
{
DartApiScope apiScope;
Builtin::SetNativeResolver(Builtin::kUILibrary);
Builtin::SetNativeResolver(Builtin::kMojoInternalLibrary);
Builtin::SetNativeResolver(Builtin::kIOLibrary);
DartApiScope dart_api_scope;
DartIO::InitForIsolate();
DartUI::InitForIsolate();
DartMojoInternal::InitForIsolate();
if (!script_uri)
CreateEmptyRootLibraryIfNeeded();
......
......@@ -7,6 +7,7 @@
#include "base/logging.h"
#include "dart/runtime/include/dart_api.h"
#include "sky/engine/tonic/dart_error.h"
#include "sky/engine/tonic/dart_library_natives.h"
#include "sky/engine/tonic/dart_string.h"
#define RETURN_ERROR_HANDLE(handle) \
......@@ -38,6 +39,24 @@ namespace mojo {
}
namespace blink {
namespace {
static Dart_LibraryTagHandler g_embedder_tag_handler;
static DartLibraryNatives* g_natives;
Dart_NativeFunction GetNativeFunction(Dart_Handle name,
int argument_count,
bool* auto_setup_scope) {
CHECK(g_natives);
return g_natives->GetNativeFunction(name, argument_count, auto_setup_scope);
}
const uint8_t* GetSymbol(Dart_NativeFunction native_function) {
CHECK(g_natives);
return g_natives->GetSymbol(native_function);
}
} // namespace
class Resources {
public:
......@@ -96,28 +115,6 @@ void DartServiceIsolate::Shutdown(Dart_NativeArguments args) {
// NO-OP.
}
DartBuiltin::Natives DartServiceIsolate::native_entries_[] = {
{"ServiceIsolate_TriggerResourceLoad", TriggerResourceLoad, 0 },
{"ServiceIsolate_NotifyServerState", NotifyServerState, 2 },
{"ServiceIsolate_Shutdown", Shutdown, 0 },
};
Dart_NativeFunction DartServiceIsolate::NativeResolver(Dart_Handle name,
int argument_count,
bool* auto_setup_scope) {
CHECK(builtins_);
return builtins_->Resolver(name, argument_count, auto_setup_scope);
}
const uint8_t* DartServiceIsolate::NativeSymbolizer(
Dart_NativeFunction native_function) {
CHECK(builtins_);
return builtins_->Symbolizer(native_function);
}
Dart_LibraryTagHandler DartServiceIsolate::embedder_tag_handler_ = nullptr;
DartBuiltin* DartServiceIsolate::builtins_ = nullptr;
bool DartServiceIsolate::Startup(std::string server_ip,
intptr_t server_port,
Dart_LibraryTagHandler embedder_tag_handler,
......@@ -126,13 +123,18 @@ bool DartServiceIsolate::Startup(std::string server_ip,
CHECK(isolate);
// Remember the embedder's library tag handler.
embedder_tag_handler_ = embedder_tag_handler;
CHECK(embedder_tag_handler_);
g_embedder_tag_handler = embedder_tag_handler;
CHECK(g_embedder_tag_handler);
// Setup native entries.
builtins_ =
new DartBuiltin(&DartServiceIsolate::native_entries_[0],
arraysize(native_entries_));
if (!g_natives) {
g_natives = new DartLibraryNatives();
g_natives->Register({
{"ServiceIsolate_TriggerResourceLoad", TriggerResourceLoad, 0, true },
{"ServiceIsolate_NotifyServerState", NotifyServerState, 2, true },
{"ServiceIsolate_Shutdown", Shutdown, 0, true },
});
}
Dart_Handle result;
......@@ -143,7 +145,7 @@ bool DartServiceIsolate::Startup(std::string server_ip,
DCHECK(library != Dart_Null());
SHUTDOWN_ON_ERROR(library);
// Setup native entry resolution.
result = Dart_SetNativeResolver(library, NativeResolver, NativeSymbolizer);
result = Dart_SetNativeResolver(library, GetNativeFunction, GetSymbol);
SHUTDOWN_ON_ERROR(result);
// Finalize loading.
......@@ -285,7 +287,7 @@ Dart_Handle DartServiceIsolate::LibraryTagHandler(Dart_LibraryTag tag,
}
if (tag == Dart_kImportTag) {
// Embedder handles all requests for external libraries.
return embedder_tag_handler_(tag, library, url);
return g_embedder_tag_handler(tag, library, url);
}
DCHECK((tag == Dart_kSourceTag) || (tag == Dart_kCanonicalizeUrl));
if (tag == Dart_kCanonicalizeUrl) {
......
......@@ -8,7 +8,6 @@
#include <string>
#include "include/dart_api.h"
#include "sky/engine/tonic/dart_builtin.h"
namespace blink {
......@@ -26,13 +25,6 @@ class DartServiceIsolate {
static void TriggerResourceLoad(Dart_NativeArguments args);
static void NotifyServerState(Dart_NativeArguments args);
static void Shutdown(Dart_NativeArguments args);
// Native entry resolution.
static Dart_NativeFunction NativeResolver(Dart_Handle name,
int argument_count,
bool* auto_setup_scope);
static const uint8_t* NativeSymbolizer(Dart_NativeFunction native_function);
static DartBuiltin::Natives native_entries_[];
static DartBuiltin* builtins_;
// Script loading.
static Dart_Handle GetSource(const char* name);
......@@ -44,11 +36,8 @@ class DartServiceIsolate {
// Observatory resource loading.
static Dart_Handle LoadResources(Dart_Handle library);
static Dart_Handle LoadResource(Dart_Handle library, const char* name);
static Dart_LibraryTagHandler embedder_tag_handler_;
};
} // namespace blink
#endif // SKY_ENGINE_CORE_SCRIPT_DART_SERVICE_ISOLATE_H_
......@@ -4,7 +4,6 @@
#include "sky/engine/core/script/dom_dart_state.h"
#include "sky/engine/tonic/dart_builtin.h"
#include "sky/engine/tonic/dart_converter.h"
namespace blink {
......@@ -32,8 +31,8 @@ void DOMDartState::DidSetIsolate() {
dy_handle_.Set(this, ToDart("_dy"));
value_handle_.Set(this, ToDart("_value"));
Dart_Handle sky_library = DartBuiltin::LookupLibrary("dart:ui");
color_class_.Set(this, Dart_GetType(sky_library, ToDart("Color"), 0, 0));
Dart_Handle library = Dart_LookupLibrary(ToDart("dart:ui"));
color_class_.Set(this, Dart_GetType(library, ToDart("Color"), 0, 0));
}
} // namespace blink
......@@ -5,8 +5,6 @@
source_set("tonic") {
sources = [
"dart_api_scope.h",
"dart_builtin.cc",
"dart_builtin.h",
"dart_class_library.cc",
"dart_class_library.h",
"dart_class_provider.cc",
......@@ -27,10 +25,14 @@ source_set("tonic") {
"dart_gc_visitor.h",
"dart_invoke.cc",
"dart_invoke.h",
"dart_io.cc",
"dart_io.h",
"dart_isolate_scope.cc",
"dart_isolate_scope.h",
"dart_library_loader.cc",
"dart_library_loader.h",
"dart_library_natives.cc",
"dart_library_natives.h",
"dart_library_provider.cc",
"dart_library_provider.h",
"dart_persistent_value.cc",
......@@ -63,6 +65,7 @@ source_set("tonic") {
deps = [
"//base",
"//dart/runtime/bin:embedded_dart_io",
"//mojo/data_pipe_utils",
"//mojo/public/cpp/system",
"//sky/engine/wtf",
......
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "sky/engine/tonic/dart_builtin.h"
#include <stdlib.h>
#include <string.h>
#include "base/logging.h"
#include "sky/engine/tonic/dart_converter.h"
namespace blink {
DartBuiltin::DartBuiltin(const Natives* natives, size_t count)
: natives_(natives), count_(count) {
}
DartBuiltin::~DartBuiltin() {
}
Dart_NativeFunction DartBuiltin::Resolver(Dart_Handle name,
int argument_count,
bool* auto_setup_scope) const {
const char* function_name = nullptr;
Dart_Handle result = Dart_StringToCString(name, &function_name);
DART_CHECK_VALID(result);
DCHECK(function_name != nullptr);
DCHECK(auto_setup_scope != nullptr);
*auto_setup_scope = true;
for (size_t i = 0; i < count_; ++i) {
const Natives& entry = natives_[i];
if (!strcmp(function_name, entry.name) &&
(entry.argument_count == argument_count)) {
return entry.function;
}
}
return nullptr;
}
const uint8_t* DartBuiltin::Symbolizer(Dart_NativeFunction native_function) const {
for (size_t i = 0; i < count_; ++i) {
const Natives& entry = natives_[i];
if (entry.function == native_function)
return reinterpret_cast<const uint8_t*>(entry.name);
}
return nullptr;
}
Dart_Handle DartBuiltin::LookupLibrary(const char* name) {
Dart_Handle library = Dart_LookupLibrary(ToDart(name));
DCHECK(!Dart_IsError(library));
return library;
}
} // namespace blink
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef SKY_ENGINE_TONIC_DART_BUILTIN_H_
#define SKY_ENGINE_TONIC_DART_BUILTIN_H_
#include "base/macros.h"
#include "dart/runtime/include/dart_api.h"
namespace blink {
class DartBuiltin {
public:
struct Natives {
const char* name;
Dart_NativeFunction function;
int argument_count;
};
DartBuiltin(const Natives* natives, size_t count);
~DartBuiltin();
Dart_NativeFunction Resolver(Dart_Handle name,
int argument_count,
bool* auto_setup_scope) const;
const uint8_t* Symbolizer(Dart_NativeFunction native_function) const;
// Helper around Dart_LookupLibrary.
static Dart_Handle LookupLibrary(const char* name);
private:
const Natives* natives_;
size_t count_;
DISALLOW_COPY_AND_ASSIGN(DartBuiltin);
};
} // namespace blink
#endif // SKY_ENGINE_TONIC_DART_BUILTIN_H_
......@@ -9,7 +9,7 @@
namespace blink {
DartClassLibrary::DartClassLibrary() : provider_(nullptr) {
DartClassLibrary::DartClassLibrary() {
}
DartClassLibrary::~DartClassLibrary() {
......
......@@ -5,7 +5,9 @@
#ifndef SKY_ENGINE_TONIC_DART_CLASS_LIBRARY_H_
#define SKY_ENGINE_TONIC_DART_CLASS_LIBRARY_H_
#include <memory>
#include <unordered_map>
#include "base/macros.h"
#include "dart/runtime/include/dart_api.h"
#include "sky/engine/tonic/dart_class_provider.h"
......@@ -18,11 +20,14 @@ class DartClassLibrary {
explicit DartClassLibrary();
~DartClassLibrary();
void set_provider(DartClassProvider* provider) { provider_ = provider; }
void set_provider(std::unique_ptr<DartClassProvider> provider) {
provider_ = std::move(provider);
}
Dart_PersistentHandle GetClass(const DartWrapperInfo& info);
private:
DartClassProvider* provider_;
std::unique_ptr<DartClassProvider> provider_;
std::unordered_map<const DartWrapperInfo*, Dart_PersistentHandle> cache_;
DISALLOW_COPY_AND_ASSIGN(DartClassLibrary);
......
......@@ -4,9 +4,25 @@
#include "sky/engine/tonic/dart_class_provider.h"
#include "sky/engine/tonic/dart_converter.h"
#include "sky/engine/tonic/dart_error.h"
#include "sky/engine/tonic/dart_state.h"
namespace blink {
DartClassProvider::DartClassProvider(DartState* dart_state,
const char* class_name) {
library_.Set(dart_state, Dart_LookupLibrary(ToDart(class_name)));
}
DartClassProvider::~DartClassProvider() {
}
Dart_Handle DartClassProvider::GetClassByName(const char* class_name) {
Dart_Handle name_handle = ToDart(class_name);
Dart_Handle class_handle = Dart_GetType(library_.value(), name_handle, 0, nullptr);
DCHECK(!Dart_IsError(class_handle)) << class_name << ": " << Dart_GetError(class_handle);
return class_handle;
}
} // namespace blink
......@@ -5,16 +5,24 @@
#ifndef SKY_ENGINE_TONIC_DART_CLASS_PROVIDER_H_
#define SKY_ENGINE_TONIC_DART_CLASS_PROVIDER_H_
#include "base/macros.h"
#include "dart/runtime/include/dart_api.h"
#include "sky/engine/tonic/dart_persistent_value.h"
namespace blink {
class DartState;
class DartClassProvider {
public:
virtual Dart_Handle GetClassByName(const char* class_name) = 0;
DartClassProvider(DartState* dart_state, const char* library_name);
~DartClassProvider();
protected:
virtual ~DartClassProvider();
Dart_Handle GetClassByName(const char* class_name);
private:
DartPersistentValue library_;
DISALLOW_COPY_AND_ASSIGN(DartClassProvider);
};
} // namespace blink
......
......@@ -5,7 +5,6 @@
#include "sky/engine/tonic/dart_exception_factory.h"
#include "sky/engine/tonic/dart_converter.h"
#include "sky/engine/tonic/dart_builtin.h"
namespace blink {
......@@ -109,7 +108,7 @@ Dart_Handle DartExceptionFactory::CreateException(const std::string& class_name,
Dart_Handle DartExceptionFactory::CreateException(const std::string& class_name,
Dart_Handle message) {
if (core_library_.is_empty()) {
Dart_Handle library = DartBuiltin::LookupLibrary("dart:core");
Dart_Handle library = Dart_LookupLibrary(ToDart("dart:core"));
core_library_.Set(dart_state_, library);
}
......
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "sky/engine/tonic/dart_io.h"
#include "dart/runtime/bin/io_natives.h"
#include "dart/runtime/include/dart_api.h"
#include "sky/engine/tonic/dart_converter.h"
namespace blink {
void DartIO::InitForIsolate() {
DART_CHECK_VALID(Dart_SetNativeResolver(
Dart_LookupLibrary(ToDart("dart:io")),
dart::bin::IONativeLookup,
dart::bin::IONativeSymbol));
}
} // namespace blink
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef SKY_ENGINE_TONIC_DART_IO_H_
#define SKY_ENGINE_TONIC_DART_IO_H_
#include "base/macros.h"
namespace blink {
class DartIO {
public:
static void InitForIsolate();
private:
DISALLOW_IMPLICIT_CONSTRUCTORS(DartIO);
};
} // namespace blink
#endif // SKY_ENGINE_TONIC_DART_IO_H_
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "sky/engine/tonic/dart_library_natives.h"
#include "sky/engine/tonic/dart_converter.h"
namespace blink {
DartLibraryNatives::DartLibraryNatives() {
}
DartLibraryNatives::~DartLibraryNatives() {
}
void DartLibraryNatives::Register(std::initializer_list<Entry> entries) {
for (const Entry& entry : entries) {
symbols_.emplace(entry.native_function, entry.symbol);
entries_.emplace(entry.symbol, entry);
}
}
Dart_NativeFunction DartLibraryNatives::GetNativeFunction(
Dart_Handle name, int argument_count, bool* auto_setup_scope) {
std::string name_string = StdStringFromDart(name);
auto it = entries_.find(name_string);
if (it == entries_.end())
return nullptr;
const Entry& entry = it->second;
if (entry.argument_count != argument_count)
return nullptr;
*auto_setup_scope = entry.auto_setup_scope;
return entry.native_function;
}
const uint8_t* DartLibraryNatives::GetSymbol(
Dart_NativeFunction native_function) {
auto it = symbols_.find(native_function);
if (it == symbols_.end())
return nullptr;
return reinterpret_cast<const uint8_t*>(it->second);
}
} // namespace blink
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef SKY_ENGINE_TONIC_DART_LIBRARY_NATIVES_H_
#define SKY_ENGINE_TONIC_DART_LIBRARY_NATIVES_H_
#include <string>
#include <unordered_map>
#include <initializer_list>
#include "base/logging.h"
#include "dart/runtime/include/dart_api.h"
namespace blink {
class DartLibraryNatives {
public:
DartLibraryNatives();
~DartLibraryNatives();
struct Entry {
const char* symbol;
Dart_NativeFunction native_function;
int argument_count;
bool auto_setup_scope;
};
void Register(std::initializer_list<Entry> entries);
Dart_NativeFunction GetNativeFunction(Dart_Handle name,
int argument_count,
bool* auto_setup_scope);
const uint8_t* GetSymbol(Dart_NativeFunction native_function);
private:
std::unordered_map<std::string, Entry> entries_;
std::unordered_map<Dart_NativeFunction, const char*> symbols_;
DISALLOW_COPY_AND_ASSIGN(DartLibraryNatives);
};
} // namespace blink
#endif // SKY_ENGINE_TONIC_DART_LIBRARY_NATIVES_H_
......@@ -85,6 +85,7 @@ component("wtf") {
"Locker.h",
"MainThread.cpp",
"MainThread.h",
"MakeUnique.h",
"MallocZoneSupport.h",
"MathExtras.h",
"MessageQueue.h",
......
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// This is almost the source from N3656 ("make_unique (Revision 1)";
// https://isocpp.org/files/papers/N3656.txt) almost verbatim, so that we have a
// "make_unique" to use until we can use C++14. The following changes have been
// made:
// - It's called |MakeUnique| instead of |make_unique|.
// - It's in the |WTF| namespace instead of |std|; this also
// necessitates adding some |std::|s.
// - It's been formatted.
#ifndef SKY_ENGINE_WTF_MAKE_UNIQUE_H_
#define SKY_ENGINE_WTF_MAKE_UNIQUE_H_
#include <cstddef>
#include <memory>
#include <type_traits>
#include <utility>
namespace WTF {
template <class T>
struct _Unique_if {
typedef std::unique_ptr<T> _Single_object;
};
template <class T>
struct _Unique_if<T[]> {
typedef std::unique_ptr<T[]> _Unknown_bound;
};
template <class T, size_t N>
struct _Unique_if<T[N]> {
typedef void _Known_bound;
};
template <class T, class... Args>
typename _Unique_if<T>::_Single_object MakeUnique(Args&&... args) {
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
template <class T>
typename _Unique_if<T>::_Unknown_bound MakeUnique(size_t n) {
typedef typename std::remove_extent<T>::type U;
return std::unique_ptr<T>(new U[n]());
}
template <class T, class... Args>
typename _Unique_if<T>::_Known_bound MakeUnique(Args&&...) = delete;
} // namespace WTF
#endif // SKY_ENGINE_WTF_MAKE_UNIQUE_H_
......@@ -8,9 +8,9 @@
#include "mojo/public/cpp/application/connect.h"
#include "mojo/public/cpp/bindings/array.h"
#include "services/asset_bundle/asset_unpacker_impl.h"
#include "sky/engine/tonic/dart_builtin.h"
#include "sky/engine/tonic/dart_converter.h"
#include "sky/engine/tonic/dart_error.h"
#include "sky/engine/tonic/dart_library_natives.h"
#include "sky/engine/tonic/dart_state.h"
using namespace blink;
......@@ -48,44 +48,45 @@ void TakeServiceRegistry(Dart_NativeArguments args) {
Dart_SetIntegerReturnValue(args, 0);
}
const DartBuiltin::Natives kNativeFunctions[] = {
{"takeRootBundleHandle", TakeRootBundleHandle, 0},
{"takeServiceRegistry", TakeServiceRegistry, 0},
{"takeServicesProvidedByEmbedder", TakeServicesProvidedByEmbedder, 0},
{"takeServicesProvidedToEmbedder", TakeServicesProvidedToEmbedder, 0},
{"takeShellProxyHandle", TakeShellProxyHandle, 0},
};
const DartBuiltin& GetBuiltin() {
static DartBuiltin& builtin = *new DartBuiltin(kNativeFunctions,
arraysize(kNativeFunctions));
return builtin;
static DartLibraryNatives* g_natives;
void EnsureNatives() {
if (g_natives)
return;
g_natives = new DartLibraryNatives();
g_natives->Register({
{"takeRootBundleHandle", TakeRootBundleHandle, 0, true},
{"takeServiceRegistry", TakeServiceRegistry, 0, true},
{"takeServicesProvidedByEmbedder", TakeServicesProvidedByEmbedder, 0, true},
{"takeServicesProvidedToEmbedder", TakeServicesProvidedToEmbedder, 0, true},
{"takeShellProxyHandle", TakeShellProxyHandle, 0, true},
});
}
Dart_NativeFunction Resolver(Dart_Handle name,
int argument_count,
bool* auto_setup_scope) {
return GetBuiltin().Resolver(name, argument_count, auto_setup_scope);
Dart_NativeFunction GetNativeFunction(Dart_Handle name,
int argument_count,
bool* auto_setup_scope) {
return g_natives->GetNativeFunction(name, argument_count, auto_setup_scope);
}
const uint8_t* Symbolizer(Dart_NativeFunction native_function) {
return GetBuiltin().Symbolizer(native_function);
const uint8_t* GetSymbol(Dart_NativeFunction native_function) {
return g_natives->GetSymbol(native_function);
}
const char kLibraryName[] = "dart:ui_internals";
} // namespace
void Internals::Create(Dart_Isolate isolate,
mojo::ServiceProviderPtr service_provider,
mojo::asset_bundle::AssetBundlePtr root_bundle) {
EnsureNatives();
DartState* state = DartState::From(isolate);
state->SetUserData(&kInternalsKey, new Internals(service_provider.Pass(),
root_bundle.Pass()));
Dart_Handle library =
Dart_LookupLibrary(Dart_NewStringFromCString(kLibraryName));
Dart_Handle library = Dart_LookupLibrary(ToDart("dart:ui_internals"));
CHECK(!LogIfError(library));
CHECK(!LogIfError(Dart_SetNativeResolver(library, Resolver, Symbolizer)));
CHECK(!LogIfError(Dart_SetNativeResolver(
library, GetNativeFunction, GetSymbol)));
}
Internals::Internals(mojo::ServiceProviderPtr platform_service_provider,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册