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

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

Issue 8492015: Allow printf-style arguments for Dart_Error and Api::Error. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: '' Created 9 years, 1 month 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/vm/dart_api_impl.h ('k') | runtime/vm/dart_api_impl_test.cc » ('j') | 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 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
60 failure ^= obj.raw(); 60 failure ^= obj.raw();
61 const String& message = String::Handle(failure.message()); 61 const String& message = String::Handle(failure.message());
62 const char* msg = message.ToCString(); 62 const char* msg = message.ToCString();
63 intptr_t len = strlen(msg) + 1; 63 intptr_t len = strlen(msg) + 1;
64 char* msg_copy = reinterpret_cast<char*>(Api::Allocate(len)); 64 char* msg_copy = reinterpret_cast<char*>(Api::Allocate(len));
65 OS::SNPrint(msg_copy, len, "%s", msg); 65 OS::SNPrint(msg_copy, len, "%s", msg);
66 return msg_copy; 66 return msg_copy;
67 } 67 }
68 68
69 69
70 DART_EXPORT Dart_Handle Dart_Error(const char* error) { 70 DART_EXPORT Dart_Handle Dart_Error(const char* format, ...) {
71 return Api::Error(error); 71 va_list args;
72 va_start(args, format);
73 Dart_Handle error = Api::VError(format, args);
74 va_end(args);
75 return error;
72 } 76 }
73 77
74 78
75 // TODO(iposva): This is a placeholder for the eventual external Dart API. 79 // TODO(iposva): This is a placeholder for the eventual external Dart API.
76 DART_EXPORT bool Dart_Initialize(int argc, 80 DART_EXPORT bool Dart_Initialize(int argc,
77 char** argv, 81 char** argv,
78 Dart_IsolateInitCallback callback) { 82 Dart_IsolateInitCallback callback) {
79 return Dart::InitOnce(argc, argv, callback); 83 return Dart::InitOnce(argc, argv, callback);
80 } 84 }
81 85
(...skipping 438 matching lines...) Expand 10 before | Expand all | Expand 10 after
520 return Api::Error("Invalid class name specified"); 524 return Api::Error("Invalid class name specified");
521 } 525 }
522 const Library& lib = Library::CheckedHandle(Api::UnwrapHandle(library)); 526 const Library& lib = Library::CheckedHandle(Api::UnwrapHandle(library));
523 if (lib.IsNull()) { 527 if (lib.IsNull()) {
524 return Api::Error("Invalid parameter, Unknown library specified"); 528 return Api::Error("Invalid parameter, Unknown library specified");
525 } 529 }
526 String& cls_name = String::Handle(); 530 String& cls_name = String::Handle();
527 cls_name ^= param.raw(); 531 cls_name ^= param.raw();
528 const Class& cls = Class::Handle(lib.LookupClass(cls_name)); 532 const Class& cls = Class::Handle(lib.LookupClass(cls_name));
529 if (cls.IsNull()) { 533 if (cls.IsNull()) {
530 return Api::Error("Specified class does not exist"); 534 const String& lib_name = String::Handle(lib.name());
535 return Api::Error("Class '%s' not found in library '%s'.",
536 cls_name.ToCString(), lib_name.ToCString());
531 } 537 }
532 return Api::NewLocalHandle(cls); 538 return Api::NewLocalHandle(cls);
533 } 539 }
534 540
535 541
536 // TODO(iposva): This call actually implements IsInstanceOfClass. 542 // TODO(iposva): This call actually implements IsInstanceOfClass.
537 // Do we also need a real Dart_IsInstanceOf, which should take an instance 543 // Do we also need a real Dart_IsInstanceOf, which should take an instance
538 // rather than an object and a type rather than a class? 544 // rather than an object and a type rather than a class?
539 DART_EXPORT Dart_Handle Dart_IsInstanceOf(Dart_Handle object, 545 DART_EXPORT Dart_Handle Dart_IsInstanceOf(Dart_Handle object,
540 Dart_Handle clazz, 546 Dart_Handle clazz,
(...skipping 1460 matching lines...) Expand 10 before | Expand all | Expand 10 after
2001 Dart_Handle Api::Success() { 2007 Dart_Handle Api::Success() {
2002 Isolate* isolate = Isolate::Current(); 2008 Isolate* isolate = Isolate::Current();
2003 ASSERT(isolate != NULL); 2009 ASSERT(isolate != NULL);
2004 ApiState* state = isolate->api_state(); 2010 ApiState* state = isolate->api_state();
2005 ASSERT(state != NULL); 2011 ASSERT(state != NULL);
2006 PersistentHandle* true_handle = state->True(); 2012 PersistentHandle* true_handle = state->True();
2007 return reinterpret_cast<Dart_Handle>(true_handle); 2013 return reinterpret_cast<Dart_Handle>(true_handle);
2008 } 2014 }
2009 2015
2010 2016
2011 Dart_Handle Api::Error(const char* text) { 2017 Dart_Handle Api::VError(const char* format, va_list args) {
2012 Zone zone; // Setup a VM zone as we are creating some handles. 2018 Zone zone; // Setup a VM zone as we are creating some handles.
2013 HandleScope scope; // Setup a VM handle scope. 2019 HandleScope scope; // Setup a VM handle scope.
2014 const String& message = String::Handle(String::New(text)); 2020
2021 intptr_t len = OS::VSNPrint(NULL, 0, format, args);
2022 char* buffer = reinterpret_cast<char*>(zone.Allocate(len+1));
2023 OS::VSNPrint(buffer, len+1, format, args);
Anton Muhin 2011/11/08 08:32:18 nit: space around + ?
turnidge 2011/11/08 19:02:02 Done.
2024
2025 const String& message = String::Handle(String::New(buffer));
2015 const Object& obj = Object::Handle(ApiFailure::New(message)); 2026 const Object& obj = Object::Handle(ApiFailure::New(message));
2016 return Api::NewLocalHandle(obj); 2027 return Api::NewLocalHandle(obj);
2017 } 2028 }
2018 2029
2019 2030
2031 Dart_Handle Api::Error(const char* format, ...) {
Anton Muhin 2011/11/08 08:32:18 looks pretty much like Dart_Error. Do we need bot
turnidge 2011/11/08 19:02:02 I am providing Dart_Error for external developers.
2032 va_list args;
2033 va_start(args, format);
2034 Dart_Handle error = Api::VError(format, args);
2035 va_end(args);
2036 return error;
2037 }
2038
2039
2020 Dart_Handle Api::Null() { 2040 Dart_Handle Api::Null() {
2021 Isolate* isolate = Isolate::Current(); 2041 Isolate* isolate = Isolate::Current();
2022 ASSERT(isolate != NULL); 2042 ASSERT(isolate != NULL);
2023 ApiState* state = isolate->api_state(); 2043 ApiState* state = isolate->api_state();
2024 ASSERT(state != NULL); 2044 ASSERT(state != NULL);
2025 PersistentHandle* null_handle = state->Null(); 2045 PersistentHandle* null_handle = state->Null();
2026 return reinterpret_cast<Dart_Handle>(null_handle); 2046 return reinterpret_cast<Dart_Handle>(null_handle);
2027 } 2047 }
2028 2048
2029 2049
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
2063 ASSERT(isolate != NULL); 2083 ASSERT(isolate != NULL);
2064 ApiState* state = isolate->api_state(); 2084 ApiState* state = isolate->api_state();
2065 ASSERT(state != NULL); 2085 ASSERT(state != NULL);
2066 ApiLocalScope* scope = state->top_scope(); 2086 ApiLocalScope* scope = state->top_scope();
2067 ASSERT(scope != NULL); 2087 ASSERT(scope != NULL);
2068 return scope->zone().Reallocate(ptr, old_size, new_size); 2088 return scope->zone().Reallocate(ptr, old_size, new_size);
2069 } 2089 }
2070 2090
2071 2091
2072 } // namespace dart 2092 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/dart_api_impl.h ('k') | runtime/vm/dart_api_impl_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698