# Anyone for Javascript?

**URL:** <https://forum.juce.com/t/anyone-for-javascript/11675>\
**Category:** General JUCE discussion\
**Created:** [October 17, 2013, 8:09am UTC](https://forum.juce.com/t/anyone-for-javascript/11675 "2013-10-17T08:09:26Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![lilin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/lilin/32/24_2.png) [@lilin](https://forum.juce.com/u/lilin)\
**Post date:** [October 17, 2013, 8:09am UTC](https://forum.juce.com/t/anyone-for-javascript/11675/1 "2013-10-17T08:09:26Z")

</div>

> http://www.juce.com/forum/topic/anyone-javascript

&nbsp;

Very good news :)

In our GUI system we use JS ( + XML for layout and styling) for all GUI logic.

We decided to use Google's V8. It is big, but&nbsp;it works like a charm ! It is easy to use, easy to communicate with C++ and it is performant.

A very good point for V8 is its debugger. In Chrome dev tools there is a plugin for eclipse that you can connect to your app's VM.

---

<div class="post-metadata">

**Author:** ![chrisboy2000](https://avatars.discourse-cdn.com/v4/letter/c/a3d4f5/32.png) [@chrisboy2000](https://forum.juce.com/u/chrisboy2000)\
**Post date:** [October 17, 2013, 10:35am UTC](https://forum.juce.com/t/anyone-for-javascript/11675/2 "2013-10-17T10:35:42Z")

</div>

Hi Jules,

seems great! I played around with a few scripting engines myself and decided on Lua, but I think two additional features would make your JS script engine more versatile. However, these feature requests are all about real time usage, so if your expected field of usage is GUI management (which is mostly executed at initialization) these requests are not relevant :)

1.) divide the parsing and the execution into two functions. According to your API documentation there is only the possibility to run&nbsp;a javascript code that is parsed just before execution. If some functions are executed several times (eg. callbacks for note-on messages) the overhead of reparsing the script string is unnecessary. This of course needs a new object type for bytecode:

```
String code = "..." // JS code

JSByteCode b = compile(code);

Result r = run(b, 100); // this function be executed repeatedly without the parsing overhead
```

2. I don't know if it is already possible but can javascripts make a non-busy wait (something like sleep())? I don't think there is a necessary for OS-level multithreading, but if a js function can be yielded and resumed at a later time, it should be possible to implement it.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [October 17, 2013, 10:52am UTC](https://forum.juce.com/t/anyone-for-javascript/11675/3 "2013-10-17T10:52:18Z")

</div>

Ouch.. not sure about real-time use! Even if no parsing gets done, there are still loads of strings and objects being constructed and passed around during execution, with plenty of allocator use, so running it in e.g. an audio thread would be at your own risk! (I guess the same is true of lua and other scripts too though)

Re: parsing:&nbsp;the way I'd expect it to be used would be by calling execute() to parse your script file once, then call&nbsp;evaluate() to invoke functions/values. Parsing something evaluate ("myfunction (1, 2)")&nbsp;will take negligible time, though I will also add a method to directly invoke a function by name, mainly to make it easier&nbsp;to pass it some var objects as&nbsp;args rather than a string.

Re: sleep(), it's easy enough to give it some native code to provide functions&nbsp;like that if you need them. I'm not planning on providing a massive set of JS library calls&nbsp;- it's up to you to provide what you&nbsp;need for your own purposes!

---

<div class="post-metadata">

**Author:** ![Mike](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/mike/32/629_2.png) [@Mike](https://forum.juce.com/u/Mike)\
**Post date:** [October 17, 2013, 10:58am UTC](https://forum.juce.com/t/anyone-for-javascript/11675/4 "2013-10-17T10:58:30Z")

</div>

This is very impressive Jules. I'm probably never going to use it, but I must congratulate you!

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [October 17, 2013, 11:20am UTC](https://forum.juce.com/t/anyone-for-javascript/11675/5 "2013-10-17T11:20:50Z")

</div>

Thanks! Yes, I'm pretty proud of it as a piece of coding!

---

<div class="post-metadata">

**Author:** ![sambecket](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/sambecket/32/1862_2.png) [@sambecket](https://forum.juce.com/u/sambecket)\
**Post date:** [October 19, 2013, 1:03am UTC](https://forum.juce.com/t/anyone-for-javascript/11675/6 "2013-10-19T01:03:26Z")

</div>

It is amazing the Javascript engine, i will try to make it run the x86 emulator just for the fun of it:

http://copy.sh/v24/

Jules, is there anything planned to bring some sort of functionality with the Javascript engine + Browser Component? To use it for callbacks and reading vars to/from it?

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [October 19, 2013, 8:13am UTC](https://forum.juce.com/t/anyone-for-javascript/11675/7 "2013-10-19T08:13:36Z")

</div>

Thanks! No, no plans to integrate with the browser comp, I don't think the embedded browsers expose that kind of functionality.

---

<div class="post-metadata">

**Author:** ![jack\_anderson](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/jack_anderson/32/737_2.png) [@jack\_anderson](https://forum.juce.com/u/jack_anderson)\
**Post date:** [October 19, 2013, 7:51pm UTC](https://forum.juce.com/t/anyone-for-javascript/11675/8 "2013-10-19T19:51:53Z")

</div>

yeah. juce works quite well with v8. I had messed around with it a year or two ago. I had planned to release a thing that would allow creating full gui applications using js, but sorta dropped the ball with it :/

---

<div class="post-metadata">

**Author:** ![stijnvanbeek](https://avatars.discourse-cdn.com/v4/letter/s/e480ec/32.png) [@stijnvanbeek](https://forum.juce.com/u/stijnvanbeek)\
**Post date:** [September 2, 2015, 1:14pm UTC](https://forum.juce.com/t/anyone-for-javascript/11675/9 "2015-09-02T13:14:08Z")

</div>

Hello,

&nbsp;

I have a problem using the JavaScriptEngine class in combination with a DynamicObject descendant. For example, this is my class:

&nbsp;

class TestObject : public DynamicObject {

public:

&nbsp; &nbsp; bool hasMethod(const Identifier &methodName) const override final

&nbsp; &nbsp; {

&nbsp; &nbsp; &nbsp; &nbsp; if (methodName.toString().toStdString() == "do")

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;

&nbsp; &nbsp; &nbsp; &nbsp; else

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return DynamicObject::hasMethod(methodName);

&nbsp; &nbsp; }

&nbsp;&nbsp; &nbsp;

&nbsp; &nbsp; juce::var invokeMethod (Identifier methodName, const var::NativeFunctionArgs &args) override final

&nbsp; &nbsp; {

&nbsp; &nbsp; &nbsp; &nbsp; if (methodName.toString().toStdString() == "do")

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return var(10);

&nbsp; &nbsp; &nbsp; &nbsp; else

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return DynamicObject::invokeMethod(methodName, args);

&nbsp; &nbsp; }

private:

};

&nbsp;

and then I run this code:

&nbsp;

&nbsp; &nbsp;&nbsp;JavascriptEngine javaScriptEngine;

&nbsp; &nbsp; TestObject::Ptr object(new TestObject);

&nbsp; &nbsp; TestObject::Ptr nestedObject(new TestObject);

&nbsp;&nbsp; &nbsp;

&nbsp; &nbsp; object-\>setProperty("nested", var(nestedObject));

&nbsp; &nbsp; nestedObject-\>setProperty("x", var(10));

&nbsp;&nbsp; &nbsp;

&nbsp; &nbsp; javaScriptEngine.registerNativeObject("object", object);

&nbsp;

&nbsp; &nbsp; cout \<\< javaScriptEngine.evaluate("object.nested.x").toString().toStdString() \<\< endl; &nbsp;// this works: outputs 10

&nbsp; &nbsp; cout \<\< javaScriptEngine.evaluate("object.do()").toString().toStdString() \<\< endl; // this doesnt work: outputs undefined

&nbsp; &nbsp; cout \<\< javaScriptEngine.evaluate("object.nested.do()").toString().toStdString() \<\< endl; // doesnt work either

&nbsp;

TestObject::hasMethod and TestObject::invokeMethod are never called during the evaluate() calls. Is this a bug or am I doing something wrong?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [September 7, 2015, 10:49am UTC](https://forum.juce.com/t/anyone-for-javascript/11675/10 "2015-09-07T10:49:20Z")

</div>

(There's really no need to call&nbsp;"toStdString", this code would probably all be fine without it)

It sounds like it should probably work - did you debug it to see if your DynamicObject methods were being called?

---

<div class="post-metadata">

**Author:** ![jzrake](https://avatars.discourse-cdn.com/v4/letter/j/b19c9b/32.png) [@jzrake](https://forum.juce.com/u/jzrake)\
**Post date:** [March 15, 2017, 7:59pm UTC](https://forum.juce.com/t/anyone-for-javascript/11675/11 "2017-03-15T19:59:13Z")

</div>

Hi Jules,

The JS parser fails with some internal assertion if you try to evaluate the string “{”. Doesn’t bother me if it doesn’t bother you.

Thanks!

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [March 16, 2017, 9:13am UTC](https://forum.juce.com/t/anyone-for-javascript/11675/12 "2017-03-16T09:13:57Z")

</div>

OK, ta - probably just an unnecessary assertion, but will have a look.

---

<div class="post-metadata">

**Author:** ![jzrake](https://avatars.discourse-cdn.com/v4/letter/j/b19c9b/32.png) [@jzrake](https://forum.juce.com/u/jzrake)\
**Post date:** [March 23, 2017, 9:03pm UTC](https://forum.juce.com/t/anyone-for-javascript/11675/13 "2017-03-23T21:03:11Z")

</div>

This is a little off topic, but just curious: is there a way to add anything other than a DynamicObject to the root namespace in the JS engine? A method such as

`registerVariable (const Identifier &objectName, var someVariable)`

would be nice to have in some situations.

---

<div class="post-metadata">

**Author:** ![chrisboy2000](https://avatars.discourse-cdn.com/v4/letter/c/a3d4f5/32.png) [@chrisboy2000](https://forum.juce.com/u/chrisboy2000)\
**Post date:** [March 24, 2017, 11:08pm UTC](https://forum.juce.com/t/anyone-for-javascript/11675/14 "2017-03-24T23:08:18Z")

</div>

There is the getRootProperties (or something like that) method which returns a NamedValueSet that you can alter. You’ll have to const cast it though…

---

<div class="post-metadata">

**Author:** ![jzrake](https://avatars.discourse-cdn.com/v4/letter/j/b19c9b/32.png) [@jzrake](https://forum.juce.com/u/jzrake)\
**Post date:** [March 25, 2017, 2:14pm UTC](https://forum.juce.com/t/anyone-for-javascript/11675/15 "2017-03-25T14:14:12Z")

</div>

Well, I thought there might be a good reason the return value was const. That, and lack of the method I asked for suggests to me that Jules meant for the root namespace to be left alone.

---

<div class="post-metadata">

**Author:** ![chrisboy2000](https://avatars.discourse-cdn.com/v4/letter/c/a3d4f5/32.png) [@chrisboy2000](https://forum.juce.com/u/chrisboy2000)\
**Post date:** [March 25, 2017, 10:07pm UTC](https://forum.juce.com/t/anyone-for-javascript/11675/16 "2017-03-25T22:07:13Z")

</div>

I think the reason it is const is just because no one asked for it (I was [requesting](https://forum.juce.com/t/access-to-the-properties-of-the-javascript-root-object/13689) this method for implementing some debugging features and didn’t need to alter it).

I can’t think of any scenario that might cause harms if you change the NamedValueSet. Take for instance the assignment of a UnqualifiedName:

```javascript
        void assign (const Scope& s, const var& newValue) const override
        {
            if (var* v = getPropertyPointer (s.scope, name))
                *v = newValue;
            else
                s.root->setProperty (name, newValue);
        }

```

So even if you do the most evil thing possible and delete a property (which I would not recommend), it just sets it again - we’ll ignore the race condition that might occur if you delete it on another thread just after the pointer was created in the if-condition since the whole engine is not threadsafe at all.

It would be more problematic if this assignment would store a pointer to a var in the NamedValueSet which changes as soon as you call a non const method on the NamedValueSet.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [March 30, 2017, 9:47am UTC](https://forum.juce.com/t/anyone-for-javascript/11675/17 "2017-03-30T09:47:31Z")

</div>

> [@zrake](#):
>
> The JS parser fails with some internal assertion if you try to evaluate the string “{”. Doesn’t bother me if it doesn’t bother you.

Tried this in the demo Javascript parser page and it seems completely fine - gives a sensible error message. How did you trigger it?

---

<div class="post-metadata">

**Author:** ![jzrake](https://avatars.discourse-cdn.com/v4/letter/j/b19c9b/32.png) [@jzrake](https://forum.juce.com/u/jzrake)\
**Post date:** [March 30, 2017, 12:41pm UTC](https://forum.juce.com/t/anyone-for-javascript/11675/18 "2017-03-30T12:41:09Z")

</div>

```
JavascriptEngine engine;
engine.evaluate ("{");

```

Here is the traceback:

 ![](https://us1.discourse-cdn.com/flex026/uploads/juce/original/2X/1/17bb578190abf668ed7a80a59e3b394178e2a326.png)

```
Identifier::Identifier (const String& nm)
    : name (StringPool::getGlobalPool().getPooledString (nm))
{
    // An Identifier cannot be created from an empty string!
    jassert (nm.isNotEmpty()); // <- fails this assertion
}
```

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [March 30, 2017, 1:11pm UTC](https://forum.juce.com/t/anyone-for-javascript/11675/19 "2017-03-30T13:11:05Z")

</div>

OK, thanks, I’ll sort that out.

---

<div class="post-metadata">

**Author:** ![jzrake](https://avatars.discourse-cdn.com/v4/letter/j/b19c9b/32.png) [@jzrake](https://forum.juce.com/u/jzrake)\
**Post date:** [December 31, 2017, 7:49pm UTC](https://forum.juce.com/t/anyone-for-javascript/11675/20 "2017-12-31T19:49:09Z")

</div>

Hi Jules,

Here are other things that break the JS parser:

```
JavascriptEngine engine;
engine.evaluate ("function(");
engine.evaluate ("function(var)");

```

It’s always failing that same assertion, trying to create an identifier from an empty string.

Sounds petty, but shouldn’t users be able to type gibberish into the JS parser without it crashing the application?

[Next page](https://forum.juce.com/t/anyone-for-javascript/11675.md?page=2)
