From 7eb9908f1afd1f235aef95543fb5d9e852257f0b Mon Sep 17 00:00:00 2001 From: Guldoman Date: Wed, 12 Jan 2022 00:07:14 +0100 Subject: [PATCH 1/3] Improve bit32 polyfill --- data/core/bit.lua | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/data/core/bit.lua b/data/core/bit.lua index f357d5fd..114ea3cb 100644 --- a/data/core/bit.lua +++ b/data/core/bit.lua @@ -12,19 +12,20 @@ local function mask(n) end function bit.extract(n, field, width) - local r = trim(field) - local f = width - r = (r >> f) & mask(width) - return r + local w = width or 1 + assert(w > 0, "width must be positive") + assert(field + w < LUA_NBITS and field + w >= 0, "trying to access non-existent bits") + local m = trim(n) + return m >> field & mask(w) end function bit.replace(n, v, field, width) - local r = trim(v); - local v = trim(field); - local f = width - local m = mask(width); - r = (r & ~(m << f)) | ((v & m) << f); - return r + local w = width or 1 + assert(w > 0, "width must be positive") + assert(field + w < LUA_NBITS and field + w >= 0, "trying to access non-existent bits") + local m = trim(n) + local x = v & mask(width); + return m & ~(mask(w) << field) | (x << field) end -return bit \ No newline at end of file +return bit From 51975472a93a35b9add46aca26bb8eaa44dbb443 Mon Sep 17 00:00:00 2001 From: Guldoman Date: Wed, 12 Jan 2022 00:07:53 +0100 Subject: [PATCH 2/3] Add bit32 polyfill globally --- data/core/start.lua | 2 ++ data/core/tokenizer.lua | 1 - 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/data/core/start.lua b/data/core/start.lua index 482f8bd0..b3bcc18e 100644 --- a/data/core/start.lua +++ b/data/core/start.lua @@ -32,3 +32,5 @@ end } table.pack = table.pack or pack or function(...) return {...} end table.unpack = table.unpack or unpack + +bit32 = bit32 or require "core.bit" diff --git a/data/core/tokenizer.lua b/data/core/tokenizer.lua index 03769c46..5fd8c69f 100644 --- a/data/core/tokenizer.lua +++ b/data/core/tokenizer.lua @@ -1,6 +1,5 @@ local syntax = require "core.syntax" local common = require "core.common" -local bit32 = require "core.bit" local tokenizer = {} From e51c76c72b79e1453b135de8acd12d3c50014b12 Mon Sep 17 00:00:00 2001 From: Guldoman Date: Wed, 12 Jan 2022 19:56:09 +0100 Subject: [PATCH 3/3] Assert for negative `field` in bit32 polyfill This more closely matches the behavior of lua5.2. --- data/core/bit.lua | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/data/core/bit.lua b/data/core/bit.lua index 114ea3cb..e55fb9bf 100644 --- a/data/core/bit.lua +++ b/data/core/bit.lua @@ -11,18 +11,23 @@ local function mask(n) return (~((ALLONES << 1) << ((n) - 1))) end +local function check_args(field, width) + assert(field >= 0, "field cannot be negative") + assert(width > 0, "width must be positive") + assert(field + width < LUA_NBITS and field + width >= 0, + "trying to access non-existent bits") +end + function bit.extract(n, field, width) local w = width or 1 - assert(w > 0, "width must be positive") - assert(field + w < LUA_NBITS and field + w >= 0, "trying to access non-existent bits") + check_args(field, w) local m = trim(n) return m >> field & mask(w) end function bit.replace(n, v, field, width) local w = width or 1 - assert(w > 0, "width must be positive") - assert(field + w < LUA_NBITS and field + w >= 0, "trying to access non-existent bits") + check_args(field, w) local m = trim(n) local x = v & mask(width); return m & ~(mask(w) << field) | (x << field)