Comments (2)
Tentative plan:
- Modify function calls - For calls to user functions and runtime library functions, convert all parameters to a single parameters object that JavaScript can handle, such as
{formals:[], keywords:{}}
. - Modify function definitions - For all user function and library function definitions, move parameter variables to function body, and function body to internal iife.
What does this get us?
- Our original Python parameters have been converted to something JavaScript understands.
- We can process the parameters in the function definition, while maintaining the original function's body (scope and code).
The example below doesn't include unpacking argument lists, but we can imagine passing a similar object containing the correct parameters.
Python input:
def foo(voltage, amps='a', charge='high', *args, **keywords):
print('voltage', voltage)
print('amps', amps)
print('charge', charge)
print('args', [a for a in args])
print('keywords', [(k, keywords[k]) for k in keywords])
print('')
foo(3)
foo(amps=4, voltage=7)
foo(1, 2, 3, 4, 5, 6, p="hi", q='boo')
JavaScript output:
// print() is in runtime library, included here for completeness
function print() {
var s = '';
for (var i = 0; i < arguments.length; i++)
s += arguments[i] + ' ';
console.log(s);
}
function foo() {
var _p = arguments[0];
var _fi = 0;
var voltage;
if (_fi < _p.formals.length) {
voltage = _p.formals[_fi];
_fi++;
} else if ('voltage' in _p.keywords) {
voltage = _p.keywords['voltage'];
delete _p.keywords['voltage'];
}
var amps = 'a';
if (_fi < _p.formals.length) {
amps = _p.formals[_fi];
_fi++;
} else if ('amps' in _p.keywords) {
amps = _p.keywords['amps'];
delete _p.keywords['amps'];
}
var charge = 'high';
if (_fi < _p.formals.length) {
charge = _p.formals[_fi];
_fi++;
} else if ('charge' in _p.keywords) {
charge = _p.keywords['charge'];
delete _p.keywords['charge'];
}
var args = [];
while (_fi < _p.formals.length) {
args.push(_p.formals[_fi]);
_fi++;
}
var keywords = _p.keywords;
return (function() {
print('voltage', voltage)
print('amps', amps)
print('charge', charge)
print('args', args)
var kws = '{'
for (var k in keywords) {
kws += k + ":" + keywords[k] + " ";
}
kws += '}';
print('keywords', kws)
print('')
})();
}
foo({formals:[3], keywords:{}});
foo({formals:[], keywords:{amps:4, voltage:7}})
foo({formals:[1, 2, 3, 4, 5, 6], keywords:{p:"hi", q:'boo'}})
from filbert.
Closing this uber-issue, and will open separate ones for remaining work.
Example for user-defined functions:
def foo(voltage, amps='a', charge='high', *args, **keywords):
print('voltage', voltage)
print('amps', amps)
print('charge', charge)
print('args length', len(args))
print('keywords length', keywords.length)
print('')
foo(3)
foo(amps=4, voltage=7)
foo(1, 2, 3, 4, 5, 6, p="hi", q='boo')
function foo() {
var __params0 = arguments.length === 1 && arguments[0].formals && arguments[0].keywords ? arguments[0] : null;
var __formalsIndex0 = 0;
var __args0 = arguments;
function __getParam0(v, d) {
var r = d;
if (__params0) {
if (__formalsIndex0 < __params0.formals.length) {
r = __params0.formals[__formalsIndex0++];
} else if (v in __params0.keywords) {
r = __params0.keywords[v];
delete __params0.keywords[v];
}
} else if (__formalsIndex0 < __args0.length) {
r = __args0[__formalsIndex0++];
}
return r;
}
var voltage = __getParam0('voltage');
var amps = __getParam0('amps', 'a');
var charge = __getParam0('charge', 'high');
var args = [];
var keywords = {};
if (__params0) {
while (__formalsIndex0 < __params0.formals.length) {
args.push(__params0.formals[__formalsIndex0++]);
}
keywords = __params0.keywords;
} else {
while (__formalsIndex0 < __args0.length) {
args.push(__args0[__formalsIndex0++]);
}
}
return function () {
__pythonRuntime.functions.print('voltage', voltage);
__pythonRuntime.functions.print('amps', amps);
__pythonRuntime.functions.print('charge', charge);
__pythonRuntime.functions.print('args length', __pythonRuntime.functions.len(args));
__pythonRuntime.functions.print('keywords length', keywords.length);
__pythonRuntime.functions.print('');
}.call(this);
}
foo(__pythonRuntime.utils.createParamsObj(3));
foo(__pythonRuntime.utils.createParamsObj({
amps: 4,
__kwp: true
}, {
voltage: 7,
__kwp: true
}));
foo(__pythonRuntime.utils.createParamsObj(1, 2, 3, 4, 5, 6, {
p: 'hi',
__kwp: true
}, {
q: 'boo',
__kwp: true
}));
from filbert.
Related Issues (20)
- int() rounds negative numbers in the wrong direction HOT 2
- Looping over characters in string gives indexes, not values HOT 1
- [Python] Reassigning a function parameter treats it as undefined until reassignment HOT 1
- Multiple Assignment doesn't work
- Move test python snippets to own files
- Python interpreter
- Implement chained comparisons for numerical ranges (0 < x < 10)
- String to list transformation HOT 2
- Memory usage spikes while trying to parse_dammit certain code. HOT 1
- str(True) and str(False): Cannot read property 'truthy' of undefined
- list.pop(index) doesn't work HOT 3
- Python lists don't have .push method HOT 1
- Null makes its way into ast body.body HOT 1
- v1 target refinement? HOT 2
- Compatibility with browsers HOT 1
- Is the project reviving? HOT 1
- Is there a way to check the types of nodes? (similar to @babel/types) HOT 3
- Taking back parsed object into python code HOT 1
- npm version is out of date HOT 2
- Support of named arguments?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from filbert.