This article is based on my old V8 article published on 23 august 2015.
V8 is the JavaScript execution engine built for Google Chrome and open-sou...
For further actions, you may consider blocking this person and/or reporting abuse
Above you say, "You can create a reference to the V8 instance itself. This is a terrible idea no matter what the circumstances.".
In my project (which uses ClearScript to wrap V8) I have
which by definition is "not a good thing."
Seeing as I want to have an include function (and having it in such as form as to be able to be put inside at try/catch), how should I have done it?
It is a long time ago I did all this but let me see. Ignoring most dogmas, What you're doing here looks about right.
A terrible idea is to expose the internal instance of the interpreter to the higher context using addHostObject. It will stay there in that (more) global context after the first call to include. Simply wrap that reference too somewhere or safely delete it afterwards, otherwise
I don't know from memory how V8 responds to setting a host object twice but maybe your code will crash if you make a second call to include. (maaaaybe)
Make sure that you can rely on CSFile.Exists because if it is a direct reference to System.IO.File.Exists which expects a String, ...
You can wrap it somewhere else but I advise to be lazy like me and put the call to CSFile.Exists it inside a try block of include(). Again, only if needed. I don't know the rest of your program.
Maybe have a look at NiL. I never really liked the extra .dll files of v8, also didn't feel like embedding them or building ClearScript every time I test my code. NiL.js is an ECMAScript evaluator written in C# that understands modern ES6/7/8/9/10 and takes only a second to compile. Not that compiling is needed. It also provides APIs to extend the evaluator with your custom syntax which allowed me to implement an include method function keyword natively into the interpreter. Not for beginners though. In the documentation are some examples, they show you how to add an echo keyword or something. It is platform agnostic in the sense that it runs on Mono.
They've updated their examples a bit since you were last there. There are custom statements and custom operators at github.com/nilproject/NiL.JS/tree/...
What are you working on?
When I get the readme done and a folder with a couple of samples in, I'll send the link. In the meantime, I've got a an enhanced JavaScript called Lychen ("Lichen" is already taken). For some years my work has been using an enhanced JScript using the same technique. I'm now pursuing V8 as a personal project.
Project now on GitHub in a public repo at github.com/axtens/Lychen. Other repos have been deleted.
Hey Bruce, it's been some time since you commented on this article. I went to check out Lychen (again) on GitHub today and noticed your last commit was 29 days ago. May I ask, what do you use Lychen for? Why can't you do it with regular JavaScript? What is the use case? Anyone else using it as far as you know? Cyanobacteria sure but what is the objective of this project? :)
As far as I know, no one is using it. I use it as a test-bed for ideas which I then propagate to our in-house tool, which also uses ClearScript but targets JScript rather than V8. I've recently started fiddling with LychenBASIC, which targets the VBScript engine via ClearScript.
In some respects I work in a backwater. We have a commitment to the Windows platform (which I chafe against from time to time): Azure, SQL Server, C# with JavaScript as the extension language, Visual Studio and Visual Studio Code (when not using Notepad++).
As for "why can't you do it with regular JavaScript?" well-l-l-l, okay, maybe I'll start learning nodejs. Maybe there's some way of using it in our current ecosystem.
FYI: ClearScript doesn't yet support .Net Core, but V8.Net now does: nuget.org/packages/V8.Net/
FYI: Maybe not out of the box but it is really not that hard getting it to work (ish).
As per Clear Script authors, github.com/Microsoft/ClearScript/i... , ClearScript is written with Mixed Assemblies which is not supported on any other platform except Windows. So it is not that easy.
Thank you for clarifying but this article is a rewrite of an even older article targeted at Windows from the beginning. When this was written a couple years ago there was no support for .NET on Linux besides Mono. All the examples assume Windows and even show screenshots of Visual Studio running on Windows so someone who wants to implement V8 into a Linux application really should read a different article and I see no point of dragging this out.