Calling Display (console.logging) on a function object will print its inner properties.
We need to implement Display for function objects, currently there arent any rules for function objects.
An entry would need to be made here:
https://github.com/jasonwilliams/boa/blob/master/boa/src/builtins/value/mod.rs#L728-L783
for ObjectKind::Function.
If the function is builtIn you can use String() { [native code] }
for an Ordinary function you may need to try displaying the Node inside.
You can test by adding
console.log(String);
then running it.
Relies on https://github.com/jasonwilliams/boa/pull/255 being merged
See also:
https://github.com/jasonwilliams/boa/blob/master/docs/debugging.md
I can take a stab at this!
I can take a stab at this!
Sure! Let's wait for #255 to land first, though :)
I can take a stab at this!
quick question: getting the name of the function doesn't seem to be trivial. I don't think the Function object stores that info.
Is there a reason for that? I might be missing something
quick question: getting the name of the function doesn't seem to be trivial. I don't think the Function object stores that info.
Is there a reason for that? I might be missing something
As you say, it's not trivial, but it's there. It's set as an internal field, so you need to call to get_field_slice("name") in the object to get the name.
Note that there are also anonymous functions that don't have name, and I think that we are not assigning a name on assignment of an anonymous function (which is something we should at some point in the future).
@1ntEgr8 how is it going with this?
@jasonwilliams sorry, i've been busy with work. i'll try to get this done within the next week. sorry for the delayed response
I'm actually fine with printing the function as an object with its properties. Like:
{
prototype: {
constructor: [Cycle],
__proto__: {
constructor: {
setPrototypeOf: {
length: 2
},
prototype: [Cycle],
name: Object,
length: 1,
defineProperty: {
length: 3
},
getPrototypeOf: {
length: 1
},
is: {
length: 2
},
__proto__: {
constructor: {
name: Function,
prototype: [Cycle],
length: 1,
__proto__: undefined
},
__proto__: undefined
}
},
hasOwnProperty: {
length: 0
},
propertyIsEnumerable: {
length: 0
},
toString: {
length: 0
}
}
},
length: 0,
__proto__: {
constructor: {
name: Function,
prototype: [Cycle],
length: 1,
__proto__: undefined
},
__proto__: undefined
}
}
This is something that currently gets outputted on:
const a = function () {}; a
But for console.log(a) it fails with:
thread 'main' panicked at 'assertion failed: o.get_type() == Type::Object', boa/src/exec/mod.rs:336:9
I would like at first to output function in console.log same as it is outputted when evaluating the function.
@1ntEgr8 has there been any progress with this?
Took a stab at it! I saw your release on the Reddit and thought I might try and contribute.
There are some fiddly bits about where to source the name of a function from for sure. I think the function name should be included in the Function enum instead of its wrapping value. (Or maybe both?)
I think it should be possible. All of the points of construction of function have access to the name. Just wanted to get your thoughts on it
Most helpful comment
As you say, it's not trivial, but it's there. It's set as an internal field, so you need to call to
get_field_slice("name")in the object to get the name.Note that there are also anonymous functions that don't have name, and I think that we are not assigning a name on assignment of an anonymous function (which is something we should at some point in the future).