Skip to content

Commit 84a976e

Browse files
committed
Fix singleton bug
1 parent 32d9273 commit 84a976e

File tree

7 files changed

+67
-7
lines changed

7 files changed

+67
-7
lines changed

compiler-core/templates/echo.mjs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,22 @@ class Echo$Inspector {
8080
return "//js(circular reference)";
8181
}
8282

83-
if (Array.isArray(v))
84-
return `#(${v.map((v) => this.inspect(v)).join(", ")})`;
85-
if (v instanceof $List) return this.#list(v);
86-
if (v instanceof $CustomType) return this.#customType(v);
87-
if (this.#isDict(v)) return this.#dict(v);
88-
if (v instanceof Set)
83+
let printed;
84+
if (Array.isArray(v)) {
85+
printed = `#(${v.map((v) => this.inspect(v)).join(", ")})`;
86+
} else if (v instanceof $List) {
87+
printed = this.#list(v);
88+
} else if (v instanceof $CustomType) {
89+
printed = this.#customType(v);
90+
} else if (this.#isDict(v)) {
91+
printed = this.#dict(v);
92+
} else if (v instanceof Set) {
8993
return `//js(Set(${[...v].map((v) => this.inspect(v)).join(", ")}))`;
90-
return this.#object(v);
94+
} else {
95+
printed = this.#object(v);
96+
}
97+
this.#references.delete(v);
98+
return printed;
9199
}
92100

93101
#object(v) {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
name = "echo_circular_reference"
2+
version = "1.0.0"
3+
target = "javascript"
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import thing
2+
3+
pub fn main() {
4+
echo #(1, singleton(), singleton())
5+
Nil
6+
}
7+
8+
@external(javascript, "./main_ffi.mjs", "singleton")
9+
fn singleton() -> thing.Thing {
10+
thing.Thing
11+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Thing } from "./thing.mjs";
2+
3+
const it = new Thing();
4+
5+
export function singleton() {
6+
return it;
7+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub type Thing {
2+
Thing
3+
}

test-output/src/tests/echo.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,3 +200,8 @@ fn echo_non_record_atom_tag() {
200200
fn echo_circular_reference() {
201201
assert_echo!(Target::JavaScript, "echo_circular_reference");
202202
}
203+
204+
#[test]
205+
fn echo_singleton() {
206+
assert_echo!("echo_singleton");
207+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
source: test-output/src/tests/echo.rs
3+
assertion_line: 206
4+
expression: output
5+
snapshot_kind: text
6+
---
7+
--- main.gleam ----------------------
8+
import thing
9+
10+
pub fn main() {
11+
echo #(1, singleton(), singleton())
12+
Nil
13+
}
14+
15+
@external(javascript, "./main_ffi.mjs", "singleton")
16+
fn singleton() -> thing.Thing {
17+
thing.Thing
18+
}
19+
20+
21+
--- gleam run output ----------------
22+
src/main.gleam:4
23+
#(1, Thing, Thing)

0 commit comments

Comments
 (0)