JSClassDef.alloc constructor

JSClassDef.alloc(
  1. JSContext _ctx,
  2. String className, {
  3. JSClassFinalizer? finalizer,
  4. JSClassGCMark? gc_mark,
  5. JSClassCall? call,
  6. JSClassExoticMethodsRef? exotic,
  7. bool autoDispose = true,
})

Implementation

JSClassDef.alloc(
  this._ctx,
  this.className, {
  JSClassFinalizer? finalizer,
  JSClassGCMark? gc_mark,
  JSClassCall? call,
  JSClassExoticMethodsRef? exotic,
  bool autoDispose = true,
}) {
  _finalizerNC = NativeCallable.listener((JSRuntimeRef rt, JSValue_ val) {
    return finalizer?.call(JSRuntime(rt), JSValue.ptr(_ctx, val));
  });
  _gc_markNC = NativeCallable.listener((
    JSRuntimeRef rt,
    JSValue_ val,
    Pointer<NativeFunction<JS_MarkFunc_>> mark_func,
  ) {
    // TODO: transfer native callback into dart function
    return gc_mark?.call(JSRuntime(rt), JSValue.ptr(_ctx, val), mark_func);
  });
  _callNC = NativeCallable.isolateLocal((
    Pointer<JSContext_> ctx,
    JSValue_ func_obj,
    JSValue_ this_val,
    int argc,
    Pointer<JSValue_> argv,
    int flags,
  ) {
    return call
            ?.call(
              JSContext(ctx),
              JSValue.ptr(_ctx, func_obj),
              JSValue.ptr(_ctx, this_val),
              argv.toList(_ctx, argc),
              flags,
            )
            .ref
            .ref ??
        JSValue.makeUninitialized(_ctx).ref.ref;
  });
  final ptr =
      calloc.call<JSClassDef_>(sizeOf<JSClassDef_>())
        ..ref.class_name = className.toNativeUtf8()
        ..ref.call = _callNC?.nativeFunction ?? nullptr
        ..ref.finalizer = _finalizerNC?.nativeFunction ?? nullptr
        ..ref.gc_mark = _gc_markNC?.nativeFunction ?? nullptr
        ..ref.exotic = exotic ?? nullptr;
  _ref = ptr;
  attach(calloc.nativeFree, _ref.cast(), autoDispose: autoDispose);
}