⭐️ A friendly language for building type-safe, scalable systems!

Fix singleton bug

Changed files
+67 -7
compiler-core
templates
test-output
+15 -7
compiler-core/templates/echo.mjs
··· 80 80 return "//js(circular reference)"; 81 81 } 82 82 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) { 89 93 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; 91 99 } 92 100 93 101 #object(v) {
+3
test-output/cases/echo_singleton/gleam.toml
··· 1 + name = "echo_circular_reference" 2 + version = "1.0.0" 3 + target = "javascript"
+11
test-output/cases/echo_singleton/src/main.gleam
··· 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 + }
+7
test-output/cases/echo_singleton/src/main_ffi.mjs
··· 1 + import { Thing } from "./thing.mjs"; 2 + 3 + const it = new Thing(); 4 + 5 + export function singleton() { 6 + return it; 7 + }
+3
test-output/cases/echo_singleton/src/thing.gleam
··· 1 + pub type Thing { 2 + Thing 3 + }
+5
test-output/src/tests/echo.rs
··· 200 200 fn echo_circular_reference() { 201 201 assert_echo!(Target::JavaScript, "echo_circular_reference"); 202 202 } 203 + 204 + #[test] 205 + fn echo_singleton() { 206 + assert_echo!("echo_singleton"); 207 + }
+23
test-output/src/tests/snapshots/test_output__tests__echo__echo_singleton.snap
··· 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)