1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-22 14:45:08 +00:00

LibJS: Add js_string(Interpreter&, String)

This commit is contained in:
Andreas Kling 2020-04-04 12:57:37 +02:00
parent d8e944e899
commit faac43597a
11 changed files with 33 additions and 26 deletions

View file

@ -323,19 +323,19 @@ Value UnaryExpression::execute(Interpreter& interpreter) const
case UnaryOp::Typeof:
switch (lhs_result.type()) {
case Value::Type::Undefined:
return js_string(interpreter.heap(), "undefined");
return js_string(interpreter, "undefined");
case Value::Type::Null:
// yes, this is on purpose. yes, this is how javascript works.
// yes, it's silly.
return js_string(interpreter.heap(), "object");
return js_string(interpreter, "object");
case Value::Type::Number:
return js_string(interpreter.heap(), "number");
return js_string(interpreter, "number");
case Value::Type::String:
return js_string(interpreter.heap(), "string");
return js_string(interpreter, "string");
case Value::Type::Object:
return js_string(interpreter.heap(), "object");
return js_string(interpreter, "object");
case Value::Type::Boolean:
return js_string(interpreter.heap(), "boolean");
return js_string(interpreter, "boolean");
}
}
@ -806,7 +806,7 @@ Value MemberExpression::execute(Interpreter& interpreter) const
Value StringLiteral::execute(Interpreter& interpreter) const
{
return js_string(interpreter.heap(), m_value);
return js_string(interpreter, m_value);
}
Value NumericLiteral::execute(Interpreter&) const

View file

@ -50,7 +50,7 @@ Value DateConstructor::call(Interpreter& interpreter)
auto date = construct(interpreter);
if (!date.is_object())
return {};
return js_string(interpreter.heap(), static_cast<Date&>(date.as_object()).string());
return js_string(interpreter, static_cast<Date&>(date.as_object()).string());
}
Value DateConstructor::construct(Interpreter& interpreter)

View file

@ -155,7 +155,7 @@ Value DatePrototype::to_date_string(Interpreter& interpreter)
if (!this_object)
return {};
auto string = this_object->date_string();
return js_string(interpreter.heap(), move(string));
return js_string(interpreter, move(string));
}
Value DatePrototype::to_time_string(Interpreter& interpreter)
@ -164,7 +164,7 @@ Value DatePrototype::to_time_string(Interpreter& interpreter)
if (!this_object)
return {};
auto string = this_object->time_string();
return js_string(interpreter.heap(), move(string));
return js_string(interpreter, move(string));
}
Value DatePrototype::to_string(Interpreter& interpreter)
@ -173,7 +173,7 @@ Value DatePrototype::to_string(Interpreter& interpreter)
if (!this_object)
return {};
auto string = this_object->string();
return js_string(interpreter.heap(), move(string));
return js_string(interpreter, move(string));
}
}

View file

@ -52,7 +52,7 @@ Value ErrorPrototype::name_getter(Interpreter& interpreter)
return {};
if (!this_object->is_error())
return interpreter.throw_exception<Error>("TypeError", "Not an Error object");
return js_string(interpreter.heap(), static_cast<const Error*>(this_object)->name());
return js_string(interpreter, static_cast<const Error*>(this_object)->name());
}
Value ErrorPrototype::message_getter(Interpreter& interpreter)
@ -62,7 +62,7 @@ Value ErrorPrototype::message_getter(Interpreter& interpreter)
return {};
if (!this_object->is_error())
return interpreter.throw_exception<Error>("TypeError", "Not an Error object");
return js_string(interpreter.heap(), static_cast<const Error*>(this_object)->message());
return js_string(interpreter, static_cast<const Error*>(this_object)->message());
}
Value ErrorPrototype::to_string(Interpreter& interpreter)
@ -82,10 +82,10 @@ Value ErrorPrototype::to_string(Interpreter& interpreter)
message = object_message_property.value().to_string();
if (name.length() == 0)
return js_string(interpreter.heap(), message);
return js_string(interpreter, message);
if (message.length() == 0)
return js_string(interpreter.heap(), name);
return js_string(interpreter.heap(), String::format("%s: %s", name.characters(), message.characters()));
return js_string(interpreter, name);
return js_string(interpreter, String::format("%s: %s", name.characters(), message.characters()));
}
}

View file

