Skip to content

Commit

Permalink
Cleanup fill/dup, add tests for fill
Browse files Browse the repository at this point in the history
  • Loading branch information
jacogr committed Mar 19, 2018
1 parent ca2884e commit f08926b
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 4 deletions.
11 changes: 11 additions & 0 deletions packages/client-runtime/src/environment/heap/dup.js
@@ -0,0 +1,11 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow

import type { Pointer } from '../../types';
import type { Memory } from './types';

module.exports = function dup (memory: Memory, ptr: Pointer, len: number): Uint8Array {
return memory.uint8.slice(ptr, ptr + len);
};
11 changes: 11 additions & 0 deletions packages/client-runtime/src/environment/heap/fill.js
@@ -0,0 +1,11 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow

import type { Pointer } from '../../types';
import type { Memory } from './types';

module.exports = function fill (memory: Memory, ptr: Pointer, value: number, len: number): Uint8Array {
return memory.uint8.fill(value, ptr, ptr + len);
};
24 changes: 24 additions & 0 deletions packages/client-runtime/src/environment/heap/fill.spec.js
@@ -0,0 +1,24 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.

const envHeap = require('./index');

describe('fill', () => {
let heap;

beforeEach(() => {
const buffer = [0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0];

heap = envHeap();
heap.setWasmMemory({ buffer });
});

it('uses fill to set values', () => {
expect(
heap.fill(2, 5, 4)
).toEqual(
new Uint8Array([0x0, 0x0, 0x5, 0x5, 0x5, 0x5, 0x0, 0x0])
);
});
});
11 changes: 11 additions & 0 deletions packages/client-runtime/src/environment/heap/get.js
@@ -0,0 +1,11 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow

import type { Pointer } from '../../types';
import type { Memory } from './types';

module.exports = function get (memory: Memory, ptr: Pointer, len: number): Uint8Array {
return memory.uint8.subarray(ptr, ptr + len);
};
11 changes: 11 additions & 0 deletions packages/client-runtime/src/environment/heap/getU32.js
@@ -0,0 +1,11 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow

import type { Pointer } from '../../types';
import type { Memory } from './types';

module.exports = function getU32 (memory: Memory, ptr: Pointer): number {
return memory.view.getUint32(ptr, true);
};
12 changes: 8 additions & 4 deletions packages/client-runtime/src/environment/heap/index.js
Expand Up @@ -8,6 +8,10 @@ import type { HeapState, SizeUsed } from './types';

const allocate = require('./allocate');
const deallocate = require('./deallocate');
const dup = require('./dup');
const fill = require('./fill');
const get = require('./get');
const getU32 = require('./getU32');
const set = require('./set');
const setMemory = require('./setMemory');
const setU32 = require('./setU32');
Expand All @@ -22,13 +26,13 @@ module.exports = function envHeap (): RuntimeEnv$Heap {
deallocate: (ptr: Pointer): number =>
deallocate(state.memory, ptr),
dup: (ptr: Pointer, len: number): Uint8Array =>
state.memory.uint8.slice(ptr, ptr + len),
dup(state.memory, ptr, len),
fill: (ptr: Pointer, value: number, len: number): Uint8Array =>
state.memory.uint8.fill(value, ptr, len),
fill(state.memory, ptr, value, len),
get: (ptr: Pointer, len: number): Uint8Array =>
state.memory.uint8.subarray(ptr, ptr + len),
get(state.memory, ptr, len),
getU32: (ptr: Pointer): number =>
state.memory.view.getUint32(ptr, true),
getU32(state.memory, ptr),
set: (ptr: Pointer, data: Uint8Array): Pointer =>
set(state.memory, ptr, data),
setU32: (ptr: Pointer, value: number): Pointer =>
Expand Down

0 comments on commit f08926b

Please sign in to comment.