FunctionPrototype.cpp 6.4 KB

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