本文整理汇总了Java中jdk.nashorn.internal.runtime.JSType类的典型用法代码示例。如果您正苦于以下问题:Java JSType类的具体用法?Java JSType怎么用?Java JSType使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
JSType类属于jdk.nashorn.internal.runtime包,在下文中一共展示了JSType类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: max
import jdk.nashorn.internal.runtime.JSType; //导入依赖的package包/类
/**
* ECMA 15.8.2.11 max(x)
*
* @param self self reference
* @param args arguments
*
* @return the largest of the arguments, {@link Double#NEGATIVE_INFINITY} if no args given, or identity if one arg is given
*/
@Function(arity = 2, attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static double max(final Object self, final Object... args) {
switch (args.length) {
case 0:
return Double.NEGATIVE_INFINITY;
case 1:
return JSType.toNumber(args[0]);
default:
double res = JSType.toNumber(args[0]);
for (int i = 1; i < args.length; i++) {
res = Math.max(res, JSType.toNumber(args[i]));
}
return res;
}
}
示例2: testToString_Object
import jdk.nashorn.internal.runtime.JSType; //导入依赖的package包/类
/**
* Test of toString method, of class Runtime.
*/
@Test
public void testToString_Object() {
assertEquals(JSType.toString(ScriptRuntime.UNDEFINED), "undefined");
assertEquals(JSType.toString(null), "null");
assertEquals(JSType.toString(Boolean.TRUE), "true");
assertEquals(JSType.toString(Boolean.FALSE), "false");
assertEquals(JSType.toString(""), "");
assertEquals(JSType.toString("nashorn"), "nashorn");
assertEquals(JSType.toString(Double.NaN), "NaN");
assertEquals(JSType.toString(Double.POSITIVE_INFINITY), "Infinity");
assertEquals(JSType.toString(Double.NEGATIVE_INFINITY), "-Infinity");
assertEquals(JSType.toString(0.0), "0");
// FIXME: add more number-to-string test cases
// FIXME: add case for Object type (JSObject with getDefaultValue)
}
示例3: join
import jdk.nashorn.internal.runtime.JSType; //导入依赖的package包/类
/**
* ECMA 15.4.4.5 Array.prototype.join (separator)
*
* @param self self reference
* @param separator element separator
* @return string representation after join
*/
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static String join(final Object self, final Object separator) {
final StringBuilder sb = new StringBuilder();
final Iterator<Object> iter = arrayLikeIterator(self, true);
final String sep = separator == ScriptRuntime.UNDEFINED ? "," : JSType.toString(separator);
while (iter.hasNext()) {
final Object obj = iter.next();
if (obj != null && obj != ScriptRuntime.UNDEFINED) {
sb.append(JSType.toString(obj));
}
if (iter.hasNext()) {
sb.append(sep);
}
}
return sb.toString();
}
示例4: toLocaleString
import jdk.nashorn.internal.runtime.JSType; //导入依赖的package包/类
/**
* ECMA 15.2.4.3 Object.prototype.toLocaleString ( )
*
* @param self self reference
* @return localized ToString
*/
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static Object toLocaleString(final Object self) {
final Object obj = JSType.toScriptObject(self);
if (obj instanceof ScriptObject) {
final InvokeByName toStringInvoker = getTO_STRING();
final ScriptObject sobj = (ScriptObject)obj;
try {
final Object toString = toStringInvoker.getGetter().invokeExact(sobj);
if (Bootstrap.isCallable(toString)) {
return toStringInvoker.getInvoker().invokeExact(toString, sobj);
}
} catch (final RuntimeException | Error e) {
throw e;
} catch (final Throwable t) {
throw new RuntimeException(t);
}
throw typeError("not.a.function", "toString");
}
return ScriptRuntime.builtinObjectToString(self);
}
示例5: split
import jdk.nashorn.internal.runtime.JSType; //导入依赖的package包/类
/**
* ECMA 15.5.4.14 String.prototype.split (separator, limit)
*
* @param self self reference
* @param separator separator for split
* @param limit limit for splits
* @return array object in which splits have been placed
*/
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static ScriptObject split(final Object self, final Object separator, final Object limit) {
final String str = checkObjectToString(self);
final long lim = limit == UNDEFINED ? JSType.MAX_UINT : JSType.toUint32(limit);
if (separator == UNDEFINED) {
return lim == 0 ? new NativeArray() : new NativeArray(new Object[]{str});
}
if (separator instanceof NativeRegExp) {
return ((NativeRegExp) separator).split(str, lim);
}
// when separator is a string, it is treated as a literal search string to be used for splitting.
return splitString(str, JSType.toString(separator), lim);
}
示例6: push
import jdk.nashorn.internal.runtime.JSType; //导入依赖的package包/类
/**
* ECMA 15.4.4.7 Array.prototype.push (args...) specialized for single object argument
*
* @param self self reference
* @param arg argument to push
* @return array after pushes
*/
@SpecializedFunction
public static double push(final Object self, final Object arg) {
try {
final ScriptObject sobj = (ScriptObject)self;
final ArrayData arrayData = sobj.getArray();
final long length = arrayData.length();
if (bulkable(sobj) && length < JSType.MAX_UINT) {
sobj.setArray(arrayData.push(true, arg));
return length + 1;
}
long len = JSType.toUint32(sobj.getLength());
sobj.set(len++, arg, CALLSITE_STRICT);
sobj.set("length", len, CALLSITE_STRICT);
return len;
} catch (final ClassCastException | NullPointerException e) {
throw typeError("not.an.object", ScriptRuntime.safeToString(self));
}
}
示例7: fillFrom
import jdk.nashorn.internal.runtime.JSType; //导入依赖的package包/类
@Override
public PropertyDescriptor fillFrom(final ScriptObject sobj) {
if (sobj.has(CONFIGURABLE)) {
this.configurable = JSType.toBoolean(sobj.get(CONFIGURABLE));
} else {
delete(CONFIGURABLE, false);
}
if (sobj.has(ENUMERABLE)) {
this.enumerable = JSType.toBoolean(sobj.get(ENUMERABLE));
} else {
delete(ENUMERABLE, false);
}
return this;
}
示例8: min
import jdk.nashorn.internal.runtime.JSType; //导入依赖的package包/类
/**
* ECMA 15.8.2.12 min(x)
*
* @param self self reference
* @param args arguments
*
* @return the smallest of the arguments, {@link Double#NEGATIVE_INFINITY} if no args given, or identity if one arg is given
*/
@Function(arity = 2, attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static double min(final Object self, final Object... args) {
switch (args.length) {
case 0:
return Double.POSITIVE_INFINITY;
case 1:
return JSType.toNumber(args[0]);
default:
double res = JSType.toNumber(args[0]);
for (int i = 1; i < args.length; i++) {
res = Math.min(res, JSType.toNumber(args[i]));
}
return res;
}
}
示例9: convertCtorArgs
import jdk.nashorn.internal.runtime.JSType; //导入依赖的package包/类
private static double[] convertCtorArgs(final Object[] args) {
final double[] d = new double[7];
boolean nullReturn = false;
// should not bailout on first NaN or infinite. Need to convert all
// subsequent args for possible side-effects via valueOf/toString overrides
// on argument objects.
for (int i = 0; i < d.length; i++) {
if (i < args.length) {
final double darg = JSType.toNumber(args[i]);
if (isNaN(darg) || isInfinite(darg)) {
nullReturn = true;
}
d[i] = (long)darg;
} else {
d[i] = i == 2 ? 1 : 0; // day in month defaults to 1
}
}
if (0 <= d[0] && d[0] <= 99) {
d[0] += 1900;
}
return nullReturn? null : d;
}
示例10: toNumber
import jdk.nashorn.internal.runtime.JSType; //导入依赖的package包/类
@Override
public double toNumber() {
return inGlobal(new Callable<Double>() {
@Override public Double call() {
return JSType.toNumber(sobj);
}
});
}
示例11: get
import jdk.nashorn.internal.runtime.JSType; //导入依赖的package包/类
@SuppressWarnings("unused")
private static Object get(final Object self, final Object key) {
final CharSequence cs = JSType.toCharSequence(self);
final Object primitiveKey = JSType.toPrimitive(key, String.class);
final int index = ArrayIndex.getArrayIndex(primitiveKey);
if (index >= 0 && index < cs.length()) {
return String.valueOf(cs.charAt(index));
}
return ((ScriptObject) Global.toObject(self)).get(primitiveKey);
}
示例12: toString
import jdk.nashorn.internal.runtime.JSType; //导入依赖的package包/类
/**
* Converts {@code result} to a printable string. The reason we don't use {@link JSType#toString(Object)}
* or {@link ScriptRuntime#safeToString(Object)} is that we want to be able to render Symbol values
* even if they occur within an Array, and therefore have to implement our own Array to String
* conversion.
*
* @param result the result
* @param global the global object
* @return the string representation
*/
protected static String toString(final Object result, final Global global) {
if (result instanceof Symbol) {
// Normal implicit conversion of symbol to string would throw TypeError
return result.toString();
}
if (result instanceof NativeSymbol) {
return JSType.toPrimitive(result).toString();
}
if (isArrayWithDefaultToString(result, global)) {
// This should yield the same string as Array.prototype.toString but
// will not throw if the array contents include symbols.
final StringBuilder sb = new StringBuilder();
final Iterator<Object> iter = ArrayLikeIterator.arrayLikeIterator(result, true);
while (iter.hasNext()) {
final Object obj = iter.next();
if (obj != null && obj != ScriptRuntime.UNDEFINED) {
sb.append(toString(obj, global));
}
if (iter.hasNext()) {
sb.append(',');
}
}
return sb.toString();
}
return JSType.toString(result);
}
示例13: getIntOptimistic
import jdk.nashorn.internal.runtime.JSType; //导入依赖的package包/类
@Override
public int getIntOptimistic(final int index, final int programPoint) {
if (index >= length()) {
return JSType.toInt32Optimistic(get(index), programPoint);
}
return underlying.getIntOptimistic(index, programPoint);
}
示例14: fillFrom
import jdk.nashorn.internal.runtime.JSType; //导入依赖的package包/类
@Override
public PropertyDescriptor fillFrom(final ScriptObject sobj) {
if (sobj.has(CONFIGURABLE)) {
this.configurable = JSType.toBoolean(sobj.get(CONFIGURABLE));
} else {
delete(CONFIGURABLE, false);
}
if (sobj.has(ENUMERABLE)) {
this.enumerable = JSType.toBoolean(sobj.get(ENUMERABLE));
} else {
delete(ENUMERABLE, false);
}
if (sobj.has(WRITABLE)) {
this.writable = JSType.toBoolean(sobj.get(WRITABLE));
} else {
delete(WRITABLE, false);
}
if (sobj.has(VALUE)) {
this.value = sobj.get(VALUE);
} else {
delete(VALUE, false);
}
return this;
}
示例15: hasOwnProperty
import jdk.nashorn.internal.runtime.JSType; //导入依赖的package包/类
/**
* ECMA 15.2.4.5 Object.prototype.hasOwnProperty (V)
*
* @param self self reference
* @param v property to check for
* @return true if property exists in object
*/
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static boolean hasOwnProperty(final Object self, final Object v) {
// Convert ScriptObjects to primitive with String.class hint
// but no need to convert other primitives to string.
final Object key = JSType.toPrimitive(v, String.class);
final Object obj = Global.toObject(self);
return obj instanceof ScriptObject && ((ScriptObject)obj).hasOwnProperty(key);
}