最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin7PV0评论

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

Post a comment

comment list (0)

  1. No comments so far