Java Code Examples for jdk.nashorn.internal.objects.annotations.Constructor

The following examples show how to use jdk.nashorn.internal.objects.annotations.Constructor. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source Project: TencentKona-8   Author: Tencent   File: NativeFunction.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.3.2.1 new Function (p1, p2, ... , pn, body)
 *
 * Constructor
 *
 * @param newObj is the new operator used for constructing this function
 * @param self   self reference
 * @param args   arguments
 * @return new NativeFunction
 */
@Constructor(arity = 1)
public static ScriptFunction function(final boolean newObj, final Object self, final Object... args) {
    final StringBuilder sb = new StringBuilder();

    sb.append("(function (");
    final String funcBody;
    if (args.length > 0) {
        final StringBuilder paramListBuf = new StringBuilder();
        for (int i = 0; i < args.length - 1; i++) {
            paramListBuf.append(JSType.toString(args[i]));
            if (i < args.length - 2) {
                paramListBuf.append(",");
            }
        }

        // now convert function body to a string
        funcBody = JSType.toString(args[args.length - 1]);

        final String paramList = paramListBuf.toString();
        if (!paramList.isEmpty()) {
            checkFunctionParameters(paramList);
            sb.append(paramList);
        }
    } else {
        funcBody = null;
    }

    sb.append(") {\n");
    if (args.length > 0) {
        checkFunctionBody(funcBody);
        sb.append(funcBody);
        sb.append('\n');
    }
    sb.append("})");

    final Global global = Global.instance();
    final Context context = global.getContext();
    return (ScriptFunction)context.eval(global, sb.toString(), global, "<function>");
}
 
Example #2
Source Project: TencentKona-8   Author: Tencent   File: NativeArrayBuffer.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructor
 * @param newObj is this invoked with new
 * @param self   self reference
 * @param args   arguments to constructor
 * @return new NativeArrayBuffer
 */
@Constructor(arity = 1)
public static NativeArrayBuffer constructor(final boolean newObj, final Object self, final Object... args) {
    if (!newObj) {
        throw typeError("constructor.requires.new", "ArrayBuffer");
    }

    if (args.length == 0) {
        return new NativeArrayBuffer(0);
    }

    return new NativeArrayBuffer(JSType.toInt32(args[0]));
}
 
Example #3
Source Project: TencentKona-8   Author: Tencent   File: NativeBoolean.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.6.2.1 new Boolean (value)
 *
 * @param newObj is the new operator used to instantiate this NativeBoolean
 * @param self   self reference
 * @param value  value of boolean
 * @return the new NativeBoolean
 */
@Constructor(arity = 1)
public static Object constructor(final boolean newObj, final Object self, final Object value) {
    final boolean flag = JSType.toBoolean(value);

    if (newObj) {
        return new NativeBoolean(flag);
    }

    return flag;
}
 
Example #4
Source Project: TencentKona-8   Author: Tencent   File: NativeDate.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructor - ECMA 15.9.3.1 new Date (year, month [, date [, hours [, minutes [, seconds [, ms ] ] ] ] ] )
 *
 * @param isNew is this Date constructed with the new operator
 * @param self  self reference
 * @param args  arguments
 * @return new Date
 */
@Constructor(arity = 7)
public static Object construct(final boolean isNew, final Object self, final Object... args) {
    if (! isNew) {
        return toStringImpl(new NativeDate(), FORMAT_DATE_TIME);
    }

    NativeDate result;
    switch (args.length) {
    case 0:
        result = new NativeDate();
        break;

    case 1:
        double num;
        final Object arg = JSType.toPrimitive(args[0]);
        if (JSType.isString(arg)) {
            num = parseDateString(arg.toString());
        } else {
            num = timeClip(JSType.toNumber(args[0]));
        }
        result = new NativeDate(num);
        break;

    default:
        result = new NativeDate(0);
        final double[] d = convertCtorArgs(args);
        if (d == null) {
            result.setTime(Double.NaN);
        } else {
            final double time = timeClip(utc(makeDate(d), result.getTimeZone()));
            result.setTime(time);
        }
        break;
     }

     return result;
}
 
Example #5
Source Project: TencentKona-8   Author: Tencent   File: NativeObject.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.2.2.1 , 15.2.1.1 new Object([value]) and Object([value])
 *
 * Constructor
 *
 * @param newObj is the new object instantiated with the new operator
 * @param self   self reference
 * @param value  value of object to be instantiated
 * @return the new NativeObject
 */
