Skip to content

Commit

Permalink
Merge branch 'main' into jc.remove-console-log-redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffcharles committed Jan 8, 2025
2 parents 6e38637 + dd36f47 commit 0bb3517
Show file tree
Hide file tree
Showing 19 changed files with 58 additions and 195 deletions.
43 changes: 27 additions & 16 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ wasmtime = "23"
wasmtime-wasi = "23"
wasi-common = "23"
anyhow = "1.0"
javy = { path = "crates/javy", version = "3.1.1-alpha.1" }
javy = { path = "crates/javy", version = "4.0.0-alpha.1" }
tempfile = "3.15.0"
uuid = { version = "1.11", features = ["v4"] }
serde = { version = "1.0", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ wasmtime = { workspace = true }
wasmtime-wasi = { workspace = true }
wasi-common = { workspace = true }
walrus = "0.23.3"
swc_core = { version = "8.0.1", features = [
swc_core = { version = "10.0.1", features = [
"common_sourcemap",
"ecma_ast",
"ecma_parser",
Expand Down
24 changes: 0 additions & 24 deletions crates/cli/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,29 +374,10 @@ mod tests {
#[test]
fn js_config_from_config_values() -> Result<()> {
let group = JsConfig::from_group_values(&Plugin::Default, vec![])?;
assert_eq!(group.get("javy-json"), None);
assert_eq!(group.get("javy-stream-io"), None);
assert_eq!(group.get("simd-json-builtins"), None);
assert_eq!(group.get("text-encoding"), None);

let group = JsConfig::from_group_values(
&Plugin::Default,
vec![JsGroupValue::Option(JsGroupOption {
name: "javy-json".to_string(),
enabled: false,
})],
)?;
assert_eq!(group.get("javy-json"), Some(false));

let group = JsConfig::from_group_values(
&Plugin::Default,
vec![JsGroupValue::Option(JsGroupOption {
name: "javy-json".to_string(),
enabled: true,
})],
)?;
assert_eq!(group.get("javy-json"), Some(true));

let group = JsConfig::from_group_values(
&Plugin::Default,
vec![JsGroupValue::Option(JsGroupOption {
Expand Down Expand Up @@ -454,10 +435,6 @@ mod tests {
let group = JsConfig::from_group_values(
&Plugin::Default,
vec![
JsGroupValue::Option(JsGroupOption {
name: "javy-json".to_string(),
enabled: false,
}),
JsGroupValue::Option(JsGroupOption {
name: "javy-stream-io".to_string(),
enabled: false,
Expand All @@ -472,7 +449,6 @@ mod tests {
}),
],
)?;
assert_eq!(group.get("javy-json"), Some(false));
assert_eq!(group.get("javy-stream-io"), Some(false));
assert_eq!(group.get("simd-json-builtins"), Some(false));
assert_eq!(group.get("text-encoding"), Some(false));
Expand Down
1 change: 0 additions & 1 deletion crates/cli/tests/dynamic-linking-scripts/javy-json-id.js

This file was deleted.

19 changes: 0 additions & 19 deletions crates/cli/tests/dynamic_linking_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,25 +111,6 @@ fn test_using_runtime_flag_with_dynamic_triggers_error(builder: &mut Builder) ->
Ok(())
}

#[javy_cli_test(
dyn = true,
root = "tests/dynamic-linking-scripts",
commands(not(Compile))
)]
fn javy_json_identity(builder: &mut Builder) -> Result<()> {
let mut runner = builder.input("javy-json-id.js").build()?;

let input = "{\"x\":5}";

let bytes = String::from(input).into_bytes();
let (out, logs, _) = runner.exec(&bytes)?;

assert_eq!(String::from_utf8(out)?, input);
assert_eq!(String::from_utf8(logs)?, "undefined\n");

Ok(())
}

#[javy_cli_test(dyn = true, commands(not(Compile)))]
fn test_using_plugin_with_dynamic_works(builder: &mut Builder) -> Result<()> {
let plugin = Plugin::User;
Expand Down
28 changes: 1 addition & 27 deletions crates/cli/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,33 +81,7 @@ fn test_console_log(builder: &mut Builder) -> Result<()> {
let (output, logs, fuel_consumed) = run(&mut runner, &[]);
assert_eq!(b"hello world from console.log\n".to_vec(), output);
assert_eq!("hello world from console.error\n", logs.as_str());
assert_fuel_consumed_within_threshold(36_641, fuel_consumed);
Ok(())
}

#[javy_cli_test(commands(not(Compile)), root = "tests/dynamic-linking-scripts")]
fn test_javy_json_enabled(builder: &mut Builder) -> Result<()> {
let mut runner = builder.input("javy-json-id.js").build()?;

let input = "{\"x\":5}";
let (output, logs, _) = run(&mut runner, input.as_bytes());

assert_eq!(logs, "undefined\n");
assert_eq!(String::from_utf8(output)?, input);

Ok(())
}

#[javy_cli_test(commands(not(Compile)), root = "tests/dynamic-linking-scripts")]
fn test_javy_json_disabled(builder: &mut Builder) -> Result<()> {
let mut runner = builder
.input("javy-json-id.js")
.simd_json_builtins(false)
.build()?;

let result = runner.exec(&[]);
assert!(result.is_err());

assert_fuel_consumed_within_threshold(35_860, fuel_consumed);
Ok(())
}

Expand Down
5 changes: 5 additions & 0 deletions crates/javy/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Removed

- `Javy.JSON.fromStdin` and `Javy.JSON.toStdout` APIs and `javy_json` method on
`javy::Config`.

## [3.1.0] - 2024-11-27

### Added
Expand Down
2 changes: 1 addition & 1 deletion crates/javy/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "javy"
version = "3.1.1-alpha.1"
version = "4.0.0-alpha.1"
authors.workspace = true
edition.workspace = true
license.workspace = true
Expand Down
57 changes: 2 additions & 55 deletions crates/javy/src/apis/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
//! hotpath and doing any sort of inline processing of the parsed or stringified
//! values is likely to void any performance benefits.
use crate::{
hold, hold_and_release, json,
hold, json,
quickjs::{
function::This,
prelude::{MutFn, Rest},
Expand All @@ -32,11 +32,7 @@ use crate::serde::de::get_to_json;
use simd_json::Error as SError;

use anyhow::{anyhow, bail, Result};
use std::{
io::{Read, Write},
sync::OnceLock,
time::SystemTime,
};
use std::{sync::OnceLock, time::SystemTime};

static DEFAULT_PARSE_KEY: OnceLock<String> = OnceLock::new();

Expand Down Expand Up @@ -173,52 +169,3 @@ fn call_json_stringify(args: Args<'_>) -> Result<Value<'_>> {
)),
}
}

/// Register `Javy.JSON.fromStdin` and `Javy.JSON.toStdout` functions on the
/// global object.
pub(crate) fn register_javy_json(this: Ctx<'_>) -> Result<()> {
let globals = this.globals();
let javy = if globals.get::<_, Object>("Javy").is_err() {
Object::new(this.clone())?
} else {
globals.get::<_, Object>("Javy").unwrap()
};

let from_stdin = Function::new(this.clone(), |cx, args| {
let (cx, args) = hold_and_release!(cx, args);
from_stdin(hold!(cx.clone(), args)).map_err(|e| to_js_error(cx, e))
});

let to_stdout = Function::new(this.clone(), |cx, args| {
let (cx, args) = hold_and_release!(cx, args);
to_stdout(hold!(cx.clone(), args)).map_err(|e| to_js_error(cx, e))
});

let json = Object::new(this)?;
json.set("fromStdin", from_stdin)?;
json.set("toStdout", to_stdout)?;

javy.set("JSON", json)?;
globals.set("Javy", javy).map_err(Into::into)
}

/// Definition for Javy.JSON.fromStdin
fn from_stdin(args: Args<'_>) -> Result<Value> {
// Light experimentation shows that 1k bytes is enough to avoid paying the
// high relocation costs. We can modify as we see fit or even make this
// configurable if needed.
let mut buffer = Vec::with_capacity(1000);
let mut fd = std::io::stdin();
fd.read_to_end(&mut buffer)?;
let (ctx, _) = args.release();
json::parse(ctx, &mut buffer)
}

/// Definition for Javy.JSON.toStdout
fn to_stdout(args: Args<'_>) -> Result<()> {
let (_, args) = args.release();
let mut fd = std::io::stdout();
let buffer = json::stringify(args[0].clone())?;
fd.write_all(&buffer)?;
fd.flush().map_err(Into::into)
}
10 changes: 0 additions & 10 deletions crates/javy/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ bitflags! {
/// moved out.
pub(crate) struct JavyIntrinsics: u32 {
const STREAM_IO = 1;
const JSON = 1 << 1;
}
}

Expand Down Expand Up @@ -181,15 +180,6 @@ impl Config {
self
}

/// Whether the `Javy.JSON` intrinsic will be available.
/// Disabled by default.
/// This setting requires the `json` crate feature to be enabled.
#[cfg(feature = "json")]
pub fn javy_json(&mut self, enable: bool) -> &mut Self {
self.javy_intrinsics.set(JavyIntrinsics::JSON, enable);
self
}

/// Enables whether the output of console.log will be redirected to
/// `stderr`.
pub fn redirect_stdout_to_stderr(&mut self, enable: bool) -> &mut Self {
Expand Down
6 changes: 0 additions & 6 deletions crates/javy/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,6 @@ impl Runtime {
stream_io::register(ctx.clone())
.expect("registering StreamIO functions to succeed");
}

#[cfg(feature = "json")]
if javy_intrinsics.contains(JavyIntrinsics::JSON) {
json::register_javy_json(ctx.clone())
.expect("registering Javy.JSON builtins to succeed");
}
});

Ok(ManuallyDrop::new(context))
Expand Down
Loading

0 comments on commit 0bb3517

Please sign in to comment.