| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "sky/engine/config.h" |
| 6 #include "sky/engine/bindings2/builtin_natives.h" |
| 7 |
| 8 #include <stdio.h> |
| 9 #include <stdlib.h> |
| 10 #include <string.h> |
| 11 |
| 12 #include "base/bind.h" |
| 13 #include "base/logging.h" |
| 14 #include "base/macros.h" |
| 15 #include "dart/runtime/include/dart_api.h" |
| 16 #include "sky/engine/bindings2/builtin.h" |
| 17 #include "sky/engine/core/dom/Microtask.h" |
| 18 #include "sky/engine/core/script/dom_dart_state.h" |
| 19 #include "sky/engine/tonic/dart_api_scope.h" |
| 20 #include "sky/engine/tonic/dart_error.h" |
| 21 #include "sky/engine/tonic/dart_isolate_scope.h" |
| 22 #include "sky/engine/tonic/dart_state.h" |
| 23 #include "sky/engine/tonic/dart_value.h" |
| 24 #include "sky/engine/wtf/text/WTFString.h" |
| 25 |
| 26 namespace blink { |
| 27 |
| 28 #define REGISTER_FUNCTION(name, count) \ |
| 29 { "" #name, name, count }, |
| 30 #define DECLARE_FUNCTION(name, count) \ |
| 31 extern void name(Dart_NativeArguments args); |
| 32 |
| 33 // Lists the native functions implementing basic functionality in |
| 34 // the Mojo embedder dart, such as printing, and file I/O. |
| 35 #define BUILTIN_NATIVE_LIST(V) \ |
| 36 V(Logger_PrintString, 1) \ |
| 37 V(ScheduleMicrotask, 1) \ |
| 38 V(Timer_create, 3) \ |
| 39 V(Timer_cancel, 1) |
| 40 |
| 41 BUILTIN_NATIVE_LIST(DECLARE_FUNCTION); |
| 42 |
| 43 static struct NativeEntries { |
| 44 const char* name; |
| 45 Dart_NativeFunction function; |
| 46 int argument_count; |
| 47 } BuiltinEntries[] = {BUILTIN_NATIVE_LIST(REGISTER_FUNCTION)}; |
| 48 |
| 49 Dart_NativeFunction BuiltinNatives::NativeLookup(Dart_Handle name, |
| 50 int argument_count, |
| 51 bool* auto_setup_scope) { |
| 52 const char* function_name = nullptr; |
| 53 Dart_Handle result = Dart_StringToCString(name, &function_name); |
| 54 DART_CHECK_VALID(result); |
| 55 DCHECK(function_name != nullptr); |
| 56 DCHECK(auto_setup_scope != nullptr); |
| 57 *auto_setup_scope = true; |
| 58 size_t num_entries = arraysize(BuiltinEntries); |
| 59 for (size_t i = 0; i < num_entries; i++) { |
| 60 const struct NativeEntries& entry = BuiltinEntries[i]; |
| 61 if (!strcmp(function_name, entry.name) && |
| 62 (entry.argument_count == argument_count)) { |
| 63 return entry.function; |
| 64 } |
| 65 } |
| 66 return nullptr; |
| 67 } |
| 68 |
| 69 const uint8_t* BuiltinNatives::NativeSymbol(Dart_NativeFunction native_function)
{ |
| 70 size_t num_entries = arraysize(BuiltinEntries); |
| 71 for (size_t i = 0; i < num_entries; i++) { |
| 72 const struct NativeEntries& entry = BuiltinEntries[i]; |
| 73 if (entry.function == native_function) { |
| 74 return reinterpret_cast<const uint8_t*>(entry.name); |
| 75 } |
| 76 } |
| 77 return nullptr; |
| 78 } |
| 79 |
| 80 static Dart_Handle GetClosure(Dart_Handle builtin_library, const char* name) { |
| 81 Dart_Handle getter_name = ToDart(name); |
| 82 Dart_Handle closure = Dart_Invoke(builtin_library, getter_name, 0, nullptr); |
| 83 DART_CHECK_VALID(closure); |
| 84 return closure; |
| 85 } |
| 86 |
| 87 static void InitDartInternal(Dart_Handle builtin_library) { |
| 88 Dart_Handle print = GetClosure(builtin_library, "_getPrintClosure"); |
| 89 Dart_Handle timer = GetClosure(builtin_library, "_getCreateTimerClosure"); |
| 90 |
| 91 Dart_Handle internal_library = DartBuiltin::LookupLibrary("dart:_internal") |
| 92 |
| 93 DART_CHECK_VALID(Dart_SetField( |
| 94 internal_library, ToDart("_printClosure"), print)); |
| 95 |
| 96 Dart_Handle vm_hooks_name = ToDart("VMLibraryHooks"); |
| 97 Dart_Handle vm_hooks = Dart_GetClass(internal_library, vm_hooks_name); |
| 98 DART_CHECK_VALID(vm_hooks); |
| 99 Dart_Handle timer_name = ToDart("timerFactory"); |
| 100 DART_CHECK_VALID(Dart_SetField(vm_hooks, timer_name, timer)); |
| 101 } |
| 102 |
| 103 static void InitAsync(Dart_Handle builtin_library) { |
| 104 Dart_Handle schedule_microtask = |
| 105 GetClosure(builtin_library, "_getScheduleMicrotaskClosure"); |
| 106 Dart_Handle internal_library = DartBuiltin::LookupLibrary("dart:async") |
| 107 Dart_Handle set_schedule_microtask = ToDart("_setScheduleImmediateClosure"); |
| 108 DART_CHECK_VALID(Dart_Invoke(async_library, set_schedule_microtask, 1, |
| 109 &schedule_microtask)); |
| 110 } |
| 111 |
| 112 void BuiltinNatives::Init() { |
| 113 Dart_Handle builtin = Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary); |
| 114 DART_CHECK_VALID(builtin); |
| 115 InitDartInternal(builtin); |
| 116 InitAsync(builtin); |
| 117 } |
| 118 |
| 119 // Implementation of native functions which are used for some |
| 120 // test/debug functionality in standalone dart mode. |
| 121 void Logger_PrintString(Dart_NativeArguments args) { |
| 122 intptr_t length = 0; |
| 123 uint8_t* chars = nullptr; |
| 124 Dart_Handle str = Dart_GetNativeArgument(args, 0); |
| 125 Dart_Handle result = Dart_StringToUTF8(str, &chars, &length); |
| 126 if (Dart_IsError(result)) { |
| 127 Dart_PropagateError(result); |
| 128 } else { |
| 129 |
| 130 String message(chars, length); |
| 131 // TODO(dart): Hook up to developer console (if/when that's a thing). |
| 132 #if OS(ANDROID) |
| 133 LOG(INFO) << "CONSOLE: " << message.utf8().data(); |
| 134 #else |
| 135 printf("CONSOLE: %s\n", message.utf8().data()); |
| 136 fflush(stdout); |
| 137 #endif |
| 138 } |
| 139 } |
| 140 |
| 141 static void ExecuteMicrotask(base::WeakPtr<DartState> dart_state, |
| 142 RefPtr<DartValue> callback) { |
| 143 if (!dart_state) |
| 144 return; |
| 145 DartIsolateScope scope(dart_state->isolate()); |
| 146 DartApiScope api_scope; |
| 147 LogIfError(Dart_InvokeClosure(callback->dart_value(), 0, nullptr)); |
| 148 } |
| 149 |
| 150 void ScheduleMicrotask(Dart_NativeArguments args) { |
| 151 Dart_Handle closure = Dart_GetNativeArgument(args, 0); |
| 152 if (LogIfError(closure) || !Dart_IsClosure(closure)) |
| 153 return; |
| 154 DartState* dart_state = DartState::Current(); |
| 155 Microtask::enqueueMicrotask(base::Bind(&ExecuteMicrotask, |
| 156 dart_state->GetWeakPtr(), DartValue::Create(dart_state, closure))); |
| 157 } |
| 158 |
| 159 void Timer_create(Dart_NativeArguments args) { |
| 160 int64_t milliseconds = 0; |
| 161 DART_CHECK_VALID(Dart_GetNativeIntegerArgument(args, 0, &milliseconds)); |
| 162 Dart_Handle closure = Dart_GetNativeArgument(args, 1); |
| 163 DART_CHECK_VALID(closure); |
| 164 CHECK(Dart_IsClosure(closure)); |
| 165 bool repeating = false; |
| 166 DART_CHECK_VALID(Dart_GetNativeBooleanArgument(args, 2, &repeating)); |
| 167 |
| 168 DOMDartState* state = DOMDartState::Current(); |
| 169 int timer_id = DOMTimer::install(state->document(), |
| 170 ScheduledAction::Create(state, closure), |
| 171 milliseconds, |
| 172 !repeating); |
| 173 Dart_SetIntegerReturnValue(args, timer_id); |
| 174 } |
| 175 |
| 176 void Timer_cancel(Dart_NativeArguments args) { |
| 177 int64_t timer_id = 0; |
| 178 DART_CHECK_VALID(Dart_GetNativeIntegerArgument(args, 0, &timer_id)); |
| 179 |
| 180 DOMDartState* state = DOMDartState::Current(); |
| 181 DOMTimer::removeByID(state->document(), timer_id); |
| 182 } |
| 183 |
| 184 } // namespace blink |
| OLD | NEW |