Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support basic dynamic linking with example #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion assembly/hello-world/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import * as env from '../env'

/**
* Examples for linking a library
*/
@external("add", "incr")
export declare function incr(a: u32): u32

@external("add", "add")
export declare function add(a: u32, b: u32): u32

/*
* Increments preStateRoot by one
*/
Expand All @@ -10,7 +19,8 @@ export function main(): void {
var postStateRootPtr: u32 = __heap_base + 32

var numPtr: u32 = __heap_base + 64
store<u8>(numPtr, 1, 31)
// Silly example of how you can use lib
store<u8>(numPtr, add(0, 1), 31)

env.bignum_add256(preStateRootPtr, numPtr, postStateRootPtr)

Expand Down
7 changes: 7 additions & 0 deletions assembly/lib/add.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function incr(a: u32): u32 {
return a + 1
}

export function add(a: u32, b: u32): u32 {
return a + b
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"docs": "typedoc --out docs --mode modules --theme markdown --excludeNotExported src",
"asbuild:untouched": "asc assembly/hello-world/index.ts -b build/hello-world-untouched.wasm -t build/hello-world-untouched.wat --sourceMap --validate --debug",
"asbuild:optimized": "asc assembly/hello-world/index.ts -b build/hello-world-optimized.wasm -t build/hello-world-optimized.wat --sourceMap --validate --optimize",
"asbuild": "npm run asbuild:untouched && npm run asbuild:optimized"
"asbuild": "npm run asbuild:untouched && npm run asbuild:optimized",
"asbuild:lib": "asc assembly/lib/add.ts -b build/lib-add-optimized.wasm -t build/lib-add-optimized.wat --sourceMap --validate --optimize"
},
"author": "s1na",
"license": "Apache-2.0",
Expand Down
28 changes: 27 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ function main() {
const wasmModule = new WebAssembly.Module(wasmFile)
let preStateRoot = testCase.preStateRoot
for (const block of testCase.blocks) {
const instance = new WebAssembly.Instance(wasmModule, getImports({ preStateRoot, blockData: block }))
let libExports = getLibExports(testCase.libs)
let imports = getImports({ preStateRoot, blockData: block })
imports = { ...imports, ...libExports }
const instance = new WebAssembly.Instance(wasmModule, imports)
setMemory(instance.exports.memory)
let t = process.hrtime()
instance.exports.main()
Expand All @@ -34,4 +37,27 @@ function main() {
}
}

interface Exports {
// { libName: { exportName: Fn } }
[n: string]: { [f: string]: Function }
}

function getLibExports(libs: { [k: string]: string }): Exports {
let libExports: Exports = {}
for (const libName in libs) {
const libFile = fs.readFileSync(libs[libName])
const libModule = new WebAssembly.Module(libFile)
const libInstance = new WebAssembly.Instance(libModule, { env: { abort: () => { throw new Error('Wasm aborted') } } })
const exports: { [k: string]: Function } = {}
for (const fn in libInstance.exports) {
if (fn === 'memory' || fn.startsWith('_')) {
continue
}
exports[fn] = libInstance.exports[fn]
}
libExports[libName] = exports
}
return libExports
}

main()
5 changes: 5 additions & 0 deletions src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface ShardBlock {

export interface TestCase {
script: string
libs: { [k: string]: string } // { name: file }
preStateRoot: Buffer
blocks: Buffer[]
postStateRoot: Buffer
Expand Down Expand Up @@ -100,6 +101,9 @@ export const getImports = (env: EnvData) => {
export function parseYaml (file: string): TestCase[] {
const testCase = safeLoad(file)
const scripts = testCase.beacon_state.execution_scripts
const libs = (testCase.beacon_state.libraries || []).reduce(
(a: { [k: string]: string }, v: { name: string, file: string }) => { a[v.name] = v.file; return a }, {}
)
const shardBlocks = testCase.shard_blocks
const testCases = []
for (let i = 0; i < scripts.length; i++) {
Expand All @@ -118,6 +122,7 @@ export function parseYaml (file: string): TestCase[] {

testCases.push({
script,
libs,
preStateRoot,
postStateRoot,
blocks
Expand Down
3 changes: 3 additions & 0 deletions test.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
beacon_state:
execution_scripts:
- build/hello-world-optimized.wasm
libraries:
- name: "add"
file: build/lib-add-optimized.wasm
shard_pre_state:
exec_env_states:
- "0000000000000000000000000000000000000000000000000000000000000001"
Expand Down