diff options
author | Luke <luke.wilde@live.co.uk> | 2020-11-11 09:44:46 +0000 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-11-11 12:15:05 +0100 |
commit | bb22b04d442b67652b8fe12619074926846f1bb4 (patch) | |
tree | 9469812ab7334d939363095bad4c65d4b6062508 | |
parent | 1745e503aa95a571a28809f53bb71f46d8c546ef (diff) | |
download | serenity-bb22b04d442b67652b8fe12619074926846f1bb4.zip |
LibWeb+LibJS: Add [LegacyNullToEmptyString] attribute
If specified, to_string() returns an empty string instead of "null" for
null values.
-rw-r--r-- | Libraries/LibJS/Runtime/Value.cpp | 4 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/Value.h | 2 | ||||
-rw-r--r-- | Libraries/LibWeb/CodeGenerators/WrapperGenerator.cpp | 7 |
3 files changed, 7 insertions, 6 deletions
diff --git a/Libraries/LibJS/Runtime/Value.cpp b/Libraries/LibJS/Runtime/Value.cpp index a0b6a4f96d..ccb3f52039 100644 --- a/Libraries/LibJS/Runtime/Value.cpp +++ b/Libraries/LibJS/Runtime/Value.cpp @@ -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: diff --git a/Libraries/LibJS/Runtime/Value.h b/Libraries/LibJS/Runtime/Value.h index 09bfafd13d..e86ec76124 100644 --- a/Libraries/LibJS/Runtime/Value.h +++ b/Libraries/LibJS/Runtime/Value.h @@ -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; diff --git a/Libraries/LibWeb/CodeGenerators/WrapperGenerator.cpp b/Libraries/LibWeb/CodeGenerators/WrapperGenerator.cpp index df7378da44..e18bcf7c47 100644 --- a/Libraries/LibWeb/CodeGenerators/WrapperGenerator.cpp +++ b/Libraries/LibWeb/CodeGenerators/WrapperGenerator.cpp @@ -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") { |