public Object construct(JSFunction function, Object... args) { if (!function.isConstructor()) { throw new ThrowException(this, createTypeError("not a constructor")); } // 13.2.2 // 1. create the new object JSObject obj = function.createNewObject(this); // 2. set internal methods per 8.12 [DynObject] // 3. set class name [DynObject subclass (defaults true)] // 4. Set Extensible [DynObject subclass (defaults true)] // 5. Get the function's prototype // 6. If prototype is an object make that the new object's prototype // 7. If prototype is not an object set to the standard builtin object prototype 15.2.4 // [AbstractJavascriptFunction] // [AbstractJavascriptFunction] handles #7, subclasses may handle #6 if necessary (see // BuiltinArray#createNewObject) Object p = function.get(this, "prototype"); if (p != Types.UNDEFINED && p instanceof JSObject) { obj.setPrototype((JSObject) p); } else { JSObject defaultObjectProto = getPrototypeFor("Object"); obj.setPrototype(defaultObjectProto); } // 8. Call the function with obj as self Object result = internalCall(null, function, obj, args); // 9. If result is a JSObject return it if (result instanceof JSObject) { return (JSObject) result; } // Otherwise return obj return obj; }