Пример #1
0
        private IodineObject require(VirtualMachine vm, IodineObject self, IodineObject[] args)
        {
            if (args.Length < 1)
            {
                vm.RaiseException(new IodineArgumentException(1));
                return(null);
            }

            IodineString path = args [0] as IodineString;

            if (path == null)
            {
                vm.RaiseException(new IodineTypeException("Str"));
                return(null);
            }

            string name     = path.Value;
            string fullPath = Path.GetFullPath(name);

            if (args.Length == 1)
            {
                if (VirtualMachine.ModuleCache.ContainsKey(fullPath))
                {
                    IodineModule module = VirtualMachine.ModuleCache [fullPath];
                    vm.Top.Module.SetAttribute(vm, Path.GetFileNameWithoutExtension(fullPath),
                                               module);
                }
                else
                {
                    IodineModule module = vm.LoadModule(name);
                    vm.Top.Module.SetAttribute(vm, Path.GetFileNameWithoutExtension(
                                                   fullPath), module);
                    VirtualMachine.ModuleCache [fullPath] = module;
                    module.Initializer.Invoke(vm, new IodineObject[] { });
                }
            }
            else
            {
                IodineTuple names = args [1] as IodineTuple;
                if (names == null)
                {
                    vm.RaiseException(new IodineTypeCastException("Tuple"));
                    return(null);
                }
                IodineModule module = null;

                if (VirtualMachine.ModuleCache.ContainsKey(fullPath))
                {
                    module = VirtualMachine.ModuleCache [fullPath];
                }
                else
                {
                    module = vm.LoadModule(name);
                    VirtualMachine.ModuleCache [fullPath] = module;
                    module.Initializer.Invoke(vm, new IodineObject[] { });
                }

                vm.Top.Module.SetAttribute(vm, Path.GetFileNameWithoutExtension(fullPath),
                                           module);

                if (names.Objects.Length > 0)
                {
                    foreach (IodineObject item in names.Objects)
                    {
                        vm.Top.Module.SetAttribute(vm, item.ToString(),
                                                   module.GetAttribute(item.ToString()));
                    }
                }
                else
                {
                    foreach (KeyValuePair <string, IodineObject> kv in module.Attributes)
                    {
                        vm.Top.Module.SetAttribute(vm, kv.Key, kv.Value);
                    }
                }
            }
            return(null);
        }
Пример #2
0
        IodineObject Require(VirtualMachine vm, IodineObject self, IodineObject [] args)
        {
            if (args.Length < 1)
            {
                vm.RaiseException(new IodineArgumentException(1));
                return(IodineNull.Instance);
            }

            var path = args [0] as IodineString;

            if (path == null)
            {
                vm.RaiseException(new IodineTypeException("Str"));
                return(IodineNull.Instance);
            }

            string name = path.Value;

            var fullPath = Path.GetFullPath(name);

            if (args.Length == 1)
            {
                // use <module>
                if (VirtualMachine.ModuleCache.ContainsKey(fullPath))
                {
                    IodineModule module = VirtualMachine.ModuleCache [fullPath];
                    vm.Top.StoreLocal(Path.GetFileNameWithoutExtension(fullPath), module);
                }
                else
                {
                    var module = vm.LoadModule(name);
                    vm.Top.StoreLocal(Path.GetFileNameWithoutExtension(fullPath), module);

                    VirtualMachine.ModuleCache [fullPath] = module;

                    if (module.Initializer != null)
                    {
                        module.Invoke(vm, new IodineObject [] { });
                    }
                }
            }
            else
            {
                // use <types> from <module>
                var names = args [1] as IodineTuple;
                if (names == null)
                {
                    vm.RaiseException(new IodineTypeCastException("Tuple"));
                    return(IodineNull.Instance);
                }
                IodineModule module = null;

                if (VirtualMachine.ModuleCache.ContainsKey(fullPath))
                {
                    module = VirtualMachine.ModuleCache [fullPath];
                }
                else
                {
                    module = vm.LoadModule(name);
                    VirtualMachine.ModuleCache [fullPath] = module;
                    if (module.Initializer != null)
                    {
                        module.Invoke(vm, new IodineObject [] { });
                    }
                }

                vm.Top.StoreLocal(Path.GetFileNameWithoutExtension(fullPath), module);

                if (names.Objects.Length > 0)
                {
                    foreach (IodineObject item in names.Objects)
                    {
                        vm.Top.StoreLocal(
                            item.ToString(),
                            module.GetAttribute(item.ToString())
                            );
                    }
                }
                else
                {
                    foreach (KeyValuePair <string, IodineObject> kv in module.Attributes)
                    {
                        vm.Top.StoreLocal(kv.Key, kv.Value);
                    }
                }
            }
            return(IodineNull.Instance);
        }