Type in Chakra
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的更多相关文章
- Chakra调试笔记 TypedArray
一.TypedArray类型 TypedArray是漏洞中常见到的结构,手册用法有四 1.new TypedArray(length); //byteLength=length * sizeof(Ty ...
- salesforce 零基础学习(六十二)获取sObject中类型为Picklist的field values(含record type)
本篇引用以下三个链接: http://www.tgerm.com/2012/01/recordtype-specific-picklist-values.html?m=1 https://github ...
- 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 ...
- $.type 怎么精确判断对象类型的 --(源码学习2)
目标: var a = [1,2,3]; console.log(typeof a); //->object console.log($.type(a)); //->ar ...
- input type='file'上传控件假样式
采用bootstrap框架样式 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> &l ...
- mount报错: you must specify the filesystem type
在linux mount /dev/vdb 到 /home 分区时报错: # mount /dev/vdb /homemount: you must specify the filesystem ty ...
- error C4430:missing type specifier 解决错误
错误 3 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int ...
- 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 ...
- OpenCASCADE Ring Type Spring Modeling
OpenCASCADE Ring Type Spring Modeling eryar@163.com Abstract. The general method to directly create ...
随机推荐
- 酷炫的SVG 动态图标
在 loading.io 上能看到好多效果惊艳的loading图标.它们都是用svg写成的,寥寥几 ...
- javascript 实现手风琴特效
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- bzoj千题计划305:bzoj2565: 最长双回文串(回文自动机)
https://www.lydsy.com/JudgeOnline/problem.php?id=2565 正着构造回文自动机 倒过来再构造一个回文自动机 分别求出以位置i开始的和结尾的最长回文串 # ...
- PHP 进行支付宝开发中return_url和notify_url的区别分析
在支付宝处理业务中return_url,notify_url是返回些什么状态呢,我们要根据它来做一些处理就必须了解return_url,notify_url的区别,下面我就来给大家介绍; 一.问题描述 ...
- HTML语义化
什么是HTML语义化呢? 根据内容的结构化(内容语义化),选择合适的标签(代码语义化),便于开发者阅读,写出优雅的代码的同时让浏览器的爬虫更好的解析 语义化标签的优势: 1)代码结构清晰,方便阅读 2 ...
- 自动升级CentOS Python至官方最新版
#!/bin/bash # .检查当前系统Python版本 python_old_version=$(python -V >& | awk '{print $2}') echo &quo ...
- luogu P4036 [JSOI2008]火星人
传送门 很久以前xzz大佬就喊我做这题,结果现在才做qwq 因为要在序列中插入,所以直接用\(Splay\)维护这个串的哈希值,插入就直接把那个点插♂进去,修改就把点旋到根,然后修改和pushup,询 ...
- 阿里云apache服务器外网无法访问(配置安全组,添加80服务)
CentOS的系统 ,已经安装好了 apache php mysql 常规排错过程(ps:没耐心的童鞋请直接看最后一步,学习在阿里云控制台配置 安全组,允许 http服务) 第一步:检查apache ...
- spring源码学习1
1.下载源码: https://github.com/spring-projects/spring-framework中找到链接: git clone https://github.com/sprin ...
- js代码解析原则
js引擎在读取js代码时会进行两个步骤,第一个步骤是解释,第二个步骤是执行. 解释就是先通篇扫描所有的Js代码,然后把所有声明提升到顶端,第二步是执行,执行就是执行代码的操作. 例: 例子1: < ...