Skip to content

Commit

Permalink
add support for most "primitives"
Browse files Browse the repository at this point in the history
  • Loading branch information
littledivy committed Oct 13, 2021
1 parent a7edacb commit 8df0790
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 57 deletions.
1 change: 0 additions & 1 deletion cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ async function generate() {
return;
}

console.log(conf);
const pkgName = conf.name;

source = "// Auto-generated with deno_bindgen\n";
Expand Down
45 changes: 6 additions & 39 deletions codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,37 +43,6 @@ type Options = {
le?: boolean;
};

function createByteTypeImport(le?: boolean) {
// Endianess dependent types to be imported from the `byte_type` module.
let types = [
"i16",
"u16",
"i32",
"u32",
"i64",
"u64",
"f32",
"f64",
];

// Finalize type name based on endianness.
const typeImports = types.map((ty) => ty + (le ? "le" : "be"));

// TODO(@littledivy): version imports
let code = `import { Struct, i8, u8, ${
typeImports.join(", ")
} } from "https://deno.land/x/byte_type/mod.ts";\n`;

code += types.map((ty, idx) => `const ${ty} = ${typeImports[idx]};`).join(
"\n",
);

code += `\nconst usize = u64;\n`;
code += `const isize = i64;\n`;

return code;
}

export function codegen(
target: string,
decl: TypeDef,
Expand Down Expand Up @@ -114,16 +83,14 @@ ${
`a${i}: ${resolveType(decl, p)}`
).join(",")
}) {
${
${
signature[sig].parameters.map((p, i) =>
`${
typeof p !== "string"
? `const a${i}_buf = encode(JSON.stringify(a${i}));\n`
: ""
}`
).join("")
typeof p !== "string"
? `const a${i}_buf = encode(JSON.stringify(a${i}));`
: null
).filter((c) => c !== null).join("\n")
}
return _lib.symbols.${sig}(${
return _lib.symbols.${sig}(${
signature[sig].parameters.map((p, i) =>
typeof p !== "string" ? `a${i}_buf, a${i}_buf.byteLength` : `a${i}`
).join(", ")
Expand Down
24 changes: 10 additions & 14 deletions example/bindings/bindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,38 @@

const encode = (s: string) => new TextEncoder().encode(s);
const _lib = Deno.dlopen("target/debug/libadd.so", {
test_serde: { parameters: ["buffer", "usize"], result: "i32" },
test_mixed: { parameters: ["i32", "buffer", "usize"], result: "i32" },
test_mixed: { parameters: ["isize", "buffer", "usize"], result: "i32" },
add2: { parameters: ["buffer", "usize"], result: "i32" },
add: { parameters: ["i32", "i32"], result: "i32" },
test_mixed_order: {
parameters: ["i32", "buffer", "usize", "i32"],
result: "i32",
},
add: { parameters: ["i32", "i32"], result: "i32" },
test_serde: { parameters: ["buffer", "usize"], result: "u8" },
});
type MyStruct = { arr: any };
type Input = { b: number; a: number };
export function test_serde(a0: MyStruct) {
const a0_buf = encode(JSON.stringify(a0));

return _lib.symbols.test_serde(a0_buf, a0_buf.byteLength) as number;
}
export function test_mixed(a0: number, a1: Input) {
const a1_buf = encode(JSON.stringify(a1));

return _lib.symbols.test_mixed(a0, a1_buf, a1_buf.byteLength) as number;
}
export function add2(a0: Input) {
const a0_buf = encode(JSON.stringify(a0));

return _lib.symbols.add2(a0_buf, a0_buf.byteLength) as number;
}
export function add(a0: number, a1: number) {
return _lib.symbols.add(a0, a1) as number;
}
export function test_mixed_order(a0: number, a1: Input, a2: number) {
const a1_buf = encode(JSON.stringify(a1));

return _lib.symbols.test_mixed_order(
a0,
a1_buf,
a1_buf.byteLength,
a2,
) as number;
}
export function add(a0: number, a1: number) {
return _lib.symbols.add(a0, a1) as number;
}
export function test_serde(a0: MyStruct) {
const a0_buf = encode(JSON.stringify(a0));
return _lib.symbols.test_serde(a0_buf, a0_buf.byteLength) as number;
}
4 changes: 2 additions & 2 deletions example/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn add2(input: Input) -> i32 {
// Test mixed types

#[deno_bindgen]
fn test_mixed(a: i32, b: Input) -> i32 {
fn test_mixed(a: isize, b: Input) -> i32 {
a as i32 + b.a
}

Expand All @@ -40,7 +40,7 @@ struct MyStruct {
}

#[deno_bindgen]
fn test_serde(s: MyStruct) -> i32 {
fn test_serde(s: MyStruct) -> u8 {
if s.arr.contains(&"WORKS".to_string()) {
return 1;
}
Expand Down
24 changes: 23 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::env;
use std::fs::OpenOptions;
use std::io::Read;
use std::io::Write;
use syn::parse_quote;
use syn::Data;
use syn::DataStruct;
use syn::Fields;
Expand Down Expand Up @@ -49,6 +50,26 @@ enum Type {
// { len: usize }
}

impl From<Type> for syn::Type {
fn from(ty: Type) -> Self {
match ty {
Type::I8 => parse_quote! { i8 },
Type::U8 => parse_quote! { u8 },
Type::I16 => parse_quote! { i16 },
Type::U16 => parse_quote! { u16 },
Type::I32 => parse_quote! { i32 },
Type::U32 => parse_quote! { u32 },
Type::I64 => parse_quote! { i64 },
Type::U64 => parse_quote! { u64 },
Type::F32 => parse_quote! { f32 },
Type::F64 => parse_quote! { f64 },
Type::Usize => parse_quote! { usize },
Type::Isize => parse_quote! { isize },
_ => panic!("Unreachable"),
}
}
}

#[derive(Serialize, Deserialize, Clone)]
struct Symbol {
parameters: Vec<Type>,
Expand Down Expand Up @@ -141,7 +162,8 @@ pub fn deno_bindgen(_attr: TokenStream, input: TokenStream) -> TokenStream {
// TODO
_ => {
let ident = format_ident!("arg{}", c_index.to_string());
params.push(quote! { #ident: i32 });
let ty = syn::Type::from(parameter);
params.push(quote! { #ident: #ty });
input_idents.push(ident);
}
};
Expand Down

0 comments on commit 8df0790

Please sign in to comment.