本文整理汇总了Java中jdk.nashorn.internal.runtime.ScriptObject.set方法的典型用法代码示例。如果您正苦于以下问题:Java ScriptObject.set方法的具体用法?Java ScriptObject.set怎么用?Java ScriptObject.set使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类jdk.nashorn.internal.runtime.ScriptObject
的用法示例。
在下文中一共展示了ScriptObject.set方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: push
import jdk.nashorn.internal.runtime.ScriptObject; //导入方法依赖的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));
}
}
示例2: push
import jdk.nashorn.internal.runtime.ScriptObject; //导入方法依赖的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 JSType.toNarrowestNumber(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 JSType.toNarrowestNumber(len);
} catch (final ClassCastException | NullPointerException e) {
throw typeError(Context.getGlobal(), e, "not.an.object", ScriptRuntime.safeToString(self));
}
}
示例3: initErrorObjects
import jdk.nashorn.internal.runtime.ScriptObject; //导入方法依赖的package包/类
private void initErrorObjects() {
// Error objects
this.builtinError = initConstructor("Error", ScriptFunction.class);
final ScriptObject errorProto = getErrorPrototype();
// Nashorn specific accessors on Error.prototype - stack, lineNumber, columnNumber and fileName
final ScriptFunction getStack = ScriptFunctionImpl.makeFunction("getStack", NativeError.GET_STACK);
final ScriptFunction setStack = ScriptFunctionImpl.makeFunction("setStack", NativeError.SET_STACK);
errorProto.addOwnProperty("stack", Attribute.NOT_ENUMERABLE, getStack, setStack);
final ScriptFunction getLineNumber = ScriptFunctionImpl.makeFunction("getLineNumber", NativeError.GET_LINENUMBER);
final ScriptFunction setLineNumber = ScriptFunctionImpl.makeFunction("setLineNumber", NativeError.SET_LINENUMBER);
errorProto.addOwnProperty("lineNumber", Attribute.NOT_ENUMERABLE, getLineNumber, setLineNumber);
final ScriptFunction getColumnNumber = ScriptFunctionImpl.makeFunction("getColumnNumber", NativeError.GET_COLUMNNUMBER);
final ScriptFunction setColumnNumber = ScriptFunctionImpl.makeFunction("setColumnNumber", NativeError.SET_COLUMNNUMBER);
errorProto.addOwnProperty("columnNumber", Attribute.NOT_ENUMERABLE, getColumnNumber, setColumnNumber);
final ScriptFunction getFileName = ScriptFunctionImpl.makeFunction("getFileName", NativeError.GET_FILENAME);
final ScriptFunction setFileName = ScriptFunctionImpl.makeFunction("setFileName", NativeError.SET_FILENAME);
errorProto.addOwnProperty("fileName", Attribute.NOT_ENUMERABLE, getFileName, setFileName);
// ECMA 15.11.4.2 Error.prototype.name
// Error.prototype.name = "Error";
errorProto.set(NativeError.NAME, "Error", 0);
// ECMA 15.11.4.3 Error.prototype.message
// Error.prototype.message = "";
errorProto.set(NativeError.MESSAGE, "", 0);
tagBuiltinProperties("Error", builtinError);
this.builtinEvalError = initErrorSubtype("EvalError", errorProto);
this.builtinRangeError = initErrorSubtype("RangeError", errorProto);
this.builtinReferenceError = initErrorSubtype("ReferenceError", errorProto);
this.builtinSyntaxError = initErrorSubtype("SyntaxError", errorProto);
this.builtinTypeError = initErrorSubtype("TypeError", errorProto);
this.builtinURIError = initErrorSubtype("URIError", errorProto);
}
示例4: initErrorSubtype
import jdk.nashorn.internal.runtime.ScriptObject; //导入方法依赖的package包/类
private ScriptFunction initErrorSubtype(final String name, final ScriptObject errorProto) {
final ScriptFunction cons = initConstructor(name, ScriptFunction.class);
final ScriptObject prototype = ScriptFunction.getPrototype(cons);
prototype.set(NativeError.NAME, name, 0);
prototype.set(NativeError.MESSAGE, "", 0);
prototype.setInitialProto(errorProto);
tagBuiltinProperties(name, cons);
return cons;
}
示例5: copyOptions
import jdk.nashorn.internal.runtime.ScriptObject; //导入方法依赖的package包/类
private static void copyOptions(final ScriptObject options, final ScriptEnvironment scriptEnv) {
for (final Field f : scriptEnv.getClass().getFields()) {
try {
options.set(f.getName(), f.get(scriptEnv), 0);
} catch (final IllegalArgumentException | IllegalAccessException exp) {
throw new RuntimeException(exp);
}
}
}
示例6: pop
import jdk.nashorn.internal.runtime.ScriptObject; //导入方法依赖的package包/类
/**
* ECMA 15.4.4.6 Array.prototype.pop ()
*
* @param self self reference
* @return array after pop
*/
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static Object pop(final Object self) {
try {
final ScriptObject sobj = (ScriptObject)self;
if (bulkable(sobj)) {
return sobj.getArray().pop();
}
final long len = JSType.toUint32(sobj.getLength());
if (len == 0) {
sobj.set("length", 0, CALLSITE_STRICT);
return ScriptRuntime.UNDEFINED;
}
final long index = len - 1;
final Object element = sobj.get(index);
sobj.delete(index, true);
sobj.set("length", index, CALLSITE_STRICT);
return element;
} catch (final ClassCastException | NullPointerException e) {
throw typeError("not.an.object", ScriptRuntime.safeToString(self));
}
}
示例7: reverse
import jdk.nashorn.internal.runtime.ScriptObject; //导入方法依赖的package包/类
/**
* ECMA 15.4.4.8 Array.prototype.reverse ()
*
* @param self self reference
* @return reversed array
*/
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static Object reverse(final Object self) {
try {
final ScriptObject sobj = (ScriptObject)self;
final long len = JSType.toUint32(sobj.getLength());
final long middle = len / 2;
for (long lower = 0; lower != middle; lower++) {
final long upper = len - lower - 1;
final Object lowerValue = sobj.get(lower);
final Object upperValue = sobj.get(upper);
final boolean lowerExists = sobj.has(lower);
final boolean upperExists = sobj.has(upper);
if (lowerExists && upperExists) {
sobj.set(lower, upperValue, CALLSITE_STRICT);
sobj.set(upper, lowerValue, CALLSITE_STRICT);
} else if (!lowerExists && upperExists) {
sobj.set(lower, upperValue, CALLSITE_STRICT);
sobj.delete(upper, true);
} else if (lowerExists && !upperExists) {
sobj.delete(lower, true);
sobj.set(upper, lowerValue, CALLSITE_STRICT);
}
}
return sobj;
} catch (final ClassCastException | NullPointerException e) {
throw typeError("not.an.object", ScriptRuntime.safeToString(self));
}
}
示例8: initErrorObjects
import jdk.nashorn.internal.runtime.ScriptObject; //导入方法依赖的package包/类
private void initErrorObjects() {
// Error objects
this.builtinError = initConstructor("Error", ScriptFunction.class);
final ScriptObject errorProto = getErrorPrototype();
// Nashorn specific accessors on Error.prototype - stack, lineNumber, columnNumber and fileName
final ScriptFunction getStack = ScriptFunction.createBuiltin("getStack", NativeError.GET_STACK);
final ScriptFunction setStack = ScriptFunction.createBuiltin("setStack", NativeError.SET_STACK);
errorProto.addOwnProperty("stack", Attribute.NOT_ENUMERABLE, getStack, setStack);
final ScriptFunction getLineNumber = ScriptFunction.createBuiltin("getLineNumber", NativeError.GET_LINENUMBER);
final ScriptFunction setLineNumber = ScriptFunction.createBuiltin("setLineNumber", NativeError.SET_LINENUMBER);
errorProto.addOwnProperty("lineNumber", Attribute.NOT_ENUMERABLE, getLineNumber, setLineNumber);
final ScriptFunction getColumnNumber = ScriptFunction.createBuiltin("getColumnNumber", NativeError.GET_COLUMNNUMBER);
final ScriptFunction setColumnNumber = ScriptFunction.createBuiltin("setColumnNumber", NativeError.SET_COLUMNNUMBER);
errorProto.addOwnProperty("columnNumber", Attribute.NOT_ENUMERABLE, getColumnNumber, setColumnNumber);
final ScriptFunction getFileName = ScriptFunction.createBuiltin("getFileName", NativeError.GET_FILENAME);
final ScriptFunction setFileName = ScriptFunction.createBuiltin("setFileName", NativeError.SET_FILENAME);
errorProto.addOwnProperty("fileName", Attribute.NOT_ENUMERABLE, getFileName, setFileName);
// ECMA 15.11.4.2 Error.prototype.name
// Error.prototype.name = "Error";
errorProto.set(NativeError.NAME, "Error", 0);
// ECMA 15.11.4.3 Error.prototype.message
// Error.prototype.message = "";
errorProto.set(NativeError.MESSAGE, "", 0);
tagBuiltinProperties("Error", builtinError);
this.builtinReferenceError = initErrorSubtype("ReferenceError", errorProto);
this.builtinSyntaxError = initErrorSubtype("SyntaxError", errorProto);
this.builtinTypeError = initErrorSubtype("TypeError", errorProto);
}
示例9: defineLength
import jdk.nashorn.internal.runtime.ScriptObject; //导入方法依赖的package包/类
private boolean defineLength(final long oldLen, final PropertyDescriptor oldLenDesc, final PropertyDescriptor desc, final boolean reject) {
// Step 3a
if (!desc.has(VALUE)) {
return super.defineOwnProperty("length", desc, reject);
}
// Step 3b
final PropertyDescriptor newLenDesc = desc;
// Step 3c and 3d - get new length and convert to long
final long newLen = NativeArray.validLength(newLenDesc.getValue(), true);
// Step 3e
newLenDesc.setValue(newLen);
// Step 3f
// increasing array length - just need to set new length value (and attributes if any) and return
if (newLen >= oldLen) {
return super.defineOwnProperty("length", newLenDesc, reject);
}
// Step 3g
if (!oldLenDesc.isWritable()) {
if (reject) {
throw typeError("property.not.writable", "length", ScriptRuntime.safeToString(this));
}
return false;
}
// Step 3h and 3i
final boolean newWritable = !newLenDesc.has(WRITABLE) || newLenDesc.isWritable();
if (!newWritable) {
newLenDesc.setWritable(true);
}
// Step 3j and 3k
final boolean succeeded = super.defineOwnProperty("length", newLenDesc, reject);
if (!succeeded) {
return false;
}
// Step 3l
// make sure that length is set till the point we can delete the old elements
long o = oldLen;
while (newLen < o) {
o--;
final boolean deleteSucceeded = delete(o, false);
if (!deleteSucceeded) {
newLenDesc.setValue(o + 1);
if (!newWritable) {
newLenDesc.setWritable(false);
}
super.defineOwnProperty("length", newLenDesc, false);
if (reject) {
throw typeError("property.not.writable", "length", ScriptRuntime.safeToString(this));
}
return false;
}
}
// Step 3m
if (!newWritable) {
// make 'length' property not writable
final ScriptObject newDesc = Global.newEmptyInstance();
newDesc.set(WRITABLE, false, 0);
return super.defineOwnProperty("length", newDesc, false);
}
return true;
}
示例10: shift
import jdk.nashorn.internal.runtime.ScriptObject; //导入方法依赖的package包/类
/**
* ECMA 15.4.4.9 Array.prototype.shift ()
*
* @param self self reference
* @return shifted array
*/
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static Object shift(final Object self) {
final Object obj = Global.toObject(self);
Object first = ScriptRuntime.UNDEFINED;
if (!(obj instanceof ScriptObject)) {
return first;
}
final ScriptObject sobj = (ScriptObject) obj;
long len = JSType.toUint32(sobj.getLength());
if (len > 0) {
first = sobj.get(0);
if (bulkable(sobj)) {
sobj.getArray().shiftLeft(1);
} else {
boolean hasPrevious = true;
for (long k = 1; k < len; k++) {
final boolean hasCurrent = sobj.has(k);
if (hasCurrent) {
sobj.set(k - 1, sobj.get(k), CALLSITE_STRICT);
} else if (hasPrevious) {
sobj.delete(k - 1, true);
}
hasPrevious = hasCurrent;
}
}
sobj.delete(--len, true);
} else {
len = 0;
}
sobj.set("length", len, CALLSITE_STRICT);
return first;
}
示例11: unshift
import jdk.nashorn.internal.runtime.ScriptObject; //导入方法依赖的package包/类
/**
* ECMA 15.4.4.13 Array.prototype.unshift ( [ item1 [ , item2 [ , ... ] ] ] )
*
* @param self self reference
* @param items items for unshift
* @return unshifted array
*/
@Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1)
public static Object unshift(final Object self, final Object... items) {
final Object obj = Global.toObject(self);
if (!(obj instanceof ScriptObject)) {
return ScriptRuntime.UNDEFINED;
}
final ScriptObject sobj = (ScriptObject)obj;
final long len = JSType.toUint32(sobj.getLength());
if (items == null) {
return ScriptRuntime.UNDEFINED;
}
if (bulkable(sobj)) {
sobj.getArray().shiftRight(items.length);
for (int j = 0; j < items.length; j++) {
sobj.setArray(sobj.getArray().set(j, items[j], true));
}
} else {
for (long k = len; k > 0; k--) {
final long from = k - 1;
final long to = k + items.length - 1;
if (sobj.has(from)) {
final Object fromValue = sobj.get(from);
sobj.set(to, fromValue, CALLSITE_STRICT);
} else {
sobj.delete(to, true);
}
}
for (int j = 0; j < items.length; j++) {
sobj.set(j, items[j], CALLSITE_STRICT);
}
}
final long newLength = len + items.length;
sobj.set("length", newLength, CALLSITE_STRICT);
return newLength;
}
示例12: defineLength
import jdk.nashorn.internal.runtime.ScriptObject; //导入方法依赖的package包/类
private boolean defineLength(final long oldLen, final PropertyDescriptor oldLenDesc, final PropertyDescriptor desc, final boolean reject) {
// Step 3a
if (!desc.has(VALUE)) {
return super.defineOwnProperty("length", desc, reject);
}
// Step 3b
final PropertyDescriptor newLenDesc = desc;
// Step 3c and 3d - get new length and convert to long
final long newLen = NativeArray.validLength(newLenDesc.getValue());
// Step 3e - note that we need to convert to int or double as long is not considered a JS number type anymore
newLenDesc.setValue(JSType.toNarrowestNumber(newLen));
// Step 3f
// increasing array length - just need to set new length value (and attributes if any) and return
if (newLen >= oldLen) {
return super.defineOwnProperty("length", newLenDesc, reject);
}
// Step 3g
if (!oldLenDesc.isWritable()) {
if (reject) {
throw typeError("property.not.writable", "length", ScriptRuntime.safeToString(this));
}
return false;
}
// Step 3h and 3i
final boolean newWritable = !newLenDesc.has(WRITABLE) || newLenDesc.isWritable();
if (!newWritable) {
newLenDesc.setWritable(true);
}
// Step 3j and 3k
final boolean succeeded = super.defineOwnProperty("length", newLenDesc, reject);
if (!succeeded) {
return false;
}
// Step 3l
// make sure that length is set till the point we can delete the old elements
long o = oldLen;
while (newLen < o) {
o--;
final boolean deleteSucceeded = delete(o, false);
if (!deleteSucceeded) {
newLenDesc.setValue(o + 1);
if (!newWritable) {
newLenDesc.setWritable(false);
}
super.defineOwnProperty("length", newLenDesc, false);
if (reject) {
throw typeError("property.not.writable", "length", ScriptRuntime.safeToString(this));
}
return false;
}
}
// Step 3m
if (!newWritable) {
// make 'length' property not writable
final ScriptObject newDesc = Global.newEmptyInstance();
newDesc.set(WRITABLE, false, 0);
return super.defineOwnProperty("length", newDesc, false);
}
return true;
}
示例13: unshift
import jdk.nashorn.internal.runtime.ScriptObject; //导入方法依赖的package包/类
/**
* ECMA 15.4.4.13 Array.prototype.unshift ( [ item1 [ , item2 [ , ... ] ] ] )
*
* @param self self reference
* @param items items for unshift
* @return unshifted array
*/
@Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1)
public static Object unshift(final Object self, final Object... items) {
final Object obj = Global.toObject(self);
if (!(obj instanceof ScriptObject)) {
return ScriptRuntime.UNDEFINED;
}
final ScriptObject sobj = (ScriptObject)obj;
final long len = JSType.toUint32(sobj.getLength());
if (items == null) {
return ScriptRuntime.UNDEFINED;
}
if (bulkable(sobj)) {
sobj.getArray().shiftRight(items.length);
for (int j = 0; j < items.length; j++) {
sobj.setArray(sobj.getArray().set(j, items[j], true));
}
} else {
for (long k = len; k > 0; k--) {
final long from = k - 1;
final long to = k + items.length - 1;
if (sobj.has(from)) {
final Object fromValue = sobj.get(from);
sobj.set(to, fromValue, CALLSITE_STRICT);
} else {
sobj.delete(to, true);
}
}
for (int j = 0; j < items.length; j++) {
sobj.set(j, items[j], CALLSITE_STRICT);
}
}
final long newLength = len + items.length;
sobj.set("length", newLength, CALLSITE_STRICT);
return JSType.toNarrowestNumber(newLength);
}