Skip to content

Commit b27dd13

Browse files
committed
make compat by AI
1 parent 0a88a03 commit b27dd13

File tree

11 files changed

+271
-5
lines changed

11 files changed

+271
-5
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,7 @@
88
//"--shownode",
99
//"--lazy",
1010
],
11+
"files.associations": {
12+
"thread": "cpp"
13+
},
1114
}

COMPAT_FIXES.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# bee.lua API 兼容性修复总结
2+
3+
## 修复的问题
4+
5+
### 1. `exe_path` API 变更
6+
- **问题**: `fs.exe_path()``bee.filesystem` 模块移除
7+
- **解决方案**: 现在使用 `bee.sys.exe_path()`
8+
- **修改文件**:
9+
- `test.lua` - 更新为使用 `sys.exe_path()`
10+
- `script/meta/bee/filesystem.lua` - 添加 deprecated 标记
11+
- `script/meta/bee/sys.lua` - 新增元数据
12+
13+
### 2. Channel API 变更
14+
- **问题**: `thread.newchannel()``thread.channel()` 不再存在
15+
- **新 API**: `bee.channel.create()``bee.channel.query()`
16+
- **解决方案**: 创建兼容层 `script/bee-compat.lua`
17+
- **修改文件**:
18+
- `script/bee-compat.lua` - 新增兼容层
19+
- `script/pub/pub.lua` - 使用兼容层
20+
- `script/brave/brave.lua` - 使用兼容层
21+
- `script/meta/bee/channel.lua` - 新增元数据
22+
- `script/meta/bee/thread.lua` - 更新元数据
23+
24+
### 3. Thread API 变更
25+
- **问题**: `thread.thread()` 改名为 `thread.create()`
26+
- **解决方案**: 在兼容层中添加别名
27+
- **修改文件**: `script/bee-compat.lua`
28+
29+
### 4. Channel bpop() 实现
30+
- **问题**: 新版 channel 不支持阻塞 pop
31+
- **解决方案**: 使用 `bee.select` 模块实现阻塞等待
32+
- **实现**: 在兼容层的 metatable 中添加 `bpop()` 方法
33+
34+
## 兼容层功能
35+
36+
`script/bee-compat.lua` 提供以下功能:
37+
38+
1. **thread.newchannel(name)** - 创建 channel 并注册到 selector
39+
2. **thread.channel(name)** - 查询现有 channel
40+
3. **thread.thread(script)** - thread.create() 的别名
41+
4. **channel:bpop()** - 阻塞式 pop,使用 select 等待
42+
5. **errlog 特殊处理** - 包装 thread.errlog() 为 channel 接口
43+
44+
## 测试
45+
46+
- ✓ 基本兼容性测试通过 (`test_compat.lua`)
47+
- ✓ 主测试套件运行正常 (`test.lua`)
48+
- ✓ channel 创建、查询、push/pop 功能正常
49+
- ✓ errlog channel 工作正常
50+
- ✓ sys.exe_path() 正常工作
51+
52+
## 使用方法
53+
54+
在需要使用旧 API 的模块中,使用:
55+
```lua
56+
local thread = require 'bee-compat'
57+
```
58+
59+
而不是:
60+
```lua
61+
local thread = require 'bee.thread'
62+
```
63+
64+
这样就能自动获得兼容性支持。

script/bee-compat.lua

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
-- Compatibility layer for bee.lua API changes
2+
local thread = require 'bee.thread'
3+
local channel_mod = require 'bee.channel'
4+
local select_mod = require 'bee.select'
5+
6+
-- Store original thread.create
7+
local original_create = thread.create
8+
9+
-- Add thread.thread as alias for backward compatibility
10+
thread.thread = original_create
11+
12+
-- Store channels globally
13+
local channels = {}
14+
local selector = select_mod.create()
15+
16+
-- Create a wrapper for errlog to make it compatible with channel interface
17+
local errlog_channel = setmetatable({}, {
18+
__index = {
19+
pop = function()
20+
local err = thread.errlog()
21+
if err then
22+
return true, err
23+
else
24+
return false
25+
end
26+
end,
27+
bpop = function()
28+
while true do
29+
local err = thread.errlog()
30+
if err then
31+
return err
32+
end
33+
thread.sleep(10)
34+
end
35+
end,
36+
}
37+
})
38+
39+
-- Create a new channel
40+
function thread.newchannel(name)
41+
if name == 'errlog' then
42+
return errlog_channel
43+
end
44+
if channels[name] then
45+
error("Channel already exists: " .. name)
46+
end
47+
local ch = channel_mod.create(name)
48+
channels[name] = ch
49+
-- Add to selector for blocking operations
50+
selector:event_add(ch:fd(), select_mod.SELECT_READ)
51+
return ch
52+
end
53+
54+
-- Get an existing channel
55+
function thread.channel(name)
56+
if name == 'errlog' then
57+
return errlog_channel
58+
end
59+
local ch = channels[name]
60+
if not ch then
61+
ch = channel_mod.query(name)
62+
if ch then
63+
channels[name] = ch
64+
selector:event_add(ch:fd(), select_mod.SELECT_READ)
65+
end
66+
end
67+
return ch
68+
end
69+
70+
-- Add blocking pop support to channels
71+
local channel_mt = debug.getregistry()['bee::channel']
72+
if channel_mt and not channel_mt.bpop then
73+
function channel_mt:bpop()
74+
while true do
75+
local results = table.pack(self:pop())
76+
if results[1] then
77+
return table.unpack(results, 2, results.n)
78+
end
79+
-- Wait for data with a timeout
80+
selector:wait(100)
81+
end
82+
end
83+
end
84+
85+
return thread

script/brave/brave.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
local thread = require 'bee.thread'
1+
local thread = require 'bee-compat'
22

33
local taskPad = thread.channel('taskpad')
44
local waiter = thread.channel('waiter')

script/meta/bee/channel.lua

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---@meta
2+
3+
---@class bee.channel
4+
local channel_mod = {}
5+
6+
---@class bee.channel.object
7+
local channel = {}
8+
9+
---@param ... any
10+
function channel:push(...)
11+
end
12+
13+
---@return boolean success
14+
---@return ...
15+
function channel:pop()
16+
end
17+
18+
---@return userdata
19+
function channel:fd()
20+
end
21+
22+
---@param name string
23+
---@return bee.channel.object
24+
function channel_mod.create(name)
25+
end
26+
27+
---@param name string
28+
function channel_mod.destroy(name)
29+
end
30+
31+
---@param name string
32+
---@return bee.channel.object?
33+
function channel_mod.query(name)
34+
end
35+
36+
return channel_mod

script/meta/bee/filesystem.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ fs.copy_options = copy_options
4949
function fs.path(path)
5050
end
5151

52+
---@deprecated Use bee.sys.exe_path() instead
5253
---@return fs.path
5354
function fs.exe_path()
5455
end

script/meta/bee/sys.lua

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---@meta
2+
3+
---@class bee.sys
4+
local sys = {}
5+
6+
---@return fs.path
7+
function sys.exe_path()
8+
end
9+
10+
---@return fs.path
11+
function sys.dll_path()
12+
end
13+
14+
return sys

script/meta/bee/thread.lua

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,42 @@ local thread = {}
66
---@param time number
77
function thread.sleep(time) end
88

9+
---@deprecated Use bee.channel.create() instead, or use bee-compat module
910
---@param name string
1011
function thread.newchannel(name) end
1112

13+
---@deprecated Use bee.channel.query() instead, or use bee-compat module
1214
---@param name string
1315
---@return bee.thread.channel
1416
function thread.channel(name) end
1517

18+
---@param script string
19+
---@return userdata
20+
function thread.create(script) end
21+
22+
---@deprecated Use thread.create() instead
1623
---@param script string
1724
---@return bee.thread.thread
1825
function thread.thread(script) end
1926

2027
---@return string?
2128
function thread.errlog() end
2229

30+
---@param handle userdata
31+
function thread.wait(handle) end
32+
33+
---@param name string
34+
function thread.setname(name) end
35+
36+
---@type integer
37+
thread.id = 0
38+
2339
---@class bee.thread.channel
2440
local channel = {}
2541

2642
function channel:push(...) end
2743

28-
---@return ...
44+
---@return boolean, ...
2945
function channel:pop() end
3046

3147
---@return ...

script/pub/pub.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
local thread = require 'bee.thread'
1+
local thread = require 'bee-compat'
2+
local channel = require 'bee.channel'
23
local utility = require 'utility'
34
local await = require 'await'
45

test.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ package.path = package.path
22
.. ';./test/?.lua'
33
.. ';./test/?/init.lua'
44
local fs = require 'bee.filesystem'
5-
local rootPath = fs.exe_path():parent_path():parent_path():string()
5+
local sys = require 'bee.sys'
6+
local rootPath = sys.exe_path():parent_path():parent_path():string()
67
ROOT = fs.path(rootPath)
78
TEST = true
89
DEVELOP = true
@@ -127,6 +128,6 @@ end
127128
loadAllLibs()
128129
main()
129130

130-
log.debug('test finish.')
131+
print('test finish.')
131132
require 'bee.thread'.sleep(1000)
132133
os.exit()

0 commit comments

Comments
 (0)