Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(21)

Side by Side Diff: runtime/vm/dart_api_impl.cc

Issue 9956133: Remove many calls to Isolate::Current() in the dart embedding api by (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 "include/dart_api.h" 5 #include "include/dart_api.h"
6 6
7 #include "vm/bigint_operations.h" 7 #include "vm/bigint_operations.h"
8 #include "vm/class_finalizer.h" 8 #include "vm/class_finalizer.h"
9 #include "vm/compiler.h" 9 #include "vm/compiler.h"
10 #include "vm/dart.h" 10 #include "vm/dart.h"
(...skipping 20 matching lines...) Expand all
31 ThreadLocalKey Api::api_native_key_ = Thread::kUnsetThreadLocalKey; 31 ThreadLocalKey Api::api_native_key_ = Thread::kUnsetThreadLocalKey;
32 32
33 const char* CanonicalFunction(const char* func) { 33 const char* CanonicalFunction(const char* func) {
34 if (strncmp(func, "dart::", 6) == 0) { 34 if (strncmp(func, "dart::", 6) == 0) {
35 return func + 6; 35 return func + 6;
36 } else { 36 } else {
37 return func; 37 return func;
38 } 38 }
39 } 39 }
40 40
41 #define RETURN_TYPE_ERROR(dart_handle, Type) \ 41 #define RETURN_TYPE_ERROR(isolate, dart_handle, Type) \
42 do { \ 42 do { \
43 const Object& tmp = Object::Handle(Api::UnwrapHandle((dart_handle))); \ 43 const Object& tmp = \
44 Object::Handle(isolate, Api::UnwrapHandle((dart_handle))); \
44 if (tmp.IsNull()) { \ 45 if (tmp.IsNull()) { \
45 return Api::NewError("%s expects argument '%s' to be non-null.", \ 46 return Api::NewError("%s expects argument '%s' to be non-null.", \
46 CURRENT_FUNC, #dart_handle); \ 47 CURRENT_FUNC, #dart_handle); \
47 } else if (tmp.IsError()) { \ 48 } else if (tmp.IsError()) { \
48 return dart_handle; \ 49 return dart_handle; \
49 } else { \ 50 } else { \
50 return Api::NewError("%s expects argument '%s' to be of type %s.", \ 51 return Api::NewError("%s expects argument '%s' to be of type %s.", \
51 CURRENT_FUNC, #dart_handle, #Type); \ 52 CURRENT_FUNC, #dart_handle, #Type); \
52 } \ 53 } \
53 } while (0) 54 } while (0)
(...skipping 12 matching lines...) Expand all
66 success = isolate->object_store()->PreallocateObjects(); 67 success = isolate->object_store()->PreallocateObjects();
67 if (success) { 68 if (success) {
68 return NULL; 69 return NULL;
69 } 70 }
70 } 71 }
71 // Make a copy of the error message as the original message string 72 // Make a copy of the error message as the original message string
72 // may get deallocated when we return back from the Dart API call. 73 // may get deallocated when we return back from the Dart API call.
73 const Error& err = Error::Handle(isolate->object_store()->sticky_error()); 74 const Error& err = Error::Handle(isolate->object_store()->sticky_error());
74 const char* errmsg = err.ToErrorCString(); 75 const char* errmsg = err.ToErrorCString();
75 intptr_t errlen = strlen(errmsg) + 1; 76 intptr_t errlen = strlen(errmsg) + 1;
76 char* msg = reinterpret_cast<char*>(Api::Allocate(errlen)); 77 char* msg = reinterpret_cast<char*>(Api::Allocate(isolate, errlen));
77 OS::SNPrint(msg, errlen, "%s", errmsg); 78 OS::SNPrint(msg, errlen, "%s", errmsg);
78 return msg; 79 return msg;
79 } 80 }
80 81
81 82
82 void SetupErrorResult(Dart_Handle* handle) { 83 void SetupErrorResult(Isolate* isolate, Dart_Handle* handle) {
83 const Error& error = Error::Handle( 84 const Error& error = Error::Handle(
84 Isolate::Current()->object_store()->sticky_error()); 85 isolate, Isolate::Current()->object_store()->sticky_error());
85 *handle = Api::NewLocalHandle(error); 86 *handle = Api::NewLocalHandle(isolate, error);
86 } 87 }
87 88
88 89
89 Dart_Handle Api::NewLocalHandle(const Object& object) { 90 Dart_Handle Api::NewLocalHandle(Isolate* isolate, const Object& object) {
90 Isolate* isolate = Isolate::Current();
91 ASSERT(isolate != NULL); 91 ASSERT(isolate != NULL);
92 ApiState* state = isolate->api_state(); 92 ApiState* state = isolate->api_state();
93 ASSERT(state != NULL); 93 ASSERT(state != NULL);
94 ApiLocalScope* scope = state->top_scope(); 94 ApiLocalScope* scope = state->top_scope();
95 ASSERT(scope != NULL); 95 ASSERT(scope != NULL);
96 LocalHandles* local_handles = scope->local_handles(); 96 LocalHandles* local_handles = scope->local_handles();
97 ASSERT(local_handles != NULL); 97 ASSERT(local_handles != NULL);
98 LocalHandle* ref = local_handles->AllocateHandle(); 98 LocalHandle* ref = local_handles->AllocateHandle();
99 ref->set_raw(object); 99 ref->set_raw(object);
100 return reinterpret_cast<Dart_Handle>(ref); 100 return reinterpret_cast<Dart_Handle>(ref);
(...skipping 10 matching lines...) Expand all
111 state->IsValidPersistentHandle(object) || 111 state->IsValidPersistentHandle(object) ||
112 state->IsValidLocalHandle(object)); 112 state->IsValidLocalHandle(object));
113 ASSERT(FinalizablePersistentHandle::raw_offset() == 0 && 113 ASSERT(FinalizablePersistentHandle::raw_offset() == 0 &&
114 PersistentHandle::raw_offset() == 0 && 114 PersistentHandle::raw_offset() == 0 &&
115 LocalHandle::raw_offset() == 0); 115 LocalHandle::raw_offset() == 0);
116 #endif 116 #endif
117 return *(reinterpret_cast<RawObject**>(object)); 117 return *(reinterpret_cast<RawObject**>(object));
118 } 118 }
119 119
120 #define DEFINE_UNWRAP(Type) \ 120 #define DEFINE_UNWRAP(Type) \
121 const Type& Api::Unwrap##Type##Handle(Dart_Handle dart_handle) { \ 121 const Type& Api::Unwrap##Type##Handle(Isolate* iso, \
122 const Object& tmp = Object::Handle(Api::UnwrapHandle(dart_handle)); \ 122 Dart_Handle dart_handle) { \
123 Type& typed_handle = Type::Handle(); \ 123 const Object& tmp = Object::Handle(iso, Api::UnwrapHandle(dart_handle)); \
124 Type& typed_handle = Type::Handle(iso); \
124 if (tmp.Is##Type()) { \ 125 if (tmp.Is##Type()) { \
125 typed_handle ^= tmp.raw(); \ 126 typed_handle ^= tmp.raw(); \
126 } \ 127 } \
127 return typed_handle; \ 128 return typed_handle; \
128 } 129 }
129 CLASS_LIST_NO_OBJECT(DEFINE_UNWRAP) 130 CLASS_LIST_NO_OBJECT(DEFINE_UNWRAP)
130 #undef DEFINE_UNWRAP 131 #undef DEFINE_UNWRAP
131 132
132 133
133 LocalHandle* Api::UnwrapAsLocalHandle(const ApiState& state, 134 LocalHandle* Api::UnwrapAsLocalHandle(const ApiState& state,
(...skipping 24 matching lines...) Expand all
158 ASSERT(state.IsValidPrologueWeakPersistentHandle(object)); 159 ASSERT(state.IsValidPrologueWeakPersistentHandle(object));
159 return reinterpret_cast<FinalizablePersistentHandle*>(object); 160 return reinterpret_cast<FinalizablePersistentHandle*>(object);
160 } 161 }
161 162
162 163
163 Dart_Isolate Api::CastIsolate(Isolate* isolate) { 164 Dart_Isolate Api::CastIsolate(Isolate* isolate) {
164 return reinterpret_cast<Dart_Isolate>(isolate); 165 return reinterpret_cast<Dart_Isolate>(isolate);
165 } 166 }
166 167
167 168
168 Dart_Handle Api::Success() { 169 Dart_Handle Api::Success(Isolate* isolate) {
169 Isolate* isolate = Isolate::Current();
170 ASSERT(isolate != NULL); 170 ASSERT(isolate != NULL);
171 ApiState* state = isolate->api_state(); 171 ApiState* state = isolate->api_state();
172 ASSERT(state != NULL); 172 ASSERT(state != NULL);
173 PersistentHandle* true_handle = state->True(); 173 PersistentHandle* true_handle = state->True();
174 return reinterpret_cast<Dart_Handle>(true_handle); 174 return reinterpret_cast<Dart_Handle>(true_handle);
175 } 175 }
176 176
177 177
178 Dart_Handle Api::NewError(const char* format, ...) { 178 Dart_Handle Api::NewError(const char* format, ...) {
179 DARTSCOPE_NOCHECKS(Isolate::Current()); 179 Isolate* isolate = Isolate::Current();
180 DARTSCOPE_NOCHECKS(isolate);
180 181
181 va_list args; 182 va_list args;
182 va_start(args, format); 183 va_start(args, format);
183 intptr_t len = OS::VSNPrint(NULL, 0, format, args); 184 intptr_t len = OS::VSNPrint(NULL, 0, format, args);
184 va_end(args); 185 va_end(args);
185 186
186 char* buffer = reinterpret_cast<char*>(zone.Allocate(len + 1)); 187 char* buffer = reinterpret_cast<char*>(zone.Allocate(len + 1));
187 va_list args2; 188 va_list args2;
188 va_start(args2, format); 189 va_start(args2, format);
189 OS::VSNPrint(buffer, (len + 1), format, args2); 190 OS::VSNPrint(buffer, (len + 1), format, args2);
190 va_end(args2); 191 va_end(args2);
191 192
192 const String& message = String::Handle(String::New(buffer)); 193 const String& message = String::Handle(isolate, String::New(buffer));
193 const Object& obj = Object::Handle(ApiError::New(message)); 194 const Object& obj = Object::Handle(isolate, ApiError::New(message));
194 return Api::NewLocalHandle(obj); 195 return Api::NewLocalHandle(isolate, obj);
195 } 196 }
196 197
197 198
198 Dart_Handle Api::Null() { 199 Dart_Handle Api::Null(Isolate* isolate) {
199 Isolate* isolate = Isolate::Current();
200 ASSERT(isolate != NULL); 200 ASSERT(isolate != NULL);
201 ApiState* state = isolate->api_state(); 201 ApiState* state = isolate->api_state();
202 ASSERT(state != NULL); 202 ASSERT(state != NULL);
203 PersistentHandle* null_handle = state->Null(); 203 PersistentHandle* null_handle = state->Null();
204 return reinterpret_cast<Dart_Handle>(null_handle); 204 return reinterpret_cast<Dart_Handle>(null_handle);
205 } 205 }
206 206
207 207
208 Dart_Handle Api::True() { 208 Dart_Handle Api::True(Isolate* isolate) {
209 Isolate* isolate = Isolate::Current();
210 ASSERT(isolate != NULL); 209 ASSERT(isolate != NULL);
211 ApiState* state = isolate->api_state(); 210 ApiState* state = isolate->api_state();
212 ASSERT(state != NULL); 211 ASSERT(state != NULL);
213 PersistentHandle* true_handle = state->True(); 212 PersistentHandle* true_handle = state->True();
214 return reinterpret_cast<Dart_Handle>(true_handle); 213 return reinterpret_cast<Dart_Handle>(true_handle);
215 } 214 }
216 215
217 216
218 Dart_Handle Api::False() { 217 Dart_Handle Api::False(Isolate* isolate) {
219 Isolate* isolate = Isolate::Current();
220 ASSERT(isolate != NULL); 218 ASSERT(isolate != NULL);
221 ApiState* state = isolate->api_state(); 219 ApiState* state = isolate->api_state();
222 ASSERT(state != NULL); 220 ASSERT(state != NULL);
223 PersistentHandle* false_handle = state->False(); 221 PersistentHandle* false_handle = state->False();
224 return reinterpret_cast<Dart_Handle>(false_handle); 222 return reinterpret_cast<Dart_Handle>(false_handle);
225 } 223 }
226 224
227 225
228 uword Api::Allocate(intptr_t size) { 226 uword Api::Allocate(Isolate* isolate, intptr_t size) {
229 Isolate* isolate = Isolate::Current();
230 ASSERT(isolate != NULL); 227 ASSERT(isolate != NULL);
231 ApiState* state = isolate->api_state(); 228 ApiState* state = isolate->api_state();
232 ASSERT(state != NULL); 229 ASSERT(state != NULL);
233 ApiLocalScope* scope = state->top_scope(); 230 ApiLocalScope* scope = state->top_scope();
234 ASSERT(scope != NULL); 231 ASSERT(scope != NULL);
235 return scope->zone()->Allocate(size); 232 return scope->zone()->Allocate(size);
236 } 233 }
237 234
238 235
239 uword Api::Reallocate(uword ptr, intptr_t old_size, intptr_t new_size) { 236 uword Api::Reallocate(Isolate* isolate,
240 Isolate* isolate = Isolate::Current(); 237 uword ptr,
238 intptr_t old_size,
239 intptr_t new_size) {
241 ASSERT(isolate != NULL); 240 ASSERT(isolate != NULL);
242 ApiState* state = isolate->api_state(); 241 ApiState* state = isolate->api_state();
243 ASSERT(state != NULL); 242 ASSERT(state != NULL);
244 ApiLocalScope* scope = state->top_scope(); 243 ApiLocalScope* scope = state->top_scope();
245 ASSERT(scope != NULL); 244 ASSERT(scope != NULL);
246 return scope->zone()->Reallocate(ptr, old_size, new_size); 245 return scope->zone()->Reallocate(ptr, old_size, new_size);
247 } 246 }
248 247
249 248
250 void Api::InitOnce() { 249 void Api::InitOnce() {
251 ASSERT(api_native_key_ == Thread::kUnsetThreadLocalKey); 250 ASSERT(api_native_key_ == Thread::kUnsetThreadLocalKey);
252 api_native_key_ = Thread::CreateThreadLocal(); 251 api_native_key_ = Thread::CreateThreadLocal();
253 ASSERT(api_native_key_ != Thread::kUnsetThreadLocalKey); 252 ASSERT(api_native_key_ != Thread::kUnsetThreadLocalKey);
254 } 253 }
255 254
256 255
257 // --- Handles --- 256 // --- Handles ---
258 257
259 258
260 DART_EXPORT bool Dart_IsError(Dart_Handle handle) { 259 DART_EXPORT bool Dart_IsError(Dart_Handle handle) {
261 DARTSCOPE(Isolate::Current()); 260 Isolate* isolate = Isolate::Current();
262 const Object& obj = Object::Handle(Api::UnwrapHandle(handle)); 261 DARTSCOPE(isolate);
262 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(handle));
263 return obj.IsError(); 263 return obj.IsError();
264 } 264 }
265 265
266 266
267 DART_EXPORT const char* Dart_GetError(Dart_Handle handle) { 267 DART_EXPORT const char* Dart_GetError(Dart_Handle handle) {
268 DARTSCOPE(Isolate::Current()); 268 Isolate* isolate = Isolate::Current();
269 const Object& obj = Object::Handle(Api::UnwrapHandle(handle)); 269 DARTSCOPE(isolate);
270 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(handle));
270 if (obj.IsError()) { 271 if (obj.IsError()) {
271 Error& error = Error::Handle(); 272 Error& error = Error::Handle(isolate);
272 error ^= obj.raw(); 273 error ^= obj.raw();
273 const char* str = error.ToErrorCString(); 274 const char* str = error.ToErrorCString();
274 intptr_t len = strlen(str) + 1; 275 intptr_t len = strlen(str) + 1;
275 char* str_copy = reinterpret_cast<char*>(Api::Allocate(len)); 276 char* str_copy = reinterpret_cast<char*>(Api::Allocate(isolate, len));
276 strncpy(str_copy, str, len); 277 strncpy(str_copy, str, len);
277 return str_copy; 278 return str_copy;
278 } else { 279 } else {
279 return ""; 280 return "";
280 } 281 }
281 } 282 }
282 283
283 284
284 DART_EXPORT bool Dart_ErrorHasException(Dart_Handle handle) { 285 DART_EXPORT bool Dart_ErrorHasException(Dart_Handle handle) {
285 DARTSCOPE(Isolate::Current()); 286 Isolate* isolate = Isolate::Current();
286 const Object& obj = Object::Handle(Api::UnwrapHandle(handle)); 287 DARTSCOPE(isolate);
288 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(handle));
287 return obj.IsUnhandledException(); 289 return obj.IsUnhandledException();
288 } 290 }
289 291
290 292
291 DART_EXPORT Dart_Handle Dart_ErrorGetException(Dart_Handle handle) { 293 DART_EXPORT Dart_Handle Dart_ErrorGetException(Dart_Handle handle) {
292 DARTSCOPE(Isolate::Current()); 294 Isolate* isolate = Isolate::Current();
293 const Object& obj = Object::Handle(Api::UnwrapHandle(handle)); 295 DARTSCOPE(isolate);
296 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(handle));
294 if (obj.IsUnhandledException()) { 297 if (obj.IsUnhandledException()) {
295 UnhandledException& error = UnhandledException::Handle(); 298 UnhandledException& error = UnhandledException::Handle(isolate);
296 error ^= obj.raw(); 299 error ^= obj.raw();
297 const Object& exception = Object::Handle(error.exception()); 300 const Object& exception = Object::Handle(isolate, error.exception());
298 return Api::NewLocalHandle(exception); 301 return Api::NewLocalHandle(isolate, exception);
299 } else if (obj.IsError()) { 302 } else if (obj.IsError()) {
300 return Api::NewError("This error is not an unhandled exception error."); 303 return Api::NewError("This error is not an unhandled exception error.");
301 } else { 304 } else {
302 return Api::NewError("Can only get exceptions from error handles."); 305 return Api::NewError("Can only get exceptions from error handles.");
303 } 306 }
304 } 307 }
305 308
306 309
307 DART_EXPORT Dart_Handle Dart_ErrorGetStacktrace(Dart_Handle handle) { 310 DART_EXPORT Dart_Handle Dart_ErrorGetStacktrace(Dart_Handle handle) {
308 DARTSCOPE(Isolate::Current()); 311 Isolate* isolate = Isolate::Current();
309 const Object& obj = Object::Handle(Api::UnwrapHandle(handle)); 312 DARTSCOPE(isolate);
313 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(handle));
310 if (obj.IsUnhandledException()) { 314 if (obj.IsUnhandledException()) {
311 UnhandledException& error = UnhandledException::Handle(); 315 UnhandledException& error = UnhandledException::Handle(isolate);
312 error ^= obj.raw(); 316 error ^= obj.raw();
313 const Object& stacktrace = Object::Handle(error.stacktrace()); 317 const Object& stacktrace = Object::Handle(isolate, error.stacktrace());
314 return Api::NewLocalHandle(stacktrace); 318 return Api::NewLocalHandle(isolate, stacktrace);
315 } else if (obj.IsError()) { 319 } else if (obj.IsError()) {
316 return Api::NewError("This error is not an unhandled exception error."); 320 return Api::NewError("This error is not an unhandled exception error.");
317 } else { 321 } else {
318 return Api::NewError("Can only get stacktraces from error handles."); 322 return Api::NewError("Can only get stacktraces from error handles.");
319 } 323 }
320 } 324 }
321 325
322 326
323 // TODO(turnidge): This clones Api::NewError. I need to use va_copy to 327 // TODO(turnidge): This clones Api::NewError. I need to use va_copy to
324 // fix this but not sure if it available on all of our builds. 328 // fix this but not sure if it available on all of our builds.
325 DART_EXPORT Dart_Handle Dart_Error(const char* format, ...) { 329 DART_EXPORT Dart_Handle Dart_Error(const char* format, ...) {
326 DARTSCOPE(Isolate::Current()); 330 Isolate* isolate = Isolate::Current();
331 DARTSCOPE(isolate);
327 332
328 va_list args; 333 va_list args;
329 va_start(args, format); 334 va_start(args, format);
330 intptr_t len = OS::VSNPrint(NULL, 0, format, args); 335 intptr_t len = OS::VSNPrint(NULL, 0, format, args);
331 va_end(args); 336 va_end(args);
332 337
333 char* buffer = reinterpret_cast<char*>(zone.Allocate(len + 1)); 338 char* buffer = reinterpret_cast<char*>(zone.Allocate(len + 1));
334 va_list args2; 339 va_list args2;
335 va_start(args2, format); 340 va_start(args2, format);
336 OS::VSNPrint(buffer, (len + 1), format, args2); 341 OS::VSNPrint(buffer, (len + 1), format, args2);
337 va_end(args2); 342 va_end(args2);
338 343
339 const String& message = String::Handle(String::New(buffer)); 344 const String& message = String::Handle(isolate, String::New(buffer));
340 const Object& obj = Object::Handle(ApiError::New(message)); 345 const Object& obj = Object::Handle(isolate, ApiError::New(message));
341 return Api::NewLocalHandle(obj); 346 return Api::NewLocalHandle(isolate, obj);
342 } 347 }
343 348
344 349
345 DART_EXPORT Dart_Handle Dart_PropagateError(Dart_Handle handle) { 350 DART_EXPORT Dart_Handle Dart_PropagateError(Dart_Handle handle) {
346 Isolate* isolate = Isolate::Current(); 351 Isolate* isolate = Isolate::Current();
347 CHECK_ISOLATE(isolate); 352 CHECK_ISOLATE(isolate);
348 const Object& error = Object::Handle(Api::UnwrapHandle(handle)); 353 const Object& error = Object::Handle(isolate, Api::UnwrapHandle(handle));
349 if (!error.IsError()) { 354 if (!error.IsError()) {
350 return Api::NewError( 355 return Api::NewError(
351 "%s expects argument 'handle' to be an error handle. " 356 "%s expects argument 'handle' to be an error handle. "
352 "Did you forget to check Dart_IsError first?", 357 "Did you forget to check Dart_IsError first?",
353 CURRENT_FUNC); 358 CURRENT_FUNC);
354 } 359 }
355 if (isolate->top_exit_frame_info() == 0) { 360 if (isolate->top_exit_frame_info() == 0) {
356 // There are no dart frames on the stack so it would be illegal to 361 // There are no dart frames on the stack so it would be illegal to
357 // propagate an error here. 362 // propagate an error here.
358 return Api::NewError("No Dart frames on stack, cannot propagate error."); 363 return Api::NewError("No Dart frames on stack, cannot propagate error.");
(...skipping 14 matching lines...) Expand all
373 int line, 378 int line,
374 const char* handle, 379 const char* handle,
375 const char* message) { 380 const char* message) {
376 fprintf(stderr, "%s:%d: error handle: '%s':\n '%s'\n", 381 fprintf(stderr, "%s:%d: error handle: '%s':\n '%s'\n",
377 file, line, handle, message); 382 file, line, handle, message);
378 OS::Abort(); 383 OS::Abort();
379 } 384 }
380 385
381 386
382 DART_EXPORT Dart_Handle Dart_ToString(Dart_Handle object) { 387 DART_EXPORT Dart_Handle Dart_ToString(Dart_Handle object) {
383 DARTSCOPE(Isolate::Current()); 388 Isolate* isolate = Isolate::Current();
384 const Object& obj = Object::Handle(Api::UnwrapHandle(object)); 389 DARTSCOPE(isolate);
385 Object& result = Object::Handle(); 390 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(object));
391 Object& result = Object::Handle(isolate);
386 if (obj.IsString()) { 392 if (obj.IsString()) {
387 result = obj.raw(); 393 result = obj.raw();
388 } else if (obj.IsInstance()) { 394 } else if (obj.IsInstance()) {
389 Instance& receiver = Instance::Handle(); 395 Instance& receiver = Instance::Handle(isolate);
390 receiver ^= obj.raw(); 396 receiver ^= obj.raw();
391 result = DartLibraryCalls::ToString(receiver); 397 result = DartLibraryCalls::ToString(receiver);
392 } else { 398 } else {
393 // This is a VM internal object. Call the C++ method of printing. 399 // This is a VM internal object. Call the C++ method of printing.
394 result = String::New(obj.ToCString()); 400 result = String::New(obj.ToCString());
395 } 401 }
396 return Api::NewLocalHandle(result); 402 return Api::NewLocalHandle(isolate, result);
397 } 403 }
398 404
399 405
400 DART_EXPORT Dart_Handle Dart_IsSame(Dart_Handle obj1, Dart_Handle obj2, 406 DART_EXPORT Dart_Handle Dart_IsSame(Dart_Handle obj1, Dart_Handle obj2,
401 bool* value) { 407 bool* value) {
402 DARTSCOPE(Isolate::Current()); 408 Isolate* isolate = Isolate::Current();
403 const Object& expected = Object::Handle(Api::UnwrapHandle(obj1)); 409 DARTSCOPE(isolate);
404 const Object& actual = Object::Handle(Api::UnwrapHandle(obj2)); 410 const Object& expected = Object::Handle(isolate, Api::UnwrapHandle(obj1));
411 const Object& actual = Object::Handle(isolate, Api::UnwrapHandle(obj2));
405 *value = (expected.raw() == actual.raw()); 412 *value = (expected.raw() == actual.raw());
406 return Api::Success(); 413 return Api::Success(isolate);
407 } 414 }
408 415
409 416
410 DART_EXPORT Dart_Handle Dart_NewPersistentHandle(Dart_Handle object) { 417 DART_EXPORT Dart_Handle Dart_NewPersistentHandle(Dart_Handle object) {
411 Isolate* isolate = Isolate::Current(); 418 Isolate* isolate = Isolate::Current();
412 CHECK_ISOLATE(isolate); 419 CHECK_ISOLATE(isolate);
413 DARTSCOPE_NOCHECKS(isolate); 420 DARTSCOPE_NOCHECKS(isolate);
414 ApiState* state = isolate->api_state(); 421 ApiState* state = isolate->api_state();
415 ASSERT(state != NULL); 422 ASSERT(state != NULL);
416 const Object& old_ref = Object::Handle(Api::UnwrapHandle(object)); 423 const Object& old_ref = Object::Handle(isolate, Api::UnwrapHandle(object));
417 PersistentHandle* new_ref = state->persistent_handles().AllocateHandle(); 424 PersistentHandle* new_ref = state->persistent_handles().AllocateHandle();
418 new_ref->set_raw(old_ref); 425 new_ref->set_raw(old_ref);
419 return reinterpret_cast<Dart_Handle>(new_ref); 426 return reinterpret_cast<Dart_Handle>(new_ref);
420 } 427 }
421 428
422 static Dart_Handle AllocateFinalizableHandle( 429 static Dart_Handle AllocateFinalizableHandle(
430 Isolate* isolate,
423 FinalizablePersistentHandles* handles, 431 FinalizablePersistentHandles* handles,
424 Dart_Handle object, 432 Dart_Handle object,
425 void* peer, 433 void* peer,
426 Dart_WeakPersistentHandleFinalizer callback) { 434 Dart_WeakPersistentHandleFinalizer callback) {
427 const Object& ref = Object::Handle(Api::UnwrapHandle(object)); 435 const Object& ref = Object::Handle(isolate, Api::UnwrapHandle(object));
428 FinalizablePersistentHandle* finalizable_ref = handles->AllocateHandle(); 436 FinalizablePersistentHandle* finalizable_ref = handles->AllocateHandle();
429 finalizable_ref->set_raw(ref); 437 finalizable_ref->set_raw(ref);
430 finalizable_ref->set_peer(peer); 438 finalizable_ref->set_peer(peer);
431 finalizable_ref->set_callback(callback); 439 finalizable_ref->set_callback(callback);
432 return reinterpret_cast<Dart_Handle>(finalizable_ref); 440 return reinterpret_cast<Dart_Handle>(finalizable_ref);
433 } 441 }
434 442
435 443
436 DART_EXPORT Dart_Handle Dart_NewWeakPersistentHandle( 444 DART_EXPORT Dart_Handle Dart_NewWeakPersistentHandle(
437 Dart_Handle object, 445 Dart_Handle object,
438 void* peer, 446 void* peer,
439 Dart_WeakPersistentHandleFinalizer callback) { 447 Dart_WeakPersistentHandleFinalizer callback) {
440 Isolate* isolate = Isolate::Current(); 448 Isolate* isolate = Isolate::Current();
441 CHECK_ISOLATE(isolate); 449 CHECK_ISOLATE(isolate);
442 DARTSCOPE_NOCHECKS(isolate); 450 DARTSCOPE_NOCHECKS(isolate);
443 ApiState* state = isolate->api_state(); 451 ApiState* state = isolate->api_state();
444 ASSERT(state != NULL); 452 ASSERT(state != NULL);
445 return AllocateFinalizableHandle(&state->weak_persistent_handles(), 453 return AllocateFinalizableHandle(isolate,
454 &state->weak_persistent_handles(),
446 object, 455 object,
447 peer, 456 peer,
448 callback); 457 callback);
449 } 458 }
450 459
451 460
452 DART_EXPORT Dart_Handle Dart_NewPrologueWeakPersistentHandle( 461 DART_EXPORT Dart_Handle Dart_NewPrologueWeakPersistentHandle(
453 Dart_Handle object, 462 Dart_Handle object,
454 void* peer, 463 void* peer,
455 Dart_WeakPersistentHandleFinalizer callback) { 464 Dart_WeakPersistentHandleFinalizer callback) {
456 Isolate* isolate = Isolate::Current(); 465 Isolate* isolate = Isolate::Current();
457 CHECK_ISOLATE(isolate); 466 CHECK_ISOLATE(isolate);
458 DARTSCOPE_NOCHECKS(isolate); 467 DARTSCOPE_NOCHECKS(isolate);
459 ApiState* state = isolate->api_state(); 468 ApiState* state = isolate->api_state();
460 ASSERT(state != NULL); 469 ASSERT(state != NULL);
461 return AllocateFinalizableHandle(&state->prologue_weak_persistent_handles(), 470 return AllocateFinalizableHandle(isolate,
471 &state->prologue_weak_persistent_handles(),
462 object, 472 object,
463 peer, 473 peer,
464 callback); 474 callback);
465 } 475 }
466 476
467 477
468 DART_EXPORT void Dart_DeletePersistentHandle(Dart_Handle object) { 478 DART_EXPORT void Dart_DeletePersistentHandle(Dart_Handle object) {
469 Isolate* isolate = Isolate::Current(); 479 Isolate* isolate = Isolate::Current();
470 CHECK_ISOLATE(isolate); 480 CHECK_ISOLATE(isolate);
471 ApiState* state = isolate->api_state(); 481 ApiState* state = isolate->api_state();
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
531 } 541 }
532 if (num_values <= 0) { 542 if (num_values <= 0) {
533 return Api::NewError( 543 return Api::NewError(
534 "%s expects argument 'num_values' to be greater than 0.", 544 "%s expects argument 'num_values' to be greater than 0.",
535 CURRENT_FUNC); 545 CURRENT_FUNC);
536 } 546 }
537 547
538 WeakReference* reference = new WeakReference(keys, num_keys, 548 WeakReference* reference = new WeakReference(keys, num_keys,
539 values, num_values); 549 values, num_values);
540 state->DelayWeakReference(reference); 550 state->DelayWeakReference(reference);
541 return Api::Success(); 551 return Api::Success(isolate);
542 } 552 }
543 553
544 554
545 // --- Garbage Collection Callbacks -- 555 // --- Garbage Collection Callbacks --
546 556
547 557
548 DART_EXPORT Dart_Handle Dart_AddGcPrologueCallback( 558 DART_EXPORT Dart_Handle Dart_AddGcPrologueCallback(
549 Dart_GcPrologueCallback callback) { 559 Dart_GcPrologueCallback callback) {
550 Isolate* isolate = Isolate::Current(); 560 Isolate* isolate = Isolate::Current();
551 CHECK_ISOLATE(isolate); 561 CHECK_ISOLATE(isolate);
552 GcPrologueCallbacks& callbacks = isolate->gc_prologue_callbacks(); 562 GcPrologueCallbacks& callbacks = isolate->gc_prologue_callbacks();
553 if (callbacks.Contains(callback)) { 563 if (callbacks.Contains(callback)) {
554 return Api::NewError( 564 return Api::NewError(
555 "%s permits only one instance of 'callback' to be present in the " 565 "%s permits only one instance of 'callback' to be present in the "
556 "prologue callback list.", 566 "prologue callback list.",
557 CURRENT_FUNC); 567 CURRENT_FUNC);
558 } 568 }
559 callbacks.Add(callback); 569 callbacks.Add(callback);
560 return Api::Success(); 570 return Api::Success(isolate);
561 } 571 }
562 572
563 573
564 DART_EXPORT Dart_Handle Dart_RemoveGcPrologueCallback( 574 DART_EXPORT Dart_Handle Dart_RemoveGcPrologueCallback(
565 Dart_GcPrologueCallback callback) { 575 Dart_GcPrologueCallback callback) {
566 Isolate* isolate = Isolate::Current(); 576 Isolate* isolate = Isolate::Current();
567 CHECK_ISOLATE(isolate); 577 CHECK_ISOLATE(isolate);
568 GcPrologueCallbacks& callbacks = isolate->gc_prologue_callbacks(); 578 GcPrologueCallbacks& callbacks = isolate->gc_prologue_callbacks();
569 if (!callbacks.Contains(callback)) { 579 if (!callbacks.Contains(callback)) {
570 return Api::NewError( 580 return Api::NewError(
571 "%s expects 'callback' to be present in the prologue callback list.", 581 "%s expects 'callback' to be present in the prologue callback list.",
572 CURRENT_FUNC); 582 CURRENT_FUNC);
573 } 583 }
574 callbacks.Remove(callback); 584 callbacks.Remove(callback);
575 return Api::Success(); 585 return Api::Success(isolate);
576 } 586 }
577 587
578 588
579 DART_EXPORT Dart_Handle Dart_AddGcEpilogueCallback( 589 DART_EXPORT Dart_Handle Dart_AddGcEpilogueCallback(
580 Dart_GcEpilogueCallback callback) { 590 Dart_GcEpilogueCallback callback) {
581 Isolate* isolate = Isolate::Current(); 591 Isolate* isolate = Isolate::Current();
582 CHECK_ISOLATE(isolate); 592 CHECK_ISOLATE(isolate);
583 GcEpilogueCallbacks& callbacks = isolate->gc_epilogue_callbacks(); 593 GcEpilogueCallbacks& callbacks = isolate->gc_epilogue_callbacks();
584 if (callbacks.Contains(callback)) { 594 if (callbacks.Contains(callback)) {
585 return Api::NewError( 595 return Api::NewError(
586 "%s permits only one instance of 'callback' to be present in the " 596 "%s permits only one instance of 'callback' to be present in the "
587 "epilogue callback list.", 597 "epilogue callback list.",
588 CURRENT_FUNC); 598 CURRENT_FUNC);
589 } 599 }
590 callbacks.Add(callback); 600 callbacks.Add(callback);
591 return Api::Success(); 601 return Api::Success(isolate);
592 } 602 }
593 603
594 604
595 DART_EXPORT Dart_Handle Dart_RemoveGcEpilogueCallback( 605 DART_EXPORT Dart_Handle Dart_RemoveGcEpilogueCallback(
596 Dart_GcEpilogueCallback callback) { 606 Dart_GcEpilogueCallback callback) {
597 Isolate* isolate = Isolate::Current(); 607 Isolate* isolate = Isolate::Current();
598 CHECK_ISOLATE(isolate); 608 CHECK_ISOLATE(isolate);
599 GcEpilogueCallbacks& callbacks = isolate->gc_epilogue_callbacks(); 609 GcEpilogueCallbacks& callbacks = isolate->gc_epilogue_callbacks();
600 if (!callbacks.Contains(callback)) { 610 if (!callbacks.Contains(callback)) {
601 return Api::NewError( 611 return Api::NewError(
602 "%s expects 'callback' to be present in the epilogue callback list.", 612 "%s expects 'callback' to be present in the epilogue callback list.",
603 CURRENT_FUNC); 613 CURRENT_FUNC);
604 } 614 }
605 callbacks.Remove(callback); 615 callbacks.Remove(callback);
606 return Api::Success(); 616 return Api::Success(isolate);
607 } 617 }
608 618
609 619
610 // --- Initialization and Globals --- 620 // --- Initialization and Globals ---
611 621
612 622
613 DART_EXPORT bool Dart_Initialize(Dart_IsolateCreateCallback create, 623 DART_EXPORT bool Dart_Initialize(Dart_IsolateCreateCallback create,
614 Dart_IsolateInterruptCallback interrupt) { 624 Dart_IsolateInterruptCallback interrupt) {
615 return Dart::InitOnce(create, interrupt); 625 return Dart::InitOnce(create, interrupt);
616 } 626 }
(...skipping 14 matching lines...) Expand all
631 641
632 642
633 DART_EXPORT Dart_Isolate Dart_CreateIsolate(const char* name_prefix, 643 DART_EXPORT Dart_Isolate Dart_CreateIsolate(const char* name_prefix,
634 const uint8_t* snapshot, 644 const uint8_t* snapshot,
635 void* callback_data, 645 void* callback_data,
636 char** error) { 646 char** error) {
637 Isolate* isolate = Dart::CreateIsolate(name_prefix); 647 Isolate* isolate = Dart::CreateIsolate(name_prefix);
638 { 648 {
639 DARTSCOPE_NOCHECKS(isolate); 649 DARTSCOPE_NOCHECKS(isolate);
640 const Error& error_obj = 650 const Error& error_obj =
641 Error::Handle(Dart::InitializeIsolate(snapshot, callback_data)); 651 Error::Handle(isolate,
652 Dart::InitializeIsolate(snapshot, callback_data));
642 if (error_obj.IsNull()) { 653 if (error_obj.IsNull()) {
643 START_TIMER(time_total_runtime); 654 START_TIMER(time_total_runtime);
644 return reinterpret_cast<Dart_Isolate>(isolate); 655 return reinterpret_cast<Dart_Isolate>(isolate);
645 } 656 }
646 *error = strdup(error_obj.ToErrorCString()); 657 *error = strdup(error_obj.ToErrorCString());
647 } 658 }
648 Dart::ShutdownIsolate(); 659 Dart::ShutdownIsolate();
649 return reinterpret_cast<Dart_Isolate>(NULL); 660 return reinterpret_cast<Dart_Isolate>(NULL);
650 } 661 }
651 662
(...skipping 16 matching lines...) Expand all
668 Isolate::SetCurrent(isolate); 679 Isolate::SetCurrent(isolate);
669 } 680 }
670 681
671 682
672 DART_EXPORT void Dart_ExitIsolate() { 683 DART_EXPORT void Dart_ExitIsolate() {
673 CHECK_ISOLATE(Isolate::Current()); 684 CHECK_ISOLATE(Isolate::Current());
674 Isolate::SetCurrent(NULL); 685 Isolate::SetCurrent(NULL);
675 } 686 }
676 687
677 688
678 static uint8_t* ApiAllocator(uint8_t* ptr, 689 static uint8_t* ApiReallocate(uint8_t* ptr,
679 intptr_t old_size, 690 intptr_t old_size,
680 intptr_t new_size) { 691 intptr_t new_size) {
681 uword new_ptr = Api::Reallocate(reinterpret_cast<uword>(ptr), 692 uword new_ptr = Api::Reallocate(Isolate::Current(),
693 reinterpret_cast<uword>(ptr),
682 old_size, 694 old_size,
683 new_size); 695 new_size);
684 return reinterpret_cast<uint8_t*>(new_ptr); 696 return reinterpret_cast<uint8_t*>(new_ptr);
685 } 697 }
686 698
687 699
688 DART_EXPORT Dart_Handle Dart_CreateSnapshot(uint8_t** buffer, 700 DART_EXPORT Dart_Handle Dart_CreateSnapshot(uint8_t** buffer,
689 intptr_t* size) { 701 intptr_t* size) {
690 Isolate* isolate = Isolate::Current(); 702 Isolate* isolate = Isolate::Current();
691 DARTSCOPE(isolate); 703 DARTSCOPE(isolate);
692 TIMERSCOPE(time_creating_snapshot); 704 TIMERSCOPE(time_creating_snapshot);
693 if (buffer == NULL) { 705 if (buffer == NULL) {
694 return Api::NewError("%s expects argument 'buffer' to be non-null.", 706 return Api::NewError("%s expects argument 'buffer' to be non-null.",
695 CURRENT_FUNC); 707 CURRENT_FUNC);
696 } 708 }
697 if (size == NULL) { 709 if (size == NULL) {
698 return Api::NewError("%s expects argument 'size' to be non-null.", 710 return Api::NewError("%s expects argument 'size' to be non-null.",
699 CURRENT_FUNC); 711 CURRENT_FUNC);
700 } 712 }
701 const char* msg = CheckIsolateState(isolate, 713 const char* msg = CheckIsolateState(isolate,
702 ClassFinalizer::kGeneratingSnapshot); 714 ClassFinalizer::kGeneratingSnapshot);
703 if (msg != NULL) { 715 if (msg != NULL) {
704 return Api::NewError(msg); 716 return Api::NewError(msg);
705 } 717 }
706 // Since this is only a snapshot the root library should not be set. 718 // Since this is only a snapshot the root library should not be set.
707 isolate->object_store()->set_root_library(Library::Handle()); 719 isolate->object_store()->set_root_library(Library::Handle(isolate));
708 SnapshotWriter writer(Snapshot::kFull, buffer, ApiAllocator); 720 SnapshotWriter writer(Snapshot::kFull, buffer, ApiReallocate);
709 writer.WriteFullSnapshot(); 721 writer.WriteFullSnapshot();
710 *size = writer.BytesWritten(); 722 *size = writer.BytesWritten();
711 return Api::Success(); 723 return Api::Success(isolate);
712 } 724 }
713 725
714 726
715 DART_EXPORT Dart_Handle Dart_CreateScriptSnapshot(uint8_t** buffer, 727 DART_EXPORT Dart_Handle Dart_CreateScriptSnapshot(uint8_t** buffer,
716 intptr_t* size) { 728 intptr_t* size) {
717 Isolate* isolate = Isolate::Current(); 729 Isolate* isolate = Isolate::Current();
718 DARTSCOPE(isolate); 730 DARTSCOPE(isolate);
719 TIMERSCOPE(time_creating_snapshot); 731 TIMERSCOPE(time_creating_snapshot);
720 if (buffer == NULL) { 732 if (buffer == NULL) {
721 return Api::NewError("%s expects argument 'buffer' to be non-null.", 733 return Api::NewError("%s expects argument 'buffer' to be non-null.",
722 CURRENT_FUNC); 734 CURRENT_FUNC);
723 } 735 }
724 if (size == NULL) { 736 if (size == NULL) {
725 return Api::NewError("%s expects argument 'size' to be non-null.", 737 return Api::NewError("%s expects argument 'size' to be non-null.",
726 CURRENT_FUNC); 738 CURRENT_FUNC);
727 } 739 }
728 const char* msg = CheckIsolateState(isolate); 740 const char* msg = CheckIsolateState(isolate);
729 if (msg != NULL) { 741 if (msg != NULL) {
730 return Api::NewError(msg); 742 return Api::NewError(msg);
731 } 743 }
732 Library& library = Library::Handle(isolate->object_store()->root_library()); 744 Library& library =
745 Library::Handle(isolate, isolate->object_store()->root_library());
733 if (library.IsNull()) { 746 if (library.IsNull()) {
734 return 747 return
735 Api::NewError("%s expects the isolate to have a script loaded in it.", 748 Api::NewError("%s expects the isolate to have a script loaded in it.",
736 CURRENT_FUNC); 749 CURRENT_FUNC);
737 } 750 }
738 ScriptSnapshotWriter writer(buffer, ApiAllocator); 751 ScriptSnapshotWriter writer(buffer, ApiReallocate);
739 writer.WriteScriptSnapshot(library); 752 writer.WriteScriptSnapshot(library);
740 *size = writer.BytesWritten(); 753 *size = writer.BytesWritten();
741 return Api::Success(); 754 return Api::Success(isolate);
742 } 755 }
743 756
744 757
745 DART_EXPORT void Dart_InterruptIsolate(Dart_Isolate isolate) { 758 DART_EXPORT void Dart_InterruptIsolate(Dart_Isolate isolate) {
746 if (isolate == NULL) { 759 if (isolate == NULL) {
747 FATAL1("%s expects argument 'isolate' to be non-null.", CURRENT_FUNC); 760 FATAL1("%s expects argument 'isolate' to be non-null.", CURRENT_FUNC);
748 } 761 }
749 Isolate* iso = reinterpret_cast<Isolate*>(isolate); 762 Isolate* iso = reinterpret_cast<Isolate*>(isolate);
750 iso->ScheduleInterrupts(Isolate::kApiInterrupt); 763 iso->ScheduleInterrupts(Isolate::kApiInterrupt);
751 } 764 }
752 765
753 766
754 // --- Messages and Ports --- 767 // --- Messages and Ports ---
755 768
756 769
757 DART_EXPORT void Dart_SetMessageNotifyCallback( 770 DART_EXPORT void Dart_SetMessageNotifyCallback(
758 Dart_MessageNotifyCallback message_notify_callback) { 771 Dart_MessageNotifyCallback message_notify_callback) {
759 Isolate* isolate = Isolate::Current(); 772 Isolate* isolate = Isolate::Current();
760 CHECK_ISOLATE(isolate); 773 CHECK_ISOLATE(isolate);
761 isolate->set_message_notify_callback(message_notify_callback); 774 isolate->set_message_notify_callback(message_notify_callback);
762 } 775 }
763 776
764 777
765 DART_EXPORT Dart_Handle Dart_RunLoop() { 778 DART_EXPORT Dart_Handle Dart_RunLoop() {
766 Isolate* isolate = Isolate::Current(); 779 Isolate* isolate = Isolate::Current();
767 DARTSCOPE(isolate); 780 DARTSCOPE(isolate);
768 const Object& obj = Object::Handle(isolate->StandardRunLoop()); 781 const Object& obj = Object::Handle(isolate, isolate->StandardRunLoop());
769 if (obj.IsError()) { 782 if (obj.IsError()) {
770 return Api::NewLocalHandle(obj); 783 return Api::NewLocalHandle(isolate, obj);
771 } 784 }
772 ASSERT(obj.IsNull()); 785 ASSERT(obj.IsNull());
773 return Api::Success(); 786 return Api::Success(isolate);
774 } 787 }
775 788
776 789
777 static RawInstance* DeserializeMessage(void* data) { 790 static RawInstance* DeserializeMessage(Isolate* isolate, void* data) {
778 // Create a snapshot object using the buffer. 791 // Create a snapshot object using the buffer.
779 const Snapshot* snapshot = Snapshot::SetupFromBuffer(data); 792 const Snapshot* snapshot = Snapshot::SetupFromBuffer(data);
780 ASSERT(snapshot->IsMessageSnapshot()); 793 ASSERT(snapshot->IsMessageSnapshot());
781 794
782 // Read object back from the snapshot. 795 // Read object back from the snapshot.
783 SnapshotReader reader(snapshot, Isolate::Current()); 796 SnapshotReader reader(snapshot, isolate);
784 Instance& instance = Instance::Handle(); 797 Instance& instance = Instance::Handle(isolate);
785 instance ^= reader.ReadObject(); 798 instance ^= reader.ReadObject();
786 return instance.raw(); 799 return instance.raw();
787 } 800 }
788 801
789 802
790 DART_EXPORT Dart_Handle Dart_HandleMessage() { 803 DART_EXPORT Dart_Handle Dart_HandleMessage() {
791 Isolate* isolate = Isolate::Current(); 804 Isolate* isolate = Isolate::Current();
792 // Process all OOB messages and at most one normal message. 805 // Process all OOB messages and at most one normal message.
793 Message* message = NULL; 806 Message* message = NULL;
794 Message::Priority priority = Message::kNormalPriority; 807 Message::Priority priority = Message::kNormalPriority;
795 do { 808 do {
796 DARTSCOPE(isolate); 809 DARTSCOPE(isolate);
797 // TODO(turnidge): This code is duplicated elsewhere. Consolidate. 810 // TODO(turnidge): This code is duplicated elsewhere. Consolidate.
798 message = isolate->message_handler()->queue()->DequeueNoWait(); 811 message = isolate->message_handler()->queue()->DequeueNoWait();
799 if (message == NULL) { 812 if (message == NULL) {
800 break; 813 break;
801 } 814 }
802 const Instance& msg = 815 const Instance& msg =
803 Instance::Handle(DeserializeMessage(message->data())); 816 Instance::Handle(isolate, DeserializeMessage(isolate, message->data()));
804 priority = message->priority(); 817 priority = message->priority();
805 if (priority == Message::kOOBPriority) { 818 if (priority == Message::kOOBPriority) {
806 // For now the only OOB messages are Mirrors messages. 819 // For now the only OOB messages are Mirrors messages.
807 const Object& result = Object::Handle( 820 const Object& result = Object::Handle(
821 isolate,
808 DartLibraryCalls::HandleMirrorsMessage( 822 DartLibraryCalls::HandleMirrorsMessage(
809 message->dest_port(), message->reply_port(), msg)); 823 message->dest_port(), message->reply_port(), msg));
810 delete message; 824 delete message;
811 if (result.IsError()) { 825 if (result.IsError()) {
812 // TODO(turnidge): Propagating the error is probably wrong here. 826 // TODO(turnidge): Propagating the error is probably wrong here.
813 return Api::NewLocalHandle(result); 827 return Api::NewLocalHandle(isolate, result);
814 } 828 }
815 ASSERT(result.IsNull()); 829 ASSERT(result.IsNull());
816 } else { 830 } else {
817 const Object& result = Object::Handle( 831 const Object& result = Object::Handle(
832 isolate,
818 DartLibraryCalls::HandleMessage( 833 DartLibraryCalls::HandleMessage(
819 message->dest_port(), message->reply_port(), msg)); 834 message->dest_port(), message->reply_port(), msg));
820 delete message; 835 delete message;
821 if (result.IsError()) { 836 if (result.IsError()) {
822 return Api::NewLocalHandle(result); 837 return Api::NewLocalHandle(isolate, result);
823 } 838 }
824 ASSERT(result.IsNull()); 839 ASSERT(result.IsNull());
825 } 840 }
826 } while (priority >= Message::kOOBPriority); 841 } while (priority >= Message::kOOBPriority);
827 return Api::Success(); 842 return Api::Success(isolate);
828 } 843 }
829 844
830 845
831 DART_EXPORT bool Dart_HasLivePorts() { 846 DART_EXPORT bool Dart_HasLivePorts() {
832 Isolate* isolate = Isolate::Current(); 847 Isolate* isolate = Isolate::Current();
833 ASSERT(isolate); 848 ASSERT(isolate);
834 return isolate->message_handler()->HasLivePorts(); 849 return isolate->message_handler()->HasLivePorts();
835 } 850 }
836 851
837 852
(...skipping 26 matching lines...) Expand all
864 // Post the message at the given port. 879 // Post the message at the given port.
865 return PortMap::PostMessage(new Message( 880 return PortMap::PostMessage(new Message(
866 port_id, Message::kIllegalPort, buffer, Message::kNormalPriority)); 881 port_id, Message::kIllegalPort, buffer, Message::kNormalPriority));
867 } 882 }
868 883
869 884
870 DART_EXPORT bool Dart_Post(Dart_Port port_id, Dart_Handle handle) { 885 DART_EXPORT bool Dart_Post(Dart_Port port_id, Dart_Handle handle) {
871 Isolate* isolate = Isolate::Current(); 886 Isolate* isolate = Isolate::Current();
872 CHECK_ISOLATE(isolate); 887 CHECK_ISOLATE(isolate);
873 DARTSCOPE_NOCHECKS(isolate); 888 DARTSCOPE_NOCHECKS(isolate);
874 const Object& object = Object::Handle(Api::UnwrapHandle(handle)); 889 const Object& object = Object::Handle(isolate, Api::UnwrapHandle(handle));
875 uint8_t* data = NULL; 890 uint8_t* data = NULL;
876 SnapshotWriter writer(Snapshot::kMessage, &data, &allocator); 891 SnapshotWriter writer(Snapshot::kMessage, &data, &allocator);
877 writer.WriteObject(object.raw()); 892 writer.WriteObject(object.raw());
878 writer.FinalizeBuffer(); 893 writer.FinalizeBuffer();
879 return PortMap::PostMessage(new Message( 894 return PortMap::PostMessage(new Message(
880 port_id, Message::kIllegalPort, data, Message::kNormalPriority)); 895 port_id, Message::kIllegalPort, data, Message::kNormalPriority));
881 } 896 }
882 897
883 898
884 DART_EXPORT Dart_Port Dart_NewNativePort(const char* name, 899 DART_EXPORT Dart_Port Dart_NewNativePort(const char* name,
(...skipping 23 matching lines...) Expand all
908 Isolate::SetCurrent(NULL); 923 Isolate::SetCurrent(NULL);
909 924
910 // TODO(turnidge): Check that the port is native before trying to close. 925 // TODO(turnidge): Check that the port is native before trying to close.
911 return PortMap::ClosePort(native_port_id); 926 return PortMap::ClosePort(native_port_id);
912 } 927 }
913 928
914 929
915 DART_EXPORT Dart_Handle Dart_NewSendPort(Dart_Port port_id) { 930 DART_EXPORT Dart_Handle Dart_NewSendPort(Dart_Port port_id) {
916 Isolate* isolate = Isolate::Current(); 931 Isolate* isolate = Isolate::Current();
917 DARTSCOPE(isolate); 932 DARTSCOPE(isolate);
918 const Object& result = Object::Handle(DartLibraryCalls::NewSendPort(port_id)); 933 const Object& result =
919 return Api::NewLocalHandle(result); 934 Object::Handle(isolate, DartLibraryCalls::NewSendPort(port_id));
935 return Api::NewLocalHandle(isolate, result);
920 } 936 }
921 937
922 938
923 DART_EXPORT Dart_Handle Dart_GetReceivePort(Dart_Port port_id) { 939 DART_EXPORT Dart_Handle Dart_GetReceivePort(Dart_Port port_id) {
924 Isolate* isolate = Isolate::Current(); 940 Isolate* isolate = Isolate::Current();
925 DARTSCOPE(isolate); 941 DARTSCOPE(isolate);
926 Library& isolate_lib = Library::Handle(Library::IsolateLibrary()); 942 Library& isolate_lib = Library::Handle(isolate, Library::IsolateLibrary());
927 ASSERT(!isolate_lib.IsNull()); 943 ASSERT(!isolate_lib.IsNull());
928 const String& public_class_name = 944 const String& public_class_name =
929 String::Handle(String::New("_ReceivePortImpl")); 945 String::Handle(isolate, String::New("_ReceivePortImpl"));
930 const String& class_name = 946 const String& class_name =
931 String::Handle(isolate_lib.PrivateName(public_class_name)); 947 String::Handle(isolate, isolate_lib.PrivateName(public_class_name));
932 const String& function_name = 948 const String& function_name =
933 String::Handle(String::NewSymbol("_get_or_create")); 949 String::Handle(isolate, String::NewSymbol("_get_or_create"));
934 const int kNumArguments = 1; 950 const int kNumArguments = 1;
935 const Array& kNoArgumentNames = Array::Handle(); 951 const Array& kNoArgumentNames = Array::Handle(isolate);
936 const Function& function = Function::Handle( 952 const Function& function = Function::Handle(
953 isolate,
937 Resolver::ResolveStatic(isolate_lib, 954 Resolver::ResolveStatic(isolate_lib,
938 class_name, 955 class_name,
939 function_name, 956 function_name,
940 kNumArguments, 957 kNumArguments,
941 kNoArgumentNames, 958 kNoArgumentNames,
942 Resolver::kIsQualified)); 959 Resolver::kIsQualified));
943 GrowableArray<const Object*> arguments(kNumArguments); 960 GrowableArray<const Object*> arguments(kNumArguments);
944 arguments.Add(&Integer::Handle(Integer::New(port_id))); 961 arguments.Add(&Integer::Handle(isolate, Integer::New(port_id)));
945 const Object& result = Object::Handle( 962 const Object& result = Object::Handle(
946 DartEntry::InvokeStatic(function, arguments, kNoArgumentNames)); 963 isolate, DartEntry::InvokeStatic(function, arguments, kNoArgumentNames));
947 return Api::NewLocalHandle(result); 964 return Api::NewLocalHandle(isolate, result);
948 } 965 }
949 966
950 967
951 DART_EXPORT Dart_Port Dart_GetMainPortId() { 968 DART_EXPORT Dart_Port Dart_GetMainPortId() {
952 Isolate* isolate = Isolate::Current(); 969 Isolate* isolate = Isolate::Current();
953 CHECK_ISOLATE(isolate); 970 CHECK_ISOLATE(isolate);
954 return isolate->main_port(); 971 return isolate->main_port();
955 } 972 }
956 973
957 // --- Scopes ---- 974 // --- Scopes ----
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
994 zone = scope->zone(); 1011 zone = scope->zone();
995 } 1012 }
996 return reinterpret_cast<uint8_t*>(zone->Allocate(size)); 1013 return reinterpret_cast<uint8_t*>(zone->Allocate(size));
997 } 1014 }
998 1015
999 1016
1000 // --- Objects ---- 1017 // --- Objects ----
1001 1018
1002 1019
1003 DART_EXPORT Dart_Handle Dart_Null() { 1020 DART_EXPORT Dart_Handle Dart_Null() {
1004 CHECK_ISOLATE_SCOPE(Isolate::Current()); 1021 Isolate* isolate = Isolate::Current();
1005 return Api::Null(); 1022 CHECK_ISOLATE_SCOPE(isolate);
1023 return Api::Null(isolate);
1006 } 1024 }
1007 1025
1008 1026
1009 DART_EXPORT bool Dart_IsNull(Dart_Handle object) { 1027 DART_EXPORT bool Dart_IsNull(Dart_Handle object) {
1010 DARTSCOPE(Isolate::Current()); 1028 Isolate* isolate = Isolate::Current();
1011 const Object& obj = Object::Handle(Api::UnwrapHandle(object)); 1029 DARTSCOPE(isolate);
1030 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(object));
1012 return obj.IsNull(); 1031 return obj.IsNull();
1013 } 1032 }
1014 1033
1015 1034
1016 DART_EXPORT Dart_Handle Dart_ObjectEquals(Dart_Handle obj1, Dart_Handle obj2, 1035 DART_EXPORT Dart_Handle Dart_ObjectEquals(Dart_Handle obj1, Dart_Handle obj2,
1017 bool* value) { 1036 bool* value) {
1018 DARTSCOPE(Isolate::Current()); 1037 Isolate* isolate = Isolate::Current();
1019 const Instance& expected = Instance::CheckedHandle(Api::UnwrapHandle(obj1)); 1038 DARTSCOPE(isolate);
1020 const Instance& actual = Instance::CheckedHandle(Api::UnwrapHandle(obj2)); 1039 const Instance& expected =
1040 Instance::CheckedHandle(isolate, Api::UnwrapHandle(obj1));
1041 const Instance& actual =
1042 Instance::CheckedHandle(isolate, Api::UnwrapHandle(obj2));
1021 const Object& result = 1043 const Object& result =
1022 Object::Handle(DartLibraryCalls::Equals(expected, actual)); 1044 Object::Handle(isolate, DartLibraryCalls::Equals(expected, actual));
1023 if (result.IsBool()) { 1045 if (result.IsBool()) {
1024 Bool& b = Bool::Handle(); 1046 Bool& b = Bool::Handle(isolate);
1025 b ^= result.raw(); 1047 b ^= result.raw();
1026 *value = b.value(); 1048 *value = b.value();
1027 return Api::Success(); 1049 return Api::Success(isolate);
1028 } else if (result.IsError()) { 1050 } else if (result.IsError()) {
1029 return Api::NewLocalHandle(result); 1051 return Api::NewLocalHandle(isolate, result);
1030 } else { 1052 } else {
1031 return Api::NewError("Expected boolean result from =="); 1053 return Api::NewError("Expected boolean result from ==");
1032 } 1054 }
1033 } 1055 }
1034 1056
1035 1057
1036 // TODO(iposva): This call actually implements IsInstanceOfClass. 1058 // TODO(iposva): This call actually implements IsInstanceOfClass.
1037 // Do we also need a real Dart_IsInstanceOf, which should take an instance 1059 // Do we also need a real Dart_IsInstanceOf, which should take an instance
1038 // rather than an object and a type rather than a class? 1060 // rather than an object and a type rather than a class?
1039 DART_EXPORT Dart_Handle Dart_ObjectIsType(Dart_Handle object, 1061 DART_EXPORT Dart_Handle Dart_ObjectIsType(Dart_Handle object,
1040 Dart_Handle clazz, 1062 Dart_Handle clazz,
1041 bool* value) { 1063 bool* value) {
1042 Isolate* isolate = Isolate::Current(); 1064 Isolate* isolate = Isolate::Current();
1043 DARTSCOPE(isolate); 1065 DARTSCOPE(isolate);
1044 const Class& cls = Class::CheckedHandle(Api::UnwrapHandle(clazz)); 1066 const Class& cls = Class::CheckedHandle(isolate, Api::UnwrapHandle(clazz));
1045 if (cls.IsNull()) { 1067 if (cls.IsNull()) {
1046 return Api::NewError("instanceof check against null class"); 1068 return Api::NewError("instanceof check against null class");
1047 } 1069 }
1048 const Object& obj = Object::Handle(Api::UnwrapHandle(object)); 1070 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(object));
1049 Instance& instance = Instance::Handle(); 1071 Instance& instance = Instance::Handle(isolate);
1050 instance ^= obj.raw(); 1072 instance ^= obj.raw();
1051 // Finalize all classes. 1073 // Finalize all classes.
1052 const char* msg = CheckIsolateState(isolate); 1074 const char* msg = CheckIsolateState(isolate);
1053 if (msg != NULL) { 1075 if (msg != NULL) {
1054 return Api::NewError(msg); 1076 return Api::NewError(msg);
1055 } 1077 }
1056 const Type& type = Type::Handle(Type::NewNonParameterizedType(cls)); 1078 const Type& type = Type::Handle(isolate, Type::NewNonParameterizedType(cls));
1057 Error& malformed_type_error = Error::Handle(); 1079 Error& malformed_type_error = Error::Handle(isolate);
1058 *value = instance.IsInstanceOf( 1080 *value = instance.IsInstanceOf(
1059 type, TypeArguments::Handle(), &malformed_type_error); 1081 type, TypeArguments::Handle(isolate), &malformed_type_error);
1060 ASSERT(malformed_type_error.IsNull()); // Type was created here from a class. 1082 ASSERT(malformed_type_error.IsNull()); // Type was created here from a class.
1061 return Api::Success(); 1083 return Api::Success(isolate);
1062 } 1084 }
1063 1085
1064 1086
1065 // --- Numbers ---- 1087 // --- Numbers ----
1066 1088
1067 1089
1068 // TODO(iposva): The argument should be an instance. 1090 // TODO(iposva): The argument should be an instance.
1069 DART_EXPORT bool Dart_IsNumber(Dart_Handle object) { 1091 DART_EXPORT bool Dart_IsNumber(Dart_Handle object) {
1070 DARTSCOPE(Isolate::Current()); 1092 Isolate* isolate = Isolate::Current();
1071 const Object& obj = Object::Handle(Api::UnwrapHandle(object)); 1093 DARTSCOPE(isolate);
1094 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(object));
1072 return obj.IsNumber(); 1095 return obj.IsNumber();
1073 } 1096 }
1074 1097
1075 1098
1076 // --- Integers ---- 1099 // --- Integers ----
1077 1100
1078 1101
1079 DART_EXPORT bool Dart_IsInteger(Dart_Handle object) { 1102 DART_EXPORT bool Dart_IsInteger(Dart_Handle object) {
1080 DARTSCOPE(Isolate::Current()); 1103 Isolate* isolate = Isolate::Current();
1081 const Object& obj = Object::Handle(Api::UnwrapHandle(object)); 1104 DARTSCOPE(isolate);
1105 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(object));
1082 return obj.IsInteger(); 1106 return obj.IsInteger();
1083 } 1107 }
1084 1108
1085 1109
1086 DART_EXPORT Dart_Handle Dart_IntegerFitsIntoInt64(Dart_Handle integer, 1110 DART_EXPORT Dart_Handle Dart_IntegerFitsIntoInt64(Dart_Handle integer,
1087 bool* fits) { 1111 bool* fits) {
1088 DARTSCOPE(Isolate::Current()); 1112 Isolate* isolate = Isolate::Current();
1089 const Integer& int_obj = Api::UnwrapIntegerHandle(integer); 1113 DARTSCOPE(isolate);
1114 const Integer& int_obj = Api::UnwrapIntegerHandle(isolate, integer);
1090 if (int_obj.IsNull()) { 1115 if (int_obj.IsNull()) {
1091 RETURN_TYPE_ERROR(integer, Integer); 1116 RETURN_TYPE_ERROR(isolate, integer, Integer);
1092 } 1117 }
1093 if (int_obj.IsSmi() || int_obj.IsMint()) { 1118 if (int_obj.IsSmi() || int_obj.IsMint()) {
1094 *fits = true; 1119 *fits = true;
1095 } else { 1120 } else {
1096 ASSERT(int_obj.IsBigint()); 1121 ASSERT(int_obj.IsBigint());
1097 #if defined(DEBUG) 1122 #if defined(DEBUG)
1098 Bigint& bigint = Bigint::Handle(); 1123 Bigint& bigint = Bigint::Handle(isolate);
1099 bigint ^= int_obj.raw(); 1124 bigint ^= int_obj.raw();
1100 ASSERT(!BigintOperations::FitsIntoMint(bigint)); 1125 ASSERT(!BigintOperations::FitsIntoMint(bigint));
1101 #endif 1126 #endif
1102 *fits = false; 1127 *fits = false;
1103 } 1128 }
1104 return Api::Success(); 1129 return Api::Success(isolate);
1105 } 1130 }
1106 1131
1107 1132
1108 DART_EXPORT Dart_Handle Dart_IntegerFitsIntoUint64(Dart_Handle integer, 1133 DART_EXPORT Dart_Handle Dart_IntegerFitsIntoUint64(Dart_Handle integer,
1109 bool* fits) { 1134 bool* fits) {
1110 DARTSCOPE(Isolate::Current()); 1135 Isolate* isolate = Isolate::Current();
1111 const Integer& int_obj = Api::UnwrapIntegerHandle(integer); 1136 DARTSCOPE(isolate);
1137 const Integer& int_obj = Api::UnwrapIntegerHandle(isolate, integer);
1112 if (int_obj.IsNull()) { 1138 if (int_obj.IsNull()) {
1113 RETURN_TYPE_ERROR(integer, Integer); 1139 RETURN_TYPE_ERROR(isolate, integer, Integer);
1114 } 1140 }
1115 if (int_obj.IsSmi() || int_obj.IsMint()) { 1141 if (int_obj.IsSmi() || int_obj.IsMint()) {
1116 *fits = !int_obj.IsNegative(); 1142 *fits = !int_obj.IsNegative();
1117 } else { 1143 } else {
1118 ASSERT(int_obj.IsBigint()); 1144 ASSERT(int_obj.IsBigint());
1119 Bigint& bigint = Bigint::Handle(); 1145 Bigint& bigint = Bigint::Handle(isolate);
1120 bigint ^= int_obj.raw(); 1146 bigint ^= int_obj.raw();
1121 *fits = BigintOperations::FitsIntoUint64(bigint); 1147 *fits = BigintOperations::FitsIntoUint64(bigint);
1122 } 1148 }
1123 return Api::Success(); 1149 return Api::Success(isolate);
1124 } 1150 }
1125 1151
1126 1152
1127 DART_EXPORT Dart_Handle Dart_NewInteger(int64_t value) { 1153 DART_EXPORT Dart_Handle Dart_NewInteger(int64_t value) {
1128 DARTSCOPE(Isolate::Current()); 1154 Isolate* isolate = Isolate::Current();
1129 const Integer& obj = Integer::Handle(Integer::New(value)); 1155 DARTSCOPE(isolate);
1130 return Api::NewLocalHandle(obj); 1156 const Integer& obj = Integer::Handle(isolate, Integer::New(value));
1157 return Api::NewLocalHandle(isolate, obj);
1131 } 1158 }
1132 1159
1133 1160
1134 DART_EXPORT Dart_Handle Dart_NewIntegerFromHexCString(const char* str) { 1161 DART_EXPORT Dart_Handle Dart_NewIntegerFromHexCString(const char* str) {
1135 DARTSCOPE(Isolate::Current()); 1162 Isolate* isolate = Isolate::Current();
1136 const String& str_obj = String::Handle(String::New(str)); 1163 DARTSCOPE(isolate);
1137 const Integer& obj = Integer::Handle(Integer::New(str_obj)); 1164 const String& str_obj = String::Handle(isolate, String::New(str));
1138 return Api::NewLocalHandle(obj); 1165 const Integer& obj = Integer::Handle(isolate, Integer::New(str_obj));
1166 return Api::NewLocalHandle(isolate, obj);
1139 } 1167 }
1140 1168
1141 1169
1142 DART_EXPORT Dart_Handle Dart_IntegerToInt64(Dart_Handle integer, 1170 DART_EXPORT Dart_Handle Dart_IntegerToInt64(Dart_Handle integer,
1143 int64_t* value) { 1171 int64_t* value) {
1144 DARTSCOPE(Isolate::Current()); 1172 Isolate* isolate = Isolate::Current();
1145 const Integer& int_obj = Api::UnwrapIntegerHandle(integer); 1173 DARTSCOPE(isolate);
1174 const Integer& int_obj = Api::UnwrapIntegerHandle(isolate, integer);
1146 if (int_obj.IsNull()) { 1175 if (int_obj.IsNull()) {
1147 RETURN_TYPE_ERROR(integer, Integer); 1176 RETURN_TYPE_ERROR(isolate, integer, Integer);
1148 } 1177 }
1149 if (int_obj.IsSmi() || int_obj.IsMint()) { 1178 if (int_obj.IsSmi() || int_obj.IsMint()) {
1150 *value = int_obj.AsInt64Value(); 1179 *value = int_obj.AsInt64Value();
1151 return Api::Success(); 1180 return Api::Success(isolate);
1152 } else { 1181 } else {
1153 ASSERT(int_obj.IsBigint()); 1182 ASSERT(int_obj.IsBigint());
1154 Bigint& bigint = Bigint::Handle(); 1183 Bigint& bigint = Bigint::Handle(isolate);
1155 bigint ^= int_obj.raw(); 1184 bigint ^= int_obj.raw();
1156 if (BigintOperations::FitsIntoMint(bigint)) { 1185 if (BigintOperations::FitsIntoMint(bigint)) {
1157 *value = BigintOperations::ToMint(bigint); 1186 *value = BigintOperations::ToMint(bigint);
1158 return Api::Success(); 1187 return Api::Success(isolate);
1159 } 1188 }
1160 } 1189 }
1161 return Api::NewError("%s: Integer %s cannot be represented as an int64_t.", 1190 return Api::NewError("%s: Integer %s cannot be represented as an int64_t.",
1162 CURRENT_FUNC, int_obj.ToCString()); 1191 CURRENT_FUNC, int_obj.ToCString());
1163 } 1192 }
1164 1193
1165 1194
1166 DART_EXPORT Dart_Handle Dart_IntegerToUint64(Dart_Handle integer, 1195 DART_EXPORT Dart_Handle Dart_IntegerToUint64(Dart_Handle integer,
1167 uint64_t* value) { 1196 uint64_t* value) {
1168 DARTSCOPE(Isolate::Current()); 1197 Isolate* isolate = Isolate::Current();
1169 const Integer& int_obj = Api::UnwrapIntegerHandle(integer); 1198 DARTSCOPE(isolate);
1199 const Integer& int_obj = Api::UnwrapIntegerHandle(isolate, integer);
1170 if (int_obj.IsNull()) { 1200 if (int_obj.IsNull()) {
1171 RETURN_TYPE_ERROR(integer, Integer); 1201 RETURN_TYPE_ERROR(isolate, integer, Integer);
1172 } 1202 }
1173 if (int_obj.IsSmi() || int_obj.IsMint()) { 1203 if (int_obj.IsSmi() || int_obj.IsMint()) {
1174 if (!int_obj.IsNegative()) { 1204 if (!int_obj.IsNegative()) {
1175 *value = int_obj.AsInt64Value(); 1205 *value = int_obj.AsInt64Value();
1176 return Api::Success(); 1206 return Api::Success(isolate);
1177 } 1207 }
1178 } else { 1208 } else {
1179 ASSERT(int_obj.IsBigint()); 1209 ASSERT(int_obj.IsBigint());
1180 Bigint& bigint = Bigint::Handle(); 1210 Bigint& bigint = Bigint::Handle(isolate);
1181 bigint ^= int_obj.raw(); 1211 bigint ^= int_obj.raw();
1182 if (BigintOperations::FitsIntoUint64(bigint)) { 1212 if (BigintOperations::FitsIntoUint64(bigint)) {
1183 *value = BigintOperations::ToUint64(bigint); 1213 *value = BigintOperations::ToUint64(bigint);
1184 return Api::Success(); 1214 return Api::Success(isolate);
1185 } 1215 }
1186 } 1216 }
1187 return Api::NewError("%s: Integer %s cannot be represented as a uint64_t.", 1217 return Api::NewError("%s: Integer %s cannot be represented as a uint64_t.",
1188 CURRENT_FUNC, int_obj.ToCString()); 1218 CURRENT_FUNC, int_obj.ToCString());
1189 } 1219 }
1190 1220
1191 1221
1222 static uword ApiAllocate(intptr_t size) {
1223 return Api::Allocate(Isolate::Current(), size);
1224 }
1225
1226
1192 DART_EXPORT Dart_Handle Dart_IntegerToHexCString(Dart_Handle integer, 1227 DART_EXPORT Dart_Handle Dart_IntegerToHexCString(Dart_Handle integer,
1193 const char** value) { 1228 const char** value) {
1194 DARTSCOPE(Isolate::Current()); 1229 Isolate* isolate = Isolate::Current();
1195 const Integer& int_obj = Api::UnwrapIntegerHandle(integer); 1230 DARTSCOPE(isolate);
1231 const Integer& int_obj = Api::UnwrapIntegerHandle(isolate, integer);
1196 if (int_obj.IsNull()) { 1232 if (int_obj.IsNull()) {
1197 RETURN_TYPE_ERROR(integer, Integer); 1233 RETURN_TYPE_ERROR(isolate, integer, Integer);
1198 } 1234 }
1199 Bigint& bigint = Bigint::Handle(); 1235 Bigint& bigint = Bigint::Handle(isolate);
1200 if (int_obj.IsSmi() || int_obj.IsMint()) { 1236 if (int_obj.IsSmi() || int_obj.IsMint()) {
1201 bigint ^= BigintOperations::NewFromInt64(int_obj.AsInt64Value()); 1237 bigint ^= BigintOperations::NewFromInt64(int_obj.AsInt64Value());
1202 *value = BigintOperations::ToHexCString(bigint, &Api::Allocate); 1238 *value = BigintOperations::ToHexCString(bigint, ApiAllocate);
1203 } else { 1239 } else {
1204 ASSERT(int_obj.IsBigint()); 1240 ASSERT(int_obj.IsBigint());
1205 bigint ^= int_obj.raw(); 1241 bigint ^= int_obj.raw();
1206 *value = BigintOperations::ToHexCString(bigint, &Api::Allocate); 1242 *value = BigintOperations::ToHexCString(bigint, ApiAllocate);
1207 } 1243 }
1208 return Api::Success(); 1244 return Api::Success(isolate);
1209 } 1245 }
1210 1246
1211 1247
1212 // --- Booleans ---- 1248 // --- Booleans ----
1213 1249
1214 1250
1215 DART_EXPORT Dart_Handle Dart_True() { 1251 DART_EXPORT Dart_Handle Dart_True() {
1216 CHECK_ISOLATE_SCOPE(Isolate::Current()); 1252 Isolate* isolate = Isolate::Current();
1217 return Api::True(); 1253 CHECK_ISOLATE_SCOPE(isolate);
1254 return Api::True(isolate);
1218 } 1255 }
1219 1256
1220 1257
1221 DART_EXPORT Dart_Handle Dart_False() { 1258 DART_EXPORT Dart_Handle Dart_False() {
1222 CHECK_ISOLATE_SCOPE(Isolate::Current()); 1259 Isolate* isolate = Isolate::Current();
1223 return Api::False(); 1260 CHECK_ISOLATE_SCOPE(isolate);
1261 return Api::False(isolate);
1224 } 1262 }
1225 1263
1226 1264
1227 DART_EXPORT bool Dart_IsBoolean(Dart_Handle object) { 1265 DART_EXPORT bool Dart_IsBoolean(Dart_Handle object) {
1228 DARTSCOPE(Isolate::Current()); 1266 Isolate* isolate = Isolate::Current();
1229 const Object& obj = Object::Handle(Api::UnwrapHandle(object)); 1267 DARTSCOPE(isolate);
1268 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(object));
1230 return obj.IsBool(); 1269 return obj.IsBool();
1231 } 1270 }
1232 1271
1233 1272
1234 DART_EXPORT Dart_Handle Dart_NewBoolean(bool value) { 1273 DART_EXPORT Dart_Handle Dart_NewBoolean(bool value) {
1235 CHECK_ISOLATE_SCOPE(Isolate::Current()); 1274 Isolate* isolate = Isolate::Current();
1236 return value ? Api::True() : Api::False(); 1275 CHECK_ISOLATE_SCOPE(isolate);
1276 return value ? Api::True(isolate) : Api::False(isolate);
1237 } 1277 }
1238 1278
1239 1279
1240 DART_EXPORT Dart_Handle Dart_BooleanValue(Dart_Handle boolean_obj, 1280 DART_EXPORT Dart_Handle Dart_BooleanValue(Dart_Handle boolean_obj,
1241 bool* value) { 1281 bool* value) {
1242 DARTSCOPE(Isolate::Current()); 1282 Isolate* isolate = Isolate::Current();
1243 const Bool& obj = Api::UnwrapBoolHandle(boolean_obj); 1283 DARTSCOPE(isolate);
1284 const Bool& obj = Api::UnwrapBoolHandle(isolate, boolean_obj);
1244 if (obj.IsNull()) { 1285 if (obj.IsNull()) {
1245 RETURN_TYPE_ERROR(boolean_obj, Bool); 1286 RETURN_TYPE_ERROR(isolate, boolean_obj, Bool);
1246 } 1287 }
1247 *value = obj.value(); 1288 *value = obj.value();
1248 return Api::Success(); 1289 return Api::Success(isolate);
1249 } 1290 }
1250 1291
1251 1292
1252 // --- Doubles --- 1293 // --- Doubles ---
1253 1294
1254 1295
1255 DART_EXPORT bool Dart_IsDouble(Dart_Handle object) { 1296 DART_EXPORT bool Dart_IsDouble(Dart_Handle object) {
1256 DARTSCOPE(Isolate::Current()); 1297 Isolate* isolate = Isolate::Current();
1257 const Object& obj = Object::Handle(Api::UnwrapHandle(object)); 1298 DARTSCOPE(isolate);
1299 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(object));
1258 return obj.IsDouble(); 1300 return obj.IsDouble();
1259 } 1301 }
1260 1302
1261 1303
1262 DART_EXPORT Dart_Handle Dart_NewDouble(double value) { 1304 DART_EXPORT Dart_Handle Dart_NewDouble(double value) {
1263 DARTSCOPE(Isolate::Current()); 1305 Isolate* isolate = Isolate::Current();
1264 const Double& obj = Double::Handle(Double::New(value)); 1306 DARTSCOPE(isolate);
1265 return Api::NewLocalHandle(obj); 1307 const Double& obj = Double::Handle(isolate, Double::New(value));
1308 return Api::NewLocalHandle(isolate, obj);
1266 } 1309 }
1267 1310
1268 1311
1269 DART_EXPORT Dart_Handle Dart_DoubleValue(Dart_Handle double_obj, 1312 DART_EXPORT Dart_Handle Dart_DoubleValue(Dart_Handle double_obj,
1270 double* value) { 1313 double* value) {
1271 DARTSCOPE(Isolate::Current()); 1314 Isolate* isolate = Isolate::Current();
1272 const Double& obj = Api::UnwrapDoubleHandle(double_obj); 1315 DARTSCOPE(isolate);
1316 const Double& obj = Api::UnwrapDoubleHandle(isolate, double_obj);
1273 if (obj.IsNull()) { 1317 if (obj.IsNull()) {
1274 RETURN_TYPE_ERROR(double_obj, Double); 1318 RETURN_TYPE_ERROR(isolate, double_obj, Double);
1275 } 1319 }
1276 *value = obj.value(); 1320 *value = obj.value();
1277 return Api::Success(); 1321 return Api::Success(isolate);
1278 } 1322 }
1279 1323
1280 1324
1281 // --- Strings --- 1325 // --- Strings ---
1282 1326
1283 1327
1284 DART_EXPORT bool Dart_IsString(Dart_Handle object) { 1328 DART_EXPORT bool Dart_IsString(Dart_Handle object) {
1285 DARTSCOPE(Isolate::Current()); 1329 Isolate* isolate = Isolate::Current();
1286 const Object& obj = Object::Handle(Api::UnwrapHandle(object)); 1330 DARTSCOPE(isolate);
1331 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(object));
1287 return obj.IsString(); 1332 return obj.IsString();
1288 } 1333 }
1289 1334
1290 1335
1291 DART_EXPORT bool Dart_IsString8(Dart_Handle object) { 1336 DART_EXPORT bool Dart_IsString8(Dart_Handle object) {
1292 DARTSCOPE(Isolate::Current()); 1337 Isolate* isolate = Isolate::Current();
1293 const Object& obj = Object::Handle(Api::UnwrapHandle(object)); 1338 DARTSCOPE(isolate);
1339 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(object));
1294 return obj.IsOneByteString() || obj.IsExternalOneByteString(); 1340 return obj.IsOneByteString() || obj.IsExternalOneByteString();
1295 } 1341 }
1296 1342
1297 1343
1298 DART_EXPORT bool Dart_IsString16(Dart_Handle object) { 1344 DART_EXPORT bool Dart_IsString16(Dart_Handle object) {
1299 DARTSCOPE(Isolate::Current()); 1345 Isolate* isolate = Isolate::Current();
1300 const Object& obj = Object::Handle(Api::UnwrapHandle(object)); 1346 DARTSCOPE(isolate);
1347 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(object));
1301 return (obj.IsOneByteString() || obj.IsExternalOneByteString() || 1348 return (obj.IsOneByteString() || obj.IsExternalOneByteString() ||
1302 obj.IsTwoByteString() || obj.IsExternalTwoByteString()); 1349 obj.IsTwoByteString() || obj.IsExternalTwoByteString());
1303 } 1350 }
1304 1351
1305 1352
1306 DART_EXPORT Dart_Handle Dart_StringLength(Dart_Handle str, intptr_t* len) { 1353 DART_EXPORT Dart_Handle Dart_StringLength(Dart_Handle str, intptr_t* len) {
1307 DARTSCOPE(Isolate::Current()); 1354 Isolate* isolate = Isolate::Current();
1308 const Object& obj = Object::Handle(Api::UnwrapHandle(str)); 1355 DARTSCOPE(isolate);
1356 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(str));
1309 if (obj.IsString()) { 1357 if (obj.IsString()) {
1310 String& string_obj = String::Handle(); 1358 String& string_obj = String::Handle(isolate);
1311 string_obj ^= obj.raw(); 1359 string_obj ^= obj.raw();
1312 *len = string_obj.Length(); 1360 *len = string_obj.Length();
1313 return Api::Success(); 1361 return Api::Success(isolate);
1314 } 1362 }
1315 return Api::NewError("Object is not a String"); 1363 return Api::NewError("Object is not a String");
1316 } 1364 }
1317 1365
1318 1366
1319 DART_EXPORT Dart_Handle Dart_NewString(const char* str) { 1367 DART_EXPORT Dart_Handle Dart_NewString(const char* str) {
1320 DARTSCOPE(Isolate::Current()); 1368 Isolate* isolate = Isolate::Current();
1321 const String& obj = String::Handle(String::New(str)); 1369 DARTSCOPE(isolate);
1322 return Api::NewLocalHandle(obj); 1370 const String& obj = String::Handle(isolate, String::New(str));
1371 return Api::NewLocalHandle(isolate, obj);
1323 } 1372 }
1324 1373
1325 1374
1326 DART_EXPORT Dart_Handle Dart_NewString8(const uint8_t* codepoints, 1375 DART_EXPORT Dart_Handle Dart_NewString8(const uint8_t* codepoints,
1327 intptr_t length) { 1376 intptr_t length) {
1328 DARTSCOPE(Isolate::Current()); 1377 Isolate* isolate = Isolate::Current();
1329 const String& obj = String::Handle(String::New(codepoints, length)); 1378 DARTSCOPE(isolate);
1330 return Api::NewLocalHandle(obj); 1379 const String& obj = String::Handle(isolate, String::New(codepoints, length));
1380 return Api::NewLocalHandle(isolate, obj);
1331 } 1381 }
1332 1382
1333 1383
1334 DART_EXPORT Dart_Handle Dart_NewString16(const uint16_t* codepoints, 1384 DART_EXPORT Dart_Handle Dart_NewString16(const uint16_t* codepoints,
1335 intptr_t length) { 1385 intptr_t length) {
1336 DARTSCOPE(Isolate::Current()); 1386 Isolate* isolate = Isolate::Current();
1337 const String& obj = String::Handle(String::New(codepoints, length)); 1387 DARTSCOPE(isolate);
1338 return Api::NewLocalHandle(obj); 1388 const String& obj = String::Handle(isolate, String::New(codepoints, length));
1389 return Api::NewLocalHandle(isolate, obj);
1339 } 1390 }
1340 1391
1341 1392
1342 DART_EXPORT Dart_Handle Dart_NewString32(const uint32_t* codepoints, 1393 DART_EXPORT Dart_Handle Dart_NewString32(const uint32_t* codepoints,
1343 intptr_t length) { 1394 intptr_t length) {
1344 DARTSCOPE(Isolate::Current()); 1395 Isolate* isolate = Isolate::Current();
1345 const String& obj = String::Handle(String::New(codepoints, length)); 1396 DARTSCOPE(isolate);
1346 return Api::NewLocalHandle(obj); 1397 const String& obj = String::Handle(isolate, String::New(codepoints, length));
1398 return Api::NewLocalHandle(isolate, obj);
1347 } 1399 }
1348 1400
1349 1401
1350 DART_EXPORT bool Dart_IsExternalString(Dart_Handle object) { 1402 DART_EXPORT bool Dart_IsExternalString(Dart_Handle object) {
1351 DARTSCOPE(Isolate::Current()); 1403 Isolate* isolate = Isolate::Current();
1352 const String& str = Api::UnwrapStringHandle(object); 1404 DARTSCOPE(isolate);
1405 const String& str = Api::UnwrapStringHandle(isolate, object);
1353 if (str.IsNull()) { 1406 if (str.IsNull()) {
1354 return false; 1407 return false;
1355 } 1408 }
1356 return str.IsExternal(); 1409 return str.IsExternal();
1357 } 1410 }
1358 1411
1359 1412
1360 DART_EXPORT Dart_Handle Dart_ExternalStringGetPeer(Dart_Handle object, 1413 DART_EXPORT Dart_Handle Dart_ExternalStringGetPeer(Dart_Handle object,
1361 void** peer) { 1414 void** peer) {
1362 DARTSCOPE(Isolate::Current()); 1415 Isolate* isolate = Isolate::Current();
1363 const String& str = Api::UnwrapStringHandle(object); 1416 DARTSCOPE(isolate);
1417 const String& str = Api::UnwrapStringHandle(isolate, object);
1364 if (str.IsNull()) { 1418 if (str.IsNull()) {
1365 RETURN_TYPE_ERROR(object, String); 1419 RETURN_TYPE_ERROR(isolate, object, String);
1366 } 1420 }
1367 if (!str.IsExternal()) { 1421 if (!str.IsExternal()) {
1368 return 1422 return
1369 Api::NewError("%s expects argument 'object' to be an external String.", 1423 Api::NewError("%s expects argument 'object' to be an external String.",
1370 CURRENT_FUNC); 1424 CURRENT_FUNC);
1371 } 1425 }
1372 if (peer == NULL) { 1426 if (peer == NULL) {
1373 return Api::NewError("%s expects argument 'peer' to be non-null.", 1427 return Api::NewError("%s expects argument 'peer' to be non-null.",
1374 CURRENT_FUNC); 1428 CURRENT_FUNC);
1375 } 1429 }
1376 *peer = str.GetPeer(); 1430 *peer = str.GetPeer();
1377 return Api::Success(); 1431 return Api::Success(isolate);
1378 } 1432 }
1379 1433
1380 1434
1381 DART_EXPORT Dart_Handle Dart_NewExternalString8(const uint8_t* codepoints, 1435 DART_EXPORT Dart_Handle Dart_NewExternalString8(const uint8_t* codepoints,
1382 intptr_t length, 1436 intptr_t length,
1383 void* peer, 1437 void* peer,
1384 Dart_PeerFinalizer callback) { 1438 Dart_PeerFinalizer callback) {
1385 DARTSCOPE(Isolate::Current()); 1439 Isolate* isolate = Isolate::Current();
1440 DARTSCOPE(isolate);
1386 if (codepoints == NULL && length != 0) { 1441 if (codepoints == NULL && length != 0) {
1387 return Api::NewError("%s expects argument 'codepoints' to be non-null.", 1442 return Api::NewError("%s expects argument 'codepoints' to be non-null.",
1388 CURRENT_FUNC); 1443 CURRENT_FUNC);
1389 } 1444 }
1390 if (length < 0) { 1445 if (length < 0) {
1391 return Api::NewError("%s expects argument 'length' to be greater than 0.", 1446 return Api::NewError("%s expects argument 'length' to be greater than 0.",
1392 CURRENT_FUNC); 1447 CURRENT_FUNC);
1393 } 1448 }
1394 const String& obj = 1449 const String& obj =
1395 String::Handle(String::NewExternal(codepoints, length, peer, callback)); 1450 String::Handle(isolate,
1396 return Api::NewLocalHandle(obj); 1451 String::NewExternal(codepoints, length, peer, callback));
1452 return Api::NewLocalHandle(isolate, obj);
1397 } 1453 }
1398 1454
1399 1455
1400 DART_EXPORT Dart_Handle Dart_NewExternalString16(const uint16_t* codepoints, 1456 DART_EXPORT Dart_Handle Dart_NewExternalString16(const uint16_t* codepoints,
1401 intptr_t length, 1457 intptr_t length,
1402 void* peer, 1458 void* peer,
1403 Dart_PeerFinalizer callback) { 1459 Dart_PeerFinalizer callback) {
1404 DARTSCOPE(Isolate::Current()); 1460 Isolate* isolate = Isolate::Current();
1461 DARTSCOPE(isolate);
1405 if (codepoints == NULL && length != 0) { 1462 if (codepoints == NULL && length != 0) {
1406 return Api::NewError("%s expects argument 'codepoints' to be non-null.", 1463 return Api::NewError("%s expects argument 'codepoints' to be non-null.",
1407 CURRENT_FUNC); 1464 CURRENT_FUNC);
1408 } 1465 }
1409 if (length < 0) { 1466 if (length < 0) {
1410 return Api::NewError("%s expects argument 'length' to be greater than 0.", 1467 return Api::NewError("%s expects argument 'length' to be greater than 0.",
1411 CURRENT_FUNC); 1468 CURRENT_FUNC);
1412 } 1469 }
1413 const String& obj = 1470 const String& obj =
1414 String::Handle(String::NewExternal(codepoints, length, peer, callback)); 1471 String::Handle(isolate,
1415 return Api::NewLocalHandle(obj); 1472 String::NewExternal(codepoints, length, peer, callback));
1473 return Api::NewLocalHandle(isolate, obj);
1416 } 1474 }
1417 1475
1418 1476
1419 DART_EXPORT Dart_Handle Dart_NewExternalString32(const uint32_t* codepoints, 1477 DART_EXPORT Dart_Handle Dart_NewExternalString32(const uint32_t* codepoints,
1420 intptr_t length, 1478 intptr_t length,
1421 void* peer, 1479 void* peer,
1422 Dart_PeerFinalizer callback) { 1480 Dart_PeerFinalizer callback) {
1423 DARTSCOPE(Isolate::Current()); 1481 Isolate* isolate = Isolate::Current();
1482 DARTSCOPE(isolate);
1424 if (codepoints == NULL && length != 0) { 1483 if (codepoints == NULL && length != 0) {
1425 return Api::NewError("%s expects argument 'codepoints' to be non-null.", 1484 return Api::NewError("%s expects argument 'codepoints' to be non-null.",
1426 CURRENT_FUNC); 1485 CURRENT_FUNC);
1427 } 1486 }
1428 if (length < 0) { 1487 if (length < 0) {
1429 return Api::NewError("%s expects argument 'length' to be greater than 0.", 1488 return Api::NewError("%s expects argument 'length' to be greater than 0.",
1430 CURRENT_FUNC); 1489 CURRENT_FUNC);
1431 } 1490 }
1432 const String& obj = 1491 const String& obj =
1433 String::Handle(String::NewExternal(codepoints, length, peer, callback)); 1492 String::Handle(isolate,
1434 return Api::NewLocalHandle(obj); 1493 String::NewExternal(codepoints, length, peer, callback));
1494 return Api::NewLocalHandle(isolate, obj);
1435 } 1495 }
1436 1496
1437 1497
1438 DART_EXPORT Dart_Handle Dart_StringGet8(Dart_Handle str, 1498 DART_EXPORT Dart_Handle Dart_StringGet8(Dart_Handle str,
1439 uint8_t* codepoints, 1499 uint8_t* codepoints,
1440 intptr_t* length) { 1500 intptr_t* length) {
1441 DARTSCOPE(Isolate::Current()); 1501 Isolate* isolate = Isolate::Current();
1442 const Object& obj = Object::Handle(Api::UnwrapHandle(str)); 1502 DARTSCOPE(isolate);
1503 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(str));
1443 if (obj.IsString()) { 1504 if (obj.IsString()) {
1444 String& string_obj = String::Handle(); 1505 String& string_obj = String::Handle(isolate);
1445 string_obj ^= obj.raw(); 1506 string_obj ^= obj.raw();
1446 if (string_obj.CharSize() == String::kOneByteChar) { 1507 if (string_obj.CharSize() == String::kOneByteChar) {
1447 intptr_t str_len = string_obj.Length(); 1508 intptr_t str_len = string_obj.Length();
1448 intptr_t copy_len = (str_len > *length) ? *length : str_len; 1509 intptr_t copy_len = (str_len > *length) ? *length : str_len;
1449 for (intptr_t i = 0; i < copy_len; i++) { 1510 for (intptr_t i = 0; i < copy_len; i++) {
1450 codepoints[i] = static_cast<uint8_t>(string_obj.CharAt(i)); 1511 codepoints[i] = static_cast<uint8_t>(string_obj.CharAt(i));
1451 } 1512 }
1452 *length= copy_len; 1513 *length= copy_len;
1453 return Api::Success(); 1514 return Api::Success(isolate);
1454 } 1515 }
1455 } 1516 }
1456 return Api::NewError(obj.IsString() 1517 return Api::NewError(obj.IsString()
1457 ? "Object is not a String8" 1518 ? "Object is not a String8"
1458 : "Object is not a String"); 1519 : "Object is not a String");
1459 } 1520 }
1460 1521
1461 1522
1462 DART_EXPORT Dart_Handle Dart_StringGet16(Dart_Handle str, 1523 DART_EXPORT Dart_Handle Dart_StringGet16(Dart_Handle str,
1463 uint16_t* codepoints, 1524 uint16_t* codepoints,
1464 intptr_t* length) { 1525 intptr_t* length) {
1465 DARTSCOPE(Isolate::Current()); 1526 Isolate* isolate = Isolate::Current();
1466 const Object& obj = Object::Handle(Api::UnwrapHandle(str)); 1527 DARTSCOPE(isolate);
1528 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(str));
1467 if (obj.IsString()) { 1529 if (obj.IsString()) {
1468 String& string_obj = String::Handle(); 1530 String& string_obj = String::Handle(isolate);
1469 string_obj ^= obj.raw(); 1531 string_obj ^= obj.raw();
1470 if (string_obj.CharSize() <= String::kTwoByteChar) { 1532 if (string_obj.CharSize() <= String::kTwoByteChar) {
1471 intptr_t str_len = string_obj.Length(); 1533 intptr_t str_len = string_obj.Length();
1472 intptr_t copy_len = (str_len > *length) ? *length : str_len; 1534 intptr_t copy_len = (str_len > *length) ? *length : str_len;
1473 for (intptr_t i = 0; i < copy_len; i++) { 1535 for (intptr_t i = 0; i < copy_len; i++) {
1474 codepoints[i] = static_cast<uint16_t>(string_obj.CharAt(i)); 1536 codepoints[i] = static_cast<uint16_t>(string_obj.CharAt(i));
1475 } 1537 }
1476 *length = copy_len; 1538 *length = copy_len;
1477 return Api::Success(); 1539 return Api::Success(isolate);
1478 } 1540 }
1479 } 1541 }
1480 return Api::NewError(obj.IsString() 1542 return Api::NewError(obj.IsString()
1481 ? "Object is not a String16" 1543 ? "Object is not a String16"
1482 : "Object is not a String"); 1544 : "Object is not a String");
1483 } 1545 }
1484 1546
1485 1547
1486 DART_EXPORT Dart_Handle Dart_StringGet32(Dart_Handle str, 1548 DART_EXPORT Dart_Handle Dart_StringGet32(Dart_Handle str,
1487 uint32_t* codepoints, 1549 uint32_t* codepoints,
1488 intptr_t* length) { 1550 intptr_t* length) {
1489 DARTSCOPE(Isolate::Current()); 1551 Isolate* isolate = Isolate::Current();
1490 const Object& obj = Object::Handle(Api::UnwrapHandle(str)); 1552 DARTSCOPE(isolate);
1553 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(str));
1491 if (obj.IsString()) { 1554 if (obj.IsString()) {
1492 String& string_obj = String::Handle(); 1555 String& string_obj = String::Handle(isolate);
1493 string_obj ^= obj.raw(); 1556 string_obj ^= obj.raw();
1494 intptr_t str_len = string_obj.Length(); 1557 intptr_t str_len = string_obj.Length();
1495 intptr_t copy_len = (str_len > *length) ? *length : str_len; 1558 intptr_t copy_len = (str_len > *length) ? *length : str_len;
1496 for (intptr_t i = 0; i < copy_len; i++) { 1559 for (intptr_t i = 0; i < copy_len; i++) {
1497 codepoints[i] = static_cast<uint32_t>(string_obj.CharAt(i)); 1560 codepoints[i] = static_cast<uint32_t>(string_obj.CharAt(i));
1498 } 1561 }
1499 *length = copy_len; 1562 *length = copy_len;
1500 return Api::Success(); 1563 return Api::Success(isolate);
1501 } 1564 }
1502 return Api::NewError("Object is not a String"); 1565 return Api::NewError("Object is not a String");
1503 } 1566 }
1504 1567
1505 1568
1506 DART_EXPORT Dart_Handle Dart_StringToCString(Dart_Handle object, 1569 DART_EXPORT Dart_Handle Dart_StringToCString(Dart_Handle object,
1507 const char** result) { 1570 const char** result) {
1508 DARTSCOPE(Isolate::Current()); 1571 Isolate* isolate = Isolate::Current();
1509 const Object& obj = Object::Handle(Api::UnwrapHandle(object)); 1572 DARTSCOPE(isolate);
1573 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(object));
1510 if (obj.IsString()) { 1574 if (obj.IsString()) {
1511 const char* string_value = obj.ToCString(); 1575 const char* string_value = obj.ToCString();
1512 intptr_t string_length = strlen(string_value); 1576 intptr_t string_length = strlen(string_value);
1513 char* res = reinterpret_cast<char*>(Api::Allocate(string_length + 1)); 1577 char* res =
1578 reinterpret_cast<char*>(Api::Allocate(isolate, string_length + 1));
1514 if (res == NULL) { 1579 if (res == NULL) {
1515 return Api::NewError("Unable to allocate memory"); 1580 return Api::NewError("Unable to allocate memory");
1516 } 1581 }
1517 strncpy(res, string_value, string_length + 1); 1582 strncpy(res, string_value, string_length + 1);
1518 ASSERT(res[string_length] == '\0'); 1583 ASSERT(res[string_length] == '\0');
1519 *result = res; 1584 *result = res;
1520 return Api::Success(); 1585 return Api::Success(isolate);
1521 } 1586 }
1522 return Api::NewError("Object is not a String"); 1587 return Api::NewError("Object is not a String");
1523 } 1588 }
1524 1589
1525 1590
1526 // --- Lists --- 1591 // --- Lists ---
1527 1592
1528 1593
1529 static RawInstance* GetListInstance(Isolate* isolate, const Object& obj) { 1594 static RawInstance* GetListInstance(Isolate* isolate, const Object& obj) {
1530 if (obj.IsInstance()) { 1595 if (obj.IsInstance()) {
1531 Instance& instance = Instance::Handle(); 1596 Instance& instance = Instance::Handle(isolate);
1532 instance ^= obj.raw(); 1597 instance ^= obj.raw();
1533 const Type& type = Type::Handle(isolate->object_store()->list_interface()); 1598 const Type& type =
1534 Error& malformed_type_error = Error::Handle(); 1599 Type::Handle(isolate, isolate->object_store()->list_interface());
1600 Error& malformed_type_error = Error::Handle(isolate);
1535 if (instance.IsInstanceOf(type, 1601 if (instance.IsInstanceOf(type,
1536 TypeArguments::Handle(), 1602 TypeArguments::Handle(isolate),
1537 &malformed_type_error)) { 1603 &malformed_type_error)) {
1538 ASSERT(malformed_type_error.IsNull()); // Type is a raw List. 1604 ASSERT(malformed_type_error.IsNull()); // Type is a raw List.
1539 return instance.raw(); 1605 return instance.raw();
1540 } 1606 }
1541 } 1607 }
1542 return Instance::null(); 1608 return Instance::null();
1543 } 1609 }
1544 1610
1545 1611
1546 DART_EXPORT bool Dart_IsList(Dart_Handle object) { 1612 DART_EXPORT bool Dart_IsList(Dart_Handle object) {
1547 Isolate* isolate = Isolate::Current(); 1613 Isolate* isolate = Isolate::Current();
1548 DARTSCOPE(isolate); 1614 DARTSCOPE(isolate);
1549 const Object& obj = Object::Handle(Api::UnwrapHandle(object)); 1615 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(object));
1550 // TODO(5526318): Make access to GrowableObjectArray more efficient. 1616 // TODO(5526318): Make access to GrowableObjectArray more efficient.
1551 return (obj.IsArray() || 1617 return (obj.IsArray() ||
1552 (GetListInstance(isolate, obj) != Instance::null())); 1618 (GetListInstance(isolate, obj) != Instance::null()));
1553 } 1619 }
1554 1620
1555 1621
1556 DART_EXPORT Dart_Handle Dart_NewList(intptr_t length) { 1622 DART_EXPORT Dart_Handle Dart_NewList(intptr_t length) {
1557 DARTSCOPE(Isolate::Current()); 1623 Isolate* isolate = Isolate::Current();
1558 const Array& obj = Array::Handle(Array::New(length)); 1624 DARTSCOPE(isolate);
1559 return Api::NewLocalHandle(obj); 1625 const Array& obj = Array::Handle(isolate, Array::New(length));
1626 return Api::NewLocalHandle(isolate, obj);
1560 } 1627 }
1561 1628
1562 1629
1563 DART_EXPORT Dart_Handle Dart_ListLength(Dart_Handle list, intptr_t* len) { 1630 DART_EXPORT Dart_Handle Dart_ListLength(Dart_Handle list, intptr_t* len) {
1564 Isolate* isolate = Isolate::Current(); 1631 Isolate* isolate = Isolate::Current();
1565 DARTSCOPE(isolate); 1632 DARTSCOPE(isolate);
1566 const Object& obj = Object::Handle(Api::UnwrapHandle(list)); 1633 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(list));
1567 if (obj.IsByteArray()) { 1634 if (obj.IsByteArray()) {
1568 ByteArray& byte_array = ByteArray::Handle(); 1635 ByteArray& byte_array = ByteArray::Handle(isolate);
1569 byte_array ^= obj.raw(); 1636 byte_array ^= obj.raw();
1570 *len = byte_array.Length(); 1637 *len = byte_array.Length();
1571 return Api::Success(); 1638 return Api::Success(isolate);
1572 } 1639 }
1573 if (obj.IsArray()) { 1640 if (obj.IsArray()) {
1574 Array& array_obj = Array::Handle(); 1641 Array& array_obj = Array::Handle(isolate);
1575 array_obj ^= obj.raw(); 1642 array_obj ^= obj.raw();
1576 *len = array_obj.Length(); 1643 *len = array_obj.Length();
1577 return Api::Success(); 1644 return Api::Success(isolate);
1578 } 1645 }
1579 // TODO(5526318): Make access to GrowableObjectArray more efficient. 1646 // TODO(5526318): Make access to GrowableObjectArray more efficient.
1580 // Now check and handle a dart object that implements the List interface. 1647 // Now check and handle a dart object that implements the List interface.
1581 const Instance& instance = Instance::Handle(GetListInstance(isolate, obj)); 1648 const Instance& instance =
1649 Instance::Handle(isolate, GetListInstance(isolate, obj));
1582 if (instance.IsNull()) { 1650 if (instance.IsNull()) {
1583 return Api::NewError("Object does not implement the List inteface"); 1651 return Api::NewError("Object does not implement the List inteface");
1584 } 1652 }
1585 String& name = String::Handle(String::New("length")); 1653 String& name = String::Handle(isolate, String::New("length"));
1586 name = Field::GetterName(name); 1654 name = Field::GetterName(name);
1587 const Function& function = Function::Handle( 1655 const Function& function =
1588 Resolver::ResolveDynamic(instance, name, 1, 0)); 1656 Function::Handle(isolate, Resolver::ResolveDynamic(instance, name, 1, 0));
1589 if (function.IsNull()) { 1657 if (function.IsNull()) {
1590 return Api::NewError("List object does not have a 'length' field."); 1658 return Api::NewError("List object does not have a 'length' field.");
1591 } 1659 }
1592 1660
1593 GrowableArray<const Object*> args(0); 1661 GrowableArray<const Object*> args(0);
1594 const Array& kNoArgumentNames = Array::Handle(); 1662 const Array& kNoArgumentNames = Array::Handle(isolate);
1595 const Object& retval = Object::Handle( 1663 const Object& retval = Object::Handle(
1664 isolate,
1596 DartEntry::InvokeDynamic(instance, function, args, kNoArgumentNames)); 1665 DartEntry::InvokeDynamic(instance, function, args, kNoArgumentNames));
1597 if (retval.IsSmi() || retval.IsMint()) { 1666 if (retval.IsSmi() || retval.IsMint()) {
1598 Integer& integer = Integer::Handle(); 1667 Integer& integer = Integer::Handle(isolate);
1599 integer ^= retval.raw(); 1668 integer ^= retval.raw();
1600 *len = integer.AsInt64Value(); 1669 *len = integer.AsInt64Value();
1601 return Api::Success(); 1670 return Api::Success(isolate);
1602 } else if (retval.IsBigint()) { 1671 } else if (retval.IsBigint()) {
1603 Bigint& bigint = Bigint::Handle(); 1672 Bigint& bigint = Bigint::Handle(isolate);
1604 bigint ^= retval.raw(); 1673 bigint ^= retval.raw();
1605 if (BigintOperations::FitsIntoMint(bigint)) { 1674 if (BigintOperations::FitsIntoMint(bigint)) {
1606 *len = BigintOperations::ToMint(bigint); 1675 *len = BigintOperations::ToMint(bigint);
1607 return Api::Success(); 1676 return Api::Success(isolate);
1608 } else { 1677 } else {
1609 return Api::NewError("Length of List object is greater than the " 1678 return Api::NewError("Length of List object is greater than the "
1610 "maximum value that 'len' parameter can hold"); 1679 "maximum value that 'len' parameter can hold");
1611 } 1680 }
1612 } else if (retval.IsError()) { 1681 } else if (retval.IsError()) {
1613 return Api::NewLocalHandle(retval); 1682 return Api::NewLocalHandle(isolate, retval);
1614 } else { 1683 } else {
1615 return Api::NewError("Length of List object is not an integer"); 1684 return Api::NewError("Length of List object is not an integer");
1616 } 1685 }
1617 } 1686 }
1618 1687
1619 1688
1620 DART_EXPORT Dart_Handle Dart_ListGetAt(Dart_Handle list, intptr_t index) { 1689 DART_EXPORT Dart_Handle Dart_ListGetAt(Dart_Handle list, intptr_t index) {
1621 Isolate* isolate = Isolate::Current(); 1690 Isolate* isolate = Isolate::Current();
1622 DARTSCOPE(isolate); 1691 DARTSCOPE(isolate);
1623 const Object& obj = Object::Handle(Api::UnwrapHandle(list)); 1692 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(list));
1624 if (obj.IsArray()) { 1693 if (obj.IsArray()) {
1625 Array& array_obj = Array::Handle(); 1694 Array& array_obj = Array::Handle(isolate);
1626 array_obj ^= obj.raw(); 1695 array_obj ^= obj.raw();
1627 if ((index >= 0) && (index < array_obj.Length())) { 1696 if ((index >= 0) && (index < array_obj.Length())) {
1628 const Object& element = Object::Handle(array_obj.At(index)); 1697 const Object& element = Object::Handle(isolate, array_obj.At(index));
1629 return Api::NewLocalHandle(element); 1698 return Api::NewLocalHandle(isolate, element);
1630 } 1699 }
1631 return Api::NewError("Invalid index passed in to access array element"); 1700 return Api::NewError("Invalid index passed in to access array element");
1632 } 1701 }
1633 // TODO(5526318): Make access to GrowableObjectArray more efficient. 1702 // TODO(5526318): Make access to GrowableObjectArray more efficient.
1634 // Now check and handle a dart object that implements the List interface. 1703 // Now check and handle a dart object that implements the List interface.
1635 const Instance& instance = Instance::Handle(GetListInstance(isolate, obj)); 1704 const Instance& instance =
1705 Instance::Handle(isolate, GetListInstance(isolate, obj));
1636 if (!instance.IsNull()) { 1706 if (!instance.IsNull()) {
1637 String& name = String::Handle(String::New("[]")); 1707 String& name = String::Handle(isolate, String::New("[]"));
1638 const Function& function = Function::Handle( 1708 const Function& function =
1639 Resolver::ResolveDynamic(instance, name, 2, 0)); 1709 Function::Handle(isolate,
1710 Resolver::ResolveDynamic(instance, name, 2, 0));
1640 if (!function.IsNull()) { 1711 if (!function.IsNull()) {
1641 GrowableArray<const Object*> args(1); 1712 GrowableArray<const Object*> args(1);
1642 Integer& indexobj = Integer::Handle(); 1713 Integer& indexobj = Integer::Handle(isolate);
1643 indexobj = Integer::New(index); 1714 indexobj = Integer::New(index);
1644 args.Add(&indexobj); 1715 args.Add(&indexobj);
1645 const Array& kNoArgumentNames = Array::Handle(); 1716 const Array& kNoArgumentNames = Array::Handle(isolate);
1646 const Object& result = Object::Handle( 1717 const Object& result = Object::Handle(
1718 isolate,
1647 DartEntry::InvokeDynamic(instance, function, args, kNoArgumentNames)); 1719 DartEntry::InvokeDynamic(instance, function, args, kNoArgumentNames));
1648 return Api::NewLocalHandle(result); 1720 return Api::NewLocalHandle(isolate, result);
1649 } 1721 }
1650 } 1722 }
1651 return Api::NewError("Object does not implement the 'List' interface"); 1723 return Api::NewError("Object does not implement the 'List' interface");
1652 } 1724 }
1653 1725
1654 1726
1655 DART_EXPORT Dart_Handle Dart_ListSetAt(Dart_Handle list, 1727 DART_EXPORT Dart_Handle Dart_ListSetAt(Dart_Handle list,
1656 intptr_t index, 1728 intptr_t index,
1657 Dart_Handle value) { 1729 Dart_Handle value) {
1658 Isolate* isolate = Isolate::Current(); 1730 Isolate* isolate = Isolate::Current();
1659 DARTSCOPE(isolate); 1731 DARTSCOPE(isolate);
1660 const Object& obj = Object::Handle(Api::UnwrapHandle(list)); 1732 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(list));
1661 if (obj.IsArray()) { 1733 if (obj.IsArray()) {
1662 if (obj.IsImmutableArray()) { 1734 if (obj.IsImmutableArray()) {
1663 return Api::NewError("Cannot modify immutable array"); 1735 return Api::NewError("Cannot modify immutable array");
1664 } 1736 }
1665 Array& array_obj = Array::Handle(); 1737 Array& array_obj = Array::Handle(isolate);
1666 array_obj ^= obj.raw(); 1738 array_obj ^= obj.raw();
1667 const Object& value_obj = Object::Handle(Api::UnwrapHandle(value)); 1739 const Object& value_obj = Object::Handle(isolate, Api::UnwrapHandle(value));
1668 if ((index >= 0) && (index < array_obj.Length())) { 1740 if ((index >= 0) && (index < array_obj.Length())) {
1669 array_obj.SetAt(index, value_obj); 1741 array_obj.SetAt(index, value_obj);
1670 return Api::Success(); 1742 return Api::Success(isolate);
1671 } 1743 }
1672 return Api::NewError("Invalid index passed in to set array element"); 1744 return Api::NewError("Invalid index passed in to set array element");
1673 } 1745 }
1674 // TODO(5526318): Make access to GrowableObjectArray more efficient. 1746 // TODO(5526318): Make access to GrowableObjectArray more efficient.
1675 // Now check and handle a dart object that implements the List interface. 1747 // Now check and handle a dart object that implements the List interface.
1676 const Instance& instance = Instance::Handle(GetListInstance(isolate, obj)); 1748 const Instance& instance =
1749 Instance::Handle(isolate, GetListInstance(isolate, obj));
1677 if (!instance.IsNull()) { 1750 if (!instance.IsNull()) {
1678 String& name = String::Handle(String::New("[]=")); 1751 String& name = String::Handle(isolate, String::New("[]="));
1679 const Function& function = Function::Handle( 1752 const Function& function =
1680 Resolver::ResolveDynamic(instance, name, 3, 0)); 1753 Function::Handle(isolate,
1754 Resolver::ResolveDynamic(instance, name, 3, 0));
1681 if (!function.IsNull()) { 1755 if (!function.IsNull()) {
1682 const Integer& index_obj = Integer::Handle(Integer::New(index)); 1756 const Integer& index_obj = Integer::Handle(isolate, Integer::New(index));
1683 const Object& value_obj = Object::Handle(Api::UnwrapHandle(value)); 1757 const Object& value_obj =
1758 Object::Handle(isolate, Api::UnwrapHandle(value));
1684 GrowableArray<const Object*> args(2); 1759 GrowableArray<const Object*> args(2);
1685 args.Add(&index_obj); 1760 args.Add(&index_obj);
1686 args.Add(&value_obj); 1761 args.Add(&value_obj);
1687 const Array& kNoArgumentNames = Array::Handle(); 1762 const Array& kNoArgumentNames = Array::Handle(isolate);
1688 const Object& result = Object::Handle( 1763 const Object& result = Object::Handle(
1764 isolate,
1689 DartEntry::InvokeDynamic(instance, function, args, kNoArgumentNames)); 1765 DartEntry::InvokeDynamic(instance, function, args, kNoArgumentNames));
1690 return Api::NewLocalHandle(result); 1766 return Api::NewLocalHandle(isolate, result);
1691 } 1767 }
1692 } 1768 }
1693 return Api::NewError("Object does not implement the 'List' interface"); 1769 return Api::NewError("Object does not implement the 'List' interface");
1694 } 1770 }
1695 1771
1696 1772
1697 DART_EXPORT Dart_Handle Dart_ListGetAsBytes(Dart_Handle list, 1773 DART_EXPORT Dart_Handle Dart_ListGetAsBytes(Dart_Handle list,
1698 intptr_t offset, 1774 intptr_t offset,
1699 uint8_t* native_array, 1775 uint8_t* native_array,
1700 intptr_t length) { 1776 intptr_t length) {
1701 Isolate* isolate = Isolate::Current(); 1777 Isolate* isolate = Isolate::Current();
1702 DARTSCOPE(isolate); 1778 DARTSCOPE(isolate);
1703 const Object& obj = Object::Handle(Api::UnwrapHandle(list)); 1779 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(list));
1704 if (obj.IsByteArray()) { 1780 if (obj.IsByteArray()) {
1705 ByteArray& byte_array = ByteArray::Handle(); 1781 ByteArray& byte_array = ByteArray::Handle(isolate);
1706 byte_array ^= obj.raw(); 1782 byte_array ^= obj.raw();
1707 if (Utils::RangeCheck(offset, length, byte_array.Length())) { 1783 if (Utils::RangeCheck(offset, length, byte_array.Length())) {
1708 ByteArray::Copy(native_array, byte_array, offset, length); 1784 ByteArray::Copy(native_array, byte_array, offset, length);
1709 return Api::Success(); 1785 return Api::Success(isolate);
1710 } 1786 }
1711 return Api::NewError("Invalid length passed in to access list elements"); 1787 return Api::NewError("Invalid length passed in to access list elements");
1712 } 1788 }
1713 if (obj.IsArray()) { 1789 if (obj.IsArray()) {
1714 Array& array_obj = Array::Handle(); 1790 Array& array_obj = Array::Handle(isolate);
1715 array_obj ^= obj.raw(); 1791 array_obj ^= obj.raw();
1716 if (Utils::RangeCheck(offset, length, array_obj.Length())) { 1792 if (Utils::RangeCheck(offset, length, array_obj.Length())) {
1717 Object& element = Object::Handle(); 1793 Object& element = Object::Handle(isolate);
1718 Integer& integer = Integer::Handle(); 1794 Integer& integer = Integer::Handle(isolate);
1719 for (int i = 0; i < length; i++) { 1795 for (int i = 0; i < length; i++) {
1720 element = array_obj.At(offset + i); 1796 element = array_obj.At(offset + i);
1721 if (!element.IsInteger()) { 1797 if (!element.IsInteger()) {
1722 return Api::NewError("%s expects the argument 'list' to be " 1798 return Api::NewError("%s expects the argument 'list' to be "
1723 "a List of int", CURRENT_FUNC); 1799 "a List of int", CURRENT_FUNC);
1724 } 1800 }
1725 integer ^= element.raw(); 1801 integer ^= element.raw();
1726 native_array[i] = static_cast<uint8_t>(integer.AsInt64Value() & 0xff); 1802 native_array[i] = static_cast<uint8_t>(integer.AsInt64Value() & 0xff);
1727 ASSERT(integer.AsInt64Value() <= 0xff); 1803 ASSERT(integer.AsInt64Value() <= 0xff);
1728 // TODO(hpayer): value should always be smaller then 0xff. Add error 1804 // TODO(hpayer): value should always be smaller then 0xff. Add error
1729 // handling. 1805 // handling.
1730 } 1806 }
1731 return Api::Success(); 1807 return Api::Success(isolate);
1732 } 1808 }
1733 return Api::NewError("Invalid length passed in to access array elements"); 1809 return Api::NewError("Invalid length passed in to access array elements");
1734 } 1810 }
1735 // TODO(5526318): Make access to GrowableObjectArray more efficient. 1811 // TODO(5526318): Make access to GrowableObjectArray more efficient.
1736 // Now check and handle a dart object that implements the List interface. 1812 // Now check and handle a dart object that implements the List interface.
1737 const Instance& instance = Instance::Handle(GetListInstance(isolate, obj)); 1813 const Instance& instance =
1814 Instance::Handle(isolate, GetListInstance(isolate, obj));
1738 if (!instance.IsNull()) { 1815 if (!instance.IsNull()) {
1739 String& name = String::Handle(String::New("[]")); 1816 String& name = String::Handle(isolate, String::New("[]"));
1740 const Function& function = Function::Handle( 1817 const Function& function =
1741 Resolver::ResolveDynamic(instance, name, 2, 0)); 1818 Function::Handle(isolate,
1819 Resolver::ResolveDynamic(instance, name, 2, 0));
1742 if (!function.IsNull()) { 1820 if (!function.IsNull()) {
1743 Object& result = Object::Handle(); 1821 Object& result = Object::Handle(isolate);
1744 Integer& intobj = Integer::Handle(); 1822 Integer& intobj = Integer::Handle(isolate);
1745 for (int i = 0; i < length; i++) { 1823 for (int i = 0; i < length; i++) {
1746 intobj = Integer::New(offset + i); 1824 intobj = Integer::New(offset + i);
1747 GrowableArray<const Object*> args(1); 1825 GrowableArray<const Object*> args(1);
1748 args.Add(&intobj); 1826 args.Add(&intobj);
1749 const Array& kNoArgumentNames = Array::Handle(); 1827 const Array& kNoArgumentNames = Array::Handle(isolate);
1750 result = DartEntry::InvokeDynamic( 1828 result = DartEntry::InvokeDynamic(
1751 instance, function, args, kNoArgumentNames); 1829 instance, function, args, kNoArgumentNames);
1752 if (result.IsError()) { 1830 if (result.IsError()) {
1753 return Api::NewLocalHandle(result); 1831 return Api::NewLocalHandle(isolate, result);
1754 } 1832 }
1755 if (!result.IsInteger()) { 1833 if (!result.IsInteger()) {
1756 return Api::NewError("%s expects the argument 'list' to be " 1834 return Api::NewError("%s expects the argument 'list' to be "
1757 "a List of int", CURRENT_FUNC); 1835 "a List of int", CURRENT_FUNC);
1758 } 1836 }
1759 intobj ^= result.raw(); 1837 intobj ^= result.raw();
1760 ASSERT(intobj.AsInt64Value() <= 0xff); 1838 ASSERT(intobj.AsInt64Value() <= 0xff);
1761 // TODO(hpayer): value should always be smaller then 0xff. Add error 1839 // TODO(hpayer): value should always be smaller then 0xff. Add error
1762 // handling. 1840 // handling.
1763 native_array[i] = static_cast<uint8_t>(intobj.AsInt64Value() & 0xff); 1841 native_array[i] = static_cast<uint8_t>(intobj.AsInt64Value() & 0xff);
1764 } 1842 }
1765 return Api::Success(); 1843 return Api::Success(isolate);
1766 } 1844 }
1767 } 1845 }
1768 return Api::NewError("Object does not implement the 'List' interface"); 1846 return Api::NewError("Object does not implement the 'List' interface");
1769 } 1847 }
1770 1848
1771 1849
1772 DART_EXPORT Dart_Handle Dart_ListSetAsBytes(Dart_Handle list, 1850 DART_EXPORT Dart_Handle Dart_ListSetAsBytes(Dart_Handle list,
1773 intptr_t offset, 1851 intptr_t offset,
1774 uint8_t* native_array, 1852 uint8_t* native_array,
1775 intptr_t length) { 1853 intptr_t length) {
1776 Isolate* isolate = Isolate::Current(); 1854 Isolate* isolate = Isolate::Current();
1777 DARTSCOPE(isolate); 1855 DARTSCOPE(isolate);
1778 const Object& obj = Object::Handle(Api::UnwrapHandle(list)); 1856 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(list));
1779 if (obj.IsByteArray()) { 1857 if (obj.IsByteArray()) {
1780 ByteArray& byte_array = ByteArray::Handle(); 1858 ByteArray& byte_array = ByteArray::Handle(isolate);
1781 byte_array ^= obj.raw(); 1859 byte_array ^= obj.raw();
1782 if (Utils::RangeCheck(offset, length, byte_array.Length())) { 1860 if (Utils::RangeCheck(offset, length, byte_array.Length())) {
1783 ByteArray::Copy(byte_array, offset, native_array, length); 1861 ByteArray::Copy(byte_array, offset, native_array, length);
1784 return Api::Success(); 1862 return Api::Success(isolate);
1785 } 1863 }
1786 return Api::NewError("Invalid length passed in to set list elements"); 1864 return Api::NewError("Invalid length passed in to set list elements");
1787 } 1865 }
1788 if (obj.IsArray()) { 1866 if (obj.IsArray()) {
1789 if (obj.IsImmutableArray()) { 1867 if (obj.IsImmutableArray()) {
1790 return Api::NewError("Cannot modify immutable array"); 1868 return Api::NewError("Cannot modify immutable array");
1791 } 1869 }
1792 Array& array_obj = Array::Handle(); 1870 Array& array_obj = Array::Handle(isolate);
1793 array_obj ^= obj.raw(); 1871 array_obj ^= obj.raw();
1794 Integer& integer = Integer::Handle(); 1872 Integer& integer = Integer::Handle(isolate);
1795 if (Utils::RangeCheck(offset, length, array_obj.Length())) { 1873 if (Utils::RangeCheck(offset, length, array_obj.Length())) {
1796 for (int i = 0; i < length; i++) { 1874 for (int i = 0; i < length; i++) {
1797 integer = Integer::New(native_array[i]); 1875 integer = Integer::New(native_array[i]);
1798 array_obj.SetAt(offset + i, integer); 1876 array_obj.SetAt(offset + i, integer);
1799 } 1877 }
1800 return Api::Success(); 1878 return Api::Success(isolate);
1801 } 1879 }
1802 return Api::NewError("Invalid length passed in to set array elements"); 1880 return Api::NewError("Invalid length passed in to set array elements");
1803 } 1881 }
1804 // TODO(5526318): Make access to GrowableObjectArray more efficient. 1882 // TODO(5526318): Make access to GrowableObjectArray more efficient.
1805 // Now check and handle a dart object that implements the List interface. 1883 // Now check and handle a dart object that implements the List interface.
1806 const Instance& instance = Instance::Handle(GetListInstance(isolate, obj)); 1884 const Instance& instance =
1885 Instance::Handle(isolate, GetListInstance(isolate, obj));
1807 if (!instance.IsNull()) { 1886 if (!instance.IsNull()) {
1808 String& name = String::Handle(String::New("[]=")); 1887 String& name = String::Handle(isolate, String::New("[]="));
1809 const Function& function = Function::Handle( 1888 const Function& function =
1810 Resolver::ResolveDynamic(instance, name, 3, 0)); 1889 Function::Handle(isolate,
1890 Resolver::ResolveDynamic(instance, name, 3, 0));
1811 if (!function.IsNull()) { 1891 if (!function.IsNull()) {
1812 Integer& indexobj = Integer::Handle(); 1892 Integer& indexobj = Integer::Handle(isolate);
1813 Integer& valueobj = Integer::Handle(); 1893 Integer& valueobj = Integer::Handle(isolate);
1814 for (int i = 0; i < length; i++) { 1894 for (int i = 0; i < length; i++) {
1815 indexobj = Integer::New(offset + i); 1895 indexobj = Integer::New(offset + i);
1816 valueobj = Integer::New(native_array[i]); 1896 valueobj = Integer::New(native_array[i]);
1817 GrowableArray<const Object*> args(2); 1897 GrowableArray<const Object*> args(2);
1818 args.Add(&indexobj); 1898 args.Add(&indexobj);
1819 args.Add(&valueobj); 1899 args.Add(&valueobj);
1820 const Array& kNoArgumentNames = Array::Handle(); 1900 const Array& kNoArgumentNames = Array::Handle(isolate);
1821 const Object& result = Object::Handle( 1901 const Object& result = Object::Handle(
1902 isolate,
1822 DartEntry::InvokeDynamic( 1903 DartEntry::InvokeDynamic(
1823 instance, function, args, kNoArgumentNames)); 1904 instance, function, args, kNoArgumentNames));
1824 if (result.IsError()) { 1905 if (result.IsError()) {
1825 return Api::NewLocalHandle(result); 1906 return Api::NewLocalHandle(isolate, result);
1826 } 1907 }
1827 } 1908 }
1828 return Api::Success(); 1909 return Api::Success(isolate);
1829 } 1910 }
1830 } 1911 }
1831 return Api::NewError("Object does not implement the 'List' interface"); 1912 return Api::NewError("Object does not implement the 'List' interface");
1832 } 1913 }
1833 1914
1834 1915
1835 // --- Byte Arrays --- 1916 // --- Byte Arrays ---
1836 1917
1837 1918
1838 DART_EXPORT bool Dart_IsByteArray(Dart_Handle object) { 1919 DART_EXPORT bool Dart_IsByteArray(Dart_Handle object) {
1839 DARTSCOPE(Isolate::Current()); 1920 Isolate* isolate = Isolate::Current();
1840 const Object& obj = Object::Handle(Api::UnwrapHandle(object)); 1921 DARTSCOPE(isolate);
1922 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(object));
1841 return obj.IsByteArray(); 1923 return obj.IsByteArray();
1842 } 1924 }
1843 1925
1844 1926
1845 DART_EXPORT Dart_Handle Dart_NewByteArray(intptr_t length) { 1927 DART_EXPORT Dart_Handle Dart_NewByteArray(intptr_t length) {
1846 DARTSCOPE(Isolate::Current()); 1928 Isolate* isolate = Isolate::Current();
1929 DARTSCOPE(isolate);
1847 const InternalByteArray& obj = 1930 const InternalByteArray& obj =
1848 InternalByteArray::Handle(InternalByteArray::New(length)); 1931 InternalByteArray::Handle(isolate, InternalByteArray::New(length));
1849 return Api::NewLocalHandle(obj); 1932 return Api::NewLocalHandle(isolate, obj);
1850 } 1933 }
1851 1934
1852 1935
1853 DART_EXPORT Dart_Handle Dart_NewExternalByteArray(uint8_t* data, 1936 DART_EXPORT Dart_Handle Dart_NewExternalByteArray(uint8_t* data,
1854 intptr_t length, 1937 intptr_t length,
1855 void* peer, 1938 void* peer,
1856 Dart_PeerFinalizer callback) { 1939 Dart_PeerFinalizer callback) {
1857 DARTSCOPE(Isolate::Current()); 1940 Isolate* isolate = Isolate::Current();
1941 DARTSCOPE(isolate);
1858 if (data == NULL && length != 0) { 1942 if (data == NULL && length != 0) {
1859 return Api::NewError("%s expects argument 'data' to be non-null.", 1943 return Api::NewError("%s expects argument 'data' to be non-null.",
1860 CURRENT_FUNC); 1944 CURRENT_FUNC);
1861 } 1945 }
1862 if (length < 0) { 1946 if (length < 0) {
1863 return Api::NewError("%s expects argument 'length' to be greater than 0.", 1947 return Api::NewError("%s expects argument 'length' to be greater than 0.",
1864 CURRENT_FUNC); 1948 CURRENT_FUNC);
1865 } 1949 }
1866 const ExternalByteArray& obj = 1950 const ExternalByteArray& obj =
1867 ExternalByteArray::Handle(ExternalByteArray::New(data, 1951 ExternalByteArray::Handle(isolate,
1952 ExternalByteArray::New(data,
1868 length, 1953 length,
1869 peer, 1954 peer,
1870 callback)); 1955 callback));
1871 return Api::NewLocalHandle(obj); 1956 return Api::NewLocalHandle(isolate, obj);
1872 } 1957 }
1873 1958
1874 1959
1875 DART_EXPORT Dart_Handle Dart_ExternalByteArrayGetPeer(Dart_Handle object, 1960 DART_EXPORT Dart_Handle Dart_ExternalByteArrayGetPeer(Dart_Handle object,
1876 void** peer) { 1961 void** peer) {
1877 DARTSCOPE(Isolate::Current()); 1962 Isolate* isolate = Isolate::Current();
1963 DARTSCOPE(isolate);
1878 const ExternalByteArray& array = 1964 const ExternalByteArray& array =
1879 Api::UnwrapExternalByteArrayHandle(object); 1965 Api::UnwrapExternalByteArrayHandle(isolate, object);
1880 if (array.IsNull()) { 1966 if (array.IsNull()) {
1881 RETURN_TYPE_ERROR(object, ExternalByteArray); 1967 RETURN_TYPE_ERROR(isolate, object, ExternalByteArray);
1882 } 1968 }
1883 if (peer == NULL) { 1969 if (peer == NULL) {
1884 return Api::NewError("%s expects argument 'peer' to be non-null.", 1970 return Api::NewError("%s expects argument 'peer' to be non-null.",
1885 CURRENT_FUNC); 1971 CURRENT_FUNC);
1886 } 1972 }
1887 *peer = array.GetPeer(); 1973 *peer = array.GetPeer();
1888 return Api::Success(); 1974 return Api::Success(isolate);
1889 } 1975 }
1890 1976
1891 1977
1892 template<typename T> 1978 template<typename T>
1893 Dart_Handle ByteArrayGetAt(T* value, Dart_Handle array, intptr_t offset) { 1979 Dart_Handle ByteArrayGetAt(T* value, Dart_Handle array, intptr_t offset) {
1894 const ByteArray& array_obj = Api::UnwrapByteArrayHandle(array); 1980 Isolate* isolate = Isolate::Current();
1981 CHECK_ISOLATE(isolate);
1982 const ByteArray& array_obj = Api::UnwrapByteArrayHandle(isolate, array);
1895 if (array_obj.IsNull()) { 1983 if (array_obj.IsNull()) {
1896 RETURN_TYPE_ERROR(array, ByteArray); 1984 RETURN_TYPE_ERROR(isolate, array, ByteArray);
1897 } 1985 }
1898 intptr_t length = sizeof(T); 1986 intptr_t length = sizeof(T);
1899 if (!Utils::RangeCheck(offset, length, array_obj.Length())) { 1987 if (!Utils::RangeCheck(offset, length, array_obj.Length())) {
1900 return Api::NewError("Invalid index passed in to get byte array element"); 1988 return Api::NewError("Invalid index passed in to get byte array element");
1901 } 1989 }
1902 uint8_t* dst = reinterpret_cast<uint8_t*>(value); 1990 uint8_t* dst = reinterpret_cast<uint8_t*>(value);
1903 ByteArray::Copy(dst, array_obj, offset, length); 1991 ByteArray::Copy(dst, array_obj, offset, length);
1904 return Api::Success(); 1992 return Api::Success(isolate);
1905 } 1993 }
1906 1994
1907 1995
1908 template<typename T> 1996 template<typename T>
1909 Dart_Handle ByteArraySetAt(Dart_Handle array, intptr_t offset, T value) { 1997 Dart_Handle ByteArraySetAt(Dart_Handle array, intptr_t offset, T value) {
1910 const ByteArray& array_obj = Api::UnwrapByteArrayHandle(array); 1998 Isolate* isolate = Isolate::Current();
1999 CHECK_ISOLATE(isolate);
2000 const ByteArray& array_obj = Api::UnwrapByteArrayHandle(isolate, array);
1911 if (array_obj.IsNull()) { 2001 if (array_obj.IsNull()) {
1912 RETURN_TYPE_ERROR(array, ByteArray); 2002 RETURN_TYPE_ERROR(isolate, array, ByteArray);
1913 } 2003 }
1914 intptr_t length = sizeof(T); 2004 intptr_t length = sizeof(T);
1915 if (!Utils::RangeCheck(offset, length, array_obj.Length())) { 2005 if (!Utils::RangeCheck(offset, length, array_obj.Length())) {
1916 return Api::NewError("Invalid index passed in to get byte array element"); 2006 return Api::NewError("Invalid index passed in to get byte array element");
1917 } 2007 }
1918 const uint8_t* src = reinterpret_cast<uint8_t*>(&value); 2008 const uint8_t* src = reinterpret_cast<uint8_t*>(&value);
1919 ByteArray::Copy(array_obj, offset, src, length); 2009 ByteArray::Copy(array_obj, offset, src, length);
1920 return Api::Success(); 2010 return Api::Success(isolate);
1921 } 2011 }
1922 2012
1923 2013
1924 DART_EXPORT Dart_Handle Dart_ByteArrayGetInt8At(Dart_Handle array, 2014 DART_EXPORT Dart_Handle Dart_ByteArrayGetInt8At(Dart_Handle array,
1925 intptr_t offset, 2015 intptr_t offset,
1926 int8_t* value) { 2016 int8_t* value) {
1927 return ByteArrayGetAt(value, array, offset); 2017 return ByteArrayGetAt(value, array, offset);
1928 } 2018 }
1929 2019
1930 2020
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
2058 intptr_t offset, 2148 intptr_t offset,
2059 double value) { 2149 double value) {
2060 return ByteArraySetAt(array, offset, value); 2150 return ByteArraySetAt(array, offset, value);
2061 } 2151 }
2062 2152
2063 2153
2064 // --- Closures --- 2154 // --- Closures ---
2065 2155
2066 2156
2067 DART_EXPORT bool Dart_IsClosure(Dart_Handle object) { 2157 DART_EXPORT bool Dart_IsClosure(Dart_Handle object) {
2068 DARTSCOPE(Isolate::Current()); 2158 Isolate* isolate = Isolate::Current();
2069 const Object& obj = Object::Handle(Api::UnwrapHandle(object)); 2159 DARTSCOPE(isolate);
2160 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(object));
2070 return obj.IsClosure(); 2161 return obj.IsClosure();
2071 } 2162 }
2072 2163
2073 2164
2074 DART_EXPORT Dart_Handle Dart_InvokeClosure(Dart_Handle closure, 2165 DART_EXPORT Dart_Handle Dart_InvokeClosure(Dart_Handle closure,
2075 int number_of_arguments, 2166 int number_of_arguments,
2076 Dart_Handle* arguments) { 2167 Dart_Handle* arguments) {
2077 Isolate* isolate = Isolate::Current(); 2168 Isolate* isolate = Isolate::Current();
2078 DARTSCOPE(isolate); 2169 DARTSCOPE(isolate);
2079 const Object& obj = Object::Handle(Api::UnwrapHandle(closure)); 2170 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(closure));
2080 if (obj.IsNull()) { 2171 if (obj.IsNull()) {
2081 return Api::NewError("Null object passed in to invoke closure"); 2172 return Api::NewError("Null object passed in to invoke closure");
2082 } 2173 }
2083 if (!obj.IsClosure()) { 2174 if (!obj.IsClosure()) {
2084 return Api::NewError("Invalid closure passed to invoke closure"); 2175 return Api::NewError("Invalid closure passed to invoke closure");
2085 } 2176 }
2086 ASSERT(ClassFinalizer::AllClassesFinalized()); 2177 ASSERT(ClassFinalizer::AllClassesFinalized());
2087 2178
2088 // Now try to invoke the closure. 2179 // Now try to invoke the closure.
2089 Closure& closure_obj = Closure::Handle(); 2180 Closure& closure_obj = Closure::Handle(isolate);
2090 closure_obj ^= obj.raw(); 2181 closure_obj ^= obj.raw();
2091 GrowableArray<const Object*> dart_arguments(number_of_arguments); 2182 GrowableArray<const Object*> dart_arguments(number_of_arguments);
2092 for (int i = 0; i < number_of_arguments; i++) { 2183 for (int i = 0; i < number_of_arguments; i++) {
2093 const Object& arg = Object::Handle(Api::UnwrapHandle(arguments[i])); 2184 const Object& arg =
2185 Object::Handle(isolate, Api::UnwrapHandle(arguments[i]));
2094 dart_arguments.Add(&arg); 2186 dart_arguments.Add(&arg);
2095 } 2187 }
2096 const Array& kNoArgumentNames = Array::Handle(); 2188 const Array& kNoArgumentNames = Array::Handle(isolate);
2097 const Object& result = Object::Handle( 2189 const Object& result = Object::Handle(
2190 isolate,
2098 DartEntry::InvokeClosure(closure_obj, dart_arguments, kNoArgumentNames)); 2191 DartEntry::InvokeClosure(closure_obj, dart_arguments, kNoArgumentNames));
2099 return Api::NewLocalHandle(result); 2192 return Api::NewLocalHandle(isolate, result);
2100 } 2193 }
2101 2194
2102 2195
2103 DART_EXPORT int64_t Dart_ClosureSmrck(Dart_Handle object) { 2196 DART_EXPORT int64_t Dart_ClosureSmrck(Dart_Handle object) {
2104 DARTSCOPE(Isolate::Current()); 2197 Isolate* isolate = Isolate::Current();
2105 const Closure& obj = Closure::CheckedHandle(Api::UnwrapHandle(object)); 2198 DARTSCOPE(isolate);
2106 const Integer& smrck = Integer::Handle(obj.smrck()); 2199 const Closure& obj =
2200 Closure::CheckedHandle(isolate, Api::UnwrapHandle(object));
2201 const Integer& smrck = Integer::Handle(isolate, obj.smrck());
2107 return smrck.IsNull() ? 0 : smrck.AsInt64Value(); 2202 return smrck.IsNull() ? 0 : smrck.AsInt64Value();
2108 } 2203 }
2109 2204
2110 2205
2111 DART_EXPORT void Dart_ClosureSetSmrck(Dart_Handle object, int64_t value) { 2206 DART_EXPORT void Dart_ClosureSetSmrck(Dart_Handle object, int64_t value) {
2112 DARTSCOPE(Isolate::Current()); 2207 Isolate* isolate = Isolate::Current();
2113 const Closure& obj = Closure::CheckedHandle(Api::UnwrapHandle(object)); 2208 DARTSCOPE(isolate);
2114 const Integer& smrck = Integer::Handle(Integer::New(value)); 2209 const Closure& obj =
2210 Closure::CheckedHandle(isolate, Api::UnwrapHandle(object));
2211 const Integer& smrck = Integer::Handle(isolate, Integer::New(value));
2115 obj.set_smrck(smrck); 2212 obj.set_smrck(smrck);
2116 } 2213 }
2117 2214
2118 2215
2119 // --- Methods and Fields --- 2216 // --- Methods and Fields ---
2120 2217
2121 2218
2122 DART_EXPORT Dart_Handle Dart_Invoke(Dart_Handle target, 2219 DART_EXPORT Dart_Handle Dart_Invoke(Dart_Handle target,
2123 Dart_Handle name, 2220 Dart_Handle name,
2124 int number_of_arguments, 2221 int number_of_arguments,
2125 Dart_Handle* arguments) { 2222 Dart_Handle* arguments) {
2126 Isolate* isolate = Isolate::Current(); 2223 Isolate* isolate = Isolate::Current();
2127 DARTSCOPE(isolate); 2224 DARTSCOPE(isolate);
2128 2225
2129 const String& function_name = Api::UnwrapStringHandle(name); 2226 const String& function_name = Api::UnwrapStringHandle(isolate, name);
2130 if (function_name.IsNull()) { 2227 if (function_name.IsNull()) {
2131 RETURN_TYPE_ERROR(name, String); 2228 RETURN_TYPE_ERROR(isolate, name, String);
2132 } 2229 }
2133 if (number_of_arguments < 0) { 2230 if (number_of_arguments < 0) {
2134 return Api::NewError( 2231 return Api::NewError(
2135 "%s expects argument 'number_of_arguments' to be non-negative.", 2232 "%s expects argument 'number_of_arguments' to be non-negative.",
2136 CURRENT_FUNC); 2233 CURRENT_FUNC);
2137 } 2234 }
2138 2235
2139 // Check for malformed arguments in the arguments list. 2236 // Check for malformed arguments in the arguments list.
2140 GrowableArray<const Object*> dart_args(number_of_arguments); 2237 GrowableArray<const Object*> dart_args(number_of_arguments);
2141 for (int i = 0; i < number_of_arguments; i++) { 2238 for (int i = 0; i < number_of_arguments; i++) {
2142 const Object& arg = Object::Handle(Api::UnwrapHandle(arguments[i])); 2239 const Object& arg =
2240 Object::Handle(isolate, Api::UnwrapHandle(arguments[i]));
2143 if (!arg.IsNull() && !arg.IsInstance()) { 2241 if (!arg.IsNull() && !arg.IsInstance()) {
2144 if (arg.IsError()) { 2242 if (arg.IsError()) {
2145 return Api::NewLocalHandle(arg); 2243 return Api::NewLocalHandle(isolate, arg);
2146 } else { 2244 } else {
2147 return Api::NewError( 2245 return Api::NewError(
2148 "%s expects argument %d to be an instance of Object.", 2246 "%s expects argument %d to be an instance of Object.",
2149 CURRENT_FUNC, i); 2247 CURRENT_FUNC, i);
2150 } 2248 }
2151 } 2249 }
2152 dart_args.Add(&arg); 2250 dart_args.Add(&arg);
2153 } 2251 }
2154 2252
2155 const Array& kNoArgNames = Array::Handle(); 2253 const Array& kNoArgNames = Array::Handle(isolate);
2156 const Object& obj = Object::Handle(Api::UnwrapHandle(target)); 2254 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(target));
2157 if (obj.IsNull()) { 2255 if (obj.IsNull()) {
2158 return Api::NewError("%s expects argument 'target' to be non-null.", 2256 return Api::NewError("%s expects argument 'target' to be non-null.",
2159 CURRENT_FUNC); 2257 CURRENT_FUNC);
2160 } else if (obj.IsInstance()) { 2258 } else if (obj.IsInstance()) {
2161 Instance& instance = Instance::Handle(); 2259 Instance& instance = Instance::Handle(isolate);
2162 instance ^= obj.raw(); 2260 instance ^= obj.raw();
2163 const Function& function = Function::Handle( 2261 const Function& function = Function::Handle(
2262 isolate,
2164 Resolver::ResolveDynamic(instance, 2263 Resolver::ResolveDynamic(instance,
2165 function_name, 2264 function_name,
2166 (number_of_arguments + 1), 2265 (number_of_arguments + 1),
2167 Resolver::kIsQualified)); 2266 Resolver::kIsQualified));
2168 if (function.IsNull()) { 2267 if (function.IsNull()) {
2169 const Type& type = Type::Handle(instance.GetType()); 2268 const Type& type = Type::Handle(isolate, instance.GetType());
2170 const String& cls_name = String::Handle(type.ClassName()); 2269 const String& cls_name = String::Handle(isolate, type.ClassName());
2171 return Api::NewError("%s: did not find instance method '%s.%s'.", 2270 return Api::NewError("%s: did not find instance method '%s.%s'.",
2172 CURRENT_FUNC, 2271 CURRENT_FUNC,
2173 cls_name.ToCString(), 2272 cls_name.ToCString(),
2174 function_name.ToCString()); 2273 function_name.ToCString());
2175 } 2274 }
2176 const Object& result = Object::Handle( 2275 const Object& result = Object::Handle(
2276 isolate,
2177 DartEntry::InvokeDynamic(instance, function, dart_args, kNoArgNames)); 2277 DartEntry::InvokeDynamic(instance, function, dart_args, kNoArgNames));
2178 return Api::NewLocalHandle(result); 2278 return Api::NewLocalHandle(isolate, result);
2179 2279
2180 } else if (obj.IsClass()) { 2280 } else if (obj.IsClass()) {
2181 // Finalize all classes. 2281 // Finalize all classes.
2182 const char* msg = CheckIsolateState(isolate); 2282 const char* msg = CheckIsolateState(isolate);
2183 if (msg != NULL) { 2283 if (msg != NULL) {
2184 return Api::NewError(msg); 2284 return Api::NewError(msg);
2185 } 2285 }
2186 2286
2187 Class& cls = Class::Handle(); 2287 Class& cls = Class::Handle(isolate);
2188 cls ^= obj.raw(); 2288 cls ^= obj.raw();
2189 const Function& function = Function::Handle( 2289 const Function& function = Function::Handle(
2290 isolate,
2190 Resolver::ResolveStatic(cls, 2291 Resolver::ResolveStatic(cls,
2191 function_name, 2292 function_name,
2192 number_of_arguments, 2293 number_of_arguments,
2193 Array::Handle(), 2294 Array::Handle(isolate),
2194 Resolver::kIsQualified)); 2295 Resolver::kIsQualified));
2195 if (function.IsNull()) { 2296 if (function.IsNull()) {
2196 const String& cls_name = String::Handle(cls.Name()); 2297 const String& cls_name = String::Handle(isolate, cls.Name());
2197 return Api::NewError("%s: did not find static method '%s.%s'.", 2298 return Api::NewError("%s: did not find static method '%s.%s'.",
2198 CURRENT_FUNC, 2299 CURRENT_FUNC,
2199 cls_name.ToCString(), 2300 cls_name.ToCString(),
2200 function_name.ToCString()); 2301 function_name.ToCString());
2201 } 2302 }
2202 const Object& result = Object::Handle( 2303 const Object& result = Object::Handle(
2304 isolate,
2203 DartEntry::InvokeStatic(function, dart_args, kNoArgNames)); 2305 DartEntry::InvokeStatic(function, dart_args, kNoArgNames));
2204 return Api::NewLocalHandle(result); 2306 return Api::NewLocalHandle(isolate, result);
2205 2307
2206 } else if (obj.IsLibrary()) { 2308 } else if (obj.IsLibrary()) {
2207 // Check whether class finalization is needed. 2309 // Check whether class finalization is needed.
2208 bool finalize_classes = true; 2310 bool finalize_classes = true;
2209 Library& lib = Library::Handle(); 2311 Library& lib = Library::Handle(isolate);
2210 lib ^= obj.raw(); 2312 lib ^= obj.raw();
2211 2313
2212 // When calling functions in the dart:builtin library do not finalize as it 2314 // When calling functions in the dart:builtin library do not finalize as it
2213 // should have been prefinalized. 2315 // should have been prefinalized.
2214 Library& builtin = 2316 Library& builtin =
2215 Library::Handle(isolate->object_store()->builtin_library()); 2317 Library::Handle(isolate, isolate->object_store()->builtin_library());
2216 if (builtin.raw() == lib.raw()) { 2318 if (builtin.raw() == lib.raw()) {
2217 finalize_classes = false; 2319 finalize_classes = false;
2218 } 2320 }
2219 2321
2220 // Finalize all classes if needed. 2322 // Finalize all classes if needed.
2221 if (finalize_classes) { 2323 if (finalize_classes) {
2222 const char* msg = CheckIsolateState(isolate); 2324 const char* msg = CheckIsolateState(isolate);
2223 if (msg != NULL) { 2325 if (msg != NULL) {
2224 return Api::NewError(msg); 2326 return Api::NewError(msg);
2225 } 2327 }
2226 } 2328 }
2227 2329
2228 const Function& function = Function::Handle( 2330 const Function& function =
2229 lib.LookupLocalFunction(function_name)); 2331 Function::Handle(isolate, lib.LookupLocalFunction(function_name));
2230 if (function.IsNull()) { 2332 if (function.IsNull()) {
2231 return Api::NewError("%s: did not find top-level function '%s'.", 2333 return Api::NewError("%s: did not find top-level function '%s'.",
2232 CURRENT_FUNC, 2334 CURRENT_FUNC,
2233 function_name.ToCString()); 2335 function_name.ToCString());
2234 } 2336 }
2235 const Object& result = Object::Handle( 2337 const Object& result = Object::Handle(
2236 DartEntry::InvokeStatic(function, dart_args, kNoArgNames)); 2338 isolate, DartEntry::InvokeStatic(function, dart_args, kNoArgNames));
2237 return Api::NewLocalHandle(result); 2339 return Api::NewLocalHandle(isolate, result);
2238 2340
2239 } else { 2341 } else {
2240 return Api::NewError( 2342 return Api::NewError(
2241 "%s expects argument 'target' to be an object, class, or library.", 2343 "%s expects argument 'target' to be an object, class, or library.",
2242 CURRENT_FUNC); 2344 CURRENT_FUNC);
2243 } 2345 }
2244 } 2346 }
2245 2347
2246 2348
2247 DART_EXPORT Dart_Handle Dart_InvokeStatic(Dart_Handle library_in, 2349 DART_EXPORT Dart_Handle Dart_InvokeStatic(Dart_Handle library_in,
2248 Dart_Handle class_name_in, 2350 Dart_Handle class_name_in,
2249 Dart_Handle function_name_in, 2351 Dart_Handle function_name_in,
2250 int number_of_arguments, 2352 int number_of_arguments,
2251 Dart_Handle* arguments) { 2353 Dart_Handle* arguments) {
2252 Isolate* isolate = Isolate::Current(); 2354 Isolate* isolate = Isolate::Current();
2253 DARTSCOPE(isolate); 2355 DARTSCOPE(isolate);
2254 // Check whether class finalization is needed. 2356 // Check whether class finalization is needed.
2255 bool finalize_classes = true; 2357 bool finalize_classes = true;
2256 const Library& library = 2358 const Library& library =
2257 Library::CheckedHandle(Api::UnwrapHandle(library_in)); 2359 Library::CheckedHandle(isolate, Api::UnwrapHandle(library_in));
2258 if (library.IsNull()) { 2360 if (library.IsNull()) {
2259 return Api::NewError("No library specified"); 2361 return Api::NewError("No library specified");
2260 } 2362 }
2261 2363
2262 // When calling functions in the dart:builtin library do not finalize as it 2364 // When calling functions in the dart:builtin library do not finalize as it
2263 // should have been prefinalized. 2365 // should have been prefinalized.
2264 Library& builtin = 2366 Library& builtin =
2265 Library::Handle(isolate->object_store()->builtin_library()); 2367 Library::Handle(isolate, isolate->object_store()->builtin_library());
2266 if (builtin.raw() == library.raw()) { 2368 if (builtin.raw() == library.raw()) {
2267 finalize_classes = false; 2369 finalize_classes = false;
2268 } 2370 }
2269 2371
2270 // Finalize all classes if needed. 2372 // Finalize all classes if needed.
2271 if (finalize_classes) { 2373 if (finalize_classes) {
2272 const char* msg = CheckIsolateState(isolate); 2374 const char* msg = CheckIsolateState(isolate);
2273 if (msg != NULL) { 2375 if (msg != NULL) {
2274 return Api::NewError(msg); 2376 return Api::NewError(msg);
2275 } 2377 }
2276 } 2378 }
2277 2379
2278 // Now try to resolve and invoke the static function. 2380 // Now try to resolve and invoke the static function.
2279 const String& class_name = 2381 const String& class_name =
2280 String::CheckedHandle(Api::UnwrapHandle(class_name_in)); 2382 String::CheckedHandle(isolate, Api::UnwrapHandle(class_name_in));
2281 const String& function_name = 2383 const String& function_name =
2282 String::CheckedHandle(Api::UnwrapHandle(function_name_in)); 2384 String::CheckedHandle(isolate, Api::UnwrapHandle(function_name_in));
2283 const Function& function = Function::Handle( 2385 const Function& function = Function::Handle(
2386 isolate,
2284 Resolver::ResolveStatic(library, 2387 Resolver::ResolveStatic(library,
2285 class_name, 2388 class_name,
2286 function_name, 2389 function_name,
2287 number_of_arguments, 2390 number_of_arguments,
2288 Array::Handle(), // Named arguments are not yet 2391 Array::Handle(isolate), // Named arguments are
2289 // supported in the API. 2392 // not yet supported.
2290 Resolver::kIsQualified)); 2393 Resolver::kIsQualified));
2291 if (function.IsNull()) { 2394 if (function.IsNull()) {
2292 char* msg; 2395 char* msg;
2293 if (class_name.IsNull()) { 2396 if (class_name.IsNull()) {
2294 const char* format = "Unable to find entrypoint: %s()"; 2397 const char* format = "Unable to find entrypoint: %s()";
2295 intptr_t length = OS::SNPrint(NULL, 0, format, function_name.ToCString()); 2398 intptr_t length = OS::SNPrint(NULL, 0, format, function_name.ToCString());
2296 msg = reinterpret_cast<char*>(Api::Allocate(length + 1)); 2399 msg = reinterpret_cast<char*>(Api::Allocate(isolate, length + 1));
2297 OS::SNPrint(msg, (length + 1), format, function_name.ToCString()); 2400 OS::SNPrint(msg, (length + 1), format, function_name.ToCString());
2298 } else { 2401 } else {
2299 const char* format = "Unable to find entrypoint: static %s.%s()"; 2402 const char* format = "Unable to find entrypoint: static %s.%s()";
2300 intptr_t length = OS::SNPrint(NULL, 0, format, 2403 intptr_t length = OS::SNPrint(NULL, 0, format,
2301 class_name.ToCString(), 2404 class_name.ToCString(),
2302 function_name.ToCString()); 2405 function_name.ToCString());
2303 msg = reinterpret_cast<char*>(Api::Allocate(length + 1)); 2406 msg = reinterpret_cast<char*>(Api::Allocate(isolate, length + 1));
2304 OS::SNPrint(msg, (length + 1), format, 2407 OS::SNPrint(msg, (length + 1), format,
2305 class_name.ToCString(), function_name.ToCString()); 2408 class_name.ToCString(), function_name.ToCString());
2306 } 2409 }
2307 return Api::NewError(msg); 2410 return Api::NewError(msg);
2308 } 2411 }
2309 GrowableArray<const Object*> dart_arguments(number_of_arguments); 2412 GrowableArray<const Object*> dart_arguments(number_of_arguments);
2310 for (int i = 0; i < number_of_arguments; i++) { 2413 for (int i = 0; i < number_of_arguments; i++) {
2311 const Object& arg = Object::Handle(Api::UnwrapHandle(arguments[i])); 2414 const Object& arg =
2415 Object::Handle(isolate, Api::UnwrapHandle(arguments[i]));
2312 dart_arguments.Add(&arg); 2416 dart_arguments.Add(&arg);
2313 } 2417 }
2314 const Array& kNoArgumentNames = Array::Handle(); 2418 const Array& kNoArgumentNames = Array::Handle(isolate);
2315 const Object& result = Object::Handle( 2419 const Object& result = Object::Handle(
2420 isolate,
2316 DartEntry::InvokeStatic(function, dart_arguments, kNoArgumentNames)); 2421 DartEntry::InvokeStatic(function, dart_arguments, kNoArgumentNames));
2317 return Api::NewLocalHandle(result); 2422 return Api::NewLocalHandle(isolate, result);
2318 } 2423 }
2319 2424
2320 2425
2321 DART_EXPORT Dart_Handle Dart_InvokeDynamic(Dart_Handle object, 2426 DART_EXPORT Dart_Handle Dart_InvokeDynamic(Dart_Handle object,
2322 Dart_Handle function_name, 2427 Dart_Handle function_name,
2323 int number_of_arguments, 2428 int number_of_arguments,
2324 Dart_Handle* arguments) { 2429 Dart_Handle* arguments) {
2325 Isolate* isolate = Isolate::Current(); 2430 Isolate* isolate = Isolate::Current();
2326 DARTSCOPE(isolate); 2431 DARTSCOPE(isolate);
2327 const Object& obj = Object::Handle(Api::UnwrapHandle(object)); 2432 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(object));
2328 // Let the resolver figure out the correct target for null receiver. 2433 // Let the resolver figure out the correct target for null receiver.
2329 // E.g., (null).toString() should execute correctly. 2434 // E.g., (null).toString() should execute correctly.
2330 if (!obj.IsNull() && !obj.IsInstance()) { 2435 if (!obj.IsNull() && !obj.IsInstance()) {
2331 return Api::NewError( 2436 return Api::NewError(
2332 "Invalid receiver (not instance) passed to invoke dynamic"); 2437 "Invalid receiver (not instance) passed to invoke dynamic");
2333 } 2438 }
2334 if (function_name == NULL) { 2439 if (function_name == NULL) {
2335 return Api::NewError("Invalid function name specified"); 2440 return Api::NewError("Invalid function name specified");
2336 } 2441 }
2337 ASSERT(ClassFinalizer::AllClassesFinalized()); 2442 ASSERT(ClassFinalizer::AllClassesFinalized());
2338 2443
2339 // Now try to resolve and invoke the dynamic function on this object. 2444 // Now try to resolve and invoke the dynamic function on this object.
2340 Instance& receiver = Instance::Handle(); 2445 Instance& receiver = Instance::Handle(isolate);
2341 receiver ^= obj.raw(); 2446 receiver ^= obj.raw();
2342 const String& name = String::CheckedHandle(Api::UnwrapHandle(function_name)); 2447 const String& name =
2448 String::CheckedHandle(isolate, Api::UnwrapHandle(function_name));
2343 const Function& function = Function::Handle( 2449 const Function& function = Function::Handle(
2450 isolate,
2344 Resolver::ResolveDynamic(receiver, 2451 Resolver::ResolveDynamic(receiver,
2345 name, 2452 name,
2346 (number_of_arguments + 1), 2453 (number_of_arguments + 1),
2347 0)); // Named args not yet supported in API. 2454 0)); // Named args not yet supported in API.
2348 if (function.IsNull()) { 2455 if (function.IsNull()) {
2349 // TODO(5415268): Invoke noSuchMethod instead of failing. 2456 // TODO(5415268): Invoke noSuchMethod instead of failing.
2350 OS::PrintErr("Unable to find instance function: %s\n", name.ToCString()); 2457 OS::PrintErr("Unable to find instance function: %s\n", name.ToCString());
2351 return Api::NewError("Unable to find instance function"); 2458 return Api::NewError("Unable to find instance function");
2352 } 2459 }
2353 GrowableArray<const Object*> dart_arguments(number_of_arguments); 2460 GrowableArray<const Object*> dart_arguments(number_of_arguments);
2354 for (int i = 0; i < number_of_arguments; i++) { 2461 for (int i = 0; i < number_of_arguments; i++) {
2355 const Object& arg = Object::Handle(Api::UnwrapHandle(arguments[i])); 2462 const Object& arg =
2463 Object::Handle(isolate, Api::UnwrapHandle(arguments[i]));
2356 dart_arguments.Add(&arg); 2464 dart_arguments.Add(&arg);
2357 } 2465 }
2358 const Array& kNoArgumentNames = Array::Handle(); 2466 const Array& kNoArgumentNames = Array::Handle(isolate);
2359 const Object& result = Object::Handle( 2467 const Object& result = Object::Handle(
2468 isolate,
2360 DartEntry::InvokeDynamic( 2469 DartEntry::InvokeDynamic(
2361 receiver, function, dart_arguments, kNoArgumentNames)); 2470 receiver, function, dart_arguments, kNoArgumentNames));
2362 return Api::NewLocalHandle(result); 2471 return Api::NewLocalHandle(isolate, result);
2363 } 2472 }
2364 2473
2365 2474
2366 static const bool kGetter = true; 2475 static const bool kGetter = true;
2367 static const bool kSetter = false; 2476 static const bool kSetter = false;
2368 2477
2369 2478
2370 static bool FieldIsUninitialized(const Field& fld) { 2479 static bool FieldIsUninitialized(Isolate* isolate, const Field& fld) {
2371 ASSERT(!fld.IsNull()); 2480 ASSERT(!fld.IsNull());
2372 2481
2373 // Return getter method for uninitialized fields, rather than the 2482 // Return getter method for uninitialized fields, rather than the
2374 // field object, since the value in the field object will not be 2483 // field object, since the value in the field object will not be
2375 // initialized until the first time the getter is invoked. 2484 // initialized until the first time the getter is invoked.
2376 const Instance& value = Instance::Handle(fld.value()); 2485 const Instance& value = Instance::Handle(isolate, fld.value());
2377 ASSERT(value.raw() != Object::transition_sentinel()); 2486 ASSERT(value.raw() != Object::transition_sentinel());
2378 return value.raw() == Object::sentinel(); 2487 return value.raw() == Object::sentinel();
2379 } 2488 }
2380 2489
2381 2490
2382 static Dart_Handle LookupStaticField(Dart_Handle clazz, 2491 static Dart_Handle LookupStaticField(Isolate* isolate,
2492 Dart_Handle clazz,
2383 Dart_Handle field_name, 2493 Dart_Handle field_name,
2384 bool is_getter) { 2494 bool is_getter) {
2385 const Object& param1 = Object::Handle(Api::UnwrapHandle(clazz)); 2495 const Object& param1 = Object::Handle(isolate, Api::UnwrapHandle(clazz));
2386 const Object& param2 = Object::Handle(Api::UnwrapHandle(field_name)); 2496 const Object& param2 = Object::Handle(isolate, Api::UnwrapHandle(field_name));
2387 if (param1.IsNull() || !param1.IsClass()) { 2497 if (param1.IsNull() || !param1.IsClass()) {
2388 return Api::NewError("Invalid class specified"); 2498 return Api::NewError("Invalid class specified");
2389 } 2499 }
2390 if (param2.IsNull() || !param2.IsString()) { 2500 if (param2.IsNull() || !param2.IsString()) {
2391 return Api::NewError("Invalid field name specified"); 2501 return Api::NewError("Invalid field name specified");
2392 } 2502 }
2393 Class& cls = Class::Handle(); 2503 Class& cls = Class::Handle(isolate);
2394 cls ^= param1.raw(); 2504 cls ^= param1.raw();
2395 String& fld_name = String::Handle(); 2505 String& fld_name = String::Handle(isolate);
2396 fld_name ^= param2.raw(); 2506 fld_name ^= param2.raw();
2397 const Field& fld = Field::Handle(cls.LookupStaticField(fld_name)); 2507 const Field& fld = Field::Handle(isolate, cls.LookupStaticField(fld_name));
2398 if (is_getter && (fld.IsNull() || FieldIsUninitialized(fld))) { 2508 if (is_getter && (fld.IsNull() || FieldIsUninitialized(isolate, fld))) {
2399 const String& func_name = String::Handle(Field::GetterName(fld_name)); 2509 const String& func_name =
2510 String::Handle(isolate, Field::GetterName(fld_name));
2400 const Function& function = 2511 const Function& function =
2401 Function::Handle(cls.LookupStaticFunction(func_name)); 2512 Function::Handle(isolate, cls.LookupStaticFunction(func_name));
2402 if (!function.IsNull()) { 2513 if (!function.IsNull()) {
2403 return Api::NewLocalHandle(function); 2514 return Api::NewLocalHandle(isolate, function);
2404 } 2515 }
2405 return Api::NewError("Specified field is not found in the class"); 2516 return Api::NewError("Specified field is not found in the class");
2406 } 2517 }
2407 if (fld.IsNull()) { 2518 if (fld.IsNull()) {
2408 return Api::NewError("Specified field is not found in the class"); 2519 return Api::NewError("Specified field is not found in the class");
2409 } 2520 }
2410 return Api::NewLocalHandle(fld); 2521 return Api::NewLocalHandle(isolate, fld);
2411 } 2522 }
2412 2523
2413 2524
2414 static Dart_Handle LookupInstanceField(const Object& object, 2525 static Dart_Handle LookupInstanceField(Isolate* isolate,
2526 const Object& object,
2415 Dart_Handle name, 2527 Dart_Handle name,
2416 bool is_getter) { 2528 bool is_getter) {
2417 const Object& param = Object::Handle(Api::UnwrapHandle(name)); 2529 const Object& param = Object::Handle(isolate, Api::UnwrapHandle(name));
2418 if (param.IsNull() || !param.IsString()) { 2530 if (param.IsNull() || !param.IsString()) {
2419 return Api::NewError("Invalid field name specified"); 2531 return Api::NewError("Invalid field name specified");
2420 } 2532 }
2421 String& field_name = String::Handle(); 2533 String& field_name = String::Handle(isolate);
2422 field_name ^= param.raw(); 2534 field_name ^= param.raw();
2423 String& func_name = String::Handle(); 2535 String& func_name = String::Handle(isolate);
2424 Field& fld = Field::Handle(); 2536 Field& fld = Field::Handle(isolate);
2425 Class& cls = Class::Handle(object.clazz()); 2537 Class& cls = Class::Handle(isolate, object.clazz());
2426 while (!cls.IsNull()) { 2538 while (!cls.IsNull()) {
2427 fld = cls.LookupInstanceField(field_name); 2539 fld = cls.LookupInstanceField(field_name);
2428 if (!fld.IsNull()) { 2540 if (!fld.IsNull()) {
2429 if (!is_getter && fld.is_final()) { 2541 if (!is_getter && fld.is_final()) {
2430 return Api::NewError("Cannot set value of final fields"); 2542 return Api::NewError("Cannot set value of final fields");
2431 } 2543 }
2432 func_name = (is_getter 2544 func_name = (is_getter
2433 ? Field::GetterName(field_name) 2545 ? Field::GetterName(field_name)
2434 : Field::SetterName(field_name)); 2546 : Field::SetterName(field_name));
2435 const Function& function = Function::Handle( 2547 const Function& function =
2436 cls.LookupDynamicFunction(func_name)); 2548 Function::Handle(isolate, cls.LookupDynamicFunction(func_name));
2437 if (function.IsNull()) { 2549 if (function.IsNull()) {
2438 return Api::NewError("Unable to find accessor function in the class"); 2550 return Api::NewError("Unable to find accessor function in the class");
2439 } 2551 }
2440 return Api::NewLocalHandle(function); 2552 return Api::NewLocalHandle(isolate, function);
2441 } 2553 }
2442 cls = cls.SuperClass(); 2554 cls = cls.SuperClass();
2443 } 2555 }
2444 return Api::NewError("Unable to find field '%s'.", field_name.ToCString()); 2556 return Api::NewError("Unable to find field '%s'.", field_name.ToCString());
2445 } 2557 }
2446 2558
2447 2559
2448 DART_EXPORT Dart_Handle Dart_GetField(Dart_Handle container, Dart_Handle name) { 2560 DART_EXPORT Dart_Handle Dart_GetField(Dart_Handle container, Dart_Handle name) {
2449 Isolate* isolate = Isolate::Current(); 2561 Isolate* isolate = Isolate::Current();
2450 DARTSCOPE(isolate); 2562 DARTSCOPE(isolate);
2451 2563
2452 const String& field_name = Api::UnwrapStringHandle(name); 2564 const String& field_name = Api::UnwrapStringHandle(isolate, name);
2453 if (field_name.IsNull()) { 2565 if (field_name.IsNull()) {
2454 RETURN_TYPE_ERROR(name, String); 2566 RETURN_TYPE_ERROR(isolate, name, String);
2455 } 2567 }
2456 2568
2457 const Object& obj = Object::Handle(Api::UnwrapHandle(container)); 2569 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(container));
2458 2570
2459 Field& field = Field::Handle(); 2571 Field& field = Field::Handle(isolate);
2460 Function& getter = Function::Handle(); 2572 Function& getter = Function::Handle(isolate);
2461 if (obj.IsNull()) { 2573 if (obj.IsNull()) {
2462 return Api::NewError("%s expects argument 'container' to be non-null.", 2574 return Api::NewError("%s expects argument 'container' to be non-null.",
2463 CURRENT_FUNC); 2575 CURRENT_FUNC);
2464 } else if (obj.IsInstance()) { 2576 } else if (obj.IsInstance()) {
2465 // Every instance field has a getter Function. Try to find the 2577 // Every instance field has a getter Function. Try to find the
2466 // getter in any superclass and use that function to access the 2578 // getter in any superclass and use that function to access the
2467 // field. 2579 // field.
2468 Instance& instance = Instance::Handle(); 2580 Instance& instance = Instance::Handle(isolate);
2469 instance ^= obj.raw(); 2581 instance ^= obj.raw();
2470 Class& cls = Class::Handle(instance.clazz()); 2582 Class& cls = Class::Handle(isolate, instance.clazz());
2471 while (!cls.IsNull()) { 2583 while (!cls.IsNull()) {
2472 String& getter_name = String::Handle(Field::GetterName(field_name)); 2584 String& getter_name =
2585 String::Handle(isolate, Field::GetterName(field_name));
2473 getter = cls.LookupDynamicFunction(getter_name); 2586 getter = cls.LookupDynamicFunction(getter_name);
2474 if (!getter.IsNull()) { 2587 if (!getter.IsNull()) {
2475 break; 2588 break;
2476 } 2589 }
2477 cls = cls.SuperClass(); 2590 cls = cls.SuperClass();
2478 } 2591 }
2479 2592
2480 if (getter.IsNull()) { 2593 if (getter.IsNull()) {
2481 return Api::NewError("%s: did not find instance field '%s'.", 2594 return Api::NewError("%s: did not find instance field '%s'.",
2482 CURRENT_FUNC, field_name.ToCString()); 2595 CURRENT_FUNC, field_name.ToCString());
2483 } 2596 }
2484 2597
2485 // Invoke the getter and return the result. 2598 // Invoke the getter and return the result.
2486 GrowableArray<const Object*> args; 2599 GrowableArray<const Object*> args;
2487 const Array& kNoArgNames = Array::Handle(); 2600 const Array& kNoArgNames = Array::Handle(isolate);
2488 const Object& result = Object::Handle( 2601 const Object& result = Object::Handle(
2602 isolate,
2489 DartEntry::InvokeDynamic(instance, getter, args, kNoArgNames)); 2603 DartEntry::InvokeDynamic(instance, getter, args, kNoArgNames));
2490 return Api::NewLocalHandle(result); 2604 return Api::NewLocalHandle(isolate, result);
2491 2605
2492 } else if (obj.IsClass()) { 2606 } else if (obj.IsClass()) {
2493 // To access a static field we may need to use the Field or the 2607 // To access a static field we may need to use the Field or the
2494 // getter Function. 2608 // getter Function.
2495 Class& cls = Class::Handle(); 2609 Class& cls = Class::Handle(isolate);
2496 cls ^= obj.raw(); 2610 cls ^= obj.raw();
2497 field = cls.LookupStaticField(field_name); 2611 field = cls.LookupStaticField(field_name);
2498 if (field.IsNull() || FieldIsUninitialized(field)) { 2612 if (field.IsNull() || FieldIsUninitialized(isolate, field)) {
2499 const String& getter_name = String::Handle(Field::GetterName(field_name)); 2613 const String& getter_name =
2614 String::Handle(isolate, Field::GetterName(field_name));
2500 getter = cls.LookupStaticFunction(getter_name); 2615 getter = cls.LookupStaticFunction(getter_name);
2501 } 2616 }
2502 2617
2503 if (!getter.IsNull()) { 2618 if (!getter.IsNull()) {
2504 // Invoke the getter and return the result. 2619 // Invoke the getter and return the result.
2505 GrowableArray<const Object*> args; 2620 GrowableArray<const Object*> args;
2506 const Array& kNoArgNames = Array::Handle(); 2621 const Array& kNoArgNames = Array::Handle(isolate);
2507 const Object& result = Object::Handle( 2622 const Object& result = Object::Handle(
2508 DartEntry::InvokeStatic(getter, args, kNoArgNames)); 2623 isolate, DartEntry::InvokeStatic(getter, args, kNoArgNames));
2509 return Api::NewLocalHandle(result); 2624 return Api::NewLocalHandle(isolate, result);
2510 } else if (!field.IsNull()) { 2625 } else if (!field.IsNull()) {
2511 const Object& result = Object::Handle(field.value()); 2626 const Object& result = Object::Handle(isolate, field.value());
2512 return Api::NewLocalHandle(result); 2627 return Api::NewLocalHandle(isolate, result);
2513 } else { 2628 } else {
2514 return Api::NewError("%s: did not find static field '%s'.", 2629 return Api::NewError("%s: did not find static field '%s'.",
2515 CURRENT_FUNC, field_name.ToCString()); 2630 CURRENT_FUNC, field_name.ToCString());
2516 } 2631 }
2517 2632
2518 } else if (obj.IsLibrary()) { 2633 } else if (obj.IsLibrary()) {
2519 // To access a top-level we may need to use the Field or the 2634 // To access a top-level we may need to use the Field or the
2520 // getter Function. The getter function may either be in the 2635 // getter Function. The getter function may either be in the
2521 // library or in the field's owner class, depending. 2636 // library or in the field's owner class, depending.
2522 Library& lib = Library::Handle(); 2637 Library& lib = Library::Handle(isolate);
2523 lib ^= obj.raw(); 2638 lib ^= obj.raw();
2524 field = lib.LookupLocalField(field_name); 2639 field = lib.LookupLocalField(field_name);
2525 if (field.IsNull()) { 2640 if (field.IsNull()) {
2526 // No field found. Check for a getter in the lib. 2641 // No field found. Check for a getter in the lib.
2527 const String& getter_name = String::Handle(Field::GetterName(field_name)); 2642 const String& getter_name =
2643 String::Handle(isolate, Field::GetterName(field_name));
2528 getter = lib.LookupLocalFunction(getter_name); 2644 getter = lib.LookupLocalFunction(getter_name);
2529 } else if (FieldIsUninitialized(field)) { 2645 } else if (FieldIsUninitialized(isolate, field)) {
2530 // A field was found. Check for a getter in the field's owner classs. 2646 // A field was found. Check for a getter in the field's owner classs.
2531 const Class& cls = Class::Handle(field.owner()); 2647 const Class& cls = Class::Handle(isolate, field.owner());
2532 const String& getter_name = String::Handle(Field::GetterName(field_name)); 2648 const String& getter_name =
2649 String::Handle(isolate, Field::GetterName(field_name));
2533 getter = cls.LookupStaticFunction(getter_name); 2650 getter = cls.LookupStaticFunction(getter_name);
2534 } 2651 }
2535 2652
2536 if (!getter.IsNull()) { 2653 if (!getter.IsNull()) {
2537 // Invoke the getter and return the result. 2654 // Invoke the getter and return the result.
2538 GrowableArray<const Object*> args; 2655 GrowableArray<const Object*> args;
2539 const Array& kNoArgNames = Array::Handle(); 2656 const Array& kNoArgNames = Array::Handle(isolate);
2540 const Object& result = Object::Handle( 2657 const Object& result = Object::Handle(
2541 DartEntry::InvokeStatic(getter, args, kNoArgNames)); 2658 isolate, DartEntry::InvokeStatic(getter, args, kNoArgNames));
2542 return Api::NewLocalHandle(result); 2659 return Api::NewLocalHandle(isolate, result);
2543 } else if (!field.IsNull()) { 2660 } else if (!field.IsNull()) {
2544 const Object& result = Object::Handle(field.value()); 2661 const Object& result = Object::Handle(isolate, field.value());
2545 return Api::NewLocalHandle(result); 2662 return Api::NewLocalHandle(isolate, result);
2546 } else { 2663 } else {
2547 return Api::NewError("%s: did not find top-level variable '%s'.", 2664 return Api::NewError("%s: did not find top-level variable '%s'.",
2548 CURRENT_FUNC, field_name.ToCString()); 2665 CURRENT_FUNC, field_name.ToCString());
2549 } 2666 }
2550 2667
2551 } else { 2668 } else {
2552 return Api::NewError( 2669 return Api::NewError(
2553 "%s expects argument 'container' to be an object, class, or library.", 2670 "%s expects argument 'container' to be an object, class, or library.",
2554 CURRENT_FUNC); 2671 CURRENT_FUNC);
2555 } 2672 }
2556 } 2673 }
2557 2674
2558 2675
2559 DART_EXPORT Dart_Handle Dart_SetField(Dart_Handle container, 2676 DART_EXPORT Dart_Handle Dart_SetField(Dart_Handle container,
2560 Dart_Handle name, 2677 Dart_Handle name,
2561 Dart_Handle value) { 2678 Dart_Handle value) {
2562 Isolate* isolate = Isolate::Current(); 2679 Isolate* isolate = Isolate::Current();
2563 DARTSCOPE(isolate); 2680 DARTSCOPE(isolate);
2564 2681
2565 const String& field_name = Api::UnwrapStringHandle(name); 2682 const String& field_name = Api::UnwrapStringHandle(isolate, name);
2566 if (field_name.IsNull()) { 2683 if (field_name.IsNull()) {
2567 RETURN_TYPE_ERROR(name, String); 2684 RETURN_TYPE_ERROR(isolate, name, String);
2568 } 2685 }
2569 2686
2570 // Since null is allowed for value, we don't use UnwrapInstanceHandle. 2687 // Since null is allowed for value, we don't use UnwrapInstanceHandle.
2571 const Object& value_obj = Object::Handle(Api::UnwrapHandle(value)); 2688 const Object& value_obj = Object::Handle(isolate, Api::UnwrapHandle(value));
2572 if (!value_obj.IsNull() && !value_obj.IsInstance()) { 2689 if (!value_obj.IsNull() && !value_obj.IsInstance()) {
2573 RETURN_TYPE_ERROR(value, Instance); 2690 RETURN_TYPE_ERROR(isolate, value, Instance);
2574 } 2691 }
2575 Instance& value_instance = Instance::Handle(); 2692 Instance& value_instance = Instance::Handle(isolate);
2576 value_instance ^= value_obj.raw(); 2693 value_instance ^= value_obj.raw();
2577 2694
2578 Field& field = Field::Handle(); 2695 Field& field = Field::Handle(isolate);
2579 Function& setter = Function::Handle(); 2696 Function& setter = Function::Handle(isolate);
2580 const Object& obj = Object::Handle(Api::UnwrapHandle(container)); 2697 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(container));
2581 if (obj.IsNull()) { 2698 if (obj.IsNull()) {
2582 return Api::NewError("%s expects argument 'container' to be non-null.", 2699 return Api::NewError("%s expects argument 'container' to be non-null.",
2583 CURRENT_FUNC); 2700 CURRENT_FUNC);
2584 } else if (obj.IsInstance()) { 2701 } else if (obj.IsInstance()) {
2585 // Every instance field has a setter Function. Try to find the 2702 // Every instance field has a setter Function. Try to find the
2586 // setter in any superclass and use that function to access the 2703 // setter in any superclass and use that function to access the
2587 // field. 2704 // field.
2588 Instance& instance = Instance::Handle(); 2705 Instance& instance = Instance::Handle(isolate);
2589 instance ^= obj.raw(); 2706 instance ^= obj.raw();
2590 Class& cls = Class::Handle(instance.clazz()); 2707 Class& cls = Class::Handle(isolate, instance.clazz());
2591 while (!cls.IsNull()) { 2708 while (!cls.IsNull()) {
2592 field = cls.LookupInstanceField(field_name); 2709 field = cls.LookupInstanceField(field_name);
2593 if (!field.IsNull() && field.is_final()) { 2710 if (!field.IsNull() && field.is_final()) {
2594 return Api::NewError("%s: cannot set final field '%s'.", 2711 return Api::NewError("%s: cannot set final field '%s'.",
2595 CURRENT_FUNC, field_name.ToCString()); 2712 CURRENT_FUNC, field_name.ToCString());
2596 } 2713 }
2597 String& setter_name = String::Handle(Field::SetterName(field_name)); 2714 String& setter_name =
2715 String::Handle(isolate, Field::SetterName(field_name));
2598 setter = cls.LookupDynamicFunction(setter_name); 2716 setter = cls.LookupDynamicFunction(setter_name);
2599 if (!setter.IsNull()) { 2717 if (!setter.IsNull()) {
2600 break; 2718 break;
2601 } 2719 }
2602 cls = cls.SuperClass(); 2720 cls = cls.SuperClass();
2603 } 2721 }
2604 2722
2605 if (setter.IsNull()) { 2723 if (setter.IsNull()) {
2606 return Api::NewError("%s: did not find instance field '%s'.", 2724 return Api::NewError("%s: did not find instance field '%s'.",
2607 CURRENT_FUNC, field_name.ToCString()); 2725 CURRENT_FUNC, field_name.ToCString());
2608 } 2726 }
2609 2727
2610 // Invoke the setter and return the result. 2728 // Invoke the setter and return the result.
2611 GrowableArray<const Object*> args(1); 2729 GrowableArray<const Object*> args(1);
2612 args.Add(&value_instance); 2730 args.Add(&value_instance);
2613 const Array& kNoArgNames = Array::Handle(); 2731 const Array& kNoArgNames = Array::Handle(isolate);
2614 const Object& result = Object::Handle( 2732 const Object& result = Object::Handle(
2733 isolate,
2615 DartEntry::InvokeDynamic(instance, setter, args, kNoArgNames)); 2734 DartEntry::InvokeDynamic(instance, setter, args, kNoArgNames));
2616 return Api::NewLocalHandle(result); 2735 return Api::NewLocalHandle(isolate, result);
2617 2736
2618 } else if (obj.IsClass()) { 2737 } else if (obj.IsClass()) {
2619 // To access a static field we may need to use the Field or the 2738 // To access a static field we may need to use the Field or the
2620 // setter Function. 2739 // setter Function.
2621 Class& cls = Class::Handle(); 2740 Class& cls = Class::Handle(isolate);
2622 cls ^= obj.raw(); 2741 cls ^= obj.raw();
2623 field = cls.LookupStaticField(field_name); 2742 field = cls.LookupStaticField(field_name);
2624 if (field.IsNull()) { 2743 if (field.IsNull()) {
2625 String& setter_name = String::Handle(Field::SetterName(field_name)); 2744 String& setter_name =
2745 String::Handle(isolate, Field::SetterName(field_name));
2626 setter = cls.LookupStaticFunction(setter_name); 2746 setter = cls.LookupStaticFunction(setter_name);
2627 } 2747 }
2628 2748
2629 if (!setter.IsNull()) { 2749 if (!setter.IsNull()) {
2630 // Invoke the setter and return the result. 2750 // Invoke the setter and return the result.
2631 GrowableArray<const Object*> args(1); 2751 GrowableArray<const Object*> args(1);
2632 args.Add(&value_instance); 2752 args.Add(&value_instance);
2633 const Array& kNoArgNames = Array::Handle(); 2753 const Array& kNoArgNames = Array::Handle(isolate);
2634 const Object& result = Object::Handle( 2754 const Object& result = Object::Handle(
2755 isolate,
2635 DartEntry::InvokeStatic(setter, args, kNoArgNames)); 2756 DartEntry::InvokeStatic(setter, args, kNoArgNames));
2636 if (result.IsError()) { 2757 if (result.IsError()) {
2637 return Api::NewLocalHandle(result); 2758 return Api::NewLocalHandle(isolate, result);
2638 } else { 2759 } else {
2639 return Api::Success(); 2760 return Api::Success(isolate);
2640 } 2761 }
2641 } else if (!field.IsNull()) { 2762 } else if (!field.IsNull()) {
2642 if (field.is_final()) { 2763 if (field.is_final()) {
2643 return Api::NewError("%s: cannot set final field '%s'.", 2764 return Api::NewError("%s: cannot set final field '%s'.",
2644 CURRENT_FUNC, field_name.ToCString()); 2765 CURRENT_FUNC, field_name.ToCString());
2645 } else { 2766 } else {
2646 field.set_value(value_instance); 2767 field.set_value(value_instance);
2647 return Api::Success(); 2768 return Api::Success(isolate);
2648 } 2769 }
2649 } else { 2770 } else {
2650 return Api::NewError("%s: did not find static field '%s'.", 2771 return Api::NewError("%s: did not find static field '%s'.",
2651 CURRENT_FUNC, field_name.ToCString()); 2772 CURRENT_FUNC, field_name.ToCString());
2652 } 2773 }
2653 2774
2654 } else if (obj.IsLibrary()) { 2775 } else if (obj.IsLibrary()) {
2655 // To access a top-level we may need to use the Field or the 2776 // To access a top-level we may need to use the Field or the
2656 // setter Function. The setter function may either be in the 2777 // setter Function. The setter function may either be in the
2657 // library or in the field's owner class, depending. 2778 // library or in the field's owner class, depending.
2658 Library& lib = Library::Handle(); 2779 Library& lib = Library::Handle(isolate);
2659 lib ^= obj.raw(); 2780 lib ^= obj.raw();
2660 field = lib.LookupLocalField(field_name); 2781 field = lib.LookupLocalField(field_name);
2661 if (field.IsNull()) { 2782 if (field.IsNull()) {
2662 const String& setter_name = String::Handle(Field::SetterName(field_name)); 2783 const String& setter_name =
2784 String::Handle(isolate, Field::SetterName(field_name));
2663 setter ^= lib.LookupLocalFunction(setter_name); 2785 setter ^= lib.LookupLocalFunction(setter_name);
2664 } 2786 }
2665 2787
2666 if (!setter.IsNull()) { 2788 if (!setter.IsNull()) {
2667 // Invoke the setter and return the result. 2789 // Invoke the setter and return the result.
2668 GrowableArray<const Object*> args(1); 2790 GrowableArray<const Object*> args(1);
2669 args.Add(&value_instance); 2791 args.Add(&value_instance);
2670 const Array& kNoArgNames = Array::Handle(); 2792 const Array& kNoArgNames = Array::Handle(isolate);
2671 const Object& result = Object::Handle( 2793 const Object& result = Object::Handle(
2672 DartEntry::InvokeStatic(setter, args, kNoArgNames)); 2794 isolate, DartEntry::InvokeStatic(setter, args, kNoArgNames));
2673 if (result.IsError()) { 2795 if (result.IsError()) {
2674 return Api::NewLocalHandle(result); 2796 return Api::NewLocalHandle(isolate, result);
2675 } else { 2797 } else {
2676 return Api::Success(); 2798 return Api::Success(isolate);
2677 } 2799 }
2678 } else if (!field.IsNull()) { 2800 } else if (!field.IsNull()) {
2679 if (field.is_final()) { 2801 if (field.is_final()) {
2680 return Api::NewError("%s: cannot set final top-level variable '%s'.", 2802 return Api::NewError("%s: cannot set final top-level variable '%s'.",
2681 CURRENT_FUNC, field_name.ToCString()); 2803 CURRENT_FUNC, field_name.ToCString());
2682 } else { 2804 } else {
2683 field.set_value(value_instance); 2805 field.set_value(value_instance);
2684 return Api::Success(); 2806 return Api::Success(isolate);
2685 } 2807 }
2686 } else { 2808 } else {
2687 return Api::NewError("%s: did not find top-level variable '%s'.", 2809 return Api::NewError("%s: did not find top-level variable '%s'.",
2688 CURRENT_FUNC, field_name.ToCString()); 2810 CURRENT_FUNC, field_name.ToCString());
2689 } 2811 }
2690 2812
2691 } else { 2813 } else {
2692 return Api::NewError( 2814 return Api::NewError(
2693 "%s expects argument 'container' to be an object, class, or library.", 2815 "%s expects argument 'container' to be an object, class, or library.",
2694 CURRENT_FUNC); 2816 CURRENT_FUNC);
2695 } 2817 }
2696 } 2818 }
2697 2819
2698 2820
2699 DART_EXPORT Dart_Handle Dart_GetStaticField(Dart_Handle cls, 2821 DART_EXPORT Dart_Handle Dart_GetStaticField(Dart_Handle cls,
2700 Dart_Handle name) { 2822 Dart_Handle name) {
2701 Isolate* isolate = Isolate::Current(); 2823 Isolate* isolate = Isolate::Current();
2702 DARTSCOPE(isolate); 2824 DARTSCOPE(isolate);
2703 Dart_Handle result = LookupStaticField(cls, name, kGetter); 2825 Dart_Handle result = LookupStaticField(isolate, cls, name, kGetter);
2704 if (::Dart_IsError(result)) { 2826 if (::Dart_IsError(result)) {
2705 return result; 2827 return result;
2706 } 2828 }
2707 Object& retval = Object::Handle(); 2829 Object& retval = Object::Handle(isolate);
2708 const Object& obj = Object::Handle(Api::UnwrapHandle(result)); 2830 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(result));
2709 if (obj.IsField()) { 2831 if (obj.IsField()) {
2710 Field& fld = Field::Handle(); 2832 Field& fld = Field::Handle(isolate);
2711 fld ^= obj.raw(); 2833 fld ^= obj.raw();
2712 retval = fld.value(); 2834 retval = fld.value();
2713 return Api::NewLocalHandle(retval); 2835 return Api::NewLocalHandle(isolate, retval);
2714 } else { 2836 } else {
2715 Function& func = Function::Handle(); 2837 Function& func = Function::Handle(isolate);
2716 func ^= obj.raw(); 2838 func ^= obj.raw();
2717 GrowableArray<const Object*> args; 2839 GrowableArray<const Object*> args;
2718 const Array& kNoArgumentNames = Array::Handle(); 2840 const Array& kNoArgumentNames = Array::Handle(isolate);
2719 const Object& result = Object::Handle( 2841 const Object& result = Object::Handle(
2720 DartEntry::InvokeStatic(func, args, kNoArgumentNames)); 2842 isolate, DartEntry::InvokeStatic(func, args, kNoArgumentNames));
2721 return Api::NewLocalHandle(result); 2843 return Api::NewLocalHandle(isolate, result);
2722 } 2844 }
2723 } 2845 }
2724 2846
2725 2847
2726 // TODO(iposva): The value parameter should be documented as being an instance. 2848 // TODO(iposva): The value parameter should be documented as being an instance.
2727 // TODO(turnidge): Is this skipping the setter? 2849 // TODO(turnidge): Is this skipping the setter?
2728 DART_EXPORT Dart_Handle Dart_SetStaticField(Dart_Handle cls, 2850 DART_EXPORT Dart_Handle Dart_SetStaticField(Dart_Handle cls,
2729 Dart_Handle name, 2851 Dart_Handle name,
2730 Dart_Handle value) { 2852 Dart_Handle value) {
2731 DARTSCOPE(Isolate::Current()); 2853 Isolate* isolate = Isolate::Current();
2732 Dart_Handle result = LookupStaticField(cls, name, kSetter); 2854 DARTSCOPE(isolate);
2855 Dart_Handle result = LookupStaticField(isolate, cls, name, kSetter);
2733 if (::Dart_IsError(result)) { 2856 if (::Dart_IsError(result)) {
2734 return result; 2857 return result;
2735 } 2858 }
2736 Field& fld = Field::Handle(); 2859 Field& fld = Field::Handle(isolate);
2737 fld ^= Api::UnwrapHandle(result); 2860 fld ^= Api::UnwrapHandle(result);
2738 if (fld.is_final()) { 2861 if (fld.is_final()) {
2739 return Api::NewError( 2862 return Api::NewError(
2740 "Specified field is a static final field in the class"); 2863 "Specified field is a static final field in the class");
2741 } 2864 }
2742 const Object& val = Object::Handle(Api::UnwrapHandle(value)); 2865 const Object& val = Object::Handle(isolate, Api::UnwrapHandle(value));
2743 Instance& instance = Instance::Handle(); 2866 Instance& instance = Instance::Handle(isolate);
2744 instance ^= val.raw(); 2867 instance ^= val.raw();
2745 fld.set_value(instance); 2868 fld.set_value(instance);
2746 return Api::NewLocalHandle(val); 2869 return Api::NewLocalHandle(isolate, val);
2747 } 2870 }
2748 2871
2749 2872
2750 DART_EXPORT Dart_Handle Dart_GetInstanceField(Dart_Handle obj, 2873 DART_EXPORT Dart_Handle Dart_GetInstanceField(Dart_Handle obj,
2751 Dart_Handle name) { 2874 Dart_Handle name) {
2752 Isolate* isolate = Isolate::Current(); 2875 Isolate* isolate = Isolate::Current();
2753 DARTSCOPE(isolate); 2876 DARTSCOPE(isolate);
2754 const Object& param = Object::Handle(Api::UnwrapHandle(obj)); 2877 const Object& param = Object::Handle(isolate, Api::UnwrapHandle(obj));
2755 if (param.IsNull() || !param.IsInstance()) { 2878 if (param.IsNull() || !param.IsInstance()) {
2756 return Api::NewError("Invalid object passed in to access instance field"); 2879 return Api::NewError("Invalid object passed in to access instance field");
2757 } 2880 }
2758 Instance& object = Instance::Handle(); 2881 Instance& object = Instance::Handle(isolate);
2759 object ^= param.raw(); 2882 object ^= param.raw();
2760 Dart_Handle result = LookupInstanceField(object, name, kGetter); 2883 Dart_Handle result = LookupInstanceField(isolate, object, name, kGetter);
2761 if (::Dart_IsError(result)) { 2884 if (::Dart_IsError(result)) {
2762 return result; 2885 return result;
2763 } 2886 }
2764 Function& func = Function::Handle(); 2887 Function& func = Function::Handle(isolate);
2765 func ^= Api::UnwrapHandle(result); 2888 func ^= Api::UnwrapHandle(result);
2766 GrowableArray<const Object*> arguments; 2889 GrowableArray<const Object*> arguments;
2767 const Array& kNoArgumentNames = Array::Handle(); 2890 const Array& kNoArgumentNames = Array::Handle(isolate);
2768 const Object& retval = Object::Handle( 2891 const Object& retval = Object::Handle(
2892 isolate,
2769 DartEntry::InvokeDynamic(object, func, arguments, kNoArgumentNames)); 2893 DartEntry::InvokeDynamic(object, func, arguments, kNoArgumentNames));
2770 return Api::NewLocalHandle(retval); 2894 return Api::NewLocalHandle(isolate, retval);
2771 } 2895 }
2772 2896
2773 2897
2774 DART_EXPORT Dart_Handle Dart_SetInstanceField(Dart_Handle obj, 2898 DART_EXPORT Dart_Handle Dart_SetInstanceField(Dart_Handle obj,
2775 Dart_Handle name, 2899 Dart_Handle name,
2776 Dart_Handle value) { 2900 Dart_Handle value) {
2777 Isolate* isolate = Isolate::Current(); 2901 Isolate* isolate = Isolate::Current();
2778 DARTSCOPE(isolate); 2902 DARTSCOPE(isolate);
2779 const Object& param = Object::Handle(Api::UnwrapHandle(obj)); 2903 const Object& param = Object::Handle(isolate, Api::UnwrapHandle(obj));
2780 if (param.IsNull() || !param.IsInstance()) { 2904 if (param.IsNull() || !param.IsInstance()) {
2781 return Api::NewError("Invalid object passed in to access instance field"); 2905 return Api::NewError("Invalid object passed in to access instance field");
2782 } 2906 }
2783 Instance& object = Instance::Handle(); 2907 Instance& object = Instance::Handle(isolate);
2784 object ^= param.raw(); 2908 object ^= param.raw();
2785 Dart_Handle result = LookupInstanceField(object, name, kSetter); 2909 Dart_Handle result = LookupInstanceField(isolate, object, name, kSetter);
2786 if (::Dart_IsError(result)) { 2910 if (::Dart_IsError(result)) {
2787 return result; 2911 return result;
2788 } 2912 }
2789 Function& func = Function::Handle(); 2913 Function& func = Function::Handle(isolate);
2790 func ^= Api::UnwrapHandle(result); 2914 func ^= Api::UnwrapHandle(result);
2791 GrowableArray<const Object*> arguments(1); 2915 GrowableArray<const Object*> arguments(1);
2792 const Object& arg = Object::Handle(Api::UnwrapHandle(value)); 2916 const Object& arg = Object::Handle(isolate, Api::UnwrapHandle(value));
2793 arguments.Add(&arg); 2917 arguments.Add(&arg);
2794 const Array& kNoArgumentNames = Array::Handle(); 2918 const Array& kNoArgumentNames = Array::Handle(isolate);
2795 const Object& retval = Object::Handle( 2919 const Object& retval = Object::Handle(
2920 isolate,
2796 DartEntry::InvokeDynamic(object, func, arguments, kNoArgumentNames)); 2921 DartEntry::InvokeDynamic(object, func, arguments, kNoArgumentNames));
2797 return Api::NewLocalHandle(retval); 2922 return Api::NewLocalHandle(isolate, retval);
2798 } 2923 }
2799 2924
2800 2925
2801 DART_EXPORT Dart_Handle Dart_CreateNativeWrapperClass(Dart_Handle library, 2926 DART_EXPORT Dart_Handle Dart_CreateNativeWrapperClass(Dart_Handle library,
2802 Dart_Handle name, 2927 Dart_Handle name,
2803 int field_count) { 2928 int field_count) {
2804 Isolate* isolate = Isolate::Current(); 2929 Isolate* isolate = Isolate::Current();
2805 DARTSCOPE(isolate); 2930 DARTSCOPE(isolate);
2806 const Object& param = Object::Handle(Api::UnwrapHandle(name)); 2931 const Object& param = Object::Handle(isolate, Api::UnwrapHandle(name));
2807 if (param.IsNull() || !param.IsString() || field_count <= 0) { 2932 if (param.IsNull() || !param.IsString() || field_count <= 0) {
2808 return Api::NewError( 2933 return Api::NewError(
2809 "Invalid arguments passed to Dart_CreateNativeWrapperClass"); 2934 "Invalid arguments passed to Dart_CreateNativeWrapperClass");
2810 } 2935 }
2811 String& cls_name = String::Handle(); 2936 String& cls_name = String::Handle(isolate);
2812 cls_name ^= param.raw(); 2937 cls_name ^= param.raw();
2813 cls_name = String::NewSymbol(cls_name); 2938 cls_name = String::NewSymbol(cls_name);
2814 Library& lib = Library::Handle(); 2939 Library& lib = Library::Handle(isolate);
2815 lib ^= Api::UnwrapHandle(library); 2940 lib ^= Api::UnwrapHandle(library);
2816 if (lib.IsNull()) { 2941 if (lib.IsNull()) {
2817 return Api::NewError( 2942 return Api::NewError(
2818 "Invalid arguments passed to Dart_CreateNativeWrapperClass"); 2943 "Invalid arguments passed to Dart_CreateNativeWrapperClass");
2819 } 2944 }
2820 const Class& cls = Class::Handle(Class::NewNativeWrapper(&lib, 2945 const Class& cls = Class::Handle(
2821 cls_name, 2946 isolate, Class::NewNativeWrapper(&lib, cls_name, field_count));
2822 field_count));
2823 if (cls.IsNull()) { 2947 if (cls.IsNull()) {
2824 return Api::NewError( 2948 return Api::NewError(
2825 "Unable to create native wrapper class : already exists"); 2949 "Unable to create native wrapper class : already exists");
2826 } 2950 }
2827 return Api::NewLocalHandle(cls); 2951 return Api::NewLocalHandle(isolate, cls);
2828 } 2952 }
2829 2953
2830 2954
2831 DART_EXPORT Dart_Handle Dart_GetNativeInstanceField(Dart_Handle obj, 2955 DART_EXPORT Dart_Handle Dart_GetNativeInstanceField(Dart_Handle obj,
2832 int index, 2956 int index,
2833 intptr_t* value) { 2957 intptr_t* value) {
2834 DARTSCOPE(Isolate::Current()); 2958 Isolate* isolate = Isolate::Current();
2835 const Object& param = Object::Handle(Api::UnwrapHandle(obj)); 2959 DARTSCOPE(isolate);
2960 const Object& param = Object::Handle(isolate, Api::UnwrapHandle(obj));
2836 if (param.IsNull() || !param.IsInstance()) { 2961 if (param.IsNull() || !param.IsInstance()) {
2837 return Api::NewError( 2962 return Api::NewError(
2838 "Invalid object passed in to access native instance field"); 2963 "Invalid object passed in to access native instance field");
2839 } 2964 }
2840 Instance& object = Instance::Handle(); 2965 Instance& object = Instance::Handle(isolate);
2841 object ^= param.raw(); 2966 object ^= param.raw();
2842 if (!object.IsValidNativeIndex(index)) { 2967 if (!object.IsValidNativeIndex(index)) {
2843 return Api::NewError( 2968 return Api::NewError(
2844 "Invalid index passed in to access native instance field"); 2969 "Invalid index passed in to access native instance field");
2845 } 2970 }
2846 *value = object.GetNativeField(index); 2971 *value = object.GetNativeField(index);
2847 return Api::Success(); 2972 return Api::Success(isolate);
2848 } 2973 }
2849 2974
2850 2975
2851 DART_EXPORT Dart_Handle Dart_SetNativeInstanceField(Dart_Handle obj, 2976 DART_EXPORT Dart_Handle Dart_SetNativeInstanceField(Dart_Handle obj,
2852 int index, 2977 int index,
2853 intptr_t value) { 2978 intptr_t value) {
2854 DARTSCOPE(Isolate::Current()); 2979 Isolate* isolate = Isolate::Current();
2855 const Object& param = Object::Handle(Api::UnwrapHandle(obj)); 2980 DARTSCOPE(isolate);
2981 const Object& param = Object::Handle(isolate, Api::UnwrapHandle(obj));
2856 if (param.IsNull() || !param.IsInstance()) { 2982 if (param.IsNull() || !param.IsInstance()) {
2857 return Api::NewError( 2983 return Api::NewError(
2858 "Invalid object passed in to set native instance field"); 2984 "Invalid object passed in to set native instance field");
2859 } 2985 }
2860 Instance& object = Instance::Handle(); 2986 Instance& object = Instance::Handle(isolate);
2861 object ^= param.raw(); 2987 object ^= param.raw();
2862 if (!object.IsValidNativeIndex(index)) { 2988 if (!object.IsValidNativeIndex(index)) {
2863 return Api::NewError( 2989 return Api::NewError(
2864 "Invalid index passed in to set native instance field"); 2990 "Invalid index passed in to set native instance field");
2865 } 2991 }
2866 object.SetNativeField(index, value); 2992 object.SetNativeField(index, value);
2867 return Api::Success(); 2993 return Api::Success(isolate);
2868 } 2994 }
2869 2995
2870 2996
2871 // --- Exceptions ---- 2997 // --- Exceptions ----
2872 2998
2873 2999
2874 DART_EXPORT Dart_Handle Dart_ThrowException(Dart_Handle exception) { 3000 DART_EXPORT Dart_Handle Dart_ThrowException(Dart_Handle exception) {
2875 Isolate* isolate = Isolate::Current(); 3001 Isolate* isolate = Isolate::Current();
2876 DARTSCOPE(isolate); 3002 DARTSCOPE(isolate);
2877 if (isolate->top_exit_frame_info() == 0) { 3003 if (isolate->top_exit_frame_info() == 0) {
2878 // There are no dart frames on the stack so it would be illegal to 3004 // There are no dart frames on the stack so it would be illegal to
2879 // throw an exception here. 3005 // throw an exception here.
2880 return Api::NewError("No Dart frames on stack, cannot throw exception"); 3006 return Api::NewError("No Dart frames on stack, cannot throw exception");
2881 } 3007 }
2882 const Instance& excp = Instance::CheckedHandle(Api::UnwrapHandle(exception)); 3008 const Instance& excp =
3009 Instance::CheckedHandle(isolate, Api::UnwrapHandle(exception));
2883 // Unwind all the API scopes till the exit frame before throwing an 3010 // Unwind all the API scopes till the exit frame before throwing an
2884 // exception. 3011 // exception.
2885 ApiState* state = isolate->api_state(); 3012 ApiState* state = isolate->api_state();
2886 ASSERT(state != NULL); 3013 ASSERT(state != NULL);
2887 state->UnwindScopes(isolate->top_exit_frame_info()); 3014 state->UnwindScopes(isolate->top_exit_frame_info());
2888 Exceptions::Throw(excp); 3015 Exceptions::Throw(excp);
2889 return Api::NewError("Exception was not thrown, internal error"); 3016 return Api::NewError("Exception was not thrown, internal error");
2890 } 3017 }
2891 3018
2892 3019
2893 DART_EXPORT Dart_Handle Dart_ReThrowException(Dart_Handle exception, 3020 DART_EXPORT Dart_Handle Dart_ReThrowException(Dart_Handle exception,
2894 Dart_Handle stacktrace) { 3021 Dart_Handle stacktrace) {
2895 Isolate* isolate = Isolate::Current(); 3022 Isolate* isolate = Isolate::Current();
2896 CHECK_ISOLATE(isolate); 3023 CHECK_ISOLATE(isolate);
2897 if (isolate->top_exit_frame_info() == 0) { 3024 if (isolate->top_exit_frame_info() == 0) {
2898 // There are no dart frames on the stack so it would be illegal to 3025 // There are no dart frames on the stack so it would be illegal to
2899 // throw an exception here. 3026 // throw an exception here.
2900 return Api::NewError("No Dart frames on stack, cannot throw exception"); 3027 return Api::NewError("No Dart frames on stack, cannot throw exception");
2901 } 3028 }
2902 DARTSCOPE(isolate); 3029 DARTSCOPE(isolate);
2903 const Instance& excp = Instance::CheckedHandle(Api::UnwrapHandle(exception)); 3030 const Instance& excp =
2904 const Instance& stk = Instance::CheckedHandle(Api::UnwrapHandle(stacktrace)); 3031 Instance::CheckedHandle(isolate, Api::UnwrapHandle(exception));
3032 const Instance& stk =
3033 Instance::CheckedHandle(isolate, Api::UnwrapHandle(stacktrace));
2905 // Unwind all the API scopes till the exit frame before throwing an 3034 // Unwind all the API scopes till the exit frame before throwing an
2906 // exception. 3035 // exception.
2907 ApiState* state = isolate->api_state(); 3036 ApiState* state = isolate->api_state();
2908 ASSERT(state != NULL); 3037 ASSERT(state != NULL);
2909 state->UnwindScopes(isolate->top_exit_frame_info()); 3038 state->UnwindScopes(isolate->top_exit_frame_info());
2910 Exceptions::ReThrow(excp, stk); 3039 Exceptions::ReThrow(excp, stk);
2911 return Api::NewError("Exception was not re thrown, internal error"); 3040 return Api::NewError("Exception was not re thrown, internal error");
2912 } 3041 }
2913 3042
2914 3043
2915 // --- Native functions --- 3044 // --- Native functions ---
2916 3045
2917 3046
2918 DART_EXPORT Dart_Handle Dart_GetNativeArgument(Dart_NativeArguments args, 3047 DART_EXPORT Dart_Handle Dart_GetNativeArgument(Dart_NativeArguments args,
2919 int index) { 3048 int index) {
2920 DARTSCOPE(Isolate::Current());
2921 NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args); 3049 NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args);
2922 const Object& obj = Object::Handle(arguments->At(index)); 3050 Isolate* isolate = arguments->isolate();
2923 return Api::NewLocalHandle(obj); 3051 DARTSCOPE(isolate);
3052 const Object& obj = Object::Handle(isolate, arguments->At(index));
3053 return Api::NewLocalHandle(isolate, obj);
2924 } 3054 }
2925 3055
2926 3056
2927 DART_EXPORT int Dart_GetNativeArgumentCount(Dart_NativeArguments args) { 3057 DART_EXPORT int Dart_GetNativeArgumentCount(Dart_NativeArguments args) {
2928 CHECK_ISOLATE(Isolate::Current());
2929 NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args); 3058 NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args);
3059 CHECK_ISOLATE(arguments->isolate());
2930 return arguments->Count(); 3060 return arguments->Count();
2931 } 3061 }
2932 3062
2933 3063
2934 DART_EXPORT void Dart_SetReturnValue(Dart_NativeArguments args, 3064 DART_EXPORT void Dart_SetReturnValue(Dart_NativeArguments args,
2935 Dart_Handle retval) { 3065 Dart_Handle retval) {
2936 DARTSCOPE(Isolate::Current());
2937 NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args); 3066 NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args);
2938 arguments->SetReturn(Object::Handle(Api::UnwrapHandle(retval))); 3067 Isolate* isolate = arguments->isolate();
3068 DARTSCOPE(isolate);
3069 arguments->SetReturn(Object::Handle(isolate, Api::UnwrapHandle(retval)));
2939 } 3070 }
2940 3071
2941 3072
2942 // --- Scripts and Libraries --- 3073 // --- Scripts and Libraries ---
2943 3074
2944 3075
2945 // NOTE: Need to pass 'result' as a parameter here in order to avoid 3076 // NOTE: Need to pass 'result' as a parameter here in order to avoid
2946 // warning: variable 'result' might be clobbered by 'longjmp' or 'vfork' 3077 // warning: variable 'result' might be clobbered by 'longjmp' or 'vfork'
2947 // which shows up because of the use of setjmp. 3078 // which shows up because of the use of setjmp.
2948 static void CompileSource(Isolate* isolate, 3079 static void CompileSource(Isolate* isolate,
2949 const Library& lib, 3080 const Library& lib,
2950 const String& url, 3081 const String& url,
2951 const String& source, 3082 const String& source,
2952 RawScript::Kind kind, 3083 RawScript::Kind kind,
2953 Dart_Handle* result) { 3084 Dart_Handle* result) {
2954 bool update_lib_status = (kind == RawScript::kScript || 3085 bool update_lib_status = (kind == RawScript::kScript ||
2955 kind == RawScript::kLibrary); 3086 kind == RawScript::kLibrary);
2956 if (update_lib_status) { 3087 if (update_lib_status) {
2957 lib.SetLoadInProgress(); 3088 lib.SetLoadInProgress();
2958 } 3089 }
2959 const Script& script = Script::Handle(Script::New(url, source, kind)); 3090 const Script& script =
3091 Script::Handle(isolate, Script::New(url, source, kind));
2960 ASSERT(isolate != NULL); 3092 ASSERT(isolate != NULL);
2961 const Error& error = Error::Handle(Compiler::Compile(lib, script)); 3093 const Error& error = Error::Handle(isolate, Compiler::Compile(lib, script));
2962 if (error.IsNull()) { 3094 if (error.IsNull()) {
2963 *result = Api::NewLocalHandle(lib); 3095 *result = Api::NewLocalHandle(isolate, lib);
2964 if (update_lib_status) { 3096 if (update_lib_status) {
2965 lib.SetLoaded(); 3097 lib.SetLoaded();
2966 } 3098 }
2967 } else { 3099 } else {
2968 *result = Api::NewLocalHandle(error); 3100 *result = Api::NewLocalHandle(isolate, error);
2969 if (update_lib_status) { 3101 if (update_lib_status) {
2970 lib.SetLoadError(); 3102 lib.SetLoadError();
2971 } 3103 }
2972 } 3104 }
2973 } 3105 }
2974 3106
2975 3107
2976 DART_EXPORT Dart_Handle Dart_LoadScript(Dart_Handle url, 3108 DART_EXPORT Dart_Handle Dart_LoadScript(Dart_Handle url,
2977 Dart_Handle source, 3109 Dart_Handle source,
2978 Dart_LibraryTagHandler handler, 3110 Dart_LibraryTagHandler handler,
2979 Dart_Handle import_map) { 3111 Dart_Handle import_map) {
2980 TIMERSCOPE(time_script_loading); 3112 TIMERSCOPE(time_script_loading);
2981 Isolate* isolate = Isolate::Current(); 3113 Isolate* isolate = Isolate::Current();
2982 DARTSCOPE(isolate); 3114 DARTSCOPE(isolate);
2983 const String& url_str = Api::UnwrapStringHandle(url); 3115 const String& url_str = Api::UnwrapStringHandle(isolate, url);
2984 if (url_str.IsNull()) { 3116 if (url_str.IsNull()) {
2985 RETURN_TYPE_ERROR(url, String); 3117 RETURN_TYPE_ERROR(isolate, url, String);
2986 } 3118 }
2987 const String& source_str = Api::UnwrapStringHandle(source); 3119 const String& source_str = Api::UnwrapStringHandle(isolate, source);
2988 if (source_str.IsNull()) { 3120 if (source_str.IsNull()) {
2989 RETURN_TYPE_ERROR(source, String); 3121 RETURN_TYPE_ERROR(isolate, source, String);
2990 } 3122 }
2991 const Array& mapping_array = Api::UnwrapArrayHandle(import_map); 3123 const Array& mapping_array = Api::UnwrapArrayHandle(isolate, import_map);
2992 Library& library = Library::Handle(isolate->object_store()->root_library()); 3124 Library& library =
3125 Library::Handle(isolate, isolate->object_store()->root_library());
2993 if (!library.IsNull()) { 3126 if (!library.IsNull()) {
2994 const String& library_url = String::Handle(library.url()); 3127 const String& library_url = String::Handle(isolate, library.url());
2995 return Api::NewError("%s: A script has already been loaded from '%s'.", 3128 return Api::NewError("%s: A script has already been loaded from '%s'.",
2996 CURRENT_FUNC, library_url.ToCString()); 3129 CURRENT_FUNC, library_url.ToCString());
2997 } 3130 }
2998 isolate->set_library_tag_handler(handler); 3131 isolate->set_library_tag_handler(handler);
2999 library = Library::New(url_str); 3132 library = Library::New(url_str);
3000 if (mapping_array.IsNull()) { 3133 if (mapping_array.IsNull()) {
3001 library.set_import_map(Array::Handle(Array::Empty())); 3134 library.set_import_map(Array::Handle(isolate, Array::Empty()));
3002 } else { 3135 } else {
3003 library.set_import_map(mapping_array); 3136 library.set_import_map(mapping_array);
3004 } 3137 }
3005 library.Register(); 3138 library.Register();
3006 isolate->object_store()->set_root_library(library); 3139 isolate->object_store()->set_root_library(library);
3007 Dart_Handle result; 3140 Dart_Handle result;
3008 CompileSource(isolate, 3141 CompileSource(isolate,
3009 library, 3142 library,
3010 url_str, 3143 url_str,
3011 source_str, 3144 source_str,
3012 RawScript::kScript, 3145 RawScript::kScript,
3013 &result); 3146 &result);
3014 return result; 3147 return result;
3015 } 3148 }
3016 3149
3017 3150
3018 DART_EXPORT Dart_Handle Dart_LoadScriptFromSnapshot(const uint8_t* buffer) { 3151 DART_EXPORT Dart_Handle Dart_LoadScriptFromSnapshot(const uint8_t* buffer) {
3019 Isolate* isolate = Isolate::Current(); 3152 Isolate* isolate = Isolate::Current();
3020 DARTSCOPE(isolate); 3153 DARTSCOPE(isolate);
3021 TIMERSCOPE(time_script_loading); 3154 TIMERSCOPE(time_script_loading);
3022 if (buffer == NULL) { 3155 if (buffer == NULL) {
3023 return Api::NewError("%s expects argument 'buffer' to be non-null.", 3156 return Api::NewError("%s expects argument 'buffer' to be non-null.",
3024 CURRENT_FUNC); 3157 CURRENT_FUNC);
3025 } 3158 }
3026 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); 3159 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer);
3027 if (!snapshot->IsScriptSnapshot()) { 3160 if (!snapshot->IsScriptSnapshot()) {
3028 return Api::NewError("%s expects parameter 'buffer' to be a script type" 3161 return Api::NewError("%s expects parameter 'buffer' to be a script type"
3029 " snapshot", CURRENT_FUNC); 3162 " snapshot", CURRENT_FUNC);
3030 } 3163 }
3031 Library& library = Library::Handle(isolate->object_store()->root_library()); 3164 Library& library =
3165 Library::Handle(isolate, isolate->object_store()->root_library());
3032 if (!library.IsNull()) { 3166 if (!library.IsNull()) {
3033 const String& library_url = String::Handle(library.url()); 3167 const String& library_url = String::Handle(isolate, library.url());
3034 return Api::NewError("%s: A script has already been loaded from '%s'.", 3168 return Api::NewError("%s: A script has already been loaded from '%s'.",
3035 CURRENT_FUNC, library_url.ToCString()); 3169 CURRENT_FUNC, library_url.ToCString());
3036 } 3170 }
3037 SnapshotReader reader(snapshot, isolate); 3171 SnapshotReader reader(snapshot, isolate);
3038 const Object& tmp = Object::Handle(reader.ReadObject()); 3172 const Object& tmp = Object::Handle(isolate, reader.ReadObject());
3039 if (!tmp.IsLibrary()) { 3173 if (!tmp.IsLibrary()) {
3040 return Api::NewError("%s: Unable to deserialize snapshot correctly.", 3174 return Api::NewError("%s: Unable to deserialize snapshot correctly.",
3041 CURRENT_FUNC); 3175 CURRENT_FUNC);
3042 } 3176 }
3043 library ^= tmp.raw(); 3177 library ^= tmp.raw();
3044 isolate->object_store()->set_root_library(library); 3178 isolate->object_store()->set_root_library(library);
3045 return Api::NewLocalHandle(library); 3179 return Api::NewLocalHandle(isolate, library);
3046 } 3180 }
3047 3181
3048 3182
3049 static void CompileAll(Isolate* isolate, Dart_Handle* result) { 3183 static void CompileAll(Isolate* isolate, Dart_Handle* result) {
3050 ASSERT(isolate != NULL); 3184 ASSERT(isolate != NULL);
3051 const Error& error = Error::Handle(Library::CompileAll()); 3185 const Error& error = Error::Handle(isolate, Library::CompileAll());
3052 if (error.IsNull()) { 3186 if (error.IsNull()) {
3053 *result = Api::Success(); 3187 *result = Api::Success(isolate);
3054 } else { 3188 } else {
3055 *result = Api::NewLocalHandle(error); 3189 *result = Api::NewLocalHandle(isolate, error);
3056 } 3190 }
3057 } 3191 }
3058 3192
3059 3193
3060 DART_EXPORT Dart_Handle Dart_CompileAll() { 3194 DART_EXPORT Dart_Handle Dart_CompileAll() {
3061 Isolate* isolate = Isolate::Current(); 3195 Isolate* isolate = Isolate::Current();
3062 DARTSCOPE(isolate); 3196 DARTSCOPE(isolate);
3063 Dart_Handle result; 3197 Dart_Handle result;
3064 const char* msg = CheckIsolateState(isolate); 3198 const char* msg = CheckIsolateState(isolate);
3065 if (msg != NULL) { 3199 if (msg != NULL) {
3066 return Api::NewError(msg); 3200 return Api::NewError(msg);
3067 } 3201 }
3068 CompileAll(isolate, &result); 3202 CompileAll(isolate, &result);
3069 return result; 3203 return result;
3070 } 3204 }
3071 3205
3072 3206
3073 DART_EXPORT bool Dart_IsLibrary(Dart_Handle object) { 3207 DART_EXPORT bool Dart_IsLibrary(Dart_Handle object) {
3074 DARTSCOPE(Isolate::Current()); 3208 Isolate* isolate = Isolate::Current();
3075 const Object& obj = Object::Handle(Api::UnwrapHandle(object)); 3209 DARTSCOPE(isolate);
3210 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(object));
3076 return obj.IsLibrary(); 3211 return obj.IsLibrary();
3077 } 3212 }
3078 3213
3079 3214
3080 DART_EXPORT Dart_Handle Dart_GetClass(Dart_Handle library, Dart_Handle name) { 3215 DART_EXPORT Dart_Handle Dart_GetClass(Dart_Handle library, Dart_Handle name) {
3081 DARTSCOPE(Isolate::Current()); 3216 Isolate* isolate = Isolate::Current();
3082 const Object& param = Object::Handle(Api::UnwrapHandle(name)); 3217 DARTSCOPE(isolate);
3218 const Object& param = Object::Handle(isolate, Api::UnwrapHandle(name));
3083 if (param.IsNull() || !param.IsString()) { 3219 if (param.IsNull() || !param.IsString()) {
3084 return Api::NewError("Invalid class name specified"); 3220 return Api::NewError("Invalid class name specified");
3085 } 3221 }
3086 const Library& lib = Library::CheckedHandle(Api::UnwrapHandle(library)); 3222 const Library& lib =
3223 Library::CheckedHandle(isolate, Api::UnwrapHandle(library));
3087 if (lib.IsNull()) { 3224 if (lib.IsNull()) {
3088 return Api::NewError("Invalid parameter, Unknown library specified"); 3225 return Api::NewError("Invalid parameter, Unknown library specified");
3089 } 3226 }
3090 String& cls_name = String::Handle(); 3227 String& cls_name = String::Handle(isolate);
3091 cls_name ^= param.raw(); 3228 cls_name ^= param.raw();
3092 const Class& cls = Class::Handle(lib.LookupClass(cls_name)); 3229 const Class& cls = Class::Handle(isolate, lib.LookupClass(cls_name));
3093 if (cls.IsNull()) { 3230 if (cls.IsNull()) {
3094 const String& lib_name = String::Handle(lib.name()); 3231 const String& lib_name = String::Handle(isolate, lib.name());
3095 return Api::NewError("Class '%s' not found in library '%s'.", 3232 return Api::NewError("Class '%s' not found in library '%s'.",
3096 cls_name.ToCString(), lib_name.ToCString()); 3233 cls_name.ToCString(), lib_name.ToCString());
3097 } 3234 }
3098 return Api::NewLocalHandle(cls); 3235 return Api::NewLocalHandle(isolate, cls);
3099 } 3236 }
3100 3237
3101 3238
3102 DART_EXPORT Dart_Handle Dart_LibraryUrl(Dart_Handle library) { 3239 DART_EXPORT Dart_Handle Dart_LibraryUrl(Dart_Handle library) {
3103 DARTSCOPE(Isolate::Current()); 3240 Isolate* isolate = Isolate::Current();
3104 const Library& lib = Api::UnwrapLibraryHandle(library); 3241 DARTSCOPE(isolate);
3242 const Library& lib = Api::UnwrapLibraryHandle(isolate, library);
3105 if (lib.IsNull()) { 3243 if (lib.IsNull()) {
3106 RETURN_TYPE_ERROR(library, Library); 3244 RETURN_TYPE_ERROR(isolate, library, Library);
3107 } 3245 }
3108 const String& url = String::Handle(lib.url()); 3246 const String& url = String::Handle(isolate, lib.url());
3109 ASSERT(!url.IsNull()); 3247 ASSERT(!url.IsNull());
3110 return Api::NewLocalHandle(url); 3248 return Api::NewLocalHandle(isolate, url);
3111 } 3249 }
3112 3250
3113 3251
3114 DART_EXPORT Dart_Handle Dart_LookupLibrary(Dart_Handle url) { 3252 DART_EXPORT Dart_Handle Dart_LookupLibrary(Dart_Handle url) {
3115 DARTSCOPE(Isolate::Current()); 3253 Isolate* isolate = Isolate::Current();
3116 const String& url_str = Api::UnwrapStringHandle(url); 3254 DARTSCOPE(isolate);
3255 const String& url_str = Api::UnwrapStringHandle(isolate, url);
3117 if (url_str.IsNull()) { 3256 if (url_str.IsNull()) {
3118 RETURN_TYPE_ERROR(url, String); 3257 RETURN_TYPE_ERROR(isolate, url, String);
3119 } 3258 }
3120 const Library& library = Library::Handle(Library::LookupLibrary(url_str)); 3259 const Library& library =
3260 Library::Handle(isolate, Library::LookupLibrary(url_str));
3121 if (library.IsNull()) { 3261 if (library.IsNull()) {
3122 return Api::NewError("%s: library '%s' not found.", 3262 return Api::NewError("%s: library '%s' not found.",
3123 CURRENT_FUNC, url_str.ToCString()); 3263 CURRENT_FUNC, url_str.ToCString());
3124 } else { 3264 } else {
3125 return Api::NewLocalHandle(library); 3265 return Api::NewLocalHandle(isolate, library);
3126 } 3266 }
3127 } 3267 }
3128 3268
3129 3269
3130 DART_EXPORT Dart_Handle Dart_LoadLibrary(Dart_Handle url, 3270 DART_EXPORT Dart_Handle Dart_LoadLibrary(Dart_Handle url,
3131 Dart_Handle source, 3271 Dart_Handle source,
3132 Dart_Handle import_map) { 3272 Dart_Handle import_map) {
3133 TIMERSCOPE(time_script_loading); 3273 TIMERSCOPE(time_script_loading);
3134 Isolate* isolate = Isolate::Current(); 3274 Isolate* isolate = Isolate::Current();
3135 DARTSCOPE(isolate); 3275 DARTSCOPE(isolate);
3136 const String& url_str = Api::UnwrapStringHandle(url); 3276 const String& url_str = Api::UnwrapStringHandle(isolate, url);
3137 if (url_str.IsNull()) { 3277 if (url_str.IsNull()) {
3138 RETURN_TYPE_ERROR(url, String); 3278 RETURN_TYPE_ERROR(isolate, url, String);
3139 } 3279 }
3140 const String& source_str = Api::UnwrapStringHandle(source); 3280 const String& source_str = Api::UnwrapStringHandle(isolate, source);
3141 if (source_str.IsNull()) { 3281 if (source_str.IsNull()) {
3142 RETURN_TYPE_ERROR(source, String); 3282 RETURN_TYPE_ERROR(isolate, source, String);
3143 } 3283 }
3144 const Array& mapping_array = Api::UnwrapArrayHandle(import_map); 3284 const Array& mapping_array = Api::UnwrapArrayHandle(isolate, import_map);
3145 Library& library = Library::Handle(Library::LookupLibrary(url_str)); 3285 Library& library = Library::Handle(isolate, Library::LookupLibrary(url_str));
3146 if (library.IsNull()) { 3286 if (library.IsNull()) {
3147 library = Library::New(url_str); 3287 library = Library::New(url_str);
3148 if (mapping_array.IsNull()) { 3288 if (mapping_array.IsNull()) {
3149 library.set_import_map(Array::Handle(Array::Empty())); 3289 library.set_import_map(Array::Handle(isolate, Array::Empty()));
3150 } else { 3290 } else {
3151 library.set_import_map(mapping_array); 3291 library.set_import_map(mapping_array);
3152 } 3292 }
3153 library.Register(); 3293 library.Register();
3154 } else if (!library.LoadNotStarted()) { 3294 } else if (!library.LoadNotStarted()) {
3155 // The source for this library has either been loaded or is in the 3295 // The source for this library has either been loaded or is in the
3156 // process of loading. Return an error. 3296 // process of loading. Return an error.
3157 return Api::NewError("%s: library '%s' has already been loaded.", 3297 return Api::NewError("%s: library '%s' has already been loaded.",
3158 CURRENT_FUNC, url_str.ToCString()); 3298 CURRENT_FUNC, url_str.ToCString());
3159 } 3299 }
(...skipping 16 matching lines...) Expand all
3176 if (msg != NULL) { 3316 if (msg != NULL) {
3177 return Api::NewError(msg); 3317 return Api::NewError(msg);
3178 } 3318 }
3179 } 3319 }
3180 return result; 3320 return result;
3181 } 3321 }
3182 3322
3183 3323
3184 DART_EXPORT Dart_Handle Dart_LibraryImportLibrary(Dart_Handle library, 3324 DART_EXPORT Dart_Handle Dart_LibraryImportLibrary(Dart_Handle library,
3185 Dart_Handle import) { 3325 Dart_Handle import) {
3186 DARTSCOPE(Isolate::Current()); 3326 Isolate* isolate = Isolate::Current();
3187 const Library& library_vm = Api::UnwrapLibraryHandle(library); 3327 DARTSCOPE(isolate);
3328 const Library& library_vm = Api::UnwrapLibraryHandle(isolate, library);
3188 if (library_vm.IsNull()) { 3329 if (library_vm.IsNull()) {
3189 RETURN_TYPE_ERROR(library, Library); 3330 RETURN_TYPE_ERROR(isolate, library, Library);
3190 } 3331 }
3191 const Library& import_vm = Api::UnwrapLibraryHandle(import); 3332 const Library& import_vm = Api::UnwrapLibraryHandle(isolate, import);
3192 if (import_vm.IsNull()) { 3333 if (import_vm.IsNull()) {
3193 RETURN_TYPE_ERROR(import, Library); 3334 RETURN_TYPE_ERROR(isolate, import, Library);
3194 } 3335 }
3195 library_vm.AddImport(import_vm); 3336 library_vm.AddImport(import_vm);
3196 return Api::Success(); 3337 return Api::Success(isolate);
3197 } 3338 }
3198 3339
3199 3340
3200 DART_EXPORT Dart_Handle Dart_LoadSource(Dart_Handle library, 3341 DART_EXPORT Dart_Handle Dart_LoadSource(Dart_Handle library,
3201 Dart_Handle url, 3342 Dart_Handle url,
3202 Dart_Handle source) { 3343 Dart_Handle source) {
3203 TIMERSCOPE(time_script_loading); 3344 TIMERSCOPE(time_script_loading);
3204 Isolate* isolate = Isolate::Current(); 3345 Isolate* isolate = Isolate::Current();
3205 DARTSCOPE(isolate); 3346 DARTSCOPE(isolate);
3206 const Library& lib = Api::UnwrapLibraryHandle(library); 3347 const Library& lib = Api::UnwrapLibraryHandle(isolate, library);
3207 if (lib.IsNull()) { 3348 if (lib.IsNull()) {
3208 RETURN_TYPE_ERROR(library, Library); 3349 RETURN_TYPE_ERROR(isolate, library, Library);
3209 } 3350 }
3210 const String& url_str = Api::UnwrapStringHandle(url); 3351 const String& url_str = Api::UnwrapStringHandle(isolate, url);
3211 if (url_str.IsNull()) { 3352 if (url_str.IsNull()) {
3212 RETURN_TYPE_ERROR(url, String); 3353 RETURN_TYPE_ERROR(isolate, url, String);
3213 } 3354 }
3214 const String& source_str = Api::UnwrapStringHandle(source); 3355 const String& source_str = Api::UnwrapStringHandle(isolate, source);
3215 if (source_str.IsNull()) { 3356 if (source_str.IsNull()) {
3216 RETURN_TYPE_ERROR(source, String); 3357 RETURN_TYPE_ERROR(isolate, source, String);
3217 } 3358 }
3218 Dart_Handle result; 3359 Dart_Handle result;
3219 CompileSource(isolate, lib, url_str, source_str, RawScript::kSource, &result); 3360 CompileSource(isolate, lib, url_str, source_str, RawScript::kSource, &result);
3220 return result; 3361 return result;
3221 } 3362 }
3222 3363
3223 3364
3224 DART_EXPORT Dart_Handle Dart_SetNativeResolver( 3365 DART_EXPORT Dart_Handle Dart_SetNativeResolver(
3225 Dart_Handle library, 3366 Dart_Handle library,
3226 Dart_NativeEntryResolver resolver) { 3367 Dart_NativeEntryResolver resolver) {
3227 DARTSCOPE(Isolate::Current()); 3368 Isolate* isolate = Isolate::Current();
3228 const Library& lib = Api::UnwrapLibraryHandle(library); 3369 DARTSCOPE(isolate);
3370 const Library& lib = Api::UnwrapLibraryHandle(isolate, library);
3229 if (lib.IsNull()) { 3371 if (lib.IsNull()) {
3230 RETURN_TYPE_ERROR(library, Library); 3372 RETURN_TYPE_ERROR(isolate, library, Library);
3231 } 3373 }
3232 lib.set_native_entry_resolver(resolver); 3374 lib.set_native_entry_resolver(resolver);
3233 return Api::Success(); 3375 return Api::Success(isolate);
3234 } 3376 }
3235 3377
3236 3378
3237 // --- Profiling support ---- 3379 // --- Profiling support ----
3238 3380
3239 3381
3240 DART_EXPORT void Dart_InitPprofSupport() { 3382 DART_EXPORT void Dart_InitPprofSupport() {
3241 DebugInfo* pprof_symbol_generator = DebugInfo::NewGenerator(); 3383 DebugInfo* pprof_symbol_generator = DebugInfo::NewGenerator();
3242 ASSERT(pprof_symbol_generator != NULL); 3384 ASSERT(pprof_symbol_generator != NULL);
3243 Dart::set_pprof_symbol_generator(pprof_symbol_generator); 3385 Dart::set_pprof_symbol_generator(pprof_symbol_generator);
3244 } 3386 }
3245 3387
3246 3388
3247 DART_EXPORT void Dart_GetPprofSymbolInfo(void** buffer, int* buffer_size) { 3389 DART_EXPORT void Dart_GetPprofSymbolInfo(void** buffer, int* buffer_size) {
3390 Isolate* isolate = Isolate::Current();
3248 DebugInfo* pprof_symbol_generator = Dart::pprof_symbol_generator(); 3391 DebugInfo* pprof_symbol_generator = Dart::pprof_symbol_generator();
3249 if (pprof_symbol_generator != NULL) { 3392 if (pprof_symbol_generator != NULL) {
3250 DebugInfo::ByteBuffer* debug_region = new DebugInfo::ByteBuffer(); 3393 DebugInfo::ByteBuffer* debug_region = new DebugInfo::ByteBuffer();
3251 ASSERT(debug_region != NULL); 3394 ASSERT(debug_region != NULL);
3252 pprof_symbol_generator->WriteToMemory(debug_region); 3395 pprof_symbol_generator->WriteToMemory(debug_region);
3253 *buffer_size = debug_region->size(); 3396 *buffer_size = debug_region->size();
3254 if (*buffer_size != 0) { 3397 if (*buffer_size != 0) {
3255 *buffer = reinterpret_cast<void*>(Api::Allocate(*buffer_size)); 3398 *buffer = reinterpret_cast<void*>(Api::Allocate(isolate, *buffer_size));
3256 memmove(*buffer, debug_region->data(), *buffer_size); 3399 memmove(*buffer, debug_region->data(), *buffer_size);
3257 } else { 3400 } else {
3258 *buffer = NULL; 3401 *buffer = NULL;
3259 } 3402 }
3260 delete debug_region; 3403 delete debug_region;
3261 } else { 3404 } else {
3262 *buffer = NULL; 3405 *buffer = NULL;
3263 *buffer_size = 0; 3406 *buffer_size = 0;
3264 } 3407 }
3265 } 3408 }
3266 3409
3267 } // namespace dart 3410 } // namespace dart
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698