Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions src/core/Microsoft.Scripting/ErrorSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,26 @@
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.

#nullable enable

using System.Threading;
using Microsoft.Scripting.Utils;

namespace Microsoft.Scripting {

public class ErrorSink {
public static readonly ErrorSink/*!*/ Default = new();
public static readonly ErrorSink Default = new();

public static readonly ErrorSink/*!*/ Null = new NullErrorSink();
public static readonly ErrorSink Null = new NullErrorSink();

protected ErrorSink() {
}

public virtual void Add(SourceUnit source, string/*!*/ message, SourceSpan span, int errorCode, Severity severity) {
/// <summary>
/// Reports an error against a source unit. Use the <see cref="Add(string, string?, string?, string?, SourceSpan, int, Severity)"/>
/// overload when no <see cref="SourceUnit"/> is available (e.g. remote execution).
/// </summary>
public virtual void Add(SourceUnit source, string message, SourceSpan span, int errorCode, Severity severity) {
if (severity == Severity.FatalError || severity == Severity.Error) {
throw new SyntaxErrorException(message, source, span, errorCode, severity);
}
Expand All @@ -25,7 +31,7 @@ public virtual void Add(SourceUnit source, string/*!*/ message, SourceSpan span,
/// This overload will be called when a SourceUnit is not available. This can happen if the code is being executed remotely,
/// since SourceUnit cannot be marshaled across AppDomains.
/// </summary>
public virtual void Add(string message, string path, string code, string line, SourceSpan span, int errorCode, Severity severity) {
public virtual void Add(string message, string? path, string? code, string? line, SourceSpan span, int errorCode, Severity severity) {
if (severity == Severity.FatalError || severity == Severity.Error) {
throw new SyntaxErrorException(message, path, code, line, span, errorCode, severity);
}
Expand All @@ -34,12 +40,12 @@ public virtual void Add(string message, string path, string code, string line, S

internal sealed class NullErrorSink : ErrorSink {

public override void Add(SourceUnit source, string/*!*/ message, SourceSpan span, int errorCode, Severity severity) {
public override void Add(SourceUnit source, string message, SourceSpan span, int errorCode, Severity severity) {
}
}

public class ErrorCounter : ErrorSink {
private readonly ErrorSink/*!*/ _sink;
private readonly ErrorSink _sink;

private int _fatalErrorCount;
private int _errorCount;
Expand All @@ -63,15 +69,15 @@ public bool AnyError {
}
}

public ErrorCounter()
public ErrorCounter()
: this(ErrorSink.Null) {
}

public ErrorCounter(ErrorSink/*!*/ sink) {
public ErrorCounter(ErrorSink sink) {
ContractUtils.RequiresNotNull(sink, nameof(sink));
_sink = sink;
}

protected virtual void CountError(Severity severity) {
if (severity == Severity.FatalError) Interlocked.Increment(ref _fatalErrorCount);
else if (severity == Severity.Error) Interlocked.Increment(ref _errorCount);
Expand All @@ -82,7 +88,7 @@ public void ClearCounters() {
_warningCount = _errorCount = _fatalErrorCount = 0;
}

public override void Add(SourceUnit source, string/*!*/ message, SourceSpan span, int errorCode, Severity severity) {
public override void Add(SourceUnit source, string message, SourceSpan span, int errorCode, Severity severity) {
CountError(severity);
_sink.Add(source, message, span, errorCode, severity);
}
Expand Down
11 changes: 6 additions & 5 deletions src/core/Microsoft.Scripting/Hosting/ExceptionOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.

#nullable enable

using System;
using System.Collections.Generic;

Expand Down Expand Up @@ -46,15 +48,15 @@ public string FormatException(ObjectHandle exception) {
var exceptionObj = exception.Unwrap() as Exception;
ContractUtils.Requires(exceptionObj is not null, nameof(exception), "ObjectHandle must be to Exception object");

return _context.FormatException(exceptionObj);
return _context.FormatException(exceptionObj!);
}

public void GetExceptionMessage(ObjectHandle exception, out string message, out string errorTypeName) {
ContractUtils.RequiresNotNull(exception, nameof(exception));
var exceptionObj = exception.Unwrap() as Exception;
ContractUtils.Requires(exceptionObj is not null, nameof(exception), "ObjectHandle must be to Exception object");

_context.GetExceptionMessage(exceptionObj, out message, out errorTypeName);
_context.GetExceptionMessage(exceptionObj!, out message, out errorTypeName);
}

public bool HandleException(ObjectHandle exception) {
Expand All @@ -69,12 +71,11 @@ public IList<DynamicStackFrame> GetStackFrames(ObjectHandle exception) {
var exceptionObj = exception.Unwrap() as Exception;
ContractUtils.Requires(exceptionObj is not null, nameof(exception), "ObjectHandle must be to Exception object");

return _context.GetStackFrames(exceptionObj);
return _context.GetStackFrames(exceptionObj!);
}

// TODO: Figure out what is the right lifetime
public override object InitializeLifetimeService() {
return null;
return base.InitializeLifetimeService();
}
#endif
}
Expand Down
1 change: 1 addition & 0 deletions src/core/Microsoft.Scripting/Microsoft.Scripting.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<EmitCompilerGeneratedFiles>True</EmitCompilerGeneratedFiles>
<MeziantouPolyfill_IncludedPolyfills>
T:System.Diagnostics.CodeAnalysis.MaybeNullAttribute;
T:System.Diagnostics.CodeAnalysis.MaybeNullWhenAttribute;
T:System.Diagnostics.CodeAnalysis.MemberNotNullWhenAttribute;
T:System.Diagnostics.CodeAnalysis.NotNullAttribute;
T:System.Diagnostics.CodeAnalysis.NotNullWhenAttribute;
Expand Down
35 changes: 19 additions & 16 deletions src/core/Microsoft.Scripting/Runtime/DlrConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.

#nullable enable

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using System.Threading;
Expand All @@ -17,9 +20,9 @@ namespace Microsoft.Scripting.Runtime {
/// </summary>
internal sealed class LanguageConfiguration {
private readonly IDictionary<string, object> _options;
private LanguageContext _context;
private LanguageContext? _context;

public LanguageContext LanguageContext => _context;
public LanguageContext? LanguageContext => _context;

public AssemblyQualifiedTypeName ProviderName { get; }

Expand All @@ -41,32 +44,32 @@ internal LanguageContext LoadLanguageContext(ScriptDomainManager domainManager,
// Let assembly load errors bubble out
var assembly = domainManager.Platform.LoadAssembly(ProviderName.AssemblyName.FullName);

Type type = assembly.GetType(ProviderName.TypeName);
Type? type = assembly.GetType(ProviderName.TypeName);
if (type is null) {
throw new InvalidOperationException(
String.Format(
"Failed to load language '{0}': assembly '{1}' does not contain type '{2}'",
DisplayName,
DisplayName,
#if FEATURE_FILESYSTEM
assembly.Location,
#else
assembly.FullName,
#endif
ProviderName.TypeName
ProviderName.TypeName
));
}

if (!type.IsSubclassOf(typeof(LanguageContext))) {
throw new InvalidOperationException(
$"Failed to load language '{DisplayName}': type '{type}' is not a valid language provider because it does not inherit from LanguageContext");
$"Failed to load language '{DisplayName}': type '{type}' is not a valid language provider because it does not inherit from LanguageContext");
}

LanguageContext context;
try {
context = (LanguageContext)Activator.CreateInstance(type, new object[] { domainManager, _options });
context = (LanguageContext)Activator.CreateInstance(type, new object[] { domainManager, _options })!;
} catch (TargetInvocationException e) {
throw new TargetInvocationException(
$"Failed to load language '{DisplayName}': {e.InnerException.Message}",
$"Failed to load language '{DisplayName}': {e.InnerException?.Message}",
e.InnerException
);
} catch (Exception e) {
Expand Down Expand Up @@ -131,8 +134,8 @@ public void AddLanguage(string languageTypeName, string displayName, IList<strin
AddLanguage(languageTypeName, displayName, names, fileExtensions, options, null);
}

internal void AddLanguage(string languageTypeName, string displayName, IList<string> names, IList<string> fileExtensions,
IDictionary<string, object> options, string paramName) {
internal void AddLanguage(string languageTypeName, string displayName, IList<string> names, IList<string> fileExtensions,
IDictionary<string, object> options, string? paramName) {
ContractUtils.Requires(!_frozen, "Configuration cannot be modified once the runtime is initialized");
ContractUtils.Requires(
names.All(id => !String.IsNullOrEmpty(id) && !_languageNames.ContainsKey(id)),
Expand Down Expand Up @@ -187,10 +190,10 @@ internal void Freeze() {
_frozen = true;
}

internal bool TryLoadLanguage(ScriptDomainManager manager, in AssemblyQualifiedTypeName providerName, out LanguageContext language) {
internal bool TryLoadLanguage(ScriptDomainManager manager, in AssemblyQualifiedTypeName providerName, [NotNullWhen(true)] out LanguageContext? language) {
Assert.NotNull(manager);

if (_languageConfigurations.TryGetValue(providerName, out LanguageConfiguration config)) {
if (_languageConfigurations.TryGetValue(providerName, out LanguageConfiguration? config)) {
language = LoadLanguageContext(manager, config);
return true;
}
Expand All @@ -199,12 +202,12 @@ internal bool TryLoadLanguage(ScriptDomainManager manager, in AssemblyQualifiedT
return false;
}

internal bool TryLoadLanguage(ScriptDomainManager manager, string str, bool isExtension, out LanguageContext language) {
internal bool TryLoadLanguage(ScriptDomainManager manager, string str, bool isExtension, [NotNullWhen(true)] out LanguageContext? language) {
Assert.NotNull(manager, str);

var dict = isExtension ? _languageExtensions : _languageNames;

if (dict.TryGetValue(str, out LanguageConfiguration config)) {
if (dict.TryGetValue(str, out LanguageConfiguration? config)) {
language = LoadLanguageContext(manager, config);
return true;
}
Expand All @@ -222,7 +225,7 @@ private LanguageContext LoadLanguageContext(ScriptDomainManager manager, Languag
// The check takes place after config.LoadLanguageContext is called to avoid calling user code while holding a lock.
lock (_loadedProviderTypes) {
Type type = language.GetType();
if (_loadedProviderTypes.TryGetValue(type, out LanguageConfiguration existingConfig)) {
if (_loadedProviderTypes.TryGetValue(type, out LanguageConfiguration? existingConfig)) {
throw new InvalidOperationException(
$"Language implemented by type '{config.ProviderName}' has already been loaded using name '{existingConfig.ProviderName}'");
}
Expand Down Expand Up @@ -289,7 +292,7 @@ public string[] GetFileExtensions() {
return ArrayUtils.MakeArray<string>(_languageExtensions.Keys);
}

internal LanguageConfiguration GetLanguageConfig(LanguageContext context) {
internal LanguageConfiguration? GetLanguageConfig(LanguageContext context) {
foreach (var config in _languageConfigurations.Values) {
if (config.LanguageContext == context) {
return config;
Expand Down
Loading