Skip to content

Commit ab4a8d8

Browse files
committed
term.c: add term_fprint function
Add term_fprint function as a replacement, that doesn't require a Context, for term_display. term_display is reimplemented on top of it. Signed-off-by: Davide Bettio <[email protected]>
1 parent afea246 commit ab4a8d8

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

src/libAtomVM/globalcontext.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ typedef struct Module Module;
5858

5959
struct Module;
6060

61-
typedef struct
61+
struct GlobalContext
6262
{
6363
struct ListHead ready_processes;
6464
struct ListHead waiting_processes;
@@ -83,7 +83,7 @@ typedef struct
8383
uint64_t ref_ticks;
8484

8585
void *platform_data;
86-
} GlobalContext;
86+
};
8787

8888
/**
8989
* @brief Creates a new GlobalContext

src/libAtomVM/term.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,16 @@
3333
const term empty_tuple = 0;
3434

3535
void term_display(FILE *fd, term t, const Context *ctx)
36+
{
37+
term_fprint(fd, t, ctx->global);
38+
}
39+
40+
void term_fprint(FILE *fd, term t, const GlobalContext *global)
3641
{
3742
if (term_is_atom(t)) {
3843
int atom_index = term_to_atom_index(t);
39-
AtomString atom_string = (AtomString) valueshashtable_get_value(ctx->global->atoms_ids_table, atom_index, (unsigned long) NULL);
44+
AtomString atom_string = (AtomString) valueshashtable_get_value(
45+
global->atoms_ids_table, atom_index, (unsigned long) NULL);
4046
fprintf(fd, "%.*s", (int) atom_string_len(atom_string), (char *) atom_string_data(atom_string));
4147

4248
} else if (term_is_integer(t)) {
@@ -79,12 +85,12 @@ void term_display(FILE *fd, term t, const Context *ctx)
7985
display_separator = 1;
8086
}
8187

82-
term_display(fd, term_get_list_head(t), ctx);
88+
term_fprint(fd, term_get_list_head(t), global);
8389
t = term_get_list_tail(t);
8490
}
8591
if (!term_is_nil(t)) {
8692
fputc('|', fd);
87-
term_display(fd, t, ctx);
93+
term_fprint(fd, t, global);
8894
}
8995
fputc(']', fd);
9096
}
@@ -111,7 +117,7 @@ void term_display(FILE *fd, term t, const Context *ctx)
111117
if (i != 0) {
112118
fputc(',', fd);
113119
}
114-
term_display(fd, term_get_tuple_element(t, i), ctx);
120+
term_fprint(fd, term_get_tuple_element(t, i), global);
115121
}
116122

117123
fputc('}', fd);
@@ -124,9 +130,9 @@ void term_display(FILE *fd, term t, const Context *ctx)
124130
if (i != 0) {
125131
fputc(',', fd);
126132
}
127-
term_display(fd, term_get_map_key(t, i), ctx);
133+
term_fprint(fd, term_get_map_key(t, i), global);
128134
fprintf(fd, "=>");
129-
term_display(fd, term_get_map_value(t, i), ctx);
135+
term_fprint(fd, term_get_map_value(t, i), global);
130136
}
131137

132138
fputc('}', fd);

src/libAtomVM/term.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ extern "C" {
8686

8787
#define TERM_FROM_ATOM_INDEX(atom_index) ((atom_index << 6) | 0xB)
8888

89+
#ifndef TYPEDEF_GLOBALCONTEXT
90+
#define TYPEDEF_GLOBALCONTEXT
91+
typedef struct GlobalContext GlobalContext;
92+
#endif
93+
8994
enum RefcBinaryFlags
9095
{
9196
RefcNoFlags = 0,
@@ -1392,12 +1397,23 @@ static inline int term_is_number(term t)
13921397
/**
13931398
* @brief Prints a term to stdout
13941399
*
1395-
* @details Print any given term to the standard output.
1400+
* @details Print any term to the given file.
1401+
* @param fd the file where the term will be printed.
13961402
* @param t the term that will be printed.
13971403
* @param ctx the context.
13981404
*/
13991405
void term_display(FILE *fd, term t, const Context *ctx);
14001406

1407+
/**
1408+
* @brief Prints a term to the given file
1409+
*
1410+
* @details Print any given term to the given file.
1411+
* @param fd the file where the term will be printed.
1412+
* @param t the term that will be printed.
1413+
* @param global the \c GlobalContext.
1414+
*/
1415+
void term_fprint(FILE *fd, term t, const GlobalContext *global);
1416+
14011417
/**
14021418
* @brief Checks if a term is a string (i.e., a list of characters)
14031419
*

0 commit comments

Comments
 (0)