JSValue.alloc constructor

JSValue.alloc(
  1. JSContext ctx,
  2. JSTag tag, {
  3. int? int32,
  4. double? float64,
  5. Uint8List? list,
  6. Pointer<Void>? ptr,
  7. bool autoDispose = true,
})

Implementation

factory JSValue.alloc(
  JSContext ctx,
  JSTag tag, {
  int? int32,
  double? float64,
  Uint8List? list,
  Pointer<Void>? ptr,
  bool autoDispose = true,
}) {
  final u = calloc.call<JSValueUnion_>(sizeOf<JSValueUnion_>());
  final v = calloc.call<JSValue_>(sizeOf<JSValue_>());
  v.ref.tag = tag.value;
  if (int32 != null) {
    u.ref.int32 = int32;
  } else if (float64 != null) {
    u.ref.float64 = float64;
  } else if (list != null) {
    // TODO: Pointer<Void> -> Uint8List
    u.ref.ptr = list.toCAny();
  } else if (ptr != null) {
    u.ref.ptr = ptr;
  } else {
    throw Exception('invalid argument: u is JSValueUnion sturct');
  }
  v.ref.u = u.ref;
  return JSValue(ctx, v, autoDispose: autoDispose);
}