From f98ce04c2c4b99c17eb4f2c9fd7376db41cff63f Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Wed, 11 Oct 2023 20:46:30 -0500 Subject: [PATCH] bugfix: `cbindgen` and captures by pointer --- compiler/src/checker.c | 4 +++ core/onyx/cbindgen.onyx | 72 +++++++++++++++++++-------------------- tests/lazy_iterators.onyx | 2 +- 3 files changed, 41 insertions(+), 37 deletions(-) diff --git a/compiler/src/checker.c b/compiler/src/checker.c index 01f4fc28..496bff56 100644 --- a/compiler/src/checker.c +++ b/compiler/src/checker.c @@ -2442,6 +2442,10 @@ CheckStatus check_expression(AstTyped** pexpr) { ERROR_(cl->token->pos, "Cannot pass '%b' by pointer because it is not an l-value.", cl->token->text, cl->token->length); } + if (cl->captured_value->kind == Ast_Kind_Local) { + cl->captured_value->flags |= Ast_Flag_Address_Taken; + } + expr->type = type_make_pointer(context.ast_alloc, cl->captured_value->type); } else { diff --git a/core/onyx/cbindgen.onyx b/core/onyx/cbindgen.onyx index b351145a..b2678a88 100644 --- a/core/onyx/cbindgen.onyx +++ b/core/onyx/cbindgen.onyx @@ -293,26 +293,26 @@ compile_c_file :: ( param_info := get_type_info(t); switch param_info.kind { - case .Basic do return switch t { - case bool => "i32"; - case i8 => "i32"; - case u8 => "i32"; - case i16 => "i32"; - case u16 => "i32"; - case i32 => "i32"; - case u32 => "i32"; - case i64 => "i64"; - case u64 => "i64"; - - case f32 => "f32"; - case f64 => "f64"; - - case rawptr => "ptr"; + case .Basic do switch t { + case bool do return "i32"; + case i8 do return "i32"; + case u8 do return "i32"; + case i16 do return "i32"; + case u16 do return "i32"; + case i32 do return "i32"; + case u32 do return "i32"; + case i64 do return "i64"; + case u64 do return "i64"; + + case f32 do return "f32"; + case f64 do return "f64"; + + case rawptr do return "ptr"; - case i8x16, i16x8, i32x4, i64x2, f32x4, f64x2, v128 => "v128"; + case i8x16, i16x8, i32x4, i64x2, f32x4, f64x2, v128 do return "v128"; - case type_expr => "i32"; - }; + case type_expr do return "i32"; + } case .Pointer do return "ptr"; case .Multi_Pointer do return "ptr"; @@ -350,26 +350,26 @@ compile_c_file :: ( param_info := get_type_info(t); switch param_info.kind { - case .Basic do return switch t { - case bool => "WASM_I32"; - case i8 => "WASM_I32"; - case u8 => "WASM_I32"; - case i16 => "WASM_I32"; - case u16 => "WASM_I32"; - case i32 => "WASM_I32"; - case u32 => "WASM_I32"; - case i64 => "WASM_I64"; - case u64 => "WASM_I64"; - - case f32 => "WASM_F32"; - case f64 => "WASM_F64"; - - case rawptr => "WASM_I32"; // This will have to depend on the pointer size... + case .Basic do switch t { + case bool do return "WASM_I32"; + case i8 do return "WASM_I32"; + case u8 do return "WASM_I32"; + case i16 do return "WASM_I32"; + case u16 do return "WASM_I32"; + case i32 do return "WASM_I32"; + case u32 do return "WASM_I32"; + case i64 do return "WASM_I64"; + case u64 do return "WASM_I64"; + + case f32 do return "WASM_F32"; + case f64 do return "WASM_F64"; + + case rawptr do return "WASM_I32"; // This will have to depend on the pointer size... - case i8x16, i16x8, i32x4, i64x2, f32x4, f64x2, v128 => "WASM_V128"; + case i8x16, i16x8, i32x4, i64x2, f32x4, f64x2, v128 do return "WASM_V128"; - case type_expr => "WASM_I32"; - }; + case type_expr do return "WASM_I32"; + } case .Pointer do return "WASM_I32"; // This will also have to depend on the pointer size... case .Multi_Pointer do return "WASM_I32"; // This will also have to depend on the pointer size... diff --git a/tests/lazy_iterators.onyx b/tests/lazy_iterators.onyx index 3b7d4c5d..953f9737 100644 --- a/tests/lazy_iterators.onyx +++ b/tests/lazy_iterators.onyx @@ -32,7 +32,7 @@ main :: (args: [] cstr) { count_iterator(1.0f, 20.0f) |> iter.map(x => x * 2) |> iter.filter(lower_bound, (x, l) => x > ~~l) - |> iter.map(addition, (x, addition) => x + ~~addition) + |> iter.map((x, [addition]) => x + ~~addition) |> iter.take(5); for v: quick_iterator { -- 2.25.1