Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions hs-bindgen/blocktest/c/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
libblocktest: iterator.o
clang -Wall -shared -lBlocksRuntime -o libblocktest.so iterator.o

iterator.o:
clang -Wall -std=c23 -fblocks -fPIC -c -o iterator.o iterator.c

.PHONY: clean
clean:
rm -f *.o *.so
64 changes: 64 additions & 0 deletions hs-bindgen/blocktest/c/iterator.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include "Block.h"

#include "iterator.h"

// Toggle

Toggle makeToggle(bool start) {
__block int b = start;

return Block_copy( ^(void) {
bool ret = b;
b = !b;
return ret;
});
}

bool toggleNext(Toggle block) {
return block();
}

void releaseToggle(Toggle block) {
Block_release(block);
}

// Counter

Counter makeCounter(int start, int increment) {
__block int i = start; // __block storage makes it assignable

return Block_copy( ^(void) {
int ret = i;
i += increment;
return ret;
});
}

int counterNext(Counter block) {
return block();
}

void releaseCounter(Counter block) {
Block_release(block);
}

// Variable increment

VarCounter makeVarCounter(int start) {
__block int i = start; // __block storage makes it assignable

return Block_copy( ^(int increment) {
int ret = i;
i += increment;
return ret;
});
}

int varCounterNext(VarCounter block, int increment) {
return block(increment);
}

void releaseVarCounter(VarCounter block) {
Block_release(block);
}

20 changes: 20 additions & 0 deletions hs-bindgen/blocktest/c/iterator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Toggle between true and false

typedef bool (^Toggle)();
Toggle makeToggle(bool start);
bool toggleNext(Toggle block);
void releaseToggle(Toggle block);

// Simple counter, fixed increment

typedef int(^Counter)();
Counter makeCounter(int start, int increment);
int counterNext(Counter block);
void releaseCounter(Counter block);

// Counter with variable increment

typedef int(^VarCounter)(int increment);
VarCounter makeVarCounter(int start);
int varCounterNext(VarCounter block, int increment);
void releaseVarCounter(VarCounter block);
10 changes: 10 additions & 0 deletions hs-bindgen/blocktest/generate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

cabal run -- \
hs-bindgen-cli preprocess \
-I c \
-o hs/generated/Iterator.hs \
--module Iterator \
--standard c23 \
--enable-blocks \
iterator.h
29 changes: 29 additions & 0 deletions hs-bindgen/blocktest/hs/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Copyright (c) 2024-2025, Well-Typed LLP and Anduril Industries Inc.


Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.

* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
21 changes: 21 additions & 0 deletions hs-bindgen/blocktest/hs/app/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Main where

import Control.Exception
import Control.Monad

import Iterator

main :: IO ()
main = do
bracket (makeToggle 0) releaseToggle $ \toggle ->
replicateM_ 5 $
print =<< toggleNext toggle

bracket (makeCounter 5 2) releaseCounter $ \counter ->
replicateM_ 5 $
print =<< counterNext counter

bracket (makeVarCounter 5) releaseVarCounter $ \varCounter ->
forM_ [0 .. 4] $ \i ->
print =<< varCounterNext varCounter i

24 changes: 24 additions & 0 deletions hs-bindgen/blocktest/hs/blocktest.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
cabal-version: 3.0
name: blocktest
version: 0.1.0
license: BSD-3-Clause
license-file: LICENSE
author: Edsko de Vries
maintainer: [email protected]
build-type: Simple

common lang
build-depends: base >= 4.17 && < 5
default-language: GHC2021
ghc-options: -Wall

executable blocktest
import: lang
main-is: Main.hs
hs-source-dirs: app, generated
other-modules: Iterator
extra-libraries: blocktest
build-depends: hs-bindgen-runtime
ghc-options: -pgmc clang -optc -std=c23 -optc -fblocks
-pgma clang

1 change: 1 addition & 0 deletions hs-bindgen/blocktest/hs/cabal.project
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
packages: ., ../../../hs-bindgen-runtime
1 change: 1 addition & 0 deletions hs-bindgen/blocktest/hs/generated/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.hs
Loading