OxL Reference

src/native/native.oxl

oxl
@nonblocking
import c "math.h" { sqrt, pow }

@nonblocking
import c "stdlib.h" { qsort, abs }

/// Safe wrapper: the hypotenuse of a right triangle through libm.
pub fn hypot(a: f64, b: f64) -> f64  pure {
  unsafe { sqrt(pow(a, 2.0) + pow(b, 2.0)) }
}

/// Absolute value through libc.
pub fn absolute(x: i32) -> i32  pure {
  unsafe { abs(x) }
}

// export c fn: callable from C with the C ABI; here it is the comparator of qsort.
export c fn compareInts(a: *u8, b: *u8) -> i32 {
  unsafe {
    let x = a.cast<i32>().read()
    let y = b.cast<i32>().read()
    if x < y { -1 } else if x > y { 1 } else { 0 }
  }
}

/// Sorts `xs` in place with libc's qsort.
pub fn sortInts(xs: &var List<i32>) {
  unsafe { qsort(xs.asPtr().cast<u8>(), xs.len(), 4, compareInts) }
}

Source: examples/ffi-demo/src/native/native.oxl