Skip to content

Short-hand methods don't have prototype / construct #1371

@mhofman

Description

@mhofman

Bug Description

Short hand method in object literals (e.g. { foo () {} } are not constructor functions and as such should not have a prototype property nor have a [[Construct]] behavior.

A similar issue was fixed last year for arrow functions in c42491d

  • I have run gradle clean and confirmed this bug does not occur with JSC
  • The issue is reproducible with the latest version of React Native.

Hermes git revision (if applicable): c2f7dcd
React Native version: N/A
OS: Linux
Platform (most likely one of arm64-v8a, armeabi-v7a, x86, x86_64): x86_64

Steps To Reproduce

const hasConstruct = target => {
  const proxy = new Proxy(target, {
    construct() {
      return {};
    },
  });
  try {
    new proxy();
    return true;
  } catch (e) {
    return false;
  }
};
for (const [name, fn] of [
  ['function', function () {}],
  ['short-hand method', { foo() {} }.foo],
  ['arrow', () => {}],
]) {
  print(name, 'hasConstruct', hasConstruct(fn));
  print(
    name,
    '.prototype',
    JSON.stringify(Object.getOwnPropertyDescriptor(fn, 'prototype')),
  );
}
  1. eshost -s test-has-construct.js

Expected Behavior

#### JavaScriptCore, Moddable XS, SpiderMonkey, V8
function hasConstruct true
function .prototype {"value":{},"writable":true,"enumerable":false,"configurable":false}
short-hand method hasConstruct false
short-hand method .prototype undefined
arrow hasConstruct false
arrow .prototype undefined

Actual Behavior

#### Hermes
function hasConstruct true
function .prototype {"value":{},"writable":true,"enumerable":false,"configurable":false}
short-hand method hasConstruct true
short-hand method .prototype {"value":{},"writable":true,"enumerable":false,"configurable":false}
arrow hasConstruct false
arrow .prototype undefined

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions