| OLD | NEW |
| 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 19 matching lines...) Expand all Loading... |
| 30 static const char* CanonicalFunction(const char* func) { | 30 static const char* CanonicalFunction(const char* func) { |
| 31 if (strncmp(func, "dart::", 6) == 0) { | 31 if (strncmp(func, "dart::", 6) == 0) { |
| 32 return func + 6; | 32 return func + 6; |
| 33 } else { | 33 } else { |
| 34 return func; | 34 return func; |
| 35 } | 35 } |
| 36 } | 36 } |
| 37 | 37 |
| 38 #define CURRENT_FUNC CanonicalFunction(__FUNCTION__) | 38 #define CURRENT_FUNC CanonicalFunction(__FUNCTION__) |
| 39 | 39 |
| 40 #define UNWRAP_NONNULL(dart_handle, vm_handle, Type) \ | 40 #define RETURN_TYPE_ERROR(dart_handle, Type) \ |
| 41 do { \ | 41 do { \ |
| 42 const Object& tmp = Object::Handle(Api::UnwrapHandle((dart_handle))); \ | 42 const Object& tmp = Object::Handle(Api::UnwrapHandle((dart_handle))); \ |
| 43 if (tmp.Is##Type()) { \ | 43 if (tmp.IsNull()) { \ |
| 44 (vm_handle) ^= tmp.raw(); \ | |
| 45 } else if (tmp.IsNull()) { \ | |
| 46 return Api::Error("%s expects argument '%s' to be non-null.", \ | 44 return Api::Error("%s expects argument '%s' to be non-null.", \ |
| 47 CURRENT_FUNC, #dart_handle); \ | 45 CURRENT_FUNC, #dart_handle); \ |
| 48 } else if (tmp.IsApiError()) { \ | 46 } else if (tmp.IsApiError()) { \ |
| 49 return dart_handle; \ | 47 return dart_handle; \ |
| 50 } else { \ | 48 } else { \ |
| 51 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.", \ |
| 52 CURRENT_FUNC, #dart_handle, #Type); \ | 50 CURRENT_FUNC, #dart_handle, #Type); \ |
| 53 } \ | 51 } \ |
| 54 } while (0) | 52 } while (0) |
| 55 | 53 |
| (...skipping 330 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 386 isolate->set_long_jump_base(base); | 384 isolate->set_long_jump_base(base); |
| 387 } | 385 } |
| 388 | 386 |
| 389 | 387 |
| 390 DART_EXPORT Dart_Handle Dart_LoadScript(Dart_Handle url, | 388 DART_EXPORT Dart_Handle Dart_LoadScript(Dart_Handle url, |
| 391 Dart_Handle source, | 389 Dart_Handle source, |
| 392 Dart_LibraryTagHandler handler) { | 390 Dart_LibraryTagHandler handler) { |
| 393 Isolate* isolate = Isolate::Current(); | 391 Isolate* isolate = Isolate::Current(); |
| 394 DARTSCOPE(isolate); | 392 DARTSCOPE(isolate); |
| 395 TIMERSCOPE(time_script_loading); | 393 TIMERSCOPE(time_script_loading); |
| 396 const String& url_str = String::CheckedHandle(Api::UnwrapHandle(url)); | 394 const String& url_str = Api::UnwrapStringHandle(url); |
| 397 const String& source_str = String::CheckedHandle(Api::UnwrapHandle(source)); | 395 if (url_str.IsNull()) { |
| 396 RETURN_TYPE_ERROR(url, String); |
| 397 } |
| 398 const String& source_str = Api::UnwrapStringHandle(source); |
| 399 if (source_str.IsNull()) { |
| 400 RETURN_TYPE_ERROR(source, String); |
| 401 } |
| 398 Library& library = Library::Handle(isolate->object_store()->root_library()); | 402 Library& library = Library::Handle(isolate->object_store()->root_library()); |
| 399 if (!library.IsNull()) { | 403 if (!library.IsNull()) { |
| 400 return Api::Error("Script already loaded"); | 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()); |
| 401 } | 407 } |
| 402 isolate->set_library_tag_handler(handler); | 408 isolate->set_library_tag_handler(handler); |
| 403 library = Library::New(url_str); | 409 library = Library::New(url_str); |
| 404 library.Register(); | 410 library.Register(); |
| 405 isolate->object_store()->set_root_library(library); | 411 isolate->object_store()->set_root_library(library); |
| 406 Dart_Handle result; | 412 Dart_Handle result; |
| 407 CompileSource(isolate, | 413 CompileSource(isolate, |
| 408 library, | 414 library, |
| 409 url_str, | 415 url_str, |
| 410 source_str, | 416 source_str, |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 466 | 472 |
| 467 DART_EXPORT bool Dart_IsLibrary(Dart_Handle object) { | 473 DART_EXPORT bool Dart_IsLibrary(Dart_Handle object) { |
| 468 DARTSCOPE(Isolate::Current()); | 474 DARTSCOPE(Isolate::Current()); |
| 469 const Object& obj = Object::Handle(Api::UnwrapHandle(object)); | 475 const Object& obj = Object::Handle(Api::UnwrapHandle(object)); |
| 470 return obj.IsLibrary(); | 476 return obj.IsLibrary(); |
| 471 } | 477 } |
| 472 | 478 |
| 473 | 479 |
| 474 DART_EXPORT Dart_Handle Dart_LibraryUrl(Dart_Handle library) { | 480 DART_EXPORT Dart_Handle Dart_LibraryUrl(Dart_Handle library) { |
| 475 DARTSCOPE(Isolate::Current()); | 481 DARTSCOPE(Isolate::Current()); |
| 476 const Library& lib = Library::CheckedHandle(Api::UnwrapHandle(library)); | 482 const Library& lib = Api::UnwrapLibraryHandle(library); |
| 477 if (lib.IsNull()) { | 483 if (lib.IsNull()) { |
| 478 return Api::Error("Null library"); | 484 RETURN_TYPE_ERROR(library, Library); |
| 479 } | 485 } |
| 480 const String& url = String::Handle(lib.url()); | 486 const String& url = String::Handle(lib.url()); |
| 481 ASSERT(!url.IsNull()); | 487 ASSERT(!url.IsNull()); |
| 482 return Api::NewLocalHandle(url); | 488 return Api::NewLocalHandle(url); |
| 483 } | 489 } |
| 484 | 490 |
| 485 | 491 |
| 486 DART_EXPORT Dart_Handle Dart_LibraryImportLibrary(Dart_Handle library_in, | 492 DART_EXPORT Dart_Handle Dart_LibraryImportLibrary(Dart_Handle library, |
| 487 Dart_Handle import_in) { | 493 Dart_Handle import) { |
| 488 DARTSCOPE(Isolate::Current()); | 494 DARTSCOPE(Isolate::Current()); |
| 489 const Library& library = | 495 const Library& library_vm = Api::UnwrapLibraryHandle(library); |
| 490 Library::CheckedHandle(Api::UnwrapHandle(library_in)); | 496 if (library_vm.IsNull()) { |
| 491 if (library.IsNull()) { | 497 RETURN_TYPE_ERROR(library, Library); |
| 492 return Api::Error("Null library"); | |
| 493 } | 498 } |
| 494 const Library& import = | 499 const Library& import_vm = Api::UnwrapLibraryHandle(import); |
| 495 Library::CheckedHandle(Api::UnwrapHandle(import_in)); | 500 if (import_vm.IsNull()) { |
| 496 library.AddImport(import); | 501 RETURN_TYPE_ERROR(import, Library); |
| 502 } |
| 503 library_vm.AddImport(import_vm); |
| 497 return Api::Success(); | 504 return Api::Success(); |
| 498 } | 505 } |
| 499 | 506 |
| 500 | 507 |
| 501 DART_EXPORT Dart_Handle Dart_LookupLibrary(Dart_Handle url) { | 508 DART_EXPORT Dart_Handle Dart_LookupLibrary(Dart_Handle url) { |
| 502 DARTSCOPE(Isolate::Current()); | 509 DARTSCOPE(Isolate::Current()); |
| 503 String& url_str = String::Handle(); | 510 const String& url_str = Api::UnwrapStringHandle(url); |
| 504 UNWRAP_NONNULL(url, url_str, String); | 511 if (url_str.IsNull()) { |
| 512 RETURN_TYPE_ERROR(url, String); |
| 513 } |
| 505 const Library& library = Library::Handle(Library::LookupLibrary(url_str)); | 514 const Library& library = Library::Handle(Library::LookupLibrary(url_str)); |
| 506 if (library.IsNull()) { | 515 if (library.IsNull()) { |
| 507 return Api::Error("%s: library '%s' not found.", | 516 return Api::Error("%s: library '%s' not found.", |
| 508 CURRENT_FUNC, url_str.ToCString()); | 517 CURRENT_FUNC, url_str.ToCString()); |
| 509 } else { | 518 } else { |
| 510 return Api::NewLocalHandle(library); | 519 return Api::NewLocalHandle(library); |
| 511 } | 520 } |
| 512 } | 521 } |
| 513 | 522 |
| 514 | 523 |
| 515 DART_EXPORT Dart_Handle Dart_LoadLibrary(Dart_Handle url, Dart_Handle source) { | 524 DART_EXPORT Dart_Handle Dart_LoadLibrary(Dart_Handle url, Dart_Handle source) { |
| 516 Isolate* isolate = Isolate::Current(); | 525 Isolate* isolate = Isolate::Current(); |
| 517 DARTSCOPE(isolate); | 526 DARTSCOPE(isolate); |
| 518 const String& url_str = String::CheckedHandle(Api::UnwrapHandle(url)); | 527 const String& url_str = Api::UnwrapStringHandle(url); |
| 519 const String& source_str = String::CheckedHandle(Api::UnwrapHandle(source)); | 528 if (url_str.IsNull()) { |
| 529 RETURN_TYPE_ERROR(url, String); |
| 530 } |
| 531 const String& source_str = Api::UnwrapStringHandle(source); |
| 532 if (source_str.IsNull()) { |
| 533 RETURN_TYPE_ERROR(source, String); |
| 534 } |
| 520 Library& library = Library::Handle(Library::LookupLibrary(url_str)); | 535 Library& library = Library::Handle(Library::LookupLibrary(url_str)); |
| 521 if (library.IsNull()) { | 536 if (library.IsNull()) { |
| 522 library = Library::New(url_str); | 537 library = Library::New(url_str); |
| 523 library.Register(); | 538 library.Register(); |
| 539 } else if (!library.LoadNotStarted()) { |
| 540 // The source for this library has either been loaded or is in the |
| 541 // process of loading. Return an error. |
| 542 return Api::Error("%s: library '%s' has already been loaded.", |
| 543 CURRENT_FUNC, url_str.ToCString()); |
| 524 } | 544 } |
| 525 Dart_Handle result; | 545 Dart_Handle result; |
| 526 CompileSource(isolate, | 546 CompileSource(isolate, |
| 527 library, | 547 library, |
| 528 url_str, | 548 url_str, |
| 529 source_str, | 549 source_str, |
| 530 RawScript::kLibrary, | 550 RawScript::kLibrary, |
| 531 &result); | 551 &result); |
| 532 return result; | 552 return result; |
| 533 } | 553 } |
| 534 | 554 |
| 535 | 555 |
| 536 DART_EXPORT Dart_Handle Dart_LoadSource(Dart_Handle library_in, | 556 DART_EXPORT Dart_Handle Dart_LoadSource(Dart_Handle library, |
| 537 Dart_Handle url_in, | 557 Dart_Handle url, |
| 538 Dart_Handle source_in) { | 558 Dart_Handle source) { |
| 539 Isolate* isolate = Isolate::Current(); | 559 Isolate* isolate = Isolate::Current(); |
| 540 DARTSCOPE(isolate); | 560 DARTSCOPE(isolate); |
| 541 const String& url = String::CheckedHandle(Api::UnwrapHandle(url_in)); | 561 const Library& lib = Api::UnwrapLibraryHandle(library); |
| 542 const String& source = String::CheckedHandle(Api::UnwrapHandle(source_in)); | 562 if (lib.IsNull()) { |
| 543 const Library& library = | 563 RETURN_TYPE_ERROR(library, Library); |
| 544 Library::CheckedHandle(Api::UnwrapHandle(library_in)); | 564 } |
| 565 const String& url_str = Api::UnwrapStringHandle(url); |
| 566 if (url_str.IsNull()) { |
| 567 RETURN_TYPE_ERROR(url, String); |
| 568 } |
| 569 const String& source_str = Api::UnwrapStringHandle(source); |
| 570 if (source_str.IsNull()) { |
| 571 RETURN_TYPE_ERROR(source, String); |
| 572 } |
| 545 Dart_Handle result; | 573 Dart_Handle result; |
| 546 CompileSource(isolate, library, url, source, RawScript::kSource, &result); | 574 CompileSource(isolate, lib, url_str, source_str, RawScript::kSource, &result); |
| 547 return result; | 575 return result; |
| 548 } | 576 } |
| 549 | 577 |
| 550 | 578 |
| 551 DART_EXPORT Dart_Handle Dart_SetNativeResolver( | 579 DART_EXPORT Dart_Handle Dart_SetNativeResolver( |
| 552 Dart_Handle library, | 580 Dart_Handle library, |
| 553 Dart_NativeEntryResolver resolver) { | 581 Dart_NativeEntryResolver resolver) { |
| 554 DARTSCOPE(Isolate::Current()); | 582 DARTSCOPE(Isolate::Current()); |
| 555 const Library& lib = Library::CheckedHandle(Api::UnwrapHandle(library)); | 583 const Library& lib = Api::UnwrapLibraryHandle(library); |
| 556 if (lib.IsNull()) { | 584 if (lib.IsNull()) { |
| 557 return Api::Error("Invalid parameter, Unknown library specified"); | 585 RETURN_TYPE_ERROR(library, Library); |
| 558 } | 586 } |
| 559 lib.set_native_entry_resolver(resolver); | 587 lib.set_native_entry_resolver(resolver); |
| 560 return Api::Success(); | 588 return Api::Success(); |
| 561 } | 589 } |
| 562 | 590 |
| 563 | 591 |
| 564 DART_EXPORT Dart_Handle Dart_ToString(Dart_Handle object) { | 592 DART_EXPORT Dart_Handle Dart_ToString(Dart_Handle object) { |
| 565 DARTSCOPE(Isolate::Current()); | 593 DARTSCOPE(Isolate::Current()); |
| 566 const Object& obj = Object::Handle(Api::UnwrapHandle(object)); | 594 const Object& obj = Object::Handle(Api::UnwrapHandle(object)); |
| 567 Object& result = Object::Handle(); | 595 Object& result = Object::Handle(); |
| (...skipping 1500 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2068 ASSERT(state != NULL); | 2096 ASSERT(state != NULL); |
| 2069 ApiLocalScope* scope = state->top_scope(); | 2097 ApiLocalScope* scope = state->top_scope(); |
| 2070 ASSERT(scope != NULL); | 2098 ASSERT(scope != NULL); |
| 2071 LocalHandles* local_handles = scope->local_handles(); | 2099 LocalHandles* local_handles = scope->local_handles(); |
| 2072 ASSERT(local_handles != NULL); | 2100 ASSERT(local_handles != NULL); |
| 2073 LocalHandle* ref = local_handles->AllocateHandle(); | 2101 LocalHandle* ref = local_handles->AllocateHandle(); |
| 2074 ref->set_raw(object); | 2102 ref->set_raw(object); |
| 2075 return reinterpret_cast<Dart_Handle>(ref); | 2103 return reinterpret_cast<Dart_Handle>(ref); |
| 2076 } | 2104 } |
| 2077 | 2105 |
| 2078 | |
| 2079 RawObject* Api::UnwrapHandle(Dart_Handle object) { | 2106 RawObject* Api::UnwrapHandle(Dart_Handle object) { |
| 2080 #ifdef DEBUG | 2107 #ifdef DEBUG |
| 2081 Isolate* isolate = Isolate::Current(); | 2108 Isolate* isolate = Isolate::Current(); |
| 2082 ASSERT(isolate != NULL); | 2109 ASSERT(isolate != NULL); |
| 2083 ApiState* state = isolate->api_state(); | 2110 ApiState* state = isolate->api_state(); |
| 2084 ASSERT(state != NULL); | 2111 ASSERT(state != NULL); |
| 2085 ASSERT(state->IsValidPersistentHandle(object) || | 2112 ASSERT(state->IsValidPersistentHandle(object) || |
| 2086 state->IsValidLocalHandle(object)); | 2113 state->IsValidLocalHandle(object)); |
| 2087 ASSERT(PersistentHandle::raw_offset() == 0 && | 2114 ASSERT(PersistentHandle::raw_offset() == 0 && |
| 2088 LocalHandle::raw_offset() == 0); | 2115 LocalHandle::raw_offset() == 0); |
| 2089 #endif | 2116 #endif |
| 2090 return *(reinterpret_cast<RawObject**>(object)); | 2117 return *(reinterpret_cast<RawObject**>(object)); |
| 2091 } | 2118 } |
| 2092 | 2119 |
| 2120 #define DEFINE_UNWRAP(Type) \ |
| 2121 const Type& Api::Unwrap##Type##Handle(Dart_Handle dart_handle) { \ |
| 2122 const Object& tmp = Object::Handle(Api::UnwrapHandle(dart_handle)); \ |
| 2123 Type& typed_handle = Type::Handle(); \ |
| 2124 if (tmp.Is##Type()) { \ |
| 2125 typed_handle ^= tmp.raw(); \ |
| 2126 } \ |
| 2127 return typed_handle; \ |
| 2128 } |
| 2129 CLASS_LIST_NO_OBJECT(DEFINE_UNWRAP) |
| 2130 #undef DEFINE_UNWRAP |
| 2131 |
| 2093 | 2132 |
| 2094 LocalHandle* Api::UnwrapAsLocalHandle(const ApiState& state, | 2133 LocalHandle* Api::UnwrapAsLocalHandle(const ApiState& state, |
| 2095 Dart_Handle object) { | 2134 Dart_Handle object) { |
| 2096 ASSERT(state.IsValidLocalHandle(object)); | 2135 ASSERT(state.IsValidLocalHandle(object)); |
| 2097 return reinterpret_cast<LocalHandle*>(object); | 2136 return reinterpret_cast<LocalHandle*>(object); |
| 2098 } | 2137 } |
| 2099 | 2138 |
| 2100 | 2139 |
| 2101 PersistentHandle* Api::UnwrapAsPersistentHandle(const ApiState& state, | 2140 PersistentHandle* Api::UnwrapAsPersistentHandle(const ApiState& state, |
| 2102 Dart_Handle object) { | 2141 Dart_Handle object) { |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2196 ASSERT(isolate != NULL); | 2235 ASSERT(isolate != NULL); |
| 2197 ApiState* state = isolate->api_state(); | 2236 ApiState* state = isolate->api_state(); |
| 2198 ASSERT(state != NULL); | 2237 ASSERT(state != NULL); |
| 2199 ApiLocalScope* scope = state->top_scope(); | 2238 ApiLocalScope* scope = state->top_scope(); |
| 2200 ASSERT(scope != NULL); | 2239 ASSERT(scope != NULL); |
| 2201 return scope->zone().Reallocate(ptr, old_size, new_size); | 2240 return scope->zone().Reallocate(ptr, old_size, new_size); |
| 2202 } | 2241 } |
| 2203 | 2242 |
| 2204 | 2243 |
| 2205 } // namespace dart | 2244 } // namespace dart |
| OLD | NEW |