Skip to content

Commit 7960a7d

Browse files
committed
Factorize mfa as a key logic
Signed-off-by: Paul Guyot <pguyot@kallisys.net>
1 parent f05c37e commit 7960a7d

File tree

6 files changed

+28
-47
lines changed

6 files changed

+28
-47
lines changed

src/libAtomVM/atom.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,19 +87,17 @@ static void utoa10(unsigned int int_value, char *integer_string)
8787
integer_string[integer_string_len] = '\0';
8888
}
8989

90-
void atom_write_mfa(char *buf, size_t buf_size, AtomString module, AtomString function, unsigned int arity)
90+
void atom_write_mfa(char *buf, size_t buf_size, size_t module_name_len, const void *module_data, size_t function_name_len, const void *function_data, unsigned int arity)
9191
{
92-
size_t module_name_len = atom_string_len(module);
93-
memcpy(buf, atom_string_data(module), module_name_len);
92+
memcpy(buf, module_data, module_name_len);
9493

9594
buf[module_name_len] = ':';
9695

97-
size_t function_name_len = atom_string_len(function);
9896
if (UNLIKELY((module_name_len + 1 + function_name_len + 1 + 4 + 1 > buf_size))) {
9997
fprintf(stderr, "Insufficient room to write mfa.\n");
10098
AVM_ABORT();
10199
}
102-
memcpy(buf + module_name_len + 1, atom_string_data(function), function_name_len);
100+
memcpy(buf + module_name_len + 1, function_data, function_name_len);
103101

104102
buf[module_name_len + function_name_len + 1] = '/';
105103
utoa10(arity, buf + module_name_len + 1 + function_name_len + 1);

src/libAtomVM/atom.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,13 @@ static inline const void *atom_string_data(AtomString atom_str)
9999
* buffer size.
100100
* @param buf the buffer to write into
101101
* @param buf_size the amount of room in the buffer
102-
* @param module the module name
103-
* @param function the function name
102+
* @param module_name_len length of the module name
103+
* @param module_data the module name
104+
* @param function_name_len length of the function name
105+
* @param function_data the function name
104106
* @param arity the function arity
105107
*/
106-
void atom_write_mfa(char *buf, size_t buf_size, AtomString module, AtomString function, unsigned int arity);
108+
void atom_write_mfa(char *buf, size_t buf_size, size_t module_name_len, const void *module_data, size_t function_name_len, const void *function_data, unsigned int arity);
107109

108110
#ifdef __cplusplus
109111
}

src/libAtomVM/bif.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,9 @@
5757
RAISE_ERROR_BIF(fail_label, BADARG_ATOM); \
5858
}
5959

60-
const struct ExportedFunction *bif_registry_get_handler(AtomString module, AtomString function, int arity)
60+
const struct ExportedFunction *bif_registry_get_handler(const char *mfa)
6161
{
62-
char bifname[MAX_BIF_NAME_LEN];
63-
64-
atom_write_mfa(bifname, MAX_BIF_NAME_LEN, module, function, arity);
65-
const BifNameAndPtr *nameAndPtr = in_word_set(bifname, strlen(bifname));
62+
const BifNameAndPtr *nameAndPtr = in_word_set(mfa, strlen(mfa));
6663
if (!nameAndPtr) {
6764
return NULL;
6865
}

src/libAtomVM/bif.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ extern "C" {
3939

4040
#define MAX_BIF_NAME_LEN 260
4141

42-
const struct ExportedFunction *bif_registry_get_handler(AtomString module, AtomString function, int arity);
42+
const struct ExportedFunction *bif_registry_get_handler(const char *mfa);
4343

4444
term bif_erlang_self_0(Context *ctx);
4545
term bif_erlang_byte_size_1(Context *ctx, uint32_t fail_label, int live, term arg1);

src/libAtomVM/nifs.c

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858
#include "unicode.h"
5959
#include "utils.h"
6060

61-
#define MAX_NIF_NAME_LEN 260
6261
#define FLOAT_BUF_SIZE 64
6362

6463
#define RAISE(a, b) \
@@ -873,30 +872,11 @@ DEFINE_MATH_NIF(tanh)
873872
#include "nifs_hash.h"
874873
#pragma GCC diagnostic pop
875874

876-
const struct Nif *nifs_get(AtomString module, AtomString function, int arity)
875+
const struct Nif *nifs_get(const char *mfa)
877876
{
878-
char nifname[MAX_NIF_NAME_LEN];
879-
880-
int module_name_len = atom_string_len(module);
881-
memcpy(nifname, atom_string_data(module), module_name_len);
882-
883-
nifname[module_name_len] = ':';
884-
885-
int function_name_len = atom_string_len(function);
886-
if (UNLIKELY((arity > 9) || (module_name_len + function_name_len + 4 > MAX_NIF_NAME_LEN))) {
887-
// In AtomVM NIFs are limited to 9 parameters
888-
return NULL;
889-
}
890-
memcpy(nifname + module_name_len + 1, atom_string_data(function), function_name_len);
891-
892-
//TODO: handle NIFs with more than 9 parameters
893-
nifname[module_name_len + function_name_len + 1] = '/';
894-
nifname[module_name_len + function_name_len + 2] = '0' + arity;
895-
nifname[module_name_len + function_name_len + 3] = 0;
896-
897-
const NifNameAndNifPtr *nameAndPtr = nif_in_word_set(nifname, strlen(nifname));
877+
const NifNameAndNifPtr *nameAndPtr = nif_in_word_set(mfa, strlen(mfa));
898878
if (!nameAndPtr) {
899-
return platform_nifs_get_nif(nifname);
879+
return platform_nifs_get_nif(mfa);
900880
}
901881

902882
return nameAndPtr->nif;
@@ -3476,16 +3456,20 @@ static term nif_erlang_function_exported(Context *ctx, int argc, term argv[])
34763456
VALIDATE_VALUE(function, term_is_atom);
34773457
VALIDATE_VALUE(arity_term, term_is_integer);
34783458

3479-
AtomString module_name = globalcontext_atomstring_from_term(ctx->global, module);
3480-
AtomString function_name = globalcontext_atomstring_from_term(ctx->global, function);
3459+
AtomIndex module_name = term_to_atom_index(module);
3460+
AtomIndex function_name = term_to_atom_index(function);
3461+
34813462
avm_int_t arity = term_to_int(arity_term);
34823463

3483-
const struct ExportedFunction *bif = bif_registry_get_handler(module_name, function_name, arity);
3464+
char mfa[MAX_MFA_NAME_LEN];
3465+
atom_table_write_mfa(ctx->global->atom_table, mfa, sizeof(mfa), module_name, function_name, arity);
3466+
3467+
const struct ExportedFunction *bif = bif_registry_get_handler(mfa);
34843468
if (bif) {
34853469
return TRUE_ATOM;
34863470
}
34873471

3488-
struct Nif *nif = (struct Nif *) nifs_get(module_name, function_name, arity);
3472+
struct Nif *nif = (struct Nif *) nifs_get(mfa);
34893473
if (nif) {
34903474
return TRUE_ATOM;
34913475
}
@@ -3495,7 +3479,7 @@ static term nif_erlang_function_exported(Context *ctx, int argc, term argv[])
34953479
return FALSE_ATOM;
34963480
}
34973481

3498-
int target_label = module_search_exported_function(target_module, function_name, arity, ctx->global);
3482+
int target_label = module_search_exported_function(target_module, function_name, arity);
34993483
if (target_label == 0) {
35003484
return FALSE_ATOM;
35013485
}
@@ -3625,20 +3609,20 @@ static term nif_erlang_fun_info_2(Context *ctx, int argc, term argv[])
36253609
switch (key) {
36263610
case MODULE_ATOM: {
36273611
term module_name;
3628-
term_get_function_mfa(fun, &module_name, NULL, NULL, ctx->global);
3612+
term_get_function_mfa(fun, &module_name, NULL, NULL);
36293613
value = module_name;
36303614
break;
36313615
}
36323616
case NAME_ATOM: {
36333617
term function_name;
3634-
term_get_function_mfa(fun, NULL, &function_name, NULL, ctx->global);
3618+
term_get_function_mfa(fun, NULL, &function_name, NULL);
36353619
value = function_name;
36363620
break;
36373621
}
36383622

36393623
case ARITY_ATOM: {
36403624
term arity;
3641-
term_get_function_mfa(fun, NULL, NULL, &arity, ctx->global);
3625+
term_get_function_mfa(fun, NULL, NULL, &arity);
36423626
value = arity;
36433627
break;
36443628
}
@@ -3977,7 +3961,7 @@ static term nif_erlang_get_module_info(Context *ctx, int argc, term argv[])
39773961
if (UNLIKELY(memory_ensure_free(ctx, info_size) != MEMORY_GC_OK)) {
39783962
RAISE_ERROR(OUT_OF_MEMORY_ATOM);
39793963
}
3980-
term exports = module_get_exported_functions(target_module, &ctx->heap, ctx->global);
3964+
term exports = module_get_exported_functions(target_module, &ctx->heap);
39813965
if (argc == 2) {
39823966
return exports;
39833967
}

src/libAtomVM/nifs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ extern "C" {
4646
ctx->x[1] = (error_type_atom); \
4747
return term_invalid_term();
4848

49-
const struct Nif *nifs_get(AtomString module, AtomString function, int arity);
49+
const struct Nif *nifs_get(const char *mfa);
5050

5151
#ifdef __cplusplus
5252
}

0 commit comments

Comments
 (0)