Skip to content

A pm.new and other helpers #146

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

Merged
merged 6 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion python/pythonmonkey/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Export public PythonMonkey APIs
from .pythonmonkey import *
from .helpers import *
from .require import *

# Expose the package version
Expand All @@ -24,4 +25,4 @@
Object.defineProperty(Object.prototype, "keys", keysMethod)
Object.defineProperty(Array.prototype, "keys", keysMethod)
}
""")(lambda *args: list(args))
""", { 'filename': __file__, 'fromPythonFrame': True })(lambda *args: list(args))
58 changes: 58 additions & 0 deletions python/pythonmonkey/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# @file helpers.py - Python->JS helpers for PythonMonkey
# - typeof operator wrapper
# - new operator wrapper
#
# @author Wes Garland, [email protected]
# @date July 2023
#

from . import pythonmonkey as pm
evalOpts = { 'filename': __file__, 'fromPythonFrame': True }

def typeof(jsval):
"""
typeof function - wraps JS typeof operator
"""
return pm.eval("""'use strict'; (
function pmTypeof(jsval)
{
return typeof jsval;
}
)""", evalOpts)(jsval);

def new(ctor):
"""
new function - emits function which wraps JS new operator, emitting a lambda which constructs a new
JS object upon invocation.
"""
if (typeof(ctor) == 'string'):
ctor = pm.eval(ctor)

newCtor = pm.eval("""'use strict'; (
function pmNewFactory(ctor)
{
return function newCtor(args) {
args = Array.from(args || []);
return new ctor(...args);
};
}
)""", evalOpts)(ctor)
return (lambda *args: newCtor(list(args)))

# List which symbols are exposed to the pythonmonkey module.
__all__ = [ "new", "typeof" ]

# Add the non-enumerable properties of globalThis which don't collide with pythonmonkey.so as exports:
globalThis = pm.eval('globalThis');
pmGlobals = vars(pm)

exports = pm.eval("""
Object.getOwnPropertyNames(globalThis)
.filter(prop => Object.keys(globalThis).indexOf(prop) === -1);
""", evalOpts)

for index in range(0, int(exports.length)):
name = exports[index]
if (pmGlobals.get(name) == None):
globals().update({name: globalThis[name]})
__all__.append(name)
2 changes: 1 addition & 1 deletion python/pythonmonkey/require.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,5 +352,5 @@ def require(moduleIdentifier: str):
filename = os.path.join(os.getcwd(), "__main__") # use the CWD instead
return createRequire(filename)(moduleIdentifier)

# Restrict what are exposed to the pythonmonkey module.
# Restrict what symbols are exposed to the pythonmonkey module.
__all__ = ["globalThis", "require", "createRequire", "runProgramModule"]