Tue, 14 Oct 2014 16:16:12 +0530
8050977: Java8 Javascript Nashorn exception: no current Global instance for nashorn
Reviewed-by: attila, lagergren, hannesw
1 /*
2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
24 /**
25 * mh_coverage.js
26 * Screen scrape various logs to ensure that we cover enough functionality,
27 * e.g. method handle instrumentation
28 *
29 * @test
30 * @run
31 */
33 /*
34 * creates new script engine initialized with given options and
35 * runs given code on it. Returns standard output captured.
36 */
38 function runScriptEngine(opts, name) {
39 var imports = new JavaImporter(
40 Packages.jdk.nashorn.api.scripting,
41 java.io, java.lang, java.util);
43 with(imports) {
44 var fac = new NashornScriptEngineFactory();
45 // get current System.err
46 var oldErr = System.err;
47 var oldOut = System.out;
48 var baosErr = new ByteArrayOutputStream();
49 var newErr = new PrintStream(baosErr);
50 var baosOut = new ByteArrayOutputStream();
51 var newOut = new PrintStream(baosOut);
52 try {
53 // set new standard err
54 System.setErr(newErr);
55 System.setOut(newOut);
56 var engine = fac.getScriptEngine(Java.to(opts, "java.lang.String[]"));
57 var reader = new java.io.FileReader(name);
58 engine.eval(reader);
59 newErr.flush();
60 newOut.flush();
61 return new java.lang.String(baosErr.toByteArray());
62 } finally {
63 // restore System.err to old value
64 System.setErr(oldErr);
65 System.setOut(oldOut);
66 }
67 }
68 }
70 var str;
72 var methodsCalled = [
73 'asCollector',
74 'asType',
75 'bindTo',
76 'dropArguments',
77 'explicitCastArguments',
78 'filterArguments',
79 'filterReturnValue',
80 'findStatic',
81 'findVirtual',
82 'foldArguments',
83 'getter',
84 'guardWithTest',
85 'insertArguments',
86 'methodType',
87 'setter'
88 ];
90 function check(str, strs) {
91 for each (s in strs) {
92 if (str.indexOf(s) !== -1) {
93 continue;
94 }
95 print(s + " not found");
96 return;
97 }
98 print("check ok!");
99 }
101 str = runScriptEngine([ "--log=codegen,compiler=finest,methodhandles=finest,fields=finest" ], __DIR__ + "../basic/NASHORN-19.js");
102 str += runScriptEngine([ "--log=codegen,compiler=finest,methodhandles=finest,fields=finest" ], __DIR__ + "../basic/varargs.js");
104 check(str, methodsCalled);
105 check(str, ['return', 'get', 'set', '[fields]']);
106 check(str, ['codegen']);
108 print("hello, world!");