WriteFixOffset.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. function foo() {}
  6. var all = [ undefined, null,
  7. true, false, new Boolean(true), new Boolean(false),
  8. NaN, +0, -0, 0, 1, 10.0, 10.1, -1, -5, 5,
  9. 124, 248, 654, 987, -1026, +98768.2546, -88754.15478,
  10. 1<<32, -(1<<32), (1<<32)-1, 1<<31, -(1<<31), 1<<25, -1<<25,
  11. Number.MAX_VALUE, Number.MIN_VALUE, Number.NaN, Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY,
  12. new Number(NaN), new Number(+0), new Number( -0), new Number(0), new Number(1),
  13. new Number(10.0), new Number(10.1),
  14. new Number(Number.MAX_VALUE), new Number(Number.MIN_VALUE), new Number(Number.NaN),
  15. new Number(Number.POSITIVE_INFINITY), new Number(Number.NEGATIVE_INFINITY),
  16. "", "hello", "hel" + "lo", "+0", "-0", "0", "1", "10.0", "10.1",
  17. new String(""), new String("hello"), new String("he" + "llo"),
  18. new Object(), [1,2,3], new Object(), [1,2,3] , foo
  19. ];
  20. function AsmModule(stdlib,foreign,buffer) {
  21. "use asm";
  22. var fround = stdlib.Math.fround;
  23. //views
  24. var HEAP8 =new stdlib.Int8Array(buffer);
  25. var HEAP16 =new stdlib.Int16Array(buffer);
  26. var HEAP32 =new stdlib.Int32Array(buffer);
  27. var HEAPU8 =new stdlib.Uint8Array(buffer);
  28. var HEAPU16=new stdlib.Uint16Array(buffer);
  29. var HEAPU32=new stdlib.Uint32Array(buffer);
  30. var HEAPF32=new stdlib.Float32Array(buffer);
  31. var HEAP64 =new stdlib.Float64Array(buffer);
  32. function write8 (x,y){x = x|0; y = y|0; HEAP8 [0xFFF] = y; }
  33. function write16 (x,y){x = x|0; y = y|0; HEAP16 [0xFFF] = y; }
  34. function write32 (x,y){x = x|0; y = y|0; HEAP32 [0xFFF] = y; }
  35. function writeU8 (x,y){x = x|0; y = y|0; HEAPU8 [0xFFF] = y; }
  36. function writeU16(x,y){x = x|0; y = y|0; HEAPU16[0xFFF] = y; }
  37. function writeU32(x,y){x = x|0; y = y|0; HEAPU32[0xFFF] = y; }
  38. function writeF32(x,y){x = x|0; y = +y; HEAPF32[0xFFF] = y; }
  39. function writeF32f(x,y){x = x|0; y = fround(y); HEAPF32[0xFFF] = y; }
  40. function write64 (x,y){x = x|0; y = +y; HEAP64 [0xFFF] = y; }
  41. return {
  42. write8 : write8
  43. ,write16 : write16
  44. ,write32 : write32
  45. ,writeU8 : writeU8
  46. ,writeU16 : writeU16
  47. ,writeU32 : writeU32
  48. ,writeF32 : writeF32
  49. ,writeF32f: writeF32f
  50. ,write64 : write64
  51. };
  52. }
  53. var stdlib = {Math:Math,Int8Array:Int8Array,Int16Array:Int16Array,Int32Array:Int32Array,Uint8Array:Uint8Array,Uint16Array:Uint16Array,Uint32Array:Uint32Array,Float32Array:Float32Array,Float64Array:Float64Array,Infinity:Infinity, NaN:NaN}
  54. var env = {fun1:function(x){print(x);}, fun2:function(x,y){print(x,y);},i1:155,i2:658,d1:68.25,d2:3.14156,f1:48.1523,f2:14896.2514}
  55. var buffer =new ArrayBuffer(1<<20); // small enough to allow this test to succeed on a memory-limited system, but not necessarily the smallest
  56. var HEAP8 =new stdlib.Int8Array(buffer);
  57. var HEAP16 =new stdlib.Int16Array(buffer);
  58. var HEAP32 =new stdlib.Int32Array(buffer);
  59. var HEAPU8 =new stdlib.Uint8Array(buffer);
  60. var HEAPU16=new stdlib.Uint16Array(buffer);
  61. var HEAPU32=new stdlib.Uint32Array(buffer);
  62. var HEAPF32=new stdlib.Float32Array(buffer);
  63. var HEAP64 =new stdlib.Float64Array(buffer);
  64. var asmModule = AsmModule(stdlib,env,buffer);
  65. for (var i=0; i<all.length; ++i) {
  66. var x = all[i]|0;
  67. for (var j=0; j<all.length; ++j) {
  68. var y = all[j];
  69. asmModule.write8 (x, y); print("write8 HEAP8 ["+x+"] = all["+j+"]("+y+"); = " + (HEAP8 [0xFFF] |0));
  70. asmModule.writeF32f(x, y); print("writeF32f HEAPF32["+x+"] = all["+j+"]("+y+"); = " + (Math.fround(HEAPF32[0xFFF]) ));
  71. asmModule.write16 (x, y); print("write16 HEAP16 ["+x+"] = all["+j+"]("+y+"); = " + (HEAP16 [0xFFF]|0));
  72. asmModule.write32 (x, y); print("write32 HEAP32 ["+x+"] = all["+j+"]("+y+"); = " + (HEAP32 [0xFFF]|0));
  73. asmModule.writeU8 (x, y); print("writeU8 HEAPU8 ["+x+"] = all["+j+"]("+y+"); = " + (HEAPU8 [0xFFF] |0));
  74. asmModule.writeF32 (x, y); print("writeF32 HEAPF32["+x+"] = all["+j+"]("+y+"); = " + (+HEAPF32[0xFFF] ));
  75. asmModule.writeU16 (x, y); print("writeU16 HEAPU16["+x+"] = all["+j+"]("+y+"); = " + (HEAPU16[0xFFF]|0));
  76. asmModule.writeU32 (x, y); print("writeU32 HEAPU32["+x+"] = all["+j+"]("+y+"); = " + (HEAPU32[0xFFF]|0));
  77. asmModule.write64 (x, y); print("write64 HEAP64 ["+x+"] = all["+j+"]("+y+"); = " + (+HEAP64 [0xFFF] ));
  78. }
  79. }