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 issues between reference counting and the custom JSON.parse implementation #673

Merged
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
34 changes: 9 additions & 25 deletions crates/javy/src/serde/ser.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use crate::quickjs::{
qjs::{JS_DefinePropertyValue, JS_ValueToAtom, JS_PROP_C_W_E},
Array, Ctx, Object, String as JSString, Value,
};
use crate::quickjs::{object::Property, Array, Ctx, Object, String as JSString, Value};
use crate::serde::err::{Error, Result};
use anyhow::anyhow;

Expand Down Expand Up @@ -340,29 +337,16 @@ impl<'a> ser::SerializeMap for &'a mut Serializer<'_> {
{
let mut map_serializer = Serializer::from_context(self.context.clone())?;
value.serialize(&mut map_serializer)?;
let atom = unsafe { JS_ValueToAtom(self.context.as_raw().as_ptr(), self.key.as_raw()) };

if let Some(o) = self.value.as_object() {
// Use `JS_DefinePropertyValue` to keep the semantics of the object
// unchanged.
let result = unsafe {
JS_DefinePropertyValue(
self.context.as_raw().as_ptr(),
o.as_raw(),
atom,
map_serializer.value.as_raw(),
JS_PROP_C_W_E as i32,
)
};

return if result != 0 {
Ok(())
} else {
Err(Error::custom("Error while serializing object"))
};
let prop = Property::from(map_serializer.value.clone())
.writable()
.configurable()
.enumerable();
o.prop::<_, _, _>(self.key.clone(), prop)
.map_err(|e| Error::custom(e.to_string()))
} else {
Err(Error::custom("Expected to be an object"))
}

Err(Error::custom("Expected to be an object"))
}

fn end(self) -> Result<()> {
Expand Down
21 changes: 21 additions & 0 deletions crates/javy/tests/misc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use anyhow::Result;
use javy::{quickjs::context::EvalOptions, Config, Runtime};

#[cfg(feature = "json")]
#[test]
fn string_keys_and_ref_counting() -> Result<()> {
let mut config = Config::default();
config.override_json_parse_and_stringify(true);

let source = include_bytes!("string_keys_and_ref_counting.js");
let rt = Runtime::new(config)?;

rt.context().with(|this| {
let _: () = this
.eval_with_options(*source, EvalOptions::default())
.inspect_err(|e| println!("{e}"))
.expect("source evaluation to succeed");
});

Ok(())
}
8 changes: 8 additions & 0 deletions crates/javy/tests/string_keys_and_ref_counting.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const input = JSON.parse('{"elements":[{"id":"zza"},{"id":"zzb"},{"id":"zzc"},{"id":"zzd"},{"id":"zze"}]}');

const acc = {};
input.elements.forEach(e => {
if (!acc[e.id]) {
acc[e.id] = 1;
}
});
Loading