Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Calling @netopyr's CoffeeScript javax.script engine from Groovy with @grab
@Grab('com.netopyr.coffee4java:coffee4java:1.0.0')
import javax.script.ScriptEngineManager
def coffeescript = new ScriptEngineManager().getEngineByName('coffeescript').factory.scriptEngine
assert coffeescript.eval('''\
square = (x) -> x * x
return square 3''') == 9
@glaforge

This comment has been minimized.

Copy link
Owner Author

@glaforge glaforge commented Oct 15, 2014

It's strange I have to use return to actually return a value, as square 3 wouldn't.
But that's perhaps my CoffeeScript which is weak!

@netopyr

This comment has been minimized.

Copy link

@netopyr netopyr commented Oct 15, 2014

CoffeScript wraps everything in an anonymous function by default to make it harder to pollute the global namespace. I should probably add an option that makes the wrapper optional.

@musketyr

This comment has been minimized.

Copy link

@musketyr musketyr commented Oct 21, 2014

I have this silly idea few day ago that it would be fantastic if you can write some code in CoffeeScript instead of Groovy because as soon as you start to use CoffeeScript more you find a plethora of unnecessary brackets everywhere. I'm totally lame in writing anything non-trivial such as class loaders or or compilers but the idea is

  1. have a .coffee file in similar structure as the groovy files (but maybe in src/main/coffee instead of src/main/groovy)
  2. when requested (hooking to the class loader?) parse the .coffee file to CoffeeScript AST and from it generate either a .groovy file or directly the Groovy script on the fly using byte code manipulation

The point is to be able to call Groovy code from CoffeeScript and on the other hand to be able to declare classes in CoffeeScript which can be called later from Groovy.

And idea how to achieve with small effort e.g. global AST executed in very early phase?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment