Skip to content

Commit 74a4644

Browse files
committed
add bit and lua module
1 parent d56540e commit 74a4644

File tree

5 files changed

+672
-0
lines changed

5 files changed

+672
-0
lines changed
Lines changed: 278 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,278 @@
1+
# core.base.bit
2+
3+
This module provides bitwise operations for Lua scripts in Xmake.
4+
5+
::: tip TIP
6+
To use this module, you need to import it first: `import("core.base.bit")`
7+
:::
8+
9+
## Overview
10+
11+
Although Lua 5.4+ natively supports bitwise operators, Xmake can be configured to use LuaJIT as the runtime. To ensure compatibility across different Lua versions, Xmake provides the `bit` module for more generic bitwise operations.
12+
13+
This module provides a common set of bitwise operations that work consistently regardless of whether you're using standard Lua or LuaJIT.
14+
15+
## bit.band
16+
17+
- Bitwise AND operation
18+
19+
#### Function Prototype
20+
21+
::: tip API
22+
```lua
23+
bit.band(a: <number>, b: <number>)
24+
```
25+
:::
26+
27+
#### Parameter Description
28+
29+
| Parameter | Description |
30+
|-----------|-------------|
31+
| a | Required. First number |
32+
| b | Required. Second number |
33+
34+
#### Return Value
35+
36+
| Type | Description |
37+
|------|-------------|
38+
| number | Returns the bitwise AND result of a and b |
39+
40+
#### Usage
41+
42+
```lua
43+
import("core.base.bit")
44+
45+
local result = bit.band(5, 3) -- Returns 1
46+
```
47+
48+
## bit.bor
49+
50+
- Bitwise OR operation
51+
52+
#### Function Prototype
53+
54+
::: tip API
55+
```lua
56+
bit.bor(a: <number>, b: <number>)
57+
```
58+
:::
59+
60+
#### Parameter Description
61+
62+
| Parameter | Description |
63+
|-----------|-------------|
64+
| a | Required. First number |
65+
| b | Required. Second number |
66+
67+
#### Return Value
68+
69+
| Type | Description |
70+
|------|-------------|
71+
| number | Returns the bitwise OR result of a and b |
72+
73+
#### Usage
74+
75+
```lua
76+
import("core.base.bit")
77+
78+
local result = bit.bor(5, 3) -- Returns 7
79+
```
80+
81+
## bit.bxor
82+
83+
- Bitwise XOR operation
84+
85+
#### Function Prototype
86+
87+
::: tip API
88+
```lua
89+
bit.bxor(a: <number>, b: <number>)
90+
```
91+
:::
92+
93+
#### Parameter Description
94+
95+
| Parameter | Description |
96+
|-----------|-------------|
97+
| a | Required. First number |
98+
| b | Required. Second number |
99+
100+
#### Return Value
101+
102+
| Type | Description |
103+
|------|-------------|
104+
| number | Returns the bitwise XOR result of a and b |
105+
106+
#### Usage
107+
108+
```lua
109+
import("core.base.bit")
110+
111+
local result = bit.bxor(5, 3) -- Returns 6
112+
```
113+
114+
## bit.bnot
115+
116+
- Bitwise NOT operation
117+
118+
#### Function Prototype
119+
120+
::: tip API
121+
```lua
122+
bit.bnot(a: <number>)
123+
```
124+
:::
125+
126+
#### Parameter Description
127+
128+
| Parameter | Description |
129+
|-----------|-------------|
130+
| a | Required. Number to negate |
131+
132+
#### Return Value
133+
134+
| Type | Description |
135+
|------|-------------|
136+
| number | Returns the bitwise NOT result of a |
137+
138+
#### Usage
139+
140+
```lua
141+
import("core.base.bit")
142+
143+
local result = bit.bnot(5) -- Returns the bitwise NOT of 5
144+
```
145+
146+
## bit.lshift
147+
148+
- Left shift operation
149+
150+
#### Function Prototype
151+
152+
::: tip API
153+
```lua
154+
bit.lshift(a: <number>, b: <number>)
155+
```
156+
:::
157+
158+
#### Parameter Description
159+
160+
| Parameter | Description |
161+
|-----------|-------------|
162+
| a | Required. Number to shift |
163+
| b | Required. Number of bits to shift left |
164+
165+
#### Return Value
166+
167+
| Type | Description |
168+
|------|-------------|
169+
| number | Returns a shifted left by b bits |
170+
171+
#### Usage
172+
173+
```lua
174+
import("core.base.bit")
175+
176+
local result = bit.lshift(5, 2) -- Returns 20 (5 << 2)
177+
```
178+
179+
## bit.rshift
180+
181+
- Right shift operation
182+
183+
#### Function Prototype
184+
185+
::: tip API
186+
```lua
187+
bit.rshift(a: <number>, b: <number>)
188+
```
189+
:::
190+
191+
#### Parameter Description
192+
193+
| Parameter | Description |
194+
|-----------|-------------|
195+
| a | Required. Number to shift |
196+
| b | Required. Number of bits to shift right |
197+
198+
#### Return Value
199+
200+
| Type | Description |
201+
|------|-------------|
202+
| number | Returns a shifted right by b bits |
203+
204+
#### Usage
205+
206+
```lua
207+
import("core.base.bit")
208+
209+
local result = bit.rshift(20, 2) -- Returns 5 (20 >> 2)
210+
```
211+
212+
## bit.tobit
213+
214+
- Convert to 32-bit integer
215+
216+
#### Function Prototype
217+
218+
::: tip API
219+
```lua
220+
bit.tobit(x: <number>)
221+
```
222+
:::
223+
224+
#### Parameter Description
225+
226+
| Parameter | Description |
227+
|-----------|-------------|
228+
| x | Required. Number to convert |
229+
230+
#### Return Value
231+
232+
| Type | Description |
233+
|------|-------------|
234+
| number | Returns x masked to 32-bit integer (0xffffffff) |
235+
236+
#### Usage
237+
238+
```lua
239+
import("core.base.bit")
240+
241+
local result = bit.tobit(0x12345678) -- Returns value masked to 32 bits
242+
```
243+
244+
## bit.tohex
245+
246+
- Convert number to hexadecimal string
247+
248+
#### Function Prototype
249+
250+
::: tip API
251+
```lua
252+
bit.tohex(x: <number>, n?: <number>)
253+
```
254+
:::
255+
256+
#### Parameter Description
257+
258+
| Parameter | Description |
259+
|-----------|-------------|
260+
| x | Required. Number to convert |
261+
| n | Optional. Number of hexadecimal digits (default: 8). Use negative for uppercase |
262+
263+
#### Return Value
264+
265+
| Type | Description |
266+
|------|-------------|
267+
| string | Returns hexadecimal string representation |
268+
269+
#### Usage
270+
271+
```lua
272+
import("core.base.bit")
273+
274+
local hex = bit.tohex(255, 2) -- Returns "ff"
275+
local hex = bit.tohex(255, -2) -- Returns "FF"
276+
local hex = bit.tohex(255) -- Returns "000000ff"
277+
```
278+
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
# lib.lua.package
3+
4+
This module provides access to native Lua package interfaces for loading dynamic libraries and Lua modules.
5+
6+
::: tip TIP
7+
To use this module, you need to import it first: `import("lib.lua.package")`
8+
:::
9+
10+
Xmake restricts access to native Lua modules and interfaces by default for safety reasons. This module provides access to some of the APIs provided by Lua when needed.
11+
12+
## package.loadlib
13+
14+
- Load Lua module from dynamic library
15+
16+
#### Function Prototype
17+
18+
::: tip API
19+
```lua
20+
package.loadlib(libfile: <string>, symbol: <string>)
21+
```
22+
:::
23+
24+
#### Parameter Description
25+
26+
| Parameter | Description |
27+
|-----------|-------------|
28+
| libfile | Required. The dynamic library file path (e.g., foo.dll, libfoo.so, libfoo.dylib) |
29+
| symbol | Required. The export symbol name (e.g., luaopen_xxx) |
30+
31+
#### Return Value
32+
33+
| Type | Description |
34+
|------|-------------|
35+
| function | Returns a function to initialize the module |
36+
37+
#### Usage
38+
39+
This is typically used in high-performance scenarios where you need to load Lua modules from native dynamic libraries.
40+
41+
```lua
42+
import("lib.lua.package")
43+
44+
-- Load the module from dynamic library
45+
local script = package.loadlib("/xxx/libfoo.so", "luaopen_mymodule")
46+
47+
-- Initialize and use the module
48+
local mymodule = script()
49+
mymodule.hello()
50+
```
51+
52+

docs/sidebar.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ function coreBaseModulesApiSidebar(): DefaultTheme.SidebarItem {
6262
text: 'base',
6363
collapsed: true,
6464
items: [
65+
{ text: 'bit', link: 'extension-modules/core/base/bit' },
6566
{ text: 'bytes', link: 'extension-modules/core/base/bytes' },
6667
{ text: 'global', link: 'extension-modules/core/base/global' },
6768
{ text: 'graph', link: 'extension-modules/core/base/graph' },
@@ -164,6 +165,17 @@ function libModulesApiSidebar(): DefaultTheme.SidebarItem {
164165
collapsed: true,
165166
items: [
166167
{ text: 'detect', link: 'extension-modules/lib/detect' },
168+
libLuaModulesApiSidebar(),
169+
]
170+
}
171+
}
172+
173+
function libLuaModulesApiSidebar(): DefaultTheme.SidebarItem {
174+
return {
175+
text: 'lua',
176+
collapsed: true,
177+
items: [
178+
{ text: 'package', link: 'extension-modules/lib/lua/package' },
167179
]
168180
}
169181
}

0 commit comments

Comments
 (0)