Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate Types #5

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Conversation

bconnorwhite
Copy link

@bconnorwhite bconnorwhite commented Oct 15, 2023

This uses tree sitter to generate types for the Rust loader.

The nice thing about tree sitter is that there are parsers for most languages, so it should be fairly easy to add new type mappings.

Unfortunately, there's an open issue in Bun that prevents tree sitter from working with Bun, so I'm spawning a node process instead to do the actual parsing. This would be pretty easy to move back to Bun though once the issue is fixed.


Here's an example of how it works:

import { CString, JSCallback, ptr } from "bun:ffi";
import { add, call, echo, hello, mul, not, sub } from "./math.rs";

console.log(add(3, 2));
console.log(sub(3, 2));
console.log(mul(3.2, 2.1));
console.log(not(true));

hello();

echo(ptr(Buffer.from("hello from JS\0", "utf-8")));

const callback = new JSCallback(
  (ptr, length) => /Hello/.test(new CString(ptr, length)),
  {
    returns: "bool",
    args: ["ptr", "usize"]
  }
);

const result = call(callback);
console.log(result);

Rust:

use std::os::raw::c_char;

#[no_mangle]
pub extern "C" fn add(a: u32, b: u32) -> u32 {
    a + b
}

#[no_mangle]
pub extern "C" fn sub(a: i32, b: i32) -> i32 {
    a - b
}

#[no_mangle]
pub extern "C" fn mul(a: f32, b: f32) -> f32 {
    a * b
}

#[no_mangle]
pub extern "C" fn not(a: bool) -> bool {
    !a
}

#[no_mangle]
pub extern "C" fn hello() -> () {
    println!("Hello, world!");
}

#[no_mangle]
pub extern "C" fn echo(c_string: *const c_char) -> () {
    // Convert the C string to a Rust string for use within Rust
    let rust_str = unsafe {
        std::ffi::CStr::from_ptr(c_string).to_str().unwrap()
    };
    println!("{}", rust_str);
}

#[no_mangle]
pub extern "C" fn call(callback: extern fn(*const u8, usize) -> bool) -> bool {
    const string: &str = "Hello, world!";
    callback(string.as_ptr(), string.len())
}

The resulting config.ts:

import { LoaderConfig, T } from "hyperimport";
export default {
	buildCommand: ["rustc","--crate-type","cdylib","<path>/src/math.rs","--out-dir","build/math.rs"],
	outDir: "build/math.rs",
	symbols: {
		add: {
			args: [T.u32, T.u32],
			returns: T.u32
		},
		call: {
			args: [T.function],
			returns: T.bool
		},
		echo: {
			args: [T.ptr],
			returns: T.void
		},
		hello: {
			args: [],
			returns: T.void
		},
		mul: {
			args: [T.f32, T.f32],
			returns: T.f32
		},
		not: {
			args: [T.bool],
			returns: T.bool
		},
		sub: {
			args: [T.i32, T.i32],
			returns: T.i32
		},
	}
} satisfies LoaderConfig.Main;

@@ -77,6 +81,7 @@ export default class {
const lmfile = `${this.cwd}/@types/${basename(this.config.importPath)}/lastModified`;
if (lm !== await Bun.file(lmfile).text()) {
await this.build();
await this.initConfigTypes();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should maybe be in a separate PR, it just regenerates the config types if the source has changed

// Use Node to run tree-sitter due to Bun issue:
// https://github.com/oven-sh/bun/issues/4188
const types = Bun.spawnSync([
"node",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of node, tree-sitter provides wasm bindings which can be used https://github.com/tree-sitter/tree-sitter/blob/master/lib/binding_web/README.md

@@ -25,6 +25,10 @@
"url": "https://github.com/tr1ckydev/hyperimport/issues"
},
"homepage": "https://github.com/tr1ckydev/hyperimport#readme",
"dependencies": {
"tree-sitter": "^0.20.5",
"tree-sitter-rust": "^0.20.4"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should probably conditionally import per-lang packages instead of including this in deps

@@ -0,0 +1,70 @@
const fs = require("fs");
const Parser = require("tree-sitter");
const Rust = require("tree-sitter-rust");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

conditional import + try to load for all the loaders specified in bunfig.toml

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants