| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "sky/engine/config.h" | 5 #include "sky/engine/config.h" |
| 6 #include "sky/engine/bindings/builtin_natives.h" | 6 #include "sky/engine/bindings/builtin_natives.h" |
| 7 | 7 |
| 8 #include <stdio.h> | 8 #include <stdio.h> |
| 9 #include <stdlib.h> | 9 #include <stdlib.h> |
| 10 #include <string.h> | 10 #include <string.h> |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 #define REGISTER_FUNCTION(name, count) \ | 29 #define REGISTER_FUNCTION(name, count) \ |
| 30 { "" #name, name, count }, | 30 { "" #name, name, count }, |
| 31 #define DECLARE_FUNCTION(name, count) \ | 31 #define DECLARE_FUNCTION(name, count) \ |
| 32 extern void name(Dart_NativeArguments args); | 32 extern void name(Dart_NativeArguments args); |
| 33 | 33 |
| 34 // Lists the native functions implementing basic functionality in | 34 // Lists the native functions implementing basic functionality in |
| 35 // the Mojo embedder dart, such as printing, and file I/O. | 35 // the Mojo embedder dart, such as printing, and file I/O. |
| 36 #define BUILTIN_NATIVE_LIST(V) \ | 36 #define BUILTIN_NATIVE_LIST(V) \ |
| 37 V(Logger_PrintString, 1) \ | 37 V(Logger_PrintString, 1) \ |
| 38 V(ScheduleMicrotask, 1) \ | 38 V(ScheduleMicrotask, 1) \ |
| 39 V(GetBaseURLString, 0) \ |
| 39 V(Timer_create, 3) \ | 40 V(Timer_create, 3) \ |
| 40 V(Timer_cancel, 1) | 41 V(Timer_cancel, 1) |
| 41 | 42 |
| 42 BUILTIN_NATIVE_LIST(DECLARE_FUNCTION); | 43 BUILTIN_NATIVE_LIST(DECLARE_FUNCTION); |
| 43 | 44 |
| 44 static struct NativeEntries { | 45 static struct NativeEntries { |
| 45 const char* name; | 46 const char* name; |
| 46 Dart_NativeFunction function; | 47 Dart_NativeFunction function; |
| 47 int argument_count; | 48 int argument_count; |
| 48 } BuiltinEntries[] = {BUILTIN_NATIVE_LIST(REGISTER_FUNCTION)}; | 49 } BuiltinEntries[] = {BUILTIN_NATIVE_LIST(REGISTER_FUNCTION)}; |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 DART_CHECK_VALID(Dart_SetField( | 95 DART_CHECK_VALID(Dart_SetField( |
| 95 internal_library, ToDart("_printClosure"), print)); | 96 internal_library, ToDart("_printClosure"), print)); |
| 96 | 97 |
| 97 Dart_Handle vm_hooks_name = ToDart("VMLibraryHooks"); | 98 Dart_Handle vm_hooks_name = ToDart("VMLibraryHooks"); |
| 98 Dart_Handle vm_hooks = Dart_GetClass(internal_library, vm_hooks_name); | 99 Dart_Handle vm_hooks = Dart_GetClass(internal_library, vm_hooks_name); |
| 99 DART_CHECK_VALID(vm_hooks); | 100 DART_CHECK_VALID(vm_hooks); |
| 100 Dart_Handle timer_name = ToDart("timerFactory"); | 101 Dart_Handle timer_name = ToDart("timerFactory"); |
| 101 DART_CHECK_VALID(Dart_SetField(vm_hooks, timer_name, timer)); | 102 DART_CHECK_VALID(Dart_SetField(vm_hooks, timer_name, timer)); |
| 102 } | 103 } |
| 103 | 104 |
| 104 static void InitAsync(Dart_Handle builtin_library) { | 105 static void InitDartCore(Dart_Handle builtin) { |
| 106 Dart_Handle get_base_url = GetClosure(builtin, "_getGetBaseURLClosure"); |
| 107 Dart_Handle core_library = DartBuiltin::LookupLibrary("dart:core"); |
| 108 DART_CHECK_VALID(Dart_SetField(core_library, |
| 109 ToDart("_uriBaseClosure"), get_base_url)); |
| 110 } |
| 111 |
| 112 static void InitDartAsync(Dart_Handle builtin_library) { |
| 105 Dart_Handle schedule_microtask = | 113 Dart_Handle schedule_microtask = |
| 106 GetClosure(builtin_library, "_getScheduleMicrotaskClosure"); | 114 GetClosure(builtin_library, "_getScheduleMicrotaskClosure"); |
| 107 Dart_Handle async_library = DartBuiltin::LookupLibrary("dart:async"); | 115 Dart_Handle async_library = DartBuiltin::LookupLibrary("dart:async"); |
| 108 Dart_Handle set_schedule_microtask = ToDart("_setScheduleImmediateClosure"); | 116 Dart_Handle set_schedule_microtask = ToDart("_setScheduleImmediateClosure"); |
| 109 DART_CHECK_VALID(Dart_Invoke(async_library, set_schedule_microtask, 1, | 117 DART_CHECK_VALID(Dart_Invoke(async_library, set_schedule_microtask, 1, |
| 110 &schedule_microtask)); | 118 &schedule_microtask)); |
| 111 } | 119 } |
| 112 | 120 |
| 113 void BuiltinNatives::Init() { | 121 void BuiltinNatives::Init() { |
| 114 Dart_Handle builtin = Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary); | 122 Dart_Handle builtin = Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary); |
| 115 DART_CHECK_VALID(builtin); | 123 DART_CHECK_VALID(builtin); |
| 116 InitDartInternal(builtin); | 124 InitDartInternal(builtin); |
| 117 InitAsync(builtin); | 125 InitDartCore(builtin); |
| 126 InitDartAsync(builtin); |
| 118 } | 127 } |
| 119 | 128 |
| 120 // Implementation of native functions which are used for some | 129 // Implementation of native functions which are used for some |
| 121 // test/debug functionality in standalone dart mode. | 130 // test/debug functionality in standalone dart mode. |
| 122 void Logger_PrintString(Dart_NativeArguments args) { | 131 void Logger_PrintString(Dart_NativeArguments args) { |
| 123 intptr_t length = 0; | 132 intptr_t length = 0; |
| 124 uint8_t* chars = nullptr; | 133 uint8_t* chars = nullptr; |
| 125 Dart_Handle str = Dart_GetNativeArgument(args, 0); | 134 Dart_Handle str = Dart_GetNativeArgument(args, 0); |
| 126 Dart_Handle result = Dart_StringToUTF8(str, &chars, &length); | 135 Dart_Handle result = Dart_StringToUTF8(str, &chars, &length); |
| 127 if (Dart_IsError(result)) { | 136 if (Dart_IsError(result)) { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 150 | 159 |
| 151 void ScheduleMicrotask(Dart_NativeArguments args) { | 160 void ScheduleMicrotask(Dart_NativeArguments args) { |
| 152 Dart_Handle closure = Dart_GetNativeArgument(args, 0); | 161 Dart_Handle closure = Dart_GetNativeArgument(args, 0); |
| 153 if (LogIfError(closure) || !Dart_IsClosure(closure)) | 162 if (LogIfError(closure) || !Dart_IsClosure(closure)) |
| 154 return; | 163 return; |
| 155 DartState* dart_state = DartState::Current(); | 164 DartState* dart_state = DartState::Current(); |
| 156 Microtask::enqueueMicrotask(base::Bind(&ExecuteMicrotask, | 165 Microtask::enqueueMicrotask(base::Bind(&ExecuteMicrotask, |
| 157 dart_state->GetWeakPtr(), DartValue::Create(dart_state, closure))); | 166 dart_state->GetWeakPtr(), DartValue::Create(dart_state, closure))); |
| 158 } | 167 } |
| 159 | 168 |
| 169 void GetBaseURLString(Dart_NativeArguments args) { |
| 170 String url = DOMDartState::CurrentDocument()->url().string(); |
| 171 Dart_SetReturnValue(args, StringToDart(DartState::Current(), url)); |
| 172 } |
| 173 |
| 160 void Timer_create(Dart_NativeArguments args) { | 174 void Timer_create(Dart_NativeArguments args) { |
| 161 int64_t milliseconds = 0; | 175 int64_t milliseconds = 0; |
| 162 DART_CHECK_VALID(Dart_GetNativeIntegerArgument(args, 0, &milliseconds)); | 176 DART_CHECK_VALID(Dart_GetNativeIntegerArgument(args, 0, &milliseconds)); |
| 163 Dart_Handle closure = Dart_GetNativeArgument(args, 1); | 177 Dart_Handle closure = Dart_GetNativeArgument(args, 1); |
| 164 DART_CHECK_VALID(closure); | 178 DART_CHECK_VALID(closure); |
| 165 CHECK(Dart_IsClosure(closure)); | 179 CHECK(Dart_IsClosure(closure)); |
| 166 bool repeating = false; | 180 bool repeating = false; |
| 167 DART_CHECK_VALID(Dart_GetNativeBooleanArgument(args, 2, &repeating)); | 181 DART_CHECK_VALID(Dart_GetNativeBooleanArgument(args, 2, &repeating)); |
| 168 | 182 |
| 169 DOMDartState* state = DOMDartState::Current(); | 183 DOMDartState* state = DOMDartState::Current(); |
| 170 int timer_id = DOMTimer::install(state->document(), | 184 int timer_id = DOMTimer::install(state->document(), |
| 171 ScheduledAction::Create(state, closure), | 185 ScheduledAction::Create(state, closure), |
| 172 milliseconds, | 186 milliseconds, |
| 173 !repeating); | 187 !repeating); |
| 174 Dart_SetIntegerReturnValue(args, timer_id); | 188 Dart_SetIntegerReturnValue(args, timer_id); |
| 175 } | 189 } |
| 176 | 190 |
| 177 void Timer_cancel(Dart_NativeArguments args) { | 191 void Timer_cancel(Dart_NativeArguments args) { |
| 178 int64_t timer_id = 0; | 192 int64_t timer_id = 0; |
| 179 DART_CHECK_VALID(Dart_GetNativeIntegerArgument(args, 0, &timer_id)); | 193 DART_CHECK_VALID(Dart_GetNativeIntegerArgument(args, 0, &timer_id)); |
| 180 | 194 |
| 181 DOMDartState* state = DOMDartState::Current(); | 195 DOMDartState* state = DOMDartState::Current(); |
| 182 DOMTimer::removeByID(state->document(), timer_id); | 196 DOMTimer::removeByID(state->document(), timer_id); |
| 183 } | 197 } |
| 184 | 198 |
| 185 } // namespace blink | 199 } // namespace blink |
| OLD | NEW |