diff --git a/src/core/Microsoft.Scripting/Hosting/CompiledCode.cs b/src/core/Microsoft.Scripting/Hosting/CompiledCode.cs index 63331493..1f17d98c 100644 --- a/src/core/Microsoft.Scripting/Hosting/CompiledCode.cs +++ b/src/core/Microsoft.Scripting/Hosting/CompiledCode.cs @@ -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_REMOTING using System.Runtime.Remoting; #else @@ -9,6 +11,7 @@ #endif using System; +using System.Diagnostics.CodeAnalysis; using System.Threading; using Microsoft.Scripting.Utils; @@ -19,7 +22,7 @@ namespace Microsoft.Scripting.Hosting { /// Hosting API counterpart for . /// public sealed class CompiledCode : MarshalByRefObject { - private ScriptScope _defaultScope; + private ScriptScope? _defaultScope; internal ScriptCode ScriptCode { get; } @@ -44,21 +47,21 @@ public ScriptScope DefaultScope { if (_defaultScope is null) { Interlocked.CompareExchange(ref _defaultScope, new ScriptScope(Engine, ScriptCode.CreateScope()), null); } - return _defaultScope; + return _defaultScope; } } /// /// Executes code in a default scope. /// - public dynamic Execute() { + public dynamic? Execute() { return ScriptCode.Run(DefaultScope.Scope); } /// /// Execute code within a given scope and returns the result. /// - public dynamic Execute(ScriptScope scope) { + public dynamic? Execute(ScriptScope scope) { ContractUtils.RequiresNotNull(scope, nameof(scope)); return ScriptCode.Run(scope.Scope); } @@ -66,51 +69,53 @@ public dynamic Execute(ScriptScope scope) { /// /// Executes code in in a default scope and converts to a given type. /// + [return: MaybeNull] public T Execute() { - return Engine.Operations.ConvertTo((object)Execute()); + return Engine.Operations.ConvertTo((object?)Execute()); } /// /// Execute code within a given scope and converts result to a given type. /// + [return: MaybeNull] public T Execute(ScriptScope scope) { - return Engine.Operations.ConvertTo((object)Execute(scope)); + return Engine.Operations.ConvertTo((object?)Execute(scope)); } #if FEATURE_REMOTING /// /// 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. /// public ObjectHandle ExecuteAndWrap() { - return new ObjectHandle((object)Execute()); + return new ObjectHandle((object?)Execute()); } /// /// 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. /// public ObjectHandle ExecuteAndWrap(ScriptScope scope) { - return new ObjectHandle((object)Execute(scope)); + return new ObjectHandle((object?)Execute(scope)); } /// /// 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. /// /// - /// 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. /// - [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; @@ -119,28 +124,27 @@ public ObjectHandle ExecuteAndWrap(out ObjectHandle exception) { /// /// 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. /// /// - /// 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. /// - [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 diff --git a/src/core/Microsoft.Scripting/Hosting/Configuration/LanguageElementCollection.cs b/src/core/Microsoft.Scripting/Hosting/Configuration/LanguageElementCollection.cs index 71610005..1a9abf77 100644 --- a/src/core/Microsoft.Scripting/Hosting/Configuration/LanguageElementCollection.cs +++ b/src/core/Microsoft.Scripting/Hosting/Configuration/LanguageElementCollection.cs @@ -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 { diff --git a/src/core/Microsoft.Scripting/Hosting/Configuration/OptionElementCollection.cs b/src/core/Microsoft.Scripting/Hosting/Configuration/OptionElementCollection.cs index 9421c8a3..363bdbd6 100644 --- a/src/core/Microsoft.Scripting/Hosting/Configuration/OptionElementCollection.cs +++ b/src/core/Microsoft.Scripting/Hosting/Configuration/OptionElementCollection.cs @@ -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 { diff --git a/src/core/Microsoft.Scripting/Hosting/Configuration/Section.cs b/src/core/Microsoft.Scripting/Hosting/Configuration/Section.cs index 24627df4..40c37c9c 100644 --- a/src/core/Microsoft.Scripting/Hosting/Configuration/Section.cs +++ b/src/core/Microsoft.Scripting/Hosting/Configuration/Section.cs @@ -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; @@ -83,7 +85,7 @@ public IEnumerable 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)) { @@ -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; @@ -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 ?? ""; + 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 tag for options that were already included in the setup object; // Keep the options though. diff --git a/src/core/Microsoft.Scripting/Hosting/DocumentationOperations.cs b/src/core/Microsoft.Scripting/Hosting/DocumentationOperations.cs index 626abdb8..c52f93e2 100644 --- a/src/core/Microsoft.Scripting/Hosting/DocumentationOperations.cs +++ b/src/core/Microsoft.Scripting/Hosting/DocumentationOperations.cs @@ -56,8 +56,8 @@ public ICollection 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 } diff --git a/src/core/Microsoft.Scripting/Hosting/ErrorListener.cs b/src/core/Microsoft.Scripting/Hosting/ErrorListener.cs index e97d1bba..1045d022 100644 --- a/src/core/Microsoft.Scripting/Hosting/ErrorListener.cs +++ b/src/core/Microsoft.Scripting/Hosting/ErrorListener.cs @@ -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 @@ -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 diff --git a/src/core/Microsoft.Scripting/Hosting/ErrorListenerProxy.cs b/src/core/Microsoft.Scripting/Hosting/ErrorListenerProxy.cs index 925a0ad7..e2d02bf1 100644 --- a/src/core/Microsoft.Scripting/Hosting/ErrorListenerProxy.cs +++ b/src/core/Microsoft.Scripting/Hosting/ErrorListenerProxy.cs @@ -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 { /// - /// Bridges ErrorSink and ErrorListener. + /// Bridges ErrorSink and ErrorListener. /// Errors reported by language compilers to ErrorSink are forwarded to the ErrorListener provided by the host. /// /// @@ -18,10 +20,10 @@ namespace Microsoft.Scripting.Hosting { /// within the context of compilation unit. /// 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; } diff --git a/src/core/Microsoft.Scripting/Hosting/ErrorSinkProxyListener.cs b/src/core/Microsoft.Scripting/Hosting/ErrorSinkProxyListener.cs index 4bb32e68..4d032c29 100644 --- a/src/core/Microsoft.Scripting/Hosting/ErrorSinkProxyListener.cs +++ b/src/core/Microsoft.Scripting/Hosting/ErrorSinkProxyListener.cs @@ -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 { /// /// Bridges ErrorListener and ErrorSink. It provides the reverse functionality as ErrorSinkProxyListener @@ -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); diff --git a/src/core/Microsoft.Scripting/Hosting/ExceptionOperations.cs b/src/core/Microsoft.Scripting/Hosting/ExceptionOperations.cs index e06c6e34..2d32ebce 100644 --- a/src/core/Microsoft.Scripting/Hosting/ExceptionOperations.cs +++ b/src/core/Microsoft.Scripting/Hosting/ExceptionOperations.cs @@ -74,8 +74,8 @@ public IList GetStackFrames(ObjectHandle exception) { return _context.GetStackFrames(exceptionObj!); } - public override object InitializeLifetimeService() { - return base.InitializeLifetimeService(); + public override object? InitializeLifetimeService() { + return null; } #endif } diff --git a/src/core/Microsoft.Scripting/Hosting/ObjectOperations.cs b/src/core/Microsoft.Scripting/Hosting/ObjectOperations.cs index 5f829f79..3284045a 100644 --- a/src/core/Microsoft.Scripting/Hosting/ObjectOperations.cs +++ b/src/core/Microsoft.Scripting/Hosting/ObjectOperations.cs @@ -2,7 +2,7 @@ // 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. -using System.Linq.Expressions; +#nullable enable #if FEATURE_REMOTING using System.Runtime.Remoting; @@ -13,21 +13,26 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; +using System.Linq.Expressions; using Microsoft.Scripting.Runtime; using Microsoft.Scripting.Utils; +using NotNull = Microsoft.Scripting.Runtime.NotNullAttribute; + namespace Microsoft.Scripting.Hosting { /// /// ObjectOperations provide a large catalogue of object operations such as member access, conversions, /// indexing, and things like addition. There are several introspection and tool support services available - /// for more advanced hosts. - /// + /// for more advanced hosts. + /// + /// /// You get ObjectOperation instances from ScriptEngine, and they are bound to their engines for the semantics /// of the operations. There is a default instance of ObjectOperations you can share across all uses of the /// engine. However, very advanced hosts can create new instances. - /// + /// public sealed class ObjectOperations : MarshalByRefObject { private readonly DynamicOperations _ops; @@ -46,42 +51,42 @@ internal ObjectOperations(DynamicOperations ops, ScriptEngine engine) { #region Local Operations /// - /// Returns true if the object can be called, false if it cannot. - /// + /// Returns true if the object can be called, false if it cannot. + /// /// Even if an object is callable Call may still fail if an incorrect number of arguments or type of arguments are provided. /// - public bool IsCallable(object obj) { + public bool IsCallable(object? obj) { return _ops.IsCallable(obj); } /// /// Invokes the provided object with the given parameters and returns the result. - /// - /// The prefered way of calling objects is to convert the object to a strongly typed delegate + /// + /// The prefered way of calling objects is to convert the object to a strongly typed delegate /// using the ConvertTo methods and then invoking that delegate. /// - public dynamic Invoke(object obj, params object[] parameters) { + public dynamic? Invoke(object? obj, params object?[] parameters) { return _ops.Invoke(obj, parameters); } /// /// Invokes a member on the provided object with the given parameters and returns the result. /// - public dynamic InvokeMember(object obj, string memberName, params object[] parameters) { + public dynamic? InvokeMember(object? obj, string memberName, params object?[] parameters) { return _ops.InvokeMember(obj, memberName, parameters); } /// /// Creates a new instance from the provided object using the given parameters, and returns the result. /// - public dynamic CreateInstance(object obj, params object[] parameters) { + public dynamic? CreateInstance(object? obj, params object?[] parameters) { return _ops.CreateInstance(obj, parameters); } /// /// Gets the member name from the object obj. Throws an exception if the member does not exist or is write-only. /// - public dynamic GetMember(object obj, string name) { + public dynamic? GetMember(object? obj, string name) { return _ops.GetMember(obj, name); } @@ -89,36 +94,37 @@ public dynamic GetMember(object obj, string name) { /// Gets the member name from the object obj and converts it to the type T. Throws an exception if the /// member does not exist, is write-only, or cannot be converted. /// - public T GetMember(object obj, string name) { + [return: MaybeNull] + public T GetMember(object? obj, string name) { return _ops.GetMember(obj, name); } /// - /// Gets the member name from the object obj. Returns true if the member is successfully retrieved and + /// Gets the member name from the object obj. Returns true if the member is successfully retrieved and /// stores the value in the value out param. /// - public bool TryGetMember(object obj, string name, out object value) { + public bool TryGetMember(object? obj, string name, out object? value) { return _ops.TryGetMember(obj, name, out value); } /// /// Returns true if the object has a member named name, false if the member does not exist. /// - public bool ContainsMember(object obj, string name) { + public bool ContainsMember(object? obj, string name) { return _ops.ContainsMember(obj, name); } /// - /// Removes the member name from the object obj. + /// Removes the member name from the object obj. /// - public void RemoveMember(object obj, string name) { + public void RemoveMember(object? obj, string name) { _ops.RemoveMember(obj, name); } /// /// Sets the member name on object obj to value. /// - public void SetMember(object obj, string name, object value) { + public void SetMember(object? obj, string name, object? value) { _ops.SetMember(obj, name, value); } @@ -126,14 +132,14 @@ public void SetMember(object obj, string name, object value) { /// Sets the member name on object obj to value. This overload can be used to avoid /// boxing and casting of strongly typed members. /// - public void SetMember(object obj, string name, T value) { + public void SetMember(object? obj, string name, T value) { _ops.SetMember(obj, name, value); } /// /// Gets the member name from the object obj. Throws an exception if the member does not exist or is write-only. /// - public dynamic GetMember(object obj, string name, bool ignoreCase) { + public dynamic? GetMember(object? obj, string name, bool ignoreCase) { return _ops.GetMember(obj, name, ignoreCase); } @@ -141,36 +147,37 @@ public dynamic GetMember(object obj, string name, bool ignoreCase) { /// Gets the member name from the object obj and converts it to the type T. Throws an exception if the /// member does not exist, is write-only, or cannot be converted. /// - public T GetMember(object obj, string name, bool ignoreCase) { + [return: MaybeNull] + public T GetMember(object? obj, string name, bool ignoreCase) { return _ops.GetMember(obj, name, ignoreCase); } /// - /// Gets the member name from the object obj. Returns true if the member is successfully retrieved and + /// Gets the member name from the object obj. Returns true if the member is successfully retrieved and /// stores the value in the value out param. /// - public bool TryGetMember(object obj, string name, bool ignoreCase, out object value) { + public bool TryGetMember(object? obj, string name, bool ignoreCase, out object? value) { return _ops.TryGetMember(obj, name, ignoreCase, out value); } /// /// Returns true if the object has a member named name, false if the member does not exist. /// - public bool ContainsMember(object obj, string name, bool ignoreCase) { + public bool ContainsMember(object? obj, string name, bool ignoreCase) { return _ops.ContainsMember(obj, name, ignoreCase); } /// - /// Removes the member name from the object obj. + /// Removes the member name from the object obj. /// - public void RemoveMember(object obj, string name, bool ignoreCase) { + public void RemoveMember(object? obj, string name, bool ignoreCase) { _ops.RemoveMember(obj, name, ignoreCase); } /// /// Sets the member name on object obj to value. /// - public void SetMember(object obj, string name, object value, bool ignoreCase) { + public void SetMember(object? obj, string name, object? value, bool ignoreCase) { _ops.SetMember(obj, name, value, ignoreCase); } @@ -178,23 +185,24 @@ public void SetMember(object obj, string name, object value, bool ignoreCase) { /// Sets the member name on object obj to value. This overload can be used to avoid /// boxing and casting of strongly typed members. /// - public void SetMember(object obj, string name, T value, bool ignoreCase) { + public void SetMember(object? obj, string name, T value, bool ignoreCase) { _ops.SetMember(obj, name, value, ignoreCase); } /// - /// Converts the object obj to the type T. The conversion will be explicit or implicit depending on + /// Converts the object obj to the type T. The conversion will be explicit or implicit depending on /// what the langauge prefers. /// - public T ConvertTo(object obj) { + [return: MaybeNull] + public T ConvertTo(object? obj) { return _ops.ConvertTo(obj); } /// - /// Converts the object obj to the type type. The conversion will be explicit or implicit depending on + /// Converts the object obj to the type type. The conversion will be explicit or implicit depending on /// what the langauge prefers. /// - public object ConvertTo(object obj, Type type) { + public object? ConvertTo(object? obj, Type type) { ContractUtils.RequiresNotNull(type, nameof(type)); return _ops.ConvertTo(obj, type); @@ -202,33 +210,34 @@ public object ConvertTo(object obj, Type type) { /// /// Converts the object obj to the type T. Returns true if the value can be converted, false if it cannot. - /// + /// /// The conversion will be explicit or implicit depending on what the langauge prefers. /// - public bool TryConvertTo(object obj, out T result) { + public bool TryConvertTo(object? obj, [MaybeNull] out T result) { return _ops.TryConvertTo(obj, out result); } /// /// Converts the object obj to the type type. Returns true if the value can be converted, false if it cannot. - /// + /// /// The conversion will be explicit or implicit depending on what the langauge prefers. /// - public bool TryConvertTo(object obj, Type type, out object result) { + public bool TryConvertTo(object? obj, Type type, out object? result) { return _ops.TryConvertTo(obj, type, out result); } /// /// Converts the object obj to the type T including explicit conversions which may lose information. /// - public T ExplicitConvertTo(object obj) { + [return: MaybeNull] + public T ExplicitConvertTo(object? obj) { return _ops.ExplicitConvertTo(obj); } /// /// Converts the object obj to the type type including explicit conversions which may lose information. /// - public object ExplicitConvertTo(object obj, Type type) { + public object? ExplicitConvertTo(object? obj, Type type) { ContractUtils.RequiresNotNull(type, nameof(type)); return _ops.ExplicitConvertTo(obj, type); @@ -236,19 +245,19 @@ public object ExplicitConvertTo(object obj, Type type) { /// /// Converts the object obj to the type T including explicit conversions which may lose information. - /// + /// /// Returns true if the value can be converted, false if it cannot. /// - public bool TryExplicitConvertTo(object obj, out T result) { + public bool TryExplicitConvertTo(object? obj, [MaybeNull] out T result) { return _ops.TryExplicitConvertTo(obj, out result); } /// - /// Converts the object obj to the type type including explicit conversions which may lose information. - /// + /// Converts the object obj to the type type including explicit conversions which may lose information. + /// /// Returns true if the value can be converted, false if it cannot. /// - public bool TryExplicitConvertTo(object obj, Type type, out object result) { + public bool TryExplicitConvertTo(object? obj, Type type, out object? result) { return _ops.TryExplicitConvertTo(obj, type, out result); } @@ -256,14 +265,15 @@ public bool TryExplicitConvertTo(object obj, Type type, out object result) { /// /// Converts the object obj to the type T including implicit conversions. /// - public T ImplicitConvertTo(object obj) { + [return: MaybeNull] + public T ImplicitConvertTo(object? obj) { return _ops.ImplicitConvertTo(obj); } /// /// Converts the object obj to the type type including implicit conversions. /// - public object ImplicitConvertTo(object obj, Type type) { + public object? ImplicitConvertTo(object? obj, Type type) { ContractUtils.RequiresNotNull(type, nameof(type)); return _ops.ImplicitConvertTo(obj, type); @@ -271,32 +281,33 @@ public object ImplicitConvertTo(object obj, Type type) { /// /// Converts the object obj to the type T including implicit conversions. - /// + /// /// Returns true if the value can be converted, false if it cannot. /// - public bool TryImplicitConvertTo(object obj, out T result) { + public bool TryImplicitConvertTo(object? obj, [MaybeNull] out T result) { return _ops.TryImplicitConvertTo(obj, out result); } /// - /// Converts the object obj to the type type including implicit conversions. - /// + /// Converts the object obj to the type type including implicit conversions. + /// /// Returns true if the value can be converted, false if it cannot. /// - public bool TryImplicitConvertTo(object obj, Type type, out object result) { + public bool TryImplicitConvertTo(object? obj, Type type, out object? result) { return _ops.TryImplicitConvertTo(obj, type, out result); } /// /// Performs a generic unary operation on the specified target and returns the result. /// - public dynamic DoOperation(ExpressionType operation, object target) { - return _ops.DoOperation(operation, target); + public dynamic? DoOperation(ExpressionType operation, object? target) { + return _ops.DoOperation(operation, target); } /// /// Performs a generic unary operation on the strongly typed target and returns the value as the specified type /// + [return: MaybeNull] public TResult DoOperation(ExpressionType operation, TTarget target) { return _ops.DoOperation(operation, target); } @@ -304,14 +315,15 @@ public TResult DoOperation(ExpressionType operation, TTarget t /// /// Performs the generic binary operation on the specified targets and returns the result. /// - public dynamic DoOperation(ExpressionType operation, object target, object other) { - return _ops.DoOperation(operation, target, other); + public dynamic? DoOperation(ExpressionType operation, object? target, object? other) { + return _ops.DoOperation(operation, target, other); } /// /// Peforms the generic binary operation on the specified strongly typed targets and returns /// the strongly typed result. /// + [return: MaybeNull] public TResult DoOperation(ExpressionType operation, TTarget target, TOther other) { return _ops.DoOperation(operation, target, other); } @@ -320,7 +332,7 @@ public TResult DoOperation(ExpressionType operation, T /// Performs addition on the specified targets and returns the result. Throws an exception /// if the operation cannot be performed. /// - public dynamic Add(object self, object other) { + public dynamic? Add(object? self, object? other) { return DoOperation(ExpressionType.Add, self, other); } @@ -328,7 +340,7 @@ public dynamic Add(object self, object other) { /// Performs subtraction on the specified targets and returns the result. Throws an exception /// if the operation cannot be performed. /// - public dynamic Subtract(object self, object other) { + public dynamic? Subtract(object? self, object? other) { return DoOperation(ExpressionType.Subtract, self, other); } @@ -336,7 +348,7 @@ public dynamic Subtract(object self, object other) { /// Raises the first object to the power of the second object. Throws an exception /// if the operation cannot be performed. /// - public dynamic Power(object self, object other) { + public dynamic? Power(object? self, object? other) { return DoOperation(ExpressionType.Power, self, other); } @@ -344,7 +356,7 @@ public dynamic Power(object self, object other) { /// Multiplies the two objects. Throws an exception /// if the operation cannot be performed. /// - public dynamic Multiply(object self, object other) { + public dynamic? Multiply(object? self, object? other) { return DoOperation(ExpressionType.Multiply, self, other); } @@ -352,7 +364,7 @@ public dynamic Multiply(object self, object other) { /// Divides the first object by the second object. Throws an exception /// if the operation cannot be performed. /// - public dynamic Divide(object self, object other) { + public dynamic? Divide(object? self, object? other) { return DoOperation(ExpressionType.Divide, self, other); } @@ -360,14 +372,14 @@ public dynamic Divide(object self, object other) { /// Performs modulus of the 1st object by the second object. Throws an exception /// if the operation cannot be performed. /// - public dynamic Modulo(object self, object other) { + public dynamic? Modulo(object? self, object? other) { return DoOperation(ExpressionType.Modulo, self, other); } /// /// Shifts the left object left by the right object. Throws an exception if the /// operation cannot be performed. /// - public dynamic LeftShift(object self, object other) { + public dynamic? LeftShift(object? self, object? other) { return DoOperation(ExpressionType.LeftShift, self, other); } @@ -375,31 +387,31 @@ public dynamic LeftShift(object self, object other) { /// Shifts the left object right by the right object. Throws an exception if the /// operation cannot be performed. /// - public dynamic RightShift(object self, object other) { + public dynamic? RightShift(object? self, object? other) { return DoOperation(ExpressionType.RightShift, self, other); } /// - /// Performs a bitwise-and of the two operands. Throws an exception if the operation + /// Performs a bitwise-and of the two operands. Throws an exception if the operation /// cannot be performed. /// - public dynamic BitwiseAnd(object self, object other) { + public dynamic? BitwiseAnd(object? self, object? other) { return DoOperation(ExpressionType.And, self, other); } /// - /// Performs a bitwise-or of the two operands. Throws an exception if the operation + /// Performs a bitwise-or of the two operands. Throws an exception if the operation /// cannot be performed. /// - public dynamic BitwiseOr(object self, object other) { + public dynamic? BitwiseOr(object? self, object? other) { return DoOperation(ExpressionType.Or, self, other); } /// - /// Performs a exclusive-or of the two operands. Throws an exception if the operation + /// Performs a exclusive-or of the two operands. Throws an exception if the operation /// cannot be performed. /// - public dynamic ExclusiveOr(object self, object other) { + public dynamic? ExclusiveOr(object? self, object? other) { return DoOperation(ExpressionType.ExclusiveOr, self, other); } @@ -407,55 +419,55 @@ public dynamic ExclusiveOr(object self, object other) { /// Compares the two objects and returns true if the left object is less than the right object. /// Throws an exception if hte comparison cannot be performed. /// - public bool LessThan(object self, object other) { - return ConvertTo(_ops.DoOperation(ExpressionType.LessThan, self, other)); + public bool LessThan(object? self, object? other) { + return ConvertTo(_ops.DoOperation(ExpressionType.LessThan, self, other)); } /// /// Compares the two objects and returns true if the left object is greater than the right object. /// Throws an exception if hte comparison cannot be performed. /// - public bool GreaterThan(object self, object other) { - return ConvertTo(_ops.DoOperation(ExpressionType.GreaterThan, self, other)); + public bool GreaterThan(object? self, object? other) { + return ConvertTo(_ops.DoOperation(ExpressionType.GreaterThan, self, other)); } /// /// Compares the two objects and returns true if the left object is less than or equal to the right object. /// Throws an exception if hte comparison cannot be performed. /// - public bool LessThanOrEqual(object self, object other) { - return ConvertTo(_ops.DoOperation(ExpressionType.LessThanOrEqual, self, other)); + public bool LessThanOrEqual(object? self, object? other) { + return ConvertTo(_ops.DoOperation(ExpressionType.LessThanOrEqual, self, other)); } /// /// Compares the two objects and returns true if the left object is greater than or equal to the right object. /// Throws an exception if hte comparison cannot be performed. /// - public bool GreaterThanOrEqual(object self, object other) { - return ConvertTo(_ops.DoOperation(ExpressionType.GreaterThanOrEqual, self, other)); + public bool GreaterThanOrEqual(object? self, object? other) { + return ConvertTo(_ops.DoOperation(ExpressionType.GreaterThanOrEqual, self, other)); } /// /// Compares the two objects and returns true if the left object is equal to the right object. /// Throws an exception if the comparison cannot be performed. /// - public bool Equal(object self, object other) { - return ConvertTo(_ops.DoOperation(ExpressionType.Equal, self, other)); + public bool Equal(object? self, object? other) { + return ConvertTo(_ops.DoOperation(ExpressionType.Equal, self, other)); } /// /// Compares the two objects and returns true if the left object is not equal to the right object. /// Throws an exception if hte comparison cannot be performed. /// - public bool NotEqual(object self, object other) { - return ConvertTo(_ops.DoOperation(ExpressionType.NotEqual, self, other)); + public bool NotEqual(object? self, object? other) { + return ConvertTo(_ops.DoOperation(ExpressionType.NotEqual, self, other)); } /// /// Returns a string which describes the object as it appears in source code /// [Obsolete("Use Format method instead.")] - public string GetCodeRepresentation(object obj) { + public string? GetCodeRepresentation(object obj) { return obj.ToString(); //return _ops.DoOperation(StandardOperators.CodeRepresentation, obj); } @@ -463,28 +475,28 @@ public string GetCodeRepresentation(object obj) { /// /// Returns a string representation of the object in a language specific object display format. /// - public string Format(object obj) { + public string Format(object? obj) { return _ops.Format(obj); } /// /// Returns a list of strings which contain the known members of the object. /// - public IList GetMemberNames(object obj) { + public IList GetMemberNames(object? obj) { return _ops.GetMemberNames(obj); } /// /// Returns a string providing documentation for the specified object. /// - public string GetDocumentation(object obj) { + public string GetDocumentation(object? obj) { return _ops.GetDocumentation(obj); } /// /// Returns a list of signatures applicable for calling the specified object in a form displayable to the user. /// - public IList GetCallSignatures(object obj) { + public IList GetCallSignatures(object? obj) { return _ops.GetCallSignatures(obj); } @@ -507,35 +519,35 @@ public bool IsCallable([NotNull]ObjectHandle obj) { /// /// Invokes the specified remote object with the specified remote parameters. - /// + /// /// Though delegates are preferable for calls they may not always be usable for remote objects. /// public ObjectHandle Invoke([NotNull]ObjectHandle obj, params ObjectHandle[] parameters) { ContractUtils.RequiresNotNull(parameters, nameof(parameters)); - return new ObjectHandle((object)Invoke(GetLocalObject(obj), GetLocalObjects(parameters))); + return new ObjectHandle((object?)Invoke(GetLocalObject(obj), GetLocalObjects(parameters))); } /// /// Invokes the specified remote object with the local parameters which will be serialized /// to the remote app domain. /// - public ObjectHandle Invoke([NotNull]ObjectHandle obj, params object[] parameters) { - return new ObjectHandle((object)Invoke(GetLocalObject(obj), parameters)); + public ObjectHandle Invoke([NotNull]ObjectHandle obj, params object?[] parameters) { + return new ObjectHandle((object?)Invoke(GetLocalObject(obj), parameters)); } /// /// Creates a new remote instance from the provided remote object using the given parameters, and returns the result. /// public ObjectHandle CreateInstance([NotNull]ObjectHandle obj, [NotNull]params ObjectHandle[] parameters) { - return new ObjectHandle((object)CreateInstance(GetLocalObject(obj), GetLocalObjects(parameters))); + return new ObjectHandle((object?)CreateInstance(GetLocalObject(obj), GetLocalObjects(parameters))); } /// /// Creates a new remote instance from the provided remote object using the given parameters, and returns the result. /// - public ObjectHandle CreateInstance([NotNull]ObjectHandle obj, params object[] parameters) { - return new ObjectHandle((object)CreateInstance(GetLocalObject(obj), parameters)); + public ObjectHandle CreateInstance([NotNull]ObjectHandle obj, params object?[] parameters) { + return new ObjectHandle((object?)CreateInstance(GetLocalObject(obj), parameters)); } /// @@ -558,13 +570,14 @@ public void SetMember([NotNull]ObjectHandle obj, string name, T value) { /// is write-only. /// public ObjectHandle GetMember([NotNull]ObjectHandle obj, string name) { - return new ObjectHandle((object)GetMember(GetLocalObject(obj), name)); + return new ObjectHandle((object?)GetMember(GetLocalObject(obj), name)); } /// /// Gets the member name on the remote object. Throws an exception if the member is not defined or /// is write-only. /// + [return: MaybeNull] public T GetMember([NotNull]ObjectHandle obj, string name) { return GetMember(GetLocalObject(obj), name); } @@ -573,8 +586,8 @@ public T GetMember([NotNull]ObjectHandle obj, string name) { /// Gets the member name on the remote object. Returns false if the member is not defined or /// is write-only. /// - public bool TryGetMember([NotNull]ObjectHandle obj, string name, out ObjectHandle value) { - if (TryGetMember(GetLocalObject(obj), name, out object val)) { + public bool TryGetMember([NotNull]ObjectHandle obj, string name, [NotNullWhen(true)] out ObjectHandle? value) { + if (TryGetMember(GetLocalObject(obj), name, out object? val)) { value = new ObjectHandle(val); return true; } @@ -584,7 +597,7 @@ public bool TryGetMember([NotNull]ObjectHandle obj, string name, out ObjectHandl } /// - /// Tests to see if the member name is defined on the remote object. + /// Tests to see if the member name is defined on the remote object. /// public bool ContainsMember([NotNull]ObjectHandle obj, string name) { return ContainsMember(GetLocalObject(obj), name); @@ -599,7 +612,7 @@ public void RemoveMember([NotNull]ObjectHandle obj, string name) { /// /// Converts the remote object into the specified type returning a handle to - /// the new remote object. The conversion will be explicit or implicit depending on + /// the new remote object. The conversion will be explicit or implicit depending on /// what the langauge prefers. /// public ObjectHandle ConvertTo([NotNull]ObjectHandle obj) { @@ -608,7 +621,7 @@ public ObjectHandle ConvertTo([NotNull]ObjectHandle obj) { /// /// Converts the remote object into the specified type returning a handle to - /// the new remote object. The conversion will be explicit or implicit depending on + /// the new remote object. The conversion will be explicit or implicit depending on /// what the langauge prefers. /// public ObjectHandle ConvertTo([NotNull]ObjectHandle obj, Type type) { @@ -618,11 +631,11 @@ public ObjectHandle ConvertTo([NotNull]ObjectHandle obj, Type type) { /// /// Converts the remote object into the specified type returning a handle to /// the new remote object. Returns true if the value can be converted, - /// false if it cannot. The conversion will be explicit or implicit depending on + /// false if it cannot. The conversion will be explicit or implicit depending on /// what the langauge prefers. /// - public bool TryConvertTo([NotNull]ObjectHandle obj, out ObjectHandle result) { - if (TryConvertTo(GetLocalObject(obj), out T resultObj)) { + public bool TryConvertTo([NotNull]ObjectHandle obj, [NotNullWhen(true)] out ObjectHandle? result) { + if (TryConvertTo(GetLocalObject(obj), out var resultObj)) { result = new ObjectHandle(resultObj); return true; } @@ -633,11 +646,11 @@ public bool TryConvertTo([NotNull]ObjectHandle obj, out ObjectHandle result) /// /// Converts the remote object into the specified type returning a handle to /// the new remote object. Returns true if the value can be converted, - /// false if it cannot. The conversion will be explicit or implicit depending on + /// false if it cannot. The conversion will be explicit or implicit depending on /// what the langauge prefers. /// - public bool TryConvertTo([NotNull]ObjectHandle obj, Type type, out ObjectHandle result) { - if (TryConvertTo(GetLocalObject(obj), type, out object resultObj)) { + public bool TryConvertTo([NotNull]ObjectHandle obj, Type type, [NotNullWhen(true)] out ObjectHandle? result) { + if (TryConvertTo(GetLocalObject(obj), type, out object? resultObj)) { result = new ObjectHandle(resultObj); return true; } @@ -663,22 +676,22 @@ public ObjectHandle ExplicitConvertTo([NotNull]ObjectHandle obj, Type type) { /// /// Converts the object obj to the type T including explicit conversions which may lose information. - /// + /// /// Returns true if the value can be converted, false if it cannot. /// - public bool TryExplicitConvertTo([NotNull]ObjectHandle obj, out ObjectHandle result) { - bool res = _ops.TryExplicitConvertTo(GetLocalObject(obj), out T _); + public bool TryExplicitConvertTo([NotNull]ObjectHandle obj, [NotNullWhen(true)] out ObjectHandle? result) { + bool res = _ops.TryExplicitConvertTo(GetLocalObject(obj), out _); result = res ? new ObjectHandle(obj) : null; return res; } /// - /// Converts the object obj to the type type including explicit conversions which may lose information. - /// + /// Converts the object obj to the type type including explicit conversions which may lose information. + /// /// Returns true if the value can be converted, false if it cannot. /// - public bool TryExplicitConvertTo([NotNull]ObjectHandle obj, Type type, out ObjectHandle result) { - bool res = _ops.TryExplicitConvertTo(GetLocalObject(obj), type, out object _); + public bool TryExplicitConvertTo([NotNull]ObjectHandle obj, Type type, [NotNullWhen(true)] out ObjectHandle? result) { + bool res = _ops.TryExplicitConvertTo(GetLocalObject(obj), type, out object? _); result = res ? new ObjectHandle(obj) : null; return res; } @@ -701,22 +714,22 @@ public ObjectHandle ImplicitConvertTo([NotNull]ObjectHandle obj, Type type) { /// /// Converts the object obj to the type T including implicit conversions. - /// + /// /// Returns true if the value can be converted, false if it cannot. /// - public bool TryImplicitConvertTo([NotNull]ObjectHandle obj, out ObjectHandle result) { - bool res = _ops.TryImplicitConvertTo(GetLocalObject(obj), out T _); + public bool TryImplicitConvertTo([NotNull]ObjectHandle obj, [NotNullWhen(true)] out ObjectHandle? result) { + bool res = _ops.TryImplicitConvertTo(GetLocalObject(obj), out _); result = res ? new ObjectHandle(obj) : null; return res; } /// - /// Converts the object obj to the type type including implicit conversions. - /// + /// Converts the object obj to the type type including implicit conversions. + /// /// Returns true if the value can be converted, false if it cannot. /// - public bool TryImplicitConvertTo([NotNull]ObjectHandle obj, Type type, out ObjectHandle result) { - bool res = _ops.TryImplicitConvertTo(GetLocalObject(obj), type, out object _); + public bool TryImplicitConvertTo([NotNull]ObjectHandle obj, Type type, [NotNullWhen(true)] out ObjectHandle? result) { + bool res = _ops.TryImplicitConvertTo(GetLocalObject(obj), type, out object? _); result = res ? new ObjectHandle(obj) : null; return res; } @@ -725,6 +738,7 @@ public bool TryImplicitConvertTo([NotNull]ObjectHandle obj, Type type, out Objec /// Unwraps the remote object and converts it into the specified type before /// returning it. /// + [return: MaybeNull] public T Unwrap([NotNull]ObjectHandle obj) { return ConvertTo(GetLocalObject(obj)); } @@ -733,91 +747,91 @@ public T Unwrap([NotNull]ObjectHandle obj) { /// Performs the specified unary operator on the remote object. /// public ObjectHandle DoOperation(ExpressionType op, [NotNull]ObjectHandle target) { - return new ObjectHandle((object)DoOperation(op, GetLocalObject(target))); + return new ObjectHandle((object?)DoOperation(op, GetLocalObject(target))); } /// /// Performs the specified binary operator on the remote object. /// public ObjectHandle DoOperation(ExpressionType op, ObjectHandle target, ObjectHandle other) { - return new ObjectHandle((object)DoOperation(op, GetLocalObject(target), GetLocalObject(other))); + return new ObjectHandle((object?)DoOperation(op, GetLocalObject(target), GetLocalObject(other))); } /// /// Adds the two remote objects. Throws an exception if the operation cannot be performed. /// public ObjectHandle Add([NotNull]ObjectHandle self, [NotNull]ObjectHandle other) { - return new ObjectHandle((object)Add(GetLocalObject(self), GetLocalObject(other))); + return new ObjectHandle((object?)Add(GetLocalObject(self), GetLocalObject(other))); } /// /// Subtracts the 1st remote object from the second. Throws an exception if the operation cannot be performed. /// public ObjectHandle Subtract([NotNull]ObjectHandle self, [NotNull]ObjectHandle other) { - return new ObjectHandle((object)Subtract(GetLocalObject(self), GetLocalObject(other))); + return new ObjectHandle((object?)Subtract(GetLocalObject(self), GetLocalObject(other))); } /// /// Raises the 1st remote object to the power of the 2nd. Throws an exception if the operation cannot be performed. /// public ObjectHandle Power([NotNull]ObjectHandle self, [NotNull]ObjectHandle other) { - return new ObjectHandle((object)Power(GetLocalObject(self), GetLocalObject(other))); + return new ObjectHandle((object?)Power(GetLocalObject(self), GetLocalObject(other))); } /// /// Multiplies the two remote objects. Throws an exception if the operation cannot be performed. /// public ObjectHandle Multiply([NotNull]ObjectHandle self, [NotNull]ObjectHandle other) { - return new ObjectHandle((object)Multiply(GetLocalObject(self), GetLocalObject(other))); + return new ObjectHandle((object?)Multiply(GetLocalObject(self), GetLocalObject(other))); } /// /// Divides the 1st remote object by the 2nd. Throws an exception if the operation cannot be performed. /// public ObjectHandle Divide([NotNull]ObjectHandle self, [NotNull]ObjectHandle other) { - return new ObjectHandle((object)Divide(GetLocalObject(self), GetLocalObject(other))); + return new ObjectHandle((object?)Divide(GetLocalObject(self), GetLocalObject(other))); } /// /// Performs modulus on the 1st remote object by the 2nd. Throws an exception if the operation cannot be performed. - /// + /// public ObjectHandle Modulo([NotNull]ObjectHandle self, [NotNull]ObjectHandle other) { - return new ObjectHandle((object)Modulo(GetLocalObject(self), GetLocalObject(other))); + return new ObjectHandle((object?)Modulo(GetLocalObject(self), GetLocalObject(other))); } /// /// Shifts the 1st remote object left by the 2nd remote object. Throws an exception if the operation cannot be performed. /// public ObjectHandle LeftShift([NotNull]ObjectHandle self, [NotNull]ObjectHandle other) { - return new ObjectHandle((object)LeftShift(GetLocalObject(self), GetLocalObject(other))); + return new ObjectHandle((object?)LeftShift(GetLocalObject(self), GetLocalObject(other))); } /// /// Shifts the 1st remote object right by the 2nd remote object. Throws an exception if the operation cannot be performed. /// public ObjectHandle RightShift([NotNull]ObjectHandle self, [NotNull]ObjectHandle other) { - return new ObjectHandle((object)RightShift(GetLocalObject(self), GetLocalObject(other))); + return new ObjectHandle((object?)RightShift(GetLocalObject(self), GetLocalObject(other))); } /// /// Performs bitwise-and on the two remote objects. Throws an exception if the operation cannot be performed. /// public ObjectHandle BitwiseAnd([NotNull]ObjectHandle self, [NotNull]ObjectHandle other) { - return new ObjectHandle((object)BitwiseAnd(GetLocalObject(self), GetLocalObject(other))); + return new ObjectHandle((object?)BitwiseAnd(GetLocalObject(self), GetLocalObject(other))); } /// /// Performs bitwise-or on the two remote objects. Throws an exception if the operation cannot be performed. /// public ObjectHandle BitwiseOr([NotNull]ObjectHandle self, [NotNull]ObjectHandle other) { - return new ObjectHandle((object)BitwiseOr(GetLocalObject(self), GetLocalObject(other))); + return new ObjectHandle((object?)BitwiseOr(GetLocalObject(self), GetLocalObject(other))); } /// /// Performs exclusive-or on the two remote objects. Throws an exception if the operation cannot be performed. /// public ObjectHandle ExclusiveOr([NotNull]ObjectHandle self, [NotNull]ObjectHandle other) { - return new ObjectHandle((object)ExclusiveOr(GetLocalObject(self), GetLocalObject(other))); + return new ObjectHandle((object?)ExclusiveOr(GetLocalObject(self), GetLocalObject(other))); } /// @@ -893,7 +907,7 @@ public IList GetCallSignatures([NotNull]ObjectHandle obj) { /// /// Helper to unwrap an object - in the future maybe we should validate the current app domain. /// - private static object GetLocalObject([NotNull]ObjectHandle obj) { + private static object? GetLocalObject([NotNull]ObjectHandle obj) { ContractUtils.RequiresNotNull(obj, nameof(obj)); return obj.Unwrap(); @@ -902,10 +916,10 @@ private static object GetLocalObject([NotNull]ObjectHandle obj) { /// /// Helper to unwrap multiple objects /// - private static object[] GetLocalObjects(ObjectHandle[] ohs) { - Debug.Assert(ohs is not null); + private static object?[] GetLocalObjects(ObjectHandle[] ohs) { + Assert.NotNull(ohs); - object[] res = new object[ohs.Length]; + object?[] res = new object?[ohs.Length]; for (int i = 0; i < res.Length; i++) { res[i] = GetLocalObject(ohs[i]); } @@ -913,8 +927,7 @@ private static object[] GetLocalObjects(ObjectHandle[] ohs) { return res; } - // TODO: Figure out what is the right lifetime - public override object InitializeLifetimeService() { + public override object? InitializeLifetimeService() { return null; } #endif diff --git a/src/core/Microsoft.Scripting/Hosting/Providers/HostingHelpers.cs b/src/core/Microsoft.Scripting/Hosting/Providers/HostingHelpers.cs index 02f3c3ef..c7b5ef15 100644 --- a/src/core/Microsoft.Scripting/Hosting/Providers/HostingHelpers.cs +++ b/src/core/Microsoft.Scripting/Hosting/Providers/HostingHelpers.cs @@ -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_REMOTING using System.Runtime.Remoting; #else diff --git a/src/core/Microsoft.Scripting/Hosting/ScriptEngine.cs b/src/core/Microsoft.Scripting/Hosting/ScriptEngine.cs index f1c27fc0..2ea8a812 100644 --- a/src/core/Microsoft.Scripting/Hosting/ScriptEngine.cs +++ b/src/core/Microsoft.Scripting/Hosting/ScriptEngine.cs @@ -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_REMOTING using System.Runtime.Remoting; #else @@ -12,6 +14,7 @@ using System.CodeDom; using System.Collections.Generic; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.Dynamic; using System.Text; using System.Threading; @@ -27,12 +30,11 @@ namespace Microsoft.Scripting.Hosting { /// [DebuggerDisplay("{Setup.DisplayName}")] public sealed class ScriptEngine : MarshalByRefObject { - private LanguageSetup _config; - private ObjectOperations _operations; + private LanguageSetup? _config; + private ObjectOperations? _operations; internal ScriptEngine(ScriptRuntime runtime, LanguageContext context) { - Debug.Assert(runtime is not null); - Debug.Assert(context is not null); + Assert.NotNull(runtime, context); Runtime = runtime; LanguageContext = context; @@ -94,10 +96,10 @@ public ObjectOperations CreateOperations(ScriptScope scope) { /// /// The engine doesn't support code execution. /// is a null reference. - public dynamic Execute(string expression) { - // The host doesn't need the scope so do not create it here. + public dynamic? Execute(string expression) { + // The host doesn't need the scope so do not create it here. // The language can treat the code as not bound to a DLR scope and change global lookup semantics accordingly. - return CreateScriptSourceFromString(expression).Execute(); + return CreateScriptSourceFromString(expression).Execute(); } /// @@ -106,7 +108,7 @@ public dynamic Execute(string expression) { /// The engine doesn't support code execution. /// is a null reference. /// is a null reference. - public dynamic Execute(string expression, ScriptScope scope) { + public dynamic? Execute(string expression, ScriptScope scope) { return CreateScriptSourceFromString(expression).Execute(scope); } @@ -115,18 +117,20 @@ public dynamic Execute(string expression, ScriptScope scope) { /// /// The engine doesn't support code execution. /// is a null reference. + [return: MaybeNull] public T Execute(string expression) { - return Operations.ConvertTo((object)Execute(expression)); + return Operations.ConvertTo((object?)Execute(expression)); } - + /// /// Executes an expression within the specified scope and converts result to the given type. /// /// The engine doesn't support code execution. /// is a null reference. /// is a null reference. + [return: MaybeNull] public T Execute(string expression, ScriptScope scope) { - return Operations.ConvertTo((object)Execute(expression, scope)); + return Operations.ConvertTo((object?)Execute(expression, scope)); } /// @@ -153,37 +157,37 @@ public ScriptScope ExecuteFile(string path, ScriptScope scope) { #if FEATURE_REMOTING /// /// 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. /// public ObjectHandle ExecuteAndWrap(string expression, ScriptScope scope) { - return new ObjectHandle((object)Execute(expression, scope)); + return new ObjectHandle((object?)Execute(expression, scope)); } /// /// 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. /// public ObjectHandle ExecuteAndWrap(string expression) { - return new ObjectHandle((object)Execute(expression)); + return new ObjectHandle((object?)Execute(expression)); } /// /// 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. /// /// - /// Use this API in case the exception is not serializable (for example, due to security restrictions) or its serialization + /// Use this API in case the exception is not serializable (for example, due to security restrictions) or its serialization /// loses information that you need to access. /// [Obsolete("Use ScriptSource.ExecuteAndWrap instead")] - [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")] - public ObjectHandle ExecuteAndWrap(string expression, ScriptScope scope, out ObjectHandle exception) { + [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")] + public ObjectHandle? ExecuteAndWrap(string expression, ScriptScope scope, out ObjectHandle? exception) { exception = null; try { - return new ObjectHandle((object)Execute(expression, scope)); + return new ObjectHandle((object?)Execute(expression, scope)); } catch (Exception e) { exception = new ObjectHandle(e); return null; @@ -192,21 +196,21 @@ public ObjectHandle ExecuteAndWrap(string expression, ScriptScope scope, out Obj /// /// 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. /// /// - /// Use this API in case the exception is not serializable (for example, due to security restrictions) or its serialization + /// Use this API in case the exception is not serializable (for example, due to security restrictions) or its serialization /// loses information that you need to access. /// [Obsolete("Use ScriptSource.ExecuteAndWrap instead")] - [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")] - public ObjectHandle ExecuteAndWrap(string expression, out ObjectHandle exception) { + [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")] + public ObjectHandle? ExecuteAndWrap(string expression, out ObjectHandle? exception) { exception = null; try { - return new ObjectHandle((object)Execute(expression)); + return new ObjectHandle((object?)Execute(expression)); } catch (Exception e) { exception = new ObjectHandle(e); return null; @@ -230,7 +234,7 @@ public ScriptScope CreateScope() { /// /// Accesses to the ScriptScope will turn into get,set, and delete members against this dictionary /// - public ScriptScope CreateScope(IDictionary dictionary) + public ScriptScope CreateScope(IDictionary dictionary) { ContractUtils.RequiresNotNull(dictionary, nameof(dictionary)); return new ScriptScope(this, LanguageContext.CreateScope(dictionary)); @@ -260,9 +264,9 @@ public ScriptScope CreateScope(IDynamicMetaObjectProvider storage) { /// The tool would need to find Bar's ScriptScope for setting the appropriate context in its interpreter window. /// This method helps with this scenario. /// - public ScriptScope GetScope(string path) { + public ScriptScope? GetScope(string path) { ContractUtils.RequiresNotNull(path, nameof(path)); - Scope scope = LanguageContext.GetScope(path); + Scope? scope = LanguageContext.GetScope(path); return (scope is not null) ? new ScriptScope(this, scope) : null; } @@ -300,7 +304,7 @@ public ScriptSource CreateScriptSourceFromString(string code, SourceCodeKind kin /// /// The default SourceCodeKind is AutoDetect. /// - public ScriptSource CreateScriptSourceFromString(string expression, string path) { + public ScriptSource CreateScriptSourceFromString(string expression, string? path) { ContractUtils.RequiresNotNull(expression, nameof(expression)); return CreateScriptSource(new SourceStringContentProvider(expression), path, SourceCodeKind.AutoDetect); @@ -309,7 +313,7 @@ public ScriptSource CreateScriptSourceFromString(string expression, string path) /// /// Return a ScriptSource object from string contents. These are helpers for creating ScriptSources' with the right language binding. /// - public ScriptSource CreateScriptSourceFromString(string code, string path, SourceCodeKind kind) { + public ScriptSource CreateScriptSourceFromString(string code, string? path, SourceCodeKind kind) { ContractUtils.RequiresNotNull(code, nameof(code)); ContractUtils.Requires(kind.IsValid(), nameof(kind)); @@ -365,12 +369,12 @@ public ScriptSource CreateScriptSourceFromFile(string path, Encoding encoding, S #if FEATURE_CODEDOM /// - /// This method returns a ScriptSource object from a System.CodeDom.CodeObject. + /// This method returns a ScriptSource object from a System.CodeDom.CodeObject. /// This is a factory method for creating a ScriptSources with this language binding. - /// - /// The expected CodeDom support is extremely minimal for syntax-independent expression of semantics. - /// - /// Languages may do more, but hosts should only expect CodeMemberMethod support, + /// + /// The expected CodeDom support is extremely minimal for syntax-independent expression of semantics. + /// + /// Languages may do more, but hosts should only expect CodeMemberMethod support, /// and only sub nodes consisting of the following: /// CodeSnippetStatement /// CodeSnippetExpression @@ -383,12 +387,12 @@ public ScriptSource CreateScriptSource(CodeObject content) { } /// - /// This method returns a ScriptSource object from a System.CodeDom.CodeObject. + /// This method returns a ScriptSource object from a System.CodeDom.CodeObject. /// This is a factory method for creating a ScriptSources with this language binding. - /// - /// The expected CodeDom support is extremely minimal for syntax-independent expression of semantics. - /// - /// Languages may do more, but hosts should only expect CodeMemberMethod support, + /// + /// The expected CodeDom support is extremely minimal for syntax-independent expression of semantics. + /// + /// Languages may do more, but hosts should only expect CodeMemberMethod support, /// and only sub nodes consisting of the following: /// CodeSnippetStatement /// CodeSnippetExpression @@ -396,17 +400,17 @@ public ScriptSource CreateScriptSource(CodeObject content) { /// CodeMethodInvokeExpression /// CodeExpressionStatement (for holding MethodInvoke) /// - public ScriptSource CreateScriptSource(CodeObject content, string path) { + public ScriptSource CreateScriptSource(CodeObject content, string? path) { return CreateScriptSource(content, path, SourceCodeKind.File); } /// - /// This method returns a ScriptSource object from a System.CodeDom.CodeObject. + /// This method returns a ScriptSource object from a System.CodeDom.CodeObject. /// This is a factory method for creating a ScriptSources with this language binding. - /// - /// The expected CodeDom support is extremely minimal for syntax-independent expression of semantics. - /// - /// Languages may do more, but hosts should only expect CodeMemberMethod support, + /// + /// The expected CodeDom support is extremely minimal for syntax-independent expression of semantics. + /// + /// Languages may do more, but hosts should only expect CodeMemberMethod support, /// and only sub nodes consisting of the following: /// CodeSnippetStatement /// CodeSnippetExpression @@ -419,12 +423,12 @@ public ScriptSource CreateScriptSource(CodeObject content, SourceCodeKind kind) } /// - /// This method returns a ScriptSource object from a System.CodeDom.CodeObject. + /// This method returns a ScriptSource object from a System.CodeDom.CodeObject. /// This is a factory method for creating a ScriptSources with this language binding. - /// - /// The expected CodeDom support is extremely minimal for syntax-independent expression of semantics. - /// - /// Languages may do more, but hosts should only expect CodeMemberMethod support, + /// + /// The expected CodeDom support is extremely minimal for syntax-independent expression of semantics. + /// + /// Languages may do more, but hosts should only expect CodeMemberMethod support, /// and only sub nodes consisting of the following: /// CodeSnippetStatement /// CodeSnippetExpression @@ -432,7 +436,7 @@ public ScriptSource CreateScriptSource(CodeObject content, SourceCodeKind kind) /// CodeMethodInvokeExpression /// CodeExpressionStatement (for holding MethodInvoke) /// - public ScriptSource CreateScriptSource(CodeObject content, string path, SourceCodeKind kind) { + public ScriptSource CreateScriptSource(CodeObject content, string? path, SourceCodeKind kind) { ContractUtils.RequiresNotNull(content, nameof(content)); if (!LanguageContext.CanCreateSourceCode) throw new NotSupportedException("Invariant engine cannot create scripts"); @@ -441,24 +445,24 @@ public ScriptSource CreateScriptSource(CodeObject content, string path, SourceCo #endif /// - /// These methods return ScriptSource objects from stream contents with the current engine as the language binding. - /// + /// These methods return ScriptSource objects from stream contents with the current engine as the language binding. + /// /// The default SourceCodeKind is File. - /// + /// /// The encoding defaults to the default encoding of the language. /// - public ScriptSource CreateScriptSource(StreamContentProvider content, string path) { + public ScriptSource CreateScriptSource(StreamContentProvider content, string? path) { ContractUtils.RequiresNotNull(content, nameof(content)); return CreateScriptSource(content, path, LanguageContext.DefaultEncoding, SourceCodeKind.File); } /// - /// These methods return ScriptSource objects from stream contents with the current engine as the language binding. - /// + /// These methods return ScriptSource objects from stream contents with the current engine as the language binding. + /// /// The default SourceCodeKind is File. /// - public ScriptSource CreateScriptSource(StreamContentProvider content, string path, Encoding encoding) { + public ScriptSource CreateScriptSource(StreamContentProvider content, string? path, Encoding encoding) { ContractUtils.RequiresNotNull(content, nameof(content)); ContractUtils.RequiresNotNull(encoding, nameof(encoding)); @@ -466,11 +470,11 @@ public ScriptSource CreateScriptSource(StreamContentProvider content, string pat } /// - /// These methods return ScriptSource objects from stream contents with the current engine as the language binding. - /// + /// These methods return ScriptSource objects from stream contents with the current engine as the language binding. + /// /// The encoding defaults to Encoding.Default. /// - public ScriptSource CreateScriptSource(StreamContentProvider content, string path, Encoding encoding, SourceCodeKind kind) { + public ScriptSource CreateScriptSource(StreamContentProvider content, string? path, Encoding encoding, SourceCodeKind kind) { ContractUtils.RequiresNotNull(content, nameof(content)); ContractUtils.RequiresNotNull(encoding, nameof(encoding)); ContractUtils.Requires(kind.IsValid(), nameof(kind)); @@ -480,10 +484,10 @@ public ScriptSource CreateScriptSource(StreamContentProvider content, string pat /// /// This method returns a ScriptSource with the content provider supplied with the current engine as the language binding. - /// + /// /// This helper lets you own the content provider so that you can implement a stream over internal host data structures, such as an editor's text representation. /// - public ScriptSource CreateScriptSource(TextContentProvider contentProvider, string path, SourceCodeKind kind) { + public ScriptSource CreateScriptSource(TextContentProvider contentProvider, string? path, SourceCodeKind kind) { ContractUtils.RequiresNotNull(contentProvider, nameof(contentProvider)); ContractUtils.Requires(kind.IsValid(), nameof(kind)); if (!LanguageContext.CanCreateSourceCode) throw new NotSupportedException("Invariant engine cannot create scripts"); @@ -506,22 +510,22 @@ public ScriptSource CreateScriptSource(TextContentProvider contentProvider, stri /// Provides standardized tokenization of source code /// ExceptionOperations /// Provides formatting of exception objects. - /// DocumentationProvidera + /// DocumentationProvider /// Provides documentation for live object. /// - public TService GetService(params object[] args) where TService : class { + public TService? GetService(params object?[] args) where TService : class { if (typeof(TService) == typeof(TokenCategorizer)) { - TokenizerService service = LanguageContext.GetService(ArrayUtils.Insert((object)LanguageContext, args)); + TokenizerService? service = LanguageContext.GetService(ArrayUtils.Insert((object?)LanguageContext, args)); return (service is not null) ? (TService)(object)new TokenCategorizer(service) : null; } if (typeof(TService) == typeof(ExceptionOperations)) { - ExceptionOperations service = LanguageContext.GetService(); + ExceptionOperations? service = LanguageContext.GetService(); return (service is not null) ? (TService)(object)service : (TService)(object)new ExceptionOperations(LanguageContext); } if (typeof(TService) == typeof(DocumentationOperations)) { - DocumentationProvider service = LanguageContext.GetService(args); + DocumentationProvider? service = LanguageContext.GetService(args); return (service is not null) ? (TService)(object)new DocumentationOperations(service) : null; } @@ -546,14 +550,15 @@ public LanguageSetup Setup { Debug.Assert(LanguageContext is not InvariantContext); // Find the matching language configuration - LanguageConfiguration config = Runtime.Manager.Configuration.GetLanguageConfig(LanguageContext); - Debug.Assert(config is not null); + LanguageConfiguration config = Runtime.Manager.Configuration.GetLanguageConfig(LanguageContext)!; + Debug.Assert(config as object is not null); foreach (var language in Runtime.Setup.LanguageSetups) { if (config.ProviderName == new AssemblyQualifiedTypeName(language.TypeName)) { return _config = language; } } + throw new InvalidOperationException($"LanguageContext '{LanguageContext.GetType().FullName}' is not properly registered in DlrConfiguration"); } return _config; } @@ -614,8 +619,7 @@ internal TRet Call(Func f, T arg) { #region Remote API #if FEATURE_REMOTING - // TODO: Figure out what is the right lifetime - public override object InitializeLifetimeService() { + public override object? InitializeLifetimeService() { return null; } #endif diff --git a/src/core/Microsoft.Scripting/Hosting/ScriptHost.cs b/src/core/Microsoft.Scripting/Hosting/ScriptHost.cs index 1fb1ae6c..9aedbf09 100644 --- a/src/core/Microsoft.Scripting/Hosting/ScriptHost.cs +++ b/src/core/Microsoft.Scripting/Hosting/ScriptHost.cs @@ -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_REMOTING using System.Runtime.Remoting; #else @@ -30,9 +32,9 @@ public class ScriptHost : MarshalByRefObject { /// /// The runtime the host is attached to. /// - private ScriptRuntime _runtime; - - // Called by ScriptRuntime when it is completely initialized. + private ScriptRuntime? _runtime; + + // Called by ScriptRuntime when it is completely initialized. // Notifies the host implementation that the runtime is available now. internal void SetRuntime(ScriptRuntime runtime) { Assert.NotNull(runtime); @@ -70,8 +72,7 @@ protected internal virtual void EngineCreated(ScriptEngine engine) { #endregion #if FEATURE_REMOTING - // TODO: Figure out what is the right lifetime - public override object InitializeLifetimeService() { + public override object? InitializeLifetimeService() { return null; } #endif diff --git a/src/core/Microsoft.Scripting/Hosting/ScriptHostProxy.cs b/src/core/Microsoft.Scripting/Hosting/ScriptHostProxy.cs index 0ee707ec..110dbd66 100644 --- a/src/core/Microsoft.Scripting/Hosting/ScriptHostProxy.cs +++ b/src/core/Microsoft.Scripting/Hosting/ScriptHostProxy.cs @@ -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 Microsoft.Scripting.Runtime; using Microsoft.Scripting.Utils; diff --git a/src/core/Microsoft.Scripting/Hosting/ScriptRuntime.cs b/src/core/Microsoft.Scripting/Hosting/ScriptRuntime.cs index 3f1a3bd1..c16ecd67 100644 --- a/src/core/Microsoft.Scripting/Hosting/ScriptRuntime.cs +++ b/src/core/Microsoft.Scripting/Hosting/ScriptRuntime.cs @@ -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_REMOTING using System.Runtime.Remoting; #else @@ -11,9 +13,10 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.Dynamic; -using System.Linq; using System.IO; +using System.Linq; using System.Reflection; using Microsoft.Scripting.Runtime; @@ -29,8 +32,8 @@ public sealed class ScriptRuntime : MarshalByRefObject { private readonly InvariantContext _invariantContext; private readonly Lock _lock = new(); private ScriptScope _globals; - private Scope _scopeGlobals; - private ScriptEngine _invariantEngine; + private Scope? _scopeGlobals; + private ScriptEngine? _invariantEngine; /// /// Creates ScriptRuntime in the current app-domain and initialized according to the the specified settings. @@ -45,9 +48,9 @@ public ScriptRuntime(ScriptRuntimeSetup setup) { Setup = setup; try { - Host = (ScriptHost)Activator.CreateInstance(setup.HostType, setup.HostArguments.ToArray()); + Host = (ScriptHost)Activator.CreateInstance(setup.HostType, setup.HostArguments.ToArray())!; } catch (TargetInvocationException e) { - throw new InvalidImplementationException(Strings.InvalidCtorImplementation(setup.HostType, e.InnerException.Message), e.InnerException); + throw new InvalidImplementationException(Strings.InvalidCtorImplementation(setup.HostType, e.InnerException?.Message ?? string.Empty), e.InnerException); } catch (Exception e) { throw new InvalidImplementationException(Strings.InvalidCtorImplementation(setup.HostType, e.Message), e); } @@ -66,7 +69,7 @@ public ScriptRuntime(ScriptRuntimeSetup setup) { Host.SetRuntime(this); - if (!setup.Options.TryGetValue("NoDefaultReferences", out object noDefaultRefs) || Convert.ToBoolean(noDefaultRefs) == false) { + if (!setup.Options.TryGetValue("NoDefaultReferences", out object? noDefaultRefs) || Convert.ToBoolean(noDefaultRefs) == false) { LoadAssembly(typeof(string).Assembly); LoadAssembly(typeof(Debug).Assembly); } @@ -98,19 +101,18 @@ public static ScriptRuntime CreateFromConfiguration() { public static ScriptRuntime CreateRemote(AppDomain domain, ScriptRuntimeSetup setup) { ContractUtils.RequiresNotNull(domain, nameof(domain)); return (ScriptRuntime)domain.CreateInstanceAndUnwrap( - typeof(ScriptRuntime).Assembly.FullName, - typeof(ScriptRuntime).FullName, - false, - BindingFlags.Default, - null, - new object[] { setup }, + typeof(ScriptRuntime).Assembly.FullName!, + typeof(ScriptRuntime).FullName!, + false, + BindingFlags.Default, + null, + new object[] { setup }, null, null - ); + )!; } - // TODO: Figure out what is the right lifetime - public override object InitializeLifetimeService() { + public override object? InitializeLifetimeService() { return null; } #endif @@ -123,7 +125,7 @@ public override object InitializeLifetimeService() { public ScriptEngine GetEngine(string languageName) { ContractUtils.RequiresNotNull(languageName, nameof(languageName)); - if (!TryGetEngine(languageName, out ScriptEngine engine)) { + if (!TryGetEngine(languageName, out ScriptEngine? engine)) { throw new ArgumentException($"Unknown language name: '{languageName}'"); } @@ -140,15 +142,15 @@ public ScriptEngine GetEngineByTypeName(string assemblyQualifiedTypeName) { public ScriptEngine GetEngineByFileExtension(string fileExtension) { ContractUtils.RequiresNotNull(fileExtension, nameof(fileExtension)); - if (!TryGetEngineByFileExtension(fileExtension, out ScriptEngine engine)) { + if (!TryGetEngineByFileExtension(fileExtension, out ScriptEngine? engine)) { throw new ArgumentException($"Unknown file extension: '{fileExtension}'"); } return engine; } - public bool TryGetEngine(string languageName, out ScriptEngine engine) { - if (!Manager.TryGetLanguage(languageName, out LanguageContext language)) { + public bool TryGetEngine(string languageName, [NotNullWhen(true)] out ScriptEngine? engine) { + if (!Manager.TryGetLanguage(languageName, out LanguageContext? language)) { engine = null; return false; } @@ -157,8 +159,8 @@ public bool TryGetEngine(string languageName, out ScriptEngine engine) { return true; } - public bool TryGetEngineByFileExtension(string fileExtension, out ScriptEngine engine) { - if (!Manager.TryGetLanguageByFileExtension(fileExtension, out LanguageContext language)) { + public bool TryGetEngineByFileExtension(string fileExtension, [NotNullWhen(true)] out ScriptEngine? engine) { + if (!Manager.TryGetLanguageByFileExtension(fileExtension, out LanguageContext? language)) { engine = null; return false; } @@ -191,11 +193,14 @@ internal ScriptEngine GetEngine(LanguageContext language) { /// The method doesn't lock nor send notifications to the host. /// private ScriptEngine GetEngineNoLockNoNotification(LanguageContext language, out bool freshEngineCreated) { - Debug.Assert(_engines is not null, "Invalid ScriptRuntime initialization order"); + Assert.NotNull(_engines); - if (freshEngineCreated = !_engines.TryGetValue(language, out ScriptEngine engine)) { + if (!_engines.TryGetValue(language, out ScriptEngine? engine)) { engine = new ScriptEngine(this, language); _engines.Add(language, engine); + freshEngineCreated = true; + } else { + freshEngineCreated = false; } return engine; } @@ -220,11 +225,11 @@ public ScriptScope CreateScope(string languageId, IDynamicMetaObjectProvider sto return GetEngine(languageId).CreateScope(storage); } - public ScriptScope CreateScope(IDictionary dictionary) { + public ScriptScope CreateScope(IDictionary dictionary) { return InvariantEngine.CreateScope(dictionary); } - public ScriptScope CreateScope(string languageId, IDictionary storage) { + public ScriptScope CreateScope(string languageId, IDictionary storage) { return GetEngine(languageId).CreateScope(storage); } @@ -238,7 +243,7 @@ public ScriptScope ExecuteFile(string path) { ContractUtils.RequiresNotEmpty(path, nameof(path)); string extension = Path.GetExtension(path); - if (!TryGetEngineByFileExtension(extension, out ScriptEngine engine)) { + if (!TryGetEngineByFileExtension(extension, out ScriptEngine? engine)) { throw new ArgumentException($"File extension '{extension}' is not associated with any language."); } @@ -253,7 +258,7 @@ public ScriptScope UseFile(string path) { ContractUtils.RequiresNotEmpty(path, nameof(path)); string extension = Path.GetExtension(path); - if (!TryGetEngineByFileExtension(extension, out ScriptEngine engine)) { + if (!TryGetEngineByFileExtension(extension, out ScriptEngine? engine)) { throw new ArgumentException($"File extension '{extension}' is not associated with any language."); } @@ -266,7 +271,7 @@ public ScriptScope UseFile(string path) { // See if the file is already loaded, if so return the scope foreach (string searchPath in searchPaths) { string filePath = Path.Combine(searchPath, path); - ScriptScope scope = engine.GetScope(filePath); + ScriptScope? scope = engine.GetScope(filePath); if (scope is not null) { return scope; } @@ -347,6 +352,6 @@ public void Shutdown() { } internal ScriptEngine InvariantEngine => - _invariantEngine ?? (_invariantEngine = GetEngine(_invariantContext)); + _invariantEngine ??= GetEngine(_invariantContext); } } diff --git a/src/core/Microsoft.Scripting/Hosting/ScriptRuntimeSetup.cs b/src/core/Microsoft.Scripting/Hosting/ScriptRuntimeSetup.cs index c820d612..106754f0 100644 --- a/src/core/Microsoft.Scripting/Hosting/ScriptRuntimeSetup.cs +++ b/src/core/Microsoft.Scripting/Hosting/ScriptRuntimeSetup.cs @@ -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; using System.Collections.ObjectModel; @@ -160,7 +162,7 @@ public static ScriptRuntimeSetup ReadConfiguration() { #if FEATURE_CONFIGURATION - private static void LoadRuntimeSetup(ScriptRuntimeSetup setup, Stream configFileStream) { + private static void LoadRuntimeSetup(ScriptRuntimeSetup setup, Stream? configFileStream) { try { LoadRuntimeSetupImpl(setup, configFileStream); } catch (FileNotFoundException) { @@ -169,7 +171,7 @@ private static void LoadRuntimeSetup(ScriptRuntimeSetup setup, Stream configFile } [MethodImpl(MethodImplOptions.NoInlining)] - private static void LoadRuntimeSetupImpl(ScriptRuntimeSetup setup, Stream configFileStream) { + private static void LoadRuntimeSetupImpl(ScriptRuntimeSetup setup, Stream? configFileStream) { Configuration.Section.LoadRuntimeSetup(setup, configFileStream); } diff --git a/src/core/Microsoft.Scripting/Hosting/ScriptScope.cs b/src/core/Microsoft.Scripting/Hosting/ScriptScope.cs index 9bbadce8..c60beecf 100644 --- a/src/core/Microsoft.Scripting/Hosting/ScriptScope.cs +++ b/src/core/Microsoft.Scripting/Hosting/ScriptScope.cs @@ -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_REMOTING using System.Runtime.Remoting; #else @@ -11,6 +13,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.Dynamic; using System.Linq.Expressions; using System.Runtime.Serialization; @@ -50,7 +53,7 @@ internal ScriptScope(ScriptEngine engine, Scope scope) { /// /// The specified name is not defined in the scope. /// is a null reference. - public dynamic GetVariable(string name) { + public dynamic? GetVariable(string name) { return Engine.LanguageContext.ScopeGetVariable(Scope, name); } @@ -61,6 +64,7 @@ public dynamic GetVariable(string name) { /// /// The specified name is not defined in the scope. /// is a null reference. + [return: MaybeNull] public T GetVariable(string name) { return Engine.LanguageContext.ScopeGetVariable(Scope, name); } @@ -69,7 +73,7 @@ public T GetVariable(string name) { /// Tries to get a value stored in the scope under the given name. /// /// is a null reference. - public bool TryGetVariable(string name, out dynamic value) { + public bool TryGetVariable(string name, out dynamic? value) { return Engine.LanguageContext.ScopeTryGetVariable(Scope, name, out value); } @@ -79,8 +83,8 @@ public bool TryGetVariable(string name, out dynamic value) { /// If no language is associated with the scope, the default CLR conversion is attempted. /// /// is a null reference. - public bool TryGetVariable(string name, out T value) { - if(Engine.LanguageContext.ScopeTryGetVariable(Scope, name, out object result)) { + public bool TryGetVariable(string name, [MaybeNull] out T value) { + if (Engine.LanguageContext.ScopeTryGetVariable(Scope, name, out object? result)) { value = Engine.Operations.ConvertTo(result); return true; } @@ -92,7 +96,7 @@ public bool TryGetVariable(string name, out T value) { /// Sets the name to the specified value. /// /// is a null reference. - public void SetVariable(string name, object value) { + public void SetVariable(string name, object? value) { Engine.LanguageContext.ScopeSetVariable(Scope, name, value); } @@ -103,16 +107,16 @@ public void SetVariable(string name, object value) { /// The specified name is not defined in the scope. /// is a null reference. public ObjectHandle GetVariableHandle(string name) { - return new ObjectHandle((object)GetVariable(name)); + return new ObjectHandle((object?)GetVariable(name)); } /// /// Tries to get a handle for a value stored in the scope under the given name. - /// Returns true if there is such name, false otherwise. + /// Returns true if there is such name, false otherwise. /// /// is a null reference. - public bool TryGetVariableHandle(string name, out ObjectHandle handle) { - if (TryGetVariable(name, out object value)) { + public bool TryGetVariableHandle(string name, [NotNullWhen(true)] out ObjectHandle? handle) { + if (TryGetVariable(name, out object? value)) { handle = new ObjectHandle(value); return true; } @@ -139,8 +143,7 @@ public void SetVariable(string name, ObjectHandle handle) { /// /// is a null reference. public bool ContainsVariable(string name) { - object dummy; - return TryGetVariable(name, out dummy); + return TryGetVariable(name, out object? _); } /// @@ -160,7 +163,7 @@ public bool RemoveVariable(string name) { /// /// Gets a list of variable names stored in the scope. /// - [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")] + [SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")] public IEnumerable GetVariableNames() { // Remoting: we eagerly enumerate all variables to avoid cross domain calls for each item. return Engine.Operations.GetMemberNames((object)Scope.Storage); @@ -169,13 +172,13 @@ public IEnumerable GetVariableNames() { /// /// Gets an array of variable names and their values stored in the scope. /// - [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")] - public IEnumerable> GetItems() { + [SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")] + public IEnumerable> GetItems() { // Remoting: we eagerly enumerate all variables to avoid cross domain calls for each item. - var result = new List>(); - + var result = new List>(); + foreach (string name in GetVariableNames()) { - result.Add(new KeyValuePair(name, (object)Engine.Operations.GetMember((object)Scope.Storage, name))); + result.Add(new KeyValuePair(name, (object?)Engine.Operations.GetMember((object)Scope.Storage, name))); } result.TrimExcess(); @@ -198,7 +201,7 @@ public System.Collections.Hashtable Variables { get { System.Collections.Hashtable result = new System.Collections.Hashtable(); foreach (var variable in _scope.GetItems()) { - result[variable.Key] = (object)variable.Value; + result[variable.Key] = (object?)variable.Value; } return result; } @@ -229,7 +232,7 @@ public override DynamicMetaObject BindGetMember(GetMemberBinder action) { Expression.Condition( Expression.Call( Expression.Convert(Expression, typeof(ScriptScope)), - typeof(ScriptScope).GetMethod(nameof(ScriptScope.TryGetVariable), new[] { typeof(string), typeof(object).MakeByRefType() }), + typeof(ScriptScope).GetMethod(nameof(ScriptScope.TryGetVariable), new[] { typeof(string), typeof(object).MakeByRefType() })!, Expression.Constant(action.Name), result ), @@ -248,7 +251,7 @@ public override DynamicMetaObject BindSetMember(SetMemberBinder action, DynamicM Expression.Block( Expression.Call( Expression.Convert(Expression, typeof(ScriptScope)), - typeof(ScriptScope).GetMethod(nameof(ScriptScope.SetVariable), new[] { typeof(string), typeof(object) }), + typeof(ScriptScope).GetMethod(nameof(ScriptScope.SetVariable), new[] { typeof(string), typeof(object) })!, Expression.Constant(action.Name), objValue ), @@ -265,7 +268,7 @@ public override DynamicMetaObject BindDeleteMember(DeleteMemberBinder action) { Expression.IfThenElse( Expression.Call( Expression.Convert(Expression, typeof(ScriptScope)), - typeof(ScriptScope).GetMethod(nameof(ScriptScope.RemoveVariable)), + typeof(ScriptScope).GetMethod(nameof(ScriptScope.RemoveVariable))!, Expression.Constant(action.Name) ), Expression.Empty(), @@ -288,7 +291,7 @@ public override DynamicMetaObject BindInvokeMember(InvokeMemberBinder action, Dy Expression.Condition( Expression.Call( Expression.Convert(Expression, typeof(ScriptScope)), - typeof(ScriptScope).GetMethod(nameof(ScriptScope.TryGetVariable), new[] { typeof(string), typeof(object).MakeByRefType() }), + typeof(ScriptScope).GetMethod(nameof(ScriptScope.TryGetVariable), new[] { typeof(string), typeof(object).MakeByRefType() })!, Expression.Constant(action.Name), result ), @@ -301,15 +304,14 @@ public override DynamicMetaObject BindInvokeMember(InvokeMemberBinder action, Dy } public override IEnumerable GetDynamicMemberNames() { - return ((ScriptScope)Value).GetVariableNames(); + return ((ScriptScope)Value!).GetVariableNames(); // Value initialized in the base constructor to non-null ScriptScope } } #endregion #if FEATURE_REMOTING - // TODO: Figure out what is the right lifetime - public override object InitializeLifetimeService() { + public override object? InitializeLifetimeService() { return null; } #endif diff --git a/src/core/Microsoft.Scripting/Hosting/ScriptSource.cs b/src/core/Microsoft.Scripting/Hosting/ScriptSource.cs index 28232fa6..d0d97dc9 100644 --- a/src/core/Microsoft.Scripting/Hosting/ScriptSource.cs +++ b/src/core/Microsoft.Scripting/Hosting/ScriptSource.cs @@ -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_REMOTING using System.Runtime.Remoting; #else @@ -9,8 +11,9 @@ #endif using System; -using System.IO; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; +using System.IO; using System.Text; using Microsoft.Scripting.Utils; @@ -25,12 +28,12 @@ public sealed class ScriptSource : MarshalByRefObject internal SourceUnit SourceUnit { get; } /// - /// Identification of the source unit. Assigned by the host. + /// Identification of the source unit. Assigned by the host. /// The format and semantics is host dependent (could be a path on file system or URL). /// null for anonymous script source. /// Cannot be an empty string. /// - public string Path => SourceUnit.Path; + public string? Path => SourceUnit.Path; private string DebugString => Path ?? ""; @@ -47,48 +50,48 @@ internal ScriptSource(ScriptEngine engine, SourceUnit sourceUnit) { #region Compilation and Execution /// - /// Compile the ScriptSource into CompileCode object that can be executed + /// Compile the ScriptSource into CompileCode object that can be executed /// repeatedly in its default scope or in other scopes without having to recompile the code. /// /// Code cannot be compiled. - public CompiledCode Compile() { + public CompiledCode? Compile() { return CompileInternal(null, null); } /// - /// Errors are reported to the specified listener. + /// Errors are reported to the specified listener. /// Returns null if the parser cannot compile the code due to errors. /// - public CompiledCode Compile(ErrorListener errorListener) { + public CompiledCode? Compile(ErrorListener errorListener) { ContractUtils.RequiresNotNull(errorListener, nameof(errorListener)); return CompileInternal(null, errorListener); } /// - /// Errors are reported to the specified listener. + /// Errors are reported to the specified listener. /// Returns null if the parser cannot compile the code due to error(s). /// - public CompiledCode Compile(CompilerOptions compilerOptions) { + public CompiledCode? Compile(CompilerOptions compilerOptions) { ContractUtils.RequiresNotNull(compilerOptions, nameof(compilerOptions)); return CompileInternal(compilerOptions, null); } /// - /// Errors are reported to the specified listener. + /// Errors are reported to the specified listener. /// Returns null if the parser cannot compile the code due to error(s). /// - public CompiledCode Compile(CompilerOptions compilerOptions, ErrorListener errorListener) { + public CompiledCode? Compile(CompilerOptions compilerOptions, ErrorListener errorListener) { ContractUtils.RequiresNotNull(errorListener, nameof(errorListener)); ContractUtils.RequiresNotNull(compilerOptions, nameof(compilerOptions)); return CompileInternal(compilerOptions, errorListener); } - private CompiledCode CompileInternal(CompilerOptions compilerOptions, ErrorListener errorListener) { + private CompiledCode? CompileInternal(CompilerOptions? compilerOptions, ErrorListener? errorListener) { ErrorSink errorSink = new ErrorListenerProxySink(this, errorListener); - ScriptCode code = compilerOptions is not null ? SourceUnit.Compile(compilerOptions, errorSink) : SourceUnit.Compile(errorSink); + ScriptCode? code = compilerOptions is not null ? SourceUnit.Compile(compilerOptions, errorSink) : SourceUnit.Compile(errorSink); return (code is not null) ? new CompiledCode(Engine, code) : null; } @@ -103,7 +106,7 @@ private CompiledCode CompileInternal(CompilerOptions compilerOptions, ErrorListe /// based may return null. /// /// Code cannot be compiled. - public dynamic Execute(ScriptScope scope) { + public dynamic? Execute(ScriptScope scope) { ContractUtils.RequiresNotNull(scope, nameof(scope)); return SourceUnit.Execute(scope.Scope); @@ -112,8 +115,8 @@ public dynamic Execute(ScriptScope scope) { /// /// Executes the source code. The execution is not bound to any particular scope. /// - public dynamic Execute() { - // The host doesn't need the scope so do not create it here. + public dynamic? Execute() { + // The host doesn't need the scope so do not create it here. // The language can treat the code as not bound to a DLR scope and change global lookup semantics accordingly. return SourceUnit.Execute(); } @@ -122,51 +125,53 @@ public dynamic Execute() { /// Executes the code in a specified scope and converts the result to the specified type. /// The conversion is language specific. /// + [return: MaybeNull] public T Execute(ScriptScope scope) { - return Engine.Operations.ConvertTo((object)Execute(scope)); + return Engine.Operations.ConvertTo((object?)Execute(scope)); } /// /// Executes the code in an empty scope and converts the result to the specified type. /// The conversion is language specific. /// + [return: MaybeNull] public T Execute() { - return Engine.Operations.ConvertTo((object)Execute()); + return Engine.Operations.ConvertTo((object?)Execute()); } #if FEATURE_REMOTING /// /// 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. /// public ObjectHandle ExecuteAndWrap() { - return new ObjectHandle((object)Execute()); + return new ObjectHandle((object?)Execute()); } /// /// 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. /// public ObjectHandle ExecuteAndWrap(ScriptScope scope) { - return new ObjectHandle((object)Execute(scope)); + return new ObjectHandle((object?)Execute(scope)); } /// /// 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. /// /// - /// 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. /// - [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; @@ -175,20 +180,20 @@ public ObjectHandle ExecuteAndWrap(out ObjectHandle exception) { /// /// 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. /// /// - /// 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. /// - [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; @@ -213,7 +218,7 @@ public int ExecuteProgram() { #endregion - [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")] + [SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")] public ScriptCodeParseResult GetCodeProperties() { return SourceUnit.GetCodeProperties(); } @@ -222,7 +227,7 @@ public ScriptCodeParseResult GetCodeProperties(CompilerOptions options) { return SourceUnit.GetCodeProperties(options); } - [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")] + [SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")] public SourceCodeReader GetReader() { return SourceUnit.GetReader(); } @@ -235,12 +240,12 @@ public SourceCodeReader GetReader() { /// Null if the content is already textual and no transcoding is performed. /// /// - /// Note that the default encoding specified when the script source is created could be overridden by + /// Note that the default encoding specified when the script source is created could be overridden by /// an encoding that is found in the content preamble (Unicode BOM or a language specific encoding preamble). /// In that case the preamble encoding is returned. Otherwise, the default encoding is returned. /// /// An I/O error occurs. - public Encoding DetectEncoding() { + public Encoding? DetectEncoding() { using (var reader = SourceUnit.GetReader()) { return reader.Encoding; } @@ -270,8 +275,8 @@ public string[] GetCodeLines(int start, int count) { /// Which character sequences are considered line separators is language specific. /// If language doesn't specify otherwise "\r", "\n", "\r\n" are recognized line separators. /// - [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")] - public string GetCodeLine(int line) { + [SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")] + public string? GetCodeLine(int line) { return SourceUnit.GetCodeLine(line); } @@ -281,11 +286,11 @@ public string GetCodeLine(int line) { /// Entire content. /// An I/O error occurs. /// - /// The result includes language specific preambles (e.g. "#coding:UTF-8" encoding preamble recognized by Ruby), + /// The result includes language specific preambles (e.g. "#coding:UTF-8" encoding preamble recognized by Ruby), /// but not the preamble defined by the content encoding (e.g. BOM). /// The entire content of the source unit is encoded by single encoding (if it is read from binary stream). /// - [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")] + [SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")] public string GetCode() { return SourceUnit.GetCode(); } @@ -307,16 +312,15 @@ public SourceLocation MapLine(SourceLocation location) { // TODO: remove this? we don't support file mapping // (but it's still in the hosting spec) - [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "line")] - public string MapLinetoFile(int line) { + [SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "line")] + public string? MapLinetoFile(int line) { return SourceUnit.Path; } #endregion #if FEATURE_REMOTING - // TODO: Figure out what is the right lifetime - public override object InitializeLifetimeService() { + public override object? InitializeLifetimeService() { return null; } #endif diff --git a/src/core/Microsoft.Scripting/Hosting/TokenCategorizer.cs b/src/core/Microsoft.Scripting/Hosting/TokenCategorizer.cs index 6294e871..d7ef799c 100644 --- a/src/core/Microsoft.Scripting/Hosting/TokenCategorizer.cs +++ b/src/core/Microsoft.Scripting/Hosting/TokenCategorizer.cs @@ -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 + +using System; + #if FEATURE_REMOTING using System.Runtime.Remoting; #else using MarshalByRefObject = System.Object; #endif -using System; using System.Collections.Generic; using Microsoft.Scripting.Runtime; @@ -89,8 +92,7 @@ public bool SkipTokens(int characterCount) { } #if FEATURE_REMOTING - // TODO: Figure out what is the right lifetime - public override object InitializeLifetimeService() { + public override object? InitializeLifetimeService() { return null; } #endif diff --git a/src/core/Microsoft.Scripting/Runtime/LanguageContext.cs b/src/core/Microsoft.Scripting/Runtime/LanguageContext.cs index c3d352c1..8f472ba0 100644 --- a/src/core/Microsoft.Scripting/Runtime/LanguageContext.cs +++ b/src/core/Microsoft.Scripting/Runtime/LanguageContext.cs @@ -237,7 +237,7 @@ public virtual ICollection GetSearchPaths() { #if FEATURE_CODEDOM // Convert a CodeDom to source code, and output the generated code and the line number mappings (if any) - public virtual SourceUnit GenerateSourceCode(System.CodeDom.CodeObject codeDom, string path, SourceCodeKind kind) { + public virtual SourceUnit GenerateSourceCode(System.CodeDom.CodeObject codeDom, string? path, SourceCodeKind kind) { throw new NotImplementedException(); } #endif