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