- All Implemented Interfaces:
Serializable,Callable,ConstProperties<Scriptable>,Constructable,DebuggableObject,Function,PropHolder<Scriptable>,Scriptable,SymbolScriptable
In micro benchmarks (as of 2021) using this class to implement a built-in class is about 15% more efficient than using IdScriptableObject, and about 25% faster than using reflection via the ScriptableObject.defineClass() family of methods. Furthermore, it results in code that more directly maps to JavaScript idioms than either methods, it is much easier to implement than IdScriptableObject, and the lambda pattern makes it easier to maintain state in various ways that don't always map directly to the existing concepts.
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class org.htmlunit.corejs.javascript.ScriptableObject
ScriptableObject.DescriptorInfo, ScriptableObject.KeyComparator, ScriptableObject.LambdaGetterFunction, ScriptableObject.LambdaSetterFunction -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intBy default, the constructor may be invoked either waystatic final intIf this flag is set, the constructor may be invoked as an ordinary functionstatic final intIf this flag is set, the constructor may be invoked using "new"protected final SerializableConstructableFields inherited from class org.htmlunit.corejs.javascript.LambdaFunction
targetFields inherited from class org.htmlunit.corejs.javascript.BaseFunction
Id_arguments, Id_arity, Id_length, Id_name, Id_prototype, MAX_INSTANCE_IDFields inherited from class org.htmlunit.corejs.javascript.ScriptableObject
CONST, DONTENUM, EMPTY, LEGACY_CONST, PERMANENT, READONLY, STRICTLY_READONLY, UNINITIALIZED_CONSTFields inherited from class org.htmlunit.corejs.javascript.SlotMapOwner
isSealedFields inherited from interface org.htmlunit.corejs.javascript.Scriptable
NOT_FOUND -
Constructor Summary
ConstructorsConstructorDescriptionLambdaConstructor(VarScope scope, String name, int length, int flags, SerializableConstructable target) Create a new function that may be used as a constructor.LambdaConstructor(VarScope scope, String name, int length, Object prototype, SerializableCallable target, SerializableConstructable targetConstructor) LambdaConstructor(VarScope scope, String name, int length, SerializableCallable target, SerializableConstructable targetConstructor) Create a new function that may be used as a constructor.LambdaConstructor(VarScope scope, String name, int length, SerializableConstructable target) Create a new function that may be used as a constructor. -
Method Summary
Modifier and TypeMethodDescriptionCall the function as a constructor.static <T> TconvertThisObject(Object thisObj, Class<T> targetClass) A convenience method to convert JavaScript's "this" object into a target class and throw a TypeError if it does not match.voiddefineConstructorMethod(VarScope scope, String name, int length, Object prototype, SerializableCallable target, int attributes, int propertyAttributes) Define a function property directly on the constructor that is implemented under the covers by a LambdaFunction, and override the properties of its "name", "length", "arity", and "protoyupe" properties.voiddefineConstructorMethod(VarScope scope, String name, int length, SerializableCallable target) Define a function property directly on the constructor that is implemented under the covers by a LambdaFunction.voiddefineConstructorMethod(VarScope scope, String name, int length, SerializableCallable target, int attributes, int propertyAttributes) Define a function property directly on the constructor that is implemented under the covers by a LambdaFunction, and override the properties of its "name", "length", and "arity" properties.voiddefineConstructorMethod(VarScope scope, Symbol key, String name, int length, SerializableCallable target) Define a function property directly on the constructor that is implemented under the covers by a LambdaFunction.voiddefineKnownBuiltInPrototypeMethod(Object tag, VarScope scope, String name, int length, Object prototype, SerializableCallable target, int attributes, int propertyAttributes) Define a function property on the prototype of the constructor using a KnownBuiltInFunction under the covers.voiddefineProperty(Context cx, String name, ScriptableObject.LambdaGetterFunction getter, int attributes) voiddefineProperty(Context cx, String name, ScriptableObject.LambdaGetterFunction getter, ScriptableObject.LambdaSetterFunction setter, int attributes) voiddefinePrototypeAlias(String name, String alias, int attributes) Define a property on the prototype that has the same value as another property.voiddefinePrototypeAlias(String name, SymbolKey alias, int attributes) Define a property on the prototype that has the same value as another property.voiddefinePrototypeMethod(VarScope scope, String name, int length, Object prototype, SerializableCallable target, int attributes, int propertyAttributes) Define a function property on the prototype of the constructor using a LambdaFunction under the covers.voiddefinePrototypeMethod(VarScope scope, String name, int length, SerializableCallable target) Define a function property on the prototype of the constructor using a LambdaFunction under the covers.voiddefinePrototypeMethod(VarScope scope, String name, int length, SerializableCallable target, int attributes, int propertyAttributes) Define a function property on the prototype of the constructor using a LambdaFunction under the covers.voiddefinePrototypeMethod(VarScope scope, SymbolKey name, int length, Object prototype, SerializableCallable target, int attributes, int propertyAttributes) Define a function property on the prototype of the constructor using a LambdaFunction under the covers.voiddefinePrototypeMethod(VarScope scope, SymbolKey name, int length, SerializableCallable target) Define a function property on the prototype of the constructor using a LambdaFunction under the covers.voiddefinePrototypeMethod(VarScope scope, SymbolKey name, int length, SerializableCallable target, int attributes) voiddefinePrototypeMethod(VarScope scope, SymbolKey name, int length, SerializableCallable target, int attributes, int propertyAttributes) Define a function property on the prototype of the constructor using a LambdaFunction under the covers.voiddefinePrototypeProperty(String name, Object value, int attributes) Define a property that may be of any type on the prototype of this constructor.voiddefinePrototypeProperty(Context cx, String name, ScriptableObject descriptor) voiddefinePrototypeProperty(Context cx, String name, ScriptableObject.LambdaGetterFunction getter) voiddefinePrototypeProperty(Context cx, String name, ScriptableObject.LambdaGetterFunction getter, int attributes) Define a property on the prototype using a function.voiddefinePrototypeProperty(Context cx, String name, ScriptableObject.LambdaGetterFunction getter, ScriptableObject.LambdaSetterFunction setter) voiddefinePrototypeProperty(Context cx, String name, ScriptableObject.LambdaGetterFunction getter, ScriptableObject.LambdaSetterFunction setter, int attributes) Define a property on the prototype using functions for getter and setter.voiddefinePrototypeProperty(Context cx, Symbol key, ScriptableObject descriptor) voiddefinePrototypeProperty(Context cx, Symbol key, ScriptableObject.LambdaGetterFunction getter, int attributes) voiddefinePrototypeProperty(Context cx, Symbol name, ScriptableObject.LambdaGetterFunction getter, ScriptableObject.LambdaSetterFunction setter) voiddefinePrototypeProperty(Context cx, Symbol key, ScriptableObject.LambdaGetterFunction getter, ScriptableObject.LambdaSetterFunction setter, int attributes) voiddefinePrototypeProperty(Symbol key, Object value, int attributes) protected ConstructablebooleanReturn whether this function can be called as a constructor.voidReplace the default "Object" prototype with a prototype of a specific implementation.Methods inherited from class org.htmlunit.corejs.javascript.LambdaFunction
getArity, getFunctionName, getLengthMethods inherited from class org.htmlunit.corejs.javascript.BaseFunction
call, createObject, createObject, createProperties, createPrototypeProperty, createPrototypeProperty, defaultGet, defaultHas, defaultPut, getClassName, getClassPrototype, getDeclarationScope, getHomeObject, getPrototypeProperty, getTypeOf, hasDefaultParameters, hasInstance, hasPrototypeProperty, isGeneratorFunction, prototypeDescSetter, setHomeObject, setImmunePrototypeProperty, setPrototypeProperty, setPrototypePropertyAttributes, setStandardPropertyAttributes, setupDefaultPrototypeMethods inherited from class org.htmlunit.corejs.javascript.ScriptableObject
applyDescriptorToAttributeBitset, avoidObjectDetection, buildDataDescriptor, callMethod, callMethod, checkNotSealed, checkPropertyChangeForSlot, checkPropertyChangeForSlot, checkPropertyDefinition, checkPropertyDefinition, checkSlotRemoval, defineBuiltinProperty, defineBuiltinProperty, defineBuiltInProperty, defineClass, defineClass, defineClass, defineConst, defineConstProperty, defineFunctionProperties, defineOwnProperties, defineOwnProperty, defineOwnProperty, defineOwnProperty, defineProperty, defineProperty, defineProperty, defineProperty, defineProperty, defineProperty, defineProperty, defineProperty, defineProperty, defineProperty, delete, delete, delete, deleteProperty, deleteProperty, deleteProperty, ensureScriptable, ensureScriptableObject, ensureScriptableObjectButNotSymbol, ensureSymbolScriptable, ensureType, equivalentValues, get, get, getAllIds, getAncestor, getArrayPrototype, getClassPrototype, getClassPrototype, getDefaultValue, getDefaultValue, getExternalArrayData, getExternalArrayLength, getFunctionPrototype, getGeneratorFunctionPrototype, getGetterOrSetter, getGetterOrSetter, getObjectPrototype, getOwnPropertyDescriptor, getParentScope, getProperty, getProperty, getProperty, getProperty, getProperty, getPropertyIds, getPrototype, getSuperProperty, getSuperProperty, getSuperProperty, getThis, getTopLevelScope, getTopScopeValue, getTypedProperty, getTypedProperty, has, hasProperty, hasProperty, hasProperty, hasProperty, isAccessorDescriptor, isAccessorDescriptor, isConst, isDataDescriptor, isEmpty, isExtensible, isFalse, isGenericDescriptor, isGenericDescriptor, isGetterOrSetter, isGetterOrSetter, isTrue, preventExtensions, put, put, put, putConst, putConstProperty, putConstProperty, putOwnProperty, putOwnProperty, putOwnProperty, putProperty, putProperty, putProperty, putProperty, putSuperProperty, putSuperProperty, putSuperProperty, querySlot, redefineProperty, redefineProperty, sameValue, sealObject, setCommonDescriptorProperties, setExternalArrayData, setGetterOrSetter, setParentScope, setPrototype, sizeMethods inherited from class org.htmlunit.corejs.javascript.SlotMapOwner
associateValue, checkNotSealed, copyAssociatedValue, createSlotMap, createSlotMap, defineProperty, defineProperty, get, get, getAssociatedValue, getAttributes, getAttributes, getAttributes, getAttributes, getAttributes, getIds, has, has, isSealed, readMaps, setAttributes, setAttributes, setAttributes, setAttributes, setAttributes, writeMapsMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.htmlunit.corejs.javascript.Constructable
constructMethods inherited from interface org.htmlunit.corejs.javascript.Scriptable
delete, delete, delete, get, get, get, getAncestor, getDefaultValue, getIds, getParentScope, getPrototype, has, has, has, put, put, put, setParentScope, setPrototypeMethods inherited from interface org.htmlunit.corejs.javascript.SymbolScriptable
get, has
-
Field Details
-
CONSTRUCTOR_FUNCTION
public static final int CONSTRUCTOR_FUNCTIONIf this flag is set, the constructor may be invoked as an ordinary function- See Also:
-
CONSTRUCTOR_NEW
public static final int CONSTRUCTOR_NEWIf this flag is set, the constructor may be invoked using "new"- See Also:
-
CONSTRUCTOR_DEFAULT
public static final int CONSTRUCTOR_DEFAULTBy default, the constructor may be invoked either way- See Also:
-
targetConstructor
-
-
Constructor Details
-
LambdaConstructor
Create a new function that may be used as a constructor. The new object will have the Function prototype and no parent. The caller is responsible for binding this object to the appropriate scope. The new constructor function can be invoked using "new" or by calling it directly, and in either case will result in a new object being returned and wired to the correct prototype and scope.- Parameters:
scope- scope of the calling contextname- name of the functionlength- the arity of the functiontarget- an object that implements the function in Java. Since Constructable is a single-function interface this will typically be implemented as a lambda.
-
LambdaConstructor
public LambdaConstructor(VarScope scope, String name, int length, int flags, SerializableConstructable target) Create a new function that may be used as a constructor. The new object will have the Function prototype and no parent. The caller is responsible for binding this object to the appropriate scope. The "flags" argument controls whether the function may be invoked using "new," via a direct call, or both. If allowed by the flags, then the constructor will have the same effect either way. If not allowed by the flags, then a TypeError will be thrown.- Parameters:
scope- scope of the calling contextname- name of the functionlength- the arity of the functionflags- which may be a combination of CONSTRUCTOR_NEW and CONSTRUCTOR_FUNCTIONtarget- an object that implements the function in Java. Since Constructable is a single-function interface this will typically be implemented as a lambda.
-
LambdaConstructor
public LambdaConstructor(VarScope scope, String name, int length, SerializableCallable target, SerializableConstructable targetConstructor) Create a new function that may be used as a constructor. The new object will have the Function prototype and no parent. The caller is responsible for binding this object to the appropriate scope. The new constructor function will have different behavior depending on whether it is invoked via "new" or via a direct call. In the case of "new", a new object with a prototype and scope chain be returned, but in the case of a direct call, the user must implement whatever they need. This is typically used in the case of functions like the native Date constructor, which has totally different behavior depending on how it's invoked.- Parameters:
scope- scope of the calling contextname- name of the functionlength- the arity of the functiontarget- an object that implements the function in Java. Since Constructable is a single-function interface this will typically be implemented as a lambda.
-
LambdaConstructor
public LambdaConstructor(VarScope scope, String name, int length, Object prototype, SerializableCallable target, SerializableConstructable targetConstructor)
-
-
Method Details
-
getTargetConstructor
-
call
- Overrides:
callin classLambdaFunction
-
construct
Description copied from interface:FunctionCall the function as a constructor.This method is invoked by the runtime in order to satisfy a use of the JavaScript
newoperator. This method is expected to create a new object and return it.If nt is equal to this then this call is equivalent `new f(...arguments), but reflection and super constructor calls may pass in different values.
- Specified by:
constructin interfaceConstructable- Specified by:
constructin interfaceFunction- Overrides:
constructin classLambdaFunction- Parameters:
cx- the current Context for this threadnt- the new.target for this call,scope- an enclosing scope of the caller except when the function is called from a closure.args- the array of arguments- Returns:
- the allocated object
-
definePrototypeMethod
public void definePrototypeMethod(VarScope scope, String name, int length, SerializableCallable target) Define a function property on the prototype of the constructor using a LambdaFunction under the covers. -
definePrototypeMethod
public void definePrototypeMethod(VarScope scope, String name, int length, SerializableCallable target, int attributes, int propertyAttributes) Define a function property on the prototype of the constructor using a LambdaFunction under the covers. -
definePrototypeMethod
public void definePrototypeMethod(VarScope scope, SymbolKey name, int length, SerializableCallable target) Define a function property on the prototype of the constructor using a LambdaFunction under the covers. -
definePrototypeMethod
public void definePrototypeMethod(VarScope scope, SymbolKey name, int length, SerializableCallable target, int attributes) -
definePrototypeMethod
public void definePrototypeMethod(VarScope scope, SymbolKey name, int length, SerializableCallable target, int attributes, int propertyAttributes) Define a function property on the prototype of the constructor using a LambdaFunction under the covers. -
definePrototypeMethod
public void definePrototypeMethod(VarScope scope, String name, int length, Object prototype, SerializableCallable target, int attributes, int propertyAttributes) Define a function property on the prototype of the constructor using a LambdaFunction under the covers. -
defineKnownBuiltInPrototypeMethod
public void defineKnownBuiltInPrototypeMethod(Object tag, VarScope scope, String name, int length, Object prototype, SerializableCallable target, int attributes, int propertyAttributes) Define a function property on the prototype of the constructor using a KnownBuiltInFunction under the covers. -
definePrototypeMethod
public void definePrototypeMethod(VarScope scope, SymbolKey name, int length, Object prototype, SerializableCallable target, int attributes, int propertyAttributes) Define a function property on the prototype of the constructor using a LambdaFunction under the covers. -
definePrototypeProperty
Define a property that may be of any type on the prototype of this constructor. -
definePrototypeProperty
-
definePrototypeProperty
-
definePrototypeProperty
-
definePrototypeProperty
public void definePrototypeProperty(Context cx, String name, ScriptableObject.LambdaGetterFunction getter, int attributes) Define a property on the prototype using a function. The function will be wired to a JavaScript function, so the resulting property will look just like one that was defined using "Object.defineOwnProperty" with a property descriptor. -
defineProperty
public void defineProperty(Context cx, String name, ScriptableObject.LambdaGetterFunction getter, int attributes) -
definePrototypeProperty
public void definePrototypeProperty(Context cx, String name, ScriptableObject.LambdaGetterFunction getter) -
definePrototypeProperty
public void definePrototypeProperty(Context cx, Symbol key, ScriptableObject.LambdaGetterFunction getter, int attributes) -
definePrototypeProperty
public void definePrototypeProperty(Context cx, String name, ScriptableObject.LambdaGetterFunction getter, ScriptableObject.LambdaSetterFunction setter, int attributes) Define a property on the prototype using functions for getter and setter. The function will be wired to a JavaScript function, so the resulting property will look just like one that was defined using "Object.defineOwnProperty" with a property descriptor. -
definePrototypeProperty
public void definePrototypeProperty(Context cx, String name, ScriptableObject.LambdaGetterFunction getter, ScriptableObject.LambdaSetterFunction setter) -
definePrototypeProperty
public void definePrototypeProperty(Context cx, Symbol key, ScriptableObject.LambdaGetterFunction getter, ScriptableObject.LambdaSetterFunction setter, int attributes) -
definePrototypeProperty
public void definePrototypeProperty(Context cx, Symbol name, ScriptableObject.LambdaGetterFunction getter, ScriptableObject.LambdaSetterFunction setter) -
definePrototypeAlias
Define a property on the prototype that has the same value as another property. -
definePrototypeAlias
Define a property on the prototype that has the same value as another property. -
defineProperty
public void defineProperty(Context cx, String name, ScriptableObject.LambdaGetterFunction getter, ScriptableObject.LambdaSetterFunction setter, int attributes) -
defineConstructorMethod
public void defineConstructorMethod(VarScope scope, String name, int length, SerializableCallable target) Define a function property directly on the constructor that is implemented under the covers by a LambdaFunction.- Parameters:
name- the key to use to look up the new function property, and also the value to return for the "name" property of the Function objectlength- the value to return for the "length" property of the Function objecttarget- the target to call when the method is invoked
-
defineConstructorMethod
public void defineConstructorMethod(VarScope scope, Symbol key, String name, int length, SerializableCallable target) Define a function property directly on the constructor that is implemented under the covers by a LambdaFunction.- Parameters:
key- the Symbol to use to look up the propertyname- the value to return for the "name" property of the Function objectlength- the value to return for the "length" property of the Function objecttarget- the target to call when the method is invoked
-
defineConstructorMethod
public void defineConstructorMethod(VarScope scope, String name, int length, SerializableCallable target, int attributes, int propertyAttributes) Define a function property directly on the constructor that is implemented under the covers by a LambdaFunction, and override the properties of its "name", "length", and "arity" properties. -
defineConstructorMethod
public void defineConstructorMethod(VarScope scope, String name, int length, Object prototype, SerializableCallable target, int attributes, int propertyAttributes) Define a function property directly on the constructor that is implemented under the covers by a LambdaFunction, and override the properties of its "name", "length", "arity", and "protoyupe" properties. -
setPrototypeScriptable
Replace the default "Object" prototype with a prototype of a specific implementation. This is only necessary for a few built-in constructors, like Boolean, that must have their prototype be an object with a specific "internal data slot." -
convertThisObject
A convenience method to convert JavaScript's "this" object into a target class and throw a TypeError if it does not match. This is useful for implementing lambda functions, as "this" in JavaScript doesn't necessarily map to an instance of the class. -
isConstructor
public boolean isConstructor()Description copied from interface:FunctionReturn whether this function can be called as a constructor.- Specified by:
isConstructorin interfaceFunction- Overrides:
isConstructorin classLambdaFunction
-