Skip to content

Commit

Permalink
Introducing NoRJSScope
Browse files Browse the repository at this point in the history
Summary: NoRJSScope is a RAII class to temporarily disallow JS execution.

Reviewed By: neildhar

Differential Revision: D43834350

fbshipit-source-id: cf1b6e874212ea95fb1ede61ad9bcc0d10d7cdfc
  • Loading branch information
werew authored and facebook-github-bot committed Mar 7, 2023
1 parent 21fd7fc commit 34d0781
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
15 changes: 15 additions & 0 deletions include/hermes/VM/Runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -1377,6 +1377,12 @@ class HERMES_EMPTY_BASES Runtime : public PointerBase,
/// making it optional. If this is accessed when the optional value is cleared
/// (the invalid state) we assert.
llvh::Optional<const inst::Inst *> currentIP_{(const inst::Inst *)nullptr};

/// The number of alive/active NoRJSScopes. If nonzero, then no JS execution
/// is allowed
uint32_t noRJSLevel_{0};

friend class NoRJSScope;
#endif

public:
Expand Down Expand Up @@ -1738,6 +1744,7 @@ class NoAllocScope {
NoAllocScope() = delete;
};
using NoHandleScope = NoAllocScope;
using NoRJSScope = NoAllocScope;

#else

Expand Down Expand Up @@ -1809,6 +1816,14 @@ class NoAllocScope : public BaseNoScope {
using BaseNoScope::BaseNoScope;
using BaseNoScope::operator=;
};

/// RAII class to temporarily disallow reentering JS execution.
class NoRJSScope : public BaseNoScope {
public:
explicit NoRJSScope(Runtime &runtime) : BaseNoScope(&runtime.noRJSLevel_) {}
using BaseNoScope::BaseNoScope;
using BaseNoScope::operator=;
};
#endif

//===----------------------------------------------------------------------===//
Expand Down
3 changes: 3 additions & 0 deletions lib/VM/Interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,9 @@ CallResult<HermesValue> Runtime::interpretFunctionImpl(
}

CallResult<HermesValue> Runtime::interpretFunction(CodeBlock *newCodeBlock) {
// Make sure we are not re-entering JS execution from a context that doesn't
// allow reentrancy
assert(this->noRJSLevel_ == 0 && "No JS execution allowed right now.");
return interpretFunctionImpl(newCodeBlock);
}

Expand Down

0 comments on commit 34d0781

Please sign in to comment.