1 /*
   2  * Copyright (c) 2015, 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  */
  23 
  24 /**
  25  * JDK-8130853: Non-extensible global is not handled property
  26  *
  27  * @test
  28  * @run
  29  */
  30 
  31 // don't allow extensions to global
  32 Object.preventExtensions(this);
  33 try {
  34     eval("var x = 34;");
  35     throw new Error("should have thrown TypeError");
  36 } catch (e) {
  37     if (! (e instanceof TypeError)) {
  38         throw e;
  39     }
  40 }
  41 
  42 try {
  43     eval("function func() {}");
  44     throw new Error("should have thrown TypeError");
  45 } catch (e) {
  46     if (! (e instanceof TypeError)) {
  47         throw e;
  48     }
  49 }
  50 
  51 function checkLoad(code) {
  52     try {
  53         load({ name: "test", script: code });
  54         throw new Error("should have thrown TypeError for load: " + code);
  55     } catch (e) {
  56         if (! (e instanceof TypeError)) {
  57             throw e;
  58         }
  59     }
  60 }
  61 
  62 checkLoad("var y = 55");
  63 checkLoad("function f() {}");
  64 
  65 // check script engine eval
  66 var ScriptEngineManager = Java.type("javax.script.ScriptEngineManager");
  67 var e = new ScriptEngineManager().getEngineByName("nashorn");
  68 var global = e.eval("this");
  69 e.eval("Object.preventExtensions(this);");
  70 try {
  71     e.eval("var myVar = 33;");
  72     throw new Error("should have thrown TypeError");
  73 } catch (e) {
  74     if (! (e.cause.ecmaError instanceof global.TypeError)) {
  75         throw e;
  76     }
  77 }
  78 
  79 // Object.bindProperties on arbitrary non-extensible object
  80 var obj = {};
  81 Object.preventExtensions(obj);
  82 try {
  83     Object.bindProperties(obj, { foo: 434 });
  84     throw new Error("should have thrown TypeError");
  85 } catch (e) {
  86     if (! (e instanceof TypeError)) {
  87         throw e;
  88     }
  89 }