compiler

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 30, 2020 License: AGPL-3.0 Imports: 37 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (

	// DefaultPriority is the default value for a script's priority during execution
	DefaultPriority = 100

	// DefaultTimeout is the default time in seconds a script will be allowed to run
	DefaultTimeout = 30
)
View Source
var (

	//CallablesByEngineVersion is a table that maps the entry points to the expected script versions
	CallablesByEngineVersion = map[int][]string{
		1: []string{
			"BeforeDeploy",
			"Deploy",
			"AfterDeploy",
		},
		2: []string{
			"Deploy",
		},
	}
)
View Source
var (

	// NamespaceConversionMap is a temporary mapping to allow genesis scripts
	// written in the v0 specification to be backwards compatible with v1 scripts.
	// The major difference is that v1 did not have namespaces for the standard library
	// where as v1 implements them with a deprecation and v2 will remove them entirely
	NamespaceConversionMap = map[string]*LegacyFunctionCall{}
)

Functions

func ExampleDecodeEmbed added in v0.1.1

func ExampleDecodeEmbed(b64encoded string, key string) []byte

ExampleDecodeEmbed is a reference implementation of how embedded assets should be unpacked inside of a genesis engine

func GetDefaultImportNamespace added in v0.1.1

func GetDefaultImportNamespace(ip string) string

GetDefaultImportNamespace returns the corrasponding namespace to the import path provided that is used in the intermediate representation

func IsBuiltInGoType added in v0.1.1

func IsBuiltInGoType(s string) bool

IsBuiltInGoType takes a string argument and determines if is a valid built-in type in golang

func IsDefaultImport added in v0.1.1

func IsDefaultImport(ip string) bool

IsDefaultImport tests a golang import path to determine if it is already defined in the intermediate representation

Types

type Compiler

type Compiler struct {
	// lock to prevent race conditions during compilation
	sync.RWMutex

	// array of VMs that will be bundled into this build
	VMs []*GenesisVM

	// a map that places subsets of VMs into buckets according to their priority
	SortedVMs map[int][]*GenesisVM

	// logging object to be used
	Logger logger.Logger

	// a slice of unique priorities that can be found within this VMs bundled into this build
	UniqPriorities []int

	// configuration object for the compiler
	computil.Options
	// contains filtered or unexported fields
}

Compiler is the primary type for building native binaries with gscript

func NewWithDefault added in v0.1.1

func NewWithDefault() *Compiler

NewWithDefault returns a new compiler object with default options

func NewWithOptions added in v0.1.1

func NewWithOptions(o computil.Options) *Compiler

NewWithOptions returns a new compiler object with custom options

func (*Compiler) AddScript added in v0.1.1

func (c *Compiler) AddScript(scriptPath string) error

AddScript attempts to create a virtual machine object based on the given parameter to be included in compilation

func (*Compiler) BuildGolangASTs added in v0.1.1

func (c *Compiler) BuildGolangASTs() error

BuildGolangASTs enumerates each genesis vm's golang native packages and matches exported function declarations to their genesis script caller. This creates a reference in the VM's linker object which will be used to generate native interfaces between the genesis VM and the underlying golang packages.

func (*Compiler) BuildNativeBinary added in v0.1.1

func (c *Compiler) BuildNativeBinary() error

BuildNativeBinary uses the golang compiler to attempt to build a native binary for the target platform specified in the compiler options

func (*Compiler) CreateBuildDir

func (c *Compiler) CreateBuildDir() error

CreateBuildDir creates the compiler's build directory, with an additional asset directory as well

func (*Compiler) CreateEntryPoint added in v0.1.1

func (c *Compiler) CreateEntryPoint() error

CreateEntryPoint renders the final main() entry point for the final binary in the build directory

func (*Compiler) DetectVersions added in v0.1.1

func (c *Compiler) DetectVersions() error

DetectVersions enumerates all VMs to determine the engine version based on the entrypoint. For more information on this, look at GenesisVM.DetectTargetEngineVersion()

func (*Compiler) Do

func (c *Compiler) Do() error

Do runs all compiler functions once scripts are added to it

func (*Compiler) EncodeAssets added in v0.1.1

func (c *Compiler) EncodeAssets() error

EncodeAssets renders all embedded assets into intermediate representation

func (*Compiler) GatherAssets added in v0.1.1

func (c *Compiler) GatherAssets() error

GatherAssets enumerates all bundled virtual machines for any embedded assets and copies them into the build directory's asset cache

func (*Compiler) GetIDLiterals added in v0.1.1

func (c *Compiler) GetIDLiterals() []string

GetIDLiterals returns all interesting IDs used by this compiler

func (*Compiler) InitializeImports added in v0.1.1

func (c *Compiler) InitializeImports() error

InitializeImports enumerates the compiler's genesis VMs and writes a cached version of the genesis source to the asset directory to prevent race condiditons with script filesystem locations

func (*Compiler) IsProductionBuild added in v0.1.1

func (c *Compiler) IsProductionBuild() bool

IsProductionBuild is an convienience method for checking to see if dumping a stack trace should be disabled for production builds

func (*Compiler) LocateGoDependencies added in v0.1.1

func (c *Compiler) LocateGoDependencies() error

LocateGoDependencies gathers a list of all installed golang packages, hands a copy to each VM, then has every VM resolve it's own golang dependencies from that package list

func (*Compiler) MapVMsByPriority added in v0.1.1

func (c *Compiler) MapVMsByPriority() error

MapVMsByPriority creates a pointer mapping of each VM by it's unique priority

func (*Compiler) PerformPostCompileObfuscation added in v0.1.1

func (c *Compiler) PerformPostCompileObfuscation() error

PerformPostCompileObfuscation runs the post-compilation obfuscation routines on compiled binary

func (*Compiler) PerformPreCompileObfuscation added in v0.1.1

func (c *Compiler) PerformPreCompileObfuscation() error

PerformPreCompileObfuscation runs the pre-compilation obfuscation routines on the intermediate representation

func (*Compiler) ProcessMacros added in v0.1.1

func (c *Compiler) ProcessMacros() error

ProcessMacros enumerates the compilers virtual machines with the pre-processor to extract compiler macros for each virtual machine

func (*Compiler) SanityCheckScriptToNativeMapping added in v0.1.1

func (c *Compiler) SanityCheckScriptToNativeMapping() error

SanityCheckScriptToNativeMapping enumerates all VMs ensuring that the script calls to native functions actually exist within that native go package and were resolved by the linker

func (*Compiler) SanityCheckSwizzles added in v0.1.1

func (c *Compiler) SanityCheckSwizzles() error

SanityCheckSwizzles enumerates all VMs to make sure their linked native functions are being called correctly by the corrasponding javascript callers

func (*Compiler) SetLogger added in v0.1.1

func (c *Compiler) SetLogger(l logger.Logger)

SetLogger overrides the logger for the compiler (defaults to an engine.NullLogger)

func (*Compiler) SwizzleNativeCalls added in v0.1.1

func (c *Compiler) SwizzleNativeCalls() error

SwizzleNativeCalls enumerates all native golang function calls mapped to genesis script function calls and generates the type declarations for both arguments and return values.

func (*Compiler) WalkGenesisASTs added in v0.1.1

func (c *Compiler) WalkGenesisASTs() error

WalkGenesisASTs scans all genesis VMs scripts to identify Golang packages that have been called from inside the script using the namespace identifier from the compiler macros

func (*Compiler) WritePreloads added in v0.1.1

func (c *Compiler) WritePreloads() error

WritePreloads renders preload libraries for every virtual machine in the compilers asset directory

func (*Compiler) WriteScripts added in v0.1.1

func (c *Compiler) WriteScripts() error

WriteScripts enumerates the compiler's genesis VMs and writes a cached version of the genesis source to the asset directory to prevent race condiditons with script filesystem locations

func (*Compiler) WriteVMBundles added in v0.1.1

func (c *Compiler) WriteVMBundles() error

WriteVMBundles writes the intermediate representation for each virtual machine to it's vm bundle file within the build directory

type EmbeddedFile

type EmbeddedFile struct {
	// local file path to the target file
	SourcePath string

	// url to the target file (optional)
	CachedPath string

	// the composite ($ID_$OrigName) filename of the referenced file
	Filename string

	// the original basename of the referenced file
	OrigName string

	// unique identifier that is used by the compiler to reference file contents
	ID string

	// unique identifier that is used by the compiler to swizzle the file decoding and decrypting into a function pointer
	FuncID string

	// uncompressed embedded file data
	Uncompressed []byte

	// compressed embedded file data
	Compressed []byte

	// used to AES encrypt the embedded assets
	EncryptionKey []byte

	// temporary buffer used to generate the intermediate representation of the compressed data
	EmbedData *bytes.Buffer
}

EmbeddedFile is an object that manages the lifecycle of resolving and translating embedded assets referenced in the Genesis VM into callable values that are embedded by the compiler

func NewEmbeddedFile added in v0.1.1

func NewEmbeddedFile(source string, key []byte) (*EmbeddedFile, error)

NewEmbeddedFile takes a path on the local file system and returns an EmbeddedFile object reference

func (*EmbeddedFile) CacheFile added in v0.1.1

func (e *EmbeddedFile) CacheFile(cacheDir string) error

CacheFile attempts to copy the files original location (e.SourcePath) to the destination cacheDir provided as an argument to this function call

func (*EmbeddedFile) Data added in v0.0.14

func (e *EmbeddedFile) Data() string

Data retrieves the current EmbedData's buffer as a string

func (*EmbeddedFile) GenerateEmbedData added in v0.0.14

func (e *EmbeddedFile) GenerateEmbedData() error

GenerateEmbedData enumerates the compressed embed and creates a byte slice representation of it

type FunctionCall added in v0.1.1

type FunctionCall struct {
	// genesis vm namespace the function call corrasponds to
	Namespace string

	// name of the function call as used in the genesis vm
	FuncName string

	// list of arguments passed to the genesis vm function caller
	ArgumentList []gast.Expression
}

FunctionCall contains information relating to a Golang native function call that is being used within a genesis vm

type GenesisVM added in v0.1.1

type GenesisVM struct {
	// mutex for compiler manipulation
	sync.RWMutex

	// generated script ID
	ID string `json:"id"`

	// Absolute path to the script file
	SourceFile string `json:"source"`

	// map of embedded files
	Embeds map[string]*EmbeddedFile

	// name of the script (base name of file)
	Name string

	// raw script contents
	Data []byte

	// FileSet for parsing the genesis ASTs
	FileSet *gfile.FileSet

	// represents script as an GenesisAST
	GenesisAST *gast.Program

	// holds the value of parsed macros
	Macros []*Macro

	// maps the current build environment's golang imports
	// key = golang import path
	// value = reference to go package object
	GoPackageByImport map[string]*GoPackage

	// maps the namespace of genesis go imports to their
	// corrasponding go packages
	// key = genesis import namespace
	// value = reference to a go package object
	GoPackageByNamespace map[string]*GoPackage

	// StandardLibraries holds all references to used genesis standard library
	// packages that will be included in the build
	EnabledStandardLibs map[string]*GoPackage

	// required operating system for this script (GOOS)
	RequiredOS string

	// required architecture for this script (GOARCH)
	RequiredArch string

	// Object that holds the translation targets between golang and gscript
	Linker *Linker

	// maintains a map of the function names to the obfuscated references
	EntryPointMapping map[string]string

	// unique variable name to reference this scripts entry point
	PreloadAlias string

	// list of functions that need to be waterfalled for successful execution of the VM
	// usually either single element (Deploy) or the legacy compatible BeforeDeploy, Deploy, AfterDeploy
	EngineVersion int

	// reference to compiler options
	computil.Options

	// GenesisFile holds the intermediate representation of this VM's bundle code
	GenesisFile *bytes.Buffer

	// Logger to publish output from
	Logger logger.Logger

	// EnginePackage is the go package this engine should use
	EnginePackage *GoPackage

	// StandardLibs holds references to all possible standard libs
	StandardLibs map[string]*GoPackage
}

GenesisVM is the object representation of a genesis script including it's imports and dynamic linking targets

func NewGenesisVM added in v0.1.1

func NewGenesisVM(name, path string, data []byte, prog *gast.Program, opts computil.Options, logger logger.Logger) *GenesisVM

NewGenesisVM creates a new virtual machine object for the compiler

func (*GenesisVM) BuildGolangAST added in v0.1.1

func (g *GenesisVM) BuildGolangAST() error

BuildGolangAST walks the golang packages imported into the script to build a mapping of functions, the files they're in, imports to each file (for aliases), and locations in the genesis script where these are referenced

func (*GenesisVM) CacheAssets added in v0.1.1

func (g *GenesisVM) CacheAssets() error

CacheAssets indexes all //import: compiler macros and retrieves the corrasponding asset

func (*GenesisVM) DetectTargetEngineVersion added in v0.1.1

func (g *GenesisVM) DetectTargetEngineVersion() error

DetectTargetEngineVersion examines the genesis script's GenesisAST to determine whether required top level functions exist, and if so, for what version of the engine they target. This mapping can be found in CallablesByEngineVersion

func (*GenesisVM) EnableStandardLibrary added in v0.1.1

func (g *GenesisVM) EnableStandardLibrary(name string) (*GoPackage, error)

EnableStandardLibrary attempts to resolve a discovered standard library and returns either the package or an error

func (*GenesisVM) EncodeBundledAssets added in v0.1.1

func (g *GenesisVM) EncodeBundledAssets() error

EncodeBundledAssets encodes all assets within the asset pack into their compressed format

func (*GenesisVM) FunctionKey added in v0.1.1

func (g *GenesisVM) FunctionKey(k string) string

FunctionKey is used by the intermediate representation generator to map specific functions in the virtual machine's constructors to unique identifiers in the IR

func (*GenesisVM) GenerateFunctionKeys added in v0.1.1

func (g *GenesisVM) GenerateFunctionKeys()

GenerateFunctionKeys creates random functions for the various parts of the VM's source file

func (*GenesisVM) GetIDLiterals added in v0.1.1

func (g *GenesisVM) GetIDLiterals() []string

GetIDLiterals returns all interesting IDs used by this GenesisVM

func (*GenesisVM) GetMaskedImports added in v0.1.1

func (g *GenesisVM) GetMaskedImports() []*MaskedImport

GetMaskedImports is a helper function to gather the masked imports during rendering of the vm bundle

func (*GenesisVM) GetNewDecryptionKey added in v0.1.1

func (g *GenesisVM) GetNewDecryptionKey() string

GetNewDecryptionKey creates a new decryption key

func (*GenesisVM) GetSimpleMacroValue added in v0.1.1

func (g *GenesisVM) GetSimpleMacroValue(key string) string

GetSimpleMacroValue returns a string of the VM's macro defined by key argument

