admin管理员组文章数量:1025278
Please tell me where I'm wrong. I use protobuf for interaction between javascript and c++
There is person.proto which defines
syntax = "proto2";
package tutorial;
message Person {
optional int32 id = 1;
}
There is javascript code
const obj = new WebAssembly.Instance(...);
load("person.proto", function(err, root) {
// Create an instance of Person
const Person = root.lookupType("tutorial.Person");
let message = Person.create({ id: 52 });
let personData = Person.encode(message).finish();
// Write to memory
const strOffset = obj.exports.getStrOffset(personData.byteLength + 1);
let memory = new Uint8Array(obj.exports.memory.buffer, strOffset, personData.byteLength + 1);
memory.set(personData);
// Get the identifier for verification
console.log(obj.exports.setPersonDataAndGetPersonId(personData.byteLength));
});
There is code in c++
#include "person.pb.h"
char *message;
tutorial::Person *person
char* EMSCRIPTEN_KEEPALIVE getStrOffset(const int size) {
if (message) {
delete message;
message = nullptr;
}
message = new char [size];
return message;
}
int EMSCRIPTEN_KEEPALIVE setPersonDataAndGetPersonId(int size) {
tutorial::Person *person = new tutorial::Person();
absl::string_view serialized(message, size);
// serialized.size() // 2
// serialized[0] // 16
// serialized[1] // 52
person->ParseFromString(serialized); // Fail
return person->id();
}
The ParseFromString function does not work. There is a feeling that something other than two bytes is expected there I also tried ParseFromArray, but I got signature errors
Cpp is built with the command
emcc -L ./lib -lprotobuf idw.cpp ./proto/addressbook.pb -Oz -s WASM=1 -s --no-entry -s ERROR_ON_UNDEFINED_SYMBOLS=0 -s STANDALONE_WASM -o idw.wasm -lstdc++
How to pass message from javascript to c++? I will be very grateful for any advice.
AAAAAND
Okay, I decided to check if I'm crazy
// C++
const void EMSCRIPTEN_KEEPALIVE qwe() {
std::string data;
tutorial::Person person;
person.set_name("qwe");
person.set_id(52);
person.set_email("qwe");
person.SerializeToString(&data); // null function or function signature mismatch
}
// Build
// emcc -L ./lib -lprotobuf qwe.cpp ./proto/person.pb -Oz -s WASM=1 -s --no-entry -s ERROR_ON_UNDEFINED_SYMBOLS=0 -s STANDALONE_WASM -o idw.wasm -lstdc++
// Javascript
const obj = new WebAssembly.Instance(idwModule, importObject);
obj.exports.qwe();
I don't understand what's going on. Basic stuff doesn't work
Please tell me where I'm wrong. I use protobuf for interaction between javascript and c++
There is person.proto which defines
syntax = "proto2";
package tutorial;
message Person {
optional int32 id = 1;
}
There is javascript code
const obj = new WebAssembly.Instance(...);
load("person.proto", function(err, root) {
// Create an instance of Person
const Person = root.lookupType("tutorial.Person");
let message = Person.create({ id: 52 });
let personData = Person.encode(message).finish();
// Write to memory
const strOffset = obj.exports.getStrOffset(personData.byteLength + 1);
let memory = new Uint8Array(obj.exports.memory.buffer, strOffset, personData.byteLength + 1);
memory.set(personData);
// Get the identifier for verification
console.log(obj.exports.setPersonDataAndGetPersonId(personData.byteLength));
});
There is code in c++
#include "person.pb.h"
char *message;
tutorial::Person *person
char* EMSCRIPTEN_KEEPALIVE getStrOffset(const int size) {
if (message) {
delete message;
message = nullptr;
}
message = new char [size];
return message;
}
int EMSCRIPTEN_KEEPALIVE setPersonDataAndGetPersonId(int size) {
tutorial::Person *person = new tutorial::Person();
absl::string_view serialized(message, size);
// serialized.size() // 2
// serialized[0] // 16
// serialized[1] // 52
person->ParseFromString(serialized); // Fail
return person->id();
}
The ParseFromString function does not work. There is a feeling that something other than two bytes is expected there I also tried ParseFromArray, but I got signature errors
Cpp is built with the command
emcc -L ./lib -lprotobuf idw.cpp ./proto/addressbook.pb -Oz -s WASM=1 -s --no-entry -s ERROR_ON_UNDEFINED_SYMBOLS=0 -s STANDALONE_WASM -o idw.wasm -lstdc++
How to pass message from javascript to c++? I will be very grateful for any advice.
AAAAAND
Okay, I decided to check if I'm crazy
// C++
const void EMSCRIPTEN_KEEPALIVE qwe() {
std::string data;
tutorial::Person person;
person.set_name("qwe");
person.set_id(52);
person.set_email("qwe");
person.SerializeToString(&data); // null function or function signature mismatch
}
// Build
// emcc -L ./lib -lprotobuf qwe.cpp ./proto/person.pb -Oz -s WASM=1 -s --no-entry -s ERROR_ON_UNDEFINED_SYMBOLS=0 -s STANDALONE_WASM -o idw.wasm -lstdc++
// Javascript
const obj = new WebAssembly.Instance(idwModule, importObject);
obj.exports.qwe();
I don't understand what's going on. Basic stuff doesn't work
本文标签: Passing message from javascript to c using protobuf and WASMStack Overflow
版权声明:本文标题:Passing message from javascript to c++ using protobuf and WASM - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745613895a2159169.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论