Skip to content

Object methods use incorrect this #172

Closed
@wiwichips

Description

@wiwichips

Issue type

Bug

How did you install PythonMonkey?

Installed from pip

OS platform and distribution

Ubuntu 22.04

Python version (python --version)

Python 3.10.12

PythonMonkey version (pip show pythonmonkey)

0.2.1

Bug Description

When calling the method of a class, this in the scope of the function is globalThis and not the function's this

example.js

// old style class
function Rectangle(w, h) {
  this.w = w;
  this.h = h
}
 
Rectangle.prototype = {
  getThis: function () {
    return this;
  },
  getArea: function () {
    return this.w * this.h;
  },
};

// es5 class
class Rectangle2 {
  constructor(w,h) {
    this.w = w;
    this.h = h;
  }

  getThis() {
    return this;
  }

  getArea() {
    return this.w * this.h;
  }
}

module.exports.Rectangle = Rectangle;
module.exports.Rectangle2 = Rectangle2;

main.py

import pythonmonkey as pm

example = pm.require('./example')
r = pm.new(example.Rectangle)(1,2)

print(r.getArea()) # nan, should be 2
print(r.getThis()) # globalThis, should be { w: 1, h: 2 }

###
r2 = pm.new(example.Rectangle2)(1,2)

print(r2.getArea()) # nan, should be 2
print(r2.getThis()) # globalThis, should be { w: 1, h: 2 }

--- When I run python3 main.py I get the following output:

nan
{'python': {'pythonMonkey': {'dir': '/home/will/.local/lib/python3.10/site-packages/pythonmonkey', 'isCompilableUnit': <built-in function isCompilableUnit>, 'nodeModules': '/home/will/.local/lib/python3.10/site-packages/pythonmonkey/node_modules'}, 'stdout': {'write': <function <lambda> at 0x7fec94386710>, 'read': <function <lambda> at 0x7fec90642290>}, 'stderr': {'write': <function <lambda> at 0x7fec9079a830>, 'read': <function <lambda> at 0x7fec90642320>}, 'print': <built-in function print>, 'eval': <built-in function eval>, 'exec': <built-in function exec>, 'getenv': <function getenv at 0x7fec94373880>, 'paths': ['/home/will/test/pm-bug/getArea', '/usr/lib/python310.zip', '/usr/lib/python3.10', '/usr/lib/python3.10/lib-dynload', '/home/will/.local/lib/python3.10/site-packages', '/usr/local/lib/python3.10/dist-packages', '/usr/lib/python3/dist-packages'], 'exit': <built-in method JSFunctionCallable of tuple object at 0x7fec90458340>, 'load': <function load at 0x7fec90642560>}, 'pmEval': <built-in function eval>, 'bootstrap': {'modules': {'vm': {'runInContext': <built-in method JSFunctionCallable of tuple object at 0x7fec904584c0>}, 'ctx-module': {'makeNodeProgramContext': <built-in method JSFunctionCallable of tuple object at 0x7fec90458540>, 'CtxModule': <built-in method JSFunctionCallable of tuple object at 0x7fec90458500>}, 'debug': <built-in method JSFunctionCallable of tuple object at 0x7fec90458480>, 'fs': {'constants': {'S_IFDIR': 16384.0}, 'statSync': <built-in method JSFunctionCallable of tuple object at 0x7fec90458580>, 'statSync_inner': <function statSync_inner at 0x7fec906423b0>, 'readFileSync': <function readFileSync at 0x7fec90642440>, 'existsSync': <function existsSync at 0x7fec906424d0>}}, 'require': <built-in method JSFunctionCallable of tuple object at 0x7fec90458440>, 'builtinModules': {'debug': <built-in method JSFunctionCallable of tuple object at 0x7fec90458380>}}, 'console': {'log': <built-in method JSFunctionCallable of tuple object at 0x7fec90458600>, 'debug': <built-in method JSFunctionCallable of tuple object at 0x7fec90458640>, 'info': <built-in method JSFunctionCallable of tuple object at 0x7fec90458680>, 'warn': <built-in method JSFunctionCallable of tuple object at 0x7fec904586c0>, 'error': <built-in method JSFunctionCallable of tuple object at 0x7fec90458700>}, 'atob': <function <lambda> at 0x7fec90439d80>, 'btoa': <function <lambda> at 0x7fec90439ea0>}
nan
{'python': {'pythonMonkey': {'dir': '/home/will/.local/lib/python3.10/site-packages/pythonmonkey', 'isCompilableUnit': <built-in function isCompilableUnit>, 'nodeModules': '/home/will/.local/lib/python3.10/site-packages/pythonmonkey/node_modules'}, 'stdout': {'write': <function <lambda> at 0x7fec94386710>, 'read': <function <lambda> at 0x7fec90642290>}, 'stderr': {'write': <function <lambda> at 0x7fec9079a830>, 'read': <function <lambda> at 0x7fec90642320>}, 'print': <built-in function print>, 'eval': <built-in function eval>, 'exec': <built-in function exec>, 'getenv': <function getenv at 0x7fec94373880>, 'paths': ['/home/will/test/pm-bug/getArea', '/usr/lib/python310.zip', '/usr/lib/python3.10', '/usr/lib/python3.10/lib-dynload', '/home/will/.local/lib/python3.10/site-packages', '/usr/local/lib/python3.10/dist-packages', '/usr/lib/python3/dist-packages'], 'exit': <built-in method JSFunctionCallable of tuple object at 0x7fec90458bc0>, 'load': <function load at 0x7fec90642560>}, 'pmEval': <built-in function eval>, 'bootstrap': {'modules': {'vm': {'runInContext': <built-in method JSFunctionCallable of tuple object at 0x7fec90459300>}, 'ctx-module': {'makeNodeProgramContext': <built-in method JSFunctionCallable of tuple object at 0x7fec90459400>, 'CtxModule': <built-in method JSFunctionCallable of tuple object at 0x7fec90459480>}, 'debug': <built-in method JSFunctionCallable of tuple object at 0x7fec90459200>, 'fs': {'constants': {'S_IFDIR': 16384.0}, 'statSync': <built-in method JSFunctionCallable of tuple object at 0x7fec90459580>, 'statSync_inner': <function statSync_inner at 0x7fec906423b0>, 'readFileSync': <function readFileSync at 0x7fec90642440>, 'existsSync': <function existsSync at 0x7fec906424d0>}}, 'require': <built-in method JSFunctionCallable of tuple object at 0x7fec90459080>, 'builtinModules': {'debug': <built-in method JSFunctionCallable of tuple object at 0x7fec90459780>}}, 'console': {'log': <built-in method JSFunctionCallable of tuple object at 0x7fec90459840>, 'debug': <built-in method JSFunctionCallable of tuple object at 0x7fec904598c0>, 'info': <built-in method JSFunctionCallable of tuple object at 0x7fec90459940>, 'warn': <built-in method JSFunctionCallable of tuple object at 0x7fec904599c0>, 'error': <built-in method JSFunctionCallable of tuple object at 0x7fec90459a40>}, 'atob': <function <lambda> at 0x7fec90439d80>, 'btoa': <function <lambda> at 0x7fec90439ea0>}

