0
var fun1=function(){console.log('hello');}
var fun2=fun1
console.log(fun2);

The above code run in Firefox prints fun2. In Chrome it prints the function body, in Node.js it prints Function.

Why is this difference?

How can I get Firefox's behaviour in Node.js ?

Why I am asking this ?

I am asking this because I would like to debug JS code generated from Idris where the JS runtime uses explicit call stack, and I would like to print the callstack content in a meaningful way and Firefox does that the best but I want to debug code on Node.js hence I would like to get Node.js to print functions as Firefox, how can I do that ?

EDIT:

A typical compiled function looks like this:

var _idris__123_io_95_bind2_125_ = function(oldbase){
  var myoldbase = new i$POINTER();
  i$valstack_top += 1;
  i$ret = new i$CON(65646,[i$valstack[i$valstack_base],i$valstack[i$valstack_base + 1],i$valstack[i$valstack_base + 2],i$valstack[i$valstack_base + 3],i$valstack[i$valstack_base + 4],i$valstack[i$valstack_base + 5]],_idris__123_APPLY0_125_$65646,null);
  i$valstack_top = i$valstack_base;
  i$valstack_base = oldbase.addr;
}

So here the useful information is the variable name _idris__123_io_95_bind2_125_ itself, and that is what is printed by Firefox, but not by node.js, and that is the problem, Firfox prints useful information, node.js does not.

So the question is, how can I make node.js to print _idris__123_io_95_bind2_125_ for the above function ?

EDIT 2:

Trying some of the suggestions don't work unfortunately :

>cat deb.js
var fun1=function(){console.log('hello');}
var fun2=fun1
console.log(fun2);
console.log(fun2.name);
console.log(fun2.toString());
console.log(fun2+'');
>node deb.js
[Function]

function (){console.log('hello');}
function (){console.log('hello');}
>
jhegedus
  • 20,244
  • 16
  • 99
  • 167

2 Answers2

2

you need to convert it to string try this :

console.log(fun2.toString());
shaouari
  • 236
  • 1
  • 3
1

UPDATE: Works with v0.12.7 as well. So I guess it would work with all node versions.

In node Following works

function someCoolFuntion (){
    /* Some code */
}
var fun1 = someCoolFuntion;

//Show name; Similar to Firefox's behaviour!
console.log(fun1.name)

//Show fullbody; Chrome's behaviour (as shaouari suggested)
console.log(fun1.toString());

Output

enter image description here

Hope this helps!

Dave Amit
  • 2,299
  • 12
  • 17
  • Hi, thanks, this prints the function body, but not `func2` (what Firefox prints) and that would be the useful name to know. – jhegedus Mar 08 '16 at 15:59
  • In other words `fun.name` prints an empty string. Really strange. – jhegedus Mar 08 '16 at 16:22
  • Oh, that would be expected behaviour, :( ... becuase function goes like `var x = function () ....`, `x.name` would print function name. Meaning, `var x = function abc() ...`, `x.name` would print `abc`. Make sense? I'm still looking into the matter. Just wanted to convey that it was expected behaviour. :) – Dave Amit Mar 08 '16 at 16:32
  • Yes, I agree, it is somewhat strange how and why Firefox does this, but it seems that it is pretty useful that it does it, hence this question. It would be nice to have this feature in node.js too. – jhegedus Mar 08 '16 at 16:37
  • 1
    @jhegedus ... As far as I was able to find and comprehend. It is not possible to get programatic access to `variable name`, though one can list out param name of a given function, but extraction name of a variable (Like what you expect --- firefox like) is not possible :( because, `ref` gets passed along and not the `variable` is self, meaning: `var x = function(){}` and when i do `console.log(x)` it merely passes the `function(){}` ref and no trace of `x`. – Dave Amit Mar 08 '16 at 17:40
  • Thanks Dave, I got help on #idris and now I changed the code-generator to include the missing info. – jhegedus Mar 08 '16 at 20:49