-
Notifications
You must be signed in to change notification settings - Fork 10
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,8 +46,12 @@ export default class { | |
Bun.write(`${this.cwd}/@types/${filename}/lastModified`, lastModified(this.config.importPath)); | ||
const configWriter = Bun.file(`${this.cwd}/@types/${filename}/config.ts`).writer(); | ||
configWriter.write(`import { LoaderConfig, T } from "hyperimport";\nexport default {\n\tbuildCommand: ${JSON.stringify(this.config.buildCommand)},\n\toutDir: "${this.config.outDir}",\n\tsymbols: {`); | ||
for (const symbol of nm(this.config.libPath)) { | ||
configWriter.write(`\n\t\t${symbol}: {\n\t\t\targs: [],\n\t\t\treturns: T.void\n\t\t},`); | ||
const symbols = nm(this.config.libPath); | ||
const types = this._config.parseTypes?.(this.config.importPath, symbols); | ||
for (const symbol of symbols) { | ||
const type = types?.[symbol] ?? { args: [], returns: "T.void" }; | ||
const args = type.args.join(", "); | ||
configWriter.write(`\n\t\t${symbol}: {\n\t\t\targs: [${args}],\n\t\t\treturns: ${type.returns}\n\t\t},`); | ||
} | ||
configWriter.write(`\n\t}\n} satisfies LoaderConfig.Main;`); | ||
configWriter.end(); | ||
|
@@ -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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
Bun.write(lmfile, lm); | ||
} | ||
} | ||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { basename, join } from "path"; | ||
import Loader from "../../loader"; | ||
|
||
export default class extends Loader { | ||
constructor() { | ||
super("Rust Loader", | ||
{ | ||
extension: "rs", | ||
buildCommand: (importPath, outDir) => [ | ||
"rustc", | ||
"--crate-type", | ||
"cdylib", | ||
importPath, | ||
"--out-dir", | ||
outDir | ||
], | ||
outDir: importPath => `build/${basename(importPath)}`, | ||
parseTypes: (importPath, targets) => { | ||
// Use Node to run tree-sitter due to Bun issue: | ||
// https://github.com/oven-sh/bun/issues/4188 | ||
const types = Bun.spawnSync([ | ||
"node", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
join(import.meta.dir, "parse.js"), | ||
importPath, | ||
...targets | ||
]).stdout.toString(); | ||
try { | ||
const result = JSON.parse(types); | ||
return result; | ||
} catch(e) { | ||
return undefined; | ||
} | ||
} | ||
} | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
const fs = require("fs"); | ||
const arch = require("arch"); | ||
const Parser = require("tree-sitter"); | ||
const Rust = require("tree-sitter-rust"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
const path = process.argv[2]; | ||
const targets = process.argv.slice(3); | ||
|
||
const parser = new Parser(); | ||
parser.setLanguage(Rust); | ||
|
||
const source = fs.readFileSync(path, "utf8"); | ||
const tree = parser.parse(source); | ||
|
||
const size = arch() === "x64" ? "64" : "32"; | ||
|
||
const typeMap = { | ||
"()": "T.void", | ||
"bool": "T.bool", | ||
"u8": "T.u8", | ||
"u16": "T.u16", | ||
"u32": "T.u32", | ||
"u64": "T.u64", | ||
"i8": "T.i8", | ||
"i16": "T.i16", | ||
"i32": "T.i32", | ||
"i64": "T.i64", | ||
"f32": "T.f32", | ||
"f64": "T.f64", | ||
"usize": `T.u${size}`, | ||
"isize": `T.i${size}`, | ||
"char": "T.u32" | ||
}; | ||
|
||
function mapType(type) { | ||
if(typeMap[type]) { | ||
return typeMap[type]; | ||
} | ||
return type; | ||
} | ||
|
||
const types = tree.rootNode.children.reduce((acc, node) => { | ||
if(node.type === "function_item") { | ||
const fnNameNode = node.children.find(child => child.type === "identifier"); | ||
if(fnNameNode && targets.includes(fnNameNode.text)) { | ||
const parameters = node.children.find(child => child.type === "parameters"); | ||
const parameterTypes = parameters.children | ||
.filter((child) => child.type === "parameter") | ||
.map((child) => { | ||
const primitiveType = child.children.find((child) => child.type === "primitive_type"); | ||
if(primitiveType) { | ||
return mapType(primitiveType.text); | ||
} | ||
const pointerType = child.children.find((child) => child.type === "pointer_type"); | ||
if(pointerType) { | ||
return "T.ptr"; | ||
} | ||
const functionType = child.children.find((child) => child.type === "function_type"); | ||
if(functionType) { | ||
return "T.function"; | ||
} | ||
}); | ||
const returnType = node.children.find(child => child.type === "primitive_type" || child.type === "unit_type").text; | ||
acc[fnNameNode.text] = { | ||
args: parameterTypes, | ||
returns: mapType(returnType) | ||
}; | ||
} | ||
} | ||
return acc; | ||
}, {}); | ||
|
||
process.stdout.write(JSON.stringify(types)); |
There was a problem hiding this comment.
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