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 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 return nullptr; | 80 return nullptr; |
81 } | 81 } |
82 | 82 |
83 static Dart_Handle GetClosure(Dart_Handle builtin_library, const char* name) { | 83 static Dart_Handle GetClosure(Dart_Handle builtin_library, const char* name) { |
84 Dart_Handle getter_name = ToDart(name); | 84 Dart_Handle getter_name = ToDart(name); |
85 Dart_Handle closure = Dart_Invoke(builtin_library, getter_name, 0, nullptr); | 85 Dart_Handle closure = Dart_Invoke(builtin_library, getter_name, 0, nullptr); |
86 DART_CHECK_VALID(closure); | 86 DART_CHECK_VALID(closure); |
87 return closure; | 87 return closure; |
88 } | 88 } |
89 | 89 |
90 static void InitDartInternal(Dart_Handle builtin_library) { | 90 static void InitDartInternal(Dart_Handle builtin_library, bool dom_isolate) { |
91 Dart_Handle print = GetClosure(builtin_library, "_getPrintClosure"); | 91 Dart_Handle print = GetClosure(builtin_library, "_getPrintClosure"); |
92 Dart_Handle timer = GetClosure(builtin_library, "_getCreateTimerClosure"); | 92 Dart_Handle timer = GetClosure(builtin_library, "_getCreateTimerClosure"); |
93 | 93 |
94 Dart_Handle internal_library = DartBuiltin::LookupLibrary("dart:_internal"); | 94 Dart_Handle internal_library = DartBuiltin::LookupLibrary("dart:_internal"); |
95 | 95 |
96 DART_CHECK_VALID(Dart_SetField( | 96 DART_CHECK_VALID(Dart_SetField( |
97 internal_library, ToDart("_printClosure"), print)); | 97 internal_library, ToDart("_printClosure"), print)); |
98 | 98 |
99 Dart_Handle vm_hooks_name = ToDart("VMLibraryHooks"); | 99 if (dom_isolate) { |
100 Dart_Handle vm_hooks = Dart_GetClass(internal_library, vm_hooks_name); | 100 Dart_Handle vm_hooks_name = ToDart("VMLibraryHooks"); |
101 DART_CHECK_VALID(vm_hooks); | 101 Dart_Handle vm_hooks = Dart_GetClass(internal_library, vm_hooks_name); |
102 Dart_Handle timer_name = ToDart("timerFactory"); | 102 DART_CHECK_VALID(vm_hooks); |
103 DART_CHECK_VALID(Dart_SetField(vm_hooks, timer_name, timer)); | 103 Dart_Handle timer_name = ToDart("timerFactory"); |
| 104 DART_CHECK_VALID(Dart_SetField(vm_hooks, timer_name, timer)); |
| 105 } else { |
| 106 Dart_Handle io_lib = DartBuiltin::LookupLibrary("dart:io"); |
| 107 Dart_Handle setup_hooks = Dart_NewStringFromCString("_setupHooks"); |
| 108 DART_CHECK_VALID(Dart_Invoke(io_lib, setup_hooks, 0, NULL)); |
| 109 Dart_Handle isolate_lib = DartBuiltin::LookupLibrary("dart:isolate"); |
| 110 DART_CHECK_VALID(Dart_Invoke(isolate_lib, setup_hooks, 0, NULL)); |
| 111 } |
104 } | 112 } |
105 | 113 |
106 static void InitDartCore(Dart_Handle builtin) { | 114 static void InitDartCore(Dart_Handle builtin, bool dom_isolate) { |
107 Dart_Handle get_base_url = GetClosure(builtin, "_getGetBaseURLClosure"); | 115 Dart_Handle get_base_url = GetClosure(builtin, "_getGetBaseURLClosure"); |
108 Dart_Handle core_library = DartBuiltin::LookupLibrary("dart:core"); | 116 Dart_Handle core_library = DartBuiltin::LookupLibrary("dart:core"); |
109 DART_CHECK_VALID(Dart_SetField(core_library, | 117 DART_CHECK_VALID(Dart_SetField(core_library, |
110 ToDart("_uriBaseClosure"), get_base_url)); | 118 ToDart("_uriBaseClosure"), get_base_url)); |
111 } | 119 } |
112 | 120 |
113 static void InitDartAsync(Dart_Handle builtin_library) { | 121 static void InitDartAsync(Dart_Handle builtin_library, bool dom_isolate) { |
114 Dart_Handle schedule_microtask = | 122 Dart_Handle schedule_microtask; |
115 GetClosure(builtin_library, "_getScheduleMicrotaskClosure"); | 123 if (dom_isolate) { |
| 124 schedule_microtask = |
| 125 GetClosure(builtin_library, "_getScheduleMicrotaskClosure"); |
| 126 } else { |
| 127 Dart_Handle isolate_lib = DartBuiltin::LookupLibrary("dart:isolate"); |
| 128 Dart_Handle method_name = |
| 129 Dart_NewStringFromCString("_getIsolateScheduleImmediateClosure"); |
| 130 schedule_microtask = Dart_Invoke(isolate_lib, method_name, 0, NULL); |
| 131 } |
116 Dart_Handle async_library = DartBuiltin::LookupLibrary("dart:async"); | 132 Dart_Handle async_library = DartBuiltin::LookupLibrary("dart:async"); |
117 Dart_Handle set_schedule_microtask = ToDart("_setScheduleImmediateClosure"); | 133 Dart_Handle set_schedule_microtask = ToDart("_setScheduleImmediateClosure"); |
118 DART_CHECK_VALID(Dart_Invoke(async_library, set_schedule_microtask, 1, | 134 DART_CHECK_VALID(Dart_Invoke(async_library, set_schedule_microtask, 1, |
119 &schedule_microtask)); | 135 &schedule_microtask)); |
120 } | 136 } |
121 | 137 |
122 void BuiltinNatives::Init() { | 138 void BuiltinNatives::Init(bool dom_isolate) { |
123 Dart_Handle builtin = Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary); | 139 Dart_Handle builtin = Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary); |
124 DART_CHECK_VALID(builtin); | 140 DART_CHECK_VALID(builtin); |
125 InitDartInternal(builtin); | 141 InitDartInternal(builtin, dom_isolate); |
126 InitDartCore(builtin); | 142 InitDartCore(builtin, dom_isolate); |
127 InitDartAsync(builtin); | 143 InitDartAsync(builtin, dom_isolate); |
128 } | 144 } |
129 | 145 |
130 // Implementation of native functions which are used for some | 146 // Implementation of native functions which are used for some |
131 // test/debug functionality in standalone dart mode. | 147 // test/debug functionality in standalone dart mode. |
132 void Logger_PrintString(Dart_NativeArguments args) { | 148 void Logger_PrintString(Dart_NativeArguments args) { |
133 intptr_t length = 0; | 149 intptr_t length = 0; |
134 uint8_t* chars = nullptr; | 150 uint8_t* chars = nullptr; |
135 Dart_Handle str = Dart_GetNativeArgument(args, 0); | 151 Dart_Handle str = Dart_GetNativeArgument(args, 0); |
136 Dart_Handle result = Dart_StringToUTF8(str, &chars, &length); | 152 Dart_Handle result = Dart_StringToUTF8(str, &chars, &length); |
137 if (Dart_IsError(result)) { | 153 if (Dart_IsError(result)) { |
(...skipping 18 matching lines...) Expand all Loading... |
156 DartIsolateScope scope(dart_state->isolate()); | 172 DartIsolateScope scope(dart_state->isolate()); |
157 DartApiScope api_scope; | 173 DartApiScope api_scope; |
158 DartInvokeAppClosure(callback->dart_value(), 0, nullptr); | 174 DartInvokeAppClosure(callback->dart_value(), 0, nullptr); |
159 } | 175 } |
160 | 176 |
161 void ScheduleMicrotask(Dart_NativeArguments args) { | 177 void ScheduleMicrotask(Dart_NativeArguments args) { |
162 Dart_Handle closure = Dart_GetNativeArgument(args, 0); | 178 Dart_Handle closure = Dart_GetNativeArgument(args, 0); |
163 if (LogIfError(closure) || !Dart_IsClosure(closure)) | 179 if (LogIfError(closure) || !Dart_IsClosure(closure)) |
164 return; | 180 return; |
165 DartState* dart_state = DartState::Current(); | 181 DartState* dart_state = DartState::Current(); |
| 182 CHECK(dart_state); |
166 Microtask::enqueueMicrotask(base::Bind(&ExecuteMicrotask, | 183 Microtask::enqueueMicrotask(base::Bind(&ExecuteMicrotask, |
167 dart_state->GetWeakPtr(), DartValue::Create(dart_state, closure))); | 184 dart_state->GetWeakPtr(), DartValue::Create(dart_state, closure))); |
168 } | 185 } |
169 | 186 |
170 void GetBaseURLString(Dart_NativeArguments args) { | 187 void GetBaseURLString(Dart_NativeArguments args) { |
171 String url = DOMDartState::CurrentDocument()->url().string(); | 188 String url = DOMDartState::CurrentDocument()->url().string(); |
172 Dart_SetReturnValue(args, StringToDart(DartState::Current(), url)); | 189 Dart_SetReturnValue(args, StringToDart(DartState::Current(), url)); |
173 } | 190 } |
174 | 191 |
175 void Timer_create(Dart_NativeArguments args) { | 192 void Timer_create(Dart_NativeArguments args) { |
176 int64_t milliseconds = 0; | 193 int64_t milliseconds = 0; |
177 DART_CHECK_VALID(Dart_GetNativeIntegerArgument(args, 0, &milliseconds)); | 194 DART_CHECK_VALID(Dart_GetNativeIntegerArgument(args, 0, &milliseconds)); |
178 Dart_Handle closure = Dart_GetNativeArgument(args, 1); | 195 Dart_Handle closure = Dart_GetNativeArgument(args, 1); |
179 DART_CHECK_VALID(closure); | 196 DART_CHECK_VALID(closure); |
180 CHECK(Dart_IsClosure(closure)); | 197 CHECK(Dart_IsClosure(closure)); |
181 bool repeating = false; | 198 bool repeating = false; |
182 DART_CHECK_VALID(Dart_GetNativeBooleanArgument(args, 2, &repeating)); | 199 DART_CHECK_VALID(Dart_GetNativeBooleanArgument(args, 2, &repeating)); |
183 | 200 |
184 DOMDartState* state = DOMDartState::Current(); | 201 DOMDartState* state = DOMDartState::Current(); |
| 202 CHECK(state); |
185 int timer_id = DOMTimer::install(state->document(), | 203 int timer_id = DOMTimer::install(state->document(), |
186 ScheduledAction::Create(state, closure), | 204 ScheduledAction::Create(state, closure), |
187 milliseconds, | 205 milliseconds, |
188 !repeating); | 206 !repeating); |
189 Dart_SetIntegerReturnValue(args, timer_id); | 207 Dart_SetIntegerReturnValue(args, timer_id); |
190 } | 208 } |
191 | 209 |
192 void Timer_cancel(Dart_NativeArguments args) { | 210 void Timer_cancel(Dart_NativeArguments args) { |
193 int64_t timer_id = 0; | 211 int64_t timer_id = 0; |
194 DART_CHECK_VALID(Dart_GetNativeIntegerArgument(args, 0, &timer_id)); | 212 DART_CHECK_VALID(Dart_GetNativeIntegerArgument(args, 0, &timer_id)); |
195 | 213 |
196 DOMDartState* state = DOMDartState::Current(); | 214 DOMDartState* state = DOMDartState::Current(); |
| 215 CHECK(state); |
197 DOMTimer::removeByID(state->document(), timer_id); | 216 DOMTimer::removeByID(state->document(), timer_id); |
198 } | 217 } |
199 | 218 |
200 } // namespace blink | 219 } // namespace blink |
OLD | NEW |