Passing message from javascript to c++ using protobuf and WASM - Stack Overflow

Please tell me where I'm wrong. I use protobuf for interaction between javascript and c++There is

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745613897a4636114.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信