Skip to content

Commit b8088ae

Browse files
authored
Merge pull request #100 from Distributive-Network/bugfix/issue-90-console-this
console.js - use own props instead of inheritance
2 parents 8a352b9 + 117d00f commit b8088ae

File tree

5 files changed

+94
-16
lines changed

5 files changed

+94
-16
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ dist
1717
*.dylib
1818
*.dll
1919
*.pyd
20+
*~
2021
# Virtual Environment
2122
.venv/

python/pythonmonkey/builtin_modules/console.js

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ const { customInspectSymbol, format } = require("util");
1111
/**
1212
* @see https://developer.mozilla.org/en-US/docs/Web/API/Console_API
1313
*/
14-
// TODO (Tom Tang): It's easier to copy implementations from Node.js version 8 than Node.js 20,
15-
// see https://github.com/nodejs/node/blob/v8.17.0/lib/console.js
1614
// TODO (Tom Tang): adhere https://console.spec.whatwg.org/
1715
class Console {
1816
/** @type {WriteFn} */
@@ -49,8 +47,15 @@ class Console {
4947
ignoreErrors = true;
5048
options = { stdout, stderr, ignoreErrors };
5149
}
50+
5251
this.#writeToStdout = options.stdout.write;
5352
this.#writeToStderr = options.stderr.write;
53+
54+
this.log = (...args) => this.#writeToStdout(this.#formatToStr(...args));
55+
this.debug = (...args) => this.#writeToStdout(this.#formatToStr(...args));
56+
this.info = (...args) => this.#writeToStdout(this.#formatToStr(...args));
57+
this.warn = (...args) => this.#writeToStderr(this.#formatToStr(...args));
58+
this.error = (...args) => this.#writeToStderr(this.#formatToStr(...args));
5459
}
5560

5661
/**
@@ -60,14 +65,6 @@ class Console {
6065
return format(...args) + "\n"
6166
}
6267

63-
log(...args) {
64-
this.#writeToStdout(this.#formatToStr(...args))
65-
}
66-
67-
warn(...args) {
68-
this.#writeToStderr(this.#formatToStr(...args))
69-
}
70-
7168
// TODO (Tom Tang): implement more methods
7269

7370
/**
@@ -83,11 +80,6 @@ class Console {
8380
static customInspectSymbol = customInspectSymbol;
8481
}
8582

86-
// https://github.com/nodejs/node/blob/v20.1.0/lib/internal/console/constructor.js#L681-L685
87-
Console.prototype.debug = Console.prototype.log;
88-
Console.prototype.info = Console.prototype.log;
89-
Console.prototype.error = Console.prototype.warn;
90-
9183
if (!globalThis.console) {
9284
globalThis.console = new Console(
9385
python.stdout /* sys.stdout */,

tests/js/console-stdio.bash

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#! /bin/bash
2+
#
3+
# @file console-stdio.bash
4+
# A peter-jr test which ensures that the console object uses the right file descriptors.
5+
#
6+
# @author Wes Garland, [email protected]
7+
# @date July 2023
8+
9+
set -u
10+
set -o pipefail
11+
12+
panic()
13+
{
14+
echo "FAIL: $*" >&2
15+
exit 2
16+
}
17+
18+
cd `dirname "$0"` || panic "could not change to test directory"
19+
20+
"${PMJS:-../../pmjs}" \
21+
-e 'console.log("stdout")' \
22+
-e 'console.debug("stdout")' \
23+
-e 'console.info("stdout")' \
24+
< /dev/null \
25+
| grep -c '^stdout$' \
26+
| while read qty
27+
do
28+
echo "stdout: $qty"
29+
[ "$qty" != "3" ] && panic qty should not be $qty
30+
break
31+
done || exit $?
32+
33+
"${PMJS:-../../pmjs}" \
34+
-e 'console.error("stderr")' \
35+
-e 'console.warn("stderr")' \
36+
< /dev/null 2>&1 \
37+
| grep -c '^stderr$' \
38+
| while read qty
39+
do
40+
echo "stderr: $qty"
41+
[ "$qty" != "2" ] && panic qty should not be $qty
42+
break
43+
done || exit $?
44+
45+
echo "done"

tests/js/console-this.simple

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @file console-this.simple
3+
*
4+
* Test which ensures that the Console objects have unique own properties and bound this.
5+
* Regression test for issue #90.
6+
*
7+
* @author Wes Garland, [email protected]
8+
* @date July 2023
9+
*/
10+
'use strict';
11+
12+
var exitCode = 0;
13+
function assert(test)
14+
{
15+
if (!test)
16+
{
17+
exitCode = 1;
18+
console.error(new Error('Assertion failure'));
19+
}
20+
}
21+
22+
const c = console;
23+
24+
assert(c.log !== c.debug);
25+
assert(c.log !== c.warn);
26+
assert(c.log !== c.info);
27+
assert(c.warn !== c.error);
28+
assert(typeof c.log === 'function');
29+
assert(typeof c.debug === 'function');
30+
assert(typeof c.info === 'function');
31+
assert(typeof c.error === 'function');
32+
assert(typeof c.warn === 'function');
33+
assert(c.hasOwnProperty('debug'));
34+
assert(c.hasOwnProperty('log'));
35+
assert(c.hasOwnProperty('info'));
36+
assert(c.hasOwnProperty('warn'));
37+
assert(c.hasOwnProperty('error'));
38+
39+
c.log('test done'); /* throws if wrong this, issue #90 */
40+
python.exit(exitCode);

tests/js/is-compilable-unit.simple

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @file is-compilabe-unit.simple
2+
* @file is-compilable-unit.simple
33
* Simple test which ensures that pm.isCompilableUnit works from JS as expected
44
* written in Python instead of JavaScript
55
* @author Wes Garland, [email protected]

0 commit comments

Comments
 (0)