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

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

问题描述

src v8 树c $ c>目录,有些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行,git clone来自github / v8 / v8

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:


使用最小的C ++运行时函数集,JavaScript库中的大部分JavaScript库都是用
本身实现的可从
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.

如果您查看 parser.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天全站免登陆