-1

Just curious, Just started using V8ScriptEngine, I have this code fragment

private void button1_Click(object sender, EventArgs e)
        {
            string f = "function myFunc(x) {if(x >= 3500.00) return '001'; else return '002'; }";
            string r = "";
            using (var eng = new V8ScriptEngine()) {
                eng.Evaluate(f);
                r = (string)eng.Script.myFunc(3500.0000001);
            }
            Console.WriteLine("r={0}", r);
        }

and that works fine..

How does Script engine infer that the scripting language is Javascript ?, I would imagine that it has to evaluate entire script-block to assert if it is java or vb or any other supported language, wouldn't it be an expensive task ?, or is it possible to tell the Scriptengine that the language target is of a specific flavour ?

TonyP
  • 5,655
  • 13
  • 60
  • 94
  • 4
    Doesn't V8 only handle Javascript? You specifically created a new instance of V8ScriptEngine in this code. Try modifying your string to something clearly not Javascript and see if you get an error. – Khauri Jul 19 '17 at 00:54
  • I would like to accept this comment as an Answer, please post as an answer. Thanks – TonyP Jul 22 '17 at 12:43

1 Answers1

0

The V8 engine is only executing Javascript code. Therefore it assumes any code passed to it is Javascript.

If by other scripting languages you mean things like TypeScript or CoffeeScript those are not directly executed by the V8 engine. They need to be first compiled to Javascript (a process often referred as transpilation). This process will generate Javascript code that is equivalent to the TypeScript/CoffeeScript source code.

Maxime
  • 2,192
  • 1
  • 18
  • 23