Skip to content
/ CSharpJavaScriptRunner Public template

Use C# and V8 to run JavaScript code, Node.js/CommonJS module; from memory, filesystem, serialized zip files, etc. As fallback use other C# JavaScript engines

License

Notifications You must be signed in to change notification settings

77it/CSharpJavaScriptRunner

Repository files navigation

C# JavaScript Runner

This is a C# .NET Core project that shows usage of ClearScript V8 and Jint engines to run JavaScript code, using also other JavaScript libraries (as Node.js @hapi/formula etc).

Code is MIT licensed as ClearScript.

Highlights:

  • starting from some barebone code that mimic the functionality of require/CommonJs and his explanation I adapted it for this project (myrequire.js) to use real and virtual fs, to append the ./lib search path and the .js extension when missing, etc
  • implemented optional use of Base64 encoded zip files to store an entire folder of Js files and libraries in a single C# string. Working tests and assets provided.
  • built a virtual filesystem (using simply a dictionary inside the scripting context) to intercept requests of files stored in memory
  • wrote a simple JavaScript filesystem API layer that calls Node.js and C# methods to be able to read/write a file without worrying if the script is executed in memory or against a real fs, from ClearScript or Node. For security purposes the C# code doesn't allow any writing in the real filesystem above the scriptsPath

When possible I used TypeScript, to allow debug/static typing.

Interesting files

Scripting.Js.v1 project

Interesting files in the core library:

Scripting.JsScripts.v1

Interesting JavaScript files:

Scripting.Tests

Interesting tests files:

About

Use C# and V8 to run JavaScript code, Node.js/CommonJS module; from memory, filesystem, serialized zip files, etc. As fallback use other C# JavaScript engines

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published