From f08926bf3e7b9ec00a5851f02b0190af1734b630 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Mon, 19 Mar 2018 14:26:43 +0100 Subject: [PATCH] Cleanup fill/dup, add tests for fill --- .../src/environment/heap/dup.js | 11 +++++++++ .../src/environment/heap/fill.js | 11 +++++++++ .../src/environment/heap/fill.spec.js | 24 +++++++++++++++++++ .../src/environment/heap/get.js | 11 +++++++++ .../src/environment/heap/getU32.js | 11 +++++++++ .../src/environment/heap/index.js | 12 ++++++---- 6 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 packages/client-runtime/src/environment/heap/dup.js create mode 100644 packages/client-runtime/src/environment/heap/fill.js create mode 100644 packages/client-runtime/src/environment/heap/fill.spec.js create mode 100644 packages/client-runtime/src/environment/heap/get.js create mode 100644 packages/client-runtime/src/environment/heap/getU32.js diff --git a/packages/client-runtime/src/environment/heap/dup.js b/packages/client-runtime/src/environment/heap/dup.js new file mode 100644 index 00000000..d781b5f1 --- /dev/null +++ b/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); +}; diff --git a/packages/client-runtime/src/environment/heap/fill.js b/packages/client-runtime/src/environment/heap/fill.js new file mode 100644 index 00000000..d6bc3803 --- /dev/null +++ b/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); +}; diff --git a/packages/client-runtime/src/environment/heap/fill.spec.js b/packages/client-runtime/src/environment/heap/fill.spec.js new file mode 100644 index 00000000..11f9d343 --- /dev/null +++ b/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]) + ); + }); +}); diff --git a/packages/client-runtime/src/environment/heap/get.js b/packages/client-runtime/src/environment/heap/get.js new file mode 100644 index 00000000..8dc334b1 --- /dev/null +++ b/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); +}; diff --git a/packages/client-runtime/src/environment/heap/getU32.js b/packages/client-runtime/src/environment/heap/getU32.js new file mode 100644 index 00000000..870762a7 --- /dev/null +++ b/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); +}; diff --git a/packages/client-runtime/src/environment/heap/index.js b/packages/client-runtime/src/environment/heap/index.js index 6f1a7c27..3bde4810 100644 --- a/packages/client-runtime/src/environment/heap/index.js +++ b/packages/client-runtime/src/environment/heap/index.js @@ -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'); @@ -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 =>