Note that this is not a problem of preference but a problem of correctness.
For example, if define() can be called asynchronously by DOM (just like event
listeners), you need to store the ScriptState when the registry object is
created and use the ScriptState in the define(). This is a well-established
programming pattern in the binding layer.
If define() is guaranteed to be called by JS, the binding layer already
guarantees that define() is called with the same ScriptState that created the
registry object. That's why you don't need to store ScriptState anywhere (and
you should just use the ScriptState passed into define() by the binding layer).
So the latest patchset looks correct.
How to remove ScriptState for modularization is a separate discussion. If we
need to store the ScriptState for correctness reasons, we must do that. Then if
you need to remove the dependency on ScriptState, you'll need to introduce some
abstraction class that wraps ScriptState in a way that doesn't depend on V8.
(This is why I want to separate a discussion to correctly implement the feature
from a discussion to modularize the feature.)
https://codereview.chromium.org/2003033004/diff/140001/third_party/WebKit/Source/bindings/core/v8/ScriptCustomElementDefinition.cppFile
third_party/WebKit/Source/bindings/core/v8/ScriptCustomElementDefinition.cpp
(right):
https://codereview.chromium.org/2003033004/diff/140001/third_party/WebKit/Source/bindings/core/v8/ScriptCustomElementDefinition.cpp#newcode40third_party/WebKit/Source/bindings/core/v8/ScriptCustomElementDefinition.cpp:40:
if (!scriptState->contextIsValid())
Nit: I'd remove this check.
We normally don't check scriptState->contextIsValid() when the C++
method is being called by JS (which means that you have a valid
scriptState). We only check it when the C++ method is invoked
asynchronously by DOM (e.g., event listeners).
You're right in that even if the C++ method is called by JS (and thus
it's guaranteed that you have a valid scriptState at the point when the
C++ method is invoked), the scriptState can go invalid when you call
some V8 API (e.g., Get()). However, we don't check it every time for the
following (subtle) reasons:
- You need to check it every time you call V8 APIs on stack. This messes
up the code base and slows down performance.
- The spec does not have a concept of scriptState->contextIsValid(). Per
the spec, scriptState must be valid as long as you have any reference to
the browsing context. So ideally, we should remove
scriptState->contextIsValid() entirely.
- Lifetime management around scriptState->contextIsValid() is already
broken in many ways in other browsers as well.
https://codereview.chromium.org/2003033004/diff/140001/third_party/WebKit/Source/bindings/core/v8/ScriptCustomElementDefinitionBuilder.cppFile
third_party/WebKit/Source/bindings/core/v8/ScriptCustomElementDefinitionBuilder.cpp
(right):
https://codereview.chromium.org/2003033004/diff/140001/third_party/WebKit/Source/bindings/core/v8/ScriptCustomElementDefinitionBuilder.cpp#newcode82third_party/WebKit/Source/bindings/core/v8/ScriptCustomElementDefinitionBuilder.cpp:82:
return m_scriptState->contextIsValid();
Ditto. I'd just return true here.
https://codereview.chromium.org/2003033004/