forked from wavedrom/vcd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vcd.c
202 lines (170 loc) · 5.18 KB
/
vcd.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#define NAPI_VERSION 1
#include <stdio.h>
#include <stdlib.h>
#include "vcd_parser.h"
#include <node_api.h>
#define DECLARE_NAPI_METHOD(name, func) { \
napi_property_descriptor desc = { name, 0, func, 0, 0, 0, napi_default, 0 }; \
if (napi_define_properties(env, exports, 1, &desc) != napi_ok) { \
napi_throw_error(env, 0, "Error"); \
} \
}
#define METHOD(name) napi_value name(napi_env env, napi_callback_info cbInfo)
#define ASSERT(val, expr) \
if (expr != napi_ok) { \
napi_throw(env, val); \
}
#define ASSERT_ARGC(count) \
napi_value args[count]; \
{ \
size_t argc = count; \
if (napi_get_cb_info(env, cbInfo, &argc, args, 0, 0) != napi_ok) { \
napi_throw_error(env, 0, "Error"); \
return 0; \
} \
if (argc < count) { \
napi_throw_type_error(env, 0, "Wrong number of arguments"); \
return 0; \
} \
}
#define ASSERT_BUFFER(name, var, len) \
const char * var; \
size_t len; \
{ \
bool result; \
if(napi_is_buffer(env, name, &result) != napi_ok) { \
napi_throw(env, name); \
return 0; \
} \
if (napi_get_buffer_info(env, name, (void **)&var, &len) != napi_ok) { \
napi_throw(env, name); \
return 0; \
} \
}
#define ASSERT_STRING(name, var) \
{ \
napi_value tmp; \
if (napi_coerce_to_string(env, name, &tmp) != napi_ok) { \
napi_throw(env, name); \
return 0; \
} \
size_t result; \
if (napi_get_value_string_latin1(env, tmp, var, 4096, &result) != napi_ok) { \
napi_throw(env, name); \
return 0; \
} \
}
#define ASSERT_EXTERNAL(name, var) { \
napi_valuetype valuetype; \
if (napi_typeof(env, name, &valuetype) != napi_ok) { \
napi_throw(env, name); \
return 0; \
} \
if (valuetype != napi_external) { \
napi_throw_type_error(env, 0, "Wrong arguments"); \
return 0; \
} \
if (napi_get_value_external(env, name, (void **)(&var)) != napi_ok) { \
napi_throw(env, name); \
return 0; \
} \
}
#define ASSERT_FUNCTION(name, var) { \
napi_valuetype valuetype; \
if (napi_typeof(env, name, &valuetype) != napi_ok) { \
napi_throw(env, name); \
return 0; \
} \
if (valuetype != napi_function) { \
napi_throw_type_error(env, 0, "Wrong arguments"); \
return 0; \
} \
var = name; \
}
#define ASSERT_OBJECT(name, var) { \
napi_valuetype valuetype; \
if (napi_typeof(env, name, &valuetype) != napi_ok) { \
napi_throw(env, name); \
return 0; \
} \
if (valuetype != napi_object) { \
napi_throw_type_error(env, 0, "Wrong arguments"); \
return 0; \
} \
var = name; \
}
METHOD(init) {
napi_value res;
struct vcd_parser_s *state = malloc(sizeof *state);
const int32_t error = vcd_parser_init(state);
if (error) {
ASSERT(res, napi_create_int32(env, error, &res))
return res;
}
ASSERT_ARGC(3)
ASSERT_FUNCTION(args[0], state->lifee)
ASSERT_FUNCTION(args[1], state->triee)
ASSERT_OBJECT(args[2], state->info)
static char triggerString [4096] = " ";
static char tmpStr [4096] = " ";
state->trigger = triggerString;
state->reason = "NO REASON";
state->napi_env = env;
state->tmpStr = tmpStr;
napi_value status;
ASSERT(status, napi_create_string_latin1(env, "declaration", NAPI_AUTO_LENGTH, &status))
ASSERT(state->info, napi_set_named_property(env, state->info, "status", status))
// ASSERT(status, napi_get_named_property(env, state->info, "wires", &state->wires))
// napi_value hierObj;
// ASSERT(hierObj, napi_create_object(env, &hierObj))
// state->info = hierObj;
// ASSERT(state->info, napi_create_object(env, &state->info))
ASSERT(res, napi_create_external(env, state, 0, 0, &res))
return res;
}
METHOD(done) {
ASSERT_ARGC(4)
struct vcd_parser_s *state;
// last use of all external objects
napi_value lifee, triee, info;
ASSERT_EXTERNAL(args[0], state)
ASSERT_FUNCTION(args[1], lifee)
ASSERT_FUNCTION(args[2], triee)
ASSERT_OBJECT(args[3], info)
// FIXME destroy parser state
const int32_t error = 0;
napi_value res;
ASSERT(res, napi_create_int32(env, error, &res))
return res;
}
METHOD(execute) {
ASSERT_ARGC(5)
struct vcd_parser_s *state;
ASSERT_EXTERNAL(args[0], state)
ASSERT_FUNCTION(args[1], state->lifee)
ASSERT_FUNCTION(args[2], state->triee)
ASSERT_OBJECT(args[3], state->info)
ASSERT_BUFFER(args[4], p, plen)
state->napi_env = env;
const int32_t error = vcd_parser_execute(state, p, p + plen);
napi_value res;
ASSERT(res, napi_create_int32(env, error, &res))
return res;
}
METHOD(setTrigger) {
ASSERT_ARGC(2)
struct vcd_parser_s *state;
ASSERT_EXTERNAL(args[0], state)
ASSERT_STRING(args[1], state->trigger)
napi_value res;
ASSERT(res, napi_create_int32(env, state->error, &res))
return res;
}
napi_value Init(napi_env env, napi_value exports) {
DECLARE_NAPI_METHOD("init", init)
DECLARE_NAPI_METHOD("done", done)
DECLARE_NAPI_METHOD("execute", execute)
DECLARE_NAPI_METHOD("setTrigger", setTrigger)
return exports;
}
NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)