🚨 Shai-Hulud Strikes Again:834 Packages Compromised.Technical Analysis β†’
Socket
Book a DemoInstallSign in
Socket

compilets

Package Overview
Dependencies
Maintainers
0
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

compilets

Compile TypeScript to C++ Executables

Source
npmnpm
Version
0.0.3
Version published
Weekly downloads
2
-83.33%
Maintainers
0
Weekly downloads
Β 
Created
Source

Compilets

TypeScript to C++ compiler.

To have an idea of how the converted code looks like, you can check the tests/data-conversion/ directory.

Note that this is an ongoing research and not production ready, if you are looking for a compiler that works with real code, please check TypeScript2Cxx and lots of other options.

CLI

Install:

npm install -g compilets

Help:

━━━ Compilets - 0.0.1-dev ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

  $ compilets <command>

━━━ General commands ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

  compilets build [--target #0]
    Build C++ project.

  compilets gen [--root #0] [--target #0]
    Generate a C++ project from TypeScript project.

Example:

mkdir example
cd example
echo 'class A {}' > main.ts
compilets gen
compilets build
./cpp-project/out/Debug/example

Principles

  • Only the static part of TypeScript will be supported - for example there is no support for prototype manipulation, object literal and Record are separate types that can not be converted to each other.
  • Interoperability with Node.js - the TypeScript code should be able to be compiled into a native module that works with the JavaScript code.
  • Language support comes first than runtime - this project will focus on implementing core TypeScript language support first, things like Object and String's methods will only be implemented at very last.

The unusual parts

The reason this project was created despite the existences of all other ones, is that I believe the C++ and TypeScript languages have some subtle overlaps and there is a beautiful way to map TypeScript code to C++.

This project means to validate my ideas which I did not find anywhere else.

Oilpan GC

The Oilpan GC is a garbage collection library designed for V8, but also can be used as an independant library. Compilets uses Oilpan to implement the GC in translated C++ code.

Most existing TypeScript to C++ compilers manage the lifetime of objects with std::shared_ptr, which is simple reference counting and leaks for cyclic references.

With Compilets you can safely translate a cyclic referenced class to C++:

class LinkNode {
  next?: LinkNode;
}

const a = new LinkNode();
const b = new LinkNode();
a.next = b;
b.next = a;
class LinkNode : public compilets::Object {
 public:
  cppgc::Member<LinkNode> next;

  void Trace(cppgc::Visitor* visitor) const override {
    visitor->Trace(next);
  }
};

LinkNode* a = compilets::MakeObject<LinkNode>();
LinkNode* b = compilets::MakeObject<LinkNode>();
a->next = b;
b->next = a;

Function object

In TypeScript a function is also an Object, while it is trivial to use lambda functions in C++ to represent functions objects, the lifetime of the function and the objects captured in its closure still needs management. In the meanwhile using std::function for everything would hurt the performance.

This is solved by using a custom C++ object to represent function expressions and arrow functions (i.e. the ones assigned to variables), while still using the plain C++ functions to represent the top-level function declarations.

For lifetime management, the function object is managed by Oilpan GC. And the function body is parsed so all the objects captured in the closure become hiddden members of the function object, managed by Oilpan GC too.

function Simple() {}

let simple = Simple;
let callback = () => { simple() };
void Simple() {}

compilets::Function<void()>* simple = compilets::MakeFunction<void()>(Simple);
compilets::Function<void()>* callback = compilets::MakeFunction<void()>([=]() -> void {
  simple->value()();
}, simple);

Union types and std::variant

The union types in TypeScript are represented as std::variant in C++, for example number | string becomes std::variant<double, std::string>.

For union types that includes undefined, the std::monostate is used to represent the empty state.

Question mark and std::optional

The optional function parameters and class properties in TypeScript are represented as std::optional in C++. For example func(arg?: boolean) becomes func(std::optional<bool> arg).

null and undefined

Unlike JavaScript, there is no undefined state for variables in C++, it is possible simulate undefined but it would be at the cost of performance.

Thus in Compilets both null and undefined are treated as null states of types: std::nullopt for std::optional, std::monostate for std::variant, std::nullptr for other pointer types. This will of course break some even strictly typed code, and to avoid generating incorrect code, errors will be thrown when the TypeScript code needs to strictly distinguish between null and undefined.

Developement

The documentations of Oilpan GC (cppgc) can be found at:

You can get familiar with TypeScript's compiler APIs with following articles:

Tools below will help developement of this project:

License

This project is published under GPLv3 license, including the C++ files that are built into the final binary.

I'll change the license to a permissive one when I consider this project as a ready product.

Contributions

It is discouraged to submit patches as this project is still an ongoing experiment. If you are still kind enough to fix bugs, please note that you must agree to allow me to re-license the contributions to permissive licenses in future.

FAQs

Package last updated on 08 Aug 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts