Skip to content

Commit

Permalink
feat(sdk): Add AssemblyScript SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
FranklinWaller committed Aug 22, 2023
1 parent c074579 commit 7fe85d5
Show file tree
Hide file tree
Showing 28 changed files with 6,922 additions and 262 deletions.
4 changes: 3 additions & 1 deletion .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
ARG VARIANT=18
FROM mcr.microsoft.com/vscode/devcontainers/typescript-node:${VARIANT}
RUN apt-get update
RUN apt-get -y install --no-install-recommends zsh
RUN apt-get -y install --no-install-recommends zsh
RUN apt-get -y install binaryen
RUN apt-get -y install build-essential cmake
23 changes: 23 additions & 0 deletions libs/as-sdk-integration-tests/asconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"extends": "../../node_modules/@assemblyscript/wasi-shim/asconfig.json",
"targets": {
"debug": {
"outFile": "../../dist/libs/as-sdk-integration-tests/debug.wasm",
"textFile": "../../dist/libs/as-sdk-integration-tests/debug.wat",
"sourceMap": true,
"debug": true
},
"release": {
"outFile": "../../dist/libs/as-sdk-integration-tests/release.wasm",
"textFile": "../../dist/libs/as-sdk-integration-tests/release.wat",
"sourceMap": false,
"optimizeLevel": 3,
"shrinkLevel": 2,
"converge": true,
"noAssert": true
}
},
"options": {
"bindings": "esm"
}
}
37 changes: 37 additions & 0 deletions libs/as-sdk-integration-tests/assembly/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// The entry file of your WebAssembly module.

import { httpFetch, Process } from '../../as-sdk/assembly/index';

const args = Process.args()[0];

if (args === 'testHttpRejection') {
testHttpRejection();
} else if (args === 'testHttpSuccess') {
testHttpSuccess();
}

export function testHttpRejection(): void {
const response = httpFetch('example.com/');
const rejected = response.rejected;

if (rejected !== null) {
const msg = String.UTF8.encode('ok');
const buffer = Uint8Array.wrap(msg);

Process.exit_with_result(0, buffer);
}
}

export function testHttpSuccess(): void {
const response = httpFetch('http://example.com/');
const fulfilled = response.fulfilled;

if (fulfilled !== null) {
const msg = String.UTF8.encode('ok');
const buffer = Uint8Array.wrap(msg);

Process.exit_with_result(0, buffer);
}
}


4 changes: 4 additions & 0 deletions libs/as-sdk-integration-tests/assembly/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "../../../node_modules/assemblyscript/std/assembly.json",
"include": ["./**/*.ts"]
}
6 changes: 6 additions & 0 deletions libs/as-sdk-integration-tests/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/* eslint-disable */
export default {
displayName: 'vm',
preset: '../../jest.preset.mjs',
coverageDirectory: '../../coverage/libs/as-sdk-integration-tests',
};
4 changes: 4 additions & 0 deletions libs/as-sdk-integration-tests/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "@seda/as-sdk-integration-tests",
"type": "module"
}
30 changes: 30 additions & 0 deletions libs/as-sdk-integration-tests/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "as-sdk-integration-tests",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "libs/as-sdk-integration-tests/assembly",
"projectType": "library",
"targets": {
"build": {
"executor": "nx:run-commands",
"options": {
"command": "asc ./assembly/index.ts --target debug",
"cwd": "libs/as-sdk-integration-tests"
}
},
"test": {
"executor": "@nx/jest:jest",
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
"options": {
"jestConfig": "libs/as-sdk-integration-tests/jest.config.ts",
"passWithNoTests": true
},
"configurations": {
"ci": {
"ci": true,
"codeCoverage": true
}
}
}
},
"tags": []
}
51 changes: 51 additions & 0 deletions libs/as-sdk-integration-tests/src/http.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { callVm, } from '../../../dist/libs/vm';
import { jest } from '@jest/globals';
import { readFile } from 'node:fs/promises';
import { HttpFetchResponse } from '../../../dist/libs/vm/src/types/vm-actions';
import { PromiseStatus } from '../../../dist/libs/vm/src/types/vm-promise';

const mockHttpFetch = jest.fn();

const TestVmAdapter = jest.fn().mockImplementation(() => {
return { httpFetch: mockHttpFetch };
});

describe('Http', () => {
it('Test SDK HTTP Rejection', async () => {

const wasmBinary = await readFile('dist/libs/as-sdk-integration-tests/debug.wasm');
const result = await callVm({
args: ['testHttpRejection'],
envs: {},
binary: new Uint8Array(wasmBinary),
});

expect(result.exitCode).toBe(0);
expect(result.result).toEqual(new TextEncoder().encode("ok"));
});

it('Test SDK HTTP Success', async () => {
const wasmBinary = await readFile(
'dist/libs/as-sdk-integration-tests/debug.wasm'
);

const mockResponse = new HttpFetchResponse({
content_length: 1,
bytes: [1],
headers: {},
status: 200,
url: 'http://example.com',
});

mockHttpFetch.mockResolvedValue(PromiseStatus.fulfilled(mockResponse));

const result = await callVm({
args: ['testHttpSuccess'],
envs: {},
binary: new Uint8Array(wasmBinary),
}, undefined, new TestVmAdapter());

expect(result.exitCode).toBe(0);
expect(result.result).toEqual(new TextEncoder().encode('ok'));
});
});
24 changes: 24 additions & 0 deletions libs/as-sdk/as-pect.asconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"targets": {
"coverage": {
"lib": ["../../node_modules/@as-covers/assembly/index.ts"],
"transform": ["@as-covers/transform", "@as-pect/transform"]
},
"noCoverage": {
"transform": ["@as-pect/transform"]
}
},
"options": {
"exportMemory": true,
"outFile": "output.wasm",
"textFile": "output.wat",
"bindings": "raw",
"exportStart": "_start",
"exportRuntime": true,
"use": ["RTRACE=1"],
"debug": true,
"exportTable": true
},
"extends": "./asconfig.json",
"entries": ["../../node_modules/@as-pect/assembly/assembly/index.ts"]
}
32 changes: 32 additions & 0 deletions libs/as-sdk/as-pect.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
export default {
/**
* A set of globs passed to the glob package that qualify typescript files for testing.
*/
entries: ['./assembly/__test__/**/*.spec.ts'],
/**
* A set of globs passed to the glob package that quality files to be added to each test.
*/
include: ['./assembly/__test__/**/*.include.ts'],
/**
* A set of regexp that will disclude source files from testing.
*/
disclude: [/node_modules/],
/**
* Add your required AssemblyScript imports here.
*/
async instantiate(memory, createImports, instantiate, binary) {
let instance; // Imports can reference this
const myImports = {
env: { memory },
// put your web assembly imports here, and return the module promise
};
instance = instantiate(binary, createImports(myImports));
return instance;
},
/** Enable code coverage. */
// coverage: ["assembly/**/*.ts"],
/**
* Specify if the binary wasm file should be written to the file system.
*/
outputBinary: false,
};
23 changes: 23 additions & 0 deletions libs/as-sdk/asconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"extends": "../../node_modules/@assemblyscript/wasi-shim/asconfig.json",
"targets": {
"debug": {
"outFile": "../../dist/libs/as-sdk/debug.wasm",
"textFile": "../../dist/libs/as-sdk/debug.wat",
"sourceMap": true,
"debug": true
},
"release": {
"outFile": "../../dist/libs/as-sdk/release.wasm",
"textFile": "../../dist/libs/as-sdk/release.wat",
"sourceMap": true,
"optimizeLevel": 3,
"shrinkLevel": 0,
"converge": false,
"noAssert": false
}
},
"options": {
"bindings": "esm"
}
}
1 change: 1 addition & 0 deletions libs/as-sdk/assembly/__test__/as-pect.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="@as-pect/assembly/types/as-pect" />
15 changes: 15 additions & 0 deletions libs/as-sdk/assembly/__test__/http.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { httpFetch, HttpFetchOptions } from '../http';

describe('http', () => {
it('do a http fetch', () => {
const options = new HttpFetchOptions();
const headers = new Map<string, string>();

headers.set('Content-Type', 'application/json');
options.headers = headers;

const result = httpFetch('http://example.com', options);

expect(result.toString()).toBe("{}");
});
});
11 changes: 11 additions & 0 deletions libs/as-sdk/assembly/bindings/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export declare function http_fetch(
action_ptr: usize,
action_length: u32
): u32;

export declare function call_result_write(
result: usize,
result_length: u32
): void;

export declare function execution_result(result: usize, result_length: u32): void;
Loading

0 comments on commit 7fe85d5

Please sign in to comment.