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.cc -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.