Cross-backend execution
Cross-backend execution
Section titled “Cross-backend execution”ws.run(filePath, params?) executes any script in the workspace using its own
backend and returns the result — all from inside another running script.
const ws = require('@javasculpt/workspace')const output = await ws.run('generator.plaintext.js', { seed: 42 })This is the foundation for multi-step pipelines where one backend generates data and another consumes it: a Plain Text script produces a JSON configuration, an SVG script reads it and draws a diagram, an XM script uses the same data to compose a melody.
How it works
Section titled “How it works”When your script calls ws.run(), a request is sent back to JavaSculpt’s main
thread. JavaSculpt spins up the target script’s backend worker, runs it, and
passes the result back to your script. Your script stays suspended until the
sub-execution finishes — the await handles this transparently.
The target script receives a fresh copy of the workspace files and the same
javasculpt_data constants that the calling script has.
The output object
Section titled “The output object”The return value is a BackendOutput object. Its shape depends on the target
backend:
| Target backend | Output fields |
|---|---|
| Plain Text | { kind: 'plaintext', text: string } |
| SVG | { kind: 'svg', svg: string } |
| Canvas 2D | { kind: 'canvas2d', png: ArrayBuffer, width: number, height: number } |
{ kind: 'pdf', data: ArrayBuffer } |
|
| WAV Audio | { kind: 'wav', data: ArrayBuffer, sampleRate: number, channels: number, duration: number } |
| XM Tracker | { kind: 'xm', data: ArrayBuffer, meta: object | null } |
| JSCAD 3D | { kind: 'jscad', vertices: Float32Array, colors: Float32Array, lines: Float32Array, lineColors: Float32Array } |
The most common pattern is calling a Plain Text script that returns a JSON string, then parsing it:
const ws = require('@javasculpt/workspace')
async function main() { const out = await ws.run('data.plaintext.js') const data = JSON.parse(out.text) // use data here...}Passing parameters
Section titled “Passing parameters”The second argument to ws.run() is an optional params object. These values
are merged with the target script’s own defaults, exactly as if a user had set
them in the Parameters panel.
const out = await ws.run('report.plaintext.js', { format: 'csv', limit: 100 })If you leave out a parameter its initial / default value from
getParameterDefinitions is used. You can read those defaults first with
ws.getParamDefs() (see the Workspace introspection tutorial).
Circular dependency detection
Section titled “Circular dependency detection”JavaSculpt tracks the call chain across all nested ws.run() calls. If a cycle
is detected — script A calls script B which calls script A again — the call is
rejected with a clear error message:
Circular ws.run() dependency: main.plaintext.js → helper.plaintext.js → main.plaintext.jsExample: Plain Text → SVG pipeline
Section titled “Example: Plain Text → SVG pipeline”In this example a Plain Text script generates tabular data and an SVG script renders it as a bar chart.
data.plaintext.js — generates rows:
function getParameterDefinitions() { return [ { name: 'count', type: 'number', widget: 'slider', caption: 'Bars:', default: 6, min: 2, max: 12, step: 1 }, ]}
function main({ count = 6 }) { const rows = [] for (let i = 0; i < count; i++) { rows.push({ label: `Item ${i + 1}`, value: Math.round(20 + Math.random() * 80) }) } return JSON.stringify(rows)}
module.exports = { main, getParameterDefinitions }chart.svg.js — reads the data and draws bars:
const ws = require('@javasculpt/workspace')
async function main() { const out = await ws.run('data.plaintext.js', { count: 8 }) const rows = JSON.parse(out.text)
const W = 400, H = 300, pad = 40 const max = Math.max(...rows.map(r => r.value)) const barW = (W - pad * 2) / rows.length - 4
const bars = rows.map((r, i) => { const bh = ((H - pad * 2) * r.value) / max const x = pad + i * ((W - pad * 2) / rows.length) const y = H - pad - bh return `<rect x="${x}" y="${y}" width="${barW}" height="${bh}" fill="#3498db"/> <text x="${x + barW / 2}" y="${H - pad + 14}" text-anchor="middle" font-size="10" fill="currentColor">${r.label}</text>` }).join('\n')
return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${W} ${H}">${bars}</svg>`}
module.exports = { main }Run chart.svg.js — it will automatically execute data.plaintext.js first,
receive the JSON, parse it, and render the chart.
Example: sharing parameters between files
Section titled “Example: sharing parameters between files”A common need is keeping one canonical set of parameters in a single file and having other scripts read and re-use them. Here a JSCAD script reads its own defaults from a Plain Text configuration file:
config.plaintext.js:
function main() { return JSON.stringify({ radius: 15, height: 40, segments: 32 })}
module.exports = { main }part.jscad.js:
const ws = require('@javasculpt/workspace')
async function main() { const { cylinder } = require('@jscad/modeling').primitives
const out = await ws.run('config.plaintext.js') const config = JSON.parse(out.text)
return cylinder({ radius: config.radius, height: config.height, segments: config.segments })}
module.exports = { main }- Always
awaitcalls tows.run()and mark yourmainasasync. - Prefer Plain Text scripts as data providers — they are the lightest backend and JSON output is trivially parsed by any other script.
- Call
ws.getParamDefs('target.js')beforews.run('target.js', params)if you need to build a params object from the target’s declared defaults. ws.run()counts against the outer script’s execution — if the sub-script is slow or produces a large output (e.g. a high-resolution Canvas 2D image), the caller will block for that duration.