Skip to content

Commit c498576

Browse files
committed
helpers.py - add standard globals from ~ JS 1.7
1 parent d06ba9c commit c498576

File tree

1 file changed

+19
-38
lines changed

1 file changed

+19
-38
lines changed

python/pythonmonkey/helpers.py

Lines changed: 19 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -16,35 +16,10 @@ def typeof(jsval):
1616
return pm.eval("""'use strict'; (
1717
function pmTypeof(jsval)
1818
{
19-
return typeof(jsval)
19+
return typeof jsval;
2020
}
2121
)""", evalOpts)(jsval);
2222

23-
def instanciate(ctor, *args):
24-
"""
25-
instanciate function - wraps JS new operator and arguments
26-
"""
27-
if (typeof(ctor) == 'string'):
28-
ctor = pm.eval(ctor)
29-
30-
return pm.eval("""'use strict'; (
31-
function pmNew(ctor, args)
32-
{
33-
if (arguments.length === 1)
34-
args = [];
35-
36-
// work around pm list->Array bug, /wg july 2023
37-
if (!Array.isArray(args))
38-
{
39-
const newArgs = [];
40-
for (let i=0; i < args.length; i++)
41-
newArgs[i] = args[i];
42-
args = newArgs;
43-
}
44-
return new ctor(...args);
45-
}
46-
)""", evalOpts)(ctor, list(args));
47-
4823
def new(ctor):
4924
"""
5025
new function - emits function which wraps JS new operator, emitting a lambda which constructs a new
@@ -57,22 +32,28 @@ def new(ctor):
5732
function pmNewFactory(ctor)
5833
{
5934
return function newCtor(args) {
60-
if (arguments.length === 0)
61-
args = [];
62-
63-
// work around pm list->Array bug, /wg july 2023
64-
if (!Array.isArray(args))
65-
{
66-
const newArgs = [];
67-
for (let i=0; i < args.length; i++)
68-
newArgs[i] = args[i];
69-
args = newArgs;
70-
}
35+
args = Array.from(args || []);
7136
return new ctor(...args);
7237
};
7338
}
7439
)""", evalOpts)(ctor)
7540
return (lambda *args: newCtor(list(args)))
7641

42+
globalThis = pm.eval('globalThis');
43+
# standard ECMAScript global properties defined in ECMA 262-3 §15.1 except eval as it is ~supplied in pythonmonkey.so
44+
standard_globals = [ "Array", "Boolean", "Date", "decodeURI", "decodeURIComponent", "encodeURI",
45+
"encodeURIComponent", "Error", "EvalError", "Function", "Infinity", "isNaN",
46+
"isFinite", "Math", "NaN", "Number", "Object", "parseInt", "parseFloat",
47+
"RangeError", "ReferenceError", "RegExp", "String", "SyntaxError", "TypeError",
48+
"undefined", "URIError" ]
49+
# SpiderMonkey-specific globals, depending on compile-time options
50+
spidermonkey_extra_globals = [ "escape", "unescape", "uneval", "InternalError", "Script", "XML",
51+
"Namespace", "QName", "File", "Generator", "Iterator", "StopIteration" ]
52+
7753
# Restrict what symbols are exposed to the pythonmonkey module.
78-
__all__ = ["instanciate", "new", "typeof"]
54+
__all__ = [ "new", "typeof" ]
55+
56+
for name in standard_globals + spidermonkey_extra_globals:
57+
if (globalThis['hasOwnProperty'](name)):
58+
globals().update({name: globalThis[name]})
59+
__all__.append(name)

0 commit comments

Comments
 (0)