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

[builtins] Builtin function doesnt have constructor property #531

Closed
croraf opened this issue Jun 25, 2020 · 5 comments · Fixed by #604
Closed

[builtins] Builtin function doesnt have constructor property #531

croraf opened this issue Jun 25, 2020 · 5 comments · Fixed by #604
Assignees
Labels
bug Something isn't working builtins PRs and Issues related to builtins/intrinsics execution Issues or PRs related to code execution
Milestone

Comments

@croraf
Copy link
Contributor

croraf commented Jun 25, 2020

To Reproduce
[].findIndex.constructor

Expected behavior
Should return a Function constructor. Instead undefined is returned.

Custom functions do have constructor property which returns correctly.

@croraf croraf added the bug Something isn't working label Jun 25, 2020
@54k1
Copy link
Contributor

54k1 commented Aug 6, 2020

I think make_builtin_fn should take even global as parameter (apart from parent), just like make_constructor_fn. Then, we can set the constructor field as "Function" (fetched from global). Something like the following:

pub fn make_builtin_fn<N>(function: NativeFunctionData, name: N, parent: &Value, length: usize, global: &Value)
where
    N: Into<String> {
    ...
    let mut function = Object::function(Function::builtin(Vec::new(), function), Value::null());
    let constructor = Object::function(function, global.get_field("Function"));
    function.insert_property("constructor", Property::data_descriptor(constructor.into(),
        Attribute::READONLY | Attribute::NON_ENUMERABLE | Attribute::PERMANENT));
    ...
}

@HalidOdat Any thoughts about this?

@HalidOdat
Copy link
Member

I think make_builtin_fn should take even global as parameter (apart from parent), just like make_constructor_fn. Then, we can set the constructor field as "Function" (fetched from global).

Yes. this is good solution, maybe we should accept an Interpreter so we have more control and we can call Interpreter::global() which return the global object. Accepting Interpreter can make it easier to implement caching in the future for common objects like Object, Array, Function and there prototypes instead of doing it through .get_field.

@54k1 should I assign you to this issue?

@HalidOdat HalidOdat added execution Issues or PRs related to code execution builtins PRs and Issues related to builtins/intrinsics labels Aug 6, 2020
@54k1
Copy link
Contributor

54k1 commented Aug 6, 2020

Yes, that would be a good solution to take Interpreter as the parameter. Thanks for pointing it out.
I was however just checking the specification of Function instances. It does not mention anything about the "constructor" field. Is it something left to the implementation?

Sure, I can take this up.

@HalidOdat
Copy link
Member

HalidOdat commented Aug 6, 2020

I was however just checking the specification of Function instances. It does not mention anything about the "constructor" field. Is it something left to the implementation?

Good catch! I should have looked at the spec first. What we are trying to do is not spec compliant. the "constructor" field is not a part of the function instance, it is from the Function prototype. So what we need to do is set the prototype (__proto__), not the owned property field "constructor". here there already is a FIXME comment on it, currently the function instance prototype is set to Value::null() this is wrong we should set it to Function.prototype

We don't have to worry about setting "constructor" it is already done it all make_constructor_fn here.

[].findIndex.constructor This works because properties are searched in the prototype chain.

@54k1
Copy link
Contributor

54k1 commented Aug 6, 2020

[].findIndex.constructor This works because properties are searched in the prototype chain.

Thanks for pointing this out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working builtins PRs and Issues related to builtins/intrinsics execution Issues or PRs related to code execution
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants