Skip to content

Commit

Permalink
setting strings and ints from c
Browse files Browse the repository at this point in the history
  • Loading branch information
esromneb committed Sep 27, 2020
1 parent 351f988 commit f46ad25
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 52 deletions.
23 changes: 2 additions & 21 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ WASM_MAIN = wasm_main.cpp

HPP_FILES = \
vcd_parser.h \
wasm_main.hpp \


CPP_FILES = \
Expand All @@ -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 = \
Expand Down
31 changes: 25 additions & 6 deletions lib/wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() => {
Expand All @@ -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);
Expand All @@ -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) => {

Expand Down
10 changes: 10 additions & 0 deletions test/wasm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());


Expand Down
92 changes: 67 additions & 25 deletions wasm_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,47 @@
using namespace std;


/// 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 type, const int v0, const int v1);



/// function pointer for c->js
static externalJsMethodOne* externalOne = 0;
static externalJsMethodTwo* externalTwo = 0;
static externalJsSetProperty* bound_set_property = 0;
static externalJsGetProperty* bound_get_property = 0;

void set_property_int(const char* name, const int value) {
bound_set_property(name, strlen(name), 0, value, 0);
}

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(void) {
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);
Expand All @@ -20,41 +55,41 @@ int init(void) {
return -1;
}

return 0;
}

int main(void) {
cout << "main()\n";
return 0;
}
bound_set_property = sfn;
bound_get_property = gfn;
externalOne = f1;
externalTwo = f2;

state->lifee = (void*) externalOne;
state->triee = (void*) externalTwo;

/// 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);
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;

typedef int externalJsGetProperty(const char* name, const size_t len);
typedef void externalJsSetProperty(const char* name, const size_t len, const int value);
set_property_string("status", "declaration");


/// function pointer for c->js
static externalJsMethodOne* externalOne = 0;
static externalJsMethodTwo* externalTwo = 0;
static externalJsSetProperty* bound_set_property = 0;
static externalJsGetProperty* bound_get_property = 0;
static int context = 0;
context++;

static void set_property(const char* name, const int value) {
bound_set_property(name, strlen(name), value);
return context;
}

static int get_property(const char* name) {
return bound_get_property(name, strlen(name));
}



extern "C" {



void execute(
const int context,
Expand All @@ -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
8 changes: 8 additions & 0 deletions wasm_main.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once



// void set_property(const char* name, const int value);
// int get_property(const char* name);


0 comments on commit f46ad25

Please sign in to comment.