@ -67,10 +67,10 @@ Value ObjectConstructor::get_own_property_names(Interpreter& interpreter)
if (object->is_array()) {
auto* array = static_cast<const Array*>(object);
for (i32 i = 0; i < array->length(); ++i)
result->push(js_string(interpreter.heap(), String::number(i)));
result->push(js_string(interpreter, String::number(i)));
}
for (auto& it : object->shape().property_table())
result->push(js_string(interpreter.heap(), it.key));
result->push(js_string(interpreter, it.key));
return result;
}

View file

@ -25,6 +25,7 @@
*/
#include <LibJS/Heap/Heap.h>
#include <LibJS/Interpreter.h>
#include <LibJS/Runtime/PrimitiveString.h>
namespace JS {
@ -43,4 +44,9 @@ PrimitiveString* js_string(Heap& heap, String string)
return heap.allocate<PrimitiveString>(move(string));
}
PrimitiveString* js_string(Interpreter& interpreter, String string)
{
return js_string(interpreter.heap(), string);
}
}

View file

@ -45,5 +45,6 @@ private:
};
PrimitiveString* js_string(Heap&, String);
PrimitiveString* js_string(Interpreter&, String);
}

View file

@ -59,8 +59,8 @@ Value StringPrototype::char_at(Interpreter& interpreter)
ASSERT(this_object->is_string_object());
auto underlying_string = static_cast<const StringObject*>(this_object)->primitive_string()->string();
if (index < 0 || index >= static_cast<i32>(underlying_string.length()))
return js_string(interpreter.heap(), String::empty());
return js_string(interpreter.heap(), underlying_string.substring(index, 1));
return js_string(interpreter, String::empty());
return js_string(interpreter, underlying_string.substring(index, 1));
}
Value StringPrototype::repeat(Interpreter& interpreter)
@ -70,7 +70,7 @@ Value StringPrototype::repeat(Interpreter& interpreter)
return {};
ASSERT(this_object->is_string_object());
if (!interpreter.argument_count())
return js_string(interpreter.heap(), String::empty());
return js_string(interpreter, String::empty());
i32 count = 0;
count = interpreter.argument(0).to_i32();
if (count < 0) {
@ -81,7 +81,7 @@ Value StringPrototype::repeat(Interpreter& interpreter)
StringBuilder builder;
for (i32 i = 0; i < count; ++i)
builder.append(string_object->primitive_string()->string());
return js_string(interpreter.heap(), builder.to_string());
return js_string(interpreter, builder.to_string());
}
Value StringPrototype::starts_with(Interpreter& interpreter)

View file

@ -76,7 +76,7 @@ JS::Value CanvasRenderingContext2DWrapper::fill_style_getter(JS::Interpreter& in
auto* impl = impl_from(interpreter);
if (!impl)
return {};
return JS::js_string(interpreter.heap(), impl->fill_style());
return JS::js_string(interpreter, impl->fill_style());
}
void CanvasRenderingContext2DWrapper::fill_style_setter(JS::Interpreter& interpreter, JS::Value value)

View file

@ -68,7 +68,7 @@ static Element* impl_from(JS::Interpreter& interpreter)
JS::Value ElementWrapper::inner_html_getter(JS::Interpreter& interpreter)
{
if (auto* impl = impl_from(interpreter))
return JS::js_string(interpreter.heap(), impl->inner_html());
return JS::js_string(interpreter, impl->inner_html());
return {};
}
@ -81,7 +81,7 @@ void ElementWrapper::inner_html_setter(JS::Interpreter& interpreter, JS::Value v
JS::Value ElementWrapper::id_getter(JS::Interpreter& interpreter)
{
if (auto* impl = impl_from(interpreter))
return JS::js_string(interpreter.heap(), impl->attribute("id"));
return JS::js_string(interpreter, impl->attribute("id"));
return {};
}

View file

@ -277,7 +277,7 @@ JS::Value ReplObject::repl_help(JS::Interpreter& interpreter)
help_text.append(" help(): display this menu\n");
help_text.append(" load(files): Accepts file names as params to load into running session. For example repl.load(\"js/1.js\", \"js/2.js\", \"js/3.js\")\n");
String result = help_text.to_string();
return js_string(interpreter.heap(), result);
return js_string(interpreter, result);
}
JS::Value ReplObject::load_file(JS::Interpreter& interpreter)