Skip to content

Commit

Permalink
chore: quickjs PoC
Browse files Browse the repository at this point in the history
  • Loading branch information
ppedziwiatr committed Nov 30, 2023
1 parent 4ee509c commit cb05ed7
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
"json-schema-to-typescript": "^11.0.1",
"node-stdlib-browser": "^1.2.0",
"prettier": "^2.3.2",
"quickjs-emscripten": "^0.23.0",
"rimraf": "^3.0.2",
"smartweave": "0.4.48",
"ts-jest": "^28.0.7",
Expand Down
79 changes: 79 additions & 0 deletions tools/quickjs.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { getQuickJS, newQuickJSAsyncWASMModule } from "quickjs-emscripten";

// the example smart contract code loaded from Arweave blockchain
const code = `
function handle(state, action) {
console.log('handle before timeout');
const timeoutResult = timeout(100); // no 'await' here, because fuck logic
console.log('handle after timeout:', timeoutResult);
const someShit = {};
for (i = 0; i < 100000; i++) {
someShit[""+i] = i*i;
}
return 1;
}
`.trim();

async function main() {

// 1. creating the QJS context
const QuickJS = await newQuickJSAsyncWASMModule();
const runtime = QuickJS.newRuntime();
// "Should be enough for everyone" -- attributed to B. Gates
// runtime.setMemoryLimit(1024 * 640);
// Limit stack size
runtime.setMaxStackSize(1024 * 320);
let interruptCycles = 0
runtime.setInterruptHandler((runtime) => { interruptCycles++ });

const vm = runtime.newContext();

// 2. registering APIs
const logHandle = vm.newFunction("log", (...args) => {
const nativeArgs = args.map(vm.dump)
console.log("QuickJS:", ...nativeArgs)
});

const consoleHandle = vm.newObject();
vm.setProp(consoleHandle, "log", logHandle);
vm.setProp(vm.global, "console", consoleHandle);
consoleHandle.dispose();
logHandle.dispose();

const timeoutHandle = vm.newAsyncifiedFunction("timeout", async (msHandle) => {
const ms = vm.getNumber(msHandle);
console.log("omg, that's an async shit!");
await timeout(1000);
return vm.newString("check your head!");
})
timeoutHandle.consume((fn) => vm.setProp(vm.global, "timeout", fn))

// 4. calling the "handle" function
const result = await vm.evalCodeAsync(`(() => {
${code}
return handle();
})()`);

if (result.error) {
console.log('Execution failed:', vm.dump(result.error));
result.error.dispose();
} else {
const parsedResult = vm.unwrapResult(result).consume(vm.getNumber);
console.log("result", parsedResult);
console.log("Cycles", interruptCycles);
}

vm.dispose();
runtime.dispose();
}

main().finally();

function timeout(delay) {
return new Promise(function (resolve) {
setTimeout(resolve, delay);
});
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6623,6 +6623,11 @@ quick-lru@^5.1.1:
resolved "https://registry.yarnpkg.com/quick-lru/-/quick-lru-5.1.1.tgz#366493e6b3e42a3a6885e2e99d18f80fb7a8c932"
integrity sha512-WuyALRjWPDGtt/wzJiadO5AXY+8hZ80hVpe6MyivgraREW751X3SbhRvG3eLKOYN+8VEvqLcf3wdnt44Z4S4SA==

quickjs-emscripten@^0.23.0:
version "0.23.0"
resolved "https://registry.yarnpkg.com/quickjs-emscripten/-/quickjs-emscripten-0.23.0.tgz#94f412d0ee5f3021fc12ddf6c0b00bd8ce0d28b9"
integrity sha512-CIP+NDRYDDqbT3cTiN8Bon1wsZ7IgISVYCJHYsPc86oxszpepVMPXFfttyQgn1u1okg1HPnCnM7Xv1LrCO/VmQ==

randombytes@^2.0.0, randombytes@^2.0.1, randombytes@^2.0.5, randombytes@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/randombytes/-/randombytes-2.1.0.tgz#df6f84372f0270dc65cdf6291349ab7a473d4f2a"
Expand Down

0 comments on commit cb05ed7

Please sign in to comment.