Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closures that are modified (like objects) are mistaken for static functions #31

Open
JobLeonard opened this issue Sep 21, 2017 · 2 comments
Labels

Comments

@JobLeonard
Copy link

JobLeonard commented Sep 21, 2017

In general plugin speeds up my code-base quite a bit, but in one crucial part it breaks my (admittedly quite unusual) code: the part where I generates a recursive tree of functions. I found a work-around, but I think this counts as a bug.

The part of the code in question that breaks is this:

//== INPUT
export function vectorOf(patternArr){
	let retVal= function(){};
	retVal.encoder = encodeVector(patternArr);
	retVal.decoder = decodeVector(patternArr);
	return retVal;
}

//== OUTPUT, functionally different:
function _retVal2() {}

function vectorOf(patternArr) {
	var retVal = _retVal2;
	retVal.encoder = encodeVector(patternArr);
	retVal.decoder = decodeVector(patternArr);
	return retVal;
}

(note that I have already removed the fat arrow syntax, but the problem remains)

The problem is that vectorOf should return newly instantiated function object whenever it is called, instead of modifying a static one. I understand that the whole point of this plugin is turn closures into the latter, but the part where I modify the function object breaks that :P

EDIT: my workaround didn't work, turns out I forgot to turn on the closure-elimination plugin while testing.

@jhen0409
Copy link
Collaborator

You may can be avoid the problem by new Function() first.

I think this problem involves the main feature, so it may be difficult to fix. Maybe we can use some comment rule in line to avoid transform? @Gvozd @phpnode

@jhen0409 jhen0409 added the bug label Oct 10, 2017
@JobLeonard
Copy link
Author

Ah, I'll try that!

Maybe I should just switch to old-school prototypical inheritance and use instanceof. It might actually perform better here too (not that this code was a bottleneck).

But I think a comment rule to prevent transformations on a section of code would be a useful feature to have anyway!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants