v8

JavaScript深入浅出第4课:V8引擎是如何工作的?

旧城冷巷雨未停 提交于 2019-12-04 01:23:07
摘要: 性能彪悍的V8引擎。 《 JavaScript深入浅出 》系列 : JavaScript深入浅出第1课:箭头函数中的this究竟是什么鬼? JavaScript深入浅出第2课:函数是一等公民是什么意思呢? JavaScript深入浅出第3课:什么是垃圾回收算法? JavaScript深入浅出第4课:V8是如何工作的? 最近,JavaScript生态系统又多了2个非常硬核的项目。 大神 Fabrice Bellard 发布了一个新的JS引擎 QuickJS ,可以将JavaScript源码转换为C语言代码,然后再使用系统编译器(gcc或者clang)生成可执行文件。 Facebook为React Native开发了新的JS引擎 Hermes ,用于优化安卓端的性能。它可以在构建APP的时候将JavaScript源码编译为Bytecode,从而减少APK大小、减少内存使用,提高APP启动速度。 作为JavaScript程序员,只有极少数人有机会和能力去实现一个JS引擎,但是理解JS引擎还是很有必要的。本文将介绍一下V8引擎的原理,希望可以给大家一些帮助。 JavaScript引擎 我们写的JavaScript代码直接交给浏览器或者Node执行时,底层的CPU是不认识的,也没法执行。CPU只认识自己的指令集,指令集对应的是汇编代码。写汇编代码是一件很痛苦的事情,比如

Node vs Chrome, assigning console.log to a variable?

随声附和 提交于 2019-12-04 00:51:59
问题 When I assign console.log to a variable in node.js it works fine, var l = console.log l(1) # outputs 1 However, if I do the same thing in Chromium 30's dev tools, var l = console.log l(1) # TypeError: Illegal invocation How come it doesn't work in Chromium's dev tools? Why am I getting, TypeError: Illegal invocation 回答1: Exactly why this requirement is in place, I don't know, but I guess Chrome's console.log requires the value of this to be console . If you want to store it in a variable, you

Can this function be garbage-collected?

空扰寡人 提交于 2019-12-04 00:39:00
Consider this piece of cake... ehm, code: 'use strict' function doWork () { return new Promise(function (resolve, reject) { // work work work... // Done! But... where's the resolve() ??? }) } doWork().then(function doMoreWork () { // Some more work to do... }) Once the function in the Promise's constructor finishes... Is the Promise object garbage-collectible? Is doMoreWork() garbage-collectible? My guess is that doMoreWork() cannot be GC-ed directly because the Promise keeps a reference to it, but once the promise's body finishes and returns the execution context to the upper (?) scope, the

how to run / debug javascript in command line

蹲街弑〆低调 提交于 2019-12-03 20:12:48
I'm wondering (actually I know there definitely is such a way to run js in command line, because when I watch google I/0 presentations, they use a command like d8, maybe it's part of V8 engine) is there any way to run/debug my javascripts in command line? such as d8 myJsfile.js --prof etc.? Does anybody have any idea about this? Yes, D8 is a command line tool that allows you to run and debug Javascript scripts. It comes with V8. Getting D8 Getting V8 on your machine is not hard. Follow these instructions: https://developers.google.com/v8/build (The part with GYP looks a little messy but I got

Implementing inheritance in node.js bindings

亡梦爱人 提交于 2019-12-03 17:36:28
I am writing Node.js bindings around a C++ library. I can identify key objects in the C++ library that I can expose as classes to the Node.js (i.e. derivatives of ObjectWrap). I can also see an inheritance relationship between these objects. How can I expose ClassA , ClassB , ClassC as node.js classes (derivatives of ObjectWrap ) and manipulate their prototypes (in v8 C++ code) so that ClassB and ClassC are derivates of ClassA ? This can be done using v8::FunctionTemplate 's Inherit method. It's explained here . Here's a working example. C++ code: #include <v8.h> #include <node.h> using

Trace the execution of ALL Javascript in a web app

倾然丶 夕夏残阳落幕 提交于 2019-12-03 17:33:18
问题 Here is the situation: A complex web app is not working, and it is possible to produce undesired behavior consistently. The cause of the problem is not known. Proposal: Trace the execution paths of all javascript code. Essentially, produce two monstrous logs which can then be fed into a diff algorithm to determine where the behavior related to the bug begins to diverge (as the cause is not apparent from application behavior, and both comprehending and obtaining a copy of the actual JS code

NodeJS memory consumption in an infinite loop

大城市里の小女人 提交于 2019-12-03 16:14:19
问题 I don't know if this is a bug with Node or V8, but if I run the following code the node process leaks memory. The GC never seems to kick in and in a few seconds it's consuming >1GB of memory. This is unexpected behavior. Am I missing something? Here's the code: for(;;) { console.log(1+1); } Obviously, this is a little bit of a contrived situation, but I can see an issue with a long-running process that would never free memory. Edit: I tried both with v0.5.10 (unstable) and v0.4.12 (stable)

In Nodejs, when I console.log a req object, what does [Circular] reference? How to determine that

我们两清 提交于 2019-12-03 15:44:37
问题 In Nodejs, when I console.log a req object, what does [Circular] mean? Here's an example console.log(req) against a basic nodejs example. Notice the request.socket._readWatcher.socket is a [Circular]. Does that mean it refers to itself? How can I dereference that? { socket: { bufferSize: 0, fd: 7, type: 'tcp4', allowHalfOpen: true, _readWatcher: { socket: [Circular], callback: [Function: onReadable] }, destroyed: false, readable: true, _writeQueue: [], _writeQueueEncoding: [], _writeQueueFD:

is there any workaround for broken v8 date parser?

那年仲夏 提交于 2019-12-03 15:13:26
问题 V8 Date parser is broken: > new Date('asd qw 101') Sat Jan 01 101 00:00:00 GMT+0100 (CET) I can use fragile regular expression like this: \d{1,2} (jan|feb|mar|may|jun|jul|aug|sep|oct|nov|dec) \d{1,4} but it is too fragile. I cannot rely on new Date (issue in V8) and also moment cant help me because moment is getting rid off date detection (github issue-thread). is there any workaround for broken v8 date parser? To be clear. We have Gecko and V8, both have Date . V8 has broken Date, Gecko has

What happens to v8 status code on optimization / function run?

牧云@^-^@ 提交于 2019-12-03 14:47:50
I saw a question about v8 Optimization which led me to play a bit with v8 Optimization. I've also seen bluebird post about v8 Optimization killers . According to v8 repo, optimization status codes are in multiplications of 2: 1,2,4 ,8 and so on (see OptimizationStatus enum ) However, the following code gave me strange status codes like 17 and 65, and only in these specific cases (see the last few lines of code). Any ideas about why this is happening? function adder(a, b) { return new Function('a', 'b', 'return b%2 ? a + b : b%3 ? a - b : b%5 ? b / a : a * b')(a, b); } function addereval(a, b)