Skip to content

Commit

Permalink
Don't define prototype on arrow functions
Browse files Browse the repository at this point in the history
Summary: Functions defined with the arrow syntax should not have a `prototype` defined on them, as they cannot be used as constructors.

Reviewed By: avp

Differential Revision: D42178843

fbshipit-source-id: 7031f9e43b92cd6aa3698866025a02e775202f39
  • Loading branch information
Michael Anthony Leon authored and facebook-github-bot committed Feb 16, 2023
1 parent d0398d2 commit c42491d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/VM/Callable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,12 @@ void Callable::defineLazyProperties(Handle<Callable> fn, Runtime &runtime) {

// According to ES12 26.7.4, AsyncFunction instances do not have a
// 'prototype' property, hence we need to set an null handle here.
auto prototypeObjectHandle = vmisa<JSAsyncFunction>(*jsFun)
// Functions that cannot be used with `new` should also not define a
// 'prototype' property, such as arrow functions per 15.3.4, except for
// generator functions.
auto prototypeObjectHandle =
codeBlock->getHeaderFlags().isCallProhibited(/* construct */ true) &&
!vmisa<JSGeneratorFunction>(*jsFun)
? Runtime::makeNullHandle<JSObject>()
: runtime.makeHandle(JSObject::create(runtime, prototypeParent));

Expand Down
7 changes: 7 additions & 0 deletions test/hermes/function-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,10 @@ print(isNaN.name);
//CHECK: isNaN
print(Function.name);
//CHECK: Function

var a_func = function(){};
print(a_func.hasOwnProperty("prototype"));
//CHECK: true
var a_arrow = () => {};
print(a_arrow.hasOwnProperty("prototype"));
//CHECK: false

0 comments on commit c42491d

Please sign in to comment.