JSClass constructor

JSClass(
  1. JSContext ctx, {
  2. required JSClassDef classDef,
  3. List<JSCFunctionListEntry>? funcs,
  4. JSValue? constructor,
  5. bool autoDispose = true,
})

Implementation

JSClass(
  this.ctx, {
  required this.classDef,
  this.funcs,
  JSValue? constructor,
  bool autoDispose = true,
}) {
  _classID = calloc.call(sizeOf<Uint32>());
  assert(_classID != null);

  classID = newClassID(_classID!);
  final rt = ctx.runtime.ref;
  JS_NewClass(rt, _classID!.value, classDef.ref);

  proto = JSValue.obj(ctx);
  ctx.setClassProto(_classID!.value, proto!);
  if (funcs != null && funcs!.isNotEmpty) {
    proto!.setPropertyFunctionList(funcs!);
  }

  if (constructor != null) {
    JS_SetConstructor(ctx.ref, constructor.ref.ref, proto!.ref.ref);

    /// ctx.globalObject.definePropertyValueStr('xxx', proto) not worked!
    /// must use this
    /// constructor.setPropertyFunctionList() // must use JSCFunctionListEntry.defPropXXX
    /// ctx.globalObject.definePropertyValueStr('xxx', constructor)
  }
  attach(calloc.nativeFree, _classID!.cast(), autoDispose: autoDispose);
}