Accessing line number in V8 JavaScript (Chrome & Node.js)

Object.defineProperty(global, ‘__stack’, { get: function(){ var orig = Error.prepareStackTrace; Error.prepareStackTrace = function(_, stack){ return stack; }; var err = new Error; Error.captureStackTrace(err, arguments.callee); var stack = err.stack; Error.prepareStackTrace = orig; return stack; } }); Object.defineProperty(global, ‘__line’, { get: function(){ return __stack[1].getLineNumber(); } }); console.log(__line); The above will log 19. Combined with arguments.callee.caller you can get … Read more

How can I see the machine code generated by v8?

I don’t know how to invoke the disassembler from C++ code, but there is a quick-and-dirty way to get a disassembly from the shell. First, compile v8 with disassembler support: scons [your v8 build options here] disassembler=on sample=shell Now you can invoke the shell with the “–print_code” option: ./shell –print_code hello.js Which should give you … Read more

How can % signs be used in identifiers

It is not technically valid JavaScript. These are calls to V8 runtime functions. From that page: 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 … Read more

Object descriptor getter/setter performance in recent Chrome/V8 versions

The changes in performance are relevant to this Chromium issue (credits go to @VyacheslavEgorov). To avoid performance issues, a prototype should be used instead. This is one of few reasons why singleton classes may be used to instantiate an object once. With ES5: var _a = 1; function Obj() {} Object.defineProperty(Obj.prototype, ‘a’, { enumerable: true, … Read more

Access function location programmatically

The answer, for now, is no. The [[FunctionLocation]] property you see in Inspector is added in V8Debugger::internalProperties() in the debugger’s C++ code, which uses another C++ function V8Debugger::functionLocation() to gather information about the function. functionLocation() then uses a number of V8-specific C++ APIs such as v8::Function::GetScriptLineNumber() and GetScriptColumnNumber() to find out the exact information. All … Read more

Are JavaScript Arrays actually implemented as arrays?

In SpiderMonkey, arrays are implemented basically as C arrays of jsvals. These are referred to as “dense arrays”. However, if you start doing un-array-like things to them — like treating them like objects — their implementation is changed to something which very much resembles objects. Moral of the story: when you want an array, use … Read more