这两个JS代码在V8中有什么区别?

问题描述 投票:0回答:1

我尝试通过V8创建一个codeCache。使用 ScriptCompiler::CreateCodeCache 函数。

ScriptCompiler::CachedData* CreateCachedData(Local<String> sourceContent, Local<String> sourceName) {
    ScriptOrigin* scriptOriginPtr = nullptr;
    scriptOriginPtr = new ScriptOrigin(
            String::NewFromTwoByte(isolate_, sourceName, NewStringType::kNormal).ToLocalChecked(),
            Integer::New(isolate_, 0), Integer::New(isolate_, 0));
    ScriptCompiler::Source* source = nullptr;
    source = new ScriptCompiler::Source(sourceContent, *scriptOriginPtr);

    Local<Script> script;
    ScriptCompiler::Compile(context_, source, ScriptCompiler::CompileOptions::kEagerCompile).ToLocal(&script);

    ScriptCompiler::CachedData* data = ScriptCompiler::CreateCodeCache(script->GetUnboundScript());

    return data;
}

当我使用这些代码时,data 的值为 nullptr。

function A() {
    "use asm"
    function R() {}
    return R
}

当我使用这些代码时,data 的值不是 nullptr。

function A() {
    "use asm"
    function R() {};
    return R
}

这两个JS代码在V8和JS中有什么区别?

javascript v8
1个回答
0
投票

区别在于函数声明后的空语句

{}
对于
V8
正确识别和编译asm.js代码至关重要。

如果没有空语句,

V8
会将代码误解为常规
JavaScript
并且无法应用
asm.js
优化。这会导致生成一个用于缓存数据的
null
指针。空语句充当占位符块,允许
V8
识别函数并应用必要的
asm.js
优化。

© www.soinside.com 2019 - 2024. All rights reserved.