|
@@ -6,6 +6,7 @@
|
|
|
*/
|
|
|
|
|
|
#include <AK/Function.h>
|
|
|
+#include <LibJS/AST.h>
|
|
|
#include <LibJS/Runtime/Error.h>
|
|
|
#include <LibJS/Runtime/ErrorPrototype.h>
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
|
@@ -27,6 +28,10 @@ void ErrorPrototype::initialize(GlobalObject& global_object)
|
|
|
define_direct_property(vm.names.name, js_string(vm, "Error"), attr);
|
|
|
define_direct_property(vm.names.message, js_string(vm, ""), attr);
|
|
|
define_native_function(vm.names.toString, to_string, 0, attr);
|
|
|
+ // Non standard property "stack"
|
|
|
+ // Every other engine seems to have this in some way or another, and the spec
|
|
|
+ // proposal for this is only Stage 1
|
|
|
+ define_native_accessor(vm.names.stack, stack, nullptr, attr);
|
|
|
}
|
|
|
|
|
|
// 20.5.3.4 Error.prototype.toString ( ), https://tc39.es/ecma262/#sec-error.prototype.tostring
|
|
@@ -54,6 +59,34 @@ JS_DEFINE_NATIVE_FUNCTION(ErrorPrototype::to_string)
|
|
|
return js_string(vm, String::formatted("{}: {}", name, message));
|
|
|
}
|
|
|
|
|
|
+JS_DEFINE_NATIVE_FUNCTION(ErrorPrototype::stack)
|
|
|
+{
|
|
|
+ auto this_value = vm.this_value(global_object);
|
|
|
+ if (!this_value.is_object())
|
|
|
+ return vm.throw_completion<TypeError>(global_object, ErrorType::NotAnObject, this_value.to_string_without_side_effects());
|
|
|
+ auto& this_object = this_value.as_object();
|
|
|
+
|
|
|
+ if (!is<Error>(this_object))
|
|
|
+ return vm.throw_completion<TypeError>(global_object, ErrorType::NotAnObjectOfType, "Error");
|
|
|
+
|
|
|
+ String name = "Error";
|
|
|
+ auto name_property = TRY(this_object.get(vm.names.name));
|
|
|
+ if (!name_property.is_undefined())
|
|
|
+ name = TRY(name_property.to_string(global_object));
|
|
|
+
|
|
|
+ String message = "";
|
|
|
+ auto message_property = TRY(this_object.get(vm.names.message));
|
|
|
+ if (!message_property.is_undefined())
|
|
|
+ message = TRY(message_property.to_string(global_object));
|
|
|
+
|
|
|
+ String header = name;
|
|
|
+ if (!message.is_empty())
|
|
|
+ header = String::formatted("{}: {}", name, message);
|
|
|
+
|
|
|
+ return js_string(vm,
|
|
|
+ String::formatted("{}\n{}", header, static_cast<Error&>(this_object).stack_string()));
|
|
|
+}
|
|
|
+
|
|
|
#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \
|
|
|
PrototypeName::PrototypeName(GlobalObject& global_object) \
|
|
|
: Object(*global_object.error_prototype()) \
|