Skip to content

Commit

Permalink
LibJS: Add JSON.stringify
Browse files Browse the repository at this point in the history
  • Loading branch information
mattco98 authored and awesomekling committed Jun 13, 2020
1 parent b4577ff commit 39576b2
Show file tree
Hide file tree
Showing 19 changed files with 678 additions and 10 deletions.
1 change: 1 addition & 0 deletions Libraries/LibJS/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ set(SOURCES
Runtime/FunctionPrototype.cpp
Runtime/GlobalObject.cpp
Runtime/IndexedProperties.cpp
Runtime/JSONObject.cpp
Runtime/LexicalEnvironment.cpp
Runtime/MarkedValueList.cpp
Runtime/MathObject.cpp
Expand Down
5 changes: 1 addition & 4 deletions Libraries/LibJS/Runtime/ArrayConstructor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,7 @@ Value ArrayConstructor::construct(Interpreter& interpreter)
Value ArrayConstructor::is_array(Interpreter& interpreter)
{
auto value = interpreter.argument(0);
if (!value.is_array())
return Value(false);
// Exclude TypedArray and similar
return Value(StringView(value.as_object().class_name()) == "Array");
return Value(value.is_array());
}

Value ArrayConstructor::of(Interpreter& interpreter)
Expand Down
2 changes: 1 addition & 1 deletion Libraries/LibJS/Runtime/BooleanObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class BooleanObject : public Object {

private:
virtual const char* class_name() const override { return "BooleanObject"; }
virtual bool is_boolean() const override { return true; }
virtual bool is_boolean_object() const override { return true; }
bool m_value { false };
};
}
4 changes: 2 additions & 2 deletions Libraries/LibJS/Runtime/BooleanPrototype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Value BooleanPrototype::to_string(Interpreter& interpreter)
if (this_object.is_boolean()) {
return js_string(interpreter.heap(), this_object.as_bool() ? "true" : "false");
}
if (!this_object.is_object() || !this_object.as_object().is_boolean()) {
if (!this_object.is_object() || !this_object.as_object().is_boolean_object()) {
interpreter.throw_exception<TypeError>(ErrorType::NotA, "Boolean");
return {};
}
Expand All @@ -62,7 +62,7 @@ Value BooleanPrototype::value_of(Interpreter& interpreter)
if (this_object.is_boolean()) {
return this_object;
}
if (!this_object.is_object() || !this_object.as_object().is_boolean()) {
if (!this_object.is_object() || !this_object.as_object().is_boolean_object()) {
interpreter.throw_exception<TypeError>(ErrorType::NotA, "Boolean");
return {};
}
Expand Down
2 changes: 2 additions & 0 deletions Libraries/LibJS/Runtime/ErrorTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
M(InvalidLeftHandAssignment, "Invalid left-hand side in assignment") \
M(IsNotA, "%s is not a %s") \
M(IsNotAEvaluatedFrom, "%s is not a %s (evaluated from '%s')") \
M(JsonBigInt, "Cannot serialize BigInt value to JSON") \
M(JsonCircular, "Cannot stringify circular object") \
M(NotA, "Not a %s object") \
M(NotACtor, "%s is not a constructor") \
M(NotAFunction, "%s is not a function") \
Expand Down
2 changes: 2 additions & 0 deletions Libraries/LibJS/Runtime/GlobalObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include <LibJS/Runtime/FunctionConstructor.h>
#include <LibJS/Runtime/FunctionPrototype.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/JSONObject.h>
#include <LibJS/Runtime/MathObject.h>
#include <LibJS/Runtime/NativeFunction.h>
#include <LibJS/Runtime/NumberConstructor.h>
Expand Down Expand Up @@ -96,6 +97,7 @@ void GlobalObject::initialize()
define_property("globalThis", this, attr);
define_property("console", heap().allocate<ConsoleObject>(), attr);
define_property("Math", heap().allocate<MathObject>(), attr);
define_property("JSON", heap().allocate<JSONObject>(), attr);
define_property("Reflect", heap().allocate<ReflectObject>(), attr);

add_constructor("Array", m_array_constructor, *m_array_prototype);
Expand Down
Loading

0 comments on commit 39576b2

Please sign in to comment.