瀏覽代碼

LibJS: Implement the OrdinaryCreateFromConstructor() abstract operation

Linus Groh 4 年之前
父節點
當前提交
df095afbcc
共有 1 個文件被更改,包括 12 次插入0 次删除
  1. 12 0
      Userland/Libraries/LibJS/Runtime/AbstractOperations.h

+ 12 - 0
Userland/Libraries/LibJS/Runtime/AbstractOperations.h

@@ -8,6 +8,7 @@
 
 #include <AK/Forward.h>
 #include <LibJS/Forward.h>
+#include <LibJS/Runtime/GlobalObject.h>
 #include <LibJS/Runtime/Value.h>
 
 namespace JS {
@@ -20,4 +21,15 @@ Function* species_constructor(GlobalObject&, Object const&, Function& default_co
 GlobalObject* get_function_realm(GlobalObject&, Function const&);
 Object* get_prototype_from_constructor(GlobalObject&, Function const& constructor, Object* (GlobalObject::*intrinsic_default_prototype)());
 
+// 10.1.13 OrdinaryCreateFromConstructor ( constructor, intrinsicDefaultProto [ , internalSlotsList ] ), https://tc39.es/ecma262/#sec-ordinarycreatefromconstructor
+template<typename T, typename... Args>
+T* ordinary_create_from_constructor(GlobalObject& global_object, Function const& constructor, Object* (GlobalObject::*intrinsic_default_prototype)(), Args&&... args)
+{
+    auto& vm = global_object.vm();
+    auto* prototype = get_prototype_from_constructor(global_object, constructor, intrinsic_default_prototype);
+    if (vm.exception())
+        return nullptr;
+    return global_object.heap().allocate<T>(global_object, forward<Args>(args)..., *prototype);
+}
+
 }