├── .gitignore ├── .vscode ├── launch.json └── tasks.json ├── Jint.CommonJS.Tests ├── InternalModule.Tests.cs ├── Jint.CommonJS.Tests.csproj ├── Module.Tests.cs └── ModuleLoader.Tests.cs ├── Jint.CommonJS.sln ├── Jint.CommonJS ├── CommonJSPathResolver.cs ├── Engine.Extensions.cs ├── IModule.cs ├── IModuleResolver.cs ├── InternalModule.cs ├── Jint.CommonJS.csproj ├── Module.cs ├── ModuleLoadingEngine.cs └── ModuleRequestedEventArgs.cs ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.suo 8 | *.user 9 | *.userosscache 10 | *.sln.docstates 11 | 12 | # User-specific files (MonoDevelop/Xamarin Studio) 13 | *.userprefs 14 | 15 | # Build results 16 | [Dd]ebug/ 17 | [Dd]ebugPublic/ 18 | [Rr]elease/ 19 | [Rr]eleases/ 20 | x64/ 21 | x86/ 22 | bld/ 23 | [Bb]in/ 24 | [Oo]bj/ 25 | [Ll]og/ 26 | 27 | # Visual Studio 2015 cache/options directory 28 | .vs/ 29 | # Uncomment if you have tasks that create the project's static files in wwwroot 30 | #wwwroot/ 31 | 32 | # MSTest test Results 33 | [Tt]est[Rr]esult*/ 34 | [Bb]uild[Ll]og.* 35 | 36 | # NUNIT 37 | *.VisualState.xml 38 | TestResult.xml 39 | 40 | # Build Results of an ATL Project 41 | [Dd]ebugPS/ 42 | [Rr]eleasePS/ 43 | dlldata.c 44 | 45 | # .NET Core 46 | project.lock.json 47 | project.fragment.lock.json 48 | artifacts/ 49 | **/Properties/launchSettings.json 50 | 51 | *_i.c 52 | *_p.c 53 | *_i.h 54 | *.ilk 55 | *.meta 56 | *.obj 57 | *.pch 58 | *.pdb 59 | *.pgc 60 | *.pgd 61 | *.rsp 62 | *.sbr 63 | *.tlb 64 | *.tli 65 | *.tlh 66 | *.tmp 67 | *.tmp_proj 68 | *.log 69 | *.vspscc 70 | *.vssscc 71 | .builds 72 | *.pidb 73 | *.svclog 74 | *.scc 75 | 76 | # Chutzpah Test files 77 | _Chutzpah* 78 | 79 | # Visual C++ cache files 80 | ipch/ 81 | *.aps 82 | *.ncb 83 | *.opendb 84 | *.opensdf 85 | *.sdf 86 | *.cachefile 87 | *.VC.db 88 | *.VC.VC.opendb 89 | 90 | # Visual Studio profiler 91 | *.psess 92 | *.vsp 93 | *.vspx 94 | *.sap 95 | 96 | # TFS 2012 Local Workspace 97 | $tf/ 98 | 99 | # Guidance Automation Toolkit 100 | *.gpState 101 | 102 | # ReSharper is a .NET coding add-in 103 | _ReSharper*/ 104 | *.[Rr]e[Ss]harper 105 | *.DotSettings.user 106 | 107 | # JustCode is a .NET coding add-in 108 | .JustCode 109 | 110 | # TeamCity is a build add-in 111 | _TeamCity* 112 | 113 | # DotCover is a Code Coverage Tool 114 | *.dotCover 115 | 116 | # Visual Studio code coverage results 117 | *.coverage 118 | *.coveragexml 119 | 120 | # NCrunch 121 | _NCrunch_* 122 | .*crunch*.local.xml 123 | nCrunchTemp_* 124 | 125 | # MightyMoose 126 | *.mm.* 127 | AutoTest.Net/ 128 | 129 | # Web workbench (sass) 130 | .sass-cache/ 131 | 132 | # Installshield output folder 133 | [Ee]xpress/ 134 | 135 | # DocProject is a documentation generator add-in 136 | DocProject/buildhelp/ 137 | DocProject/Help/*.HxT 138 | DocProject/Help/*.HxC 139 | DocProject/Help/*.hhc 140 | DocProject/Help/*.hhk 141 | DocProject/Help/*.hhp 142 | DocProject/Help/Html2 143 | DocProject/Help/html 144 | 145 | # Click-Once directory 146 | publish/ 147 | 148 | # Publish Web Output 149 | *.[Pp]ublish.xml 150 | *.azurePubxml 151 | # TODO: Comment the next line if you want to checkin your web deploy settings 152 | # but database connection strings (with potential passwords) will be unencrypted 153 | *.pubxml 154 | *.publishproj 155 | 156 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 157 | # checkin your Azure Web App publish settings, but sensitive information contained 158 | # in these scripts will be unencrypted 159 | PublishScripts/ 160 | 161 | # NuGet Packages 162 | *.nupkg 163 | # The packages folder can be ignored because of Package Restore 164 | **/packages/* 165 | # except build/, which is used as an MSBuild target. 166 | !**/packages/build/ 167 | # Uncomment if necessary however generally it will be regenerated when needed 168 | #!**/packages/repositories.config 169 | # NuGet v3's project.json files produces more ignorable files 170 | *.nuget.props 171 | *.nuget.targets 172 | 173 | # Microsoft Azure Build Output 174 | csx/ 175 | *.build.csdef 176 | 177 | # Microsoft Azure Emulator 178 | ecf/ 179 | rcf/ 180 | 181 | # Windows Store app package directories and files 182 | AppPackages/ 183 | BundleArtifacts/ 184 | Package.StoreAssociation.xml 185 | _pkginfo.txt 186 | 187 | # Visual Studio cache files 188 | # files ending in .cache can be ignored 189 | *.[Cc]ache 190 | # but keep track of directories ending in .cache 191 | !*.[Cc]ache/ 192 | 193 | # Others 194 | ClientBin/ 195 | ~$* 196 | *~ 197 | *.dbmdl 198 | *.dbproj.schemaview 199 | *.jfm 200 | *.pfx 201 | *.publishsettings 202 | orleans.codegen.cs 203 | 204 | # Since there are multiple workflows, uncomment next line to ignore bower_components 205 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 206 | #bower_components/ 207 | 208 | # RIA/Silverlight projects 209 | Generated_Code/ 210 | 211 | # Backup & report files from converting an old project file 212 | # to a newer Visual Studio version. Backup files are not needed, 213 | # because we have git ;-) 214 | _UpgradeReport_Files/ 215 | Backup*/ 216 | UpgradeLog*.XML 217 | UpgradeLog*.htm 218 | 219 | # SQL Server files 220 | *.mdf 221 | *.ldf 222 | *.ndf 223 | 224 | # Business Intelligence projects 225 | *.rdl.data 226 | *.bim.layout 227 | *.bim_*.settings 228 | 229 | # Microsoft Fakes 230 | FakesAssemblies/ 231 | 232 | # GhostDoc plugin setting file 233 | *.GhostDoc.xml 234 | 235 | # Node.js Tools for Visual Studio 236 | .ntvs_analysis.dat 237 | node_modules/ 238 | 239 | # Typescript v1 declaration files 240 | typings/ 241 | 242 | # Visual Studio 6 build log 243 | *.plg 244 | 245 | # Visual Studio 6 workspace options file 246 | *.opt 247 | 248 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 249 | *.vbw 250 | 251 | # Visual Studio LightSwitch build output 252 | **/*.HTMLClient/GeneratedArtifacts 253 | **/*.DesktopClient/GeneratedArtifacts 254 | **/*.DesktopClient/ModelManifest.xml 255 | **/*.Server/GeneratedArtifacts 256 | **/*.Server/ModelManifest.xml 257 | _Pvt_Extensions 258 | 259 | # Paket dependency manager 260 | .paket/paket.exe 261 | paket-files/ 262 | 263 | # FAKE - F# Make 264 | .fake/ 265 | 266 | # JetBrains Rider 267 | .idea/ 268 | *.sln.iml 269 | 270 | # CodeRush 271 | .cr/ 272 | 273 | # Python Tools for Visual Studio (PTVS) 274 | __pycache__/ 275 | *.pyc 276 | 277 | # Cake - Uncomment if you are using it 278 | # tools/** 279 | # !tools/packages.config 280 | 281 | # Telerik's JustMock configuration file 282 | *.jmconfig 283 | 284 | # BizTalk build output 285 | *.btp.cs 286 | *.btm.cs 287 | *.odx.cs 288 | *.xsd.cs 289 | -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // Use IntelliSense to find out which attributes exist for C# debugging 3 | // Use hover for the description of the existing attributes 4 | // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md 5 | "version": "0.2.0", 6 | "configurations": [ 7 | { 8 | "name": ".NET Core Launch (console)", 9 | "type": "coreclr", 10 | "request": "launch", 11 | "preLaunchTask": "build", 12 | // If you have changed target frameworks, make sure to update the program path. 13 | "program": "${workspaceFolder}/Jint.CommonJS.Tests/bin/Debug/netcoreapp2.0/Jint.CommonJS.Tests.dll", 14 | "args": [], 15 | "cwd": "${workspaceFolder}/Jint.CommonJS.Tests", 16 | // For more information about the 'console' field, see https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md#console-terminal-window 17 | "console": "internalConsole", 18 | "stopAtEntry": true, 19 | "internalConsoleOptions": "openOnSessionStart", 20 | }, 21 | 22 | { 23 | "name": ".NET Core Attach", 24 | "type": "coreclr", 25 | "request": "attach", 26 | "processId": "${command:pickProcess}" 27 | } 28 | ,] 29 | } -------------------------------------------------------------------------------- /.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "2.0.0", 3 | "tasks": [ 4 | { 5 | "label": "build", 6 | "type": "shell", 7 | "command": "/usr/bin/dotnet", 8 | "args": [ 9 | "build", 10 | "${workspaceFolder}/Jint.CommonJS.Tests/Jint.CommonJS.Tests.csproj" 11 | ], 12 | "options": { 13 | "shell": { 14 | "executable": "/bin/bash", 15 | "args": [ 16 | "-c" 17 | ] 18 | }, 19 | "env": { 20 | "BASH_ENV": "/home/tw/.vscode-workaround" 21 | } 22 | }, 23 | "problemMatcher": "$msCompile" 24 | } 25 | ] 26 | } -------------------------------------------------------------------------------- /Jint.CommonJS.Tests/InternalModule.Tests.cs: -------------------------------------------------------------------------------- 1 | 2 | using Jint; 3 | using Xunit; 4 | using Jint.CommonJS; 5 | using Jint.Runtime.Interop; 6 | using System.IO; 7 | using System; 8 | using Jint.Native; 9 | 10 | public class InternalModuleTests 11 | { 12 | public class TestClass 13 | { 14 | public static string staticMethod() 15 | { 16 | return "test"; 17 | } 18 | 19 | public string instanceMethod() 20 | { 21 | return "test instance"; 22 | } 23 | } 24 | 25 | [Fact(DisplayName = "It supports interop to C# classes with type references")] 26 | public void ItSupportsTypeReferences() 27 | { 28 | Directory.SetCurrentDirectory(Path.GetTempPath()); 29 | 30 | File.WriteAllText("ItSupportsTypeReferences.js", @" 31 | var testClass = require('test'); 32 | var testInstance = new testClass(); 33 | 34 | module.exports = { 35 | statics: testClass.staticMethod(), 36 | instances: testInstance.instanceMethod(), 37 | } 38 | "); 39 | 40 | var engine = new Engine(); 41 | var exports = engine 42 | .CommonJS() 43 | .RegisterInternalModule("test", typeof(TestClass)) 44 | .RunMain("./ItSupportsTypeReferences"); 45 | 46 | Assert.Equal("test", exports.AsObject().Get("statics").AsString()); 47 | Assert.Equal("test instance", exports.AsObject().Get("instances").AsString()); 48 | } 49 | 50 | [Fact(DisplayName = "It supports delegates")] 51 | public void ItSupportsDelegates() 52 | { 53 | Directory.SetCurrentDirectory(Path.GetTempPath()); 54 | 55 | File.WriteAllText("ItSupportsDelegates.js", @" 56 | module.exports = require('func')(); 57 | "); 58 | var engine = new Engine(); 59 | var exports = engine 60 | .CommonJS() 61 | .RegisterInternalModule("func", new Func(() => "test delegate value")) 62 | .RunMain("./ItSupportsDelegates"); 63 | 64 | Assert.Equal("test delegate value", exports.AsString()); 65 | } 66 | 67 | [Fact(DisplayName = "It supports Object Instances")] 68 | public void ItSupportsObjectInstances() 69 | { 70 | Directory.SetCurrentDirectory(Path.GetTempPath()); 71 | 72 | File.WriteAllText("ItSupportsObjectInstances.js", @" 73 | module.exports = require('objectInstance'); 74 | "); 75 | 76 | var testObject = new 77 | { 78 | test = "test value" 79 | }; 80 | 81 | var engine = new Engine(); 82 | var exports = engine 83 | .CommonJS() 84 | .RegisterInternalModule("objectInstance", testObject) 85 | .RunMain("./ItSupportsObjectInstances"); 86 | 87 | Assert.Equal("test value", exports.AsObject().Get("test").AsString()); 88 | } 89 | 90 | [Fact(DisplayName = "It raises the ModuleRequested event when being asked for an internal module not in the cache")] 91 | public void ItRaisesTheModuleRequestedEvent() 92 | { 93 | var engine = new Engine().CommonJS(); 94 | var testModuleName = "TestModule"; 95 | 96 | Directory.SetCurrentDirectory(Path.GetTempPath()); 97 | 98 | File.WriteAllText("ModuleRequestedEvent.js", $@" 99 | module.exports = require('{testModuleName}'); 100 | "); 101 | 102 | var eventData = Assert.RaisesAny(e => ModuleLoadingEngine.ModuleRequested += e, e => ModuleLoadingEngine.ModuleRequested -= e, () => { 103 | // An exception is expected here; the RaisesAny method does not assign the exports a value. 104 | Assert.Throws(() => engine.RunMain("./ModuleRequestedEvent")); 105 | }); 106 | 107 | Assert.Equal(testModuleName, eventData.Arguments.ModuleId); 108 | } 109 | 110 | [Fact] 111 | public void ItLoadsModulesFromTheModuleRequestedEventHandler() 112 | { 113 | Directory.SetCurrentDirectory(Path.GetTempPath()); 114 | 115 | var engine = new Engine().CommonJS(); 116 | var testModuleName = "TestModule"; 117 | 118 | File.WriteAllText("ModuleRequestedEvent2.js", $@" 119 | module.exports = require('{testModuleName}'); 120 | "); 121 | 122 | ModuleLoadingEngine.ModuleRequested += (sender, args) => { 123 | if (args.ModuleId == testModuleName) 124 | { 125 | args.Exports = JsValue.FromObject(engine.engine, "test value"); 126 | } 127 | }; 128 | 129 | var exports = engine.RunMain("./ModuleRequestedEvent2"); 130 | 131 | Assert.Equal("test value", exports.AsString()); 132 | } 133 | 134 | 135 | } -------------------------------------------------------------------------------- /Jint.CommonJS.Tests/Jint.CommonJS.Tests.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp2.0 5 | 6 | false 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /Jint.CommonJS.Tests/Module.Tests.cs: -------------------------------------------------------------------------------- 1 | 2 | using System.IO; 3 | using Jint; 4 | using Jint.CommonJS; 5 | using Xunit; 6 | 7 | public class ModuleTests 8 | { 9 | 10 | [Fact(DisplayName = "Modules should allow requiring of other modules from inside a module")] 11 | public void ItShouldAllowRequiringOfOtherModulesFromInsideAModule() 12 | { 13 | Directory.SetCurrentDirectory(Path.GetTempPath()); 14 | 15 | File.WriteAllText("mod3.js", @" 16 | module.exports = { 17 | test: ""module 3"" 18 | } 19 | "); 20 | File.WriteAllText("index3.js", @" 21 | module.exports = require('./mod3'); 22 | "); 23 | 24 | var exports = new Engine().CommonJS().RunMain("./index3"); 25 | 26 | Assert.Equal("module 3", exports.AsObject().Get("test").AsString()); 27 | } 28 | 29 | [Fact(DisplayName = "It should support overriding module.exports")] 30 | public void ItShouldSupportOverridingModuleExports() 31 | { 32 | Directory.SetCurrentDirectory(Path.GetTempPath()); 33 | File.WriteAllText("index6.js", @" 34 | module.exports = ""banana""; 35 | "); 36 | 37 | var exports = new Engine().CommonJS().RunMain("./index6"); 38 | Assert.Equal("banana", exports.AsString()); 39 | } 40 | 41 | [Fact(DisplayName = "It should support recursively loading modules")] 42 | public void ItShouldSupportRecursivelyLoadingModules() 43 | { 44 | Directory.SetCurrentDirectory(Path.GetTempPath()); 45 | 46 | File.WriteAllText("r_mod2.js", @" 47 | exports.func2 = function() { 48 | return 'func2'; 49 | }; 50 | 51 | exports.mod1 = require('./r_mod1'); 52 | "); 53 | File.WriteAllText("r_mod1.js", @" 54 | module.exports = { 55 | func1: function() { 56 | return 'func1'; 57 | }, 58 | mod2: require('./r_mod2'), 59 | } 60 | "); 61 | 62 | File.WriteAllText("r_main.js", @" 63 | module.exports = require('./r_mod1'); 64 | "); 65 | 66 | var exports = new Engine().CommonJS().RunMain("./r_main"); 67 | 68 | Assert.Equal("func1", exports.AsObject().Get("func1").Invoke(new Jint.Native.JsValue[] { }).AsString()); 69 | Assert.Equal("func2", exports.AsObject().Get("mod2").AsObject().Get("func2").Invoke(new Jint.Native.JsValue[] { }).AsString()); 70 | } 71 | } -------------------------------------------------------------------------------- /Jint.CommonJS.Tests/ModuleLoader.Tests.cs: -------------------------------------------------------------------------------- 1 | 2 | using System; 3 | using System.IO; 4 | using Jint; 5 | using Jint.CommonJS; 6 | using Xunit; 7 | 8 | public class ModuleLoaderTests 9 | { 10 | [Fact(DisplayName = "RunMain should throw an error if a module ID is not provided")] 11 | public void RunMainShouldThrowAnErrorIfAModuleIdIsNotProvided() 12 | { 13 | Assert.Throws(() => new Engine().CommonJS().RunMain(null)); 14 | } 15 | 16 | [Fact(DisplayName = "RunMain should load an index file on disk")] 17 | public void RunMainShouldLoadAnIndexFileOnDisk() 18 | { 19 | Directory.SetCurrentDirectory(Path.GetTempPath()); 20 | File.WriteAllText("index1.js", @" 21 | exports.helloWorld = ""Hello World!""; 22 | "); 23 | 24 | var exports = new Engine().CommonJS().RunMain("./index1"); 25 | Assert.Equal("Hello World!", exports.AsObject().Get("helloWorld").AsString()); 26 | } 27 | 28 | [Fact(DisplayName = "RunMain should load an index file via the './' construct")] 29 | public void RunMainShouldLoadAnIndexFileRelativelyOnDisk() 30 | { 31 | Directory.SetCurrentDirectory(Path.GetTempPath()); 32 | File.WriteAllText("index2.js", @" 33 | exports.helloWorld = ""Hello World 2""; 34 | "); 35 | 36 | var exports = new Engine().CommonJS().RunMain("./index2"); 37 | Assert.Equal("Hello World 2", exports.AsObject().Get("helloWorld").AsString()); 38 | } 39 | 40 | [Fact(DisplayName = "It should require JSON")] 41 | public void ItShouldLoadJSON() 42 | { 43 | Directory.SetCurrentDirectory(Path.GetTempPath()); 44 | 45 | File.WriteAllText("file.json", @" 46 | { 47 | ""testValue"": ""test"" 48 | } 49 | "); 50 | 51 | var exports = new Engine().CommonJS().RunMain("./file.json"); 52 | Assert.Equal("test", exports.AsObject().Get("testValue").AsString()); 53 | } 54 | } -------------------------------------------------------------------------------- /Jint.CommonJS.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.26124.0 5 | MinimumVisualStudioVersion = 15.0.26124.0 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Jint.CommonJS", "Jint.CommonJS\Jint.CommonJS.csproj", "{69DD853D-54D3-4134-8F6E-0ECFEDFCA3F2}" 7 | EndProject 8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Jint.CommonJS.Tests", "Jint.CommonJS.Tests\Jint.CommonJS.Tests.csproj", "{3491690C-CE5E-4F93-97A1-1B4219164EE6}" 9 | EndProject 10 | Global 11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 12 | Debug|Any CPU = Debug|Any CPU 13 | Debug|x64 = Debug|x64 14 | Debug|x86 = Debug|x86 15 | Release|Any CPU = Release|Any CPU 16 | Release|x64 = Release|x64 17 | Release|x86 = Release|x86 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 23 | {69DD853D-54D3-4134-8F6E-0ECFEDFCA3F2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 24 | {69DD853D-54D3-4134-8F6E-0ECFEDFCA3F2}.Debug|Any CPU.Build.0 = Debug|Any CPU 25 | {69DD853D-54D3-4134-8F6E-0ECFEDFCA3F2}.Debug|x64.ActiveCfg = Debug|x64 26 | {69DD853D-54D3-4134-8F6E-0ECFEDFCA3F2}.Debug|x64.Build.0 = Debug|x64 27 | {69DD853D-54D3-4134-8F6E-0ECFEDFCA3F2}.Debug|x86.ActiveCfg = Debug|x86 28 | {69DD853D-54D3-4134-8F6E-0ECFEDFCA3F2}.Debug|x86.Build.0 = Debug|x86 29 | {69DD853D-54D3-4134-8F6E-0ECFEDFCA3F2}.Release|Any CPU.ActiveCfg = Release|Any CPU 30 | {69DD853D-54D3-4134-8F6E-0ECFEDFCA3F2}.Release|Any CPU.Build.0 = Release|Any CPU 31 | {69DD853D-54D3-4134-8F6E-0ECFEDFCA3F2}.Release|x64.ActiveCfg = Release|x64 32 | {69DD853D-54D3-4134-8F6E-0ECFEDFCA3F2}.Release|x64.Build.0 = Release|x64 33 | {69DD853D-54D3-4134-8F6E-0ECFEDFCA3F2}.Release|x86.ActiveCfg = Release|x86 34 | {69DD853D-54D3-4134-8F6E-0ECFEDFCA3F2}.Release|x86.Build.0 = Release|x86 35 | {3491690C-CE5E-4F93-97A1-1B4219164EE6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 36 | {3491690C-CE5E-4F93-97A1-1B4219164EE6}.Debug|Any CPU.Build.0 = Debug|Any CPU 37 | {3491690C-CE5E-4F93-97A1-1B4219164EE6}.Debug|x64.ActiveCfg = Debug|x64 38 | {3491690C-CE5E-4F93-97A1-1B4219164EE6}.Debug|x64.Build.0 = Debug|x64 39 | {3491690C-CE5E-4F93-97A1-1B4219164EE6}.Debug|x86.ActiveCfg = Debug|x86 40 | {3491690C-CE5E-4F93-97A1-1B4219164EE6}.Debug|x86.Build.0 = Debug|x86 41 | {3491690C-CE5E-4F93-97A1-1B4219164EE6}.Release|Any CPU.ActiveCfg = Release|Any CPU 42 | {3491690C-CE5E-4F93-97A1-1B4219164EE6}.Release|Any CPU.Build.0 = Release|Any CPU 43 | {3491690C-CE5E-4F93-97A1-1B4219164EE6}.Release|x64.ActiveCfg = Release|x64 44 | {3491690C-CE5E-4F93-97A1-1B4219164EE6}.Release|x64.Build.0 = Release|x64 45 | {3491690C-CE5E-4F93-97A1-1B4219164EE6}.Release|x86.ActiveCfg = Release|x86 46 | {3491690C-CE5E-4F93-97A1-1B4219164EE6}.Release|x86.Build.0 = Release|x86 47 | EndGlobalSection 48 | EndGlobal 49 | -------------------------------------------------------------------------------- /Jint.CommonJS/CommonJSPathResolver.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using System.IO; 5 | using System.Linq; 6 | 7 | namespace Jint.CommonJS 8 | { 9 | public class CommonJSPathResolver : IModuleResolver 10 | { 11 | private readonly IEnumerable extensionHandlers; 12 | 13 | public CommonJSPathResolver(IEnumerable extensionHandlers) 14 | { 15 | this.extensionHandlers = extensionHandlers; 16 | } 17 | 18 | public string ResolvePath(string moduleId, Module parent) 19 | { 20 | // if (!moduleId.StartsWith(".")) 21 | // { 22 | // throw new Exception($"Module path {moduleId} is not valid. Internal modules are not supported at this time."); 23 | // } 24 | 25 | var cwd = parent.filePath != null ? Path.GetDirectoryName(parent.filePath) : Environment.CurrentDirectory; 26 | var path = Path.Combine(cwd, moduleId); 27 | 28 | /* 29 | * - Try direct file in case an extension is provided 30 | * - if directory, return directory/index 31 | */ 32 | 33 | if (Directory.Exists(path)) 34 | { 35 | path = Path.Combine(path, "index"); 36 | } 37 | 38 | if (!File.Exists(path)) 39 | { 40 | foreach (var tryExtension in extensionHandlers.Where(i => i != "default")) 41 | { 42 | string innerCandidate = path + tryExtension; 43 | if (File.Exists(innerCandidate)) 44 | { 45 | return innerCandidate; 46 | } 47 | } 48 | 49 | throw new FileNotFoundException($"Module {path} could not be resolved."); 50 | } 51 | 52 | return path; 53 | } 54 | } 55 | } -------------------------------------------------------------------------------- /Jint.CommonJS/Engine.Extensions.cs: -------------------------------------------------------------------------------- 1 | 2 | namespace Jint.CommonJS 3 | { 4 | public static class EngineExtensions 5 | { 6 | public static ModuleLoadingEngine CommonJS(this Jint.Engine e) 7 | { 8 | return new ModuleLoadingEngine(e); 9 | } 10 | } 11 | } -------------------------------------------------------------------------------- /Jint.CommonJS/IModule.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using Jint.Native; 4 | 5 | namespace Jint.CommonJS 6 | { 7 | public interface IModule 8 | { 9 | string Id { get; set; } 10 | List Children { get; } 11 | 12 | JsValue Exports { get; set;} 13 | } 14 | } -------------------------------------------------------------------------------- /Jint.CommonJS/IModuleResolver.cs: -------------------------------------------------------------------------------- 1 | 2 | namespace Jint.CommonJS 3 | { 4 | public interface IModuleResolver 5 | { 6 | /// 7 | /// Resolves a module ID to a file on disk. 8 | /// 9 | string ResolvePath(string moduleId, Module fromModule = null); 10 | } 11 | } -------------------------------------------------------------------------------- /Jint.CommonJS/InternalModule.cs: -------------------------------------------------------------------------------- 1 | 2 | using System.Collections.Generic; 3 | using Jint.Native; 4 | using Jint.Native.Function; 5 | 6 | namespace Jint.CommonJS 7 | { 8 | public class InternalModule : IModule 9 | { 10 | public string Id { get; set; } 11 | 12 | public List Children => new List(); 13 | 14 | public JsValue Exports { get; set; } 15 | 16 | public InternalModule(string id, JsValue constructor) 17 | { 18 | Id = id; 19 | Exports = constructor; 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /Jint.CommonJS/Jint.CommonJS.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netstandard2.0 5 | 6 | 7 | 8 | 9 | 10 | A CommonJS implementation for the Jint .NET JavaScript engine 11 | Tyler Watson 12 | https://github.com/tylerjwatson/Jint.CommonJS 13 | https://github.com/tylerjwatson/Jint.CommonJS 14 | https://raw.githubusercontent.com/tylerjwatson/Jint.CommonJS/master/LICENSE 15 | jint,commonjs,module 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /Jint.CommonJS/Module.cs: -------------------------------------------------------------------------------- 1 | 2 | using System; 3 | using System.Collections.Generic; 4 | using System.IO; 5 | using System.Linq; 6 | using Jint.Native; 7 | using Jint.Native.Function; 8 | using Jint.Native.Object; 9 | using Jint.Runtime; 10 | using Jint.Runtime.Interop; 11 | 12 | namespace Jint.CommonJS 13 | { 14 | 15 | public class Module : IModule 16 | { 17 | /// 18 | /// This module's children 19 | /// 20 | public List Children { get; } = new List(); 21 | 22 | protected Module parentModule; 23 | 24 | protected ModuleLoadingEngine engine; 25 | 26 | /// 27 | /// Determines if this module is the main module. 28 | /// 29 | public bool isMainModule => this.parentModule == null; 30 | 31 | public string Id { get; set; } 32 | 33 | /// 34 | /// Contains the module's public API. 35 | /// 36 | public JsValue Exports { get; set; } 37 | 38 | public readonly string filePath; 39 | 40 | /// 41 | /// Creates a new Module instaznce with the specified module id. The module is resolved to a file on disk 42 | /// according to the CommonJS specification. 43 | /// 44 | internal Module(ModuleLoadingEngine e, string moduleId, Module parent = null) 45 | { 46 | if (e == null) 47 | { 48 | throw new System.ArgumentNullException(nameof(e)); 49 | } 50 | 51 | this.engine = e; 52 | 53 | if (string.IsNullOrEmpty(moduleId)) 54 | { 55 | throw new System.ArgumentException("A moduleId is required.", nameof(moduleId)); 56 | } 57 | 58 | Id = moduleId; 59 | this.filePath = e.Resolver.ResolvePath(Id, parent ?? this); 60 | this.parentModule = parent; 61 | 62 | if (parent != null) 63 | { 64 | parent.Children.Add(this); 65 | } 66 | 67 | this.Exports = engine.engine.Object.Construct(new JsValue[] { }); 68 | 69 | string extension = Path.GetExtension(this.filePath); 70 | var loader = this.engine.FileExtensionParsers[extension] ?? this.engine.FileExtensionParsers["default"]; 71 | 72 | e.ModuleCache.Add(Id, this); 73 | 74 | loader(this.filePath, this); 75 | } 76 | 77 | protected JsValue Require(string moduleId) 78 | { 79 | return engine.Load(moduleId, this); 80 | } 81 | 82 | public JsValue Compile(string sourceCode, string filePath) 83 | { 84 | var moduleObject = JsValue.FromObject(this.engine.engine, this); 85 | 86 | // moduleObject.AsObject().DefineOwnProperty("exports", new Runtime.Descriptors.PropertyDescriptor() { 87 | // Get = new ClrFunctionInstance(engine.engine, (thisObj, args) => Exports), 88 | // Set = new ClrFunctionInstance(engine.engine, (thisObj, args) => Exports = args.At(0)), 89 | // Enumerable = true, 90 | // Configurable = true, 91 | // }, throwOnError: true); 92 | 93 | engine.engine.Execute($@" 94 | ;(function (module, exports, __dirname, require) {{ 95 | {sourceCode} 96 | }}) 97 | ").GetCompletionValue().As().Call( 98 | JsValue.FromObject(this.engine.engine, this), 99 | new JsValue[] { 100 | moduleObject, 101 | this.Exports, 102 | Path.GetDirectoryName(filePath), 103 | new ClrFunctionInstance(this.engine.engine, (thisObj, arguments) => Require(arguments.At(0).AsString())) 104 | // new DelegateWrapper(engine.engine, new Func(this.Require)), 105 | } 106 | ); 107 | 108 | return Exports; 109 | } 110 | } 111 | 112 | } -------------------------------------------------------------------------------- /Jint.CommonJS/ModuleLoadingEngine.cs: -------------------------------------------------------------------------------- 1 | 2 | using System; 3 | using System.Collections.Generic; 4 | using System.IO; 5 | using System.Linq; 6 | using Jint.Native; 7 | using Jint.Native.Object; 8 | using Jint.Runtime.Interop; 9 | 10 | namespace Jint.CommonJS 11 | { 12 | 13 | public class ModuleLoadingEngine 14 | { 15 | public static event EventHandler ModuleRequested; 16 | 17 | public delegate JsValue FileExtensionParser(string path, IModule module); 18 | 19 | public Dictionary ModuleCache = new Dictionary(); 20 | public Dictionary FileExtensionParsers = new Dictionary(); 21 | 22 | public readonly Engine engine; 23 | public IModuleResolver Resolver { get; set; } 24 | 25 | public ModuleLoadingEngine(Engine e, IModuleResolver resolver = null) 26 | { 27 | this.engine = e; 28 | this.Resolver = resolver; 29 | 30 | FileExtensionParsers.Add("default", this.LoadJS); 31 | FileExtensionParsers.Add(".js", this.LoadJS); 32 | FileExtensionParsers.Add(".json", this.LoadJson); 33 | 34 | if (resolver == null) 35 | { 36 | this.Resolver = new CommonJSPathResolver(this.FileExtensionParsers.Keys); 37 | } 38 | } 39 | 40 | private JsValue LoadJS(string path, IModule module) 41 | { 42 | var sourceCode = File.ReadAllText(path); 43 | if (module is Module) 44 | { 45 | module.Exports = (module as Module).Compile(sourceCode, path); 46 | } 47 | else 48 | { 49 | module.Exports = engine.Execute(sourceCode).GetCompletionValue(); 50 | } 51 | return module.Exports; 52 | } 53 | 54 | private JsValue LoadJson(string path, IModule module) 55 | { 56 | var sourceCode = File.ReadAllText(path); 57 | module.Exports = engine.Json.Parse(JsValue.Undefined, new[] { JsValue.FromObject(this.engine, sourceCode) }).AsObject(); 58 | return module.Exports; 59 | } 60 | 61 | protected ModuleLoadingEngine RegisterInternalModule(InternalModule mod) 62 | { 63 | ModuleCache.Add(mod.Id, mod); 64 | return this; 65 | } 66 | 67 | /// 68 | /// Registers an internal module to the provided delegate handler. 69 | /// 70 | public ModuleLoadingEngine RegisterInternalModule(string id, Delegate d) 71 | { 72 | this.RegisterInternalModule(id, new DelegateWrapper(engine, d)); 73 | return this; 74 | } 75 | 76 | /// 77 | /// Registers an internal module under the specified id to the provided .NET CLR type. 78 | /// 79 | public ModuleLoadingEngine RegisterInternalModule(string id, Type clrType) 80 | { 81 | this.RegisterInternalModule(id, TypeReference.CreateTypeReference(engine, clrType)); 82 | return this; 83 | } 84 | 85 | /// 86 | /// Registers an internal module under the specified id to any JsValue instance. 87 | /// 88 | public ModuleLoadingEngine RegisterInternalModule(string id, JsValue value) 89 | { 90 | this.RegisterInternalModule(new InternalModule(id, value)); 91 | return this; 92 | } 93 | 94 | /// 95 | /// Registers an internal module to the specified ID to the provided object instance. 96 | /// 97 | public ModuleLoadingEngine RegisterInternalModule(string id, object instance) 98 | { 99 | this.RegisterInternalModule(id, JsValue.FromObject(this.engine, instance)); 100 | return this; 101 | } 102 | 103 | public JsValue RunMain(string mainModuleName) 104 | { 105 | if (string.IsNullOrWhiteSpace(mainModuleName)) 106 | { 107 | throw new System.ArgumentException("A Main module path is required.", nameof(mainModuleName)); 108 | } 109 | 110 | return this.Load(mainModuleName); 111 | } 112 | 113 | public JsValue Load(string moduleName, Module parent = null) 114 | { 115 | IModule mod; 116 | 117 | if (string.IsNullOrEmpty(moduleName)) 118 | { 119 | throw new System.ArgumentException("moduleName is required.", nameof(moduleName)); 120 | } 121 | 122 | if (ModuleCache.ContainsKey(moduleName)) 123 | { 124 | mod = ModuleCache[moduleName]; 125 | parent.Children.Add(mod); 126 | return mod.Exports; 127 | } 128 | 129 | var requestedModule = new ModuleRequestedEventArgs(moduleName); 130 | ModuleRequested?.Invoke(this, requestedModule); 131 | 132 | if (requestedModule.Exports != null && requestedModule.Exports != JsValue.Undefined) 133 | { 134 | ModuleCache.Add(moduleName, (mod = new InternalModule(moduleName, requestedModule.Exports))); 135 | return mod.Exports; 136 | } 137 | 138 | return new Module(this, moduleName, parent).Exports; 139 | } 140 | } 141 | } -------------------------------------------------------------------------------- /Jint.CommonJS/ModuleRequestedEventArgs.cs: -------------------------------------------------------------------------------- 1 | 2 | using System; 3 | using Jint.Native; 4 | 5 | namespace Jint.CommonJS 6 | { 7 | public class ModuleRequestedEventArgs : EventArgs 8 | { 9 | public string ModuleId { get; } 10 | 11 | public JsValue Exports { get; set; } 12 | 13 | public ModuleRequestedEventArgs(string id) 14 | { 15 | this.ModuleId = id; 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Tyler Watson 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Jint.CommonJS 2 | 3 | What's a GitHub project without build badges? [![Build status](https://ci.appveyor.com/api/projects/status/eqxh6wt0gx7fdi04/branch/master?svg=true&retina=true)](https://ci.appveyor.com/project/tylerjwatson/jint-commonjs/branch/master) 4 | 5 | Jint.CommonJS is an extremely simple CommonJS-compatible module loader for the [Jint .NET Javascript Engine](https://github.com/sebastienros/jint). It's written in .NET core 2.0 and should be compatible with all .NET frameworks that Jint targets. It allows you to require JavaScript modules from other modules in the Jint interpreter using the familiar `require` function we all know and love. 6 | 7 | Every loaded module is wrapped in the following closure: 8 | 9 | ``` 10 | (function(module, exports, __dirname, require)) 11 | ``` 12 | 13 | ...and thus module bodies are not globally scoped. 14 | 15 | * `module` points to the CLR module instance 16 | * `exports` points to the module's public API 17 | * `__dirname` is the directory that this module resides in 18 | * `require` is a function which loads other modules relative to this module's directory 19 | 20 | The library is MIT licensed. 21 | 22 | ## A note about Node.JS compatibilty 23 | 24 | **You are not able to load npm packages with Jint.CommonJS.** Although the library loads modules in relatively the same format at NodeJS's [Module specification](https://nodejs.org/api/modules.html), there are some important distinctions. The library does not support node_modules, or reading package.json files for modules. 25 | 26 | ## Features 27 | 28 | * `require` another JavaScript module from a JavaScript file with `require('./module')` 29 | * `require` JSON with `require('./file.json')` 30 | * `require` modules from other modules 31 | * Register internal modules with the `RegisterInternalModule` method 32 | * A small but succinct unit test suite. 33 | 34 | ## Using the library 35 | 1. Import the project reference via NuGet, or by cloning and building the project directly 36 | 1. Import the `Jint.CommonJS` namespace in your code 37 | 1. Use the `CommonJS()` extension method on `Jint.Engine` to enable CommonJS functionality 38 | 39 | ## Example 40 | 41 | The following example runs a main module from the C# program's current directory. 42 | 43 | ```csharp 44 | using Jint; 45 | using Jint.CommonJS; 46 | 47 | public static class Program 48 | { 49 | public static Engine engine = new Engine(); 50 | 51 | public static void Main(string[] args) 52 | { 53 | // Creates a new Jint instance and runs the myModule.js file in the program's 54 | // current working directory. 55 | Jint.Native.JsValue exports = engine.CommonJS().RunMain("./myModule"); 56 | } 57 | } 58 | ``` 59 | 60 | myModule.js 61 | ```js 62 | exports.value = require('./myOtherModule'); 63 | ``` 64 | 65 | ## Internal Modules 66 | 67 | Jint.CommonJS provides an API to register internal modules to any value which Jint supports. It supports CLR type references, namespace references, and any `JsValue` instance, so you may require a CLR type into your module using the require mechanism. 68 | 69 | Both static and instance members are supported when binding CLR types. 70 | 71 | ### Registering an mscorlib type as a module 72 | 73 | ```csharp 74 | var e = new Engine(); 75 | var cjs = e 76 | .CommonJS() 77 | .RegisterInternalModule("console", typeof(Console)) 78 | .RunMain("./") 79 | ``` 80 | 81 | index.js 82 | ```js 83 | var console = require('console'); 84 | console.WriteLine('Test from System.Console!'); 85 | ``` 86 | 87 | ### Registering an internal module to a CLR class 88 | 89 | ```csharp 90 | public class MyClass 91 | { 92 | public void Method() 93 | { 94 | 95 | } 96 | } 97 | 98 | var cjs = e 99 | .CommonJS() 100 | .RegisterInternalModule("cls", typeof(MyClass)) 101 | .RunMain("./") 102 | ``` 103 | 104 | index.js 105 | ```js 106 | var cls = require('cls'); 107 | var instance = new cls(); 108 | cls.Method(); 109 | ``` --------------------------------------------------------------------------------