我寫了一個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}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章