Thursday, February 26, 2015

Nim and Java and C++


This single markdown file, nim_java.md mixes: Nim, Java, and C++. The output is a single executeable that includes the Nim code translated to C, the Nim C runtime, Rusthon code translated to C++, and links to the JVM. This allows you to call Nim and Java functions from Rusthon, and manage passing data between them. For how to load a JAR library, see here.

#backend:c++

import nim
import jvm
jvm.namespace('mymod')

@jvm
class PointSubclass( Point ):
 def __init__(self, x:int, y:int):
  self.set(x,y)

 def show(self):
  print self.getx()
  print self.gety()


def main():
 nim.main()
 p1 = jvm( PointSubclass(1,2) )
 p2 = jvm( PointSubclass(10,20) )
 p1.show()
 p2.show()
 p2.scale( nim_sum(100, 1000) )
 p2.show()

Mixing Nim, Java and C++ in a single binary would normally require some complex build, and running a wrapper generator like Nimrod-Java. But Nimrod-Java has not been updated in a year, and calling JNI through Nim is going to complicate threading.

No comments:

Post a Comment