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

Unified Diff: runtime/lib/mirrors.cc

Issue 24631003: Add proper API for creating private symbols wrt a library. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: maintain dart2js coverage Created 7 years, 2 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/mirrors_impl.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/lib/mirrors.cc
diff --git a/runtime/lib/mirrors.cc b/runtime/lib/mirrors.cc
index b071591ed8989fb190440813c4577313bc76223f..21000a9a7c705c9582ab568441d843694c0a9119 100644
--- a/runtime/lib/mirrors.cc
+++ b/runtime/lib/mirrors.cc
@@ -257,7 +257,11 @@ static RawInstance* CreateMethodMirror(const Function& func,
const Instance& owner_mirror) {
const Array& args = Array::Handle(Array::New(12));
args.SetAt(0, MirrorReference::Handle(MirrorReference::New(func)));
- args.SetAt(1, String::Handle(func.UserVisibleName()));
+
+ String& name = String::Handle(func.name());
+ name = String::IdentifierPrettyNameRetainPrivate(name);
+ args.SetAt(1, name);
+
args.SetAt(2, owner_mirror);
args.SetAt(3, Bool::Get(func.is_static()));
args.SetAt(4, Bool::Get(func.is_abstract()));
@@ -280,7 +284,7 @@ static RawInstance* CreateVariableMirror(const Field& field,
const MirrorReference& field_ref =
MirrorReference::Handle(MirrorReference::New(field));
- const String& name = String::Handle(field.UserVisibleName());
+ const String& name = String::Handle(field.name());
const Array& args = Array::Handle(Array::New(6));
args.SetAt(0, field_ref);
@@ -338,7 +342,7 @@ static RawInstance* CreateClassMirror(const Class& cls,
// use a different naming convention than the VM (lib.S with lib.M and S&M
// respectively).
if (!cls.IsMixinApplication() || cls.is_mixin_typedef()) {
- args.SetAt(2, String::Handle(cls.UserVisibleName()));
+ args.SetAt(2, String::Handle(cls.Name()));
}
args.SetAt(3, is_generic);
args.SetAt(4, is_mixin_typedef);
@@ -479,6 +483,67 @@ static RawInstance* InvokeLibraryGetter(const Library& library,
// class, depending on whether it was an actual getter, or an uninitialized
// field.
const Field& field = Field::Handle(
+ library.LookupLocalField(getter_name));
+ Function& getter = Function::Handle();
+ if (field.IsNull()) {
+ // No field found. Check for a getter in the lib.
+ const String& internal_getter_name =
+ String::Handle(Field::GetterName(getter_name));
+ getter = library.LookupLocalFunction(internal_getter_name);
+ if (getter.IsNull()) {
+ getter = library.LookupLocalFunction(getter_name);
+ if (!getter.IsNull()) {
+ // Looking for a getter but found a regular method: closurize it.
+ const Function& closure_function =
+ Function::Handle(getter.ImplicitClosureFunction());
+ return closure_function.ImplicitStaticClosure();
+ }
+ }
+ } else {
+ if (!field.IsUninitialized()) {
+ return field.value();
+ }
+ // An uninitialized field was found. Check for a getter in the field's
+ // owner classs.
+ const Class& klass = Class::Handle(field.owner());
+ const String& internal_getter_name =
+ String::Handle(Field::GetterName(getter_name));
+ getter = klass.LookupStaticFunction(internal_getter_name);
+ }
+
+ if (!getter.IsNull() && getter.is_visible()) {
+ // Invoke the getter and return the result.
+ const Object& result = Object::Handle(
+ DartEntry::InvokeFunction(getter, Object::empty_array()));
+ return ReturnResult(result);
+ }
+
+ if (throw_nsm_if_absent) {
+ ThrowNoSuchMethod(Instance::null_instance(),
+ getter_name,
+ getter,
+ InvocationMirror::kTopLevel,
+ InvocationMirror::kGetter);
+ UNREACHABLE();
+ }
+
+ // Fall through case: Indicate that we didn't find any function or field using
+ // a special null instance. This is different from a field being null. Callers
+ // make sure that this null does not leak into Dartland.
+ return Object::sentinel().raw();
+}
+
+
+// TODO(13656): Remove AllowPrivate.
+static RawInstance* InvokeLibraryGetterAllowImports(
+ const Library& library,
+ const String& getter_name,
+ const bool throw_nsm_if_absent) {
+ // To access a top-level we may need to use the Field or the getter Function.
+ // The getter function may either be in the library or in the field's owner
+ // class, depending on whether it was an actual getter, or an uninitialized
+ // field.
+ const Field& field = Field::Handle(
library.LookupFieldAllowPrivate(getter_name));
Function& getter = Function::Handle();
if (field.IsNull()) {
@@ -504,7 +569,7 @@ static RawInstance* InvokeLibraryGetter(const Library& library,
const Class& klass = Class::Handle(field.owner());
const String& internal_getter_name =
String::Handle(Field::GetterName(getter_name));
- getter = klass.LookupStaticFunctionAllowPrivate(internal_getter_name);
+ getter = klass.LookupStaticFunction(internal_getter_name);
}
if (!getter.IsNull() && getter.is_visible()) {
@@ -539,11 +604,11 @@ static RawInstance* InvokeClassGetter(const Class& klass,
const String& internal_getter_name = String::Handle(
Field::GetterName(getter_name));
Function& getter = Function::Handle(
- klass.LookupStaticFunctionAllowPrivate(internal_getter_name));
+ klass.LookupStaticFunction(internal_getter_name));
if (getter.IsNull() || !getter.is_visible()) {
if (getter.IsNull()) {
- getter = klass.LookupStaticFunctionAllowPrivate(getter_name);
+ getter = klass.LookupStaticFunction(getter_name);
if (!getter.IsNull()) {
// Looking for a getter but found a regular method: closurize it.
const Function& closure_function =
@@ -583,7 +648,7 @@ static RawInstance* InvokeInstanceGetter(const Class& klass,
const String& internal_getter_name = String::Handle(
Field::GetterName(getter_name));
Function& function = Function::Handle(
- Resolver::ResolveDynamicAnyArgsAllowPrivate(klass, internal_getter_name));
+ Resolver::ResolveDynamicAnyArgs(klass, internal_getter_name));
if (!function.IsNull() || throw_nsm_if_absent) {
const int kNumArgs = 1;
@@ -617,7 +682,7 @@ static RawInstance* LookupFunctionOrFieldInLibraryPrefix(
const Class& field_owner = Class::Handle(field.owner());
const Library& field_library = Library::Handle(field_owner.library());
const Instance& result = Instance::Handle(
- InvokeLibraryGetter(field_library, lookup_name, false));
+ InvokeLibraryGetterAllowImports(field_library, lookup_name, false));
if (result.raw() != Object::sentinel().raw()) {
return result.raw();
}
@@ -648,7 +713,7 @@ static RawInstance* LookupStaticFunctionOrFieldInClass(
Function& func = Function::Handle();
Class& lookup_class = Class::Handle(klass.raw());
while (func.IsNull() && !lookup_class.IsNull()) {
- func ^= lookup_class.LookupStaticFunctionAllowPrivate(lookup_name);
+ func ^= lookup_class.LookupStaticFunction(lookup_name);
lookup_class = lookup_class.SuperClass();
}
if (!func.IsNull()) {
@@ -709,12 +774,12 @@ static RawInstance* LookupFunctionOrFieldInLibraryHelper(
const String& lookup_name) {
if (class_name.IsNull()) {
const Instance& result = Instance::Handle(
- InvokeLibraryGetter(library, lookup_name, false));
+ InvokeLibraryGetterAllowImports(library, lookup_name, false));
if (result.raw() != Object::sentinel().raw()) {
return result.raw();
}
const Function& func = Function::Handle(
- library.LookupFunctionAllowPrivate(lookup_name));
+ library.LookupLocalFunction(lookup_name));
if (!func.IsNull()) {
const Function& closure_function = Function::Handle(
func.ImplicitClosureFunction());
@@ -722,7 +787,7 @@ static RawInstance* LookupFunctionOrFieldInLibraryHelper(
}
} else {
const Class& cls = Class::Handle(
- library.LookupClassAllowPrivate(class_name));
+ library.LookupClass(class_name));
if (!cls.IsNull()) {
return LookupStaticFunctionOrFieldInClass(cls, lookup_name);
}
@@ -791,6 +856,25 @@ DEFINE_NATIVE_ENTRY(Mirrors_makeLocalTypeMirror, 1) {
return CreateTypeMirror(type);
}
+DEFINE_NATIVE_ENTRY(Mirrors_mangleName, 2) {
+ GET_NON_NULL_NATIVE_ARGUMENT(String, name, arguments->NativeArgAt(0));
+ GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(1));
+ const Library& lib = Library::Handle(ref.GetLibraryReferent());
+ return lib.IsPrivate(name) ? lib.PrivateName(name) : name.raw();
+}
+
+DEFINE_NATIVE_ENTRY(Mirrors_unmangleName, 1) {
+ GET_NON_NULL_NATIVE_ARGUMENT(String, name, arguments->NativeArgAt(0));
+ // It would be nice to unconditionally use IdentifierPrettyName, alas it
+ // cannot cope with the symbols for the names of anonymous mixin applications.
+ if (Library::IsPrivate(name) ||
+ Field::IsGetterName(name) ||
+ Field::IsSetterName(name)) {
+ return String::IdentifierPrettyName(name);
+ }
+ return name.raw();
+}
+
DEFINE_NATIVE_ENTRY(MirrorReference_equals, 2) {
GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, a, arguments->NativeArgAt(0));
@@ -1156,7 +1240,7 @@ DEFINE_NATIVE_ENTRY(InstanceMirror_invoke, 5) {
Class& klass = Class::Handle(reflectee.clazz());
Function& function = Function::Handle(
- Resolver::ResolveDynamicAnyArgsAllowPrivate(klass, function_name));
+ Resolver::ResolveDynamicAnyArgs(klass, function_name));
const Array& args_descriptor =
Array::Handle(ArgumentsDescriptor::New(args.Length(), arg_names));
@@ -1205,7 +1289,7 @@ DEFINE_NATIVE_ENTRY(InstanceMirror_invokeSetter, 4) {
ThrowMirroredCompilationError(message);
UNREACHABLE();
}
- setter = klass.LookupDynamicFunctionAllowPrivate(internal_setter_name);
+ setter = klass.LookupDynamicFunction(internal_setter_name);
if (!setter.IsNull()) {
break;
}
@@ -1357,8 +1441,7 @@ DEFINE_NATIVE_ENTRY(ClassMirror_invoke, 5) {
Array::Handle(ArgumentsDescriptor::New(args.Length(), arg_names));
const Function& function = Function::Handle(
- klass.LookupStaticFunctionAllowPrivate(function_name));
-
+ klass.LookupStaticFunction(function_name));
ArgumentsDescriptor args_descriptor(args_descriptor_array);
if (function.IsNull() ||
@@ -1408,7 +1491,7 @@ DEFINE_NATIVE_ENTRY(ClassMirror_invokeSetter, 4) {
const String& internal_setter_name = String::Handle(
Field::SetterName(setter_name));
const Function& setter = Function::Handle(
- klass.LookupStaticFunctionAllowPrivate(internal_setter_name));
+ klass.LookupStaticFunction(internal_setter_name));
if (setter.IsNull() || !setter.is_visible()) {
ThrowNoSuchMethod(AbstractType::Handle(klass.RareType()),
@@ -1469,7 +1552,7 @@ DEFINE_NATIVE_ENTRY(ClassMirror_invokeConstructor, 5) {
}
Function& lookup_constructor = Function::Handle(
- klass.LookupFunctionAllowPrivate(internal_constructor_name));
+ klass.LookupFunction(internal_constructor_name));
if (lookup_constructor.IsNull() ||
!(lookup_constructor.IsConstructor() || lookup_constructor.IsFactory()) ||
@@ -1601,7 +1684,7 @@ DEFINE_NATIVE_ENTRY(LibraryMirror_invoke, 5) {
Array::Handle(ArgumentsDescriptor::New(args.Length(), arg_names));
const Function& function = Function::Handle(
- library.LookupFunctionAllowPrivate(function_name));
+ library.LookupLocalFunction(function_name));
ArgumentsDescriptor args_descriptor(args_descriptor_array);
if (function.IsNull() ||
@@ -1649,13 +1732,13 @@ DEFINE_NATIVE_ENTRY(LibraryMirror_invokeSetter, 4) {
// setter Function. The setter function may either be in the
// library or in the field's owner class, depending.
const Field& field = Field::Handle(
- library.LookupFieldAllowPrivate(setter_name));
+ library.LookupLocalField(setter_name));
if (field.IsNull()) {
const String& internal_setter_name =
String::Handle(Field::SetterName(setter_name));
const Function& setter = Function::Handle(
- library.LookupFunctionAllowPrivate(internal_setter_name));
+ library.LookupLocalFunction(internal_setter_name));
if (setter.IsNull() || !setter.is_visible()) {
ThrowNoSuchMethod(Instance::null_instance(),
setter_name,
« no previous file with comments | « no previous file | runtime/lib/mirrors_impl.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698