@Constructor
public static Object construct(final boolean newObj, final Object self, final Object value) {
    final JSType type = JSType.ofNoFunction(value);

    // Object(null), Object(undefined), Object() are same as "new Object()"

    if (newObj || type == JSType.NULL || type == JSType.UNDEFINED) {
        switch (type) {
        case BOOLEAN:
        case NUMBER:
        case STRING:
            return Global.toObject(value);
        case OBJECT:
            return value;
        case NULL:
        case UNDEFINED:
            // fall through..
        default:
            break;
        }

        return Global.newEmptyInstance();
    }

    return Global.toObject(value);
}
 
Example #6
Source Project: TencentKona-8   Author: Tencent   File: NativeRegExp.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.10.4
 *
 * Constructor
 *
 * @param isNew is the new operator used for instantiating this regexp
 * @param self  self reference
 * @param args  arguments (optional: pattern and flags)
 * @return new NativeRegExp
 */
@Constructor(arity = 2)
public static NativeRegExp constructor(final boolean isNew, final Object self, final Object... args) {
    if (args.length > 1) {
        return newRegExp(args[0], args[1]);
    } else if (args.length > 0) {
        return newRegExp(args[0], UNDEFINED);
    }

    return newRegExp(UNDEFINED, UNDEFINED);
}
 
Example #7
Source Project: jdk8u60   Author: chenghanpeng   File: NativeFunction.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.3.2.1 new Function (p1, p2, ... , pn, body)
 *
 * Constructor
 *
 * @param newObj is the new operator used for constructing this function
 * @param self   self reference
 * @param args   arguments
 * @return new NativeFunction
 */
@Constructor(arity = 1)
public static ScriptFunction function(final boolean newObj, final Object self, final Object... args) {
    final StringBuilder sb = new StringBuilder();

    sb.append("(function (");
    final String funcBody;
    if (args.length > 0) {
        final StringBuilder paramListBuf = new StringBuilder();
        for (int i = 0; i < args.length - 1; i++) {
            paramListBuf.append(JSType.toString(args[i]));
            if (i < args.length - 2) {
                paramListBuf.append(",");
            }
        }

        // now convert function body to a string
        funcBody = JSType.toString(args[args.length - 1]);

        final String paramList = paramListBuf.toString();
        if (!paramList.isEmpty()) {
            checkFunctionParameters(paramList);
            sb.append(paramList);
        }
    } else {
        funcBody = null;
    }

    sb.append(") {\n");
    if (args.length > 0) {
        checkFunctionBody(funcBody);
        sb.append(funcBody);
        sb.append('\n');
    }
    sb.append("})");

    final Global global = Global.instance();
    final Context context = global.getContext();
    return (ScriptFunction)context.eval(global, sb.toString(), global, "<function>");
}
 
Example #8
Source Project: jdk8u60   Author: chenghanpeng   File: NativeArrayBuffer.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructor
 * @param newObj is this invoked with new
 * @param self   self reference
 * @param args   arguments to constructor
 * @return new NativeArrayBuffer
 */
@Constructor(arity = 1)
public static NativeArrayBuffer constructor(final boolean newObj, final Object self, final Object... args) {
    if (!newObj) {
        throw typeError("constructor.requires.new", "ArrayBuffer");
    }

    if (args.length == 0) {
        return new NativeArrayBuffer(0);
    }

    return new NativeArrayBuffer(JSType.toInt32(args[0]));
}
 
Example #9
Source Project: jdk8u60   Author: chenghanpeng   File: NativeBoolean.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.6.2.1 new Boolean (value)
 *
 * @param newObj is the new operator used to instantiate this NativeBoolean
 * @param self   self reference
 * @param value  value of boolean
 * @return the new NativeBoolean
 */
@Constructor(arity = 1)
public static Object constructor(final boolean newObj, final Object self, final Object value) {
    final boolean flag = JSType.toBoolean(value);

    if (newObj) {
        return new NativeBoolean(flag);
    }

    return flag;
}
 
Example #10
Source Project: jdk8u60   Author: chenghanpeng   File: NativeDate.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructor - ECMA 15.9.3.1 new Date (year, month [, date [, hours [, minutes [, seconds [, ms ] ] ] ] ] )
 *
 * @param isNew is this Date constructed with the new operator
 * @param self  self reference
 * @param args  arguments
 * @return new Date
 */
