memberSet method

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

HTInstance overrided HTEntity'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 varName, dynamic varValue,
    {String? from, String? cast, bool throws = true}) {
  final setter = '${InternalIdentifier.setter}$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();
        decl.value = varValue;
        return;
      } else if (space.symbols.containsKey(setter)) {
        final decl = space.symbols[setter]!;
        if (decl.isPrivate &&
            from != null &&
            !from.startsWith(namespace.fullName)) {
          throw HTError.privateMember(varName);
        }
        decl.resolve();
        final method = decl as HTFunction;
        method.namespace = namespace;
        method.instance = this;
        method.call(positionalArgs: [varValue]);
        return;
      }
    }
  } else {
    if (!_namespaces.containsKey(cast)) {
      throw HTError.notSuper(cast, classId);
    }

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

  throw HTError.undefined(varName);
}