Skip to content

Commit 7098df6

Browse files
authored
Merge pull request #33 from Dretch/stbuffer-typeclass
Explore using ST for effects (issue #24)
2 parents d279daa + 7a48e33 commit 7098df6

18 files changed

+1024
-493
lines changed

bower.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@
1515
"dependencies": {
1616
"purescript-effect": "^2.0.0",
1717
"purescript-maybe": "^4.0.0",
18-
"purescript-arraybuffer-types": "^2.0.0"
18+
"purescript-arraybuffer-types": "^2.0.0",
19+
"purescript-st": "^4.0.0",
20+
"purescript-unsafe-coerce": "^4.0.0"
1921
},
2022
"devDependencies": {
2123
"purescript-assert": "^4.0.0",
2224
"purescript-console": "^4.1.0",
23-
"purescript-foldable-traversable": "^4.0.0"
25+
"purescript-foldable-traversable": "^4.0.0",
26+
"purescript-proxy": "^3.0.0"
2427
}
2528
}

src/Node/Buffer.js

Lines changed: 0 additions & 176 deletions
This file was deleted.

src/Node/Buffer.purs

Lines changed: 33 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -1,164 +1,40 @@
1+
-- | Mutable buffers and associated operations.
12
module Node.Buffer
2-
( Octet()
3-
, Offset()
4-
, Buffer()
5-
, BufferValueType(..)
6-
, create
7-
, fromArray
8-
, fromString
9-
, fromArrayBuffer
10-
, toArrayBuffer
11-
, read
12-
, readString
13-
, toString
14-
, write
15-
, writeString
16-
, toArray
17-
, getAtOffset
18-
, setAtOffset
19-
, size
20-
, concat
21-
, concat'
22-
, copy
23-
, fill
3+
( Buffer
4+
, module TypesExports
5+
, module Class
246
) where
257

26-
import Prelude
27-
288
import Effect (Effect)
29-
import Data.ArrayBuffer.Types (ArrayBuffer)
30-
import Data.Maybe (Maybe(..))
31-
import Node.Encoding (Encoding, encodingToNode)
32-
33-
-- | Type synonym indicating the value should be an octet (0-255). If the value
34-
-- | provided is outside this range it will be used as modulo 256.
35-
type Octet = Int
9+
import Node.Buffer.Class (class MutableBuffer)
10+
import Node.Buffer.Class (class MutableBuffer, concat, concat', copy, create, fill, freeze, fromArray, fromArrayBuffer, fromString, getAtOffset, read, readString, setAtOffset, size, slice, thaw, toArray, toArrayBuffer, toString, unsafeFreeze, unsafeThaw, write, writeString) as Class
11+
import Node.Buffer.Internal as Internal
12+
import Node.Buffer.Types (BufferValueType(..), Octet, Offset) as TypesExports
3613

37-
-- | Type synonym indicating the value refers to an offset in a buffer.
38-
type Offset = Int
39-
40-
-- | An instance of Node's Buffer class.
14+
-- | A reference to a mutable buffer for use with `Effect`
4115
foreign import data Buffer :: Type
4216

43-
instance showBuffer :: Show Buffer where
44-
show = showImpl
45-
46-
foreign import showImpl :: Buffer -> String
47-
48-
-- | Enumeration of the numeric types that can be written to a buffer.
49-
data BufferValueType
50-
= UInt8
51-
| UInt16LE
52-
| UInt16BE
53-
| UInt32LE
54-
| UInt32BE
55-
| Int8
56-
| Int16LE
57-
| Int16BE
58-
| Int32LE
59-
| Int32BE
60-
| FloatLE
61-
| FloatBE
62-
| DoubleLE
63-
| DoubleBE
64-
65-
instance showBufferValueType :: Show BufferValueType where
66-
show UInt8 = "UInt8"
67-
show UInt16LE = "UInt16LE"
68-
show UInt16BE = "UInt16BE"
69-
show UInt32LE = "UInt32LE"
70-
show UInt32BE = "UInt32BE"
71-
show Int8 = "Int8"
72-
show Int16LE = "Int16LE"
73-
show Int16BE = "Int16BE"
74-
show Int32LE = "Int32LE"
75-
show Int32BE = "Int32BE"
76-
show FloatLE = "FloatLE"
77-
show FloatBE = "FloatBE"
78-
show DoubleLE = "DoubleLE"
79-
show DoubleBE = "DoubleBE"
80-
81-
-- | Creates a new buffer of the specified size.
82-
foreign import create :: Int -> Effect Buffer
83-
84-
-- | Creates a new buffer from an array of octets, sized to match the array.
85-
foreign import fromArray :: Array Octet -> Effect Buffer
86-
87-
-- | Creates a buffer view from a JS ArrayByffer without copying data.
88-
--
89-
-- Requires Node >= v5.10.0
90-
foreign import fromArrayBuffer :: ArrayBuffer -> Effect Buffer
91-
92-
-- | Creates a new buffer from a string with the specified encoding, sized to
93-
-- | match the string.
94-
fromString :: String -> Encoding -> Effect Buffer
95-
fromString str = fromStringImpl str <<< encodingToNode
96-
97-
foreign import fromStringImpl :: String -> String -> Effect Buffer
98-
99-
foreign import toArrayBuffer :: Buffer -> Effect ArrayBuffer
100-
101-
-- | Reads a numeric value from a buffer at the specified offset.
102-
read :: BufferValueType -> Offset -> Buffer -> Effect Int
103-
read = readImpl <<< show
104-
105-
foreign import readImpl :: String -> Offset -> Buffer -> Effect Int
106-
107-
-- | Reads a section of a buffer as a string with the specified encoding.
108-
readString :: Encoding -> Offset -> Offset -> Buffer -> Effect String
109-
readString = readStringImpl <<< encodingToNode
110-
111-
foreign import readStringImpl ::
112-
String -> Offset -> Offset -> Buffer -> Effect String
113-
114-
-- | Reads the buffer as a string with the specified encoding.
115-
toString :: Encoding -> Buffer -> Effect String
116-
toString = toStringImpl <<< encodingToNode
117-
118-
foreign import toStringImpl :: String -> Buffer -> Effect String
119-
120-
-- | Writes a numeric value to a buffer at the specified offset.
121-
write :: BufferValueType -> Int -> Offset -> Buffer -> Effect Unit
122-
write = writeImpl <<< show
123-
124-
foreign import writeImpl :: String -> Int -> Offset -> Buffer -> Effect Unit
125-
126-
-- | Writes octets from a string to a buffer at the specified offset. Multi-byte
127-
-- | characters will not be written to the buffer if there is not enough capacity
128-
-- | to write them fully. The number of bytes written is returned.
129-
writeString :: Encoding -> Offset -> Int -> String -> Buffer -> Effect Int
130-
writeString = writeStringImpl <<< encodingToNode
131-
132-
foreign import writeStringImpl ::
133-
String -> Offset -> Int -> String -> Buffer -> Effect Int
134-
135-
-- | Creates an array of octets from a buffer's contents.
136-
foreign import toArray :: Buffer -> Effect (Array Octet)
137-
138-
-- | Reads an octet from a buffer at the specified offset.
139-
getAtOffset :: Offset -> Buffer -> Effect (Maybe Octet)
140-
getAtOffset = getAtOffsetImpl Just Nothing
141-
142-
foreign import getAtOffsetImpl ::
143-
(Octet -> Maybe Octet) -> Maybe Octet -> Offset -> Buffer -> Effect (Maybe Octet)
144-
145-
-- | Writes an octet in the buffer at the specified offset.
146-
foreign import setAtOffset :: Octet -> Offset -> Buffer -> Effect Unit
147-
148-
-- | Returns the size of a buffer.
149-
foreign import size :: Buffer -> Effect Int
150-
151-
-- | Concatenates a list of buffers.
152-
foreign import concat :: Array Buffer -> Effect Buffer
153-
154-
-- | Concatenates a list of buffers, combining them into a new buffer of the
155-
-- | specified length.
156-
foreign import concat' :: Array Buffer -> Int -> Effect Buffer
157-
158-
-- | Copies a section of a source buffer into a target buffer at the specified
159-
-- | offset, and returns the number of octets copied.
160-
foreign import copy :: Offset -> Offset -> Buffer -> Offset -> Buffer -> Effect Int
161-
162-
-- | Fills a range in a buffer with the specified octet.
163-
foreign import fill ::
164-
Octet -> Offset -> Offset -> Buffer -> Effect Unit
17+
instance mutableBufferEffect :: MutableBuffer Buffer Effect where
18+
create = Internal.create
19+
freeze = Internal.copyAll
20+
unsafeFreeze = Internal.unsafeFreeze
21+
thaw = Internal.copyAll
22+
unsafeThaw = Internal.unsafeThaw
23+
fromArray = Internal.fromArray
24+
fromString = Internal.fromString
25+
fromArrayBuffer = Internal.fromArrayBuffer
26+
toArrayBuffer = Internal.toArrayBuffer
27+
read = Internal.read
28+
readString = Internal.readString
29+
toString = Internal.toString
30+
write = Internal.write
31+
writeString = Internal.writeString
32+
toArray = Internal.toArray
33+
getAtOffset = Internal.getAtOffset
34+
setAtOffset = Internal.setAtOffset
35+
slice = Internal.slice
36+
size = Internal.size
37+
concat = Internal.concat
38+
concat' = Internal.concat'
39+
copy = Internal.copy
40+
fill = Internal.fill

0 commit comments

Comments
 (0)