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

perf(loader): allow skipping outputting memory #1029

Open
wants to merge 1 commit into
base: main
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
13 changes: 11 additions & 2 deletions loader/src/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,17 @@ const metering = require('@permaweb/wasm-metering')
* @property {AssignmentTypes.Assignment[]} Assignments
*/

/**
* @typedef HandleOptions
* @property {boolean} [outputMemory=true]
*/

/**
* @callback handleFunction
* @param {ArrayBuffer | null} buffer
* @param {Message} msg
* @param {Environment} env
* @param {HandleOptions} [options]
* @returns {Promise<HandleResponse>}
*/

Expand Down Expand Up @@ -156,7 +162,8 @@ module.exports = async function (binary, options) {
WebAssembly.instantiate = originalInstantiate;
}

return async (buffer, msg, env) => {
return async (buffer, msg, env, opts) => {
const outputMemory = opts?.outputMemory ?? true

const originalRandom = Math.random
// const OriginalDate = Date
Expand Down Expand Up @@ -195,7 +202,9 @@ module.exports = async function (binary, options) {
/** end unmock */

return {
Memory: instance.HEAPU8.slice(),
Memory: outputMemory
? instance.HEAPU8.slice()
: null,
Error: response.Error,
Output: response.Output,
Messages: response.Messages,
Expand Down
40 changes: 40 additions & 0 deletions loader/test/handle.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { test } from 'node:test'
import * as assert from 'node:assert'
import fs from 'fs'

const MODULE_PATH = process.env.MODULE_PATH || '../src/index.cjs'

const env = {
Process: {
Id: 'AOS',
Owner: 'FOOBAR',
Tags: [
{ name: 'Name', value: 'Thomas' }
]
}
}
const msg = (cmd) => ({
Target: 'AOS',
Owner: 'FOOBAR',
'Block-Height': '1000',
Id: '1234xyxfoo',
Module: 'WOOPAWOOPA',
Tags: [
{ name: 'Action', value: 'Eval' }
],
Data: cmd
})

const { default: AoLoader } = await import(MODULE_PATH)
const wasm = fs.readFileSync('./test/aos/process.wasm')

test('do return memory based on outputMemory option', async () => {
const handle = await AoLoader(wasm, { format: 'wasm32-unknown-emscripten2' })
const testMsg = msg('X = 1')

const result1 = await handle(null, testMsg, env, { outputMemory: true })
assert.ok(result1.Memory)

const result2 = await handle(result1.Memory, testMsg, env, { outputMemory: false })
assert.equal(result2.Memory, null)
})