// Module 获取模块实例
340089
func (s *Application) Module(args ...string) *Module {
	name := s.RootModuleName
	if len(args) > 0 {
		name = args[0]
		if ap, ok := s.moduleNames[name]; ok {
			return ap
		}
	}
	return s.NewModule(name)
}
1294033
func (t *Tree) Modules() []*Module {
	result := make([]*Module, len(t.config.Modules))
	for i, m := range t.config.Modules {
		result[i] = &Module{
			Name:      m.Name,
			Version:   m.Version,
			Source:    m.Source,
			Providers: m.Providers,
		}
	}

	return result
}
473976
func ActiveModule(local *config.LocalConfig, moduleKind ModuleKind) string {
	var (
		modulesValue = reflect.ValueOf(local).Elem().FieldByName("Modules")
		modulesType  = modulesValue.Type()
		numField     = modulesType.NumField()
		kind         = string(moduleKind)
	)
	for i := 0; i < numField; i++ {
		var (
			fieldValue = modulesValue.Field(i)
			fieldType  = modulesType.Field(i)
		)
		if fieldType.Tag.Get("json") == kind {
			return fieldValue.Interface().(string)
		}
	}

	return ""
}
995191
func Module(id string) ModuleLoader {
	globalMu.RLock()
	defer globalMu.RUnlock()

	return globalModules[id]
}
501997
func (s *State) AddModule(path addrs.ModuleInstance) *ModuleState {
	s.Lock()
	defer s.Unlock()

	return s.addModule(path)
}
262670
func (m Module) Function(name string) (Function, error) {
	var fn Function
	cstr := C.CString(name)
	defer C.free(unsafe.Pointer(cstr))
	err := result(C.cuModuleGetFunction(&fn.fn, m.mod, cstr))
	return fn, err
}
25426
func GetModule(name string) (interfaces.Module, error) {
	if m, ok := modulesMap[name]; ok {
		return m, nil
	}
	return nil, fmt.Errorf("module with name %s not found", name)
}
958872
func RegisterModule(module interfaces.Module, name string) error {
	return RegisterModuleAfter(module, name)
}
237555
func loModule(L *LState) int {
	name := L.CheckString(1)
	loaded := L.GetField(L.Get(RegistryIndex), "_LOADED")
	tb := L.GetField(loaded, name)
	if _, ok := tb.(*LTable); !ok {
		tb = L.FindTable(L.Get(GlobalsIndex).(*LTable), name, 1)
		if tb == LNil {
			L.RaiseError("name conflict for module: %v", name)
		}
		L.SetField(loaded, name, tb)
	}
	if L.GetField(tb, "_NAME") == LNil {
		L.SetField(tb, "_M", tb)
		L.SetField(tb, "_NAME", LString(name))
		names := strings.Split(name, ".")
		pname := ""
		if len(names) > 1 {
			pname = strings.Join(names[:len(names)-1], ".") + "."
		}
		L.SetField(tb, "_PACKAGE", LString(pname))
	}

	caller := L.currentFrame.Parent
	if caller == nil {
		L.RaiseError("no calling stack.")
	} else if caller.Fn.IsG {
		L.RaiseError("module() can not be called from GFunctions.")
	}
	L.SetFEnv(caller.Fn, tb)

	top := L.GetTop()
	for i := 2; i <= top; i++ {
		L.Push(L.Get(i))
		L.Push(tb)
		L.Call(1, 0)
	}
	L.Push(tb)
	return 1
}
920101
func (m Module) Parent() Module {
	if len(m) == 0 {
		return m
	}
	return m[:len(m)-1]
}
250815
func (s Storage) recordModule(rec moduleRecord) error {
	manifest, err := s.loadManifest()
	if err != nil {
		// if there was a problem with the file, we will attempt to write a new
		// one. Any non-data related error should surface there.
		log.Printf("[WARN] error reading module manifest: %s", err)
	}

	// do nothing if we already have the exact module
	for i, stored := range manifest.Modules {
		if rec == stored {
			return nil
		}

		// they are not equal, but if the storage path is the same we need to
		// remove this rec to be replaced.
		if rec.Dir == stored.Dir {
			manifest.Modules[i] = manifest.Modules[len(manifest.Modules)-1]
			manifest.Modules = manifest.Modules[:len(manifest.Modules)-1]
			break
		}
	}

	manifest.Modules = append(manifest.Modules, rec)

	js, err := json.Marshal(manifest)
	if err != nil {
		panic(err)
	}

	manifestPath := filepath.Join(s.StorageDir, manifestName)
	return ioutil.WriteFile(manifestPath, js, 0644)
}
75851
func (s *State) EnsureModule(addr addrs.ModuleInstance) *Module {
	ms := s.Module(addr)
	if ms == nil {
		ms = NewModule(addr)
		s.Modules[addr.String()] = ms
	}
	return ms
}
389556
func loadModules() {
	keys := []string{}
	for _, key := range Config.Options("module.") {
		keys = append(keys, key)
	}

	// Reorder module order by key name, a poor mans sort but at least it is consistent
	sort.Strings(keys)
	for _, key := range keys {
		moduleLog.Debug("Sorted keys", "keys", key)

	}
	for _, key := range keys {
		moduleImportPath := Config.StringDefault(key, "")
		if moduleImportPath == "" {
			continue
		}

		modulePath, err := ResolveImportPath(moduleImportPath)
		if err != nil {
			moduleLog.Error("Failed to load module.  Import of path failed", "modulePath", moduleImportPath, "error", err)
		}
		// Drop anything between module.???.<name of module>
		subKey := key[len("module."):]
		if index := strings.Index(subKey, "."); index > -1 {
			subKey = subKey[index+1:]
		}
		addModule(subKey, moduleImportPath, modulePath)
	}

	// Modules loaded, now show module path
	for key, callback := range appModule.initializedModules {
		if m := ModuleFromPath(key, false); m != nil {
			callback(m)
		} else {
			RevelLog.Error("Callback for non registered module initializing with application module", "modulePath", key)
			callback(appModule)
		}
	}
}
919685
func (s Storage) findModule(key string) (string, error) {
	if s.Mode == GetModeUpdate {
		return "", nil
	}

	return s.moduleDir(key)
}
334214
func (s *Application) Modules(args ...bool) map[string]*Module {
	if len(args) > 0 && args[0] {
		return s.moduleHosts
	}
	return s.moduleNames
}
538471
func (c compiler) load(p string) (*Module, error) {
	p, err := c.fs.Abs(p)
	if err != nil {
		return nil, err
	}

	if m, ok := c.Modules[p]; ok {
		// Already loaded.
		return m, nil
	}

	s, err := c.fs.Read(p)
	if err != nil {
		return nil, fileReadError{Path: p, Reason: err}
	}

	prog, err := idl.Parse(s)
	if err != nil {
		return nil, parseError{Path: p, Reason: err}
	}

	m := &Module{
		Name:       fileBaseName(p),
		ThriftPath: p,
		Includes:   make(map[string]*IncludedModule),
		Constants:  make(map[string]*Constant),
		Types:      make(map[string]TypeSpec),
		Services:   make(map[string]*ServiceSpec),
	}

	m.Raw = s
	c.Modules[p] = m
	// the module is added to the map before processing includes to break
	// cyclic includes.

	if err := c.gather(m, prog); err != nil {
		return nil, fileCompileError{Path: p, Reason: err}
	}
	return m, nil
}
773495
func WriteModule(w io.Writer, module *module.Module) error {

	if err := writeMagic(w); err != nil {
		return err
	}

	if err := writeVersion(w); err != nil {
		return err
	}

	if module == nil {
		return nil
	}

	if err := writeTypeSection(w, module.Type); err != nil {
		return err
	}

	if err := writeImportSection(w, module.Import); err != nil {
		return err
	}

	if err := writeFunctionSection(w, module.Function); err != nil {
		return err
	}

	if err := writeTableSection(w, module.Table); err != nil {
		return err
	}

	if err := writeGlobalSection(w, module.Global); err != nil {
		return err
	}

	if err := writeExportSection(w, module.Export); err != nil {
		return err
	}

	if err := writeRawCodeSection(w, module.Code); err != nil {
		return err
	}

	if err := writeDataSection(w, module.Data); err != nil {
		return err
	}

	return nil
}