andrei-markeev / ts2c

Convert Javascript/TypeScript to C
ISC License
1.26k stars 95 forks source link

Finishing ES3 features once and for all #69

Closed decahedron1 closed 4 years ago

decahedron1 commented 4 years ago

We are really, really close to finishing all ES3 features. I'd love to implement all of these, but I'm a bit busy right now...

Dates will take a lot of work; let's not touch it for now.

Regexes. Oh boy. We could use something like SLRE which seems pretty promising and light.

As for functions, I think we should completely redesign the way we define & call them in order to have .call(), .apply(), and arguments. The first parameter of every function will be their own this object. When calling a function, then, if it's not .call()ed, then we just pass either a global this (like window in the browser or global on node) or the parent object of the function. For example, a.b.c() -> a.b.c(a.b), but foo() -> foo(GLOBAL_THIS). The second parameter would be an array of arguments, simply called arguments. Then, we parse the JS function parameters and define variables corresponding to each argument in the array.

So, function foo(a, b, c) { ... } would turn into... (pseudocode)

void foo(object this, array arguments) {
    type a = arguments[0];
    type b = arguments[1];
    type c = arguments[2];
    ...
}

Then, we can implement global this by creating an object called GLOBAL_THIS and every global variable defined will be put into that object.

Also, I might make a PR soon to change int16_t to size_t, so all processors are happy.

adrien-thierry commented 4 years ago

Hi @eclipticccc ,

I'm developing NectarJS ( https://github.com/NectarJS/nectarjs ) and we are able to compile a good part of js, object, class ... Our function system looks like what you described.

We use a c++ backend to achieve that, and NectarJS is right now compatible with a lot of environment ( Windows, Mac, Linux, Android, iPhone, Arduino ...)

We will target to properly finish to compile ES3 before the other specs, but the intetesting part is that we can also compile TypeScript, and it's easy to include C or C++ lib with our ffi system (kind of c++ js macro). We also manage infinity, Object, JSON, and we are starting Date and Regexp. We are experimenting some databases module (mysqlx). We use different --env flags regarding the compilation target (std, node, android, ios...), and we started to integrate LibUV for Node env.

Memory is completly managed in real time without garbage collector and we beat NodeJS with mathematical functions (you can try fibo.js)

Don't hesitate to take a look at the project and to submit issues.

kungfooman commented 4 years ago

Oh god, AGPL

adrien-thierry commented 4 years ago

@kungfooman we will change it soon

decahedron1 commented 4 years ago

@adrien-thierry NectarJS looks great! However, the license is a bit disappointing as kungfooman said. Right now I'd say NectarJS and ts2c are about the same in terms of ES3 support. Nectar looks really promising though, and maybe ts2c can learn from some parts of Nectar.

I think Nectar would be perfect if:

I won't go too much farther as this is a ts2c issue. If you'd like I could make some pulls to address some of these issues to the best of my ability (again, not too good with C).

As for ts2c though, I feel that functions being objects should be our main priority. If we can get this implemented then we can finally finish classes.

adrien-thierry commented 4 years ago

@eclipticccc i will add . prototype support, object are working in NectarJS, i just have to add a default property to objects to support it.

In NectarJS, functions are objects and this is already implemented.

Object, Array, Date, JSON are already builtin

Don't hesitate to open issues on NectarJS. Right now, we are developing what we need for production, if people Can Say what they need, we can add it.

adrien-thierry commented 4 years ago

Btw, the license problem will be solved very soon