-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate extern variable declarations
Due to scala-native/scala-native#202 it is not possible to use `var` so for now `val`s are used although not semantically correct. References #70
- Loading branch information
Showing
9 changed files
with
98 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#include "Extern.h" | ||
|
||
int forty_two = 42; | ||
const char version[] = "0.1.0"; | ||
|
||
enum mode mode = USER; | ||
|
||
struct version semver_instance = { .major = 1, .minor = 2, .patch = 3 }; | ||
struct version *semver = &semver_instance; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
extern int forty_two; | ||
extern const char version[]; | ||
|
||
enum mode { SYSTEM, USER }; | ||
extern enum mode mode; | ||
|
||
struct version { | ||
int major; | ||
int minor; | ||
int patch; | ||
}; | ||
extern struct version *semver; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package org.scalanative.bindgen.samples | ||
|
||
import scala.scalanative._ | ||
import scala.scalanative.native._ | ||
|
||
@native.link("bindgentests") | ||
@native.extern | ||
object Extern { | ||
type enum_mode = native.CUnsignedInt | ||
type struct_version = native.CStruct3[native.CInt, native.CInt, native.CInt] | ||
val forty_two: native.CInt = native.extern | ||
val version: native.CString = native.extern | ||
val mode: enum_mode = native.extern | ||
val semver: native.Ptr[struct_version] = native.extern | ||
} | ||
|
||
import Extern._ | ||
|
||
object ExternEnums { | ||
final val enum_mode_SYSTEM: enum_mode = 0.toUInt | ||
final val enum_mode_USER: enum_mode = 1.toUInt | ||
} | ||
|
||
object ExternHelpers { | ||
|
||
implicit class struct_version_ops(val p: native.Ptr[struct_version]) extends AnyVal { | ||
def major: native.CInt = !p._1 | ||
def major_=(value: native.CInt):Unit = !p._1 = value | ||
def minor: native.CInt = !p._2 | ||
def minor_=(value: native.CInt):Unit = !p._2 = value | ||
def patch: native.CInt = !p._3 | ||
def patch_=(value: native.CInt):Unit = !p._3 = value | ||
} | ||
|
||
def struct_version()(implicit z: native.Zone): native.Ptr[struct_version] = native.alloc[struct_version] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
tests/samples/src/test/scala/org/scalanative/bindgen/samples/ExternTests.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package org.scalanative.bindgen.samples | ||
|
||
import utest._ | ||
import scalanative.native._ | ||
|
||
object ExternTests extends TestSuite { | ||
val tests = Tests { | ||
'forty_two - { | ||
assert(Extern.forty_two == 42) | ||
} | ||
|
||
'mode - { | ||
assert(Extern.mode == ExternEnums.enum_mode_USER) | ||
} | ||
|
||
'semver - { | ||
import ExternHelpers._ | ||
assert(Extern.semver.major == 1 && Extern.semver.minor == 2 && Extern.semver.patch == 3) | ||
} | ||
} | ||
} |