memberGet method

  1. @override
dynamic memberGet(
  1. dynamic varName, {
  2. String? from,
  3. bool isRecursivelyGet = false,
})
override

isSelf means wether this is called by the struct itself, or a recursive one

Implementation

@override
dynamic memberGet(dynamic varName,
    {String? from, bool isRecursivelyGet = false}) {
  if (varName == null) {
    return null;
  }
  if (varName is! String) {
    varName = varName.toString();
  }
  if (varName == InternalIdentifier.prototype) {
    return prototype;
  }

  dynamic value;
  final getter = '${InternalIdentifier.getter}$varName';
  final constructor = varName != id
      ? '${InternalIdentifier.namedConstructorPrefix}$varName'
      : InternalIdentifier.defaultConstructor;

  if (_fields.containsKey(varName)) {
    if (interpreter.lexicon.isPrivate(varName) &&
        from != null &&
        !from.startsWith(namespace.fullName)) {
      throw HTError.privateMember(varName);
    }
    value = _fields[varName];
  } else if (_fields.containsKey(getter)) {
    if (interpreter.lexicon.isPrivate(varName) &&
        from != null &&
        !from.startsWith(namespace.fullName)) {
      throw HTError.privateMember(varName);
    }
    value = _fields[getter]!;
  } else if (_fields.containsKey(constructor)) {
    if (interpreter.lexicon.isPrivate(varName) &&
        from != null &&
        !from.startsWith(namespace.fullName)) {
      throw HTError.privateMember(varName);
    }
    value = _fields[constructor]!;
  } else if (prototype != null) {
    value = prototype!.memberGet(varName, from: from, isRecursivelyGet: true);
  }

  if (value is HTDeclaration) {
    value.resolve();
  }
  // assign the original struct as instance, not the prototype object
  if (!isRecursivelyGet) {
    if (value is HTFunction) {
      value.namespace = namespace;
      value.instance = this;
      if (value.category == FunctionCategory.getter) {
        value = value.call();
      }
    }
  }
  return value;
}