1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/script/basic/NASHORN-377-payload.js Mon Nov 17 14:36:15 2014 +0100 1.3 @@ -0,0 +1,226 @@ 1.4 +/* 1.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +/* 1.28 + * NASHORN-377: Typed arrays. Payload for litte and big endian platforms. 1.29 + * 1.30 + * @subtest 1.31 + * @run 1.32 + */ 1.33 + 1.34 +var types = [Int8Array,Uint8Array,Uint8ClampedArray,Int16Array,Uint16Array,Int32Array,Uint32Array,Float32Array,Float64Array]; 1.35 + 1.36 +//--------------------------------------------------------------------------- 1.37 +// utility functions 1.38 +//--------------------------------------------------------------------------- 1.39 +function tohex(d, w) { 1.40 + var hex = Number(d).toString(16); 1.41 + var pad = (w ? w : 8) - hex.length; 1.42 + hex = "00000000".substr(0, pad) + hex; 1.43 + return hex; 1.44 +} 1.45 + 1.46 +function arrstr(a, n, w) { 1.47 + var s = ""; 1.48 + if (typeof n == "undefined") n = a.length; 1.49 + if (typeof w == "undefined") w = a.constructor.BYTES_PER_ELEMENT * 2; 1.50 + for (var i = 0; i < n; i++) { 1.51 + s += tohex(a[i], w); 1.52 + } 1.53 + return s; 1.54 +} 1.55 +function bufstr(b) { 1.56 + if (b.buffer !== undefined) { 1.57 + b = b.buffer; 1.58 + } 1.59 + return arrstr(new Uint8Array(b)); 1.60 +} 1.61 + 1.62 +function assertFail(f) { 1.63 + try { 1.64 + f(); 1.65 + } catch (e) { 1.66 + //print(e); 1.67 + return; 1.68 + } 1.69 + throw "assertion failed: expected exception"; 1.70 +} 1.71 + 1.72 +function assertTrue(f) { 1.73 + if (f() !== true) throw "assertion failed: " + f; 1.74 +} 1.75 + 1.76 +function isUndefined(x) { 1.77 + return typeof x === "undefined"; 1.78 +} 1.79 + 1.80 +function fillArray(a, start) { 1.81 + if (typeof start == "undefined") start = 1; 1.82 + for (var i = 0; i < a.length; i++) { 1.83 + a[i] = i + start; 1.84 + } 1.85 + return a; 1.86 +} 1.87 + 1.88 +//--------------------------------------------------------------------------- 1.89 +// tests 1.90 +//--------------------------------------------------------------------------- 1.91 +(function() { 1.92 + var b = new ArrayBuffer(8); 1.93 + var i8 = new Int8Array(b); 1.94 + print(i8.buffer.byteLength, b.byteLength, i8.buffer === b, b.length); 1.95 + print(b, i8.buffer, i8); 1.96 +})(); 1.97 + 1.98 +(function test_attributes() { 1.99 + var b = new ArrayBuffer(8); 1.100 + for (var i in types) { 1.101 + var x = new types[i](b); 1.102 + print(x.byteOffset, x.byteLength, x.length, x.constructor.BYTES_PER_ELEMENT); 1.103 + assertTrue(function(){ return x.constructor === types[i] }); 1.104 + } 1.105 +})(); 1.106 + 1.107 +(function() { 1.108 + var b = new ArrayBuffer(8); 1.109 + var i8 = new Int8Array(b); 1.110 + fillArray(i8, 0x70); 1.111 + 1.112 + var i8_2 = new Int8Array(b, 2); 1.113 + var i8_2_4 = new Uint8Array(b, 2, 4); 1.114 + 1.115 + i8_2_4[3] = 0x80; 1.116 + 1.117 + print(arrstr(i8, 8, 2) + " " + bufstr(i8)); 1.118 + print(arrstr(i8_2, 6) + " " + i8_2.byteOffset + " " + i8_2.byteLength); 1.119 + print(arrstr(i8_2_4, 4) + " " + i8_2_4.byteOffset + " " + i8_2_4.byteLength); 1.120 + 1.121 + var i8_1_5 = i8.subarray(1, 5); 1.122 + i8_2_4.subarray(1, 5); 1.123 + print(arrstr(i8_1_5, 4) + " " + i8_1_5.byteOffset + " " + i8_1_5.byteLength); 1.124 + 1.125 + print(bufstr(b.slice(1,7))); 1.126 +})(); 1.127 + 1.128 +(function() { 1.129 + var b = new ArrayBuffer(8); 1.130 + fillArray(new Int8Array(b), 0x70); 1.131 + new Int8Array(b)[5] = 0x80; 1.132 + 1.133 + var i32 = new Int32Array(b); 1.134 + var u32 = new Uint32Array(b); 1.135 + print(arrstr(i32), i32[0], i32[1]); 1.136 + i32[1] = 0xfefdfcfb; 1.137 + print(arrstr(i32), i32[0], i32[1]); 1.138 + print(arrstr(u32), u32[0], u32[1]); 1.139 + 1.140 + var pi = 3.1415926; 1.141 + var f32 = new Float32Array(b); 1.142 + var f64 = new Float64Array(b); 1.143 + f32[0] = pi; 1.144 + print(bufstr(b), f32.length); 1.145 + f64[0] = pi; 1.146 + print(bufstr(b), f64.length); 1.147 + print(arrstr(u32), u32[0], u32[1]); 1.148 + 1.149 + var d = new Int32Array(3); 1.150 + d.set(i32,1); 1.151 + print(bufstr(d)); 1.152 + 1.153 + var s = new Int16Array(b); 1.154 + var t = new Uint16Array(b); 1.155 + print(arrstr(s), arrstr(t)); 1.156 + s[0] = -1; s[1] = 0x80; 1.157 + print(arrstr(s), arrstr(t)); 1.158 +})(); 1.159 + 1.160 +(function enumerate_properties() { 1.161 + var i8 = new Int8Array(new ArrayBuffer(8)); 1.162 + var s = ""; for (var i in i8) { s += i + " "; } print(s.trim()); 1.163 +})(); 1.164 + 1.165 +// check that ScriptObject fallback is still working 1.166 +// DISABLED because correct behavior is unclear 1.167 +(function() { 1.168 + // NB: firefox will never set any out-of-bounds or non-array values although it does get both from prototype. 1.169 + var z = new Uint8Array(4); 1.170 + z["asdf"] = "asdf"; print(z["asdf"]); 1.171 + z[0x100000000] = "asdf"; print(z[0x100000000]); 1.172 + z[-1] = "asdf"; print(z[-1]); 1.173 + 1.174 + // v8 and nashorn disagree on out-of-bounds uint32 indices: v8 won't go to the prototype. 1.175 + z[0xf0000000] = "asdf"; print(z[0xf0000000]); 1.176 + z[0xffffffff] = "asdf"; print(z[0xffffffff]); 1.177 + z[0x70000000] = "asdf"; print(z[0x70000000]); 1.178 + 1.179 + // this will work in firefox and nashorn (not in v8). 1.180 + Uint8Array.prototype[4] = "asdf"; print(z[4]); 1.181 +}); 1.182 + 1.183 +(function test_exceptions() { 1.184 + assertFail(function() { new Int32Array(new ArrayBuffer(7)); }); 1.185 + assertFail(function() { new Int32Array(new ArrayBuffer(8), 0, 4); }); 1.186 + assertFail(function() { new Int32Array(new ArrayBuffer(8),-1, 2); }); 1.187 + assertFail(function() { new Int32Array(new ArrayBuffer(8), 0,-1); }); 1.188 +})(); 1.189 + 1.190 +(function test_subarray() { 1.191 + var x = fillArray(new Int8Array(8)); 1.192 + print(arrstr(x)); 1.193 + print("subarray(2,4)=" + arrstr(x.subarray(2, 4)), "subarray(-6,-4)=" + arrstr(x.subarray(-6, -4))); // negative index refers from the end of the array 1.194 + print(arrstr(x.subarray(-10, -2))); // negative index clamped to 0 1.195 + assertTrue(function(){ return arrstr(x.subarray(6, 4)) === ""; }); // negative length clamped to 0 1.196 + print(arrstr(x.subarray(1,-1).subarray(1,-1)), arrstr(x.subarray(1,-1).subarray(1,-1).subarray(1,-1))); // subarray of subarray 1.197 +})(); 1.198 + 1.199 +(function test_slice() { 1.200 + var b = new ArrayBuffer(16); 1.201 + fillArray(new Int8Array(b)); 1.202 + print(bufstr(b)); 1.203 + print("slice(4,8)=" + bufstr(b.slice(4, 8)), "slice(-8,-4)=" + bufstr(b.slice(-8, -4))); // negative index refers from the end of the array 1.204 + print(bufstr(b.slice(-20, -4))); // negative index clamped to 0 1.205 + assertTrue(function(){ return bufstr(b.slice(8, 4)) === ""; }); // negative length clamped to 0 1.206 + print(arrstr(new Int16Array(b.slice(1,-1).slice(2,-1).slice(1,-2).slice(1,-1)))); // slice of slice 1.207 +})(); 1.208 + 1.209 +(function test_clamped() { 1.210 + var a = new Uint8ClampedArray(10); 1.211 + a[0] = -17; // clamped to 0 1.212 + a[1] = 4711; // clamped to 255 1.213 + a[2] = 17.5; // clamped to 18 1.214 + a[3] = 16.5; // clamped to 16 1.215 + a[4] = 255.9; // clamped to 255 1.216 + a[5] = Infinity; // clamped to 255 1.217 + a[6] = -Infinity; // clamped to 0 1.218 + a[7] = NaN; // 0 1.219 + assertTrue(function(){ return a[0] === 0 && a[1] === 255 && a[2] === 18 && a[3] === 16 && a[4] === 255 && a[5] === 255 && a[6] === 0 && a[7] === 0; }); 1.220 +})(); 1.221 + 1.222 +(function test_out_of_bounds() { 1.223 + var a = new Int32Array(10); 1.224 + a[10] = 10; 1.225 + a[100] = 100; 1.226 + a[1000] = 1000; 1.227 + assertTrue(function(){ return isUndefined(a[10]) && isUndefined(a[11]) && isUndefined(a[100]) && isUndefined(a[123]) && isUndefined(a[1000]); }); 1.228 +})(); 1.229 +