Seeking some light on how to deal with Javascript asynchronous events (via callbacks/promises)

74 views
Skip to first unread message

Michel

unread,
Aug 30, 2015, 11:19:28 AM8/30/15
to DukeScript
Hey,

I am back on my PouchDB-based project and so far so good with my first tests playing with the great introduction given by Tony.

Now I need to delve a bit further "dukescripting" the PouchDB Javascript API, which, as you can see, has async events, and I am stuck on the implementation of these callbacks/promises.

Could anyone give me a light / pointer to an example to get me started?

Michel

unread,
Aug 30, 2015, 12:08:21 PM8/30/15
to DukeScript
An example of what I'd like to implement is the listener to an asynchronous database change event (as you can see, it has 3 callbacks):

var changes = db.changes({
  since: 'now',
  live: true,
  include_docs: true
}).on('change', function(change) {
  // handle change
}).on('complete', function(info) {
  // changes() was canceled
}).on('error', function (err) {
  console.log(err);
});

Michel

unread,
Aug 30, 2015, 1:32:15 PM8/30/15
to DukeScript
In an online presentation I found that it is possible to call Java from JavaScript that way:

Identify method with a FQN and type signature
- r.@java.lang.Runnable::run()()
- @java.lang.String::valueOf(I)(10)

But this is very cryptic to me. Do we have a nice example of such calls somewhere, or better, does anyone have any time to show me the way specifically taylored to my example of database change callbacks I posted previously?

Jaroslav Tulach

unread,
Aug 30, 2015, 3:27:31 PM8/30/15
to Michel, DukeScript

--
You received this message because you are subscribed to the Google Groups "DukeScript" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dukescript+...@googlegroups.com.
Visit this group at http://groups.google.com/group/dukescript.
To view this discussion on the web visit https://groups.google.com/d/msgid/dukescript/84133891-df62-425d-b7b5-044d9a252dba%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Michel Schrameck

unread,
Sep 1, 2015, 5:37:35 AM9/1/15
to Jaroslav Tulach, DukeScript
What's still difficult to understand is the function call signature syntax (highlighted below) as the documentation does not really give much details... Where can I learn more about that?

------------------------------

Callback to Java

Often JavaScript code needs to call back into the Java classes. For example when a button in a browser is pressed and your code would like to invoke a runnable to handle such situation:
 
@JavaScriptBody(args = {"id", "r"}, javacall = true, body = "\n" + 
"       document.getElementById(id).onclick = function() {\n" + 
"        r.@java.lang.Runnable::run()();\n" + 
"       };\n" + 
"    ")
public static native void onClick(String id, Runnable r);
As can be seen, there is a special syntax (starting with @) to properly identify the right Java method to call on a Java object passed into the JavaScript interpreter. The syntax starts with a fully qualified name of the class, followed by :: and name of the method including signature of its parameters. In case of runnable, this is just () as the method has no parameters, but the signature can be more complicated. For example in case of following method
static int compare(int i1, String s1, int i2, String s2)
it would be (ILjava/lang/String;ILjava/lang/String;) (btw. the return type is not included in the signature). The actual parameters then follows. The JavaScript call to such compare method would then look like:
@the.pkg.Clazz::compare(ILjava/lang/String;ILjava/lang/String;)(1, 'One', 2, 'Two');
This syntax gives enough flexibility, helps to properly select one of overloaded methods and follows the tradition of previous attempts to provide JavaScript to Java calling conventions.

Please note that to turn the special Java callback syntax on, one needs to set the JavaScriptBody.javacall() attribute to true.

Editing hint: there is an associated annotation processor that checks the syntax and verifies the referenced class and method of the right signature exist. If they do not, the compilation fails. Thus don't despair seeing the syntax, you'll get early warnings when there is a typo.

Jaroslav Tulach

unread,
Sep 1, 2015, 6:08:25 AM9/1/15
to Michel Schrameck, DukeScript
I see, Michel.
Let's look for some references and once we find one that is descriptive enough, I can modify the documentation to link to it. Here are things I have found on the Internet:

http://stackoverflow.com/questions/9909228/what-does-v-mean-in-a-class-signature
https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.3.2
http://stackoverflow.com/questions/5085889/l-array-notation-where-does-it-come-from

Btw. the easiest way is to type some signature in and compile - the error message will give you good enough advice - it will list all available signatures, and you can choose the right one.
-jt

Emanuele Ziglioli

unread,
Sep 2, 2015, 5:32:27 PM9/2/15
to DukeScript, jarosla...@gmail.com
Hi Michael,

I find calls from JavaScript into Java the most intricate part of DukeScript.
I've got an example of a callback that waits for the Firebug console to be loaded before it sets stdout and stderr pipes to Firebug:
 
I hope it helps, I was quite proud when I managed :-)

Anton Epple

unread,
Sep 15, 2015, 2:05:32 AM9/15/15
to Emanuele Ziglioli, DukeScript, jarosla...@gmail.com
Hi Michael,

you are right that it’s not easy to understand and probably underdocumented. The calls have the following form:

[instance-expr.]@class-name::method-name(param-signature)(arguments)

whereby 

instance-expr. : must be present when calling an instance method and must be absent when calling a static method
class-name : the fully-qualified name of the class in which the method is declared (or a subclass thereof)
param-signature : the internal Java method signature as specified at JNI Type Signatures but without the trailing signature of the method return type since it is not needed to choose the overload
arguments : is the actual argument list to pass to the called method

The only difficult part is the param-signature. It follows this pattern:

Type Signature
Java Type
Z
boolean
B
byte
C
char
S
short
I
int
J
long
F
float
D
double
L fully-qualified-class ;
fully-qualified-class
[ type
type[]
( arg-types ) ret-type
method type

For example, the Java method:

long f (int n, String s, int[] arr); 

has the following type signature:

(ILjava/lang/String;[I)J 

hope that helps

—Toni



Michel Schrameck

unread,
Sep 15, 2015, 8:13:44 AM9/15/15
to Anton Epple, Emanuele Ziglioli, DukeScript, Jaroslav Tulach
Thanks Anton, I really appreciate the time you're spending in order to make sure I understood this matter. I guess I got it now - it's more a notation rule rather than a complexity.

Now, another thing is bothering me: does DukeScript apps support multithreading? For example, can I implement as many threads, timers and events as a normal Java app would allow? If not, what are the differences?


--
You received this message because you are subscribed to a topic in the Google Groups "DukeScript" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/dukescript/fn9ECjNk_gQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to dukescript+...@googlegroups.com.

Jaroslav Tulach

unread,
Aug 13, 2016, 6:35:22 AM8/13/16
to Michel Schrameck, Anton Epple, Emanuele Ziglioli, DukeScript

To unsubscribe from this group and stop receiving emails from it, send an email to dukescript+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to a topic in the Google Groups "DukeScript" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/dukescript/fn9ECjNk_gQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to dukescript+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages