1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 06:48:12 +00:00

LibJS: Add String.raw

This commit is contained in:
Matthew Olsson 2020-05-06 17:49:48 -07:00 committed by Andreas Kling
parent b5f1df57ed
commit ab652fa1ee
3 changed files with 67 additions and 1 deletions

View file

@ -24,12 +24,13 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <AK/StringBuilder.h>
#include <LibJS/Interpreter.h>
#include <LibJS/Runtime/Array.h>
#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/StringConstructor.h>
#include <LibJS/Runtime/StringObject.h>
#include <math.h>
namespace JS {
@ -38,6 +39,8 @@ StringConstructor::StringConstructor()
{
put("prototype", interpreter().global_object().string_prototype(), 0);
put("length", Value(1), Attribute::Configurable);
put_native_function("raw", raw, 0, Attribute::Writable | Attribute::Configurable);
}
StringConstructor::~StringConstructor()
@ -63,4 +66,30 @@ Value StringConstructor::construct(Interpreter& interpreter)
return StringObject::create(interpreter.global_object(), *primitive_string);
}
Value StringConstructor::raw(Interpreter& interpreter)
{
auto* template_object = interpreter.argument(0).to_object(interpreter.heap());
if (interpreter.exception())
return {};
auto raw = template_object->get("raw");
if (raw.is_empty() || raw.is_undefined() || raw.is_null()) {
interpreter.throw_exception<TypeError>(String::format("Cannot convert property 'raw' to object from %s", raw.is_null() ? "null" : "undefined"));
return {};
}
if (!raw.is_array())
return js_string(interpreter, "");
auto& raw_array_elements = static_cast<Array*>(raw.to_object(interpreter.heap()))->elements();
StringBuilder builder;
for (size_t i = 0; i < raw_array_elements.size(); ++i) {
builder.append(raw_array_elements.at(i).to_string());
if (i + 1 < interpreter.argument_count() && i < raw_array_elements.size() - 1)
builder.append(interpreter.argument(i + 1).to_string());
}
return js_string(interpreter, builder.build());
}
}