V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
爱意满满的作品展示区。
codehz

一个新的玩具,在 js 里套娃 c 编译器

  •  
  •   codehz ·
    CodeHz · Oct 12, 2020 · 4189 views
    This topic created in 2026 days ago, the information mentioned may be changed or developed.

    仓库地址 https://github.com/codehz/tjs

    这是某天折腾 tcc 时突发奇想的的 idea,也就是用 js 做脚本,然后任何涉及系统的功能直接用 tcc 运行时编译出来调用(

    查了查 node 方面有 node-ffi,但是显而易见都用了 nodejs 了,系统交互基本很多都能在纯 js 层做了,于是再搞这个

    目前测试可以直接使用 windows api MessageBox 弹个框(暂时只支持 win,32 位+64 位)

    特(que)色(xian):

    1. 没有事件循环(咕)
    2. 没有常见 js 函数,除了内置的(咕)
    3. 强制启用 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}`);
    
    Supplement 1  ·  Oct 13, 2020
    更新:支持 linux x86+x86_64 了(
    下载方式是 github action 里找最新的 artifacts
    9 replies    2021-01-24 18:31:32 +08:00
    azh7138m
        1
    azh7138m  
       Oct 12, 2020
    💥 可也太..... tql
    charten
        2
    charten  
       Oct 13, 2020
    刚开始没看清以为 windows api 指的是浏览器内的那一套,后面才发现是微软的 windows.h....流批啊,一起黑子下发恶意代码是通常是 vb,现在可以直接下发 c 代码搞破坏了 hhhhhhh
    codehz
        3
    codehz  
    OP
       Oct 13, 2020
    @charten #2 类似想法的恶意软件多了去了,不过不会这么 naive 直接下发 c 代码,通常是直接下发 shellcode,最多加上点重定位的操作,就可以跑了。
    Themyth
        4
    Themyth  
       Oct 13, 2020
    我想问。。。
    是不是把这个代码改成.js 放到 html 里面 然后访问就可以弹窗?
    codehz
        5
    codehz  
    OP
       Oct 13, 2020
    @Themyth #4 你在 peach,这是独立的运行时
    sologgfun
        6
    sologgfun  
       Oct 14, 2020
    这是个啥阿?在 node 里面写 c 语言-编译-调用系统 api ?
    codehz
        7
    codehz  
    OP
       Oct 14, 2020
    @sologgfun #6 不是 node,用 quickjs 做的一个很小的 js 运行时,压缩后<4MB,node 请直接用 node-ffi (
    sarvatathagata
        8
    sarvatathagata  
       Jan 24, 2021
    这和 wasm 相比有什么有点呢
    codehz
        9
    codehz  
    OP
       Jan 24, 2021 via Android
    @sarvatathagata 为了调用系统 api 的(不是为了提高性能
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2648 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 02:27 · PVG 10:27 · LAX 19:27 · JFK 22:27
    ♥ Do have faith in what you're doing.