Skip to content
Open
Changes from 4 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
6 changes: 6 additions & 0 deletions src/content/docs/script/learn-lua/from-lsl.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ local items: {number} = {1, 2, 3} -- tables, not lists
| Power | `llPow(2, 3)` | `2 ^ 3` | Native operator |
| Integer division | `7 / 4` → `1` | `7 // 4` → `1` | Use `//` not `/` |
| String length | `llStringLength(s)` | `#s` | Native operator |
| Bitwise AND | `llGetAgentInfo(id) & AGENT_TYPING` | `bit32.btest(ll.GetAgentInfo(id), AGENT_TYPING)` | No `&` operator |
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

btest() gives a boolean result - band() is wanted here

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

in my experience, band is more a footgun, because, most of the time, you want to put it in an if block. and only btest makes sense in an if statement. (I even wrote a linter rule about this because it bit me so hard: luau-lang/luau#2184)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

added a note

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Perhaps for idiomatic Lua, there are two modes of expression for bitwise AND:

  • b32.band() where the resulting value is important
  • (b32.btest() [and/or] b32.btest() [and/or] b32.btest() ...) where a conditional test is being constructed. Test varies whether you want all mask bits set or any mask bit set.

Anyway, changes accepted.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

btest seems to be the one that matches the main use of & in LSL:

> flags = bit32.bor(CONTROL_UP, CONTROL_LEFT)
> bit32.btest(flags, CONTROL_UP)
true
> bit32.btest(flags, CONTROL_LEFT)
true
> bit32.btest(flags, CONTROL_DOWN)
false

| Bitwise OR | `PASSIVE | SCRIPTED` | `bit32.bor(PASSIVE | SCRIPTED)` | No `|` operator |
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Should be 'bit32.bor(PASSIVE, SCRIPTED)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

fixed

| Bitwise NOT | `~0` | `bit32.bnot(0)` | No `~` operator |
| Bitwise eXclusive OR | `6 ^ 3` | `bit32.bxor(6, 3)` | `^` means Power instead |
| Bitwise Shift Left | `1 << 2` | `bit32.lshift(1, 2)` | No `<<` operator |
| Bitwise Shift Right | `0x80000000 >> 2` | `bit32.arshift(0x80000000, 2)` | No `>>` operator |
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

In LSL, this is an arithmetic shift on signed, 32-bit value, not bitwise on unsigned. '0x80000000 >> 2' produces the result -536870912. In Luau, 'bit32.arshift(0x80000000, 2)' is a pseudo-signed operator producing the result 3758096384. This is bit correct though not value correct but that's expected here. May wish to warn the reader.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

added a note


### Control Flow

Expand Down