getArea returns nan because globalThis.w === undefined and undefined * undefined = NaN, getThis returns globalThis because this is not set to the object this.

Standalone code to reproduce the issue

import pythonmonkey as pm

pm.eval("""
function Square(w) {
  this.w = w;
}
Square.prototype = {
  getThis: function () {
    return this;
  },
};
""")

Square = pm.globalThis.Square
r = pm.new(Square)(5)

print(r.getThis()) # globalThis, but should just be { w: 5 }

Relevant log output or backtrace

{'python': {'pythonMonkey': {'dir': '/home/will/.local/lib/python3.10/site-packages/pythonmonkey', 'isCompilableUnit': <built-in function isCompilableUnit>, 'nodeModules': '/home/will/.local/lib/python3.10/site-packages/pythonmonkey/node_modules'}, 'stdout': {'write': <function <lambda> at 0x7fb456a867a0>, 'read': <function <lambda> at 0x7fb452c42200>}, 'stderr': {'write': <function <lambda> at 0x7fb452d9a7a0>, 'read': <function <lambda> at 0x7fb452c42290>}, 'print': <built-in function print>, 'eval': <built-in function eval>, 'exec': <built-in function exec>, 'getenv': <function getenv at 0x7fb456a73880>, 'paths': ['/home/will/test/pm-bug/getArea', '/usr/lib/python310.zip', '/usr/lib/python3.10', '/usr/lib/python3.10/lib-dynload', '/home/will/.local/lib/python3.10/site-packages', '/usr/local/lib/python3.10/dist-packages', '/usr/lib/python3/dist-packages'], 'exit': <built-in method JSFunctionCallable of tuple object at 0x7fb452a58380>, 'load': <function load at 0x7fb452c424d0>}, 'pmEval': <built-in function eval>, 'bootstrap': {'modules': {'vm': {'runInContext': <built-in method JSFunctionCallable of tuple object at 0x7fb452a58500>}, 'ctx-module': {'makeNodeProgramContext': <built-in method JSFunctionCallable of tuple object at 0x7fb452a58580>, 'CtxModule': <built-in method JSFunctionCallable of tuple object at 0x7fb452a58540>}, 'debug': <built-in method JSFunctionCallable of tuple object at 0x7fb452a584c0>, 'fs': {'constants': {'S_IFDIR': 16384.0}, 'statSync': <built-in method JSFunctionCallable of tuple object at 0x7fb452a585c0>, 'statSync_inner': <function statSync_inner at 0x7fb452c42320>, 'readFileSync': <function readFileSync at 0x7fb452c423b0>, 'existsSync': <function existsSync at 0x7fb452c42440>}}, 'require': <built-in method JSFunctionCallable of tuple object at 0x7fb452a58480>, 'builtinModules': {'debug': <built-in method JSFunctionCallable of tuple object at 0x7fb452a583c0>}}, 'console': {'log': <built-in method JSFunctionCallable of tuple object at 0x7fb452a58640>, 'debug': <built-in method JSFunctionCallable of tuple object at 0x7fb452a58680>, 'info': <built-in method JSFunctionCallable of tuple object at 0x7fb452a586c0>, 'warn': <built-in method JSFunctionCallable of tuple object at 0x7fb452a58700>, 'error': <built-in method JSFunctionCallable of tuple object at 0x7fb452a58740>}, 'atob': <function <lambda> at 0x7fb452a39cf0>, 'btoa': <function <lambda> at 0x7fb452a39e10>, 'Square': <built-in method JSFunctionCallable of tuple object at 0x7fb452a58340>}

Additional info if applicable

No response

What branch of PythonMonkey were you developing on? (If applicable)

No response

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions