Image
--[[

Version 1.01 2nd May 2023
Author: Hayasidist

How to use this file.
=====================

Copy this file to the Custom Content / Scripts / Utility folder.

In any script that needs to run in both Lua 5.4 and Lua 5.2 (i.e. Moho 14 and previous versions) include a call to:

	HS_LuaCompatibility ()

Ideally this should be in any "run once" code (e.g. LoadPrefs or Run) but others such as IsRelevant will also work.

The concept is that code developed for Lua 5.2 will run unchanged apart from the inclusion of the call to HS_LuaCompatibility () 

If a script is intended to be run in only a Lua 5.2 or a Lua 5.4 environment, this script is unnecessary.

 

More information
================ 

This file contains functions that have been developed to allow scripts to run in Moho 14 (Lua 5.4) and earlier versions of Moho (Lua 5.2)

It will be updated in response to any bugs and to address any other compatibility issues that can be resolved in this way.

The intention is to allow bit32.xxx and the deprecated math.atan2 and math.pow calls to be usable in a Lua 5.4 environment.

This is a transitionary service and will be deprecated once there is no signifucant user base for moho 13.5 (i.e. Lua 5.2) and earlier versions.
Scripts developed only for Moho 14 or later should use the Lua 5.4 bitwise operators.



About Bitwise operations
========================

http://www.lua.org/manual/5.2/manual.html#6.7 describes the set of bit32 functions. Note that they, and the replacement functions here, work on 32 bit integers.
http://www.lua.org/manual/5.4/manual.html#3.4.2 describes the bitwise operators. Note that these are intended for 64 bit integers.

for completeness, this is the list of Moho bitwise functions:

MOHO.bit(p)
MOHO.hasbit(x, p) -- Typical call: if hasbit(x, bit(3)) then ...
MOHO.setbit(x, p)
MOHO.clearbit(x, p)


The bit32 functions and their corresponding bitwise operations are:
band		--	a & b & ...
bnot		-- 	~a
bor		--	a | b | ...
bxor		--	a ~ b ~ ...
btest		--	band (...) ~= 0

lshift		--	a << n (if n<0 this shifts right; if n>0 this shifts left)
rshift		--	a >> n (if n>0 this shifts right; if n<0 this shifts left)
arshift		--	a >> n (if n>0 this shifts right; if n<0 this shifts left)

extract		--	no direct replacement
replace		--	no direct replacement

lrotate		--	no direct replacement
rrotate		--	no direct replacement

]]

function HS_LuaCompatibility ()

	if type(math.atan2) == "nil" then
		math.atan2 = math.atan
	end

	if type(math.pow) == "nil" then
		local function HSMath_pow (_v1, _v2)
			if (type(_v1) ~="number") or (type(_v2) ~="number") then
				error ("math.pow - number expected, got non-numeric", 2)
			end
			return _v1 ^ _v2
		end
		math.pow = HSMath_pow
	end


	if type (bit32) == "nil" then
		bit32 = {}

		local function HSBit_band(...)
			local arg = {...}
			local i
			local rtx = 0

			if #arg == 1 then
				return arg[1]
			elseif #arg == 0 then
				return 0
			end

			rtx = arg[1]

			for i = 2, #arg do
				rtx = load(("return(%s & %s)"):format(rtx, arg[i]))()
			end
			return rtx
		end


		local function HSBit_bnot(_v1)
			return HSBit_band(load(("return(~%s)"):format(_v1))(), 0xFFFFFFFF)
		end

		local function HSBit_bor(...)
			local arg = {...}
			local i
			local rtx = 0

			if #arg == 1 then
				return arg[1]
			elseif #arg == 0 then
				return 0
			end

			rtx = arg[1]

			for i = 2, #arg do
				rtx = load(("return(%s | %s)"):format(rtx, arg[i]))()
			end
			return rtx
		end

		local function HSBit_bxor(...)
			local arg = {...}
			local i
			local rtx = 0

			if #arg == 1 then
				return arg[1]
			elseif #arg == 0 then
				return 0
			end

			rtx = arg[1]

			for i = 2, #arg do
				rtx = load(("return(%s ~ %s)"):format(rtx, arg[i]))()
			end
			return rtx
		end

		local function HSBit_btest (...)
			local arg = {...}
			return HSBit_band(arg) ~= 0
		end

		local function HSBit_lshift(_v1, _v2)
			return HSBit_band(load(("return(%s << %s)"):format(_v1, _v2))(), 0xFFFFFFFF)
		end

		local function HSBit_rshift(_v1, _v2)
			return HSBit_band(load(("return(%s >> %s)"):format(_v1, _v2))(), 0xFFFFFFFF)
		end

		local function HSBit_arshift(_v1, _v2)
			return HSBit_band(HSBit_rshift(_v1, _v2), 0xFFFFFFFF)
		end

		local function HSBit_lrotate (_v1, _disp)
			_disp = _disp or 0
			_disp = _disp % 32
			_v1 = HSBit_band(_v1, 0xFFFFFFFF) 
			local cBit = HSBit_band(_v1, 0x80000000)
			_v1 = HSBit_lshift(_v1, _disp)
			cBit = HSBit_rshift(cBit, 32 - _disp)
			return HSBit_bor(_v1, cBit)
		end

		local function HSBit_rrotate (_v1, _disp)
			_disp = _disp or 0
			_disp = _disp % 32
			_v1 = HSBit_band(_v1, 0xFFFFFFFF) 
			local cBit = HSBit_band(_v1, 0x1)
			_v1 = HSBit_rshift(_v1, _disp)
			cBit = HSBit_lshift(cBit, 32 - _disp)
			return HSBit_bor(_v1, cBit)
		end


		local function HSBit_extract (_v1, _start, _ct)
			_start = _start or 0
			_ct = _ct or 1
			_v1 = HSBit_band(_v1, 0xFFFFFFFF) 
			if _start > 31 or _start < 0 then
				error ("bit32.extract - bit out of range", 2)
			end
			if _ct > 32 -_start or _ct < 0 then
				error ("bit32.extract - bit out of range", 2)
			end
			local i, m = 0, 0
			for i = 1, _ct do
				m = HSBit_bor(HSBit_lshift(m, 1),1)
			end
			return (HSBit_band(HSBit_rshift(_v1, _start),m))
		end

		local function HSBit_replace (_v1, _v2, _start, _ct)
			_start = _start or 0
			_ct = _ct or 1
			_v1 = HSBit_band(_v1, 0xFFFFFFFF) 
			if _start > 31 or _start < 0 then
				error ("bit32.replace - bit out of range", 2)
			end
			if _ct > 32 -_start or _ct < 0 then
				error ("bit32.replace - bit out of range", 2)
			end
			local i, m = 0, 0
			for i = 1, _ct do
				m = HSBit_bor(HSBit_lshift(m, 1),1)
			end
			_v2 = HSBit_band(_v2, m)
			m = HSBit_bnot(HSBit_lshift(m, _start))
			_v2 = HSBit_lshift(_v2, _start)
			return (HSBit_bor(HSBit_band(_v1, m), _v2))
		end

		bit32.arshift = HSBit_arshift
		bit32.band = HSBit_band
		bit32.bnot = HSBit_bnot
		bit32.bor = HSBit_bor
		bit32.btest = HSBit_btest
		bit32.bxor = HSBit_bxor
		bit32.extract = HSBit_extract
		bit32.replace = HSBit_replace
		bit32.lshift = HSBit_lshift
		bit32.rshift = HSBit_rshift
		bit32.lrotate = HSBit_lrotate
		bit32.rrotate = HSBit_rrotate

	end

