我写了一个TypeScript虚拟机。

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我写了一个TypeScript虚拟机: Tser。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Github地址:"},{"type":"link","attrs":{"href":"https://github.com/tser-project/tser","title":""},"content":[{"type":"text","text":"tser-project/tser"}]},{"type":"text","text":"。"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"安装使用"}]},{"type":"codeblock","attrs":{"lang":"bash"},"content":[{"type":"text","text":"$ brew tap tser-project/tser && brew install tser;\n\n$ tser ./input.ts;"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"为什么要创造Tser?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"TypeScript(TS)是一个伟大的发明,让我们在复用JS生态的同时拥有了静态类型语言的开发体验。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"TS本质是一门预编译语言,编译到JS后再使用JS虚拟机执行,由于强依赖于JS,也因此无法摆脱JS的一些顽疾,比如执行效率。而TS本身是静态类型语言,拥有确定的数据类型标记,只是在转义为JS时丢失了类型标记;如果我们能直接执行TS程序,而不是先转义为JS再执行,这些数据类型标记可以为程序带来很大的性能提升。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我们来看一组性能对比数据,仅对比"},{"type":"codeinline","content":[{"type":"text","text":"fib(42)"}]},{"type":"text","text":"在各个虚拟机或语言中的性能表现(并不能完全依此来作为性能评价标准;测试条件:同一设备同一状态,编译过程均未使用任何优化)。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/61/61a3889c9e071382704172b2ad175288.png","alt":null,"title":"","style":[{"key":"width","value":"100%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"TS技术发展很快,业界已经有越来越多的项目使用TS开发和重构;抛开眼下去看TS技术的发展,它的终点会在哪里?会一直停留在一个预编译语言上吗?当TS生态发展越来越健全,是不是还有必要完全依托于JS的生态?业界会不会诞生一个真正的TS虚拟机(Deno并不是)?如果业界有一个稳定且高性能的TS虚拟机,对TS生态是不是一件好事,会不会将TS推向一个新的高度?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"italic"}],"text":"TS应该无法取代JS的生态,但是在某些领域,TS可以脱离JS生态而独立存在;TS虚拟机是独立TS生态的基石,可以让TS在这些领域有明显优于JS的运行时表现,并为这些领域带来实际的业务收益。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"Tser能做什么?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Tser编译性能相对不高而运行时性能高,更适合独立后台服务、Serverless等场景。如果语法支持完善,可以运行绝大部分现有的使用TS编写的后台服务时,这些服务可以因此获得很大的性能提升。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Tser目前能做的事情还非常少,因为语法支持很不完善,仅能支持一些简单脚本的执行,比如应该能够支持一些简单的云函数场景。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"Tser技术原理"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Tser前端依托于Antlr来生成的语法解析器,然后进行语法树的生成与遍历;后端依托于LLVM建设,将TS代码编译为LLVM IR,并使用其JIT引擎立即执行IR。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"业界雷同产品: "},{"type":"link","attrs":{"href":"https://www.assemblyscript.org/","title":""},"content":[{"type":"text","text":"AssemblyScript"}]},{"type":"text","text":"、"},{"type":"link","attrs":{"href":"https://github.com/ovr/StaticScript","title":""},"content":[{"type":"text","text":"StaticScript"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"Tser语法支持"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"变量"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"支持"},{"type":"codeinline","content":[{"type":"text","text":"var"}]},{"type":"text","text":" "},{"type":"codeinline","content":[{"type":"text","text":"let"}]},{"type":"text","text":" "},{"type":"codeinline","content":[{"type":"text","text":"const"}]},{"type":"text","text":", "},{"type":"codeinline","content":[{"type":"text","text":"var"}]},{"type":"text","text":"与"},{"type":"codeinline","content":[{"type":"text","text":"let"}]},{"type":"text","text":"相同。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"基础类型"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/8f/8ff6c93629b9c8e70e3bb86991aaba89.png","alt":null,"title":"","style":[{"key":"width","value":"100%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"运算符"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/34/341ddb9efb8ae8bb1ec05bd8ae4e8275.png","alt":null,"title":"","style":[{"key":"width","value":"100%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"逻辑控制语句"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/49/491049198c7a006d12c93f5c273e25de.png","alt":null,"title":"","style":[{"key":"width","value":"100%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"函数"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"支持绝大部分函数功能,函数嵌套,暂不支持闭包和函数参数。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"Class"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Class支持继承、多态,支持Class组合,支持静态属性与静态方法,不支持方法重载;Class继承和多态使用类似虚拟表的方式来支持,多态支持方法和属性。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"内置对象"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"目前内置对象支持力度非常低,仅用于测试。"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/1f/1f5deec8bf7a63aa563065072b86567a.png","alt":null,"title":"","style":[{"key":"width","value":"100%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"暂不支持"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"module"}]},{"type":"text","text":" "},{"type":"codeinline","content":[{"type":"text","text":"内置对象"}]},{"type":"text","text":"、"},{"type":"codeinline","content":[{"type":"text","text":"Event Loop"}]},{"type":"text","text":"、"},{"type":"codeinline","content":[{"type":"text","text":"GC"}]},{"type":"text","text":"等等。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"贡献Tser"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Tser是一个巨大的工程,很难用一己之力完成。Tser现在还是一个baby,更希望它能起到一个抛砖引玉的作用,能聚集起一些有能力的人来一起建设。"}]},{"type":"horizontalrule"},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"italic"}],"text":" "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://flicker.cool/?mark=art_tser_1_infoq","title":""},"content":[{"type":"text","text":"闪点清单,一款不一样的悬浮清单软件。"}]}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/86/860113c2ae6929e0b55e9cfb221e5a39.gif","alt":"闪点清单,一款不一样的悬浮清单软件","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章