Skip to content
Merged
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
52 changes: 28 additions & 24 deletions src/core/Microsoft.Scripting/Hosting/CompiledCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
// 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

#if FEATURE_REMOTING
using System.Runtime.Remoting;
#else
using MarshalByRefObject = System.Object;
#endif

using System;
using System.Diagnostics.CodeAnalysis;
using System.Threading;

using Microsoft.Scripting.Utils;
Expand All @@ -19,7 +22,7 @@ namespace Microsoft.Scripting.Hosting {
/// Hosting API counterpart for <see cref="ScriptCode"/>.
/// </summary>
public sealed class CompiledCode : MarshalByRefObject {
private ScriptScope _defaultScope;
private ScriptScope? _defaultScope;

internal ScriptCode ScriptCode { get; }

Expand All @@ -44,73 +47,75 @@ public ScriptScope DefaultScope {
if (_defaultScope is null) {
Interlocked.CompareExchange(ref _defaultScope, new ScriptScope(Engine, ScriptCode.CreateScope()), null);
}
return _defaultScope;
return _defaultScope;
}
}

/// <summary>
/// Executes code in a default scope.
/// </summary>
public dynamic Execute() {
public dynamic? Execute() {
return ScriptCode.Run(DefaultScope.Scope);
}

/// <summary>
/// Execute code within a given scope and returns the result.
/// </summary>
public dynamic Execute(ScriptScope scope) {
public dynamic? Execute(ScriptScope scope) {
ContractUtils.RequiresNotNull(scope, nameof(scope));
return ScriptCode.Run(scope.Scope);
}

/// <summary>
/// Executes code in in a default scope and converts to a given type.
/// </summary>
[return: MaybeNull]
public T Execute<T>() {
return Engine.Operations.ConvertTo<T>((object)Execute());
return Engine.Operations.ConvertTo<T>((object?)Execute());
}

/// <summary>
/// Execute code within a given scope and converts result to a given type.
/// </summary>
[return: MaybeNull]
public T Execute<T>(ScriptScope scope) {
return Engine.Operations.ConvertTo<T>((object)Execute(scope));
return Engine.Operations.ConvertTo<T>((object?)Execute(scope));
}


#if FEATURE_REMOTING
/// <summary>
/// Executes the code in an empty scope.
/// Returns an ObjectHandle wrapping the resulting value of running the code.
/// Returns an ObjectHandle wrapping the resulting value of running the code.
/// </summary>
public ObjectHandle ExecuteAndWrap() {
return new ObjectHandle((object)Execute());
return new ObjectHandle((object?)Execute());
}

/// <summary>
/// Executes the code in the specified scope.
/// Returns an ObjectHandle wrapping the resulting value of running the code.
/// Returns an ObjectHandle wrapping the resulting value of running the code.
/// </summary>
public ObjectHandle ExecuteAndWrap(ScriptScope scope) {
return new ObjectHandle((object)Execute(scope));
return new ObjectHandle((object?)Execute(scope));
}

/// <summary>
/// Executes the code in an empty scope.
/// Returns an ObjectHandle wrapping the resulting value of running the code.
///
/// Returns an ObjectHandle wrapping the resulting value of running the code.
///
/// If an exception is thrown the exception is caught and an ObjectHandle to
/// the exception is provided.
/// </summary>
/// <remarks>
/// Use this API to handle non-serializable exceptions (exceptions might not be serializable due to security restrictions)
/// Use this API to handle non-serializable exceptions (exceptions might not be serializable due to security restrictions)
/// or if an exception serialization loses information.
/// </remarks>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
public ObjectHandle ExecuteAndWrap(out ObjectHandle exception) {
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
public ObjectHandle? ExecuteAndWrap(out ObjectHandle? exception) {
exception = null;
try {
return new ObjectHandle((object)Execute());
return new ObjectHandle((object?)Execute());
} catch (Exception e) {
exception = new ObjectHandle(e);
return null;
Expand All @@ -119,28 +124,27 @@ public ObjectHandle ExecuteAndWrap(out ObjectHandle exception) {

/// <summary>
/// Executes the expression in the specified scope and return a result.
/// Returns an ObjectHandle wrapping the resulting value of running the code.
///
/// Returns an ObjectHandle wrapping the resulting value of running the code.
///
/// If an exception is thrown the exception is caught and an ObjectHandle to
/// the exception is provided.
/// </summary>
/// <remarks>
/// Use this API to handle non-serializable exceptions (exceptions might not be serializable due to security restrictions)
/// Use this API to handle non-serializable exceptions (exceptions might not be serializable due to security restrictions)
/// or if an exception serialization loses information.
/// </remarks>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
public ObjectHandle ExecuteAndWrap(ScriptScope scope, out ObjectHandle exception) {
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
public ObjectHandle? ExecuteAndWrap(ScriptScope scope, out ObjectHandle? exception) {
exception = null;
try{
return new ObjectHandle((object)Execute(scope));
return new ObjectHandle((object?)Execute(scope));
} catch (Exception e) {
exception = new ObjectHandle(e);
return null;
}
}

// TODO: Figure out what is the right lifetime
public override object InitializeLifetimeService() {
public override object? InitializeLifetimeService() {
return null;
}
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
// 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.

#if FEATURE_CONFIGURATION

#nullable enable

#if FEATURE_CONFIGURATION

using System.Configuration;

namespace Microsoft.Scripting.Hosting.Configuration {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
// 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.

#if FEATURE_CONFIGURATION

#nullable enable

#if FEATURE_CONFIGURATION

using System.Configuration;

namespace Microsoft.Scripting.Hosting.Configuration {
Expand Down
14 changes: 8 additions & 6 deletions src/core/Microsoft.Scripting/Hosting/Configuration/Section.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

#if FEATURE_CONFIGURATION

using System;
Expand Down Expand Up @@ -83,7 +85,7 @@ public IEnumerable<OptionElement> GetOptions() {
}
}

private static Section LoadFromFile(Stream configFileStream) {
private static Section? LoadFromFile(Stream configFileStream) {
var result = new Section();
using (var reader = XmlReader.Create(configFileStream)) {
if (reader.ReadToDescendant("configuration") && reader.ReadToDescendant(SectionName)) {
Expand All @@ -95,7 +97,7 @@ private static Section LoadFromFile(Stream configFileStream) {
return result;
}

internal static void LoadRuntimeSetup(ScriptRuntimeSetup setup, Stream configFileStream) {
internal static void LoadRuntimeSetup(ScriptRuntimeSetup setup, Stream? configFileStream) {
var config = configFileStream is not null
? LoadFromFile(configFileStream)
: System.Configuration.ConfigurationManager.GetSection(SectionName) as Section;
Expand All @@ -112,10 +114,10 @@ internal static void LoadRuntimeSetup(ScriptRuntimeSetup setup, Stream configFil
}

foreach (var languageConfig in config.GetLanguages()) {
var provider = languageConfig.Type;
var names = languageConfig.GetNamesArray();
var extensions = languageConfig.GetExtensionsArray();
var displayName = languageConfig.DisplayName ?? ((names.Length > 0) ? names[0] : languageConfig.Type);
string provider = languageConfig.Type ?? "<unknown>";
string[] names = languageConfig.GetNamesArray();
string[] extensions = languageConfig.GetExtensionsArray();
string displayName = languageConfig.DisplayName ?? ((names.Length > 0) ? names[0] : provider);

// Honor the latest-wins behavior of the <languages> tag for options that were already included in the setup object;
// Keep the options though.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ public ICollection<OverloadDoc> GetOverloads(ObjectHandle value) {
}

// TODO: Figure out what is the right lifetime
public override object InitializeLifetimeService() {
return base.InitializeLifetimeService();
public override object? InitializeLifetimeService() {
return null;
}
#endif
}
Expand Down
5 changes: 3 additions & 2 deletions src/core/Microsoft.Scripting/Hosting/ErrorListener.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;

#if FEATURE_REMOTING
Expand All @@ -27,8 +29,7 @@ internal void ReportError(ScriptSource source, string message, in SourceSpan spa
public abstract void ErrorReported(ScriptSource source, string message, SourceSpan span, int errorCode, Severity severity);

#if FEATURE_REMOTING
// TODO: Figure out what is the right lifetime
public override object InitializeLifetimeService() {
public override object? InitializeLifetimeService() {
return null;
}
#endif
Expand Down
8 changes: 5 additions & 3 deletions src/core/Microsoft.Scripting/Hosting/ErrorListenerProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +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

namespace Microsoft.Scripting.Hosting {

/// <summary>
/// Bridges ErrorSink and ErrorListener.
/// Bridges ErrorSink and ErrorListener.
/// Errors reported by language compilers to ErrorSink are forwarded to the ErrorListener provided by the host.
/// </summary>
/// <remarks>
Expand All @@ -18,10 +20,10 @@ namespace Microsoft.Scripting.Hosting {
/// within the context of compilation unit.
/// </remarks>
internal sealed class ErrorListenerProxySink : ErrorSink {
private readonly ErrorListener _listener;
private readonly ErrorListener? _listener;
private readonly ScriptSource _source;

public ErrorListenerProxySink(ScriptSource source, ErrorListener listener) {
public ErrorListenerProxySink(ScriptSource source, ErrorListener? listener) {
_listener = listener;
_source = source;
}
Expand Down
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

namespace Microsoft.Scripting.Hosting {
/// <summary>
/// Bridges ErrorListener and ErrorSink. It provides the reverse functionality as ErrorSinkProxyListener
Expand All @@ -14,11 +16,11 @@ public ErrorSinkProxyListener(ErrorSink errorSink) {
}

public override void ErrorReported(ScriptSource source, string message, SourceSpan span, int errorCode, Severity severity) {
// Note that we cannot use "source.SourceUnit" since "source" may be a proxy object, and we will not be able to marshall
// Note that we cannot use "source.SourceUnit" since "source" may be a proxy object, and we will not be able to marshall
// "source.SourceUnit" to the current AppDomain

string code = null;
string line = null;
string? code = null;
string? line = null;
try {
code = source.GetCode();
line = source.GetCodeLine(span.Start.Line);
Expand Down
4 changes: 2 additions & 2 deletions src/core/Microsoft.Scripting/Hosting/ExceptionOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ public IList<DynamicStackFrame> GetStackFrames(ObjectHandle exception) {
return _context.GetStackFrames(exceptionObj!);
}

public override object InitializeLifetimeService() {
return base.InitializeLifetimeService();
public override object? InitializeLifetimeService() {
return null;
}
#endif
}
Expand Down
Loading
Loading