Skip to content

Convert FFI to uncurried functions #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"purescript-effect": "^4.0.0",
"purescript-maybe": "^6.0.0",
"purescript-st": "^6.0.0",
"purescript-unsafe-coerce": "^6.0.0"
"purescript-unsafe-coerce": "^6.0.0",
"purescript-nullable": "^6.0.0"
},
"devDependencies": {
"purescript-assert": "^6.0.0",
Expand Down
95 changes: 18 additions & 77 deletions src/Node/Buffer/Immutable.js
Original file line number Diff line number Diff line change
@@ -1,99 +1,40 @@
/* global Buffer */
import { Buffer } from "node:buffer";

import { inspect } from "util";
export const showImpl = inspect;

export function eqImpl(a) {
return b => {
return a.equals(b);
};
}
export const eqImpl = (a, b) => a.equals(b);

export function compareImpl(a) {
return b => {
return a.compare(b);
};
}
export const compareImpl = (a, b) => a.compare(b);

export function create(size) {
return Buffer.alloc(size);
}
export const create = (size) => Buffer.alloc(size);

export function fromArray(octets) {
return Buffer.from(octets);
}
export const fromArray = (octets) => Buffer.from(octets);

export function size(buff) {
return buff.length;
}
export const size = (buff) => buff.length;

export function toArray(buff) {
var json = buff.toJSON();
return json.data || json;
}

export function toArrayBuffer(buff) {
return buff.buffer.slice(buff.byteOffset, buff.byteOffset + buff.byteLength);
}
export const toArrayBuffer = (buff) =>
buff.buffer.slice(buff.byteOffset, buff.byteOffset + buff.byteLength);

export function fromArrayBuffer(ab) {
return Buffer.from(ab);
}
export const fromArrayBuffer = (ab) => Buffer.from(ab);

export function fromStringImpl(str) {
return encoding => {
return Buffer.from(str, encoding);
};
}
export const fromStringImpl = (str, encoding) => Buffer.from(str, encoding);

export function readImpl(ty) {
return offset => {
return buf => {
return buf["read" + ty](offset);
};
};
}
export const readImpl = (ty, offset, buf) => buf["read" + ty](offset);

export function readStringImpl(enc) {
return start => {
return end => {
return buff => {
return buff.toString(enc, start, end);
};
};
};
}
export const readStringImpl = (enc, start, end, buff) => buff.toString(enc, start, end);

export function getAtOffsetImpl(just) {
return nothing => {
return offset => {
return buff => {
var octet = buff[offset];
return octet == null ? nothing : just(octet);
};
};
};
}
export const getAtOffsetImpl = (offset, buff) => buff[offset];

export function toStringImpl(enc) {
return buff => {
return buff.toString(enc);
};
}
export const toStringImpl = (enc, buff) => buff.toString(enc);

export function slice(start) {
return end => {
return buff => {
return buff.slice(start, end);
};
};
}
export const sliceImpl = (start, end, buff) => buff.slice(start, end);

export function concat(buffs) {
return Buffer.concat(buffs);
}
export const concat = (buffs) => Buffer.concat(buffs);

export function concatToLength(buffs) {
return totalLength => {
return Buffer.concat(buffs, totalLength);
};
}
export const concatToLength = (buffs, totalLength) => Buffer.concat(buffs, totalLength);
43 changes: 23 additions & 20 deletions src/Node/Buffer/Immutable.purs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ module Node.Buffer.Immutable
import Prelude

import Data.ArrayBuffer.Types (ArrayBuffer)
import Data.Maybe (Maybe(..))
import Data.Function.Uncurried (Fn2, Fn3, Fn4, runFn2, runFn3, runFn4)
import Data.Maybe (Maybe)
import Data.Nullable (Nullable, toMaybe)
import Node.Buffer.Types (BufferValueType, Octet, Offset)
import Node.Encoding (Encoding, encodingToNode)

Expand All @@ -33,18 +35,18 @@ instance showBuffer :: Show ImmutableBuffer where
foreign import showImpl :: ImmutableBuffer -> String

instance eqBuffer :: Eq ImmutableBuffer where
eq = eqImpl
eq a b = runFn2 eqImpl a b

foreign import eqImpl :: ImmutableBuffer -> ImmutableBuffer -> Boolean
foreign import eqImpl :: Fn2 ImmutableBuffer ImmutableBuffer Boolean

instance ordBuffer :: Ord ImmutableBuffer where
compare a b =
case compareImpl a b of
case runFn2 compareImpl a b of
x | x < 0 -> LT
x | x > 0 -> GT
_ -> EQ

foreign import compareImpl :: ImmutableBuffer -> ImmutableBuffer -> Int
foreign import compareImpl :: Fn2 ImmutableBuffer ImmutableBuffer Int

-- | Creates a new buffer of the specified size.
foreign import create :: Int -> ImmutableBuffer
Expand All @@ -59,28 +61,27 @@ foreign import fromArrayBuffer :: ArrayBuffer -> ImmutableBuffer

-- | Creates a new buffer from a string with the specified encoding, sized to match the string.
fromString :: String -> Encoding -> ImmutableBuffer
fromString str = fromStringImpl str <<< encodingToNode
fromString str = runFn2 fromStringImpl str <<< encodingToNode

foreign import fromStringImpl :: String -> String -> ImmutableBuffer
foreign import fromStringImpl :: Fn2 String String ImmutableBuffer

-- | Reads a numeric value from a buffer at the specified offset.
read :: BufferValueType -> Offset -> ImmutableBuffer -> Number
read = readImpl <<< show
read ty off buf = runFn3 readImpl (show ty) off buf

foreign import readImpl :: String -> Offset -> ImmutableBuffer -> Number
foreign import readImpl :: Fn3 String Offset ImmutableBuffer Number

-- | Reads a section of a buffer as a string with the specified encoding.
readString :: Encoding -> Offset -> Offset -> ImmutableBuffer -> String
readString = readStringImpl <<< encodingToNode
readString enc start end buf = runFn4 readStringImpl (encodingToNode enc) start end buf

foreign import readStringImpl
:: String -> Offset -> Offset -> ImmutableBuffer -> String
foreign import readStringImpl :: Fn4 String Offset Offset ImmutableBuffer String

-- | Reads the buffer as a string with the specified encoding.
toString :: Encoding -> ImmutableBuffer -> String
toString = toStringImpl <<< encodingToNode
toString enc buf = runFn2 toStringImpl (encodingToNode enc) buf

foreign import toStringImpl :: String -> ImmutableBuffer -> String
foreign import toStringImpl :: Fn2 String ImmutableBuffer String

-- | Creates an array of octets from a buffer's contents.
foreign import toArray :: ImmutableBuffer -> Array Octet
Expand All @@ -90,23 +91,25 @@ foreign import toArrayBuffer :: ImmutableBuffer -> ArrayBuffer

-- | Reads an octet from a buffer at the specified offset.
getAtOffset :: Offset -> ImmutableBuffer -> Maybe Octet
getAtOffset = getAtOffsetImpl Just Nothing
getAtOffset off buf = toMaybe $ runFn2 getAtOffsetImpl off buf

foreign import getAtOffsetImpl
:: (Octet -> Maybe Octet) -> Maybe Octet -> Offset -> ImmutableBuffer -> Maybe Octet
foreign import getAtOffsetImpl :: Fn2 Offset ImmutableBuffer (Nullable Octet)

-- | Concatenates a list of buffers.
foreign import concat :: Array ImmutableBuffer -> ImmutableBuffer

-- | Concatenates a list of buffers, combining them into a new buffer of the
-- | specified length.
concat' :: Array ImmutableBuffer -> Int -> ImmutableBuffer
concat' = concatToLength
concat' a i = runFn2 concatToLength a i

foreign import concatToLength :: Array ImmutableBuffer -> Int -> ImmutableBuffer
foreign import concatToLength :: Fn2 (Array ImmutableBuffer) Int ImmutableBuffer

-- | Creates a new buffer slice that shares the memory of the original buffer.
foreign import slice :: Offset -> Offset -> ImmutableBuffer -> ImmutableBuffer
slice :: Offset -> Offset -> ImmutableBuffer -> ImmutableBuffer
slice start end buf = runFn3 sliceImpl start end buf

foreign import sliceImpl :: Fn3 Offset Offset ImmutableBuffer ImmutableBuffer

-- | Returns the size of a buffer.
foreign import size :: ImmutableBuffer -> Int
77 changes: 14 additions & 63 deletions src/Node/Buffer/Internal.js
Original file line number Diff line number Diff line change
@@ -1,68 +1,19 @@
/* global Buffer */
export function copyAll(a) {
return () => {
return Buffer.from(a);
};
}
import { Buffer } from "node:buffer";

export function writeInternal(ty) {
return value => {
return offset => {
return buf => {
return () => {
buf["write" + ty](value, offset);
};
};
};
};
}
export const freezeImpl = (a) => Buffer.from(a);
export const thawImpl = (a) => Buffer.from(a);

export function writeStringInternal(encoding) {
return offset => {
return length => {
return value => {
return buff => {
return () => {
return buff.write(value, offset, length, encoding);
};
};
};
};
};
}
export const writeInternal = (ty, value, offset, buf) => buf["write" + ty](value, offset);

export function setAtOffset(value) {
return offset => {
return buff => {
return () => {
buff[offset] = value;
};
};
};
}
export const writeStringInternal = (encoding, offset, length, value, buff) =>
buff.write(value, offset, length, encoding);

export function copy(srcStart) {
return srcEnd => {
return src => {
return targStart => {
return targ => {
return () => {
return src.copy(targ, targStart, srcStart, srcEnd);
};
};
};
};
};
}
export const setAtOffsetImpl = (value, offset, buff) => {
buff[offset] = value;
};

export function fill(octet) {
return start => {
return end => {
return buf => {
return () => {
buf.fill(octet, start, end);
};
};
};
};
}
export const copyImpl = (srcStart, srcEnd, src, targStart, targ) =>
src.copy(targ, targStart, srcStart, srcEnd);

export const fillImpl = (octet, start, end, buf) =>
buf.fill(octet, start, end);
Loading