add json parser, try tests

This commit is contained in:
DieMyst
2019-03-14 17:35:54 +03:00
parent a0374469ce
commit 9f869ea8b6
12 changed files with 5174 additions and 33 deletions

14
assembly/dice.ts Normal file
View File

@ -0,0 +1,14 @@
import { JSONDecoder } from "../node_modules/assemblyscript-json/assembly/decoder";
import { JSONEncoder } from "../node_modules/assemblyscript-json/assembly/encoder";
const PLAYERS_MAX_COUNT: usize = 1024;
const SEED: u64 = 12345678;
// the account balance of new players
const INIT_ACCOUNT_BALANCE: u64 = 100;
// if win, player receives bet_amount * PAYOUT_RATE money
const PAYOUT_RATE: u64 = 5;
export function handler(input: string): string {
return "";
}

View File

@ -1,15 +1,9 @@
import "allocator/tlsf";
// import "allocator/tlsf";
import {handler} from "./dice";
import {JSONDecoder, JSONHandler} from "../node_modules/assemblyscript-json/assembly/decoder";
//import "allocator/buddy";
//import "allocator/arena";
import { u128 } from "../node_modules/bignum/assembly/integer/u128";
// this is causes OOM
import { AES } from "../node_modules/crypto-ts/src/algo/AES";
// this is too
// import { Queue } from "../node_modules/typescript-collections/src/lib/Queue";
export function allocate(size: i32) :i32 {
return memory.allocate(size);
}
@ -20,24 +14,67 @@ export function deallocate(ptr: i32, size: i32): void {
export function invoke(ptr: i32, size: i32): i32 {
let a = u128.One;
let bb: Uint8Array = new Uint8Array(size);
let inputStr: string = String.fromUTF8(ptr, size);
for (let i = 0; i < size; i++) {
bb[i] = load<u8>(ptr + i)
}
let str = "Hello, world! From user " + inputStr;
let jsonHandler = new ToDictJSONEventsHandler();
let decoder = new JSONDecoder<ToDictJSONEventsHandler>(jsonHandler);
let strLen: i32 = str.length;
let addr = memory.allocate(strLen + 4);
for (let i = 0; i < 4; i++) {
let b: u8 = (strLen >> i * 8) as u8 & 0xFF;
store<u8>(addr + i, b);
}
decoder.deserialize(bb);
let strAddr = addr + 4;
for (let i = 0; i < strLen; i++) {
let b: u8 = str.charCodeAt(i) as u8;
store<u8>(strAddr + i, b);
}
let result = jsonHandler.strings.get("test");
return addr;
let strLen: i32 = result.length;
let addr = memory.allocate(strLen + 4);
for (let i = 0; i < 4; i++) {
let b: u8 = (strLen >> i * 8) as u8 & 0xFF;
store<u8>(addr + i, b);
}
let strAddr = addr + 4;
for (let i = 0; i < strLen; i++) {
let b: u8 = result.charCodeAt(i) as u8;
store<u8>(strAddr + i, b);
}
return addr;
}
class ToDictJSONEventsHandler extends JSONHandler {
public strings: Map<string, string> = new Map();
public booleans: Map<string, bool> = new Map();
public integers: Map<string, i64> = new Map();
setString(name: string, value: string): void {
this.strings.set(name, value);
}
setBoolean(name: string, value: bool): void {
this.booleans.set(name, value);
}
setNull(name: string): void {
}
setInteger(name: string, value: i64): void {
this.integers.set(name, value);
}
pushArray(name: string): bool {
return true;
}
popArray(): void {
}
pushObject(name: string): bool {
return true;
}
popObject(): void {
}
}

View File

@ -3,4 +3,4 @@
"include": [
"./**/*.ts"
]
}
}

4727
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -4,19 +4,38 @@
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"asbuild:optimized": "asc assembly/index.ts -b build/optimized.wasm -t build/optimized.wat --use abort=''",
"asbuild": "npm run asbuild:optimized"
"asbuild:optimized": "asc assembly/index.ts -b build/optimized.wasm -t build/optimized.wat --use abort='' --sourceMap --validate",
"asbuild": "npm run asbuild:optimized",
"asbuild:test": "npm run asbuild:test:roundtrip",
"asbuild:test:roundtrip": "npx asc tests/assembly/roundtrip.spec.as.ts -b tests/build/roundtrip.wasm -t tests/build/roundtrip.wat --validate --sourceMap --importMemory",
"test": "npm run asbuild:test && ava -v --serial"
},
"author": "",
"license": "ISC",
"devDependencies": {
"assemblyscript": "github:AssemblyScript/assemblyscript"
"@types/node": "^10.12.3",
"assemblyscript": "AssemblyScript/assemblyscript",
"ava": "1.0.0-rc.1",
"ts-node": "^7.0.1",
"typedoc": "^0.13.0",
"typescript": "^3.1.6"
},
"dependencies": {
"bignum": "github:MaxGraey/bignum.wasm",
"assemblyscript-bson": "github:nearprotocol/assemblyscript-bson",
"assemblyscript-json": "github:nearprotocol/assemblyscript-json",
"typescript-collections": "github:fluencelabs/typescript-collections",
"crypto-ts": "github:hmoog/crypto-ts"
},
"ava": {
"compileEnhancements": true,
"extensions": [
"ts"
],
"require": [
"ts-node/register/transpile-only"
],
"files": [
"tests/**/*.spec.ts"
]
}
}

View File

@ -0,0 +1,37 @@
import "allocator/arena";
import { invoke } from "../../assembly/index";
declare function logStr(str: string): void;
declare function logF64(val: f64): void;
export class SomeTest {
static shouldHandleEmptyObject(): bool {
return this.roundripTest("{\"test\": \"heloooooooooooooooooo\"}");
}
private static roundripTest(jsonString: string): bool {
logStr("----------------------------------------------");
let inputLen = jsonString.lengthUTF8;
let utf8ptr = jsonString.toUTF8();
let resultPtr = invoke(utf8ptr, inputLen);
let resultLength: i32 = 0;
for (let i = 3; i >= 0; i--) {
let b = load<u8>(resultPtr + i) as i32;
logF64(b);
resultLength = resultLength + b << i * 8;
}
logF64(resultLength);
logStr("result: " + String.fromUTF8(resultPtr + 4, resultLength));
return true;
}
}

View File

@ -0,0 +1,63 @@
{
"compilerOptions": {
/* Basic Options */
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
// "lib": [], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
// "declaration": true, /* Generates corresponding '.d.ts' file. */
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
// "sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
// "outDir": "./", /* Redirect output structure to the directory. */
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "composite": true, /* Enable project compilation */
// "removeComments": true, /* Do not emit comments to output. */
// "noEmit": true, /* Do not emit outputs. */
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
/* Strict Type-Checking Options */
"strict": true, /* Enable all strict type-checking options. */
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* Enable strict null checks. */
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
/* Additional Checks */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
/* Module Resolution Options */
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
// "typeRoots": [], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
/* Source Map Options */
// "sourceRoot": "./", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
// "mapRoot": "./", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
/* Experimental Options */
"experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
},
"extends": "../../node_modules/assemblyscript/std/assembly.json",
"include": [
"./**/*.ts","*.ts"
]
}

3
tests/roundtrip.spec.ts Normal file
View File

@ -0,0 +1,3 @@
import { defineTestsFromModule } from './utils/spec';
defineTestsFromModule('roundtrip');

96
tests/types/webassembly/index.d.ts vendored Normal file
View File

@ -0,0 +1,96 @@
/**
* WebAssembly v1 (MVP) declaration file for TypeScript
* Definitions by: 01alchemist (https://twitter.com/01alchemist)
*/
declare namespace WebAssembly {
/**
* WebAssembly.Module
**/
class Module {
constructor(bufferSource: ArrayBuffer | ArrayBufferView<number>);
static customSections(module: Module, sectionName: string): ArrayBuffer[];
static exports(module: Module): { name: string, kind: string }[];
static imports(module: Module): { module: string, name: string, kind: string }[];
}
/**
* WebAssembly.Instance
**/
class Instance {
readonly exports: any;
constructor(module: Module, importObject?: any);
}
/**
* WebAssembly.Memory
* Note: A WebAssembly page has a constant size of 65,536 bytes, i.e., 64KiB.
**/
interface MemoryDescriptor {
initial: number;
maximum?: number;
}
class Memory {
readonly buffer: ArrayBuffer;
constructor(memoryDescriptor: MemoryDescriptor);
grow(numPages: number): number;
}
/**
* WebAssembly.Table
**/
interface TableDescriptor {
element: "anyfunc",
initial: number;
maximum?: number;
}
class Table {
readonly length: number;
constructor(tableDescriptor: TableDescriptor);
get(index: number): Function;
grow(numElements: number): number;
set(index: number, value: Function): void;
}
/**
* Errors
*/
class CompileError extends Error {
readonly fileName: string;
readonly lineNumber: string;
readonly columnNumber: string;
constructor(message?: string, fileName?: string, lineNumber?: number);
toString(): string;
}
class LinkError extends Error {
readonly fileName: string;
readonly lineNumber: string;
readonly columnNumber: string;
constructor(message?: string, fileName?: string, lineNumber?: number);
toString(): string;
}
class RuntimeError extends Error {
readonly fileName: string;
readonly lineNumber: string;
readonly columnNumber: string;
constructor(message?: string, fileName?: string, lineNumber?: number);
toString(): string;
}
function compile(bufferSource: ArrayBuffer | ArrayBufferView<number>): Promise<Module>;
interface ResultObject {
module: Module;
instance: Instance;
}
function instantiateStreaming(bufferSource: ArrayBuffer | ArrayBufferView<number>, importObject?: any): Promise<ResultObject>;
function instantiate(bufferSource: ArrayBuffer | ArrayBufferView<number>, importObject?: any): Promise<ResultObject>;
function instantiate(module: Module, importObject?: any): Promise<Instance>;
function validate(bufferSource: ArrayBuffer | ArrayBufferView<number>): boolean;
}

View File

@ -0,0 +1,3 @@
{
"types": "index.d.ts"
}

114
tests/utils/helpers.ts Normal file
View File

