c calls js
This commit is contained in:
parent
d57cc3b147
commit
70d64632a3
2
Makefile
2
Makefile
@ -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", \
|
||||
|
@ -11,18 +11,60 @@ module.exports = () => {
|
||||
// let total = 0;
|
||||
// let start = 0;
|
||||
|
||||
const c = {};
|
||||
|
||||
const wasm = require('../out/vcd.js');
|
||||
|
||||
let bindCallback;
|
||||
|
||||
let start = async() => {
|
||||
const bindCWrap = () => {
|
||||
const w = wasm.cwrap;
|
||||
c.execute = w('execute', 'void', ['number', 'number', 'number', 'number']);
|
||||
};
|
||||
|
||||
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) => {
|
||||
|
||||
},
|
||||
|
@ -22,6 +22,10 @@ describe('basic', () => {
|
||||
|
||||
console.log("test");
|
||||
|
||||
// wrapper.c.execute('hello world');
|
||||
|
||||
wrapper.execute();
|
||||
|
||||
// console.log(wrapper.log());
|
||||
|
||||
|
||||
|
@ -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
|
Loading…
x
Reference in New Issue
Block a user