Possibly stupid question

303 views
Skip to first unread message

Andrew Myers

unread,
Dec 26, 2011, 6:52:09 AM12/26/11
to gosu...@googlegroups.com
Hi,

If I have a class in a .gs file and a .gsp that references it, how do I compile it? Do I have to use vark? I couldn't find a 'gosuc' command but now I look at that more closely I figure it's not a great name for a compiler ;-)

Next question:

In groovy I can define a class, compile it, put it on the classpath and reference it from Java a though it was a Java class. Can we do this with a Gosu class, or do we need to implement something else to reference Java from Gosu?

Thanks in advance,
Andrew

Carson Gross

unread,
Dec 26, 2011, 10:53:38 AM12/26/11
to gosu...@googlegroups.com
Heya Andrew,

Gosu is lazily compiled from source at runtime, like ruby or python.  So you just run the program (with the appropriate classpath, set via the 'classpath' statement):

  gosu myprogram.gsp

Because of this model, it is currently non-trivial to include gosu resources in a java-hosted project: you have to initialize Gosu:

  Gosu.init()

somewhere in your program setup, and then use ReflectUtil to create new instances of Gosu object, which must be cast to a java interface that the gosu class implements:

  SomeJavaInterface instance = (SomeJavaInterface) ReflectUtil.construct( "pkg.MyClass", new Object[] );

  instance.someMethodOnTheInterface();

Obviously not ideal.  We plan on supporting cross compiling at some point (and, in fact, the IntelliJ work has given us a big chunk of the infrastructure we'd need to do so).

For the time being, it is easier to have Gosu host the project (either via a gosu program or a gosu framework like Ronin).  And, as I keep saying, developing in Gosu is going to be much easier for the open source world Any Minute Now, when we release the IntelliJ plugin, featuring Duke Nukem Forever support.  ;)

Merry Christmas!

Cheers,
Carson


--
You received this message because you are subscribed to the Google Groups "gosu-lang" group.
To post to this group, send email to gosu...@googlegroups.com.
To unsubscribe from this group, send email to gosu-lang+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/gosu-lang?hl=en.


Andrew Myers

unread,
Dec 26, 2011, 6:53:51 PM12/26/11
to gosu...@googlegroups.com
Hi Carson,

Thanks for your reply. So what have I done wrong here then :-(

main.gsp:

use abm.Person

var p = new Person("andrew", "myers", 37)

print (p.age)

===============

abm/Person.gs

package abm

class Person {

var _age : Integer as age
var _lastName : String as lastName
var _firstName : String as firstName

/* Constructs a new Person */
construct( firstName : String, lastName : String, age : Integer) {
_firstName = firstName
_lastName = lastName
_age = age
}

}

Output:

c:\Users\amyers\projects\gosu-dev\classes-tut\src>gosu main.gsp
gw.lang.parser.exceptions.ParseResultsException: program_.__Program__0

Errors:

No constructor found for class, Person [line:3 col:39] in
line 2:
line 3: var p = new Person("andrew", "myers", 37)
line 4:
Line Number: 3 Column: 39

"Person" is not a valid type. [line:3 col:13] in
line 2:
line 3: var p = new Person("andrew", "myers", 37)
line 4:
Line Number: 3 Column: 13

Not a statement. [line:1 col:1] in
line 1: use abm.Person
line 2:
Line Number: 1 Column: 1

Could not resolve symbol for : use [line:1 col:1] in
line 1: use abm.Person
line 2:
Line Number: 1 Column: 1

Expecting '=' for assignment statement. [line:1 col:9] in
line 1: use abm.Person
line 2:
Line Number: 1 Column: 9

Could not resolve symbol for : abm [line:1 col:5] in
line 1: use abm.Person
line 2:
Line Number: 1 Column: 5

No constructor found for class, Person [line:3 col:39] in
line 2:
line 3: var p = new Person("andrew", "myers", 37)
line 4:
Line Number: 3 Column: 39

"Person" is not a valid type. [line:3 col:13] in
line 2:
line 3: var p = new Person("andrew", "myers", 37)
line 4:
Line Number: 3 Column: 13

Could not resolve symbol for : p [line:5 col:8] in
line 4:
line 5: print (p.age)
Expected Type: Object
Line Number: 5 Column: 8


at gw.internal.gosu.parser.ParserBase.verifyParsedElement(ParserBase.jav
a:248)
at gw.internal.gosu.parser.GosuClassParser.parseDefinitions(GosuClassPar
ser.java:422)
at gw.internal.gosu.parser.GosuClass.compileDefinitionsIfNeeded(GosuClas
s.java:1650)
at gw.internal.gosu.parser.GosuClass.compileDefinitionsIfNeeded(GosuClas
s.java:1599)
at gw.internal.gosu.parser.GosuClass.isValid(GosuClass.java:897)
at gw.internal.gosu.parser.GosuProgram_Proxy.isValid(gw.internal.gosu.pa
rser.GosuProgram_Proxy:2)
at gw.internal.gosu.parser.GosuProgramParser.parseExpressionOrProgram(Go
suProgramParser.java:122)
at gw.lang.shell.Gosu.main(Gosu.java:149)

Andrew Myers

unread,
Dec 26, 2011, 6:55:26 PM12/26/11
to gosu...@googlegroups.com
I just realised it should be *uses abm.Person*, fixed that ,but still
getting "abm.Person is not a valid type" error....

Carson Gross

unread,
Dec 26, 2011, 10:36:04 PM12/26/11
to gosu...@googlegroups.com
Sounds like a classpath issue.  Where is your program with respect to your source directory?

So, with gosu, you can either pass in a classpath explicitly with the -classpath option or, better, you can specify the classpath *in* the program itself, using the "classpath" statement:

  classpath "../src/,../lib/"

The classpath statement is interpreted relative to the location of the program, and uses a comma to separate the path (so it is OS agnostic).

I'm online if you need some support over IM.

Cheers,
Carson

Andrew Myers

unread,
Dec 27, 2011, 8:27:19 AM12/27/11
to gosu...@googlegroups.com
Hi Carson,

Definitely was a classpath issue. My .gsp was in the root of my
directory structure, and my classes in packages beneath that. When I
included:

classpath "."

all worked fine.

Also, I managed to call a gosu class which implements a Java interface
from Java as you described.

So I made progress ;)

If I can make time tomorrow, I'll stick some of the simple examples
I've done so far on github and write up a blog post, mainly for my own
benefit but it might help others down the track.

Andrew.

P.S. For the CF integration I was talking about earlier, I need to
think some more. I clearly don't want to have to write Java
interfaces for everything. I'm thinking there'll be a better way, but
I haven't quite figured it out yet.

Carson Gross

unread,
Dec 27, 2011, 9:44:56 AM12/27/11
to gosu...@googlegroups.com
Great, I'll be online today (PST)

Yes, obviously the java-hosted solution isn't ideal for Gosu right now.  Scott (the Gosu lead) and Dumitru (the IJ lead) have done some foundational work for a cross-compiler, which is the obvious solution.  Scott is on vacation right now, but maybe he'll have something to say about it when he gets back.

With respect to CF, I think what I'd need to understand is how CF expressions compile/evaluate.  If it is purely bytecode based, loading classes, etc, then we'd need to figure out a way to get the gosu classloader into context for the CF expressions.  If it is pluggable (for example, to support JRuby) then we'd need to figure out how that works so we can hook it into Gosu correctly.

If it can work for Groovy/JRuby, we should be able to make it work for Gosu.

Cheers,
Carson 

Carson Gross

unread,
Dec 27, 2011, 8:31:36 PM12/27/11
to gosu...@googlegroups.com
I dug around a bit and found this blog post:


I think we could do something similar to this with Gosu pretty easily.

Cheers,
Carson

Andrew Myers

unread,
Dec 27, 2011, 10:30:38 PM12/27/11
to gosu...@googlegroups.com
Reckon we can pass a string of Gosu code in like that, rather than a file?  That'd be shit hot

Sent from my mobile

Carson Gross

unread,
Dec 27, 2011, 10:35:05 PM12/27/11
to gosu...@googlegroups.com
Well, we can certainly eval a string.  :)

I'm unclear on how argument passing works (is it just string replacement or something, or is there a symbol table somewhere?  Are there types?)

We should at least be able to match what this thing does.

Cheers,
Carson

Andrew Myers

unread,
Dec 27, 2011, 10:45:51 PM12/27/11
to gosu...@googlegroups.com
I'll have a fiddle with it but I think in the examples at scripting.riaforge.org they were passing in the various CF variable scopes (which are essentially 'structs' or maps).

So there's say a request scope.  Then Ruby can refer to $request.param1 or whatever, if that makes sense...

If my kids will have a lay down I can get playing with this soon.  Grrr :-)

Sent from my iPod.

Carson Gross

unread,
Dec 27, 2011, 10:49:10 PM12/27/11
to gosu...@googlegroups.com
Yeah, the trick will be determining the types of the symbols.  Which actually isn't much of a trick once you know how to do it, assuming we can get the gosu typesystem set up.

Cheers,
Carson

Andrew Myers

unread,
Dec 28, 2011, 2:13:25 AM12/28/11
to gosu...@googlegroups.com
Yeah CF is loosely typed so I think it'll be up to Gosu to try and figure it.  Unless we come up with some kind of parameter object we create from the CF side, maybe sonething like:

<cfscript>
  param1 = {}; // creates an empty struct
  param1.type = 'String'; // string literal describing type
  param1.value = '1234 Main Road';

  params = ArrayNew(1); // create empty array of dimension 1
  ArrayAppend(params, param1); // add param1 to array
  someGosuWrapper.invokeMethod('methodName', params)
</cfscript>

Pretty sure the CF array implements  java.util.Collection, can confirm later.

Sent from my mobile

Andrew Myers

unread,
Dec 28, 2011, 2:24:02 AM12/28/11
to Andrew Myers, gosu...@googlegroups.com
Actually if you're calling a method Godu already knows the params types so this  example is pointless.

I guess what you mean is the types of arbitrary variables, inside the various CF scopes

Sent from my mobile
Reply all
Reply to author
Forward
0 new messages