javascript - 无法解决 Firefox 的 AMS.js 链接限制

标签 javascript firefox asm.js

我收到错误:

asm.js link error: As a temporary limitation, modules cannot be linked more than once.
This limitation should be removed in a future release. To work around this, compile a second module (e.g., using the Function constructor).

我尚未找到有关此解决方法的任何文档。有人有一个可行的例子吗?

不是我的代码,但这遇到了同样的问题:http://jsperf.com/simplex-noise-comparison-asm 使用 FireFox 在控制台中查看问题。

最佳答案

我自己解决了这个问题。这是一个 JSFiddle 链接,供遇到相同问题的人使用: http://jsfiddle.net/QLtMB/

// Store template as actual function, do not use real pragma to avoid compile
var asmTemplate = function MyAsmModule( stdlib, foreign, heap ) {
   "asm template";

   var HEAP = new stdlib.Uint8Array(heap);
   var _sqrt = stdlib.Math.sqrt;
   var _sin = stdlib.Math.sin;
   var _cos = stdlib.Math.cos;


   function fastSqr( x ) {
      x = +x;
      return +(x*x);
   }

   function fastSqrt( x ) {
      x = +x;
      return +_sqrt( x );
   }

   function fastSin( x ) {
      x = +x;
      return +_sin( x );
   }

   function fastCos( x ) {
      x = +x;
      return +_cos( x );
   }

   function fastTest( x ) {
      x = +x;
      return +fastSqr( 2.17 * (+fastSqrt( 0.75 * +fastSin( x ) + 0.25 * +fastCos( x ) )) );
   }

   return { sqrt: fastSqrt, sin: fastSin, cos: fastCos, test: fastTest };
}

// Generate source string once, replace with real pragma.
asmTemplate = asmTemplate.toString().replace('asm template', 'use asm');

function createASM(buffer) {
    window.asmBufferExport = buffer;
    return Function(asmTemplate + "\nreturn MyAsmModule( Function( 'return this;' )(), null, asmBufferExport.buffer );")();
}

var Asm = createASM(new Uint8Array(4 * 1024));
var Asm2 = createASM(new Uint8Array(4 * 1024));

作为奖励,这会将您的堆大小四舍五入到 asm.js 标准:

    var size = <actual size>;
    // Round up to multiple of 4096
    var remainder = size % 4096;
    size = remainder ? size + 4096 - remainder : size;
    // Round up to the nearest power of 2
    size--;
    size |= size >> 1;  // handle  2 bit numbers
    size |= size >> 2;  // handle  4 bit numbers
    size |= size >> 4;  // handle  8 bit numbers
    size |= size >> 8;  // handle 16 bit numbers
    size |= size >> 16; // handle 32 bit numbers
    size++;

资源:

关于javascript - 无法解决 Firefox 的 AMS.js 链接限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21541705/

相关文章:

javascript - JQuery - 如果 href 等于某物,如何将类添加到特定范围

javascript - 在 HR 上设置类别会破坏 TD colspan

css - Firefox 背景图片怪异

css - 出现滚动条时 FireFox 中的长页面偏移

javascript - 可以将常规 JavaScript 转换为 asm.js,还是仅用于加速静态类型的低级语言?

javascript - 表 - 循环和数组

未为 Bootstrap Modal 定义 Javascript 函数

javascript - 将 arraybuffer 的一部分绘制到 Canvas 上的最佳方法是什么?

javascript - 了解 Emscripten/ASM.js 和浏览器沙箱

javascript - 手动调整CSS属性大小