end



Lua Compatibility
Listed

Script type: Utility

Uploaded: Sep 12 2023, 06:57

Script Version: 0

Provides Lua 5.4 functions for Lua 5.2 scripting
Version 1.01 2nd May 2023

How to use this file.
=====================

Copy this file to the Custom Content / Scripts / Utility folder.

In any script that needs to run in both Lua 5.4 and Lua 5.2 (i.e. Moho 14 and previous versions) include a call to:

HS_LuaCompatibility ()

Ideally this should be in any "run once" code (e.g. LoadPrefs or Run) but others such as IsRelevant will also work.

The concept is that code developed for Lua 5.2 will run unchanged apart from the inclusion of the call to HS_LuaCompatibility ()

If a script is intended to be run in only a Lua 5.2 or a Lua 5.4 environment, this script is unnecessary.



More information
================

This file contains functions that have been developed to allow scripts to run in Moho 14 (Lua 5.4) and earlier versions of Moho (Lua 5.2)

It will be updated in response to any bugs and to address any other compatibility issues that can be resolved in this way.

The intention is to allow bit32.xxx and the deprecated math.atan2 and math.pow calls to be usable in a Lua 5.4 environment.

This is a transitionary service and will be deprecated once there is no significant user base for Moho 13.5 (i.e. Lua 5.2) and earlier versions.

Scripts developed only for Moho 14 or later should use the Lua 5.4 functions directly.



About Bitwise operations
========================

http://www.lua.org/manual/5.2/manual.html#6.7 describes the set of bit32 functions. Note that they, and the replacement functions here, work on 32 bit integers.
http://www.lua.org/manual/5.4/manual.html#3.4.2 describes the bitwise operators. Note that these are intended for 64 bit integers.

for completeness, this is the list of Moho bitwise functions:

MOHO.bit(p)
MOHO.hasbit(x, p) -- Typical call: if hasbit(x, bit(3)) then ...
MOHO.setbit(x, p)
MOHO.clearbit(x, p)


The bit32 functions and their corresponding bitwise operations are:
band -- a & b & ...
bnot -- ~a
bor -- a | b | ...
bxor -- a ~ b ~ ...
btest -- band (...) ~= 0

lshift -- a << n (if n<0 this shifts right; if n>0 this shifts left)
rshift -- a >> n (if n>0 this shifts right; if n<0 this shifts left)
arshift -- a >> n (if n>0 this shifts right; if n<0 this shifts left)

extract -- no direct replacement
replace -- no direct replacement

lrotate -- no direct replacement
rrotate -- no direct replacement


This script, and all other scripts on this site are distributed as free software under the GNU General Public License 3.0 or later.
Downloads count: 725