Skip to content

feat(driver): add getBuildCode() to extract current build as shareable code#150

Open
SFerenczy wants to merge 1 commit intoatty303:mainfrom
SFerenczy:feat/get-build-code
Open

feat(driver): add getBuildCode() to extract current build as shareable code#150
SFerenczy wants to merge 1 commit intoatty303:mainfrom
SFerenczy:feat/get-build-code

Conversation

@SFerenczy
Copy link
Copy Markdown

Summary

Adds getBuildCode() as the symmetric counterpart to the existing loadBuildFromCode(). Serializes the current build back to the standard PoB share-code format — base64url(zlib(xml)) — so embedders can round-trip: load a code → user/agent edits → read the code back out.

The shape mirrors loadBuildFromCode exactly; changes are threaded through all four layers:

Layer Addition
packages/driver/boot.lua getBuildCode() Lua function — calls build:SaveDB("code"), deflates, URL-safe base64
packages/driver/src/c/driver.c get_build_code() exported C function with static buffer lifecycle matching the existing s_* pattern
packages/driver/src/js/worker.ts getBuildCode() on DriverWorker + cwrap binding
packages/driver/src/js/driver.ts getBuildCode(): Promise<string> public API on Driver

Motivation

I'm embedding pob-web in a build-analysis app where an AI agent mutates the build XML. After mutation, I need to serialize the current in-memory build back to a code to persist/share it. Today loadBuildFromCode gets you in to pob-web; there's no symmetric way to get out. This closes that gap with a minimally-invasive addition.

Design notes

  • No new dependencies — reuses common.base64 and Deflate already in PoB, same as loadBuildFromCode.
  • Error surface matches existing code — Lua-side error() on missing mainObject.main / wrong mode, C-side prints to stderr and returns NULL on pcall failure, matches the style of load_build_from_code in the same file.
  • Static buffer s_build_code follows the existing pattern in driver.c; freed on next call.
  • URL-safe base64 (+-, /_) matches what loadBuildFromCode accepts, so the output is a drop-in input for the inverse call.

Test plan

  • Manual round-trip: await driver.loadBuildFromCode(code); const out = await driver.getBuildCode(); and confirm the resulting XML (after base64/inflate) equals the input XML for at least one non-trivial build
  • Error path: call getBuildCode() before a build is loaded — expect a rejected promise with a clear error
  • Verify dist/driver.wasm size doesn't meaningfully regress

Happy to adjust naming, error handling, or scope if you'd prefer a different shape.

…e code

Symmetric counterpart to loadBuildFromCode. Serializes the current build
to XML via PoB's SaveDB, then deflates and base64-encodes it into the
standard URL-safe build code format.

Added across all layers:
- boot.lua: getBuildCode() Lua function
- driver.c: get_build_code() exported C function with safe string handling
- worker.ts: getBuildCode() on DriverWorker with cwrap binding
- driver.ts: getBuildCode(): Promise<string> public API on Driver

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant