function pointers and callbacks

This commit is contained in:
2026-06-15 21:09:46 +02:00
parent 3b7c3fcbd0
commit f5605fd3ec
21 changed files with 1927 additions and 122 deletions
+10
View File
@@ -4,12 +4,22 @@ pass_alias :: func(value native.imported_int_alias) native.imported_int {
return value
}
double_value :: c_func(value c_int) c_int {
return value + value
}
call_mapper :: func(mapper native.Imported_Mapper) c_int {
return mapper?(21)
}
main :: func() void {
_ = native.imported_add(pass_alias(20), 22)
_ = native.imported_scalar(3, 4)
_ = native.child_value(7)
_ = native.imported_read(native.imported_handle()?)
_ = native.imported_string("hello")
_ = native.imported_apply(double_value, 21)
_ = call_mapper(double_value)
_ = native.configured_value(9)
signed i8 :: -2
unsigned u16 :: 3
+30
View File
@@ -7,12 +7,35 @@ typedef int imported_int;
typedef imported_int imported_int_alias;
typedef struct Imported_Handle Imported_Handle;
typedef void (*Imported_Callback)(int value);
typedef int (*Imported_Mapper)(int value);
typedef struct Imported_Value {
int value;
} Imported_Value;
typedef union Imported_Union {
int value;
} Imported_Union;
typedef struct Imported_Bitfield {
unsigned value : 1;
} Imported_Bitfield;
typedef struct __attribute__((packed)) Imported_Packed {
char first;
int second;
} Imported_Packed;
typedef struct Imported_Flexible {
int count;
int values[];
} Imported_Flexible;
typedef struct Imported_Qualified {
const int value;
} Imported_Qualified;
typedef struct Imported_Overaligned {
_Alignas(16) int value;
} Imported_Overaligned;
typedef struct Imported_Anonymous {
struct {
int value;
};
} Imported_Anonymous;
typedef enum Imported_Enum {
IMPORTED_ENUM_VALUE,
} Imported_Enum;
@@ -22,6 +45,7 @@ unsigned long imported_scalar(unsigned char value, unsigned long extra);
Imported_Handle *imported_handle(void);
int imported_read(const Imported_Handle *handle);
int imported_string(const char *value);
int imported_apply(Imported_Mapper mapper, int value);
Imported_Value imported_by_value(Imported_Value value);
int imported_volatile(volatile int *value);
_Bool imported_bool(_Bool value);
@@ -37,5 +61,11 @@ int configured_value(int value);
#define IMPORTED_MACRO 42
int imported_variadic(int marker, ...);
Imported_Bitfield imported_bitfield(Imported_Bitfield value);
Imported_Packed imported_packed(Imported_Packed value);
Imported_Flexible imported_flexible(Imported_Flexible value);
Imported_Qualified imported_qualified(Imported_Qualified value);
Imported_Overaligned imported_overaligned(Imported_Overaligned value);
Imported_Anonymous imported_anonymous(Imported_Anonymous value);
#endif
+11
View File
@@ -36,6 +36,17 @@ int imported_string(const char *value) {
return 0;
}
int imported_apply(Imported_Mapper mapper, int value) {
if (mapper == NULL) {
abort();
}
int result = mapper(value);
if (result != value * 2) {
abort();
}
return result;
}
int imported_variadic(int marker, ...) {
va_list args;
va_start(args, marker);
@@ -3,10 +3,17 @@ native :: import "../header/include/native.h"
use_callback :: c_func(value native.Imported_Callback) void
use_union :: c_func(value native.Imported_Union) void
use_enum :: c_func(value native.Imported_Enum) void
use_opaque :: c_func(value native.Imported_Handle) void
main :: func() void {
_ = native.IMPORTED_MACRO
_ = native.imported_by_value()
_ = native.imported_bitfield()
_ = native.imported_packed()
_ = native.imported_flexible()
_ = native.imported_qualified()
_ = native.imported_overaligned()
_ = native.imported_anonymous()
_ = native.imported_volatile()
_ = native.imported_bool()
_ = native.imported_global
+46
View File
@@ -0,0 +1,46 @@
native :: import "../include/native.h"
Manual :: c_struct {
value c_int
}
mirror_manual :: c_func(value Manual) Manual {
return value
}
mirror_large :: c_func(value native.Large) native.Large {
return value
}
native_pair :: func(value native.Pair) native.Pair {
return value
}
global_pair native.Pair :: native.Pair { left = 1, right = 2 }
main :: func() i32 {
pair native.Pair = native_pair(global_pair)
pair.left = 10
pair = native.echo_pair(pair)
pairs [1]native.Pair :: [pair]
_ = native_pair(pairs[0])
triple native.Triple :: native.echo_triple(native.Triple { first = 3, second = 4, third = 5 })
floats native.Floats :: native.echo_floats(native.Floats { x = 6.0, y = 7.0 })
large native.Large :: mirror_large(native.echo_large(native.Large { values = [8, 9, 10] }))
nested native.Nested :: native.echo_nested(native.Nested {
pair = native.Pair { left = 11, right = 12 },
tail = 13,
})
arrays native.Arrays :: native.echo_arrays(native.Arrays { values = [14, 15, 16] })
choice native.Choice = native.Choice { decimal = 1.0 }
choice.integer = 17
choice = native.echo_choice(choice)
forward native.Forward :: native.echo_forward(native.Forward { value = 19 })
manual Manual :: mirror_manual(Manual { value = 18 })
_ = manual.value
_ = forward.value
_ = native.verify_records(pair, triple, floats, large, nested, arrays, choice)
return 1
}
+53
View File
@@ -0,0 +1,53 @@
#ifndef BROLANG_RECORDS_H
#define BROLANG_RECORDS_H
typedef struct Pair {
int left;
int right;
} Pair;
typedef struct Triple {
int first;
int second;
int third;
} Triple;
typedef struct Floats {
float x;
float y;
} Floats;
typedef struct Large {
long values[3];
} Large;
typedef struct Nested {
Pair pair;
int tail;
} Nested;
typedef struct Arrays {
int values[3];
} Arrays;
typedef union Choice {
long integer;
double decimal;
} Choice;
typedef struct Forward Forward;
struct Forward {
int value;
};
Pair echo_pair(Pair value);
Triple echo_triple(Triple value);
Floats echo_floats(Floats value);
Large echo_large(Large value);
Nested echo_nested(Nested value);
Arrays echo_arrays(Arrays value);
Choice echo_choice(Choice value);
Forward echo_forward(Forward value);
int verify_records(Pair pair, Triple triple, Floats floats, Large large, Nested nested, Arrays arrays, Choice choice);
#endif
+24
View File
@@ -0,0 +1,24 @@
#include "include/native.h"
#include <stdlib.h>
Pair echo_pair(Pair value) { return value; }
Triple echo_triple(Triple value) { return value; }
Floats echo_floats(Floats value) { return value; }
Large echo_large(Large value) { return value; }
Nested echo_nested(Nested value) { return value; }
Arrays echo_arrays(Arrays value) { return value; }
Choice echo_choice(Choice value) { return value; }
Forward echo_forward(Forward value) { return value; }
int verify_records(Pair pair, Triple triple, Floats floats, Large large, Nested nested, Arrays arrays, Choice choice) {
if (!(pair.left == 10 && pair.right == 2 &&
triple.first == 3 && triple.second == 4 && triple.third == 5 &&
floats.x == 6.0f && floats.y == 7.0f &&
large.values[0] == 8 && large.values[1] == 9 && large.values[2] == 10 &&
nested.pair.left == 11 && nested.pair.right == 12 && nested.tail == 13 &&
arrays.values[0] == 14 && arrays.values[1] == 15 && arrays.values[2] == 16 &&
choice.integer == 17)) {
abort();
}
return 1;
}