Skip to content

Commit

Permalink
chore(loader): update dependencies and add wasm-metering #1016
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterFarber committed Sep 26, 2024
1 parent 411661f commit 76f05f4
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 10 deletions.
86 changes: 85 additions & 1 deletion loader/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion loader/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
"test": "npm run build && node --experimental-wasm-memory64 --test",
"test:integration": "npm run build && MODULE_PATH='../dist/index.cjs' node --experimental-wasm-memory64 --test"
},
"dependencies": {},
"dependencies": {
"@permaweb/wasm-metering": "^0.2.2"
},
"devDependencies": {
"esbuild": "^0.19.5",
"typescript": "^5.2.2"
Expand Down
2 changes: 1 addition & 1 deletion loader/src/formats/wasm64-emscripten.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var Module = (() => {
limit: Module.computeLimit || DEFAULT_GAS_LIMIT,
used: 0,
use: (amount) => {
Module.gas.used += amount;
Module.gas.used += Number(amount);
},
refill: (amount) => {
if (!amount) Module.gas.used = 0;
Expand Down
15 changes: 14 additions & 1 deletion loader/src/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const Emscripten2 = require('./formats/emscripten2.cjs')
const Emscripten3 = require('./formats/emscripten3.cjs')
const Emscripten4 = require('./formats/emscripten4.cjs')
const Wasm64 = require('./formats/wasm64-emscripten.cjs')
const metering = require('@permaweb/wasm-metering')

/* eslint-enable */

Expand Down Expand Up @@ -93,6 +94,10 @@ const Wasm64 = require('./formats/wasm64-emscripten.cjs')
module.exports = async function (binary, options) {
let instance = null
let doHandle = null

let meterType = options.format.startsWith("wasm32") ? "i32" : "i64";
const originalInstantiate = WebAssembly.instantiate;

if (options === null) {
options = { format: 'wasm32-unknown-emscripten' }
}
Expand All @@ -105,15 +110,20 @@ module.exports = async function (binary, options) {
} else {

if (typeof binary === "function") {
WebAssembly.instantiate = async function (wasm, info) {
const meteredWasm = metering.meterWASM(wasm, { meterType });
return originalInstantiate(meteredWasm, info);
};
options.instantiateWasm = binary
} else {
binary = metering.meterWASM(binary, { meterType })
options.wasmBinary = binary
}

if (options.format === "wasm64-unknown-emscripten-draft_2024_02_15") {
instance = await Wasm64(options)
} else if (options.format === "wasm32-unknown-emscripten4") {
instance = await Emscripten4(binary, options)
instance = await Emscripten4(options)
}

await instance['FS_createPath']('/', 'data')
Expand Down Expand Up @@ -141,6 +151,9 @@ module.exports = async function (binary, options) {
doHandle = instance.cwrap('handle', 'string', ['string', 'string'])
}

if (typeof binary === "function") {
WebAssembly.instantiate = originalInstantiate;
}

return async (buffer, msg, env) => {
const originalRandom = Math.random
Expand Down
12 changes: 6 additions & 6 deletions loader/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ describe('loader', async () => {
* but they should be within ~75k of each other, effectively meaning the gas is not
* "stacking" and being refilled every time
*/
assert.ok(Math.abs(result.GasUsed - result2.GasUsed) < 600000)
assert.ok(Math.abs(result.GasUsed - result2.GasUsed) < 750000)
})

it('should run out of gas', async () => {
Expand Down Expand Up @@ -209,18 +209,18 @@ describe('loader', async () => {

it('should handle Assignments', async () => {
const handle = await AoLoader(wasmBinary, { format: 'wasm32-unknown-emscripten' })

// eslint-disable-next-line
const result = await handle(null,
{ Owner: 'tom', Target: '1', Tags: [{ name: 'Action', value: 'Assignment' }], Data: '' },
{ Process: { Id: '1', Tags: [] } }
)

assert.deepStrictEqual(
result.Output,
[
{ Processes: [ 'pid-1', 'pid-2' ], Message: 'mid-1' },
{ Processes: [ 'pid-1', 'pid-2' ], Message: 'mid-2' }
result.Output,
[
{ Processes: ['pid-1', 'pid-2'], Message: 'mid-1' },
{ Processes: ['pid-1', 'pid-2'], Message: 'mid-2' }
]
)

Expand Down

0 comments on commit 76f05f4

Please sign in to comment.