Values of V8 NamedProperty object properties are not shown in debugger - javascript

We are using the V8 version 5.3.332 in my Android App. We are trying to debug the javascript using the Node-Inspector(0.11.3)(Also tried with Eclipse Chrome Dev Tools) but strangely values of certain object properties are not shown when we hover or seen it in scoped variables section. This behaviour is particularly seen for the V8 objects which has Named Property handler attached. If the V8 object is prepared with SetAccessor then values of all properties are shown properly.
We observed that, For Named Property objects, its NamedGetter is not called for its properties so all properties are being shown as undefined.
We are preparing the V8 object as below
Local funTem = FunctionTemplate::New(isolate,callback);
Local inst = funTem->InstanceTemplate();
inst->SetNamedPropertyHandler(NamedGetter,NamedSetter,NamedQueryCallback,0,NamedEnumerator);
When Debugger tries to evaluate the expression of this object, only its NamedEnumarator is called but not its NamedGetter.
Can you please let me know what could be the issue here.

For reference, this was also asked and answered on the mailing list: https://groups.google.com/forum/#!topic/v8-dev/wTYrzdx4Y3Y.

Related

Assign and immediately delete property

I am no javascript programmer and am totally puzzled by what this code does and what it is used for:
function map(x) {
x = Object.create(null);
x.x = 0;
delete x.x;
return x;
}
It's part of what you get when using the dart2js compiler.
I'm not trying to understand the whole context, but what does assigning a property and deleting it directly again help achieve?
This looks like outsmarting some internal JS engine behaviour.
EDIT: As requested, here's the complete out.js as generated by dart2js (input is the "Hello world!" example from Wikipedia): https://gist.github.com/Blutkoete/59be155b2642832e9acd383df0857d02
EDIT 2: gurvinder372's link indicates is has to do with "delegating to anonymous JS objects for performance", but I'll probably need a lot of experience with JS to understand that.
Well... This is an interesting topic and understanding this trick takes to read a little bit on the object representation of V8 compiler. I am not an expert on this but the topic was interesting enough to intrigue me to search for some answer. So here is what i have found.
First of all deleting a property seems to be a trick to change the internal structure of how object properties are kept and accessed. In other words deleting a property switches the object to dictionary mode where the properties are kept in a hash map. So when a dummy property is deleted immediately after it's created gives you an object in dictionary mode.
V8 can handle minor divergences like this just fine, but if your code
assigns all sorts of random properties to objects from the same
constructor in no particular order, or if you delete properties, V8
will drop the object into dictionary mode, where properties are stored
in a hash table. This prevents an absurd number of maps from being
allocated.
Taken from this nice article A tour of V8: object representation

The "Object" object in node console vs in web console

I am relatively new to JavaScript and still trying to get my head around the prototypal inheritance. while trying to understand it I tried getting prototype property of Object object using Object.prototype in web console as well as in nodejs console. In web console it shows that the prototype property of Object object has an object with few members while in node the Object.prototype has an empty object. So my question is "is the Object object in node (console) different than the one in the web console because the are different javascript engines??"
When you ask for Object.prototype in node console, it will simply show an empty object. This is merely a representation of the base object. It still contains all the functions and properties you would expect and that show up in the browser console.
If you don't believe me, you can verify it yourself. Try typing Object.getOwnPropertyNames(Object) or Object.getOwnPropertyNames(Object.prototype) in your node console.
No, Object is the same in all implementations, the difference is just from the ways of representing it in the console.

Javascript access functions of constructor

I have an object called twillioObject.
When I do a console.log of twillioObject, I get this output in developer tools:
I want an object of the Connection and Device classes. It seems that I can't reach it.
I have experience in JavaScript but I haven't seen this situation before. How does this work?

How to implement an ES6 WeakMap polyfill (compared to Java) [duplicate]

I've run across a JavaScript library that implement a cross-browser WeakMap in ES5. (WeakMap is slated for ES6.)
How can this possibly work without support in the JavaScript language itself?
Edit: Just to be clear, I'm referring to a Weak Map, not a regular Map. I tested this project out using Chrome's profiler and the keys are not held by strong references. They get GC'ed without having to remove them from the WeakMap.
It took me a while to grok the code, but then it hit me: the key itself is used to store a reference to the value.
For example, several layers into set it does
defProp(obj, globalID, { value: store });
where defProp has been defined to be Object.defineProperty, obj is the key, globalID is a guid and store is a storage object that contains the value.
Then down in get it looks up the value with
obj[globalID];
This is very clever. The WeakMap doesn't actually contain a reference to anything (weak or otherwise)-- it just sets up a policy of where to secretly store the value. The use of Object.defineProperty means that you won't accidentally discover the value storage-- you have to know the magic guid to look it up.
Since the key directly refers to the value (and the WeakMap doesn't refer to it), when all references to the key are gone, it gets GCed like normal.

extracting all used javascript variables

I have a big object defined in the global scope called global. I would like to dynamically find all the referenced properties under my variable global. That is, all the properties that were accessed during the execution of the code.
I want to do static code analysis to extract all the referenced properties under my variable. I can search for these patterns: global.PROPERTY_NAME AND global[PROPERTY_NAM]. However, what about the complicated cases like these ones
var tmp="PROPERTY_NAME";
global[tmp]
OR
var tmp=global;
tmp.PROPERTY_NAME
and the other ones?
I don't want to get all the variable's properties. I only want a list of the referenced ONES!! the properties that were referenced in my source code only
After your edit:
What you're looking for is JavaScript Proxy objects. Here is a tutorial on how to do this using them.
Proxy objects let you wrap an object and execute a method whenever its properties are accessed. Unfortunately as it currently stands they are not widely supported.
This is currently only way in JavaScript to accomplish this without changing your original global object.
You can turn them on in Chrome by enabling experimental JavaScript in the about:flags tab.
Before your edit:
The feature you're looking for is called reflection, JavaScript supports it well and natively
Here is some code that iterates through an object and gets its properties
for(var prop in global){
if(global.hasOwnProperty(prop)){ //this is to only get its properties and not its prototype's
alert(prop+" => "+global[prop]);
}
}
This is fairly cross-browser. More modern browsers allow you to do this in simpler ways like Object.keys(global) which returns an array containing all its enumerable properties, or Object.getOwnPropertyNames(global) which returns both enumerable and not-enumerable properties.
Due to the dynamic nature of JavaScript you won't achieve that with static code analysis. Think about cases like this:
var prop = document.getElementById('prop').value;
global[prop];
Impossible. The alternative, dynamic analysis, would mean that you modify your global object to log access to its properties, then run the code. This is easily possible in JavaScript but it won't help you either because how would you assure that you have covered every possible access? Especially in a 5 MB JavaScript, there are most likely edge cases that you will oversee.
So, if you can't narrow down your requirement, it won't be possible.

Categories