func (*GenesisVM) GetTimeout added in v0.1.1

func (g *GenesisVM) GetTimeout() int

GetTimeout attempts to get the timeout value set in the macro (if it was set)

func (*GenesisVM) HasDebuggingEnabled added in v0.1.1

func (g *GenesisVM) HasDebuggingEnabled() bool

HasDebuggingEnabled is an convienience method for checking to see if the debugger should be included

func (*GenesisVM) HasLoggingEnabled added in v0.1.1

func (g *GenesisVM) HasLoggingEnabled() bool

HasLoggingEnabled is an convienience method for checking to see if logging should be included

func (*GenesisVM) InitializeGoImports added in v0.1.1

func (g *GenesisVM) InitializeGoImports() error

InitializeGoImports enumerates the go_import macros to initialize mappings for dynamic linking

func (*GenesisVM) LocateGoPackages added in v0.1.1

func (g *GenesisVM) LocateGoPackages() error

LocateGoPackages enumerates the installed go packages on the current system and appends directory and namespace information to golang packages being declared by this script

func (*GenesisVM) Priority added in v0.1.1

func (g *GenesisVM) Priority() int

Priority returns the priority value if defined in the macros, else returns default

func (*GenesisVM) ProcessMacros added in v0.1.1

func (g *GenesisVM) ProcessMacros() error

ProcessMacros runs the preprocessor to locate and extract genesis macro's out of the script to be used during compilation

func (*GenesisVM) RenderVMBundle added in v0.1.1

func (g *GenesisVM) RenderVMBundle(templateFile string) error

RenderVMBundle generates the virtual machine's bundled intermediate representation file

func (*GenesisVM) RetrieveAsset added in v0.1.1

func (g *GenesisVM) RetrieveAsset(m *Macro) error

RetrieveAsset attempts to copy the asset into the build directory

func (*GenesisVM) SanityCheckLinkedSymbols added in v0.1.1

func (g *GenesisVM) SanityCheckLinkedSymbols() error

SanityCheckLinkedSymbols checks to make sure all linked functions do not violate caller conventions between the javascript and golang method signatures.

func (*GenesisVM) SanityCheckNativeFunctionCalls added in v0.1.1

func (g *GenesisVM) SanityCheckNativeFunctionCalls() error

SanityCheckNativeFunctionCalls enumerates the VMs go packages ensuring that there are no script callers who do not exist within the GoPackage symbol table

func (*GenesisVM) ShouldIncludeAssetPackage added in v0.1.1

func (g *GenesisVM) ShouldIncludeAssetPackage() bool

ShouldIncludeAssetPackage is a helper function for VM bundle rendering to asset whether it needs to create asset functions in the intermediate representation

func (*GenesisVM) SwizzleNativeFunctionCalls added in v0.1.1

func (g *GenesisVM) SwizzleNativeFunctionCalls() error

SwizzleNativeFunctionCalls enumerates all LinkedFunctions held by the linker and generates structured mappings of both arguments (left swizzle) and returns (right swizzle) so the compiler can map the function's shim in the intermediate representation

func (*GenesisVM) UnresolvedGoPackages added in v0.1.1

func (g *GenesisVM) UnresolvedGoPackages() []string

UnresolvedGoPackages enumerates the import table to determine if any packages have not been resolved to local dependencies yet. Returns a string slice of go import paths that have yet to be resolved.

func (*GenesisVM) WalkGenesisAST added in v0.1.1

func (g *GenesisVM) WalkGenesisAST() error

WalkGenesisAST walks the genesis script in order to inspect function calls that should be targeted for both legacy dynamic linking as well as native golang dynamic linking. Reference type genesisWalker and it's associated functions inside genesis_ast.go

func (*GenesisVM) WalkGoPackageAST added in v0.1.1

func (g *GenesisVM) WalkGoPackageAST(gop *GoPackage, wg *sync.WaitGroup, errChan chan error)

WalkGoPackageAST parses the GoPackage directory for all AST files and concurrently walks each child file's AST looking for functions that should be included by the linker

func (*GenesisVM) WriteGenesisScript added in v0.1.1

func (g *GenesisVM) WriteGenesisScript(name string, src []byte) (*EmbeddedFile, error)

WriteGenesisScript writes a genesis script to the asset directory and returns a reference to an embeddedfile for use by the compiler

func (*GenesisVM) WritePreload added in v0.1.1

func (g *GenesisVM) WritePreload() error

WritePreload writes the preload library to the asset directory and tags it in the embed table as the preload library for the virtual machine

func (*GenesisVM) WriteScript added in v0.1.1

func (g *GenesisVM) WriteScript() error

WriteScript writes the initial user supplied script to the asset directory and tags it in the embed table as the entry point for the user defined functions

func (*GenesisVM) WriteVMBundle added in v0.1.1

func (g *GenesisVM) WriteVMBundle() error

WriteVMBundle generates the VM bundle's intermediate representation using RenderVMBundle and then writes it to the compilers build directory

type GoConst added in v0.1.1

type GoConst struct {
	// Name refers to the const name within the package
	Name string

	// Key refers to the unique identifier for a const in the intermediate representation
	Key string
}

GoConst defines the name of a golang constant declaration

type GoPackage added in v0.1.1

type GoPackage struct {
	sync.RWMutex

	// Dir is the local path where this package is found
	Dir string

	// MaskedName is the masked import representation of this gopackage
	MaskedName string

	// ImportPath is the golang import path used for this package
	ImportPath string

	// Name defines the go package's name
	Name string

	// VM references the script that is importing this package
	VM *GenesisVM

	// Namespace is the namespace aliased in the parent script
	Namespace string

	// ImportKey is the import path defined in the parent script
	ImportKey string

	// ScriptCallers maps the go package function names to genesis AST function calls
	ScriptCallers map[string]*FunctionCall

	// ImportsByFile is the map of each file within the package and what dependencies it imports
	ImportsByFile map[string][]*ast.ImportSpec

	// ImportsByAlias is a map of each import's alias and its dependency information
	ImportsByAlias map[string]*ast.ImportSpec

	// FuncToFileMap maps each public function within the package to it's corrasponding source file
	FuncToFileMap map[string]string

	// FuncTable is the map of each function name to it's Golang AST declaration
	FuncTable map[string]*ast.FuncDecl

	// LinkedFuncs defines references to the dynamically linked functions for this go package
	LinkedFuncs []*LinkedFunction

	// Reference to know if this go package is part of the standard library
	IsStandardLib bool

	// FileSet is used by the parser to interpret the current golang's file tokens
	FileSet *token.FileSet

	// GoTypes are struct type declarations that need to be accounted for in the engine
	GoTypes map[string]*GoStructDef

	// GoConsts are const declarations that need to be accounted for in the engine
	GoConsts map[string]*GoConst

	// GoVars are top level var declarations that need to be accounted for in the engine
	GoVars map[string]*GoVar
}

GoPackage holds all the information about a Golang package that is being resolved to a given script

func NewGoPackage added in v0.1.1

func NewGoPackage(v *GenesisVM, ns, ikey string, stdlib bool) *GoPackage

NewGoPackage is a constructor for a gopackage that will be used in dynamically linking native code

func (*GoPackage) NewConst added in v0.1.1

func (gop *GoPackage) NewConst(name string) *GoConst

NewConst creates a new const reference within a golang package

func (*GoPackage) NewGoVar added in v0.1.1

func (gop *GoPackage) NewGoVar(goast *ast.File, vs *ast.ValueSpec, offset int, ident *ast.Ident, expr ast.Expr, name string) (*GoVar, error)

NewGoVar creates a new GoVar object if one doesn't already exist with that name in the GoPackage

func (*GoPackage) ParseFuncDecl added in v0.1.1

func (gop *GoPackage) ParseFuncDecl(goast *ast.File, funcdecl *ast.FuncDecl, wg *sync.WaitGroup, errChan chan error)

ParseFuncDecl walks a func declaration inside of a gopackage to result it's method signature

func (*GoPackage) ParseStructDef added in v0.1.1

func (gop *GoPackage) ParseStructDef(goast *ast.File, v *ast.TypeSpec, t *ast.StructType, wg *sync.WaitGroup, errChan chan error)

ParseStructDef creates a new mapping between a go package and a struct type definition

func (*GoPackage) ParseTypeSpec added in v0.1.1

func (gop *GoPackage) ParseTypeSpec(goast *ast.File, v *ast.TypeSpec, wg *sync.WaitGroup, errChan chan error)

ParseTypeSpec attempts to filter type spec definitions within the AST to only struct types

func (*GoPackage) ParseVarSpec added in v0.1.1

func (gop *GoPackage) ParseVarSpec(goast *ast.File, vardecl *ast.ValueSpec, offset int, wg *sync.WaitGroup, errChan chan error)

ParseVarSpec walks a var declaration inside of a gopackage to make sure it can sanely be accounted for by the compiler

func (*GoPackage) SanityCheckScriptCallers added in v0.1.1

func (gop *GoPackage) SanityCheckScriptCallers() error

SanityCheckScriptCallers enumerates all of the parent gopackage's script callers looking for any callers who do not have an entry in this go package's symbol table

func (*GoPackage) SuccessfullyLinkedFuncs added in v0.1.1

func (gop *GoPackage) SuccessfullyLinkedFuncs() []*LinkedFunction

SuccessfullyLinkedFuncs is used during rendering to make sure that only linked functions that successfully swizzled are going to be built into the source

func (*GoPackage) ValidVars added in v0.1.1

func (gop *GoPackage) ValidVars() []*GoVar

ValidVars is used during rendering to ensure that only valid var declarations that have been processed by the swizzler are included in the intermediate representation

func (*GoPackage) WalkGoFileAST added in v0.1.1

func (gop *GoPackage) WalkGoFileAST(goast *ast.File, wg *sync.WaitGroup, errChan chan error)

WalkGoFileAST walks the AST of a golang file and determines if it should be included as a linked function based on one of the following statements being true: Parent GoPackage is a member of the standard library OR Compiler option ImportAllNativeFunc is set to true OR VM Script calls this function explicitly

type GoParamDef added in v0.1.1

type GoParamDef struct {
	// Type denotes the type of parameter def (struct, function, etc.)
	Type string

	// OriginalName is to reference the original name of the field incase of problems
	OriginalName string

	// SigBuffer is used to create a definition to the actual type declaration in the genesis compiler's linker
	SigBuffer bytes.Buffer

	// NameBuffer is used to create a label that the genesis linker can use to create it's translations
	NameBuffer bytes.Buffer

	// ImportRefs holds a mapping of any golang dependencies required for this parameter's type declaration
	ImportRefs map[string]*ast.ImportSpec

	// VarName holds a representation of the logical representation of a parameter's label with it's offset appended
	VarName string

	// ParamIdx will be the relative position within the parameter declaration.
	// This is inclusive to multiple declarations of the same type. Example:
	//
	// func Foo(a, b string, c int) {}
	// GoParamDef objects for "a" and "b" would hold the same ParamIdx, but different
	// ArgOffset values
	ParamIdx int

	// ArgOffset defines the absolute position within the parameter declaration. It will increment
	// Regardless of multiple labels defined in the same type.
	// This is used by the linker to correctly translate arguments for golang functions
	ArgOffset int

	// ExtSig will hold the final result of the SigBuffer rendering as a string
	ExtSig string

	// GoLabel is used to represent the label name within Golang
	GoLabel string

	// NeedsMapping defines a hard coded type mapping between golang and javascript types
	NeedsMapping bool

	// MappedTypeAlias is a helper method to look up the type a method should be
	MappedTypeAlias string

	// LinkedFUnction is used to reference the parent LinkedFunction object
	LinkedFunction *LinkedFunction

	// IsInterfaceType defines ifthe GoParamDef an interface type?
	IsInterfaceType bool

	// SkipResolution defines whether the left recursive lexer should skip package resolution
	SkipResolution bool

	// Reference to the parent GoPackage
	GoPackage *GoPackage

	// False, unless the interpretation fails
	Errored bool

	// Holds the error message if the interpretation fails
	ErrorMessage string
}

GoParamDef defines a type to represent parameters found in a Golang function declaration (arguments or return types)

func NewGoParamDef added in v0.1.1

func NewGoParamDef(l *LinkedFunction, idx int) *GoParamDef

NewGoParamDef creates a new definition object for a go parameter (either return or argument) and returns a pointer to itself.

func (*GoParamDef) BuiltInJSType added in v0.1.1

func (p *GoParamDef) BuiltInJSType() string

BuiltInJSType returns the type that JS will return that we need to convert the ParamDef to golang

func (*GoParamDef) BuiltInTranslationRequired added in v0.1.1

func (p *GoParamDef) BuiltInTranslationRequired() bool

BuiltInTranslationRequired is a compiler helper to determine if the param definition requires a built in translation

func (*GoParamDef) Interpret added in v0.1.1

func (p *GoParamDef) Interpret(i interface{}) error

Interpret is a recursive walk function that is used to dispatch the next walker depending on the type of the provided interface (i). This is used to build up buffers of both names and golang type declarations to be used during linking.

func (*GoParamDef) MappedType added in v0.1.1

func (p *GoParamDef) MappedType(pkg, sel string) string

MappedType looks at whether there is a type mapping that needs to be honored

func (*GoParamDef) ParseArrayType added in v0.1.1

func (p *GoParamDef) ParseArrayType(a *ast.ArrayType) error

ParseArrayType interprets a golang array/slice type into the appropriate GoParamDef structure

func (*GoParamDef) ParseIdent added in v0.1.1

func (p *GoParamDef) ParseIdent(a *ast.Ident) error

ParseIdent interprets a golang identifier into the appropriate GoParamDef structure

func (*GoParamDef) ParseInterfaceType added in v0.1.1

func (p *GoParamDef) ParseInterfaceType(i *ast.InterfaceType) error

ParseInterfaceType is used to parse an interface type that is being passed into a linked function

func (*GoParamDef) ParseMapType added in v0.1.1

func (p *GoParamDef) ParseMapType(a *ast.MapType) error

ParseMapType interprets a golang map type into the appropriate GoParamDef structure

func (*GoParamDef) ParseSelectorExpr added in v0.1.1

func (p *GoParamDef) ParseSelectorExpr(a *ast.SelectorExpr) error

ParseSelectorExpr interprets a golang namespace external to the function declarations package and maps it into the appropriate GoParamDef structure

func (*GoParamDef) ParseStarExpr added in v0.1.1

func (p *GoParamDef) ParseStarExpr(a *ast.StarExpr) error

ParseStarExpr interprets a golang pointer into the appropriate GoParamDef structure

type GoStructDef added in v0.1.1

type GoStructDef struct {
	sync.RWMutex

	// Package is a reference to the parent GoPackage
	Package *GoPackage

	// File is a reference to the parent AST file
	File *ast.File

	// TypeSpec references the typespec node within the file's AST
	TypeSpec *ast.TypeSpec

	// AST references the actual struct definition within the file's AST
	AST *ast.StructType

	// Name represents the name of the struct definition
	Name string

	// Key represents the masked name of the function declaration in the intermediate representation
	Key string

	// ImportRefs hold a mapping of any golang dependencies required for this type declaration
	ImportRefs map[string]*ast.ImportSpec

	// Fields are the fields that can be used within this struct (compatible with genesis type conversion)
	Fields map[string]*GoParamDef

	// Incompatible are fields that CANNOT be used with genesis
	Incompatible map[string]*GoParamDef

	// Embeds is a convenience reference showing any embedded types that might exist within this struct
	Embeds map[string]*GoParamDef
}

GoStructDef defines a struct type definition to be used within the genesis engine

func (*GoStructDef) ParseStructField added in v0.1.1

func (gsd *GoStructDef) ParseStructField(f *ast.Field, name string, offset int, wg *sync.WaitGroup, errChan chan error)

ParseStructField attempts to interpret the struct fields within exported go types

func (*GoStructDef) WalkStruct added in v0.1.1

func (gsd *GoStructDef) WalkStruct(wg *sync.WaitGroup, errChan chan error)

WalkStruct walks the type definition AST for exported struct fields

type GoVar added in v0.1.1

type GoVar struct {
	sync.RWMutex

	// Package is a reference to the parent GoPackage
	Package *GoPackage

	// File is a reference to the parent AST file
	File *ast.File

	// ValueSpec is the parent ValueSpec declaration in the AST
	ValueSpec *ast.ValueSpec

	// VSOffset is the offset within the ValueSpec's Ident this var is found
	VSOffset int

	// Ident is a reference to the actual Golang AST ident object
	Ident *ast.Ident

	// Expr holds the data we are checking out!
	Expr ast.Expr

	// Name is the name of the exported var
	Name string

	// Key is the swizzled unique identifier to the declaration
	Key string

	// Valid means it has passed swizzle sanity check for types we can't mess with
	Valid bool

	// Def is the swizzling point of reference for this particular package
	Def *GoParamDef
}

GoVar defines a top level var declaration within a Golang package

func (*GoVar) ParseDeclaration added in v0.1.1

func (gv *GoVar) ParseDeclaration(wg *sync.WaitGroup, errChan chan error)

ParseDeclaration attempts to parse a top level var declaration and account for any incompatible types

func (*GoVar) Signature added in v0.1.1

func (gv *GoVar) Signature() string

Signature is a helper function that returns the type representation of a GoVar

type LegacyArgDef added in v0.1.1

type LegacyArgDef struct {
	// the name of the argument passed into the function
	Name string `yaml:"label"`

	// the golang type argument is expected to be
	GoType string `yaml:"gotype"`
}

LegacyArgDef is used by LegacyFunctionCall objects to create mappings for legacy function argument's for the linker to inject into the build

type LegacyFunctionCall added in v0.1.1

type LegacyFunctionCall struct {
	// name of the legacy function
	Name string `yaml:"name"`

	// description of the legacy function
	Description string `yaml:"description"`

	// owner of the legacy function
	Author string `yaml:"author"`

	// package name the legacy function is in
	Package string `yaml:"package"`

	// the expected arguments to the legacy function call
	ExpectedArgTypes []LegacyArgDef `yaml:"args"`

	// the expected returns to the legacy function call
	ExpectedReturnTypes []LegacyRetDef `yaml:"returns"`
}

LegacyFunctionCall uses the old generator to represent v0 standard library functions so they can be deprecated in subsequent versions without forcing users to convert all v0 scripts to v1 at this time

type LegacyLibrary added in v0.1.1

type LegacyLibrary struct {
	// name of the owning package in the legacy standard library
	Name string `yaml:"package"`

	// path to the golang file located in which this library is implemented
	Path string `yaml:"path"`

	// file set for the AST walk and re-write
	FSet *token.FileSet `yaml:"-"`

	// Golang AST representation of the library's source file
	AST *ast.File `yaml:"-"`

	// map of the function names to their LegacyFunctionCall objects
	Funcs map[string]*LegacyFunctionCall `yaml:"-"`

	// the raw file data for the legacy library source
	Source []byte `yaml:"-"`
}

LegacyLibrary defines one of the original standard library golang packages for backward compatibility during linking to associate global namespace function calls with the v1 package style

type LegacyRetDef added in v0.1.1

type LegacyRetDef struct {
	// name of the rerturn value parameter
	Name string `yaml:"label"`

	// the golang type return parameter is expected to be
	GoType string `yaml:"gotype"`

	// optional value to determine if you wish to return this value to the VM
	ReturnToVM bool `yaml:"return,omitempty"`
}

LegacyRetDef is used by LegacyFunctionCall objects to create mappings for legacy return values for the linker to inject into the build

type LinkedFunction added in v0.1.1

type LinkedFunction struct {
	// ID of the function that is linked
	ID string

	// string representation of the function basename
	Function string

	// reference to the caller in the genesis script AST
	Caller *FunctionCall

	// reference to the golang AST tree of the file this function is declared in
	File *ast.File

	// reference to the declaration of this function in the golang AST
	GoDecl *ast.FuncDecl

	// list of references to any imports needed in declaring argument and return parameters
	// for this linked function
	Imports []*ast.ImportSpec

	// reference to the compiler's go package object
	GoPackage *GoPackage

	// a slice of all the go parameters that make up the argument signature
	GoArgs []*GoParamDef

	// a slice of all the go parameters that make up the return signature
	GoReturns []*GoParamDef

	// a reference to the parent genesis VM
	GenesisVM *GenesisVM

	// reference back to the parent linker
	Linker *Linker

	// A check to make sure this swizzle was successful
	SwizzleSuccessful bool

	// Any errors from the swizzling of this function
	SwizzleError error

	// Signature is a saved version of the golang's method signature
	Signature string
}

LinkedFunction is the type that represents the gscript <=> golang native binding so proper interfaces can be generated at compile time for calling native go from the genesis VM.

func (*LinkedFunction) CanResolveImportDep added in v0.1.1

func (l *LinkedFunction) CanResolveImportDep(pkg string) (string, error)

CanResolveImportDep takes a package string and compares it against the linked functions known import table to determine if the referenced namespace is declared in the golang AST as a referenced sub-type

func (*LinkedFunction) GenerateArgString added in v0.1.1

func (l *LinkedFunction) GenerateArgString(prefix string) string

GenerateArgString generates a golang argument string to use in the interface code based on the number of arguments required for the supplied function

func (*LinkedFunction) GenerateReturnString added in v0.1.1

func (l *LinkedFunction) GenerateReturnString(prefix string) string

GenerateReturnString generates a golang return signature to use in the interface code

func (*LinkedFunction) SwizzleToTheLeft added in v0.1.1

func (l *LinkedFunction) SwizzleToTheLeft() error

SwizzleToTheLeft enumerates the function arguments of both the caller and the native function to build a structured list of parameters and their types. It also compares the caller argument signature and throws an error if the caller is providing an incompatible number of arguments.

func (*LinkedFunction) SwizzleToTheRight added in v0.1.1

func (l *LinkedFunction) SwizzleToTheRight() error

SwizzleToTheRight enumerates the function returns of the native function to build a structured list of the return value types. This is then used by the linker to generate a special wrapper to allow multiple return values to be returned in single value context (required by javascript)

type Linker added in v0.1.1

type Linker struct {
	sync.RWMutex

	// a reference to the parent genesis VM
	VM *GenesisVM

	// mapping of function name to the linked function object used during generation
	Funcs map[string]*LinkedFunction

	// mapping of type definitions to be linked into the engine
	Types map[string]*GoStructDef

	// MaskedImports map a golang dependency by import path into a translated path
	MaskedImports map[string]*MaskedImport
}

Linker holds the maps between functions called from the genesis script and their associated golang equivalent, including package references. The linker will use this mapping to generate import shims for each golang public golang function called.

func NewLinker added in v0.1.1

func NewLinker(vm *GenesisVM) *Linker

NewLinker creates a new linker for the given genesis VM

func (*Linker) NewLinkedFunction added in v0.1.1

func (l *Linker) NewLinkedFunction(fnName string, caller *FunctionCall, file *ast.File, godecl *ast.FuncDecl, imports []*ast.ImportSpec, gopkg *GoPackage) (*LinkedFunction, error)

NewLinkedFunction creates a function mapping in the VMs linker between golang AST function declearations and genesis AST function calls so the compiler can build the function interfaces between the virtual machine and the native golang package

func (*Linker) SuccessfullyLinkedFuncs added in v0.1.1

func (l *Linker) SuccessfullyLinkedFuncs() []*LinkedFunction

SuccessfullyLinkedFuncs is a helper method for the linker for any edge case packages to the standard library (asset for example)

type Macro added in v0.1.1

type Macro struct {
	// the genesis compiler option's name for this macro
	Key string

	// map of the values associated with this macro
	Params map[string]string
}

Macro defines the object created by each macro parsed by the preprocessor

func ScanForMacros added in v0.1.1

func ScanForMacros(commentMap gast.CommentMap) []*Macro

ScanForMacros takes the genesis comments from the AST and parses known macros out for the compiler

type MaskedImport added in v0.1.1

type MaskedImport struct {
	// ImportPath of the masked Import
	ImportPath string

	// OldAlias represents the alias in the target package source
	OldAlias string

	// NewAlias represents the aliased package name in the intermediate representation
	NewAlias string
}

MaskedImport is used to separate import namespaces within the intermediate representation

func NewMaskedImport added in v0.1.1

func NewMaskedImport(ip, oa string) *MaskedImport

NewMaskedImport creates a new import mask based on an import path and old alias

Directories

Path Synopsis
Package computil Code generated by go-bindata.
Package computil Code generated by go-bindata.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL