[solved] Adding global functions in JavascriptEngine

I’m using JavascriptEngine, and I want to be able to add functions to the global namespace, so that I can do this:
engine.execute("myJSfunction(1, 2, 3)")

rather than this:
engine.execute("RegisteredObject.myJSfunction(1, 2, 3)")

I’ve considered the following approaches, none of which seem very promising:

  1. Adding functions to the root object in JavascriptEngine, but this doesn’t seem to work, since getRootObjectProperties() returns const.

  2. Defining a function in Javascript, ie:
    function myJSfunction(var v) { RegisteredObject.myJSfunction(v); }
    But how would I feed this isn’t the JavascriptEngine object such that it didn’t forget it?

  3. Pre-processing the String to that it prepends “RegisteredObject.” to the beginning. This would work, but it seems hackish.

My Javascript isn’t that good, so I’m not even sure if I’m asking a question about JUCE or about Javascript here. If there’s an error in my thinking, please point it out.

I’m still stuck on this. Can anyone help?

I’ve found that it is possible to add to the root object using getRootObjectProperties(). (I’m not sure why it is possible, since the function clearly returns a const reference, but I’ll take it).

However, if I add a method to the root object, I still can’t access it as a function from within the script.

    JavascriptEngine js;

    auto root = js.getRootObjectProperties();

    const auto id = Identifier("dbg");

    var::NativeFunction testFunction = [](const var::NativeFunctionArgs& a)
                                       {
                                           DBG(a.arguments->toString());
                                           return var::undefined();
                                       };

    root.set(id, testFunction);

    const auto error = js.execute(" dbg(\"hello world\") ");

    if (error.failed()) DBG(error.getErrorMessage());
              // prints "Line 1, column 5 : This expression is not a function!"

I’m struggling to see why this doesn’t work, as the root object has a constructor which defines several other native functions is basically the same way, and they are all accessible from the global namespace.

OK, I figured it out. In my example code, root was a copy of the root object, not a reference to it. It should have been auto& root = js.getRootObjectProperties();. From there, you can do a const_cast to modify the actual root object. It’s taboo, but it works!

If anyone has any other ideas about how to access the root, please let me know.