bytecodealliance / wasm-micro-runtime

WebAssembly Micro Runtime (WAMR)
Apache License 2.0
4.99k stars 628 forks source link

Compiling TypeScript to WebAssembly #2037

Open xujuntwt95329 opened 1 year ago

xujuntwt95329 commented 1 year ago

Compiling TypeScript to WebAssembly

WebAssembly lacks of application programming language, currently supported languages are mainly for system programming (C/C++/Rust). Some approaches embed a whole language runtime inside WebAssembly to support other languages (JS, python), but this is not friendly to footprint and performance. On the other hand, TypeScript, as a statically-typed super set of JavaScript, can likely run faster on WebAssembly if we make use of the type information during compilation.

This issue is to track and discuss possible approaches to support compiling TypeScript to WebAssembly.

Motivation

Experimental implementation

https://github.com/bytecodealliance/wasm-micro-runtime/pull/2034

SIG

https://github.com/bytecodealliance/governance/pull/28/files

wenyongh commented 1 year ago

The source code of ts2wasm compiler is under branch ts2wasm_experiment of WAMR: https://github.com/bytecodealliance/wasm-micro-runtime/tree/ts2wasm_experiment

Joneyao commented 1 year ago

请问一下 当前 ts2wasm 项目编译生成的wasm模块,只能通过v8引擎运行吗? 其他常用的wasm 运行时无法运行?这个是什么原因?是因为当前的wasm内部有什么特殊规范吗?

Joneyao commented 1 year ago

image

xujuntwt95329 commented 1 year ago

@Joneyao ts2wasm rely on the WebAssembly GC proposal, we use latest version of binaryen to generate GC related opcodes, these opcodes only compatible to V8 version 11+, so currently we can only run the wasm module in these two approaches:

  1. download latest V8 source code, and compile a d8 executable, see run_generated_module for more details
  2. run in the browser (latest chrome is shipped with V8 version 11+), we will add some document on how to do this.

Note that the functionality of std libraries (like console.log) is under development, so currently the wasm module may lack of output capabilities, you can use chrome dev-tools to inspect the internal data (local/globals) inside the VM

goodcodedev commented 1 year ago

Did you consider https://github.com/dudykr/stc as a possible base for typechecking? I don't know if it's possibly more mallable to the wasm case, anyway likely faster.

xujuntwt95329 commented 1 year ago

@goodcodedev Thanks for suggesting such a wonderful project, we do find that tsc is relatively slow to parse the source code, but currently we are focusing on how to support TypeScript's rich semantic based on WasmGC, so replacing the type checker may not be on the top priority.

I believe the compilation speed is very important for developer experience, so we may need to consider this in the future, and https://github.com/dudykr/stc you suggested seems a great approach :), we may make this as a topic in the future SIG meeting, and any further suggestions and discussions are welcome.

acomagu commented 1 year ago

The source code of ts2wasm compiler is under branch ts2wasm_experiment of WAMR: https://github.com/bytecodealliance/wasm-micro-runtime/tree/ts2wasm_experiment

This branch seems to be removed or renamed. Where did it go?