FunctionPrototype.cpp 7.1 KB

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