FunctionPrototype.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
  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/StringBuilder.h>
  28. #include <LibJS/AST.h>
  29. #include <LibJS/Interpreter.h>
  30. #include <LibJS/Runtime/BoundFunction.h>
  31. #include <LibJS/Runtime/Error.h>
  32. #include <LibJS/Runtime/Function.h>
  33. #include <LibJS/Runtime/FunctionPrototype.h>
  34. #include <LibJS/Runtime/GlobalObject.h>
  35. #include <LibJS/Runtime/MarkedValueList.h>
  36. #include <LibJS/Runtime/NativeFunction.h>
  37. #include <LibJS/Runtime/ScriptFunction.h>
  38. namespace JS {
  39. FunctionPrototype::FunctionPrototype(GlobalObject& global_object)
  40. : Object(*global_object.object_prototype())
  41. {
  42. }
  43. void FunctionPrototype::initialize(GlobalObject& global_object)
  44. {
  45. auto& vm = this->vm();
  46. Object::initialize(global_object);
  47. u8 attr = Attribute::Writable | Attribute::Configurable;
  48. define_native_function(vm.names.apply, apply, 2, attr);
  49. define_native_function(vm.names.bind, bind, 1, attr);
  50. define_native_function(vm.names.call, call, 1, attr);
  51. define_native_function(vm.names.toString, to_string, 0, attr);
  52. define_native_function(vm.well_known_symbol_has_instance(), symbol_has_instance, 1, 0);
  53. define_property(vm.names.length, Value(0), Attribute::Configurable);
  54. define_property(vm.names.name, js_string(heap(), ""), Attribute::Configurable);
  55. }
  56. FunctionPrototype::~FunctionPrototype()
  57. {
  58. }
  59. JS_DEFINE_NATIVE_FUNCTION(FunctionPrototype::apply)
  60. {
  61. auto* this_object = vm.this_value(global_object).to_object(global_object);
  62. if (!this_object)
  63. return {};
  64. if (!this_object->is_function()) {
  65. vm.throw_exception<TypeError>(global_object, ErrorType::NotA, "Function");
  66. return {};
  67. }
  68. auto& function = static_cast<Function&>(*this_object);
  69. auto this_arg = vm.argument(0);
  70. auto arg_array = vm.argument(1);
  71. if (arg_array.is_nullish())
  72. return vm.call(function, this_arg);
  73. if (!arg_array.is_object()) {
  74. vm.throw_exception<TypeError>(global_object, ErrorType::FunctionArgsNotObject);
  75. return {};
  76. }
  77. auto length_property = arg_array.as_object().get(vm.names.length);
  78. if (vm.exception())
  79. return {};
  80. auto length = length_property.to_size_t(global_object);
  81. if (vm.exception())
  82. return {};
  83. MarkedValueList arguments(vm.heap());
  84. for (size_t i = 0; i < length; ++i) {
  85. auto element = arg_array.as_object().get(i);
  86. if (vm.exception())
  87. return {};
  88. arguments.append(element.value_or(js_undefined()));
  89. }
  90. return vm.call(function, this_arg, move(arguments));
  91. }
  92. JS_DEFINE_NATIVE_FUNCTION(FunctionPrototype::bind)
  93. {
  94. auto* this_object = vm.this_value(global_object).to_object(global_object);
  95. if (!this_object)
  96. return {};
  97. if (!this_object->is_function()) {
  98. vm.throw_exception<TypeError>(global_object, ErrorType::NotA, "Function");
  99. return {};
  100. }
  101. auto& this_function = static_cast<Function&>(*this_object);
  102. auto bound_this_arg = vm.argument(0);
  103. Vector<Value> arguments;
  104. if (vm.argument_count() > 1) {
  105. arguments = vm.call_frame().arguments;
  106. arguments.remove(0);
  107. }
  108. return this_function.bind(bound_this_arg, move(arguments));
  109. }
  110. JS_DEFINE_NATIVE_FUNCTION(FunctionPrototype::call)
  111. {
  112. auto* this_object = vm.this_value(global_object).to_object(global_object);
  113. if (!this_object)
  114. return {};
  115. if (!this_object->is_function()) {
  116. vm.throw_exception<TypeError>(global_object, ErrorType::NotA, "Function");
  117. return {};
  118. }
  119. auto& function = static_cast<Function&>(*this_object);
  120. auto this_arg = vm.argument(0);
  121. MarkedValueList arguments(vm.heap());
  122. if (vm.argument_count() > 1) {
  123. for (size_t i = 1; i < vm.argument_count(); ++i)
  124. arguments.append(vm.argument(i));
  125. }
  126. return vm.call(function, this_arg, move(arguments));
  127. }
  128. JS_DEFINE_NATIVE_FUNCTION(FunctionPrototype::to_string)
  129. {
  130. auto* this_object = vm.this_value(global_object).to_object(global_object);
  131. if (!this_object)
  132. return {};
  133. if (!this_object->is_function()) {
  134. vm.throw_exception<TypeError>(global_object, ErrorType::NotA, "Function");
  135. return {};
  136. }
  137. String function_name = static_cast<Function*>(this_object)->name();
  138. String function_parameters = "";
  139. String function_body;
  140. if (is<NativeFunction>(this_object) || is<BoundFunction>(this_object)) {
  141. function_body = String::formatted(" [{}]", this_object->class_name());
  142. } else {
  143. StringBuilder parameters_builder;
  144. auto first = true;
  145. for (auto& parameter : static_cast<ScriptFunction*>(this_object)->parameters()) {
  146. if (!first)
  147. parameters_builder.append(", ");
  148. first = false;
  149. parameters_builder.append(parameter.name);
  150. if (parameter.default_value) {
  151. // FIXME: See note below
  152. parameters_builder.append(" = TODO");
  153. }
  154. }
  155. function_parameters = parameters_builder.build();
  156. // FIXME: ASTNodes should be able to dump themselves to source strings - something like this:
  157. // auto& body = static_cast<ScriptFunction*>(this_object)->body();
  158. // function_body = body.to_source();
  159. function_body = " ???";
  160. }
  161. auto function_source = String::formatted(
  162. "function {}({}) {{\n{}\n}}",
  163. function_name.is_null() ? "" : function_name, function_parameters, function_body);
  164. return js_string(vm, function_source);
  165. }
  166. JS_DEFINE_NATIVE_FUNCTION(FunctionPrototype::symbol_has_instance)
  167. {
  168. auto* this_object = vm.this_value(global_object).to_object(global_object);
  169. if (!this_object)
  170. return {};
  171. if (!this_object->is_function()) {
  172. vm.throw_exception<TypeError>(global_object, ErrorType::NotA, "Function");
  173. return {};
  174. }
  175. return ordinary_has_instance(global_object, vm.argument(0), this_object);
  176. }
  177. }