memberSet method

  1. @override
void memberSet(
  1. String id,
  2. dynamic value, {
  3. String? from,
  4. bool defineIfAbsent = false,
  5. String? cast,
  6. bool throws = true,
})
override

Set the value of a member from this HTInstace via memberGet operator '.' for symbol searching, use the same name method on HTInstanceNamespace instead. HTInstance overrided HTObject's memberSet, with a new named parameter cast. If cast is provided, then the instance will only search that cast's corresponed HTInstanceNamespace.

Implementation

@override
void memberSet(
  String id,
  dynamic value, {
  String? from,
  bool defineIfAbsent = false,
  String? cast,
  bool throws = true,
}) {
  final setter = '${InternalIdentifier.setter}$id';

  if (cast == null) {
    for (final space in _namespaces.values) {
      if (space.symbols.containsKey(id)) {
        final decl = space.symbols[id];
        if (decl is HTDeclaration) {
          if (decl.isPrivate &&
              from != null &&
              !from.startsWith(namespace.fullName)) {
            throw HTError.privateMember(id);
          }
          decl.resolve();
          decl.value = value;
        } else {
          if (interpreter.lexicon.isPrivate(id) &&
              from != null &&
              !from.startsWith(namespace.fullName)) {
            throw HTError.privateMember(id);
          }
          space.symbols[id] = value;
        }
        return;
      } else if (space.symbols.containsKey(setter)) {
        final decl = space.symbols[setter];
        if (decl is! HTFunction) return;
        if (decl.isPrivate &&
            from != null &&
            !from.startsWith(namespace.fullName)) {
          throw HTError.privateMember(id);
        }
        decl.resolve();
        decl.namespace = namespace;
        decl.instance = this;
        decl.call(positionalArgs: [value]);
        return;
      }
    }
  } else {
    if (!_namespaces.containsKey(cast)) {
      throw HTError.notSuper(cast, classId);
    }

    final space = _namespaces[cast]!;
    if (space.symbols.containsKey(id)) {
      var decl = space.symbols[id];
      if (decl is HTDeclaration) {
        if (decl.isPrivate &&
            from != null &&
            !from.startsWith(namespace.fullName)) {
          throw HTError.privateMember(id);
        }
        decl.resolve();
        decl.value = value;
      } else {
        if (interpreter.lexicon.isPrivate(id) &&
            from != null &&
            !from.startsWith(namespace.fullName)) {
          throw HTError.privateMember(id);
        }
        space.symbols[id] = value;
      }
      return;
    } else if (space.symbols.containsKey(setter)) {
      final decl = space.symbols[setter];
      if (decl is! HTFunction) return;
      if (decl.isPrivate &&
          from != null &&
          !from.startsWith(namespace.fullName)) {
        throw HTError.privateMember(id);
      }
      decl.resolve();
      decl.namespace = _namespaces[cast];
      decl.instance = this;
      decl.call(positionalArgs: [value]);
      return;
    }
  }

  throw HTError.undefined(id);
}