问答

emscripten 里面 export函数改如何编译引入

作者:admin 2021-06-22 我要评论

大佬的项目 https://www.bilibili.com/vide... https://github.com/lizzz0523/... 里面的public/main.c改如何编译,已经搭建好了emcc环境,使用命令 REM emcc -o...

在说正事之前,我要推荐一个福利:你还在原价购买阿里云、腾讯云、华为云服务器吗?那太亏啦!来这里,新购、升级、续费都打折,能够为您省60%的钱呢!2核4G企业级云服务器低至69元/年,点击进去看看吧>>>)

大佬的项目https://www.bilibili.com/vide...
https://github.com/lizzz0523/...

里面的public/main.c改如何编译,已经搭建好了emcc环境,使用命令REM emcc -o ./test/build/main.html ./test/main.cc编译引入会报错

我的项目:
https://github.com/thetime50/...
https://github.com/thetime50/...

main.c:

#define WASM_EXPORT __attribute__((visibility("default")))

typedef unsigned char uint8_t;

WASM_EXPORT
uint8_t *create_buf(int size) {
  return (uint8_t *)malloc(size);
}

helperwasm-canvas.js


export async function instanceRenderer(){
    let memoryStates = new WeakMap() // instance -> memoryState map
    let instance = null
    function syncall (instance, n, args) {
        switch (n) {
            default:
                break
            case /* brk */ 45:
                return 0
            case /* mmap2 */ 192:
                const memory = instance.exports.memory
                const requested = args[1] // 代码块需要的空间
                let memoryState = memoryStates.get(instance)
                if (!memoryState){// 判断 并构造 加入map
                    memoryState = {
                        object : memory,
                        currentPosition : memory.buffer.byteLength
                    }
                    memoryStates.set(instance, memoryState)// 好像也没别的地方用了
                }
                const position = memoryState.currentPosition // todo 代码块位置??
                if (position + requested > memory.buffer.byteLength){
                    const need = Math.ceil(
                        (position + requested - memory.buffer.byteLength) / 65536
                    )
                    // https://cloud.tencent.com/developer/section/1192279
                    // 指定定数量的WebAssembly页面增加内存实例的大小
                    memory.grow(need) //todo 难道是给mock用的??
                }
                memoryState.currentPosition += requested
                return position
        }
    }

    //用的是原生 webassembly 方法 没用用到vuecli4的库
    const response = await fetch('./main.wasm') // 类似 XMLHttpRequest
    // const response = await fetch('./magic.wasm') //****使用自己编译的wasm会报错****
    const bytes = await response.arrayBuffer() // 得到二进制文件数据
    const result = await WebAssembly.instantiate(bytes, {
        env: {// todo 这里面是执行异常处理回调吗 n又是什么??
            __syscall0: function __syscall0 (n, ...args) {
                // 不知道在里面的instance是哪来的
                dbg('__syscall0',n,args)
                return syncall(instance, n, args)
            },
            __syscall1: function __syscall1 (n, ...args) {
                dbg('__syscall1',n,args)
                return syncall(instance, n, args)
            },
            __syscall2: function __syscall2 (n, ...args) {
                dbg('__syscall2',n,args)
                return syncall(instance, n, args)
            },
            __syscall3: function __syscall3 (n, ...args) {
                dbg('__syscall3',n,args)
                return syncall(instance, n, args)
            },
            __syscall4: function __syscall4 (n, ...args) {
                dbg('__syscall4',n,args)
                return syncall(instance, n, args)
            },
            // __syscall5: function __syscall5 (n, ...args) {
            //     dbg('__syscall5',n,args)
            //     return syncall(instance, n, args)
            // },
            __syscall6: function __syscall6 (n, ...args) {
                dbg('__syscall6',n,args)
                return syncall(instance, n, args)
            },
            
        }
    })
    instance = result.instance

    return result
}

使用自己的wasm会报错

Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist

vue.runtime.esm.js?2b0e:619 [Vue warn]: Error in mounted hook (Promise/async): "LinkError: WebAssembly.instantiate(): Import #0 module="env" function="emscripten_resize_heap" error: function import requires a callable"

found in

---> <FeedMagic> at src/components/statsAnimation/feedMagic.vue
       <FeedDetail> at src/components/statsAnimation/feedDetail.vue
         <StatsAnimation> at src/views/statsAnimation/statsAnimation.vue
           <Routerviewcomp> at src/views/routerviewcomp.vue
             <App> at src/App.vue
               <Root>
               
LinkError: WebAssembly.instantiate(): Import #0 module="env" function="emscripten_resize_heap" error: function import requires a callable

版权声明:本文转载自网络,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。本站转载出于传播更多优秀技术知识之目的,如有侵权请联系QQ/微信:153890879删除

相关文章
  •  emscripten 里面 export函数改如何编

    emscripten 里面 export函数改如何编

  • Gateway路由转发503,是路由配置问题?注

    Gateway路由转发503,是路由配置问题?注

  • antd 想要datepicker只能选择每一年的

    antd 想要datepicker只能选择每一年的

  • echarts-曲线图封装series的类型多少能

    echarts-曲线图封装series的类型多少能

腾讯云代理商
海外云服务器