Skip to content

Commit

Permalink
Update for pure-c/purec#60
Browse files Browse the repository at this point in the history
  • Loading branch information
Felix Schlitter committed Nov 13, 2019
1 parent 42373ba commit 2137008
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 22 deletions.
14 changes: 14 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
default: test
.PHONY: default

include $(PUREC_DIR)/mk/target.mk

main: .spago

main_CFLAGS = -g
main_LD_FLAGS =

$(eval $(call purs_mk_target,main,Test.Main,src test))

test: main
valgrind --track-origins=yes --leak-check=full ./main.out
5 changes: 5 additions & 0 deletions spago.dhall
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{ name = "st"
, dependencies = ["effect", "prelude", "assert"]
, packages = ../purec--master/package-sets/packages.dhall
, sources = [ "src/**/*.purs", "test/**/*.purs" ]
}
68 changes: 46 additions & 22 deletions src/Effect/Ref.c
Original file line number Diff line number Diff line change
@@ -1,24 +1,48 @@
#include <purescript.h>

PURS_FFI_FUNC_2(Effect_Ref_new, val, _, {
return (const purs_any_t *)purs_any_foreign_new(NULL, (void *) val);
});

PURS_FFI_FUNC_2(Effect_Ref_read, _ref, _, {
return purs_any_get_foreign(_ref)->data;
});

PURS_FFI_FUNC_3(Effect_Ref_modify$, f, _ref, _, {
purs_foreign_t * ref = (purs_foreign_t *)purs_any_get_foreign(_ref);
const purs_record_t * res = purs_any_get_record(purs_any_app(f, (const purs_any_t *)ref->data));
const purs_record_t * state = purs_record_find_by_key(res, "state");
const purs_record_t * value = purs_record_find_by_key(res, "value");
ref->data = (purs_any_t *)state->value;
return value -> value;
});

PURS_FFI_FUNC_3(Effect_Ref_write, val, _ref, _, {
purs_foreign_t * ref = (purs_foreign_t *)purs_any_get_foreign(_ref);
ref->data = (purs_any_t *)val;
return (const purs_any_t *) NULL;
});
static void release_val(void* tag, void* data) {
purs_any_t *v = data;
PURS_ANY_RELEASE(*v);
free(v);
}

PURS_FFI_FUNC_2(Effect_Ref_new, val, _) {
PURS_ANY_RETAIN(val);
purs_any_t* v = purs_malloc(sizeof(purs_any_t));
memcpy(v, &val, sizeof(purs_any_t));
const purs_foreign_t* foreign = purs_foreign_new(NULL, v, release_val);
return purs_any_foreign(foreign);
}

PURS_FFI_FUNC_2(Effect_Ref_read, _ref, _) {
return *(purs_any_t*)purs_any_get_foreign(_ref)->data;
}

PURS_FFI_FUNC_3(Effect_Ref_modify$, f, _ref, _) {
const purs_foreign_t* foreign = purs_any_force_foreign(_ref);

purs_any_t _result = purs_any_app(f, *(purs_any_t*)foreign->data);
const purs_record_t * x = purs_any_force_record(_result);

purs_any_t * state = purs_record_find_by_key(x, "state");
purs_any_t value = *purs_record_find_by_key(x, "value");

PURS_ANY_RELEASE(*(purs_any_t*)foreign->data);
PURS_ANY_RETAIN(*state);
memcpy(foreign->data, state, sizeof(purs_any_t));

PURS_RC_RELEASE(x);
PURS_ANY_RELEASE(_result);
PURS_RC_RELEASE(foreign);

return value;
}

PURS_FFI_FUNC_3(Effect_Ref_write, val, _ref, _) {
const purs_foreign_t* foreign = purs_any_force_foreign(_ref);
PURS_ANY_RELEASE(*(purs_any_t*)foreign->data);
PURS_ANY_RETAIN(val);
memcpy(foreign->data, &val, sizeof(purs_any_t));
PURS_RC_RELEASE(foreign);
return purs_any_null;
}

0 comments on commit 2137008

Please sign in to comment.