Skip to content

Commit

Permalink
Restructure and internalize (#1738)
Browse files Browse the repository at this point in the history
* Move Reference to Jint.Runtime namespace
* Internalize Binding
* Internalize Advanced operations constructors
* Make Advanced operations inner classes
* Make Options sub-options inner classes
* Move delegate definitions under options
* Make StrictModeScope internal
* Move ModuleBuilder under Runtime.Modules namespace
  • Loading branch information
lahma committed Jan 13, 2024
1 parent d3e65ac commit a178818
Show file tree
Hide file tree
Showing 30 changed files with 304 additions and 323 deletions.
1 change: 0 additions & 1 deletion Jint.Tests/Runtime/NullPropagation.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Jint.Native;
using Jint.Runtime;
using Jint.Runtime.Interop;
using Jint.Runtime.References;

namespace Jint.Tests.Runtime
{
Expand Down
94 changes: 47 additions & 47 deletions Jint/Engine.Advanced.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>
/// Gets current stack trace that is active in engine.
/// </summary>
public string StackTrace
{
get
internal AdvancedOperations(Engine engine)
{
var lastSyntaxElement = _engine._lastSyntaxElement;
if (lastSyntaxElement is null)
_engine = engine;
}

/// <summary>
/// Gets current stack trace that is active in engine.
/// </summary>
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);
}
}
}

/// <summary>
/// Initializes list of references of called functions
/// </summary>
public void ResetCallStack()
{
_engine.ResetCallStack();
}
/// <summary>
/// Initializes list of references of called functions
/// </summary>
public void ResetCallStack()
{
_engine.ResetCallStack();
}

/// <summary>
/// Forcefully processes the current task queues (micro and regular), this API may break and change behavior!
/// </summary>
public void ProcessTasks()
{
_engine.RunAvailableContinuations();
}
/// <summary>
/// Forcefully processes the current task queues (micro and regular), this API may break and change behavior!
/// </summary>
public void ProcessTasks()
{
_engine.RunAvailableContinuations();
}

/// <summary>
/// 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.
/// </summary>
/// <returns>a Promise instance and functions to either resolve or reject it</returns>
public ManualPromise RegisterPromise()
{
return _engine.RegisterPromise();
/// <summary>
/// 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.
/// </summary>
/// <returns>a Promise instance and functions to either resolve or reject it</returns>
public ManualPromise RegisterPromise()
{
return _engine.RegisterPromise();
}
}
}
66 changes: 33 additions & 33 deletions Jint/Engine.Constraints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>
/// Checks engine's active constraints. Propagates exceptions from constraints.
/// </summary>
public void Check()
{
foreach (var constraint in _engine._constraints)
internal ConstraintOperations(Engine engine)
{
constraint.Check();
_engine = engine;
}
}

/// <summary>
/// Return the first constraint that matches the predicate.
/// </summary>
public T? Find<T>() where T : Constraint
{
foreach (var constraint in _engine._constraints)
/// <summary>
/// Checks engine's active constraints. Propagates exceptions from constraints.
/// </summary>
public void Check()
{
if (constraint.GetType() == typeof(T))
foreach (var constraint in _engine._constraints)
{
return (T) constraint;
constraint.Check();
}
}

return null;
}
/// <summary>
/// Return the first constraint that matches the predicate.
/// </summary>
public T? Find<T>() where T : Constraint
{
foreach (var constraint in _engine._constraints)
{
if (constraint.GetType() == typeof(T))
{
return (T) constraint;
}
}

/// <summary>
/// Resets all execution constraints back to their initial state.
/// </summary>
public void Reset()
{
foreach (var constraint in _engine._constraints)
return null;
}

/// <summary>
/// Resets all execution constraints back to their initial state.
/// </summary>
public void Reset()
{
constraint.Reset();
foreach (var constraint in _engine._constraints)
{
constraint.Reset();
}
}
}
}
23 changes: 11 additions & 12 deletions Jint/Engine.Modules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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!;

/// <summary>
/// https://tc39.es/ecma262/#sec-getactivescriptormodule
/// </summary>
internal IScriptOrModule? GetActiveScriptOrModule()
{
return _executionContexts?.GetActiveScriptOrModule();
}
/// <summary>
/// https://tc39.es/ecma262/#sec-getactivescriptormodule
/// </summary>
internal IScriptOrModule? GetActiveScriptOrModule()
{
return _executionContexts?.GetActiveScriptOrModule();
}

public class ModuleOperations
Expand Down Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion Jint/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Jint/Native/Json/JsonParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public sealed class JsonParser
private readonly int _maxDepth;

/// <summary>
/// Creates a new parser using the recursion depth specified in <see cref="JsonOptions.MaxParseDepth"/>.
/// Creates a new parser using the recursion depth specified in <see cref="Options.JsonOptions.MaxParseDepth"/>.
/// </summary>
public JsonParser(Engine engine)
: this(engine, engine.Options.Json.MaxParseDepth)
Expand Down
6 changes: 3 additions & 3 deletions Jint/Options.Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
/// </summary>
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;
Expand All @@ -146,7 +146,7 @@ public static Options SetTypeConverter(this Options options, Func<Engine, ITypeC
/// The delegate to invoke for each CLR member. If the delegate
/// returns <c>null</c>, the standard evaluation is performed.
/// </param>
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;
Expand Down Expand Up @@ -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.
/// </summary>
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;
Expand Down

0 comments on commit a178818

Please sign in to comment.