FunctionPrototype.cpp 6.7 KB

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