Skip to content

Commit

Permalink
Update SideEffect model for CreateClassInst
Browse files Browse the repository at this point in the history
Summary:
It's only possible for `CreateClass` to have execute side effects
when it's a derived class constructor, because it fetches the
`.prototype` of the super class.

Reviewed By: tmikov

Differential Revision: D67404012

fbshipit-source-id: 9ca7af807f27ae127c342ece686d7cfcd42c199b
  • Loading branch information
fbmal7 authored and facebook-github-bot committed Dec 18, 2024
1 parent cf8cc6e commit 0ca4504
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
2 changes: 1 addition & 1 deletion doc/IR.md
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ Description | Constructs a new class into the current scope from its constructor
Example | %0 = CreateClassInst %scope, %function, %superClass, %homeObjectOutput
Arguments | %function is the function that represents the code of the constructor. %scope is the surrounding environment. %superClass is the class to inherit from; in base classes this is an empty sentinel value. %homeObjectOutput is an out parameter which will contain the home object (.prototype) of the class.
Semantics | The instruction creates a new class that may access the lexical scope of the current function, and an inherit from a given super class. (ES2023 15.7.14) This results in the creation of 2 objects: the class function object itself, and the "home" object. The home object is where methods are put. The home object can be found on the .prototype of the class, and the class can be found on the .constructor of the home object.
Effects | May execute JS and write to stack memory.
Effects | Writes to stack memory. May execute JS if it's a derived class.

### BinaryOperatorInst

Expand Down
14 changes: 9 additions & 5 deletions include/hermes/IR/Instrs.h
Original file line number Diff line number Diff line change
Expand Up @@ -1037,7 +1037,7 @@ class CreateClassInst : public BaseCreateCallableInst {
return getOperand(HomeObjectOutIdx);
}

Value *getSuperClass() {
Value *getSuperClass() const {
return getOperand(SuperClassIdx);
}

Expand All @@ -1046,10 +1046,14 @@ class CreateClassInst : public BaseCreateCallableInst {
}

SideEffect getSideEffectImpl() const {
// When creating a derived class, we look up the .prototype of the super
// class we are deriving from. This property look up can potentially trigger
// JS.
return SideEffect::createExecute().setWriteStack();
if (llvh::isa<EmptySentinel>(getSuperClass())) {
// When creating a derived class, we look up the .prototype of the super
// class we are deriving from. This property look up can potentially
// trigger JS.
return SideEffect::createExecute().setWriteStack();
} else {
return SideEffect{}.setReadHeap().setWriteStack();
}
}

static bool hasOutput() {
Expand Down

0 comments on commit 0ca4504

Please sign in to comment.