@Constructor(arity = 7)
public static Object construct(final boolean isNew, final Object self, final Object... args) {
    if (! isNew) {
        return toStringImpl(new NativeDate(), FORMAT_DATE_TIME);
    }

    NativeDate result;
    switch (args.length) {
    case 0:
        result = new NativeDate();
        break;

    case 1:
        double num;
        final Object arg = JSType.toPrimitive(args[0]);
        if (JSType.isString(arg)) {
            num = parseDateString(arg.toString());
        } else {
            num = timeClip(JSType.toNumber(args[0]));
        }
        result = new NativeDate(num);
        break;

    default:
        result = new NativeDate(0);
        final double[] d = convertCtorArgs(args);
        if (d == null) {
            result.setTime(Double.NaN);
        } else {
            final double time = timeClip(utc(makeDate(d), result.getTimeZone()));
            result.setTime(time);
        }
        break;
     }

     return result;
}
 
Example #11
Source Project: jdk8u60   Author: chenghanpeng   File: NativeObject.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.2.2.1 , 15.2.1.1 new Object([value]) and Object([value])
 *
 * Constructor
 *
 * @param newObj is the new object instantiated with the new operator
 * @param self   self reference
 * @param value  value of object to be instantiated
 * @return the new NativeObject
 */
@Constructor
public static Object construct(final boolean newObj, final Object self, final Object value) {
    final JSType type = JSType.ofNoFunction(value);

    // Object(null), Object(undefined), Object() are same as "new Object()"

    if (newObj || type == JSType.NULL || type == JSType.UNDEFINED) {
        switch (type) {
        case BOOLEAN:
        case NUMBER:
        case STRING:
            return Global.toObject(value);
        case OBJECT:
            return value;
        case NULL:
        case UNDEFINED:
            // fall through..
        default:
            break;
        }

        return Global.newEmptyInstance();
    }

    return Global.toObject(value);
}
 
Example #12
Source Project: jdk8u60   Author: chenghanpeng   File: NativeRegExp.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.10.4
 *
 * Constructor
 *
 * @param isNew is the new operator used for instantiating this regexp
 * @param self  self reference
 * @param args  arguments (optional: pattern and flags)
 * @return new NativeRegExp
 */
@Constructor(arity = 2)
public static NativeRegExp constructor(final boolean isNew, final Object self, final Object... args) {
    if (args.length > 1) {
        return newRegExp(args[0], args[1]);
    } else if (args.length > 0) {
        return newRegExp(args[0], UNDEFINED);
    }

    return newRegExp(UNDEFINED, UNDEFINED);
}
 
Example #13
Source Project: openjdk-jdk8u   Author: AdoptOpenJDK   File: NativeFunction.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.3.2.1 new Function (p1, p2, ... , pn, body)
 *
 * Constructor
 *
 * @param newObj is the new operator used for constructing this function
 * @param self   self reference
 * @param args   arguments
 * @return new NativeFunction
 */
@Constructor(arity = 1)
public static ScriptFunction function(final boolean newObj, final Object self, final Object... args) {
    final StringBuilder sb = new StringBuilder();

    sb.append("(function (");
    final String funcBody;
    if (args.length > 0) {
        final StringBuilder paramListBuf = new StringBuilder();
        for (int i = 0; i < args.length - 1; i++) {
            paramListBuf.append(JSType.toString(args[i]));
            if (i < args.length - 2) {
                paramListBuf.append(",");
            }
        }

        // now convert function body to a string
        funcBody = JSType.toString(args[args.length - 1]);

        final String paramList = paramListBuf.toString();
        if (!paramList.isEmpty()) {
            checkFunctionParameters(paramList);
            sb.append(paramList);
        }
    } else {
        funcBody = null;
    }

    sb.append(") {\n");
    if (args.length > 0) {
        checkFunctionBody(funcBody);
        sb.append(funcBody);
        sb.append('\n');
    }
    sb.append("})");

    final Global global = Global.instance();
    final Context context = global.getContext();
    return (ScriptFunction)context.eval(global, sb.toString(), global, "<function>");
}
 
Example #14
Source Project: openjdk-jdk8u   Author: AdoptOpenJDK   File: NativeArrayBuffer.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructor
 * @param newObj is this invoked with new
 * @param self   self reference
 * @param args   arguments to constructor
 * @return new NativeArrayBuffer
 */
