Skip to content

Commit

Permalink
Remove Engine.Realm, expose Engine.Intrinsics and Engine.Global direc…
Browse files Browse the repository at this point in the history
…tly (#1722)
  • Loading branch information
lahma authored Jan 5, 2024
1 parent 982a751 commit 7e08bab
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 20 deletions.
6 changes: 3 additions & 3 deletions Jint.Tests.PublicInterface/ConstructorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,17 @@ public override ObjectInstance Construct(JsValue[] arguments, JsValue newTarget)
var day = arguments.Length == 2 ? 0 : TypeConverter.ToNumber(arguments[2]) - 1;
if (double.IsNaN(year))
{
throw new JavaScriptException(_engine.Realm.Intrinsics.TypeError, $"Invalid year {year.ToString(CultureInfo.InvariantCulture)}");
throw new JavaScriptException(_engine.Intrinsics.TypeError, $"Invalid year {year.ToString(CultureInfo.InvariantCulture)}");
}

if (double.IsNaN(month))
{
throw new JavaScriptException(_engine.Realm.Intrinsics.TypeError, $"Invalid month {month.ToString(CultureInfo.InvariantCulture)}");
throw new JavaScriptException(_engine.Intrinsics.TypeError, $"Invalid month {month.ToString(CultureInfo.InvariantCulture)}");
}

if (double.IsNaN(day))
{
throw new JavaScriptException(_engine.Realm.Intrinsics.TypeError, $"Invalid day {day.ToString(CultureInfo.InvariantCulture)}");
throw new JavaScriptException(_engine.Intrinsics.TypeError, $"Invalid day {day.ToString(CultureInfo.InvariantCulture)}");
}

var dt = new DateTime((int) year, 1, 1);
Expand Down
2 changes: 1 addition & 1 deletion Jint.Tests.PublicInterface/JavaScriptExceptionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public void CanCreateAndThrowJavaScriptException()

engine.SetValue("throw1", () =>
{
throw new JavaScriptException(engine.Realm.Intrinsics.Error, "message 1");
throw new JavaScriptException(engine.Intrinsics.Error, "message 1");
});

engine.SetValue("throw2", () =>
Expand Down
8 changes: 8 additions & 0 deletions Jint.Tests.PublicInterface/PublicInterfaceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ namespace Jint.Tests.PublicInterface;

public class PublicInterfaceTests
{
[Fact]
public void CanCallEval()
{
var engine = new Engine();
var value = engine.Intrinsics.Eval.Call("1 + 1");
Assert.Equal(2, value);
}

[Fact]
public void BindFunctionInstancesArePublic()
{
Expand Down
14 changes: 7 additions & 7 deletions Jint.Tests.PublicInterface/ShadowRealmTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class ShadowRealmTests
public void CanUseViaEngineMethods()
{
var engine = new Engine(options => options.EnableModules(GetBasePath()));
var shadowRealm = engine.Realm.Intrinsics.ShadowRealm.Construct();
var shadowRealm = engine.Intrinsics.ShadowRealm.Construct();

// lexically scoped (let/const) are visible during single call
Assert.Equal(123, shadowRealm.Evaluate("const s = 123; const f = () => s; f();"));
Expand Down Expand Up @@ -37,11 +37,11 @@ public void MultipleShadowRealmsDoNotInterfere()

Assert.Equal("world", engine.Evaluate("hello();"));

var shadowRealm = engine.Realm.Intrinsics.ShadowRealm.Construct();
var shadowRealm = engine.Intrinsics.ShadowRealm.Construct();
shadowRealm.SetValue("message", "realm 1");
shadowRealm.Evaluate("function hello() {return message}");

var shadowRealm2 = engine.Realm.Intrinsics.ShadowRealm.Construct();
var shadowRealm2 = engine.Intrinsics.ShadowRealm.Construct();
shadowRealm2.SetValue("message", "realm 2");
shadowRealm2.Evaluate("function hello() {return message}");

Expand All @@ -57,11 +57,11 @@ public void MultipleShadowRealm_SettingGlobalVariable_DoNotInterfere()
engine.SetValue("message", "hello ");
engine.Evaluate("(function hello() {message += \"engine\"})();");

var shadowRealm = engine.Realm.Intrinsics.ShadowRealm.Construct();
var shadowRealm = engine.Intrinsics.ShadowRealm.Construct();
shadowRealm.SetValue("message", "hello ");
shadowRealm.Evaluate("(function hello() {message += \"realm 1\"})();");

var shadowRealm2 = engine.Realm.Intrinsics.ShadowRealm.Construct();
var shadowRealm2 = engine.Intrinsics.ShadowRealm.Construct();
shadowRealm2.SetValue("message", "hello ");
shadowRealm2.Evaluate("(function hello() {message += \"realm 2\"})();");

Expand All @@ -77,10 +77,10 @@ public void CanReuseScriptWithShadowRealm()
var engine = new Engine(options => options.EnableModules(GetBasePath()));
engine.SetValue("message", "engine");

var shadowRealm = engine.Realm.Intrinsics.ShadowRealm.Construct();
var shadowRealm = engine.Intrinsics.ShadowRealm.Construct();
shadowRealm.SetValue("message", "realm 1");

var shadowRealm2 = engine.Realm.Intrinsics.ShadowRealm.Construct();
var shadowRealm2 = engine.Intrinsics.ShadowRealm.Construct();
shadowRealm2.SetValue("message", "realm 2");

var parser = new Esprima.JavaScriptParser();
Expand Down
3 changes: 0 additions & 3 deletions Jint/Engine.Advanced.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using Jint.Native;
using Jint.Native.Function;
using Jint.Native.Promise;
using Jint.Runtime;
using Jint.Runtime.Environments;
using Environment = Jint.Runtime.Environments.Environment;

Expand Down
12 changes: 11 additions & 1 deletion Jint/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,17 @@ internal ref readonly ExecutionContext ExecutionContext

internal SyntaxElement? _lastSyntaxElement;

public Realm Realm => _realmInConstruction ?? ExecutionContext.Realm;
internal Realm Realm => _realmInConstruction ?? ExecutionContext.Realm;

/// <summary>
/// The well-known intrinsics for this engine instance.
/// </summary>
public Intrinsics Intrinsics => Realm.Intrinsics;

/// <summary>
/// The global object for this engine instance.
/// </summary>
public ObjectInstance Global => Realm.GlobalObject;

internal GlobalSymbolRegistry GlobalSymbolRegistry { get; } = new();

Expand Down
1 change: 0 additions & 1 deletion Jint/JsValueExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,6 @@ public static JsValue Call(this JsValue value, params JsValue[] arguments)
return ThrowNotObject(value);
}

[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static JsValue Call(this JsValue value, JsValue thisObj, JsValue[] arguments)
{
Expand Down
6 changes: 3 additions & 3 deletions Jint/Native/Function/EvalFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@

namespace Jint.Native.Function;

internal sealed class EvalFunction : Function
public sealed class EvalFunction : Function
{
private static readonly JsString _functionName = new("eval");

private static readonly ParserOptions _parserOptions = ParserOptions.Default with { Tolerant = true };
private readonly JavaScriptParser _parser = new(_parserOptions);

public EvalFunction(
internal EvalFunction(
Engine engine,
Realm realm,
FunctionPrototype functionPrototype)
Expand All @@ -40,7 +40,7 @@ protected internal override JsValue Call(JsValue thisObject, JsValue[] arguments
/// <summary>
/// https://tc39.es/ecma262/#sec-performeval
/// </summary>
public JsValue PerformEval(JsValue x, Realm callerRealm, bool strictCaller, bool direct)
internal JsValue PerformEval(JsValue x, Realm callerRealm, bool strictCaller, bool direct)
{
if (!x.IsString())
{
Expand Down
2 changes: 1 addition & 1 deletion Jint/Runtime/Intrinsics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ internal Intrinsics(Engine engine, Realm realm)
internal GeneratorFunctionConstructor GeneratorFunction =>
_generatorFunction ??= new GeneratorFunctionConstructor(_engine, _realm, Function.PrototypeObject, IteratorPrototype);

internal EvalFunction Eval =>
public EvalFunction Eval =>
_eval ??= new EvalFunction(_engine, _realm, Function.PrototypeObject);

public ErrorConstructor Error =>
Expand Down

0 comments on commit 7e08bab

Please sign in to comment.