Skip to content

Commit

Permalink
update to Native 0.5.5
Browse files Browse the repository at this point in the history
- ch08 progress
  • Loading branch information
spamegg1 committed Aug 19, 2024
1 parent eb1d70f commit c882821
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 14 deletions.
2 changes: 1 addition & 1 deletion project.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//> using scala 3.4.2
//> using platform native
//> using nativeVersion 0.5.4
//> using nativeVersion 0.5.5
//> using exclude "gatling/*"
//> using options -explain-cyclic -Ydebug-cyclic
//> using dep io.argonaut::argonaut:6.3.10
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/ch07/common/libuv/libuv.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import scalanative.unsafe.*
@link("uv")
@extern
object LibUV:
type PipeHandle = Ptr[Byte]
type PipeHandle = Ptr[Byte] // mentioned in ch08
type PollHandle = Ptr[Ptr[Byte]]
type TCPHandle = Ptr[Byte] // different in ch09
type TTYHandle = Ptr[Byte]
Expand Down
6 changes: 5 additions & 1 deletion src/main/scala/ch08/common/Pipe.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,21 @@ import scala.util.{Try, Success, Failure}
import concurrent.{Future, ExecutionContext, Promise}

trait Pipe[T, U]:
val handlers = mutable.Set[Pipe[U, ?]]() // implemented
def feed(input: T): Unit // unimplemented
val handlers = mutable.Set[Pipe[U, ?]]() // the rest is implemented
def done(): Unit = for h <- handlers do h.done()

def addDestination[V](dest: Pipe[U, V]): Pipe[U, V] =
handlers += dest
dest

def map[V](g: U => V): Pipe[U, V] = addDestination(SyncPipe(g))
def mapConcat[V](g: U => Seq[V]): Pipe[U, V] = addDestination(ConcatPipe(g))
def mapOption[V](g: U => Option[V]): Pipe[U, V] = addDestination(OptionPipe(g))

def mapAsync[V](g: U => Future[V])(using ec: ExecutionContext): Pipe[U, V] =
addDestination(AsyncPipe(g))

def onComplete(using ec: ExecutionContext): Future[Unit] =
val sink = OnComplete[U]()
addDestination(sink)
Expand Down
15 changes: 9 additions & 6 deletions src/main/scala/ch08/common/SyncPipe.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import scala.util.{Try, Success, Failure}
import concurrent.{Future, ExecutionContext, Promise}

case class SyncPipe[T, U](f: T => U) extends Pipe[T, U]:
override def feed(input: T): Unit =
def feed(input: T): Unit = // implements the unimplemented method in Pipe
val output = f(input)
for h <- handlers do h.feed(output)

Expand All @@ -21,27 +21,30 @@ object SyncPipe:

def activeRequests: Int = activeStreams.size

def stream(fd: Int): Pipe[String, String] =
def apply(fd: Int): SyncPipe[String, String] = // book different than code, this is book
val handle = stdlib.malloc(uv_handle_size(UV_PIPE_T))
uv_pipe_init(ch07.EventLoop.loop, handle, 0)

val pipeData = handle.asInstanceOf[Ptr[Int]]
!pipeData = serial
activeStreams += serial
val pipe = Pipe.source[String]

val pipe = SyncPipe[String, String](identity) // book diffrent than code, this is book
handlers(serial) = pipe

serial += 1
uv_pipe_open(handle, fd)
uv_read_start(handle, allocCB, readCB)

pipe

val allocCB = CFuncPtr3.fromScalaFunction[TCPHandle, CSize, Ptr[Buffer], Unit]:
val allocCB: AllocCB = CFuncPtr3.fromScalaFunction:
(client: PipeHandle, size: CSize, buffer: Ptr[Buffer]) =>
val buf = stdlib.malloc(4096.toUSize) // 0.5
val buf = stdlib.malloc(4096) // malloc can take Int now! No need for .toUSize
buffer._1 = buf
buffer._2 = 4096.toUSize // 0.5

val readCB: ReadCB = CFuncPtr3.fromScalaFunction[TCPHandle, CSSize, Ptr[Buffer], Unit]:
val readCB: ReadCB = CFuncPtr3.fromScalaFunction:
(handle: TCPHandle, size: CSSize, buffer: Ptr[Buffer]) =>
val pipeData = handle.asInstanceOf[Ptr[Int]]
val pipeId = !pipeData
Expand Down
9 changes: 4 additions & 5 deletions src/main/scala/ch08/simplePipe/main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ package simplePipe

import scalanative.unsigned.{UnsignedRichLong, UnsignedRichInt}
import scalanative.unsafe.*
import scalanative.libc.stdlib
import scalanative.libc.string
import scalanative.libc.{stdlib, string}
import collection.mutable
import scala.util.{Try, Success, Failure}
import ch07.LibUV.*, ch07.LibUVConstants.*
Expand All @@ -30,13 +29,13 @@ object SyncPipe:
uv_read_start(handle, SyncPipe.allocCB, SyncPipe.readCB)
pipe

val allocCB = CFuncPtr3.fromScalaFunction[PipeHandle, CSize, Ptr[Buffer], Unit]:
val allocCB: AllocCB = CFuncPtr3.fromScalaFunction:
(client: PipeHandle, size: CSize, buffer: Ptr[Buffer]) =>
val buf = stdlib.malloc(4096) // 0.5
buffer._1 = buf
buffer._2 = 4096.toUSize // 0.5

val readCB: ReadCB = CFuncPtr3.fromScalaFunction[TCPHandle, CSSize, Ptr[Buffer], Unit]:
val readCB: ReadCB = CFuncPtr3.fromScalaFunction:
(handle: TCPHandle, size: CSSize, buffer: Ptr[Buffer]) =>
val pipeData = handle.asInstanceOf[Ptr[Int]]
val pipeId = !pipeData
Expand All @@ -55,7 +54,7 @@ object SyncPipe:
pipeDestination.feed(dataString.trim())

@main
def simplePipe(args: String*): Unit =
def run: Unit =
println("hello!")
val p = SyncPipe(0)
val q = p
Expand Down

0 comments on commit c882821

Please sign in to comment.