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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.nashorn.internal.runtime;
  27 
  28 import java.lang.invoke.MethodHandle;
  29 import java.util.concurrent.Callable;
  30 import jdk.internal.dynalink.linker.GuardedInvocation;
  31 import jdk.internal.dynalink.linker.LinkRequest;
  32 import jdk.nashorn.internal.runtime.linker.InvokeByName;
  33 
  34 /**
  35  * Runtime interface to the global scope objects.
  36  */
  37 
  38 public interface GlobalObject {
  39     /**
  40      * Is this global of the given Context?
  41      * @param ctxt the context
  42      * @return true if this global belongs to the given Context
  43      */
  44     public boolean isOfContext(final Context ctxt);
  45 
  46     /**
  47      * Does this global belong to a strict Context?
  48      * @return true if this global belongs to a strict Context
  49      */
  50     public boolean isStrictContext();
  51 
  52     /**
  53      * Initialize standard builtin objects like "Object", "Array", "Function" etc.
  54      * as well as our extension builtin objects like "Java", "JSAdapter" as properties
  55      * of the global scope object.
  56      */
  57     public void initBuiltinObjects();
  58 
  59     /**
  60      * Wrapper for {@link jdk.nashorn.internal.objects.Global#newScriptFunction(String, MethodHandle, ScriptObject, boolean)}
  61      *
  62      * @param name   function name
  63      * @param handle invocation handle for function
  64      * @param scope  the scope
  65      * @param strict are we in strict mode
  66      *
  67      * @return new script function
  68      */
  69    public ScriptFunction newScriptFunction(String name, MethodHandle handle, ScriptObject scope, boolean strict);
  70 
  71     /**
  72      * Wrapper for {@link jdk.nashorn.internal.objects.Global#wrapAsObject(Object)}
  73      *
  74      * @param obj object to wrap
  75      * @return    wrapped object
  76      */
  77    public Object wrapAsObject(Object obj);
  78 
  79 
  80     /**
  81      * Wrapper for {@link jdk.nashorn.internal.objects.Global#primitiveLookup(LinkRequest, Object)}
  82      *
  83      * @param request the link request for the dynamic call site.
  84      * @param self     self reference
  85      *
  86      * @return guarded invocation
  87      */
  88    public GuardedInvocation primitiveLookup(LinkRequest request, Object self);
  89 
  90 
  91     /**
  92      * Wrapper for {@link jdk.nashorn.internal.objects.Global#newObject()}
  93      *
  94      * @return the new ScriptObject
  95      */
  96    public ScriptObject newObject();
  97 
  98     /**
  99      * Wrapper for {@link jdk.nashorn.internal.objects.Global#isError(ScriptObject)}
 100      *
 101      * @param sobj to check if it is an error object
 102      * @return true if error object
 103      */
 104    public boolean isError(ScriptObject sobj);
 105 
 106     /**
 107      * Wrapper for {@link jdk.nashorn.internal.objects.Global#newError(String)}
 108      *
 109      * @param msg the error message
 110      *
 111      * @return the new ScriptObject representing the error
 112      */
 113    public ScriptObject newError(String msg);
 114 
 115     /**
 116      * Wrapper for {@link jdk.nashorn.internal.objects.Global#newEvalError(String)}
 117      *
 118      * @param msg the error message
 119      *
 120      * @return the new ScriptObject representing the eval error
 121      */
 122    public ScriptObject newEvalError(String msg);
 123 
 124     /**
 125      * Wrapper for {@link jdk.nashorn.internal.objects.Global#newRangeError(String)}
 126      *
 127      * @param msg the error message
 128      *
 129      * @return the new ScriptObject representing the range error
 130      */
 131    public ScriptObject newRangeError(String msg);
 132 
 133     /**
 134      * Wrapper for {@link jdk.nashorn.internal.objects.Global#newReferenceError(String)}
 135      *
 136      * @param msg the error message
 137      *
 138      * @return the new ScriptObject representing the reference error
 139      */
 140    public ScriptObject newReferenceError(String msg);
 141 
 142     /**
 143      * Wrapper for {@link jdk.nashorn.internal.objects.Global#newSyntaxError(String)}
 144      *
 145      * @param msg the error message
 146      *
 147      * @return the new ScriptObject representing the syntax error
 148      */
 149    public ScriptObject newSyntaxError(String msg);
 150 
 151     /**
 152      * Wrapper for {@link jdk.nashorn.internal.objects.Global#newTypeError(String)}
 153      *
 154      * @param msg the error message
 155      *
 156      * @return the new ScriptObject representing the type error
 157      */
 158    public ScriptObject newTypeError(String msg);
 159 
 160     /**
 161      * Wrapper for {@link jdk.nashorn.internal.objects.Global#newURIError(String)}
 162      *
 163      * @param msg the error message
 164      *
 165      * @return the new ScriptObject representing the URI error
 166      */
 167     public ScriptObject newURIError(String msg);
 168 
 169     /**
 170      * Wrapper for {@link jdk.nashorn.internal.objects.Global#newGenericDescriptor(boolean, boolean)}
 171      *
 172      * @param configurable is the described property configurable
 173      * @param enumerable   is the described property enumerable
 174      *
 175      * @return property descriptor
 176      */
 177     public PropertyDescriptor newGenericDescriptor(boolean configurable, boolean enumerable);
 178 
 179     /**
 180      * Wrapper for {@link jdk.nashorn.internal.objects.Global#newDataDescriptor(Object, boolean, boolean, boolean)}
 181      *
 182      * @param value        data value
 183      * @param configurable is the described property configurable
 184      * @param enumerable   is the described property enumerable
 185      * @param writable     is the described property writable
 186      *
 187      * @return property descriptor
 188      */
 189     public PropertyDescriptor newDataDescriptor(Object value, boolean configurable, boolean enumerable, boolean writable);
 190 
 191     /**
 192      * Wrapper for {@link jdk.nashorn.internal.objects.Global#newAccessorDescriptor(Object, Object, boolean, boolean)}
 193      *
 194      * @param get          property getter, or null if none
 195      * @param set          property setter, or null if none
 196      * @param configurable is the described property configurable
 197      * @param enumerable   is the described property enumerable
 198      *
 199      * @return property descriptor
 200      */
 201     public PropertyDescriptor newAccessorDescriptor(Object get, Object set, boolean configurable, boolean enumerable);
 202 
 203     /**
 204      * Wrapper for {@link jdk.nashorn.internal.objects.Global#getDefaultValue(ScriptObject, Class)}
 205      *
 206      * @param sobj     script object
 207      * @param typeHint type hint
 208      *
 209      * @return default value
 210      */
 211     public Object getDefaultValue(ScriptObject sobj, Class<?> typeHint);
 212 
 213     /**
 214      * Get cached InvokeByName object for the given key
 215      * @param key key to be associated with InvokeByName object
 216      * @param creator if InvokeByName is absent 'creator' is called to make one (lazy init)
 217      * @return InvokeByName object associated with the key.
 218      */
 219     public InvokeByName getInvokeByName(final Object key, final Callable<InvokeByName> creator);
 220 
 221     /**
 222      * Get cached dynamic method handle for the given key
 223      * @param key key to be associated with dynamic method handle
 224      * @param creator if method handle is absent 'creator' is called to make one (lazy init)
 225      * @return dynamic method handle associated with the key.
 226      */
 227     public MethodHandle getDynamicInvoker(final Object key, final Callable<MethodHandle> creator);
 228 }