Skip to content

Commit a8b24c4

Browse files
✨ feat: Add alloc, malloc, and _calloc.
1 parent 99b51e0 commit a8b24c4

File tree

13 files changed

+9554
-25
lines changed

13 files changed

+9554
-25
lines changed
File renamed without changes.

README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
:seat: [@array-like/alloc](https://array-like.github.io/alloc)
22
==
33

4-
ArrayLike allocation helper functions for JavaScript.
4+
`ArrayLike` allocation helper functions for JavaScript.
55
See [docs](https://array-like.github.io/alloc/index.html).
66

7-
> :building_construction: Caveat emptor! This is work in progress. Code may be
8-
> working. Documentation may be present. Coherence may be. Maybe.
7+
```js
8+
import {alloc, malloc, _calloc} from '@array-like/alloc';
99

10-
> :warning: Depending on your environment, the code may require
11-
> `regeneratorRuntime` to be defined, for instance by importing
12-
> [regenerator-runtime/runtime](https://www.npmjs.com/package/regenerator-runtime).
10+
alloc(10); // Array
11+
malloc(10); // ArrayBuffer
12+
const calloc = _calloc(Int32Array);
13+
calloc(10); // Int32Array
14+
```
1315

1416
[![License](https://img.shields.io/github/license/array-like/alloc.svg)](https://raw.githubusercontent.com/array-like/alloc/main/LICENSE)
1517
[![Version](https://img.shields.io/npm/v/@array-like/alloc.svg)](https://www.npmjs.org/package/@array-like/alloc)

doc/manual/usage.md

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,6 @@
11
# Usage
22

3-
> :warning: Depending on your environment, the code may require
4-
> `regeneratorRuntime` to be defined, for instance by importing
5-
> [regenerator-runtime/runtime](https://www.npmjs.com/package/regenerator-runtime).
6-
7-
First, require the polyfill at the entry point of your application
8-
```js
9-
await import('regenerator-runtime/runtime.js');
10-
// or
11-
import 'regenerator-runtime/runtime.js';
12-
```
13-
14-
Then, import the library where needed
3+
Import the library where needed
154
```js
165
const alloc = await import('@array-like/alloc');
176
// or

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@
199199
"unicorn"
200200
],
201201
"rules": {
202+
"unicorn/no-new-array": "off",
202203
"unicorn/filename-case": [
203204
"error",
204205
{

src/_calloc.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Method that creates an allocator from an array constructor.
3+
*
4+
* @param {any} ArrayConstructor
5+
* @return {Function}
6+
*/
7+
const _calloc = (ArrayConstructor) => (n) => new ArrayConstructor(n);
8+
export default _calloc;

src/alloc.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Method that allocates an Array.
3+
*
4+
* @param {number} n
5+
* @return {Array}
6+
*/
7+
const alloc = (n) => new Array(n);
8+
export default alloc;

src/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
const answer = 42;
2-
export default answer;
1+
export {default as _calloc} from './_calloc.js';
2+
export {default as alloc} from './alloc.js';
3+
export {default as malloc} from './malloc.js';

src/malloc.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Method that allocates an ArrayBuffer.
3+
*
4+
* @param {number} n
5+
* @return {ArrayBuffer}
6+
*/
7+
const malloc = (n) => new ArrayBuffer(n);
8+
export default malloc;

test/src/alloc.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import test from 'ava';
2+
3+
import {alloc} from '../../src/index.js';
4+
5+
test('alloc', (t) => {
6+
const a = alloc(10);
7+
8+
t.is(a.length, 10, 'check length');
9+
t.true(Array.isArray(a), 'check type');
10+
});

test/src/api.js

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

test/src/calloc.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import test from 'ava';
2+
3+
import {_calloc} from '../../src/index.js';
4+
5+
const macro = (t, TypedArray, n) => {
6+
const calloc = _calloc(TypedArray);
7+
8+
const a = calloc(n);
9+
10+
t.is(a.length, n, 'check length');
11+
t.true(a instanceof TypedArray, 'check type');
12+
};
13+
14+
macro.title = (title, TypedArray, n) =>
15+
title ?? `_calloc(${TypedArray.name})(${n})`;
16+
17+
test(macro, Uint8Array, 10);
18+
test(macro, Uint16Array, 133);
19+
test(macro, Int32Array, 27);

test/src/malloc.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import test from 'ava';
2+
3+
import {malloc} from '../../src/index.js';
4+
5+
test('malloc', (t) => {
6+
const a = malloc(10);
7+
8+
t.is(a.byteLength, 10, 'check length');
9+
t.true(a instanceof ArrayBuffer, 'check type');
10+
});

0 commit comments

Comments
 (0)