denoland / deno

A modern runtime for JavaScript and TypeScript.
https://deno.com
MIT License
98.19k stars 5.41k forks source link

ffi: expose Deno.PointerObject #19683

Open sigmaSd opened 1 year ago

sigmaSd commented 1 year ago

I have a function like this

export function decode<T>(ptr: Deno.PointerValue): T {
  if (!ptr) throw new Error("tried to decode null ptr: " + ptr);
  // ptr is a cstring
  const cstr = new Deno.UnsafePointerView(ptr).getCString();
  return JSON.parse(cstr);
}

but I want to hoist the ptr check outside of the function, but PointerObject is not exposed

// deno-lint-ignore no-explicit-any
export function decode<T>(ptr: any /*Deno.PointerObject*/): T {
  // ptr is a cstring
  const cstr = new Deno.UnsafePointerView(ptr).getCString();
  return JSON.parse(cstr);
}
aapoalas commented 1 year ago

Yeah, I was just thinking about this today. As a temporary workaround you can get to the type via NonNullable