@Constructor(arity = 1)
public static NativeArrayBuffer constructor(final boolean newObj, final Object self, final Object... args) {
    if (!newObj) {
        throw typeError("constructor.requires.new", "ArrayBuffer");
    }

    if (args.length == 0) {
        return new NativeArrayBuffer(0);
    }

    return new NativeArrayBuffer(JSType.toInt32(args[0]));
}
 
Example #15
Source Project: openjdk-jdk8u   Author: AdoptOpenJDK   File: NativeBoolean.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.6.2.1 new Boolean (value)
 *
 * @param newObj is the new operator used to instantiate this NativeBoolean
 * @param self   self reference
 * @param value  value of boolean
 * @return the new NativeBoolean
 */
@Constructor(arity = 1)
public static Object constructor(final boolean newObj, final Object self, final Object value) {
    final boolean flag = JSType.toBoolean(value);

    if (newObj) {
        return new NativeBoolean(flag);
    }

    return flag;
}
 
Example #16
Source Project: openjdk-jdk8u   Author: AdoptOpenJDK   File: NativeDate.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructor - ECMA 15.9.3.1 new Date (year, month [, date [, hours [, minutes [, seconds [, ms ] ] ] ] ] )
 *
 * @param isNew is this Date constructed with the new operator
 * @param self  self reference
 * @param args  arguments
 * @return new Date
 */
@Constructor(arity = 7)
public static Object construct(final boolean isNew, final Object self, final Object... args) {
    if (! isNew) {
        return toStringImpl(new NativeDate(), FORMAT_DATE_TIME);
    }

    NativeDate result;
    switch (args.length) {
    case 0:
        result = new NativeDate();
        break;

    case 1:
        double num;
        final Object arg = JSType.toPrimitive(args[0]);
        if (JSType.isString(arg)) {
            num = parseDateString(arg.toString());
        } else {
            num = timeClip(JSType.toNumber(args[0]));
        }
        result = new NativeDate(num);
        break;

    default:
        result = new NativeDate(0);
        final double[] d = convertCtorArgs(args);
        if (d == null) {
            result.setTime(Double.NaN);
        } else {
            final double time = timeClip(utc(makeDate(d), result.getTimeZone()));
            result.setTime(time);
        }
        break;
     }

     return result;
}
 
Example #17
Source Project: openjdk-jdk8u   Author: AdoptOpenJDK   File: NativeObject.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.2.2.1 , 15.2.1.1 new Object([value]) and Object([value])
 *
 * Constructor
 *
 * @param newObj is the new object instantiated with the new operator
 * @param self   self reference
 * @param value  value of object to be instantiated
 * @return the new NativeObject
 */
@Constructor
public static Object construct(final boolean newObj, final Object self, final Object value) {
    final JSType type = JSType.ofNoFunction(value);

    // Object(null), Object(undefined), Object() are same as "new Object()"

    if (newObj || type == JSType.NULL || type == JSType.UNDEFINED) {
        switch (type) {
        case BOOLEAN:
        case NUMBER:
        case STRING:
            return Global.toObject(value);
        case OBJECT:
            return value;
        case NULL:
        case UNDEFINED:
            // fall through..
        default:
            break;
        }

        return Global.newEmptyInstance();
    }

    return Global.toObject(value);
}
 
Example #18
Source Project: openjdk-jdk8u   Author: AdoptOpenJDK   File: NativeRegExp.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.10.4
 *
 * Constructor
 *
 * @param isNew is the new operator used for instantiating this regexp
 * @param self  self reference
 * @param args  arguments (optional: pattern and flags)
 * @return new NativeRegExp
 */
@Constructor(arity = 2)
public static NativeRegExp constructor(final boolean isNew, final Object self, final Object... args) {
    if (args.length > 1) {
        return newRegExp(args[0], args[1]);
    } else if (args.length > 0) {
        return newRegExp(args[0], UNDEFINED);
    }

    return newRegExp(UNDEFINED, UNDEFINED);
}
 
Example #19
Source Project: TencentKona-8   Author: Tencent   File: NativeJSAdapter.java    License: GNU General Public License v2.0 4 votes vote down vote up
/**
 * Constructor
 *
 * @param isNew is this NativeJSAdapter instantiated with the new operator
 * @param self  self reference
 * @param args  arguments ([adaptee], [overrides, adaptee] or [proto, overrides, adaptee]
 * @return new NativeJSAdapter
 */
