Commit 5b41233f authored by Yoshisato Yanagisawa's avatar Yoshisato Yanagisawa Committed by V8 LUCI CQ

Implement Function::Experimental_IsNopFunction.

The function returns true if the function does not do anything like:
() => {}.

Change-Id: I049d7956c443b5d2bb8017a48547376f13acd0a2
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3778969Reviewed-by: 's avatarToon Verwaest <verwaest@chromium.org>
Commit-Queue: Yoshisato Yanagisawa <yyanagisawa@chromium.org>
Cr-Commit-Position: refs/heads/main@{#82093}
parent 776b9eb9
......@@ -106,6 +106,14 @@ class V8_EXPORT Function : public Object {
V8_WARN_UNUSED_RESULT MaybeLocal<String> FunctionProtoToString(
Local<Context> context);
/**
* Returns true if the function does nothing.
* The function returns false on error.
* Note that this function is experimental. Embedders should not rely on
* this existing. We may remove this function in the future.
*/
V8_WARN_UNUSED_RESULT bool Experimental_IsNopFunction() const;
ScriptOrigin GetScriptOrigin() const;
V8_INLINE static Function* Cast(Value* value) {
#ifdef V8_ENABLE_CHECKS
......
......@@ -5421,6 +5421,34 @@ Local<v8::Value> Function::GetBoundFunction() const {
return v8::Undefined(reinterpret_cast<v8::Isolate*>(self->GetIsolate()));
}
bool Function::Experimental_IsNopFunction() const {
auto self = Utils::OpenHandle(this);
if (!self->IsJSFunction()) return false;
i::SharedFunctionInfo sfi = i::JSFunction::cast(*self).shared();
i::Isolate* i_isolate = sfi.GetIsolate();
i::IsCompiledScope is_compiled_scope(sfi.is_compiled_scope(i_isolate));
if (!i::Compiler::Compile(i_isolate, i::handle(sfi, i_isolate),
i::Compiler::CLEAR_EXCEPTION, &is_compiled_scope)) {
return false;
}
DCHECK(is_compiled_scope.is_compiled());
// Since |sfi| can be GC'ed, we get it again.
sfi = i::JSFunction::cast(*self).shared();
if (!sfi.HasBytecodeArray()) return false;
i::Handle<i::BytecodeArray> bytecode_array(sfi.GetBytecodeArray(i_isolate),
i_isolate);
i::interpreter::BytecodeArrayIterator it(bytecode_array, 0);
if (it.current_bytecode() != i::interpreter::Bytecode::kLdaUndefined) {
return false;
}
it.Advance();
DCHECK(!it.done());
if (it.current_bytecode() != i::interpreter::Bytecode::kReturn) return false;
it.Advance();
DCHECK(it.done());
return true;
}
MaybeLocal<String> v8::Function::FunctionProtoToString(Local<Context> context) {
PREPARE_FOR_EXECUTION(context, Function, FunctionProtoToString, String);
auto self = Utils::OpenHandle(this);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment