jdk/src/share/classes/com/sun/script/javascript/RhinoTopLevel.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 7800 cf50efba2e1d
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 7800
diff changeset
     2
 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.script.javascript;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import sun.org.mozilla.javascript.internal.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.script.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * This class serves as top level scope for Rhino. This class adds
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * 3 top level functions (bindings, scope, sync) and two constructors
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * (JSAdapter, JavaAdapter).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * @author A. Sundararajan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public final class RhinoTopLevel extends ImporterTopLevel {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    RhinoTopLevel(Context cx, RhinoScriptEngine engine) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        super(cx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        this.engine = engine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        // initialize JSAdapter lazily. Reduces footprint & startup time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        new LazilyLoadedCtor(this, "JSAdapter",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
                "com.sun.script.javascript.JSAdapter",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
                false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
         * initialize JavaAdapter. We can't lazy initialize this because
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
         * lazy initializer attempts to define a new property. But, JavaAdapter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
         * is an exisiting property that we overwrite.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        JavaAdapter.init(cx, this, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        // add top level functions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        String names[] = { "bindings", "scope", "sync"  };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        defineFunctionProperties(names, RhinoTopLevel.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                ScriptableObject.DONTENUM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * The bindings function takes a JavaScript scope object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * of type ExternalScriptable and returns the underlying Bindings
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     *    var page = scope(pageBindings);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     *    with (page) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     *       // code that uses page scope
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     *    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     *    var b = bindings(page);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     *    // operate on bindings here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    public static Object bindings(Context cx, Scriptable thisObj, Object[] args,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            Function funObj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        if (args.length == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            Object arg = args[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            if (arg instanceof Wrapper) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                arg = ((Wrapper)arg).unwrap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            if (arg instanceof ExternalScriptable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                ScriptContext ctx = ((ExternalScriptable)arg).getContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                Bindings bind = ctx.getBindings(ScriptContext.ENGINE_SCOPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                return Context.javaToJS(bind,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                           ScriptableObject.getTopLevelScope(thisObj));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        return cx.getUndefinedValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * The scope function creates a new JavaScript scope object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * with given Bindings object as backing store. This can be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * to create a script scope based on arbitrary Bindings instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * For example, in webapp scenario, a 'page' level Bindings instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * may be wrapped as a scope and code can be run in JavaScripe 'with'
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * statement:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *    var page = scope(pageBindings);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     *    with (page) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *       // code that uses page scope
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    public static Object scope(Context cx, Scriptable thisObj, Object[] args,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            Function funObj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        if (args.length == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            Object arg = args[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            if (arg instanceof Wrapper) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                arg = ((Wrapper)arg).unwrap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            if (arg instanceof Bindings) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                ScriptContext ctx = new SimpleScriptContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                ctx.setBindings((Bindings)arg, ScriptContext.ENGINE_SCOPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                Scriptable res = new ExternalScriptable(ctx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                res.setPrototype(ScriptableObject.getObjectPrototype(thisObj));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                res.setParentScope(ScriptableObject.getTopLevelScope(thisObj));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        return cx.getUndefinedValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * The sync function creates a synchronized function (in the sense
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * of a Java synchronized method) from an existing function. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * new function synchronizes on the <code>this</code> object of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * its invocation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * js> var o = { f : sync(function(x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *       print("entry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *       Packages.java.lang.Thread.sleep(x*1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *       print("exit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *     })};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * js> thread(function() {o.f(5);});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * js> thread(function() {o.f(5);});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * js>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * exit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * exit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    public static Object sync(Context cx, Scriptable thisObj, Object[] args,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            Function funObj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        if (args.length == 1 && args[0] instanceof Function) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            return new Synchronizer((Function)args[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            throw Context.reportRuntimeError("wrong argument(s) for sync");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    RhinoScriptEngine getScriptEngine() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        return engine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    private RhinoScriptEngine engine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
}