6. function DiagModule(stdlib, foreign, heap) {
"use asm";
// Variable Declarations
var sqrt = stdlib.Math.sqrt;
// Function Declarations
function square(x) {
x = +x;
return +(x*x);
}
function diag(x, y) {
x = +x; y = +y;
return +sqrt(square(x) + square(y));
}
return { diag: diag };
}
Math WebGL heap
var diag = DiagModule({ Math: Math }).diag;
alert(diag(10, 100));
7. asm.js의 특징
• 모든 브라우저에서 동작
• 제한된 타입(숫자)만 가능 (링크)
• 직접 heap을 만들어서 메모리 관리 (링크)
8. 왜 성능이 좋은가?
• 고정타입
– e|0 (integer), +a(double)
– 엔진에서는 “use asm”이라고 되어 있으면
타입이 정적이라고 판단하여 AOT
컴파일(ahead-of-time compile)함.
• 메모리를 직접 관리
– heap을 구현하여 malloc과 free와 유사하게 관리