LibWeb+LibJS: Add [LegacyNullToEmptyString] attribute
If specified, to_string() returns an empty string instead of "null" for null values.
This commit is contained in:
parent
1745e503aa
commit
bb22b04d44
Notes:
sideshowbarker
2024-07-19 01:27:19 +09:00
Author: https://github.com/Lubrsi Commit: https://github.com/SerenityOS/serenity/commit/bb22b04d442 Pull-request: https://github.com/SerenityOS/serenity/pull/4044
3 changed files with 7 additions and 6 deletions
|
@ -143,13 +143,13 @@ PrimitiveString* Value::to_primitive_string(GlobalObject& global_object)
|
|||
return js_string(global_object.heap(), string);
|
||||
}
|
||||
|
||||
String Value::to_string(GlobalObject& global_object) const
|
||||
String Value::to_string(GlobalObject& global_object, bool legacy_null_to_empty_string) const
|
||||
{
|
||||
switch (m_type) {
|
||||
case Type::Undefined:
|
||||
return "undefined";
|
||||
case Type::Null:
|
||||
return "null";
|
||||
return !legacy_null_to_empty_string ? "null" : String::empty();
|
||||
case Type::Boolean:
|
||||
return m_value.as_bool ? "true" : "false";
|
||||
case Type::Number:
|
||||
|
|
|
@ -241,7 +241,7 @@ public:
|
|||
i32 as_i32() const;
|
||||
size_t as_size_t() const;
|
||||
|
||||
String to_string(GlobalObject&) const;
|
||||
String to_string(GlobalObject&, bool legacy_null_to_empty_string = false) const;
|
||||
PrimitiveString* to_primitive_string(GlobalObject&);
|
||||
Value to_primitive(PreferredType preferred_type = PreferredType::Default) const;
|
||||
Object* to_object(GlobalObject&) const;
|
||||
|
|
|
@ -594,11 +594,12 @@ static @fully_qualified_name@* impl_from(JS::VM& vm, JS::GlobalObject& global_ob
|
|||
)~~~");
|
||||
}
|
||||
|
||||
auto generate_to_cpp = [&](auto& parameter, auto& js_name, const auto& js_suffix, auto cpp_name, bool return_void = false) {
|
||||
auto generate_to_cpp = [&](auto& parameter, auto& js_name, const auto& js_suffix, auto cpp_name, bool return_void = false, bool legacy_null_to_empty_string = false) {
|
||||
auto scoped_generator = generator.fork();
|
||||
scoped_generator.set("cpp_name", cpp_name);
|
||||
scoped_generator.set("js_name", js_name);
|
||||
scoped_generator.set("js_suffix", js_suffix);
|
||||
scoped_generator.set("legacy_null_to_empty_string", legacy_null_to_empty_string ? "true" : "false");
|
||||
scoped_generator.set("parameter.type.name", parameter.type.name);
|
||||
|
||||
if (return_void)
|
||||
|
@ -608,7 +609,7 @@ static @fully_qualified_name@* impl_from(JS::VM& vm, JS::GlobalObject& global_ob
|
|||
|
||||
if (parameter.type.name == "DOMString") {
|
||||
scoped_generator.append(R"~~~(
|
||||
auto @cpp_name@ = @js_name@@js_suffix@.to_string(global_object);
|
||||
auto @cpp_name@ = @js_name@@js_suffix@.to_string(global_object, @legacy_null_to_empty_string@);
|
||||
if (vm.exception())
|
||||
@return_statement@
|
||||
)~~~");
|
||||
|
@ -785,7 +786,7 @@ JS_DEFINE_NATIVE_SETTER(@wrapper_class@::@attribute.setter_callback@)
|
|||
return;
|
||||
)~~~");
|
||||
|
||||
generate_to_cpp(attribute, "value", "", "cpp_value", true);
|
||||
generate_to_cpp(attribute, "value", "", "cpp_value", true, attribute.extended_attributes.contains("LegacyNullToEmptyString"));
|
||||
|
||||
if (attribute.extended_attributes.contains("Reflect")) {
|
||||
if (attribute.type.name != "boolean") {
|
||||
|
|
Loading…
Add table
Reference in a new issue