memberSet method

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

Assign a value to a member by the varName, in the form of

object.varName = varValue

varName must be of String type.

Implementation

@override
bool memberSet(dynamic varName, dynamic varValue,
    {String? from, bool defineIfAbsent = true, bool recursive = true}) {
  if (varName == null) {
    throw HTError.nullSubSetKey();
  }
  if (varName is! String) {
    varName = varName.toString();
  }
  if (varName == InternalIdentifier.prototype) {
    if (varValue is! HTStruct) {
      throw HTError.notStruct();
    }
    prototype = varValue;
    return true;
  }

  final setter = '${InternalIdentifier.setter}$varName';
  if (_fields.containsKey(varName)) {
    if (interpreter.lexicon.isPrivate(varName) &&
        from != null &&
        !from.startsWith(namespace.fullName)) {
      throw HTError.privateMember(varName);
    }
    _fields[varName] = varValue;
    return true;
  } else if (_fields.containsKey(setter)) {
    if (interpreter.lexicon.isPrivate(varName) &&
        from != null &&
        !from.startsWith(namespace.fullName)) {
      throw HTError.privateMember(varName);
    }
    HTFunction func = _fields[setter]!;
    func.namespace = namespace;
    func.instance = this;
    func.call(positionalArgs: [varValue]);
    return true;
  } else if (recursive && prototype != null) {
    final success = prototype!
        .memberSet(varName, varValue, from: from, defineIfAbsent: false);
    if (success) {
      return true;
    }
  }
  if (defineIfAbsent) {
    _fields[varName] = varValue;
    return true;
  }
  return false;
}