@Constructor
public static NativeJSAdapter construct(final boolean isNew, final Object self, final Object... args) {
    Object proto     = UNDEFINED;
    Object overrides = UNDEFINED;
    Object adaptee;

    if (args == null || args.length == 0) {
        throw typeError("not.an.object", "null");
    }

    switch (args.length) {
    case 1:
        adaptee = args[0];
        break;

    case 2:
        overrides = args[0];
        adaptee   = args[1];
        break;

    default:
        //fallthru
    case 3:
        proto = args[0];
        overrides = args[1];
        adaptee = args[2];
        break;
    }

    if (!(adaptee instanceof ScriptObject)) {
        throw typeError("not.an.object", ScriptRuntime.safeToString(adaptee));
    }

    final Global global = Global.instance();
    if (proto != null && !(proto instanceof ScriptObject)) {
        proto = global.getJSAdapterPrototype();
    }

    return new NativeJSAdapter(overrides, (ScriptObject)adaptee, (ScriptObject)proto, $nasgenmap$);
}
 
Example #20
Source Project: TencentKona-8   Author: Tencent   File: NativeDataView.java    License: GNU General Public License v2.0 4 votes vote down vote up
/**
 * Create a new DataView object using the passed ArrayBuffer for its
 * storage. Optional byteOffset and byteLength can be used to limit the
 * section of the buffer referenced. The byteOffset indicates the offset in
 * bytes from the start of the ArrayBuffer, and the byteLength is the number
 * of bytes from the offset that this DataView will reference. If both
 * byteOffset and byteLength are omitted, the DataView spans the entire
 * ArrayBuffer range. If the byteLength is omitted, the DataView extends from
 * the given byteOffset until the end of the ArrayBuffer.
 *
 * If the given byteOffset and byteLength references an area beyond the end
 * of the ArrayBuffer an exception is raised.

 * @param newObj if this constructor was invoked with 'new' or not
 * @param self   constructor function object
 * @param args   arguments to the constructor
 * @return newly constructed DataView object
 */
@Constructor(arity = 1)
public static NativeDataView constructor(final boolean newObj, final Object self, final Object... args) {
    if (args.length == 0 || !(args[0] instanceof NativeArrayBuffer)) {
        throw typeError("not.an.arraybuffer.in.dataview");
    }

    final NativeArrayBuffer arrBuf = (NativeArrayBuffer)args[0];
    switch (args.length) {
    case 1:
        return new NativeDataView(arrBuf);
    case 2:
        return new NativeDataView(arrBuf, JSType.toInt32(args[1]));
    default:
        return new NativeDataView(arrBuf, JSType.toInt32(args[1]), JSType.toInt32(args[2]));
    }
}
 
Example #21
Source Project: jdk8u60   Author: chenghanpeng   File: NativeJSAdapter.java    License: GNU General Public License v2.0 4 votes vote down vote up
/**
 * Constructor
 *
 * @param isNew is this NativeJSAdapter instantiated with the new operator
 * @param self  self reference
 * @param args  arguments ([adaptee], [overrides, adaptee] or [proto, overrides, adaptee]
 * @return new NativeJSAdapter
 */
@Constructor
public static NativeJSAdapter construct(final boolean isNew, final Object self, final Object... args) {
    Object proto     = UNDEFINED;
    Object overrides = UNDEFINED;
    Object adaptee;

    if (args == null || args.length == 0) {
        throw typeError("not.an.object", "null");
    }

    switch (args.length) {
    case 1:
        adaptee = args[0];
        break;

    case 2:
        overrides = args[0];
        adaptee   = args[1];
        break;

    default:
        //fallthru
    case 3:
        proto = args[0];
        overrides = args[1];
        adaptee = args[2];
        break;
    }

    if (!(adaptee instanceof ScriptObject)) {
        throw typeError("not.an.object", ScriptRuntime.safeToString(adaptee));
    }

    final Global global = Global.instance();
    if (proto != null && !(proto instanceof ScriptObject)) {
        proto = global.getJSAdapterPrototype();
    }

    return new NativeJSAdapter(overrides, (ScriptObject)adaptee, (ScriptObject)proto, $nasgenmap$);
}
 
