Start to implement Clownfish methods for C bindings
Project: http://git-wip-us.apache.org/repos/asf/lucy/repo Commit: http://git-wip-us.apache.org/repos/asf/lucy/commit/40257360 Tree: http://git-wip-us.apache.org/repos/asf/lucy/tree/40257360 Diff: http://git-wip-us.apache.org/repos/asf/lucy/diff/40257360 Branch: refs/heads/master Commit: 40257360526a0bd149dba2c5b0e49b6e049b0975 Parents: 01106c0 Author: Nick Wellnhofer <[email protected]> Authored: Sun Nov 25 18:01:52 2012 +0100 Committer: Nick Wellnhofer <[email protected]> Committed: Sat Mar 9 17:51:54 2013 +0100 ---------------------------------------------------------------------- c/src/CFBind.h | 1 + c/src/Clownfish/Err.c | 81 +++++++++++++++++++++++-------- c/src/Clownfish/LockFreeRegistry.c | 8 ++- c/src/Clownfish/Obj.c | 40 ++++++++++----- c/src/Clownfish/VTable.c | 40 +++++++++------- 5 files changed, 117 insertions(+), 53 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucy/blob/40257360/c/src/CFBind.h ---------------------------------------------------------------------- diff --git a/c/src/CFBind.h b/c/src/CFBind.h index f8443cd..31e57b0 100644 --- a/c/src/CFBind.h +++ b/c/src/CFBind.h @@ -40,6 +40,7 @@ extern "C" { */ #define THROW CFISH_THROW #define WARN CFISH_WARN +#define UNUSED_VAR CHY_UNUSED_VAR #define UNREACHABLE_RETURN CHY_UNREACHABLE_RETURN #ifdef __cplusplus http://git-wip-us.apache.org/repos/asf/lucy/blob/40257360/c/src/Clownfish/Err.c ---------------------------------------------------------------------- diff --git a/c/src/Clownfish/Err.c b/c/src/Clownfish/Err.c index b2de599..fa85b82 100644 --- a/c/src/Clownfish/Err.c +++ b/c/src/Clownfish/Err.c @@ -14,47 +14,88 @@ * limitations under the License. */ -#include "CFBind.h" +#define CHY_USE_SHORT_NAMES +#define LUCY_USE_SHORT_NAMES + +#include <setjmp.h> +#include <stdio.h> +#include <stdlib.h> + +#include "Clownfish/Err.h" +#include "Clownfish/CharBuf.h" +#include "Clownfish/VTable.h" + +/* TODO: Thread safety */ +static Err *current_error; +static Err *thrown_error; +static jmp_buf *current_env; void -lucy_Err_init_class(void) { +Err_init_class(void) { } -lucy_Err* -lucy_Err_get_error() { - THROW(LUCY_ERR, "TODO"); - UNREACHABLE_RETURN(lucy_Err*); +Err* +Err_get_error() { + return current_error; } void -lucy_Err_set_error(lucy_Err *error) { - THROW(LUCY_ERR, "TODO"); +Err_set_error(Err *error) { + if (current_error) { + DECREF(current_error); + } + current_error = error; } void -lucy_Err_do_throw(lucy_Err *err) { - THROW(LUCY_ERR, "TODO"); +Err_do_throw(Err *error) { + if (current_env) { + thrown_error = error; + longjmp(*current_env, 1); + } + else { + CharBuf *message = Err_get_mess(error); + fprintf(stderr, "%s", CB_Get_Ptr8(message)); + exit(EXIT_FAILURE); + } } void* -lucy_Err_to_host(lucy_Err *self) { - THROW(LUCY_ERR, "TODO"); +Err_to_host(Err *self) { + THROW(ERR, "TODO"); UNREACHABLE_RETURN(void*); } void -lucy_Err_throw_mess(lucy_VTable *vtable, lucy_CharBuf *message) { - THROW(LUCY_ERR, "TODO"); +Err_throw_mess(VTable *vtable, CharBuf *message) { + Err_Make_t make + = METHOD_PTR(CERTIFY(vtable, VTABLE), Lucy_Err_Make); + Err *err = (Err*)CERTIFY(make(NULL), ERR); + Err_Cat_Mess(err, message); + DECREF(message); + Err_do_throw(err); } void -lucy_Err_warn_mess(lucy_CharBuf *message) { - THROW(LUCY_ERR, "TODO"); +Err_warn_mess(CharBuf *message) { + fprintf(stderr, "%s", CB_Get_Ptr8(message)); + DECREF(message); } -lucy_Err* -lucy_Err_trap(Cfish_Err_Attempt_t routine, void *context) { - THROW(LUCY_ERR, "TODO"); - UNREACHABLE_RETURN(lucy_Err*); +Err* +Err_trap(Err_Attempt_t routine, void *context) { + jmp_buf env; + jmp_buf *prev_env = current_env; + current_env = &env; + + if (!setjmp(env)) { + routine(context); + } + + current_env = prev_env; + + Err *error = thrown_error; + thrown_error = NULL; + return error; } http://git-wip-us.apache.org/repos/asf/lucy/blob/40257360/c/src/Clownfish/LockFreeRegistry.c ---------------------------------------------------------------------- diff --git a/c/src/Clownfish/LockFreeRegistry.c b/c/src/Clownfish/LockFreeRegistry.c index 1f208c4..d6350c4 100644 --- a/c/src/Clownfish/LockFreeRegistry.c +++ b/c/src/Clownfish/LockFreeRegistry.c @@ -15,13 +15,15 @@ */ #define C_LUCY_LOCKFREEREGISTRY +#define CHY_USE_SHORT_NAMES +#define LUCY_USE_SHORT_NAMES -#include "CFBind.h" #include "Clownfish/LockFreeRegistry.h" +#include "Clownfish/Err.h" void* -lucy_LFReg_to_host(lucy_LockFreeRegistry *self) { - THROW(LUCY_ERR, "TODO"); +LFReg_to_host(LockFreeRegistry *self) { + THROW(ERR, "TODO"); UNREACHABLE_RETURN(void*); } http://git-wip-us.apache.org/repos/asf/lucy/blob/40257360/c/src/Clownfish/Obj.c ---------------------------------------------------------------------- diff --git a/c/src/Clownfish/Obj.c b/c/src/Clownfish/Obj.c index e529513..17fa4ab 100644 --- a/c/src/Clownfish/Obj.c +++ b/c/src/Clownfish/Obj.c @@ -15,30 +15,44 @@ */ #define C_LUCY_OBJ +#define CHY_USE_SHORT_NAMES +#define LUCY_USE_SHORT_NAMES -#include "CFBind.h" +#include "Clownfish/Obj.h" +#include "Clownfish/Err.h" uint32_t -lucy_Obj_get_refcount(lucy_Obj *self) { - THROW(LUCY_ERR, "TODO"); - UNREACHABLE_RETURN(uint32_t); +Obj_get_refcount(Obj *self) { + return self->ref.count; } -lucy_Obj* -lucy_Obj_inc_refcount(lucy_Obj *self) { - THROW(LUCY_ERR, "TODO"); - UNREACHABLE_RETURN(lucy_Obj*); +Obj* +Obj_inc_refcount(Obj *self) { + self->ref.count++; + return self; } uint32_t -lucy_Obj_dec_refcount(lucy_Obj *self) { - THROW(LUCY_ERR, "TODO"); - UNREACHABLE_RETURN(uint32_t); +Obj_dec_refcount(Obj *self) { + uint32_t modified_refcount = INT32_MAX; + switch (self->ref.count) { + case 0: + THROW(ERR, "Illegal refcount of 0"); + break; // useless + case 1: + modified_refcount = 0; + Obj_Destroy(self); + break; + default: + modified_refcount = --self->ref.count; + break; + } + return modified_refcount; } void* -lucy_Obj_to_host(lucy_Obj *self) { - THROW(LUCY_ERR, "TODO"); +Obj_to_host(Obj *self) { + THROW(ERR, "TODO"); UNREACHABLE_RETURN(void*); } http://git-wip-us.apache.org/repos/asf/lucy/blob/40257360/c/src/Clownfish/VTable.c ---------------------------------------------------------------------- diff --git a/c/src/Clownfish/VTable.c b/c/src/Clownfish/VTable.c index 00c9901..42d7f7e 100644 --- a/c/src/Clownfish/VTable.c +++ b/c/src/Clownfish/VTable.c @@ -14,37 +14,43 @@ * limitations under the License. */ +#define CHY_USE_SHORT_NAMES +#define LUCY_USE_SHORT_NAMES #define C_LUCY_OBJ #define C_LUCY_VTABLE -#include "CFBind.h" +#include "Clownfish/VTable.h" +#include "Clownfish/CharBuf.h" +#include "Clownfish/Err.h" +#include "Clownfish/VArray.h" -lucy_Obj* -lucy_VTable_foster_obj(lucy_VTable *self, void *host_obj) { - THROW(LUCY_ERR, "TODO"); - UNREACHABLE_RETURN(lucy_Obj*); +Obj* +VTable_foster_obj(VTable *self, void *host_obj) { + THROW(ERR, "TODO"); + UNREACHABLE_RETURN(Obj*); } void -lucy_VTable_register_with_host(lucy_VTable *singleton, lucy_VTable *parent) { - THROW(LUCY_ERR, "TODO"); +VTable_register_with_host(VTable *singleton, VTable *parent) { + UNUSED_VAR(singleton); + UNUSED_VAR(parent); } -lucy_VArray* -lucy_VTable_fresh_host_methods(const lucy_CharBuf *class_name) { - THROW(LUCY_ERR, "TODO"); - UNREACHABLE_RETURN(lucy_VArray*); +VArray* +VTable_fresh_host_methods(const CharBuf *class_name) { + UNUSED_VAR(class_name); + return VA_new(0); } -lucy_CharBuf* -lucy_VTable_find_parent_class(const lucy_CharBuf *class_name) { - THROW(LUCY_ERR, "TODO"); - UNREACHABLE_RETURN(lucy_CharBuf*); +CharBuf* +VTable_find_parent_class(const CharBuf *class_name) { + THROW(ERR, "TODO"); + UNREACHABLE_RETURN(CharBuf*); } void* -lucy_VTable_to_host(lucy_VTable *self) { - THROW(LUCY_ERR, "TODO"); +VTable_to_host(VTable *self) { + THROW(ERR, "TODO"); UNREACHABLE_RETURN(void*); }
