starwing / lua-protobuf

A Lua module to work with Google protobuf
MIT License
1.71k stars 388 forks source link

Running on ESP32: load() function not working #265

Open yesItsGaren opened 3 months ago

yesItsGaren commented 3 months ago

Hello, I am new with Lua. I am trying to use it on the ESP32. I have already tried using it to run the example script on Windows and it works flawlessly. The problems start when I try to use the pb.c and import it using require "pb".

Some prerequisite info:

const char test_script_protobuf[] = R"delimiter( local protoc = require "protoc" local pb = require "pb"

local p = protoc.new()

-- load schema from text (just for demo, use protoc.new() in real world) local retVal = p:load ([[ message Phone { optional string name = 1; optional int64 phonenumber = 2; } message Person { optional string name = 1; optional int32 age = 2; optional string address = 3; repeated Phone contacts = 4; } ]],"Person")

print("p:load ") print(retVal)

-- lua table data local data = { name = "ilse", age = 18, contacts = { { name = "alice", phonenumber = 12312341234 }, { name = "bob", phonenumber = 45645674567 } } }

-- encode lua table data into binary format in lua string and return local bytes = assert(pb.encode("Person", data)) print(pb.tohex(bytes))

-- and decode the binary data back into lua table local data2 = assert(pb.decode("Person", bytes)) print(require "serpent".block(data2))

)delimiter";

My question: What am I missing here? How can I make the load() function and other functions to run properly?

starwing commented 3 months ago

Which Lua version you used? protoc.lua is not recommend in productive environment, you could just compile your .proto files into .pb file using protoc -o out.pb file.proto.

yesItsGaren commented 3 months ago

I am using Lua 5.1. But my goal is to write a protoc schema in Lua dynamically on the ESP32. I have managed to run Lua scripts on the ESP32, but I can't seem to make the protoc-Lua Library to run properly.

starwing commented 3 months ago

@yesItsGaren I think I found the issue: you should require "pb" before require "protoc"