一个新的玩具,在 js 里套娃 c 编译器
資深大佬 : codehz 0
仓库地址 https://github.com/codehz/tjs
这是某天折腾 tcc 时突发奇想的的 idea,也就是用 js 做脚本,然后任何涉及系统的功能直接用 tcc 运行时编译出来调用(
查了查 node 方面有 node-ffi,但是显而易见都用了 nodejs 了,系统交互基本很多都能在纯 js 层做了,于是再搞这个
目前测试可以直接使用 windows api MessageBox 弹个框(暂时只支持 win,32 位+64 位)
特(que)色(xian):
- 没有事件循环(咕)
- 没有常见 js 函数,除了内置的(咕)
- 强制启用 ESM (和严格模式)
示例代码:
import { Compiler } from "builtin:c"; const compiler = new Compiler("memory"); compiler.link("user32"); compiler.compile(` #include <windows.h> void msgbox(char const *text) { MessageBoxA(NULL, text, "from js", 0); } `); const obj = compiler.relocate({ msgbox: { arguments: ["string"] } }); obj.msgbox(`from ${import.meta.url}`);
大佬有話說 (7)