Skip to content

Commit d0fd29d

Browse files
Cleanup buffer
1 parent 38c5a60 commit d0fd29d

File tree

8 files changed

+121
-41
lines changed

8 files changed

+121
-41
lines changed

jls/lang/Buffer-buffer.lua

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,11 @@
11
local class = require('jls.lang.class')
22

33
local bufferLib = require('buffer')
4-
assert(bufferLib._VERSION >= '0.3', 'bad buffer lib version '..tostring(bufferLib._VERSION))
4+
assert(type(bufferLib.toreference) == 'function', 'bad buffer lib version '..tostring(bufferLib._VERSION))
55

6-
return class.create(function(buffer)
6+
return class.create('jls.lang.Buffer', function(buffer)
77

88
function buffer:initialize(value, size)
9-
if type(value) ~= 'userdata' then
10-
error('invalid buffer type '..type(value))
11-
end
12-
if size == nil then
13-
size = bufferLib.len(value)
14-
end
15-
if math.type(size) ~= 'integer' or size <= 0 then
16-
error('invalid size '..tostring(size))
17-
end
189
self.buffer = value
1910
self.size = size
2011
end
@@ -51,18 +42,15 @@ return class.create(function(buffer)
5142
return bufferLib.toreference(self.buffer, nil, 'jls.lang.Buffer')
5243
end
5344

54-
function buffer:toString()
55-
return bufferLib.sub(self.buffer, 1, self.size)
56-
end
57-
5845
end, function(Buffer)
5946

60-
function Buffer.allocate(sizeOrData)
61-
return Buffer:new(bufferLib.new(sizeOrData))
47+
function Buffer.allocate(size)
48+
return Buffer:new(bufferLib.new(size), size)
6249
end
6350

6451
function Buffer.fromReference(reference)
65-
return Buffer:new(bufferLib.fromreference(reference, nil, 'jls.lang.Buffer'))
52+
local buffer, size = bufferLib.fromreference(reference, nil, 'jls.lang.Buffer')
53+
return Buffer:new(buffer, size)
6654
end
6755

6856
end)

jls/lang/Buffer.lua

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,101 @@
1-
return require('jls.lang.loader').requireOne('jls.lang.Buffer-buffer', 'jls.lang.Buffer-')
1+
--- Represents a byte array to store temporary data.
2+
-- @module jls.lang.Buffer
3+
-- @pragma nostrip
4+
5+
local class = require('jls.lang.class')
6+
7+
--- The Buffer class.
8+
-- @type Buffer
9+
return class.create(function(buffer)
10+
11+
--- Returns the size of the buffer.
12+
-- @return The size of the buffer
13+
-- @function buffer:length
14+
buffer.length = class.notImplementedFunction
15+
16+
--- Returns the string at the specified position.
17+
-- @tparam[opt] number from The start position, default to 1
18+
-- @tparam[opt] number to The end position included, default to this buffer size
19+
-- @return The string at the specified position
20+
-- @function buffer:get
21+
buffer.get = class.notImplementedFunction
22+
23+
--- Sets the string at the specified position.
24+
-- @param value The value to set in this buffer, could be a buffer or a string
25+
-- @tparam[opt] number offset The position in this buffer to set, default to 1
26+
-- @tparam[opt] number from The start position in the value, default to 1
27+
-- @tparam[opt] number to The end position in the value included, default to this buffer size
28+
-- @function buffer:set
29+
buffer.set = class.notImplementedFunction
30+
31+
--- Returns the bytes at the specified position.
32+
-- @tparam[opt] number from The start position, default to 1
33+
-- @tparam[opt] number to The end position included, default to from
34+
-- @return The bytes at the specified position
35+
-- @function buffer:getBytes
36+
buffer.getBytes = class.notImplementedFunction
37+
38+
--- Sets the bytes at the specified position.
39+
-- @tparam number at The position in this buffer to set, default to 1
40+
-- @param ... The bytes
41+
-- @function buffer:setBytes
42+
buffer.setBytes = class.notImplementedFunction
43+
44+
--- Returns a reference for this buffer.
45+
-- @treturn string a reference for this buffer
46+
-- @function buffer:toReference
47+
buffer.toReference = class.notImplementedFunction
48+
49+
end, function(Buffer)
50+
51+
local function getClassName(mode)
52+
if mode == 'local' then
53+
return require('jls.lang.BufferLocal')
54+
elseif mode == 'global' then
55+
return require('jls.lang.BufferGlobal')
56+
elseif mode == 'shared' then
57+
return require('jls.lang.BufferShared')
58+
end
59+
error('invalid buffer mode '..tostring(mode))
60+
end
61+
62+
--- Returns a new buffer for the specified mode.
63+
-- A local buffer resides in the Lua state.
64+
-- A global buffer resides out of the Lua state but in the Lua process.
65+
-- A shared buffer resides out of the Lua process but in the host.
66+
-- @tparam number size the size to allocate, could be a non empty string
67+
-- @tparam[opt] string mode the mode of buffer, defaults to local
68+
-- @return The new allocated buffer
69+
function Buffer.allocate(size, mode)
70+
if type(size) == 'string' then
71+
local b = Buffer.allocate(#size, mode)
72+
b:set(size)
73+
return b
74+
end
75+
if math.type(size) ~= 'integer' or size <= 0 then
76+
error('invalid size '..tostring(size))
77+
end
78+
if mode == nil then
79+
mode = 'local'
80+
end
81+
return getClassName(mode).allocate(size, mode)
82+
end
83+
84+
--- Returns the buffer represented by the specified reference.
85+
-- @tparam string reference the reference
86+
-- @tparam[opt] string mode the mode of buffer
87+
-- @return The referenced buffer
88+
function Buffer.fromReference(reference, mode)
89+
if mode == nil then
90+
local status, b
91+
for _, m in ipairs({'local', 'global', 'shared'}) do
92+
status, b = pcall(Buffer.fromReference, reference, m)
93+
if status then
94+
return b
95+
end
96+
end
97+
end
98+
return getClassName(mode).fromReference(reference, mode)
99+
end
100+
101+
end)

jls/lang/Buffer-.lua renamed to jls/lang/BufferFile.lua

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
local class = require('jls.lang.class')
22

3-
return class.create(function(buffer)
3+
return class.create('jls.lang.Buffer', function(buffer)
44

55
function buffer:initialize(size, name, preserve)
6-
if math.type(size) ~= 'integer' or size <= 0 then
7-
error('invalid size '..tostring(size))
8-
end
9-
self.size = size or 0
6+
self.size = size
107
self.name = name or string.format('.%s-%p.tmp', 'jls.lang.Buffer', self)
118
self.file = assert(io.open(self.name, preserve and 'r+b' or 'w+b'))
129
if not preserve then
@@ -63,19 +60,10 @@ return class.create(function(buffer)
6360
return self.name..'#'..tostring(self.size)
6461
end
6562

66-
function buffer:toString()
67-
return self:get()
68-
end
69-
7063
end, function(Buffer)
7164

72-
function Buffer.allocate(sizeOrData)
73-
if type(sizeOrData) == 'string' then
74-
local buffer = Buffer:new(#sizeOrData)
75-
buffer:set(sizeOrData)
76-
return buffer
77-
end
78-
return Buffer:new(sizeOrData)
65+
function Buffer.allocate(size)
66+
return Buffer:new(size)
7967
end
8068

8169
function Buffer.fromReference(reference)

jls/lang/BufferGlobal.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
return require('jls.lang.loader').requireOne('jls.lang.Buffer-buffer', 'jls.lang.BufferFile')

jls/lang/BufferLocal.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
return require('jls.lang.loader').requireOne('jls.lang.Buffer-buffer', 'jls.lang.BufferFile')

jls/lang/BufferShared.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
return require('jls.lang.BufferFile')

tests/full/Buffer.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ end
1010
function Test_buffer_from_string()
1111
local buffer = Buffer.allocate('Hello')
1212
lu.assertEquals(buffer:length(), 5)
13-
lu.assertEquals(buffer:toString(), 'Hello')
13+
lu.assertEquals(buffer:get(), 'Hello')
1414
end
1515

1616
function Test_buffer_bytes()

tests/full/thread_memory.lua

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@ local loop = require('jls.lang.loopWithTimeout')
88

99
-- Indicate to a polling thread that it must terminate
1010
function Test_thread_buffer()
11-
local buffer = Buffer.allocate(1)
11+
local buffer = Buffer.allocate(1, 'global')
1212
buffer:setBytes(1, 0)
1313
lu.assertEquals(buffer:getBytes(), 0)
1414
local result = nil
15-
Thread:new(function(l)
16-
local Mem = require('jls.lang.Buffer')
15+
Thread:new(function(ref)
16+
local Buf = require('jls.lang.Buffer')
1717
local sys = require('jls.lang.system')
18-
local mem = Mem.fromReference(l, 1)
18+
local buf = Buf.fromReference(ref, 'global')
1919
local n = 0
2020
while true do
21-
local v = mem:getBytes()
21+
local v = buf:getBytes()
2222
n = n + 1
2323
if v ~= 0 then
2424
return n
@@ -65,6 +65,7 @@ function Test_thread_lock()
6565
if not loop() then
6666
lu.fail('Timeout reached')
6767
end
68+
lock:finalize()
6869
lu.assertNotNil(result)
6970
lu.assertEquals(result, 'tryLock=false')
7071
lu.assertTrue(ms >= 400) -- may fail

0 commit comments

Comments
 (0)