Skip to content

Commit 1808c07

Browse files
committed
refactor: move commands to client implementation
Signed-off-by: Snehil Shah <snehilshah.989@gmail.com> --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- type: pre_push_report description: Results of running various checks prior to pushing changes. report: - task: run_javascript_examples status: passed - task: run_c_examples status: na - task: run_cpp_examples status: na - task: run_javascript_readme_examples status: passed - task: run_c_benchmarks status: na - task: run_cpp_benchmarks status: na - task: run_fortran_benchmarks status: na - task: run_javascript_benchmarks status: passed - task: run_julia_benchmarks status: na - task: run_python_benchmarks status: na - task: run_r_benchmarks status: na - task: run_javascript_tests status: passed ---
1 parent 9367245 commit 1808c07

File tree

128 files changed

+710
-1035
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

128 files changed

+710
-1035
lines changed
Lines changed: 271 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,271 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# REPL
22+
23+
> Base class for Read-Eval-Print Loop (REPL) environment.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
A Read-Eval-Print Loop (REPL) environment is an interactive programming environment which takes individual user inputs (e.g., single expressions), evaluates those inputs, and returns the result. Accordingly, a program written in a REPL environment is executed piecewise and sequentially.
30+
31+
REPL environments find common use in exploratory programming, prototyping, and debugging.
32+
33+
The REPL environment exposed here is available both as a standalone application and as a library which is embeddable in other libraries and applications.
34+
35+
</section>
36+
37+
<!-- /.intro -->
38+
39+
<!-- Package usage documentation. -->
40+
41+
<section class="usage">
42+
43+
## Usage
44+
45+
```javascript
46+
var REPL = require( '@stdlib/repl/base' );
47+
```
48+
49+
#### REPL( \[options] )
50+
51+
Returns a `REPL` instance.
52+
53+
```javascript
54+
// Create a new REPL:
55+
var repl = new REPL();
56+
57+
// ...
58+
59+
// Execute a command:
60+
repl.emit( 'input', '2 + 3' );
61+
62+
// ...
63+
64+
// Close the REPL:
65+
repl.close();
66+
```
67+
68+
The function accepts the following `options`:
69+
70+
- **input**: input (readable) stream. Default: [`stdin`][@stdlib/streams/node/stdin].
71+
- **output**: output (writable) stream. Default: [`stdout`][@stdlib/streams/node/stdout].
72+
- **error**: error (writable) stream. Default: [`stderr`][@stdlib/streams/node/stderr].
73+
- **sandbox**: boolean indicating whether to run a REPL in a sandboxed context. Default: `false`.
74+
- **timeout**: number of milliseconds to execute a command before terminating execution. Default: `4294967295`.
75+
- **save**: file path specifying where to save REPL command history.
76+
- **log**: file path specifying where to save REPL commands and printed output.
77+
- **quiet**: boolean indicating whether log information, confirmation messages, and other possible REPL diagnostics should be silenced. Default: `false`.
78+
79+
#### REPL.prototype.createContext()
80+
81+
Returns a REPL context.
82+
83+
```javascript
84+
// Create a new REPL:
85+
var repl = new REPL();
86+
87+
// ...
88+
89+
// Return a new REPL context:
90+
var ctx = repl.createContext();
91+
92+
// ...
93+
94+
// Close the REPL:
95+
repl.close();
96+
```
97+
98+
#### REPL.prototype.resetContext()
99+
100+
Resets a REPL's execution context.
101+
102+
```javascript
103+
// Create a new REPL:
104+
var repl = new REPL();
105+
106+
// ...
107+
108+
// Reset the REPL context:
109+
repl.resetContext();
110+
111+
// ...
112+
113+
// Close the REPL:
114+
repl.close();
115+
```
116+
117+
#### REPL.prototype.clearHistory()
118+
119+
Clears a REPL's history.
120+
121+
```javascript
122+
// Create a new REPL:
123+
var repl = new REPL();
124+
125+
// ...
126+
127+
// Clear the REPL history:
128+
repl.clearHistory();
129+
130+
// ...
131+
132+
// Close the REPL:
133+
repl.close();
134+
```
135+
136+
#### REPL.prototype.clearUserDocs()
137+
138+
Clears user-defined documentation.
139+
140+
```javascript
141+
// Create a new REPL:
142+
var repl = new REPL();
143+
144+
// ...
145+
146+
// Clear user-defined documentation:
147+
repl.clearUserDocs();
148+
149+
// ...
150+
151+
// Close the REPL:
152+
repl.close();
153+
```
154+
155+
#### REPL.prototype.reset()
156+
157+
Resets a REPL.
158+
159+
```javascript
160+
// Create a new REPL:
161+
var repl = new REPL();
162+
163+
// ...
164+
165+
// Reset the REPL:
166+
repl.reset();
167+
168+
// ...
169+
170+
// Close the REPL:
171+
repl.close();
172+
```
173+
174+
#### REPL.prototype.close()
175+
176+
Closes a REPL.
177+
178+
```javascript
179+
// Create a new REPL:
180+
var repl = new REPL();
181+
182+
// ...
183+
184+
// Close the REPL:
185+
repl.close();
186+
```
187+
188+
* * *
189+
190+
</section>
191+
192+
<!-- /.usage -->
193+
194+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
195+
196+
<section class="notes">
197+
198+
</section>
199+
200+
<!-- /.notes -->
201+
202+
<!-- Package usage examples. -->
203+
204+
* * *
205+
206+
<section class="examples">
207+
208+
## Examples
209+
210+
<!-- eslint no-undef: "error" -->
211+
212+
```javascript
213+
var REPL = require( '@stdlib/repl/base' );
214+
215+
function onCommand( cmd, success, res ) {
216+
console.log( cmd + ' = ' + res.toString() );
217+
}
218+
219+
// Create a new REPL:
220+
var repl = new REPL();
221+
repl.on( 'command', onCommand );
222+
223+
// Execute a command:
224+
repl.emit( 'input', '3 + 2' );
225+
226+
// Close the REPL:
227+
repl.close();
228+
console.log( 'REPL closed.' );
229+
```
230+
231+
</section>
232+
233+
<!-- /.examples -->
234+
235+
* * *
236+
237+
<section class="notes">
238+
239+
</section>
240+
241+
<!-- /.notes -->
242+
243+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
244+
245+
<section class="references">
246+
247+
</section>
248+
249+
<!-- /.references -->
250+
251+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
252+
253+
<section class="related">
254+
255+
</section>
256+
257+
<!-- /.related -->
258+
259+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
260+
261+
<section class="links">
262+
263+
[@stdlib/streams/node/stdin]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/streams/node/stdin
264+
265+
[@stdlib/streams/node/stdout]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/streams/node/stdout
266+
267+
[@stdlib/streams/node/stderr]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/streams/node/stderr
268+
269+
</section>
270+
271+
<!-- /.links -->
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2019 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var inspectSinkStream = require( '@stdlib/streams/node/inspect-sink' );
25+
var randu = require( '@stdlib/random/streams/randu' );
26+
var noop = require( '@stdlib/utils/noop' );
27+
var pkg = require( './../package.json' ).name;
28+
var REPL = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( pkg+'::new', function benchmark( b ) {
34+
var sopts;
35+
var opts;
36+
var r;
37+
var i;
38+
39+
opts = {
40+
'output': inspectSinkStream( noop ),
41+
'error': inspectSinkStream( noop )
42+
};
43+
sopts = {
44+
'iter': 1
45+
};
46+
47+
b.tic();
48+
for ( i = 0; i < b.iterations; i++ ) {
49+
opts.input = randu( sopts ); // Note: this is slow, but should not be rate-limiting
50+
r = new REPL( opts );
51+
if ( typeof r !== 'object' ) {
52+
b.fail( 'should return an object' );
53+
}
54+
r.close();
55+
}
56+
b.toc();
57+
if ( !( r instanceof REPL ) ) {
58+
b.fail( 'should return an instance' );
59+
}
60+
b.pass( 'benchmark finished' );
61+
b.end();
62+
});
63+
64+
bench( pkg+'::no_new', function benchmark( b ) {
65+
var sopts;
66+
var repl;
67+
var opts;
68+
var r;
69+
var i;
70+
71+
repl = REPL;
72+
opts = {
73+
'output': inspectSinkStream( noop ),
74+
'error': inspectSinkStream( noop )
75+
};
76+
sopts = {
77+
'iter': 1
78+
};
79+
80+
b.tic();
81+
for ( i = 0; i < b.iterations; i++ ) {
82+
opts.input = randu( sopts ); // Note: this is slow, but should not be rate-limiting
83+
r = repl( opts );
84+
if ( typeof r !== 'object' ) {
85+
b.fail( 'should return an object' );
86+
}
87+
r.close();
88+
}
89+
b.toc();
90+
if ( !( r instanceof REPL ) ) {
91+
b.fail( 'should return an instance' );
92+
}
93+
b.pass( 'benchmark finished' );
94+
b.end();
95+
});

0 commit comments

Comments
 (0)