Skip to content

Commit dd0f9b2

Browse files
tohlhRichDom2185
andauthored
Typed modules (#331)
* Typed Rune module using decorators * Using "placeholder class" for representing module types * Remove unwanted line * Add types to Curve module * Rename type map decorators * Add type information for RunDisplay * Organize imports * Favor destructuring for safety and readability * Restore unintentionally deleted export * Add comments for clarification --------- Co-authored-by: Richard Dominick <[email protected]>
1 parent 740559c commit dd0f9b2

File tree

8 files changed

+1017
-705
lines changed

8 files changed

+1017
-705
lines changed

src/bundles/curve/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,11 @@ export {
7272
y_of,
7373
z_of
7474
} from './functions';
75+
76+
// This line explicitly imports the decorators in type_interface, so esbuild doesn't remove them during tree-shaking.
77+
// It preserves the type definitions by signaling to esbuild that the file is actively used.
78+
export {} from './type_interface';
79+
80+
export {
81+
type_map
82+
} from '../../typings/type_map';
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
import { classDeclaration, typeDeclaration, functionDeclaration } from '../../typings/type_map';
2+
3+
@classDeclaration('Point')
4+
export class Point {}
5+
6+
@classDeclaration('AnimatedCurve')
7+
export class AnimatedCurve{}
8+
9+
@typeDeclaration('(u: number) => Point')
10+
export class Curve {}
11+
12+
@typeDeclaration('(t: number) => Curve')
13+
export class CurveAnimation {}
14+
15+
export class TypeInterface {
16+
@functionDeclaration('duration: number, fps: number, drawer: (func: Curve) => Curve, func: (func: Curve) => Curve', 'AnimatedCurve')
17+
animate_3D_curve() {}
18+
19+
@functionDeclaration('duration: number, fps: number, drawer: (func: Curve) => Curve, func: (func: Curve) => Curve', 'AnimatedCurve')
20+
animate_curve() {}
21+
22+
@functionDeclaration('t: number', 'number')
23+
arc() {}
24+
25+
@functionDeclaration('p: Point', 'number')
26+
b_of() {}
27+
28+
@functionDeclaration('curve1: Curve, curve2: Curve', 'Curve')
29+
connect_ends() {}
30+
31+
@functionDeclaration('curve1: Curve, curve2: Curve', 'Curve')
32+
connect_rigidly() {}
33+
34+
@functionDeclaration('numPoints: number', '(func: Curve) => Curve')
35+
draw_3D_connected() {}
36+
37+
@functionDeclaration('numPoints: number', '(func: Curve) => Curve')
38+
draw_3D_connected_full_view() {}
39+
40+
@functionDeclaration('numPoints: number', '(func: Curve) => Curve')
41+
draw_3D_connected_full_view_proportional() {}
42+
43+
@functionDeclaration('numPoints: number', '(func: Curve) => Curve')
44+
draw_3D_points() {}
45+
46+
@functionDeclaration('numPoints: number', '(func: Curve) => Curve')
47+
draw_3D_points_full_view() {}
48+
49+
@functionDeclaration('numPoints: number', '(func: Curve) => Curve')
50+
draw_3D_points_full_view_proportional() {}
51+
52+
@functionDeclaration('numPoints: number', '(func: Curve) => Curve')
53+
draw_connected() {}
54+
55+
@functionDeclaration('numPoints: number', '(func: Curve) => Curve')
56+
draw_connected_full_view() {}
57+
58+
@functionDeclaration('numPoints: number', '(func: Curve) => Curve')
59+
draw_connected_full_view_proportional() {}
60+
61+
@functionDeclaration('numPoints: number', '(func: Curve) => Curve')
62+
draw_points() {}
63+
64+
@functionDeclaration('numPoints: number', '(func: Curve) => Curve')
65+
draw_points_full_view() {}
66+
67+
@functionDeclaration('numPoints: number', '(func: Curve) => Curve')
68+
draw_points_full_view_proportional() {}
69+
70+
@functionDeclaration('p: Point', 'number')
71+
g_of() {}
72+
73+
@functionDeclaration('curve: Curve', 'Curve')
74+
invert() {}
75+
76+
@functionDeclaration('x: number, y: number, z: number, r: number, g: number, b: number', 'Point')
77+
make_3D_color_point() {}
78+
79+
@functionDeclaration('x: number, y: number, z: number', 'Point')
80+
make_3D_point() {}
81+
82+
@functionDeclaration('x: number, y: number, r: number, g: number, b: number', 'Point')
83+
make_color_point() {}
84+
85+
@functionDeclaration('x: number, y: number', 'Point')
86+
make_point() {}
87+
88+
@functionDeclaration('curve: Curve', 'Curve')
89+
put_in_standard_position() {}
90+
91+
@functionDeclaration('p: Point', 'number')
92+
r_of() {}
93+
94+
@functionDeclaration('theta1: number, theta2: number, theta3: number', '(c: Curve) => Curve')
95+
rotate_around_origin() {}
96+
97+
@functionDeclaration('x: number, y: number', '(c: Curve) => Curve')
98+
scale() {}
99+
100+
@functionDeclaration('s: number', '(c: Curve) => Curve')
101+
scale_proportional() {}
102+
103+
@functionDeclaration('x0: number, y0: number, z0: number', '(c: Curve) => Curve')
104+
translate() {}
105+
106+
@functionDeclaration('t: number', 'Point')
107+
unit_circle() {}
108+
109+
@functionDeclaration('t: number', 'Point')
110+
unit_line() {}
111+
112+
@functionDeclaration('t: number', 'Curve')
113+
unit_line_at() {}
114+
115+
@functionDeclaration('p: Point', 'number')
116+
x_of() {}
117+
118+
@functionDeclaration('p: Point', 'number')
119+
y_of() {}
120+
121+
@functionDeclaration('p: Point', 'number')
122+
z_of() {}
123+
}

src/bundles/rune/display.ts

Lines changed: 104 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import context from 'js-slang/context';
2+
import { functionDeclaration } from '../../typings/type_map';
23
import { AnaglyphRune, HollusionRune } from './functions';
3-
import { type DrawnRune, AnimatedRune, type Rune, NormalRune, type RuneAnimation } from './rune';
4+
import { AnimatedRune, NormalRune, type DrawnRune, type Rune, type RuneAnimation } from './rune';
45
import { throwIfNotRune } from './runes_ops';
56

67
// =============================================================================
@@ -12,103 +13,111 @@ context.moduleContexts.rune.state = {
1213
drawnRunes
1314
};
1415

15-
/**
16-
* Renders the specified Rune in a tab as a basic drawing.
17-
* @param rune - The Rune to render
18-
* @return {Rune} The specified Rune
19-
*
20-
* @category Main
21-
*/
22-
export function show(rune: Rune): Rune {
23-
throwIfNotRune(show.name, rune);
24-
drawnRunes.push(new NormalRune(rune));
25-
return rune;
26-
}
16+
class RuneDisplay {
17+
/**
18+
* Renders the specified Rune in a tab as a basic drawing.
19+
* @param rune - The Rune to render
20+
* @return {Rune} The specified Rune
21+
*
22+
* @category Main
23+
*/
24+
@functionDeclaration('rune: Rune', 'Rune')
25+
static show(rune: Rune): Rune {
26+
throwIfNotRune(RuneDisplay.show.name, rune);
27+
drawnRunes.push(new NormalRune(rune));
28+
return rune;
29+
}
2730

28-
/**
29-
* Renders the specified Rune in a tab as an anaglyph. Use 3D glasses to view the
30-
* anaglyph.
31-
* @param rune - The Rune to render
32-
* @return {Rune} The specified Rune
33-
*
34-
* @category Main
35-
*/
36-
export function anaglyph(rune: Rune): Rune {
37-
throwIfNotRune(anaglyph.name, rune);
38-
drawnRunes.push(new AnaglyphRune(rune));
39-
return rune;
40-
}
31+
/**
32+
* Renders the specified Rune in a tab as an anaglyph. Use 3D glasses to view the
33+
* anaglyph.
34+
* @param rune - The Rune to render
35+
* @return {Rune} The specified Rune
36+
*
37+
* @category Main
38+
*/
39+
@functionDeclaration('rune: Rune', 'Rune')
40+
static anaglyph(rune: Rune): Rune {
41+
throwIfNotRune(RuneDisplay.anaglyph.name, rune);
42+
drawnRunes.push(new AnaglyphRune(rune));
43+
return rune;
44+
}
4145

42-
/**
43-
* Renders the specified Rune in a tab as a hollusion, using the specified
44-
* magnitude.
45-
* @param rune - The Rune to render
46-
* @param {number} magnitude - The hollusion's magnitude
47-
* @return {Rune} The specified Rune
48-
*
49-
* @category Main
50-
*/
51-
export function hollusion_magnitude(rune: Rune, magnitude: number): Rune {
52-
throwIfNotRune(hollusion_magnitude.name, rune);
53-
drawnRunes.push(new HollusionRune(rune, magnitude));
54-
return rune;
55-
}
46+
/**
47+
* Renders the specified Rune in a tab as a hollusion, using the specified
48+
* magnitude.
49+
* @param rune - The Rune to render
50+
* @param {number} magnitude - The hollusion's magnitude
51+
* @return {Rune} The specified Rune
52+
*
53+
* @category Main
54+
*/
55+
@functionDeclaration('rune: Rune, magnitude: number', 'Rune')
56+
static hollusion_magnitude(rune: Rune, magnitude: number): Rune {
57+
throwIfNotRune(RuneDisplay.hollusion_magnitude.name, rune);
58+
drawnRunes.push(new HollusionRune(rune, magnitude));
59+
return rune;
60+
}
5661

57-
/**
58-
* Renders the specified Rune in a tab as a hollusion, with a default magnitude
59-
* of 0.1.
60-
* @param rune - The Rune to render
61-
* @return {Rune} The specified Rune
62-
*
63-
* @category Main
64-
*/
65-
export function hollusion(rune: Rune): Rune {
66-
throwIfNotRune(hollusion.name, rune);
67-
return hollusion_magnitude(rune, 0.1);
68-
}
62+
/**
63+
* Renders the specified Rune in a tab as a hollusion, with a default magnitude
64+
* of 0.1.
65+
* @param rune - The Rune to render
66+
* @return {Rune} The specified Rune
67+
*
68+
* @category Main
69+
*/
70+
@functionDeclaration('rune: Rune', 'Rune')
71+
static hollusion(rune: Rune): Rune {
72+
throwIfNotRune(RuneDisplay.hollusion.name, rune);
73+
return RuneDisplay.hollusion_magnitude(rune, 0.1);
74+
}
6975

70-
/**
71-
* Create an animation of runes
72-
* @param duration Duration of the entire animation in seconds
73-
* @param fps Duration of each frame in frames per seconds
74-
* @param func Takes in the timestamp and returns a Rune to draw
75-
* @returns A rune animation
76-
*
77-
* @category Main
78-
*/
79-
export function animate_rune(
80-
duration: number,
81-
fps: number,
82-
func: RuneAnimation
83-
) {
84-
const anim = new AnimatedRune(duration, fps, (n) => {
85-
const rune = func(n);
86-
throwIfNotRune(animate_rune.name, rune);
87-
return new NormalRune(rune);
88-
});
89-
drawnRunes.push(anim);
90-
return anim;
91-
}
76+
/**
77+
* Create an animation of runes
78+
* @param duration Duration of the entire animation in seconds
79+
* @param fps Duration of each frame in frames per seconds
80+
* @param func Takes in the timestamp and returns a Rune to draw
81+
* @returns A rune animation
82+
*
83+
* @category Main
84+
*/
85+
@functionDeclaration('duration: number, fps: number, func: RuneAnimation', 'AnimatedRune')
86+
static animate_rune(duration: number, fps: number, func: RuneAnimation) {
87+
const anim = new AnimatedRune(duration, fps, (n) => {
88+
const rune = func(n);
89+
throwIfNotRune(RuneDisplay.animate_rune.name, rune);
90+
return new NormalRune(rune);
91+
});
92+
drawnRunes.push(anim);
93+
return anim;
94+
}
9295

93-
/**
94-
* Create an animation of anaglyph runes
95-
* @param duration Duration of the entire animation in seconds
96-
* @param fps Duration of each frame in frames per seconds
97-
* @param func Takes in the timestamp and returns a Rune to draw
98-
* @returns A rune animation
99-
*
100-
* @category Main
101-
*/
102-
export function animate_anaglyph(
103-
duration: number,
104-
fps: number,
105-
func: RuneAnimation
106-
) {
107-
const anim = new AnimatedRune(duration, fps, (n) => {
108-
const rune = func(n);
109-
throwIfNotRune(animate_anaglyph.name, rune);
110-
return new AnaglyphRune(rune);
111-
});
112-
drawnRunes.push(anim);
113-
return anim;
96+
/**
97+
* Create an animation of anaglyph runes
98+
* @param duration Duration of the entire animation in seconds
99+
* @param fps Duration of each frame in frames per seconds
100+
* @param func Takes in the timestamp and returns a Rune to draw
101+
* @returns A rune animation
102+
*
103+
* @category Main
104+
*/
105+
@functionDeclaration('duration: number, fps: number, func: RuneAnimation', 'AnimatedRune')
106+
static animate_anaglyph(duration: number, fps: number, func: RuneAnimation) {
107+
const anim = new AnimatedRune(duration, fps, (n) => {
108+
const rune = func(n);
109+
throwIfNotRune(RuneDisplay.animate_anaglyph.name, rune);
110+
return new AnaglyphRune(rune);
111+
});
112+
drawnRunes.push(anim);
113+
return anim;
114+
}
114115
}
116+
117+
export const {show,
118+
anaglyph,
119+
hollusion,
120+
hollusion_magnitude,
121+
animate_rune,
122+
animate_anaglyph,
123+
} = RuneDisplay;

0 commit comments

Comments
 (0)