github.com/goccy/go-jit@v0.0.0-20200514131505-ff78d45cf6af/context.go (about)

     1  package jit
     2  
     3  import (
     4  	"github.com/goccy/go-jit/internal/ccall"
     5  )
     6  
     7  type Context struct {
     8  	*ccall.Context
     9  }
    10  
    11  func NewContext() *Context {
    12  	return &Context{ccall.CreateContext()}
    13  }
    14  
    15  func toContext(c *ccall.Context) *Context {
    16  	return &Context{c}
    17  }
    18  
    19  func (c *Context) Close() {
    20  	c.Destroy()
    21  }
    22  
    23  func (c *Context) Build(cb func(*Context) (*Function, error)) (*Function, error) {
    24  	c.BuildStart()
    25  	fn, err := cb(c)
    26  	if err != nil {
    27  		return nil, err
    28  	}
    29  	c.BuildEnd()
    30  	return fn, nil
    31  }
    32  
    33  func (c *Context) CreateFunction(argtypes Types, rtype *Type) *Function {
    34  	signature := CreateSignature(argtypes, rtype)
    35  	defer signature.Free()
    36  	return &Function{c.Context.CreateFunction(signature.Type)}
    37  }
    38  
    39  func (c *Context) CreateNestedFunction(signature *Type, parent *Function) *Function {
    40  	return toFunction(c.Context.CreateNestedFunction(signature.Type, parent.Function))
    41  }