forked from Distributive-Network/PythonMonkey
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.py
More file actions
58 lines (50 loc) · 1.55 KB
/
helpers.py
File metadata and controls
58 lines (50 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# @file helpers.py - Python->JS helpers for PythonMonkey
# - typeof operator wrapper
# - new operator wrapper
#
# @author Wes Garland, wes@distributive.network
# @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)