Skip to content

Commit 20cf6a5

Browse files
committed
Fix singleton printing bug
1 parent 0cfa7f9 commit 20cf6a5

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

src/gleam_stdlib.mjs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -701,14 +701,22 @@ class Inspector {
701701
return "//js(circular reference)";
702702
}
703703

704-
if (Array.isArray(v))
705-
return `#(${v.map((v) => this.inspect(v)).join(", ")})`;
706-
if (v instanceof List) return this.#list(v);
707-
if (v instanceof CustomType) return this.#customType(v);
708-
if (v instanceof Dict) return this.#dict(v);
709-
if (v instanceof Set)
704+
let printed;
705+
if (Array.isArray(v)) {
706+
printed = `#(${v.map((v) => this.inspect(v)).join(", ")})`;
707+
} else if (v instanceof List) {
708+
printed = this.#list(v);
709+
} else if (v instanceof CustomType) {
710+
printed = this.#customType(v);
711+
} else if (v instanceof Dict) {
712+
printed = this.#dict(v);
713+
} else if (v instanceof Set) {
710714
return `//js(Set(${[...v].map((v) => this.inspect(v)).join(", ")}))`;
711-
return this.#object(v);
715+
} else {
716+
printed = this.#object(v);
717+
}
718+
this.#references.delete(v);
719+
return printed;
712720
}
713721

714722
#object(v) {

test/gleam/string_test.gleam

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,6 +1252,16 @@ pub fn inspect_circular_reference_test() {
12521252
== "#(1, 2, 3, //js(circular reference))"
12531253
}
12541254

1255+
@target(javascript)
1256+
@external(javascript, "../gleam_stdlib_test_ffi.mjs", "singleton_object")
1257+
fn singleton_object() -> Dynamic
1258+
1259+
@target(javascript)
1260+
pub fn inspect_singleton_test() {
1261+
assert string.inspect(#(singleton_object(), singleton_object()))
1262+
== "#(//js({ \"a\": 1 }), //js({ \"a\": 1 }))"
1263+
}
1264+
12551265
@target(javascript)
12561266
@external(javascript, "../gleam_stdlib_test_ffi.mjs", "js_error")
12571267
fn js_error() -> Dynamic

test/gleam_stdlib_test_ffi.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ export function map(items) {
2727
return object;
2828
}
2929

30+
const singleton = { a: 1 };
31+
32+
export function singleton_object() {
33+
return singleton;
34+
}
35+
3036
export function circular_reference() {
3137
const x = [1, 2, 3];
3238
x.push(x);

0 commit comments

Comments
 (0)