Skip to content

Commit

Permalink
avoid clases with user defined types
Browse files Browse the repository at this point in the history
  • Loading branch information
giacomocavalieri committed Oct 25, 2024
1 parent 6ff16c8 commit 40a1a38
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 16 deletions.
8 changes: 4 additions & 4 deletions compiler-core/src/javascript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,10 @@ impl<'a> Generator<'a> {
)),
&[],
);
self.register_prelude_usage(&mut imports, "BitArray", None);
self.register_prelude_usage(&mut imports, "List", None);
self.register_prelude_usage(&mut imports, "UtfCodepoint", None);
self.register_prelude_usage(&mut imports, "CustomType", None);
self.register_prelude_usage(&mut imports, "BitArray", Some("$BitArray"));
self.register_prelude_usage(&mut imports, "List", Some("$List"));
self.register_prelude_usage(&mut imports, "UtfCodepoint", Some("$UtfCodepoint"));
self.register_prelude_usage(&mut imports, "CustomType", Some("$CustomType"));
docvec![line(), std::include_str!("../templates/echo.mjs"), line()]
} else {
nil()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ source: compiler-core/src/javascript/tests/echo.rs
expression: "\npub fn main() {\n [1, 2, 3]\n |> echo\n |> wibble\n}\n\npub fn wibble(n) { n }\n"
---
import * as $stdlib$dict from "../../gleam_stdlib/dict.mjs";
import { toList, BitArray, List, UtfCodepoint, CustomType } from "../gleam.mjs";
import {
toList,
BitArray as $BitArray,
List as $List,
UtfCodepoint as $UtfCodepoint,
CustomType as $CustomType,
} from "../gleam.mjs";

export function wibble(n) {
return n;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ source: compiler-core/src/javascript/tests/echo.rs
expression: "\npub fn main() {\n echo {\n Nil\n 1\n }\n}\n"
---
import * as $stdlib$dict from "../../gleam_stdlib/dict.mjs";
import { BitArray, List, UtfCodepoint, CustomType } from "../gleam.mjs";
import {
BitArray as $BitArray,
List as $List,
UtfCodepoint as $UtfCodepoint,
CustomType as $CustomType,
} from "../gleam.mjs";

export function main() {
return echo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ source: compiler-core/src/javascript/tests/echo.rs
expression: "\npub fn main() {\n echo case 1 {\n _ -> 2\n }\n}\n"
---
import * as $stdlib$dict from "../../gleam_stdlib/dict.mjs";
import { BitArray, List, UtfCodepoint, CustomType } from "../gleam.mjs";
import {
BitArray as $BitArray,
List as $List,
UtfCodepoint as $UtfCodepoint,
CustomType as $CustomType,
} from "../gleam.mjs";

export function main() {
return echo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ source: compiler-core/src/javascript/tests/echo.rs
expression: "\npub fn main() {\n echo wibble(1, 2)\n}\n\nfn wibble(n: Int, m: Int) { n + m }\n"
---
import * as $stdlib$dict from "../../gleam_stdlib/dict.mjs";
import { BitArray, List, UtfCodepoint, CustomType } from "../gleam.mjs";
import {
BitArray as $BitArray,
List as $List,
UtfCodepoint as $UtfCodepoint,
CustomType as $CustomType,
} from "../gleam.mjs";

function wibble(n, m) {
return n + m;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ source: compiler-core/src/javascript/tests/echo.rs
expression: "\npub fn main() {\n echo panic\n}\n"
---
import * as $stdlib$dict from "../../gleam_stdlib/dict.mjs";
import { makeError, BitArray, List, UtfCodepoint, CustomType } from "../gleam.mjs";
import {
makeError,
BitArray as $BitArray,
List as $List,
UtfCodepoint as $UtfCodepoint,
CustomType as $CustomType,
} from "../gleam.mjs";

export function main() {
return echo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ source: compiler-core/src/javascript/tests/echo.rs
expression: "\npub fn main() {\n echo 1\n}\n"
---
import * as $stdlib$dict from "../../gleam_stdlib/dict.mjs";
import { BitArray, List, UtfCodepoint, CustomType } from "../gleam.mjs";
import {
BitArray as $BitArray,
List as $List,
UtfCodepoint as $UtfCodepoint,
CustomType as $CustomType,
} from "../gleam.mjs";

export function main() {
return echo(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ source: compiler-core/src/javascript/tests/echo.rs
expression: "\npub fn main() {\n [1, 2, 3]\n |> echo\n |> wibble\n |> echo\n |> wibble\n |> echo\n}\n\npub fn wibble(n) { n }\n"
---
import * as $stdlib$dict from "../../gleam_stdlib/dict.mjs";
import { toList, BitArray, List, UtfCodepoint, CustomType } from "../gleam.mjs";
import {
toList,
BitArray as $BitArray,
List as $List,
UtfCodepoint as $UtfCodepoint,
CustomType as $CustomType,
} from "../gleam.mjs";

export function wibble(n) {
return n;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ source: compiler-core/src/javascript/tests/echo.rs
expression: "\npub fn main() {\n echo 1\n echo 2\n}\n"
---
import * as $stdlib$dict from "../../gleam_stdlib/dict.mjs";
import { BitArray, List, UtfCodepoint, CustomType } from "../gleam.mjs";
import {
BitArray as $BitArray,
List as $List,
UtfCodepoint as $UtfCodepoint,
CustomType as $CustomType,
} from "../gleam.mjs";

export function main() {
echo(1);
Expand Down
8 changes: 4 additions & 4 deletions compiler-core/templates/echo.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ function $inspect(v) {
if (t === "string") return $inspectString(v);
if (t === "bigint" || t === "number") return v.toString();
if (Array.isArray(v)) return `#(${v.map($inspect).join(", ")})`;
if (v instanceof List) return `[${v.toArray().map($inspect).join(", ")}]`;
if (v instanceof UtfCodepoint) return `//utfcodepoint(${String.fromCodePoint(v.value)})`;
if (v instanceof BitArray) return `<<${Array.from(v.buffer).join(", ")}>>`;
if (v instanceof CustomType) return $inspectCustomType(v);
if (v instanceof $List) return `[${v.toArray().map($inspect).join(", ")}]`;
if (v instanceof $UtfCodepoint) return `//utfcodepoint(${String.fromCodePoint(v.value)})`;
if (v instanceof $BitArray) return `<<${Array.from(v.buffer).join(", ")}>>`;
if (v instanceof $CustomType) return $inspectCustomType(v);
if ($isDict(v)) return $inspectDict(v);
if (v instanceof Set) return `//js(Set(${[...v].map($inspect).join(", ")}))`;
if (v instanceof RegExp) return `//js(${v})`;
Expand Down

0 comments on commit 40a1a38

Please sign in to comment.