Mon, 07 Mar 2016 10:53:05 -0800
Added tag jdk8u76-b11 for changeset 0608b63feb60
yan@995 | 1 | /* |
yan@995 | 2 | * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. |
yan@995 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
yan@995 | 4 | * |
yan@995 | 5 | * This code is free software; you can redistribute it and/or modify it |
yan@995 | 6 | * under the terms of the GNU General Public License version 2 only, as |
yan@995 | 7 | * published by the Free Software Foundation. |
yan@995 | 8 | * |
yan@995 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
yan@995 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
yan@995 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
yan@995 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
yan@995 | 13 | * accompanied this code). |
yan@995 | 14 | * |
yan@995 | 15 | * You should have received a copy of the GNU General Public License version |
yan@995 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
yan@995 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
yan@995 | 18 | * |
yan@995 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
yan@995 | 20 | * or visit www.oracle.com if you need additional information or have any |
yan@995 | 21 | * questions. |
yan@995 | 22 | */ |
yan@995 | 23 | |
yan@995 | 24 | /** |
yan@995 | 25 | * JDK-8057678: Tests for let&const keywords in Nashorn |
yan@995 | 26 | * |
yan@995 | 27 | * @test |
yan@995 | 28 | * @run |
yan@995 | 29 | * @option --language=es6 |
yan@995 | 30 | * @option -scripting |
yan@995 | 31 | */ |
yan@995 | 32 | |
yan@995 | 33 | function tryIt(code) { |
yan@995 | 34 | try { |
yan@995 | 35 | eval(code) |
yan@995 | 36 | } catch (e) { |
yan@995 | 37 | print(e) |
yan@995 | 38 | } |
yan@995 | 39 | } |
yan@995 | 40 | |
yan@995 | 41 | |
yan@995 | 42 | tryIt(<<CODE |
yan@995 | 43 | function a () { |
yan@995 | 44 | this.val = 41 |
yan@995 | 45 | let self = this |
yan@995 | 46 | this.b = function () { |
yan@995 | 47 | return self.val; |
yan@995 | 48 | } |
yan@995 | 49 | } |
yan@995 | 50 | c = new a() |
yan@995 | 51 | print(c.b.call(null)) |
yan@995 | 52 | CODE) |
yan@995 | 53 | |
yan@995 | 54 | |
yan@995 | 55 | tryIt(<<CODE |
yan@995 | 56 | function a () { |
yan@995 | 57 | this.val = 42 |
yan@995 | 58 | let self = this |
yan@995 | 59 | this.b = function () { |
yan@995 | 60 | return this.val; |
yan@995 | 61 | }.bind(self) |
yan@995 | 62 | } |
yan@995 | 63 | c = new a() |
yan@995 | 64 | print(c.b.call(null)) |
yan@995 | 65 | CODE) |
yan@995 | 66 | |
yan@995 | 67 | tryIt(<<CODE |
yan@995 | 68 | function a () { |
yan@995 | 69 | this.val = 43 |
yan@995 | 70 | const self = this |
yan@995 | 71 | this.b = function () { |
yan@995 | 72 | return self.val; |
yan@995 | 73 | } |
yan@995 | 74 | } |
yan@995 | 75 | c = new a() |
yan@995 | 76 | print(c.b.call(null)) |
yan@995 | 77 | CODE) |
yan@995 | 78 | |
yan@995 | 79 | tryIt(<<CODE |
yan@995 | 80 | function a () { |
yan@995 | 81 | this.val = 44 |
yan@995 | 82 | const self = this |
yan@995 | 83 | this.b = function () { |
yan@995 | 84 | return this.val; |
yan@995 | 85 | }.bind(self) |
yan@995 | 86 | } |
yan@995 | 87 | c = new a() |
yan@995 | 88 | print(c.b.call(null)) |
yan@995 | 89 | CODE) |
yan@995 | 90 | |
yan@995 | 91 | tryIt(<<CODE |
yan@995 | 92 | let a = {name : 'test'} |
yan@995 | 93 | let f = function () { |
yan@995 | 94 | print(this.name) |
yan@995 | 95 | } |
yan@995 | 96 | let nf = f.bind(a) |
yan@995 | 97 | nf() |
yan@995 | 98 | if (true) { |
yan@995 | 99 | let a = null |
yan@995 | 100 | nf() |
yan@995 | 101 | } |
yan@995 | 102 | nf() |
yan@995 | 103 | CODE) |
yan@995 | 104 | |
yan@995 | 105 | |
yan@995 | 106 | tryIt(<<CODE |
yan@995 | 107 | let arr = [] |
yan@995 | 108 | for (let i = 0; i < 3; i++) { |
yan@995 | 109 | arr[i] = function(){return i;} |
yan@995 | 110 | } |
yan@995 | 111 | for (let i in arr) { |
yan@995 | 112 | print(arr[i]()) |
yan@995 | 113 | } |
yan@995 | 114 | arr = [] |
yan@995 | 115 | for (var i = 0; i < 3; i++) { |
yan@995 | 116 | (function(i){ |
yan@995 | 117 | arr[i] = function(){return i;} |
yan@995 | 118 | })(i) |
yan@995 | 119 | } |
yan@995 | 120 | for (let i in arr) { |
yan@995 | 121 | print(arr[i]()) |
yan@995 | 122 | } |
yan@995 | 123 | CODE) |