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

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

Issue 8636021: Reorder the functions in dart_api_impl.cc to match dart_api.h. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 9 years 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
« no previous file with comments | « runtime/include/dart_api.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, 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 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
45 CURRENT_FUNC, #dart_handle); \ 45 CURRENT_FUNC, #dart_handle); \
46 } else if (tmp.IsApiError()) { \ 46 } else if (tmp.IsApiError()) { \
47 return dart_handle; \ 47 return dart_handle; \
48 } else { \ 48 } else { \
49 return Api::Error("%s expects argument '%s' to be of type %s.", \ 49 return Api::Error("%s expects argument '%s' to be of type %s.", \
50 CURRENT_FUNC, #dart_handle, #Type); \ 50 CURRENT_FUNC, #dart_handle, #Type); \
51 } \ 51 } \
52 } while (0) 52 } while (0)
53 53
54 54
55 // Return error if isolate is in an inconsistent state.
56 // Return NULL when no error condition exists.
57 static const char* CheckIsolateState(
58 Isolate* isolate,
59 bool generating_snapshot = ClassFinalizer::kNotGeneratingSnapshot) {
60 bool result = (generating_snapshot) ?
61 ClassFinalizer::FinalizePendingClassesForSnapshotCreation() :
62 ClassFinalizer::FinalizePendingClasses();
63 if (!result) {
64 // Make a copy of the error message as the original message string
65 // may get deallocated when we return back from the Dart API call.
66 const String& err =
67 String::Handle(isolate->object_store()->sticky_error());
68 const char* errmsg = err.ToCString();
69 intptr_t errlen = strlen(errmsg) + 1;
70 char* msg = reinterpret_cast<char*>(Api::Allocate(errlen));
71 OS::SNPrint(msg, errlen, "%s", errmsg);
72 return msg;
73 }
74 return NULL;
75 }
76
77
78 static void SetupErrorResult(Dart_Handle* handle) {
79 // Make a copy of the error message as the original message string
80 // may get deallocated when we return back from the Dart API call.
81 const String& error = String::Handle(
82 Isolate::Current()->object_store()->sticky_error());
83 const Object& obj = Object::Handle(ApiError::New(error));
84 *handle = Api::NewLocalHandle(obj);
85 }
86
87
88 Dart_Handle Api::NewLocalHandle(const Object& object) {
89 Isolate* isolate = Isolate::Current();
90 ASSERT(isolate != NULL);
91 ApiState* state = isolate->api_state();
92 ASSERT(state != NULL);
93 ApiLocalScope* scope = state->top_scope();
94 ASSERT(scope != NULL);
95 LocalHandles* local_handles = scope->local_handles();
96 ASSERT(local_handles != NULL);
97 LocalHandle* ref = local_handles->AllocateHandle();
98 ref->set_raw(object);
99 return reinterpret_cast<Dart_Handle>(ref);
100 }
101
102 RawObject* Api::UnwrapHandle(Dart_Handle object) {
103 #ifdef DEBUG
104 Isolate* isolate = Isolate::Current();
105 ASSERT(isolate != NULL);
106 ApiState* state = isolate->api_state();
107 ASSERT(state != NULL);
108 ASSERT(state->IsValidPersistentHandle(object) ||
109 state->IsValidLocalHandle(object));
110 ASSERT(PersistentHandle::raw_offset() == 0 &&
111 LocalHandle::raw_offset() == 0);
112 #endif
113 return *(reinterpret_cast<RawObject**>(object));
114 }
115
116 #define DEFINE_UNWRAP(Type) \
117 const Type& Api::Unwrap##Type##Handle(Dart_Handle dart_handle) { \
118 const Object& tmp = Object::Handle(Api::UnwrapHandle(dart_handle)); \
119 Type& typed_handle = Type::Handle(); \
120 if (tmp.Is##Type()) { \
121 typed_handle ^= tmp.raw(); \
122 } \
123 return typed_handle; \
124 }
125 CLASS_LIST_NO_OBJECT(DEFINE_UNWRAP)
126 #undef DEFINE_UNWRAP
127
128
129 LocalHandle* Api::UnwrapAsLocalHandle(const ApiState& state,
130 Dart_Handle object) {
131 ASSERT(state.IsValidLocalHandle(object));
132 return reinterpret_cast<LocalHandle*>(object);
133 }
134
135
136 PersistentHandle* Api::UnwrapAsPersistentHandle(const ApiState& state,
137 Dart_Handle object) {
138 ASSERT(state.IsValidPersistentHandle(object));
139 return reinterpret_cast<PersistentHandle*>(object);
140 }
141
142
143 Dart_Handle Api::Success() {
144 Isolate* isolate = Isolate::Current();
145 ASSERT(isolate != NULL);
146 ApiState* state = isolate->api_state();
147 ASSERT(state != NULL);
148 PersistentHandle* true_handle = state->True();
149 return reinterpret_cast<Dart_Handle>(true_handle);
150 }
151
152
153 Dart_Handle Api::Error(const char* format, ...) {
154 DARTSCOPE(Isolate::Current());
155
156 va_list args;
157 va_start(args, format);
158 intptr_t len = OS::VSNPrint(NULL, 0, format, args);
159 va_end(args);
160
161 char* buffer = reinterpret_cast<char*>(zone.Allocate(len + 1));
162 va_list args2;
163 va_start(args2, format);
164 OS::VSNPrint(buffer, (len + 1), format, args2);
165 va_end(args2);
166
167 const String& message = String::Handle(String::New(buffer));
168 const Object& obj = Object::Handle(ApiError::New(message));
169 return Api::NewLocalHandle(obj);
170 }
171
172
173 Dart_Handle Api::ErrorFromException(const Object& obj) {
174 DARTSCOPE(Isolate::Current());
175
176 ASSERT(obj.IsUnhandledException());
177 if (obj.IsUnhandledException()) {
178 UnhandledException& uhe = UnhandledException::Handle();
179 uhe ^= obj.raw();
180 const Object& error = Object::Handle(ApiError::New(uhe));
181 return Api::NewLocalHandle(error);
182 } else {
183 return Api::Error("Internal error: expected obj.IsUnhandledException().");
184 }
185 }
186
187
188 Dart_Handle Api::Null() {
189 Isolate* isolate = Isolate::Current();
190 ASSERT(isolate != NULL);
191 ApiState* state = isolate->api_state();
192 ASSERT(state != NULL);
193 PersistentHandle* null_handle = state->Null();
194 return reinterpret_cast<Dart_Handle>(null_handle);
195 }
196
197
198 Dart_Handle Api::True() {
199 Isolate* isolate = Isolate::Current();
200 ASSERT(isolate != NULL);
201 ApiState* state = isolate->api_state();
202 ASSERT(state != NULL);
203 PersistentHandle* true_handle = state->True();
204 return reinterpret_cast<Dart_Handle>(true_handle);
205 }
206
207
208 Dart_Handle Api::False() {
209 Isolate* isolate = Isolate::Current();
210 ASSERT(isolate != NULL);
211 ApiState* state = isolate->api_state();
212 ASSERT(state != NULL);
213 PersistentHandle* false_handle = state->False();
214 return reinterpret_cast<Dart_Handle>(false_handle);
215 }
216
217
218 uword Api::Allocate(intptr_t size) {
219 Isolate* isolate = Isolate::Current();
220 ASSERT(isolate != NULL);
221 ApiState* state = isolate->api_state();
222 ASSERT(state != NULL);
223 ApiLocalScope* scope = state->top_scope();
224 ASSERT(scope != NULL);
225 return scope->zone().Allocate(size);
226 }
227
228
229 uword Api::Reallocate(uword ptr, intptr_t old_size, intptr_t new_size) {
230 Isolate* isolate = Isolate::Current();
231 ASSERT(isolate != NULL);
232 ApiState* state = isolate->api_state();
233 ASSERT(state != NULL);
234 ApiLocalScope* scope = state->top_scope();
235 ASSERT(scope != NULL);
236 return scope->zone().Reallocate(ptr, old_size, new_size);
237 }
238
239
240 // --- Handles ---
241
242
55 DART_EXPORT bool Dart_IsError(const Dart_Handle& handle) { 243 DART_EXPORT bool Dart_IsError(const Dart_Handle& handle) {
56 DARTSCOPE(Isolate::Current()); 244 DARTSCOPE(Isolate::Current());
57 const Object& obj = Object::Handle(Api::UnwrapHandle(handle)); 245 const Object& obj = Object::Handle(Api::UnwrapHandle(handle));
58 return obj.IsApiError(); 246 return obj.IsApiError();
59 } 247 }
60 248
61 249
62 DART_EXPORT bool Dart_ErrorHasException(Dart_Handle handle) {
63 DARTSCOPE(Isolate::Current());
64 const Object& obj = Object::Handle(Api::UnwrapHandle(handle));
65 if (obj.IsApiError()) {
66 const ApiError& error = ApiError::CheckedHandle(obj.raw());
67 const Object& data = Object::Handle(error.data());
68 return data.IsUnhandledException();
69 }
70 return false;
71 }
72
73
74 DART_EXPORT Dart_Handle Dart_ErrorGetException(Dart_Handle handle) {
75 DARTSCOPE(Isolate::Current());
76 const Object& obj = Object::Handle(Api::UnwrapHandle(handle));
77 if (obj.IsApiError()) {
78 const ApiError& error = ApiError::CheckedHandle(obj.raw());
79 const Object& data = Object::Handle(error.data());
80 if (data.IsUnhandledException()) {
81 const UnhandledException& unhandled = UnhandledException::Handle(
82 reinterpret_cast<RawUnhandledException*>(data.raw()));
83 const Object& exception = Object::Handle(unhandled.exception());
84 return Api::NewLocalHandle(exception);
85 } else {
86 return Api::Error("This error is not an unhandled exception error.");
87 }
88 } else {
89 return Api::Error("Can only get exceptions from error handles.");
90 }
91 }
92
93
94 DART_EXPORT Dart_Handle Dart_ErrorGetStacktrace(Dart_Handle handle) {
95 DARTSCOPE(Isolate::Current());
96 const Object& obj = Object::Handle(Api::UnwrapHandle(handle));
97 if (obj.IsApiError()) {
98 ApiError& failure = ApiError::Handle();
99 failure ^= obj.raw();
100 const Object& data = Object::Handle(failure.data());
101 if (data.IsUnhandledException()) {
102 const UnhandledException& unhandled = UnhandledException::Handle(
103 reinterpret_cast<RawUnhandledException*>(data.raw()));
104 const Object& stacktrace = Object::Handle(unhandled.stacktrace());
105 return Api::NewLocalHandle(stacktrace);
106 } else {
107 return Api::Error("This error is not an unhandled exception error.");
108 }
109 } else {
110 return Api::Error("Can only get stacktraces from error handles.");
111 }
112 }
113
114
115 DART_EXPORT void _Dart_ReportErrorHandle(const char* file,
116 int line,
117 const char* handle,
118 const char* message) {
119 fprintf(stderr, "%s:%d: error handle: '%s':\n '%s'\n",
120 file, line, handle, message);
121 OS::Abort();
122 }
123
124
125 static const char* MakeUnhandledExceptionCString( 250 static const char* MakeUnhandledExceptionCString(
126 const UnhandledException& uhe) { 251 const UnhandledException& uhe) {
127 const Instance& exception = Instance::Handle(uhe.exception()); 252 const Instance& exception = Instance::Handle(uhe.exception());
128 Object& strtmp = Object::Handle(DartLibraryCalls::ToString(exception)); 253 Object& strtmp = Object::Handle(DartLibraryCalls::ToString(exception));
129 const char* exc_str = 254 const char* exc_str =
130 "<Received exception while converting exception to string>"; 255 "<Received exception while converting exception to string>";
131 if (!strtmp.IsUnhandledException()) { 256 if (!strtmp.IsUnhandledException()) {
132 exc_str = strtmp.ToCString(); 257 exc_str = strtmp.ToCString();
133 } 258 }
134 259
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
174 UnhandledException& uhe = UnhandledException::Handle(); 299 UnhandledException& uhe = UnhandledException::Handle();
175 uhe ^= data.raw(); 300 uhe ^= data.raw();
176 return MakeUnhandledExceptionCString(uhe); 301 return MakeUnhandledExceptionCString(uhe);
177 302
178 } else { 303 } else {
179 return "<Internal error in Dart_GetError: malformed error handle>"; 304 return "<Internal error in Dart_GetError: malformed error handle>";
180 } 305 }
181 } 306 }
182 307
183 308
309 DART_EXPORT bool Dart_ErrorHasException(Dart_Handle handle) {
310 DARTSCOPE(Isolate::Current());
311 const Object& obj = Object::Handle(Api::UnwrapHandle(handle));
312 if (obj.IsApiError()) {
313 const ApiError& error = ApiError::CheckedHandle(obj.raw());
314 const Object& data = Object::Handle(error.data());
315 return data.IsUnhandledException();
316 }
317 return false;
318 }
319
320
321 DART_EXPORT Dart_Handle Dart_ErrorGetException(Dart_Handle handle) {
322 DARTSCOPE(Isolate::Current());
323 const Object& obj = Object::Handle(Api::UnwrapHandle(handle));
324 if (obj.IsApiError()) {
325 const ApiError& error = ApiError::CheckedHandle(obj.raw());
326 const Object& data = Object::Handle(error.data());
327 if (data.IsUnhandledException()) {
328 const UnhandledException& unhandled = UnhandledException::Handle(
329 reinterpret_cast<RawUnhandledException*>(data.raw()));
330 const Object& exception = Object::Handle(unhandled.exception());
331 return Api::NewLocalHandle(exception);
332 } else {
333 return Api::Error("This error is not an unhandled exception error.");
334 }
335 } else {
336 return Api::Error("Can only get exceptions from error handles.");
337 }
338 }
339
340
341 DART_EXPORT Dart_Handle Dart_ErrorGetStacktrace(Dart_Handle handle) {
342 DARTSCOPE(Isolate::Current());
343 const Object& obj = Object::Handle(Api::UnwrapHandle(handle));
344 if (obj.IsApiError()) {
345 ApiError& failure = ApiError::Handle();
346 failure ^= obj.raw();
347 const Object& data = Object::Handle(failure.data());
348 if (data.IsUnhandledException()) {
349 const UnhandledException& unhandled = UnhandledException::Handle(
350 reinterpret_cast<RawUnhandledException*>(data.raw()));
351 const Object& stacktrace = Object::Handle(unhandled.stacktrace());
352 return Api::NewLocalHandle(stacktrace);
353 } else {
354 return Api::Error("This error is not an unhandled exception error.");
355 }
356 } else {
357 return Api::Error("Can only get stacktraces from error handles.");
358 }
359 }
360
361
184 // TODO(turnidge): This clonse Api::Error. I need to use va_copy to 362 // TODO(turnidge): This clonse Api::Error. I need to use va_copy to
185 // fix this but not sure if it available on all of our builds. 363 // fix this but not sure if it available on all of our builds.
186 DART_EXPORT Dart_Handle Dart_Error(const char* format, ...) { 364 DART_EXPORT Dart_Handle Dart_Error(const char* format, ...) {
187 DARTSCOPE(Isolate::Current()); 365 DARTSCOPE(Isolate::Current());
188 366
189 va_list args; 367 va_list args;
190 va_start(args, format); 368 va_start(args, format);
191 intptr_t len = OS::VSNPrint(NULL, 0, format, args); 369 intptr_t len = OS::VSNPrint(NULL, 0, format, args);
192 va_end(args); 370 va_end(args);
193 371
194 char* buffer = reinterpret_cast<char*>(zone.Allocate(len + 1)); 372 char* buffer = reinterpret_cast<char*>(zone.Allocate(len + 1));
195 va_list args2; 373 va_list args2;
196 va_start(args2, format); 374 va_start(args2, format);
197 OS::VSNPrint(buffer, (len + 1), format, args2); 375 OS::VSNPrint(buffer, (len + 1), format, args2);
198 va_end(args2); 376 va_end(args2);
199 377
200 const String& message = String::Handle(String::New(buffer)); 378 const String& message = String::Handle(String::New(buffer));
201 const Object& obj = Object::Handle(ApiError::New(message)); 379 const Object& obj = Object::Handle(ApiError::New(message));
202 return Api::NewLocalHandle(obj); 380 return Api::NewLocalHandle(obj);
203 } 381 }
204 382
205 383
384 DART_EXPORT void _Dart_ReportErrorHandle(const char* file,
385 int line,
386 const char* handle,
387 const char* message) {
388 fprintf(stderr, "%s:%d: error handle: '%s':\n '%s'\n",
389 file, line, handle, message);
390 OS::Abort();
391 }
392
393
394 DART_EXPORT Dart_Handle Dart_ToString(Dart_Handle object) {
395 DARTSCOPE(Isolate::Current());
396 const Object& obj = Object::Handle(Api::UnwrapHandle(object));
397 Object& result = Object::Handle();
398 if (obj.IsString()) {
399 result = obj.raw();
400 } else if (obj.IsInstance()) {
401 Instance& receiver = Instance::Handle();
402 receiver ^= obj.raw();
403 result = DartLibraryCalls::ToString(receiver);
404 if (result.IsUnhandledException()) {
405 return Api::ErrorFromException(result);
406 }
407 } else {
408 // This is a VM internal object. Call the C++ method of printing.
409 result = String::New(obj.ToCString());
410 }
411 return Api::NewLocalHandle(result);
412 }
413
414
415 DART_EXPORT Dart_Handle Dart_IsSame(Dart_Handle obj1, Dart_Handle obj2,
416 bool* value) {
417 DARTSCOPE(Isolate::Current());
418 const Object& expected = Object::Handle(Api::UnwrapHandle(obj1));
419 const Object& actual = Object::Handle(Api::UnwrapHandle(obj2));
420 *value = (expected.raw() == actual.raw());
421 return Api::Success();
422 }
423
424
425 DART_EXPORT Dart_Handle Dart_NewPersistentHandle(Dart_Handle object) {
426 Isolate* isolate = Isolate::Current();
427 DARTSCOPE(isolate);
428 ApiState* state = isolate->api_state();
429 ASSERT(state != NULL);
430 const Object& old_ref = Object::Handle(Api::UnwrapHandle(object));
431 PersistentHandle* new_ref = state->persistent_handles().AllocateHandle();
432 new_ref->set_raw(old_ref);
433 return reinterpret_cast<Dart_Handle>(new_ref);
434 }
435
436
437 DART_EXPORT void Dart_DeletePersistentHandle(Dart_Handle object) {
438 Isolate* isolate = Isolate::Current();
439 ASSERT(isolate != NULL);
440 ApiState* state = isolate->api_state();
441 ASSERT(state != NULL);
442 PersistentHandle* ref = Api::UnwrapAsPersistentHandle(*state, object);
443 ASSERT(!ref->IsProtected());
444 if (!ref->IsProtected()) {
445 state->persistent_handles().FreeHandle(ref);
446 }
447 }
448
449
450 DART_EXPORT Dart_Handle Dart_MakeWeakPersistentHandle(Dart_Handle object) {
451 UNIMPLEMENTED();
452 return NULL;
453 }
454
455
456 DART_EXPORT Dart_Handle Dart_MakePersistentHandle(Dart_Handle object) {
457 UNIMPLEMENTED();
458 return NULL;
459 }
460
461
462 // --- Initialization and Globals ---
463
464
206 // TODO(iposva): This is a placeholder for the eventual external Dart API. 465 // TODO(iposva): This is a placeholder for the eventual external Dart API.
207 DART_EXPORT bool Dart_Initialize(int argc, 466 DART_EXPORT bool Dart_Initialize(int argc,
208 const char** argv, 467 const char** argv,
209 Dart_IsolateInitCallback callback) { 468 Dart_IsolateInitCallback callback) {
210 return Dart::InitOnce(argc, argv, callback); 469 return Dart::InitOnce(argc, argv, callback);
211 } 470 }
212 471
213 472
473 DART_EXPORT bool Dart_IsVMFlagSet(const char* flag_name) {
474 if (Flags::Lookup(flag_name) != NULL) {
475 return true;
476 }
477 return false;
478 }
479
480
481 // --- Isolates ---
482
483
214 DART_EXPORT Dart_Isolate Dart_CreateIsolate(const Dart_Snapshot* snapshot, 484 DART_EXPORT Dart_Isolate Dart_CreateIsolate(const Dart_Snapshot* snapshot,
215 void* data) { 485 void* data) {
216 Isolate* isolate = Dart::CreateIsolate(); 486 Isolate* isolate = Dart::CreateIsolate();
217 ASSERT(isolate != NULL); 487 ASSERT(isolate != NULL);
218 LongJump* base = isolate->long_jump_base(); 488 LongJump* base = isolate->long_jump_base();
219 LongJump jump; 489 LongJump jump;
220 isolate->set_long_jump_base(&jump); 490 isolate->set_long_jump_base(&jump);
221 if (setjmp(*jump.Set()) == 0) { 491 if (setjmp(*jump.Set()) == 0) {
222 Dart::InitializeIsolate(snapshot, data); 492 Dart::InitializeIsolate(snapshot, data);
223 START_TIMER(time_total_runtime); 493 START_TIMER(time_total_runtime);
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
255 Isolate::SetCurrent(isolate); 525 Isolate::SetCurrent(isolate);
256 } 526 }
257 527
258 528
259 DART_EXPORT void Dart_ExitIsolate() { 529 DART_EXPORT void Dart_ExitIsolate() {
260 ASSERT(Isolate::Current() != NULL); 530 ASSERT(Isolate::Current() != NULL);
261 Isolate::SetCurrent(NULL); 531 Isolate::SetCurrent(NULL);
262 } 532 }
263 533
264 534
265 static void SetupErrorResult(Dart_Handle* handle) { 535 static uint8_t* ApiAllocator(uint8_t* ptr,
266 // Make a copy of the error message as the original message string 536 intptr_t old_size,
267 // may get deallocated when we return back from the Dart API call. 537 intptr_t new_size) {
268 const String& error = String::Handle( 538 uword new_ptr = Api::Reallocate(reinterpret_cast<uword>(ptr),
269 Isolate::Current()->object_store()->sticky_error()); 539 old_size,
270 const Object& obj = Object::Handle(ApiError::New(error)); 540 new_size);
271 *handle = Api::NewLocalHandle(obj); 541 return reinterpret_cast<uint8_t*>(new_ptr);
272 } 542 }
273 543
274 544
545 DART_EXPORT Dart_Handle Dart_CreateSnapshot(uint8_t** snapshot_buffer,
546 intptr_t* snapshot_size) {
547 Isolate* isolate = Isolate::Current();
548 DARTSCOPE(isolate);
549 if (snapshot_buffer == NULL || snapshot_size == NULL) {
550 return Api::Error("Invalid input parameters to Dart_CreateSnapshot");
551 }
552 const char* msg = CheckIsolateState(isolate,
553 ClassFinalizer::kGeneratingSnapshot);
554 if (msg != NULL) {
555 return Api::Error(msg);
556 }
557 // Since this is only a snapshot the root library should not be set.
558 isolate->object_store()->set_root_library(Library::Handle());
559 SnapshotWriter writer(true, snapshot_buffer, ApiAllocator);
560 writer.WriteFullSnapshot();
561 *snapshot_size = writer.Size();
562 return Api::Success();
563 }
564
565
566 // --- Messages and Ports ---
567
568
275 DART_EXPORT void Dart_SetMessageCallbacks( 569 DART_EXPORT void Dart_SetMessageCallbacks(
276 Dart_PostMessageCallback post_message_callback, 570 Dart_PostMessageCallback post_message_callback,
277 Dart_ClosePortCallback close_port_callback) { 571 Dart_ClosePortCallback close_port_callback) {
278 Isolate* isolate = Isolate::Current(); 572 Isolate* isolate = Isolate::Current();
279 ASSERT(isolate != NULL); 573 ASSERT(isolate != NULL);
280 ASSERT(post_message_callback != NULL); 574 ASSERT(post_message_callback != NULL);
281 ASSERT(close_port_callback != NULL); 575 ASSERT(close_port_callback != NULL);
282 isolate->set_post_message_callback(post_message_callback); 576 isolate->set_post_message_callback(post_message_callback);
283 isolate->set_close_port_callback(close_port_callback); 577 isolate->set_close_port_callback(close_port_callback);
284 } 578 }
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
343 isolate->StandardRunLoop(); 637 isolate->StandardRunLoop();
344 result = Api::Success(); 638 result = Api::Success();
345 } else { 639 } else {
346 SetupErrorResult(&result); 640 SetupErrorResult(&result);
347 } 641 }
348 isolate->set_long_jump_base(base); 642 isolate->set_long_jump_base(base);
349 return result; 643 return result;
350 } 644 }
351 645
352 646
353 // NOTE: Need to pass 'result' as a parameter here in order to avoid 647 static uint8_t* allocator(uint8_t* ptr, intptr_t old_size, intptr_t new_size) {
354 // warning: variable 'result' might be clobbered by 'longjmp' or 'vfork' 648 void* new_ptr = realloc(reinterpret_cast<void*>(ptr), new_size);
355 // which shows up because of the use of setjmp. 649 return reinterpret_cast<uint8_t*>(new_ptr);
356 static void CompileSource(Isolate* isolate,
357 const Library& lib,
358 const String& url,
359 const String& source,
360 RawScript::Kind kind,
361 Dart_Handle* result) {
362 bool update_lib_status = (kind == RawScript::kScript ||
363 kind == RawScript::kLibrary);
364 if (update_lib_status) {
365 lib.SetLoadInProgress();
366 }
367 const Script& script = Script::Handle(Script::New(url, source, kind));
368 ASSERT(isolate != NULL);
369 LongJump* base = isolate->long_jump_base();
370 LongJump jump;
371 isolate->set_long_jump_base(&jump);
372 if (setjmp(*jump.Set()) == 0) {
373 Compiler::Compile(lib, script);
374 *result = Api::NewLocalHandle(lib);
375 if (update_lib_status) {
376 lib.SetLoaded();
377 }
378 } else {
379 SetupErrorResult(result);
380 if (update_lib_status) {
381 lib.SetLoadError();
382 }
383 }
384 isolate->set_long_jump_base(base);
385 } 650 }
386 651
387 652
388 DART_EXPORT Dart_Handle Dart_LoadScript(Dart_Handle url, 653 DART_EXPORT bool Dart_PostIntArray(Dart_Port port,
389 Dart_Handle source, 654 intptr_t len,
390 Dart_LibraryTagHandler handler) { 655 intptr_t* data) {
391 Isolate* isolate = Isolate::Current(); 656 uint8_t* buffer = NULL;
392 DARTSCOPE(isolate); 657 MessageWriter writer(&buffer, &allocator);
393 TIMERSCOPE(time_script_loading); 658
394 const String& url_str = Api::UnwrapStringHandle(url); 659 writer.WriteMessage(len, data);
395 if (url_str.IsNull()) { 660
396 RETURN_TYPE_ERROR(url, String); 661 // Post the message at the given port.
397 } 662 return PortMap::PostMessage(port, kNoReplyPort, buffer);
398 const String& source_str = Api::UnwrapStringHandle(source);
399 if (source_str.IsNull()) {
400 RETURN_TYPE_ERROR(source, String);
401 }
402 Library& library = Library::Handle(isolate->object_store()->root_library());
403 if (!library.IsNull()) {
404 const String& library_url = String::Handle(library.url());
405 return Api::Error("%s: A script has already been loaded from '%s'.",
406 CURRENT_FUNC, library_url.ToCString());
407 }
408 isolate->set_library_tag_handler(handler);
409 library = Library::New(url_str);
410 library.Register();
411 isolate->object_store()->set_root_library(library);
412 Dart_Handle result;
413 CompileSource(isolate,
414 library,
415 url_str,
416 source_str,
417 RawScript::kScript,
418 &result);
419 return result;
420 } 663 }
421 664
422 665
423 DEFINE_FLAG(bool, compile_all, false, "Eagerly compile all code."); 666 DART_EXPORT bool Dart_Post(Dart_Port port, Dart_Handle handle) {
424 667 DARTSCOPE(Isolate::Current());
425 static void CompileAll(Isolate* isolate, Dart_Handle* result) { 668 const Object& object = Object::Handle(Api::UnwrapHandle(handle));
426 *result = Api::Success(); 669 uint8_t* data = NULL;
427 if (FLAG_compile_all) { 670 SnapshotWriter writer(false, &data, &allocator);
428 ASSERT(isolate != NULL); 671 writer.WriteObject(object.raw());
429 LongJump* base = isolate->long_jump_base(); 672 writer.FinalizeBuffer();
430 LongJump jump; 673 return PortMap::PostMessage(port, kNoReplyPort, data);
431 isolate->set_long_jump_base(&jump);
432 if (setjmp(*jump.Set()) == 0) {
433 Library::CompileAll();
434 } else {
435 SetupErrorResult(result);
436 }
437 isolate->set_long_jump_base(base);
438 }
439 } 674 }
440 675
441 676
442 // Return error if isolate is in an inconsistent state. 677 // --- Scopes ----
443 // Return NULL when no error condition exists. 678
444 static const char* CheckIsolateState( 679
445 Isolate* isolate, 680 DART_EXPORT void Dart_EnterScope() {
446 bool generating_snapshot = ClassFinalizer::kNotGeneratingSnapshot) { 681 Isolate* isolate = Isolate::Current();
447 bool result = (generating_snapshot) ? 682 ASSERT(isolate != NULL);
448 ClassFinalizer::FinalizePendingClassesForSnapshotCreation() : 683 ApiState* state = isolate->api_state();
449 ClassFinalizer::FinalizePendingClasses(); 684 ASSERT(state != NULL);
450 if (!result) { 685 ApiLocalScope* new_scope = new ApiLocalScope(state->top_scope(),
451 // Make a copy of the error message as the original message string 686 reinterpret_cast<uword>(&state));
452 // may get deallocated when we return back from the Dart API call. 687 ASSERT(new_scope != NULL);
453 const String& err = 688 state->set_top_scope(new_scope); // New scope is now the top scope.
454 String::Handle(isolate->object_store()->sticky_error());
455 const char* errmsg = err.ToCString();
456 intptr_t errlen = strlen(errmsg) + 1;
457 char* msg = reinterpret_cast<char*>(Api::Allocate(errlen));
458 OS::SNPrint(msg, errlen, "%s", errmsg);
459 return msg;
460 }
461 return NULL;
462 } 689 }
463 690
464 691
465 DART_EXPORT Dart_Handle Dart_CompileAll() { 692 DART_EXPORT void Dart_ExitScope() {
466 Isolate* isolate = Isolate::Current(); 693 Isolate* isolate = Isolate::Current();
467 DARTSCOPE(isolate); 694 ASSERT(isolate != NULL);
468 Dart_Handle result; 695 ApiState* state = isolate->api_state();
469 const char* msg = CheckIsolateState(isolate); 696 ASSERT(state != NULL);
470 if (msg != NULL) { 697 ApiLocalScope* scope = state->top_scope();
471 return Api::Error(msg); 698 ASSERT(scope != NULL);
472 } 699 state->set_top_scope(scope->previous()); // Reset top scope to previous.
473 CompileAll(isolate, &result); 700 delete scope; // Free up the old scope which we have just exited.
474 return result;
475 } 701 }
476 702
477 703
478 DART_EXPORT bool Dart_IsLibrary(Dart_Handle object) { 704 // --- Objects ----
479 DARTSCOPE(Isolate::Current());
480 const Object& obj = Object::Handle(Api::UnwrapHandle(object));
481 return obj.IsLibrary();
482 }
483
484
485 DART_EXPORT Dart_Handle Dart_LibraryUrl(Dart_Handle library) {
486 DARTSCOPE(Isolate::Current());
487 const Library& lib = Api::UnwrapLibraryHandle(library);
488 if (lib.IsNull()) {
489 RETURN_TYPE_ERROR(library, Library);
490 }
491 const String& url = String::Handle(lib.url());
492 ASSERT(!url.IsNull());
493 return Api::NewLocalHandle(url);
494 }
495
496
497 DART_EXPORT Dart_Handle Dart_LibraryImportLibrary(Dart_Handle library,
498 Dart_Handle import) {
499 DARTSCOPE(Isolate::Current());
500 const Library& library_vm = Api::UnwrapLibraryHandle(library);
501 if (library_vm.IsNull()) {
502 RETURN_TYPE_ERROR(library, Library);
503 }
504 const Library& import_vm = Api::UnwrapLibraryHandle(import);
505 if (import_vm.IsNull()) {
506 RETURN_TYPE_ERROR(import, Library);
507 }
508 library_vm.AddImport(import_vm);
509 return Api::Success();
510 }
511
512
513 DART_EXPORT Dart_Handle Dart_LookupLibrary(Dart_Handle url) {
514 DARTSCOPE(Isolate::Current());
515 const String& url_str = Api::UnwrapStringHandle(url);
516 if (url_str.IsNull()) {
517 RETURN_TYPE_ERROR(url, String);
518 }
519 const Library& library = Library::Handle(Library::LookupLibrary(url_str));
520 if (library.IsNull()) {
521 return Api::Error("%s: library '%s' not found.",
522 CURRENT_FUNC, url_str.ToCString());
523 } else {
524 return Api::NewLocalHandle(library);
525 }
526 }
527
528
529 DART_EXPORT Dart_Handle Dart_LoadLibrary(Dart_Handle url, Dart_Handle source) {
530 Isolate* isolate = Isolate::Current();
531 DARTSCOPE(isolate);
532 const String& url_str = Api::UnwrapStringHandle(url);
533 if (url_str.IsNull()) {
534 RETURN_TYPE_ERROR(url, String);
535 }
536 const String& source_str = Api::UnwrapStringHandle(source);
537 if (source_str.IsNull()) {
538 RETURN_TYPE_ERROR(source, String);
539 }
540 Library& library = Library::Handle(Library::LookupLibrary(url_str));
541 if (library.IsNull()) {
542 library = Library::New(url_str);
543 library.Register();
544 } else if (!library.LoadNotStarted()) {
545 // The source for this library has either been loaded or is in the
546 // process of loading. Return an error.
547 return Api::Error("%s: library '%s' has already been loaded.",
548 CURRENT_FUNC, url_str.ToCString());
549 }
550 Dart_Handle result;
551 CompileSource(isolate,
552 library,
553 url_str,
554 source_str,
555 RawScript::kLibrary,
556 &result);
557 return result;
558 }
559
560
561 DART_EXPORT Dart_Handle Dart_LoadSource(Dart_Handle library,
562 Dart_Handle url,
563 Dart_Handle source) {
564 Isolate* isolate = Isolate::Current();
565 DARTSCOPE(isolate);
566 const Library& lib = Api::UnwrapLibraryHandle(library);
567 if (lib.IsNull()) {
568 RETURN_TYPE_ERROR(library, Library);
569 }
570 const String& url_str = Api::UnwrapStringHandle(url);
571 if (url_str.IsNull()) {
572 RETURN_TYPE_ERROR(url, String);
573 }
574 const String& source_str = Api::UnwrapStringHandle(source);
575 if (source_str.IsNull()) {
576 RETURN_TYPE_ERROR(source, String);
577 }
578 Dart_Handle result;
579 CompileSource(isolate, lib, url_str, source_str, RawScript::kSource, &result);
580 return result;
581 }
582
583
584 DART_EXPORT Dart_Handle Dart_SetNativeResolver(
585 Dart_Handle library,
586 Dart_NativeEntryResolver resolver) {
587 DARTSCOPE(Isolate::Current());
588 const Library& lib = Api::UnwrapLibraryHandle(library);
589 if (lib.IsNull()) {
590 RETURN_TYPE_ERROR(library, Library);
591 }
592 lib.set_native_entry_resolver(resolver);
593 return Api::Success();
594 }
595
596
597 DART_EXPORT Dart_Handle Dart_ToString(Dart_Handle object) {
598 DARTSCOPE(Isolate::Current());
599 const Object& obj = Object::Handle(Api::UnwrapHandle(object));
600 Object& result = Object::Handle();
601 if (obj.IsString()) {
602 result = obj.raw();
603 } else if (obj.IsInstance()) {
604 Instance& receiver = Instance::Handle();
605 receiver ^= obj.raw();
606 result = DartLibraryCalls::ToString(receiver);
607 if (result.IsUnhandledException()) {
608 return Api::ErrorFromException(result);
609 }
610 } else {
611 // This is a VM internal object. Call the C++ method of printing.
612 result = String::New(obj.ToCString());
613 }
614 return Api::NewLocalHandle(result);
615 }
616 705
617 706
618 DART_EXPORT Dart_Handle Dart_Null() { 707 DART_EXPORT Dart_Handle Dart_Null() {
619 return Api::Null(); 708 return Api::Null();
620 } 709 }
621 710
622 711
623 DART_EXPORT bool Dart_IsNull(Dart_Handle object) { 712 DART_EXPORT bool Dart_IsNull(Dart_Handle object) {
624 DARTSCOPE(Isolate::Current()); 713 DARTSCOPE(Isolate::Current());
625 const Object& obj = Object::Handle(Api::UnwrapHandle(object)); 714 const Object& obj = Object::Handle(Api::UnwrapHandle(object));
626 return obj.IsNull(); 715 return obj.IsNull();
627 } 716 }
628 717
629 718
630 DART_EXPORT bool Dart_IsClosure(Dart_Handle object) {
631 DARTSCOPE(Isolate::Current());
632 const Object& obj = Object::Handle(Api::UnwrapHandle(object));
633 return obj.IsClosure();
634 }
635
636
637 DART_EXPORT int64_t Dart_ClosureSmrck(Dart_Handle object) {
638 DARTSCOPE(Isolate::Current());
639 const Closure& obj = Closure::CheckedHandle(Api::UnwrapHandle(object));
640 const Integer& smrck = Integer::Handle(obj.smrck());
641 return smrck.IsNull() ? 0 : smrck.AsInt64Value();
642 }
643
644
645 DART_EXPORT void Dart_ClosureSetSmrck(Dart_Handle object, int64_t value) {
646 DARTSCOPE(Isolate::Current());
647 const Closure& obj = Closure::CheckedHandle(Api::UnwrapHandle(object));
648 const Integer& smrck = Integer::Handle(Integer::New(value));
649 obj.set_smrck(smrck);
650 }
651
652
653 DART_EXPORT Dart_Handle Dart_ObjectEquals(Dart_Handle obj1, Dart_Handle obj2, 719 DART_EXPORT Dart_Handle Dart_ObjectEquals(Dart_Handle obj1, Dart_Handle obj2,
654 bool* value) { 720 bool* value) {
655 DARTSCOPE(Isolate::Current()); 721 DARTSCOPE(Isolate::Current());
656 const Instance& expected = Instance::CheckedHandle(Api::UnwrapHandle(obj1)); 722 const Instance& expected = Instance::CheckedHandle(Api::UnwrapHandle(obj1));
657 const Instance& actual = Instance::CheckedHandle(Api::UnwrapHandle(obj2)); 723 const Instance& actual = Instance::CheckedHandle(Api::UnwrapHandle(obj2));
658 const Instance& result = 724 const Instance& result =
659 Instance::Handle(DartLibraryCalls::Equals(expected, actual)); 725 Instance::Handle(DartLibraryCalls::Equals(expected, actual));
660 if (result.IsBool()) { 726 if (result.IsBool()) {
661 Bool& b = Bool::Handle(); 727 Bool& b = Bool::Handle();
662 b ^= result.raw(); 728 b ^= result.raw();
663 *value = b.value(); 729 *value = b.value();
664 return Api::Success(); 730 return Api::Success();
665 } else if (result.IsUnhandledException()) { 731 } else if (result.IsUnhandledException()) {
666 return Api::ErrorFromException(result); 732 return Api::ErrorFromException(result);
667 } else { 733 } else {
668 return Api::Error("Expected boolean result from =="); 734 return Api::Error("Expected boolean result from ==");
669 } 735 }
670 } 736 }
671 737
672 738
673 DART_EXPORT Dart_Handle Dart_IsSame(Dart_Handle obj1, Dart_Handle obj2,
674 bool* value) {
675 DARTSCOPE(Isolate::Current());
676 const Object& expected = Object::Handle(Api::UnwrapHandle(obj1));
677 const Object& actual = Object::Handle(Api::UnwrapHandle(obj2));
678 *value = (expected.raw() == actual.raw());
679 return Api::Success();
680 }
681
682
683 DART_EXPORT Dart_Handle Dart_GetClass(Dart_Handle library, Dart_Handle name) {
684 DARTSCOPE(Isolate::Current());
685 const Object& param = Object::Handle(Api::UnwrapHandle(name));
686 if (param.IsNull() || !param.IsString()) {
687 return Api::Error("Invalid class name specified");
688 }
689 const Library& lib = Library::CheckedHandle(Api::UnwrapHandle(library));
690 if (lib.IsNull()) {
691 return Api::Error("Invalid parameter, Unknown library specified");
692 }
693 String& cls_name = String::Handle();
694 cls_name ^= param.raw();
695 const Class& cls = Class::Handle(lib.LookupClass(cls_name));
696 if (cls.IsNull()) {
697 const String& lib_name = String::Handle(lib.name());
698 return Api::Error("Class '%s' not found in library '%s'.",
699 cls_name.ToCString(), lib_name.ToCString());
700 }
701 return Api::NewLocalHandle(cls);
702 }
703
704
705 // TODO(iposva): This call actually implements IsInstanceOfClass. 739 // TODO(iposva): This call actually implements IsInstanceOfClass.
706 // Do we also need a real Dart_IsInstanceOf, which should take an instance 740 // Do we also need a real Dart_IsInstanceOf, which should take an instance
707 // rather than an object and a type rather than a class? 741 // rather than an object and a type rather than a class?
708 DART_EXPORT Dart_Handle Dart_ObjectIsType(Dart_Handle object, 742 DART_EXPORT Dart_Handle Dart_ObjectIsType(Dart_Handle object,
709 Dart_Handle clazz, 743 Dart_Handle clazz,
710 bool* value) { 744 bool* value) {
711 Isolate* isolate = Isolate::Current(); 745 Isolate* isolate = Isolate::Current();
712 DARTSCOPE(isolate); 746 DARTSCOPE(isolate);
713 const Class& cls = Class::CheckedHandle(Api::UnwrapHandle(clazz)); 747 const Class& cls = Class::CheckedHandle(Api::UnwrapHandle(clazz));
714 if (cls.IsNull()) { 748 if (cls.IsNull()) {
715 return Api::Error("instanceof check against null class"); 749 return Api::Error("instanceof check against null class");
716 } 750 }
717 const Object& obj = Object::Handle(Api::UnwrapHandle(object)); 751 const Object& obj = Object::Handle(Api::UnwrapHandle(object));
718 Instance& instance = Instance::Handle(); 752 Instance& instance = Instance::Handle();
719 instance ^= obj.raw(); 753 instance ^= obj.raw();
720 // Finalize all classes. 754 // Finalize all classes.
721 const char* msg = CheckIsolateState(isolate); 755 const char* msg = CheckIsolateState(isolate);
722 if (msg != NULL) { 756 if (msg != NULL) {
723 return Api::Error(msg); 757 return Api::Error(msg);
724 } 758 }
725 const Type& type = Type::Handle(Type::NewNonParameterizedType(cls)); 759 const Type& type = Type::Handle(Type::NewNonParameterizedType(cls));
726 *value = instance.Is(type); 760 *value = instance.Is(type);
727 return Api::Success(); 761 return Api::Success();
728 } 762 }
729 763
730 764
765 // --- Numbers ----
766
767
731 // TODO(iposva): The argument should be an instance. 768 // TODO(iposva): The argument should be an instance.
732 DART_EXPORT bool Dart_IsNumber(Dart_Handle object) { 769 DART_EXPORT bool Dart_IsNumber(Dart_Handle object) {
733 DARTSCOPE(Isolate::Current()); 770 DARTSCOPE(Isolate::Current());
734 const Object& obj = Object::Handle(Api::UnwrapHandle(object)); 771 const Object& obj = Object::Handle(Api::UnwrapHandle(object));
735 return obj.IsNumber(); 772 return obj.IsNumber();
736 } 773 }
737 774
738 775
776 // --- Integers ----
777
778
739 DART_EXPORT bool Dart_IsInteger(Dart_Handle object) { 779 DART_EXPORT bool Dart_IsInteger(Dart_Handle object) {
740 DARTSCOPE(Isolate::Current()); 780 DARTSCOPE(Isolate::Current());
741 const Object& obj = Object::Handle(Api::UnwrapHandle(object)); 781 const Object& obj = Object::Handle(Api::UnwrapHandle(object));
742 return obj.IsInteger(); 782 return obj.IsInteger();
743 } 783 }
744 784
745 785
786 DART_EXPORT Dart_Handle Dart_IntegerFitsIntoInt64(Dart_Handle integer,
787 bool* fits) {
788 DARTSCOPE(Isolate::Current());
789 const Object& obj = Object::Handle(Api::UnwrapHandle(integer));
790 if (obj.IsSmi() || obj.IsMint()) {
791 *fits = true;
792 return Api::Success();
793 } else if (obj.IsBigint()) {
794 #if defined(DEBUG)
795 Bigint& bigint = Bigint::Handle();
796 bigint ^= obj.raw();
797 ASSERT(!BigintOperations::FitsIntoInt64(bigint));
798 #endif
799 *fits = false;
800 return Api::Success();
801 }
802 return Api::Error("Object is not a Integer");
803 }
804
805
746 DART_EXPORT Dart_Handle Dart_NewInteger(int64_t value) { 806 DART_EXPORT Dart_Handle Dart_NewInteger(int64_t value) {
747 DARTSCOPE(Isolate::Current()); 807 DARTSCOPE(Isolate::Current());
748 const Integer& obj = Integer::Handle(Integer::New(value)); 808 const Integer& obj = Integer::Handle(Integer::New(value));
749 return Api::NewLocalHandle(obj); 809 return Api::NewLocalHandle(obj);
750 } 810 }
751 811
752 812
753 DART_EXPORT Dart_Handle Dart_NewIntegerFromHexCString(const char* str) { 813 DART_EXPORT Dart_Handle Dart_NewIntegerFromHexCString(const char* str) {
754 DARTSCOPE(Isolate::Current()); 814 DARTSCOPE(Isolate::Current());
755 const String& str_obj = String::Handle(String::New(str)); 815 const String& str_obj = String::Handle(String::New(str));
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
795 } 855 }
796 if (obj.IsBigint()) { 856 if (obj.IsBigint()) {
797 bigint ^= obj.raw(); 857 bigint ^= obj.raw();
798 *value = BigintOperations::ToHexCString(bigint, &Api::Allocate); 858 *value = BigintOperations::ToHexCString(bigint, &Api::Allocate);
799 return Api::Success(); 859 return Api::Success();
800 } 860 }
801 return Api::Error("Object is not a Integer"); 861 return Api::Error("Object is not a Integer");
802 } 862 }
803 863
804 864
805 DART_EXPORT Dart_Handle Dart_IntegerFitsIntoInt64(Dart_Handle integer, 865 // --- Booleans ----
806 bool* fits) {
807 DARTSCOPE(Isolate::Current());
808 const Object& obj = Object::Handle(Api::UnwrapHandle(integer));
809 if (obj.IsSmi() || obj.IsMint()) {
810 *fits = true;
811 return Api::Success();
812 } else if (obj.IsBigint()) {
813 #if defined(DEBUG)
814 Bigint& bigint = Bigint::Handle();
815 bigint ^= obj.raw();
816 ASSERT(!BigintOperations::FitsIntoInt64(bigint));
817 #endif
818 *fits = false;
819 return Api::Success();
820 }
821 return Api::Error("Object is not a Integer");
822 }
823 866
824 867
825 DART_EXPORT Dart_Handle Dart_True() { 868 DART_EXPORT Dart_Handle Dart_True() {
826 return Api::True(); 869 return Api::True();
827 } 870 }
828 871
829 872
830 DART_EXPORT Dart_Handle Dart_False() { 873 DART_EXPORT Dart_Handle Dart_False() {
831 return Api::False(); 874 return Api::False();
832 } 875 }
(...skipping 18 matching lines...) Expand all
851 if (obj.IsBool()) { 894 if (obj.IsBool()) {
852 Bool& bool_obj = Bool::Handle(); 895 Bool& bool_obj = Bool::Handle();
853 bool_obj ^= obj.raw(); 896 bool_obj ^= obj.raw();
854 *value = bool_obj.value(); 897 *value = bool_obj.value();
855 return Api::Success(); 898 return Api::Success();
856 } 899 }
857 return Api::Error("Object is not a Boolean"); 900 return Api::Error("Object is not a Boolean");
858 } 901 }
859 902
860 903
904 // --- Doubles ---
905
906
861 DART_EXPORT bool Dart_IsDouble(Dart_Handle object) { 907 DART_EXPORT bool Dart_IsDouble(Dart_Handle object) {
862 DARTSCOPE(Isolate::Current()); 908 DARTSCOPE(Isolate::Current());
863 const Object& obj = Object::Handle(Api::UnwrapHandle(object)); 909 const Object& obj = Object::Handle(Api::UnwrapHandle(object));
864 return obj.IsDouble(); 910 return obj.IsDouble();
865 } 911 }
866 912
867 913
868 DART_EXPORT Dart_Handle Dart_NewDouble(double value) { 914 DART_EXPORT Dart_Handle Dart_NewDouble(double value) {
869 DARTSCOPE(Isolate::Current()); 915 DARTSCOPE(Isolate::Current());
870 const Double& obj = Double::Handle(Double::New(value)); 916 const Double& obj = Double::Handle(Double::New(value));
871 return Api::NewLocalHandle(obj); 917 return Api::NewLocalHandle(obj);
872 } 918 }
873 919
874 920
875 DART_EXPORT Dart_Handle Dart_DoubleValue(Dart_Handle integer, double* result) { 921 DART_EXPORT Dart_Handle Dart_DoubleValue(Dart_Handle integer, double* result) {
876 DARTSCOPE(Isolate::Current()); 922 DARTSCOPE(Isolate::Current());
877 const Object& obj = Object::Handle(Api::UnwrapHandle(integer)); 923 const Object& obj = Object::Handle(Api::UnwrapHandle(integer));
878 if (obj.IsDouble()) { 924 if (obj.IsDouble()) {
879 Double& double_obj = Double::Handle(); 925 Double& double_obj = Double::Handle();
880 double_obj ^= obj.raw(); 926 double_obj ^= obj.raw();
881 *result = double_obj.value(); 927 *result = double_obj.value();
882 return Api::Success(); 928 return Api::Success();
883 } 929 }
884 return Api::Error("Object is not a Double"); 930 return Api::Error("Object is not a Double");
885 } 931 }
886 932
887 933
934 // --- Strings ---
935
936
888 DART_EXPORT bool Dart_IsString(Dart_Handle object) { 937 DART_EXPORT bool Dart_IsString(Dart_Handle object) {
889 DARTSCOPE(Isolate::Current()); 938 DARTSCOPE(Isolate::Current());
890 const Object& obj = Object::Handle(Api::UnwrapHandle(object)); 939 const Object& obj = Object::Handle(Api::UnwrapHandle(object));
891 return obj.IsString(); 940 return obj.IsString();
892 } 941 }
893 942
894 943
944 DART_EXPORT bool Dart_IsString8(Dart_Handle object) {
945 DARTSCOPE(Isolate::Current());
946 const Object& obj = Object::Handle(Api::UnwrapHandle(object));
947 return obj.IsOneByteString() || obj.IsExternalOneByteString();
948 }
949
950
951 DART_EXPORT bool Dart_IsString16(Dart_Handle object) {
952 DARTSCOPE(Isolate::Current());
953 const Object& obj = Object::Handle(Api::UnwrapHandle(object));
954 return (obj.IsOneByteString() || obj.IsExternalOneByteString() ||
955 obj.IsTwoByteString() || obj.IsExternalTwoByteString());
956 }
957
958
895 DART_EXPORT Dart_Handle Dart_StringLength(Dart_Handle str, intptr_t* len) { 959 DART_EXPORT Dart_Handle Dart_StringLength(Dart_Handle str, intptr_t* len) {
896 DARTSCOPE(Isolate::Current()); 960 DARTSCOPE(Isolate::Current());
897 const Object& obj = Object::Handle(Api::UnwrapHandle(str)); 961 const Object& obj = Object::Handle(Api::UnwrapHandle(str));
898 if (obj.IsString()) { 962 if (obj.IsString()) {
899 String& string_obj = String::Handle(); 963 String& string_obj = String::Handle();
900 string_obj ^= obj.raw(); 964 string_obj ^= obj.raw();
901 *len = string_obj.Length(); 965 *len = string_obj.Length();
902 return Api::Success(); 966 return Api::Success();
903 } 967 }
904 return Api::Error("Object is not a String"); 968 return Api::Error("Object is not a String");
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
962 intptr_t length, 1026 intptr_t length,
963 void* peer, 1027 void* peer,
964 Dart_PeerFinalizer callback) { 1028 Dart_PeerFinalizer callback) {
965 DARTSCOPE(Isolate::Current()); 1029 DARTSCOPE(Isolate::Current());
966 const String& obj = 1030 const String& obj =
967 String::Handle(String::NewExternal(codepoints, length, peer, callback)); 1031 String::Handle(String::NewExternal(codepoints, length, peer, callback));
968 return Api::NewLocalHandle(obj); 1032 return Api::NewLocalHandle(obj);
969 } 1033 }
970 1034
971 1035
972 DART_EXPORT bool Dart_IsString8(Dart_Handle object) {
973 DARTSCOPE(Isolate::Current());
974 const Object& obj = Object::Handle(Api::UnwrapHandle(object));
975 return obj.IsOneByteString() || obj.IsExternalOneByteString();
976 }
977
978
979 DART_EXPORT bool Dart_IsString16(Dart_Handle object) {
980 DARTSCOPE(Isolate::Current());
981 const Object& obj = Object::Handle(Api::UnwrapHandle(object));
982 return (obj.IsOneByteString() || obj.IsExternalOneByteString() ||
983 obj.IsTwoByteString() || obj.IsExternalTwoByteString());
984 }
985
986
987 DART_EXPORT Dart_Handle Dart_StringGet8(Dart_Handle str, 1036 DART_EXPORT Dart_Handle Dart_StringGet8(Dart_Handle str,
988 uint8_t* codepoints, 1037 uint8_t* codepoints,
989 intptr_t* length) { 1038 intptr_t* length) {
990 DARTSCOPE(Isolate::Current()); 1039 DARTSCOPE(Isolate::Current());
991 const Object& obj = Object::Handle(Api::UnwrapHandle(str)); 1040 const Object& obj = Object::Handle(Api::UnwrapHandle(str));
992 if (obj.IsString()) { 1041 if (obj.IsString()) {
993 String& string_obj = String::Handle(); 1042 String& string_obj = String::Handle();
994 string_obj ^= obj.raw(); 1043 string_obj ^= obj.raw();
995 if (string_obj.CharSize() == String::kOneByteChar) { 1044 if (string_obj.CharSize() == String::kOneByteChar) {
996 intptr_t str_len = string_obj.Length(); 1045 intptr_t str_len = string_obj.Length();
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
1065 } 1114 }
1066 strncpy(res, string_value, string_length + 1); 1115 strncpy(res, string_value, string_length + 1);
1067 ASSERT(res[string_length] == '\0'); 1116 ASSERT(res[string_length] == '\0');
1068 *result = res; 1117 *result = res;
1069 return Api::Success(); 1118 return Api::Success();
1070 } 1119 }
1071 return Api::Error("Object is not a String"); 1120 return Api::Error("Object is not a String");
1072 } 1121 }
1073 1122
1074 1123
1124 // --- Lists ---
1125
1126
1075 static RawInstance* GetListInstance(Isolate* isolate, const Object& obj) { 1127 static RawInstance* GetListInstance(Isolate* isolate, const Object& obj) {
1076 if (obj.IsInstance()) { 1128 if (obj.IsInstance()) {
1077 Instance& instance = Instance::Handle(); 1129 Instance& instance = Instance::Handle();
1078 instance ^= obj.raw(); 1130 instance ^= obj.raw();
1079 const Type& type = Type::Handle(isolate->object_store()->list_interface()); 1131 const Type& type = Type::Handle(isolate->object_store()->list_interface());
1080 return instance.Is(type) ? instance.raw() : Instance::null(); 1132 return instance.Is(type) ? instance.raw() : Instance::null();
1081 } 1133 }
1082 return Instance::null(); 1134 return Instance::null();
1083 } 1135 }
1084 1136
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
1187 } 1239 }
1188 isolate->set_long_jump_base(base); 1240 isolate->set_long_jump_base(base);
1189 return retval.raw(); 1241 return retval.raw();
1190 } 1242 }
1191 SetupErrorResult(result); 1243 SetupErrorResult(result);
1192 isolate->set_long_jump_base(base); 1244 isolate->set_long_jump_base(base);
1193 return Object::null(); 1245 return Object::null();
1194 } 1246 }
1195 1247
1196 1248
1197 DART_EXPORT Dart_Handle Dart_ListGetAsBytes(Dart_Handle list,
1198 intptr_t offset,
1199 uint8_t* native_array,
1200 intptr_t length) {
1201 Isolate* isolate = Isolate::Current();
1202 DARTSCOPE(isolate);
1203 const Object& obj = Object::Handle(Api::UnwrapHandle(list));
1204 if (obj.IsArray()) {
1205 Array& array_obj = Array::Handle();
1206 array_obj ^= obj.raw();
1207 if ((offset + length) <= array_obj.Length()) {
1208 Object& element = Object::Handle();
1209 Integer& integer = Integer::Handle();
1210 for (int i = 0; i < length; i++) {
1211 element = array_obj.At(offset + i);
1212 if (!element.IsInteger()) {
1213 return Api::Error("%s expects the argument 'list' to be "
1214 "a List of int", CURRENT_FUNC);
1215 }
1216 integer ^= element.raw();
1217 native_array[i] = static_cast<uint8_t>(integer.AsInt64Value() & 0xff);
1218 ASSERT(integer.AsInt64Value() <= 0xff);
1219 // TODO(hpayer): value should always be smaller then 0xff. Add error
1220 // handling.
1221 }
1222 return Api::Success();
1223 }
1224 return Api::Error("Invalid length passed in to access array elements");
1225 }
1226 // TODO(5526318): Make access to GrowableObjectArray more efficient.
1227 // Now check and handle a dart object that implements the List interface.
1228 const Instance& instance = Instance::Handle(GetListInstance(isolate, obj));
1229 if (!instance.IsNull()) {
1230 String& name = String::Handle(String::New("[]"));
1231 const Function& function = Function::Handle(
1232 Resolver::ResolveDynamic(instance, name, 2, 0));
1233 if (!function.IsNull()) {
1234 Object& element = Object::Handle();
1235 Integer& intobj = Integer::Handle();
1236 Dart_Handle result;
1237 for (int i = 0; i < length; i++) {
1238 intobj = Integer::New(offset + i);
1239 element = GetListAt(isolate, instance, intobj, function, &result);
1240 if (Dart_IsError(result)) {
1241 return result; // Error condition.
1242 }
1243 if (!element.IsInteger()) {
1244 return Api::Error("%s expects the argument 'list' to be "
1245 "a List of int", CURRENT_FUNC);
1246 }
1247 intobj ^= element.raw();
1248 ASSERT(intobj.AsInt64Value() <= 0xff);
1249 // TODO(hpayer): value should always be smaller then 0xff. Add error
1250 // handling.
1251 native_array[i] = static_cast<uint8_t>(intobj.AsInt64Value() & 0xff);
1252 }
1253 return Api::Success();
1254 }
1255 }
1256 return Api::Error("Object does not implement the 'List' interface");
1257 }
1258
1259
1260 DART_EXPORT Dart_Handle Dart_ListGetAt(Dart_Handle list, intptr_t index) { 1249 DART_EXPORT Dart_Handle Dart_ListGetAt(Dart_Handle list, intptr_t index) {
1261 Isolate* isolate = Isolate::Current(); 1250 Isolate* isolate = Isolate::Current();
1262 DARTSCOPE(isolate); 1251 DARTSCOPE(isolate);
1263 const Object& obj = Object::Handle(Api::UnwrapHandle(list)); 1252 const Object& obj = Object::Handle(Api::UnwrapHandle(list));
1264 if (obj.IsArray()) { 1253 if (obj.IsArray()) {
1265 Array& array_obj = Array::Handle(); 1254 Array& array_obj = Array::Handle();
1266 array_obj ^= obj.raw(); 1255 array_obj ^= obj.raw();
1267 if ((index >= 0) && (index < array_obj.Length())) { 1256 if ((index >= 0) && (index < array_obj.Length())) {
1268 const Object& element = Object::Handle(array_obj.At(index)); 1257 const Object& element = Object::Handle(array_obj.At(index));
1269 return Api::NewLocalHandle(element); 1258 return Api::NewLocalHandle(element);
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
1319 } else { 1308 } else {
1320 *result = Api::Success(); 1309 *result = Api::Success();
1321 } 1310 }
1322 } else { 1311 } else {
1323 SetupErrorResult(result); 1312 SetupErrorResult(result);
1324 } 1313 }
1325 isolate->set_long_jump_base(base); 1314 isolate->set_long_jump_base(base);
1326 } 1315 }
1327 1316
1328 1317
1329 DART_EXPORT Dart_Handle Dart_ListSetAsBytes(Dart_Handle list, 1318 DART_EXPORT Dart_Handle Dart_ListSetAt(Dart_Handle list,
1330 intptr_t offset, 1319 intptr_t index,
1331 uint8_t* native_array, 1320 Dart_Handle value) {
1332 intptr_t length) {
1333 Isolate* isolate = Isolate::Current(); 1321 Isolate* isolate = Isolate::Current();
1334 DARTSCOPE(isolate); 1322 DARTSCOPE(isolate);
1335 const Object& obj = Object::Handle(Api::UnwrapHandle(list)); 1323 const Object& obj = Object::Handle(Api::UnwrapHandle(list));
1336 if (obj.IsArray()) { 1324 if (obj.IsArray()) {
1337 if (obj.IsImmutableArray()) { 1325 if (obj.IsImmutableArray()) {
1338 return Api::Error("Cannot modify immutable array"); 1326 return Api::Error("Cannot modify immutable array");
1339 } 1327 }
1340 Array& array_obj = Array::Handle(); 1328 Array& array_obj = Array::Handle();
1341 array_obj ^= obj.raw(); 1329 array_obj ^= obj.raw();
1342 Integer& integer = Integer::Handle(); 1330 const Object& value_obj = Object::Handle(Api::UnwrapHandle(value));
1343 if ((offset + length) <= array_obj.Length()) { 1331 if ((index >= 0) && (index < array_obj.Length())) {
1344 for (int i = 0; i < length; i++) { 1332 array_obj.SetAt(index, value_obj);
1345 integer = Integer::New(native_array[i]);
1346 array_obj.SetAt(offset + i, integer);
1347 }
1348 return Api::Success(); 1333 return Api::Success();
1349 } 1334 }
1350 return Api::Error("Invalid length passed in to set array elements"); 1335 return Api::Error("Invalid index passed in to set array element");
1351 } 1336 }
1352 // TODO(5526318): Make access to GrowableObjectArray more efficient. 1337 // TODO(5526318): Make access to GrowableObjectArray more efficient.
1353 // Now check and handle a dart object that implements the List interface. 1338 // Now check and handle a dart object that implements the List interface.
1354 const Instance& instance = Instance::Handle(GetListInstance(isolate, obj)); 1339 const Instance& instance = Instance::Handle(GetListInstance(isolate, obj));
1355 if (!instance.IsNull()) { 1340 if (!instance.IsNull()) {
1356 String& name = String::Handle(String::New("[]=")); 1341 String& name = String::Handle(String::New("[]="));
1357 const Function& function = Function::Handle( 1342 const Function& function = Function::Handle(
1358 Resolver::ResolveDynamic(instance, name, 3, 0)); 1343 Resolver::ResolveDynamic(instance, name, 3, 0));
1359 if (!function.IsNull()) { 1344 if (!function.IsNull()) {
1360 Integer& indexobj = Integer::Handle(); 1345 Dart_Handle result;
1361 Integer& valueobj = Integer::Handle(); 1346 const Integer& index_obj = Integer::Handle(Integer::New(index));
1347 const Object& value_obj = Object::Handle(Api::UnwrapHandle(value));
1348 SetListAt(isolate, instance, index_obj, value_obj, function, &result);
1349 return result;
1350 }
1351 }
1352 return Api::Error("Object does not implement the 'List' interface");
1353 }
1354
1355
1356 DART_EXPORT Dart_Handle Dart_ListGetAsBytes(Dart_Handle list,
1357 intptr_t offset,
1358 uint8_t* native_array,
1359 intptr_t length) {
1360 Isolate* isolate = Isolate::Current();
1361 DARTSCOPE(isolate);
1362 const Object& obj = Object::Handle(Api::UnwrapHandle(list));
1363 if (obj.IsArray()) {
1364 Array& array_obj = Array::Handle();
1365 array_obj ^= obj.raw();
1366 if ((offset + length) <= array_obj.Length()) {
1367 Object& element = Object::Handle();
1368 Integer& integer = Integer::Handle();
1369 for (int i = 0; i < length; i++) {
1370 element = array_obj.At(offset + i);
1371 if (!element.IsInteger()) {
1372 return Api::Error("%s expects the argument 'list' to be "
1373 "a List of int", CURRENT_FUNC);
1374 }
1375 integer ^= element.raw();
1376 native_array[i] = static_cast<uint8_t>(integer.AsInt64Value() & 0xff);
1377 ASSERT(integer.AsInt64Value() <= 0xff);
1378 // TODO(hpayer): value should always be smaller then 0xff. Add error
1379 // handling.
1380 }
1381 return Api::Success();
1382 }
1383 return Api::Error("Invalid length passed in to access array elements");
1384 }
1385 // TODO(5526318): Make access to GrowableObjectArray more efficient.
1386 // Now check and handle a dart object that implements the List interface.
1387 const Instance& instance = Instance::Handle(GetListInstance(isolate, obj));
1388 if (!instance.IsNull()) {
1389 String& name = String::Handle(String::New("[]"));
1390 const Function& function = Function::Handle(
1391 Resolver::ResolveDynamic(instance, name, 2, 0));
1392 if (!function.IsNull()) {
1393 Object& element = Object::Handle();
1394 Integer& intobj = Integer::Handle();
1362 Dart_Handle result; 1395 Dart_Handle result;
1363 for (int i = 0; i < length; i++) { 1396 for (int i = 0; i < length; i++) {
1364 indexobj = Integer::New(offset + i); 1397 intobj = Integer::New(offset + i);
1365 valueobj = Integer::New(native_array[i]); 1398 element = GetListAt(isolate, instance, intobj, function, &result);
1366 SetListAt(isolate, instance, indexobj, valueobj, function, &result);
1367 if (Dart_IsError(result)) { 1399 if (Dart_IsError(result)) {
1368 return result; // Error condition. 1400 return result; // Error condition.
1369 } 1401 }
1402 if (!element.IsInteger()) {
1403 return Api::Error("%s expects the argument 'list' to be "
1404 "a List of int", CURRENT_FUNC);
1405 }
1406 intobj ^= element.raw();
1407 ASSERT(intobj.AsInt64Value() <= 0xff);
1408 // TODO(hpayer): value should always be smaller then 0xff. Add error
1409 // handling.
1410 native_array[i] = static_cast<uint8_t>(intobj.AsInt64Value() & 0xff);
1370 } 1411 }
1371 return Api::Success(); 1412 return Api::Success();
1372 } 1413 }
1373 } 1414 }
1374 return Api::Error("Object does not implement the 'List' interface"); 1415 return Api::Error("Object does not implement the 'List' interface");
1375 } 1416 }
1376 1417
1377 1418
1378 DART_EXPORT Dart_Handle Dart_ListSetAt(Dart_Handle list, 1419 DART_EXPORT Dart_Handle Dart_ListSetAsBytes(Dart_Handle list,
1379 intptr_t index, 1420 intptr_t offset,
1380 Dart_Handle value) { 1421 uint8_t* native_array,
1422 intptr_t length) {
1381 Isolate* isolate = Isolate::Current(); 1423 Isolate* isolate = Isolate::Current();
1382 DARTSCOPE(isolate); 1424 DARTSCOPE(isolate);
1383 const Object& obj = Object::Handle(Api::UnwrapHandle(list)); 1425 const Object& obj = Object::Handle(Api::UnwrapHandle(list));
1384 if (obj.IsArray()) { 1426 if (obj.IsArray()) {
1385 if (obj.IsImmutableArray()) { 1427 if (obj.IsImmutableArray()) {
1386 return Api::Error("Cannot modify immutable array"); 1428 return Api::Error("Cannot modify immutable array");
1387 } 1429 }
1388 Array& array_obj = Array::Handle(); 1430 Array& array_obj = Array::Handle();
1389 array_obj ^= obj.raw(); 1431 array_obj ^= obj.raw();
1390 const Object& value_obj = Object::Handle(Api::UnwrapHandle(value)); 1432 Integer& integer = Integer::Handle();
1391 if ((index >= 0) && (index < array_obj.Length())) { 1433 if ((offset + length) <= array_obj.Length()) {
1392 array_obj.SetAt(index, value_obj); 1434 for (int i = 0; i < length; i++) {
1435 integer = Integer::New(native_array[i]);
1436 array_obj.SetAt(offset + i, integer);
1437 }
1393 return Api::Success(); 1438 return Api::Success();
1394 } 1439 }
1395 return Api::Error("Invalid index passed in to set array element"); 1440 return Api::Error("Invalid length passed in to set array elements");
1396 } 1441 }
1397 // TODO(5526318): Make access to GrowableObjectArray more efficient. 1442 // TODO(5526318): Make access to GrowableObjectArray more efficient.
1398 // Now check and handle a dart object that implements the List interface. 1443 // Now check and handle a dart object that implements the List interface.
1399 const Instance& instance = Instance::Handle(GetListInstance(isolate, obj)); 1444 const Instance& instance = Instance::Handle(GetListInstance(isolate, obj));
1400 if (!instance.IsNull()) { 1445 if (!instance.IsNull()) {
1401 String& name = String::Handle(String::New("[]=")); 1446 String& name = String::Handle(String::New("[]="));
1402 const Function& function = Function::Handle( 1447 const Function& function = Function::Handle(
1403 Resolver::ResolveDynamic(instance, name, 3, 0)); 1448 Resolver::ResolveDynamic(instance, name, 3, 0));
1404 if (!function.IsNull()) { 1449 if (!function.IsNull()) {
1450 Integer& indexobj = Integer::Handle();
1451 Integer& valueobj = Integer::Handle();
1405 Dart_Handle result; 1452 Dart_Handle result;
1406 const Integer& index_obj = Integer::Handle(Integer::New(index)); 1453 for (int i = 0; i < length; i++) {
1407 const Object& value_obj = Object::Handle(Api::UnwrapHandle(value)); 1454 indexobj = Integer::New(offset + i);
1408 SetListAt(isolate, instance, index_obj, value_obj, function, &result); 1455 valueobj = Integer::New(native_array[i]);
1409 return result; 1456 SetListAt(isolate, instance, indexobj, valueobj, function, &result);
1457 if (Dart_IsError(result)) {
1458 return result; // Error condition.
1459 }
1460 }
1461 return Api::Success();
1410 } 1462 }
1411 } 1463 }
1412 return Api::Error("Object does not implement the 'List' interface"); 1464 return Api::Error("Object does not implement the 'List' interface");
1413 } 1465 }
1414 1466
1415 1467
1468 // --- Closures ---
1469
1470
1471 DART_EXPORT bool Dart_IsClosure(Dart_Handle object) {
1472 DARTSCOPE(Isolate::Current());
1473 const Object& obj = Object::Handle(Api::UnwrapHandle(object));
1474 return obj.IsClosure();
1475 }
1476
1477
1416 // NOTE: Need to pass 'result' as a parameter here in order to avoid 1478 // NOTE: Need to pass 'result' as a parameter here in order to avoid
1417 // warning: variable 'result' might be clobbered by 'longjmp' or 'vfork' 1479 // warning: variable 'result' might be clobbered by 'longjmp' or 'vfork'
1418 // which shows up because of the use of setjmp. 1480 // which shows up because of the use of setjmp.
1419 static void InvokeStatic(Isolate* isolate, 1481 static void InvokeClosure(Isolate* isolate,
1420 const Function& function, 1482 const Closure& closure,
1421 GrowableArray<const Object*>& args, 1483 GrowableArray<const Object*>& args,
1422 Dart_Handle* result) { 1484 Dart_Handle* result) {
1423 ASSERT(isolate != NULL); 1485 ASSERT(isolate != NULL);
1424 LongJump* base = isolate->long_jump_base(); 1486 LongJump* base = isolate->long_jump_base();
1425 LongJump jump; 1487 LongJump jump;
1426 isolate->set_long_jump_base(&jump); 1488 isolate->set_long_jump_base(&jump);
1427 if (setjmp(*jump.Set()) == 0) { 1489 if (setjmp(*jump.Set()) == 0) {
1428 const Array& kNoArgumentNames = Array::Handle(); 1490 const Array& kNoArgumentNames = Array::Handle();
1429 const Instance& retval = Instance::Handle( 1491 const Instance& retval = Instance::Handle(
1430 DartEntry::InvokeStatic(function, args, kNoArgumentNames)); 1492 DartEntry::InvokeClosure(closure, args, kNoArgumentNames));
1431 if (retval.IsUnhandledException()) { 1493 if (retval.IsUnhandledException()) {
1432 *result = Api::ErrorFromException(retval); 1494 *result = Api::ErrorFromException(retval);
1433 } else { 1495 } else {
1434 *result = Api::NewLocalHandle(retval); 1496 *result = Api::NewLocalHandle(retval);
1435 } 1497 }
1436 } else { 1498 } else {
1437 SetupErrorResult(result); 1499 SetupErrorResult(result);
1438 } 1500 }
1439 isolate->set_long_jump_base(base); 1501 isolate->set_long_jump_base(base);
1440 } 1502 }
1441 1503
1442 1504
1505 DART_EXPORT Dart_Handle Dart_InvokeClosure(Dart_Handle closure,
1506 int number_of_arguments,
1507 Dart_Handle* arguments) {
1508 Isolate* isolate = Isolate::Current();
1509 DARTSCOPE(isolate);
1510 const Object& obj = Object::Handle(Api::UnwrapHandle(closure));
1511 if (obj.IsNull()) {
1512 return Api::Error("Null object passed in to invoke closure");
1513 }
1514 if (!obj.IsClosure()) {
1515 return Api::Error("Invalid closure passed to invoke closure");
1516 }
1517 ASSERT(ClassFinalizer::AllClassesFinalized());
1518
1519 // Now try to invoke the closure.
1520 Closure& closure_obj = Closure::Handle();
1521 closure_obj ^= obj.raw();
1522 Dart_Handle retval;
1523 GrowableArray<const Object*> dart_arguments(number_of_arguments);
1524 for (int i = 0; i < number_of_arguments; i++) {
1525 const Object& arg = Object::Handle(Api::UnwrapHandle(arguments[i]));
1526 dart_arguments.Add(&arg);
1527 }
1528 InvokeClosure(isolate, closure_obj, dart_arguments, &retval);
1529 return retval;
1530 }
1531
1532
1533 DART_EXPORT int64_t Dart_ClosureSmrck(Dart_Handle object) {
1534 DARTSCOPE(Isolate::Current());
1535 const Closure& obj = Closure::CheckedHandle(Api::UnwrapHandle(object));
1536 const Integer& smrck = Integer::Handle(obj.smrck());
1537 return smrck.IsNull() ? 0 : smrck.AsInt64Value();
1538 }
1539
1540
1541 DART_EXPORT void Dart_ClosureSetSmrck(Dart_Handle object, int64_t value) {
1542 DARTSCOPE(Isolate::Current());
1543 const Closure& obj = Closure::CheckedHandle(Api::UnwrapHandle(object));
1544 const Integer& smrck = Integer::Handle(Integer::New(value));
1545 obj.set_smrck(smrck);
1546 }
1547
1548
1549 // --- Methods and Fields ---
1550
1551
1443 // NOTE: Need to pass 'result' as a parameter here in order to avoid 1552 // NOTE: Need to pass 'result' as a parameter here in order to avoid
1444 // warning: variable 'result' might be clobbered by 'longjmp' or 'vfork' 1553 // warning: variable 'result' might be clobbered by 'longjmp' or 'vfork'
1445 // which shows up because of the use of setjmp. 1554 // which shows up because of the use of setjmp.
1446 static void InvokeDynamic(Isolate* isolate, 1555 static void InvokeStatic(Isolate* isolate,
1447 const Instance& receiver, 1556 const Function& function,
1448 const Function& function, 1557 GrowableArray<const Object*>& args,
1449 GrowableArray<const Object*>& args, 1558 Dart_Handle* result) {
1450 Dart_Handle* result) {
1451 ASSERT(isolate != NULL); 1559 ASSERT(isolate != NULL);
1452 LongJump* base = isolate->long_jump_base(); 1560 LongJump* base = isolate->long_jump_base();
1453 LongJump jump; 1561 LongJump jump;
1454 isolate->set_long_jump_base(&jump); 1562 isolate->set_long_jump_base(&jump);
1455 if (setjmp(*jump.Set()) == 0) { 1563 if (setjmp(*jump.Set()) == 0) {
1456 const Array& kNoArgumentNames = Array::Handle(); 1564 const Array& kNoArgumentNames = Array::Handle();
1457 const Instance& retval = Instance::Handle( 1565 const Instance& retval = Instance::Handle(
1458 DartEntry::InvokeDynamic(receiver, function, args, kNoArgumentNames)); 1566 DartEntry::InvokeStatic(function, args, kNoArgumentNames));
1459 if (retval.IsUnhandledException()) { 1567 if (retval.IsUnhandledException()) {
1460 *result = Api::ErrorFromException(retval); 1568 *result = Api::ErrorFromException(retval);
1461 } else { 1569 } else {
1462 *result = Api::NewLocalHandle(retval); 1570 *result = Api::NewLocalHandle(retval);
1463 } 1571 }
1464 } else { 1572 } else {
1465 SetupErrorResult(result); 1573 SetupErrorResult(result);
1466 } 1574 }
1467 isolate->set_long_jump_base(base); 1575 isolate->set_long_jump_base(base);
1468 } 1576 }
1469 1577
1470 1578
1471 // NOTE: Need to pass 'result' as a parameter here in order to avoid 1579 // NOTE: Need to pass 'result' as a parameter here in order to avoid
1472 // warning: variable 'result' might be clobbered by 'longjmp' or 'vfork' 1580 // warning: variable 'result' might be clobbered by 'longjmp' or 'vfork'
1473 // which shows up because of the use of setjmp. 1581 // which shows up because of the use of setjmp.
1474 static void InvokeClosure(Isolate* isolate, 1582 static void InvokeDynamic(Isolate* isolate,
1475 const Closure& closure, 1583 const Instance& receiver,
1584 const Function& function,
1476 GrowableArray<const Object*>& args, 1585 GrowableArray<const Object*>& args,
1477 Dart_Handle* result) { 1586 Dart_Handle* result) {
1478 ASSERT(isolate != NULL); 1587 ASSERT(isolate != NULL);
1479 LongJump* base = isolate->long_jump_base(); 1588 LongJump* base = isolate->long_jump_base();
1480 LongJump jump; 1589 LongJump jump;
1481 isolate->set_long_jump_base(&jump); 1590 isolate->set_long_jump_base(&jump);
1482 if (setjmp(*jump.Set()) == 0) { 1591 if (setjmp(*jump.Set()) == 0) {
1483 const Array& kNoArgumentNames = Array::Handle(); 1592 const Array& kNoArgumentNames = Array::Handle();
1484 const Instance& retval = Instance::Handle( 1593 const Instance& retval = Instance::Handle(
1485 DartEntry::InvokeClosure(closure, args, kNoArgumentNames)); 1594 DartEntry::InvokeDynamic(receiver, function, args, kNoArgumentNames));
1486 if (retval.IsUnhandledException()) { 1595 if (retval.IsUnhandledException()) {
1487 *result = Api::ErrorFromException(retval); 1596 *result = Api::ErrorFromException(retval);
1488 } else { 1597 } else {
1489 *result = Api::NewLocalHandle(retval); 1598 *result = Api::NewLocalHandle(retval);
1490 } 1599 }
1491 } else { 1600 } else {
1492 SetupErrorResult(result); 1601 SetupErrorResult(result);
1493 } 1602 }
1494 isolate->set_long_jump_base(base); 1603 isolate->set_long_jump_base(base);
1495 } 1604 }
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
1591 GrowableArray<const Object*> dart_arguments(number_of_arguments); 1700 GrowableArray<const Object*> dart_arguments(number_of_arguments);
1592 for (int i = 0; i < number_of_arguments; i++) { 1701 for (int i = 0; i < number_of_arguments; i++) {
1593 const Object& arg = Object::Handle(Api::UnwrapHandle(arguments[i])); 1702 const Object& arg = Object::Handle(Api::UnwrapHandle(arguments[i]));
1594 dart_arguments.Add(&arg); 1703 dart_arguments.Add(&arg);
1595 } 1704 }
1596 InvokeDynamic(isolate, receiver, function, dart_arguments, &retval); 1705 InvokeDynamic(isolate, receiver, function, dart_arguments, &retval);
1597 return retval; 1706 return retval;
1598 } 1707 }
1599 1708
1600 1709
1601 DART_EXPORT Dart_Handle Dart_InvokeClosure(Dart_Handle closure,
1602 int number_of_arguments,
1603 Dart_Handle* arguments) {
1604 Isolate* isolate = Isolate::Current();
1605 DARTSCOPE(isolate);
1606 const Object& obj = Object::Handle(Api::UnwrapHandle(closure));
1607 if (obj.IsNull()) {
1608 return Api::Error("Null object passed in to invoke closure");
1609 }
1610 if (!obj.IsClosure()) {
1611 return Api::Error("Invalid closure passed to invoke closure");
1612 }
1613 ASSERT(ClassFinalizer::AllClassesFinalized());
1614
1615 // Now try to invoke the closure.
1616 Closure& closure_obj = Closure::Handle();
1617 closure_obj ^= obj.raw();
1618 Dart_Handle retval;
1619 GrowableArray<const Object*> dart_arguments(number_of_arguments);
1620 for (int i = 0; i < number_of_arguments; i++) {
1621 const Object& arg = Object::Handle(Api::UnwrapHandle(arguments[i]));
1622 dart_arguments.Add(&arg);
1623 }
1624 InvokeClosure(isolate, closure_obj, dart_arguments, &retval);
1625 return retval;
1626 }
1627
1628
1629 DART_EXPORT Dart_Handle Dart_GetNativeArgument(Dart_NativeArguments args,
1630 int index) {
1631 DARTSCOPE(Isolate::Current());
1632 NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args);
1633 const Object& obj = Object::Handle(arguments->At(index));
1634 return Api::NewLocalHandle(obj);
1635 }
1636
1637
1638 DART_EXPORT int Dart_GetNativeArgumentCount(Dart_NativeArguments args) {
1639 NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args);
1640 return arguments->Count();
1641 }
1642
1643
1644 DART_EXPORT void Dart_SetReturnValue(Dart_NativeArguments args,
1645 Dart_Handle retval) {
1646 DARTSCOPE(Isolate::Current());
1647 NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args);
1648 arguments->SetReturn(Object::Handle(Api::UnwrapHandle(retval)));
1649 }
1650
1651
1652 DART_EXPORT Dart_Handle Dart_ThrowException(Dart_Handle exception) {
1653 Isolate* isolate = Isolate::Current();
1654 DARTSCOPE(isolate);
1655 if (isolate->top_exit_frame_info() == 0) {
1656 // There are no dart frames on the stack so it would be illegal to
1657 // throw an exception here.
1658 return Api::Error("No Dart frames on stack, cannot throw exception");
1659 }
1660 const Instance& excp = Instance::CheckedHandle(Api::UnwrapHandle(exception));
1661 // Unwind all the API scopes till the exit frame before throwing an
1662 // exception.
1663 ApiState* state = isolate->api_state();
1664 ASSERT(state != NULL);
1665 state->UnwindScopes(isolate->top_exit_frame_info());
1666 Exceptions::Throw(excp);
1667 return Api::Error("Exception was not thrown, internal error");
1668 }
1669
1670
1671 DART_EXPORT Dart_Handle Dart_ReThrowException(Dart_Handle exception,
1672 Dart_Handle stacktrace) {
1673 Isolate* isolate = Isolate::Current();
1674 ASSERT(isolate != NULL);
1675 if (isolate->top_exit_frame_info() == 0) {
1676 // There are no dart frames on the stack so it would be illegal to
1677 // throw an exception here.
1678 return Api::Error("No Dart frames on stack, cannot throw exception");
1679 }
1680 DARTSCOPE(isolate);
1681 const Instance& excp = Instance::CheckedHandle(Api::UnwrapHandle(exception));
1682 const Instance& stk = Instance::CheckedHandle(Api::UnwrapHandle(stacktrace));
1683 // Unwind all the API scopes till the exit frame before throwing an
1684 // exception.
1685 ApiState* state = isolate->api_state();
1686 ASSERT(state != NULL);
1687 state->UnwindScopes(isolate->top_exit_frame_info());
1688 Exceptions::ReThrow(excp, stk);
1689 return Api::Error("Exception was not re thrown, internal error");
1690 }
1691
1692
1693 DART_EXPORT void Dart_EnterScope() {
1694 Isolate* isolate = Isolate::Current();
1695 ASSERT(isolate != NULL);
1696 ApiState* state = isolate->api_state();
1697 ASSERT(state != NULL);
1698 ApiLocalScope* new_scope = new ApiLocalScope(state->top_scope(),
1699 reinterpret_cast<uword>(&state));
1700 ASSERT(new_scope != NULL);
1701 state->set_top_scope(new_scope); // New scope is now the top scope.
1702 }
1703
1704
1705 DART_EXPORT void Dart_ExitScope() {
1706 Isolate* isolate = Isolate::Current();
1707 ASSERT(isolate != NULL);
1708 ApiState* state = isolate->api_state();
1709 ASSERT(state != NULL);
1710 ApiLocalScope* scope = state->top_scope();
1711 ASSERT(scope != NULL);
1712 state->set_top_scope(scope->previous()); // Reset top scope to previous.
1713 delete scope; // Free up the old scope which we have just exited.
1714 }
1715
1716
1717 DART_EXPORT Dart_Handle Dart_NewPersistentHandle(Dart_Handle object) {
1718 Isolate* isolate = Isolate::Current();
1719 DARTSCOPE(isolate);
1720 ApiState* state = isolate->api_state();
1721 ASSERT(state != NULL);
1722 const Object& old_ref = Object::Handle(Api::UnwrapHandle(object));
1723 PersistentHandle* new_ref = state->persistent_handles().AllocateHandle();
1724 new_ref->set_raw(old_ref);
1725 return reinterpret_cast<Dart_Handle>(new_ref);
1726 }
1727
1728
1729 DART_EXPORT Dart_Handle Dart_MakeWeakPersistentHandle(Dart_Handle object) {
1730 UNIMPLEMENTED();
1731 return NULL;
1732 }
1733
1734
1735 DART_EXPORT Dart_Handle Dart_MakePersistentHandle(Dart_Handle object) {
1736 UNIMPLEMENTED();
1737 return NULL;
1738 }
1739
1740
1741 DART_EXPORT void Dart_DeletePersistentHandle(Dart_Handle object) {
1742 Isolate* isolate = Isolate::Current();
1743 ASSERT(isolate != NULL);
1744 ApiState* state = isolate->api_state();
1745 ASSERT(state != NULL);
1746 PersistentHandle* ref = Api::UnwrapAsPersistentHandle(*state, object);
1747 ASSERT(!ref->IsProtected());
1748 if (!ref->IsProtected()) {
1749 state->persistent_handles().FreeHandle(ref);
1750 }
1751 }
1752
1753
1754 static const bool kGetter = true; 1710 static const bool kGetter = true;
1755 static const bool kSetter = false; 1711 static const bool kSetter = false;
1756 1712
1757 1713
1758 static bool UseGetterForStaticField(const Field& fld) { 1714 static bool UseGetterForStaticField(const Field& fld) {
1759 if (fld.IsNull()) { 1715 if (fld.IsNull()) {
1760 return true; 1716 return true;
1761 } 1717 }
1762 1718
1763 // Return getter method for uninitialized fields, rather than the 1719 // Return getter method for uninitialized fields, rather than the
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after
1991 Instance& object = Instance::Handle(); 1947 Instance& object = Instance::Handle();
1992 object ^= param.raw(); 1948 object ^= param.raw();
1993 if (!object.IsValidNativeIndex(index)) { 1949 if (!object.IsValidNativeIndex(index)) {
1994 return Api::Error("Invalid index passed in to set native instance field"); 1950 return Api::Error("Invalid index passed in to set native instance field");
1995 } 1951 }
1996 object.SetNativeField(index, value); 1952 object.SetNativeField(index, value);
1997 return Api::Success(); 1953 return Api::Success();
1998 } 1954 }
1999 1955
2000 1956
2001 static uint8_t* ApiAllocator(uint8_t* ptr, 1957 // --- Exceptions ----
2002 intptr_t old_size, 1958
2003 intptr_t new_size) { 1959
2004 uword new_ptr = Api::Reallocate(reinterpret_cast<uword>(ptr), 1960 DART_EXPORT Dart_Handle Dart_ThrowException(Dart_Handle exception) {
2005 old_size, 1961 Isolate* isolate = Isolate::Current();
2006 new_size); 1962 DARTSCOPE(isolate);
2007 return reinterpret_cast<uint8_t*>(new_ptr); 1963 if (isolate->top_exit_frame_info() == 0) {
2008 } 1964 // There are no dart frames on the stack so it would be illegal to
2009 1965 // throw an exception here.
2010 1966 return Api::Error("No Dart frames on stack, cannot throw exception");
2011 DART_EXPORT Dart_Handle Dart_CreateSnapshot(uint8_t** snapshot_buffer, 1967 }
2012 intptr_t* snapshot_size) { 1968 const Instance& excp = Instance::CheckedHandle(Api::UnwrapHandle(exception));
2013 Isolate* isolate = Isolate::Current(); 1969 // Unwind all the API scopes till the exit frame before throwing an
2014 DARTSCOPE(isolate); 1970 // exception.
2015 if (snapshot_buffer == NULL || snapshot_size == NULL) { 1971 ApiState* state = isolate->api_state();
2016 return Api::Error("Invalid input parameters to Dart_CreateSnapshot"); 1972 ASSERT(state != NULL);
2017 } 1973 state->UnwindScopes(isolate->top_exit_frame_info());
2018 const char* msg = CheckIsolateState(isolate, 1974 Exceptions::Throw(excp);
2019 ClassFinalizer::kGeneratingSnapshot); 1975 return Api::Error("Exception was not thrown, internal error");
1976 }
1977
1978
1979 DART_EXPORT Dart_Handle Dart_ReThrowException(Dart_Handle exception,
1980 Dart_Handle stacktrace) {
1981 Isolate* isolate = Isolate::Current();
1982 ASSERT(isolate != NULL);
1983 if (isolate->top_exit_frame_info() == 0) {
1984 // There are no dart frames on the stack so it would be illegal to
1985 // throw an exception here.
1986 return Api::Error("No Dart frames on stack, cannot throw exception");
1987 }
1988 DARTSCOPE(isolate);
1989 const Instance& excp = Instance::CheckedHandle(Api::UnwrapHandle(exception));
1990 const Instance& stk = Instance::CheckedHandle(Api::UnwrapHandle(stacktrace));
1991 // Unwind all the API scopes till the exit frame before throwing an
1992 // exception.
1993 ApiState* state = isolate->api_state();
1994 ASSERT(state != NULL);
1995 state->UnwindScopes(isolate->top_exit_frame_info());
1996 Exceptions::ReThrow(excp, stk);
1997 return Api::Error("Exception was not re thrown, internal error");
1998 }
1999
2000
2001 // --- Native functions ---
2002
2003
2004 DART_EXPORT Dart_Handle Dart_GetNativeArgument(Dart_NativeArguments args,
2005 int index) {
2006 DARTSCOPE(Isolate::Current());
2007 NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args);
2008 const Object& obj = Object::Handle(arguments->At(index));
2009 return Api::NewLocalHandle(obj);
2010 }
2011
2012
2013 DART_EXPORT int Dart_GetNativeArgumentCount(Dart_NativeArguments args) {
2014 NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args);
2015 return arguments->Count();
2016 }
2017
2018
2019 DART_EXPORT void Dart_SetReturnValue(Dart_NativeArguments args,
2020 Dart_Handle retval) {
2021 DARTSCOPE(Isolate::Current());
2022 NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args);
2023 arguments->SetReturn(Object::Handle(Api::UnwrapHandle(retval)));
2024 }
2025
2026
2027 // --- Scripts and Libraries ---
2028
2029
2030 // NOTE: Need to pass 'result' as a parameter here in order to avoid
2031 // warning: variable 'result' might be clobbered by 'longjmp' or 'vfork'
2032 // which shows up because of the use of setjmp.
2033 static void CompileSource(Isolate* isolate,
2034 const Library& lib,
2035 const String& url,
2036 const String& source,
2037 RawScript::Kind kind,
2038 Dart_Handle* result) {
2039 bool update_lib_status = (kind == RawScript::kScript ||
2040 kind == RawScript::kLibrary);
2041 if (update_lib_status) {
2042 lib.SetLoadInProgress();
2043 }
2044 const Script& script = Script::Handle(Script::New(url, source, kind));
2045 ASSERT(isolate != NULL);
2046 LongJump* base = isolate->long_jump_base();
2047 LongJump jump;
2048 isolate->set_long_jump_base(&jump);
2049 if (setjmp(*jump.Set()) == 0) {
2050 Compiler::Compile(lib, script);
2051 *result = Api::NewLocalHandle(lib);
2052 if (update_lib_status) {
2053 lib.SetLoaded();
2054 }
2055 } else {
2056 SetupErrorResult(result);
2057 if (update_lib_status) {
2058 lib.SetLoadError();
2059 }
2060 }
2061 isolate->set_long_jump_base(base);
2062 }
2063
2064
2065 DART_EXPORT Dart_Handle Dart_LoadScript(Dart_Handle url,
2066 Dart_Handle source,
2067 Dart_LibraryTagHandler handler) {
2068 Isolate* isolate = Isolate::Current();
2069 DARTSCOPE(isolate);
2070 TIMERSCOPE(time_script_loading);
2071 const String& url_str = Api::UnwrapStringHandle(url);
2072 if (url_str.IsNull()) {
2073 RETURN_TYPE_ERROR(url, String);
2074 }
2075 const String& source_str = Api::UnwrapStringHandle(source);
2076 if (source_str.IsNull()) {
2077 RETURN_TYPE_ERROR(source, String);
2078 }
2079 Library& library = Library::Handle(isolate->object_store()->root_library());
2080 if (!library.IsNull()) {
2081 const String& library_url = String::Handle(library.url());
2082 return Api::Error("%s: A script has already been loaded from '%s'.",
2083 CURRENT_FUNC, library_url.ToCString());
2084 }
2085 isolate->set_library_tag_handler(handler);
2086 library = Library::New(url_str);
2087 library.Register();
2088 isolate->object_store()->set_root_library(library);
2089 Dart_Handle result;
2090 CompileSource(isolate,
2091 library,
2092 url_str,
2093 source_str,
2094 RawScript::kScript,
2095 &result);
2096 return result;
2097 }
2098
2099
2100 DEFINE_FLAG(bool, compile_all, false, "Eagerly compile all code.");
2101
2102 static void CompileAll(Isolate* isolate, Dart_Handle* result) {
2103 *result = Api::Success();
2104 if (FLAG_compile_all) {
2105 ASSERT(isolate != NULL);
2106 LongJump* base = isolate->long_jump_base();
2107 LongJump jump;
2108 isolate->set_long_jump_base(&jump);
2109 if (setjmp(*jump.Set()) == 0) {
2110 Library::CompileAll();
2111 } else {
2112 SetupErrorResult(result);
2113 }
2114 isolate->set_long_jump_base(base);
2115 }
2116 }
2117
2118
2119 DART_EXPORT Dart_Handle Dart_CompileAll() {
2120 Isolate* isolate = Isolate::Current();
2121 DARTSCOPE(isolate);
2122 Dart_Handle result;
2123 const char* msg = CheckIsolateState(isolate);
2020 if (msg != NULL) { 2124 if (msg != NULL) {
2021 return Api::Error(msg); 2125 return Api::Error(msg);
2022 } 2126 }
2023 // Since this is only a snapshot the root library should not be set. 2127 CompileAll(isolate, &result);
2024 isolate->object_store()->set_root_library(Library::Handle()); 2128 return result;
2025 SnapshotWriter writer(true, snapshot_buffer, ApiAllocator); 2129 }
2026 writer.WriteFullSnapshot(); 2130
2027 *snapshot_size = writer.Size(); 2131
2132 DART_EXPORT bool Dart_IsLibrary(Dart_Handle object) {
2133 DARTSCOPE(Isolate::Current());
2134 const Object& obj = Object::Handle(Api::UnwrapHandle(object));
2135 return obj.IsLibrary();
2136 }
2137
2138
2139 DART_EXPORT Dart_Handle Dart_GetClass(Dart_Handle library, Dart_Handle name) {
2140 DARTSCOPE(Isolate::Current());
2141 const Object& param = Object::Handle(Api::UnwrapHandle(name));
2142 if (param.IsNull() || !param.IsString()) {
2143 return Api::Error("Invalid class name specified");
2144 }
2145 const Library& lib = Library::CheckedHandle(Api::UnwrapHandle(library));
2146 if (lib.IsNull()) {
2147 return Api::Error("Invalid parameter, Unknown library specified");
2148 }
2149 String& cls_name = String::Handle();
2150 cls_name ^= param.raw();
2151 const Class& cls = Class::Handle(lib.LookupClass(cls_name));
2152 if (cls.IsNull()) {
2153 const String& lib_name = String::Handle(lib.name());
2154 return Api::Error("Class '%s' not found in library '%s'.",
2155 cls_name.ToCString(), lib_name.ToCString());
2156 }
2157 return Api::NewLocalHandle(cls);
2158 }
2159
2160
2161 DART_EXPORT Dart_Handle Dart_LibraryUrl(Dart_Handle library) {
2162 DARTSCOPE(Isolate::Current());
2163 const Library& lib = Api::UnwrapLibraryHandle(library);
2164 if (lib.IsNull()) {
2165 RETURN_TYPE_ERROR(library, Library);
2166 }
2167 const String& url = String::Handle(lib.url());
2168 ASSERT(!url.IsNull());
2169 return Api::NewLocalHandle(url);
2170 }
2171
2172
2173 DART_EXPORT Dart_Handle Dart_LookupLibrary(Dart_Handle url) {
2174 DARTSCOPE(Isolate::Current());
2175 const String& url_str = Api::UnwrapStringHandle(url);
2176 if (url_str.IsNull()) {
2177 RETURN_TYPE_ERROR(url, String);
2178 }
2179 const Library& library = Library::Handle(Library::LookupLibrary(url_str));
2180 if (library.IsNull()) {
2181 return Api::Error("%s: library '%s' not found.",
2182 CURRENT_FUNC, url_str.ToCString());
2183 } else {
2184 return Api::NewLocalHandle(library);
2185 }
2186 }
2187
2188
2189 DART_EXPORT Dart_Handle Dart_LoadLibrary(Dart_Handle url, Dart_Handle source) {
2190 Isolate* isolate = Isolate::Current();
2191 DARTSCOPE(isolate);
2192 const String& url_str = Api::UnwrapStringHandle(url);
2193 if (url_str.IsNull()) {
2194 RETURN_TYPE_ERROR(url, String);
2195 }
2196 const String& source_str = Api::UnwrapStringHandle(source);
2197 if (source_str.IsNull()) {
2198 RETURN_TYPE_ERROR(source, String);
2199 }
2200 Library& library = Library::Handle(Library::LookupLibrary(url_str));
2201 if (library.IsNull()) {
2202 library = Library::New(url_str);
2203 library.Register();
2204 } else if (!library.LoadNotStarted()) {
2205 // The source for this library has either been loaded or is in the
2206 // process of loading. Return an error.
2207 return Api::Error("%s: library '%s' has already been loaded.",
2208 CURRENT_FUNC, url_str.ToCString());
2209 }
2210 Dart_Handle result;
2211 CompileSource(isolate,
2212 library,
2213 url_str,
2214 source_str,
2215 RawScript::kLibrary,
2216 &result);
2217 return result;
2218 }
2219
2220
2221 DART_EXPORT Dart_Handle Dart_LibraryImportLibrary(Dart_Handle library,
2222 Dart_Handle import) {
2223 DARTSCOPE(Isolate::Current());
2224 const Library& library_vm = Api::UnwrapLibraryHandle(library);
2225 if (library_vm.IsNull()) {
2226 RETURN_TYPE_ERROR(library, Library);
2227 }
2228 const Library& import_vm = Api::UnwrapLibraryHandle(import);
2229 if (import_vm.IsNull()) {
2230 RETURN_TYPE_ERROR(import, Library);
2231 }
2232 library_vm.AddImport(import_vm);
2028 return Api::Success(); 2233 return Api::Success();
2029 } 2234 }
2030 2235
2031 2236
2032 static uint8_t* allocator(uint8_t* ptr, intptr_t old_size, intptr_t new_size) { 2237 DART_EXPORT Dart_Handle Dart_LoadSource(Dart_Handle library,
2033 void* new_ptr = realloc(reinterpret_cast<void*>(ptr), new_size); 2238 Dart_Handle url,
2034 return reinterpret_cast<uint8_t*>(new_ptr); 2239 Dart_Handle source) {
2035 } 2240 Isolate* isolate = Isolate::Current();
2036 2241 DARTSCOPE(isolate);
2037 2242 const Library& lib = Api::UnwrapLibraryHandle(library);
2038 DART_EXPORT bool Dart_PostIntArray(Dart_Port port, 2243 if (lib.IsNull()) {
2039 intptr_t len, 2244 RETURN_TYPE_ERROR(library, Library);
2040 intptr_t* data) { 2245 }
2041 uint8_t* buffer = NULL; 2246 const String& url_str = Api::UnwrapStringHandle(url);
2042 MessageWriter writer(&buffer, &allocator); 2247 if (url_str.IsNull()) {
2043 2248 RETURN_TYPE_ERROR(url, String);
2044 writer.WriteMessage(len, data); 2249 }
2045 2250 const String& source_str = Api::UnwrapStringHandle(source);
2046 // Post the message at the given port. 2251 if (source_str.IsNull()) {
2047 return PortMap::PostMessage(port, kNoReplyPort, buffer); 2252 RETURN_TYPE_ERROR(source, String);
2048 } 2253 }
2049 2254 Dart_Handle result;
2050 2255 CompileSource(isolate, lib, url_str, source_str, RawScript::kSource, &result);
2051 DART_EXPORT bool Dart_Post(Dart_Port port, Dart_Handle handle) { 2256 return result;
2052 DARTSCOPE(Isolate::Current()); 2257 }
2053 const Object& object = Object::Handle(Api::UnwrapHandle(handle)); 2258
2054 uint8_t* data = NULL; 2259
2055 SnapshotWriter writer(false, &data, &allocator); 2260 DART_EXPORT Dart_Handle Dart_SetNativeResolver(
2056 writer.WriteObject(object.raw()); 2261 Dart_Handle library,
2057 writer.FinalizeBuffer(); 2262 Dart_NativeEntryResolver resolver) {
2058 return PortMap::PostMessage(port, kNoReplyPort, data); 2263 DARTSCOPE(Isolate::Current());
2059 } 2264 const Library& lib = Api::UnwrapLibraryHandle(library);
2265 if (lib.IsNull()) {
2266 RETURN_TYPE_ERROR(library, Library);
2267 }
2268 lib.set_native_entry_resolver(resolver);
2269 return Api::Success();
2270 }
2271
2272
2273 // --- Profiling support ----
2060 2274
2061 2275
2062 DART_EXPORT void Dart_InitPprofSupport() { 2276 DART_EXPORT void Dart_InitPprofSupport() {
2063 DebugInfo* pprof_symbol_generator = DebugInfo::NewGenerator(); 2277 DebugInfo* pprof_symbol_generator = DebugInfo::NewGenerator();
2064 ASSERT(pprof_symbol_generator != NULL); 2278 ASSERT(pprof_symbol_generator != NULL);
2065 Dart::set_pprof_symbol_generator(pprof_symbol_generator); 2279 Dart::set_pprof_symbol_generator(pprof_symbol_generator);
2066 } 2280 }
2067 2281
2068 2282
2069 DART_EXPORT void Dart_GetPprofSymbolInfo(void** buffer, int* buffer_size) { 2283 DART_EXPORT void Dart_GetPprofSymbolInfo(void** buffer, int* buffer_size) {
(...skipping 10 matching lines...) Expand all
2080 *buffer = NULL; 2294 *buffer = NULL;
2081 } 2295 }
2082 delete debug_region; 2296 delete debug_region;
2083 } else { 2297 } else {
2084 *buffer = NULL; 2298 *buffer = NULL;
2085 *buffer_size = 0; 2299 *buffer_size = 0;
2086 } 2300 }
2087 } 2301 }
2088 2302
2089 2303
2090 DART_EXPORT bool Dart_IsVMFlagSet(const char* flag_name) {
2091 if (Flags::Lookup(flag_name) != NULL) {
2092 return true;
2093 }
2094 return false;
2095 }
2096
2097
2098 Dart_Handle Api::NewLocalHandle(const Object& object) {
2099 Isolate* isolate = Isolate::Current();
2100 ASSERT(isolate != NULL);
2101 ApiState* state = isolate->api_state();
2102 ASSERT(state != NULL);
2103 ApiLocalScope* scope = state->top_scope();
2104 ASSERT(scope != NULL);
2105 LocalHandles* local_handles = scope->local_handles();
2106 ASSERT(local_handles != NULL);
2107 LocalHandle* ref = local_handles->AllocateHandle();
2108 ref->set_raw(object);
2109 return reinterpret_cast<Dart_Handle>(ref);
2110 }
2111
2112 RawObject* Api::UnwrapHandle(Dart_Handle object) {
2113 #ifdef DEBUG
2114 Isolate* isolate = Isolate::Current();
2115 ASSERT(isolate != NULL);
2116 ApiState* state = isolate->api_state();
2117 ASSERT(state != NULL);
2118 ASSERT(state->IsValidPersistentHandle(object) ||
2119 state->IsValidLocalHandle(object));
2120 ASSERT(PersistentHandle::raw_offset() == 0 &&
2121 LocalHandle::raw_offset() == 0);
2122 #endif
2123 return *(reinterpret_cast<RawObject**>(object));
2124 }
2125
2126 #define DEFINE_UNWRAP(Type) \
2127 const Type& Api::Unwrap##Type##Handle(Dart_Handle dart_handle) { \
2128 const Object& tmp = Object::Handle(Api::UnwrapHandle(dart_handle)); \
2129 Type& typed_handle = Type::Handle(); \
2130 if (tmp.Is##Type()) { \
2131 typed_handle ^= tmp.raw(); \
2132 } \
2133 return typed_handle; \
2134 }
2135 CLASS_LIST_NO_OBJECT(DEFINE_UNWRAP)
2136 #undef DEFINE_UNWRAP
2137
2138
2139 LocalHandle* Api::UnwrapAsLocalHandle(const ApiState& state,
2140 Dart_Handle object) {
2141 ASSERT(state.IsValidLocalHandle(object));
2142 return reinterpret_cast<LocalHandle*>(object);
2143 }
2144
2145
2146 PersistentHandle* Api::UnwrapAsPersistentHandle(const ApiState& state,
2147 Dart_Handle object) {
2148 ASSERT(state.IsValidPersistentHandle(object));
2149 return reinterpret_cast<PersistentHandle*>(object);
2150 }
2151
2152
2153 Dart_Handle Api::Success() {
2154 Isolate* isolate = Isolate::Current();
2155 ASSERT(isolate != NULL);
2156 ApiState* state = isolate->api_state();
2157 ASSERT(state != NULL);
2158 PersistentHandle* true_handle = state->True();
2159 return reinterpret_cast<Dart_Handle>(true_handle);
2160 }
2161
2162
2163 Dart_Handle Api::Error(const char* format, ...) {
2164 DARTSCOPE(Isolate::Current());
2165
2166 va_list args;
2167 va_start(args, format);
2168 intptr_t len = OS::VSNPrint(NULL, 0, format, args);
2169 va_end(args);
2170
2171 char* buffer = reinterpret_cast<char*>(zone.Allocate(len + 1));
2172 va_list args2;
2173 va_start(args2, format);
2174 OS::VSNPrint(buffer, (len + 1), format, args2);
2175 va_end(args2);
2176
2177 const String& message = String::Handle(String::New(buffer));
2178 const Object& obj = Object::Handle(ApiError::New(message));
2179 return Api::NewLocalHandle(obj);
2180 }
2181
2182
2183 Dart_Handle Api::ErrorFromException(const Object& obj) {
2184 DARTSCOPE(Isolate::Current());
2185
2186 ASSERT(obj.IsUnhandledException());
2187 if (obj.IsUnhandledException()) {
2188 UnhandledException& uhe = UnhandledException::Handle();
2189 uhe ^= obj.raw();
2190 const Object& error = Object::Handle(ApiError::New(uhe));
2191 return Api::NewLocalHandle(error);
2192 } else {
2193 return Api::Error("Internal error: expected obj.IsUnhandledException().");
2194 }
2195 }
2196
2197
2198 Dart_Handle Api::Null() {
2199 Isolate* isolate = Isolate::Current();
2200 ASSERT(isolate != NULL);
2201 ApiState* state = isolate->api_state();
2202 ASSERT(state != NULL);
2203 PersistentHandle* null_handle = state->Null();
2204 return reinterpret_cast<Dart_Handle>(null_handle);
2205 }
2206
2207
2208 Dart_Handle Api::True() {
2209 Isolate* isolate = Isolate::Current();
2210 ASSERT(isolate != NULL);
2211 ApiState* state = isolate->api_state();
2212 ASSERT(state != NULL);
2213 PersistentHandle* true_handle = state->True();
2214 return reinterpret_cast<Dart_Handle>(true_handle);
2215 }
2216
2217
2218 Dart_Handle Api::False() {
2219 Isolate* isolate = Isolate::Current();
2220 ASSERT(isolate != NULL);
2221 ApiState* state = isolate->api_state();
2222 ASSERT(state != NULL);
2223 PersistentHandle* false_handle = state->False();
2224 return reinterpret_cast<Dart_Handle>(false_handle);
2225 }
2226
2227
2228 uword Api::Allocate(intptr_t size) {
2229 Isolate* isolate = Isolate::Current();
2230 ASSERT(isolate != NULL);
2231 ApiState* state = isolate->api_state();
2232 ASSERT(state != NULL);
2233 ApiLocalScope* scope = state->top_scope();
2234 ASSERT(scope != NULL);
2235 return scope->zone().Allocate(size);
2236 }
2237
2238
2239 uword Api::Reallocate(uword ptr, intptr_t old_size, intptr_t new_size) {
2240 Isolate* isolate = Isolate::Current();
2241 ASSERT(isolate != NULL);
2242 ApiState* state = isolate->api_state();
2243 ASSERT(state != NULL);
2244 ApiLocalScope* scope = state->top_scope();
2245 ASSERT(scope != NULL);
2246 return scope->zone().Reallocate(ptr, old_size, new_size);
2247 }
2248
2249
2250 } // namespace dart 2304 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/include/dart_api.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698