Comments (4)
Hi,
Thanks for your feedback! Yes I tried Haxe but there are some problems with it (see section "Ts2hx and Haxe" below). Many people ask questions along same lines, so let's convert this answer into a little "wiki" if you don't mind - maybe I'll incorporate this into the main README.md later.
IoT and wearables: challenges and constraints
One of the main goals of this project is JavaScript/TypeScript on IoT devices and wearables.
I have in mind first of all ESP8266, Pebble watch, MSP430 family and Atmel AVR family (used in Arduino boards), which means microcontrollers and very small amounts of memory available.
So, RAM ranges literally from 512 bytes to 120KB, and ROM/Flash from 1KB to 4MB.
Why not some more powerful devices? The thing is, if you want to create a sustainable IoT device that works couple of years from battery, you have to use low-power microcontrollers and forget about things like Raspberry Pi.
But in this case, usually the only option available is to use C, which is very low-level, and many people don't know C (but everyone knows JavaScript!).
Let's take a simple example: consider I want to build a device that fetches some data from a sensor and periodically send it over WiFi to the cloud. It is probably a 2-hour deal in JavaScript, including tests, but it might easily be 2 days in C even for a person who knows C well.
I personally faced this problem when developing applications for Pebble watch. It's either Pebble.js which runs on phone and sends all the UI updates via Bluetooth, consuming battery like crazy, or you have C API and 24 KB maximum binary size. They're now spending huge amount of efforts trying to bring JavaScript interpreter on board, but it likely will be supported only on newer watches that have more memory and better CPU.
JavaScript on microcontrollers: current state
Many people understand that we need to bring JS to microcontrollers, and nowadays even big companies like Samsung create JavaScript interpreters tailored specifically for microcontrollers and IoT. However these projects even in best case have relatively big overhead.
For example:
- v7: base binary size 40-120KB and initialization memory footprint starts from 16KB;
- jerryscript: 160Kb binary size and 64KB RAM
- espruino: 128KB Flash and 8KB RAM
Ts2hx and Haxe
I tried this combination a while ago. These are the problems I spotted:
- Haxe compiles to C++, not to C
- It has relatively huge runtime "shipped" with the application
- "Hello world" example produces 900KB executable
- Haxe more like compiles, not transpiles - so mostly unreadable output, e.g. this is what a simple method call looks like:
HX_STACK_LINE(18)
::String tmp1 = f->myMethod(HX_HCSTRING("test","\x52","\xc8","\xf9","\x4c"));
HX_STACK_VAR(tmp1,"tmp1");
TS2C project
Below are some stand-out features of TS2C:
- I'm adding only the stuff that developer actually uses in his code. No dead code is ever added during transpilation.
- I'm using only C89 features sticking to ANSI/ISO 9899-1990 International Standard as some microcontrollers don't support anything newer.
- I'm putting significant efforts into ensuring that output is as readable as possible and maps well to the original source.
I realize that some JavaScript language features are have to be left out, first of all certainly eval
, but current impression is that it is possible to transpile most of the language.
Let's see :)
from ts2c.
Thanks for the feedback @endel!
At this point the idea is to support only JS language core and built-in objects, and then if you need e.g. OpenGL, you can do something like import {glColor, ...} from 'GL/gl.h';
and call those functions you imported, and TS2C just generates #include <GL/gl.h>
and transpiles the function calls.
So the code is not really intended to be fully universal, maybe only parts of it, like pure functions, etc.
This is in part because of idea to program microcontrollers - they all have very different APIs and often don't even fully support standard library as in C89 spec, so it's quite unrealistic to create any common interface for them.
Of course it should be possible to make some wrappers so that WebGL API transpiles to corresponding OpenGL API, but that would be a separate project 😉
Anyway, even at this point it feels like ✨ magic ✨ - you create quite high-level JS code and boom - it transpiles into C-code and you start looking into it and you understand that you would not write much better yourself 😜
from ts2c.
Actually there is already a project that transpiles TS to Haxe and then you can compile it to C... it sort of works but it needs some updating:
jeremyfa/node-ts2hx
from ts2c.
Nice initiative. 👍
The idea is kinda crazy, but I'm really looking forward to seeing what can be achieved out of this. I often find myself thinking that it would be INSANE to be able to create a game using WebGL/TypeScript and "magically" transpile it down to C. Just hit the watch button. ;)
Cheers!
from ts2c.
Related Issues (20)
- Calling C Functions
- Feature: Classes (for TS at least) HOT 8
- Finishing ES3 features once and for all HOT 6
- Compiling TypeScript to C via WebAssembly HOT 2
- Please include the original code as a comment in the transpiled code HOT 12
- Memory allocation bug HOT 6
- Function.prototype.toString HOT 1
- C functions HOT 2
- Syntax errors are handled incorrectly
- Playground autocomplete keeps repeating
- Order in which properties are added is not honored
- Incorrect generation of 'free' call
- Using C++ instead of C? HOT 4
- npm - Unexpected end of JSON
- Error when combining "for in" and "for of"
- labelled statements don't work
- Ain't work HOT 1
- arguments array ain;t work
- problem with functions called main
- error with functions returning functions HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ts2c.