<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using ICSharpCode.NRefactory.TypeSystem;
using ICSharpCode.NRefactory.TypeSystem.Implementation;
using Saltarelle.Compiler;
using Saltarelle.Compiler.Compiler;
using Saltarelle.Compiler.JSModel;
using Saltarelle.Compiler.JSModel.Expressions;
using Saltarelle.Compiler.ScriptSemantics;

namespace CoreLib.Plugin {
	public class RuntimeLibrary : IRuntimeLibrary {
		private enum TypeContext {
			TypeOf,
			GetScriptType,
			CastTarget,
			GenericArgument,
		}

		private readonly ITypeReference _systemScript = ReflectionHelper.ParseReflectionName("System.Script");

		private readonly IMetadataImporter _metadataImporter;
		private readonly IErrorReporter _errorReporter;
		private readonly ICompilation _compilation;
		private readonly bool _omitDowncasts;
		private readonly bool _omitNullableChecks;

		public RuntimeLibrary(IMetadataImporter metadataImporter, IErrorReporter errorReporter, ICompilation compilation) {
			_metadataImporter = metadataImporter;
			_errorReporter = errorReporter;
			_compilation = compilation;
			_omitDowncasts = MetadataUtils.OmitDowncasts(compilation);
			_omitNullableChecks = MetadataUtils.OmitNullableChecks(compilation);
		}

		private JsTypeReferenceExpression CreateTypeReferenceExpression(ITypeDefinition td) {
			return new JsTypeReferenceExpression(td);
		}

		private JsTypeReferenceExpression CreateTypeReferenceExpression(ITypeReference tr) {
			return new JsTypeReferenceExpression(tr.Resolve(_compilation).GetDefinition());
		}

		private JsExpression GetTypeDefinitionScriptType(ITypeDefinition type, TypeContext context) {
			if (MetadataUtils.IsSerializable(type) && (context == TypeContext.CastTarget)) {
				return null;
			}
			else if (context != TypeContext.GetScriptType && context != TypeContext.TypeOf && !MetadataUtils.DoesTypeObeyTypeSystem(type)) {
				return CreateTypeReferenceExpression(KnownTypeReference.Object);
			}
			else {
				return CreateTypeReferenceExpression(type);
			}
		}

		private JsExpression GetScriptType(IType type, TypeContext context, Func<ITypeParameter, JsExpression> resolveTypeParameter) {
			if (type.Kind == TypeKind.Delegate) {
				return CreateTypeReferenceExpression(KnownTypeReference.Delegate);
			}
			else if (type is ParameterizedType) {
				var pt = (ParameterizedType)type;<commit_msg>Treat [Imported, Serializable] types as Object in all occasions, including typeof.<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using ICSharpCode.NRefactory.TypeSystem;
using ICSharpCode.NRefactory.TypeSystem.Implementation;
using Saltarelle.Compiler;
using Saltarelle.Compiler.Compiler;
using Saltarelle.Compiler.JSModel;
using Saltarelle.Compiler.JSModel.Expressions;
using Saltarelle.Compiler.ScriptSemantics;

namespace CoreLib.Plugin {
	public class RuntimeLibrary : IRuntimeLibrary {
		private enum TypeContext {
			TypeOf,
			GetScriptType,
			CastTarget,
			GenericArgument,
		}

		private readonly ITypeReference _systemScript = ReflectionHelper.ParseReflectionName("System.Script");

		private readonly IMetadataImporter _metadataImporter;
		private readonly IErrorReporter _errorReporter;
		private readonly ICompilation _compilation;
		private readonly bool _omitDowncasts;
		private readonly bool _omitNullableChecks;

		public RuntimeLibrary(IMetadataImporter metadataImporter, IErrorReporter errorReporter, ICompilation compilation) {
			_metadataImporter = metadataImporter;
			_errorReporter = errorReporter;
			_compilation = compilation;
			_omitDowncasts = MetadataUtils.OmitDowncasts(compilation);
			_omitNullableChecks = MetadataUtils.OmitNullableChecks(compilation);
		}

		private JsTypeReferenceExpression CreateTypeReferenceExpression(ITypeDefinition td) {
			return new JsTypeReferenceExpression(td);
		}

		private JsTypeReferenceExpression CreateTypeReferenceExpression(ITypeReference tr) {
			return new JsTypeReferenceExpression(tr.Resolve(_compilation).GetDefinition());
		}

		private JsExpression GetTypeDefinitionScriptType(ITypeDefinition type, TypeContext context) {
			if (MetadataUtils.IsSerializable(type) && (context == TypeContext.CastTarget)) {
				return null;
			}
			else if (context != TypeContext.GetScriptType && context != TypeContext.TypeOf && !MetadataUtils.DoesTypeObeyTypeSystem(type)) {
				return CreateTypeReferenceExpression(KnownTypeReference.Object);
			}
			else if (MetadataUtils.IsSerializable(type) && !MetadataUtils.DoesTypeObeyTypeSystem(type)) {
				return CreateTypeReferenceExpression(KnownTypeReference.Object);
			}
			else {
				return CreateTypeReferenceExpression(type);
			}
		}

		private JsExpression GetScriptType(IType type, TypeContext context, Func<ITypeParameter, JsExpression> resolveTypeParameter) {
			if (type.Kind == TypeKind.Delegate) {
				return CreateTypeReferenceExpression(KnownTypeReference.Delegate);
			}
			else if (type is ParameterizedType) {
				var pt = (ParameterizedType)type;
{'fraction_non_alphanumeric': 0.07291666666666667, 'fraction_numerical': 0.0, 'mean_word_length': 6.236231884057971, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '5550344', 'n_tokens_mistral': 1524, 'n_tokens_neox': 1337, 'n_words': 285}