Skip to content

Commit

Permalink
THRIFT-4386 Add Lua 5.3/5.4 support
Browse files Browse the repository at this point in the history
Clint: lua
Patch: Thomas Bruggink

This closes #3012
  • Loading branch information
thomasbruggink authored and Jens-G committed Dec 9, 2024
1 parent f54bdbd commit eb684d3
Show file tree
Hide file tree
Showing 20 changed files with 410 additions and 59 deletions.
4 changes: 2 additions & 2 deletions build/docker/ubuntu-jammy/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ RUN apt-get install -y --no-install-recommends \

RUN apt-get install -y --no-install-recommends \
`# Lua dependencies` \
lua5.2 \
lua5.2-dev
lua5.4 \
liblua5.4-dev
# https://bugs.launchpad.net/ubuntu/+source/lua5.3/+bug/1707212
# lua5.3 does not install alternatives!
# need to update our luasocket code, lua doesn't have luaL_openlib any more
Expand Down
13 changes: 13 additions & 0 deletions compiler/cpp/src/thrift/generate/t_lua_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,9 @@ string t_lua_generator::render_const_value(t_type* type, t_const_value* value) {
out << value->get_double();
}
break;
case t_base_type::TYPE_UUID:
out << "TUUIDfromString(" << value->get_string() << ")";
break;
default:
throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase);
}
Expand Down Expand Up @@ -727,6 +730,8 @@ void t_lua_generator::generate_process_function(ostream& out,
if (!tfunction->is_oneway()) {
out << indent() << "local result = " << resultname
<< ":new{}" << '\n';
} else {
out << indent() << "oprot.trans:flushOneway()" << '\n';
}

out << indent() << "local status, res = pcall(self.handler." << fn_name
Expand Down Expand Up @@ -845,6 +850,9 @@ void t_lua_generator::generate_deserialize_field(ostream& out,
case t_base_type::TYPE_DOUBLE:
out << "readDouble()";
break;
case t_base_type::TYPE_UUID:
out << "readUuid()";
break;
default:
throw "compiler error: no PHP name for base type " + t_base_type::t_base_name(tbase);
}
Expand Down Expand Up @@ -1000,6 +1008,9 @@ void t_lua_generator::generate_serialize_field(ostream& out, t_field* tfield, st
case t_base_type::TYPE_DOUBLE:
out << "writeDouble(" << name << ")";
break;
case t_base_type::TYPE_UUID:
out << "writeUuid(" << name << ")";
break;
default:
throw "compiler error: no PHP name for base type " + t_base_type::t_base_name(tbase);
}
Expand Down Expand Up @@ -1151,6 +1162,8 @@ string t_lua_generator::type_to_enum(t_type* type) {
return "TType.I64";
case t_base_type::TYPE_DOUBLE:
return "TType.DOUBLE";
case t_base_type::TYPE_UUID:
return "TType.UUID";
default:
throw "compiler error: unhandled type";
}
Expand Down
35 changes: 33 additions & 2 deletions lib/lua/TBinaryProtocol.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
--

require 'TProtocol'
require 'libluabpack'
require 'libluabitwise'
local libluabpack = require 'libluabpack'
local libluabitwise = require 'libluabitwise'

TBinaryProtocol = __TObject.new(TProtocolBase, {
__type = 'TBinaryProtocol',
Expand Down Expand Up @@ -111,6 +111,11 @@ function TBinaryProtocol:writeI32(i32)
self.trans:write(buff)
end

function TBinaryProtocol:writeUI32(i32)
local buff = libluabpack.bpack('I', i32)
self.trans:write(buff)
end

function TBinaryProtocol:writeI64(i64)
local buff = libluabpack.bpack('l', i64)
self.trans:write(buff)
Expand All @@ -127,6 +132,13 @@ function TBinaryProtocol:writeString(str)
self.trans:write(str)
end

function TBinaryProtocol:writeUuid(uuid)
self:writeUI32(uuid.two)
self:writeUI32(uuid.three)
self:writeUI32(uuid.zero)
self:writeUI32(uuid.one)
end

function TBinaryProtocol:readMessageBegin()
local sz, ttype, name, seqid = self:readI32()
if sz < 0 then
Expand Down Expand Up @@ -226,6 +238,12 @@ function TBinaryProtocol:readI32()
return val
end

function TBinaryProtocol:readUI32()
local buff = self.trans:readAll(4)
local val = libluabpack.bunpack('I', buff)
return val
end

function TBinaryProtocol:readI64()
local buff = self.trans:readAll(8)
local val = libluabpack.bunpack('l', buff)
Expand All @@ -244,6 +262,19 @@ function TBinaryProtocol:readString()
return str
end

function TBinaryProtocol:readUuid()
local a = self:readUI32()
local b = self:readUI32()
local c = self:readUI32()
local d = self:readUI32()
return TUUID:new {
zero = c,
one = d,
two = a,
three = b
}
end

TBinaryProtocolFactory = TProtocolFactory:new{
__type = 'TBinaryProtocolFactory',
strictRead = false
Expand Down
66 changes: 50 additions & 16 deletions lib/lua/TCompactProtocol.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
--

require 'TProtocol'
require 'libluabpack'
require 'libluabitwise'
require 'liblualongnumber'
local libluabpack = require 'libluabpack'
local libluabitwise = require 'libluabitwise'
local liblualongnumber = require 'liblualongnumber'

TCompactProtocol = __TObject.new(TProtocolBase, {
__type = 'TCompactProtocol',
Expand Down Expand Up @@ -61,7 +61,8 @@ TCompactType = {
COMPACT_LIST = 0x09,
COMPACT_SET = 0x0A,
COMPACT_MAP = 0x0B,
COMPACT_STRUCT = 0x0C
COMPACT_STRUCT = 0x0C,
COMPACT_UUID = 0x0D,
}

TTypeToCompactType = {}
Expand All @@ -77,21 +78,23 @@ TTypeToCompactType[TType.LIST] = TCompactType.COMPACT_LIST
TTypeToCompactType[TType.SET] = TCompactType.COMPACT_SET
TTypeToCompactType[TType.MAP] = TCompactType.COMPACT_MAP
TTypeToCompactType[TType.STRUCT] = TCompactType.COMPACT_STRUCT
TTypeToCompactType[TType.UUID] = TCompactType.COMPACT_UUID

CompactTypeToTType = {}
CompactTypeToTType[TType.STOP] = TType.STOP
CompactTypeToTType[TCompactType.COMPACT_BOOLEAN_TRUE] = TType.BOOL
CompactTypeToTType[TType.STOP] = TType.STOP
CompactTypeToTType[TCompactType.COMPACT_BOOLEAN_TRUE] = TType.BOOL
CompactTypeToTType[TCompactType.COMPACT_BOOLEAN_FALSE] = TType.BOOL
CompactTypeToTType[TCompactType.COMPACT_BYTE] = TType.BYTE
CompactTypeToTType[TCompactType.COMPACT_I16] = TType.I16
CompactTypeToTType[TCompactType.COMPACT_I32] = TType.I32
CompactTypeToTType[TCompactType.COMPACT_I64] = TType.I64
CompactTypeToTType[TCompactType.COMPACT_DOUBLE] = TType.DOUBLE
CompactTypeToTType[TCompactType.COMPACT_BINARY] = TType.STRING
CompactTypeToTType[TCompactType.COMPACT_LIST] = TType.LIST
CompactTypeToTType[TCompactType.COMPACT_SET] = TType.SET
CompactTypeToTType[TCompactType.COMPACT_MAP] = TType.MAP
CompactTypeToTType[TCompactType.COMPACT_STRUCT] = TType.STRUCT
CompactTypeToTType[TCompactType.COMPACT_BYTE] = TType.BYTE
CompactTypeToTType[TCompactType.COMPACT_I16] = TType.I16
CompactTypeToTType[TCompactType.COMPACT_I32] = TType.I32
CompactTypeToTType[TCompactType.COMPACT_I64] = TType.I64
CompactTypeToTType[TCompactType.COMPACT_DOUBLE] = TType.DOUBLE
CompactTypeToTType[TCompactType.COMPACT_BINARY] = TType.STRING
CompactTypeToTType[TCompactType.COMPACT_LIST] = TType.LIST
CompactTypeToTType[TCompactType.COMPACT_SET] = TType.SET
CompactTypeToTType[TCompactType.COMPACT_MAP] = TType.MAP
CompactTypeToTType[TCompactType.COMPACT_STRUCT] = TType.STRUCT
CompactTypeToTType[TCompactType.COMPACT_UUID] = TType.UUID

function TCompactProtocol:resetLastField()
self.lastField = {}
Expand Down Expand Up @@ -197,6 +200,11 @@ function TCompactProtocol:writeI32(i32)
self:writeVarint32(libluabpack.i32ToZigzag(i32))
end

function TCompactProtocol:writeUI32(i32)
local buff = libluabpack.bpack('I', i32)
self.trans:write(buff)
end

function TCompactProtocol:writeI64(i64)
self:writeVarint64(libluabpack.i64ToZigzag(i64))
end
Expand All @@ -211,6 +219,13 @@ function TCompactProtocol:writeString(str)
self:writeBinary(str)
end

function TCompactProtocol:writeUuid(uuid)
self:writeUI32(uuid.two)
self:writeUI32(uuid.three)
self:writeUI32(uuid.zero)
self:writeUI32(uuid.one)
end

function TCompactProtocol:writeBinary(str)
-- Should be utf-8
self:writeVarint32(string.len(str))
Expand Down Expand Up @@ -385,6 +400,12 @@ function TCompactProtocol:readI32()
return value
end

function TCompactProtocol:readUI32()
local buff = self.trans:readAll(4)
local val = libluabpack.bunpack('I', buff)
return val
end

function TCompactProtocol:readI64()
local value = self:readVarint64()
return value
Expand All @@ -400,6 +421,19 @@ function TCompactProtocol:readString()
return self:readBinary()
end

function TCompactProtocol:readUuid()
local a = self:readUI32()
local b = self:readUI32()
local c = self:readUI32()
local d = self:readUI32()
return TUUID:new {
zero = c,
one = d,
two = a,
three = b
}
end

function TCompactProtocol:readBinary()
local size = self:readVarint32()
if size <= 0 then
Expand Down
2 changes: 1 addition & 1 deletion lib/lua/TFramedTransport.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
--

require 'TTransport'
require 'libluabpack'
local libluabpack = require 'libluabpack'

TFramedTransport = TTransportBase:new{
__type = 'TFramedTransport',
Expand Down
13 changes: 11 additions & 2 deletions lib/lua/THttpTransport.lua
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,21 @@ function THttpTransport:writeHttpHeader(content_len)
end
end

function THttpTransport:flushOneway()
self.wBuf = ''
self:writeHttpHeader(0)
self.trans:flush()
end

function THttpTransport:flush()
-- If the write fails we still want wBuf to be clear
local tmp = self.wBuf
self.wBuf = ''
self:writeHttpHeader(string.len(tmp))
self.trans:write(tmp)
local dataLen = string.len(tmp)
self:writeHttpHeader(dataLen)
if dataLen > 0 then
self.trans:write(tmp)
end
self.trans:flush()
end

Expand Down
19 changes: 15 additions & 4 deletions lib/lua/TJsonProtocol.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
--

require 'TProtocol'
require 'libluabpack'
require 'libluabitwise'
local libluabpack = require 'libluabpack'
local libluabitwise = require 'libluabitwise'
local liblualongnumber = require 'liblualongnumber'

TJSONProtocol = __TObject.new(TProtocolBase, {
__type = 'TJSONProtocol',
Expand All @@ -42,6 +43,7 @@ TTypeToString[TType.STRUCT] = "rec"
TTypeToString[TType.LIST] = "lst"
TTypeToString[TType.SET] = "set"
TTypeToString[TType.MAP] = "map"
TTypeToString[TType.UUID] = "uid"

StringToTType = {
tf = TType.BOOL,
Expand All @@ -54,7 +56,8 @@ StringToTType = {
rec = TType.STRUCT,
map = TType.MAP,
set = TType.SET,
lst = TType.LIST
lst = TType.LIST,
uid = TType.UUID,
}

JSONNode = {
Expand Down Expand Up @@ -402,13 +405,17 @@ function TJSONProtocol:writeI64(i64)
end

function TJSONProtocol:writeDouble(dub)
self:writeJSONDouble(string.format("%.16f", dub))
self:writeJSONDouble(string.format("%.20f", dub))
end

function TJSONProtocol:writeString(str)
self:writeJSONString(str)
end

function TJSONProtocol:writeUuid(uuid)
self:writeJSONString(uuid:getString())
end

function TJSONProtocol:writeBinary(str)
-- Should be utf-8
self:writeJSONBase64(str)
Expand Down Expand Up @@ -706,6 +713,10 @@ function TJSONProtocol:readString()
return self:readJSONString()
end

function TJSONProtocol:readUuid()
return TUUIDfromString(self:readJSONString())
end

function TJSONProtocol:readBinary()
return self:readJSONBase64()
end
Expand Down
4 changes: 4 additions & 0 deletions lib/lua/TProtocol.lua
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ function TProtocolBase:writeI32(i32) end
function TProtocolBase:writeI64(i64) end
function TProtocolBase:writeDouble(dub) end
function TProtocolBase:writeString(str) end
function TProtocolBase:writeUuid(uuid) end
function TProtocolBase:readMessageBegin() end
function TProtocolBase:readMessageEnd() end
function TProtocolBase:readStructBegin() end
Expand All @@ -105,6 +106,7 @@ function TProtocolBase:readI32() end
function TProtocolBase:readI64() end
function TProtocolBase:readDouble() end
function TProtocolBase:readString() end
function TProtocolBase:readUuid() end

function TProtocolBase:skip(ttype)
if ttype == TType.BOOL then
Expand Down Expand Up @@ -151,6 +153,8 @@ function TProtocolBase:skip(ttype)
self:skip(ettype)
end
self:readListEnd()
elseif ttype == TType.UUID then
self:readUuid()
else
terror(TProtocolException:new{
message = 'Invalid data'
Expand Down
2 changes: 1 addition & 1 deletion lib/lua/TSocket.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
--

require 'TTransport'
require 'libluasocket'
local luasocket = require 'libluasocket'

-- TSocketBase
TSocketBase = TTransportBase:new{
Expand Down
2 changes: 2 additions & 0 deletions lib/lua/TTransport.lua
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ function TTransportBase:readAll(len)
return buf
end
function TTransportBase:write(buf) end
-- flushOneway is a NOOP for most transport types.
function TTransportBase:flushOneway() end
function TTransportBase:flush() end

TServerTransportBase = __TObject:new{
Expand Down
Loading

0 comments on commit eb684d3

Please sign in to comment.