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
|
# the version here has a prepended underscore
|
||||||
# all lines must have trailing comma
|
# all lines must have trailing comma
|
||||||
EXPORT_STRING = \
|
EXPORT_STRING = \
|
||||||
# "_somefn", \
|
"_execute", \
|
||||||
# "_int_sqrt", \
|
# "_int_sqrt", \
|
||||||
# "_pass_write_fn", \
|
# "_pass_write_fn", \
|
||||||
# "_get_saved_node_count", \
|
# "_get_saved_node_count", \
|
||||||
|
@ -11,18 +11,60 @@ module.exports = () => {
|
|||||||
// let total = 0;
|
// let total = 0;
|
||||||
// let start = 0;
|
// let start = 0;
|
||||||
|
|
||||||
|
const c = {};
|
||||||
|
|
||||||
const wasm = require('../out/vcd.js');
|
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);
|
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 {
|
return {
|
||||||
start,
|
start,
|
||||||
|
c,
|
||||||
log: () => {
|
log: () => {
|
||||||
console.log(wasm);
|
console.log(wasm);
|
||||||
},
|
},
|
||||||
|
execute: () => {
|
||||||
|
c.execute(0,0,boundSet,0);
|
||||||
|
},
|
||||||
onB: (time, cmd) => {
|
onB: (time, cmd) => {
|
||||||
|
|
||||||
},
|
},
|
||||||
|
@ -22,6 +22,10 @@ describe('basic', () => {
|
|||||||
|
|
||||||
console.log("test");
|
console.log("test");
|
||||||
|
|
||||||
|
// wrapper.c.execute('hello world');
|
||||||
|
|
||||||
|
wrapper.execute();
|
||||||
|
|
||||||
// console.log(wrapper.log());
|
// console.log(wrapper.log());
|
||||||
|
|
||||||
|
|
||||||
|
@ -10,19 +10,58 @@ using namespace std;
|
|||||||
static struct vcd_parser_s* state;
|
static struct vcd_parser_s* state;
|
||||||
|
|
||||||
|
|
||||||
static void init(void) {
|
// returns context
|
||||||
|
int init(void) {
|
||||||
state = (struct vcd_parser_s*) malloc(sizeof *state);
|
state = (struct vcd_parser_s*) malloc(sizeof *state);
|
||||||
|
|
||||||
const int32_t error = vcd_parser_init(state);
|
const int32_t error = vcd_parser_init(state);
|
||||||
if (error) {
|
if (error) {
|
||||||
cout << "ERROR: " << error << "\n";
|
cout << "ERROR: " << error << "\n";
|
||||||
return;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
cout << "main()\n";
|
cout << "main()\n";
|
||||||
init();
|
|
||||||
return 0;
|
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