-
-
Notifications
You must be signed in to change notification settings - Fork 547
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6a87e76
commit 996377d
Showing
19 changed files
with
292 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using Xunit; | ||
|
||
namespace Jint.Tests.Runtime | ||
{ | ||
public class ModuleTests | ||
{ | ||
private readonly Engine _engine; | ||
|
||
public ModuleTests() | ||
{ | ||
_engine = new Engine(); | ||
} | ||
|
||
[Fact] | ||
public void CanExportNamed() | ||
{ | ||
_engine.DefineModule(@"export const value = 'exported value';", "my-module"); | ||
var ns = _engine.ImportModule("my-module"); | ||
|
||
Assert.Equal("exported value", ns.Get("value").AsString()); | ||
} | ||
|
||
[Fact] | ||
public void CanExportDefault() | ||
{ | ||
_engine.DefineModule(@"export default 'exported value';", "my-module"); | ||
var ns = _engine.ImportModule("my-module"); | ||
|
||
Assert.Equal("exported value", ns.Get("default").AsString()); | ||
} | ||
|
||
[Fact] | ||
public void CanExportAll() | ||
{ | ||
_engine.DefineModule(@"export const value = 'exported value';", "module1"); | ||
_engine.DefineModule(@"export * from 'module 1';", "module2"); | ||
var ns = _engine.ImportModule("module2"); | ||
|
||
Assert.Equal("exported value", ns.Get("value").AsString()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using Jint.Runtime.Modules; | ||
|
||
#nullable enable | ||
|
||
namespace Jint.Runtime; | ||
|
||
public static class ScriptOrModuleExtensions | ||
{ | ||
public static JsModule? AsModule(this IScriptOrModule? scriptOrModule) => scriptOrModule as JsModule; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
namespace Jint.Runtime; | ||
|
||
public interface IScriptOrModule | ||
{ | ||
} |
17 changes: 17 additions & 0 deletions
17
Jint/Runtime/Interpreter/Statements/JintExportAllDeclarationStatement.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#nullable enable | ||
|
||
using Esprima.Ast; | ||
|
||
namespace Jint.Runtime.Interpreter.Statements; | ||
|
||
internal sealed class JintExportAllDeclarationStatement : JintExportDeclarationStatement<ExportAllDeclaration> | ||
{ | ||
public JintExportAllDeclarationStatement(ExportAllDeclaration statement) : base(statement) | ||
{ | ||
} | ||
|
||
protected override void Initialize(EvaluationContext context) | ||
{ | ||
InitializeDeclaration(context.Engine, _statement.Exported); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
Jint/Runtime/Interpreter/Statements/JintExportDeclarationStatement.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using System; | ||
using Esprima.Ast; | ||
using Jint.Native; | ||
using Jint.Runtime.Interpreter.Expressions; | ||
|
||
#nullable enable | ||
|
||
namespace Jint.Runtime.Interpreter.Statements; | ||
|
||
internal abstract class JintExportDeclarationStatement<T> : JintStatement<T> where T : ExportDeclaration | ||
{ | ||
private JintExpression? _declarationExpression; | ||
private JintStatement? _declarationStatement; | ||
|
||
protected JintExportDeclarationStatement(T statement) : base(statement) | ||
{ | ||
} | ||
|
||
protected void InitializeDeclaration(Engine engine, StatementListItem? declaration) | ||
{ | ||
switch (declaration) | ||
{ | ||
case null: | ||
return; | ||
case Expression e: | ||
_declarationExpression = JintExpression.Build(engine, e); | ||
break; | ||
case Statement s: | ||
_declarationStatement = Build(s); | ||
break; | ||
default: | ||
throw new NotSupportedException(); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// https://tc39.es/ecma262/#sec-exports-runtime-semantics-evaluation | ||
/// </summary> | ||
protected override Completion ExecuteInternal(EvaluationContext context) | ||
{ | ||
// TODO: This will be required for default exports | ||
// var module = context.Engine.GetActiveScriptOrModule() as JsModule; | ||
// if (module == null) throw new JavaScriptException("Export can only be used in a module"); | ||
|
||
if (_declarationStatement != null) | ||
{ | ||
// TODO: Not tested | ||
_declarationStatement.Execute(context); | ||
return NormalCompletion(Undefined.Instance); | ||
} | ||
|
||
if (_declarationExpression != null) | ||
{ | ||
// Named exports don't require anything more since the values are available in the lexical context | ||
return _declarationExpression.GetValue(context); | ||
} | ||
|
||
return NormalCompletion(Undefined.Instance); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
Jint/Runtime/Interpreter/Statements/JintExportDefaultDeclarationStatement.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#nullable enable | ||
|
||
using Esprima.Ast; | ||
|
||
namespace Jint.Runtime.Interpreter.Statements; | ||
|
||
internal sealed class JintExportDefaultDeclarationStatement : JintExportDeclarationStatement<ExportDefaultDeclaration> | ||
{ | ||
public JintExportDefaultDeclarationStatement(ExportDefaultDeclaration statement) : base(statement) | ||
{ | ||
} | ||
|
||
protected override void Initialize(EvaluationContext context) | ||
{ | ||
InitializeDeclaration(context.Engine, _statement.Declaration); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
Jint/Runtime/Interpreter/Statements/JintExportNamedDeclarationStatement.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#nullable enable | ||
|
||
using Esprima.Ast; | ||
|
||
namespace Jint.Runtime.Interpreter.Statements; | ||
|
||
internal sealed class JintExportNamedDeclarationStatement : JintExportDeclarationStatement<ExportNamedDeclaration> | ||
{ | ||
public JintExportNamedDeclarationStatement(ExportNamedDeclaration statement) : base(statement) | ||
{ | ||
} | ||
|
||
protected override void Initialize(EvaluationContext context) | ||
{ | ||
InitializeDeclaration(context.Engine, _statement.Declaration); | ||
} | ||
} |
Oops, something went wrong.