boa_cli 0.21.1

Boa is a Javascript lexer, parser and compiler written in Rust. Currently, it has support for some of the language.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
//! A CLI implementation for `boa_engine` that comes complete with file execution and a REPL.
#![doc = include_str!("../ABOUT.md")]
#![doc(
    html_logo_url = "https://raw.githubusercontent.com/boa-dev/boa/main/assets/logo_black.svg",
    html_favicon_url = "https://raw.githubusercontent.com/boa-dev/boa/main/assets/logo_black.svg"
)]
#![cfg_attr(not(test), deny(clippy::unwrap_used))]
#![allow(clippy::print_stdout, clippy::print_stderr)]

mod debug;
mod helper;
mod logger;

use crate::logger::SharedExternalPrinterLogger;
use boa_engine::context::time::JsInstant;
use boa_engine::job::{GenericJob, TimeoutJob};
use boa_engine::{
    Context, JsError, JsResult, Source,
    builtins::promise::PromiseState,
    context::ContextBuilder,
    job::{Job, JobExecutor, NativeAsyncJob, PromiseJob},
    module::{Module, SimpleModuleLoader},
    optimizer::OptimizerOptions,
    script::Script,
    vm::flowgraph::{Direction, Graph},
};
use boa_parser::source::ReadChar;
use clap::{Parser, ValueEnum, ValueHint};
use color_eyre::{
    Result, Section,
    eyre::{WrapErr, eyre},
};
use colored::Colorize;
use debug::init_boa_debug_object;
use futures_concurrency::future::FutureGroup;
use futures_lite::{StreamExt, future};
use rustyline::{EditMode, Editor, config::Config, error::ReadlineError};
use std::collections::BTreeMap;
use std::mem;
use std::sync::mpsc::{Sender, TryRecvError};
use std::time::Duration;
use std::{
    cell::RefCell,
    collections::VecDeque,
    eprintln,
    fs::OpenOptions,
    io,
    path::{Path, PathBuf},
    println,
    rc::Rc,
    thread,
};

// ----

#[cfg(all(
    target_arch = "x86_64",
    target_os = "linux",
    target_env = "gnu",
    feature = "dhat"
))]
use jemallocator as _;

#[cfg(all(
    target_arch = "x86_64",
    target_os = "linux",
    target_env = "gnu",
    feature = "fast-allocator",
    not(feature = "dhat")
))]
#[global_allocator]
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;

#[cfg(all(target_os = "windows", feature = "dhat"))]
use mimalloc_safe as _;

#[cfg(all(
    target_os = "windows",
    feature = "fast-allocator",
    not(feature = "dhat")
))]
#[global_allocator]
static ALLOC: mimalloc_safe::MiMalloc = mimalloc_safe::MiMalloc;

#[cfg(feature = "dhat")]
#[global_allocator]
static ALLOC: dhat::Alloc = dhat::Alloc;

/// CLI configuration for Boa.
static CLI_HISTORY: &str = ".boa_history";

// Added #[allow(clippy::option_option)] because to StructOpt an Option<Option<T>>
// is an optional argument that optionally takes a value ([--opt=[val]]).
// https://docs.rs/structopt/0.3.11/structopt/#type-magic
#[derive(Debug, Parser)]
#[command(author, version, about, name = "boa")]
#[allow(clippy::struct_excessive_bools)] // NOTE: Allow having more than 3 bools in struct
struct Opt {
    /// The JavaScript file(s) to be evaluated.
    #[arg(name = "FILE", value_hint = ValueHint::FilePath)]
    files: Vec<PathBuf>,

    /// Run in strict mode.
    #[arg(long)]
    strict: bool,

    /// Dump the AST to stdout with the given format.
    #[arg(
        long,
        short = 'a',
        value_name = "FORMAT",
        ignore_case = true,
        value_enum,
        conflicts_with = "graph"
    )]
    #[allow(clippy::option_option)]
    dump_ast: Option<Option<DumpFormat>>,

    /// Dump the AST to stdout with the given format.
    #[arg(long, short, conflicts_with = "graph")]
    trace: bool,

    /// Use vi mode in the REPL
    #[arg(long = "vi")]
    vi_mode: bool,

    #[arg(long, short = 'O', group = "optimizer")]
    optimize: bool,

    #[arg(long, requires = "optimizer")]
    optimizer_statistics: bool,

    /// Generate instruction flowgraph. Default is Graphviz.
    #[arg(
        long,
        value_name = "FORMAT",
        ignore_case = true,
        value_enum,
        group = "graph"
    )]
    #[allow(clippy::option_option)]
    flowgraph: Option<Option<FlowgraphFormat>>,

    /// Specifies the direction of the flowgraph. Default is top-top-bottom.
    #[arg(
        long,
        value_name = "FORMAT",
        ignore_case = true,
        value_enum,
        requires = "graph"
    )]
    flowgraph_direction: Option<FlowgraphDirection>,

    /// Inject debugging object `$boa`.
    #[arg(long)]
    debug_object: bool,

    /// Treats the input files as modules.
    #[arg(long, short = 'm', group = "mod")]
    module: bool,

    /// Root path from where the module resolver will try to load the modules.
    #[arg(long, short = 'r', default_value_os_t = PathBuf::from("."), requires = "mod")]
    root: PathBuf,

    /// Execute a JavaScript expression then exit. Files (see above) will be
    /// executed prior to the expression.
    #[arg(long, short = 'e')]
    expression: Option<String>,
}

impl Opt {
    /// Returns whether a dump flag has been used.
    const fn has_dump_flag(&self) -> bool {
        self.dump_ast.is_some()
    }
}

/// The different types of format available for dumping.
#[derive(Debug, Copy, Clone, Default, ValueEnum)]
enum DumpFormat {
    // NOTE: This can easily support other formats just by
    // adding a field to this enum and adding the necessary
    // implementation. Example: Toml, Html, etc.
    //
    // NOTE: The fields of this enum are not doc comments because
    // arg_enum! macro does not support it.
    /// This is the default format that you get from `std::fmt::Debug`.
    #[default]
    Debug,

    /// This is a minified json format.
    Json,

    /// This is a pretty printed json format.
    JsonPretty,
}

/// Represents the format of the instruction flowgraph.
#[derive(Debug, Clone, Copy, ValueEnum)]
enum FlowgraphFormat {
    /// Generates in graphviz format: <https://graphviz.org/>.
    Graphviz,
    /// Generates in mermaid format: <https://mermaid-js.github.io/mermaid/>.
    Mermaid,
}

/// Represents the direction of the instruction flowgraph.
#[derive(Debug, Clone, Copy, ValueEnum)]
enum FlowgraphDirection {
    TopToBottom,
    BottomToTop,
    LeftToRight,
    RightToLeft,
}

/// Dumps the AST to stdout with format controlled by the given arguments.
///
/// Returns a error of type String with a error message,
/// if the source has a syntax or parsing error.
fn dump<R: ReadChar>(src: Source<'_, R>, args: &Opt, context: &mut Context) -> Result<()> {
    if let Some(arg) = args.dump_ast {
        let arg = arg.unwrap_or_default();
        let mut parser = boa_parser::Parser::new(src);
        let dump =
            if args.module {
                let scope = context.realm().scope().clone();
                let module = parser
                    .parse_module(&scope, context.interner_mut())
                    .map_err(|e| eyre!("Uncaught SyntaxError: {e}"))?;

                match arg {
                    DumpFormat::Json => serde_json::to_string(&module)
                        .expect("could not convert AST to a JSON string"),
                    DumpFormat::JsonPretty => serde_json::to_string_pretty(&module)
                        .expect("could not convert AST to a pretty JSON string"),
                    DumpFormat::Debug => format!("{module:#?}"),
                }
            } else {
                let scope = context.realm().scope().clone();
                let mut script = parser
                    .parse_script(&scope, context.interner_mut())
                    .map_err(|e| eyre!("Uncaught SyntaxError: {e}"))?;

                if args.optimize {
                    context.optimize_statement_list(script.statements_mut());
                }

                match arg {
                    DumpFormat::Json => serde_json::to_string(&script)
                        .expect("could not convert AST to a JSON string"),
                    DumpFormat::JsonPretty => serde_json::to_string_pretty(&script)
                        .expect("could not convert AST to a pretty JSON string"),
                    DumpFormat::Debug => format!("{script:#?}"),
                }
            };

        println!("{dump}");
    }

    Ok(())
}

