JSCFunctionType.defGeneric constructor

JSCFunctionType.defGeneric(
  1. JSContext _ctx, {
  2. JSCFunction? generic,
  3. bool autoDispose = true,
})

Implementation

JSCFunctionType.defGeneric(
  this._ctx, {
  JSCFunction? generic,
  bool autoDispose = true,
}) {
  _cfuncNC = NativeCallable.isolateLocal((
    Pointer<JSContext_> ctx,
    JSValue_ this_val,
    int argc,
    Pointer<JSValue_> argv,
  ) {
    JSValue_ result;
    if (generic == null) {
      _jsValueRef = calloc.call<JSValue_>(sizeOf<JSValue_>());
      result = _jsValueRef!.ref;
    } else {
      result =
          generic
              .call(
                JSContext(ctx),
                JSValue.ptr(_ctx, this_val),
                argv.toList(_ctx, argc),
              )
              .ref
              .ref;
    }
    try {
      return result;
    } finally {
      if (_jsValueRef != null && _jsValueRef != nullptr) {
        calloc.free(_jsValueRef!);
      }
    }
  });
  _ref = calloc.call<JSCFunctionType_>(sizeOf<JSCFunctionType_>())
    ..ref.generic = _cfuncNC!.nativeFunction;
  attach(calloc.nativeFree, _ref.cast(), autoDispose: autoDispose);
}