java arrayindex_Java ArrayIndex.isValidArrayIndex方法代碼示例

import jdk.nashorn.internal.runtime.arrays.ArrayIndex; //導入方法依賴的package包/類

/**

* ECMA 15.4.5.1 [[DefineOwnProperty]] ( P, Desc, Throw )

*/

@Override

public boolean defineOwnProperty(final Object key, final Object propertyDesc, final boolean reject) {

final PropertyDescriptor desc = toPropertyDescriptor(Global.instance(), propertyDesc);

// never be undefined as "length" is always defined and can't be deleted for arrays

// Step 1

final PropertyDescriptor oldLenDesc = (PropertyDescriptor) super.getOwnPropertyDescriptor("length");

// Step 2

// get old length and convert to long. Always a Long/Uint32 but we take the safe road.

final long oldLen = JSType.toUint32(oldLenDesc.getValue());

// Step 3

if ("length".equals(key)) {

// check for length being made non-writable

final boolean result = defineLength(oldLen, oldLenDesc, desc, reject);

if (desc.has(WRITABLE) && !desc.isWritable()) {

setIsLengthNotWritable();

}

return result;

}

// Step 4a

final int index = ArrayIndex.getArrayIndex(key);

if (ArrayIndex.isValidArrayIndex(index)) {

final long longIndex = ArrayIndex.toLongIndex(index);

// Step 4b

// setting an element beyond current length, but 'length' is not writable

if (longIndex >= oldLen && !oldLenDesc.isWritable()) {

if (reject) {

throw typeError("property.not.writable", Long.toString(longIndex), ScriptRuntime.safeToString(this));

}

return false;

}

// Step 4c

// set the new array element

final boolean succeeded = super.defineOwnProperty(key, desc, false);

// Step 4d

if (!succeeded) {

if (reject) {

throw typeError("cant.redefine.property", key.toString(), ScriptRuntime.safeToString(this));

}

return false;

}

// Step 4e -- adjust new length based on new element index that is set

if (longIndex >= oldLen) {

oldLenDesc.setValue(longIndex + 1);

super.defineOwnProperty("length", oldLenDesc, false);

}

// Step 4f

return true;

}

// not an index property

return super.defineOwnProperty(key, desc, reject);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值