Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | load(libdir + "asm.js"); |
michael@0 | 2 | |
michael@0 | 3 | function f(stdlib, foreign, buffer) { |
michael@0 | 4 | "use asm"; |
michael@0 | 5 | var i32 = new stdlib.Int32Array(buffer); |
michael@0 | 6 | function set(i,j) { |
michael@0 | 7 | i=i|0; |
michael@0 | 8 | j=j|0; |
michael@0 | 9 | i32[i>>2] = j; |
michael@0 | 10 | } |
michael@0 | 11 | function get(i) { |
michael@0 | 12 | i=i|0; |
michael@0 | 13 | return i32[i>>2]|0 |
michael@0 | 14 | } |
michael@0 | 15 | return {get:get, set:set} |
michael@0 | 16 | } |
michael@0 | 17 | if (isAsmJSCompilationAvailable()) |
michael@0 | 18 | assertEq(isAsmJSModule(f), true); |
michael@0 | 19 | |
michael@0 | 20 | var i32 = new Int32Array(1024); |
michael@0 | 21 | var buffer = i32.buffer; |
michael@0 | 22 | var {get, set} = f(this, null, buffer); |
michael@0 | 23 | if (isAsmJSCompilationAvailable()) |
michael@0 | 24 | assertEq(isAsmJSFunction(get) && isAsmJSFunction(set), true); |
michael@0 | 25 | |
michael@0 | 26 | set(4, 42); |
michael@0 | 27 | assertEq(get(4), 42); |
michael@0 | 28 | |
michael@0 | 29 | neuter(buffer, "change-data"); |
michael@0 | 30 | neuter(buffer, "same-data"); |
michael@0 | 31 | |
michael@0 | 32 | // These operations may throw internal errors |
michael@0 | 33 | try { |
michael@0 | 34 | assertEq(get(4), 0); |
michael@0 | 35 | set(0, 42); |
michael@0 | 36 | assertEq(get(0), 0); |
michael@0 | 37 | } catch (e) { |
michael@0 | 38 | assertEq(String(e).indexOf("InternalError"), 0); |
michael@0 | 39 | } |
michael@0 | 40 | |
michael@0 | 41 | function f2(stdlib, foreign, buffer) { |
michael@0 | 42 | "use asm"; |
michael@0 | 43 | var i32 = new stdlib.Int32Array(buffer); |
michael@0 | 44 | var ffi = foreign.ffi; |
michael@0 | 45 | function inner(i) { |
michael@0 | 46 | i=i|0; |
michael@0 | 47 | ffi(); |
michael@0 | 48 | return i32[i>>2]|0 |
michael@0 | 49 | } |
michael@0 | 50 | return inner |
michael@0 | 51 | } |
michael@0 | 52 | if (isAsmJSCompilationAvailable()) |
michael@0 | 53 | assertEq(isAsmJSModule(f2), true); |
michael@0 | 54 | |
michael@0 | 55 | var i32 = new Int32Array(1024); |
michael@0 | 56 | var buffer = i32.buffer; |
michael@0 | 57 | var threw = false; |
michael@0 | 58 | function ffi() { |
michael@0 | 59 | try { |
michael@0 | 60 | neuter(buffer, "same-data"); |
michael@0 | 61 | } catch (e) { |
michael@0 | 62 | assertEq(String(e).indexOf("InternalError"), 0); |
michael@0 | 63 | threw = true; |
michael@0 | 64 | } |
michael@0 | 65 | try { |
michael@0 | 66 | neuter(buffer, "change-data"); |
michael@0 | 67 | } catch (e) { |
michael@0 | 68 | assertEq(String(e).indexOf("InternalError"), 0); |
michael@0 | 69 | threw = true; |
michael@0 | 70 | } |
michael@0 | 71 | } |
michael@0 | 72 | var inner = f2(this, {ffi:ffi}, buffer); |
michael@0 | 73 | if (isAsmJSCompilationAvailable()) |
michael@0 | 74 | assertEq(isAsmJSFunction(inner), true); |
michael@0 | 75 | i32[2] = 13; |
michael@0 | 76 | var result = inner(8); |
michael@0 | 77 | if (threw) |
michael@0 | 78 | assertEq(result, 13); |
michael@0 | 79 | else |
michael@0 | 80 | assertEq(result, 0); |
michael@0 | 81 |