I wrote a TypeScript virtual machine: Tser. Github address: Tser-project /tser

Install and use

$ brew tap tser-project/tser && brew install tser;

$ tser ./input.ts;
Copy the code

Why was Tser created?

TypeScript (TS) is a great invention that allows us to develop statically typed languages while reusing the JS ecosystem. TS is essentially a pre-compiled language, which is compiled to JS and then executed by JS virtual machine. Due to its strong dependence on JS, IT cannot get rid of some stubborn problems of JS, such as execution efficiency. TS itself is a statically typed language, with a certain datatype token, but the type token is lost when escaping to JS. If we can execute TS programs directly, rather than escaping them to JS and then executing them, these data type tags can provide a significant performance boost.

Let’s look at a set of performance comparison data. We only compare fib(42) performance in each VIRTUAL machine or language (it cannot be used as a performance evaluation criterion completely; Test conditions: same device, same state, no optimizations used during compilation).

language The virtual machine Execution Time (ms)
TypeScript deno 4150
JavaScript v8 / node 3859
TypeScript Tser 2035
C++ 2106

TS technology is developing rapidly, and there are more and more projects using TS for development and refactoring. Looking beyond the current development of TS technology, where does it end? Will you stay with a precompiled language? When TS ecological development is more and more sound, is it necessary to completely rely on JS ecology? Will there be a true TS virtual machine (Deno is not)? If the industry has a stable and high-performance TS virtual machine, is it a good thing for the TS ecosystem? Will it push TS to the next level?

I thought about these questions for a long time.

TS should not replace JS ecology, but in some fields, TS can be separated from JS ecology and independent existence; TS virtual machines are the cornerstone of the independent TS ecosystem, enabling TS to perform significantly better than JS at runtime in these areas and bring real business benefits to these areas.

What can a Tser do?

Tser compilation performance is relatively low but runtime performance is high, more suitable for independent background services, Serverless and other scenarios. If you can run most of the existing backend services written in TS with good syntax support, these services can get a significant performance boost.

There is very little Tser can do at the moment because the syntax support is not perfect and it can only support the execution of simple scripts, such as some simple cloud function scenarios.

Tser technology principle

Tser front-end relies on the syntax parser generated by Antlr, and then generates and traverses the syntax tree. The back end relies on LLVM construction to compile TS code to LLVM IR and execute IR immediately using its JIT engine.

Similar products: AssemblyScript, StaticScript

Tser syntax support

variable

Var let const. Var is the same as let.

The base type

type Bytes (in 64 – bit) support
boolean 1 ✔ ️
number 4 ✔️ 同int32
int32 4 ✔ ️
int64 8 ✔ ️
float 4 ✔ ️
double 8 ✔ ️
string ✔️ Calculation is not supported

The operator

The operator support
+ - * / % ✔ ️
++ -- ✔ ️
+ = - = * = / = % = ✔ ️
< > < = > = ✔ ️
= = = = = ! = ! = = ✔ ️= = =with= =So far there is no difference
&& lots ✔ ️
! ✔ ️
? : ✔ ️
(a) ✔ ️
. ✔ ️

Logical control statement

statements support
if else ✔ ️
while do while ✔ ️
for ✔ ️
switch ✔ ️
continue ✔ ️
break ✔ ️

function

Supports most function functions, function nesting, temporarily does not support closures and function parameters.

Class

Class support inheritance, polymorphism, support Class combination, support static properties and static methods, do not support method overload; Class inheritance and polymorphism are supported in a manner similar to virtual tables, with polymorphism supporting methods and properties.

Built-in objects

Currently, built-in object support is very low and used only for testing.

Built-in objects methods
console debug log info warn error
Date now

Temporary does not support

Module built-in objects, Event loops, GC, and more.

Contribution Tser

Tser is a huge project that is difficult to complete on one’s own. Tser is still a baby now, and I hope it can play a role of casting a brick to attract jade, and gather some capable people to build together.


Flash list, a different suspension list software.