@@ -20,9 +20,9 @@ def typeof(jsval):
2020}
2121 )""" , evalOpts )(jsval );
2222
23- def new (ctor , * args ):
23+ def instanciate (ctor , * args ):
2424 """
25- new function - wraps JS new operator
25+ instanciate function - wraps JS new operator and arguments
2626 """
2727 if (typeof (ctor ) == 'string' ):
2828 ctor = pm .eval (ctor )
@@ -39,11 +39,40 @@ def new(ctor, *args):
3939 const newArgs = [];
4040 for (let i=0; i < args.length; i++)
4141 newArgs[i] = args[i];
42- args = newArgs;
42+ args = newArgs;
4343 }
4444 return new ctor(...args);
4545}
4646 )""" , evalOpts )(ctor , list (args ));
4747
48+ def new (ctor ):
49+ """
50+ new function - emits function which wraps JS new operator, emitting a lambda which constructs a new
51+ JS object upon invocation.
52+ """
53+ if (typeof (ctor ) == 'string' ):
54+ ctor = pm .eval (ctor )
55+
56+ newCtor = pm .eval ("""'use strict'; (
57+ function pmNewFactory(ctor)
58+ {
59+ 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+ }
71+ return new ctor(...args);
72+ };
73+ }
74+ )""" , evalOpts )(ctor )
75+ return (lambda * args : newCtor (list (args )))
76+
4877# Restrict what symbols are exposed to the pythonmonkey module.
49- __all__ = ["new" , "typeof" ]
78+ __all__ = ["instanciate" , " new" , "typeof" ]
0 commit comments