OLD | NEW |
---|---|
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "platform/assert.h" | 5 #include "platform/assert.h" |
6 #include "vm/bootstrap_natives.h" | 6 #include "vm/bootstrap_natives.h" |
7 #include "vm/class_finalizer.h" | 7 #include "vm/class_finalizer.h" |
8 #include "vm/dart.h" | 8 #include "vm/dart.h" |
9 #include "vm/dart_api_impl.h" | 9 #include "vm/dart_api_impl.h" |
10 #include "vm/dart_entry.h" | 10 #include "vm/dart_entry.h" |
11 #include "vm/exceptions.h" | 11 #include "vm/exceptions.h" |
12 #include "vm/lockers.h" | 12 #include "vm/lockers.h" |
13 #include "vm/longjump.h" | 13 #include "vm/longjump.h" |
14 #include "vm/message_handler.h" | 14 #include "vm/message_handler.h" |
15 #include "vm/object.h" | 15 #include "vm/object.h" |
16 #include "vm/object_store.h" | 16 #include "vm/object_store.h" |
17 #include "vm/port.h" | 17 #include "vm/port.h" |
18 #include "vm/resolver.h" | 18 #include "vm/resolver.h" |
19 #include "vm/snapshot.h" | 19 #include "vm/snapshot.h" |
20 #include "vm/symbols.h" | 20 #include "vm/symbols.h" |
21 #include "vm/unicode.h" | |
21 | 22 |
22 namespace dart { | 23 namespace dart { |
23 | 24 |
24 static uint8_t* allocator(uint8_t* ptr, intptr_t old_size, intptr_t new_size) { | 25 static uint8_t* allocator(uint8_t* ptr, intptr_t old_size, intptr_t new_size) { |
25 void* new_ptr = realloc(reinterpret_cast<void*>(ptr), new_size); | 26 void* new_ptr = realloc(reinterpret_cast<void*>(ptr), new_size); |
26 return reinterpret_cast<uint8_t*>(new_ptr); | 27 return reinterpret_cast<uint8_t*>(new_ptr); |
27 } | 28 } |
28 | 29 |
29 | 30 |
30 DEFINE_NATIVE_ENTRY(CapabilityImpl_factory, 1) { | 31 DEFINE_NATIVE_ENTRY(CapabilityImpl_factory, 1) { |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
134 *error = zone->PrintToString( | 135 *error = zone->PrintToString( |
135 "Unable to canonicalize uri '%s': no library tag handler found.", | 136 "Unable to canonicalize uri '%s': no library tag handler found.", |
136 uri.ToCString()); | 137 uri.ToCString()); |
137 } | 138 } |
138 return retval; | 139 return retval; |
139 } | 140 } |
140 | 141 |
141 | 142 |
142 static bool CreateIsolate(Isolate* parent_isolate, | 143 static bool CreateIsolate(Isolate* parent_isolate, |
143 IsolateSpawnState* state, | 144 IsolateSpawnState* state, |
145 const char* package_root, | |
144 char** error) { | 146 char** error) { |
145 Dart_IsolateCreateCallback callback = Isolate::CreateCallback(); | 147 Dart_IsolateCreateCallback callback = Isolate::CreateCallback(); |
146 if (callback == NULL) { | 148 if (callback == NULL) { |
147 *error = strdup("Null callback specified for isolate creation\n"); | 149 *error = strdup("Null callback specified for isolate creation\n"); |
148 Isolate::SetCurrent(parent_isolate); | 150 Isolate::SetCurrent(parent_isolate); |
149 return false; | 151 return false; |
150 } | 152 } |
151 | 153 |
152 void* init_data = parent_isolate->init_callback_data(); | 154 void* init_data = parent_isolate->init_callback_data(); |
153 Isolate* child_isolate = reinterpret_cast<Isolate*>( | 155 Isolate* child_isolate = reinterpret_cast<Isolate*>( |
154 (callback)(state->script_url(), | 156 (callback)(state->script_url(), |
155 state->function_name(), | 157 state->function_name(), |
158 package_root, | |
156 init_data, | 159 init_data, |
157 error)); | 160 error)); |
158 if (child_isolate == NULL) { | 161 if (child_isolate == NULL) { |
159 Isolate::SetCurrent(parent_isolate); | 162 Isolate::SetCurrent(parent_isolate); |
160 return false; | 163 return false; |
161 } | 164 } |
162 state->set_isolate(reinterpret_cast<Isolate*>(child_isolate)); | 165 state->set_isolate(reinterpret_cast<Isolate*>(child_isolate)); |
163 | 166 |
164 Isolate::SetCurrent(parent_isolate); | 167 Isolate::SetCurrent(parent_isolate); |
165 return true; | 168 return true; |
166 } | 169 } |
167 | 170 |
168 | 171 |
169 static RawObject* Spawn(Isolate* parent_isolate, | 172 static RawObject* Spawn(Isolate* parent_isolate, |
170 IsolateSpawnState* state) { | 173 IsolateSpawnState* state, |
174 const char* package_root) { | |
171 // Create a new isolate. | 175 // Create a new isolate. |
172 char* error = NULL; | 176 char* error = NULL; |
173 if (!CreateIsolate(parent_isolate, state, &error)) { | 177 if (!CreateIsolate(parent_isolate, state, package_root, &error)) { |
174 delete state; | 178 delete state; |
175 const String& msg = String::Handle(String::New(error)); | 179 const String& msg = String::Handle(String::New(error)); |
176 free(error); | 180 free(error); |
177 ThrowIsolateSpawnException(msg); | 181 ThrowIsolateSpawnException(msg); |
178 } | 182 } |
179 | 183 |
180 // Create a SendPort for the new isolate. | 184 // Create a SendPort for the new isolate. |
181 Isolate* spawned_isolate = state->isolate(); | 185 Isolate* spawned_isolate = state->isolate(); |
182 const SendPort& port = SendPort::Handle( | 186 const SendPort& port = SendPort::Handle( |
183 SendPort::New(spawned_isolate->main_port())); | 187 SendPort::New(spawned_isolate->main_port())); |
(...skipping 15 matching lines...) Expand all Loading... | |
199 GET_NON_NULL_NATIVE_ARGUMENT(Instance, message, arguments->NativeArgAt(2)); | 203 GET_NON_NULL_NATIVE_ARGUMENT(Instance, message, arguments->NativeArgAt(2)); |
200 if (closure.IsClosure()) { | 204 if (closure.IsClosure()) { |
201 Function& func = Function::Handle(); | 205 Function& func = Function::Handle(); |
202 func = Closure::function(closure); | 206 func = Closure::function(closure); |
203 if (func.IsImplicitClosureFunction() && func.is_static()) { | 207 if (func.IsImplicitClosureFunction() && func.is_static()) { |
204 #if defined(DEBUG) | 208 #if defined(DEBUG) |
205 Context& ctx = Context::Handle(); | 209 Context& ctx = Context::Handle(); |
206 ctx = Closure::context(closure); | 210 ctx = Closure::context(closure); |
207 ASSERT(ctx.num_variables() == 0); | 211 ASSERT(ctx.num_variables() == 0); |
208 #endif | 212 #endif |
209 return Spawn(isolate, new IsolateSpawnState(port.Id(), func, message)); | 213 return Spawn(isolate, |
214 new IsolateSpawnState(port.Id(), func, message), | |
215 NULL); | |
turnidge
2014/09/08 16:40:48
Please add the package_root to IsolateSpawnState a
Anders Johnsen
2014/09/09 06:45:44
Done.
| |
210 } | 216 } |
211 } | 217 } |
212 const String& msg = String::Handle(String::New( | 218 const String& msg = String::Handle(String::New( |
213 "Isolate.spawn expects to be passed a static or top-level function")); | 219 "Isolate.spawn expects to be passed a static or top-level function")); |
214 Exceptions::ThrowArgumentError(msg); | 220 Exceptions::ThrowArgumentError(msg); |
215 return Object::null(); | 221 return Object::null(); |
216 } | 222 } |
217 | 223 |
218 | 224 |
219 DEFINE_NATIVE_ENTRY(Isolate_spawnUri, 4) { | 225 DEFINE_NATIVE_ENTRY(Isolate_spawnUri, 5) { |
220 GET_NON_NULL_NATIVE_ARGUMENT(SendPort, port, arguments->NativeArgAt(0)); | 226 GET_NON_NULL_NATIVE_ARGUMENT(SendPort, port, arguments->NativeArgAt(0)); |
221 GET_NON_NULL_NATIVE_ARGUMENT(String, uri, arguments->NativeArgAt(1)); | 227 GET_NON_NULL_NATIVE_ARGUMENT(String, uri, arguments->NativeArgAt(1)); |
222 GET_NON_NULL_NATIVE_ARGUMENT(Instance, args, arguments->NativeArgAt(2)); | 228 GET_NON_NULL_NATIVE_ARGUMENT(Instance, args, arguments->NativeArgAt(2)); |
223 GET_NON_NULL_NATIVE_ARGUMENT(Instance, message, arguments->NativeArgAt(3)); | 229 GET_NON_NULL_NATIVE_ARGUMENT(Instance, message, arguments->NativeArgAt(3)); |
230 GET_NATIVE_ARGUMENT(String, package_root, arguments->NativeArgAt(4)); | |
224 | 231 |
225 // Canonicalize the uri with respect to the current isolate. | 232 // Canonicalize the uri with respect to the current isolate. |
226 char* error = NULL; | 233 char* error = NULL; |
227 char* canonical_uri = NULL; | 234 char* canonical_uri = NULL; |
228 const Library& root_lib = | 235 const Library& root_lib = |
229 Library::Handle(arguments->isolate()->object_store()->root_library()); | 236 Library::Handle(arguments->isolate()->object_store()->root_library()); |
230 if (!CanonicalizeUri(arguments->isolate(), root_lib, uri, | 237 if (!CanonicalizeUri(arguments->isolate(), root_lib, uri, |
231 &canonical_uri, &error)) { | 238 &canonical_uri, &error)) { |
232 const String& msg = String::Handle(String::New(error)); | 239 const String& msg = String::Handle(String::New(error)); |
233 ThrowIsolateSpawnException(msg); | 240 ThrowIsolateSpawnException(msg); |
234 } | 241 } |
235 | 242 |
236 return Spawn(isolate, new IsolateSpawnState(port.Id(), canonical_uri, | 243 char* utf8_package_root = NULL; |
237 args, message)); | 244 if (!package_root.IsNull()) { |
245 const intptr_t len = Utf8::Length(package_root); | |
246 Zone* zone = isolate->current_zone(); | |
247 utf8_package_root = zone->Alloc<char>(len + 1); | |
248 package_root.ToUTF8(reinterpret_cast<uint8_t*>(utf8_package_root), len); | |
249 utf8_package_root[len] = '\0'; | |
250 } | |
251 | |
252 return Spawn(isolate, | |
253 new IsolateSpawnState(port.Id(), canonical_uri, args, message), | |
254 utf8_package_root); | |
turnidge
2014/09/08 16:40:48
Ditto above, please pass the package root via Isol
Anders Johnsen
2014/09/09 06:45:43
Done.
| |
238 } | 255 } |
239 | 256 |
240 | 257 |
241 DEFINE_NATIVE_ENTRY(Isolate_sendOOB, 2) { | 258 DEFINE_NATIVE_ENTRY(Isolate_sendOOB, 2) { |
242 GET_NON_NULL_NATIVE_ARGUMENT(SendPort, port, arguments->NativeArgAt(0)); | 259 GET_NON_NULL_NATIVE_ARGUMENT(SendPort, port, arguments->NativeArgAt(0)); |
243 GET_NON_NULL_NATIVE_ARGUMENT(Array, msg, arguments->NativeArgAt(1)); | 260 GET_NON_NULL_NATIVE_ARGUMENT(Array, msg, arguments->NativeArgAt(1)); |
244 | 261 |
245 // Make sure to route this request to the isolate library OOB mesage handler. | 262 // Make sure to route this request to the isolate library OOB mesage handler. |
246 msg.SetAt(0, Smi::Handle(Smi::New(Message::kIsolateLibOOBMsg))); | 263 msg.SetAt(0, Smi::Handle(Smi::New(Message::kIsolateLibOOBMsg))); |
247 | 264 |
248 uint8_t* data = NULL; | 265 uint8_t* data = NULL; |
249 MessageWriter writer(&data, &allocator); | 266 MessageWriter writer(&data, &allocator); |
250 writer.WriteMessage(msg); | 267 writer.WriteMessage(msg); |
251 | 268 |
252 PortMap::PostMessage(new Message(port.Id(), | 269 PortMap::PostMessage(new Message(port.Id(), |
253 data, writer.BytesWritten(), | 270 data, writer.BytesWritten(), |
254 Message::kOOBPriority)); | 271 Message::kOOBPriority)); |
255 return Object::null(); | 272 return Object::null(); |
256 } | 273 } |
257 | 274 |
258 } // namespace dart | 275 } // namespace dart |
OLD | NEW |