Skip to content

Commit 10e6350

Browse files
committed
claering hard code
1 parent 25f8ebd commit 10e6350

File tree

1 file changed

+85
-124
lines changed

1 file changed

+85
-124
lines changed

assets/js/jvm-streams.js

Lines changed: 85 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@
1111
class JavaStreamManager {
1212
constructor() {
1313
// Stream buffers
14-
this.stdout = ""; // Output buffer
15-
this.stderr = ""; // Error buffer
14+
this.stdout = ""; // Standard output buffer
15+
this.stderr = ""; // Standard error buffer
16+
this.stdin = ""; // Standard input buffer
1617

1718
// Event listeners
18-
this.inputListeners = []; // Listeners for input requests
19+
this.inputListeners = []; // Listeners for input requests
1920
this.outputListeners = []; // Listeners for output events
2021
this.errorListeners = []; // Listeners for error events
2122

@@ -24,194 +25,154 @@ class JavaStreamManager {
2425
this.inputResolver = null;
2526
this.inputPrompt = "";
2627

27-
// Flags
28+
// Stream state
2829
this.isInitialized = false;
30+
this.isClosed = false;
2931
}
30-
31-
/**
32-
* Initialize the stream manager and register event handlers
33-
* @param {Object} options Configuration options
34-
*/
32+
33+
// Initialize stream manager
3534
initialize(options = {}) {
3635
if (this.isInitialized) return this;
3736

38-
// Register event handlers if provided
39-
if (options.onInputRequest && typeof options.onInputRequest === 'function') {
40-
this.addInputListener(options.onInputRequest);
41-
}
42-
43-
if (options.onOutput && typeof options.onOutput === 'function') {
44-
this.addOutputListener(options.onOutput);
45-
}
46-
47-
if (options.onError && typeof options.onError === 'function') {
48-
this.addErrorListener(options.onError);
49-
}
37+
// Register handlers
38+
if (options.onInputRequest) this.addInputListener(options.onInputRequest);
39+
if (options.onOutput) this.addOutputListener(options.onOutput);
40+
if (options.onError) this.addErrorListener(options.onError);
5041

5142
this.isInitialized = true;
5243
return this;
5344
}
54-
55-
/**
56-
* Add a listener for input requests (when Java code needs user input)
57-
* @param {Function} listener Callback function that handles input requests
58-
*/
45+
46+
// Add input request listener
5947
addInputListener(listener) {
6048
if (typeof listener === 'function') {
6149
this.inputListeners.push(listener);
6250
}
6351
return this;
6452
}
65-
66-
/**
67-
* Add a listener for output events (when Java code writes to stdout)
68-
* @param {Function} listener Callback function that handles output
69-
*/
53+
54+
// Add output listener
7055
addOutputListener(listener) {
7156
if (typeof listener === 'function') {
7257
this.outputListeners.push(listener);
7358
}
7459
return this;
7560
}
76-
77-
/**
78-
* Add a listener for error events (when Java code writes to stderr)
79-
* @param {Function} listener Callback function that handles errors
80-
*/
61+
62+
// Add error listener
8163
addErrorListener(listener) {
8264
if (typeof listener === 'function') {
8365
this.errorListeners.push(listener);
8466
}
8567
return this;
8668
}
87-
88-
/**
89-
* Write to the standard output stream (simulates System.out.print in Java)
90-
* @param {string} text Text to write to stdout
91-
*/
69+
70+
// Write to stdout
9271
writeOutput(text) {
93-
this.stdout += text;
94-
95-
// Notify all output listeners
96-
for (const listener of this.outputListeners) {
97-
listener(text);
98-
}
72+
if (this.isClosed) return this;
9973

74+
this.stdout += text;
75+
// Notify listeners
76+
this.outputListeners.forEach(listener => listener(text));
10077
return this;
10178
}
102-
103-
/**
104-
* Write to the error output stream (simulates System.err.print in Java)
105-
* @param {string} text Text to write to stderr
106-
*/
79+
80+
// Write to stderr
10781
writeError(text) {
108-
this.stderr += text;
109-
110-
// Notify all error listeners
111-
for (const listener of this.errorListeners) {
112-
listener(text);
113-
}
82+
if (this.isClosed) return this;
11483

84+
this.stderr += text;
85+
// Notify listeners
86+
this.errorListeners.forEach(listener => listener(text));
11587
return this;
11688
}
117-
118-
/**
119-
* Request input from the user (simulates Scanner.nextLine() in Java)
120-
* @param {string} prompt Prompt to display to the user
121-
* @returns {Promise<string>} Promise that resolves with user input
122-
*/
123-
requestInput(prompt = "") {
89+
90+
// Request user input
91+
async requestInput(prompt = "") {
92+
if (this.isClosed) throw new Error("Stream is closed");
93+
12494
this.inputRequested = true;
12595
this.inputPrompt = prompt;
12696

127-
return new Promise((resolve) => {
128-
this.inputResolver = resolve;
129-
130-
// Notify all input request listeners
131-
const promises = this.inputListeners.map(listener => listener(prompt));
97+
try {
98+
// Get input from listeners or global handler
99+
if (this.inputListeners.length > 0) {
100+
const promises = this.inputListeners.map(listener => listener(prompt));
101+
const results = await Promise.all(promises);
102+
const input = results.find(result => result !== undefined);
103+
104+
if (input !== undefined) {
105+
this.inputRequested = false;
106+
this.inputResolver = null;
107+
return input;
108+
}
109+
}
132110

133-
// If we have listeners, use their response
134-
if (promises.length > 0) {
135-
Promise.all(promises)
136-
.then(results => {
137-
// Use the first non-undefined result
138-
const input = results.find(result => result !== undefined);
139-
if (input !== undefined) {
140-
this.inputRequested = false;
141-
this.inputResolver = null;
142-
resolve(input);
143-
}
144-
});
145-
} else if (window.requestJavaInput) {
146-
// Fallback to global requestJavaInput if available
147-
window.requestJavaInput(prompt)
148-
.then(input => {
149-
this.inputRequested = false;
150-
this.inputResolver = null;
151-
resolve(input);
152-
});
153-
} else {
154-
// If no input handlers are available, log an error and return a default
155-
console.error("No input handlers registered and no global requestJavaInput function found");
111+
// Fallback to global handler
112+
if (window.requestJavaInput) {
113+
const input = await window.requestJavaInput(prompt);
156114
this.inputRequested = false;
157115
this.inputResolver = null;
158-
resolve("default\n");
116+
return input;
159117
}
160-
});
161-
}
162-
163-
/**
164-
* Provide input to resolve a pending input request
165-
* @param {string} input Input text from the user
166-
*/
167-
provideInput(input) {
168-
if (this.inputRequested && this.inputResolver) {
169-
const resolver = this.inputResolver;
118+
119+
throw new Error("No input handler available");
120+
} catch (error) {
170121
this.inputRequested = false;
171122
this.inputResolver = null;
172-
resolver(input);
123+
throw error;
173124
}
125+
}
126+
127+
// Provide input directly
128+
provideInput(input) {
129+
if (!this.inputRequested || !this.inputResolver) return this;
130+
131+
const resolver = this.inputResolver;
132+
this.inputRequested = false;
133+
this.inputResolver = null;
134+
resolver(input);
174135
return this;
175136
}
176-
177-
/**
178-
* Get the current contents of the standard output buffer
179-
* @returns {string} Current stdout content
180-
*/
137+
138+
// Get stdout contents
181139
getOutput() {
182140
return this.stdout;
183141
}
184-
185-
/**
186-
* Get the current contents of the error output buffer
187-
* @returns {string} Current stderr content
188-
*/
142+
143+
// Get stderr contents
189144
getError() {
190145
return this.stderr;
191146
}
192-
193-
/**
194-
* Clear all stream buffers
195-
*/
147+
148+
// Clear all buffers
196149
clearBuffers() {
197150
this.stdout = "";
198151
this.stderr = "";
152+
this.stdin = "";
199153
return this;
200154
}
201-
202-
/**
203-
* Reset the stream manager to its initial state
204-
*/
155+
156+
// Reset to initial state
205157
reset() {
206158
this.clearBuffers();
207159
this.inputRequested = false;
208160
this.inputResolver = null;
209161
this.inputPrompt = "";
162+
this.isClosed = false;
163+
return this;
164+
}
165+
166+
// Close streams
167+
close() {
168+
this.isClosed = true;
169+
this.inputRequested = false;
170+
this.inputResolver = null;
210171
return this;
211172
}
212173
}
213174

214-
// Create a singleton instance
175+
// Create singleton instance
215176
const javaStreams = new JavaStreamManager();
216177

217178
export { javaStreams };

0 commit comments

Comments
 (0)