fixed setTrigger ; getInfo

This commit is contained in:
Aliaksei Chapyzhenka 2019-10-23 19:42:17 -07:00
parent 48053d4e6b
commit bf6a8f5b63
5 changed files with 67 additions and 67 deletions

View File

@ -16,6 +16,7 @@ p.property('i32', 'time');
p.property('i32', 'start'); p.property('i32', 'start');
p.property('i32', 'stop'); p.property('i32', 'stop');
p.property('ptr', 'trigger'); p.property('ptr', 'trigger');
p.property('ptr', 'info');
const scopeIdentifierSpan = p.span(p.code.span('scopeIdentifierSpan')); const scopeIdentifierSpan = p.span(p.code.span('scopeIdentifierSpan'));
const varSizeSpan = p.span(p.code.span('varSizeSpan')); const varSizeSpan = p.span(p.code.span('varSizeSpan'));

View File

@ -25,12 +25,14 @@ fs.readdir(dir).then(files => {
chunks++; chunks++;
}); });
s.on('end', () => { s.on('end', () => {
const info = lib.getInfo(cxt);
// console.log(info);
console.log( console.log(
fileName, fileName,
chunks, chunks,
len, len,
goodChunks, goodChunks,
lib.getStop(cxt) - lib.getStart(cxt), info.stop - info.start,
((Date.now() - t0) / 1000 + 's') ((Date.now() - t0) / 1000 + 's')
); );
callback(); callback();

View File

@ -11,14 +11,22 @@ describe('basic', () => {
it('type', done => { it('type', done => {
const cxt = lib.init(); const cxt = lib.init();
expect(cxt).to.be.an('object'); expect(cxt).to.be.an('object');
console.log(cxt);
done(); done();
}); });
it('fail: foo bar', done => { it('fail: foo bar', done => {
const cxt = lib.init(); const cxt = lib.init();
expect(lib.execute(cxt, Buffer.from(' foo bar ???'))).to.eq(1); expect(lib.execute(cxt, Buffer.from(' foo bar ???'))).to.eq(1);
expect(lib.getError(cxt)).to.eq(1); expect(lib.getInfo(cxt)).to.deep.eq({
expect(lib.getReason(cxt)).to.eq('Expected declaration command'); error: 1,
reason: 'Expected declaration command',
command: 0,
type: 0,
size: 0,
time: 0,
start: 0,
stop: 0,
trigger: ''
});
done(); done();
}); });
it('$comment', done => { it('$comment', done => {
@ -26,10 +34,17 @@ describe('basic', () => {
expect(lib.execute(cxt, Buffer.from( expect(lib.execute(cxt, Buffer.from(
' \n $comment some text $end $comment more text $end ???' ' \n $comment some text $end $comment more text $end ???'
))).to.eq(1); ))).to.eq(1);
expect(lib.getError(cxt)).to.eq(1); expect(lib.getInfo(cxt)).to.deep.eq({
expect(lib.getReason(cxt)).to.eq('Expected declaration command'); error: 1,
expect(lib.getCommand(cxt)).to.eq(1); reason: 'Expected declaration command',
// expect(lib.getErrorPos(cxt)).to.eq('Expected declaration'); command: 1,
type: 0,
size: 0,
time: 0,
start: 0,
stop: 0,
trigger: ''
});
done(); done();
}); });
it('$version', done => { it('$version', done => {
@ -68,9 +83,17 @@ b0000000011110000 {u
b0000000000001111 {u b0000000000001111 {u
` `
))).to.eq(0); ))).to.eq(0);
expect(lib.getError(cxt)).to.eq(0); expect(lib.getInfo(cxt)).to.deep.eq({
// expect(lib.getReason(cxt)).to.eq('Expected simulation command'); error: 0,
// expect(lib.getCommand(cxt)).to.eq(100); reason: 'NO REASON',
command: 19,
type: 17,
size: 64,
time: 303,
start: 300,
stop: 303,
trigger: ''
});
done(); done();
}); });
}); });

84
vcd.c
View File

@ -65,7 +65,6 @@
} }
#define ASSERT_STRING(name, var) \ #define ASSERT_STRING(name, var) \
char var[256]; \
{ \ { \
napi_value tmp; \ napi_value tmp; \
if (napi_coerce_to_string(env, name, &tmp) != napi_ok) { \ if (napi_coerce_to_string(env, name, &tmp) != napi_ok) { \
@ -83,7 +82,11 @@ METHOD(init) {
struct vcd_parser_s *state = malloc(sizeof *state); struct vcd_parser_s *state = malloc(sizeof *state);
const int32_t error = vcd_parser_init(state); const int32_t error = vcd_parser_init(state);
state->trigger = "HELLO";
static char triggerString [256];
state->trigger = triggerString;
state->reason = "NO REASON";
napi_value res; napi_value res;
if (error) { if (error) {
@ -107,73 +110,49 @@ METHOD(execute) {
return res; return res;
} }
METHOD(getError) { METHOD(getInfo) {
ASSERT_ARGC(1) ASSERT_ARGC(1)
struct vcd_parser_s *state; struct vcd_parser_s *state;
ASSERT_EXTERNAL(args[0], state) ASSERT_EXTERNAL(args[0], state)
napi_value res; napi_value infObj, error, reason, command, type, size, time, start, stop, trigger;
ASSERT(res, napi_create_int32(env, state->error, &res)) ASSERT(infObj, napi_create_object(env, &infObj))
return res;
}
METHOD(getReason) { ASSERT(error, napi_create_int32(env, state->error, &error))
ASSERT_ARGC(1) ASSERT(infObj, napi_set_named_property(env, infObj, "error", error))
struct vcd_parser_s *state;
ASSERT_EXTERNAL(args[0], state)
napi_value res; ASSERT(reason, napi_create_string_latin1(env, state->reason, NAPI_AUTO_LENGTH, &reason))
ASSERT(res, napi_create_string_utf8(env, state->reason, NAPI_AUTO_LENGTH, &res)) ASSERT(infObj, napi_set_named_property(env, infObj, "reason", reason))
return res;
}
METHOD(getCommand) { ASSERT(command, napi_create_int32(env, state->command, &command))
ASSERT_ARGC(1) ASSERT(infObj, napi_set_named_property(env, infObj, "command", command))
struct vcd_parser_s *state;
ASSERT_EXTERNAL(args[0], state)
napi_value res; ASSERT(type, napi_create_int32(env, state->type, &type))
ASSERT(res, napi_create_int32(env, state->command, &res)) ASSERT(infObj, napi_set_named_property(env, infObj, "type", type))
return res;
}
METHOD(getTime) { ASSERT(size, napi_create_int32(env, state->size, &size))
ASSERT_ARGC(1) ASSERT(infObj, napi_set_named_property(env, infObj, "size", size))
struct vcd_parser_s *state;
ASSERT_EXTERNAL(args[0], state)
napi_value res; ASSERT(time, napi_create_int32(env, state->time, &time))
ASSERT(res, napi_create_int32(env, state->time, &res)) ASSERT(infObj, napi_set_named_property(env, infObj, "time", time))
return res;
}
METHOD(getStart) { ASSERT(start, napi_create_int32(env, state->start, &start))
ASSERT_ARGC(1) ASSERT(infObj, napi_set_named_property(env, infObj, "start", start))
struct vcd_parser_s *state;
ASSERT_EXTERNAL(args[0], state)
napi_value res; ASSERT(stop, napi_create_int32(env, state->stop, &stop))
ASSERT(res, napi_create_int32(env, state->start, &res)) ASSERT(infObj, napi_set_named_property(env, infObj, "stop", stop))
return res;
}
METHOD(getStop) { ASSERT(trigger, napi_create_string_latin1(env, state->trigger, NAPI_AUTO_LENGTH, &trigger))
ASSERT_ARGC(1) ASSERT(infObj, napi_set_named_property(env, infObj, "trigger", trigger))
struct vcd_parser_s *state;
ASSERT_EXTERNAL(args[0], state)
napi_value res; return infObj;
ASSERT(res, napi_create_int32(env, state->stop, &res))
return res;
} }
METHOD(setTrigger) { METHOD(setTrigger) {
ASSERT_ARGC(2) ASSERT_ARGC(2)
struct vcd_parser_s *state; struct vcd_parser_s *state;
ASSERT_EXTERNAL(args[0], state) ASSERT_EXTERNAL(args[0], state)
ASSERT_STRING(args[1], trigger) ASSERT_STRING(args[1], state->trigger)
state->trigger = trigger;
napi_value res; napi_value res;
ASSERT(res, napi_create_int32(env, state->error, &res)) ASSERT(res, napi_create_int32(env, state->error, &res))
@ -183,12 +162,7 @@ METHOD(setTrigger) {
napi_value Init(napi_env env, napi_value exports) { napi_value Init(napi_env env, napi_value exports) {
DECLARE_NAPI_METHOD("init", init) DECLARE_NAPI_METHOD("init", init)
DECLARE_NAPI_METHOD("execute", execute) DECLARE_NAPI_METHOD("execute", execute)
DECLARE_NAPI_METHOD("getError", getError) DECLARE_NAPI_METHOD("getInfo", getInfo)
DECLARE_NAPI_METHOD("getReason", getReason)
DECLARE_NAPI_METHOD("getCommand", getCommand)
DECLARE_NAPI_METHOD("getTime", getTime)
DECLARE_NAPI_METHOD("getStart", getStart)
DECLARE_NAPI_METHOD("getStop", getStop)
DECLARE_NAPI_METHOD("setTrigger", setTrigger) DECLARE_NAPI_METHOD("setTrigger", setTrigger)
return exports; return exports;
} }

View File

@ -32,7 +32,7 @@ int varSizeSpan(vcd_parser_t* s, const unsigned char* p, const unsigned char* en
int idSpan(vcd_parser_t* s, const unsigned char* p, const unsigned char* endp) { int idSpan(vcd_parser_t* s, const unsigned char* p, const unsigned char* endp) {
// printf("{%.*s}", (int)(endp - p - 1), p); // printf("{%.*s}", (int)(endp - p - 1), p);
if (stringEq((const unsigned char *)("D1"), p, endp)) { if (stringEq((s->trigger), p, endp)) {
if (s->time < 10) { if (s->time < 10) {
return 0; return 0;
} }