如何在标识符中使用 % 符号 [英] How can % signs be used in identifiers

查看:50
本文介绍了如何在标识符中使用 % 符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

浏览 src 目录下的 v8 树,那里有一些 js 文件,提供一些基本的 JS 对象,如 Math、Array 等.浏览这些文件时,我看到标识符在其名称中包含一个百分号 (%),即 %Foo.我一开始天真地认为这是 JS 标识符中的其他一些允许字符,但是当我在 shell 中尝试它时,它对我大喊大叫,说我违反了语法规则.但是如果是语法错误,那么 d8 是怎么工作的呢?以下是来自实际源代码的示例:

Browsing the v8 tree, under the src directory, some js files were there, providing some basic JS objects like Math, Array etc. Browsing those files, I saw identifiers including a percent sign (%) in their names, i.e. %Foo. I first naively thought it was some other allowed character in JS's identifiers, but when I tried it in shell, it yelled at me, saying that I'm violating syntax rules. But if it is a syntax error, how come d8 works? Here are an example from the actual source code:

src/apinatives.js 第 44 到 47 行,来自 github/v8/v8 的 git clone

function Instantiate(data, name) {
  if (!%IsTemplate(data)) return data;
      var tag = %GetTemplateField(data, kApiTagOffset);
      switch (tag) {

src/apinatives.js 第 41 到 43 行,来自 github/v8/v8 的 git clone

function SetConstructor() {
  if (%_IsConstructCall()) {
    %SetInitialize(this);

为什么这个标识符不会产生语法错误.所有 js 文件,包括 math.js 和 string.js 以及所有其他文件?:wq

How come this identifiers do not yield syntax errors. All js files, including math.js and string.js and all others?:wq

推荐答案

它在技术上不是有效的 JavaScript.这些是对 V8 运行时函数的调用.从该页面:

It is not technically valid JavaScript. These are calls to V8 runtime functions. From that page:

大部分 JavaScript 库都是用 JavaScript 代码实现的本身,使用可调用的最小 C++ 运行时函数集JavaScript.其中一些使用以 % 开头的名称进行调用,并使用标志--allow-natives-syntax".其他人只被称为代码生成器生成的代码,在 JS 中是不可见的,即使使用 % 语法.

Much of the JavaScript library is implemented in JavaScript code itself, using a minimal set of C++ runtime functions callable from JavaScript. Some of these are called using names that start with %, and using the flag "--allow-natives-syntax". Others are only called by code generated by the code generators, and are not visible in JS, even using the % syntax.

如果您查看 解析器.cc 你可以看到一些与 allow_natives_syntax 相关的代码,这些代码决定了解析器是否接受这个扩展到 V8 用来与其运行时交互的 JavaScript 语言.必须在启用该选项的情况下解析这些文件.

If you look in parser.cc you can see some code relating to allow_natives_syntax that determines whether the parser will accept this extension to the JavaScript language that V8 is using to interact with its runtime. These files must be parsed with that option enabled.

我推测 V8 不允许您在默认情况下进行这些调用,因为它与 JavaScript 标准相矛盾,并且因为它可能允许您对运行时执行您不应该执行的操作.

I would speculate that V8 does not allow you to make these calls by default both because it would contradict the JavaScript standard and because it would probably allow you to do things to the runtime you should not be able to do.

这篇关于如何在标识符中使用 % 符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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