Skip to content

Conversation

@dborovcanin
Copy link
Contributor

@dborovcanin dborovcanin commented Jun 4, 2025

Add support for bitwise operations: and, or, xor, not, lshift, rshift.

@dborovcanin dborovcanin force-pushed the add-bitwise branch 2 times, most recently from 2a77c7e to a2d27f3 Compare June 4, 2025 15:33
Signed-off-by: Dusan Borovcanin <[email protected]>
Copy link
Collaborator

@scr-oath scr-oath left a comment

Choose a reason for hiding this comment

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

Would you consider adding the Preload to https://github.com/vadv/gopher-lua-libs/blob/master/plugin/preload.go so that anyone who wants to just load everything can do so?

return
}

func intToU32(i int) (uint32, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

question: Why why not 64 bit?

Copy link
Contributor Author

@dborovcanin dborovcanin Jun 5, 2025

Choose a reason for hiding this comment

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

Mainly because numbers in Lua 5.1 are 64-bit floating-point, and Go uint64 overflows them so Go 64-bit uint cannot be presented properly in Lua 5.1 floating-point.

Copy link
Collaborator

Choose a reason for hiding this comment

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

makes sense; it's a little disappointing that it can't use the full size… I wonder if we should suffix methods with 32 to indicate that, but I won't press that and we can consider this resolved

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can add a suffix if you insist. I was considering that, but I didn't do it because it's impossible to have <operation>u64 and u8 or u16 can be handled with the existing version. Maybe we can add a suffix in the future if we support different versions (possibly even u64), but that is unlikely to happen.

Signed-off-by: Dusan Borovcanin <[email protected]>
Signed-off-by: Dusan Borovcanin <[email protected]>
Signed-off-by: Dusan Borovcanin <[email protected]>
Signed-off-by: Dusan Borovcanin <[email protected]>
Signed-off-by: Dusan Borovcanin <[email protected]>
Signed-off-by: Dusan Borovcanin <[email protected]>
Signed-off-by: Dusan Borovcanin <[email protected]>
Signed-off-by: Dusan Borovcanin <[email protected]>
Comment on lines +5 to +28
local tests = {
{
input1 = -3,
input2 = 23,
expected = nil,
err = "cannot convert negative int -3 to uint32",
},
{
input1 = 4294967296,
input2 = 23,
expected = nil,
err = "int 4294967296 overflows uint32",
},
{
input1 = 1,
input2 = 0,
expected = 0,
},
{
input1 = 111,
input2 = 222,
expected = 78,
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

praise: I like this style - I was thinking of doing it - Github copilot did the positional thing for me so I left it, but associative arrays are more descriptive!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! I completely missed your PR on my branch, so I did it this way, similarly to the tests you linked in the previous comments (there's a bit of repetition, but I hope that's acceptable).

return
}

func intToU32(i int) (uint32, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

makes sense; it's a little disappointing that it can't use the full size… I wonder if we should suffix methods with 32 to indicate that, but I won't press that and we can consider this resolved

@scr-oath
Copy link
Collaborator

scr-oath commented Jun 5, 2025

Did you have more you wanted to add? I can merge this, but as you said that it wasn't ready before, just wanted to give you another chance to self-review and/or update before I merge… just give me the go and I'll merge/cut a release.

Copy link
Owner

@vadv vadv left a comment

Choose a reason for hiding this comment

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

thank you!

@dborovcanin
Copy link
Contributor Author

Did you have more you wanted to add? I can merge this, but as you said that it wasn't ready before, just wanted to give you another chance to self-review and/or update before I merge… just give me the go and I'll merge/cut a release.

This is ready unless there are further comments. I could add a few more operations (e.g., rol, ror like in bitop), but that doesn’t seem necessary for now.

@scr-oath scr-oath merged commit ccb2795 into vadv:master Jun 6, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants