Namespace resolution in libinterp

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Namespace resolution in libinterp

AndrewJanke
Hi, Octave maintainers,

I'm interested in writing a patch for Octave to support Java-style
syntax for invocation of Java constructors and static methods, so you
can write, for example:

     jstr = java.lang.String('foo');
     prop = java.lang.System.getProperty('foo');

instead of:

     jstr = javaObject('java.lang.String', 'foo');
     prop = javaMethod('getProperty', 'java.lang.System', 'foo');

This would make Java objects more usable, and increase compatibility
with Matlab.

Can someone point me to the place in the Octave code (probably in
libinterp?) where it handles namespace resolution, so I can use that as
a starting point?

Cheers,
Andrew

Reply | Threaded
Open this post in threaded view
|

Re: Namespace resolution in libinterp

Sahil
AndrewJanke wrote
> Can someone point me to the place in the Octave code (probably in
> libinterp?) where it handles namespace resolution, so I can use that as
> a starting point?

Hi Andrew

A quick peek led me to these two files [1], [2]  which might be of your
interest.
Also, I couldn't see if the latter syntax is used by MATLAB, it still uses
the former one [3], I believe? Not sure how will it increase matlab
compatibility.

Best
Sahil
 
[1]
https://hg.savannah.gnu.org/hgweb/octave/file/74fbe0d91994/libinterp/octave-value/ov-java.cc
[2]
https://hg.savannah.gnu.org/hgweb/octave/file/74fbe0d91994/libinterp/octave-value/ov-java.h
[3] https://in.mathworks.com/help/matlab/ref/javaobject.html




--
Sent from: http://octave.1599824.n4.nabble.com/Octave-Maintainers-f1638794.html

Reply | Threaded
Open this post in threaded view
|

Re: Namespace resolution in libinterp

Carnë Draug
In reply to this post by AndrewJanke
On 7 July 2018 at 09:35, Andrew Janke <[hidden email]> wrote:

> Hi, Octave maintainers,
>
> I'm interested in writing a patch for Octave to support Java-style syntax
> for invocation of Java constructors and static methods, so you can write,
> for example:
>
>     jstr = java.lang.String('foo');
>     prop = java.lang.System.getProperty('foo');
>
> instead of:
>
>     jstr = javaObject('java.lang.String', 'foo');
>     prop = javaMethod('getProperty', 'java.lang.System', 'foo');
>
> This would make Java objects more usable, and increase compatibility with
> Matlab.
>
> Can someone point me to the place in the Octave code (probably in
> libinterp?) where it handles namespace resolution, so I can use that as a
> starting point?
>
> Cheers,
> Andrew

Hi Andrew

I think Matlab already has support for something similar so if you do
it, can you do it a Matlab compatible way?

Also, Octave should be doing automatic wrapping and unwrapping of
selected types.  One of them is char arrays <-> java strings, so you
should not need to explicitely do javaObject to get a java string that
can then be used in other functions.

Carnë

Reply | Threaded
Open this post in threaded view
|

Re: Namespace resolution in libinterp

AndrewJanke


On 7/9/18 8:12 AM, Carnë Draug wrote:

> On 7 July 2018 at 09:35, Andrew Janke <[hidden email]> wrote:
>> Hi, Octave maintainers,
>>
>> I'm interested in writing a patch for Octave to support Java-style syntax
>> for invocation of Java constructors and static methods, so you can write,
>> for example:
>>
>>      jstr = java.lang.String('foo');
>>      prop = java.lang.System.getProperty('foo');
>>
>> instead of:
>>
>>      jstr = javaObject('java.lang.String', 'foo');
>>      prop = javaMethod('getProperty', 'java.lang.System', 'foo');
>>
>> This would make Java objects more usable, and increase compatibility with
>> Matlab.
>>
>> Can someone point me to the place in the Octave code (probably in
>> libinterp?) where it handles namespace resolution, so I can use that as a
>> starting point?
>>
>> Cheers,
>> Andrew
> Hi Andrew
>
> I think Matlab already has support for something similar so if you do
> it, can you do it a Matlab compatible way?

Yes, that's the plan.

And possibly as a bonus: Matlab does not currently support referencing
Java inner classes in this manner. I'll see if I can add support for
that too, to get one step ahead.
> Also, Octave should be doing automatic wrapping and unwrapping of
> selected types.  One of them is char arrays <-> java strings, so you
> should not need to explicitely do javaObject to get a java string that
> can then be used in other functions.
java.lang.String was just an easy example. I probably should have picked
java.util.BigDecimal or UUID instead.

(Though, while char[] automatically gets converted to java.lang.String
when passed in to Java methods, I don't believe that returned
java.lang.Strings are auto-converted to Octave char[]s. I suspect this
is by design, and is to preserve object identity, which may be
significant for java.lang.Strings. Matlab also does this.)

Cheers,
Andrew

Reply | Threaded
Open this post in threaded view
|

Re: Namespace resolution in libinterp

apjanke
In reply to this post by Sahil
> A quick peek led me to these two files [1], [2]  which might be of your
interest.

The ov-java files define how Java objects behave inside Octave, but as far
as I can tell, they don't contain the code that would let the interpreter
determine whether a given expression (like "foo.bar.Baz") should resolve to
a Java class reference.

I thinnk what I'm looking for is more something under libinterp/parse-tree.
For example, if you had a file `+foo/+bar/Baz.m`, what interpreter code
would look at the expression "foo.bar.Baz" and determine that it resolved to
that function?

> Also, I couldn't see if the latter syntax is used by MATLAB, it still uses
the former one [3], I believe? Not sure how will it increase matlab
compatibility.

Matlab supports both syntaxes, and has for a while. For example, in Matlab,
you can just say `m = java.util.HashMap` instead of `m =
javaObject('java.util.HashMap')`. This syntax is convenient if you're doing
substantial work with Java objects inside Matlab code.

Cheers,
Andrew



--
Sent from: http://octave.1599824.n4.nabble.com/Octave-Maintainers-f1638794.html

Reply | Threaded
Open this post in threaded view
|

Re: Namespace resolution in libinterp

AndrewJanke
In reply to this post by Sahil
> A quick peek led me to these two files [1], [2]  which might be of your
interest.

The ov-java.* files define how Java objects behave inside the Octave
interpreter. But I'm looking for something that happens earlier: how the
Octave interpreter would determine that an expression like "foo.bar.Baz"
should be interpreted as a Java class reference. It'd be basically the same
process that lets "foo.bar.Baz" resolve to a function file "+foo/+bar/Baz.m"
using Octave/Matlab namespaces.

> Also, I couldn't see if the latter syntax is used by MATLAB, it still uses
the former one [3], I believe? Not sure how will it increase matlab
compatibility.

Matlab supports both syntaxes. For example, you can just say "m =
java.util.HashMap" instead of "m = javaObject('java.util.HashMap')". This
syntax is convenient if you're doing extensive work with Java objects inside
Matlab, or if you're writing classes that might be implemented in either
M-code or Java.

Cheers,
Andrew



--
Sent from: http://octave.1599824.n4.nabble.com/Octave-Maintainers-f1638794.html