private IodineObject Eval(VirtualMachine host, string source, IodineDictionary dict)
        {
            VirtualMachine vm = host;

            IodineContext context = host.Context;

            if (dict != null)
            {
                context = new IodineContext();
                context.Globals.Clear();

                vm = new VirtualMachine(host.Context);

                foreach (IodineObject key in dict.Keys)
                {
                    context.Globals [key.ToString()] = dict.Get(key);
                }
            }

            SourceUnit   code   = SourceUnit.CreateFromSource(source);
            IodineModule module = null;

            try {
                module = code.Compile(context);
            } catch (SyntaxException ex) {
                vm.RaiseException(new IodineSyntaxException(ex.ErrorLog));
                return(IodineNull.Instance);
            }
            return(module.Invoke(vm, new IodineObject[] { }));
        }
Exemple #2
0
        private IodineObject LoadModule(VirtualMachine vm, IodineObject self, IodineObject[] args)
        {
            if (args.Length < 1)
            {
                vm.RaiseException(new IodineArgumentException(1));
                return(null);
            }
            IodineString pathStr = args [0] as IodineString;
            IodineModule module  = vm.Context.LoadModule(pathStr.Value);

            module.Invoke(vm, new IodineObject[] { });
            return(module);
        }
        private IodineObject Require(VirtualMachine vm, IodineObject self, IodineObject[] args)
        {
            if (args.Length < 1)
            {
                vm.RaiseException(new IodineArgumentException(1));
                return(IodineNull.Instance);
            }

            IodineString path = args [0] as IodineString;

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

            string name     = path.Value;
            string 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
                {
                    IodineModule 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>
                IodineTuple 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);
        }