ObjectPrototype.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Function.h>
  27. #include <AK/String.h>
  28. #include <LibJS/Heap/Heap.h>
  29. #include <LibJS/Runtime/BooleanObject.h>
  30. #include <LibJS/Runtime/Date.h>
  31. #include <LibJS/Runtime/GlobalObject.h>
  32. #include <LibJS/Runtime/NumberObject.h>
  33. #include <LibJS/Runtime/ObjectPrototype.h>
  34. #include <LibJS/Runtime/RegExpObject.h>
  35. #include <LibJS/Runtime/StringObject.h>
  36. #include <LibJS/Runtime/Value.h>
  37. namespace JS {
  38. ObjectPrototype::ObjectPrototype(GlobalObject& global_object)
  39. : Object(Object::ConstructWithoutPrototypeTag::Tag, global_object)
  40. {
  41. }
  42. void ObjectPrototype::initialize(GlobalObject& global_object)
  43. {
  44. auto& vm = this->vm();
  45. Object::initialize(global_object);
  46. // This must be called after the constructor has returned, so that the below code
  47. // can find the ObjectPrototype through normal paths.
  48. u8 attr = Attribute::Writable | Attribute::Configurable;
  49. define_native_function(vm.names.hasOwnProperty, has_own_property, 1, attr);
  50. define_native_function(vm.names.toString, to_string, 0, attr);
  51. define_native_function(vm.names.toLocaleString, to_locale_string, 0, attr);
  52. define_native_function(vm.names.valueOf, value_of, 0, attr);
  53. define_native_function(vm.names.propertyIsEnumerable, property_is_enumerable, 1, attr);
  54. define_native_function(vm.names.isPrototypeOf, is_prototype_of, 1, attr);
  55. }
  56. ObjectPrototype::~ObjectPrototype()
  57. {
  58. }
  59. JS_DEFINE_NATIVE_FUNCTION(ObjectPrototype::has_own_property)
  60. {
  61. auto* this_object = vm.this_value(global_object).to_object(global_object);
  62. if (!this_object)
  63. return {};
  64. auto name = vm.argument(0).to_string(global_object);
  65. if (vm.exception())
  66. return {};
  67. return Value(this_object->has_own_property(name));
  68. }
  69. JS_DEFINE_NATIVE_FUNCTION(ObjectPrototype::to_string)
  70. {
  71. auto this_value = vm.this_value(global_object);
  72. if (this_value.is_undefined())
  73. return js_string(vm, "[object Undefined]");
  74. if (this_value.is_null())
  75. return js_string(vm, "[object Null]");
  76. auto* this_object = this_value.to_object(global_object);
  77. if (!this_object)
  78. return {};
  79. String tag;
  80. auto to_string_tag = this_object->get(global_object.vm().well_known_symbol_to_string_tag());
  81. if (to_string_tag.is_string()) {
  82. tag = to_string_tag.as_string().string();
  83. } else if (this_object->is_array()) {
  84. tag = "Array";
  85. } else if (this_object->is_function()) {
  86. tag = "Function";
  87. } else if (is<Error>(this_object)) {
  88. tag = "Error";
  89. } else if (is<BooleanObject>(this_object)) {
  90. tag = "Boolean";
  91. } else if (is<NumberObject>(this_object)) {
  92. tag = "Number";
  93. } else if (is<StringObject>(this_object)) {
  94. tag = "String";
  95. } else if (is<Date>(this_object)) {
  96. tag = "Date";
  97. } else if (is<RegExpObject>(this_object)) {
  98. tag = "RegExp";
  99. } else {
  100. tag = "Object";
  101. }
  102. return js_string(vm, String::formatted("[object {}]", tag));
  103. }
  104. JS_DEFINE_NATIVE_FUNCTION(ObjectPrototype::to_locale_string)
  105. {
  106. auto* this_object = vm.this_value(global_object).to_object(global_object);
  107. if (!this_object)
  108. return {};
  109. return this_object->invoke("toString");
  110. }
  111. JS_DEFINE_NATIVE_FUNCTION(ObjectPrototype::value_of)
  112. {
  113. auto* this_object = vm.this_value(global_object).to_object(global_object);
  114. if (!this_object)
  115. return {};
  116. return this_object->value_of();
  117. }
  118. JS_DEFINE_NATIVE_FUNCTION(ObjectPrototype::property_is_enumerable)
  119. {
  120. auto name = vm.argument(0).to_string(global_object);
  121. if (vm.exception())
  122. return {};
  123. auto* this_object = vm.this_value(global_object).to_object(global_object);
  124. if (!this_object)
  125. return {};
  126. auto property_descriptor = this_object->get_own_property_descriptor(name);
  127. if (!property_descriptor.has_value())
  128. return Value(false);
  129. return Value(property_descriptor.value().attributes.is_enumerable());
  130. }
  131. JS_DEFINE_NATIVE_FUNCTION(ObjectPrototype::is_prototype_of)
  132. {
  133. auto object_argument = vm.argument(0);
  134. if (!object_argument.is_object())
  135. return Value(false);
  136. auto* object = &object_argument.as_object();
  137. auto* this_object = vm.this_value(global_object).to_object(global_object);
  138. if (!this_object)
  139. return {};
  140. for (;;) {
  141. object = object->prototype();
  142. if (!object)
  143. return Value(false);
  144. if (same_value(this_object, object))
  145. return Value(true);
  146. }
  147. }
  148. }