fglock / Perlito

"Perlito" Perl programming language compiler
http://fglock.github.io/Perlito/
Other
414 stars 47 forks source link

Lost Java types #52

Closed dionys closed 6 years ago

dionys commented 6 years ago

I have test script which imported java class:

package Java::Object { import => 'java.lang.Object' };
my Java::Object $obj = Java::Object->new();

So I try to run and got error:

> java -jar perlito5.jar -I src5/lib test.pl
/PlEval1.java:14: error: <identifier> expected
            obj_102 = new ();
                         ^
1 error
Unknown error during compilation. at test.pl

But this works ok:

 perl perlito5.pl -I src5/lib -C java test.pl > test.java; javac test.java; java Main

Configuration:

> java -version
openjdk version "1.8.0_151"
OpenJDK Runtime Environment (build 1.8.0_151-8u151-b12-0ubuntu0.16.04.2-b12)
OpenJDK 64-Bit Server VM (build 25.151-b12, mixed mode)

and last master.

fglock commented 6 years ago

The extension syntax package Java::Object { import => 'java.lang.Object' }; is currently only supported in pre-compilation mode (that is, using -Cjava).

This is in the documentation (although it could be more clear): Java extensions are disabled inside eval-string (only plain-perl)

The direction of development for Java extensions is not clear yet.

The current plan is to do something closer to the Nashorn API, https://docs.oracle.com/javase/8/docs/technotes/guides/scripting/prog_guide/javascript.html#A1147187 but something like JRuby is also possible, https://github.com/jruby/jruby/wiki/CallingJavaFromJRuby or Jython, http://www.jython.org/jythonbook/en/1.0/JythonAndJavaIntegration.html

Alternately, it would also be possible to make the current extension syntax "official" and go ahead with the integration in eval-string

dionys commented 6 years ago

The JRuby/Jython way looks good:

use java::lang::Object;

but there are many pitfalls in the implementation (namespace conflicts, etc.).

You also may using Perl-ish way, like use if pragma, e.g.:

use java 'java::lang::Object', as => 'Object';
fglock commented 6 years ago

I've added some more support for Java objects in the eval-string runtime, https://github.com/fglock/Perlito/blob/master/README-perlito5-Java.md#java-fields-methods-and-constructors It is work-in-progress, there is no "new syntax" yet (the examples are using Java::inline).

I think since the runtime supports the basic operations, we can experiment with several variants of the syntax.

fglock commented 6 years ago

There are some new features available: https://github.com/fglock/Perlito/blob/master/README-perlito5-Java.md#java-perl-module

Java is a Perl module: src5/lib/Perlito5X/Java/Java.pm

fglock commented 6 years ago

Typed variables now work in eval-string:

$ jrunscript -cp . -l Perl5
perl> package Java::Object { import => 'java.lang.Object' };
import
perl> my Java::Object $obj = Java::Object->new();
Object(0x4b44655e)

Note that some restrictions apply - https://github.com/fglock/Perlito/blob/master/README-perlito5-Java.md#using-typed-variables