NASHORN-500.js revision 2:da1e581c933b
1351278Sdim/*
2351278Sdim * Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved.
3351278Sdim * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4351278Sdim *
5351278Sdim * This code is free software; you can redistribute it and/or modify it
6351278Sdim * under the terms of the GNU General Public License version 2 only, as
7351278Sdim * published by the Free Software Foundation.
8351278Sdim *
9351278Sdim * This code is distributed in the hope that it will be useful, but WITHOUT
10351278Sdim * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11351278Sdim * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12351278Sdim * version 2 for more details (a copy is included in the LICENSE file that
13351278Sdim * accompanied this code).
14351278Sdim *
15351278Sdim * You should have received a copy of the GNU General Public License version
16351278Sdim * 2 along with this work; if not, write to the Free Software Foundation,
17351278Sdim * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18351278Sdim *
19351278Sdim * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20351278Sdim * or visit www.oracle.com if you need additional information or have any
21351278Sdim * questions.
22351278Sdim */
23351278Sdim
24351278Sdim/**
25360784Sdim * NASHORN-500 :  Using ScriptRuntime.toString for error reporting from runtime is problematic
26360784Sdim *
27360784Sdim * @test
28360784Sdim * @run
29360784Sdim */
30360784Sdim
31351278Sdimvar origToString = Object.prototype.toString;
32351278SdimObject.prototype.toString = function () {
33351278Sdim    return this.myStr();
34351278Sdim}
35351278Sdim
36351278Sdimtry {
37351278Sdim    print({});
38351278Sdim    fail("#1 toString should have thrown TypeError");
39351278Sdim} catch (e) {
40351278Sdim    if (! (e instanceof TypeError)) {
41351278Sdim        fail("#2 TypeError expected, got " + e);
42351278Sdim    }
43351278Sdim} finally {
44351278Sdim    Object.prototype.toString = origToString;
45351278Sdim}
46360784Sdim
47360784Sdimtry {
48360784Sdim    print({ toString: function() { return this.foo(); } });
49360784Sdim    fail("#3 toString should have thrown TypeError");
50360784Sdim} catch (e) {
51360784Sdim    if (! (e instanceof TypeError)) {
52360784Sdim        fail("#4 TypeError expected, got " + e);
53351278Sdim    }
54351278Sdim}
55351278Sdim
56351278Sdimtry {
57351278Sdim    var e = new Error();
58351278Sdim    e.toString = function() { return this.foo(); }
59351278Sdim    var lineNumber = __LINE__;
60351278Sdim    throw e;
61351278Sdim} catch (e) {
62351278Sdim    if (e.nashornException.lineNumber !== lineNumber  + 1) {
63351278Sdim        fail("#5 exception throw line number is incorrect");
64351278Sdim    }
65351278Sdim}
66351278Sdim
67351278Sdim