jdk.nashorn.internal.runtime.Undefined Java Examples

The following examples show how to use jdk.nashorn.internal.runtime.Undefined. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JavaAdapterServices.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Given a JS script object, retrieves a function from it by name, binds it to the script object as its "this", and
 * adapts its parameter types, return types, and arity to the specified type and arity. This method is public mainly
 * for implementation reasons, so the adapter classes can invoke it from their constructors that take a Object
 * in its first argument to obtain the method handles for their method implementations.
 * @param obj the script obj
 * @param name the name of the property that contains the function
 * @param type the method type it has to conform to
 * @return the appropriately adapted method handle for invoking the script function, or null if the value of the
 * property is either null or undefined, or "toString" was requested as the name, but the object doesn't directly
 * define it but just inherits it through prototype.
 */
public static MethodHandle getHandle(final Object obj, final String name, final MethodType type) {
    if (! (obj instanceof ScriptObject)) {
        throw typeError("not.an.object", ScriptRuntime.safeToString(obj));
    }

    final ScriptObject sobj = (ScriptObject)obj;
    // Since every JS Object has a toString, we only override "String toString()" it if it's explicitly specified
    if ("toString".equals(name) && !sobj.hasOwnProperty("toString")) {
        return null;
    }

    final Object fnObj = sobj.get(name);
    if (fnObj instanceof ScriptFunction) {
        return bindAndAdaptHandle((ScriptFunction)fnObj, sobj, type);
    } else if(fnObj == null || fnObj instanceof Undefined) {
        return null;
    } else {
        throw typeError("not.a.function", name);
    }
}
 
Example #2
Source File: ObjectType.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Type ldc(final MethodVisitor method, final Object c) {
    if (c == null) {
        method.visitInsn(ACONST_NULL);
    } else if (c instanceof Undefined) {
        return loadUndefined(method);
    } else if (c instanceof String) {
        method.visitLdcInsn(c);
        return STRING;
    } else if (c instanceof Handle) {
        method.visitLdcInsn(c);
        return Type.typeFor(MethodHandle.class);
    } else {
        throw new UnsupportedOperationException("implementation missing for class " + c.getClass() + " value=" + c);
    }

    return Type.OBJECT;
}
 
Example #3
Source File: JavaAdapterServices.java    From nashorn with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Given a JS script object, retrieves a function from it by name, binds it to the script object as its "this", and
 * adapts its parameter types, return types, and arity to the specified type and arity. This method is public mainly
 * for implementation reasons, so the adapter classes can invoke it from their constructors that take a Object
 * in its first argument to obtain the method handles for their method implementations.
 * @param obj the script obj
 * @param name the name of the property that contains the function
 * @param type the method type it has to conform to
 * @return the appropriately adapted method handle for invoking the script function, or null if the value of the
 * property is either null or undefined, or "toString" was requested as the name, but the object doesn't directly
 * define it but just inherits it through prototype.
 */
public static MethodHandle getHandle(final Object obj, final String name, final MethodType type) {
    if (! (obj instanceof ScriptObject)) {
        throw typeError("not.an.object", ScriptRuntime.safeToString(obj));
    }

    final ScriptObject sobj = (ScriptObject)obj;
    // Since every JS Object has a toString, we only override "String toString()" it if it's explicitly specified
    if ("toString".equals(name) && !sobj.hasOwnProperty("toString")) {
        return null;
    }

    final Object fnObj = sobj.get(name);
    if (fnObj instanceof ScriptFunction) {
        return adaptHandle(((ScriptFunction)fnObj).getBoundInvokeHandle(sobj), type);
    } else if(fnObj == null || fnObj instanceof Undefined) {
        return null;
    } else {
        throw typeError("not.a.function", name);
    }
}
 
Example #4
Source File: JavaAdapterServices.java    From jdk8u_nashorn with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Given a JS script object, retrieves a function from it by name, binds it to the script object as its "this", and
 * adapts its parameter types, return types, and arity to the specified type and arity. This method is public mainly
 * for implementation reasons, so the adapter classes can invoke it from their constructors that take a Object
 * in its first argument to obtain the method handles for their method implementations.
 * @param obj the script obj
 * @param name the name of the property that contains the function
 * @param type the method type it has to conform to
 * @return the appropriately adapted method handle for invoking the script function, or null if the value of the
 * property is either null or undefined, or "toString" was requested as the name, but the object doesn't directly
 * define it but just inherits it through prototype.
 */
public static MethodHandle getHandle(final Object obj, final String name, final MethodType type) {
    if (! (obj instanceof ScriptObject)) {
        throw typeError("not.an.object", ScriptRuntime.safeToString(obj));
    }

    final ScriptObject sobj = (ScriptObject)obj;
    // Since every JS Object has a toString, we only override "String toString()" it if it's explicitly specified
    if ("toString".equals(name) && !sobj.hasOwnProperty("toString")) {
        return null;
    }

    final Object fnObj = sobj.get(name);
    if (fnObj instanceof ScriptFunction) {
        return bindAndAdaptHandle((ScriptFunction)fnObj, sobj, type);
    } else if(fnObj == null || fnObj instanceof Undefined) {
        return null;
    } else {
        throw typeError("not.a.function", name);
    }
}
 
Example #5
Source File: JavaAdapterServices.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Given a JS script object, retrieves a function from it by name, binds it to the script object as its "this", and
 * adapts its parameter types, return types, and arity to the specified type and arity. This method is public mainly
 * for implementation reasons, so the adapter classes can invoke it from their constructors that take a Object
 * in its first argument to obtain the method handles for their method implementations.
 * @param obj the script obj
 * @param name the name of the property that contains the function
 * @param type the method type it has to conform to
 * @return the appropriately adapted method handle for invoking the script function, or null if the value of the
 * property is either null or undefined, or "toString" was requested as the name, but the object doesn't directly
 * define it but just inherits it through prototype.
 */
public static MethodHandle getHandle(final Object obj, final String name, final MethodType type) {
    if (! (obj instanceof ScriptObject)) {
        throw typeError("not.an.object", ScriptRuntime.safeToString(obj));
    }

    final ScriptObject sobj = (ScriptObject)obj;
    // Since every JS Object has a toString, we only override "String toString()" it if it's explicitly specified
    if ("toString".equals(name) && !sobj.hasOwnProperty("toString")) {
        return null;
    }

    final Object fnObj = sobj.get(name);
    if (fnObj instanceof ScriptFunction) {
        return bindAndAdaptHandle((ScriptFunction)fnObj, sobj, type);
    } else if(fnObj == null || fnObj instanceof Undefined) {
        return null;
    } else {
        throw typeError("not.a.function", name);
    }
}
 
Example #6
Source File: JavaAdapterServices.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Given a JS script object, retrieves a function from it by name, binds it to the script object as its "this", and
 * adapts its parameter types, return types, and arity to the specified type and arity. This method is public mainly
 * for implementation reasons, so the adapter classes can invoke it from their constructors that take a Object
 * in its first argument to obtain the method handles for their method implementations.
 * @param obj the script obj
 * @param name the name of the property that contains the function
 * @param type the method type it has to conform to
 * @return the appropriately adapted method handle for invoking the script function, or null if the value of the
 * property is either null or undefined, or "toString" was requested as the name, but the object doesn't directly
 * define it but just inherits it through prototype.
 */
public static MethodHandle getHandle(final Object obj, final String name, final MethodType type) {
    if (! (obj instanceof ScriptObject)) {
        throw typeError("not.an.object", ScriptRuntime.safeToString(obj));
    }

    final ScriptObject sobj = (ScriptObject)obj;
    // Since every JS Object has a toString, we only override "String toString()" it if it's explicitly specified
    if ("toString".equals(name) && !sobj.hasOwnProperty("toString")) {
        return null;
    }

    final Object fnObj = sobj.get(name);
    if (fnObj instanceof ScriptFunction) {
        return bindAndAdaptHandle((ScriptFunction)fnObj, sobj, type);
    } else if(fnObj == null || fnObj instanceof Undefined) {
        return null;
    } else {
        throw typeError("not.a.function", name);
    }
}
 
Example #7
Source File: ObjectType.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Type ldc(final MethodVisitor method, final Object c) {
    if (c == null) {
        method.visitInsn(ACONST_NULL);
    } else if (c instanceof Undefined) {
        return loadUndefined(method);
    } else if (c instanceof String) {
        method.visitLdcInsn(c);
        return STRING;
    } else if (c instanceof Handle) {
        method.visitLdcInsn(c);
        return Type.typeFor(MethodHandle.class);
    } else {
        throw new UnsupportedOperationException("implementation missing for class " + c.getClass() + " value=" + c);
    }

    return Type.OBJECT;
}
 
