tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

generate-nosuchproperty-tests.js (2627B)


      1 // This code generates the test cases jit-test/tests/baseline/no-such-property-getprop.js
      2 //
      3 // In particular, it generates the testChain_<N>_<I>() and runChain_<N>_<I>() functions
      4 // at the tail of the file.
      5 
      6 var TEST_CASE_FUNCS =  [];
      7 function runChain_NNNN_DDDD(obj) {
      8    var sum = 0;
      9    for (var i = 0; i < 100; i++)
     10        sum += obj.foo;
     11    return sum;
     12 }
     13 function testChain_NNNN_DDDD() {
     14    var obj = createTower(NNNN);
     15    assertEq(runChain_NNNN_DDDD(obj), NaN);
     16    updateChain(obj, DDDD, 'foo', 9);
     17    assertEq(runChain_NNNN_DDDD(obj), 900);
     18 }
     19 function generateTestCase(n, d) {
     20    var runFuncName = "runChain_" + n + "_" + d;
     21    var testFuncName = "testChain_" + n + "_" + d;
     22    TEST_CASE_FUNCS.push(testFuncName);
     23 
     24    print("//// Test chain of length " + n + " with late-property-addition at depth " + d);
     25    print(String(runChain_NNNN_DDDD).replace(/NNNN/g, ''+n).replace(/DDDD/g, ''+d));
     26    print(String(testChain_NNNN_DDDD).replace(/NNNN/g, ''+n).replace(/DDDD/g, ''+d));
     27    print("");
     28 }
     29 
     30 // Helper function to create an object with a proto-chain of length N.
     31 function createTower(n) {
     32    var result = Object.create(null);
     33    for (var i = 0; i < n; i++)
     34        result = Object.create(result);
     35    return result;
     36 }
     37 
     38 function updateChain(obj, depth, prop, value) {
     39    // Walk down the proto chain |depth| iterations and set |prop| to |value|.
     40    var cur = obj;
     41    for (var i = 0; i < depth; i++)
     42        cur = Object.getPrototypeOf(cur);
     43 
     44    var desc = {value:value, writable:true, configurable:true, enumerable:true};
     45    Object.defineProperty(cur, prop, desc);
     46 }
     47 
     48 print("/////////////////////////////////////////");
     49 print("// This is a generated file!");
     50 print("// See jit-tests/etc/generate-nosuchproperty-tests.js for the code");
     51 print("// that generated this code!");
     52 print("/////////////////////////////////////////");
     53 print("");
     54 print("/////////////////////////////////////////");
     55 print("// PRELUDE                             //");
     56 print("/////////////////////////////////////////");
     57 print("");
     58 print(createTower);
     59 print(updateChain);
     60 print("");
     61 print("/////////////////////////////////////////");
     62 print("// TEST CASES                          //");
     63 print("/////////////////////////////////////////");
     64 print("");
     65 for (var n = 0; n <= 10; n++) {
     66    for (var d = 0; d <= n; d++) {
     67        generateTestCase(n, d);
     68    }
     69 }
     70 
     71 print("");
     72 print("/////////////////////////////////////////");
     73 print("// RUNNER                              //");
     74 print("/////////////////////////////////////////");
     75 print("");
     76 for (var i = 0; i < TEST_CASE_FUNCS.length; i++)
     77    print(TEST_CASE_FUNCS[i] + "();");