Browse Source

LibJS: Add Function.prototype and make "new" Objects delegate to it

Andreas Kling 5 years ago
parent
commit
37fe16a99c
2 changed files with 6 additions and 0 deletions
  1. 3 0
      Libraries/LibJS/AST.cpp
  2. 3 0
      Libraries/LibJS/Runtime/Function.cpp

+ 3 - 0
Libraries/LibJS/AST.cpp

@@ -83,6 +83,9 @@ Value CallExpression::execute(Interpreter& interpreter) const
     Object* new_object = nullptr;
     if (is_new_expression()) {
         new_object = interpreter.heap().allocate<Object>();
+        auto prototype = function->get("prototype");
+        if (prototype.has_value() && prototype.value().is_object())
+            new_object->set_prototype(prototype.value().as_object());
         call_frame.this_value = new_object;
     } else {
         if (m_callee->is_member_expression()) {

+ 3 - 0
Libraries/LibJS/Runtime/Function.cpp

@@ -24,6 +24,8 @@
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+#include <AK/FlyString.h>
+#include <LibJS/Heap/Heap.h>
 #include <LibJS/Runtime/Function.h>
 #include <LibJS/Runtime/Value.h>
 
@@ -31,6 +33,7 @@ namespace JS {
 
 Function::Function()
 {
+    put("prototype", heap().allocate<Object>());
 }
 
 Function::~Function()