Example #8
Source File: StringIterator.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected IteratorResult next(final Object arg) {
    final int index = nextIndex;
    final String string = iteratedString;

    if (string == null || index >= string.length()) {
        // ES6 21.1.5.2.1 step 8
        iteratedString = null;
        return makeResult(Undefined.getUndefined(), Boolean.TRUE, global);
    }

    final char first = string.charAt(index);
    if (first >= 0xd800 && first <= 0xdbff && index < string.length() - 1) {
        final char second = string.charAt(index + 1);
        if (second >= 0xdc00 && second <= 0xdfff) {
            nextIndex += 2;
            return makeResult(String.valueOf(new char[] {first, second}), Boolean.FALSE, global);
        }
    }

    nextIndex++;
    return makeResult(String.valueOf(first), Boolean.FALSE, global);
}
 
Example #9
Source File: ObjectType.java    From jdk8u_nashorn with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Type ldc(final MethodVisitor method, final Object c) {
    if (c == null) {
        method.visitInsn(ACONST_NULL);
    } else if (c instanceof Undefined) {
        return loadUndefined(method);
    } else if (c instanceof String) {
        method.visitLdcInsn(c);
        return STRING;
    } else if (c instanceof Handle) {
        method.visitLdcInsn(c);
        return Type.typeFor(MethodHandle.class);
    } else {
        throw new UnsupportedOperationException("implementation missing for class " + c.getClass() + " value=" + c);
    }

    return Type.OBJECT;
}
 
Example #10
Source File: SetIterator.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected  IteratorResult next(final Object arg) {
    if (iterator == null) {
        return makeResult(Undefined.getUndefined(), Boolean.TRUE, global);
    }

    final LinkedMap.Node node = iterator.next();

    if (node == null) {
        iterator = null;
        return makeResult(Undefined.getUndefined(), Boolean.TRUE, global);
    }

    if (iterationKind == IterationKind.KEY_VALUE) {
        final NativeArray array = new NativeArray(new Object[] {node.getKey(), node.getKey()});
        return makeResult(array, Boolean.FALSE, global);
    }

    return makeResult(node.getKey(), Boolean.FALSE, global);
}
 
Example #11
Source File: MapIterator.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected  IteratorResult next(final Object arg) {
    if (iterator == null) {
        return makeResult(Undefined.getUndefined(), Boolean.TRUE, global);
    }

    final LinkedMap.Node node = iterator.next();

    if (node == null) {
        iterator = null;
        return makeResult(Undefined.getUndefined(), Boolean.TRUE, global);
    }

    if (iterationKind == IterationKind.KEY_VALUE) {
        final NativeArray array = new NativeArray(new Object[] {node.getKey(), node.getValue()});
        return makeResult(array, Boolean.FALSE, global);
    }

    return makeResult(iterationKind == IterationKind.KEY ? node.getKey() : node.getValue(), Boolean.FALSE, global);
}
 
Example #12
Source File: ObjectType.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Type ldc(final MethodVisitor method, final Object c) {
    if (c == null) {
        method.visitInsn(ACONST_NULL);
    } else if (c instanceof Undefined) {
        return loadUndefined(method);
    } else if (c instanceof String) {
        method.visitLdcInsn(c);
        return STRING;
    } else if (c instanceof Handle) {
        method.visitLdcInsn(c);
        return Type.typeFor(MethodHandle.class);
    } else {
        throw new UnsupportedOperationException("implementation missing for class " + c.getClass() + " value=" + c);
    }

    return Type.OBJECT;
}
 
Example #13
Source File: NashornLinker.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
@Override
public GuardedInvocation getGuardedInvocation(final LinkRequest request, final LinkerServices linkerServices) throws Exception {
    final LinkRequest requestWithoutContext = request.withoutRuntimeContext(); // Nashorn has no runtime context
    final Object self = requestWithoutContext.getReceiver();
    final CallSiteDescriptor desc = requestWithoutContext.getCallSiteDescriptor();

    if (desc.getNameTokenCount() < 2 || !"dyn".equals(desc.getNameToken(CallSiteDescriptor.SCHEME))) {
        // We only support standard "dyn:*[:*]" operations
        return null;
    }

    final GuardedInvocation inv;
    if (self instanceof ScriptObject) {
        inv = ((ScriptObject)self).lookup(desc, request);
    } else if (self instanceof Undefined) {
        inv = Undefined.lookup(desc);
    } else {
        throw new AssertionError(); // Should never reach here.
    }

    return Bootstrap.asType(inv, linkerServices, desc);
}
 
Example #14
Source File: NativeWeakMap.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
static void populateMap(final Map<Object, Object> map, final Object arg, final Global global) {
    // This method is similar to NativeMap.populateMap, but it uses a different
    // map implementation and the checking/conversion of keys differs as well.
    if (arg != null && arg != Undefined.getUndefined()) {
        AbstractIterator.iterate(arg, global, value -> {
            if (isPrimitive(value)) {
                throw typeError(global, "not.an.object", ScriptRuntime.safeToString(value));
            }
            if (value instanceof ScriptObject) {
                final ScriptObject sobj = (ScriptObject) value;
                map.put(checkKey(sobj.get(0)), sobj.get(1));
            }
        });
    }
}
 
Example #15
Source File: NativeWeakMap.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA6 23.3.3.3 WeakMap.prototype.get ( key )
 *
 * @param self the self reference
 * @param key the key
 * @return the associated value or undefined
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static Object get(final Object self, final Object key) {
    final NativeWeakMap map = getMap(self);
    if (isPrimitive(key)) {
        return Undefined.getUndefined();
    }
    return map.jmap.get(key);
}
 
Example #16
Source File: NashornLinker.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private static GuardedInvocation getGuardedInvocation(final Object self, final LinkRequest request, final CallSiteDescriptor desc) {
    final GuardedInvocation inv;
    if (self instanceof ScriptObject) {
        inv = ((ScriptObject)self).lookup(desc, request);
    } else if (self instanceof Undefined) {
        inv = Undefined.lookup(desc);
    } else {
        throw new AssertionError(self.getClass().getName()); // Should never reach here.
    }

    return inv;
}
 
Example #17
Source File: NativeArray.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private static MethodHandle getREDUCE_CALLBACK_INVOKER() {
    return Global.instance().getDynamicInvoker(REDUCE_CALLBACK_INVOKER,
            new Callable<MethodHandle>() {
                @Override
                public MethodHandle call() {
                    return Bootstrap.createDynamicInvoker("dyn:call", Object.class, Object.class,
                         Undefined.class, Object.class, Object.class, long.class, Object.class);
                }
            });
}
 
Example #18
Source File: NativeArray.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private static MethodHandle getREDUCE_CALLBACK_INVOKER() {
    return Global.instance().getDynamicInvoker(REDUCE_CALLBACK_INVOKER,
            new Callable<MethodHandle>() {
                @Override
                public MethodHandle call() {
                    return Bootstrap.createDynamicInvoker("dyn:call", Object.class, Object.class,
                         Undefined.class, Object.class, Object.class, double.class, Object.class);
                }
            });
}
 
Example #19
Source File: NashornLinker.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static GuardedInvocation getGuardedInvocation(final Object self, final LinkRequest request, final CallSiteDescriptor desc) {
    final GuardedInvocation inv;
    if (self instanceof ScriptObject) {
        inv = ((ScriptObject)self).lookup(desc, request);
    } else if (self instanceof Undefined) {
        inv = Undefined.lookup(desc);
    } else {
        throw new AssertionError(self.getClass().getName()); // Should never reach here.
    }

    return inv;
}
 
Example #20
Source File: NativeArray.java    From jdk8u_nashorn with GNU General Public License v2.0 5 votes vote down vote up
private static MethodHandle getREDUCE_CALLBACK_INVOKER() {
    return Global.instance().getDynamicInvoker(REDUCE_CALLBACK_INVOKER,
            new Callable<MethodHandle>() {
                @Override
                public MethodHandle call() {
                    return Bootstrap.createDynamicInvoker("dyn:call", Object.class, Object.class,
                         Undefined.class, Object.class, Object.class, double.class, Object.class);
                }
            });
}
 
Example #21
Source File: NativeArray.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static MethodHandle getREDUCE_CALLBACK_INVOKER() {
    return Global.instance().getDynamicInvoker(REDUCE_CALLBACK_INVOKER,
            new Callable<MethodHandle>() {
                @Override
                public MethodHandle call() {
                    return Bootstrap.createDynamicInvoker("dyn:call", Object.class, Object.class,
                         Undefined.class, Object.class, Object.class, double.class, Object.class);
                }
            });
}
 
