swog - ScalaNative Wrapper Object Generator

swog provides seamless integration of Scala Native with external, object-oriented libaries written in C, C++, or Objective-C, as well as integration with embedded scripting languages (currently: Lua). To this end it takes a plain Scala class/object and transforms it into a wrapper object that handles the interop with the underlying external object under the hood.

Note

This guide is work in progress.

Example for C

Suppose we have the following C struct + functions to operate on fractional numbers:

typedef struct {
  // numerator
  int num;
  // denominator
  int denom;
} Fraction;

/* constructor */
Fraction* fraction_new(int num, int denom) { /* ... */ }

/* property accessors */
int fraction_get_numerator(Fraction* this) { return this->num; }
void fraction_set_numerator(Fraction* this, int num) { this->num = num; }
// ...

/* multiply with another fraction */
void fraction_multiply(Fraction* this, Fraction* that) { /* ... */ }

// use it:
Fraction* f = fraction_new(2,3);
Fraction* g = fraction_new(6,5);
fraction_multiply_with(f,g);

Using swog we can use this C type from Scala simply by declaring it as a class annotated with @CObj:

import scala.scalanative._
import unsafe._
import cobj._

@CObj
class Fraction {
  def getNumerator(): Int = extern
  def setNumerator(num: Int): Unit = extern

  def getDenominator(): Int = extern
  def setDenominator(denom: Int): Int = extern

  def multiply(that: Fraction): Unit = extern
}
object Fraction {
  // bind to the 'constructor'
  @name("fraction_new")
  def apply(numerator: Int, denominator: Int): Fraction = extern
}

// use it:
val f = Fraction(2,3)
val g = Fraction(6,5)
f.multiply(g)

f.getNumerator() // returns 4

Examples: