This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Re: Scala with embedded Scala ?

10 replies
Warren Henning
Joined: 2008-12-31,
User offline. Last seen 42 years 45 weeks ago.

Yo dawg, I heard you like Scala so we put a Scala in your Scala so you
can code while you code.

I've never heard of anyone doing this, so if you're able to, let us know.

Warren

On Wed, Mar 18, 2009 at 1:02 PM, Peter wrote:
> Hello
>
> Ok I am writing a litle framework for myself doing some simple simulations.
> And I want theese Agents to be scriptable during runtime. So my first notion
> was to embedd a script language.
>
> Embedding groovy or JRuby would be simple enough, but my question is, do I
> need them ? I found a "scala-compiler" artifact in the maven repository so I
> am wondering if I easily can use scala as embeddable "scripting".  engine ?
>
> I didnt find any documentation on a compiler interface.
>
>
>
> Greets
>
>
> Peter
>

Peter
Joined: 2009-03-16,
User offline. Last seen 42 years 45 weeks ago.
Scala with embedded Scala ?

Hello

Ok I am writing a litle framework for myself doing some simple simulations.
And I want theese Agents to be scriptable during runtime. So my first notion
was to embedd a script language.

Embedding groovy or JRuby would be simple enough, but my question is, do I
need them ? I found a "scala-compiler" artifact in the maven repository so I
am wondering if I easily can use scala as embeddable "scripting". engine ?

I didnt find any documentation on a compiler interface.

Greets

Peter

Peter
Joined: 2009-03-16,
User offline. Last seen 42 years 45 weeks ago.
Re: Scala with embedded Scala ?

Am Mittwoch 18 März 2009 21:05:51 schrieb Warren Henning:
> Yo dawg, I heard you like Scala so we put a Scala in your Scala so you
> can code while you code.
>
> I've never heard of anyone doing this, so if you're able to, let us know.
>

Well actually I know of one application that can do this. Its a little
obscure. its a command line tool called "scala" :)

Still reverse engineering the compiler interface is not really something on my
chrismas wishlist

Peter

Joshua.Suereth
Joined: 2008-09-02,
User offline. Last seen 32 weeks 5 days ago.
Re: Scala with embedded Scala ?
It is possible to do so.  If you want you can take a look at the eclipse source code (you need to find the old java version, or look for the code form before my latest update to the eclipse interpreter).

I believe it entails creating your own Interpreter/InterpreterLoop and praying it doesn't crash the rest of your application.  Paul Phillips knows the most about the interpreter at this point IIRC.  I'm not sure there's any documentation, but I'd be happy to look up what I was doing (which was rather simple) if needed.

-Josh

On Wed, Mar 18, 2009 at 4:02 PM, Peter <ptriller@soapwars.de> wrote:
Hello

Ok I am writing a litle framework for myself doing some simple simulations.
And I want theese Agents to be scriptable during runtime. So my first notion
was to embedd a script language.

Embedding groovy or JRuby would be simple enough, but my question is, do I
need them ? I found a "scala-compiler" artifact in the maven repository so I
am wondering if I easily can use scala as embeddable "scripting".  engine ?

I didnt find any documentation on a compiler interface.



Greets


Peter

Michael
Joined: 2009-02-23,
User offline. Last seen 20 weeks 1 day ago.
Re: Scala with embedded Scala ?

>
> Embedding groovy or JRuby would be simple enough, but my question is, do I
> need them ? I found a "scala-compiler" artifact in the maven repository so I
> am wondering if I easily can use scala as embeddable "scripting". engine ?

Have a look at scala.tools.nsc.Interpreter. Maybe this is what you are
looking for?

Michael

Naftoli Gugenheim
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.
Re: Scala with embedded Scala ?
There's a link on some scala Trac ticket to javax.script implementation that uses the Scala interpreter. Does anyone remember where?

On Wed, Mar 18, 2009 at 4:14 PM, Peter <ptriller@soapwars.de> wrote:
Am Mittwoch 18 März 2009 21:05:51 schrieb Warren Henning:
> Yo dawg, I heard you like Scala so we put a Scala in your Scala so you
> can code while you code.
>
> I've never heard of anyone doing this, so if you're able to, let us know.
>

Well actually I know of one application that can do this. Its a little
obscure. its a command line tool called "scala" :)

Still reverse engineering the compiler interface is not really something on my
chrismas wishlist



Peter


Michael
Joined: 2009-02-23,
User offline. Last seen 20 weeks 1 day ago.
Re: Scala with embedded Scala ?

> There's a link on some scala Trac ticket to javax.script implementation that

http://lampsvn.epfl.ch/trac/scala/ticket/874

Michael

Sciss
Joined: 2008-12-17,
User offline. Last seen 28 weeks 5 days ago.
Re: Re: Scala with embedded Scala ?

some tutorial or documentation on this would be really usefull! questions would be:

- how to create a REPL using public and future safe API
- how to use fsc or the compile server so that warm-up time is minimized as much as possible
- how to feed the classpath of the calling instance into the compiler
- how to deal with threading / deferring compilation to a background thread / picking up execution
of the code in a specific thread (e.g. AWT event dispatcher thread)

thanks, -sciss-

Dean Wampler
Joined: 2008-12-26,
User offline. Last seen 42 years 45 weeks ago.
Re: Scala with embedded Scala ?
One example is a build tool that parses a build "script" written in Scala. However, I haven't looked at what sbt, etc. do. Maybe they get around this.

On Wed, Mar 18, 2009 at 3:05 PM, Warren Henning <warren.henning@gmail.com> wrote:
Yo dawg, I heard you like Scala so we put a Scala in your Scala so you
can code while you code.

I've never heard of anyone doing this, so if you're able to, let us know.

Warren

On Wed, Mar 18, 2009 at 1:02 PM, Peter <ptriller@soapwars.de> wrote:
> Hello
>
> Ok I am writing a litle framework for myself doing some simple simulations.
> And I want theese Agents to be scriptable during runtime. So my first notion
> was to embedd a script language.
>
> Embedding groovy or JRuby would be simple enough, but my question is, do I
> need them ? I found a "scala-compiler" artifact in the maven repository so I
> am wondering if I easily can use scala as embeddable "scripting".  engine ?
>
> I didnt find any documentation on a compiler interface.
>
>
>
> Greets
>
>
> Peter
>



--
Dean Wampler
twitter: @deanwampler, @chicagoscala
Chicago-Area Scala Enthusiasts (CASE):
-  http://groups.google.com/group/chicagoscala
-  http://www.meetup.com/chicagoscala/ (Meetings)
http://www.objectmentor.com
http://www.polyglotprogramming.com
http://www.aspectprogramming.com
http://aquarium.rubyforge.org
http://www.contract4j.org
Peter
Joined: 2009-03-16,
User offline. Last seen 42 years 45 weeks ago.
Re: Scala with embedded Scala ?

Am Donnerstag 19 März 2009 00:27:45 schrieb Sc iss:
> some tutorial or documentation on this would be really usefull! questions
> would be:
>
> - how to create a REPL using public and future safe API
> - how to use fsc or the compile server so that warm-up time is minimized as
> much as possible - how to feed the classpath of the calling instance into
> the compiler - how to deal with threading / deferring compilation to a
> background thread / picking up execution of the code in a specific thread
> (e.g. AWT event dispatcher thread)
>
> thanks, -sciss-
>

Yes, I agree, thos would probably be most of the questions for a normal
embedding.

What I have in Mind is a little more complex

- there is a directory with some Common scripts. so I probably just need to
compile them and get the resulting classes into a classloader for usage and a
classpath for compilation of the Agent scripts.

- Each Agent gets its own scripts into its own classloader compiled and the
parent classloader is the common scripts classloader.

That way I can swap out Agent scripts without any problem. Swapping out Common
scripts would require to compile everything anew, but I guess there is no way
around that

So now I just have to figure out 2 things:

1) How do I compile a bunch of Scala source files into a specific classloader

2) How do can I add theese freshly compiled classes into the Compile Classpath
for other scripts.

The rest sounds trivial

If anyone has some hints on this, any help is apreciated

Peter

david.bernard
Joined: 2009-01-08,
User offline. Last seen 1 year 27 weeks ago.
Re: Scala with embedded Scala ?

May be something that could help you : an jedit scala macro handler :

...

import _root_.scala.tools.nsc.{Interpreter, Settings}
import _root_.scala.io.Source

import java.io.{File, StringWriter, PrintWriter}

class MacroHandler4Scala extends Macros.Handler("scala") {

def createMacro(macroName: String, path: String): Macros.Macro = {
var label = Macros.Macro.macroNameToLabel(macroName)
// Remove the '.scala' from the label, but not the macroName
label = label.substring(0, label.length() - 6);
new Macros.Macro(this, macroName, label, path);
}

def runMacro(view: View, macro: Macros.Macro) {
val settings = new Settings(null)
val dir = jEdit.getPlugin(classOf[ScalaMiniIDEPlugin].getName).getPluginJAR.getFile.getParentFile
settings.classpath.value = List(
settings.classpath.value,
new File(dir, "scala-library.jar").getAbsolutePath,
new File(dir, "scala-compiler.jar").getAbsolutePath
).mkString(File.pathSeparator)

val out = new StringWriter()
val interp = new Interpreter(settings, new PrintWriter(out)){
override def parentClassLoader =
classOf[MacroHandler4Scala].getClassLoader
}
try {
interp.beQuietDuring({
interp.bind("view", classOf[View].getName, view)
interp.bind("buffer", classOf[Buffer].getName, view.getBuffer)
interp.bind("editPane", classOf[EditPane].getName, view.getEditPane)
interp.bind("textArea", classOf[JEditTextArea].getName,
view.getTextArea)
})
Source.fromFile(macro.getPath).getLines.foreach(interp.interpret(_))
val output = out.toString
if (output.length > 0) {
new TextAreaDialog(view, "scala-eval", "output",
UIManager.getIcon("OptionPane.errorIcon"), output)
}
} catch {
case e => {
Log.log(Log.ERROR, classOf[MacroHandler4Scala], e);
new TextAreaDialog(view,"scala-eval-error",e)
}
} finally {
interp.close
}
}

}

On Thu, Mar 19, 2009 at 00:51, Peter wrote:
> Am Donnerstag 19 März 2009 00:27:45 schrieb Sc iss:
>> some tutorial or documentation on this would be really usefull! questions
>> would be:
>>
>> - how to create a REPL using public and future safe API
>> - how to use fsc or the compile server so that warm-up time is minimized as
>> much as possible - how to feed the classpath of the calling instance into
>> the compiler - how to deal with threading / deferring compilation to a
>> background thread / picking up execution of the code in a specific thread
>> (e.g. AWT event dispatcher thread)
>>
>> thanks, -sciss-
>>
>
> Yes, I agree, thos would probably be most of the questions for a normal
> embedding.
>
> What I have in Mind is a little more complex
>
> - there is a directory with some Common scripts. so I probably just need to
> compile them and get the resulting classes into a classloader for usage and a
> classpath for compilation of the Agent scripts.
>
> - Each Agent gets its own scripts into its own classloader compiled and the
> parent classloader is the common scripts classloader.
>
> That way I can swap out Agent scripts without any problem. Swapping out Common
> scripts would require to compile everything anew, but I guess there is no way
> around that
>
> So now I just have to figure out 2 things:
>
> 1) How do I compile a bunch of Scala source files into a specific classloader
>
> 2) How do can I add theese freshly compiled classes into the Compile Classpath
> for other scripts.
>
>
> The rest sounds trivial
>
> If anyone has some hints on this, any help is apreciated
>
> Peter
>

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland