Skip to content

Commit

Permalink
FEAT: allow custom mold callback for context handles to provide add…
Browse files Browse the repository at this point in the history
…itional handle's info, if wanted
  • Loading branch information
Oldes committed Sep 6, 2023
1 parent a74b261 commit d4878d9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/core/s-mold.c
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,18 @@ STOID Mold_Handle(REBVAL *value, REB_MOLD *mold)
if (IS_CONTEXT_HANDLE(value)) {
if (!IS_USED_HOB(VAL_HANDLE_CTX(value)))
Append_Bytes(mold->series, " unset!");
else {
REBCNT idx = VAL_HANDLE_CTX(value)->index;
REBHSP spec = PG_Handles[idx];
REBINT len;
if (spec.mold) {
len = spec.mold(VAL_HANDLE_CTX(value), BUF_PRINT);
if (len > 0) {
Append_Byte(mold->series, ' ');
Append_Bytes_Len(mold->series, STR_HEAD(BUF_PRINT), len);
}
}
}
}
Append_Byte(mold->series, ']');
}
Expand Down
3 changes: 2 additions & 1 deletion src/include/sys-value.h
Original file line number Diff line number Diff line change
Expand Up @@ -997,8 +997,8 @@ typedef void (*REBDOF)(REBVAL *ds); // DO evaltype dispatch function
typedef int (*REBPAF)(REBVAL *ds, REBVAL *p, REBCNT a); // Port action func

typedef int (*REB_HANDLE_FREE_FUNC)(void *hnd);
typedef int (*REB_HANDLE_MOLD_FUNC)(void *hnd, REBSER *ser);
typedef int (*REB_HANDLE_EVAL_PATH)(REBHOB *hob, REBCNT word, REBCNT *type, RXIARG *arg);
typedef REBSER* (*REB_HANDLE_MOLD_FUNC)(REBSER *mold, void *hnd); //TODO: not used yet!

typedef void (*ANYFUNC)(void *);
typedef void (*TRYFUNC)(void *);
Expand Down Expand Up @@ -1101,6 +1101,7 @@ typedef struct Reb_Handle_Spec {
REB_HANDLE_FREE_FUNC free;
REB_HANDLE_EVAL_PATH get_path;
REB_HANDLE_EVAL_PATH set_path;
REB_HANDLE_MOLD_FUNC mold;
} REBHSP;

typedef struct Reb_Handle_Context {
Expand Down
16 changes: 16 additions & 0 deletions src/os/host-ext-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,21 @@ int XTestContext_set_path(REBHOB *hob, REBCNT word, REBCNT *type, RXIARG *arg) {
return PE_OK;
}

int XTestContext_mold(REBHOB *hob, REBSER *str) {
int len;
XTEST* xtest = (XTEST*)hob->data;

if (!str || !xtest) return 0;

len = snprintf(
SERIES_DATA(str),
SERIES_REST(str),
"0#%lx id: %u", (unsigned long)hob->data, xtest->id
);
if (len > 0) SERIES_TAIL(str) += len;
return len;
}



void Init_Ext_Test(void)
Expand All @@ -510,6 +525,7 @@ void Init_Ext_Test(void)
spec.free = XTestContext_release;
spec.get_path = XTestContext_get_path;
spec.set_path = XTestContext_set_path;
spec.mold = XTestContext_mold;
Handle_XTest = RL_REGISTER_HANDLE_SPEC("XTEST", &spec);
}
#endif //TEST_EXTENSIONS

0 comments on commit d4878d9

Please sign in to comment.