Skip to content

Instantly share code, notes, and snippets.

@glaforge
Created October 15, 2014 12:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glaforge/f7ddece9d4e0ff2afe82 to your computer and use it in GitHub Desktop.
Save glaforge/f7ddece9d4e0ff2afe82 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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
Copy link

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
Copy link

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