From f46ad25c19811f1cd5891e0d1687b35b26c90c2f Mon Sep 17 00:00:00 2001 From: Ben Date: Sun, 27 Sep 2020 02:42:00 -0700 Subject: [PATCH] setting strings and ints from c --- Makefile | 23 +---------- lib/wrapper.js | 31 ++++++++++++--- test/wasm.js | 10 +++++ wasm_main.cpp | 102 ++++++++++++++++++++++++++++++++++--------------- wasm_main.hpp | 8 ++++ 5 files changed, 117 insertions(+), 57 deletions(-) create mode 100644 wasm_main.hpp diff --git a/Makefile b/Makefile index 0d19a87..d7f916b 100644 --- a/Makefile +++ b/Makefile @@ -8,6 +8,7 @@ WASM_MAIN = wasm_main.cpp HPP_FILES = \ vcd_parser.h \ +wasm_main.hpp \ CPP_FILES = \ @@ -22,27 +23,7 @@ vcd_spans.c \ # all lines must have trailing comma EXPORT_STRING = \ "_execute", \ -# "_int_sqrt", \ -# "_pass_write_fn", \ -# "_get_saved_node_count", \ -# "_get_saved_node_name", \ -# "_get_saved_node_id", \ -# "_get_child_node_count", \ -# "_get_child_node_id", \ -# "_register_action_node", \ -# "_register_condition_node", \ -# "_unregister_builder", \ -# "_parse_xml", \ -# "_lt", \ -# "_ltd", \ -# "_reset_trackers", \ -# "_reset_factory", \ -# "_reset_all", \ - -# Functions used in debugging -# "_callBoundJs", \ -# "_debug_example", \ - +"_init", \ # warning and error flags CLANG_WARN_FLAGS = \ diff --git a/lib/wrapper.js b/lib/wrapper.js index 0c55cb9..4a03171 100644 --- a/lib/wrapper.js +++ b/lib/wrapper.js @@ -19,7 +19,8 @@ module.exports = () => { const bindCWrap = () => { const w = wasm.cwrap; - c.execute = w('execute', 'void', ['number', 'number', 'number', 'number', 'number', 'string']); + c.execute = w('execute', 'void', ['number', 'number', 'number', 'number', 'number', 'string']); + c.init = w('init', 'number', ['number', 'number', 'number', 'number']); }; const start = async() => { @@ -39,20 +40,34 @@ module.exports = () => { return string; } + let boundInfo; + let boundSet; let boundGet; + let context = -1; + // wasm.addFunction can't be called until after // start finishes bindCallback = () => { - boundSet = wasm.addFunction(function(name, len, value) { + boundSet = wasm.addFunction(function(name, len, type, v0, v1) { let prop = getString(name, len); - console.log(`setting ${prop} to ${value}`); + switch(type) { + case 0: + boundInfo[prop] = v0; + break; + case 1: + boundInfo[prop] = getString(v0, v1); + break; + default: throw new Error(); + } - // viii means returns void, accepts int int int - }, 'viii'); + console.log(`setting ${prop} to ${boundInfo[prop]}`); + + // viiiii means returns void, accepts int int int int int + }, 'viiiii'); boundGet = wasm.addFunction(function(name, len) { let prop = getString(name, len); @@ -66,8 +81,12 @@ module.exports = () => { log: () => { console.log(wasm); }, + init: (cb0, cb1, info) => { + boundInfo = info; + context = c.init(0,0,boundSet,boundGet); + }, execute: () => { - c.execute(0,0,0,boundSet,boundGet,"hi"); + c.execute(context,0,0,boundSet,boundGet,"hi"); }, onB: (time, cmd) => { diff --git a/test/wasm.js b/test/wasm.js index 9000923..8ce9889 100644 --- a/test/wasm.js +++ b/test/wasm.js @@ -22,10 +22,20 @@ describe('basic', () => { console.log("test"); + const wires = {}; + const info = {stack: [wires], wires: wires}; + + + wrapper.init({},{},info); + + console.log(info); + + // wrapper.c.execute('hello world'); wrapper.execute(); + expect(info.foo).to.equal(10); // console.log(wrapper.log()); diff --git a/wasm_main.cpp b/wasm_main.cpp index 5b46863..f203312 100644 --- a/wasm_main.cpp +++ b/wasm_main.cpp @@ -6,36 +6,13 @@ using namespace std; - -static struct vcd_parser_s* state; - - -// returns context -int 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 -1; - } - - return 0; -} - -int main(void) { - cout << "main()\n"; - return 0; -} - - /// Typedef used as part of c->js call typedef void externalJsMethodOne(const int sz); typedef void externalJsMethodTwo(const char*, const uint64_t time, const uint8_t command, const int dnc0, const int dnc1); - typedef int externalJsGetProperty(const char* name, const size_t len); -typedef void externalJsSetProperty(const char* name, const size_t len, const int value); +typedef void externalJsSetProperty(const char* name, const size_t len, const int type, const int v0, const int v1); + /// function pointer for c->js @@ -44,18 +21,76 @@ static externalJsMethodTwo* externalTwo = 0; static externalJsSetProperty* bound_set_property = 0; static externalJsGetProperty* bound_get_property = 0; -static void set_property(const char* name, const int value) { - bound_set_property(name, strlen(name), value); +void set_property_int(const char* name, const int value) { + bound_set_property(name, strlen(name), 0, value, 0); } -static int get_property(const char* name) { +void set_property_string(const char* name, const char* value) { + bound_set_property(name, strlen(name), 1, (int)value, strlen(value)); +} + +int get_property(const char* name) { return bound_get_property(name, strlen(name)); } +static struct vcd_parser_s* state; extern "C" { +// returns context +int init( + externalJsMethodOne* f1, + externalJsMethodTwo* f2, + externalJsSetProperty* sfn, + externalJsGetProperty* gfn + ) { + + + state = (struct vcd_parser_s*) malloc(sizeof *state); + + const int32_t error = vcd_parser_init(state); + if (error) { + cout << "ERROR: " << error << "\n"; + return -1; + } + + bound_set_property = sfn; + bound_get_property = gfn; + externalOne = f1; + externalTwo = f2; + + state->lifee = (void*) externalOne; + state->triee = (void*) externalTwo; + + static char triggerString [4096] = " "; + static char tmpStr [4096] = " "; + static uint64_t valueBuf [4096] = {}; + static uint64_t maskBuf [4096] = {}; + + state->trigger = triggerString; + state->reason = "NO REASON"; + state->napi_env = 0; + state->tmpStr = tmpStr; + state->value = valueBuf; + state->mask = maskBuf; + state->digitCount = 0; + + set_property_string("status", "declaration"); + + + static int context = 0; + context++; + + return context; +} + + + + + + + void execute( const int context, externalJsMethodOne* f1, @@ -69,15 +104,22 @@ void execute( cout << "execute " << (int)sfn << " and got " << chunk << "\n"; bound_set_property = sfn; bound_get_property = gfn; + externalOne = f1; + externalTwo = f2; + set_property_int("foo", 10); int got = get_property("bar"); cout << "got " << got << " for bar\n"; - - } +int main(void) { + cout << "main()\n"; + return 0; +} + + } // extern C \ No newline at end of file diff --git a/wasm_main.hpp b/wasm_main.hpp new file mode 100644 index 0000000..ee9ac0c --- /dev/null +++ b/wasm_main.hpp @@ -0,0 +1,8 @@ +#pragma once + + + +// void set_property(const char* name, const int value); +// int get_property(const char* name); + +