From sundararajan.athijegannathan at oracle.com Mon Oct 1 03:00:08 2018 From: sundararajan.athijegannathan at oracle.com (Sundararajan Athijegannathan) Date: Mon, 01 Oct 2018 08:30:08 +0530 Subject: Extending a Java class In-Reply-To: <3236fc28-97fd-5a38-cc5d-ad5ec73203f2@pinc-software.de> References: <67ff4397-ab78-12c1-bde1-73b8af623b06@pinc-software.de> <5BADF5B1.3090806@oracle.com> <3236fc28-97fd-5a38-cc5d-ad5ec73203f2@pinc-software.de> Message-ID: <5BB18DB8.7060308@oracle.com> Hi, In your example, it seems you're trying to add a new method to the subtype created in script. That is not supported. You can override existing methods of superclass - but not add a new method to the Java type. Note that all super methods (including protected ones) are available via Java.super. For example, you can invoke Java.super(filter).fireMatchNode(). So the workaround for this case would be add utility function outside for "setEnabled". Hope this helps, -Sundar On 28/09/18, 3:19 PM, Axel D?rfler wrote: > Am 28/09/2018 um 11:34 schrieb Sundararajan Athijegannathan: >> It is hard to say what went wrong without looking at your full >> sample/test. >> >> Openjdk wiki page explains Java.extend function is here: >> http://hg.openjdk.java.net/jdk/jdk/file/7bd8d6b011c9/src/sample/nashorn/resourcetrysuggester.js > > > Thanks! This usage suggests that it should actually work. > > Here is my full example stripped down to the bare minimum: > > ----------------8<---------------- > var MatcherEditor = > Java.type("ca.odell.glazedlists.matchers.AbstractMatcherEditor"); > > var filter = new (Java.extend(MatcherEditor)) { > setEnabled: function(enabled) { > if (enabled) { > fireMatchNone(); > } else { > fireMatchAll(); > } > } > }; > filter.setEnabled(true); > ----------------8<---------------- > > The error is: > TypeError: filter.setEnabled is not a function in at line > number 13 > > For the Glazedlists class used, see here: > http://static.javadoc.io/net.java.dev.glazedlists/glazedlists_java16/1.10.0/ca/odell/glazedlists/matchers/AbstractMatcherEditor.html > > > Source: > https://github.com/glazedlists/glazedlists/blob/master/core/src/main/java/ca/odell/glazedlists/matchers/AbstractMatcherEditor.java > > > That looks like a bug to me. But if there's a workaround, I'll take it > :-) > > Kind regards, > Axel D?rfler. From axeld at pinc-software.de Tue Oct 2 10:22:26 2018 From: axeld at pinc-software.de (=?UTF-8?Q?Axel_D=c3=b6rfler?=) Date: Tue, 2 Oct 2018 12:22:26 +0200 Subject: Extending a Java class In-Reply-To: <5BB18DB8.7060308@oracle.com> References: <67ff4397-ab78-12c1-bde1-73b8af623b06@pinc-software.de> <5BADF5B1.3090806@oracle.com> <3236fc28-97fd-5a38-cc5d-ad5ec73203f2@pinc-software.de> <5BB18DB8.7060308@oracle.com> Message-ID: <2489072c-2889-fad9-7dfc-0ce0d5bbf4f9@pinc-software.de> Hi Sundar, Am 01/10/2018 um 05:00 schrieb Sundararajan Athijegannathan: > In your example, it seems you're trying to add a new method to the > subtype created in script. That is not supported. You can override > existing methods of superclass? - but not add a new method to the Java > type. Bummer! resourcetrysuggester.js added new fields to a Java type, so I thought it should work. > Note that all super methods (including protected ones) are > available via Java.super. For example, you can invoke > Java.super(filter).fireMatchNode(). So the workaround for this case > would be add utility function outside for "setEnabled". This does sound like an acceptable work-around, however, it doesn't seem to work: TypeError: Java.super(filter).fireMatchNone is not a function I guess there is no way to call protected methods from JavaScript then? Well, I guess reflection will work. Kind regards, Axel. From sundararajan.athijegannathan at oracle.com Wed Oct 3 12:32:06 2018 From: sundararajan.athijegannathan at oracle.com (Sundararajan Athijegannathan) Date: Wed, 03 Oct 2018 18:02:06 +0530 Subject: Extending a Java class In-Reply-To: <2489072c-2889-fad9-7dfc-0ce0d5bbf4f9@pinc-software.de> References: <67ff4397-ab78-12c1-bde1-73b8af623b06@pinc-software.de> <5BADF5B1.3090806@oracle.com> <3236fc28-97fd-5a38-cc5d-ad5ec73203f2@pinc-software.de> <5BB18DB8.7060308@oracle.com> <2489072c-2889-fad9-7dfc-0ce0d5bbf4f9@pinc-software.de> Message-ID: <5BB4B6C6.60607@oracle.com> protected methods of super class are made public in the generated script subclass. Java.super should have worked. If you can submit a simpler test case, it may be possible to see what's happening.. Thanks -Sundar On 02/10/18, 3:52 PM, Axel D?rfler wrote: > Hi Sundar, > > Am 01/10/2018 um 05:00 schrieb Sundararajan Athijegannathan: >> In your example, it seems you're trying to add a new method to the >> subtype created in script. That is not supported. You can override >> existing methods of superclass - but not add a new method to the >> Java type. > > Bummer! resourcetrysuggester.js added new fields to a Java type, so I > thought it should work. > >> Note that all super methods (including protected ones) are available >> via Java.super. For example, you can invoke >> Java.super(filter).fireMatchNode(). So the workaround for this case >> would be add utility function outside for "setEnabled". > > This does sound like an acceptable work-around, however, it doesn't > seem to work: > TypeError: Java.super(filter).fireMatchNone is not a function > > I guess there is no way to call protected methods from JavaScript > then? Well, I guess reflection will work. > > Kind regards, > Axel. From pmartins at redhat.com Thu Oct 11 18:21:31 2018 From: pmartins at redhat.com (Paulo Lopes) Date: Thu, 11 Oct 2018 20:21:31 +0200 Subject: casting typed array to java byte[] is it possible? Message-ID: Hi, I'm trying to handle a case where a Uint8Array is being passed to a method, that has the signature: String encode(byte[]); Sadly nashorn fails with: java.lang.ClassCastException: Cannot cast jdk.nashorn.internal.objects.NativeUint8Array to [B And trying to help the cast with: javaObj.encode(Java.to(arr, 'byte[]')); Does not help either. The documentation on typed arrays is quite scarce so I cannot see how to handle this, does anyone have a clue? Thanks! Paulo From hannes.wallnoefer at oracle.com Fri Oct 12 08:10:27 2018 From: hannes.wallnoefer at oracle.com (=?utf-8?Q?Hannes_Walln=C3=B6fer?=) Date: Fri, 12 Oct 2018 10:10:27 +0200 Subject: casting typed array to java byte[] is it possible? In-Reply-To: References: Message-ID: <4AC4978C-2CC0-42AC-88C4-572607AF4832@oracle.com> Hi Paulo, Java.to() would be the way to go, but as you found out it does not support typed arrays. What works is to convert the typed array to an ordinary JS array and convert to byte[] from there: Java.to(Array.prototype.slice.call(arr), 'byte[]?); That?s obviously not very elegant nor efficient, but it?s the only workaround I can think of. Hannes > Am 11.10.2018 um 20:21 schrieb Paulo Lopes : > > Hi, > > I'm trying to handle a case where a Uint8Array is being passed to a > method, that has the signature: > > String encode(byte[]); > > Sadly nashorn fails with: > > java.lang.ClassCastException: Cannot cast > jdk.nashorn.internal.objects.NativeUint8Array to [B > > And trying to help the cast with: > > javaObj.encode(Java.to(arr, 'byte[]')); > > Does not help either. The documentation on typed arrays is quite scarce > so I cannot see how to handle this, does anyone have a clue? > > Thanks! > Paulo > From pmartins at redhat.com Fri Oct 12 11:34:56 2018 From: pmartins at redhat.com (Paulo Lopes) Date: Fri, 12 Oct 2018 13:34:56 +0200 Subject: casting typed array to java byte[] is it possible? In-Reply-To: <4AC4978C-2CC0-42AC-88C4-572607AF4832@oracle.com> Message-ID: Hi Hannes, Thanks for the tip, knowing that the convention isn't possible as is, are there any alternatives to get the JVM buffer that backs a typed array? I wanted to perform some work on a given buffer and then pass it to the JVM for further processing. Having to copy the buffer byte by byte will introduce a penalty that kind of defeats the whole purpose of using typed arrays to start with. Paulo Lopes Principal Software Engineer ? Original Message ? From: hannes.wallnoefer at oracle.com Sent: October 12, 2018 10:10 AM To: pmartins at redhat.com Cc: nashorn-dev at openjdk.java.net Subject: Re: casting typed array to java byte[] is it possible? Hi Paulo, Java.to() would be the way to go, but as you found out it does not support typed arrays. What works is to convert the typed array to an ordinary JS array and convert to byte[] from there: ? Java.to(Array.prototype.slice.call(arr), 'byte[]?); That?s obviously not very elegant nor efficient, but it?s the only workaround I can think of. Hannes > Am 11.10.2018 um 20:21 schrieb Paulo Lopes : > > Hi, > > I'm trying to handle a case where a Uint8Array is being passed to a > method, that has the signature: > > String encode(byte[]); > > Sadly nashorn fails with: > > java.lang.ClassCastException: Cannot cast > jdk.nashorn.internal.objects.NativeUint8Array to [B > > And trying to help the cast with: > > javaObj.encode(Java.to(arr, 'byte[]')); > > Does not help either. The documentation on typed arrays is quite scarce > so I cannot see how to handle this, does anyone have a clue? > > Thanks! > Paulo > From sundararajan.athijegannathan at oracle.com Fri Oct 12 12:04:45 2018 From: sundararajan.athijegannathan at oracle.com (Sundararajan Athijegannathan) Date: Fri, 12 Oct 2018 17:34:45 +0530 Subject: casting typed array to java byte[] is it possible? In-Reply-To: References: Message-ID: <5BC08DDD.7020309@oracle.com> Hi Paulo Lopes, ArrayBuffer can be created using a nio ByteBuffer. // you could create a ByteBuffer in Java & pass var bb = java.nio.ByteBuffer.allocateDirect(100) // create JS ArrayBuffer backed by Java ByteBuffer instance var ab = new ArrayBuffer(bb) var ia = new Int8Array(ab) ia[0] = 10 print(bb.get(0)) Hope this helps, -Sundar On 12/10/18, 5:04 PM, Paulo Lopes wrote: > Hi Hannes, > > Thanks for the tip, knowing that the convention isn't possible as is, are there any alternatives to get the JVM buffer that backs a typed array? > > I wanted to perform some work on a given buffer and then pass it to the JVM for further processing. Having to copy the buffer byte by byte will introduce a penalty that kind of defeats the whole purpose of using typed arrays to start with. > > > Paulo Lopes > Principal Software Engineer > > > Original Message > From: hannes.wallnoefer at oracle.com > Sent: October 12, 2018 10:10 AM > To: pmartins at redhat.com > Cc: nashorn-dev at openjdk.java.net > Subject: Re: casting typed array to java byte[] is it possible? > > Hi Paulo, > > Java.to() would be the way to go, but as you found out it does not support typed arrays. > > What works is to convert the typed array to an ordinary JS array and convert to byte[] from there: > > Java.to(Array.prototype.slice.call(arr), 'byte[]?); > > That?s obviously not very elegant nor efficient, but it?s the only workaround I can think of. > > Hannes > > >> Am 11.10.2018 um 20:21 schrieb Paulo Lopes: >> >> Hi, >> >> I'm trying to handle a case where a Uint8Array is being passed to a >> method, that has the signature: >> >> String encode(byte[]); >> >> Sadly nashorn fails with: >> >> java.lang.ClassCastException: Cannot cast >> jdk.nashorn.internal.objects.NativeUint8Array to [B >> >> And trying to help the cast with: >> >> javaObj.encode(Java.to(arr, 'byte[]')); >> >> Does not help either. The documentation on typed arrays is quite scarce >> so I cannot see how to handle this, does anyone have a clue? >> >> Thanks! >> Paulo >> From pmartins at redhat.com Fri Oct 12 13:34:59 2018 From: pmartins at redhat.com (Paulo Lopes) Date: Fri, 12 Oct 2018 15:34:59 +0200 Subject: casting typed array to java byte[] is it possible? In-Reply-To: <5BC08DDD.7020309@oracle.com> References: <5BC08DDD.7020309@oracle.com> Message-ID: <399d918b89513ef7da8cd17ddcedf05ea77d0d53.camel@redhat.com> Hi Sundar, Yes indeed that helps and it really gets close to my needs, I notice however, if I do that, then: var ia = new Int8Array(ab) ia.length will never have a valid value, it will be zero regardless of the underlying ArrayBuffer, since the property is read only I cannot assign it myself (since I know the size at construction time) either. Trying to specify the size: var bb = java.nio.ByteBuffer.allocateDirect(12) var ab = new ArrayBuffer(bb) var ia = new Int8Array(ab, 0, 12) it's all 8 bit ints so (bytes) fails with: java.lang.RuntimeException: byteOffset + byteLength out of range, byteOffset=0, elementLength=12, bytesPerElement=1 Is this expected? Or can I specify the length somewhere? Also ab.byteLength prints 0 instead of 12... On Fri, 2018-10-12 at 17:34 +0530, Sundararajan Athijegannathan wrote: > Hi Paulo Lopes, > > ArrayBuffer can be created using a nio ByteBuffer. > > // you could create a ByteBuffer in Java & pass > var bb = java.nio.ByteBuffer.allocateDirect(100) > > // create JS ArrayBuffer backed by Java ByteBuffer instance > var ab = new ArrayBuffer(bb) > > var ia = new Int8Array(ab) > ia[0] = 10 > print(bb.get(0)) > > Hope this helps, > -Sundar > > On 12/10/18, 5:04 PM, Paulo Lopes wrote: > > Hi Hannes, > > > > Thanks for the tip, knowing that the convention isn't possible as > > is, are there any alternatives to get the JVM buffer that backs a > > typed array? > > > > I wanted to perform some work on a given buffer and then pass it to > > the JVM for further processing. Having to copy the buffer byte by > > byte will introduce a penalty that kind of defeats the whole > > purpose of using typed arrays to start with. > > > > > > Paulo Lopes > > Principal Software Engineer > > > > > > Original Message > > From: hannes.wallnoefer at oracle.com > > Sent: October 12, 2018 10:10 AM > > To: pmartins at redhat.com > > Cc: nashorn-dev at openjdk.java.net > > Subject: Re: casting typed array to java byte[] is it possible? > > > > Hi Paulo, > > > > Java.to() would be the way to go, but as you found out it does not > > support typed arrays. > > > > What works is to convert the typed array to an ordinary JS array > > and convert to byte[] from there: > > > > Java.to(Array.prototype.slice.call(arr), 'byte[]?); > > > > That?s obviously not very elegant nor efficient, but it?s the only > > workaround I can think of. > > > > Hannes > > > > > > > Am 11.10.2018 um 20:21 schrieb Paulo Lopes: > > > > > > Hi, > > > > > > I'm trying to handle a case where a Uint8Array is being passed to > > > a > > > method, that has the signature: > > > > > > String encode(byte[]); > > > > > > Sadly nashorn fails with: > > > > > > java.lang.ClassCastException: Cannot cast > > > jdk.nashorn.internal.objects.NativeUint8Array to [B > > > > > > And trying to help the cast with: > > > > > > javaObj.encode(Java.to(arr, 'byte[]')); > > > > > > Does not help either. The documentation on typed arrays is quite > > > scarce > > > so I cannot see how to handle this, does anyone have a clue? > > > > > > Thanks! > > > Paulo > > > From pmartins at redhat.com Fri Oct 12 13:59:17 2018 From: pmartins at redhat.com (Paulo Lopes) Date: Fri, 12 Oct 2018 15:59:17 +0200 Subject: casting typed array to java byte[] is it possible? In-Reply-To: <399d918b89513ef7da8cd17ddcedf05ea77d0d53.camel@redhat.com> Message-ID: <8be640fc15d6ad087ff794f1f764e7a017dcebeb.camel@redhat.com> Interesting enough, with Java >=9 the length is reported correctly, so in the case of: var bb = java.nio.ByteBuffer.allocateDirect(12) var ab = new ArrayBuffer(bb) var ia = new Int8Array(ab) ia.length // prints 12 ab.byteLength // prints 12 This fails now on JDK8 and Graal.js (is Graal.js supposed to allow this? (setting a ByteBuffer as the underlying buffer of a js ArrayBuffer?) I can imagine that for embedded applications this could make sense... From sundararajan.athijegannathan at oracle.com Mon Oct 15 04:40:47 2018 From: sundararajan.athijegannathan at oracle.com (Sundararajan Athijegannathan) Date: Mon, 15 Oct 2018 10:10:47 +0530 Subject: casting typed array to java byte[] is it possible? In-Reply-To: <8be640fc15d6ad087ff794f1f764e7a017dcebeb.camel@redhat.com> References: <8be640fc15d6ad087ff794f1f764e7a017dcebeb.camel@redhat.com> Message-ID: <5BC41A4F.3090200@oracle.com> Hi, Yes, I reproduced the issue with jdk8 (haven't tried graal.js though). Please file bug(s). -Sundar On 12/10/18, 7:29 PM, Paulo Lopes wrote: > Interesting enough, with Java>=9 the length is reported correctly, so > in the case of: > > var bb = java.nio.ByteBuffer.allocateDirect(12) > var ab = new ArrayBuffer(bb) > var ia = new Int8Array(ab) > > ia.length // prints 12 > ab.byteLength // prints 12 > > This fails now on JDK8 and Graal.js (is Graal.js supposed to allow > this? (setting a ByteBuffer as the underlying buffer of a js > ArrayBuffer?) > > I can imagine that for embedded applications this could make sense... >