Skip to content

Console refactor #393

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

Merged
merged 9 commits into from
Jun 3, 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
299 changes: 156 additions & 143 deletions Cargo.lock

Large diffs are not rendered by default.

7 changes: 2 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
[workspace]
resolver = "2"
members = [
"llrt_core",
"llrt",
]
members = ["llrt_core", "llrt"]

[profile.flame]
inherits = "release"
Expand All @@ -19,4 +16,4 @@ panic = "abort"

[profile.test]
panic = "abort"
opt-level = 3 #required for large number parsing
opt-level = 3 #required for large number parsing
2 changes: 1 addition & 1 deletion example/functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
"devDependencies": {
"@types/react": "18.3.3",
"@types/react-dom": "18.3.0",
"esbuild": "0.20.2"
"esbuild": "0.21.4"
}
}
1,671 changes: 1,671 additions & 0 deletions example/functions/yarn.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions example/infrastructure/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
"hotswap": "cdk deploy --hotswap"
},
"devDependencies": {
"@types/node": "20.13.0",
"@types/node": "20.14.0",
"aws-cdk": "2.144.0",
"aws-cdk-lib": "2.144.0",
"constructs": "10.3.0",
"esbuild": "0.20.2",
"esbuild": "0.21.4",
"ts-node": "10.9.2",
"typescript": "5.4.5"
}
Expand Down
532 changes: 532 additions & 0 deletions example/infrastructure/yarn.lock

Large diffs are not rendered by default.

47 changes: 21 additions & 26 deletions llrt_core/src/json/stringify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::json::escape::escape_json_string;

const CIRCULAR_REF_DETECTION_DEPTH: usize = 20;

struct IterationContext<'a, 'js> {
struct StringifyContext<'a, 'js> {
ctx: &'a Ctx<'js>,
result: &'a mut String,
value: &'a Value<'js>,
Expand Down Expand Up @@ -77,7 +77,7 @@ pub fn json_stringify_replacer_space<'js>(

let mut ancestors = Vec::with_capacity(10);

let mut context = IterationContext {
let mut context = StringifyContext {
ctx,
result: &mut result,
value: &value,
Expand Down Expand Up @@ -119,13 +119,13 @@ fn write_indentation(result: &mut String, indentation: Option<&str>, depth: usiz
#[inline(always)]
#[cold]
fn run_to_json<'js>(
context: &mut IterationContext<'_, 'js>,
context: &mut StringifyContext<'_, 'js>,
js_object: &Object<'js>,
) -> Result<()> {
let to_json = js_object.get::<_, Function>(PredefinedAtom::ToJSON)?;
let val = to_json.call((This(js_object.clone()),))?;
append_value(
&mut IterationContext {
&mut StringifyContext {
ctx: context.ctx,
result: context.result,
value: &val,
Expand Down Expand Up @@ -153,7 +153,7 @@ enum PrimitiveStatus {
#[inline(always)]
#[cold]
fn run_replacer<'js>(
context: &mut IterationContext<'_, 'js>,
context: &mut StringifyContext<'_, 'js>,
replacer_fn: &Function<'js>,
add_comma: bool,
) -> Result<PrimitiveStatus> {
Expand All @@ -171,7 +171,7 @@ fn run_replacer<'js>(
};
let new_value = replacer_fn.call((This(parent), get_key_or_index(key, index), value))?;
write_primitive(
&mut IterationContext {
&mut StringifyContext {
ctx,
result: context.result,
value: &new_value,
Expand All @@ -189,7 +189,7 @@ fn run_replacer<'js>(
}

#[inline(always)]
fn write_primitive(context: &mut IterationContext, add_comma: bool) -> Result<PrimitiveStatus> {
fn write_primitive(context: &mut StringifyContext, add_comma: bool) -> Result<PrimitiveStatus> {
if let Some(replacer_fn) = context.replacer_fn {
return run_replacer(context, replacer_fn, add_comma);
}
Expand Down Expand Up @@ -225,10 +225,12 @@ fn write_primitive(context: &mut IterationContext, add_comma: bool) -> Result<Pr

match type_of {
Type::Null | Type::Undefined => context.result.push_str("null"),
Type::Bool => context.result.push_str(match value.as_bool().unwrap() {
true => "true",
false => "false",
}),
Type::Bool => {
const BOOL_STRINGS: [&str; 2] = ["false", "true"];
context
.result
.push_str(BOOL_STRINGS[value.as_bool().unwrap() as usize]);
},
Type::Int => {
let mut buffer = itoa::Buffer::new();
context
Expand Down Expand Up @@ -338,7 +340,7 @@ fn detect_circular_reference(
}

#[inline(always)]
fn append_value(context: &mut IterationContext<'_, '_>, add_comma: bool) -> Result<bool> {
fn append_value(context: &mut StringifyContext<'_, '_>, add_comma: bool) -> Result<bool> {
match write_primitive(context, add_comma)? {
PrimitiveStatus::Written => Ok(true),
PrimitiveStatus::Ignored => Ok(false),
Expand All @@ -354,28 +356,21 @@ fn append_value(context: &mut IterationContext<'_, '_>, add_comma: bool) -> Resu
fn write_key(string: &mut String, key: &str, indent: bool) {
string.push('"');
escape_json_string(string, key.as_bytes());
if indent {
string.push_str("\": ");
} else {
string.push_str("\":");
}
const SUFFIXES: [&str; 2] = ["\":", "\": "];
string.push_str(SUFFIXES[indent as usize]);
}

#[inline(always)]
fn write_sep(result: &mut String, add_comma: bool, has_indentation: bool) {
if !add_comma && !has_indentation {
return;
}

const SEPARATOR_TABLE: [&str; 4] = [
"", // add_comma = false, has_indentation = false
",", // add_comma = false, has_indentation = true
"\n", // add_comma = true, has_indentation = false
",\n", // add_comma = true, has_indentation = true
];

let separator = SEPARATOR_TABLE[(add_comma as usize) | ((has_indentation as usize) << 1)];
result.push_str(separator);
let index = (add_comma as usize) | ((has_indentation as usize) << 1);
result.push_str(SEPARATOR_TABLE[index]);
}

#[inline(always)]
Expand All @@ -394,7 +389,7 @@ fn get_key_or_index(key: Option<&str>, index: Option<usize>) -> String {
}

#[inline(always)]
fn iterate(context: &mut IterationContext<'_, '_>) -> Result<()> {
fn iterate(context: &mut StringifyContext<'_, '_>) -> Result<()> {
let mut add_comma;
let mut value_written;
let elem = context.value;
Expand Down Expand Up @@ -429,7 +424,7 @@ fn iterate(context: &mut IterationContext<'_, '_>) -> Result<()> {
let val = js_object.get(&key)?;

add_comma = append_value(
&mut IterationContext {
&mut StringifyContext {
ctx,
result: context.result,
value: &val,
Expand Down Expand Up @@ -471,7 +466,7 @@ fn iterate(context: &mut IterationContext<'_, '_>) -> Result<()> {
for (i, val) in js_array.iter::<Value>().enumerate() {
let val = val?;
add_comma = append_value(
&mut IterationContext {
&mut StringifyContext {
ctx,
result: context.result,
value: &val,
Expand Down
Loading