NASHORN-768.js revision 6:5a1b0714df0e
155714Skris/*
255714Skris * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
355714Skris * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
455714Skris *
555714Skris * This code is free software; you can redistribute it and/or modify it
655714Skris * under the terms of the GNU General Public License version 2 only, as
755714Skris * published by the Free Software Foundation.
855714Skris *
955714Skris * This code is distributed in the hope that it will be useful, but WITHOUT
1055714Skris * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1155714Skris * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1255714Skris * version 2 for more details (a copy is included in the LICENSE file that
1355714Skris * accompanied this code).
1455714Skris *
1555714Skris * You should have received a copy of the GNU General Public License version
1655714Skris * 2 along with this work; if not, write to the Free Software Foundation,
1755714Skris * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1855714Skris *
1955714Skris * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2055714Skris * or visit www.oracle.com if you need additional information or have any
2155714Skris * questions.
2255714Skris */
2355714Skris
2455714Skris/**
2555714Skris * NASHORN-768 :  Implement cross context property/function access and browser JSObject access by JSObject dynalink linker
2655714Skris *
2755714Skris * @test
2855714Skris * @option -scripting
2955714Skris * @run
3055714Skris */
3155714Skris
3255714Skris// create another nashorn engine
3355714Skrisvar m = new javax.script.ScriptEngineManager();
3455714Skrisvar engine = m.getEngineByName("nashorn");
3555714Skris
3655714Skris// our global var 'id'
3755714Skrisvar id = "global-id";
3855714Skris
3955714Skrisengine.eval(<<CODE
4055714Skris
4155714Skris// code evaluated in engine
4255714Skris
4355714Skris// engine code's id
4455714Skrisvar id = "engine-global-id";
4555714Skris
4655714Skris// global object
4755714Skrisvar obj = {
4855714Skris    foo: 42,
4955714Skris    bar: function(x) {
5055714Skris        if (id != "engine-global-id") {
5155714Skris            throw "id != 'engine-global-id'";
5255714Skris        }
5355714Skris        // check this.foo is accessible
5455714Skris        if (this.foo != 42) {
5555714Skris            throw "this.foo != 42";
5655714Skris        }
5755714Skris
58238405Sjkim        return x;
59238405Sjkim    }
60238405Sjkim};
61238405Sjkim
62238405Sjkim// global function
63238405Sjkimfunction func(callback) {
64238405Sjkim    var res = callback(id);
65238405Sjkim    if (res != id) {
66238405Sjkim        fail("result of callback is wrong");
67238405Sjkim    }
68238405Sjkim}
69238405Sjkim
70238405SjkimCODE);
71238405Sjkim
72238405Sjkimvar obj = engine.get("obj");
73238405Sjkimif (obj.bar("hello") != "hello") {
74238405Sjkim    fail("obj.bar('hello') return value is wrong");
75238405Sjkim}
76238405Sjkim
77238405Sjkimif (obj.foo != 42) {
78238405Sjkim    fail("obj.foo != 42");
79238405Sjkim}
80238405Sjkim
81238405Sjkimvar func = engine.get("func");
82238405Sjkimvar inside_f = false;
83238405Sjkimfunction f(arg) {
84238405Sjkim    inside_f = true;
85238405Sjkim    if (id != "global-id") {
86238405Sjkim        fail("id != 'global-id'");
87238405Sjkim    }
88238405Sjkim    return arg;
89238405Sjkim}
90238405Sjkimfunc(f);
91238405Sjkimif (! inside_f) {
92238405Sjkim    fail("function 'f' was not called");
93238405Sjkim}
94238405Sjkim