Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Display for function objects #361

Closed
jasonwilliams opened this issue May 1, 2020 · 10 comments · Fixed by #1309
Closed

Implement Display for function objects #361

jasonwilliams opened this issue May 1, 2020 · 10 comments · Fixed by #1309
Labels
builtins PRs and Issues related to builtins/intrinsics cli Issues and PRs related to the Boa command line interface. E-Easy Easy enhancement New feature or request good first issue Good for newcomers
Milestone

Comments

@jasonwilliams
Copy link
Member

jasonwilliams commented May 1, 2020

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 jasonwilliams#255 being merged

See also:
https://github.com/jasonwilliams/boa/blob/master/docs/debugging.md

@jasonwilliams jasonwilliams added good first issue Good for newcomers E-Easy Easy labels May 1, 2020
@1ntEgr8
Copy link

1ntEgr8 commented May 2, 2020

I can take a stab at this!

@Razican
Copy link
Member

Razican commented May 2, 2020

I can take a stab at this!

Sure! Let's wait for #255 to land first, though :)

@Razican
Copy link
Member

Razican commented May 7, 2020

I can take a stab at this!

#255 landed, so you can start working on this! :D

@1ntEgr8
Copy link

1ntEgr8 commented May 13, 2020

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

@Razican
Copy link
Member

Razican commented May 13, 2020

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).

@jasonwilliams
Copy link
Member Author

@1ntEgr8 how is it going with this?

@1ntEgr8
Copy link

1ntEgr8 commented Jun 25, 2020

@jasonwilliams sorry, i've been busy with work. i'll try to get this done within the next week. sorry for the delayed response

@croraf
Copy link
Contributor

croraf commented Jun 27, 2020

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.

@Razican
Copy link
Member

Razican commented Oct 8, 2020

@1ntEgr8 has there been any progress with this?

@Razican Razican added this to the v0.12.0 milestone Jan 11, 2021
@Razican Razican modified the milestones: v0.12.0, v0.13.0 May 22, 2021
@kvnvelasco
Copy link
Contributor

kvnvelasco commented Jun 8, 2021

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

@Razican Razican added builtins PRs and Issues related to builtins/intrinsics cli Issues and PRs related to the Boa command line interface. enhancement New feature or request labels Jun 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
builtins PRs and Issues related to builtins/intrinsics cli Issues and PRs related to the Boa command line interface. E-Easy Easy enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants