我已经使用嵌入式 v8 实现了一个类似“require”的函数,它加载一个 JavaScript 文件并执行它,但是因为我的程序有多个线程,因此每个线程都有自己的隔离,我必须单独加载和编译文件在指定相同源的每个线程中。如果可能的话,我想以某种方式缓存任何已编译的脚本,以便如果另一个线程(使用另一个隔离)碰巧想要相同的文件,我可以利用某种预编译格式,并只给它运行脚本,而不是必须在需要相同文件的每个隔离内部单独编译它。
问问题
1179 次
2 回答
2
我看不出这怎么可能,所有代码、脚本、SharedFunctionInfo 等都是特定于隔离的 JavaScript 对象。
但是,您可以构建某些 V8 状态的静态快照,并始终由所有隔离器加载此状态,但这不能动态地用于运行时。这就是 V8 内置函数的作用。
于 2015-03-31T06:02:56.003 回答
1
我认为 ScriptCompiler 可以帮助你。使用 ScriptCompiler::CompileUnboundScript,您可以为脚本创建和使用缓存数据。我(还)没有测试它,但评论看起来很有希望:
/**
* Compiles the specified script (context-independent).
* Cached data as part of the source object can be optionally produced to be
* consumed later to speed up compilation of identical source scripts.
*
* Note that when producing cached data, the source must point to NULL for
* cached data. When consuming cached data, the cached data must have been
* produced by the same version of V8.
*
* \param source Script source code.
* \return Compiled script object (context independent; for running it must be
* bound to a context).
*/
于 2017-06-04T21:44:43.947 回答