Skip to content

Calling JavaScript instance functions from Lua does not respect the JS function's scope #12

@TannerRogalsky

Description

@TannerRogalsky

When calling an instance of a JS prototype's function from Lua, this always refers to Window when it should instead refer to the instance.

Consider this code:

var employee = function(name) {
  this.name = name;
}

employee.prototype.say_name = function(){
  console.log(this.name);
}

var fred = new employee("fred");

When calling fred.say_name(); from JS, this refers to the fred object.
When calling fred.say_name() from Lua, this refers to Window.

I believe this to be because of this line setting the first argument to apply to be null. It's not obvious to me how to fix this because the instance does not seem to be in scope when executing the instruction.

Perhaps the caller needs to be closed over when creating the instruction? I'm a bit out of my depth here.

I've created a small branch to showcase this issue in action if it helps: https://github.com/TannerRogalsky/moonshine-love2d/tree/prototype_scope_bug

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions