From 77c4f3ba946f995ee6ff8a6d18d7afb36ce49bcc Mon Sep 17 00:00:00 2001 From: Aliaksei Chapyzhenka Date: Mon, 21 Oct 2019 10:42:22 -0700 Subject: [PATCH] do not copy string chunk --- test/basic.js | 14 +++++++++----- vcd.c | 37 ++++++++++++++++++------------------- 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/test/basic.js b/test/basic.js index 470cc15..95b9bab 100644 --- a/test/basic.js +++ b/test/basic.js @@ -16,14 +16,16 @@ describe('basic', () => { }); it('fail: foo bar', done => { 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.getReason(cxt)).to.eq('Expected declaration command'); done(); }); it('$comment', done => { 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.getReason(cxt)).to.eq('Expected declaration command'); expect(lib.getCommand(cxt)).to.eq(1); @@ -32,7 +34,7 @@ describe('basic', () => { }); it('$version', done => { const cxt = lib.init(); - expect(lib.execute(cxt, ` + expect(lib.execute(cxt, Buffer.from(` $version Generated by VerilatedVcd $end $date Wed Sep 18 22:59:07 2019 $end @@ -43,7 +45,9 @@ $timescale 1ns $end $scope module leaf $end $var wire 64 "}> counter [63:0] $end $upscope $end -`, null)).to.eq(1); expect(lib.execute(cxt, ` + +`))).to.eq(1); expect(lib.execute(cxt, Buffer.from(` + $upscope $end $enddefinitions $end @@ -55,7 +59,7 @@ $enddefinitions $end #3 0"}G -`, null)).to.eq(1); +`))).to.eq(1); expect(lib.getError(cxt)).to.eq(1); // expect(lib.getReason(cxt)).to.eq('Expected simulation command'); // expect(lib.getCommand(cxt)).to.eq(100); diff --git a/vcd.c b/vcd.c index 8704463..2de4a97 100644 --- a/vcd.c +++ b/vcd.c @@ -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) { \ napi_valuetype valuetype; \ 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) { struct vcd_parser_s *state = malloc(sizeof *state); @@ -78,13 +78,12 @@ METHOD(init) { } METHOD(execute) { - ASSERT_ARGC(3) + ASSERT_ARGC(2) struct vcd_parser_s *state; ASSERT_EXTERNAL(args[0], state) - ASSERT_STRING(args[1], p) - ASSERT_STRING(args[2], endp) + ASSERT_BUFFER(args[1], p) - const int32_t error = vcd_parser_execute(state, p, endp); + const int32_t error = vcd_parser_execute(state, p, NULL); napi_value res; ASSERT(res, napi_create_int32(env, error, &res))