Skip to content

Commit

Permalink
properly handle Dicts
Browse files Browse the repository at this point in the history
  • Loading branch information
giacomocavalieri committed Oct 26, 2024
1 parent bb90591 commit 1bd4a95
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 23 deletions.
11 changes: 9 additions & 2 deletions compiler-core/src/build/package_compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,11 @@ where
TypeScriptDeclarations::None
};

JavaScript::new(&self.out, typescript, prelude_location, self.target_support)
.render(&self.io, modules)?;
JavaScript::new(&self.out, typescript, prelude_location, self.target_support).render(
&self.io,
modules,
self.stdlib_is_a_dependency(),
)?;

if self.copy_native_files {
self.copy_project_native_files(&self.out, &mut written)?;
Expand Down Expand Up @@ -427,6 +430,10 @@ where

Ok(())
}

fn stdlib_is_a_dependency(&self) -> bool {
self.config.dependencies.contains_key("gleam_stdlib")
}
}

fn analyse(
Expand Down
11 changes: 9 additions & 2 deletions compiler-core/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,18 @@ impl<'a> JavaScript<'a> {
}
}

pub fn render(&self, writer: &impl FileSystemWriter, modules: &[Module]) -> Result<()> {
pub fn render(
&self,
writer: &impl FileSystemWriter,
modules: &[Module],
stdlib_is_a_dependency: bool,
) -> Result<()> {
for module in modules {
let js_name = module.name.clone();
if self.typescript == TypeScriptDeclarations::Emit {
self.ts_declaration(writer, module, &js_name)?;
}
self.js_module(writer, module, &js_name)?
self.js_module(writer, module, &js_name, stdlib_is_a_dependency)?
}
self.write_prelude(writer)?;
Ok(())
Expand Down Expand Up @@ -235,6 +240,7 @@ impl<'a> JavaScript<'a> {
writer: &impl FileSystemWriter,
module: &Module,
js_name: &str,
stdblib_is_a_dependency: bool,
) -> Result<()> {
let name = format!("{js_name}.mjs");
let path = self.output_directory.join(name);
Expand All @@ -246,6 +252,7 @@ impl<'a> JavaScript<'a> {
&module.code,
self.target_support,
self.typescript,
stdblib_is_a_dependency,
);
tracing::debug!(name = ?js_name, "Generated js module");
writer.write(&path, &output?)
Expand Down
48 changes: 29 additions & 19 deletions compiler-core/src/javascript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub struct Generator<'a> {
current_module_name_segments_count: usize,
target_support: TargetSupport,
typescript: TypeScriptDeclarations,
stdlib_is_a_dependency: bool,
}

impl<'a> Generator<'a> {
Expand All @@ -53,6 +54,7 @@ impl<'a> Generator<'a> {
module: &'a TypedModule,
target_support: TargetSupport,
typescript: TypeScriptDeclarations,
stdlib_is_a_dependency: bool,
) -> Self {
let current_module_name_segments_count = module.name.split('/').count();

Expand All @@ -64,6 +66,7 @@ impl<'a> Generator<'a> {
module_scope: Default::default(),
target_support,
typescript,
stdlib_is_a_dependency,
}
}

Expand Down Expand Up @@ -170,18 +173,18 @@ impl<'a> Generator<'a> {
};

let echo = if self.tracker.echo_used {
// TODO! Check stdlib is among the dependencies or this will fail at
// runtime with an exception.
self.register_import(
&mut imports,
"gleam_stdlib",
"dict",
&Some((
AssignName::Variable("stdlib$dict".into()),
SrcSpan::default(),
)),
&[],
);
if self.stdlib_is_a_dependency {
self.register_import(
&mut imports,
"gleam_stdlib",
"dict",
&Some((
AssignName::Variable("stdlib$dict".into()),
SrcSpan::default(),
)),
&[],
);
}
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"));
Expand Down Expand Up @@ -591,14 +594,21 @@ pub fn module(
src: &EcoString,
target_support: TargetSupport,
typescript: TypeScriptDeclarations,
stdlib_is_a_dependency: bool,
) -> Result<String, crate::Error> {
let document = Generator::new(line_numbers, module, target_support, typescript)
.compile()
.map_err(|error| crate::Error::JavaScript {
path: path.to_path_buf(),
src: src.clone(),
error,
})?;
let document = Generator::new(
line_numbers,
module,
target_support,
typescript,
stdlib_is_a_dependency,
)
.compile()
.map_err(|error| crate::Error::JavaScript {
path: path.to_path_buf(),
src: src.clone(),
error,
})?;
Ok(document.to_pretty_string(80))
}

Expand Down
2 changes: 2 additions & 0 deletions compiler-core/src/javascript/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,15 @@ pub fn compile(src: &str, deps: Vec<(&str, &str, &str)>) -> TypedModule {
pub fn compile_js(src: &str, deps: Vec<(&str, &str, &str)>) -> Result<String, crate::Error> {
let ast = compile(src, deps);
let line_numbers = LineNumbers::new(src);
let stdlib_is_a_dependency = true;
let output = module(
&ast,
&line_numbers,
Utf8Path::new(""),
&"".into(),
TargetSupport::Enforced,
TypeScriptDeclarations::None,
stdlib_is_a_dependency,
)?;

Ok(output.replace(
Expand Down

0 comments on commit 1bd4a95

Please sign in to comment.