java.lang.Object
org.htmlunit.corejs.javascript.ClassDescriptor
A descriptor for a constructor and its prototype methods which can then be used to build a real
constructor function and prototype during standard object initialisation. This should be built by
creating a
ClassDescriptor.Builder and using the methods there to declare the required methods. This
should normally be done once during static initialisation of a class and then utilised when
initialising within a scope.
For example:
final class NativeBoolean extends ScriptableObject {
private static final String CLASS_NAME = "Boolean";
private static final ClassDescriptor DESCRIPTOR;
static {
DESCRIPTOR =
new ClassDescriptor.Builder(
CLASS_NAME,
1,
NativeBoolean::js_constructorFunc,
NativeBoolean::js_constructor)
.withMethod(PROTO, "toString", 0, NativeBoolean::js_toString)
.withMethod(PROTO, "toSource", 0, NativeBoolean::js_toSource)
.withMethod(PROTO, "valueOf", 0, NativeBoolean::js_valueOf)
.build();
static void init(VarScope scope, boolean sealed) {
// Boolean is an unusual object in that the prototype is itself a Boolean
var constructor = DESCRIPTOR.buildConstructor(scope, new NativeBoolean(false), sealed);
}
Properties can be defined using the ClassDescriptor.Builder.withProp(Destination, String, LambdaGetterFunction, LambdaSetterFunction, int) and the others with similar names. These either
take lambda getter and setter functions which will be converted into function objects at the
point the context is initialised, or take ClassDescriptor.ValueCreators which can populate a value
descriptor.
Convenience methods are provided to create aliases of properties (ClassDescriptor.Builder.aliss(String)) and simple values which are truly independent of the context (ClassDescriptor.Builder.value(Object)), along with variants that also accept attribute flags. More complex cases
can be handled by methods similar to NativeArray.makeUnscopables(Context, VarScope, ScriptableObject).
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classA builder for creating class descriptors.static interfaceClassDescriptor.BuiltInJSCodeExec<U extends ScriptOrFn<U>>static interfaceClassDescriptor.BuiltInJSCodeResume<U extends ScriptOrFn<U>>static enumEnum indicating the destination for an operation.static interface -
Method Summary
Modifier and TypeMethodDescriptionbuildConstructor(Context cx, VarScope scope, ScriptableObject proto, boolean sealed) Build a constructor from this descriptor.buildConstructor(Context cx, VarScope scope, ScriptableObject proto, boolean sealed, BiConsumer<Context, JSFunction> customStep) Build a constructor from this descriptor.ctorDesc()findCtorDesc(Object name) findProtoDesc(Object name) populateGlobal(Context cx, VarScope scope, ScriptableObject global, boolean sealed)
-
Method Details
-
buildConstructor
public JSFunction buildConstructor(Context cx, VarScope scope, ScriptableObject proto, boolean sealed) Build a constructor from this descriptor. -
populateGlobal
public ScriptableObject populateGlobal(Context cx, VarScope scope, ScriptableObject global, boolean sealed) -
buildConstructor
public JSFunction buildConstructor(Context cx, VarScope scope, ScriptableObject proto, boolean sealed, BiConsumer<Context, JSFunction> customStep) Build a constructor from this descriptor. The customStep will be called just before the objects would be sealed. -
findProtoDesc
-
findCtorDesc
-
ctorDesc
-
typeError
-