本文整理汇总了Java中jdk.nashorn.internal.runtime.ScriptRuntime类的典型用法代码示例。如果您正苦于以下问题:Java ScriptRuntime类的具体用法?Java ScriptRuntime怎么用?Java ScriptRuntime使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ScriptRuntime类属于jdk.nashorn.internal.runtime包,在下文中一共展示了ScriptRuntime类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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);
}
}
}
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:21,代码来源:NashornScriptEngine.java
示例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);
}
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:21,代码来源:NativeString.java
示例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);
}
}
}
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:33,代码来源:NativeSet.java
示例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");
}
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:30,代码来源:NativeFunction.java
示例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);
}
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:21,代码来源:NativeArray.java
示例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);
}
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:28,代码来源:NativeArray.java
示例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();
}
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:28,代码来源:NativeArray.java
示例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));
}
}
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:30,代码来源:NativeArray.java
示例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)
}
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:19,代码来源:JSTypeTest.java
示例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;
}
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:24,代码来源:SparseArrayData.java
示例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;
}
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:17,代码来源:SparseArrayData.java
示例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);
}
}
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:27,代码来源:NashornBottomLinker.java
示例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();
}
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:24,代码来源:NativeJSAdapter.java
示例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);
}
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:18,代码来源:NativeArray.java
示例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);
}
});
}
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:10,代码来源:ScriptObjectMirror.java
示例16: toString
import jdk.nashorn.internal.runtime.ScriptRuntime; //导入依赖的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);
}
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:44,代码来源:Shell.java
示例17: callMember
import jdk.nashorn.internal.runtime.ScriptRuntime; //导入依赖的package包/类
/**
* Call member function
* @param functionName function name
* @param args arguments
* @return return value of function
*/
public Object callMember(final String functionName, final Object... args) {
Objects.requireNonNull(functionName);
final Global oldGlobal = Context.getGlobal();
final boolean globalChanged = (oldGlobal != global);
try {
if (globalChanged) {
Context.setGlobal(global);
}
final Object val = sobj.get(functionName);
if (val instanceof ScriptFunction) {
final Object[] modArgs = globalChanged? wrapArrayLikeMe(args, oldGlobal) : args;
return wrapLikeMe(ScriptRuntime.apply((ScriptFunction)val, sobj, unwrapArray(modArgs, global)));
} else if (val instanceof JSObject && ((JSObject)val).isFunction()) {
return ((JSObject)val).call(sobj, args);
}
throw new NoSuchMethodException("No such function " + functionName);
} catch (final NashornException ne) {
throw ne.initEcmaError(global);
} catch (final RuntimeException | Error e) {
throw e;
} catch (final Throwable t) {
throw new RuntimeException(t);
} finally {
if (globalChanged) {
Context.setGlobal(oldGlobal);
}
}
}
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:38,代码来源:ScriptObjectMirror.java
示例18: checkDeleteIndex
import jdk.nashorn.internal.runtime.ScriptRuntime; //导入依赖的package包/类
private boolean checkDeleteIndex(final int index, final boolean strict) {
if (isValidStringIndex(index)) {
if (strict) {
throw typeError("cant.delete.property", Integer.toString(index), ScriptRuntime.safeToString(this));
}
return true;
}
return false;
}
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:11,代码来源:NativeString.java
示例19: trimRight
import jdk.nashorn.internal.runtime.ScriptRuntime; //导入依赖的package包/类
/**
* Nashorn extension: String.prototype.trimRight ( )
* @param self self reference
* @return string trimmed right from whitespace
*/
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static String trimRight(final Object self) {
final String str = checkObjectToString(self);
final int start = 0;
int end = str.length() - 1;
while (end >= start && ScriptRuntime.isJSWhitespace(str.charAt(end))) {
end--;
}
return str.substring(start, end + 1);
}
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:19,代码来源:NativeString.java
示例20: callAdaptee
import jdk.nashorn.internal.runtime.ScriptRuntime; //导入依赖的package包/类
private Object callAdaptee(final Object retValue, final String name, final Object... args) {
final Object func = adaptee.get(name);
if (func instanceof ScriptFunction) {
return ScriptRuntime.apply((ScriptFunction)func, this, args);
}
return retValue;
}
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:8,代码来源:NativeJSAdapter.java
注:本文中的jdk.nashorn.internal.runtime.ScriptRuntime类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论