Skip to content

Proposal for new layout of generated code #61

Closed
@jonas

Description

@jonas

This is still a draft, will fill out with more details in the coming days.

Experience from sbt-graphql suggests that code generation is much more robust if restricted to a single root object.

Example:

@native.link("bindgentests")
@native.extern
object bzip2 {
  type enum_days = native.CUnsignedInt
  object enum_days {
    final val MONDAY: enum_days = 0.toUInt
    final val TUESDAY: enum_days = 200.toUInt
    final val WEDNESDAY: enum_days = 201.toUInt
    final val THURSDAY: enum_days = 4.toUInt
    final val FRIDAY: enum_days = 5.toUInt
    final val SATURDAY: enum_days = 3.toUInt
    final val SUNDAY: enum_days = 4.toUInt
  }

  type struct_point = native.CStruct2[native.CInt, native.CInt]
  object struct_point {
    def apply(x: native.CInt, y: native.CInt)(implicit z: native.Zone): native.Ptr[struct_point] = {
      val ptr = native.alloc[struct_point]
      if (x != 0)
        !ptr._1 = x
      ptr
    }
  }

  def getPoint(): native.Ptr[struct_point] = native.extern
  def get_WEDNESDAY(): enum_days = native.extern

  object implicits {
    implicit class struct_point_ops(val p: native.Ptr[struct_point]) extends AnyVal {
      def x: native.CInt = !p._1
      def x_=(value: native.CInt):Unit = !p._1 = value
      def y: native.CInt = !p._2
      def y_=(value: native.CInt):Unit = !p._2 = value
    }
  }
}

Usage:

val a: bzip2.enum_days = bzip2.enum_days.MONDAY

import bzip2.implicits._
Zone { implicit zone =>
  val point = bzip2.struct_point(0, 1)
  assert(point.x == 0)
  point.x = 42
  assert(point.x == 42)
}

Metadata

Metadata

Assignees

Labels

bindgenBinding generatorrfcA proposal for a new functionality

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions