From 8df079077d4fc1e5f20bea400b9ddb03963d7cdb Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Wed, 13 Oct 2021 14:57:53 +0530 Subject: [PATCH] add support for most "primitives" --- cli.ts | 1 - codegen.ts | 45 +++++------------------------------- example/bindings/bindings.ts | 24 ++++++++----------- example/src/lib.rs | 4 ++-- src/lib.rs | 24 ++++++++++++++++++- 5 files changed, 41 insertions(+), 57 deletions(-) diff --git a/cli.ts b/cli.ts index d66fe2f..838b11a 100644 --- a/cli.ts +++ b/cli.ts @@ -31,7 +31,6 @@ async function generate() { return; } - console.log(conf); const pkgName = conf.name; source = "// Auto-generated with deno_bindgen\n"; diff --git a/codegen.ts b/codegen.ts index b933934..11293a3 100644 --- a/codegen.ts +++ b/codegen.ts @@ -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, @@ -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(", ") diff --git a/example/bindings/bindings.ts b/example/bindings/bindings.ts index 39ff36b..c065933 100644 --- a/example/bindings/bindings.ts +++ b/example/bindings/bindings.ts @@ -2,38 +2,27 @@ 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, @@ -41,3 +30,10 @@ export function test_mixed_order(a0: number, a1: Input, a2: number) { 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; +} diff --git a/example/src/lib.rs b/example/src/lib.rs index 3aa38c1..dc4e3ab 100644 --- a/example/src/lib.rs +++ b/example/src/lib.rs @@ -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 } @@ -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; } diff --git a/src/lib.rs b/src/lib.rs index fb17a5e..3ac8454 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; @@ -49,6 +50,26 @@ enum Type { // { len: usize } } +impl From 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, @@ -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); } };