package com.oracle.truffle.js.builtins;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.js.builtins.BigIntFunctionBuiltinsFactory.JSBigIntAsIntNNodeGen;
import com.oracle.truffle.js.builtins.BigIntFunctionBuiltinsFactory.JSBigIntAsUintNNodeGen;
import com.oracle.truffle.js.nodes.cast.JSToBigIntNode;
import com.oracle.truffle.js.nodes.cast.JSToIndexNode;
import com.oracle.truffle.js.nodes.function.JSBuiltin;
import com.oracle.truffle.js.nodes.function.JSBuiltinNode;
import com.oracle.truffle.js.runtime.BigInt;
import com.oracle.truffle.js.runtime.Errors;
import com.oracle.truffle.js.runtime.JSContext;
import com.oracle.truffle.js.runtime.JSRuntime;
import com.oracle.truffle.js.runtime.builtins.BuiltinEnum;
import com.oracle.truffle.js.runtime.builtins.JSBigInt;
public final class BigIntFunctionBuiltins extends JSBuiltinsContainer.SwitchEnum<BigIntFunctionBuiltins.BigIntFunction> {
public static final JSBuiltinsContainer BUILTINS = new BigIntFunctionBuiltins();
protected BigIntFunctionBuiltins() {
super(JSBigInt.CLASS_NAME, BigIntFunction.class);
}
public enum BigIntFunction implements BuiltinEnum<BigIntFunction> {
asUintN(2),
asIntN(2);
private final int length;
BigIntFunction(int length) {
this.length = length;
}
@Override
public int getLength() {
return length;
}
@Override
public int getECMAScriptVersion() {
return 9;
}
}
@Override
protected Object createNode(JSContext context, JSBuiltin builtin, boolean construct, boolean newTarget, BigIntFunction builtinEnum) {
switch (builtinEnum) {
case asUintN:
return JSBigIntAsUintNNodeGen.create(context, builtin, args().fixedArgs(2).createArgumentNodes(context));
case asIntN:
return JSBigIntAsIntNNodeGen.create(context, builtin, args().fixedArgs(2).createArgumentNodes(context));
}
return null;
}
public abstract static class JSBigIntAsUintNNode extends JSBuiltinNode {
public JSBigIntAsUintNNode(JSContext context, JSBuiltin builtin) {
super(context, builtin);
}
@Specialization
protected BigInt doIt(Object bitsObj, Object bigIntObj,
@Cached("create()") JSToIndexNode toIndexNode,
@Cached("create()") JSToBigIntNode toBigIntNode) {
long bits = toIndexNode.executeLong(bitsObj);
BigInt bigint = toBigIntNode.executeBigInteger(bigIntObj);
if (bits > JSRuntime.MAX_BIG_INT_EXPONENT) {
if (bigint.signum() >= 0) {
return bigint;
} else {
throw Errors.createRangeErrorBigIntMaxSizeExceeded();
}
} else {
return bigint.mod((BigInt.TWO).pow((int) bits));
}
}
}
public abstract static class JSBigIntAsIntNNode extends JSBuiltinNode {
public JSBigIntAsIntNNode(JSContext context, JSBuiltin builtin) {
super(context, builtin);
}
@Specialization
protected BigInt doIt(Object bitsObj, Object bigIntObj,
@Cached("create()") JSToIndexNode toIndexNode,
@Cached("create()") JSToBigIntNode toBigIntNode) {
long bits = toIndexNode.executeLong(bitsObj);
BigInt bigint = toBigIntNode.executeBigInteger(bigIntObj);
if (bits > JSRuntime.MAX_BIG_INT_EXPONENT) {
return bigint;
}
BigInt twoPowBits = BigInt.TWO.pow((int) bits);
BigInt mod = bigint.mod(twoPowBits);
if (bits > 0) {
if (mod.compareTo(BigInt.TWO.pow((int) bits - 1)) >= 0) {
return mod.subtract(twoPowBits);
} else {
return mod;
}
} else {
return BigInt.ZERO;
}
}
}
}