Skip to content

Struct Alloc Helper #3

New issue

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

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

Already on GitHub? # to your account

Closed
mrRosset opened this issue May 23, 2018 · 1 comment
Closed

Struct Alloc Helper #3

mrRosset opened this issue May 23, 2018 · 1 comment
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@mrRosset
Copy link
Owner

A simple helper could be added to structures definitions to allocate them in the heap.

C code

struct listing {
	int a;
	int b;
};

Should generate
Scala code

import scala.scalanative._
import scala.scalanative.native.Nat._

@native.link("example")
@native.extern
object example {
  type struct_listing = native.CStruct2[native.CInt, native.CInt]
}

import example._

object exampleHelpers {

  implicit class struct_listing_ops(val p: native.Ptr[struct_listing]) extends AnyVal {
    def a: native.CInt = !p._1
    def a_=(value: native.CInt):Unit = !p._1 = value
    def b: native.CInt = !p._2
    def b_=(value: native.CInt):Unit = !p._2 = value
  }

  // New Helper to be added
  def struct_listing()(implicit z: native.Zone): native.Ptr[struct_listing] = native.alloc[struct_listing]
  
}

Motivations

  native.Zone {z =>

    // Current way of doing it
    val list = native.alloc[example.struct_listing]
    // With the new helper, closer to scala syntax
    val list = struct_listing()

    list.a = 22;
    list.b = 23;

    println("list: {" + list.a + ", " + list.b + "}")

  }

Implementation help

This should be implemented in TreeVisitor.cpp. In the VisitRecordDecl function. It's possible to look at the existing code that generate the current helpers to access each field in a struct.

@mrRosset mrRosset added enhancement New feature or request good first issue Good for newcomers labels May 23, 2018
@mrRosset mrRosset changed the title Malloc Helper Struct Alloc Helper May 23, 2018
@mrRosset
Copy link
Owner Author

Closed in #4

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

1 participant