Skip to content

Commit 9b59a4e

Browse files
committed
default toString
1 parent 97a42b8 commit 9b59a4e

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

serde_v8/ser.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
22
use serde::ser;
33
use serde::ser::Serialize;
4+
use v8::PropertyAttribute;
45

56
use std::cell::RefCell;
67
use std::ops::DerefMut;
@@ -194,6 +195,15 @@ impl<'a, 'b, 'c> ObjectSerializer<'a, 'b, 'c> {
194195
}
195196
}
196197

198+
fn default_to_string(
199+
scope: &mut v8::HandleScope,
200+
_args: v8::FunctionCallbackArguments,
201+
mut rv: v8::ReturnValue,
202+
) {
203+
let result = v8::String::new_from_utf8(scope, "[object]".as_bytes(), v8::NewStringType::Internalized).unwrap();
204+
rv.set(result.into());
205+
}
206+
197207
impl<'a, 'b, 'c> ser::SerializeStruct for ObjectSerializer<'a, 'b, 'c> {
198208
type Ok = JsValue<'a>;
199209
type Error = Error;
@@ -220,6 +230,11 @@ impl<'a, 'b, 'c> ser::SerializeStruct for ObjectSerializer<'a, 'b, 'c> {
220230
&self.keys[..],
221231
&self.values[..],
222232
);
233+
234+
let name = v8::String::new_from_utf8(scope, "toString".as_bytes(), v8::NewStringType::Internalized).unwrap();
235+
let func = v8::Function::new(scope, default_to_string).unwrap();
236+
let _ = obj.define_own_property(scope, name.into(), func.into(), PropertyAttribute::NONE);
237+
223238
Ok(obj.into())
224239
}
225240
}

0 commit comments

Comments
 (0)