Skip to content
New issue

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

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

Already on GitHub? # to your account

try using native bitwise operations without checking lua version #177

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
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
27 changes: 18 additions & 9 deletions http/bit.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,26 @@ The bit operations are only done
This means we can ignore the differences between bit libraries.
]]

-- 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;
}]], info.source))()
-- Lua 5.1's load function doesn't support reading strings, only
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use loadstring in 5.1?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I guess it's six of one, half-dozen of the other.

This could be replaced with something like "local l = load; if loadstring then l = loadstring"

I can switch to something like that if you prefer, let me know!

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess loadstring doesn't have a source argument; so load is best.

It annoys me a little that we don't use straight up load in newer versions of lua.
I just realised we already depend on compat-5.3 in lua 5.1, so how about:

local load = load
if _VERSION == "Lua 5.1" then
    load = require "compat53.module".load
end

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re: loadstring - in the docs they list the second parameter as chunk instead of source, but it's the same thing, loadstring should work and show the source filename as intended.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See latest push, I just did local load = loadstring or load

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't want to use loadstring if load is available.

-- functions.
local function string_loader(str)
local sent = false
return function()
if sent then return nil end
sent = false
return str
end
end

-- Lua 5.3+ has built-in bit operators, wrap them in a function.
local info = debug.getinfo(1, "Sl")
local has_bitwise, bitwise = pcall(load(string_loader(("\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;
}]]), info.source))
if has_bitwise then return bitwise 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")
Expand Down