A Mozilla SpiderMonkey JavaScript engine embedded into the Python VM, using the Python engine to provide the JS host environment.
OTHER License
Published by caleb-distributive 6 months ago
pmjs -e
/ pmjs -p
was not able to call functions that require the event loopusing a JS iterator in python:
import pythonmonkey as pm
myit = pm.eval('(function* () { yield 1; yield 2; })')()
print(next(myit)) # 1.0
print(next(myit)) # 2.0
print(next(myit)) # StopIteration exception
using a python iterator in JS:
import pythonmonkey as pm
myit = iter((1,2))
pm.eval("""
(myit) => {
console.log([...myit]); // [1, 2]
}
""")(myit)
Published by caleb-distributive 7 months ago
globalThis
for the value of this
import pythonmonkey as pm
class Counter:
def __init__(self):
self.count = 0
def increment(self):
self.count = self.count + 1
counter = Counter()
pm.eval("""
(pyObject) => {
console.log(pyObject.count); // 0
pyObject.increment();
console.log(pyObject.count); // 1
}
""")(counter)
import pythonmonkey as pm
jsFunc = pm.eval("(function() { this.count++; })")
class Counter:
def __init__(self):
self.count = 0
self.increment = pm.JSMethodProxy(jsFunc, self)
counter = Counter()
print(counter.count) # 0
counter.increment()
print(counter.count) # 1
pm.eval
can now accept a file object as its first argument (such as an object returned by the open()
python built-in), which is expected to be a javascript filepm.require
(or other requires created by pm.createRequire
), .py CommonJS modules now have precedence over .js modules when there is a namespace collisionsetTimeout
now returns a Node.js-style Timeout
class for the timeout id, with .ref()
and .unref()
methodsXMLHttpRequest.withCredentials
"json"
support for XMLHttpRequest.responseType
console
methodsPublished by philippedistributive 9 months ago
Our JS Proxies now implement all Array and Object methods on Python Lists and Dicts, and our Python List and Dict subtypes implements operators and methods on JS Arrays and Objects.
One can now do
items = [1, 2, 3, 4, 5]
result = [None]
pm.eval("(result, arr) => {result[0] = arr.some((element) => element % 2 === 0)}")(result, items)
-> result[0] == True
and going the other way
a = pm.eval("([1,2,3,4,5,6])")
b = a[1:5]
-> b == [2,3,4,5]
We also have all the iterators returned by such methods as values() now working without making copies of the object being iterator upon
Published by caleb-distributive 10 months ago
This release adds a new type, JSArrayProxy, a subtype of list, to properly proxy JavaScript Arrays as python lists, including having all of the appropriate methods.
This release also adds new methods to JSObjectProxy to more closely match dict. It now has all the same methods as dict except for the following: fromkeys(), items(), popitem(), update(), and values(). These will likely be added soon in a future release.
Published by caleb-distributive 12 months ago
This release includes implementations for URLSearchParams, XMLHttpRequest, atob, btoa, setTimeout, and clearTimeout. It also adds python 3.12 compatibility, better stringification for cross-language objects, and adds an event loop to pmjs. Finally, it fixes a bug preventing some users from installing pythonmonkey from source distributions.
Published by caleb-distributive about 1 year ago
This release includes a debugger for debugging JS code in pythonmonkey. It also exposes the new
and typeof
operators from Javascript as functions, as well all of the standard built-in objects.
Published by caleb-distributive over 1 year ago