ts2c
1.0.0
從JS/TS代碼生成可讀的C89代碼。
例如,此JavaScript:
console . log ( "Hello world!" ) ;轉到以下C代碼:
#include <stdio.h>
int main () {
printf ( "Hello world!n" );
return 0 ;
}從來沒有生成實際不需要的過多代碼。
輸出盡可能可讀性,並且主要映射到原始代碼。
另一個例子:
var obj = { key : "hello" } ;
obj [ "newKey" ] = "test" ;
console . log ( obj ) ;轉到以下C代碼:
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
struct obj_t {
const char * key ;
const char * newKey ;
};
static struct obj_t * obj ;
int main ( void ) {
obj = malloc ( sizeof ( * obj ));
assert ( obj != NULL );
obj -> key = "hello" ;
obj -> newKey = "test" ;
printf ( "{ " );
printf ( "key: "%s"" , obj -> key );
printf ( ", " );
printf ( "newKey: "%s"" , obj -> newKey );
printf ( " }n" );
free ( obj );
return 0 ;
}正在進行的工作:它有效,但目前只支持ES3規範的70% :語句和表達式-95%,內置對象-17%。
值得注意的不支持的功能包括:float和大數字(當前所有數字均為int16_t ), eval , Date , Math等。
有關支持和計劃功能的詳細信息,請參見Coverage.md。
歡迎捐款!請參閱src/readme.md
您可以自己在線嘗試:
該項目的主要動機是解決問題,即目前無法通過JavaScript對物聯網和可穿戴設備進行有效編程。
事實是,對於可以在單個電池上長時間工作的可持續物聯網設備,諸如Raspberry Pi之類的事情將無法使用。您將必須使用低功率微控制器,這些微控制器通常幾乎沒有可用的內存。
RAM從字面上的範圍從512個字節到120KB,ROM/Flash從1KB到4MB。在這種情況下,即使是優化的JS解釋器,例如JerryScript,Espruino或V7有時是太多的開銷,通常會導致電池耗盡的增加和/或不會為您的程序留下大量的系統資源。
當然,Transpiler不能繪製JavaScript語言的100%,並且必須遺漏某些內容,特別是eval 。儘管如此,目前的結論是,可以傳播大多數語言。
計劃的轉卸目標:
命令行:
npm install -g ts2c
句法:
ts2c < files to transpile >node.js:
npm install ts2c
const ts2c = require ( "ts2c" ) ;
const cCode = ts2c . transpile ( "console.log('Hello world!')" ) ;
console . log ( cCode ) ;在瀏覽器中:
< script src =" https://unpkg.com/typescript " > </ script >
< script src =" ts2c.bundle.js " > </ script >
< script >
var cCode = ts2c . transpile ( "console.log('Hello world!')" ) ;
alert ( cCode ) ;
</ script >