deno_bindgen icon indicating copy to clipboard operation
deno_bindgen copied to clipboard

Accessing structs from C to Deno

Open walmartwarlord opened this issue 2 years ago • 1 comments

Hello, I know this is a rust based codegen but I hope it's ok to ask this. What could be the best way to access structs that contains strings in Deno from C? Is it advisable just to stringify the object in C so that I can do new Deno.UnsafePointerView(ptr).getCString() in Deno?

Example

typedef struct { char name; int age; } SampleObject;

walmartwarlord avatar Apr 14 '22 00:04 walmartwarlord

Consider using https://deno.land/x/byte_type for that.

import { Struct, u8, isize } from "https://deno.land/x/byte_type/ffi.ts";

const SampleObject = new Struct({
  name: u8,
  age: isize,
});

const ptr = lib.symbols.asd();
const view = new Deno.UnsafePointerView(ptr);
const object = SampleObject.read(view);

littledivy avatar May 18 '22 02:05 littledivy

Closing this. Using byte_type works .

walmartwarlord avatar Aug 01 '22 16:08 walmartwarlord