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

时间:2014-02-04 02:06:32

标签: 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在控制台中查看问题。

1 个答案:

答案 0 :(得分:1)

我能够自己解决这个问题。这是一个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++;

<强>资源