Skip to content

Commit

Permalink
rename echo functions to avoid conflicts with modules
Browse files Browse the repository at this point in the history
  • Loading branch information
giacomocavalieri committed Oct 31, 2024
1 parent 6ec97f2 commit be177a1
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
source: compiler-core/src/javascript/tests/echo.rs
expression: "\nimport other/inspect\n\npub fn main() {\n echo inspect.x\n}\n"
---
----- SOURCE CODE

import other/inspect

pub fn main() {
echo inspect.x
}


----- COMPILED JAVASCRIPT
import * as $stdlib$dict from "../../gleam_stdlib/dict.mjs";
import * as $inspect from "../../other/other/inspect.mjs";
import {
BitArray as $BitArray,
List as $List,
UtfCodepoint as $UtfCodepoint,
CustomType as $CustomType,
} from "../gleam.mjs";

export function main() {
return echo($inspect.x);
}

// ...omitted code from `templates/echo.mjs`...
34 changes: 17 additions & 17 deletions compiler-core/templates/echo.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function echo(value) {
const string = $inspect(value);
const string = echo$inspect(value);
if (typeof process === "object" && process.stderr?.write) {
// If we're in Node.js, use `stderr`
process.stderr.write(string + "\n");
Expand All @@ -14,7 +14,7 @@ function echo(value) {
return value;
}

function $inspectString(str) {
function echo$inspectString(str) {
let new_str = '"';
for (let i = 0; i < str.length; i++) {
let char = str[i];
Expand All @@ -34,64 +34,64 @@ function $inspectString(str) {
return new_str;
}

function $inspectDict(map) {
function echo$inspectDict(map) {
let body = "dict.from_list([";
let first = true;
map.forEach((value, key) => {
if (!first) body = body + ", ";
body = body + "#(" + $inspect(key) + ", " + $inspect(value) + ")";
body = body + "#(" + echo$inspect(key) + ", " + echo$inspect(value) + ")";
first = false;
});
return body + "])";
}

function $inspectCustomType(record) {
function echo$inspectCustomType(record) {
const props = Object.keys(record)
.map((label) => {
const value = $inspect(record[label]);
const value = echo$inspect(record[label]);
return isNaN(parseInt(label)) ? `${label}: ${value}` : value;
})
.join(", ");
return props ? `${record.constructor.name}(${props})` : record.constructor.name;
}

function $inspectObject(v) {
function echo$inspectObject(v) {
const name = Object.getPrototypeOf(v)?.constructor?.name || "Object";
const props = [];
for (const k of Object.keys(v)) {
props.push(`${$inspect(k)}: ${$inspect(v[k])}`);
props.push(`${echo$inspect(k)}: ${echo$inspect(v[k])}`);
}
const body = props.length ? " " + props.join(", ") + " " : "";
const head = name === "Object" ? "" : name + " ";
return `//js(${head}{${body}})`;
}

function $inspect(v) {
function echo$inspect(v) {
const t = typeof v;
if (v === true) return "True";
if (v === false) return "False";
if (v === null) return "//js(null)";
if (v === undefined) return "Nil";
if (t === "string") return $inspectString(v);
if (t === "string") return echo$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 (Array.isArray(v)) return `#(${v.map(echo$inspect).join(", ")})`;
if (v instanceof $List) return `[${v.toArray().map(echo$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 $CustomType) return echo$inspectCustomType(v);
if (echo$isDict(v)) return echo$inspectDict(v);
if (v instanceof Set) return `//js(Set(${[...v].map(echo$inspect).join(", ")}))`;
if (v instanceof RegExp) return `//js(${v})`;
if (v instanceof Date) return `//js(Date("${v.toISOString()}"))`;
if (v instanceof Function) {
const args = [];
for (const i of Array(v.length).keys()) args.push(String.fromCharCode(i + 97));
return `//fn(${args.join(", ")}) { ... }`;
}
return $inspectObject(v);
return echo$inspectObject(v);
}

function $isDict(value) {
function echo$isDict(value) {
try {
// We can only check if an object is a stdlib Dict if it is one of the
// project's dependencies.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
source: test-echo-output/src/generated_tests.rs
expression: "./cases/echo_importing_module_named_inspect"
---
--- CODE
import inspect

pub fn main() {
echo inspect.x
}

--- OUTPUT ON ERLANG TARGET
Nil

--- OUTPUT ON JAVASCRIPT TARGET
Nil

0 comments on commit be177a1

Please sign in to comment.