简体   繁体   中英

Direct use of 1/0 in V8 engine

I first thought it is python since the file is named macros.py. But some say it is not.

This part of the v8 engine came to the point in the middle of another discussion.

# 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?

The line is not valid Python syntax. The Python language has no concept of const types, nor is there any macro syntax.

The .py extension on that file is highly misleading; there is only one line that may or may not contain actual Python syntax:

# 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. 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:

# 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:

> (1/0)
Infinity

while in Python code this raises an exception:

>>> 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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