本文整理汇总了Java中jdk.nashorn.internal.runtime.ScriptRuntime类的典型用法代码示例。如果您正苦于以下问题:Java ScriptRuntime类的具体用法?Java ScriptRuntime怎么用?Java ScriptRuntime使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ScriptRuntime类属于jdk.nashorn.internal.runtime包,在下文中一共展示了ScriptRuntime类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: evalImpl
import jdk.nashorn.internal.runtime.ScriptRuntime; //导入依赖的package包/类
private static Object evalImpl(final Context.MultiGlobalCompiledScript mgcs, final ScriptContext ctxt, final Global ctxtGlobal) throws ScriptException {
final Global oldGlobal = Context.getGlobal();
final boolean globalChanged = (oldGlobal != ctxtGlobal);
try {
if (globalChanged) {
Context.setGlobal(ctxtGlobal);
}
final ScriptFunction script = mgcs.getFunction(ctxtGlobal);
ctxtGlobal.setScriptContext(ctxt);
return ScriptObjectMirror.translateUndefined(ScriptObjectMirror.wrap(ScriptRuntime.apply(script, ctxtGlobal), ctxtGlobal));
} catch (final Exception e) {
throwAsScriptException(e, ctxtGlobal);
throw new AssertionError("should not reach here");
} finally {
if (globalChanged) {
Context.setGlobal(oldGlobal);
}
}
}
示例2: trim
import jdk.nashorn.internal.runtime.ScriptRuntime; //导入依赖的package包/类
/**
* ECMA 15.5.4.20 String.prototype.trim ( )
* @param self self reference
* @return string trimmed from whitespace
*/
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static String trim(final Object self) {
final String str = checkObjectToString(self);
int start = 0;
int end = str.length() - 1;
while (start <= end && ScriptRuntime.isJSWhitespace(str.charAt(start))) {
start++;
}
while (end > start && ScriptRuntime.isJSWhitespace(str.charAt(end))) {
end--;
}
return str.substring(start, end + 1);
}
示例3: forEach
import jdk.nashorn.internal.runtime.ScriptRuntime; //导入依赖的package包/类
/**
* ECMA6 23.2.3.6 Set.prototype.forEach ( callbackfn [ , thisArg ] )
*
* @param self the self reference
* @param callbackFn the callback function
* @param thisArg optional this object
*/
@Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1)
public static void forEach(final Object self, final Object callbackFn, final Object thisArg) {
final NativeSet set = getNativeSet(self);
if (!Bootstrap.isCallable(callbackFn)) {
throw typeError("not.a.function", ScriptRuntime.safeToString(callbackFn));
}
final MethodHandle invoker = Global.instance().getDynamicInvoker(FOREACH_INVOKER_KEY,
() -> Bootstrap.createDynamicCallInvoker(Object.class, Object.class, Object.class, Object.class, Object.class, Object.class));
final LinkedMap.LinkedMapIterator iterator = set.getJavaMap().getIterator();
for (;;) {
final LinkedMap.Node node = iterator.next();
if (node == null) {
break;
}
try {
final Object result = invoker.invokeExact(callbackFn, thisArg, node.getKey(), node.getKey(), self);
} catch (final RuntimeException | Error e) {
throw e;
} catch (final Throwable t) {
throw new RuntimeException(t);
}
}
}
示例4: call
import jdk.nashorn.internal.runtime.ScriptRuntime; //导入依赖的package包/类
/**
* ECMA 15.3.4.4 Function.prototype.call (thisArg [ , arg1 [ , arg2, ... ] ] )
*
* @param self self reference
* @param args arguments for call
* @return result of call
*/
@Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1)
public static Object call(final Object self, final Object... args) {
checkCallable(self);
final Object thiz = (args.length == 0) ? UNDEFINED : args[0];
Object[] arguments;
if (args.length > 1) {
arguments = new Object[args.length - 1];
System.arraycopy(args, 1, arguments, 0, arguments.length);
} else {
arguments = ScriptRuntime.EMPTY_ARRAY;
}
if (self instanceof ScriptFunction) {
return ScriptRuntime.apply((ScriptFunction)self, thiz, arguments);
} else if (self instanceof JSObject) {
return ((JSObject)self).call(thiz, arguments);
}
throw new AssertionError("should not reach here");
}
示例5: NativeArray
import jdk.nashorn.internal.runtime.ScriptRuntime; //导入依赖的package包/类
NativeArray(final Object[] array) {
this(ArrayData.allocate(array.length));
ArrayData arrayData = this.getArray();
if (array.length > 0) {
arrayData.ensure(array.length - 1);
}
for (int index = 0; index < array.length; index++) {
final Object value = array[index];
if (value == ScriptRuntime.EMPTY) {
arrayData = arrayData.delete(index);
} else {
arrayData = arrayData.set(index, value, false);
}
}
this.setArray(arrayData);
}
示例6: toString
import jdk.nashorn.internal.runtime.ScriptRuntime; //导入依赖的package包/类
/**
* ECMA 15.4.4.2 Array.prototype.toString ( )
*
* @param self self reference
* @return string representation of array
*/
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static Object toString(final Object self) {
final Object obj = Global.toObject(self);
if (obj instanceof ScriptObject) {
final InvokeByName joinInvoker = getJOIN();
final ScriptObject sobj = (ScriptObject)obj;
try {
final Object join = joinInvoker.getGetter().invokeExact(sobj);
if (Bootstrap.isCallable(join)) {
return joinInvoker.getInvoker().invokeExact(join, sobj);
}
} catch (final RuntimeException | Error e) {
throw e;
} catch (final Throwable t) {
throw new RuntimeException(t);
}
}
// FIXME: should lookup Object.prototype.toString and call that?
return ScriptRuntime.builtinObjectToString(self);
}
示例7: join
import jdk.nashorn.internal.runtime.ScriptRuntime; //导入依赖的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();
}
示例8: push
import jdk.nashorn.internal.runtime.ScriptRuntime; //导入依赖的package包/类
/**
* ECMA 15.4.4.7 Array.prototype.push (args...)
*
* @param self self reference
* @param args arguments to push
* @return array length after pushes
*/
@Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1)
public static Object push(final Object self, final Object... args) {
try {
final ScriptObject sobj = (ScriptObject)self;
if (bulkable(sobj) && sobj.getArray().length() + args.length <= JSType.MAX_UINT) {
final ArrayData newData = sobj.getArray().push(true, args);
sobj.setArray(newData);
return newData.length();
}
long len = JSType.toUint32(sobj.getLength());
for (final Object element : args) {
sobj.set(len++, element, CALLSITE_STRICT);
}
sobj.set("length", len, CALLSITE_STRICT);
return len;
} catch (final ClassCastException | NullPointerException e) {
throw typeError(Context.getGlobal(), e, "not.an.object", ScriptRuntime.safeToString(self));
}
}
示例9: testToString_Object
import jdk.nashorn.internal.runtime.ScriptRuntime; //导入依赖的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)
}
示例10: asObjectArray
import jdk.nashorn.internal.runtime.ScriptRuntime; //导入依赖的package包/类
@Override
public Object[] asObjectArray() {
final int len = (int)Math.min(length(), Integer.MAX_VALUE);
final int underlyingLength = (int)Math.min(len, underlying.length());
final Object[] objArray = new Object[len];
for (int i = 0; i < underlyingLength; i++) {
objArray[i] = underlying.getObject(i);
}
Arrays.fill(objArray, underlyingLength, len, ScriptRuntime.UNDEFINED);
for (final Map.Entry<Long, Object> entry : sparseMap.entrySet()) {
final long key = entry.getKey();
if (key < Integer.MAX_VALUE) {
objArray[(int)key] = entry.getValue();
} else {
break; // ascending key order
}
}
return objArray;
}
示例11: pop
import jdk.nashorn.internal.runtime.ScriptRuntime; //导入依赖的package包/类
@Override
public Object pop() {
final long len = length();
final long underlyingLen = underlying.length();
if (len == 0) {
return ScriptRuntime.UNDEFINED;
}
if (len == underlyingLen) {
final Object result = underlying.pop();
setLength(underlying.length());
return result;
}
setLength(len - 1);
final Long key = len - 1;
return sparseMap.containsKey(key) ? sparseMap.remove(key) : ScriptRuntime.UNDEFINED;
}
示例12: linkBean
import jdk.nashorn.internal.runtime.ScriptRuntime; //导入依赖的package包/类
private static GuardedInvocation linkBean(final LinkRequest linkRequest) throws Exception {
final CallSiteDescriptor desc = linkRequest.getCallSiteDescriptor();
final Object self = linkRequest.getReceiver();
switch (NashornCallSiteDescriptor.getStandardOperation(desc)) {
case NEW:
if(BeansLinker.isDynamicConstructor(self)) {
throw typeError("no.constructor.matches.args", ScriptRuntime.safeToString(self));
}
if(BeansLinker.isDynamicMethod(self)) {
throw typeError("method.not.constructor", ScriptRuntime.safeToString(self));
}
throw typeError("not.a.function", NashornCallSiteDescriptor.getFunctionErrorMessage(desc, self));
case CALL:
if(BeansLinker.isDynamicConstructor(self)) {
throw typeError("constructor.requires.new", ScriptRuntime.safeToString(self));
}
if(BeansLinker.isDynamicMethod(self)) {
throw typeError("no.method.matches.args", ScriptRuntime.safeToString(self));
}
throw typeError("not.a.function", NashornCallSiteDescriptor.getFunctionErrorMessage(desc, self));
default:
// Everything else is supposed to have been already handled by Bootstrap.beansLinker
// delegating to linkNoSuchBeanMember
throw new AssertionError("unknown call type " + desc);
}
}
示例13: propertyIterator
import jdk.nashorn.internal.runtime.ScriptRuntime; //导入依赖的package包/类
@Override
public Iterator<String> propertyIterator() {
// Try __getIds__ first, if not found then try __getKeys__
// In jdk6, we had added "__getIds__" so this is just for compatibility.
Object func = adaptee.get(__getIds__);
if (!(func instanceof ScriptFunction)) {
func = adaptee.get(__getKeys__);
}
Object obj;
if (func instanceof ScriptFunction) {
obj = ScriptRuntime.apply((ScriptFunction)func, this);
} else {
obj = new NativeArray(0);
}
final List<String> array = new ArrayList<>();
for (final Iterator<Object> iter = ArrayLikeIterator.arrayLikeIterator(obj); iter.hasNext(); ) {
array.add((String)iter.next());
}
return array.iterator();
}
示例14: NativeArray
import jdk.nashorn.internal.runtime.ScriptRuntime; //导入依赖的package包/类
NativeArray(final Object[] array) {
this(ArrayData.allocate(array.length));
ArrayData arrayData = this.getArray();
for (int index = 0; index < array.length; index++) {
final Object value = array[index];
if (value == ScriptRuntime.EMPTY) {
arrayData = arrayData.delete(index);
} else {
arrayData = arrayData.set(index, value, false);
}
}
this.setArray(arrayData);
}
示例15: toString
import jdk.nashorn.internal.runtime.ScriptRuntime; //导入依赖的package包/类
@Override
public String toString() {
return inGlobal(new Callable<String>() {
@Override
public String call() {
return ScriptRuntime.safeToString(sobj);
}
});
}