github.com/gopherjs/gopherjs@v1.19.0-beta1.0.20240506212314-27071a8796e4/tests/testdata/linkname/one/one.go (about)

     1  // Package one is a root of test dependency tree, importing packages two and
     2  // three. It ensures a deterministic import and initialization order of the
     3  // test packages.
     4  package one
     5  
     6  import (
     7  	_ "unsafe" // for go:linkname
     8  
     9  	"github.com/gopherjs/gopherjs/tests/testdata/linkname/three"
    10  	"github.com/gopherjs/gopherjs/tests/testdata/linkname/two"
    11  )
    12  
    13  // DoOne is a regular function from the package one to demonstrate a call
    14  // without any special linking trickery.
    15  func DoOne() string {
    16  	return "doing one"
    17  }
    18  
    19  // doInternalOne is a function implemented in package one, but actually called
    20  // by package two using a go:linkname directive to gain access to it. Note:
    21  // dead-code elimination must be able to preserve this function.
    22  //
    23  // This is a demonstration that an imported package can linkname a function
    24  // from an importer package.
    25  func doInternalOne() string {
    26  	return "doing internal one: " + oneSecret
    27  }
    28  
    29  // oneSecret is an unexported variable in the package one, which doInternalOne()
    30  // must be able to access even when called from another package using a linkname
    31  // mechanism.
    32  var oneSecret = "one secret"
    33  
    34  // doInternalThree is implemented in the package three, but not exported (for
    35  // example, to not make it a public API), which package one gains access to
    36  // via a go:linkname directive.
    37  //
    38  // This is a demonstration that an importer package can linkname a non-exported
    39  // function from an imported package.
    40  //
    41  //go:linkname doInternalThree github.com/gopherjs/gopherjs/tests/testdata/linkname/three.doInternalThree
    42  func doInternalThree() string
    43  
    44  func DoAll() string {
    45  	result := "" +
    46  		DoOne() + "\n" + // Normal function call in the same package.
    47  		two.DoTwo() + "\n" + // Normal cross-package function call.
    48  		two.DoImportedOne() + "\n" + // Call a function that package two linknamed.
    49  		three.DoThree() + "\n" + // Normal cross-package function call.
    50  		"doing imported three: " + doInternalThree() + "\n" // Call a function from another package this package linknamed.
    51  	return result
    52  }