diff --git a/Jint.Tests/Runtime/NullPropagation.cs b/Jint.Tests/Runtime/NullPropagation.cs
index cd78410393..e84e3b36c6 100644
--- a/Jint.Tests/Runtime/NullPropagation.cs
+++ b/Jint.Tests/Runtime/NullPropagation.cs
@@ -1,7 +1,6 @@
using Jint.Native;
using Jint.Runtime;
using Jint.Runtime.Interop;
-using Jint.Runtime.References;
namespace Jint.Tests.Runtime
{
diff --git a/Jint/Engine.Advanced.cs b/Jint/Engine.Advanced.cs
index 873e085712..61b8672a0d 100644
--- a/Jint/Engine.Advanced.cs
+++ b/Jint/Engine.Advanced.cs
@@ -5,62 +5,62 @@ namespace Jint;
public partial class Engine
{
public AdvancedOperations Advanced { get; }
-}
-
-public class AdvancedOperations
-{
- private readonly Engine _engine;
- public AdvancedOperations(Engine engine)
+ public class AdvancedOperations
{
- _engine = engine;
- }
+ private readonly Engine _engine;
- ///
- /// Gets current stack trace that is active in engine.
- ///
- public string StackTrace
- {
- get
+ internal AdvancedOperations(Engine engine)
{
- var lastSyntaxElement = _engine._lastSyntaxElement;
- if (lastSyntaxElement is null)
+ _engine = engine;
+ }
+
+ ///
+ /// Gets current stack trace that is active in engine.
+ ///
+ public string StackTrace
+ {
+ get
{
- return string.Empty;
- }
+ var lastSyntaxElement = _engine._lastSyntaxElement;
+ if (lastSyntaxElement is null)
+ {
+ return string.Empty;
+ }
- return _engine.CallStack.BuildCallStackString(lastSyntaxElement.Location);
+ return _engine.CallStack.BuildCallStackString(lastSyntaxElement.Location);
+ }
}
- }
- ///
- /// Initializes list of references of called functions
- ///
- public void ResetCallStack()
- {
- _engine.ResetCallStack();
- }
+ ///
+ /// Initializes list of references of called functions
+ ///
+ public void ResetCallStack()
+ {
+ _engine.ResetCallStack();
+ }
- ///
- /// Forcefully processes the current task queues (micro and regular), this API may break and change behavior!
- ///
- public void ProcessTasks()
- {
- _engine.RunAvailableContinuations();
- }
+ ///
+ /// Forcefully processes the current task queues (micro and regular), this API may break and change behavior!
+ ///
+ public void ProcessTasks()
+ {
+ _engine.RunAvailableContinuations();
+ }
- ///
- /// EXPERIMENTAL! Subject to change.
- ///
- /// Registers a promise within the currently running EventLoop (has to be called within "ExecuteWithEventLoop" call).
- /// Note that ExecuteWithEventLoop will not trigger "onFinished" callback until ALL manual promises are settled.
- ///
- /// NOTE: that resolve and reject need to be called withing the same thread as "ExecuteWithEventLoop".
- /// The API assumes that the Engine is called from a single thread.
- ///
- /// a Promise instance and functions to either resolve or reject it
- public ManualPromise RegisterPromise()
- {
- return _engine.RegisterPromise();
+ ///
+ /// EXPERIMENTAL! Subject to change.
+ ///
+ /// Registers a promise within the currently running EventLoop (has to be called within "ExecuteWithEventLoop" call).
+ /// Note that ExecuteWithEventLoop will not trigger "onFinished" callback until ALL manual promises are settled.
+ ///
+ /// NOTE: that resolve and reject need to be called withing the same thread as "ExecuteWithEventLoop".
+ /// The API assumes that the Engine is called from a single thread.
+ ///
+ /// a Promise instance and functions to either resolve or reject it
+ public ManualPromise RegisterPromise()
+ {
+ return _engine.RegisterPromise();
+ }
}
}
diff --git a/Jint/Engine.Constraints.cs b/Jint/Engine.Constraints.cs
index c8005ec3cb..47df6eb2c4 100644
--- a/Jint/Engine.Constraints.cs
+++ b/Jint/Engine.Constraints.cs
@@ -3,52 +3,52 @@ namespace Jint;
public partial class Engine
{
public ConstraintOperations Constraints { get; }
-}
-
-public class ConstraintOperations
-{
- private readonly Engine _engine;
- public ConstraintOperations(Engine engine)
+ public class ConstraintOperations
{
- _engine = engine;
- }
+ private readonly Engine _engine;
- ///
- /// Checks engine's active constraints. Propagates exceptions from constraints.
- ///
- public void Check()
- {
- foreach (var constraint in _engine._constraints)
+ internal ConstraintOperations(Engine engine)
{
- constraint.Check();
+ _engine = engine;
}
- }
- ///
- /// Return the first constraint that matches the predicate.
- ///
- public T? Find() where T : Constraint
- {
- foreach (var constraint in _engine._constraints)
+ ///
+ /// Checks engine's active constraints. Propagates exceptions from constraints.
+ ///
+ public void Check()
{
- if (constraint.GetType() == typeof(T))
+ foreach (var constraint in _engine._constraints)
{
- return (T) constraint;
+ constraint.Check();
}
}
- return null;
- }
+ ///
+ /// Return the first constraint that matches the predicate.
+ ///
+ public T? Find() where T : Constraint
+ {
+ foreach (var constraint in _engine._constraints)
+ {
+ if (constraint.GetType() == typeof(T))
+ {
+ return (T) constraint;
+ }
+ }
- ///
- /// Resets all execution constraints back to their initial state.
- ///
- public void Reset()
- {
- foreach (var constraint in _engine._constraints)
+ return null;
+ }
+
+ ///
+ /// Resets all execution constraints back to their initial state.
+ ///
+ public void Reset()
{
- constraint.Reset();
+ foreach (var constraint in _engine._constraints)
+ {
+ constraint.Reset();
+ }
}
}
}
diff --git a/Jint/Engine.Modules.cs b/Jint/Engine.Modules.cs
index 54f155aace..6b2e12ab9d 100644
--- a/Jint/Engine.Modules.cs
+++ b/Jint/Engine.Modules.cs
@@ -6,19 +6,18 @@
using Jint.Runtime.Interpreter;
using Jint.Runtime.Modules;
-namespace Jint
+namespace Jint;
+
+public partial class Engine
{
- public partial class Engine
- {
- public ModuleOperations Modules { get; internal set; } = null!;
+ public ModuleOperations Modules { get; internal set; } = null!;
- ///
- /// https://tc39.es/ecma262/#sec-getactivescriptormodule
- ///
- internal IScriptOrModule? GetActiveScriptOrModule()
- {
- return _executionContexts?.GetActiveScriptOrModule();
- }
+ ///
+ /// https://tc39.es/ecma262/#sec-getactivescriptormodule
+ ///
+ internal IScriptOrModule? GetActiveScriptOrModule()
+ {
+ return _executionContexts?.GetActiveScriptOrModule();
}
public class ModuleOperations
@@ -56,7 +55,7 @@ internal Module Load(string? referencingModuleLocation, ModuleRequest request)
if (module is SourceTextModule sourceTextModule)
{
- _engine.Debugger.OnBeforeEvaluate(sourceTextModule._source);
+ _engine.Debugger.OnBeforeEvaluate(sourceTextModule._source);
}
return module;
diff --git a/Jint/Engine.cs b/Jint/Engine.cs
index 1484d192f3..f378baf9c2 100644
--- a/Jint/Engine.cs
+++ b/Jint/Engine.cs
@@ -19,7 +19,6 @@
using Jint.Runtime.Interop.Reflection;
using Jint.Runtime.Interpreter;
using Jint.Runtime.Interpreter.Expressions;
-using Jint.Runtime.References;
using Environment = Jint.Runtime.Environments.Environment;
namespace Jint
diff --git a/Jint/Native/Json/JsonParser.cs b/Jint/Native/Json/JsonParser.cs
index 82925ba028..db38adb8b5 100644
--- a/Jint/Native/Json/JsonParser.cs
+++ b/Jint/Native/Json/JsonParser.cs
@@ -15,7 +15,7 @@ public sealed class JsonParser
private readonly int _maxDepth;
///
- /// Creates a new parser using the recursion depth specified in .
+ /// Creates a new parser using the recursion depth specified in .
///
public JsonParser(Engine engine)
: this(engine, engine.Options.Json.MaxParseDepth)
diff --git a/Jint/Options.Extensions.cs b/Jint/Options.Extensions.cs
index 38c50753ac..86822dcae0 100644
--- a/Jint/Options.Extensions.cs
+++ b/Jint/Options.Extensions.cs
@@ -121,7 +121,7 @@ public static Options AddExtensionMethods(this Options options, params Type[] ty
/// ObjectInstance using class ObjectWrapper. This function can be used to
/// register a handler for a customized handling.
///
- public static Options SetWrapObjectHandler(this Options options, WrapObjectDelegate wrapObjectHandler)
+ public static Options SetWrapObjectHandler(this Options options, Options.WrapObjectDelegate wrapObjectHandler)
{
options.Interop.WrapObjectHandler = wrapObjectHandler;
return options;
@@ -146,7 +146,7 @@ public static Options SetTypeConverter(this Options options, Funcnull, the standard evaluation is performed.
///
- public static Options SetMemberAccessor(this Options options, MemberAccessorDelegate accessor)
+ public static Options SetMemberAccessor(this Options options, Options.MemberAccessorDelegate accessor)
{
options.Interop.MemberAccessor = accessor;
return options;
@@ -191,7 +191,7 @@ public static Options CatchClrExceptions(this Options options)
/// can be used in at try/catch statement. By default these exceptions are bubbled
/// to the CLR host and interrupt the script execution.
///
- public static Options CatchClrExceptions(this Options options, ExceptionHandlerDelegate handler)
+ public static Options CatchClrExceptions(this Options options, Options.ExceptionHandlerDelegate handler)
{
options.Interop.ExceptionHandler = handler;
return options;
diff --git a/Jint/Options.cs b/Jint/Options.cs
index f340c1acf6..a38ed7b8f0 100644
--- a/Jint/Options.cs
+++ b/Jint/Options.cs
@@ -13,12 +13,6 @@
namespace Jint
{
- public delegate JsValue? MemberAccessorDelegate(Engine engine, object target, string member);
-
- public delegate ObjectInstance? WrapObjectDelegate(Engine engine, object target, Type? type);
-
- public delegate bool ExceptionHandlerDelegate(Exception exception);
-
public class Options
{
private static readonly CultureInfo _defaultCulture = CultureInfo.CurrentCulture;
@@ -27,6 +21,12 @@ public class Options
private ITimeSystem? _timeSystem;
internal List> _configurations { get; } = new();
+ public delegate JsValue? MemberAccessorDelegate(Engine engine, object target, string member);
+
+ public delegate ObjectInstance? WrapObjectDelegate(Engine engine, object target, Type? type);
+
+ public delegate bool ExceptionHandlerDelegate(Exception exception);
+
///
/// Execution constraints for the engine.
///
@@ -146,7 +146,7 @@ internal void Apply(Engine engine)
PropertyFlag.AllForbidden));
}
- engine.Modules = new ModuleOperations(engine, Modules.ModuleLoader);
+ engine.Modules = new Engine.ModuleOperations(engine, Modules.ModuleLoader);
}
private static void AttachExtensionMethodsToPrototypes(Engine engine)
@@ -217,136 +217,206 @@ PropertyDescriptor CreateMethodInstancePropertyDescriptor(ClrFunction? function)
}
}
}
- }
-
- public class DebuggerOptions
- {
- ///
- /// Whether debugger functionality is enabled, defaults to false.
- ///
- public bool Enabled { get; set; }
-
- ///
- /// Configures the statement handling strategy, defaults to Ignore.
- ///
- public DebuggerStatementHandling StatementHandling { get; set; } = DebuggerStatementHandling.Ignore;
-
- ///
- /// Configures the step mode used when entering the script.
- ///
- public StepMode InitialStepMode { get; set; } = StepMode.None;
- }
-
- public class InteropOptions
- {
- ///
- /// Whether accessing CLR and it's types and methods is allowed from JS code, defaults to false.
- ///
- public bool Enabled { get; set; }
-
- ///
- /// Whether to expose which can allow bypassing allow lists and open a way to reflection.
- /// Defaults to false.
- ///
- public bool AllowGetType { get; set; }
-
- ///
- /// Whether Jint should allow wrapping objects from System.Reflection namespace.
- /// Defaults to false.
- ///
- public bool AllowSystemReflection { get; set; }
-
- ///
- /// Whether writing to CLR objects is allowed (set properties), defaults to true.
- ///
- public bool AllowWrite { get; set; } = true;
-
- ///
- /// Whether operator overloading resolution is allowed, defaults to false.
- ///
- public bool AllowOperatorOverloading { get; set; }
- ///
- /// Types holding extension methods that should be considered when resolving methods.
- ///
- public List ExtensionMethodTypes { get; } = new();
- ///
- /// Object converters to try when build-in conversions.
- ///
- public List ObjectConverters { get; } = new();
-
- ///
- /// Whether identity map is persisted for object wrappers in order to maintain object identity. This can cause
- /// memory usage to grow when targeting large set and freeing of memory can be delayed due to ConditionalWeakTable semantics.
- /// Defaults to false.
- ///
- public bool TrackObjectWrapperIdentity { get; set; }
-
- ///
- /// If no known type could be guessed, objects are by default wrapped as an
- /// ObjectInstance using class ObjectWrapper. This function can be used to
- /// change the behavior.
- ///
- public WrapObjectDelegate WrapObjectHandler { get; set; } = static (engine, target, type) => new ObjectWrapper(engine, target, type);
-
- ///
- ///
- ///
- public MemberAccessorDelegate MemberAccessor { get; set; } = static (engine, target, member) => null;
-
- ///
- /// Exceptions that thrown from CLR code are converted to JavaScript errors and
- /// can be used in at try/catch statement. By default these exceptions are bubbled
- /// to the CLR host and interrupt the script execution. If handler returns true these exceptions are converted
- /// to JS errors that can be caught by the script.
- ///
- public ExceptionHandlerDelegate ExceptionHandler { get; set; } = _defaultExceptionHandler;
-
- ///
- /// Assemblies to allow scripts to call CLR types directly like System.IO.File.
- ///
- public List AllowedAssemblies { get; set; } = new();
-
- ///
- /// Type and member resolving strategy, which allows filtering allowed members and configuring member
- /// name matching comparison.
- ///
- ///
- /// As this object holds caching state same instance should be shared between engines, if possible.
- ///
- public TypeResolver TypeResolver { get; set; } = TypeResolver.Default;
+ public class DebuggerOptions
+ {
+ ///
+ /// Whether debugger functionality is enabled, defaults to false.
+ ///
+ public bool Enabled { get; set; }
+
+ ///
+ /// Configures the statement handling strategy, defaults to Ignore.
+ ///
+ public DebuggerStatementHandling StatementHandling { get; set; } = DebuggerStatementHandling.Ignore;
+
+ ///
+ /// Configures the step mode used when entering the script.
+ ///
+ public StepMode InitialStepMode { get; set; } = StepMode.None;
+ }
- ///
- /// When writing values to CLR objects, how should JS values be coerced to CLR types.
- /// Defaults to only coercing to string values when writing to string targets.
- ///
- public ValueCoercionType ValueCoercion { get; set; } = ValueCoercionType.String;
+ public class InteropOptions
+ {
+ ///
+ /// Whether accessing CLR and it's types and methods is allowed from JS code, defaults to false.
+ ///
+ public bool Enabled { get; set; }
+
+ ///
+ /// Whether to expose which can allow bypassing allow lists and open a way to reflection.
+ /// Defaults to false.
+ ///
+ public bool AllowGetType { get; set; }
+
+ ///
+ /// Whether Jint should allow wrapping objects from System.Reflection namespace.
+ /// Defaults to false.
+ ///
+ public bool AllowSystemReflection { get; set; }
+
+ ///
+ /// Whether writing to CLR objects is allowed (set properties), defaults to true.
+ ///
+ public bool AllowWrite { get; set; } = true;
+
+ ///
+ /// Whether operator overloading resolution is allowed, defaults to false.
+ ///
+ public bool AllowOperatorOverloading { get; set; }
+
+ ///
+ /// Types holding extension methods that should be considered when resolving methods.
+ ///
+ public List ExtensionMethodTypes { get; } = new();
+
+ ///
+ /// Object converters to try when build-in conversions.
+ ///
+ public List ObjectConverters { get; } = new();
+
+ ///
+ /// Whether identity map is persisted for object wrappers in order to maintain object identity. This can cause
+ /// memory usage to grow when targeting large set and freeing of memory can be delayed due to ConditionalWeakTable semantics.
+ /// Defaults to false.
+ ///
+ public bool TrackObjectWrapperIdentity { get; set; }
+
+ ///
+ /// If no known type could be guessed, objects are by default wrapped as an
+ /// ObjectInstance using class ObjectWrapper. This function can be used to
+ /// change the behavior.
+ ///
+ public WrapObjectDelegate WrapObjectHandler { get; set; } = static (engine, target, type) => new ObjectWrapper(engine, target, type);
+
+ ///
+ ///
+ ///
+ public MemberAccessorDelegate MemberAccessor { get; set; } = static (engine, target, member) => null;
+
+ ///
+ /// Exceptions that thrown from CLR code are converted to JavaScript errors and
+ /// can be used in at try/catch statement. By default these exceptions are bubbled
+ /// to the CLR host and interrupt the script execution. If handler returns true these exceptions are converted
+ /// to JS errors that can be caught by the script.
+ ///
+ public ExceptionHandlerDelegate ExceptionHandler { get; set; } = _defaultExceptionHandler;
+
+ ///
+ /// Assemblies to allow scripts to call CLR types directly like System.IO.File.
+ ///
+ public List AllowedAssemblies { get; set; } = new();
+
+ ///
+ /// Type and member resolving strategy, which allows filtering allowed members and configuring member
+ /// name matching comparison.
+ ///
+ ///
+ /// As this object holds caching state same instance should be shared between engines, if possible.
+ ///
+ public TypeResolver TypeResolver { get; set; } = TypeResolver.Default;
+
+ ///
+ /// When writing values to CLR objects, how should JS values be coerced to CLR types.
+ /// Defaults to only coercing to string values when writing to string targets.
+ ///
+ public ValueCoercionType ValueCoercion { get; set; } = ValueCoercionType.String;
+
+ ///
+ /// Strategy to create a CLR object to hold converted .
+ ///
+ public Func>? CreateClrObject = _ => new ExpandoObject();
+
+ ///
+ /// Strategy to create a CLR object from TypeReference.
+ /// Defaults to retuning null which makes TypeReference attempt to find suitable constructor.
+ ///
+ public Func CreateTypeReferenceObject = (_, _, _) => null;
+
+ internal static readonly ExceptionHandlerDelegate _defaultExceptionHandler = static exception => false;
+
+ ///
+ /// When not null, is used to serialize any CLR object in an
+ /// passing through 'JSON.stringify'.
+ ///
+ public Func