nashorn/samples/try_with_resource.js
author lana
Sat, 26 Sep 2015 09:21:16 -0700
changeset 32752 43c458023730
parent 26070 1c348e8d9b88
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
26070
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
     1
/*
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
     3
 *
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
     4
 * Redistribution and use in source and binary forms, with or without
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
     5
 * modification, are permitted provided that the following conditions
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
     6
 * are met:
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
     7
 *
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
     8
 *   - Redistributions of source code must retain the above copyright
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
     9
 *     notice, this list of conditions and the following disclaimer.
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    10
 *
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    11
 *   - Redistributions in binary form must reproduce the above copyright
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    12
 *     notice, this list of conditions and the following disclaimer in the
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    13
 *     documentation and/or other materials provided with the distribution.
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    14
 *
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    15
 *   - Neither the name of Oracle nor the names of its
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    16
 *     contributors may be used to endorse or promote products derived
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    17
 *     from this software without specific prior written permission.
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    18
 *
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    20
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    21
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    22
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    23
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    24
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    25
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    26
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    27
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    28
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    29
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    30
 */
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    31
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    32
/* This sample implements Java-like try..with..resource construct for nashorn */
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    33
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    34
if (arguments.length == 0) {
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    35
    print("Usage: jjs try_with_resource.js -- <file name>");
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    36
    exit(1);
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    37
}
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    38
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    39
// Object.create accepts prototype and properties object
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    40
function With(res) {
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    41
   return Object.create(With.prototype,
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    42
       { res: { value: res } });
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    43
}
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    44
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    45
// reserved words can be used as property names - for example "try"
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    46
With.prototype.try = function(callback) {
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    47
     try {
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    48
         callback();
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    49
     } finally {
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    50
         this.res.close();
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    51
     }
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    52
}
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    53
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    54
var BufferedReader = Java.type("java.io.BufferedReader");
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    55
var FileReader = Java.type("java.io.FileReader");
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    56
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    57
var r = new BufferedReader(new FileReader(arguments[0]));
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    58
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    59
// Java-like try..with..resource
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    60
With(r).try(function() {
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    61
    var s;
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    62
    while ((s = r.readLine()) != null) {
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    63
        print(s);
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    64
    }
1c348e8d9b88 8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
sundar
parents:
diff changeset
    65
})