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      * Initialize standard builtin objects like "Object", "Array", "Function" etc.
  41      * as well as our extension builtin objects like "Java", "JSAdapter" as properties
  42      * of the global scope object.
  43      */
  44     public void initBuiltinObjects();
  45 
  46     /**
  47      * Wrapper for {@link jdk.nashorn.internal.objects.Global#newScriptFunction(String, MethodHandle, ScriptObject, boolean)}
  48      *
  49      * @param name   function name
  50      * @param handle invocation handle for function
  51      * @param scope  the scope
  52      * @param strict are we in strict mode
  53      *
  54      * @return new script function
  55      */
  56    public ScriptFunction newScriptFunction(String name, MethodHandle handle, ScriptObject scope, boolean strict);
  57 
  58     /**
  59      * Wrapper for {@link jdk.nashorn.internal.objects.Global#wrapAsObject(Object)}
  60      *
  61      * @param obj object to wrap
  62      * @return    wrapped object
  63      */
  64    public Object wrapAsObject(Object obj);
  65 
  66 
  67     /**
  68      * Wrapper for {@link jdk.nashorn.internal.objects.Global#primitiveLookup(LinkRequest, Object)}
  69      *
  70      * @param request the link request for the dynamic call site.
  71      * @param self     self reference
  72      *
  73      * @return guarded invocation
  74      */
  75    public GuardedInvocation primitiveLookup(LinkRequest request, Object self);
  76 
  77 
  78     /**
  79      * Wrapper for {@link jdk.nashorn.internal.objects.Global#newObject()}
  80      *
  81      * @return the new ScriptObject
  82      */
  83    public ScriptObject newObject();
  84 
  85     /**
  86      * Wrapper for {@link jdk.nashorn.internal.objects.Global#isError(ScriptObject)}
  87      *
  88      * @param sobj to check if it is an error object
  89      * @return true if error object
  90      */
  91    public boolean isError(ScriptObject sobj);
  92 
  93     /**
  94      * Wrapper for {@link jdk.nashorn.internal.objects.Global#newError(String)}
  95      *
  96      * @param msg the error message
  97      *
  98      * @return the new ScriptObject representing the error
  99      */
 100    public ScriptObject newError(String msg);
 101 
 102     /**
 103      * Wrapper for {@link jdk.nashorn.internal.objects.Global#newEvalError(String)}
 104      *
 105      * @param msg the error message
 106      *
 107      * @return the new ScriptObject representing the eval error
 108      */
 109    public ScriptObject newEvalError(String msg);
 110 
 111     /**
 112      * Wrapper for {@link jdk.nashorn.internal.objects.Global#newRangeError(String)}
 113      *
 114      * @param msg the error message
 115      *
 116      * @return the new ScriptObject representing the range error
 117      */
 118    public ScriptObject newRangeError(String msg);
 119 
 120     /**
 121      * Wrapper for {@link jdk.nashorn.internal.objects.Global#newReferenceError(String)}
 122      *
 123      * @param msg the error message
 124      *
 125      * @return the new ScriptObject representing the reference error
 126      */
 127    public ScriptObject newReferenceError(String msg);
 128 
 129     /**
 130      * Wrapper for {@link jdk.nashorn.internal.objects.Global#newSyntaxError(String)}
 131      *
 132      * @param msg the error message
 133      *
 134      * @return the new ScriptObject representing the syntax error
 135      */
 136    public ScriptObject newSyntaxError(String msg);
 137 
 138     /**
 139      * Wrapper for {@link jdk.nashorn.internal.objects.Global#newTypeError(String)}
 140      *
 141      * @param msg the error message
 142      *
 143      * @return the new ScriptObject representing the type error
 144      */
 145    public ScriptObject newTypeError(String msg);
 146 
 147     /**
 148      * Wrapper for {@link jdk.nashorn.internal.objects.Global#newURIError(String)}
 149      *
 150      * @param msg the error message
 151      *
 152      * @return the new ScriptObject representing the URI error
 153      */
 154     public ScriptObject newURIError(String msg);
 155 
 156     /**
 157      * Wrapper for {@link jdk.nashorn.internal.objects.Global#newGenericDescriptor(boolean, boolean)}
 158      *
 159      * @param configurable is the described property configurable
 160      * @param enumerable   is the described property enumerable
 161      *
 162      * @return property descriptor
 163      */
 164     public PropertyDescriptor newGenericDescriptor(boolean configurable, boolean enumerable);
 165 
 166     /**
 167      * Wrapper for {@link jdk.nashorn.internal.objects.Global#newDataDescriptor(Object, boolean, boolean, boolean)}
 168      *
 169      * @param value        data value
 170      * @param configurable is the described property configurable
 171      * @param enumerable   is the described property enumerable
 172      * @param writable     is the described property writable
 173      *
 174      * @return property descriptor
 175      */
 176     public PropertyDescriptor newDataDescriptor(Object value, boolean configurable, boolean enumerable, boolean writable);
 177 
 178     /**
 179      * Wrapper for {@link jdk.nashorn.internal.objects.Global#newAccessorDescriptor(Object, Object, boolean, boolean)}
 180      *
 181      * @param get          property getter, or null if none
 182      * @param set          property setter, or null if none
 183      * @param configurable is the described property configurable
 184      * @param enumerable   is the described property enumerable
 185      *
 186      * @return property descriptor
 187      */
 188     public PropertyDescriptor newAccessorDescriptor(Object get, Object set, boolean configurable, boolean enumerable);
 189 
 190     /**
 191      * Wrapper for {@link jdk.nashorn.internal.objects.Global#getDefaultValue(ScriptObject, Class)}
 192      *
 193      * @param sobj     script object
 194      * @param typeHint type hint
 195      *
 196      * @return default value
 197      */
 198     public Object getDefaultValue(ScriptObject sobj, Class<?> typeHint);
 199 
 200     /**
 201      * Find the compiled Class for the given script source, if available
 202      *
 203      * @param source Source object of the script
 204      * @return compiled Class object or null
 205      */
 206     public Class<?> findCachedClass(Source source);
 207 
 208     /**
 209      * Put the Source associated Class object in the Source-to-Class cache
 210      *
 211      * @param source Source of the script
 212      * @param clazz compiled Class object for the source
 213      */
 214     public void cacheClass(Source source, Class<?> clazz);
 215 
 216     /**
 217      * Get cached InvokeByName object for the given key
 218      * @param key key to be associated with InvokeByName object
 219      * @param creator if InvokeByName is absent 'creator' is called to make one (lazy init)
 220      * @return InvokeByName object associated with the key.
 221      */
 222     public InvokeByName getInvokeByName(final Object key, final Callable<InvokeByName> creator);
 223 
 224     /**
 225      * Get cached dynamic method handle for the given key
 226      * @param key key to be associated with dynamic method handle
 227      * @param creator if method handle is absent 'creator' is called to make one (lazy init)
 228      * @return dynamic method handle associated with the key.
 229      */
 230     public MethodHandle getDynamicInvoker(final Object key, final Callable<MethodHandle> creator);
 231 }