Package org.mozilla.javascript
Class LambdaConstructor
- java.lang.Object
- 
- org.mozilla.javascript.ScriptableObject
- 
- org.mozilla.javascript.IdScriptableObject
- 
- org.mozilla.javascript.BaseFunction
- 
- org.mozilla.javascript.LambdaFunction
- 
- org.mozilla.javascript.LambdaConstructor
 
 
 
 
 
- 
- All Implemented Interfaces:
- java.io.Serializable,- Callable,- ConstProperties,- Constructable,- DebuggableObject,- Function,- IdFunctionCall,- Scriptable,- SymbolScriptable
 
 public class LambdaConstructor extends LambdaFunction This class implements a JavaScript function that may be used as a constructor by delegating to an interface that can be easily implemented as a lambda. The LambdaFunction class may be used to add functions to the prototype that are also implemented as lambdas.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:
- Serialized Form
 
- 
- 
Nested Class Summary- 
Nested classes/interfaces inherited from class org.mozilla.javascript.ScriptableObjectScriptableObject.KeyComparator
 
- 
 - 
Field SummaryFields Modifier and Type Field Description static intCONSTRUCTOR_DEFAULTBy default, the constructor may be invoked either waystatic intCONSTRUCTOR_FUNCTIONIf this flag is set, the constructor may be invoked as an ordinary functionstatic intCONSTRUCTOR_NEWIf this flag is set, the constructor may be invoked using "new"- 
Fields inherited from class org.mozilla.javascript.BaseFunctionId_arguments, Id_arity, Id_length, Id_name, Id_prototype, MAX_INSTANCE_ID
 - 
Fields inherited from class org.mozilla.javascript.ScriptableObjectCONST, DONTENUM, EMPTY, PERMANENT, READONLY, UNINITIALIZED_CONST
 - 
Fields inherited from interface org.mozilla.javascript.ScriptableNOT_FOUND
 
- 
 - 
Constructor SummaryConstructors Constructor Description LambdaConstructor(Scriptable scope, java.lang.String name, int length, int flags, Constructable target)Create a new function and control whether it may be invoked using new, as a function, or both.LambdaConstructor(Scriptable scope, java.lang.String name, int length, Constructable target)Create a new function that may be used as a constructor.
 - 
Method SummaryAll Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.Objectcall(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args)Should be overridden.Scriptableconstruct(Context cx, Scriptable scope, java.lang.Object[] args)Call the function as a constructor.static <T> TconvertThisObject(Scriptable thisObj, java.lang.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(Scriptable scope, java.lang.String name, int length, Callable target, int attributes)Define a function property directly on the constructor that is implemented under the covers by a LambdaFunction.voiddefineConstructorMethod(Scriptable scope, java.lang.String name, int length, Callable 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(Scriptable scope, Symbol key, java.lang.String name, int length, Callable target, int attributes)Define a function property directly on the constructor that is implemented under the covers by a LambdaFunction.voiddefinePrototypeMethod(Scriptable scope, java.lang.String name, int length, Callable target)Define a function property on the prototype of the constructor using a LambdaFunction under the covers.voiddefinePrototypeMethod(Scriptable scope, java.lang.String name, int length, Callable target, int attributes, int propertyAttributes)Define a function property on the prototype of the constructor using a LambdaFunction under the covers.voiddefinePrototypeProperty(java.lang.String name, java.lang.Object value, int attributes)Define a property that may be of any type on the prototype of this constructor.voiddefinePrototypeProperty(Symbol key, java.lang.Object value, int attributes)- 
Methods inherited from class org.mozilla.javascript.LambdaFunctiongetArity, getFunctionName, getLength
 - 
Methods inherited from class org.mozilla.javascript.BaseFunctioncreateObject, execIdCall, fillConstructorProperties, findInstanceIdInfo, findPrototypeId, getClassName, getClassPrototype, getInstanceIdName, getInstanceIdValue, getMaxInstanceId, getPrototypeProperty, getTypeOf, hasInstance, hasPrototypeProperty, initPrototypeId, isGeneratorFunction, setImmunePrototypeProperty, setInstanceIdAttributes, setInstanceIdValue, setPrototypeProperty, setPrototypePropertyAttributes, setStandardPropertyAttributes, setupDefaultPrototype
 - 
Methods inherited from class org.mozilla.javascript.IdScriptableObjectactivatePrototypeMap, addIdFunctionProperty, defaultGet, defaultHas, defaultPut, defineOwnProperty, delete, delete, ensureType, exportAsJSClass, findInstanceIdInfo, findPrototypeId, get, get, getAttributes, getAttributes, getOwnPropertyDescriptor, has, has, hasPrototypeMap, initPrototypeConstructor, initPrototypeMethod, initPrototypeMethod, initPrototypeMethod, initPrototypeValue, initPrototypeValue, instanceIdInfo, put, put, setAttributes
 - 
Methods inherited from class org.mozilla.javascript.ScriptableObjectapplyDescriptorToAttributeBitset, associateValue, avoidObjectDetection, buildDataDescriptor, callMethod, callMethod, checkPropertyChange, checkPropertyDefinition, defineClass, defineClass, defineClass, defineConst, defineConstProperty, defineFunctionProperties, defineOwnProperties, defineOwnProperty, defineProperty, defineProperty, defineProperty, defineProperty, defineProperty, defineProperty, defineProperty, delete, deleteProperty, deleteProperty, ensureScriptable, ensureScriptableObject, ensureSymbolScriptable, equivalentValues, get, get, getAllIds, getArrayPrototype, getAssociatedValue, getAttributes, getAttributes, getAttributes, getClassPrototype, getDefaultValue, getDefaultValue, getExternalArrayData, getExternalArrayLength, getFunctionPrototype, getGeneratorFunctionPrototype, getGetterOrSetter, getGetterOrSetter, getIds, getObjectPrototype, getParentScope, getProperty, getProperty, getProperty, getPropertyIds, getPrototype, getTopLevelScope, getTopScopeValue, getTypedProperty, getTypedProperty, has, hasProperty, hasProperty, hasProperty, isAccessorDescriptor, isConst, isDataDescriptor, isEmpty, isExtensible, isFalse, isGenericDescriptor, isGetterOrSetter, isSealed, isTrue, preventExtensions, put, putConst, putConstProperty, putProperty, putProperty, putProperty, querySlot, redefineProperty, sameValue, sealObject, setAttributes, setAttributes, setAttributes, setAttributes, setCommonDescriptorProperties, setExternalArrayData, setGetterOrSetter, setParentScope, setPrototype, size
 - 
Methods inherited from class java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 - 
Methods inherited from interface org.mozilla.javascript.Scriptabledelete, delete, get, get, getDefaultValue, getIds, getParentScope, getPrototype, has, has, put, put, setParentScope, setPrototype
 
- 
 
- 
- 
- 
Field Detail- 
CONSTRUCTOR_FUNCTIONpublic static final int CONSTRUCTOR_FUNCTION If this flag is set, the constructor may be invoked as an ordinary function- See Also:
- Constant Field Values
 
 - 
CONSTRUCTOR_NEWpublic static final int CONSTRUCTOR_NEW If this flag is set, the constructor may be invoked using "new"- See Also:
- Constant Field Values
 
 - 
CONSTRUCTOR_DEFAULTpublic static final int CONSTRUCTOR_DEFAULT By default, the constructor may be invoked either way- See Also:
- Constant Field Values
 
 
- 
 - 
Constructor Detail- 
LambdaConstructorpublic LambdaConstructor(Scriptable scope, java.lang.String name, int length, Constructable 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.- Parameters:
- scope- scope of the calling context
- name- name of the function
- length- the arity of the function
- target- an object that implements the function in Java. Since Constructable is a single-function interface this will typically be implemented as a lambda.
 
 - 
LambdaConstructorpublic LambdaConstructor(Scriptable scope, java.lang.String name, int length, int flags, Constructable target) Create a new function and control whether it may be invoked using new, as a function, or both.
 
- 
 - 
Method Detail- 
callpublic java.lang.Object call(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args) Description copied from class:BaseFunctionShould be overridden.- Specified by:
- callin interface- Callable
- Specified by:
- callin interface- Function
- Overrides:
- callin class- LambdaFunction
- Parameters:
- cx- the current Context for this thread
- scope- 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- thisobject
- args- the array of arguments
- Returns:
- the result of the call
 
 - 
constructpublic Scriptable construct(Context cx, Scriptable scope, java.lang.Object[] args) 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.- Specified by:
- constructin interface- Constructable
- Specified by:
- constructin interface- Function
- Overrides:
- constructin class- LambdaFunction
- Parameters:
- cx- the current Context for this thread
- 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
 
 - 
definePrototypeMethodpublic void definePrototypeMethod(Scriptable scope, java.lang.String name, int length, Callable target) Define a function property on the prototype of the constructor using a LambdaFunction under the covers.
 - 
definePrototypeMethodpublic void definePrototypeMethod(Scriptable scope, java.lang.String name, int length, Callable target, int attributes, int propertyAttributes) Define a function property on the prototype of the constructor using a LambdaFunction under the covers.
 - 
definePrototypePropertypublic void definePrototypeProperty(java.lang.String name, java.lang.Object value, int attributes)Define a property that may be of any type on the prototype of this constructor.
 - 
definePrototypePropertypublic void definePrototypeProperty(Symbol key, java.lang.Object value, int attributes) 
 - 
defineConstructorMethodpublic void defineConstructorMethod(Scriptable scope, java.lang.String name, int length, Callable target, int attributes) 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 object
- length- the value to return for the "length" property of the Function object
- target- the target to call when the method is invoked
- attributes- the attributes to set on the new property
 
 - 
defineConstructorMethodpublic void defineConstructorMethod(Scriptable scope, Symbol key, java.lang.String name, int length, Callable target, int attributes) 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 property
- name- the value to return for the "name" property of the Function object
- length- the value to return for the "length" property of the Function object
- target- the target to call when the method is invoked
- attributes- the attributes to set on the new property
 
 - 
defineConstructorMethodpublic void defineConstructorMethod(Scriptable scope, java.lang.String name, int length, Callable 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.
 - 
convertThisObjectpublic static <T> T convertThisObject(Scriptable thisObj, java.lang.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. This is useful for implementing lambda functions, as "this" in JavaScript doesn't necessarily map to an instance of the class.
 
- 
 
-