用于标记模板文字的TemplateObject数组是否被它们的领域弱引用? [英] Are TemplateObject arrays for tagged template literals weakly referenced by their realm?

查看:107
本文介绍了用于标记模板文字的TemplateObject数组是否被它们的领域弱引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

while (c) {
  tag`str0 ${e} str1`
}

JavaScript运行时创建一个冻结的数组,如 Object.freeze(['str0','str1'])但是附加 .raw 属性。

The JavaScript runtime creates a frozen array like Object.freeze(['str0 ', ' str1']) but with an additional .raw property.

是否可以将该对象用作<$ c中的键$ c> WeakMap 以避免每次循环都必须重做基于数组的工作?

Is it okay to use that object as a key in a WeakMap to avoid having to redo work based on the array each time through the loop?

const memoTable = new WeakMap
function tag(templateStrings, ...values) {
  let cached = memoTable.get(templateStrings)
  if (!cached) {
    // Compute cached and put it in the table for next time.
  }
  // Do something with cached and values
}

章节 12.2.9.3运行时语义:GetTemplateObject( templateLiteral 描述了如何缓存此值:

Section 12.2.9.3 Runtime Semantics: GetTemplateObject ( templateLiteral ) describes how this value is cached:



  1. realm 是当前的领域记录。

  2. templateRegistry 成为领域。[[TemplateMap]]。

  1. Let realm be the current Realm Record.
  2. Let templateRegistry be realm.[[TemplateMap]].


所以从使用标签开始应该是相同的在上面的循环中,这将是一个很好的属性,一个键有。

so it should be the same from use to use of tag in the loop above which would be a nice property for a key to have.

在我看来,[[TemplateMap]]会有弱引用模板对象数组,因为否则

It seems to me that the [[TemplateMap]] would have to weakly reference the template object array because otherwise

for (let i = 0; i < 1e6; ++i) {
  eval('(() => {})`' + i + '`');
}

会泄漏内存。

我没有在规范中看到任何内容,但是对于广泛使用的JavaScript引擎来说,标记字符串模板的WeakMap条目最终会被收集用于不可重入的范围吗?

I don't see anything in the specification but is it the case for widely used JavaScript engines that WeakMap entries for tagged string template uses not in re-enterable scopes will eventually be collected?

我问,因为我已经实施了基于这个假设,但还没弄清楚如何测试它。

I ask because I've implemented something based on this assumption but haven't figured out how to test it.

推荐答案


是否可以使用该对象作为WeakMap中的一个键,以避免每次循环时都必须基于数组重做工作?

Is it okay to use that object as a key in a WeakMap to avoid having to redo work based on the array each time through the loop?

是的,那是正是你想要做的事情,这是模板文字的一大特色。

Yep, that's exactly what you're meant to do and it's one of the great features of template literals.


在我看来,[[TemplateMap] ]]必须弱引用模板对象数组,否则

It seems to me that the [[TemplateMap]] would have to weakly reference the template object array because otherwise

for (let i = 0; i < 1e6; ++i) {
  eval('(() => {})`' + i + '`');
}

会泄漏内存。

事实上它确实泄漏了,因为 [[TemplateMap]] 并不弱。 :(

It does leak in fact, since [[TemplateMap]] is not weak. :(

这是关于当前规范的讨论的开放点。目前的讨论是规范是否应该改为,而不是 [[TemplateMap]] 是全局状态,而不是每个源文本位置。例如,现在

This is an open point of discussion on the current spec. The discussion at the moment is whether the spec should be changed to, instead of having the [[TemplateMap]] be global state, to instead have it be per-source-text-position. e.g. right now

var id = v => v;
id`tpl` === id`tpl` // true

是否可以打破这样创建两个单独的模板?如果是这样,那么至少有可能你的 eval 示例可以被允许收集模板。

Is it acceptable to break that such that two separate templates are created? If so, then there's at least the possibility that your eval example could be allowed to collect the template.

你可以在这里看到一些讨论, https://github.com/tc39/ecma262/issues/840 ,其中至少暂时可以修复此问题。

You can see some discussion here, https://github.com/tc39/ecma262/issues/840, where at least tentatively this could be fixed.

这篇关于用于标记模板文字的TemplateObject数组是否被它们的领域弱引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