do not copy string chunk

This commit is contained in:
Aliaksei Chapyzhenka 2019-10-21 10:42:22 -07:00
parent 592ae7779a
commit 77c4f3ba94
2 changed files with 27 additions and 24 deletions

View File

@ -16,14 +16,16 @@ describe('basic', () => {
}); });
it('fail: foo bar', done => { it('fail: foo bar', done => {
const cxt = lib.init(); const cxt = lib.init();
expect(lib.execute(cxt, ' 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.getError(cxt)).to.eq(1);
expect(lib.getReason(cxt)).to.eq('Expected declaration command'); expect(lib.getReason(cxt)).to.eq('Expected declaration command');
done(); done();
}); });
it('$comment', done => { it('$comment', done => {
const cxt = lib.init(); const cxt = lib.init();
expect(lib.execute(cxt, ' \n $comment some text $end $comment more text $end ???', '')).to.eq(1); expect(lib.execute(cxt, Buffer.from(
' \n $comment some text $end $comment more text $end ???'
))).to.eq(1);
expect(lib.getError(cxt)).to.eq(1); expect(lib.getError(cxt)).to.eq(1);
expect(lib.getReason(cxt)).to.eq('Expected declaration command'); expect(lib.getReason(cxt)).to.eq('Expected declaration command');
expect(lib.getCommand(cxt)).to.eq(1); expect(lib.getCommand(cxt)).to.eq(1);
@ -32,7 +34,7 @@ describe('basic', () => {
}); });
it('$version', done => { it('$version', done => {
const cxt = lib.init(); const cxt = lib.init();
expect(lib.execute(cxt, ` expect(lib.execute(cxt, Buffer.from(`
$version Generated by VerilatedVcd $end $version Generated by VerilatedVcd $end
$date Wed Sep 18 22:59:07 2019 $date Wed Sep 18 22:59:07 2019
$end $end
@ -43,7 +45,9 @@ $timescale 1ns $end
$scope module leaf $end $scope module leaf $end
$var wire 64 "}> counter [63:0] $end $var wire 64 "}> counter [63:0] $end
$upscope $end $upscope $end
`, null)).to.eq(1); expect(lib.execute(cxt, `
`))).to.eq(1); expect(lib.execute(cxt, Buffer.from(`
$upscope $end $upscope $end
$enddefinitions $end $enddefinitions $end
@ -55,7 +59,7 @@ $enddefinitions $end
#3 #3
0"}G 0"}G
`, null)).to.eq(1); `))).to.eq(1);
expect(lib.getError(cxt)).to.eq(1); expect(lib.getError(cxt)).to.eq(1);
// expect(lib.getReason(cxt)).to.eq('Expected simulation command'); // expect(lib.getReason(cxt)).to.eq('Expected simulation command');
// expect(lib.getCommand(cxt)).to.eq(100); // expect(lib.getCommand(cxt)).to.eq(100);

37
vcd.c
View File

@ -32,21 +32,6 @@
} \ } \
} }
#define ASSERT_STRING(name, var) \
char var[4096]; \
{ \
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) { \ #define ASSERT_EXTERNAL(name, var) { \
napi_valuetype valuetype; \ napi_valuetype valuetype; \
if (napi_typeof(env, name, &valuetype) != napi_ok) { \ if (napi_typeof(env, name, &valuetype) != napi_ok) { \
@ -63,6 +48,21 @@
} \ } \
} }
#define ASSERT_BUFFER(name, var) \
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; \
} \
}
METHOD(init) { METHOD(init) {
struct vcd_parser_s *state = malloc(sizeof *state); struct vcd_parser_s *state = malloc(sizeof *state);
@ -78,13 +78,12 @@ METHOD(init) {
} }
METHOD(execute) { METHOD(execute) {
ASSERT_ARGC(3) 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], p) ASSERT_BUFFER(args[1], p)
ASSERT_STRING(args[2], endp)
const int32_t error = vcd_parser_execute(state, p, endp); const int32_t error = vcd_parser_execute(state, p, NULL);
napi_value res; napi_value res;
ASSERT(res, napi_create_int32(env, error, &res)) ASSERT(res, napi_create_int32(env, error, &res))