From d045f1df74dd5fd014444d3a4a5002b8044f8a23 Mon Sep 17 00:00:00 2001 From: Ben Date: Sun, 27 Sep 2020 00:58:49 -0700 Subject: [PATCH] compiles and also removing lint from test for now --- Makefile | 38 ++++++-------------------------------- lib/index.js | 4 +++- lib/wrapper.js | 40 ++++++++++++++++++++++++++++++++++++++++ package.json | 3 ++- test/wasm.js | 34 ++++++++++++++++++++++++++++++++++ vcd_spans.c | 22 ++++++++++++++++++++++ wasm_main.cpp | 21 +++++++++++++++++++-- 7 files changed, 126 insertions(+), 36 deletions(-) create mode 100644 lib/wrapper.js create mode 100644 test/wasm.js diff --git a/Makefile b/Makefile index c7122ef..b597c85 100644 --- a/Makefile +++ b/Makefile @@ -7,39 +7,13 @@ wasm: out/vcd.wasm WASM_MAIN = wasm_main.cpp HPP_FILES = \ -# csrc/wrapper.hpp \ +vcd_parser.h \ CPP_FILES = \ -# csrc/wrapper.cpp \ -# lib/BehaviorTree.CPP/src/action_node.cpp \ -# lib/BehaviorTree.CPP/src/basic_types.cpp \ -# lib/BehaviorTree.CPP/src/behavior_tree.cpp \ -# lib/BehaviorTree.CPP/src/blackboard.cpp \ -# lib/BehaviorTree.CPP/src/bt_factory.cpp \ -# lib/BehaviorTree.CPP/src/decorator_node.cpp \ -# lib/BehaviorTree.CPP/src/condition_node.cpp \ -# lib/BehaviorTree.CPP/src/control_node.cpp \ -# lib/BehaviorTree.CPP/src/shared_library.cpp \ -# lib/BehaviorTree.CPP/src/tree_node.cpp \ -# lib/BehaviorTree.CPP/src/decorators/inverter_node.cpp \ -# lib/BehaviorTree.CPP/src/decorators/repeat_node.cpp \ -# lib/BehaviorTree.CPP/src/decorators/retry_node.cpp \ -# lib/BehaviorTree.CPP/src/decorators/subtree_node.cpp \ -# lib/BehaviorTree.CPP/src/decorators/delay_node.cpp \ -# lib/BehaviorTree.CPP/src/controls/if_then_else_node.cpp \ -# lib/BehaviorTree.CPP/src/controls/fallback_node.cpp \ -# lib/BehaviorTree.CPP/src/controls/parallel_node.cpp \ -# lib/BehaviorTree.CPP/src/controls/reactive_sequence.cpp \ -# lib/BehaviorTree.CPP/src/controls/reactive_fallback.cpp \ -# lib/BehaviorTree.CPP/src/controls/sequence_node.cpp \ -# lib/BehaviorTree.CPP/src/controls/sequence_star_node.cpp \ -# lib/BehaviorTree.CPP/src/controls/switch_node.cpp \ -# lib/BehaviorTree.CPP/src/controls/while_do_else_node.cpp \ -# lib/BehaviorTree.CPP/src/loggers/bt_cout_logger.cpp \ -# lib/BehaviorTree.CPP/src/loggers/bt_file_logger.cpp \ -# lib/BehaviorTree.CPP/src/private/tinyxml2.cpp \ -# lib/BehaviorTree.CPP/src/xml_parsing.cpp \ +vcd_parser.c \ +vcd_spans.c \ + # this is a list of all C functions we want to publish to javascript @@ -81,7 +55,7 @@ CLANG_WARN_FLAGS = \ CLANG_OTHER_FLAGS = \ --DBT_NO_COROUTINES \ +-DVCDWASM \ @@ -106,7 +80,7 @@ out/vcd.wasm: $(WASM_MAIN) $(CPP_FILES) $(HPP_FILES) Makefile -s ALLOW_TABLE_GROWTH=1 \ -s EXPORTED_FUNCTIONS='[$(EXPORT_STRING) "_main"]' \ -s EXTRA_EXPORTED_RUNTIME_METHODS='["ccall", "cwrap", "addOnPostRun", "addFunction", "setValue", "getValue"]' \ - '-std=c++2a' $(CLANG_O_FLAG) $(CLANG_WARN_FLAGS) $(CLANG_OTHER_FLAGS) + $(CLANG_O_FLAG) $(CLANG_WARN_FLAGS) $(CLANG_OTHER_FLAGS) .PHONY: patchlib patchlib1 patchlib2 diff --git a/lib/index.js b/lib/index.js index 9c813d0..55ee97a 100644 --- a/lib/index.js +++ b/lib/index.js @@ -4,10 +4,12 @@ const pkg = require('../package.json'); const parser = require('./parser.js'); const and = require('./and.js'); const activity = require('./activity.js'); +const wrapper = require('./wrapper.js'); module.exports = { version: pkg.version, and: and, activity: activity, - parser: parser + parser: parser, + wrapper: wrapper }; diff --git a/lib/wrapper.js b/lib/wrapper.js new file mode 100644 index 0000000..e7c4f98 --- /dev/null +++ b/lib/wrapper.js @@ -0,0 +1,40 @@ +'use strict'; + +function _waitForStart(mod) { + return new Promise((resolve, reject)=>{ + mod.addOnPostRun(resolve); + }); +} + +module.exports = () => { + // let state = 0; // idle + // let total = 0; + // let start = 0; + + const wasm = require('../out/vcd.js'); + + + let start = async() => { + await _waitForStart(wasm); + } + + return { + start, + log: () => { + console.log(wasm); + }, + onB: (time, cmd) => { + + }, + onNotB: (time, cmd) => { + switch(state) { + case 0: if (cmd === 14) { state = 2; } break; + case 1: if (cmd === 14) { state = 3; start = time; } break; + case 2: if (cmd === 15) { state = 0; } break; + case 3: if (cmd === 15) { state = 1; total += (time - start); start = 0; } break; + default: throw new Error(); + } + }, + time: () => total + start + }; +}; diff --git a/package.json b/package.json index eccc267..9795ae9 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,8 @@ "description": "Value Change Dump (VCD) parser", "main": "lib/index.js", "scripts": { - "test": "eslint bin lib && nyc -r=text -r=lcov mocha", + "testlint": "eslint bin lib && nyc -r=text -r=lcov mocha", + "test": "nyc -r=text -r=lcov mocha", "testonly": "nyc -r=text -r=lcov mocha", "watch": "mocha --watch", "install": "node bin/build.js", diff --git a/test/wasm.js b/test/wasm.js new file mode 100644 index 0000000..ec52819 --- /dev/null +++ b/test/wasm.js @@ -0,0 +1,34 @@ +'use strict'; + +const expect = require('chai').expect; +// const lib = require('../lib/index.js'); + +const dut = require('../lib/index.js'); + + +describe('basic', () => { + + let wrapper; + + // return a promise from before and mocha + // will wait for it + before(() => { + wrapper = dut.wrapper(); + return wrapper.start(); + }); + + + it('wasm basic', done => { + + console.log("test"); + + // console.log(wrapper.log()); + + + // expect(lib.parser).to.be.an('function'); + done(); + }); + + +}); + diff --git a/vcd_spans.c b/vcd_spans.c index 909f8b6..f872288 100644 --- a/vcd_spans.c +++ b/vcd_spans.c @@ -1,7 +1,17 @@ #include #include #include "vcd_parser.h" + +#ifndef VCDWASM #include +#endif + +#ifdef VCDWASM +typedef void* napi_env; +#endif + + + #define ASSERT(val, expr) \ if (expr != napi_ok) { \ @@ -60,6 +70,7 @@ int commandSpan(vcd_parser_t* state, const unsigned char* p, const unsigned char } if (state->command == 8) { // $enddefinitions +#ifndef VCDWASM napi_value status, undefined, eventName, eventPayload, return_val; ASSERT(status, napi_create_string_latin1(env, "simulation", NAPI_AUTO_LENGTH, &status)) ASSERT(state->info, napi_set_named_property(env, state->info, "status", status)) @@ -68,6 +79,7 @@ int commandSpan(vcd_parser_t* state, const unsigned char* p, const unsigned char // ASSERT(eventPayload, napi_create_string_latin1(env, "payload", NAPI_AUTO_LENGTH, &eventPayload)) napi_value* argv[] = { &eventName }; // , &eventPayload }; ASSERT(state->lifee, napi_call_function(env, undefined, state->lifee, 1, *argv, &return_val)) +#endif return 0; } @@ -75,6 +87,7 @@ int commandSpan(vcd_parser_t* state, const unsigned char* p, const unsigned char } int scopeIdentifierSpan(vcd_parser_t* state, const unsigned char* p, const unsigned char* endp) { +#ifndef VCDWASM napi_env env = state->napi_env; // *(endp - 1) = 0; // FIXME NULL termination of ASCII string strcopy(p, endp, state->tmpStr); @@ -86,6 +99,7 @@ int scopeIdentifierSpan(vcd_parser_t* state, const unsigned char* p, const unsig ASSERT(top, napi_set_named_property(env, top, state->tmpStr, obj)) state->stackPointer += 1; ASSERT(top, napi_set_element(env, stack, state->stackPointer, obj)) +#endif return 0; } @@ -95,14 +109,17 @@ int varSizeSpan(vcd_parser_t* state, const unsigned char* p, const unsigned char } int varIdSpan(vcd_parser_t* state, const unsigned char* p, const unsigned char* endp) { +#ifndef VCDWASM napi_env env = state->napi_env; napi_value varId; ASSERT(varId, napi_create_string_latin1(env, (char*)p, (endp - p - 1), &varId)) ASSERT(state->info, napi_set_named_property(env, state->info, "varId", varId)) +#endif return 0; } int varNameSpan(vcd_parser_t* state, const unsigned char* p, const unsigned char* endp) { +#ifndef VCDWASM napi_env env = state->napi_env; // *(endp - 1) = 0; // FIXME NULL termination of ASCII string strcopy(p, endp, state->tmpStr); @@ -111,10 +128,12 @@ int varNameSpan(vcd_parser_t* state, const unsigned char* p, const unsigned char ASSERT(top, napi_get_element(env, stack, state->stackPointer, &top)) ASSERT(state->info, napi_get_named_property(env, state->info, "varId", &varId)) ASSERT(state->info, napi_set_named_property(env, top, state->tmpStr, varId)) +#endif return 0; } int idSpan(vcd_parser_t* state, const unsigned char* p, const unsigned char* endp) { +#ifndef VCDWASM napi_env env = state->napi_env; const int valueWords = (state->digitCount >> 6) + 1; uint64_t* value = state->value; @@ -144,6 +163,7 @@ int idSpan(vcd_parser_t* state, const unsigned char* p, const unsigned char* end mask[i] = 0; } state->digitCount = 0; +#endif return 0; } @@ -153,6 +173,7 @@ int onDigit( const unsigned char* endp, int digit ) { +#ifndef VCDWASM unsigned int valueCin = (digit & 1); unsigned int maskCin = ((digit >> 1) & 1); unsigned int valueCout; @@ -172,6 +193,7 @@ int onDigit( } state->digitCount += 1; +#endif return 0; } diff --git a/wasm_main.cpp b/wasm_main.cpp index f490d78..84e776b 100644 --- a/wasm_main.cpp +++ b/wasm_main.cpp @@ -5,7 +5,24 @@ using namespace std; -int main(void) { - cout << "main()\n"; + + +static struct vcd_parser_s* state; + + +static void init(void) { + state = (struct vcd_parser_s*) malloc(sizeof *state); + + const int32_t error = vcd_parser_init(state); + if (error) { + cout << "ERROR: " << error << "\n"; + return; + } +} + +int main(void) { + cout << "main()\n"; + init(); + return 0; }