fn generate_flowgraph<R: ReadChar>(
    context: &mut Context,
    src: Source<'_, R>,
    format: FlowgraphFormat,
    direction: Option<FlowgraphDirection>,
) -> Result<String> {
    let script = Script::parse(src, None, context).map_err(|e| e.into_erased(context))?;
    let code = script
        .codeblock(context)
        .map_err(|e| e.into_erased(context))?;
    let direction = match direction {
        Some(FlowgraphDirection::TopToBottom) | None => Direction::TopToBottom,
        Some(FlowgraphDirection::BottomToTop) => Direction::BottomToTop,
        Some(FlowgraphDirection::LeftToRight) => Direction::LeftToRight,
        Some(FlowgraphDirection::RightToLeft) => Direction::RightToLeft,
    };
    let mut graph = Graph::new(direction);
    code.to_graph(graph.subgraph(String::default()));
    let result = match format {
        FlowgraphFormat::Graphviz => graph.to_graphviz_format(),
        FlowgraphFormat::Mermaid => graph.to_mermaid_format(),
    };
    Ok(result)
}

#[must_use]
fn uncaught_error(error: &JsError) -> String {
    format!("{}: {}\n", "Uncaught".red(), error.to_string().red())
}

#[must_use]
fn uncaught_job_error(error: &JsError) -> String {
    format!(
        "{}: {}\n",
        "Uncaught error (during job evaluation)".red(),
        error.to_string().red()
    )
}

fn evaluate_expr(
    line: &str,
    args: &Opt,
    context: &mut Context,
    printer: &SharedExternalPrinterLogger,
) -> Result<()> {
    if args.has_dump_flag() {
        dump(Source::from_bytes(&line), args, context)?;
    } else if let Some(flowgraph) = args.flowgraph {
        match generate_flowgraph(
            context,
            Source::from_bytes(line),
            flowgraph.unwrap_or(FlowgraphFormat::Graphviz),
            args.flowgraph_direction,
        ) {
            Ok(v) => println!("{v}"),
            Err(v) => eprintln!("{v:?}"),
        }
    } else {
        match context.eval(Source::from_bytes(line)) {
            Ok(v) => printer.print(format!("{}\n", v.display())),
            Err(ref v) => printer.print(uncaught_error(v)),
        }
    }

    Ok(())
}

fn evaluate_file(
    file: &Path,
    args: &Opt,
    context: &mut Context,
    loader: &SimpleModuleLoader,
    printer: &SharedExternalPrinterLogger,
) -> Result<()> {
    if args.has_dump_flag() {
        return dump(Source::from_filepath(file)?, args, context);
    }

    if let Some(flowgraph) = args.flowgraph {
        let flowgraph = generate_flowgraph(
            context,
            Source::from_filepath(file)?,
            flowgraph.unwrap_or(FlowgraphFormat::Graphviz),
            args.flowgraph_direction,
        )?;

        println!("{flowgraph}");

        return Ok(());
    }

    if args.module {
        let module = Module::parse(Source::from_filepath(file)?, None, context)
            .map_err(|e| e.into_erased(context))?;

        loader.insert(
            file.canonicalize()
                .wrap_err("could not canonicalize input file path")?,
            module.clone(),
        );

        let promise = module.load_link_evaluate(context);
        context.run_jobs().map_err(|err| err.into_erased(context))?;
        let result = promise.state();

        return match result {
            PromiseState::Pending => Err(eyre!("module didn't execute")),
            PromiseState::Fulfilled(_) => Ok(()),
            PromiseState::Rejected(err) => {
                return Err(JsError::from_opaque(err).into_erased(context).into());
            }
        };
    }

    match context.eval(Source::from_filepath(file)?) {
        Ok(v) => {
            if !v.is_undefined() {
                println!("{}", v.display());
            }
        }
        Err(v) => printer.print(uncaught_error(&v)),
    }

    context
        .run_jobs()
        .map_err(|err| err.into_erased(context).into())
}

fn evaluate_files(
    args: &Opt,
    context: &mut Context,
    loader: &SimpleModuleLoader,
    printer: &SharedExternalPrinterLogger,
) -> Result<()> {
    for file in &args.files {
        evaluate_file(file, args, context, loader, printer)?;
    }
    Ok(())
}

fn main() -> Result<()> {
    color_eyre::config::HookBuilder::default()
        .display_location_section(false)
        .display_env_section(false)
        .install()?;

    #[cfg(feature = "dhat")]
    let _profiler = dhat::Profiler::new_heap();

    let args = Opt::parse();

    // A channel of expressions to run.
    let (sender, receiver) = std::sync::mpsc::channel::<String>();
    let printer = SharedExternalPrinterLogger::new();

    let executor = Rc::new(Executor::new(printer.clone()));
    let loader = Rc::new(SimpleModuleLoader::new(&args.root).map_err(|e| eyre!(e.to_string()))?);
    let mut context = ContextBuilder::new()
        .job_executor(executor)
        .module_loader(loader.clone())
        .build()
        .map_err(|e| eyre!(e.to_string()))?;

    // Strict mode
    context.strict(args.strict);

    // Add `console`.
    add_runtime(printer.clone(), &mut context);

    // Trace Output
    context.set_trace(args.trace);

    if args.debug_object {
        init_boa_debug_object(&mut context);
    }

    // Configure optimizer options
    let mut optimizer_options = OptimizerOptions::empty();
    optimizer_options.set(OptimizerOptions::STATISTICS, args.optimizer_statistics);
    optimizer_options.set(OptimizerOptions::OPTIMIZE_ALL, args.optimize);
    context.set_optimizer_options(optimizer_options);

    if !args.files.is_empty() {
        evaluate_files(&args, &mut context, &loader, &printer)?;

        if let Some(ref expr) = args.expression {
            evaluate_expr(expr, &args, &mut context, &printer)?;
        }

        return Ok(());
    } else if let Some(ref expr) = args.expression {
        evaluate_expr(expr, &args, &mut context, &printer)?;
        return Ok(());
    }

    let handle = start_readline_thread(sender, printer.clone(), args.vi_mode);

    loop {
        match receiver.try_recv() {
            Ok(line) => {
                evaluate_expr(&line, &args, &mut context, &printer)?;
            }
            Err(TryRecvError::Empty) => {}
            Err(TryRecvError::Disconnected) => break,
        }

        if let Err(err) = context.run_jobs() {
            printer.print(uncaught_job_error(&err));
        }
        thread::sleep(Duration::from_millis(10));
    }

    handle.join().expect("failed to join thread");

    Ok(())
}

fn readline_thread_main(
    sender: &Sender<String>,
    printer_out: &SharedExternalPrinterLogger,
    vi_mode: bool,
) -> Result<()> {
    let config = Config::builder()
        .keyseq_timeout(Some(1))
        .edit_mode(if vi_mode {
            EditMode::Vi
        } else {
            EditMode::Emacs
        })
        .build();

    let mut editor =
        Editor::with_config(config).wrap_err("failed to set the editor configuration")?;
    if let Ok(printer) = editor.create_external_printer() {
        printer_out.set(printer);
    }

    // Check if the history file exists. If it doesn't, create it.
    OpenOptions::new()
        .read(true)
        .write(true)
        .create(true)
        .truncate(false)
        .open(CLI_HISTORY)?;
    editor
        .load_history(CLI_HISTORY)
        .wrap_err("failed to read history file `.boa_history`")?;
    let readline = ">> ";
    editor.set_helper(Some(helper::RLHelper::new(readline)));

    loop {
        match editor.readline(readline) {
            Ok(line) if line == ".exit" => break,
            Err(ReadlineError::Interrupted | ReadlineError::Eof) => break,

            Ok(line) => {
                let line = line.trim_end();
                editor.add_history_entry(line).map_err(io::Error::other)?;
                sender.send(line.to_string())?;
                thread::sleep(Duration::from_millis(10));
            }

            Err(err) => {
                let final_error = eyre!("could not read the next line of the input");
                let final_error = if let Err(e) = editor.save_history(CLI_HISTORY) {
                    final_error.error(e)
                } else {
                    final_error
                };
                return Err(final_error.error(err));
            }
        }
    }

    editor.save_history(CLI_HISTORY)?;

    Ok(())
}

/// Create the readline thread which sends lines from stdin back to the main thread.
fn start_readline_thread(
    sender: Sender<String>,
    printer_out: SharedExternalPrinterLogger,
    vi_mode: bool,
) -> thread::JoinHandle<()> {
    thread::spawn(
        move || match readline_thread_main(&sender, &printer_out, vi_mode) {
            Ok(()) => {}
            Err(e) => eprintln!("readline thread failed: {e}"),
        },
    )
}

/// Adds the CLI runtime to the context with default options.
fn add_runtime(printer: SharedExternalPrinterLogger, context: &mut Context) {
    boa_runtime::register(
        (
            boa_runtime::extensions::ConsoleExtension(printer),
            #[cfg(feature = "fetch")]
            boa_runtime::extensions::FetchExtension(
                boa_runtime::fetch::BlockingReqwestFetcher::default(),
            ),
        ),
        None,
        context,
    )
    .expect("should not fail while registering the runtime");
}

#[allow(clippy::struct_field_names)]
struct Executor {
    promise_jobs: RefCell<VecDeque<PromiseJob>>,
    async_jobs: RefCell<VecDeque<NativeAsyncJob>>,
    timeout_jobs: RefCell<BTreeMap<JsInstant, TimeoutJob>>,
    generic_jobs: RefCell<VecDeque<GenericJob>>,

    printer: SharedExternalPrinterLogger,
}

impl Executor {
    fn new(printer: SharedExternalPrinterLogger) -> Self {
        Self {
            promise_jobs: RefCell::default(),
            async_jobs: RefCell::default(),
            timeout_jobs: RefCell::default(),
            generic_jobs: RefCell::default(),
            printer,
        }
    }

    fn is_empty(&self, context: &mut Context) -> bool {
        let now = context.clock().now();

        self.promise_jobs.borrow().is_empty()
            && self.async_jobs.borrow().is_empty()
            // The timeout jobs queue is empty IIF there are no jobs to execute right now.
            && !self.timeout_jobs.borrow().iter().any(|(t, _)| &now >= t)
            && self.generic_jobs.borrow().is_empty()
    }

    fn drain_timeout_jobs(&self, context: &mut Context) {
        let now = context.clock().now();

        let mut timeouts_borrow = self.timeout_jobs.borrow_mut();
        let mut jobs_to_keep = timeouts_borrow.split_off(&now);
        jobs_to_keep.retain(|_, job| !job.is_cancelled());
        let jobs_to_run = mem::replace(&mut *timeouts_borrow, jobs_to_keep);
        drop(timeouts_borrow);

        for job in jobs_to_run.into_values() {
            if let Err(e) = job.call(context) {
                self.printer.print(uncaught_job_error(&e));
            }
        }
    }

    fn drain_generic_jobs(&self, context: &mut Context) {
        let job = self.generic_jobs.borrow_mut().pop_front();
        if let Some(generic) = job
            && let Err(err) = generic.call(context)
        {
            self.printer.print(uncaught_job_error(&err));
        }
    }
}

impl JobExecutor for Executor {
    fn enqueue_job(self: Rc<Self>, job: Job, context: &mut Context) {
        match job {
            Job::PromiseJob(job) => self.promise_jobs.borrow_mut().push_back(job),
            Job::AsyncJob(job) => self.async_jobs.borrow_mut().push_back(job),
            Job::TimeoutJob(job) => {
                let now = context.clock().now();
                self.timeout_jobs
                    .borrow_mut()
                    .insert(now + job.timeout(), job);
            }
            Job::GenericJob(job) => self.generic_jobs.borrow_mut().push_back(job),
            job => self.printer.print(format!("unsupported job type {job:?}")),
        }
    }

    fn run_jobs(self: Rc<Self>, context: &mut Context) -> JsResult<()> {
        future::block_on(self.run_jobs_async(&RefCell::new(context)))
    }

    async fn run_jobs_async(self: Rc<Self>, context: &RefCell<&mut Context>) -> JsResult<()> {
        let mut group = FutureGroup::new();

        loop {
            for job in mem::take(&mut *self.async_jobs.borrow_mut()) {
                group.insert(job.call(context));
            }

            if self.is_empty(&mut context.borrow_mut()) && group.is_empty() {
                return Ok(());
            }

            if let Some(Err(e)) = future::poll_once(group.next()).await.flatten() {
                self.printer.print(uncaught_job_error(&e));
            }

            {
                let context = &mut context.borrow_mut();
                self.drain_timeout_jobs(context);
                self.drain_generic_jobs(context);

                let jobs = mem::take(&mut *self.promise_jobs.borrow_mut());
                for job in jobs {
                    if let Err(e) = job.call(context) {
                        self.printer.print(uncaught_job_error(&e));
                    }
                }
            }
        }
    }
}