Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Lua bitwise operations using explicit shim #957

Merged
merged 3 commits into from
Jul 18, 2020
Merged
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
1 change: 0 additions & 1 deletion .luacheckrc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ globals = {
"SU",
"std",
"pl",
"bit32",
"SYSTEM_SILE_PATH",
"SHARED_LIB_EXT",
"ProFi"
Expand Down
4 changes: 3 additions & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ AX_LUA_HEADERS
AX_LUA_LIBS

AS_IF([test "x$with_system_luarocks" = "xyes"], [
AX_LUA_MODULE([bit32], [bit32])
AS_IF([test "$LUA_SHORT_VERSION" -lt 52],
AX_LUA_MODULE([bit32], [bit32])
)
AX_LUA_MODULE([lpeg], [lpeg])
AX_LUA_MODULE([cassowary], [cassowary])
AS_IF([test "$LUA_SHORT_VERSION" -lt 53],
Expand Down
8 changes: 5 additions & 3 deletions core/harfbuzz-shaper.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
if not SILE.shapers then SILE.shapers = { } end
local hb = require("justenoughharfbuzz")
local icu = require("justenoughicu")
local bitshim = require("bitshim")

if not SILE.shapers then SILE.shapers = { } end

SILE.settings.declare({
name = "harfbuzz.subshapers",
Expand Down Expand Up @@ -62,9 +64,9 @@ SILE.shapers.harfbuzz = pl.class({
SU.debug("fonts", "Resolved font family '"..opts.family.."' -> "..(face and face.filename))
if not face or not face.filename then SU.error("Couldn't find face '"..opts.family.."'") end
if SILE.makeDeps then SILE.makeDeps:add(face.filename) end
if bit32.rshift(face.index, 16) ~= 0 then
if bitshim.rshift(face.index, 16) ~= 0 then
SU.warn("GX feature in '"..opts.family.."' is not supported, fallback to regular font face.")
face.index = bit32.band(face.index, 0xff)
face.index = bitshim.band(face.index, 0xff)
end
local fh, err = io.open(face.filename, "rb")
if err then SU.error("Can't open font file '"..face.filename.."': "..err) end
Expand Down
1 change: 0 additions & 1 deletion core/sile.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
-- Initialize Lua environment and global utilities
local lua_version = _VERSION:sub(-3)
if lua_version < "5.3" then require("compat53") end -- Backport of lots of Lua 5.3 features to Lua 5.[12]
bit32 = bit32 or require("bit32") -- Backport of Lua 5.2+ bitwise functions to Lua 5.1
pl = require("pl.import_into")() -- Penlight on-demand module loader
if (os.getenv("SILE_COVERAGE")) then require("luacov") end

Expand Down
9 changes: 5 additions & 4 deletions core/utilities.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local bitshim = require("bitshim")
local utilities = {}

local epsilon = 1E-12
Expand Down Expand Up @@ -314,9 +315,9 @@ utilities.codepoint = function (uchar)
seq = c < 0x80 and 1 or c < 0xE0 and 2 or c < 0xF0 and 3 or
c < 0xF8 and 4 or --c < 0xFC and 5 or c < 0xFE and 6 or
error("invalid UTF-8 character sequence")
val = bit32.band(c, 2^(8-seq) - 1)
val = bitshim.band(c, 2^(8-seq) - 1)
else
val = bit32.bor(bit32.lshift(val, 6), bit32.band(c, 0x3F))
val = bitshim.bor(bitshim.lshift(val, 6), bitshim.band(c, 0x3F))
end
seq = seq - 1
end
Expand Down Expand Up @@ -403,10 +404,10 @@ utilities.splitUtf8 = function (str) -- Return an array of UTF8 strings each rep
seq = c < 0x80 and 1 or c < 0xE0 and 2 or c < 0xF0 and 3 or
c < 0xF8 and 4 or --c < 0xFC and 5 or c < 0xFE and 6 or
error("invalid UTF-8 character sequence")
val = bit32.band(c, 2^(8-seq) - 1)
val = bitshim.band(c, 2^(8-seq) - 1)
this = this .. str[i]
else
val = bit32.bor(bit32.lshift(val, 6), bit32.band(c, 0x3F))
val = bitshim.bor(bitshim.lshift(val, 6), bitshim.band(c, 0x3F))
this = this .. str[i]
end
seq = seq - 1
Expand Down
50 changes: 50 additions & 0 deletions lua-libraries/bitshim.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
--[[ This module smooths over all the various Lua bit libraries

Sourced from:
https://github.com/daurnimator/lua-http/blob/v0.3/http/bit.lua

Note using these functions one must me careful to do bit operations only:
- on bytes (8 bits),
- with quantities <= LONG_MAX (0x7fffffff)
- band with 0x80000000 that is subsequently compared with 0
]]

-- Lua 5.3 has built-in bit operators, wrap them in a function.
if _VERSION == "Lua 5.3" then
-- Use debug.getinfo to get correct file+line numbers for loaded snippet
local info = debug.getinfo(1, "Sl")
return assert(load(("\n"):rep(info.currentline+1)..[[return {
band = function(a, b) return a & b end;
bor = function(a, b) return a | b end;
bxor = function(a, b) return a ~ b end;
lshift = function(a, b) return a << b end;
rshift = function(a, b) return a >> b end;
}]], info.source))()
end

-- The "bit" library that comes with luajit
-- also available for lua 5.1 as "luabitop": http://bitop.luajit.org/
local has_bit, bit = pcall(require, "bit")
if has_bit then
return {
band = bit.band;
bor = bit.bor;
bxor = bit.bxor;
lshift = bit.lshift;
rshift = bit.rshift;
}
end

-- The "bit32" library shipped with lua 5.2
local has_bit32, bit32 = pcall(require, "bit32")
if has_bit32 then
return {
band = bit32.band;
bor = bit32.bor;
bxor = bit32.bxor;
lshift = bit32.lshift;
rshift = bit32.rshift;
}
end

error("Please install a bit library")