#2439 Is it possible to write sys lib in pure Fantom

go4 Sun 16 Aug 2015

It is difficult to add other Fantom runtimes backend, because the entire sys pod have to write in native code.

I would like most of sys class written in pure Fantom.

brian Mon 17 Aug 2015

Its definitely possible, although at some point you have to drop down to native code to bind to I/O, etc. I would say about 60% of sys could be written in Fantom, but it would be a big porting effort and we already have the Java code so super optimized. But if working on a new backend, I'd probably evaluate doing it fresh in Fantom

go4 Tue 18 Aug 2015

Thanks for reply. If sys written in Fantom, how to deal with the generics type (List Map Func) in Fantom code.

SlimerDude Tue 18 Aug 2015

Hi Go4, as Briain said, only about 60% of sys would be re-written in Fantom - and I suspect generic types would not be part of that 60!

Brain / Andy, something that might help, and something I've thought about before when writing native Javascript, is optional native method implementations.

The idea is that you provide a Fantom implementation for a method, which may be (optionally) overridden with a native implementation.

Something like this:

class Wotever {
    native Str foo() { "bar" }
}

This helps with the sys re-implementation because a great deal more could be written in Fantom for general portability, all while retaining the super optimised Java implementations.

brian Tue 18 Aug 2015

The idea is that you provide a Fantom implementation for a method, which may be (optionally) overridden with a native implementation.

You can override a method a native implementation:

override native Void foo()

At least I'm pretty sure you can

SlimerDude Fri 18 Sep 2015

Hi Brian,

I was just trying this out, not sure how to make it work:

class Example {
    native Str forceNativePeer

    virtual Void doThatThing() {
        echo("Fantom World")
    }
	
    static Void main() {
        Example().doThatThing()
    }
}

with

package fan.xxx;

import fan.sys.*;

public class ExamplePeer {
    public Example  self;
    public String   forceNativePeer;

    public static ExamplePeer make(Example self) throws Exception {
        ExamplePeer peer = new ExamplePeer();
        self.peer = peer;
        peer.self = self;
        return peer;
    }	
	
    public void doThatThing() {
        self.echo("Java World");
    }
}
C:\>fan xxx::Example
Fantom World

Where as what I'm after, is for Java World to be displayed.

That way, if there were no ExamplePeer the Fantom code would execute. But if ExamplePeer exists, as in above, then I get to override Fantom behaviour with native code.

Large parts of the sys pod could then be written in Fantom to be reused by other runtimes, whilst the existing Java and Javascript runtimes still get to use their existing native implementations.

brian Fri 18 Sep 2015

I was just trying this out, not sure how to make it work:

It will not work. There are two types of natives: pure native classes where the entire class is written in Java (concurrent classes work this way where native keyword is on the class level). Then normal Fantom classes where some subset of the methods are native (for example FWT works like this). The latter is more expensive because it requires a peer field and often a peer instance. So we don't use that model for sys. Although there is a lot of other issues too since sys is such a special bootstrap case.

Login or Signup to reply.