FunctionPrototype.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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/Error.h>
  31. #include <LibJS/Runtime/Function.h>
  32. #include <LibJS/Runtime/FunctionPrototype.h>
  33. #include <LibJS/Runtime/GlobalObject.h>
  34. #include <LibJS/Runtime/ScriptFunction.h>
  35. namespace JS {
  36. FunctionPrototype::FunctionPrototype()
  37. : Object(interpreter().global_object().object_prototype())
  38. {
  39. }
  40. void FunctionPrototype::initialize()
  41. {
  42. put_native_function("apply", apply, 2);
  43. put_native_function("bind", bind, 1);
  44. put_native_function("call", call, 1);
  45. put_native_function("toString", to_string);
  46. put("length", Value(0));
  47. }
  48. FunctionPrototype::~FunctionPrototype()
  49. {
  50. }
  51. Value FunctionPrototype::apply(Interpreter& interpreter)
  52. {
  53. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  54. if (!this_object)
  55. return {};
  56. if (!this_object->is_function())
  57. return interpreter.throw_exception<TypeError>("Not a Function object");
  58. auto function = static_cast<Function*>(this_object);
  59. auto this_arg = interpreter.argument(0);
  60. auto arg_array = interpreter.argument(1);
  61. if (arg_array.is_null() || arg_array.is_undefined())
  62. return interpreter.call(function, this_arg);
  63. if (!arg_array.is_object())
  64. return interpreter.throw_exception<TypeError>("argument array must be an object");
  65. size_t length = 0;
  66. auto length_property = arg_array.as_object().get("length");
  67. if (length_property.has_value())
  68. length = length_property.value().to_number().to_i32();
  69. Vector<Value> arguments;
  70. for (size_t i = 0; i < length; ++i)
  71. arguments.append(arg_array.as_object().get(String::number(i)).value_or(js_undefined()));
  72. return interpreter.call(function, this_arg, arguments);
  73. }
  74. Value FunctionPrototype::bind(Interpreter& interpreter)
  75. {
  76. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  77. if (!this_object)
  78. return {};
  79. // FIXME: Implement me :^)
  80. ASSERT_NOT_REACHED();
  81. }
  82. Value FunctionPrototype::call(Interpreter& interpreter)
  83. {
  84. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  85. if (!this_object)
  86. return {};
  87. if (!this_object->is_function())
  88. return interpreter.throw_exception<TypeError>("Not a Function object");
  89. auto function = static_cast<Function*>(this_object);
  90. auto this_arg = interpreter.argument(0);
  91. Vector<Value> arguments;
  92. if (interpreter.argument_count() > 1) {
  93. for (size_t i = 1; i < interpreter.argument_count(); ++i)
  94. arguments.append(interpreter.argument(i));
  95. }
  96. return interpreter.call(function, this_arg, arguments);
  97. }
  98. Value FunctionPrototype::to_string(Interpreter& interpreter)
  99. {
  100. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  101. if (!this_object)
  102. return {};
  103. if (!this_object->is_function())
  104. return interpreter.throw_exception<TypeError>("Not a Function object");
  105. String function_name = static_cast<Function*>(this_object)->name();
  106. String function_parameters = "";
  107. String function_body;
  108. if (this_object->is_native_function()) {
  109. function_body = String::format(" [%s]", this_object->class_name());
  110. } else {
  111. auto& parameters = static_cast<ScriptFunction*>(this_object)->parameters();
  112. StringBuilder parameters_builder;
  113. parameters_builder.join(", ", parameters);
  114. function_parameters = parameters_builder.build();
  115. // FIXME: ASTNodes should be able to dump themselves to source strings - something like this:
  116. // auto& body = static_cast<ScriptFunction*>(this_object)->body();
  117. // function_body = body.to_source();
  118. function_body = " ???";
  119. }
  120. auto function_source = String::format("function %s(%s) {\n%s\n}",
  121. function_name.is_null() ? "" : function_name.characters(),
  122. function_parameters.characters(),
  123. function_body.characters());
  124. return js_string(interpreter, function_source);
  125. }
  126. }