Skip to content

Conversation

@jsantell
Copy link
Contributor

ct-engine is an entry point consisting of sandboxing, and eventual(?) query planning and scheduling. Initial focus is on web bindings. Some notes:

  • Simple case takes ~1ms for execution
  • This should be wrapped in a worker. We can expose an interface here that just postMessage's methods to the worker containing CTEngine. Doubly so after removing async handling!

Internals:

  • Handful of future cleanups like normalizing the marshaling of types from wasm-bindgen and providing different module/instance lifetimes (TBD on what we'll want)
  • Host callback now implemented via closures, with generics removed from Runtime.
  import init, { CTEngine } from './ct_engine.js';

  // Only needed once, call the default export function
  // to fetch and setup the web assembly artifact. 
  await init();

  // Host callback invoked by the VM via `globalThis.hostCallback()`
  const hostCallback = (input) => {
    switch (input.command) {
      case "sum":
        return Array.prototype.reduce.call(input.value, (acc, v) => {
          acc += v;
          return acc;
        }, 0);
    }
    return {};
  };

  // Instantiate an engine
  const engine = new CTEngine(hostCallback);

  // Define a module definition
  const definition = `
export const run = (input) => {
  input.foo = input.foo + 1;
  input.bar = globalThis.hostCallback({
    command: "sum",
    value: [1, 2, 3],
  });
  return input;
};
`;

  // Instantiate the module.
  let id = engine.define(definition);
  // Call the module function.
  var output = engine.run(id, { foo: 9 });
 
  console.log("Output:", output);
  console.assert(output.foo === 10);
  console.assert(output.bar === 6);

@jsantell jsantell merged commit c326f50 into main Oct 30, 2024
6 checks passed
@jsantell jsantell deleted the ct-engine branch October 30, 2024 18:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

2 participants