Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix console log level filtering and #728

Merged
merged 2 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions libs/llrt_json/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ pub mod stringify;
#[cfg(test)]
mod tests {
use llrt_test::test_sync_with;
use rquickjs::{Array, CatchResultExt, IntoJs, Null, Object, Undefined, Value};
use rquickjs::{prelude::Func, Array, CatchResultExt, IntoJs, Null, Object, Undefined, Value};

use crate::{
parse::json_parse,
parse::{json_parse, json_parse_string},
stringify::{json_stringify, json_stringify_replacer_space},
};

Expand Down Expand Up @@ -45,6 +45,27 @@ mod tests {
.await;
}

#[tokio::test]
async fn json_parse_non_string() {
test_sync_with(|ctx| {
ctx.globals().set("parse", Func::from(json_parse_string))?;

let result = ctx.eval::<(), _>("parse({})").catch(&ctx);

if let Err(err) = result {
assert_eq!(
err.to_string(),
"Error: \"[object Object]\" not valid JSON at index 1 ('o')\n at <eval> (eval_script:1:1)\n"
);
} else {
panic!("expected error")
}

Ok(())
})
.await;
}

#[tokio::test]
async fn json_stringify_undefined() {
test_sync_with(|ctx| {
Expand Down
33 changes: 30 additions & 3 deletions libs/llrt_json/src/parse.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,40 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

use llrt_utils::result::ResultExt;
use rquickjs::{Array, Ctx, IntoJs, Null, Object, Result, Undefined, Value};
use llrt_utils::bytes::ObjectBytes;
use rquickjs::{Array, Ctx, Exception, IntoJs, Null, Object, Result, Undefined, Value};
use simd_json::{Node, StaticNode};

pub fn json_parse_string<'js>(ctx: Ctx<'js>, bytes: ObjectBytes<'js>) -> Result<Value<'js>> {
let bytes = bytes.as_bytes();
json_parse(&ctx, bytes)
}

pub fn json_parse<'js, T: Into<Vec<u8>>>(ctx: &Ctx<'js>, json: T) -> Result<Value<'js>> {
let mut json: Vec<u8> = json.into();
let tape = simd_json::to_tape(&mut json).or_throw(ctx)?;
let tape = match simd_json::to_tape(&mut json) {
Ok(tape) => tape,
Err(err) => {
let mut itoa = itoa::Buffer::new();
let mut error_msg = String::with_capacity(256);
let json_length = json.len();
if json_length < 128 {
error_msg.reserve(json_length);
error_msg.push('\"');
error_msg.push_str(&std::string::String::from_utf8_lossy(&json));
error_msg.push_str("\" ");
}

error_msg.push_str("not valid JSON at index ");
error_msg.push_str(itoa.format(err.index()));
if let Some(char) = err.character() {
error_msg.push_str(" ('");
error_msg.push(char);
error_msg.push_str("')");
}
return Err(Exception::throw_syntax(ctx, &error_msg));
},
};
let tape = tape.0;

if let Some(first) = tape.first() {
Expand Down
4 changes: 2 additions & 2 deletions llrt_core/src/modules/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -779,8 +779,8 @@ fn write_lambda_log<'js>(
) -> Result<bool> {
let mut is_newline = true;

if is_json_log_format && max_log_level < level.clone() as usize {
//do not log if we don't meet the log level
//do not log if we don't meet the log level
if is_json_log_format && (level.clone() as usize) < max_log_level {
return Ok(false);
}
result.reserve(64);
Expand Down
8 changes: 1 addition & 7 deletions llrt_core/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
// SPDX-License-Identifier: Apache-2.0
use std::{cmp::min, env, fmt::Write, process::exit, result::Result as StdResult};

use llrt_json::{parse::json_parse, stringify::json_stringify_replacer_space};
use llrt_json::{parse::json_parse_string, stringify::json_stringify_replacer_space};
use llrt_numbers::number_to_string;
use llrt_utils::{
bytes::ObjectBytes,
error::ErrorExtensions,
object::ObjectExt,
primordials::{BasePrimordials, Primordial},
Expand Down Expand Up @@ -210,11 +209,6 @@ impl Vm {
}
}

fn json_parse_string<'js>(ctx: Ctx<'js>, bytes: ObjectBytes<'js>) -> Result<Value<'js>> {
let bytes = bytes.as_bytes();
json_parse(&ctx, bytes)
}

fn init(ctx: &Ctx<'_>) -> Result<()> {
llrt_context::set_spawn_error_handler(|ctx, err| {
Vm::print_error_and_exit(ctx, err);
Expand Down
Loading