Skip to content

Commit 36cb5bf

Browse files
committed
feat: Implement Static vs Instance memory simulation module with new types and useStaticSimulator hook.
1 parent 84bdef5 commit 36cb5bf

6 files changed

Lines changed: 183 additions & 1 deletion

File tree

src/contexts/ConsoleContext.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const createInitialState = (): ModuleConsoleState => ({
1919
threads: [],
2020
jit: [],
2121
jmm: [],
22+
static: [],
2223
});
2324

2425
interface ConsoleProviderProps {

src/hooks/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ export { useGCSimulator } from './useGCSimulator';
22
export { useThreadSimulator } from './useThreadSimulator';
33
export { useJITSimulator } from './useJITSimulator';
44
export { useJMMSimulator } from './useJMMSimulator';
5+
export { useStaticSimulator } from './useStaticSimulator';

src/hooks/useStaticSimulator.ts

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/**
2+
* useStaticSimulator Hook
3+
* Encapsulates Static vs Non-Static simulation logic (SRP)
4+
*/
5+
6+
import { useState, useCallback } from 'react';
7+
import type { IStaticState, IUseStaticSimulator, IObjectInstance } from '../types';
8+
import { useModuleConsole } from '../contexts';
9+
10+
const createInitialState = (): IStaticState => ({
11+
classDefinition: {
12+
name: 'Counter',
13+
members: [
14+
{ id: 'static-count', name: 'totalCount', isStatic: true, type: 'field', value: 0 },
15+
{ id: 'instance-id', name: 'instanceId', isStatic: false, type: 'field' },
16+
{ id: 'static-method', name: 'getTotalCount()', isStatic: true, type: 'method' },
17+
{ id: 'instance-method', name: 'getId()', isStatic: false, type: 'method' },
18+
],
19+
instanceCount: 0,
20+
},
21+
instances: [],
22+
nextInstanceId: 1,
23+
staticFieldValue: 0,
24+
selectedView: 'memory',
25+
});
26+
27+
export function useStaticSimulator(): IUseStaticSimulator {
28+
const [state, setState] = useState<IStaticState>(createInitialState);
29+
const { log } = useModuleConsole('static');
30+
31+
const createInstance = useCallback(() => {
32+
setState((prev) => {
33+
const newId = prev.nextInstanceId;
34+
const newInstance: IObjectInstance = {
35+
id: newId,
36+
className: prev.classDefinition.name,
37+
instanceFields: [
38+
{ name: 'instanceId', value: newId },
39+
],
40+
};
41+
42+
log(`new Counter() → Instance #${newId} created in HEAP`, 'success');
43+
log(`Counter.totalCount++ (static field incremented in Metaspace)`);
44+
45+
return {
46+
...prev,
47+
instances: [...prev.instances, newInstance],
48+
nextInstanceId: newId + 1,
49+
staticFieldValue: prev.staticFieldValue + 1,
50+
classDefinition: {
51+
...prev.classDefinition,
52+
instanceCount: prev.classDefinition.instanceCount + 1,
53+
members: prev.classDefinition.members.map((m) =>
54+
m.id === 'static-count' ? { ...m, value: prev.staticFieldValue + 1 } : m
55+
),
56+
},
57+
};
58+
});
59+
}, [log]);
60+
61+
const updateStaticField = useCallback((value: number) => {
62+
setState((prev) => {
63+
log(`Counter.totalCount = ${value} (ALL instances see this change!)`, 'warn');
64+
65+
return {
66+
...prev,
67+
staticFieldValue: value,
68+
classDefinition: {
69+
...prev.classDefinition,
70+
members: prev.classDefinition.members.map((m) =>
71+
m.id === 'static-count' ? { ...m, value } : m
72+
),
73+
},
74+
};
75+
});
76+
}, [log]);
77+
78+
const updateInstanceField = useCallback((instanceId: number, value: number) => {
79+
setState((prev) => {
80+
log(`instance#${instanceId}.instanceId = ${value} (ONLY this instance changes)`);
81+
82+
return {
83+
...prev,
84+
instances: prev.instances.map((inst) =>
85+
inst.id === instanceId
86+
? {
87+
...inst,
88+
instanceFields: inst.instanceFields.map((f) =>
89+
f.name === 'instanceId' ? { ...f, value } : f
90+
),
91+
}
92+
: inst
93+
),
94+
};
95+
});
96+
}, [log]);
97+
98+
const setView = useCallback((view: 'memory' | 'comparison') => {
99+
setState((prev) => ({ ...prev, selectedView: view }));
100+
}, []);
101+
102+
const reset = useCallback(() => {
103+
setState(createInitialState());
104+
log('Simulation reset. Class unloaded and reloaded.');
105+
}, [log]);
106+
107+
return {
108+
...state,
109+
createInstance,
110+
updateStaticField,
111+
updateInstanceField,
112+
setView,
113+
reset,
114+
};
115+
}

src/types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ export * from './thread.types';
55
export * from './jit.types';
66
export * from './jmm.types';
77
export * from './console.types';
8+
export * from './static.types';

src/types/module.types.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55

66
// Available module identifiers
7-
export type ModuleId = 'gc' | 'threads' | 'jit' | 'jmm';
7+
export type ModuleId = 'gc' | 'threads' | 'jit' | 'jmm' | 'static';
88

99
// Current view state
1010
export type ViewMode = 'hub' | ModuleId;
@@ -67,4 +67,14 @@ export const MODULE_REGISTRY: Record<ModuleId, IModuleConfig> = {
6767
glowColor: 'rgba(59, 130, 246, 0.5)',
6868
badge: 'Cache Lab',
6969
},
70+
static: {
71+
id: 'static',
72+
title: 'Static vs Instance',
73+
subtitle: 'Memory Layout',
74+
description: 'Static fields in Metaspace, instance fields in Heap, when and why to use each.',
75+
accentColor: '#f472b6',
76+
glowColor: 'rgba(244, 114, 182, 0.5)',
77+
badge: 'Memory Lab',
78+
},
7079
};
80+

src/types/static.types.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* Static Module Types
3+
* Defines types for Static vs Non-Static simulation
4+
*/
5+
6+
// Memory area types
7+
export type MemoryArea = 'metaspace' | 'heap';
8+
9+
// Member types
10+
export type MemberType = 'field' | 'method';
11+
12+
// Represents a class member (field or method)
13+
export interface IClassMember {
14+
id: string;
15+
name: string;
16+
isStatic: boolean;
17+
type: MemberType;
18+
value?: string | number;
19+
}
20+
21+
// Represents a class in memory
22+
export interface IClassDefinition {
23+
name: string;
24+
members: IClassMember[];
25+
instanceCount: number;
26+
}
27+
28+
// Represents an object instance in heap
29+
export interface IObjectInstance {
30+
id: number;
31+
className: string;
32+
instanceFields: { name: string; value: string | number }[];
33+
}
34+
35+
// Complete Static simulator state
36+
export interface IStaticState {
37+
classDefinition: IClassDefinition;
38+
instances: IObjectInstance[];
39+
nextInstanceId: number;
40+
staticFieldValue: number;
41+
selectedView: 'memory' | 'comparison';
42+
}
43+
44+
// Static simulator actions
45+
export interface IStaticActions {
46+
createInstance: () => void;
47+
updateStaticField: (value: number) => void;
48+
updateInstanceField: (instanceId: number, value: number) => void;
49+
reset: () => void;
50+
setView: (view: 'memory' | 'comparison') => void;
51+
}
52+
53+
// Combined hook return type
54+
export interface IUseStaticSimulator extends IStaticState, IStaticActions { }

0 commit comments

Comments
 (0)