Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ScriptOrModule to FunctionInstance #1156

Merged
merged 1 commit into from
May 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 32 additions & 13 deletions Jint/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,31 @@ public Engine Execute(string source, ParserOptions parserOptions)

public Engine Execute(Script script)
{
Engine DoInvoke()
var strict = _isStrict || script.Strict;
ExecuteWithConstraints(strict, () => ScriptEvaluation(new ScriptRecord(Realm, script, string.Empty)));

return this;
}

/// <summary>
/// https://tc39.es/ecma262/#sec-runtime-semantics-scriptevaluation
/// </summary>
private Engine ScriptEvaluation(ScriptRecord scriptRecord)
{
var globalEnv = Realm.GlobalEnv;

var scriptContext = new ExecutionContext(
scriptRecord,
lexicalEnvironment: globalEnv,
variableEnvironment: globalEnv,
privateEnvironment: null,
Realm);

EnterExecutionContext(scriptContext);
try
{
GlobalDeclarationInstantiation(
script,
Realm.GlobalEnv);
var script = scriptRecord.EcmaScriptCode;
GlobalDeclarationInstantiation(script, globalEnv);

var list = new JintStatementList(null, script.Body);

Expand Down Expand Up @@ -295,11 +315,10 @@ Engine DoInvoke()

return this;
}

var strict = _isStrict || script.Strict;
ExecuteWithConstraints(strict, DoInvoke);

return this;
finally
{
LeaveExecutionContext();
}
}

/// <summary>
Expand Down Expand Up @@ -397,7 +416,7 @@ internal JsValue GetValue(Reference reference, bool returnReferenceToPool)

if (baseValue.IsUndefined())
{
if (_referenceResolver.TryUnresolvableReference(this, reference, out JsValue val))
if (_referenceResolver.TryUnresolvableReference(this, reference, out var val))
{
return val;
}
Expand Down Expand Up @@ -782,7 +801,7 @@ internal JsValue ResolveThisBinding()
var fnDefinable = env.CanDeclareGlobalFunction(fn);
if (!fnDefinable)
{
ExceptionHelper.ThrowTypeError(realm);
ExceptionHelper.ThrowTypeError(realm, "Cannot declare global function " + fn);
}

declaredFunctionNames.Add(fn);
Expand Down Expand Up @@ -891,8 +910,8 @@ internal JsValue ResolveThisBinding()
var hasParameterExpressions = configuration.HasParameterExpressions;

var canInitializeParametersOnDeclaration = simpleParameterList && !configuration.HasDuplicates;
env.InitializeParameters(parameterNames, hasDuplicates,
canInitializeParametersOnDeclaration ? argumentsList : null);
var arguments = canInitializeParametersOnDeclaration ? argumentsList : null;
env.InitializeParameters(parameterNames, hasDuplicates, arguments);

ArgumentsInstance ao = null;
if (configuration.ArgumentsObjectNeeded)
Expand Down
4 changes: 3 additions & 1 deletion Jint/Native/Function/FunctionInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public abstract class FunctionInstance : ObjectInstance, ICallable

internal Realm _realm;
private PrivateEnvironmentRecord _privateEnvironment;
private readonly IScriptOrModule _scriptOrModule;

protected FunctionInstance(
Engine engine,
Expand Down Expand Up @@ -65,6 +66,7 @@ public abstract class FunctionInstance : ObjectInstance, ICallable
}
_realm = realm;
_thisMode = thisMode;
_scriptOrModule = _engine.GetActiveScriptOrModule();
}

// for example RavenDB wants to inspect this
Expand Down Expand Up @@ -349,7 +351,7 @@ internal ExecutionContext PrepareForOrdinaryCall(JsValue newTarget)
var calleeRealm = _realm;

var calleeContext = new ExecutionContext(
null,
_scriptOrModule,
localEnv,
localEnv,
_privateEnvironment,
Expand Down
5 changes: 5 additions & 0 deletions Jint/Runtime/ScriptRecord.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using Esprima.Ast;

namespace Jint.Runtime;

internal sealed record ScriptRecord(Realm Realm, Script EcmaScriptCode, string Location) : IScriptOrModule;