前端开发入门到精通的在线学习网站

网站首页 > 资源文章 正文

flutter之dartsdk-- Instance,Symbols篇

qiguaw 2024-10-08 06:50:24 资源文章 13 ℃ 0 评论

一,Instance是所有实例对象的基类(也称为Dart源代码中的Object类)


二,canonicalization(规范化) 是编译器 IR(intermediate representation) 设计中的一个重要部分,它使代码转换(transformations) 变得简单高效。大多数编译器都有 canonicalization pass,对于后续进行编译器优化也起到很大作用

/*如果无法规范化实例,则返回Instance::null()。任何非规范number,字符串都将在此处规范化。如果实例的字段中仍然包含非规范实例,则无法规范化该实例。返回error_str中的错误,如果无法发生错误,则传递NULL。 */

virtual RawInstance* CheckAndCanonicalize(Thread* thread,

const char** error_str) const;

三,检查此实例的类型是否为给定其他类型的子类型 ,如果需要,类型参数向量用于实例化其他类型.

bool IsInstanceOf(const AbstractType& other,

const TypeArguments& other_instantiator_type_arguments,

const TypeArguments& other_function_type_arguments) const;


void SetNativeField(int index, intptr_t value) const

void SetField(const Field& field, const Object& value) const;

virtual RawTypeArguments* GetTypeArguments() const;

RawAbstractType* GetType(Heap::Space space) const;


四,如果此实例的类型是FutureOr<T>的子类型,则返回true ,由实例化类型“other”指定。 如果其他类型不是FutureOr,则返回false。

bool IsFutureOrInstanceOf(Zone* zone,

const AbstractType& other) const;


五,如果实例是可调用对象,即闭包或实现“call”方法的类的实例,则返回true并将函数(如果不是NULL)设置为调用

bool IsCallable(Function* function) const;


六,指针可能有子类型,但其子类型可能没有额外的字段。子类型运行时表示具有完全相同的对象布局,只有class_id不同。因此,在指针句柄中使用子类型实例是安全的

virtual bool IsPointer() const;


七,获取instace的所有参数类型,如:int 4/8个字节等,地址是连续的

RawTypeArguments* Instance::GetTypeArguments() const {

ASSERT(!IsType());

const Class& cls = Class::Handle(clazz());

intptr_t field_offset = cls.host_type_arguments_field_offset();

ASSERT(field_offset != Class::kNoTypeArguments);

TypeArguments& type_arguments = TypeArguments::Handle();

type_arguments ^= *FieldAddrAtOffset(field_offset);

return type_arguments.raw();

}


void Instance::SetTypeArguments(const TypeArguments& value) const {

ASSERT(!IsType());

ASSERT(value.IsNull() || value.IsCanonical());

const Class& cls = Class::Handle(clazz());

intptr_t field_offset = cls.host_type_arguments_field_offset();

ASSERT(field_offset != Class::kNoTypeArguments);

SetFieldAtOffset(field_offset, value);

}


八,检查类型T0是否为类型T1的子类型。

类型T0由用“Type_arguments”参数化的类“cls”和“nullability”指定,类型T1由“other”指定,并且必须具有类型类

此函数不支持类型为T0的Null、Never、dynamic或void

bool Class::IsSubtypeOf(const Class& cls,

const TypeArguments& type_arguments,

Nullability nullability,

const AbstractType& other,

Heap::Space space){

….

// 左边 FutureOr:

// if T0 is FutureOr<S0> then:

// T0 <: T1 iff Future<S0> <: T1 && S0 <: T1

if (this_cid == kFutureOrCid) {

// Check Future<S0> <: T1.

ObjectStore* object_store = Isolate::Current()->object_store();

const Class& future_class =

Class::Handle(zone, object_store->future_class());

ASSERT(!future_class.IsNull() && future_class.NumTypeParameters() == 1 &&

this_class.NumTypeParameters() == 1);

ASSERT(type_arguments.IsNull() || type_arguments.Length() >= 1);

if (Class::IsSubtypeOf(future_class, type_arguments,

Nullability::kNonNullable, other, space)) {

// Check S0 <: T1.

const AbstractType& type_arg =

AbstractType::Handle(zone, type_arguments.TypeAtNullSafe(0));

if (type_arg.IsSubtypeOf(other, space)) {

return verified_nullability;

}

}

}

}

九,FutureOr类型后续需详细分析


十,Symbols(包含规范化格式的常用字符串列表。此列表保存在vm_isolate中,以便在隔离之间共享副本,而无需在每个隔离中维护副本)

class Symbols : public AllStatic {

}

代码扣析

define DART_TOKEN_LIST(TOK) \

TOK(kEOS, "", 0, kNoAttribute) \

\

TOK(kLPAREN, "(", 0, kNoAttribute) \

TOK(kRPAREN, ")", 0, kNoAttribute) \

TOK(kLBRACK, "[", 0, kNoAttribute) \

TOK(kRBRACK, "]", 0, kNoAttribute) \

TOK(kLBRACE, "{", 0, kNoAttribute) \

TOK(kRBRACE, "}", 0, kNoAttribute) \

TOK(kARROW, "=>", 0, kNoAttribute) \

TOK(kCOLON, ":", 0, kNoAttribute) \

TOK(kSEMICOLON, ";", 0, kNoAttribute) \

TOK(kPERIOD, ".", 0, kNoAttribute) \

TOK(kQM_PERIOD, "?.", 0, kNoAttribute) \

TOK(kINCR, "++", 0, kNoAttribute) \

TOK(kDECR, "--", 0, kNoAttribute)


#define DART_KEYWORD_LIST(KW) \

KW(kABSTRACT, "abstract", 0, kPseudoKeyword) /* == kFirstKeyword */ \

KW(kAS, "as", 11, kPseudoKeyword) \

KW(kASSERT, "assert", 0, kKeyword) \

KW(kBREAK, "break", 0, kKeyword) \

KW(kCASE, "case", 0, kKeyword) \



#define PREDEFINED_SYMBOLS_LIST(V) \

V(AbstractClassInstantiationError, "AbstractClassInstantiationError") \

V(AllocateInvocationMirror, "_allocateInvocationMirror") \

V(AllocateInvocationMirrorForClosure, "_allocateInvocationMirrorForClosure") \

V(AnonymousClosure, "<anonymous closure>") \

V(AnonymousSignature, "<anonymous signature>") \

V(ApiError, "ApiError") \

V(ArgDescVar, ":arg_desc") \

V(ArgumentError, "ArgumentError") \

V(AsFunctionInternal, "_asFunctionInternal") \

V(AssertionError, "_AssertionError") \

V(AssignIndexToken, "[]=") \

V(AsyncCompleter, ":async_completer") \

V(AsyncOperation, ":async_op") \

V(AsyncStackTraceVar, ":async_stack_trace") \

V(AsyncStarMoveNextHelper, "_asyncStarMoveNextHelper") \

V(AwaitContextVar, ":await_ctx_var") \

V(AwaitJumpVar, ":await_jump_var") \

V(Bool, "bool") \

V(BooleanExpression, "boolean expression") \

V(BoundsCheckForPartialInstantiation, "_boundsCheckForPartialInstantiation") \

V(ByteData, "ByteData") \

V(ByteDataDot, "ByteData.") \

V(ByteDataDot_view, "ByteData._view")

// List of strings that are pre created in the vm isolate.

enum { kMaxOneCharCodeSymbol = 0xFF };

enum SymbolId {

// clang-format off

kIllegal = 0,


#define DEFINE_SYMBOL_INDEX(symbol, literal) k##symbol##Id,

PREDEFINED_SYMBOLS_LIST(DEFINE_SYMBOL_INDEX)

#undef DEFINE_SYMBOL_INDEX


kTokenTableStart, // First token at kTokenTableStart + 1.


#define DEFINE_TOKEN_SYMBOL_INDEX(t, s, p, a) t##Id,

DART_TOKEN_LIST(DEFINE_TOKEN_SYMBOL_INDEX) DART_KEYWORD_LIST(

DEFINE_TOKEN_SYMBOL_INDEX)

#undef DEFINE_TOKEN_SYMBOL_INDEX


kNullCharId, // One char code symbol starts here and takes up 256 entries.

kMaxPredefinedId = kNullCharId + kMaxOneCharCodeSymbol + 1,

// clang-format on

};


static const char* names[] = {

// clang-format off

NULL,

#define DEFINE_SYMBOL_LITERAL(symbol, literal) literal,

PREDEFINED_SYMBOLS_LIST(DEFINE_SYMBOL_LITERAL)

#undef DEFINE_SYMBOL_LITERAL

"", // matches kTokenTableStart.

#define DEFINE_TOKEN_SYMBOL_INDEX(t, s, p, a) s,

DART_TOKEN_LIST(DEFINE_TOKEN_SYMBOL_INDEX)

DART_KEYWORD_LIST(DEFINE_TOKEN_SYMBOL_INDEX)

#undef DEFINE_TOKEN_SYMBOL_INDEX

// clang-format on

};



string name = names[kBoolId];//name “bool”

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表