简体   繁体   English

在V8引擎中直接使用1/0

[英]Direct use of 1/0 in V8 engine

I first thought it is python since the file is named macros.py. 我首先认为它是python,因为该文件名为macros.py。 But some say it is not. 但是有人说不是。

This part of the v8 engine came to the point in the middle of another discussion. v8引擎的这一部分在另一个讨论的中间。

# Constants.  The compiler constant folds them.
define INFINITY = (1/0);

I haven't seen anything like this in another language. 我还没有用其他语言看到过这样的东西。 What is the mechanism of this language (py?/js?) to allow such an expression? 这种语言(py?/ js?)允许这种表达的机制是什么?

The line is not valid Python syntax. 该行是无效的Python语法。 The Python language has no concept of const types, nor is there any macro syntax. Python语言没有const类型的概念,也没有macro语法。

The .py extension on that file is highly misleading; 该文件上的.py扩展名容易引起误解; there is only one line that may or may not contain actual Python syntax: 只有一行可能包含也可能不包含实际的Python语法:

# Macros implemented in Python.
python macro CHAR_CODE(str) = ord(str[1]);

The file is actually parsed by a Python script , so it is, at best, a custom domain-specific language. 该文件实际上是由Python脚本解析的,因此充其量是一种自定义域特定的语言。 The macros that are not marked as Python code with python macro , appear to be used to convert JavaScript code to character arrays, judging by the comment at the top: 根据顶部的注释判断,未使用python macro标记为Python代码的python macro似乎用于将JavaScript代码转换为字符数组:

# This is a utility for converting JavaScript source code into C-style
# char arrays. It is used for embedded JavaScript code in the V8
# library.

In JavaScript code, (1/0) produces float infinity: JavaScript代码中, (1/0)产生浮点无穷大:

> (1/0)
Infinity

while in Python code this raises an exception: 而在Python代码中,这引发了一个异常:

>>> 1/0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ZeroDivisionError: integer division or modulo by zero

but you don't need such tricks as you can just use float('inf') if you really needed to refer to infinity. 但是您并不需要这些技巧,因为如果您确实需要引用无穷大,则可以只使用float('inf')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM