loading...

re: V8Ception | How to implement V8 into your .NET applications. VIEW POST

FULL DISCUSSION
 

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

    private static void SetupIncludeFunction()
    {
        v8.AddHostObject("CSV8", v8);
        var includeCode = @"
        function include(fn) {
            if (CSFile.Exists(fn)) {
                CSV8.Evaluate(CSFile.ReadAllText(fn));
            } else {
                throw 'File not found.';
            }
        }";

        v8.Evaluate(includeCode);
    }

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

include 'file.js' // CSV8 is now available
CSV8.DestroySystem()

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, ...

include({ crash: 'boom!' }) // this was not expected!

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/...

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.

Code of Conduct Report abuse