Skip to content

Commit e679f11

Browse files
authored
Use enum for source inclusion and compression (#997)
1 parent e0aeb4b commit e679f11

5 files changed

Lines changed: 92 additions & 84 deletions

File tree

crates/cli/src/commands.rs

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,16 @@ where
171171
}
172172
}
173173

174+
#[derive(Debug, Clone, Copy, PartialEq)]
175+
pub enum Source {
176+
/// Omit the source code.
177+
Omitted,
178+
/// Compress the source code.
179+
Compressed,
180+
/// Include source code but don't compress it.
181+
Uncompressed,
182+
}
183+
174184
/// Code generation option group.
175185
/// This group gets configured from the [`CodegenOption`] enum.
176186
//
@@ -180,8 +190,7 @@ where
180190
pub struct CodegenOptionGroup {
181191
pub dynamic: bool,
182192
pub wit: WitOptions,
183-
pub source: bool,
184-
pub source_compression: bool,
193+
pub source: Source,
185194
pub plugin: Option<PathBuf>,
186195
}
187196

@@ -190,8 +199,7 @@ impl Default for CodegenOptionGroup {
190199
Self {
191200
dynamic: false,
192201
wit: WitOptions::default(),
193-
source: true,
194-
source_compression: true,
202+
source: Source::Compressed,
195203
plugin: None,
196204
}
197205
}
@@ -209,14 +217,12 @@ option_group! {
209217
/// Optional WIT world name for WIT file. Must be specified if WIT is
210218
/// file path is specified.
211219
WitWorld(String),
212-
/// Embed the JavaScript source in a custom section in the generated
213-
/// WebAssembly module.
214-
Source(bool),
215-
/// Enable source code compression, which generates smaller WebAssembly
216-
/// files at the cost of increased compile time. This option has no effect
217-
/// if the `source` shouldn't be embedded in the generated WebAssembly
218-
/// module.
219-
SourceCompression(bool),
220+
/// How to embed the JavaScript source in a custom section in the generated
221+
/// WebAssembly module. Options are `omitted`, `compressed`, and
222+
/// `uncompressed`. `compressed` enables source code compression which
223+
/// generates smaller WebAssembly files at the cost of increased compile
224+
/// time.
225+
Source(Source),
220226
/// Optional path to Javy plugin Wasm module. Required for dynamically
221227
/// linked modules. JavaScript config options are also not supported when
222228
/// using this parameter.
@@ -236,7 +242,6 @@ impl TryFrom<Vec<GroupOption<CodegenOption>>> for CodegenOptionGroup {
236242
let mut wit_specified = false;
237243
let mut wit_world_specified = false;
238244
let mut source_specified = false;
239-
let mut source_compression_specified = false;
240245
let mut plugin_specified = false;
241246

242247
for option in value.iter().flat_map(|i| i.0.iter()) {
@@ -262,20 +267,13 @@ impl TryFrom<Vec<GroupOption<CodegenOption>>> for CodegenOptionGroup {
262267
wit_world = Some(world);
263268
wit_world_specified = true;
264269
}
265-
CodegenOption::Source(enabled) => {
270+
CodegenOption::Source(source) => {
266271
if source_specified {
267272
bail!("source can only be specified once");
268273
}
269-
options.source = *enabled;
274+
options.source = *source;
270275
source_specified = true;
271276
}
272-
CodegenOption::SourceCompression(enabled) => {
273-
if source_compression_specified {
274-
bail!("source-compression can only be specified once");
275-
}
276-
options.source_compression = *enabled;
277-
source_compression_specified = true;
278-
}
279277
CodegenOption::Plugin(path) => {
280278
if plugin_specified {
281279
bail!("plugin can only be specified once");
@@ -422,7 +420,7 @@ mod tests {
422420
use std::path::PathBuf;
423421

424422
use crate::{
425-
commands::{JsGroupOption, JsGroupValue},
423+
commands::{JsGroupOption, JsGroupValue, Source},
426424
js_config::JsConfig,
427425
plugin::PLUGIN_MODULE,
428426
CliPlugin, Plugin, PluginKind,
@@ -536,19 +534,21 @@ mod tests {
536534

537535
assert_eq!(group, expected);
538536

539-
let raw = vec![GroupOption(vec![CodegenOption::Source(false)])];
537+
let raw = vec![GroupOption(vec![CodegenOption::Source(Source::Omitted)])];
540538
let group: CodegenOptionGroup = raw.try_into()?;
541539
let expected = CodegenOptionGroup {
542-
source: false,
540+
source: Source::Omitted,
543541
..Default::default()
544542
};
545543

546544
assert_eq!(group, expected);
547545

548-
let raw = vec![GroupOption(vec![CodegenOption::SourceCompression(false)])];
546+
let raw = vec![GroupOption(vec![CodegenOption::Source(
547+
Source::Uncompressed,
548+
)])];
549549
let group: CodegenOptionGroup = raw.try_into()?;
550550
let expected = CodegenOptionGroup {
551-
source_compression: false,
551+
source: Source::Uncompressed,
552552
..Default::default()
553553
};
554554

@@ -597,13 +597,13 @@ mod tests {
597597
);
598598

599599
let raw = vec![GroupOption(vec![
600-
CodegenOption::SourceCompression(true),
601-
CodegenOption::SourceCompression(false),
600+
CodegenOption::Source(Source::Compressed),
601+
CodegenOption::Source(Source::Uncompressed),
602602
])];
603603
let result: Result<CodegenOptionGroup, Error> = raw.try_into();
604604
assert_eq!(
605605
result.err().unwrap().to_string(),
606-
"source-compression can only be specified once"
606+
"source can only be specified once"
607607
);
608608

609609
let raw = vec![GroupOption(vec![

crates/cli/src/main.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,12 @@ fn main() -> Result<()> {
9191
.wit_opts(codegen_opts.wit)
9292
.js_runtime_config(js_opts.to_json()?);
9393

94-
if codegen_opts.source {
95-
if codegen_opts.source_compression {
96-
generator.source_embedding(SourceEmbedding::Compressed);
97-
} else {
98-
generator.source_embedding(SourceEmbedding::Uncompressed);
99-
}
100-
} else {
101-
generator.source_embedding(SourceEmbedding::Omitted);
102-
}
94+
let source_embedding = match codegen_opts.source {
95+
commands::Source::Omitted => SourceEmbedding::Omitted,
96+
commands::Source::Compressed => SourceEmbedding::Compressed,
97+
commands::Source::Uncompressed => SourceEmbedding::Uncompressed,
98+
};
99+
generator.source_embedding(source_embedding);
103100

104101
set_producer_version(&mut generator);
105102

crates/cli/src/option.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use anyhow::{bail, Result};
22
use std::path::PathBuf;
33

4+
use crate::commands;
5+
46
/// An option group used for parsing strings to their group option representation.
57
#[derive(Clone, Debug)]
68
pub struct GroupOption<T>(pub Vec<T>);
@@ -173,3 +175,22 @@ impl OptionValue for PathBuf {
173175
}
174176
}
175177
}
178+
179+
impl OptionValue for commands::Source {
180+
fn help() -> &'static str {
181+
"[=omitted|compressed|uncompressed]"
182+
}
183+
184+
fn parse(val: Option<&str>) -> Result<Self>
185+
where
186+
Self: Sized,
187+
{
188+
match val {
189+
Some("omitted") => Ok(Self::Omitted),
190+
Some("compressed") => Ok(Self::Compressed),
191+
Some("uncompressed") => Ok(Self::Uncompressed),
192+
Some(_) => bail!("Unexpected flag. Valid options: omitted, compressed, uncompressed"),
193+
None => bail!("Expected source argument"),
194+
}
195+
}
196+
}

crates/cli/tests/integration_test.rs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use anyhow::{bail, Result};
2-
use javy_runner::{Builder, Plugin, Runner, RunnerError};
2+
use javy_runner::{Builder, Plugin, Runner, RunnerError, Source};
33
use std::{fs, io::Read, path::PathBuf, process::Command, str};
44
use wasmparser::Parser;
55
use wasmtime::{AsContextMut, Engine, Linker, Module, Store};
@@ -287,8 +287,8 @@ fn test_source_code_default(builder: &mut Builder) -> Result<()> {
287287
}
288288

289289
#[javy_cli_test]
290-
fn test_source_code_compression_enabled(builder: &mut Builder) -> Result<()> {
291-
let runner = builder.compress_source_code(true).build()?;
290+
fn test_source_code_compressed(builder: &mut Builder) -> Result<()> {
291+
let runner = builder.source_code(Source::Compressed).build()?;
292292
let javy_source = runner
293293
.javy_source_custom_section()
294294
.expect("Should have javy_source custom section");
@@ -300,8 +300,8 @@ fn test_source_code_compression_enabled(builder: &mut Builder) -> Result<()> {
300300
}
301301

302302
#[javy_cli_test]
303-
fn test_source_code_compression_disabled(builder: &mut Builder) -> Result<()> {
304-
let runner = builder.compress_source_code(false).build()?;
303+
fn test_source_code_uncompressed(builder: &mut Builder) -> Result<()> {
304+
let runner = builder.source_code(Source::Uncompressed).build()?;
305305
let javy_source = runner
306306
.javy_source_custom_section()
307307
.expect("Should have javy_source custom section");
@@ -314,24 +314,14 @@ fn test_source_code_compression_disabled(builder: &mut Builder) -> Result<()> {
314314

315315
#[javy_cli_test(commands(not(Compile)))]
316316
fn test_source_code_omitted(builder: &mut Builder) -> Result<()> {
317-
let runner = builder.source_code(false).build()?;
317+
let runner = builder.source_code(Source::Omitted).build()?;
318318
assert!(
319319
runner.javy_source_custom_section().is_none(),
320320
"Should not have a source code section"
321321
);
322322
Ok(())
323323
}
324324

325-
#[javy_cli_test(commands(not(Compile)))]
326-
fn test_source_code_not_omitted(builder: &mut Builder) -> Result<()> {
327-
let runner = builder.source_code(true).build()?;
328-
assert!(
329-
runner.javy_source_custom_section().is_some(),
330-
"Should have a source code section"
331-
);
332-
Ok(())
333-
}
334-
335325
#[test]
336326
fn test_init_plugin() -> Result<()> {
337327
let engine = Engine::default();

crates/runner/src/lib.rs

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ pub enum Plugin {
2727
InvalidUser,
2828
}
2929

30+
#[derive(Debug, Clone)]
31+
pub enum Source {
32+
Omitted,
33+
Compressed,
34+
Uncompressed,
35+
}
36+
3037
impl Plugin {
3138
pub fn namespace(&self) -> &'static str {
3239
match self {
@@ -95,10 +102,8 @@ pub struct Builder {
95102
command: JavyCommand,
96103
/// The javy plugin.
97104
plugin: Plugin,
98-
/// Whether to compress the source code.
99-
compress_source_code: Option<bool>,
100-
/// Whether to include the source code.
101-
source_code: Option<bool>,
105+
/// How to embed the source code.
106+
source_code: Option<Source>,
102107
}
103108

104109
impl Default for Builder {
@@ -117,7 +122,6 @@ impl Default for Builder {
117122
text_encoding: None,
118123
event_loop: None,
119124
plugin: Plugin::Default,
120-
compress_source_code: None,
121125
source_code: None,
122126
}
123127
}
@@ -184,13 +188,8 @@ impl Builder {
184188
self
185189
}
186190

187-
pub fn compress_source_code(&mut self, enabled: bool) -> &mut Self {
188-
self.compress_source_code = Some(enabled);
189-
self
190-
}
191-
192-
pub fn source_code(&mut self, enabled: bool) -> &mut Self {
193-
self.source_code = Some(enabled);
191+
pub fn source_code(&mut self, source: Source) -> &mut Self {
192+
self.source_code = Some(source);
194193
self
195194
}
196195

@@ -219,14 +218,20 @@ impl Builder {
219218
preload,
220219
command,
221220
plugin,
222-
compress_source_code,
223221
source_code,
224222
} = std::mem::take(self);
225223

226224
self.built = true;
227225

228226
match command {
229227
JavyCommand::Compile => {
228+
let compress_source = source_code
229+
.map(|s| match s {
230+
Source::Omitted => bail!("Unsupported for compile"),
231+
Source::Compressed => Ok(true),
232+
Source::Uncompressed => Ok(false),
233+
})
234+
.transpose()?;
230235
if let Some(preload) = preload {
231236
Runner::compile_dynamic(
232237
bin_path,
@@ -235,10 +240,10 @@ impl Builder {
235240
wit,
236241
world,
237242
preload,
238-
compress_source_code,
243+
compress_source,
239244
)
240245
} else {
241-
Runner::compile_static(bin_path, root, input, wit, world, compress_source_code)
246+
Runner::compile_static(bin_path, root, input, wit, world, compress_source)
242247
}
243248
}
244249
JavyCommand::Build => Runner::build(
@@ -253,7 +258,6 @@ impl Builder {
253258
event_loop,
254259
preload,
255260
plugin,
256-
compress_source_code,
257261
source_code,
258262
),
259263
}
@@ -324,8 +328,7 @@ impl Runner {
324328
event_loop: Option<bool>,
325329
preload: Option<(String, PathBuf)>,
326330
plugin: Plugin,
327-
compress_source_code: Option<bool>,
328-
source_code: Option<bool>,
331+
source_code: Option<Source>,
329332
) -> Result<Self> {
330333
// This directory is unique and will automatically get deleted
331334
// when `tempdir` goes out of scope.
@@ -345,7 +348,6 @@ impl Runner {
345348
&text_encoding,
346349
&event_loop,
347350
&plugin,
348-
&compress_source_code,
349351
&source_code,
350352
);
351353

@@ -534,8 +536,7 @@ impl Runner {
534536
text_encoding: &Option<bool>,
535537
event_loop: &Option<bool>,
536538
plugin: &Plugin,
537-
compress_source_code: &Option<bool>,
538-
source_code: &Option<bool>,
539+
source_code: &Option<Source>,
539540
) -> Vec<String> {
540541
let mut args = vec![
541542
"build".to_string(),
@@ -587,19 +588,18 @@ impl Runner {
587588
args.push(format!("plugin={}", plugin.path().to_str().unwrap()));
588589
}
589590

590-
if let Some(enabled) = *compress_source_code {
591+
if let Some(source) = source_code {
591592
args.push("-C".into());
592593
args.push(format!(
593-
"source-compression={}",
594-
if enabled { "y" } else { "n" }
594+
"source={}",
595+
match source {
596+
Source::Omitted => "omitted",
597+
Source::Compressed => "compressed",
598+
Source::Uncompressed => "uncompressed",
599+
}
595600
));
596601
}
597602

598-
if let Some(enabled) = *source_code {
599-
args.push("-C".into());
600-
args.push(format!("source={}", if enabled { "y" } else { "n" }));
601-
}
602-
603603
args
604604
}
605605

0 commit comments

Comments
 (0)