Distributive-Network / PythonMonkey

A Mozilla SpiderMonkey JavaScript engine embedded into the Python VM, using the Python engine to provide the JS host environment.
https://pythonmonkey.io
Other
854 stars 40 forks source link

Add support for static class methods #368

Open olebedev opened 5 months ago

olebedev commented 5 months ago

Describe your feature request here.

Currently, when you define JS like this:

var Term = class {
  term;

  constructor(term) {
    this.term = term;
  }

  toString() {
    return this.term;
  }

  static fromString(input) {
    return new Term(input);
  }
};

module.exports = Term;

The static method is not available for that class:

import pythonmonkey as pm

Term = pm.require("./term.js")
print(Term.fromString)

Will give

Traceback (most recent call last):
  File "/test.py", line 4, in <module>
    print(Term.fromString)
AttributeError: 'pythonmonkey.JSFunctionProxy' object has no attribute 'fromString'

Code example

No response

wiwichips commented 4 months ago

Thanks for the bug report @olebedev - we're looking into this

wiwichips commented 4 months ago

In the meantime before it's fixed, if you need this behaviour, you can accomplish it through pm.eval

For example:

import pythonmonkey as pm

def js_static_class_prop(js_class, prop):
    return pm.eval('(c,p) => c[p]')(js_class, prop)

Term = pm.require("./term.js")
print(js_static_class_prop(Term, "fromString")) # <pythonmonkey.JSFunctionProxy object at 0x7fa038909570>