memberGet method

  1. @override
dynamic memberGet(
  1. String varName, {
  2. String? from,
  3. String? cast,
  4. bool throws = true,
})
override

HTInstance overrided HTEntity's memberGet, with a new named parameter cast. If cast is provided, then the instance will only search that cast's corresponed HTInstanceNamespace.

Implementation

@override
dynamic memberGet(String varName,
    {String? from, String? cast, bool throws = true}) {
  final getter = '${InternalIdentifier.getter}$varName';

  if (cast == null) {
    for (final space in _namespaces.values) {
      if (space.symbols.containsKey(varName)) {
        final decl = space.symbols[varName]!;
        if (decl.isPrivate &&
            from != null &&
            !from.startsWith(namespace.fullName)) {
          throw HTError.privateMember(varName);
        }
        decl.resolve();
        if (decl is HTFunction && decl.category != FunctionCategory.literal) {
          decl.namespace = namespace;
          decl.instance = this;
        }
        return decl.value;
      } else if (space.symbols.containsKey(getter)) {
        final decl = space.symbols[getter]!;
        if (decl.isPrivate &&
            from != null &&
            !from.startsWith(namespace.fullName)) {
          throw HTError.privateMember(varName);
        }
        decl.resolve();
        final func = decl as HTFunction;
        func.namespace = namespace;
        func.instance = this;
        return func.call();
      }
    }
  } else {
    final space = _namespaces[cast]!;
    if (space.symbols.containsKey(varName)) {
      final decl = space.symbols[varName]!;
      if (decl.isPrivate &&
          from != null &&
          !from.startsWith(namespace.fullName)) {
        throw HTError.privateMember(varName);
      }
      decl.resolve();
      if (decl is HTFunction && decl.category != FunctionCategory.literal) {
        decl.namespace = _namespaces[classId];
        decl.instance = this;
      }
      return decl.value;
    } else if (space.symbols.containsKey(getter)) {
      final decl = space.symbols[getter]!;
      if (decl.isPrivate &&
          from != null &&
          !from.startsWith(namespace.fullName)) {
        throw HTError.privateMember(varName);
      }
      decl.resolve();
      final func = decl as HTFunction;
      func.namespace = _namespaces[classId];
      func.instance = this;
      return func.call();
    }
  }

  if (throws) {
    throw HTError.undefined(varName);
  }
}