Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion example/test.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'person_pb'
person_pb = require 'person_pb'

local person = person_pb.Person()
person.id = 1000
Expand Down
8 changes: 5 additions & 3 deletions protobuf/containers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ local table = table
local rawset = rawset
local error = error

module "protobuf.containers"
local containers = {}

local _RCFC_meta = {
add = function(self)
Expand All @@ -47,7 +47,7 @@ local _RCFC_meta = {
}
_RCFC_meta.__index = _RCFC_meta

function RepeatedCompositeFieldContainer(listener, message_descriptor)
function containers.RepeatedCompositeFieldContainer(listener, message_descriptor)
local o = {
_listener = listener,
_message_descriptor = message_descriptor
Expand All @@ -71,9 +71,11 @@ local _RSFC_meta = {
}
_RSFC_meta.__index = _RSFC_meta

function RepeatedScalarFieldContainer(listener, type_checker)
function containers.RepeatedScalarFieldContainer(listener, type_checker)
local o = {}
o._listener = listener
o._type_checker = type_checker
return setmetatable(o, _RSFC_meta)
end

return containers
66 changes: 36 additions & 30 deletions protobuf/decoder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ local pb = require "protobuf.pb"
local encoder = require "protobuf.encoder"
local wire_format = require "protobuf.wire_format"

module "protobuf.decoder"
local decoder = {}

local _DecodeVarint = pb.varint_decoder
local _DecodeSignedVarint = pb.signed_varint_decoder

local _DecodeVarint32 = pb.varint_decoder
local _DecodeSignedVarint32 = pb.signed_varint_decoder

ReadTag = pb.read_tag
decoder.ReadTag = pb.read_tag

local function _SimpleDecoder(wire_type, decode_value)
return function(field_number, is_repeated, is_packed, key, new_default)
Expand Down Expand Up @@ -110,7 +110,7 @@ end
local function _StructPackDecoder(wire_type, value_size, format)
local struct_unpack = pb.struct_unpack

function InnerDecode(buffer, pos)
local function InnerDecode(buffer, pos)
local new_pos = pos + value_size
local result = struct_unpack(format, buffer, pos)
return result, new_pos
Expand All @@ -122,27 +122,27 @@ local function _Boolean(value)
return value ~= 0
end

Int32Decoder = _SimpleDecoder(wire_format.WIRETYPE_VARINT, _DecodeSignedVarint32)
EnumDecoder = Int32Decoder
decoder.Int32Decoder = _SimpleDecoder(wire_format.WIRETYPE_VARINT, _DecodeSignedVarint32)
decoder.EnumDecoder = decoder.Int32Decoder

Int64Decoder = _SimpleDecoder(wire_format.WIRETYPE_VARINT, _DecodeSignedVarint)
decoder.Int64Decoder = _SimpleDecoder(wire_format.WIRETYPE_VARINT, _DecodeSignedVarint)

UInt32Decoder = _SimpleDecoder(wire_format.WIRETYPE_VARINT, _DecodeVarint32)
UInt64Decoder = _SimpleDecoder(wire_format.WIRETYPE_VARINT, _DecodeVarint)
decoder.UInt32Decoder = _SimpleDecoder(wire_format.WIRETYPE_VARINT, _DecodeVarint32)
decoder.UInt64Decoder = _SimpleDecoder(wire_format.WIRETYPE_VARINT, _DecodeVarint)

SInt32Decoder = _ModifiedDecoder(wire_format.WIRETYPE_VARINT, _DecodeVarint32, wire_format.ZigZagDecode32)
SInt64Decoder = _ModifiedDecoder(wire_format.WIRETYPE_VARINT, _DecodeVarint, wire_format.ZigZagDecode64)
decoder.SInt32Decoder = _ModifiedDecoder(wire_format.WIRETYPE_VARINT, _DecodeVarint32, wire_format.ZigZagDecode32)
decoder.SInt64Decoder = _ModifiedDecoder(wire_format.WIRETYPE_VARINT, _DecodeVarint, wire_format.ZigZagDecode64)

Fixed32Decoder = _StructPackDecoder(wire_format.WIRETYPE_FIXED32, 4, string.byte('I'))
Fixed64Decoder = _StructPackDecoder(wire_format.WIRETYPE_FIXED64, 8, string.byte('Q'))
SFixed32Decoder = _StructPackDecoder(wire_format.WIRETYPE_FIXED32, 4, string.byte('i'))
SFixed64Decoder = _StructPackDecoder(wire_format.WIRETYPE_FIXED64, 8, string.byte('q'))
FloatDecoder = _StructPackDecoder(wire_format.WIRETYPE_FIXED32, 4, string.byte('f'))
DoubleDecoder = _StructPackDecoder(wire_format.WIRETYPE_FIXED64, 8, string.byte('d'))
decoder.Fixed32Decoder = _StructPackDecoder(wire_format.WIRETYPE_FIXED32, 4, string.byte('I'))
decoder.Fixed64Decoder = _StructPackDecoder(wire_format.WIRETYPE_FIXED64, 8, string.byte('Q'))
decoder.SFixed32Decoder = _StructPackDecoder(wire_format.WIRETYPE_FIXED32, 4, string.byte('i'))
decoder.SFixed64Decoder = _StructPackDecoder(wire_format.WIRETYPE_FIXED64, 8, string.byte('q'))
decoder.FloatDecoder = _StructPackDecoder(wire_format.WIRETYPE_FIXED32, 4, string.byte('f'))
decoder.DoubleDecoder = _StructPackDecoder(wire_format.WIRETYPE_FIXED64, 8, string.byte('d'))

BoolDecoder = _ModifiedDecoder(wire_format.WIRETYPE_VARINT, _DecodeVarint, _Boolean)
decoder.BoolDecoder = _ModifiedDecoder(wire_format.WIRETYPE_VARINT, _DecodeVarint, _Boolean)

function StringDecoder(field_number, is_repeated, is_packed, key, new_default)
function decoder.StringDecoder(field_number, is_repeated, is_packed, key, new_default)
local DecodeVarint = _DecodeVarint
local sub = string.sub
--local unicode = unicode
Expand Down Expand Up @@ -184,7 +184,7 @@ function StringDecoder(field_number, is_repeated, is_packed, key, new_default)
end
end

function BytesDecoder(field_number, is_repeated, is_packed, key, new_default)
function decoder.BytesDecoder(field_number, is_repeated, is_packed, key, new_default)
local DecodeVarint = _DecodeVarint
local sub = string.sub
assert(not is_packed)
Expand Down Expand Up @@ -225,7 +225,7 @@ function BytesDecoder(field_number, is_repeated, is_packed, key, new_default)
end
end

function MessageDecoder(field_number, is_repeated, is_packed, key, new_default)
function decoder.MessageDecoder(field_number, is_repeated, is_packed, key, new_default)
local DecodeVarint = _DecodeVarint
local sub = string.sub
assert(not is_packed)
Expand Down Expand Up @@ -275,21 +275,21 @@ function MessageDecoder(field_number, is_repeated, is_packed, key, new_default)
end
end

function _SkipVarint(buffer, pos, pend)
local function _SkipVarint(buffer, pos, pend)
local value
value, pos = _DecodeVarint(buffer, pos)
return pos
end

function _SkipFixed64(buffer, pos, pend)
local function _SkipFixed64(buffer, pos, pend)
pos = pos + 8
if pos > pend then
error('Truncated message.')
end
return pos
end

function _SkipLengthDelimited(buffer, pos, pend)
local function _SkipLengthDelimited(buffer, pos, pend)
local size
size, pos = _DecodeVarint(buffer, pos)
pos = pos + size
Expand All @@ -299,25 +299,29 @@ function _SkipLengthDelimited(buffer, pos, pend)
return pos
end

function _SkipFixed32(buffer, pos, pend)
local function _SkipFixed32(buffer, pos, pend)
pos = pos + 4
if pos > pend then
error('Truncated message.')
end
return pos
end

function _RaiseInvalidWireType(buffer, pos, pend)
local function _Unsupported(buffer, pos, pend)
error('Field not supported.')
end

local function _RaiseInvalidWireType(buffer, pos, pend)
error('Tag had invalid wire type.')
end

function _FieldSkipper()
WIRETYPE_TO_SKIPPER = {
local function _FieldSkipper()
local WIRETYPE_TO_SKIPPER = {
_SkipVarint,
_SkipFixed64,
_SkipLengthDelimited,
_SkipGroup,
_EndGroup,
_Unsupported, --_SkipGroup,
_Unsupported, --_EndGroup,
_SkipFixed32,
_RaiseInvalidWireType,
_RaiseInvalidWireType,
Expand All @@ -333,4 +337,6 @@ function _FieldSkipper()
end
end

SkipField = _FieldSkipper()
decoder.SkipField = _FieldSkipper()

return decoder
6 changes: 4 additions & 2 deletions protobuf/descriptor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
--------------------------------------------------------------------------------
--

module "protobuf.descriptor"
local descriptor = {}

FieldDescriptor = {
descriptor.FieldDescriptor = {
TYPE_DOUBLE = 1,
TYPE_FLOAT = 2,
TYPE_INT64 = 3,
Expand Down Expand Up @@ -58,3 +58,5 @@ FieldDescriptor = {
LABEL_REPEATED = 3,
MAX_LABEL = 3
}

return descriptor
Loading