@Override public Object call(ExecutionContext context, Object self, Object... args) { Arguments argsObj = (Arguments) context.resolve("arguments").getValue(context); int numArgs = (int) argsObj.get(context, "length"); if (self != Types.UNDEFINED && self != Types.NULL) { // Constructor String value = ""; if (numArgs != 0) { value = Types.toString(context, args[0]); } PrimitiveDynObject primSelf = (PrimitiveDynObject) self; primSelf.setPrimitiveValue(value); return primSelf; } else { // As function if (numArgs == 0) { return ""; } return Types.toString(context, args[0]); } }
@Override public Object call(ExecutionContext context, Object self, Object... args) { Number number = 0L; Arguments argsObj = (Arguments) context.resolve("arguments").getValue(context); int numArgs = (int) argsObj.get(context, "length"); if (numArgs != 0) { number = Types.toNumber(context, args[0]); } if (self == Types.UNDEFINED || self == Types.NULL) { // called as a function return number; } else { // called as a ctor PrimitiveDynObject numberObject = (PrimitiveDynObject) self; if (numArgs == 0) { number = 0L; } numberObject.setPrimitiveValue(number); return numberObject; } }
@Test public void testConstructorWithExponentialNumber() { PrimitiveDynObject o = (PrimitiveDynObject) eval("new String(.00000012345);"); assertThat(o.getPrimitiveValue()).isEqualTo("1.2345e-7"); }
@Test public void testConstructorWithNonString() { PrimitiveDynObject o = (PrimitiveDynObject) eval("new String(42)"); assertThat(o.getClassName()).isEqualTo("String"); assertThat(o.getPrimitiveValue()).isEqualTo("42"); }