shell.js (5243B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 /*--- 6 defines: [testDestructuringArrayDefault, formatArray, toSource] 7 allow_unused: True 8 ---*/ 9 10 (function(global) { 11 function func() { 12 } 13 class C { 14 foo() { 15 } 16 static foo() { 17 } 18 } 19 20 function test_one(pattern, val, opt) { 21 var stmts = []; 22 var i = 0; 23 var c; 24 25 stmts.push(`var ${pattern} = ${val};`); 26 27 for (var stmt of stmts) { 28 if (!opt.no_plain) { 29 eval(` 30 ${stmt} 31 `); 32 } 33 34 if (!opt.no_func) { 35 eval(` 36 function f${i}() { 37 ${stmt} 38 } 39 f${i}(); 40 `); 41 i++; 42 43 eval(` 44 var f${i} = function foo() { 45 ${stmt} 46 }; 47 f${i}(); 48 `); 49 i++; 50 51 eval(` 52 var f${i} = () => { 53 ${stmt} 54 }; 55 f${i}(); 56 `); 57 i++; 58 } 59 60 if (!opt.no_gen) { 61 eval(` 62 function* g${i}() { 63 ${stmt} 64 } 65 [...g${i}()]; 66 `); 67 i++; 68 69 eval(` 70 var g${i} = function* foo() { 71 ${stmt} 72 }; 73 [...g${i}()]; 74 `); 75 i++; 76 } 77 78 if (!opt.no_ctor) { 79 eval(` 80 class D${i} { 81 constructor() { 82 ${stmt} 83 } 84 } 85 new D${i}(); 86 `); 87 i++; 88 } 89 90 if (!opt.no_derived_ctor) { 91 if (opt.no_pre_super) { 92 eval(` 93 class D${i} extends C { 94 constructor() { 95 ${stmt} 96 try { super(); } catch (e) {} 97 } 98 } 99 new D${i}(); 100 `); 101 i++; 102 } else { 103 eval(` 104 class D${i} extends C { 105 constructor() { 106 super(); 107 ${stmt} 108 } 109 } 110 new D${i}(); 111 `); 112 i++; 113 } 114 } 115 116 if (!opt.no_method) { 117 eval(` 118 class D${i} extends C { 119 method() { 120 ${stmt} 121 } 122 static staticMethod() { 123 ${stmt} 124 } 125 } 126 new D${i}().method(); 127 D${i}.staticMethod(); 128 `); 129 i++; 130 } 131 } 132 133 if (!opt.no_func_arg) { 134 eval(` 135 function f${i}(${pattern}) {} 136 f${i}(${val}); 137 `); 138 i++; 139 140 eval(` 141 var f${i} = function foo(${pattern}) {}; 142 f${i}(${val}); 143 `); 144 i++; 145 146 eval(` 147 var f${i} = (${pattern}) => {}; 148 f${i}(${val}); 149 `); 150 i++; 151 } 152 153 if (!opt.no_gen_arg) { 154 eval(` 155 function* g${i}(${pattern}) {} 156 [...g${i}(${val})]; 157 `); 158 i++; 159 160 eval(` 161 var g${i} = function* foo(${pattern}) {}; 162 [...g${i}(${val})]; 163 `); 164 i++; 165 } 166 } 167 168 function test(expr, opt={}) { 169 var pattern = `[a=${expr}, ...c]`; 170 test_one(pattern, "[]", opt); 171 test_one(pattern, "[1]", opt); 172 173 pattern = `[,a=${expr}]`; 174 test_one(pattern, "[]", opt); 175 test_one(pattern, "[1]", opt); 176 test_one(pattern, "[1, 2]", opt); 177 178 pattern = `[{x: [a=${expr}]}]`; 179 test_one(pattern, "[{x: [1]}]", opt); 180 181 pattern = `[x=[a=${expr}]=[]]`; 182 test_one(pattern, "[]", opt); 183 test_one(pattern, "[1]", opt); 184 185 pattern = `[x=[a=${expr}]=[1]]`; 186 test_one(pattern, "[]", opt); 187 test_one(pattern, "[1]", opt); 188 } 189 190 global.testDestructuringArrayDefault = test; 191 })(this); 192 193 (function(global) { 194 /* 195 * Date: 07 February 2001 196 * 197 * Functionality common to Array testing - 198 */ 199 //----------------------------------------------------------------------------- 200 201 202 var CHAR_LBRACKET = '['; 203 var CHAR_RBRACKET = ']'; 204 var CHAR_QT_DBL = '"'; 205 var CHAR_COMMA = ','; 206 var CHAR_SPACE = ' '; 207 var TYPE_STRING = typeof 'abc'; 208 209 210 /* 211 * If available, arr.toSource() gives more detail than arr.toString() 212 * 213 * var arr = Array(1,2,'3'); 214 * 215 * arr.toSource() 216 * [1, 2, "3"] 217 * 218 * arr.toString() 219 * 1,2,3 220 * 221 * But toSource() doesn't exist in Rhino, so use our own imitation, below - 222 * 223 */ 224 function formatArray(arr) 225 { 226 try 227 { 228 return arr.toSource(); 229 } 230 catch(e) 231 { 232 return toSource(arr); 233 } 234 } 235 236 global.formatArray = formatArray; 237 238 /* 239 * Imitate SpiderMonkey's arr.toSource() method: 240 * 241 * a) Double-quote each array element that is of string type 242 * b) Represent |undefined| and |null| by empty strings 243 * c) Delimit elements by a comma + single space 244 * d) Do not add delimiter at the end UNLESS the last element is |undefined| 245 * e) Add square brackets to the beginning and end of the string 246 */ 247 function toSource(arr) 248 { 249 var delim = CHAR_COMMA + CHAR_SPACE; 250 var elt = ''; 251 var ret = ''; 252 var len = arr.length; 253 254 for (i=0; i<len; i++) 255 { 256 elt = arr[i]; 257 258 switch(true) 259 { 260 case (typeof elt === TYPE_STRING) : 261 ret += doubleQuote(elt); 262 break; 263 264 case (elt === undefined || elt === null) : 265 break; // add nothing but the delimiter, below - 266 267 default: 268 ret += elt.toString(); 269 } 270 271 if ((i < len-1) || (elt === undefined)) 272 ret += delim; 273 } 274 275 return CHAR_LBRACKET + ret + CHAR_RBRACKET; 276 } 277 278 global.toSource = toSource; 279 280 function doubleQuote(text) 281 { 282 return CHAR_QT_DBL + text + CHAR_QT_DBL; 283 } 284 })(this);