BUG: incorrect lambda closure

Open
#667 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
javascript, python
Domain
compilers

Research direction

Start with the Python reproducer and compare its generated JavaScript, especially the lambda's intent and method bindings inside the loop. Trace the lambda translation and scope handling; done means the generated code preserves each iteration's captured values and the reproducer calls method_one successfully.

Written by the indexing model from the issue text.

Description

STATE: under consideration

see javascript-closure-inside-loops

  1. the following code
def method_one(intent, *args):
    print(f'method_one {intent}: {args}')


def method_two(intent, *args):
    print(f'method_two {intent}: {args}')


config_list = [
    ('one', method_one),
    ('two', method_two),
]

method_dict = dict()

for intent, method in config_list:
    print(f"intent={intent} method={method}")
    method_dict[intent] = lambda *args, intent = intent, method = method : method(intent, args)

method_dict['one']()

  1. produces output in python:
intent=one method=<function method_one at 0x7f7cb7111830>
intent=two method=<function method_two at 0x7f7cb7139950>
method_one one: ((),)
  1. and produces error in javascript:
tester.py:28 Uncaught TypeError: method is not a function
  1. due to translation:
export var method_one = function (intent) {
	var args = tuple ([].slice.apply (arguments).slice (1));
	print ('method_one {}: {}'.format (intent, args));
};
export var method_two = function (intent) {
	var args = tuple ([].slice.apply (arguments).slice (1));
	print ('method_two {}: {}'.format (intent, args));
};
export var config_list = [tuple (['one', method_one]), tuple (['two', method_two])];
export var method_dict = dict ();
for (var [intent, method] of config_list) {
	print ('intent={} method={}'.format (intent, method));
	method_dict [intent] = (function __lambda__ () {
		var intent = intent;
		var method = method;
		var args = tuple ([].slice.apply (arguments).slice (0));
		return method (intent, args);
	});
}
method_dict ['one'] ();
  1. where both intent and method are undefined via incorrect lambda closure:
		var intent = intent;
		var method = method;

6 . and one way to transform is:

for (var [intent, method] of config_list) {
	const intent_ = intent // const
	const method_ = method // const
	print ('intent={} method={}'.format (intent, method));
	method_dict [intent] = (function __lambda__ () {
		var intent = intent_;
		var method = method_;
		var args = tuple ([].slice.apply (arguments).slice (0));
		return method (intent, args);
	});
}

Dominant language
Python
Stars
2.9k
Forks
218
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from TranscryptOrg/Transcrypt

All issues in TranscryptOrg/Transcrypt

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.