Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign upSupporting return in blocks #130
Comments
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
|
Related: #135 |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
charliesome
Oct 22, 2012
Contributor
You'd probably need to use an exception here - at least that's how I implemented this in my experimental Ruby to PHP compiler
|
You'd probably need to use an exception here - at least that's how I implemented this in my experimental Ruby to PHP compiler |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
meh
Oct 22, 2012
Member
AFAIK there was support for this, there's probably a bug in the optimization recognition.
|
AFAIK there was support for this, there's probably a bug in the optimization recognition. |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
adambeynon
Oct 22, 2012
Contributor
As @charliesome says, to get full functionality, exceptions needed to be used, which really is a pain (in terms of ugly code generation and whatever performance losses on the blocks). Its one of those areas where Im trying to look for a better solution. The generator could follow the method of detecting break. This just checks the return values of blocks, and may be able to optimize a block return without exceptions in the majority of cases. Will look into it anyway.
|
As @charliesome says, to get full functionality, exceptions needed to be used, which really is a pain (in terms of ugly code generation and whatever performance losses on the blocks). Its one of those areas where Im trying to look for a better solution. The generator could follow the method of detecting |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
adambeynon
Oct 27, 2012
Contributor
Realistically this isnt going to be implemented by Opal, as raising/catching exceptions would be the only ideal way to do it, and thats not really ideal itself. In this specific instance, you could use Enumerable#find instead. Might reopen one day if some other clever way of handling this statement comes up.
|
Realistically this isnt going to be implemented by Opal, as raising/catching exceptions would be the only ideal way to do it, and thats not really ideal itself. In this specific instance, you could use Enumerable#find instead. Might reopen one day if some other clever way of handling this statement comes up. |
adambeynon
closed this
Oct 27, 2012
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
charliesome
Oct 27, 2012
Contributor
If I get around to it, I might try and implement this. It will require exceptions, but some trickery can be done so that we only need the try/catch for the few blocks that will return early.
FWIW, this is implemented in MRI with setjmp/longjmp - which is basically the C equivalent of exceptions.
|
If I get around to it, I might try and implement this. It will require exceptions, but some trickery can be done so that we only need the try/catch for the few blocks that will return early. FWIW, this is implemented in MRI with setjmp/longjmp - which is basically the C equivalent of exceptions. |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
adambeynon
Oct 27, 2012
Contributor
Inside the parser, @scope is the current scope, so if a return is detected, we could mark it there so we know if we do need to add a try/catch loop inside the method body. That does sound a nice enough solution. An error is created in runtime.js for breaks (even though theyre not implemented with exceptions anymore), so adding a hardcoded one for returns is probably a good idea too.
Foo.prototype.$method = function() {
try {
// block raises __opal.return_err
}
catch (err) {
if (err === __opal.return_err) {
return err.$v;
}
throw err;
}
};|
Inside the parser, @scope is the current scope, so if a return is detected, we could mark it there so we know if we do need to add a try/catch loop inside the method body. That does sound a nice enough solution. An error is created in runtime.js for breaks (even though theyre not implemented with exceptions anymore), so adding a hardcoded one for returns is probably a good idea too. Foo.prototype.$method = function() {
try {
// block raises __opal.return_err
}
catch (err) {
if (err === __opal.return_err) {
return err.$v;
}
throw err;
}
}; |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
charliesome
Oct 27, 2012
Contributor
This is basically what I was thinking of doing. Should I have a crack at it?
|
This is basically what I was thinking of doing. Should I have a crack at it? |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
adambeynon
Oct 27, 2012
Contributor
Yeah, it is quite an elegant solution. If your happy to work on it, then go for it. Let me know if there is anything you need assistance with
|
Yeah, it is quite an elegant solution. If your happy to work on it, then go for it. Let me know if there is anything you need assistance with |
tillda commentedOct 19, 2012
I'm pretty skilled in both JS and Ruby, so I was wondering how would you compile this:
It seems to me pretty tough to do both blocks and functions in JS and if I'm trying it right it doesn't work for me (it should return 6).
Any plans or thoughts on that?