memberSet method

  1. @override
bool memberSet(
  1. dynamic id,
  2. dynamic value, {
  3. String? from,
  4. bool defineIfAbsent = true,
})
override

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

object.id = value

id must be of String type.

Implementation

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

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