denoland / deno_bindgen

Write high-level Deno FFI libraries in Rust.
MIT License
287 stars 28 forks source link

Accessing structs from C to Deno #69

Closed walmartwarlord closed 2 years ago

walmartwarlord commented 2 years ago

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;
littledivy commented 2 years ago

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);
walmartwarlord commented 2 years ago

Closing this. Using byte_type works .