Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal for new layout of generated code #61

Closed
jonas opened this issue Jun 20, 2018 · 3 comments
Closed

Proposal for new layout of generated code #61

jonas opened this issue Jun 20, 2018 · 3 comments
Assignees
Labels
bindgen Binding generator rfc A proposal for a new functionality
Milestone

Comments

@jonas
Copy link
Member

jonas commented Jun 20, 2018

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)
}
@kornilova203
Copy link
Member

I like the idea

I knew that it's not possible to have val inside the object because it would give the error: extern objects may only contain extern fields and methods, but object works fine.

Anonymous enums can be stored in object anonymous_enum_0, object anonymous_enum_1, ...

@jonas jonas added rfc A proposal for a new functionality bindgen Binding generator labels Jun 25, 2018
@jonas
Copy link
Member Author

jonas commented Jun 25, 2018

@jonas jonas changed the title RFC: proposal for new layout of generated code Proposal for new layout of generated code Jun 27, 2018
@jonas jonas added this to the 0.3 milestone Jul 6, 2018
@kornilova203 kornilova203 self-assigned this Aug 8, 2018
@kornilova203
Copy link
Member

I crated a separate issue for struct/union constructors #155

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bindgen Binding generator rfc A proposal for a new functionality
Projects
None yet
Development

No branches or pull requests

2 participants