Type in Chakra


Javascript是一个无类型的语言。

我们要讨论的类型是指Chakra内置的一些数据结构,这些结构维护了Object的信息。

Type在一类Object中共享数据,使用运行时类型的目的就是为了效率。

Chakra具有两类类型,static和dynamic。

static类型是提供给简单object的,这些object不会在里面储存属性。static类型有string("HELLO")、boolean(true\false)、number。

而Dynamic Object是动态类型的,它们会储存属性,比如{}。

每个RecyclableObject都维持着一个指向type的指针。Chakra中每个对象的Class都继承自RecyclableObject除了tagged float。

var greeting = "hello";
var message = "open source";

这两个字符串中chakra中定义为JavascriptString类,这个类继承自JavascriptString。greeting和message指向一个共享的type。



这两个JavascriptString都设置为TypeIds_String,并且这个Type是被两个字符串共享的(type指针指向同一个)。

事实上type对象中含有大量的值域,typeld只是其中一个。

type Js::Type
{
typeId Js::TypeId
flags TypeFlagMask
javascriptLibrary Js::JavascriptLibrary *
prototype Js::RecyclableObject * {Js::DynamicObject}
entryPoint void *(*)(Js::RecyclableObject *, Js::CallInfo)
propertyCache Js::TypePropertyCache *
}

JavascriptString是一个静态的类型,但是相比之下Dynamic类型的Dynamic object提供了许多可以共享信息的机会。(DynamicObject和StaticOject都是继承自RecyclableObject的)

以下代码创建的是Dynamic Object

function Point(x, y)
{
this.x = x;
this.y = y;
}
var one = new Point(10,20);
var two = new Point(40,50); print(one.x);
print(two.x);

创建了2个Point Object,有x、y两个属性,Chakra需要在runtime中储存以下这些信息。

1.1、2号对象有x、y两个属性

2.1号对象的x属性为10,y属性为20

3.2号对象的x属性为40,y属性为50

当脚本要get和set属性值的时候,上述的信息就需要获取了。

(1)是可以在多个对象之间共享的

(2、3)是每个Dynamic Object特有的,彼此不同

解决方法是建立property map和slot array。

Property map, maps between a property and a slot number. Example:

Property x is present at slot 0.

Slot array stores the values. Example slots[0] contains the value of

property x.

就是说Property map表示一个属性对应于一个slot,而slot实际保存数据值。

Property map是储存在dynamic type对象中的。当访问1.x的时候,chakra在1->type取出property map找到相应的slot number,图中是0。然后通过1->slots[0]取出实际的值10。

(就是说property map是Type Object中共享的,保存下标。slot array是每个对象自己的,保存实际值)

所有从构造函数Point创建的对象都可以共享一个类型。

即使你创造了一百万个,你也只需要一个类型代表它们。

通过类型的概念可以实现大量的优化,比如

Inline Cache
Object type specialization in JIT
Function specialization

想要更深入地进行优化需要一个单独的结构。 Typehanlder是Dynamic type的一部分,负责处理property map储存和一个Dynamic Object获取它的类型的方式。

Typehandler

Typehandler是一个Dynamic type负责两个目标。

1.负责维护一个property map

2.维护successor type

var one = {};
var two = {};
//Objects one and two points to Type1 one.x = 10;
//Object one points to Type2 and object two points to Type1
starwars1(one, two); one.y = 20;
//Object one points to Type3 and object two points to Type1
starwars2(one, two); two.x = 40;
//Object one points to Type3 and object two points to Type2
starwars3(one, two); two.y = 50;
//Object one and two points to Type3
starwars4(one, two);

starwars可以为one和two在创建的时候共享相同的属性。

one和two这两个对象最后都具有了x和y两个属性。

所以在starwars4执行之后,他们可以共享同一个type object。

如果在不同的时间点创建了相同属性的对象,如何确定所有具有相同属性的对象呢?

typehanlder中的successor type解决了这个问题。每个typehandler都保存有一个successor type,如果一个对象获得了一个新属性那么就由它来指派一个新的type对象。这个过程通常叫做类型提升或是类型转变。



因为typehandler保存有指向后继类型的指针,因此类型转换会很快的发生。

https://github.com/Microsoft/ChakraCore/blob/master/lib/Runtime/Types/PathTypeHandler.h#L207

SimpleTypeHanlder拥有一个叫做typePath的property map,它拥有一个[tiny dictionary]去映射property Id 到slot number。同样存在维护着下一个类型指针的successorTypeWeakRef。SimpleTypeHandler的一个比较有名的变种是 [PathTypeHandler]维持着 PropertySuccessorsMap 到多个successors。

这篇文章讨论了如下几个问题

1.为啥所有对象都要存在type对象

2.type对象是怎么实现多对象共享的

3.为啥typehandler会指向successor type,注意还存在有许多不同的typehandlers 在共享不同的数据。

http://abchatra.github.io/Type/

Type in Chakra的更多相关文章

  1. Chakra调试笔记 TypedArray

    一.TypedArray类型 TypedArray是漏洞中常见到的结构,手册用法有四 1.new TypedArray(length); //byteLength=length * sizeof(Ty ...

  2. salesforce 零基础学习(六十二)获取sObject中类型为Picklist的field values(含record type)

    本篇引用以下三个链接: http://www.tgerm.com/2012/01/recordtype-specific-picklist-values.html?m=1 https://github ...

  3. AutoMapper:Unmapped members were found. Review the types and members below. Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type

    异常处理汇总-后端系列 http://www.cnblogs.com/dunitian/p/4523006.html 应用场景:ViewModel==>Mode映射的时候出错 AutoMappe ...

  4. $.type 怎么精确判断对象类型的 --(源码学习2)

    目标:  var a = [1,2,3];     console.log(typeof a); //->object     console.log($.type(a)); //->ar ...

  5. input type='file'上传控件假样式

    采用bootstrap框架样式 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> &l ...

  6. mount报错: you must specify the filesystem type

    在linux mount /dev/vdb 到 /home 分区时报错: # mount /dev/vdb /homemount: you must specify the filesystem ty ...

  7. error C4430:missing type specifier 解决错误

    错误    3    error C4430: missing type specifier - int assumed. Note: C++ does not support default-int ...

  8. The type javax.ws.rs.core.MediaType cannot be resolved. It is indirectly referenced from required .class files

    看到了http://stackoverflow.com/questions/5547162/eclipse-error-indirectly-referenced-from-required-clas ...

  9. OpenCASCADE Ring Type Spring Modeling

    OpenCASCADE Ring Type Spring Modeling eryar@163.com Abstract. The general method to directly create ...

随机推荐

  1. 任意两点间的最短路问题(Floyd-Warshall算法)

    #define _CRT_SECURE_NO_WARNINGS /* 7 10 0 1 5 0 2 2 1 2 4 1 3 2 2 3 6 2 4 10 3 5 1 4 5 3 4 6 5 5 6 9 ...

  2. 01-单一职责原则(SPR)

    1. 背景     类T负责两个不同的职责:职责P1,职责P2.当由于职责P1需求发生改变而需要修改类T时,有可能会导致原本运行正常的职责P2功能发生故障. 2. 定义     不要存在多于一个导致类 ...

  3. mysql 原理 ~ 二阶段提交协议通说

    一 简介: 今天是第二篇,讲解的是mysql的事务日志 二 具体 1 WAL技术(先写日志,再写磁盘) 2 binlog redolog 二阶段提交协议     目的 保持 redo log和binl ...

  4. 3d图像识别基础论文:pointNet阅读笔记

    PointNet 论文阅读: 主要思路:输入独立的点云数据,进行变换不变性处理(T-net)后,通过pointNet网络训练后,最后通过最大池化和softMax分类器,输出评分结果. 摘要: 相较于之 ...

  5. SpringBoot启动方式讲解和部署war项目到tomcat9

    1.SpringBoot启动方式讲解和部署war项目到tomcat9简介:SpringBoot常见启动方式讲解和部署war项目Tomcat 1.ide启动 2.jar包方式启动 maven插件: &l ...

  6. ubuntu 上下左右键变成ABCD

    1.在ubuntu终端环境出现: 这表示你正在insert mode.... 按esc,回到command mode,上下左右就回复到正常的方向键功能了 2.可能写的程序是在insert mode(r ...

  7. linux笔记_day04

    1.cat 连接并显示 -n 显示行号 -E END 显示行尾 2.tac 从后往前显示 3.ctrl +C 4.more 向后翻  到最后会退出 5.less 翻到最后不退出 常用 支持b k sp ...

  8. LOJ 3043: 洛谷 P5280: 「ZJOI2019」线段树

    题目传送门:LOJ #3043. 题意简述: 你需要模拟线段树的懒标记过程. 初始时有一棵什么标记都没有的 \(n\) 阶线段树. 每次修改会把当前所有的线段树复制一份,然后对于这些线段树实行一次区间 ...

  9. Linux组管理和权限管理

    ⒈Linux组基本介绍 1)在Linux中的每个用户必须属于一个组,不能独立于组外. 2)Linux中每个文件都有所有者.所在组.其它组的概念 ①所有者 一般(默认)为文件的创建者,谁创建了该文件,就 ...

  10. git命令行工作环境配置【转】

    转自:http://www.cocoachina.com/ios/20171115/21163.html 本文为CocoaChina网友whf5566投稿 前言 笔者一直使用git的图形化工具sour ...