Skip to content

Commit

Permalink
c calls js
Browse files Browse the repository at this point in the history
  • Loading branch information
esromneb committed Sep 27, 2020
1 parent d57cc3b commit 70d6463
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ vcd_spans.c \
# the version here has a prepended underscore
# all lines must have trailing comma
EXPORT_STRING = \
# "_somefn", \
"_execute", \
# "_int_sqrt", \
# "_pass_write_fn", \
# "_get_saved_node_count", \
Expand Down
44 changes: 43 additions & 1 deletion lib/wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,60 @@ module.exports = () => {
// let total = 0;
// let start = 0;

const c = {};

const wasm = require('../out/vcd.js');

let bindCallback;

const bindCWrap = () => {
const w = wasm.cwrap;
c.execute = w('execute', 'void', ['number', 'number', 'number', 'number']);
};

let start = async() => {
const start = async() => {
await _waitForStart(wasm);
bindCWrap();
bindCallback();
}

// gets a string from a c heap pointer and length
const getString = (name,len) => {
const view = wasm.HEAPU8.subarray(name, name+len);

let string = '';
for (let i = 0; i < len; i++) {
string += String.fromCharCode(view[i]);
}
return string;
}

let boundSet;

// wasm.addFunction can't be called until after
// start finishes
bindCallback = () => {
boundSet = wasm.addFunction(function(name, len, value) {


let prop = getString(name, len);

console.log(`setting ${prop} to ${value}`);


// vsi means returns void accepts a string and int
}, 'viii');
};

return {
start,
c,
log: () => {
console.log(wasm);
},
execute: () => {
c.execute(0,0,boundSet,0);
},
onB: (time, cmd) => {

},
Expand Down
4 changes: 4 additions & 0 deletions test/wasm.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ describe('basic', () => {

console.log("test");

// wrapper.c.execute('hello world');

wrapper.execute();

// console.log(wrapper.log());


Expand Down
45 changes: 42 additions & 3 deletions wasm_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,58 @@ using namespace std;
static struct vcd_parser_s* state;


static void init(void) {
// 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;
return -1;
}

return 0;
}

int main(void) {
cout << "main()\n";
init();
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);
typedef void externalJsSetProperty(const char* name, const size_t len, const int value);


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



extern "C" {

void execute(
externalJsMethodOne* f1,
externalJsMethodTwo* f2,
externalJsSetProperty* sfn,
externalJsGetProperty* gfn
) {

// cout << "execute got " << p << "\n";
cout << "execute " << (int)sfn << "\n";
set_property = sfn;

set_property("foo", strlen("foo"), 4);


}

} // extern C

0 comments on commit 70d6463

Please sign in to comment.