Skip to content

Commit

Permalink
Merge pull request #156 from jonas/bindings
Browse files Browse the repository at this point in the history
Add initial bindings subproject
  • Loading branch information
jonas authored Aug 14, 2018
2 parents fc815fe + 380d4f7 commit 22355ac
Show file tree
Hide file tree
Showing 22 changed files with 716 additions and 35 deletions.
3 changes: 2 additions & 1 deletion .scalafmt.conf
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ style = defaultWithAlign
docstrings = JavaDoc
assumeStandardLibraryStripMargin = true
project.excludeFilters = [
"tests/samples/[^/]*[.]scala"
"bindings/[^/]+/src/main/.*[.]scala"
"docs/src/test/scala/org/example/.*[.]scala"
"sbt-scala-native-bindgen/.*/expected/.*[.]scala"
"tests/samples/[^/]*[.]scala"
]
project.git = true
runner.dialect = Scala211
16 changes: 16 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@ RUN set -x \
&& apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 2EE0EA64E40A89B84B2DF73499E82A75642AC823 \
&& apt update \
&& apt install -y --no-install-recommends \
locales locales-all \
g++ openjdk-8-jdk-headless sbt cmake make curl git \
zlib1g-dev \
libgc-dev libunwind8-dev libre2-dev \
nlohmann-json-dev \
&& rm -rf /var/lib/apt/lists/*

ARG LOCALE=en_US.UTF-8
ENV LANG=$LOCALE
ENV LANGUAGE=$LOCALE
ENV LC_ALL=$LOCALE

ARG LLVM_VERSION=6.0
ENV LLVM_VERSION=$LLVM_VERSION
RUN set -x \
Expand All @@ -26,4 +32,14 @@ RUN set -x \
&& rm -rf /var/lib/apt/lists/*

ENV PATH=$PATH:/usr/lib/llvm-$LLVM_VERSION/bin

##bindings-dev-package
# Install binding dependencies
RUN set -x \
&& apt update \
&& apt install -y --no-install-recommends \
libutf8proc-dev \
&& rm -rf /var/lib/apt/lists/*
##bindings-dev-package

WORKDIR /src
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.scalanative.bindgen.bindings.iconv

import scala.scalanative._
import scala.scalanative.native._

@native.extern
object iconv {
type iconv_t = native.Ptr[Byte]
def iconv_open(__tocode: native.CString, __fromcode: native.CString): native.Ptr[Byte] = native.extern
def iconv(__cd: native.Ptr[Byte], __inbuf: native.Ptr[native.CString], __inbytesleft: native.Ptr[native.CSize], __outbuf: native.Ptr[native.CString], __outbytesleft: native.Ptr[native.CSize]): native.CSize = native.extern
def iconv_close(__cd: native.Ptr[Byte]): native.CInt = native.extern

object defines {
val _ICONV_H: native.CInt = 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package org.scalanative.bindgen.bindings.iconv.tests

import org.scalatest.FunSpec

class IconvSpec extends FunSpec {
describe("iconv") {
it("should convert back and forth between UTF-8 and ISO-8859-1") {
//#usage-example
import org.scalanative.bindgen.bindings.iconv.iconv._
import scala.scalanative.native._
import java.nio.charset.Charset

val UTF8 = Charset.forName("UTF-8")
val encode = iconv_open(c"UTF-8", c"ISO-8859-1")
val decode = iconv_open(c"ISO-8859-1", c"UTF-8")

Zone { implicit zone =>
val originalBuf = toCString("øre", UTF8) // Ear in Danish
val originalBufPtr = alloc[CString]
!originalBufPtr = originalBuf
val originalBytesLeft = alloc[CSize]
!originalBytesLeft = string.strlen(originalBuf)
//#usage-example
assert(!originalBytesLeft == 4)
//#usage-example

val translatedBuf = alloc[Byte](32)
val translatedBufPtr = alloc[CString]
!translatedBufPtr = translatedBuf
val translatedBytesLeft = alloc[CSize]
!translatedBytesLeft = 32

val translatedCode = iconv(
encode,
originalBufPtr,
originalBytesLeft,
translatedBufPtr,
translatedBytesLeft
)
//#usage-example

assert(translatedCode == 0)

!translatedBufPtr = translatedBuf
!translatedBytesLeft = string.strlen(translatedBuf)

val roundtripBuf = alloc[Byte](32)
val roundtripBufPtr = alloc[CString]
!roundtripBufPtr = roundtripBuf
val roundtripBytesLeft = alloc[CSize]
!roundtripBytesLeft = 32

val roundtripCode = iconv(
decode,
translatedBufPtr,
translatedBytesLeft,
roundtripBufPtr,
roundtripBytesLeft
)

assert(string.strcmp(originalBuf, roundtripBuf) == 0)
//#usage-example
}
//#usage-example

//#usage-example
iconv_close(encode)
//#usage-example
iconv_close(decode)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.scalanative.bindgen.bindings.posix

import scala.scalanative._
import scala.scalanative.native._

@native.extern
object fnmatch {
def fnmatch(__pattern: native.CString, __name: native.CString, __flags: native.CInt): native.CInt = native.extern

object defines {
val _FNMATCH_H: native.CInt = 1
val FNM_NOMATCH: native.CInt = 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.scalanative.bindgen.bindings.posix

import scala.scalanative._
import scala.scalanative.native._

@native.extern
object regex {
type enum_reg_errcode_t = native.CUnsignedInt
object enum_reg_errcode_t {
final val REG_NOERROR: enum_reg_errcode_t = 0.toUInt
final val REG_NOMATCH: enum_reg_errcode_t = 1.toUInt
final val REG_BADPAT: enum_reg_errcode_t = 2.toUInt
final val REG_ECOLLATE: enum_reg_errcode_t = 3.toUInt
final val REG_ECTYPE: enum_reg_errcode_t = 4.toUInt
final val REG_EESCAPE: enum_reg_errcode_t = 5.toUInt
final val REG_ESUBREG: enum_reg_errcode_t = 6.toUInt
final val REG_EBRACK: enum_reg_errcode_t = 7.toUInt
final val REG_EPAREN: enum_reg_errcode_t = 8.toUInt
final val REG_EBRACE: enum_reg_errcode_t = 9.toUInt
final val REG_BADBR: enum_reg_errcode_t = 10.toUInt
final val REG_ERANGE: enum_reg_errcode_t = 11.toUInt
final val REG_ESPACE: enum_reg_errcode_t = 12.toUInt
final val REG_BADRPT: enum_reg_errcode_t = 13.toUInt
final val REG_EEND: enum_reg_errcode_t = 14.toUInt
final val REG_ESIZE: enum_reg_errcode_t = 15.toUInt
final val REG_ERPAREN: enum_reg_errcode_t = 16.toUInt
}

type s_reg_t = native.CLong
type active_reg_t = native.CUnsignedLong
type reg_syntax_t = native.CUnsignedLong
type reg_errcode_t = enum_reg_errcode_t
type struct_re_pattern_buffer = native.CArray[Byte, native.Nat.Digit[native.Nat._6, native.Nat._4]]
type regex_t = struct_re_pattern_buffer
type regoff_t = native.CInt
type struct_regmatch_t = native.CStruct2[regoff_t, regoff_t]
type regmatch_t = struct_regmatch_t
val re_syntax_options: reg_syntax_t = native.extern
def regcomp(__preg: native.Ptr[regex_t], __pattern: native.CString, __cflags: native.CInt): native.CInt = native.extern
def regexec(__preg: native.Ptr[regex_t], __string: native.CString, __nmatch: native.CSize, __pmatch: native.Ptr[regmatch_t], __eflags: native.CInt): native.CInt = native.extern
def regerror(__errcode: native.CInt, __preg: native.Ptr[regex_t], __errbuf: native.CString, __errbuf_size: native.CSize): native.CSize = native.extern
def regfree(__preg: native.Ptr[regex_t]): Unit = native.extern

object defines {
val _REGEX_H: native.CInt = 1
val REG_EXTENDED: native.CInt = 1
val REG_NOTBOL: native.CInt = 1
}

object implicits {
implicit class struct_regmatch_t_ops(val p: native.Ptr[struct_regmatch_t]) extends AnyVal {
def rm_so: regoff_t = !p._1
def rm_so_=(value: regoff_t): Unit = !p._1 = value
def rm_eo: regoff_t = !p._2
def rm_eo_=(value: regoff_t): Unit = !p._2 = value
}
def struct_regmatch_t()(implicit z: native.Zone): native.Ptr[struct_regmatch_t] = native.alloc[struct_regmatch_t]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.scalanative.bindgen.bindings.tests

import org.scalatest.FunSpec

class FnmatchSpec extends FunSpec {
describe("fnmatch") {
it("should match patterns") {
//#usage-example
import scala.scalanative.native._
import org.scalanative.bindgen.bindings.posix.fnmatch._

assert(fnmatch(c"*.md", c"README.md", 0) == 0)
assert(fnmatch(c"*.[ch]", c"main.h", 0) == 0)
assert(fnmatch(c"*.[ch]", c"main.c", 0) == 0)
assert(fnmatch(c"*.md", c"README_md", 0) == defines.FNM_NOMATCH)
//#usage-example
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.scalanative.bindgen.bindings.tests

import org.scalatest.FunSpec

class RegexSpec extends FunSpec {
describe("regex") {
it("should match regular expressions") {
//#usage-example
import scala.scalanative.native._
import org.scalanative.bindgen.bindings.posix.regex._

val reg = stackalloc[regex_t]

val compResult =
regcomp(reg, c"Scala \\(\\(J\\(VM\\|S\\)\\)\\|Native\\)", 0)
assert(compResult == 0)

assert(regexec(reg, c"Scala JVM", 0, null, 0) == 0)
assert(regexec(reg, c"Scala JS", 0, null, 0) == 0)
assert(regexec(reg, c"Scala Native", 0, null, 0) == 0)
assert(regexec(reg, c"Scala .NET", 0, null, 0) != 0)

regfree(reg)
//#usage-example
}
}
}
Loading

0 comments on commit 22355ac

Please sign in to comment.