Skip to content

Commit

Permalink
Rework repl and add support for history, cursor control, async execut…
Browse files Browse the repository at this point in the history
…ion etc (#748)

* Rework repl and add support for history, cursor control, async execution and more

* clippy
  • Loading branch information
richarddavison authored Dec 19, 2024
1 parent 72013b8 commit f801cc7
Show file tree
Hide file tree
Showing 12 changed files with 401 additions and 135 deletions.
118 changes: 92 additions & 26 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions libs/llrt_utils/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
use rquickjs::{CatchResultExt, CaughtError, Ctx, Error, Result, String as JsString, Value};
use rquickjs::{CatchResultExt, CaughtError, Ctx, Error, IntoJs, Result, Value};

pub trait ErrorExtensions<'js> {
fn into_value(self, ctx: &Ctx<'js>) -> Result<Value<'js>>;
Expand All @@ -15,9 +15,7 @@ impl<'js> ErrorExtensions<'js> for Error {
impl<'js> ErrorExtensions<'js> for CaughtError<'js> {
fn into_value(self, ctx: &Ctx<'js>) -> Result<Value<'js>> {
Ok(match self {
CaughtError::Error(err) => {
JsString::from_str(ctx.clone(), &err.to_string())?.into_value()
},
CaughtError::Error(err) => err.to_string().into_js(ctx)?,
CaughtError::Exception(ex) => ex.into_value(),
CaughtError::Value(val) => val,
})
Expand Down
35 changes: 13 additions & 22 deletions libs/llrt_utils/src/sysinfo.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
use std::env;

pub fn get_platform() -> &'static str {
let platform = env::consts::OS;
match platform {
"macos" => "darwin",
"windows" => "win32",
_ => platform,
}
}

pub fn get_arch() -> &'static str {
let arch = env::consts::ARCH;

match arch {
"x86_64" | "x86" => return "x64",
"aarch64" => return "arm64",
_ => (),
}

arch
}
#[cfg(target_os = "macos")]
pub const PLATFORM: &str = "darwin";
#[cfg(target_os = "windows")]
pub const PLATFORM: &str = "win32";
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
pub const PLATFORM: &str = std::env::consts::OS;

#[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
pub const ARCH: &str = "x64";
#[cfg(target_arch = "aarch64")]
pub const ARCH: &str = "arm64";
#[cfg(not(any(target_arch = "x86_64", target_arch = "x86", target_arch = "aarch64")))]
pub const ARCH: &str = std::env::consts::ARCH;
7 changes: 7 additions & 0 deletions llrt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,22 @@ bindgen = ["llrt_core/bindgen"]
[dependencies]
llrt_core = { path = "../llrt_core" }
tracing = { version = "0.1.41", features = ["log"] }
llrt_utils = { path = "../libs/llrt_utils" }
chrono = { version = "0.4.38", default-features = false, features = ["std"] }
tracing-core = "0.1.33"
tokio = { version = "1", features = ["full"] }
crossterm = { version = "0.28.1" }
constcat = "0.5.1"

[target.'cfg(not(target_os = "windows"))'.dependencies]
snmalloc-rs = { version = "0.3.7", features = ["lto"] }
# FIXME remove when https://github.com/rust-lang/cc-rs/issues/1278 is resolved
cc = "=1.1.31"


[dev-dependencies]
llrt_test = { version = "0.3.0-beta", path = "../libs/llrt_test" }

[[bin]]
name = "llrt"
path = "src/main.rs"
20 changes: 17 additions & 3 deletions llrt/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@
// SPDX-License-Identifier: Apache-2.0

mod minimal_tracer;
#[cfg(not(feature = "lambda"))]
mod repl;

use constcat::concat;
use llrt_core::{
async_with,
bytecode::BYTECODE_EXT,
modules::{
console::{self, LogLevel},
path::name_extname,
process::{get_arch, get_platform},
},
runtime_client,
utils::io::{is_supported_ext, DirectoryWalker, SUPPORTED_EXTENSIONS},
vm::Vm,
CatchResultExt, VERSION,
};
use llrt_utils::sysinfo::{ARCH, PLATFORM};
use minimal_tracer::MinimalTracer;
use std::{
env,
Expand Down Expand Up @@ -65,8 +68,10 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
Ok(())
}

pub const VERSION_STRING: &str = concat!("LLRT v", VERSION, " (", PLATFORM, ", ", ARCH, ")");

fn print_version() {
println!("LLRT ({} {}) {}", get_platform(), get_arch(), VERSION);
println!("{VERSION_STRING}");
}

fn usage() {
Expand Down Expand Up @@ -192,7 +197,16 @@ async fn start_cli(vm: &Vm) {
}
}
} else {
vm.run_repl().await;
#[cfg(not(feature = "lambda"))]
{
repl::run_repl(&vm.ctx).await;
}

#[cfg(feature = "lambda")]
{
eprintln!("REPL not supported in \"lambda\" version.");
exit(1);
}
}
}

Expand Down
Loading

0 comments on commit f801cc7

Please sign in to comment.