简体   繁体   English

Javascript:如何查看像toString这样的本机函数的源代码实现

[英]Javascript: How to see source code implementation of native functions like toString

I am looking to see how some Javascript functions work under the hood. 我期待看到一些Javascript函数如何工作。 For eg I want to learn how Chrome's V8 Engine implements the Unary (-) operation or the String.prototype.toString() method. 例如,我想了解Chrome的V8引擎如何实现一元( - )操作或String.prototype.toString()方法。

How can I see the native C/C++ implementation? 如何查看本机C/C++实现? I have seen many answers here linking to the Chromium repository and the V8 repository, but these are giant and not very beginner friendly, and there aren't really any guides anywhere as far as I could find. 我在这里看到许多链接到Chromium存储库和V8存储库的答案,但这些都是巨大的,并不是非常适合初学者,并且在我能找到的任何地方都没有任何指南。

I'm looking for something like this: 我在寻找这样的东西:

// Pseudo code
function -(arg) {
  return arg * -1
}

Obviously, I understand that I wouldn't find this written in Javascript . 显然,我明白我不会发现这是用Javascript编写 I'm just looking for a similar level of detail. 我只是在寻找类似的细节。

I'm yet to find an answer that concisely shows how to find the native implementation of Javascript functions anywhere. 我还没有找到一个简明扼要地展示如何在任何地方找到Javascript函数的本机实现的答案。 Could someone point me in the right direction? 有人能指出我正确的方向吗?

The ECMA specs here give the following specs for the Unary - operation: 这里的ECMA规范给出了Unary的以下规范 - 操作:

Unary - Operator 一元 - 运算符

The unary - operator converts its operand to Number type and then negates it. 一元 - 运算符将其操作数转换为数字类型然后否定它。 Note that negating +0 produces −0, and negating −0 produces +0. 注意,否定+0会产生-0,而否定-0会产生+0。

The production UnaryExpression : - UnaryExpression is evaluated as follows: 生产UnaryExpression: - UnaryExpression的计算方法如下:

Let expr be the result of evaluating UnaryExpression . expr是评估UnaryExpression的结果。 Let oldValue be ToNumber(GetValue(expr)) . oldValueToNumber(GetValue(expr)) If oldValue is NaN , return NaN . 如果oldValue是NaN ,则返回NaN Return the result of negating oldValue ; 返回否定oldValue的结果; that is, compute a Number with the same magnitude but opposite sign. 也就是说,计算具有相同幅度但符号相反的数字。

This is quite useful, but what I'm trying to understand is, how 这非常有用,但我想要了解的是,如何

compute a Number with the same magnitude but opposite sign 计算具有相同幅度但符号相反的数字

Is calculated. 算了。 Is it the number * -1 or something else? 它是number * -1还是其他什么? Or is it multiple ways? 或者是多种方式?

There is no piece of code or single implementation of individual operators in V8. V8中没有单独的运算符代码或单个实现。 V8 is a just-in-time compiler that executes all JavaScript by compiling it to native code on the fly. V8是一个即时编译器,通过动态编译本机代码来执行所有JavaScript。 V8 supports about 10 different CPU architectures and for each has 4 tiers of compilers. V8支持大约10种不同的CPU架构,每种架构有4层编译器。 That already makes 40 different implementations of every operator. 这已经使每个运营商的40个不同的实现。 In many of those, compilation goes through a long pipeline of stages that transform the input to the actual machine code. 在许多这些编译中,编译经历了一系列阶段,将输入转换为实际的机器代码。 And in each case the exact transformation depends on the type information that is available at compile time (collected on previous runs). 并且在每种情况下,确切的转换取决于在编译时可用的类型信息(在先前的运行中收集)。

To understand what's going on you would need to understand a significant part of V8's complicated architecture, so it is pretty much impossible to answer your question in an SO reply. 要了解正在发生的事情,您需要了解V8复杂架构的重要部分,因此在SO回复中回答您的问题几乎是不可能的。 If you are just interested in the semantics, I rather suggest looking at the EcmaScript language definition. 如果您只对语义感兴趣,我建议您查看EcmaScript语言定义。

(The snippet you cite is just a helper function for the converting compiler-internal type information for unary operators in one of the many stages.) (您引用的代码片段只是一个辅助函数,用于转换一元运算符的编译器内部类型信息,这是多个阶段之一。)

Edit: the excerpt from the EcmaScript definition you cite in the updated question is the right place to look. 编辑:您在更新的问题中引用的EcmaScript定义的摘录是正确的地方。 Keep in mind that all JavaScript numbers are IEEE floating point numbers. 请记住,所有JavaScript编号都是IEEE浮点数。 The sentence is basically saying that - just inverts the sign bit of such a number. 这句话基本上就是说-只是反转这个数字的符号位。 You'd have to refer to the IEEE 754 standard for more details. 您需要参考IEEE 754标准了解更多详情。 Multiplication with -1.0 is a much more complicated operation, but will have the same result in most cases (probably with the exception of NaN operands). 乘法-1.0是一个复杂得多的操作,但在大多数情况下会有相同的结果(可能除NaN操作数外)。

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

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