public class LambdaConstructor extends LambdaFunction
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.
ScriptableObject.KeyComparator, ScriptableObject.LambdaGetterFunction, ScriptableObject.LambdaSetterFunction| Modifier and Type | Field and Description |
|---|---|
static int |
CONSTRUCTOR_DEFAULT
By default, the constructor may be invoked either way
|
static int |
CONSTRUCTOR_FUNCTION
If this flag is set, the constructor may be invoked as an ordinary function
|
static int |
CONSTRUCTOR_NEW
If this flag is set, the constructor may be invoked using "new"
|
protected SerializableConstructable |
targetConstructor |
targetId_arguments, Id_arity, Id_length, Id_name, Id_prototype, MAX_INSTANCE_IDCONST, DONTENUM, EMPTY, PERMANENT, READONLY, UNINITIALIZED_CONSTNOT_FOUND| Constructor and Description |
|---|
LambdaConstructor(Scriptable scope,
String name,
int length,
int flags,
SerializableConstructable target)
Create a new function that may be used as a constructor.
|
LambdaConstructor(Scriptable scope,
String name,
int length,
Object prototype,
SerializableCallable target,
SerializableConstructable targetConstructor) |
LambdaConstructor(Scriptable scope,
String name,
int length,
SerializableCallable target,
SerializableConstructable targetConstructor)
Create a new function that may be used as a constructor.
|
LambdaConstructor(Scriptable scope,
String name,
int length,
SerializableConstructable target)
Create a new function that may be used as a constructor.
|
| Modifier and Type | Method and Description |
|---|---|
Object |
call(Context cx,
Scriptable scope,
Scriptable thisObj,
Object[] args)
Should be overridden.
|
Scriptable |
construct(Context cx,
Scriptable scope,
Object[] args)
Call the function as a constructor.
|
static <T> T |
convertThisObject(Scriptable 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.
|
void |
defineConstructorMethod(Scriptable 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.
|
void |
defineConstructorMethod(Scriptable scope,
String name,
int length,
SerializableCallable target,
int attributes)
Define a function property directly on the constructor that is implemented under the covers
by a LambdaFunction.
|
void |
defineConstructorMethod(Scriptable 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.
|
void |
defineConstructorMethod(Scriptable scope,
Symbol key,
String name,
int length,
SerializableCallable target,
int attributes)
Define a function property directly on the constructor that is implemented under the covers
by a LambdaFunction.
|
void |
defineKnownBuiltInPrototypeMethod(Object tag,
Scriptable 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.
|
void |
definePrototypeAlias(String name,
String alias,
int attributes)
Define a property on the prototype that has the same value as another property.
|
void |
definePrototypeAlias(String name,
SymbolKey alias,
int attributes)
Define a property on the prototype that has the same value as another property.
|
void |
definePrototypeMethod(Scriptable 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.
|
void |
definePrototypeMethod(Scriptable scope,
String name,
int length,
SerializableCallable target)
Define a function property on the prototype of the constructor using a LambdaFunction under
the covers.
|
void |
definePrototypeMethod(Scriptable 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.
|
void |
definePrototypeMethod(Scriptable scope,
String name,
int length,
SerializableCallable target,
int attributes,
int propertyAttributes,
boolean defaultPrototype)
Define a function property on the prototype of the constructor using a LambdaFunction under
the covers and control the prototype of the new function
|
void |
definePrototypeMethod(Scriptable 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.
|
void |
definePrototypeMethod(Scriptable 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.
|
void |
definePrototypeProperty(Context cx,
String name,
ScriptableObject.LambdaGetterFunction getter,
int attributes)
Define a property on the prototype using a function.
|
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.
|
void |
definePrototypeProperty(Context cx,
String name,
ScriptableObject descriptor) |
void |
definePrototypeProperty(Context cx,
Symbol key,
ScriptableObject.LambdaGetterFunction getter,
int attributes) |
void |
definePrototypeProperty(Context cx,
Symbol key,
ScriptableObject.LambdaGetterFunction getter,
ScriptableObject.LambdaSetterFunction setter,
int attributes) |
void |
definePrototypeProperty(Context cx,
Symbol key,
ScriptableObject descriptor) |
void |
definePrototypeProperty(String name,
Object value,
int attributes)
Define a property that may be of any type on the prototype of this constructor.
|
void |
definePrototypeProperty(Symbol key,
Object value,
int attributes) |
protected Constructable |
getTargetConstructor() |
void |
setPrototypeScriptable(ScriptableObject proto)
Replace the default "Object" prototype with a prototype of a specific implementation.
|
getArity, getFunctionName, getLengthcreateObject, createProperties, createPrototypeProperty, defaultGet, defaultHas, defaultPut, getClassName, getClassPrototype, getHomeObject, getPrototypeProperty, getTypeOf, hasDefaultParameters, hasInstance, hasPrototypeProperty, includeNonStandardProps, isGeneratorFunction, setHomeObject, setImmunePrototypeProperty, setPrototypeProperty, setPrototypePropertyAttributes, setStandardPropertyAttributes, setupDefaultPrototypeapplyDescriptorToAttributeBitset, associateValue, avoidObjectDetection, buildDataDescriptor, callMethod, callMethod, checkPropertyChangeForSlot, checkPropertyDefinition, defineBuiltInProperty, defineBuiltInProperty, defineBuiltInProperty, defineBuiltInProperty, defineClass, defineClass, defineClass, defineConst, defineConstProperty, defineFunctionProperties, defineOwnProperties, 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, get, get, getAllIds, getArrayPrototype, getAssociatedValue, getAttributes, getAttributes, getAttributes, getAttributes, getAttributes, getClassPrototype, getDefaultValue, getDefaultValue, getExternalArrayData, getExternalArrayLength, getFunctionPrototype, getGeneratorFunctionPrototype, getGetterOrSetter, getGetterOrSetter, getIds, getObjectPrototype, getOwnPropertyDescriptor, getParentScope, getProperty, getProperty, getProperty, getPropertyIds, getPrototype, getSuperProperty, getSuperProperty, getSuperProperty, getTopLevelScope, getTopScopeValue, getTypedProperty, getTypedProperty, has, has, has, hasProperty, hasProperty, hasProperty, isAccessorDescriptor, isConst, isDataDescriptor, isEmpty, isExtensible, isFalse, isGenericDescriptor, isGetterOrSetter, isSealed, isTrue, preventExtensions, put, put, put, putConst, putConstProperty, putOwnProperty, putOwnProperty, putOwnProperty, putProperty, putProperty, putProperty, putSuperProperty, putSuperProperty, putSuperProperty, querySlot, redefineProperty, sameValue, sealObject, setAttributes, setAttributes, setAttributes, setAttributes, setAttributes, setCommonDescriptorProperties, setExternalArrayData, setGetterOrSetter, setParentScope, setPrototype, sizecreateSlotMapclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitdelete, delete, get, get, getDefaultValue, getIds, getParentScope, getPrototype, has, has, put, put, setParentScope, setPrototypepublic static final int CONSTRUCTOR_FUNCTION
public static final int CONSTRUCTOR_NEW
public static final int CONSTRUCTOR_DEFAULT
protected final SerializableConstructable targetConstructor
public LambdaConstructor(Scriptable scope, String name, int length, SerializableConstructable target)
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.public LambdaConstructor(Scriptable scope, String name, int length, int flags, SerializableConstructable target)
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.public LambdaConstructor(Scriptable scope, String name, int length, SerializableCallable target, SerializableConstructable targetConstructor)
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.public LambdaConstructor(Scriptable scope, String name, int length, Object prototype, SerializableCallable target, SerializableConstructable targetConstructor)
protected Constructable getTargetConstructor()
public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args)
BaseFunctioncall in interface Callablecall in interface Functioncall in class LambdaFunctioncx - the current Context for this threadscope - the scope to execute the function relative to. This is set to the value returned
by getParentScope() except when the function is called from a closure.thisObj - the JavaScript this objectargs - the array of argumentspublic Scriptable construct(Context cx, Scriptable scope, Object[] args)
FunctionThis method is invoked by the runtime in order to satisfy a use of the JavaScript
new operator. This method is expected to create a new object and return it.
construct in interface Constructableconstruct in interface Functionconstruct in class LambdaFunctioncx - the current Context for this threadscope - an enclosing scope of the caller except when the function is called from a
closure.args - the array of argumentspublic void definePrototypeMethod(Scriptable scope, String name, int length, SerializableCallable target)
public void definePrototypeMethod(Scriptable scope, String name, int length, SerializableCallable target, int attributes, int propertyAttributes)
public void definePrototypeMethod(Scriptable scope, String name, int length, SerializableCallable target, int attributes, int propertyAttributes, boolean defaultPrototype)
public void definePrototypeMethod(Scriptable scope, SymbolKey name, int length, SerializableCallable target, int attributes, int propertyAttributes)
public void definePrototypeMethod(Scriptable scope, String name, int length, Object prototype, SerializableCallable target, int attributes, int propertyAttributes)
public void defineKnownBuiltInPrototypeMethod(Object tag, Scriptable scope, String name, int length, Object prototype, SerializableCallable target, int attributes, int propertyAttributes)
public void definePrototypeMethod(Scriptable scope, SymbolKey name, int length, Object prototype, SerializableCallable target, int attributes, int propertyAttributes)
public void definePrototypeProperty(String name, Object value, int attributes)
public void definePrototypeProperty(Symbol key, Object value, int attributes)
public void definePrototypeProperty(Context cx, String name, ScriptableObject descriptor)
public void definePrototypeProperty(Context cx, Symbol key, ScriptableObject descriptor)
public void definePrototypeProperty(Context cx, String name, ScriptableObject.LambdaGetterFunction getter, int attributes)
public void definePrototypeProperty(Context cx, Symbol key, ScriptableObject.LambdaGetterFunction getter, int attributes)
public void definePrototypeProperty(Context cx, String name, ScriptableObject.LambdaGetterFunction getter, ScriptableObject.LambdaSetterFunction setter, int attributes)
public void definePrototypeProperty(Context cx, Symbol key, ScriptableObject.LambdaGetterFunction getter, ScriptableObject.LambdaSetterFunction setter, int attributes)
public void definePrototypeAlias(String name, SymbolKey alias, int attributes)
public void definePrototypeAlias(String name, String alias, int attributes)
public void defineConstructorMethod(Scriptable scope, String name, int length, SerializableCallable target, int attributes)
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 invokedattributes - the attributes to set on the new propertypublic void defineConstructorMethod(Scriptable scope, Symbol key, String name, int length, SerializableCallable target, int attributes)
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 invokedattributes - the attributes to set on the new propertypublic void defineConstructorMethod(Scriptable scope, String name, int length, SerializableCallable target, int attributes, int propertyAttributes)
public void defineConstructorMethod(Scriptable scope, String name, int length, Object prototype, SerializableCallable target, int attributes, int propertyAttributes)
public void setPrototypeScriptable(ScriptableObject proto)
public static <T> T convertThisObject(Scriptable thisObj, Class<T> targetClass)
Copyright © 2025 HtmlUnit. All rights reserved.