Skip to content

Commit

Permalink
Sync storage interface (node-only)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacogr committed Jan 10, 2018
1 parent acaaf05 commit be2305c
Show file tree
Hide file tree
Showing 17 changed files with 171 additions and 14 deletions.
9 changes: 9 additions & 0 deletions flow-typed/deasync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// @flow

declare module 'deasync' {
declare module.exports: {
loopWhile: (fn: () => boolean) => void;
runLoopOnce: () => void;
sleep: (ms: number) => void;
}
}
3 changes: 2 additions & 1 deletion packages/client-wasm-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
},
"dependencies": {
"@polkadot/util": "^0.11.4",
"babel-runtime": "^6.26.0"
"babel-runtime": "^6.26.0",
"deasync": "^0.1.12"
}
}
4 changes: 2 additions & 2 deletions packages/client-wasm-runtime/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
// @flow

import type { DbInterface } from '@polkadot/client-db/types';
import type { RuntimeExports } from './types';

const createEnv = require('./env');
const io = require('./io');
const memory = require('./memory');
const storage = require('./storage');

// flowlint-next-line unclear-type:off
module.exports = function runtime (wasmMemory: WebAssembly.Memory, db: DbInterface): { [string]: Function } {
module.exports = function runtime (wasmMemory: WebAssembly.Memory, db: DbInterface): RuntimeExports {
const env = createEnv(wasmMemory, db);
const exports = Object.assign(
{}, io(env), memory(env), storage(env)
Expand Down
2 changes: 0 additions & 2 deletions packages/client-wasm-runtime/src/io/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

import type { RuntimeEnv, RuntimeInterface$Io, PointerType } from '../types';

require('./polyfill');

const print = require('./print');
const printNum = require('./printNum');

Expand Down
4 changes: 2 additions & 2 deletions packages/client-wasm-runtime/src/io/print.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

import type { Logger } from '@polkadot/util/types';

const utf8Coder = new TextDecoder('utf-8');
const utf8Decode = require('../util/utf8Decode');

module.exports = function print (l: Logger, data: Uint8Array): void {
l.log(
utf8Coder.decode(data)
utf8Decode(data)
);
};
2 changes: 0 additions & 2 deletions packages/client-wasm-runtime/src/io/print.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// ISC, Copyright 2017-2018 Jaco Greeff

require('./polyfill');

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

Expand Down
15 changes: 11 additions & 4 deletions packages/client-wasm-runtime/src/storage/set.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,16 @@ import type { DbInterface } from '@polkadot/client-db/types';

const u8aToBuffer = require('@polkadot/util/u8a/toBuffer');

module.exports = function set (storage: DbInterface, _key: Uint8Array, _data: Uint8Array): void {
const key = u8aToBuffer(_key);
const data = u8aToBuffer(_data);
const utf8Decode = require('../util/utf8Decode');
const syncify = require('../util/syncify');

storage.put(key, data);
module.exports = function set (storage: DbInterface, key: Uint8Array, data: Uint8Array): void {
syncify(
storage.put(
utf8Decode(
u8aToBuffer(key)
),
u8aToBuffer(data)
)
);
};
32 changes: 32 additions & 0 deletions packages/client-wasm-runtime/src/storage/set.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// ISC, Copyright 2017-2018 Jaco Greeff
/* eslint camelcase: 0 */

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

describe('set_storage', () => {
let heap;
let storage;
let set_storage;

beforeEach(() => {
const uint8 = new Uint8Array([0x53, 0x61, 0x79, 0x48, 0x65, 0x6c, 0x6c, 0x6f]);

heap = {
get: (ptr, len) => uint8.subarray(ptr, ptr + len)
};

storage = {
put: jest.fn((key, value) => Promise.resolve(true))
};

set_storage = index({ heap, storage }).set_storage;
});

it('sets the value into storage', () => {
set_storage(0, 3, 3, 5);

expect(
storage.put
).toHaveBeenCalledWith('Say', Buffer.from('Hello'));
});
});
5 changes: 5 additions & 0 deletions packages/client-wasm-runtime/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,8 @@ export type RuntimeInterface = {
memory: RuntimeInterface$Memory,
storage: RuntimeInterface$Storage
};

export type RuntimeExports = {
// flowlint-next-line unclear-type:off
[string]: (any) => any
};
31 changes: 31 additions & 0 deletions packages/client-wasm-runtime/src/util/syncify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// ISC, Copyright 2017-2018 Jaco Greeff
// @flow

const deasync = require('deasync');

const isError = require('@polkadot/util/is/error');
const isUndefined = require('@polkadot/util/is/undefined');

// FIXME: This is currently for Node environments only - for Browser environments deasync is not available, we need a different solution. Once available, could move to @polkadot/util
// flowlint-next-line unclear-type:off
module.exports = function syncify (promise: Promise<any>): any {
let result;

promise
.catch((error) => error)
.then((_result) => {
result = _result;
});

let ms = 0;

while (isUndefined(result)) {
deasync.sleep(ms++);
}

if (isError(result)) {
throw result;
}

return result;
};
25 changes: 25 additions & 0 deletions packages/client-wasm-runtime/src/util/syncify.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// ISC, Copyright 2017-2018 Jaco Greeff

const syncify = require('./syncify');

describe('syncify', () => {
it('returns the result of the promise', () => {
expect(
syncify(
new Promise((resolve) => {
setTimeout(() => resolve(12345), 100);
})
)
).toEqual(12345);
});

it('throws promise exceptions', () => {
expect(
() => syncify(
new Promise((resolve, reject) => {
setTimeout(() => reject(new Error('test reject error')), 100);
})
)
).toThrow(/test reject error/);
});
});
10 changes: 10 additions & 0 deletions packages/client-wasm-runtime/src/util/utf8Decode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// ISC, Copyright 2017-2018 Jaco Greeff
// @flow

require('./polyfill');

const decoder = new TextDecoder('utf-8');

module.exports = function utf8Decode (u8a: Uint8Array): string {
return decoder.decode(u8a);
};
15 changes: 15 additions & 0 deletions packages/client-wasm-runtime/src/util/utf8Decode.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// ISC, Copyright 2017-2018 Jaco Greeff

const utf8Decode = require('./utf8Decode');

describe('utf8Decode', () => {
const TEST = 'Привет, мир!';

it('decodes the buffer correctly', () => {
expect(
utf8Decode(
new TextEncoder('utf-8').encode(TEST)
)
).toEqual(TEST);
});
});
15 changes: 15 additions & 0 deletions packages/client-wasm/src/types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// ISC, Copyright 2017-2018 Jaco Greeff
// @flow

export type WasmExports = {
// flowlint-next-line unclear-type:off
env: Object
};

export type WasmConfigType = {
memoryInitial: number,
memoryMaximum: number
};

// flowlint-next-line unclear-type:off
export type WasmExtraImports = Object;
13 changes: 12 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1103,6 +1103,10 @@ bindings@^1.2.1, bindings@~1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/bindings/-/bindings-1.3.0.tgz#b346f6ecf6a95f5a815c5839fc7cdb22502f1ed7"

bindings@~1.2.1:
version "1.2.1"
resolved "https://registry.yarnpkg.com/bindings/-/bindings-1.2.1.tgz#14ad6113812d2d37d72e67b4cacb4bb726505f11"

bip66@^1.1.3:
version "1.1.5"
resolved "https://registry.yarnpkg.com/bip66/-/bip66-1.1.5.tgz#01fa8748785ca70955d5011217d1b3139969ca22"
Expand Down Expand Up @@ -1789,6 +1793,13 @@ dateformat@^1.0.11, dateformat@^1.0.12:
get-stdin "^4.0.1"
meow "^3.3.0"

deasync@^0.1.12:
version "0.1.12"
resolved "https://registry.yarnpkg.com/deasync/-/deasync-0.1.12.tgz#0159492a4133ab301d6c778cf01e74e63b10e549"
dependencies:
bindings "~1.2.1"
nan "^2.0.7"

debug@*, debug@^3.0.0, debug@^3.0.1, debug@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/debug/-/debug-3.1.0.tgz#5bb5a0672628b64149566ba16819e61518c67261"
Expand Down Expand Up @@ -4696,7 +4707,7 @@ mute-stream@0.0.7:
version "0.0.7"
resolved "https://registry.yarnpkg.com/mute-stream/-/mute-stream-0.0.7.tgz#3075ce93bc21b8fab43e1bc4da7e8115ed1e7bab"

nan@^2.0.5, nan@^2.2.1, nan@^2.3.0, nan@^2.3.3, nan@~2.8.0:
nan@^2.0.5, nan@^2.0.7, nan@^2.2.1, nan@^2.3.0, nan@^2.3.3, nan@~2.8.0:
version "2.8.0"
resolved "https://registry.yarnpkg.com/nan/-/nan-2.8.0.tgz#ed715f3fe9de02b57a5e6252d90a96675e1f085a"

Expand Down

0 comments on commit be2305c

Please sign in to comment.