From 13de0520e8d4d98bb9d917187878bee0eb9ea847 Mon Sep 17 00:00:00 2001 From: James Addison Date: Thu, 7 Sep 2023 16:39:39 -0700 Subject: [PATCH] concept(js-interop): sample code for react hooks --- concept-code/js-interop/vs-hooks.js | 58 +++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 concept-code/js-interop/vs-hooks.js diff --git a/concept-code/js-interop/vs-hooks.js b/concept-code/js-interop/vs-hooks.js new file mode 100644 index 0000000..60461d7 --- /dev/null +++ b/concept-code/js-interop/vs-hooks.js @@ -0,0 +1,58 @@ +// A ValueScript sandbox similar to QuickJS Emscripten would be valuable. +// https://github.com/justjake/quickjs-emscripten + +import { useVS } from "value-script"; + +// an object containing any settings to customise valuescript +// e.g. max cycles, variables exposed to it, external functionality, etc. +const context = { + maxCycles: 1000, + memoryLimitBytes: 1024 * 1024, + // arbitrary nested document tree (with functions) which is persisted across calls. + data: { + maxBlocks: 10, + request: async (config) => { + await axios(config); + }, + sleep: async (ms) => { + await new Promise((resolve) => setTimeout(resolve, ms)); + }, + console: { + log: (...args) => { + console.log(...args); + }, + }, + }, +}; + +const Page = () => { + // calling setContext should allow you to update the shared context. + // isReady is used when getting WASM ready and the initial context. + const { vs, isReady, setContext } = useVS(context); + + const handleExecute = async () => { + try { + const disposableData = { + blocks: [], + input: {}, + // etc + }; + + // should be able to access any of the methods exposed in the two contexts. + const code = `const a = 50 + const b = 25 + + a * b / 2`; + + const result = await vs.execute(disposableData, code); + + // do what you want with the result. + } catch (error) { + // handle error + } + }; + + return ; +}; + +export default Page;