HTInstance constructor

HTInstance(
  1. HTClass klass,
  2. HTInterpreter interpreter, {
  3. List<HTType> typeArgs = const [],
  4. Map<String, dynamic>? jsonObject,
})

Create a default HTInstance instance.

Implementation

HTInstance(HTClass klass, HTInterpreter interpreter,
    {List<HTType> typeArgs = const [], Map<String, dynamic>? jsonObject})
    : index = klass.instanceIndex,
      valueType = HTNominalType(klass, typeArgs: typeArgs) {
  this.interpreter = interpreter;

  HTClass? curKlass = klass;
  // final extended = <HTValueType>[];
  final myNsp = HTInstanceNamespace(
      lexicon: interpreter.lexicon,
      id: InternalIdentifier.instance,
      instance: this,
      classId: curKlass.id,
      closure: klass.namespace);
  HTInstanceNamespace? curNamespace = myNsp;
  while (curKlass != null && curNamespace != null) {
    // 继承类成员,所有超类的成员都会分别保存
    for (final key in curKlass.namespace.symbols.keys) {
      final decl = curKlass.namespace.symbols[key]!;
      if (decl.isStatic) {
        continue;
      }
      // TODO: check if override, and if so, check the type wether fits super's type.
      final clone = decl.clone();
      curNamespace.define(key, clone);

      if (jsonObject != null && jsonObject.containsKey(clone.id)) {
        final value = jsonObject[clone.id];
        clone.value = value;
      }
    }

    _namespaces[curKlass.id!] = curNamespace;

    // if (curKlass.extendedType != null) {
    //   extended.add(curKlass.extendedType!);
    // }
    curKlass = curKlass.superClass;
    if (curKlass != null) {
      final next = HTInstanceNamespace(
          lexicon: interpreter.lexicon,
          id: InternalIdentifier.instance,
          instance: this,
          runtimeInstanceNamespace: myNsp,
          classId: curKlass.id,
          closure: curKlass.namespace);
      curNamespace.next = next;
      // next.prev = curNamespace;
    } else {
      curNamespace.next = null;
    }

    curNamespace = curNamespace.next;
  }
}