@vim3d/core 0.2.0 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/esm/compile.js CHANGED
@@ -1,7 +1,11 @@
1
1
  import { expand } from '@vim3d/parser';
2
- import { loadTable, validSegment } from './channels.js';
2
+ import { boundaryMode, cyclic, loadTable, validSegment } from './channels.js';
3
+ import { channelOperators } from './ch-operators.js';
4
+ import { Extensions, jsonProp } from './contexts.js';
3
5
  import { bindExpression, parseExpression } from './expression.js';
4
6
  import { comparePaths, GraphBuilder } from './graph.js';
7
+ import { allowedSource, createContextRegistry } from './registry.js';
8
+ import { rampStops } from './ramp.js';
5
9
  import { channelPath, checkArity, copyState, defaults, editCommands, objectPath, plans, positionStep, propertyEdit, readFrame, readNumber, rotations, scales, translations, worldStep } from './model.js';
6
10
  import { EvaluationError, fail, freeze } from './types.js';
7
11
  const aliases = {
@@ -10,8 +14,11 @@ const aliases = {
10
14
  };
11
15
  const primitives = new Set(['cube', 'grid', 'sphere', 'ellipse', 'ellipseFace', 'circle', 'circleFace', 'ring']);
12
16
  const prefixes = { geo: 'geo', curve: 'curve', null: 'null', camera: 'cam', light: 'light', channel: 'ch' };
13
- const deferred = new Set(['parent', 'expr', 'register', 'ref', 'path', 'lookat', 'children']);
17
+ const deferred = new Set(['parent', 'expr', 'register', 'ref', 'path', 'lookat', 'children', 'connect', 'set', 'range']);
14
18
  const isChannel = (node) => 'ports' in node;
19
+ const isObject = (node) => 'authored' in node;
20
+ const isExtension = (node) => 'definition' in node;
21
+ const isBuiltInPath = (path) => /^\/(?:obj|ch)\//.test(path);
15
22
  const commandOf = (text) => Object.hasOwn(aliases, text) ? aliases[text] : text;
16
23
  const location = ({ file, line }) => ({ file, line });
17
24
  const prop = (name, value) => ({ name, type: typeof value, value });
@@ -71,7 +78,8 @@ export function compileScene(source, options = {}) {
71
78
  return data instanceof Uint8Array ? decoder.decode(data) : data ?? null;
72
79
  },
73
80
  expandDirectives: true,
74
- reservedCommands: ['expr', 'parent'],
81
+ reservedCommands: ['expr', 'parent', 'range'],
82
+ preserveText: true,
75
83
  });
76
84
  if (expanded.problems.length)
77
85
  return { diagnostics: expanded.problems.map(problem => ({
@@ -86,6 +94,8 @@ export function compileScene(source, options = {}) {
86
94
  }
87
95
  }
88
96
  function compile(lines, root, fps, options) {
97
+ const registry = createContextRegistry(options.contexts);
98
+ const extensions = new Extensions(registry);
89
99
  const objects = [];
90
100
  const channels = [];
91
101
  const objNames = new Map();
@@ -96,6 +106,26 @@ function compile(lines, root, fps, options) {
96
106
  const pending = [];
97
107
  const timed = [];
98
108
  let nextId = 0;
109
+ for (const [name, portNames] of Object.entries(options.liveChannels ?? {})) {
110
+ if (!validSegment(name))
111
+ fail('invalid-live-channel', 'Invalid live channel name: ' + name, root);
112
+ if (!Array.isArray(portNames) || !portNames.length) {
113
+ fail('invalid-live-channel', 'Live channel must declare at least one port: ' + name, root);
114
+ }
115
+ const ports = new Map();
116
+ for (const portName of portNames) {
117
+ if (typeof portName !== 'string' || !validSegment(portName)) {
118
+ fail('invalid-live-channel', 'Invalid live channel port name: ' + String(portName), root);
119
+ }
120
+ if (ports.has(portName)) {
121
+ fail('invalid-live-channel', 'Duplicate live channel port: /ch/' + name + '/' + portName, root);
122
+ }
123
+ ports.set(portName, { live: true, source: root });
124
+ }
125
+ const channel = { id: nextId++, name, source: root, live: true, ports };
126
+ channels.push(channel);
127
+ chNames.set(name, channel);
128
+ }
99
129
  function nextName(type, names) {
100
130
  let n = counters.get(type) ?? 0;
101
131
  let name;
@@ -123,12 +153,14 @@ function compile(lines, root, fps, options) {
123
153
  return node;
124
154
  }
125
155
  const getObject = (name, source) => lookup(name, 'obj', source);
126
- const channelOutput = (path, source) => {
156
+ const channelOutput = (path, source, ramp = false) => {
127
157
  const parts = path.split('/');
128
158
  if (parts[0] || parts[1] !== 'ch' || (parts.length !== 3 && parts.length !== 4)) {
129
159
  fail('invalid-path', 'Expected a channel output path: ' + path, source);
130
160
  }
131
161
  const node = lookup(parts[2], 'ch', source);
162
+ if (ramp && !node.ramp)
163
+ fail('invalid-ramp', 'chramp requires a ch:ramp node: ' + path, source);
132
164
  if (parts.length === 3 && (node.ports.size !== 1 || !node.ports.has('value'))) {
133
165
  fail('ambiguous-channel', 'Channel shorthand requires a sole value port: ' + path, source);
134
166
  }
@@ -137,6 +169,14 @@ function compile(lines, root, fps, options) {
137
169
  fail('missing-target', 'Unknown channel port: ' + path, source);
138
170
  return channelPath(node) + '/' + name;
139
171
  };
172
+ const channelInput = (path, source) => {
173
+ const explicit = /^\/ch\/([^/]+)\/input\/value$/.exec(path);
174
+ if (!explicit)
175
+ return channelOutput(path, source);
176
+ if (!chNames.get(explicit[1])?.operator)
177
+ fail('missing-target', 'Unknown operator input: ' + path, source);
178
+ return channelOutput('/ch/' + explicit[1] + '/value', source);
179
+ };
140
180
  const scalar = (node, param, source) => {
141
181
  if (!Object.hasOwn(node.authored.parameters, param)) {
142
182
  fail('unsupported-parameter', 'Unsupported parameter: ' + objectPath(node) + '/' + param, source);
@@ -149,6 +189,30 @@ function compile(lines, root, fps, options) {
149
189
  fail('invalid-path', 'Expected an object parameter path: ' + path, source);
150
190
  return scalar(getObject(name, source), param, source);
151
191
  };
192
+ const extensionAddress = (path, source) => {
193
+ if (!path?.startsWith('/'))
194
+ fail('invalid-path', 'Expected an absolute path: ' + path, source);
195
+ return normalizePath(path, '/', source);
196
+ };
197
+ const extensionSource = (address, source) => {
198
+ if (address.startsWith('/ch/')) {
199
+ const path = channelOutput(address, source);
200
+ return { address: path, path, type: 'number', port: 'value', stage: 'ch' };
201
+ }
202
+ if (address.startsWith('/obj/')) {
203
+ const [, , name, member, ...extra] = address.split('/');
204
+ if (!member || extra.length)
205
+ fail('invalid-path', 'Expected an object output path: ' + address, source);
206
+ const object = getObject(name, source);
207
+ if (member === 'worldMatrix' || member === 'localMatrix' || member === 'snapshot') {
208
+ return { address, path: worldStep(object), port: member === 'worldMatrix' ? 'value' : member,
209
+ type: member === 'snapshot' ? 'object' : 'matrix4', stage: 'obj' };
210
+ }
211
+ const path = scalar(object, member, source);
212
+ return { address: path, path, type: 'number', port: 'value', stage: 'obj' };
213
+ }
214
+ return extensions.output(address, source);
215
+ };
152
216
  let current = addObject('camera', root, 'Cam0');
153
217
  let viewCamera;
154
218
  let backgroundColor;
@@ -168,6 +232,28 @@ function compile(lines, root, fps, options) {
168
232
  else if (curve && command !== 'end') {
169
233
  fail('invalid-curve', 'Expected a curve vertex or end', source);
170
234
  }
235
+ else if (command === 'node') {
236
+ checkArity(command, args, [2], source);
237
+ const path = extensionAddress(args[0], source);
238
+ const definition = path.startsWith('/ch/') ? channelOperators.find(operator => operator.type === args[1]) : undefined;
239
+ if (path.startsWith('/ch/') && (args[1] === 'ch:ramp' || definition)) {
240
+ const [, , name, ...extra] = path.split('/');
241
+ if (!validSegment(name) || extra.length)
242
+ fail('invalid-path', 'Expected a channel node path: ' + path, source);
243
+ if (chNames.has(name))
244
+ fail('duplicate-name', 'Name is already used: ' + path, source);
245
+ const channel = { id: nextId++, name, source, ports: new Map([['value', { source }]]) };
246
+ if (definition)
247
+ channel.operator = { definition, props: Object.assign(Object.create(null), Object.fromEntries(definition.props.flatMap(p => p.default === undefined ? [] : [[p.name, p.default]]))) };
248
+ else
249
+ channel.ramp = [];
250
+ chNames.set(name, channel);
251
+ channels.push(channel);
252
+ current = channel;
253
+ }
254
+ else
255
+ current = extensions.add(path, args[1], source);
256
+ }
171
257
  else if (command === 'begin') {
172
258
  checkArity(command, args, [0, 1], source);
173
259
  if (args.length && args[0] !== 'draw')
@@ -211,13 +297,16 @@ function compile(lines, root, fps, options) {
211
297
  }
212
298
  else if (command === 'select') {
213
299
  checkArity(command, args, [1], source);
214
- current = lookup(args[0], 'either', source);
300
+ const address = args[0].startsWith('/') && !isBuiltInPath(args[0]) ? extensionAddress(args[0], source) : args[0];
301
+ current = extensions.nodes.get(address) ?? lookup(args[0], 'either', source);
215
302
  }
216
303
  else if (command === 'name') {
217
304
  checkArity(command, args, [1], source);
218
305
  const [name] = args;
219
306
  if (!validSegment(name))
220
307
  fail('invalid-name', 'Invalid name: ' + name, source);
308
+ if (isExtension(current))
309
+ fail('invalid-subject', 'name requires an object or channel', source);
221
310
  if (isChannel(current))
222
311
  rename(chNames, current, name, source);
223
312
  else
@@ -262,10 +351,10 @@ function compile(lines, root, fps, options) {
262
351
  timed.push({ frame, source, command: eventCommand, args: values, objectCount: nextId });
263
352
  }
264
353
  else if (deferred.has(command)) {
265
- pending.push({ command, args, source, current });
354
+ pending.push({ command, args, source, current, text: line.text });
266
355
  }
267
356
  else if (editCommands.has(command)) {
268
- if (isChannel(current))
357
+ if (!isObject(current))
269
358
  fail('invalid-subject', command + ' requires an object', source);
270
359
  propertyEdit(command, args, current, source)(current.authored);
271
360
  }
@@ -282,12 +371,15 @@ function compile(lines, root, fps, options) {
282
371
  child.parent = args[1] === '/obj' ? undefined : getObject(args[1], source);
283
372
  }
284
373
  const graph = new GraphBuilder();
374
+ const ranges = Object.create(null);
285
375
  const expressionPorts = new Map();
286
376
  for (const channel of channels) {
287
377
  for (const [name, port] of channel.ports)
288
378
  expressionPorts.set(channelPath(channel) + '/' + name, { node: channel, name, port });
289
379
  }
290
380
  const inputs = new Map();
381
+ const offTimeRefs = [];
382
+ const contextWires = [];
291
383
  const depend = (path, on) => {
292
384
  if (!inputs.has(path))
293
385
  inputs.set(path, new Set());
@@ -295,37 +387,180 @@ function compile(lines, root, fps, options) {
295
387
  };
296
388
  const bind = (text, path, source) => bindExpression(parseExpression(text, source), (ref, name) => {
297
389
  const address = normalizePath(ref, path.slice(0, path.lastIndexOf('/')), source);
390
+ if (name === 'chramp' && !address.startsWith('/ch/'))
391
+ fail('invalid-ramp', 'chramp requires a ch:ramp node: ' + address, source);
392
+ const targetStage = path.startsWith('/ch/') ? 'ch' : 'obj';
393
+ if (!isBuiltInPath(address) && registry.get(address.split('/')[1])) {
394
+ const from = extensionSource(address, source);
395
+ if (!allowedSource(targetStage, from.stage)) {
396
+ fail('extension-stage', 'Built-in nodes cannot read ' + address, source);
397
+ }
398
+ if (from.stage !== 'audio' || name !== 'ch') {
399
+ fail('off-time-extension-reference', 'Only ch() can read built-in audio scalars: ' + address, source);
400
+ }
401
+ if (from.type !== 'number')
402
+ fail('extension-port-type', 'Expected a number at ' + from.address, source);
403
+ if (!contextWires.some(wire => wire.to === path && wire.from.address === from.address)) {
404
+ contextWires.push({ to: path, input: from.address, from });
405
+ }
406
+ return from.address;
407
+ }
298
408
  if (address.startsWith('/obj/')) {
299
409
  if (path.startsWith('/ch/'))
300
410
  fail('channel-object-reference', 'Channel expressions cannot read object parameters: ' + address, source);
301
411
  if (name !== 'ch')
302
412
  fail('off-time-object-reference', name + ' requires a channel: ' + address, source);
303
413
  }
304
- const input = address.startsWith('/ch/') ? channelOutput(address, source) : objectScalar(address, source);
414
+ const input = address.startsWith('/ch/') ? channelOutput(address, source, name === 'chramp') : objectScalar(address, source);
415
+ if (name === 'chf' || name === 'cht')
416
+ offTimeRefs.push({ path: input, source });
305
417
  depend(path, input);
306
418
  return input;
307
419
  });
308
- for (const { command, args, source, current } of pending) {
420
+ const driveScalar = (path, expression, source) => {
421
+ if (path.startsWith('/ch/')) {
422
+ const { node, port } = expressionPorts.get(path);
423
+ if (node.ramp || port.samples || port.expression || port.live)
424
+ fail('overlapping-drivers', 'Channel already has a driver: ' + path, source);
425
+ port.expression = expression;
426
+ port.source = source;
427
+ }
428
+ else
429
+ addDriver(path, { kind: 'expr', expression, ...range([], 0, source) });
430
+ };
431
+ for (const { command, args, source, current, text: lineText } of pending) {
309
432
  if (command === 'parent')
310
433
  continue;
311
434
  const numberAt = (i, fallback) => args[i] === undefined ? fallback : readNumber(args[i], source);
312
- if (command === 'expr') {
435
+ if (command === 'range') {
436
+ checkArity(command, args, [3, 4], source);
437
+ const address = extensionAddress(args[0], source);
438
+ let path;
439
+ if (address.startsWith('/ch/')) {
440
+ if (address.endsWith('/input/value')) {
441
+ channelInput(address, source);
442
+ path = address;
443
+ }
444
+ else
445
+ path = channelOutput(address, source);
446
+ }
447
+ else if (address.startsWith('/obj/'))
448
+ path = objectScalar(address, source);
449
+ else
450
+ path = extensions.numericPort(address, source);
451
+ const min = readNumber(args[1], source), max = readNumber(args[2], source);
452
+ const mode = args[3] ?? 'clamp';
453
+ if (min > max || (mode !== 'clamp' && mode !== 'error')) {
454
+ fail('invalid-range', 'Expected MIN <= MAX and mode clamp or error', source);
455
+ }
456
+ if (ranges[path])
457
+ fail('overlapping-ranges', 'Range already declared for ' + path, source);
458
+ ranges[path] = { min, max, mode, source };
459
+ }
460
+ else if (command === 'set') {
461
+ const [target] = args;
462
+ if (!target)
463
+ fail('invalid-operands', 'Expected set KEY VALUE', source);
464
+ if (!target.startsWith('/') && !isExtension(current) && !isChannel(current)) {
465
+ fail('invalid-subject', 'set requires a context node', source);
466
+ }
467
+ const address = target.startsWith('/') ? extensionAddress(target, source)
468
+ : (isChannel(current) ? channelPath(current) : current.path) + '/' + target;
469
+ if (address.startsWith('/ch/')) {
470
+ const [, , name, key, ...extra] = address.split('/');
471
+ const node = chNames.get(name);
472
+ if (!node || extra.length)
473
+ fail('unknown-extension-prop', 'Unknown prop: ' + address, source);
474
+ if (key === 'extend' || key === 'boundary') {
475
+ checkArity(command, args, [2], source);
476
+ const mode = args[1];
477
+ if (node.live || !boundaryMode(mode) || (!node.table && cyclic(mode) && options.frameCount === undefined)) {
478
+ fail('invalid-channel-extend', 'Invalid extend mode for ' + address, source);
479
+ }
480
+ if (node.table)
481
+ node.table.boundary = mode;
482
+ else
483
+ node.extend = mode;
484
+ }
485
+ else if (node.ramp && key === 'stops') {
486
+ node.ramp = rampStops(jsonProp(lineText, () => fail('invalid-ramp', 'Expected JSON ramp stops', source)), source);
487
+ }
488
+ else {
489
+ const spec = node.operator?.definition.props.find(p => p.name === key);
490
+ if (!spec)
491
+ fail('unknown-extension-prop', 'Unknown prop: ' + address, source);
492
+ checkArity(command, args, [2], source);
493
+ node.operator.props[key] = spec.type === 'number' ? readNumber(args[1], source) : args[1];
494
+ }
495
+ }
496
+ else
497
+ extensions.property(address, args.slice(1), lineText, source);
498
+ }
499
+ else if (command === 'connect') {
500
+ checkArity(command, args, [2], source);
501
+ const to = extensionAddress(args[1], source);
502
+ const from = extensionSource(extensionAddress(args[0], source), source);
503
+ if (isBuiltInPath(to)) {
504
+ if (from.stage === 'audio' || from.stage === 'ch') {
505
+ if (from.type !== 'number')
506
+ fail('extension-port-type', 'Expected a numeric output at ' + from.address, source);
507
+ const path = to.startsWith('/ch/') ? channelInput(to, source) : objectScalar(to, source);
508
+ const operator = expressionPorts.get(path)?.node.operator;
509
+ if (from.stage === 'ch' && to.startsWith('/ch/') && !operator) {
510
+ fail('extension-stage', 'Expected a channel operator input: ' + to, source);
511
+ }
512
+ driveScalar(path, { kind: 'reference', name: 'ch', path: from.address }, source);
513
+ if (from.stage === 'ch')
514
+ depend(path, from.path);
515
+ contextWires.push({ to: path, input: operator ? 'value' : from.address, from });
516
+ continue;
517
+ }
518
+ const [, context, name, input, extra] = to.split('/');
519
+ if (context !== 'obj' || input !== 'geometry' || extra !== undefined || from.stage !== 'geo') {
520
+ fail('extension-stage', 'connect cannot drive this built-in input: ' + to, source);
521
+ }
522
+ const object = getObject(name, source);
523
+ if (object.kind !== 'geo')
524
+ fail('invalid-subject', 'A geometry connection requires a geometry object', source);
525
+ if (from.type !== 'object')
526
+ fail('extension-port-type', 'Expected object geometry at ' + to, source);
527
+ if (object.geometry || object.geometrySource)
528
+ fail('overlapping-drivers', 'Geometry already assigned: ' + to, source);
529
+ object.geometrySource = from.address;
530
+ contextWires.push({ to: worldStep(object), input, from });
531
+ }
532
+ else
533
+ extensions.input(to, from.type, { kind: 'wire', from }, source);
534
+ }
535
+ else if (command === 'expr') {
313
536
  checkArity(command, args, [2], source);
314
537
  const [target, text] = args;
315
538
  if (!target.startsWith('/'))
316
539
  fail('invalid-path', 'Expression targets must be absolute', source);
317
540
  const address = normalizePath(target, '/', source);
318
- const path = address.startsWith('/ch/') ? channelOutput(address, source) : objectScalar(address, source);
319
- const expression = bind(text, path, source);
320
- if (path.startsWith('/ch/')) {
321
- const { port } = expressionPorts.get(path);
322
- if (port.samples || port.expression)
323
- fail('overlapping-drivers', 'Channel already has a driver: ' + path, source);
324
- port.expression = expression;
325
- port.source = source;
541
+ if (!isBuiltInPath(address) && (registry.get(address.split('/')[1]) || options.contexts?.length)) {
542
+ const references = new Map();
543
+ const owner = address.slice(0, address.lastIndexOf('/'));
544
+ const expression = bindExpression(parseExpression(text, source), (ref, name) => {
545
+ const address = normalizePath(ref, owner, source);
546
+ if (name === 'chramp')
547
+ channelOutput(address, source, true);
548
+ const from = extensionSource(address, source);
549
+ if (name !== 'ch' && !from.address.startsWith('/ch/')) {
550
+ fail('off-time-extension-reference', name + ' requires a channel: ' + from.address, source);
551
+ }
552
+ if (from.type !== 'number')
553
+ fail('extension-port-type', 'Expected a number at ' + from.address, source);
554
+ if (name === 'chf' || name === 'cht')
555
+ offTimeRefs.push({ path: from.address, source });
556
+ references.set(from.address, from);
557
+ return from.address;
558
+ });
559
+ extensions.input(address, 'number', { kind: 'expr', expression, references: [...references.values()], source }, source);
560
+ continue;
326
561
  }
327
- else
328
- addDriver(path, { kind: 'expr', expression, ...range([], 0, source) });
562
+ const path = address.startsWith('/ch/') ? channelInput(address, source) : objectScalar(address, source);
563
+ driveScalar(path, bind(text, path, source), source);
329
564
  }
330
565
  else if (command === 'register' || command === 'ref') {
331
566
  const register = command === 'register';
@@ -341,6 +576,9 @@ function compile(lines, root, fps, options) {
341
576
  const common = { input, scale: numberAt(end + 1, 1), offset: numberAt(end + 2, 0), ...range(args, end + 4, source) };
342
577
  const freq = numberAt(end, 1);
343
578
  const shift = numberAt(end + 3, 0);
579
+ if (register && (freq !== 1 || shift !== 0 || args[1] === 'rx' || args[1] === 'ry' || args[1] === 'rz')) {
580
+ offTimeRefs.push({ path: input, source });
581
+ }
344
582
  addDriver(path, register ? { kind: 'register', freq, shift, ...common } : { kind: 'ref', ...common });
345
583
  depend(path, input);
346
584
  }
@@ -358,7 +596,7 @@ function compile(lines, root, fps, options) {
358
596
  else {
359
597
  checkArity(command, args, [2, 4], source);
360
598
  const target = getObject(args[1], source);
361
- if (command === 'children' && (args[0] !== 'lookat' || isChannel(current))) {
599
+ if (command === 'children' && (args[0] !== 'lookat' || !isObject(current))) {
362
600
  fail('invalid-subject', 'Expected children lookat TARGET with an object current', source);
363
601
  }
364
602
  const activeRange = range(args, 2, source);
@@ -374,8 +612,11 @@ function compile(lines, root, fps, options) {
374
612
  }
375
613
  function validateState(object, state, source) {
376
614
  if (state.matrix && (object.aimBindings.length || object.pathBindings.length ||
377
- [...translations, ...rotations, ...scales].some(p => drivers.has(objectPath(object) + '/' + p)))) {
378
- fail('invalid-trs', 'Drivers and constraints require a non-sheared, nonsingular TRS matrix', source);
615
+ [...rotations, ...scales].some(p => {
616
+ const target = objectPath(object) + '/' + p;
617
+ return drivers.has(target) || ranges[target];
618
+ }))) {
619
+ fail('invalid-trs', 'Rotation and scale drivers and ranges, and constraints, require a non-sheared, nonsingular TRS matrix', source);
379
620
  }
380
621
  }
381
622
  for (const object of objects)
@@ -392,7 +633,7 @@ function compile(lines, root, fps, options) {
392
633
  const command = commandOf(event.command);
393
634
  if (!editCommands.has(command))
394
635
  fail('forbidden-event-command', 'at cannot execute ' + event.command, source);
395
- if (isChannel(selected) || selected.id >= objectCount)
636
+ if (!isObject(selected) || selected.id >= objectCount)
396
637
  fail('event-missing-target', 'No current object is available at the event source', source);
397
638
  const edit = propertyEdit(command, args, selected, source);
398
639
  edit(state.get(selected));
@@ -421,10 +662,28 @@ function compile(lines, root, fps, options) {
421
662
  }
422
663
  }
423
664
  for (const [path, { node, name, port }] of expressionPorts) {
424
- if (!port.samples && !port.expression)
665
+ const extend = node.extend ? [prop('extend', node.extend)] : [];
666
+ if (node.ramp) {
667
+ if (!node.ramp.length)
668
+ fail('invalid-ramp', 'Missing ramp stops for ' + channelPath(node), port.source);
669
+ graph.add(path, 'ch:ramp', 'number', [prop('target', path), prop('stops', node.ramp), ...extend], port.source);
670
+ continue;
671
+ }
672
+ if (!port.samples && !port.expression && !port.live)
425
673
  fail('missing-channel-driver', 'Missing driver for ' + path, port.source);
426
- graph.add(path, node.table ? 'source:table' : 'source:expression', 'number', [prop('target', path), prop('port', name), ...(node.table ? [prop('asset', node.table.asset),
427
- prop('sampling', node.table)] : [prop('ast', port.expression)])], port.source);
674
+ if (node.operator) {
675
+ const { definition, props } = node.operator;
676
+ definition.validate(props, message => fail('invalid-channel-operator', channelPath(node) + ': ' + message, node.source));
677
+ graph.addNode(path, { context: 'ch', type: definition.type, inputs: definition.inputs, outputs: definition.outputs,
678
+ props: [prop('target', path), prop('expr/value', port.expression), ...Object.entries(props).map(([key, value]) => prop(key, value)), ...extend],
679
+ }, node.source);
680
+ continue;
681
+ }
682
+ const nodeType = node.table ? 'source:table' : port.live ? 'source:live' : 'source:expression';
683
+ const nodeProps = node.table
684
+ ? [prop('asset', node.table.asset), prop('sampling', node.table)]
685
+ : port.live ? [] : [prop('ast', port.expression)];
686
+ graph.add(path, nodeType, 'number', [prop('target', path), prop('port', name), ...nodeProps, ...extend], port.source);
428
687
  }
429
688
  for (const object of objects) {
430
689
  const path = objectPath(object);
@@ -460,9 +719,44 @@ function compile(lines, root, fps, options) {
460
719
  for (const [path, dependencies] of inputs)
461
720
  for (const input of dependencies)
462
721
  graph.depend(path, input);
722
+ extensions.addToGraph(graph);
723
+ for (const { to, input, from } of contextWires)
724
+ graph.wire(to, input, from.path, from.port);
463
725
  const finished = graph.finish();
464
- const scene = freeze({ fps, ...(options.frameCount !== undefined && { frameCount: options.frameCount }),
726
+ const livePorts = new Set();
727
+ for (const path of finished.order) {
728
+ const entry = expressionPorts.get(path);
729
+ if (!entry)
730
+ continue;
731
+ if (entry.port.live || [...(inputs.get(path) ?? [])].some(dep => livePorts.has(dep))) {
732
+ livePorts.add(path);
733
+ }
734
+ }
735
+ for (const ref of offTimeRefs) {
736
+ if (livePorts.has(ref.path)) {
737
+ fail('off-time-live-channel', 'Live channel does not support off-time reads: ' + ref.path, ref.source);
738
+ }
739
+ }
740
+ const liveChannels = channels.filter(channel => channel.live).map(channelPath).sort(comparePaths);
741
+ const audioChannels = new Set(contextWires.filter(({ to, from }) => to.startsWith('/ch/') && from.stage === 'audio').map(({ to }) => to));
742
+ for (const path of finished.order) {
743
+ if (path.startsWith('/ch/') && [...(inputs.get(path) ?? [])].some(input => audioChannels.has(input))) {
744
+ audioChannels.add(path);
745
+ }
746
+ }
747
+ for (const [path, { node }] of expressionPorts) {
748
+ if (!livePorts.has(path) && !audioChannels.has(path))
749
+ continue;
750
+ if (node.extend || node.operator?.definition.usesHistory?.(node.operator.props, fps)) {
751
+ fail('off-time-channel-operator', 'Channel requires unavailable off-frame input: ' + path, node.source);
752
+ }
753
+ }
754
+ const scene = freeze({
755
+ fps,
756
+ ...(options.frameCount !== undefined && { frameCount: options.frameCount }),
465
757
  graph: finished.graph,
758
+ ...(Object.keys(ranges).length && { ranges }),
759
+ liveChannels,
466
760
  ...((viewCamera || backgroundColor || display) && { view: {
467
761
  ...(viewCamera && { camera: objectPath(viewCamera) }),
468
762
  ...(backgroundColor && { backgroundColor }),
@@ -471,7 +765,7 @@ function compile(lines, root, fps, options) {
471
765
  });
472
766
  objects.sort((a, b) => comparePaths(objectPath(a), objectPath(b)));
473
767
  channels.sort((a, b) => comparePaths(channelPath(a), channelPath(b)));
474
- plans.set(scene, { objects, channels, drivers, events, order: finished.order });
768
+ plans.set(scene, { objects, channels, drivers, events, order: finished.order, extensions, audioChannels });
475
769
  return scene;
476
770
  }
477
771
  function rename(names, node, name, source) {