@ -0,0 +1,114 @@
/// <reference path="../types/webassembly/index.d.ts" />
import * as fs from 'fs';
import * as path from 'path';
import * as util from 'util';
import { demangle } from 'assemblyscript/lib/loader';
const DIGITALS_REGEXP = /([0-9]{1,})/g;
const UPPER_ALPHAS_REGEXP = /([A-Z]{1,})/g;
export type ImportEntries = { [key: string]: object };
export type ExportedEntry = { [key: string]: Function };
export type ExportedEntries = { [key: string]: ExportedEntry };
const readFile = util.promisify(fs.readFile);
const F64 = new Float64Array(1);
const U64 = new Uint32Array(F64.buffer);
export function decamelize(str: string): string {
const t = str
.replace(DIGITALS_REGEXP, ' $1')
.replace(UPPER_ALPHAS_REGEXP, m => ' ' + (m.length === 1 ? m.toLowerCase() : m));
return t.charAt(0).toUpperCase() + t.slice(1);
}
export async function setup(testFileName: string): Promise<ExportedEntries> {
const pathName = path.resolve(__dirname, `../build/${ testFileName }.wasm`);
const file = await readFile(pathName, null);
if (!WebAssembly.validate(file)) {
throw new Error(`WebAssembly binary "${ pathName }" file not valid!`);
}
const imports = buildImports(`${ testFileName }.spec.as`, new WebAssembly.Memory({ initial: 2 }));
const result = await WebAssembly.instantiate(file, imports);
return demangle<ExportedEntries>(result.instance.exports);
}
function unpackToString64(value: number): string {
F64[0] = value;
return U64[1].toString(16) + U64[0].toString(16);
}
function unpackToString128(lo: number, hi: number): string {
return `0x${ (unpackToString64(hi) + unpackToString64(lo)).padStart(32, '0') }`;
}
function getString(ptr: number, buffer: ArrayBuffer): string {
var U16 = new Uint16Array(buffer);
var U32 = new Uint32Array(buffer);
var dataLength = U32[ptr >>> 2];
var dataOffset = (ptr + 4) >>> 1;
var dataRemain = dataLength;
var parts = [];
const chunkSize = 1024;
while (dataRemain > chunkSize) {
let last = U16[dataOffset + chunkSize - 1];
let size = last >= 0xD800 && last < 0xDC00 ? chunkSize - 1 : chunkSize;
let part = U16.subarray(dataOffset, dataOffset += size);
parts.push(String.fromCharCode.apply(String, part));
dataRemain -= size;
}
return parts.join('') + String.fromCharCode.apply(String, U16.subarray(dataOffset, dataOffset + dataRemain));
}
function buildImports(name: string, memory: WebAssembly.Memory): ImportEntries {
const buffer = memory.buffer;
return {
env: {
memory,
abort(msgPtr: number, filePtr: number, line: number, column: number) {
if (msgPtr) {
throw new Error(
`Abort called by reason "${ getString(msgPtr, buffer) }" at ${ getString(filePtr, buffer) } [${ line }:${ column }]`
);
} else {
throw new Error(`Abort called at ${ getString(filePtr, buffer) } [${ line }:${ column }]`);
}
},
},
// TODO: Don't hardcode support for encoder/decoder
decoder: {
logStr(msgPtr: number) {
if (msgPtr) console.log(`[str]: ${ getString(msgPtr, buffer) }`);
},
logF64(value: number) {
console.log(`[f64]: ${ value }`);
},
},
encoder: {
logStr(msgPtr: number) {
if (msgPtr) console.log(`[str]: ${ getString(msgPtr, buffer) }`);
},
logF64(value: number) {
console.log(`[f64]: ${ value }`);
},
},
[name]: {
logF64(value: number) {
console.log(`[f64]: ${ value }`);
},
logStr(msgPtr: number) {
if (msgPtr) console.log(`[str]: ${ getString(msgPtr, buffer) }`);
},
logU128Packed(msgPtr: number, lo: number, hi: number) {
if (msgPtr) {
console.log(`[u128] ${ getString(msgPtr, buffer) }: ${ unpackToString128(lo, hi) }`);
} else {
console.log(`[u128]: ${ unpackToString128(lo, hi) }`);
}
}
}
};
}

34
tests/utils/spec.ts Normal file
View File

@ -0,0 +1,34 @@
import test from 'ava';
import { setup, decamelize } from './helpers';
export async function defineTestsFromModule(moduleName: string) {
try {
const instance = await setup(moduleName);
// TODO: Refactor into proper testing framework for AssemblyScript
for (const tests in instance) {
const testsInstance = instance[tests];
if (testsInstance.setUp) {
test.beforeEach(() => {
testsInstance.setUp();
});
}
if (testsInstance.tearDown) {
test.afterEach(() => {
testsInstance.tearDown();
});
}
for (const testName of Object.keys(testsInstance).filter(it => !(["setUp", "tearDown"].indexOf(it) != -1))) {
if (testName.startsWith("shouldAbort")) {
test(decamelize(testName), t => { t.throws(() => testsInstance[testName]()) });
} else {
test(decamelize(testName), t => t.truthy(testsInstance[testName]()));
}
}
}
} catch (e) {
console.log("Error loading WebAssembly module:", e);
throw e;
}
};