Example #22
Source File: NativeWeakSet.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
static void populateWeakSet(final Map<Object, Boolean> set, final Object arg, final Global global) {
    if (arg != null && arg != Undefined.getUndefined()) {
        AbstractIterator.iterate(arg, global, value -> {
                set.put(checkKey(value), Boolean.TRUE);
        });
    }
}
 
Example #23
Source File: NativeSymbol.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 6 19.4.1.1 Symbol ( [ description ] )
 *
 * @param newObj is this function invoked with the new operator
 * @param self   self reference
 * @param args   arguments
 * @return new symbol value
 */
@Constructor(arity = 1)
public static Object constructor(final boolean newObj, final Object self, final Object... args) {
    if (newObj) {
        throw typeError("symbol.as.constructor");
    }
    final String description = args.length > 0 && args[0] != Undefined.getUndefined() ?
            JSType.toString(args[0]) : "";
    return new Symbol(description);
}
 
Example #24
Source File: NativeSymbol.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ES6 19.4.2.5 Symbol.keyFor ( sym )
 *
 * @param self self reference
 * @param arg the argument
 * @return the symbol name
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public synchronized static Object keyFor(final Object self, final Object arg) {
    if (!(arg instanceof Symbol)) {
        throw typeError("not.a.symbol", ScriptRuntime.safeToString(arg));
    }
    final String name = ((Symbol) arg).getName();
    return globalSymbolRegistry.get(name) == arg ? name : Undefined.getUndefined();
}
 
Example #25
Source File: NativeController.java    From proxyee-down with Apache License 2.0 5 votes vote down vote up
@RequestMapping("onResolve")
public FullHttpResponse onResolve(Channel channel, FullHttpRequest request) throws Exception {
  HttpRequestForm taskRequest = getJSONParams(request, HttpRequestForm.class);
  //遍历扩展模块是否有对应的处理
  List<ExtensionInfo> extensionInfos = ExtensionContent.get();
  for (ExtensionInfo extensionInfo : extensionInfos) {
    if (extensionInfo.getMeta().isEnabled()) {
      if (extensionInfo.getHookScript() != null
          && !StringUtils.isEmpty(extensionInfo.getHookScript().getScript())) {
        Event event = extensionInfo.getHookScript().hasEvent(HookScript.EVENT_RESOLVE, taskRequest.getUrl());
        if (event != null) {
          try {
            //执行resolve方法
            Object result = ExtensionUtil.invoke(extensionInfo, event, taskRequest, false);
            if (result != null && !(result instanceof Undefined)) {
              ObjectMapper objectMapper = new ObjectMapper();
              String temp = objectMapper.writeValueAsString(result);
              TaskForm taskForm = objectMapper.readValue(temp, TaskForm.class);
              //有一个扩展解析成功的话直接返回
              return HttpHandlerUtil.buildJson(taskForm, Include.NON_DEFAULT);
            }
          } catch (Exception e) {
            LOGGER.error("An exception occurred while resolve()", e);
          }
        }
      }
    }
  }
  return new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
}
 
Example #26
Source File: StaticTypeInspector.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
public static String inspect(final Undefined x, final String w) {
    return w + ": undefined";
}
 
Example #27
Source File: NashornLinker.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@SuppressWarnings("unused")
private static boolean isNashornTypeOrUndefined(final Object obj) {
    return obj instanceof ScriptObject || obj instanceof Undefined;
}
 
Example #28
Source File: ArrayFilter.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
static Object convertUndefinedValue(final Class<?> targetType) {
    return invoke(Bootstrap.getLinkerServices().getTypeConverter(Undefined.class, targetType),
            ScriptRuntime.UNDEFINED);
}
 
Example #29
Source File: ArrayFilter.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
static Object convertUndefinedValue(final Class<?> targetType) {
    return invoke(Bootstrap.getLinkerServices().getTypeConverter(Undefined.class, targetType),
            ScriptRuntime.UNDEFINED);
}
 
Example #30
Source File: ObjectType.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Type loadUndefined(final MethodVisitor method) {
    method.visitFieldInsn(GETSTATIC, className(ScriptRuntime.class), "UNDEFINED", typeDescriptor(Undefined.class));
    return UNDEFINED;
}