• Stars
    star
    571
  • Rank 77,705 (Top 2 %)
  • Language
    C++
  • License
    MIT License
  • Created over 3 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

TypeScript Compiler (by LLVM)

TypeScript Native Compiler

Powered by LLVM|MLIR

Donate

Build

Test Build (Windows) Test Build (Linux)

What's new

  • Shared libraries shared.ts - shared library file:
export const val_num = 2.5;
export const val_str = "Hello World! - val";

export function test1()
{
	print("Hello World! test 1");
}

export function test2()
{
	print("Hello World! test 2");
}
  • Load shared library
import './shared'

function main()
{
	test1();
	test2();

	print(val_str);

	print(val_num);

	print("done.");
}
  • Debug information: option --di in tsc
tsc --opt_level=0 --di --emit=obj <file>.ts

Planning

  • Migrating to LLVM 16.0.3
  • Shared libraries (work in progress)
  • JavaScript Built-in classes library

Demo

(click here)

Demo

Try it

(click here)

Compiler Explorer

Chat Room

Want to chat with other members of the TypeScriptCompiler community?

Join the chat at https://gitter.im/ASDAlexander77/TypeScriptCompiler

Example

abstract class Department {
    constructor(public name: string) {}

    printName(): void {
        print("Department name: " + this.name);
    }

    abstract printMeeting(): void; // must be implemented in derived classes
}

class AccountingDepartment extends Department {
    constructor() {
        super("Accounting and Auditing"); // constructors in derived classes must call super()
    }

    printMeeting(): void {
        print("The Accounting Department meets each Monday at 10am.");
    }

    generateReports(): void {
        print("Generating accounting reports...");
    }
}

function main() {
    let department: Department; // ok to create a reference to an abstract type
    department = new AccountingDepartment(); // ok to create and assign a non-abstract subclass
    department.printName();
    department.printMeeting();
    //department.generateReports(); // error: department is not of type AccountingDepartment, cannot access generateReports
}

Run

tsc --emit=jit --opt --shared-libs=TypeScriptRuntime.dll example.ts

Result

Department name: Accounting and Auditing
The Accounting Department meets each Monday at 10am.

Run as JIT

  • with Garbage collection
tsc --emit=jit --opt --shared-libs=TypeScriptRuntime.dll hello.ts
  • without Garbage collection
tsc --emit=jit --nogc hello.ts

File hello.ts

function main() {
    print("Hello World!");
}

Result

Hello World!

Compile as Binary Executable

On Windows

  • with Garbage collection

File tsc-compile-gc.bat

set FILENAME=%1
set LLVMEXEPATH=C:/dev/TypeScriptCompiler/3rdParty/llvm/release/bin
set LLVMLIBPATH=C:/dev/TypeScriptCompiler/3rdParty/llvm/release/lib
set TSCLIBPATH=C:/dev/TypeScriptCompiler/__build/tsc-release/lib
set TSCEXEPATH=C:/dev/TypeScriptCompiler/__build/tsc-release/bin
set GCLIBPATH=C:/dev/TypeScriptCompiler/3rdParty/gc/Release
set LIBPATH="C:/Program Files/Microsoft Visual Studio/2022/Professional/VC/Tools/MSVC/14.35.32215/lib/x64"
set SDKPATH="C:/Program Files (x86)/Windows Kits/10/Lib/10.0.22000.0/um/x64"
set UCRTPATH="C:/Program Files (x86)/Windows Kits/10/Lib/10.0.22000.0/ucrt/x64"
%TSCEXEPATH%\tsc.exe --opt --emit=obj C:\temp\%FILENAME%.ts -o=%FILENAME%.o
%LLVMEXEPATH%\lld.exe -flavor link %FILENAME%.o /libpath:%LIBPATH% /libpath:%SDKPATH% /libpath:%UCRTPATH% /libpath:%LLVMLIBPATH% /libpath:%GCLIBPATH% /libpath:%TSCLIBPATH% msvcrt.lib ucrt.lib kernel32.lib user32.lib gcmt-lib.lib TypeScriptAsyncRuntime.lib LLVMSupport.lib

Compile

tsc-compile-gc.bat hello
  • without Garbage collection

File tsc-compile-nogc.bat

set FILENAME=%1
set LLVMEXEPATH=C:/dev/TypeScriptCompiler/3rdParty/llvm/release/bin
set LLVMLIBPATH=C:/dev/TypeScriptCompiler/3rdParty/llvm/release/lib
set TSCLIBPATH=C:/dev/TypeScriptCompiler/__build/tsc-release/lib
set TSCEXEPATH=C:/dev/TypeScriptCompiler/__build/tsc-release/bin
set LIBPATH="C:/Program Files/Microsoft Visual Studio/2022/Professional/VC/Tools/MSVC/14.35.32215/lib/x64"
set SDKPATH="C:/Program Files (x86)/Windows Kits/10/Lib/10.0.22000.0/um/x64"
set UCRTPATH="C:/Program Files (x86)/Windows Kits/10/Lib/10.0.22000.0/ucrt/x64"
%TSCEXEPATH%\tsc.exe --opt -nogc --emit=obj C:\temp\%FILENAME%.ts -o=%FILENAME%.o
%LLVMEXEPATH%\lld.exe -flavor link %FILENAME%.o /libpath:%LIBPATH% /libpath:%SDKPATH% /libpath:%UCRTPATH% /libpath:%LLVMLIBPATH% /libpath:%TSCLIBPATH% msvcrt.lib ucrt.lib kernel32.lib user32.lib TypeScriptAsyncRuntime.lib LLVMSupport.lib

Compile

tsc-compile-nogc.bat hello

Run

hello.exe

Result

Hello World!

On Linux (Ubuntu 20.04 and 22.04)

  • with Garbage collection

File tsc-compile-gc.sh

FILENAME=$1
TSCEXEPATH=/home/dev/TypeScriptCompiler/__build/tsc-ninja-release/bin
TSCLIBPATH=/home/dev/TypeScriptCompiler/__build/tsc-ninja-release/lib
LLVMLIBPATH=/home/dev/TypeScriptCompiler/3rdParty/llvm-ninja/release/lib
GCLIBPATH=/home/dev/TypeScriptCompiler/3rdParty/gc/release
$TSCEXEPATH/tsc --emit=obj --opt -relocation-model=pic $FILENAME.ts -o=$FILENAME.o
gcc -o $FILENAME -L$LLVMLIBPATH -L$GCLIBPATH -L$TSCLIBPATH $FILENAME.o -lgcmt-lib -lTypeScriptAsyncRuntime -lLLVMSupport -lLLVMDemangle -frtti -fexceptions -lstdc++ -lm -lpthread -ltinfo -ldl

Compile

sh -f tsc-compile-gc.sh hello
  • without Garbage collection

File tsc-compile-nogc.sh

FILENAME=$1
TSCEXEPATH=/home/dev/TypeScriptCompiler/__build/tsc-ninja-release/bin
TSCLIBPATH=/home/dev/TypeScriptCompiler/__build/tsc-ninja-release/lib
LLVMEXEPATH=/home/dev/TypeScriptCompiler/3rdParty/llvm-ninja/release/bin
LLVMLIBPATH=/home/dev/TypeScriptCompiler/3rdParty/llvm-ninja/release/lib
$TSCEXEPATH/tsc --emit=obj --opt -nogc -relocation-model=pic $FILENAME.ts -o=$FILENAME.o
gcc -o $FILENAME -L$LLVMLIBPATH -L$GCLIBPATH -L$TSCLIBPATH $FILENAME.o -lTypeScriptAsyncRuntime -lLLVMSupport -lLLVMDemangle -frtti -fexceptions -lstdc++ -lm -lpthread -ltinfo -ldl

Compile

sh -f tsc-compile-nogc.sh hello

Run

./hello

Result

Hello World!

Compiling as WASM

On Windows

File tsc-compile-wasm.bat

echo "check if your LLC support WebAssembly by running command: llc.exe --version --triple"
rem set %LLVM% and %TSCBIN%
set LLVMPATH=%LLVM%\llvm\release\bin
set TSCPATH=%TSCBIN%\tsc\bin
%TSCPATH%\tsc.exe -nogc --emit=obj --opt --mtriple=wasm32-unknown-unknown %FILENAME%.ts -o=%FILENAME%.ll
%LLVMPATH%\wasm-ld.exe %FILENAME%.o -o %FILENAME%.wasm --no-entry --export-all --allow-undefined

Compile

tsc-compile-wasm.bat hello

Run run.html

<!DOCTYPE html>
<html>
  <head></head>
  <body>
    <script type="module">
        let buffer;

        const config = {
            env: {
                memory_base: 0,
                table_base: 0,
                memory : new WebAssembly.Memory({ initial: 256}),
                table: new WebAssembly.Table({
                    initial: 0,
                    element: 'anyfunc',
                })
            }
        };

        fetch("./hello.wasm")
            .then(response =>{
                return response.arrayBuffer();
            })
            .then(bytes => {
                return WebAssembly.instantiate(bytes, config); 
            })
            .then(results => { 
                let { main } =  results.instance.exports;
                buffer = new Uint8Array(results.instance.exports.memory.buffer);
                main();
            });
    </script>
  </body>
</html>

Build

On Windows

First, precompile dependencies

prepare_3rdParty.bat 

To build TSC binaries:

cd tsc
config_tsc_debug.bat
build_tsc_debug.bat

On Linux (Ubuntu 20.04 and 22.04)

First, precompile dependencies

chmod +x *.sh
./prepare_3rdParty.sh

To build TSC binaries:

cd tsc
chmod +x *.sh
./config_tsc_debug.sh
./build_tsc_debug.sh