Last active
March 27, 2023 20:06
-
-
Save JonDotsoy/3c4c0a2a38f6a3ea032f028f5f8a1f2b to your computer and use it in GitHub Desktop.
Full sample make assemblyscript with JSON transformer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"targets": { | |
"debug": { | |
"outFile": "build/debug.wasm", | |
"textFile": "build/debug.wat", | |
"sourceMap": true, | |
"debug": true | |
}, | |
"release": { | |
"outFile": "build/release.wasm", | |
"textFile": "build/release.wat", | |
"sourceMap": true, | |
"optimizeLevel": 3, | |
"shrinkLevel": 0, | |
"converge": false, | |
"noAssert": false | |
} | |
}, | |
"options": { | |
"bindings": "esm", | |
"transform": ["json-as/transform"] | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { JSON } from 'json-as' | |
let tick: i32 = 0 | |
const getTick = (): i32 => ++tick | |
// @ts-ignore | |
@json | |
class OutputJSONSchema { | |
tick!: number | |
input!: string | |
} | |
// @ts-ignore | |
@json | |
class InputJSONSchema { | |
input!: string | |
} | |
export function hello(arr: ArrayBuffer): ArrayBuffer { | |
const entryStr = JSON.parse<InputJSONSchema>(String.UTF8.decode(arr)) | |
const a: OutputJSONSchema = { | |
tick: getTick(), | |
input: entryStr.input, | |
} | |
const json = JSON.stringify<OutputJSONSchema>(a) | |
return String.UTF8.encode(`${json}`, true) | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"extends": "assemblyscript/std/assembly.json", | |
"include": [ | |
"./**/*.ts" | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { writeFileSync } from "fs"; | |
import { readFile } from "fs/promises" | |
import { inspect } from "util" | |
const main = async () => { | |
const wasmModule = await WebAssembly.compile(new Uint8Array(await readFile("./build/release.wasm"))) | |
let instance = await WebAssembly.instantiate(wasmModule, { | |
env: { | |
abort: (...args) => console.log("abort:", ...args) | |
} | |
}); | |
const hello = (payload) => { | |
const inpBuff = new TextEncoder().encode(payload); | |
const p = instance.exports.__new(inpBuff.length, 1); | |
new Uint8Array(instance.exports.memory.buffer).set(inpBuff, p) | |
const ptr = instance.exports.hello(p) | |
const dv = new DataView(instance.exports.memory.buffer, ptr) | |
let len = 0 | |
for (let i = 0; i < dv.byteLength; i++) { | |
if (dv.getUint8(i) === 0) { | |
len = i | |
break | |
} | |
} | |
const buff = new Uint8Array(instance.exports.memory.buffer, ptr, len); | |
return `${utilInspect(buff)}\n${new TextDecoder().decode(buff)}` | |
} | |
writeFileSync(".log1", hello(JSON.stringify({ input: "Rick" }))) | |
writeFileSync(".log2", hello(JSON.stringify({ input: "Morty" }))) | |
} | |
main() | |
.catch(err => { | |
console.error(err.stack) | |
}) | |
function utilInspect(newLocal) { | |
return inspect(newLocal, { depth: Infinity, maxArrayLength: Infinity, maxStringLength: Infinity }); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "tdir", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "node tests", | |
"asbuild:debug": "asc assembly/index.ts --target debug", | |
"asbuild:release": "asc assembly/index.ts --target release", | |
"asbuild": "npm run asbuild:debug && npm run asbuild:release", | |
"start": "npx serve ." | |
}, | |
"keywords": [], | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"assemblyscript": "^0.27.1", | |
"json-as": "^0.5.36", | |
"visitor-as": "^0.11.4" | |
}, | |
"type": "module", | |
"exports": { | |
".": { | |
"import": "./build/release.js", | |
"types": "./build/release.d.ts" | |
} | |
}, | |
"devDependencies": { | |
"assemblyscript": "^0.27.1" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment