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