<commit_before>﻿using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Collections;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace MonoScript
{
    public class ScriptModuleBuilder
    {
        private readonly CSharpCodeCompiler _compiler;

        private readonly List<string> _sources;

        private readonly CompilerParameters _options;

        public void AddFromFile(string fileName)
        {
            _sources.Add(File.ReadAllText(fileName));
        }

        public void AddSource(string code)
        {
            _sources.Add(code);
        }

        public void AddFromStream(Stream code)
        {
            using (var reader = new StreamReader(code))
            {
                _sources.Add(reader.ReadToEnd());
            }
        }

        public void ImportType<T>()
        {
            ImportType(typeof(T));
        }

        public void ImportType(Type type)
        {
            _compiler.ImportType(type);
        }

        public void ImportTypes(IEnumerable<Type> types)
        {
            _compiler.ImportTypes(types);
        }

        public void ClearImports()
        {
            _compiler.ClearImports();
            _options.ReferencedAssemblies.Clear();
        }

        public void ImportBuiltinTypes()
        {
            _compiler.ImportTypes(BuiltinTypes);
        }

        public void ImportNamespace(string name)
        {
            ImportTypes(AppDomain.CurrentDomain.GetAssemblies()
                .SelectMany(t => t.GetTypes())
                .Where(t => t.Namespace == name));
        }

        public void ImportAssembly(Assembly asm)
        {
            ImportTypes(asm.GetTypes());
            if (asm.IsDynamic) return;
            _options.ReferencedAssemblies.Add(asm.Location);
        }

        public ScriptModuleBuilder()
        {
            _compiler = new CSharpCodeCompiler();
            _sources = new List<string>();
            _options = new CompilerParameters
            {
                GenerateExecutable = false,
                GenerateInMemory = true,
            };
        }
        
        public ScriptModule Build()
        {
            var result = _compiler.CompileAssemblyFromSourceBatch(_options, _sources.ToArray());

            foreach (var error in result.Errors)
                Console.Error.WriteLine(error);

            _sources.Clear();
            ClearImports();
            
            return new ScriptModule(result.CompiledAssembly);
        }
        
        private static readonly Type[] BuiltinTypes =
        {
            typeof(object),
            typeof(ValueType),
            typeof(Attribute),
            
            typeof(InAttribute),
            typeof(OutAttribute),
            typeof(ExtensionAttribute),
            typeof(ParamArrayAttribute),

            typeof(int),
            typeof(long),
            typeof(uint),
            typeof(ulong),
            typeof(byte),
            typeof(sbyte),
            typeof(short),
            typeof(ushort),

            typeof(IEnumerator),
            typeof(IEnumerable),
            typeof(IDisposable),

            typeof(char),
            typeof(string),
            typeof(float),
            typeof(double),
            typeof(decimal),
            typeof(bool),
            typeof(IntPtr),
            typeof(UIntPtr),

            typeof(MulticastDelegate),
            typeof(Delegate),
            typeof(Enum),
            typeof(Array),
            typeof(void),
            typeof(Type),
            typeof(Exception),
            typeof(RuntimeFieldHandle),
            typeof(RuntimeTypeHandle),
        };
    }
}
<commit_msg>ImportAssembly just calls ImportTypes, there shouldn't actually be a need to manually add to ReferencedAssemblies<commit_after>﻿using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Collections;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace MonoScript
{
    public class ScriptModuleBuilder
    {
        private readonly CSharpCodeCompiler _compiler;

        private readonly List<string> _sources;

        private readonly CompilerParameters _options;

        public void AddFromFile(string fileName)
        {
            _sources.Add(File.ReadAllText(fileName));
        }

        public void AddSource(string code)
        {
            _sources.Add(code);
        }

        public void AddFromStream(Stream code)
        {
            using (var reader = new StreamReader(code))
            {
                _sources.Add(reader.ReadToEnd());
            }
        }

        public void ImportType<T>()
        {
            ImportType(typeof(T));
        }

        public void ImportType(Type type)
        {
            _compiler.ImportType(type);
        }

        public void ImportTypes(IEnumerable<Type> types)
        {
            _compiler.ImportTypes(types);
        }

        public void ClearImports()
        {
            _compiler.ClearImports();
            _options.ReferencedAssemblies.Clear();
        }

        public void ImportBuiltinTypes()
        {
            _compiler.ImportTypes(BuiltinTypes);
        }

        public void ImportNamespace(string name)
        {
            ImportTypes(AppDomain.CurrentDomain.GetAssemblies()
                .SelectMany(t => t.GetTypes())
                .Where(t => t.Namespace == name));
        }

        public void ImportAssembly(Assembly asm)
        {
            ImportTypes(asm.GetTypes());
        }

        public ScriptModuleBuilder()
        {
            _compiler = new CSharpCodeCompiler();
            _sources = new List<string>();
            _options = new CompilerParameters
            {
                GenerateExecutable = false,
                GenerateInMemory = true,
            };
        }
        
        public ScriptModule Build()
        {
            var result = _compiler.CompileAssemblyFromSourceBatch(_options, _sources.ToArray());

            foreach (var error in result.Errors)
                Console.Error.WriteLine(error);

            _sources.Clear();
            ClearImports();
            
            return new ScriptModule(result.CompiledAssembly);
        }
        
        private static readonly Type[] BuiltinTypes =
        {
            typeof(object),
            typeof(ValueType),
            typeof(Attribute),
            
            typeof(InAttribute),
            typeof(OutAttribute),
            typeof(ExtensionAttribute),
            typeof(ParamArrayAttribute),

            typeof(int),
            typeof(long),
            typeof(uint),
            typeof(ulong),
            typeof(byte),
            typeof(sbyte),
            typeof(short),
            typeof(ushort),

            typeof(IEnumerator),
            typeof(IEnumerable),
            typeof(IDisposable),

            typeof(char),
            typeof(string),
            typeof(float),
            typeof(double),
            typeof(decimal),
            typeof(bool),
            typeof(IntPtr),
            typeof(UIntPtr),

            typeof(MulticastDelegate),
            typeof(Delegate),
            typeof(Enum),
            typeof(Array),
            typeof(void),
            typeof(Type),
            typeof(Exception),
            typeof(RuntimeFieldHandle),
            typeof(RuntimeTypeHandle),
        };
    }
}

{'fraction_non_alphanumeric': 0.08666144609762465, 'fraction_numerical': 0.0, 'mean_word_length': 1.5897262588712402, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4419089', 'n_tokens_mistral': 1863, 'n_tokens_neox': 1810, 'n_words': 354}