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

Unified Diff: runtime/lib/error.cc

Issue 9615035: Generate dynamic type errors according to spec. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | runtime/lib/error.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/lib/error.cc
===================================================================
--- runtime/lib/error.cc (revision 5063)
+++ runtime/lib/error.cc (working copy)
@@ -13,62 +13,7 @@
namespace dart {
DEFINE_FLAG(bool, trace_type_checks, false, "Trace runtime type checks.");
-DECLARE_FLAG(bool, print_stack_trace_at_throw);
-
-// Static helpers for allocating, initializing, and throwing an error instance.
-
-// Return the script of the Dart function that called the native entry or the
-// runtime entry. The frame iterator points to the callee.
-static RawScript* GetCallerScript(DartFrameIterator* iterator) {
- DartFrame* caller_frame = iterator->NextFrame();
- ASSERT(caller_frame != NULL);
- const Function& caller = Function::Handle(caller_frame->LookupDartFunction());
- ASSERT(!caller.IsNull());
- const Class& caller_class = Class::Handle(caller.owner());
- return caller_class.script();
-}
-
-
-// Allocate a new instance of the given class name.
-// TODO(hausner): Rename this NewCoreInstance to call out the fact that
-// the class name is resolved in the core library implicitly?
-static RawInstance* NewInstance(const char* class_name) {
- const String& cls_name = String::Handle(String::NewSymbol(class_name));
- const Library& core_lib = Library::Handle(Library::CoreLibrary());
- Class& cls = Class::Handle(core_lib.LookupClass(cls_name));
- ASSERT(!cls.IsNull());
- // There are no parameterized error types, so no need to set type arguments.
- return Instance::New(cls);
-}
-
-
-// Assign the value to the field given by its name in the given instance.
-static void SetField(const Instance& instance,
- const Class& cls,
- const char* field_name,
- const Object& value) {
- const Field& field = Field::Handle(cls.LookupInstanceField(
- String::Handle(String::NewSymbol(field_name))));
- ASSERT(!field.IsNull());
- instance.SetField(field, value);
-}
-
-
-// Initialize the fields 'url', 'line', and 'column' in the given instance
-// according to the given token location in the given script.
-static void SetLocationFields(const Instance& instance,
- const Class& cls,
- const Script& script,
- intptr_t location) {
- SetField(instance, cls, "url", String::Handle(script.url()));
- intptr_t line, column;
- script.GetTokenLocation(location, &line, &column);
- SetField(instance, cls, "line", Smi::Handle(Smi::New(line)));
- SetField(instance, cls, "column", Smi::Handle(Smi::New(column)));
-}
-
-
// Allocate and throw a new AssertionError.
// Arg0: index of the first token of the failed assertion.
// Arg1: index of the first token after the failed assertion.
@@ -79,21 +24,21 @@
// Allocate a new instance of type AssertionError.
const Instance& assertion_error = Instance::Handle(
- NewInstance("AssertionError"));
+ Exceptions::NewInstance("AssertionError"));
// Initialize 'url', 'line', and 'column' fields.
DartFrameIterator iterator;
iterator.NextFrame(); // Skip native call.
- const Script& script = Script::Handle(GetCallerScript(&iterator));
+ const Script& script = Script::Handle(Exceptions::GetCallerScript(&iterator));
const Class& cls = Class::Handle(assertion_error.clazz());
- SetLocationFields(assertion_error, cls, script, assertion_start);
+ Exceptions::SetLocationFields(assertion_error, cls, script, assertion_start);
// Initialize field 'failed_assertion' with source snippet.
intptr_t from_line, from_column;
script.GetTokenLocation(assertion_start, &from_line, &from_column);
intptr_t to_line, to_column;
script.GetTokenLocation(assertion_end, &to_line, &to_column);
- SetField(assertion_error, cls, "failedAssertion", String::Handle(
+ Exceptions::SetField(assertion_error, cls, "failedAssertion", String::Handle(
script.GetSnippet(from_line, from_column, to_line, to_column)));
// Throw AssertionError instance.
@@ -102,70 +47,6 @@
}
-// Allocate, initialize, and throw a TypeError.
-static void ThrowTypeError(intptr_t location,
- const String& src_type_name,
- const String& dst_type_name,
- const String& dst_name,
- const String& malformed_error) {
- // Allocate a new instance of TypeError.
- const Instance& type_error = Instance::Handle(NewInstance("TypeError"));
-
- // Initialize 'url', 'line', and 'column' fields.
- DartFrameIterator iterator;
- const Script& script = Script::Handle(GetCallerScript(&iterator));
- const Class& cls = Class::Handle(type_error.clazz());
- // Location fields are defined in AssertionError, the superclass of TypeError.
- const Class& assertion_error_class = Class::Handle(cls.SuperClass());
- SetLocationFields(type_error, assertion_error_class, script, location);
-
- // Initialize field 'failedAssertion' in AssertionError superclass.
- // Printing the src_obj value would be possible, but ToString() is expensive
- // and not meaningful for all classes, so we just print '$expr instanceof...'.
- // Users should look at TypeError.ToString(), which contains more useful
- // information than AssertionError.failedAssertion.
- String& failed_assertion = String::Handle(String::New("$expr instanceof "));
- failed_assertion = String::Concat(failed_assertion, dst_type_name);
- SetField(type_error,
- assertion_error_class,
- "failedAssertion",
- failed_assertion);
-
- // Initialize field 'srcType'.
- SetField(type_error, cls, "srcType", src_type_name);
-
- // Initialize field 'dstType'.
- SetField(type_error, cls, "dstType", dst_type_name);
-
- // Initialize field 'dstName'.
- SetField(type_error, cls, "dstName", dst_name);
-
- // Initialize field 'malformedError'.
- SetField(type_error, cls, "malformedError", malformed_error);
-
- // Type errors in the core library may be difficult to diagnose.
- // Print type error information before throwing the error when debugging.
- if (FLAG_print_stack_trace_at_throw) {
- if (!malformed_error.IsNull()) {
- OS::Print("%s", malformed_error.ToCString());
- } else {
- intptr_t line, column;
- script.GetTokenLocation(location, &line, &column);
- OS::Print("'%s': Failed type check: line %d pos %d: "
- "type '%s' is not assignable to type '%s' of '%s'.\n",
- String::Handle(script.url()).ToCString(),
- line, column,
- src_type_name.ToCString(),
- dst_type_name.ToCString(),
- dst_name.ToCString());
- }
- }
- // Throw TypeError instance.
- Exceptions::Throw(type_error);
- UNREACHABLE();
-}
-
-
// Allocate and throw a new TypeError.
// Arg0: index of the token of the failed type check.
// Arg1: src value.
@@ -181,8 +62,8 @@
const String& malformed_error = String::CheckedHandle(arguments->At(4));
const String& src_type_name =
String::Handle(Type::Handle(src_value.GetType()).Name());
- ThrowTypeError(location, src_type_name,
- dst_type_name, dst_name, malformed_error);
+ Exceptions::CreateAndThrowTypeError(location, src_type_name,
+ dst_type_name, dst_name, malformed_error);
}
@@ -195,18 +76,20 @@
// Allocate a new instance of type FallThroughError.
const Instance& fallthrough_error =
- Instance::Handle(NewInstance("FallThroughError"));
+ Instance::Handle(Exceptions::NewInstance("FallThroughError"));
ASSERT(!fallthrough_error.IsNull());
// Initialize 'url' and 'line' fields.
DartFrameIterator iterator;
iterator.NextFrame(); // Skip native call.
- const Script& script = Script::Handle(GetCallerScript(&iterator));
+ const Script& script = Script::Handle(Exceptions::GetCallerScript(&iterator));
const Class& cls = Class::Handle(fallthrough_error.clazz());
- SetField(fallthrough_error, cls, "url", String::Handle(script.url()));
+ Exceptions::SetField(fallthrough_error, cls, "url",
+ String::Handle(script.url()));
intptr_t line, column;
script.GetTokenLocation(fallthrough_pos, &line, &column);
- SetField(fallthrough_error, cls, "line", Smi::Handle(Smi::New(line)));
+ Exceptions::SetField(fallthrough_error, cls, "line",
+ Smi::Handle(Smi::New(line)));
// Throw FallThroughError instance.
Exceptions::Throw(fallthrough_error);
@@ -222,187 +105,23 @@
intptr_t call_pos = smi_pos.Value();
// Allocate a new instance of type StaticResolutionException.
const Instance& resolution_exception =
- Instance::Handle(NewInstance("StaticResolutionException"));
+ Instance::Handle(Exceptions::NewInstance("StaticResolutionException"));
ASSERT(!resolution_exception.IsNull());
// Initialize 'url', 'line', and 'column' fields.
DartFrameIterator iterator;
iterator.NextFrame(); // Skip native call.
- const Script& script = Script::Handle(GetCallerScript(&iterator));
+ const Script& script = Script::Handle(Exceptions::GetCallerScript(&iterator));
const Class& cls = Class::Handle(resolution_exception.clazz());
- SetLocationFields(resolution_exception, cls, script, call_pos);
+ Exceptions::SetLocationFields(resolution_exception, cls, script, call_pos);
intptr_t line, column;
script.GetTokenLocation(call_pos, &line, &column);
- SetField(resolution_exception, cls, "failedResolutionLine",
- String::Handle(script.GetLine(line)));
+ Exceptions::SetField(resolution_exception, cls, "failedResolutionLine",
+ String::Handle(script.GetLine(line)));
Exceptions::Throw(resolution_exception);
UNREACHABLE();
}
-
-// Check that the type of the given instance is assignable to the given type.
-// Arg0: index of the token of the assignment (source location).
-// Arg1: instance being assigned.
-// Arg2: type being assigned to.
-// Arg3: type arguments of the instantiator of the type being assigned to.
-// Arg4: name of instance being assigned to.
-// Return value: instance if assignable, otherwise throw a TypeError.
-DEFINE_RUNTIME_ENTRY(TypeCheck, 5) {
- ASSERT(arguments.Count() == kTypeCheckRuntimeEntry.argument_count());
- intptr_t location = Smi::CheckedHandle(arguments.At(0)).Value();
- const Instance& src_instance = Instance::CheckedHandle(arguments.At(1));
- const AbstractType& dst_type = AbstractType::CheckedHandle(arguments.At(2));
- const AbstractTypeArguments& dst_type_instantiator =
- AbstractTypeArguments::CheckedHandle(arguments.At(3));
- const String& dst_name = String::CheckedHandle(arguments.At(4));
- ASSERT(!dst_type.IsDynamicType()); // No need to check assignment.
- ASSERT(!src_instance.IsNull()); // Already checked in inlined code.
-
- const bool is_assignable =
- src_instance.IsAssignableTo(dst_type, dst_type_instantiator);
-
- if (FLAG_trace_type_checks) {
- const Type& src_type = Type::Handle(src_instance.GetType());
- if (dst_type.IsInstantiated()) {
- OS::Print("TypeCheck: '%s' %s assignable to '%s' of '%s'.\n",
- String::Handle(src_type.Name()).ToCString(),
- is_assignable ? "is" : "is not",
- String::Handle(dst_type.Name()).ToCString(),
- dst_name.ToCString());
- } else {
- // Instantiate dst_type before printing.
- const AbstractType& instantiated_dst_type = AbstractType::Handle(
- dst_type.InstantiateFrom(dst_type_instantiator));
- OS::Print("TypeCheck: '%s' %s assignable to '%s' of '%s' "
- "instantiated from '%s'.\n",
- String::Handle(src_type.Name()).ToCString(),
- is_assignable ? "is" : "is not",
- String::Handle(instantiated_dst_type.Name()).ToCString(),
- dst_name.ToCString(),
- String::Handle(dst_type.Name()).ToCString());
- }
- DartFrameIterator iterator;
- DartFrame* caller_frame = iterator.NextFrame();
- ASSERT(caller_frame != NULL);
- const Function& function = Function::Handle(
- caller_frame->LookupDartFunction());
- OS::Print(" -> Function %s\n", function.ToFullyQualifiedCString());
- }
- if (!is_assignable) {
- const Type& src_type = Type::Handle(src_instance.GetType());
- const String& src_type_name = String::Handle(src_type.Name());
- String& dst_type_name = String::Handle();
- if (!dst_type.IsInstantiated()) {
- // Instantiate dst_type before reporting the error.
- const AbstractType& instantiated_dst_type = AbstractType::Handle(
- dst_type.InstantiateFrom(dst_type_instantiator));
- dst_type_name = instantiated_dst_type.Name();
- } else {
- dst_type_name = dst_type.Name();
- }
- const String& no_malformed_type_error = String::Handle();
- ThrowTypeError(location, src_type_name, dst_type_name, dst_name,
- no_malformed_type_error);
- UNREACHABLE();
- }
- arguments.SetReturn(src_instance);
-}
-
-
-// Report that the type of the given object is not bool in conditional context.
-// Arg0: index of the token of the assignment (source location).
-// Arg1: bad object.
-// Return value: none, throws a TypeError.
-DEFINE_RUNTIME_ENTRY(ConditionTypeError, 2) {
- ASSERT(arguments.Count() ==
- kConditionTypeErrorRuntimeEntry.argument_count());
- intptr_t location = Smi::CheckedHandle(arguments.At(0)).Value();
- const Instance& src_instance = Instance::CheckedHandle(arguments.At(1));
- ASSERT(src_instance.IsNull() || !src_instance.IsBool());
- const Type& bool_interface = Type::Handle(Type::BoolInterface());
- const Type& src_type = Type::Handle(src_instance.GetType());
- const String& src_type_name = String::Handle(src_type.Name());
- const String& bool_type_name = String::Handle(bool_interface.Name());
- const String& expr = String::Handle(String::NewSymbol("boolean expression"));
- const String& no_malformed_type_error = String::Handle();
- ThrowTypeError(location, src_type_name, bool_type_name, expr,
- no_malformed_type_error);
- UNREACHABLE();
-}
-
-
-// Report that the type of the type check is malformed.
-// Arg0: index of the token of the failed type check.
-// Arg1: src value.
-// Arg2: malformed type error message.
-// Return value: none, throws an exception.
-DEFINE_RUNTIME_ENTRY(MalformedTypeError, 3) {
- ASSERT(arguments.Count() ==
- kMalformedTypeErrorRuntimeEntry.argument_count());
- intptr_t location = Smi::CheckedHandle(arguments.At(0)).Value();
- const Instance& src_value = Instance::CheckedHandle(arguments.At(1));
- const String& malformed_error = String::CheckedHandle(arguments.At(2));
- const String& dst_type_name =
- String::Handle(String::NewSymbol("malformed type"));
- const String& dst_name = String::Handle(String::NewSymbol(""));
- const String& src_type_name =
- String::Handle(Type::Handle(src_value.GetType()).Name());
- ThrowTypeError(location, src_type_name,
- dst_type_name, dst_name, malformed_error);
- UNREACHABLE();
-}
-
-
-// Check that the type of each element of the given array is assignable to the
-// given type.
-// Arg0: index of the token of the rest argument declaration (source location).
-// Arg1: rest argument array.
-// Arg2: element declaration type.
-// Arg3: type arguments of the instantiator of the element declaration type.
-// Arg4: name of object being assigned to, i.e. name of rest argument.
-// Return value: null if assignable, otherwise allocate and throw a TypeError.
-DEFINE_RUNTIME_ENTRY(RestArgumentTypeCheck, 5) {
- ASSERT(arguments.Count() ==
- kRestArgumentTypeCheckRuntimeEntry.argument_count());
- intptr_t location = Smi::CheckedHandle(arguments.At(0)).Value();
- const Array& rest_array = Array::CheckedHandle(arguments.At(1));
- const AbstractType& element_type =
- AbstractType::CheckedHandle(arguments.At(2));
- const AbstractTypeArguments& element_type_instantiator =
- AbstractTypeArguments::CheckedHandle(arguments.At(3));
- const String& rest_name = String::CheckedHandle(arguments.At(4));
- ASSERT(!element_type.IsDynamicType()); // No need to check assignment.
- ASSERT(!rest_array.IsNull());
-
- Instance& elem = Instance::Handle();
- for (intptr_t i = 0; i < rest_array.Length(); i++) {
- elem ^= rest_array.At(i);
- if (!elem.IsNull() &&
- !elem.IsAssignableTo(element_type, element_type_instantiator)) {
- // Allocate and throw a new instance of TypeError.
- char buf[256];
- OS::SNPrint(buf, sizeof(buf), "%s[%d]",
- rest_name.ToCString(), static_cast<int>(i));
- const String& src_type_name =
- String::Handle(Type::Handle(elem.GetType()).Name());
- String& dst_type_name = String::Handle();
- if (!element_type.IsInstantiated()) {
- // Instantiate element_type before reporting the error.
- const AbstractType& instantiated_element_type = AbstractType::Handle(
- element_type.InstantiateFrom(element_type_instantiator));
- dst_type_name = instantiated_element_type.Name();
- } else {
- dst_type_name = element_type.Name();
- }
- const String& dst_name = String::Handle(String::New(buf));
- const String& no_malformed_type_error = String::Handle();
- ThrowTypeError(location, src_type_name, dst_type_name, dst_name,
- no_malformed_type_error);
- UNREACHABLE();
- }
- }
-}
-
} // namespace dart
« no previous file with comments | « no previous file | runtime/lib/error.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698