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. 多目标遗传算法 ------ NSGA-II (部分源码解析)二元锦标赛选择 tourselect.c

    tourselect.c  文件中共有两个函数: selection (population *old_pop, population *new_pop) individual* tournament ...

  2. jedis实现操纵redis的常用api及使用场景

    简单记录一下,和描述一下常用的业务场景.好记性不如烂笔头. pom.xml <!--整合redis--> <dependency> <groupId>redis.c ...

  3. 把svn上的mycelipse导到本地的eclipse中【原】

    myeclipse和eclipse的web项目互导时会产生各种问题,现在把我遇到的情况记录如下: eclipse如何把svn上down下来的myeclipseWeb项目变成eclipse的Web项目: ...

  4. JAVA求解全排列

    一,问题描述 给定一个字符串,求出该字符串的全排列. 比如:"abc"的全排列是:abc.acb.bac.bca.cab.cba 二,实现思路 采用递归的方式求解.每次先选定一个字 ...

  5. android BroadcastReceiver组件简单的使用

    1.清单文件 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android=& ...

  6. CentOS6.8安装配置sonarqube6.4

    下载最新版本的sonar(现在改名叫sonarqube) https://www.sonarqube.org/downloads/ 我下载的版本是Sonarqube6.4        1 使用前需要 ...

  7. C static struct

    参考链接:  http://blog.csdn.net/keyeagle/article/details/6708077/ NOTICE: 静态全局变量 与 普通的全局变量的区别   static 全 ...

  8. Android性能优化系列之Bitmap图片优化

    https://blog.csdn.net/u012124438/article/details/66087785 在Android开发过程中,Bitmap往往会给开发者带来一些困扰,因为对Bitma ...

  9. TeamCity 和 Nexus 的使用

    参考:http://www.jianshu.com/p/255a484555d9 TeamCity 安装部署(Linux 环境) 在我讲之前,如果你英文还可以,就到官网这里看下: Installati ...

  10. UML和模式应用5:细化阶段(8)---逻辑架构和UML包图

    1.前言 本章是从面向分析的工作过度到软件设计 典型的OO系统设计的基础是若干架构层,如UI层.应用逻辑(领域)层 本章简要考察逻辑分层架构和相关UML表示法 2.逻辑架构和层 逻辑架构 逻辑架构是软 ...