0

I'm struggling to understand how "native method" calls in DukeScript work. In particular, the ones where no body is specified in the @JavascriptBody annotation. For example:

@JavaScriptResource(value = "userEntryComponent.js")
public final class UserEntryWidget {

    private UserEntryWidget() {
    }

    @JavaScriptBody(args = {}, body = "")
    public static native void registerComponent();
}

Where is the "registerComponent()" method defined? In knockout there's a javascript function called "ko.components.register". So "registerComponent" must be a sort of wrapper around "ko.components.register".

Another example of a native method call without body is here:

@JavaScriptResource("jquery-1.11.0.min.js")
public class JQuery {

    @JavaScriptBody(args = {},body="")
    public static native void init();   
}

So, in this case, what's "init()"? is it a Java method or a JavaScript function?

ZiglioUK
  • 2,573
  • 4
  • 27
  • 32
  • I haven't picked a good example here, as the @JavaScriptResource case is very particular in which the method name in it doesn't really matter, it just triggers a load for that resource (See Jiri's answer below). – ZiglioUK Jun 14 '15 at 09:49
  • The generic case of providing an implementation for JavaScript native method is described here: http://bits.netbeans.org/html+java/dev/net/java/html/js/package-summary.html#post-process – ZiglioUK Jun 14 '15 at 09:49

1 Answers1

1

I fully understand why the code looks magical. However if you try to comment out the init method, you should see error during javac complication:

COMPILATION ERROR : 
-------------------------------------------------------------
JQuery.java:[10,8] At least one method needs @JavaScriptBody
annotation. Otherwise it is not guaranteed the resource will
ever be loaded

The error line is the line with @JavaScriptResource usage. The init method definition is really empty and does nothing. But once called, it enforces load of resource defined in @JavaScriptResource.

In the knockout case, the ko.components.register is defined by the knockout.js resource file. The method name registerComponent can be arbitrary, it is there to just trigger the load of the knockout.js resource.

Jaroslav Tulach
  • 519
  • 4
  • 7
  • Anyway, this case is quite specific, and it doesn't cover the generic case in which a body is specified. The mystery is how native calls are evaluated by DukeScript at runtime. I keep getting UnsatisfiedLinkErrors (in Eclipse) and that hasn't to do with generated code as there is no generated code for native javascript calls. Is asm-5.0.jar being used? – ZiglioUK Jun 14 '15 at 08:49
  • I see that's the complicated bit that sometimes works and sometimes doesn't: http://bits.netbeans.org/html+java/dev/net/java/html/js/package-summary.html#post-process – ZiglioUK Jun 14 '15 at 09:44
  • I've just added asm.jar as a dependency and that helped. Not sure why the classes are not processed at compile time. Does the class scanning and class postprocessing happen during different Maven phases? perhaps I need to specify that in the pom. This problem occurs for me only when running the plugin using the maven-processor-pugin, which is Eclipse friendly. – ZiglioUK Jun 14 '15 at 23:22