Skip to content

Commit 7d4048b

Browse files
authored
Merge pull request #56 from ut-code/fix-new-components-simulation
Fix new components simulation
2 parents 422cf75 + 096521c commit 7d4048b

File tree

7 files changed

+82
-24
lines changed

7 files changed

+82
-24
lines changed

src/pages/edit/Editor/renderer/Background.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ export default function CCComponentEditorRendererBackground() {
1616
const startPerspective = componentEditorState.perspective;
1717
const startPoint = vector2.fromDomEvent(pointerDownEvent.nativeEvent);
1818

19-
pointerDownEvent.currentTarget.setPointerCapture(
20-
pointerDownEvent.pointerId,
21-
);
19+
currentTarget.setPointerCapture(pointerDownEvent.pointerId);
2220
const onPointerMove = (pointerMoveEvent: PointerEvent) => {
2321
const endPoint = vector2.fromDomEvent(pointerMoveEvent);
2422
componentEditorState.setPerspective({
@@ -35,9 +33,7 @@ export default function CCComponentEditorRendererBackground() {
3533
const onPointerUp = () => {
3634
currentTarget.removeEventListener("pointermove", onPointerMove);
3735
currentTarget.removeEventListener("pointerup", onPointerUp);
38-
pointerDownEvent.currentTarget.releasePointerCapture(
39-
pointerDownEvent.pointerId,
40-
);
36+
currentTarget.releasePointerCapture(pointerDownEvent.pointerId);
4137
};
4238
currentTarget.addEventListener("pointermove", onPointerMove);
4339
currentTarget.addEventListener("pointerup", onPointerUp);

src/pages/edit/Editor/renderer/NodePin.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export default function CCComponentEditorRendererNodePin({
2929
const componentEditorState = useComponentEditorStore()();
3030
const nodePin = nullthrows(store.nodePins.get(nodePinId));
3131
const node = nullthrows(store.nodes.get(nodePin.nodeId));
32+
const nodePins = store.nodePins.getManyByNodeId(node.id);
3233
const componentPin = nullthrows(
3334
store.componentPins.get(nodePin.componentPinId),
3435
);
@@ -152,7 +153,10 @@ export default function CCComponentEditorRendererNodePin({
152153
implementationComponentPin.type === "input"
153154
) {
154155
nodePinValue = nullthrows(
155-
componentEditorState.getInputValue(implementationComponentPin.id),
156+
componentEditorState.getInputValue(
157+
implementationComponentPin.id,
158+
nodePins,
159+
),
156160
);
157161
} else {
158162
nodePinValue = nullthrows(

src/pages/edit/Editor/store/slices/core/index.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import simulateComponent from "../../../../../../store/componentEvaluator";
55
import type { CCComponentPinId } from "../../../../../../store/componentPin";
66
import type { CCConnectionId } from "../../../../../../store/connection";
77
import type { CCNodeId } from "../../../../../../store/node";
8-
import type { CCNodePinId } from "../../../../../../store/nodePin";
8+
import type { CCNodePin, CCNodePinId } from "../../../../../../store/nodePin";
99
import type { ComponentEditorSliceCreator } from "../../types";
1010
import type { EditorStoreCoreSlice } from "./types";
1111

@@ -46,12 +46,13 @@ export const createComponentEditorStoreCoreSlice: ComponentEditorSliceCreator<
4646
},
4747
/** @private */
4848
inputValues: new Map(),
49-
getInputValue(componentPinId: CCComponentPinId) {
49+
getInputValue(componentPinId: CCComponentPinId, nodePins: CCNodePin[]) {
5050
const value = get().inputValues.get(componentPinId);
5151
if (!value) {
5252
const multiplexability =
5353
store.componentPins.getComponentPinMultiplexability(
5454
componentPinId,
55+
nodePins,
5556
);
5657
if (multiplexability === "undecidable") {
5758
throw new Error("Cannot determine multiplexability");
@@ -174,8 +175,17 @@ export const createComponentEditorStoreCoreSlice: ComponentEditorSliceCreator<
174175
const inputValues = new Map<CCComponentPinId, SimulationValue>();
175176
const pins = store.componentPins.getManyByComponentId(componentId);
176177
for (const pin of pins) {
178+
invariant(pin.implementation);
177179
if (pin.type === "input") {
178-
inputValues.set(pin.id, editorState.getInputValue(pin.id));
180+
const nodePin = store.nodePins.get(pin.implementation);
181+
invariant(nodePin);
182+
const node = store.nodes.get(nodePin.nodeId);
183+
invariant(node);
184+
const nodePins = store.nodePins.getManyByNodeId(node.id);
185+
inputValues.set(
186+
pin.id,
187+
editorState.getInputValue(pin.id, nodePins),
188+
);
179189
}
180190
}
181191
simulationCachedFrames.push(

src/pages/edit/Editor/store/slices/core/types.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { Vector2 } from "../../../../../../common/vector2";
33
import type { CCComponentPinId } from "../../../../../../store/componentPin";
44
import type { CCConnectionId } from "../../../../../../store/connection";
55
import type { CCNodeId } from "../../../../../../store/node";
6-
import type { CCNodePinId } from "../../../../../../store/nodePin";
6+
import type { CCNodePin, CCNodePinId } from "../../../../../../store/nodePin";
77

88
export type EditorMode = EditorModeEdit | EditorModePlay;
99
export type EditorModeEdit = "edit";
@@ -30,7 +30,10 @@ export type EditorStoreCoreSlice = {
3030
target: NodePinPropertyEditorTarget | null,
3131
): void;
3232
inputValues: Map<InputValueKey, SimulationValue>;
33-
getInputValue(componentPinId: CCComponentPinId): SimulationValue;
33+
getInputValue(
34+
componentPinId: CCComponentPinId,
35+
nodePins: CCNodePin[],
36+
): SimulationValue;
3437
setInputValue(componentPinId: CCComponentPinId, value: SimulationValue): void;
3538
setEditorMode(mode: EditorMode): void;
3639
resetTimeStep(): void;

src/store/componentPin.ts

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
or,
1616
xor,
1717
} from "./intrinsics/definitions";
18-
import type { CCNodePinId } from "./nodePin";
18+
import type { CCNodePin, CCNodePinId } from "./nodePin";
1919

2020
export type CCComponentPin = {
2121
readonly id: CCComponentPinId;
@@ -47,7 +47,11 @@ export type CCPinImplementation = CCNodePinId | null;
4747

4848
export type CCPinMultiplexability =
4949
| { isMultiplexable: true }
50-
| { isMultiplexable: false; multiplicity: number };
50+
| {
51+
isMultiplexable: false;
52+
multiplicity: number;
53+
};
54+
// | { isMultiplexable: false; multiplicity: number };
5155

5256
export type CCComponentPinMultiplexability =
5357
| CCPinMultiplexability
@@ -239,6 +243,7 @@ export class CCComponentPinStore extends EventEmitter<CCComponentPinStoreEvents>
239243
*/
240244
getComponentPinMultiplexability(
241245
pinId: CCComponentPinId,
246+
nodePins: CCNodePin[],
242247
): CCComponentPinMultiplexability {
243248
const pin = this.#pins.get(pinId);
244249
invariant(pin);
@@ -264,13 +269,43 @@ export class CCComponentPinStore extends EventEmitter<CCComponentPinStoreEvents>
264269
return "undecidable";
265270
}
266271
case nullthrows(aggregate.outputPin.id): {
267-
return "undecidable";
272+
const multiplicity = nodePins
273+
.filter((pin) => {
274+
const componentPin = this.#store.componentPins.get(
275+
pin.componentPinId,
276+
);
277+
invariant(componentPin);
278+
return componentPin.type === "input";
279+
})
280+
.reduce((acc, pin) => {
281+
invariant(pin.userSpecifiedBitWidth);
282+
return acc + pin.userSpecifiedBitWidth;
283+
}, 0);
284+
return {
285+
isMultiplexable: false,
286+
multiplicity,
287+
};
268288
}
269289
case nullthrows(decompose.outputPin.id): {
270290
return "undecidable";
271291
}
272292
case nullthrows(decompose.inputPin.In.id): {
273-
return "undecidable";
293+
const multiplicity = nodePins
294+
.filter((pin) => {
295+
const componentPin = this.#store.componentPins.get(
296+
pin.componentPinId,
297+
);
298+
invariant(componentPin);
299+
return componentPin.type === "output";
300+
})
301+
.reduce((acc, pin) => {
302+
invariant(pin.userSpecifiedBitWidth);
303+
return acc + pin.userSpecifiedBitWidth;
304+
}, 0);
305+
return {
306+
isMultiplexable: false,
307+
multiplicity,
308+
};
274309
}
275310
case nullthrows(broadcast.inputPin.In.id): {
276311
return { isMultiplexable: false, multiplicity: 1 };

src/store/intrinsics/definitions.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function createUnaryOperator(
2323
evaluate: (input) => {
2424
invariant(input.A[0] && !input.A[1]);
2525
const A = input.A[0];
26-
return A.map((a) => [evaluate(nullthrows(a))]);
26+
return [A.map((a) => evaluate(nullthrows(a)))];
2727
},
2828
});
2929
}
@@ -47,9 +47,11 @@ function createBinaryOperator(
4747
const A = input.A[0];
4848
const B = input.B[0];
4949
invariant(A.length === B.length);
50-
return Array.from({ length: input.A.length }, (_, i) => [
51-
evaluate(nullthrows(A[i]), nullthrows(B[i])),
52-
]);
50+
return [
51+
Array.from({ length: A.length }, (_, i) =>
52+
evaluate(nullthrows(A[i]), nullthrows(B[i])),
53+
),
54+
];
5355
},
5456
});
5557
}
@@ -123,10 +125,11 @@ export const broadcast = new IntrinsicComponentDefinition({
123125
out: { name: "Out", isBitWidthConfigurable: true },
124126
evaluate: (input, outputShape) => {
125127
invariant(input.In[0] && !input.In[1]);
126-
const inputValue = input.In[0];
128+
invariant(input.In[0][0] !== undefined && !input.In[0][1]);
129+
const inputValue = input.In[0][0];
127130
invariant(outputShape[0] && !outputShape[1]);
128131
const outputMultiplicity = outputShape[0].multiplicity;
129-
return Array.from({ length: outputMultiplicity }, () => inputValue);
132+
return [Array.from({ length: outputMultiplicity }, () => inputValue)];
130133
},
131134
});
132135

src/store/nodePin.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,13 +188,19 @@ export class CCNodePinStore extends EventEmitter<CCNodePinStoreEvents> {
188188
const node = nullthrows(this.#store.nodes.get(nodeId));
189189
const nodePins = this.getManyByNodeId(node.id);
190190
const givenPinMultiplexability =
191-
this.#store.componentPins.getComponentPinMultiplexability(pinId);
191+
this.#store.componentPins.getComponentPinMultiplexability(
192+
pinId,
193+
nodePins,
194+
);
192195
if (givenPinMultiplexability === "undecidable") {
193196
invariant(
194197
userSpecifiedBitWidth,
195198
"Multiplexability of undecidable pin must be contained in multiplexabilityEnv",
196199
);
197-
return { isMultiplexable: false, multiplicity: userSpecifiedBitWidth };
200+
return {
201+
isMultiplexable: false,
202+
multiplicity: userSpecifiedBitWidth,
203+
};
198204
}
199205
if (!givenPinMultiplexability.isMultiplexable) {
200206
return givenPinMultiplexability;
@@ -203,6 +209,7 @@ export class CCNodePinStore extends EventEmitter<CCNodePinStoreEvents> {
203209
const pinMultiplexability =
204210
this.#store.componentPins.getComponentPinMultiplexability(
205211
nodePin.componentPinId,
212+
nodePins,
206213
);
207214
if (pinMultiplexability === "undecidable") {
208215
throw new Error("unreachable");

0 commit comments

Comments
 (0)