From 2137008b72943a1555a04ce6ec547cfe2a87d2f5 Mon Sep 17 00:00:00 2001 From: Felix Schlitter Date: Thu, 14 Nov 2019 09:54:52 +1300 Subject: [PATCH] Update for pure-c/purec#60 --- Makefile | 14 ++++++++++ spago.dhall | 5 ++++ src/Effect/Ref.c | 68 ++++++++++++++++++++++++++++++++---------------- 3 files changed, 65 insertions(+), 22 deletions(-) create mode 100644 Makefile create mode 100644 spago.dhall diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..a7d343a --- /dev/null +++ b/Makefile @@ -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 diff --git a/spago.dhall b/spago.dhall new file mode 100644 index 0000000..4c2f102 --- /dev/null +++ b/spago.dhall @@ -0,0 +1,5 @@ +{ name = "st" +, dependencies = ["effect", "prelude", "assert"] +, packages = ../purec--master/package-sets/packages.dhall +, sources = [ "src/**/*.purs", "test/**/*.purs" ] +} diff --git a/src/Effect/Ref.c b/src/Effect/Ref.c index 671a6dc..bb9f505 100644 --- a/src/Effect/Ref.c +++ b/src/Effect/Ref.c @@ -1,24 +1,48 @@ #include -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; +}