mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 19:17:41 +00:00
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
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);
|
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) {
|
switch (m_type) {
|
||||||
case Type::Undefined:
|
case Type::Undefined:
|
||||||
return "undefined";
|
return "undefined";
|
||||||
case Type::Null:
|
case Type::Null:
|
||||||
return "null";
|
return !legacy_null_to_empty_string ? "null" : String::empty();
|
||||||
case Type::Boolean:
|
case Type::Boolean:
|
||||||
return m_value.as_bool ? "true" : "false";
|
return m_value.as_bool ? "true" : "false";
|
||||||
case Type::Number:
|
case Type::Number:
|
||||||
|
|
|
@ -241,7 +241,7 @@ public:
|
||||||
i32 as_i32() const;
|
i32 as_i32() const;
|
||||||
size_t as_size_t() 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&);
|
PrimitiveString* to_primitive_string(GlobalObject&);
|
||||||
Value to_primitive(PreferredType preferred_type = PreferredType::Default) const;
|
Value to_primitive(PreferredType preferred_type = PreferredType::Default) const;
|
||||||
Object* to_object(GlobalObject&) 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();
|
auto scoped_generator = generator.fork();
|
||||||
scoped_generator.set("cpp_name", cpp_name);
|
scoped_generator.set("cpp_name", cpp_name);
|
||||||
scoped_generator.set("js_name", js_name);
|
scoped_generator.set("js_name", js_name);
|
||||||
scoped_generator.set("js_suffix", js_suffix);
|
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);
|
scoped_generator.set("parameter.type.name", parameter.type.name);
|
||||||
|
|
||||||
if (return_void)
|
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") {
|
if (parameter.type.name == "DOMString") {
|
||||||
scoped_generator.append(R"~~~(
|
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())
|
if (vm.exception())
|
||||||
@return_statement@
|
@return_statement@
|
||||||
)~~~");
|
)~~~");
|
||||||
|
@ -785,7 +786,7 @@ JS_DEFINE_NATIVE_SETTER(@wrapper_class@::@attribute.setter_callback@)
|
||||||
return;
|
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.extended_attributes.contains("Reflect")) {
|
||||||
if (attribute.type.name != "boolean") {
|
if (attribute.type.name != "boolean") {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue