Beispiel #1
0
        public object Sleep(bool startRepl = true)
        {
            Sleeping = true;
            Module stdIn;

            return(NativeModules.TryGetValue("stdin", out stdIn) ? stdIn.Exports : null);
        }
        public string Resolve(string request, Module requester)
        {
            Module module;

            if (NativeModules.TryGetValue(request, out module))
            {
                return(module.FileName);
            }

            var virtualPath = PathResolver.Resolve(request, requester);

            return(virtualPath?.ResolvePath());
        }
        public Module Load(string request, Module parent, bool isMain)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            if (parent != null && parent.Loader != this)
            {
                throw new InvalidOperationException("Parent module is initialized from a different loader.");
            }

            Module module;

            if (NativeModules.TryGetValue(request, out module))
            {
                return(module);
            }

            var virtualPath = PathResolver.Resolve(request, parent);
            var identifier  = virtualPath?.Identifier;

            if (identifier == null)
            {
                throw new InvalidOperationException($"Module '{request}' not found.");
            }

            if (LoadedModules.TryGetValue(identifier, out module))
            {
                return(module);
            }

            module = new Module(identifier, virtualPath, parent, this);
            var oldMain = MainModule;

            if (isMain)
            {
                MainModule = module;
            }

            var extension = virtualPath.ResolveExtension();

            IModuleCompiler compiler;

            if (!Compilers.TryGetValue(extension, out compiler))
            {
                throw new InvalidOperationException($"Invalid file extension {extension}.");
            }

            LoadedModules[identifier] = module;
            try
            {
                compiler.Compile(module);
            }
            catch
            {
                LoadedModules.Remove(identifier);
                if (isMain)
                {
                    MainModule = oldMain;
                }

                throw;
            }

            return(module);
        }