setting strings and ints from c
This commit is contained in:
parent
351f988fb7
commit
f46ad25c19
23
Makefile
23
Makefile
@ -8,6 +8,7 @@ WASM_MAIN = wasm_main.cpp
|
|||||||
|
|
||||||
HPP_FILES = \
|
HPP_FILES = \
|
||||||
vcd_parser.h \
|
vcd_parser.h \
|
||||||
|
wasm_main.hpp \
|
||||||
|
|
||||||
|
|
||||||
CPP_FILES = \
|
CPP_FILES = \
|
||||||
@ -22,27 +23,7 @@ vcd_spans.c \
|
|||||||
# all lines must have trailing comma
|
# all lines must have trailing comma
|
||||||
EXPORT_STRING = \
|
EXPORT_STRING = \
|
||||||
"_execute", \
|
"_execute", \
|
||||||
# "_int_sqrt", \
|
"_init", \
|
||||||
# "_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", \
|
|
||||||
|
|
||||||
|
|
||||||
# warning and error flags
|
# warning and error flags
|
||||||
CLANG_WARN_FLAGS = \
|
CLANG_WARN_FLAGS = \
|
||||||
|
@ -19,7 +19,8 @@ module.exports = () => {
|
|||||||
|
|
||||||
const bindCWrap = () => {
|
const bindCWrap = () => {
|
||||||
const w = wasm.cwrap;
|
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() => {
|
const start = async() => {
|
||||||
@ -39,20 +40,34 @@ module.exports = () => {
|
|||||||
return string;
|
return string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let boundInfo;
|
||||||
|
|
||||||
let boundSet;
|
let boundSet;
|
||||||
let boundGet;
|
let boundGet;
|
||||||
|
|
||||||
|
let context = -1;
|
||||||
|
|
||||||
// wasm.addFunction can't be called until after
|
// wasm.addFunction can't be called until after
|
||||||
// start finishes
|
// start finishes
|
||||||
bindCallback = () => {
|
bindCallback = () => {
|
||||||
boundSet = wasm.addFunction(function(name, len, value) {
|
boundSet = wasm.addFunction(function(name, len, type, v0, v1) {
|
||||||
|
|
||||||
let prop = getString(name, len);
|
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
|
console.log(`setting ${prop} to ${boundInfo[prop]}`);
|
||||||
}, 'viii');
|
|
||||||
|
// viiiii means returns void, accepts int int int int int
|
||||||
|
}, 'viiiii');
|
||||||
|
|
||||||
boundGet = wasm.addFunction(function(name, len) {
|
boundGet = wasm.addFunction(function(name, len) {
|
||||||
let prop = getString(name, len);
|
let prop = getString(name, len);
|
||||||
@ -66,8 +81,12 @@ module.exports = () => {
|
|||||||
log: () => {
|
log: () => {
|
||||||
console.log(wasm);
|
console.log(wasm);
|
||||||
},
|
},
|
||||||
|
init: (cb0, cb1, info) => {
|
||||||
|
boundInfo = info;
|
||||||
|
context = c.init(0,0,boundSet,boundGet);
|
||||||
|
},
|
||||||
execute: () => {
|
execute: () => {
|
||||||
c.execute(0,0,0,boundSet,boundGet,"hi");
|
c.execute(context,0,0,boundSet,boundGet,"hi");
|
||||||
},
|
},
|
||||||
onB: (time, cmd) => {
|
onB: (time, cmd) => {
|
||||||
|
|
||||||
|
10
test/wasm.js
10
test/wasm.js
@ -22,10 +22,20 @@ describe('basic', () => {
|
|||||||
|
|
||||||
console.log("test");
|
console.log("test");
|
||||||
|
|
||||||
|
const wires = {};
|
||||||
|
const info = {stack: [wires], wires: wires};
|
||||||
|
|
||||||
|
|
||||||
|
wrapper.init({},{},info);
|
||||||
|
|
||||||
|
console.log(info);
|
||||||
|
|
||||||
|
|
||||||
// wrapper.c.execute('hello world');
|
// wrapper.c.execute('hello world');
|
||||||
|
|
||||||
wrapper.execute();
|
wrapper.execute();
|
||||||
|
|
||||||
|
expect(info.foo).to.equal(10);
|
||||||
// console.log(wrapper.log());
|
// console.log(wrapper.log());
|
||||||
|
|
||||||
|
|
||||||
|
102
wasm_main.cpp
102
wasm_main.cpp
@ -6,36 +6,13 @@
|
|||||||
using namespace std;
|
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 used as part of c->js call
|
||||||
typedef void externalJsMethodOne(const int sz);
|
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 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 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
|
/// function pointer for c->js
|
||||||
@ -44,18 +21,76 @@ static externalJsMethodTwo* externalTwo = 0;
|
|||||||
static externalJsSetProperty* bound_set_property = 0;
|
static externalJsSetProperty* bound_set_property = 0;
|
||||||
static externalJsGetProperty* bound_get_property = 0;
|
static externalJsGetProperty* bound_get_property = 0;
|
||||||
|
|
||||||
static void set_property(const char* name, const int value) {
|
void set_property_int(const char* name, const int value) {
|
||||||
bound_set_property(name, strlen(name), 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));
|
return bound_get_property(name, strlen(name));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static struct vcd_parser_s* state;
|
||||||
|
|
||||||
extern "C" {
|
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(
|
void execute(
|
||||||
const int context,
|
const int context,
|
||||||
externalJsMethodOne* f1,
|
externalJsMethodOne* f1,
|
||||||
@ -69,15 +104,22 @@ void execute(
|
|||||||
cout << "execute " << (int)sfn << " and got " << chunk << "\n";
|
cout << "execute " << (int)sfn << " and got " << chunk << "\n";
|
||||||
bound_set_property = sfn;
|
bound_set_property = sfn;
|
||||||
bound_get_property = gfn;
|
bound_get_property = gfn;
|
||||||
|
externalOne = f1;
|
||||||
|
externalTwo = f2;
|
||||||
|
|
||||||
|
set_property_int("foo", 10);
|
||||||
|
|
||||||
|
|
||||||
int got = get_property("bar");
|
int got = get_property("bar");
|
||||||
|
|
||||||
cout << "got " << got << " for bar\n";
|
cout << "got " << got << " for bar\n";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
cout << "main()\n";
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
} // extern C
|
} // extern C
|
8
wasm_main.hpp
Normal file
8
wasm_main.hpp
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// void set_property(const char* name, const int value);
|
||||||
|
// int get_property(const char* name);
|
||||||
|
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user