JsClass Class

The JavaScript class of a .NET class. The source generator derives it for each class marked with JsExportAttribute, and exposes it as the static JsClass property of the class: JsRealm.GetClass creates its constructor and its prototype in a realm.

public abstract class JsClass

Constructors

NameDescription
JsClass(string, Type, JsClass?)Initializes the class.

JsClass(string, Type, JsClass?)

JsClass.cs

Initializes the class.

protected JsClass(string name, Type type, JsClass? baseClass)

Parameters

name string
The name of the JavaScript class.
type Type
The .NET class.
baseClass JsClass?
The JavaScript class of the nearest base class marked with JsExportAttribute, or null.

Properties

NameDescription
BaseClassGets the JavaScript class of the nearest base class marked with JsExportAttribute: the prototypes of its constructor and of its prototype.
ConstructorLengthGets the length of the constructor: the number of its required parameters.
IsStaticGets whether the .NET class is static: its JavaScript value is a plain object whose properties are its static members.
NameGets the name of the JavaScript class.
TypeGets the .NET class.

BaseClass

JsClass.cs

Gets the JavaScript class of the nearest base class marked with JsExportAttribute: the prototypes of its constructor and of its prototype.

public JsClass? BaseClass { get; }

Property value

JsClass?

ConstructorLength

JsClass.cs

Gets the length of the constructor: the number of its required parameters.

protected virtual int ConstructorLength { get; }

Property value

int

IsStatic

JsClass.cs

Gets whether the .NET class is static: its JavaScript value is a plain object whose properties are its static members.

public bool IsStatic { get; }

Property value

bool

Gets the name of the JavaScript class.

public string Name { get; }

Property value

string

Gets the .NET class.

public Type Type { get; }

Property value

Type

Methods

NameDescription
CreateInstance(JsRealm, ReadOnlySpan<JsValue>)Creates the .NET object of a JavaScript object created by new.
DefineAccessor(JsRealm, JsObject, string, JsNativeFunction, JsNativeFunction?)Defines an accessor as the accessors of a JavaScript class: not enumerable and configurable.
DefineMembers(JsRealm, JsObject, JsObject?)Defines the static members on constructor, and the instance members on prototype.
DefineMethod(JsRealm, JsObject, string, int, JsNativeFunction)Defines a method as the methods of a JavaScript class: writable, not enumerable and configurable.
GetArgument<T>(JsRealm, ReadOnlySpan<JsValue>, int)Converts an argument as JsEngine.ToDotNet does; a missing argument is undefined. A value that cannot be converted to Object (a function, a symbol, a cycle) is a TypeError.
GetArgument<T>(JsRealm, ReadOnlySpan<JsValue>, int, T)Converts an optional argument: defaultValue when it is missing or undefined.
GetRequiredArgument<T>(JsRealm, ReadOnlySpan<JsValue>, int)Converts an argument whose type is a non-nullable reference type: undefined and null throw a TypeError.
GetRequiredArgument<T>(JsRealm, ReadOnlySpan<JsValue>, int, T)Converts an optional argument whose type is a non-nullable reference type: defaultValue when it is missing or undefined, and a TypeError for null.
GetThis<T>(JsValue)Gets the .NET object of the this value of a method.

CreateInstance(JsRealm, ReadOnlySpan<JsValue>)

JsClass.cs

Creates the .NET object of a JavaScript object created by new.

protected virtual object? CreateInstance(JsRealm realm, ReadOnlySpan<JsValue> arguments)

Parameters

realm JsRealm
arguments ReadOnlySpan<JsValue>

Returns

object? The .NET object, or null when the scripts cannot create the class (the constructor throws a TypeError).

DefineAccessor(JsRealm, JsObject, string, JsNativeFunction, JsNativeFunction?)

JsClass.cs

Defines an accessor as the accessors of a JavaScript class: not enumerable and configurable.

protected static void DefineAccessor(JsRealm realm, JsObject target, string name, JsNativeFunction getter, JsNativeFunction? setter)

Parameters

realm JsRealm
The realm of the functions.
target JsObject
The object that receives the property.
name string
The name of the property.
getter JsNativeFunction
The getter.
setter JsNativeFunction?
The setter, or null for a read-only property.

DefineMembers(JsRealm, JsObject, JsObject?)

Defines the static members on constructor, and the instance members on prototype.

protected abstract void DefineMembers(JsRealm realm, JsObject constructor, JsObject? prototype)

Parameters

realm JsRealm
The realm of the class.
constructor JsObject
The constructor of the class, or the object of a static class.
prototype JsObject?
The prototype of the class, or null for a static class.

DefineMethod(JsRealm, JsObject, string, int, JsNativeFunction)

JsClass.cs

Defines a method as the methods of a JavaScript class: writable, not enumerable and configurable.

protected static void DefineMethod(JsRealm realm, JsObject target, string name, int length, JsNativeFunction function)

Parameters

realm JsRealm
target JsObject
name string
length int
function JsNativeFunction

GetArgument overloads

GetArgument<T>(JsRealm, ReadOnlySpan<JsValue>, int)

JsClass.cs

Converts an argument as JsEngine.ToDotNet does; a missing argument is undefined. A value that cannot be converted to Object (a function, a symbol, a cycle) is a TypeError.

protected static T GetArgument<T>(JsRealm realm, ReadOnlySpan<JsValue> arguments, int index)
Type parameters
T
Parameters
realm JsRealm
arguments ReadOnlySpan<JsValue>
index int
Returns

T

Exceptions
JsException
The argument cannot be converted (a TypeError or a RangeError).

GetArgument<T>(JsRealm, ReadOnlySpan<JsValue>, int, T)

JsClass.cs

Converts an optional argument: defaultValue when it is missing or undefined.

protected static T GetArgument<T>(JsRealm realm, ReadOnlySpan<JsValue> arguments, int index, T defaultValue)
Type parameters
T
Parameters
realm JsRealm
arguments ReadOnlySpan<JsValue>
index int
defaultValue T
Returns

T

Exceptions
JsException
The argument cannot be converted (a TypeError or a RangeError).

GetRequiredArgument overloads

GetRequiredArgument<T>(JsRealm, ReadOnlySpan<JsValue>, int)

JsClass.cs

Converts an argument whose type is a non-nullable reference type: undefined and null throw a TypeError.

protected static T GetRequiredArgument<T>(JsRealm realm, ReadOnlySpan<JsValue> arguments, int index) where T : class
Type parameters
T
Parameters
realm JsRealm
arguments ReadOnlySpan<JsValue>
index int
Returns

T

Exceptions
JsException
The argument is missing, undefined or null, or it cannot be converted (a TypeError).

GetRequiredArgument<T>(JsRealm, ReadOnlySpan<JsValue>, int, T)

JsClass.cs

Converts an optional argument whose type is a non-nullable reference type: defaultValue when it is missing or undefined, and a TypeError for null.

protected static T GetRequiredArgument<T>(JsRealm realm, ReadOnlySpan<JsValue> arguments, int index, T defaultValue) where T : class
Type parameters
T
Parameters
realm JsRealm
arguments ReadOnlySpan<JsValue>
index int
defaultValue T
Returns

T

Exceptions
JsException
The argument is null, or it cannot be converted (a TypeError).

GetThis<T>(JsValue)

JsClass.cs

Gets the .NET object of the this value of a method.

protected static T GetThis<T>(JsValue thisValue) where T : class

Type parameters

T

Parameters

thisValue JsValue

Returns

T

Exceptions

JsException
The value is not an object of the class (a TypeError).