Re: Embedding a REPL

2009-12-09 Thread Mike
Hey thanks to everyone who replied to this thread; I appreciate all
the ideas.

I managed to get my version working by closing *in*...but I had to use
my own code to start the repl, because main uses code that calls
System/exit after the repl completes (bad).

It turns out my (our...I didn't do it!) wrapper around System.in
wasn't implementing close() properly, so infinite calls to read(...)
occurred subsequently.

Thanks again everybody!
Mike

On Dec 8, 6:28 pm, Liam liam.ga...@gmail.com wrote:
 Close the *out* stream, not the *in*. That should do it.
 (. *out* close)

 It was fun watching that the first time it happend to me.

 ;-)

 On Dec 8, 11:26 am, Mike cki...@gmail.com wrote:

  I tried this approach, and it works great.  I had to spin the call to
  main.main() in another thread, but that's expected.

  What I didn't expect is that when I try to close the
  LineNumberingPushbackReader (to end the repl), I get infinite
  exceptions:

  java.io.IOException: Stream closed
  java.io.IOException: Stream closed
  java.io.IOException: Stream closed
  ...

  It appears that somewhere in the repl loop it's trying to do a read
  (or possibly unread in skip-whitespace?), printing the exception, but
  then not registering that it should exit, and then keeps trying to
  read again.

  I haven't really followed the code to see where the problem lies, but
  let me pose this question anyways:  what's the best way to close the
  repl?  I can't call (System/exit 0), 'cause the whole thing will come
  down.  I thought calling LNPR.close() on the input Reader would be
  like sending Ctrl-D to the console, but either I'm doing it wrong or
  that doesn't work for some reason.

  Any ideas?

  I love this simple approach, I didn't have to munge hardly any code (I
  had been traveling down the replace :read and :print and :prompt
  and :flush and... path, and it wasn't as pretty as I hoped).

  Thanks in advance...
  Mike

  On Dec 7, 7:26 pm, Liam liam.ga...@gmail.com wrote:

   I think the following is “looked down upon” or “discouraged“, but I
   managed to sift through how clojure itself handles its own stuff in
   java and I came up with the following.

   Say, that you want to set *out*, *in*, and *err* in clojure to
   something from Java before starting a REPL. Here is how I passed on
   these values to the clojure RT:

   try {
   Var.pushThreadBindings(RT.map(
   RT.OUT, new OutputStreamWriter(MYout),
   RT.IN, new LineNumberingPushbackReader(new InputStreamReader(MYin)),
   RT.ERR, new PrintWriter(new OutputStreamWriter(MYout), true)));

   main.main(new String[] {-r});

   } catch (Exception e) {}

   finally {
   Var.popThreadBindings();

   }

   Don’t forget to import (after setting clojure.jar on the cp).
   import clojure.main;
   import clojure.lang.RT;
   import clojure.lang.Var;

   Note that the doc-string of the clojure (repl function allows for
   hooks for some of what you want. You just need to look into how you
   could pass on those functions for  :need-
   prompt, :prompt, :flush, :read in a way that clojure can digest, which
   I think is just a Runnable in a map of sorts. But you’ll have to look
   into that to be sure.

   Regardless, I highly recommend that you separate Java from clojure
   coding as mush as possible, or at least treat clojure in a functional
   way when touching it from Java.

   I hope this helps. If someone else has a better way, I’m all ears.

   On Dec 7, 8:19 am, Mike cki...@gmail.com wrote:

I've seen an example of launching a Clojure script from Java (http://
en.wikibooks.org/wiki/Clojure_Programming/
Tutorials_and_Tips#Invoking_Clojure_from_Java), but I've got an
application in which I'd like to run a REPL.

My app has its own JPanel for display results, and a text area for
input, so I'll need to start repl with some replacement callback
functions (read, print, prompt, need-prompt).  I'd like to code as
much as possible in Clojure, but at some point I need to pass in some
Java object instances that my wrapper functions will use to perform I/
O for the repl.

Has anyone done this recently?  Could someone point me in the right
direction for exposing Java objects into Clojure?  I've tried reading
main.java and RT.java looking for hints, but I'm not too smart yet
about the Clojure environment, the scope of when things live, and
such.

Thanks in advance for any hints...
Mike- Hide quoted text -

  - Show quoted text -

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Embedding a REPL

2009-12-08 Thread Oliver Kaiser
Have you seen this post: http://ianp.org/2009/04/embedding-clojure-part-2/ ?
I haven't tried this, but it looks like what you are asking.

Regards,
tok

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Embedding a REPL

2009-12-08 Thread Mike
I tried this approach, and it works great.  I had to spin the call to
main.main() in another thread, but that's expected.

What I didn't expect is that when I try to close the
LineNumberingPushbackReader (to end the repl), I get infinite
exceptions:

java.io.IOException: Stream closed
java.io.IOException: Stream closed
java.io.IOException: Stream closed
...

It appears that somewhere in the repl loop it's trying to do a read
(or possibly unread in skip-whitespace?), printing the exception, but
then not registering that it should exit, and then keeps trying to
read again.

I haven't really followed the code to see where the problem lies, but
let me pose this question anyways:  what's the best way to close the
repl?  I can't call (System/exit 0), 'cause the whole thing will come
down.  I thought calling LNPR.close() on the input Reader would be
like sending Ctrl-D to the console, but either I'm doing it wrong or
that doesn't work for some reason.

Any ideas?

I love this simple approach, I didn't have to munge hardly any code (I
had been traveling down the replace :read and :print and :prompt
and :flush and... path, and it wasn't as pretty as I hoped).

Thanks in advance...
Mike

On Dec 7, 7:26 pm, Liam liam.ga...@gmail.com wrote:
 I think the following is “looked down upon” or “discouraged“, but I
 managed to sift through how clojure itself handles its own stuff in
 java and I came up with the following.

 Say, that you want to set *out*, *in*, and *err* in clojure to
 something from Java before starting a REPL. Here is how I passed on
 these values to the clojure RT:

 try {
 Var.pushThreadBindings(RT.map(
 RT.OUT, new OutputStreamWriter(MYout),
 RT.IN, new LineNumberingPushbackReader(new InputStreamReader(MYin)),
 RT.ERR, new PrintWriter(new OutputStreamWriter(MYout), true)));

 main.main(new String[] {-r});

 } catch (Exception e) {}

 finally {
 Var.popThreadBindings();

 }

 Don’t forget to import (after setting clojure.jar on the cp).
 import clojure.main;
 import clojure.lang.RT;
 import clojure.lang.Var;

 Note that the doc-string of the clojure (repl function allows for
 hooks for some of what you want. You just need to look into how you
 could pass on those functions for  :need-
 prompt, :prompt, :flush, :read in a way that clojure can digest, which
 I think is just a Runnable in a map of sorts. But you’ll have to look
 into that to be sure.

 Regardless, I highly recommend that you separate Java from clojure
 coding as mush as possible, or at least treat clojure in a functional
 way when touching it from Java.

 I hope this helps. If someone else has a better way, I’m all ears.

 On Dec 7, 8:19 am, Mike cki...@gmail.com wrote:

  I've seen an example of launching a Clojure script from Java (http://
  en.wikibooks.org/wiki/Clojure_Programming/
  Tutorials_and_Tips#Invoking_Clojure_from_Java), but I've got an
  application in which I'd like to run a REPL.

  My app has its own JPanel for display results, and a text area for
  input, so I'll need to start repl with some replacement callback
  functions (read, print, prompt, need-prompt).  I'd like to code as
  much as possible in Clojure, but at some point I need to pass in some
  Java object instances that my wrapper functions will use to perform I/
  O for the repl.

  Has anyone done this recently?  Could someone point me in the right
  direction for exposing Java objects into Clojure?  I've tried reading
  main.java and RT.java looking for hints, but I'm not too smart yet
  about the Clojure environment, the scope of when things live, and
  such.

  Thanks in advance for any hints...
  Mike

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Embedding a REPL

2009-12-08 Thread Albert Cardona
On Mon, Dec 7, 2009 at 11:19 AM, Mike cki...@gmail.com wrote:
 My app has its own JPanel for display results, and a text area for
 input, so I'll need to start repl with some replacement callback
 functions (read, print, prompt, need-prompt).  I'd like to code as
 much as possible in Clojure, but at some point I need to pass in some
 Java object instances that my wrapper functions will use to perform I/
 O for the repl.

 Has anyone done this recently?


See this example:

http://pacific.mpi-cbg.de/cgi-bin/gitweb.cgi?p=fiji.git;a=blob;f=src-plugins/Clojure/Clojure_Interpreter.java;hb=HEAD

Albert
--
http://albert.rierol.net

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Embedding a REPL

2009-12-08 Thread Liam
Close the *out* stream, not the *in*. That should do it.
(. *out* close)

It was fun watching that the first time it happend to me.

;-)


On Dec 8, 11:26 am, Mike cki...@gmail.com wrote:
 I tried this approach, and it works great.  I had to spin the call to
 main.main() in another thread, but that's expected.

 What I didn't expect is that when I try to close the
 LineNumberingPushbackReader (to end the repl), I get infinite
 exceptions:

 java.io.IOException: Stream closed
 java.io.IOException: Stream closed
 java.io.IOException: Stream closed
 ...

 It appears that somewhere in the repl loop it's trying to do a read
 (or possibly unread in skip-whitespace?), printing the exception, but
 then not registering that it should exit, and then keeps trying to
 read again.

 I haven't really followed the code to see where the problem lies, but
 let me pose this question anyways:  what's the best way to close the
 repl?  I can't call (System/exit 0), 'cause the whole thing will come
 down.  I thought calling LNPR.close() on the input Reader would be
 like sending Ctrl-D to the console, but either I'm doing it wrong or
 that doesn't work for some reason.

 Any ideas?

 I love this simple approach, I didn't have to munge hardly any code (I
 had been traveling down the replace :read and :print and :prompt
 and :flush and... path, and it wasn't as pretty as I hoped).

 Thanks in advance...
 Mike

 On Dec 7, 7:26 pm, Liam liam.ga...@gmail.com wrote:



  I think the following is “looked down upon” or “discouraged“, but I
  managed to sift through how clojure itself handles its own stuff in
  java and I came up with the following.

  Say, that you want to set *out*, *in*, and *err* in clojure to
  something from Java before starting a REPL. Here is how I passed on
  these values to the clojure RT:

  try {
  Var.pushThreadBindings(RT.map(
  RT.OUT, new OutputStreamWriter(MYout),
  RT.IN, new LineNumberingPushbackReader(new InputStreamReader(MYin)),
  RT.ERR, new PrintWriter(new OutputStreamWriter(MYout), true)));

  main.main(new String[] {-r});

  } catch (Exception e) {}

  finally {
  Var.popThreadBindings();

  }

  Don’t forget to import (after setting clojure.jar on the cp).
  import clojure.main;
  import clojure.lang.RT;
  import clojure.lang.Var;

  Note that the doc-string of the clojure (repl function allows for
  hooks for some of what you want. You just need to look into how you
  could pass on those functions for  :need-
  prompt, :prompt, :flush, :read in a way that clojure can digest, which
  I think is just a Runnable in a map of sorts. But you’ll have to look
  into that to be sure.

  Regardless, I highly recommend that you separate Java from clojure
  coding as mush as possible, or at least treat clojure in a functional
  way when touching it from Java.

  I hope this helps. If someone else has a better way, I’m all ears.

  On Dec 7, 8:19 am, Mike cki...@gmail.com wrote:

   I've seen an example of launching a Clojure script from Java (http://
   en.wikibooks.org/wiki/Clojure_Programming/
   Tutorials_and_Tips#Invoking_Clojure_from_Java), but I've got an
   application in which I'd like to run a REPL.

   My app has its own JPanel for display results, and a text area for
   input, so I'll need to start repl with some replacement callback
   functions (read, print, prompt, need-prompt).  I'd like to code as
   much as possible in Clojure, but at some point I need to pass in some
   Java object instances that my wrapper functions will use to perform I/
   O for the repl.

   Has anyone done this recently?  Could someone point me in the right
   direction for exposing Java objects into Clojure?  I've tried reading
   main.java and RT.java looking for hints, but I'm not too smart yet
   about the Clojure environment, the scope of when things live, and
   such.

   Thanks in advance for any hints...
   Mike- Hide quoted text -

 - Show quoted text -

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Embedding a REPL

2009-12-07 Thread Liam
I think the following is “looked down upon” or “discouraged“, but I
managed to sift through how clojure itself handles its own stuff in
java and I came up with the following.

Say, that you want to set *out*, *in*, and *err* in clojure to
something from Java before starting a REPL. Here is how I passed on
these values to the clojure RT:

try {
Var.pushThreadBindings(RT.map(
RT.OUT, new OutputStreamWriter(MYout),
RT.IN, new LineNumberingPushbackReader(new InputStreamReader(MYin)),
RT.ERR, new PrintWriter(new OutputStreamWriter(MYout), true)));

main.main(new String[] {-r});

} catch (Exception e) {}
finally {
Var.popThreadBindings();
}

Don’t forget to import (after setting clojure.jar on the cp).
import clojure.main;
import clojure.lang.RT;
import clojure.lang.Var;

Note that the doc-string of the clojure (repl function allows for
hooks for some of what you want. You just need to look into how you
could pass on those functions for  :need-
prompt, :prompt, :flush, :read in a way that clojure can digest, which
I think is just a Runnable in a map of sorts. But you’ll have to look
into that to be sure.

Regardless, I highly recommend that you separate Java from clojure
coding as mush as possible, or at least treat clojure in a functional
way when touching it from Java.

I hope this helps. If someone else has a better way, I’m all ears.


On Dec 7, 8:19 am, Mike cki...@gmail.com wrote:
 I've seen an example of launching a Clojure script from Java (http://
 en.wikibooks.org/wiki/Clojure_Programming/
 Tutorials_and_Tips#Invoking_Clojure_from_Java), but I've got an
 application in which I'd like to run a REPL.

 My app has its own JPanel for display results, and a text area for
 input, so I'll need to start repl with some replacement callback
 functions (read, print, prompt, need-prompt).  I'd like to code as
 much as possible in Clojure, but at some point I need to pass in some
 Java object instances that my wrapper functions will use to perform I/
 O for the repl.

 Has anyone done this recently?  Could someone point me in the right
 direction for exposing Java objects into Clojure?  I've tried reading
 main.java and RT.java looking for hints, but I'm not too smart yet
 about the Clojure environment, the scope of when things live, and
 such.

 Thanks in advance for any hints...
 Mike

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en