Skip to content

Commit

Permalink
Upgrade to Scala Native 0.5
Browse files Browse the repository at this point in the history
Summary:

Test Plan:
  • Loading branch information
quelgar committed Apr 14, 2024
1 parent 34e2e9d commit f1152c2
Show file tree
Hide file tree
Showing 16 changed files with 190 additions and 371 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ object Main {

def main(args: Array[String]): Unit = {

withZone {
Zone {
val loop = uv_default_loop()

val asyncHandle = AsyncHandle.zoneAllocate()
Expand Down Expand Up @@ -82,9 +82,9 @@ scala-uv tries to expose the exact C API of libuv as directly as possible. Howev

Most of the libuv functions can be found in the `LibUv` object, with the same name as the C function. The exceptions are the following:

* `uv_loop_configuration`currently not supported
* `uv_fileno`currently not supported
* `uv_poll_init_socket`currently not supported
* `uv_loop_configuration` — not yet supported
* `uv_fileno` — not yet supported
* `uv_poll_init_socket` — not yet supported
* Process handle functions — not yet supported
* `uv_socketpair` — not yet supported
* `uv_udp_open` — not yet supported
Expand Down Expand Up @@ -233,7 +233,7 @@ def onNewConnection: ConnectionCallback = {

The libuv file I/O functions support use of multiple buffers at once. scala-uv provides the `IOVector` type for working with multiple buffers with varying amoutns of data.

scala-uv provides a shortcut for allocating, using and freeing `FileReq` objects, *if you are doing blocking I/O*: `FileReq.use`.
scala-uv provides a shortcut for allocating, using and freeing `FileReq` objects, if you are doing *blocking* I/O: `FileReq.use`.

```scala
// writes the C string pointed to by `cText` to a file
Expand Down
5 changes: 3 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
scalaVersion := "3.3.1"
scalaVersion := "3.3.3"

enablePlugins(ScalaNativePlugin)
enablePlugins(ScalaNativeJUnitPlugin)
Expand All @@ -7,7 +7,7 @@ organization := "io.github.quelgar"

name := "scala-uv"

version := "0.0.3-SNAPSHOT"
version := "0.1.0-SNAPSHOT"

ThisBuild / versionScheme := Some("early-semver")

Expand All @@ -22,6 +22,7 @@ nativeConfig ~= { c =>
c.withLTO(LTO.none) // thin
.withMode(Mode.debug) // releaseFast
.withGC(GC.immix) // commix
.withMultithreading(false)
}

scalacOptions ++= Seq(
Expand Down
2 changes: 1 addition & 1 deletion project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
addSbtPlugin("org.scala-native" % "sbt-scala-native" % "0.4.17")
addSbtPlugin("org.scala-native" % "sbt-scala-native" % "0.5.0")
addSbtPlugin("com.github.sbt" % "sbt-pgp" % "2.2.1")
addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "3.10.0")
57 changes: 0 additions & 57 deletions src/main/resources/scala-native/helpers.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
#include <uv.h>
#include <string.h>

#ifdef _WIN32
#include <winsock.h>
#else
#include <netinet/in.h>
#endif

uv_loop_t *scala_uv_fs_req_get_loop(const uv_fs_t *req)
{
return req->loop;
Expand Down Expand Up @@ -69,32 +63,6 @@ uv_stream_t *scala_uv_send_stream_handle(const uv_write_t *req)
return req->send_handle;
}

size_t scala_uv_sizeof_sockaddr_in()
{
return sizeof(struct sockaddr_in);
}

void scala_uv_init_sockaddr_in(int address, int port, struct sockaddr_in *addr)
{
addr->sin_family = AF_INET;
addr->sin_addr.s_addr = htonl(address);
addr->sin_port = htons(port);
}

size_t scala_uv_sizeof_sockaddr_in6()
{
return sizeof(struct sockaddr_in6);
}

void scala_uv_init_sockaddr_in6(const char *address, int port, unsigned int flow_info, unsigned int scope_id, struct sockaddr_in6 *addr)
{
addr->sin6_family = AF_INET6;
memcpy(&(addr->sin6_addr), address, sizeof(struct in6_addr));
addr->sin6_port = htons(port);
addr->sin6_flowinfo = htonl(flow_info);
addr->sin6_scope_id = htonl(scope_id);
}

// File open constants

int scala_uv_value_o_append()
Expand Down Expand Up @@ -206,28 +174,3 @@ int scala_uv_value_o_wronly()
{
return UV_FS_O_WRONLY;
}

int scala_uv_value_af_inet()
{
return AF_INET;
}

int scala_uv_value_af_inet6()
{
return AF_INET6;
}

sa_family_t scala_uv_sockaddr_family(const struct sockaddr *addr)
{
return addr->sa_family;
}

in_port_t scala_uv_sockaddr_port(const struct sockaddr_in *addr)
{
return addr->sin_port;
}

struct in_addr *scala_uv_sockaddr_address(const struct sockaddr_in *addr)
{
return &(addr->sin_addr);
}
11 changes: 4 additions & 7 deletions src/main/scala/scalauv/Buffer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,7 @@ object Buffer {
/** Allocates a new buffer structure on the stack and initializes it with the
* specified `base` and `length`.
*/
inline def stackAllocate(
base: Ptr[Byte],
length: CUnsignedInt
): Buffer = {
inline def stackAllocate(base: Ptr[Byte], length: CSize): Buffer = {
val uvBuf = stackalloc[Byte](structureSize)
uvBuf.init(base, length)
uvBuf
Expand Down Expand Up @@ -191,8 +188,8 @@ object Buffer {
/** Allocates a new buffer structure and initializes it with the specified
* `base` and `length`.
*/
def malloc(base: Ptr[Byte], size: CSize): Buffer = {
val uvBuf = stdlib.malloc(structureSize.toULong)
def malloc(base: Ptr[CChar], size: CSize): Buffer = {
val uvBuf = stdlib.malloc(structureSize)
h.scala_uv_buf_init(base, size.toUInt, uvBuf)
uvBuf
}
Expand All @@ -202,7 +199,7 @@ object Buffer {
* `array`. `length` is set to `array.length - index`.
*/
def malloc(array: Array[Byte], index: Int = 0): Buffer = {
val uvBuf = stdlib.malloc(structureSize.toULong)
val uvBuf = stdlib.malloc(structureSize)
h.scala_uv_buf_init(
array.at(index),
(array.length - index).toUInt,
Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/scalauv/IOVector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ object IOVector {
*/
inline def stackAllocateForBuffer(
ptr: Ptr[Byte],
size: CUnsignedInt
size: CSize
): IOVector = {
val buffer = Buffer.stackAllocate(ptr, size)
IOVector(buffer, 1)
Expand All @@ -65,7 +65,7 @@ object IOVector {
/** Stack allocates multiple buffer structures.
*/
inline def stackAllocateForBuffers(
buffers: Seq[(Ptr[Byte], CUnsignedInt)]
buffers: Seq[(Ptr[Byte], CSize)]
): IOVector = {
val uvBufs =
stackalloc[Byte](buffers.size.toUInt * Buffer.structureSize)
Expand Down
Loading

0 comments on commit f1152c2

Please sign in to comment.