Skip to content

Commit 659cd92

Browse files
committed
chore: fix dep issue
1 parent eb514eb commit 659cd92

11 files changed

Lines changed: 65 additions & 51 deletions

File tree

src/common/type/subtype.ts

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,8 @@ import type {
1414
TypeCompatibility,
1515
TypeString,
1616
} from './types';
17-
18-
// Lazy import to break subtype → parse cycle
19-
let _parseType: typeof import('./parse').parseType | undefined;
20-
function lazyParseType(s: string): Type {
21-
if (!_parseType) {
22-
const m = './parse';
23-
_parseType = require(m).parseType;
24-
}
25-
return _parseType!(s);
26-
}
17+
import { parseType } from './parse';
18+
import { widen } from './utils';
2719

2820
/** For each key, *all* the primitive subtypes of the type corresponding to that key */
2921
const PRIMITIVE_SUBTYPES: Record<PrimitiveType, PrimitiveType[]> = {
@@ -119,12 +111,12 @@ export function isSubtype(
119111
typeof lhs === 'string' &&
120112
!PRIMITIVE_TYPES.includes(lhs as PrimitiveType)
121113
)
122-
lhs = lazyParseType(lhs);
114+
lhs = parseType(lhs);
123115
if (
124116
typeof rhs === 'string' &&
125117
!PRIMITIVE_TYPES.includes(rhs as PrimitiveType)
126118
)
127-
rhs = lazyParseType(rhs);
119+
rhs = parseType(rhs);
128120

129121
// Every type is a subtype of `any`, the top type
130122
if (rhs === 'any') return true;
@@ -431,9 +423,6 @@ export function isSubtype(
431423
);
432424

433425
if (lhs.kind === 'record') {
434-
// Lazy import widen to break subtype → utils cycle
435-
const m2 = './utils';
436-
const { widen } = require(m2);
437426
return isSubtype(
438427
{
439428
kind: 'tuple',

src/compute-engine/boxed-expression/abstract-boxed-expression.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ import { cmp, eq, same } from './compare';
5151
import { CancellationError } from '../../common/interruptible';
5252
import { isSymbol, isString, isFunction } from './type-guards';
5353

54+
// Lazy reference to break circular dependency:
55+
// serialize → numerics → utils → abstract-boxed-expression
56+
import type { serializeJson as _SerializeJsonFn } from './serialize';
57+
let _serializeJson: typeof _SerializeJsonFn;
58+
/** @internal */
59+
export function _setSerializeJson(fn: typeof _SerializeJsonFn) {
60+
_serializeJson = fn;
61+
}
62+
5463
/**
5564
* _BoxedExpression
5665
*
@@ -303,9 +312,7 @@ export abstract class _BoxedExpression implements Expression {
303312
metadata: defaultOptions.metadata,
304313
};
305314

306-
// Dynamic import to avoid circular dependency
307-
const { serializeJson } = require('./serialize');
308-
return serializeJson(this.engine, this, opts);
315+
return _serializeJson(this.engine, this, opts);
309316
}
310317

311318
print(): void {

src/compute-engine/boxed-expression/compare.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,15 @@ import {
88
isString,
99
isTensor,
1010
} from './type-guards';
11-
// Dynamic import for expand to avoid circular dependency
12-
// (expand → arithmetic-add → boxed-tensor → abstract-boxed-expression → compare)
11+
12+
// Lazy reference to break circular dependency:
13+
// expand → arithmetic-add → boxed-tensor → abstract-boxed-expression → compare
14+
import type { expand as _ExpandFn } from './expand';
15+
let _expand: typeof _ExpandFn;
16+
/** @internal */
17+
export function _setExpand(fn: typeof _ExpandFn) {
18+
_expand = fn;
19+
}
1320

1421
/**
1522
* Structural equality of boxed expressions.
@@ -129,9 +136,8 @@ export function eq(
129136
// If the expression have some unknowns, we only try to prove equality
130137
// if they have the same unknowns and are structurally equal after
131138
// expansing and simplification
132-
const { expand } = require('./expand');
133-
a = (expand(a) ?? a).simplify();
134-
b = (expand(b) ?? b).simplify();
139+
a = (_expand(a) ?? a).simplify();
140+
b = (_expand(b) ?? b).simplify();
135141
if (!sameUnknowns(a, b)) return undefined;
136142
return same(a, b);
137143
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// This module wires up lazy references that break circular dependencies.
2+
// Import it once from the main entry point (index.ts) so the registrations
3+
// run before any user code.
4+
5+
import { expand } from './expand';
6+
import { _setExpand } from './compare';
7+
8+
import { serializeJson } from './serialize';
9+
import { _setSerializeJson } from './abstract-boxed-expression';
10+
11+
import { Product } from './product';
12+
import { _setProduct } from './serialize';
13+
14+
_setExpand(expand);
15+
_setSerializeJson(serializeJson);
16+
_setProduct(Product);

src/compute-engine/boxed-expression/serialize.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,22 @@ import type {
2727
IComputeEngine as ComputeEngine,
2828
Metadata,
2929
Expression,
30+
FunctionInterface,
3031
JsonSerializationOptions,
3132
} from '../global-types';
3233
import { isOperatorDef } from './utils';
3334
import { isNumber, isSymbol, isString, isFunction } from './type-guards';
3435
import { matchesNumber, matchesSymbol } from '../../math-json/utils';
3536

37+
// Lazy reference to break circular dependency:
38+
// product → arithmetic-mul-div → ... → abstract-boxed-expression → serialize
39+
import type { Product as _ProductClass } from './product';
40+
let _Product: typeof _ProductClass;
41+
/** @internal */
42+
export function _setProduct(fn: typeof _ProductClass) {
43+
_Product = fn;
44+
}
45+
3646
function _escapeJsonString(s: undefined): undefined;
3747
function _escapeJsonString(s: string): string;
3848
function _escapeJsonString(s: string | undefined): string | undefined {
@@ -129,20 +139,13 @@ function serializePrettyJsonFunction(
129139
if (name === 'Multiply' && !exclusions.includes('Divide')) {
130140
// Display a product with negative exponents as a division if
131141
// there are terms with a negative degree.
132-
// Dynamic require to avoid circular dependency:
133-
// serialize → product → ... → abstract-boxed-expression → serialize
134-
const { Product } = require('./product');
135-
const result = new Product(ce, args, {
142+
const result = new _Product(ce, args, {
136143
canonical: false,
137144
}).asRationalExpression();
138-
if (result.operator === 'Divide')
139-
return serializeJsonFunction(
140-
ce,
141-
result.operator,
142-
result.ops!,
143-
options,
144-
metadata
145-
);
145+
if (result.operator === 'Divide') {
146+
const ops = (result as Expression & FunctionInterface).ops;
147+
return serializeJsonFunction(ce, result.operator, ops, options, metadata);
148+
}
146149
}
147150

148151
if (name === 'Power') {

src/compute-engine/compilation/glsl-target.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { Expression } from '../global-types';
22
import type { CompiledFunctions } from './types';
33
import { GPUShaderTarget } from './gpu-target';
4+
import { BaseCompiler } from './base-compiler';
45

56
/**
67
* GLSL-specific function overrides.
@@ -42,9 +43,6 @@ export class GLSLTarget extends GPUShaderTarget {
4243
returnType: string,
4344
parameters: Array<[name: string, type: string]>
4445
): string {
45-
// Dynamic import to avoid circular dependency
46-
const { BaseCompiler } = require('./base-compiler');
47-
4846
const target = this.createTarget();
4947
const body = BaseCompiler.compile(expr, target);
5048

src/compute-engine/compilation/gpu-target.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type {
88
CompilationOptions,
99
CompilationResult,
1010
} from './types';
11+
import { BaseCompiler } from './base-compiler';
1112

1213
/**
1314
* GPU shader operators shared by GLSL and WGSL.
@@ -303,8 +304,6 @@ export abstract class GPUShaderTarget implements LanguageTarget<Expression> {
303304
const allFunctions = this.getFunctions();
304305
const constants = this.getConstants();
305306

306-
// Dynamic import to avoid circular dependency
307-
const { BaseCompiler } = require('./base-compiler');
308307

309308
const target = this.createTarget({
310309
functions: (id) => {
@@ -330,8 +329,6 @@ export abstract class GPUShaderTarget implements LanguageTarget<Expression> {
330329
expr: Expression,
331330
_options: CompilationOptions<Expression> = {}
332331
): string {
333-
// Dynamic import to avoid circular dependency
334-
const { BaseCompiler } = require('./base-compiler');
335332
const target = this.createTarget();
336333
return BaseCompiler.compile(expr, target);
337334
}

src/compute-engine/compilation/python-target.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type {
88
CompilationOptions,
99
CompilationResult,
1010
} from './types';
11+
import { BaseCompiler } from './base-compiler';
1112

1213
/**
1314
* Python operator mappings
@@ -348,8 +349,6 @@ export class PythonTarget implements LanguageTarget<Expression> {
348349
expr: Expression,
349350
_options: CompilationOptions<Expression> = {}
350351
): string {
351-
// Dynamic import to avoid circular dependency
352-
const { BaseCompiler } = require('./base-compiler');
353352
const target = this.createTarget();
354353
const code = BaseCompiler.compile(expr, target);
355354

@@ -378,8 +377,6 @@ export class PythonTarget implements LanguageTarget<Expression> {
378377
parameters: string[],
379378
docstring?: string
380379
): string {
381-
// Dynamic import to avoid circular dependency
382-
const { BaseCompiler } = require('./base-compiler');
383380

384381
const target = this.createTarget();
385382
const body = BaseCompiler.compile(expr, target);
@@ -444,8 +441,6 @@ export class PythonTarget implements LanguageTarget<Expression> {
444441
* @param parameters - Parameter names
445442
*/
446443
compileLambda(expr: Expression, parameters: string[]): string {
447-
// Dynamic import to avoid circular dependency
448-
const { BaseCompiler } = require('./base-compiler');
449444

450445
const target = this.createTarget();
451446
const body = BaseCompiler.compile(expr, target);

src/compute-engine/compilation/wgsl-target.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { Expression } from '../global-types';
22
import type { CompiledFunctions } from './types';
33
import { GPUShaderTarget } from './gpu-target';
4+
import { BaseCompiler } from './base-compiler';
45

56
/**
67
* WGSL-specific function overrides.
@@ -66,9 +67,6 @@ export class WGSLTarget extends GPUShaderTarget {
6667
returnType: string,
6768
parameters: Array<[name: string, type: string]>
6869
): string {
69-
// Dynamic import to avoid circular dependency
70-
const { BaseCompiler } = require('./base-compiler');
71-
7270
const target = this.createTarget();
7371
const body = BaseCompiler.compile(expr, target);
7472

src/compute-engine/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ import { validatePattern } from './boxed-expression/boxed-patterns';
7777
import { BoxedString } from './boxed-expression/boxed-string';
7878
import { BoxedFunction } from './boxed-expression/boxed-function';
7979
import { _BoxedExpression } from './boxed-expression/abstract-boxed-expression';
80+
import './boxed-expression/init-lazy-refs';
8081
import { _BoxedOperatorDefinition } from './boxed-expression/boxed-operator-definition';
8182
import {
8283
HARMONIZATION_RULES,

0 commit comments

Comments
 (0)