Example #22
Source Project: jdk8u60   Author: chenghanpeng   File: NativeDataView.java    License: GNU General Public License v2.0 4 votes vote down vote up
/**
 * Create a new DataView object using the passed ArrayBuffer for its
 * storage. Optional byteOffset and byteLength can be used to limit the
 * section of the buffer referenced. The byteOffset indicates the offset in
 * bytes from the start of the ArrayBuffer, and the byteLength is the number
 * of bytes from the offset that this DataView will reference. If both
 * byteOffset and byteLength are omitted, the DataView spans the entire
 * ArrayBuffer range. If the byteLength is omitted, the DataView extends from
 * the given byteOffset until the end of the ArrayBuffer.
 *
 * If the given byteOffset and byteLength references an area beyond the end
 * of the ArrayBuffer an exception is raised.

 * @param newObj if this constructor was invoked with 'new' or not
 * @param self   constructor function object
 * @param args   arguments to the constructor
 * @return newly constructed DataView object
 */
@Constructor(arity = 1)
public static NativeDataView constructor(final boolean newObj, final Object self, final Object... args) {
    if (args.length == 0 || !(args[0] instanceof NativeArrayBuffer)) {
        throw typeError("not.an.arraybuffer.in.dataview");
    }

    final NativeArrayBuffer arrBuf = (NativeArrayBuffer)args[0];
    switch (args.length) {
    case 1:
        return new NativeDataView(arrBuf);
    case 2:
        return new NativeDataView(arrBuf, JSType.toInt32(args[1]));
    default:
        return new NativeDataView(arrBuf, JSType.toInt32(args[1]), JSType.toInt32(args[2]));
    }
}
 
Example #23
Source Project: openjdk-jdk8u   Author: AdoptOpenJDK   File: NativeJSAdapter.java    License: GNU General Public License v2.0 4 votes vote down vote up
/**
 * Constructor
 *
 * @param isNew is this NativeJSAdapter instantiated with the new operator
 * @param self  self reference
 * @param args  arguments ([adaptee], [overrides, adaptee] or [proto, overrides, adaptee]
 * @return new NativeJSAdapter
 */
@Constructor
public static NativeJSAdapter construct(final boolean isNew, final Object self, final Object... args) {
    Object proto     = UNDEFINED;
    Object overrides = UNDEFINED;
    Object adaptee;

    if (args == null || args.length == 0) {
        throw typeError("not.an.object", "null");
    }

    switch (args.length) {
    case 1:
        adaptee = args[0];
        break;

    case 2:
        overrides = args[0];
        adaptee   = args[1];
        break;

    default:
        //fallthru
    case 3:
        proto = args[0];
        overrides = args[1];
        adaptee = args[2];
        break;
    }

    if (!(adaptee instanceof ScriptObject)) {
        throw typeError("not.an.object", ScriptRuntime.safeToString(adaptee));
    }

    final Global global = Global.instance();
    if (proto != null && !(proto instanceof ScriptObject)) {
        proto = global.getJSAdapterPrototype();
    }

    return new NativeJSAdapter(overrides, (ScriptObject)adaptee, (ScriptObject)proto, $nasgenmap$);
}
 
Example #24
Source Project: openjdk-jdk8u   Author: AdoptOpenJDK   File: NativeDataView.java    License: GNU General Public License v2.0 4 votes vote down vote up
/**
 * Create a new DataView object using the passed ArrayBuffer for its
 * storage. Optional byteOffset and byteLength can be used to limit the
 * section of the buffer referenced. The byteOffset indicates the offset in
 * bytes from the start of the ArrayBuffer, and the byteLength is the number
 * of bytes from the offset that this DataView will reference. If both
 * byteOffset and byteLength are omitted, the DataView spans the entire
 * ArrayBuffer range. If the byteLength is omitted, the DataView extends from
 * the given byteOffset until the end of the ArrayBuffer.
 *
 * If the given byteOffset and byteLength references an area beyond the end
 * of the ArrayBuffer an exception is raised.

 * @param newObj if this constructor was invoked with 'new' or not
 * @param self   constructor function object
 * @param args   arguments to the constructor
 * @return newly constructed DataView object
 */
@Constructor(arity = 1)
public static NativeDataView constructor(final boolean newObj, final Object self, final Object... args) {
    if (args.length == 0 || !(args[0] instanceof NativeArrayBuffer)) {
        throw typeError("not.an.arraybuffer.in.dataview");
    }

    final NativeArrayBuffer arrBuf = (NativeArrayBuffer)args[0];
    switch (args.length) {
    case 1:
        return new NativeDataView(arrBuf);
    case 2:
        return new NativeDataView(arrBuf, JSType.toInt32(args[1]));
    default:
        return new NativeDataView(arrBuf, JSType.toInt32(args[1]), JSType.toInt32(args[2]));
    }
}
 
Example #25
Source Project: TencentKona-8   Author: Tencent   File: NativeNumber.java    License: GNU General Public License v2.0 3 votes vote down vote up
/**
 * ECMA 15.7.2 - The Number constructor
 *
 * @param newObj is this Number instantiated with the new operator
 * @param self   self reference
 * @param args   value of number
 * @return the Number instance (internally represented as a {@code NativeNumber})
 */
@Constructor(arity = 1)
public static Object constructor(final boolean newObj, final Object self, final Object... args) {
    final double num = (args.length > 0) ? JSType.toNumber(args[0]) : 0.0;

    return newObj? new NativeNumber(num) : num;
}
 
Example #26
Source Project: jdk8u60   Author: chenghanpeng   File: NativeNumber.java    License: GNU General Public License v2.0 3 votes vote down vote up
/**
 * ECMA 15.7.2 - The Number constructor
 *
 * @param newObj is this Number instantiated with the new operator
 * @param self   self reference
 * @param args   value of number
 * @return the Number instance (internally represented as a {@code NativeNumber})
 */
@Constructor(arity = 1)
public static Object constructor(final boolean newObj, final Object self, final Object... args) {
    final double num = (args.length > 0) ? JSType.toNumber(args[0]) : 0.0;

    return newObj? new NativeNumber(num) : num;
}
 
Example #27
Source Project: openjdk-jdk8u   Author: AdoptOpenJDK   File: NativeNumber.java    License: GNU General Public License v2.0 3 votes vote down vote up
/**
 * ECMA 15.7.2 - The Number constructor
 *
 * @param newObj is this Number instantiated with the new operator
 * @param self   self reference
 * @param args   value of number
 * @return the Number instance (internally represented as a {@code NativeNumber})
 */
@Constructor(arity = 1)
public static Object constructor(final boolean newObj, final Object self, final Object... args) {
    final double num = (args.length > 0) ? JSType.toNumber(args[0]) : 0.0;

    return newObj? new NativeNumber(num) : num;
}
 
Example #28
Source Project: TencentKona-8   Author: Tencent   File: NativeInt16Array.java    License: GNU General Public License v2.0 2 votes vote down vote up
/**
 * Constructor
 *
 * @param newObj is this typed array instantiated with the new operator
 * @param self   self reference
 * @param args   args
 *
 * @return new typed array
 */
@Constructor(arity = 1)
public static NativeInt16Array constructor(final boolean newObj, final Object self, final Object... args) {
    return (NativeInt16Array)constructorImpl(newObj, args, FACTORY);
}
 
Example #29
Source Project: TencentKona-8   Author: Tencent   File: NativeString.java    License: GNU General Public License v2.0 2 votes vote down vote up
/**
 * ECMA 15.5.2.1 new String ( [ value ] )
 *
 * Constructor
 *
 * @param newObj is this constructor invoked with the new operator
 * @param self   self reference
 * @param args   arguments (a value)
 *
 * @return new NativeString, empty string if no args, extraneous args ignored
 */
@Constructor(arity = 1)
public static Object constructor(final boolean newObj, final Object self, final Object... args) {
    final CharSequence str = args.length > 0 ? JSType.toCharSequence(args[0]) : "";
    return newObj ? newObj(str) : str.toString();
}
 
Example #30
Source Project: TencentKona-8   Author: Tencent   File: NativeTypeError.java    License: GNU General Public License v2.0 2 votes vote down vote up
/**
 * ECMA 15.11.6.5 TypeError
 *
 * Constructor
 *
 * @param newObj was this error instantiated with the new operator
 * @param self   self reference
 * @param msg    error message
 *
 * @return new TypeError
 */
@Constructor(name = "TypeError")
public static NativeTypeError constructor(final boolean newObj, final Object self, final Object msg) {
    return new NativeTypeError(msg);
}