-
Notifications
You must be signed in to change notification settings - Fork 0
/
testlib-nif.c
150 lines (125 loc) · 5.15 KB
/
testlib-nif.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// line #0
// proto void nothing();
// parsed: {'args_raw': None, 'modname': 'testlib', 'returns': 'void', 'func': 'nothing', 'args': [], 'arity': 0}
// line #1
// proto void nothing2(int a);
// parsed: {'args_raw': 'int a', 'modname': 'testlib', 'returns': 'void', 'func': 'nothing2', 'args': [{'access': '', 'type': 'int', 'argname': 'a', 'modifiers': '', 'position': 0}], 'arity': 1}
// line #2
// proto int add(int a, int b);
// parsed: {'args_raw': 'int a, int b', 'modname': 'testlib', 'returns': 'int', 'func': 'add', 'args': [{'access': '', 'type': 'int', 'argname': 'a', 'modifiers': '', 'position': 0}, {'access': '', 'type': 'int', 'argname': 'b', 'modifiers': '', 'position': 1}], 'arity': 2}
// line #3
// proto float addf(float a, float b);
// parsed: {'args_raw': 'float a, float b', 'modname': 'testlib', 'returns': 'float', 'func': 'addf', 'args': [{'access': '', 'type': 'float', 'argname': 'a', 'modifiers': '', 'position': 0}, {'access': '', 'type': 'float', 'argname': 'b', 'modifiers': '', 'position': 1}], 'arity': 2}
// line #4
// proto char* reverse(char* blah);
// parsed: {'args_raw': 'char* blah', 'modname': 'testlib', 'returns': 'char*', 'func': 'reverse', 'args': [{'access': '', 'type': 'char*', 'argname': 'blah', 'modifiers': '', 'position': 0}], 'arity': 1}
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include <esp_log.h>
#include <stdlib.h>
#include <defaultatoms.h>
#include <interop.h>
#include <nifs.h>
#include <port.h>
#include <term.h>
static term nif_testlib_nothing(Context *ctx, int argc, term argv[]) {
printf("entering testlib:nothing/%d", argc);
if (argc != 0) {
printf("testlib:nothing/%d - expected %d args, got %d", 0, 0, argc);
return BADARG_ATOM;
}
nothing();
return ATOM_OK;
}
static const struct Nif testlib_nothing_nif_info =
{
.base.type = NIFFunctionType,
.nif_ptr = nif_testlib_nothing
};
static term nif_testlib_nothing2(Context *ctx, int argc, term argv[]) {
printf("entering testlib:nothing2/%d", argc);
if (argc != 1) {
printf("testlib:nothing2/%d - expected %d args, got %d", 1, 1, argc);
return BADARG_ATOM;
}
int a = (int)term_to_int(argv[0]);
nothing2(a);
return ATOM_OK;
}
static const struct Nif testlib_nothing2_nif_info =
{
.base.type = NIFFunctionType,
.nif_ptr = nif_testlib_nothing2
};
static term nif_testlib_add(Context *ctx, int argc, term argv[]) {
printf("entering testlib:add/%d", argc);
if (argc != 2) {
printf("testlib:add/%d - expected %d args, got %d", 2, 2, argc);
return BADARG_ATOM;
}
int a = (int)term_to_int(argv[0]);
int b = (int)term_to_int(argv[1]);
int func_result = add(a, b);
term atom_val = term_from_int32(func_result);
VALIDATE_VALUE(atom_val, term_is_integer);
return atom_val;
}
static const struct Nif testlib_add_nif_info =
{
.base.type = NIFFunctionType,
.nif_ptr = nif_testlib_add
};
static term nif_testlib_addf(Context *ctx, int argc, term argv[]) {
printf("entering testlib:addf/%d", argc);
if (argc != 2) {
printf("testlib:addf/%d - expected %d args, got %d", 2, 2, argc);
return BADARG_ATOM;
}
float a = (float)term_to_float(argv[0]);
float b = (float)term_to_float(argv[1]);
float func_result = addf(a, b);
term atom_val = term_from_float(func_result);
VALIDATE_VALUE(atom_val, term_is_float);
return atom_val;
}
static const struct Nif testlib_addf_nif_info =
{
.base.type = NIFFunctionType,
.nif_ptr = nif_testlib_addf
};
static term nif_testlib_reverse(Context *ctx, int argc, term argv[]) {
printf("entering testlib:reverse/%d", argc);
if (argc != 1) {
printf("testlib:reverse/%d - expected %d args, got %d", 1, 1, argc);
return BADARG_ATOM;
}
char* blah = (char*)term_binary_data(argv[0]);
char* func_result = reverse(blah);
term atom_val = term_from_literal_binary(func_result, strlen(func_result));
VALIDATE_VALUE(atom_val, term_is_binary)
return atom_val;
}
static const struct Nif testlib_reverse_nif_info =
{
.base.type = NIFFunctionType,
.nif_ptr = nif_testlib_reverse
};
const struct Nif *testlib_get_nif(const char *nifname)
{
if (strcmp("testlib:nothing/0", nifname) == 0) {
return &testlib_nothing_nif_info;
}
if (strcmp("testlib:nothing2/1", nifname) == 0) {
return &testlib_nothing2_nif_info;
}
if (strcmp("testlib:add/2", nifname) == 0) {
return &testlib_add_nif_info;
}
if (strcmp("testlib:addf/2", nifname) == 0) {
return &testlib_addf_nif_info;
}
if (strcmp("testlib:reverse/1", nifname) == 0) {
return &testlib_reverse_nif_info;
}
return NULL;
}