From 1f2eaeb47f8b7ea4f4a611ac7a3ac1d7ab292e07 Mon Sep 17 00:00:00 2001 From: hogliux Date: Thu, 8 Sep 2016 10:49:31 +0100 Subject: [PATCH] Added support for accessing the properties of a javascript object via the subscript operator --- .../juce_core/javascript/juce_Javascript.cpp | 32 +++++++++++++++---- 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/modules/juce_core/javascript/juce_Javascript.cpp b/modules/juce_core/javascript/juce_Javascript.cpp index 2fa845fa8e..13d572c30d 100644 --- a/modules/juce_core/javascript/juce_Javascript.cpp +++ b/modules/juce_core/javascript/juce_Javascript.cpp @@ -412,9 +412,16 @@ struct JavascriptEngine::RootObject : public DynamicObject var getResult (const Scope& s) const override { var arrayVar (object->getResult (s)); // must stay alive for the scope of this method + var key = index->getResult (s); if (const Array* array = arrayVar.getArray()) - return (*array) [static_cast (index->getResult (s))]; + if (key.isInt() || key.isInt64() || key.isDouble()) + return (*array) [static_cast (key)]; + + if (DynamicObject* o = arrayVar.getDynamicObject()) + if (key.isString()) + if (const var* v = getPropertyPointer (o, Identifier (key))) + return *v; return var::undefined(); } @@ -422,15 +429,28 @@ struct JavascriptEngine::RootObject : public DynamicObject void assign (const Scope& s, const var& newValue) const override { var arrayVar (object->getResult (s)); // must stay alive for the scope of this method + var key = index->getResult (s); if (Array* array = arrayVar.getArray()) { - const int i = index->getResult (s); - while (array->size() < i) - array->add (var::undefined()); + if (key.isInt() || key.isInt64() || key.isDouble()) + { + const int i = key; + while (array->size() < i) + array->add (var::undefined()); + + array->set (i, newValue); + return; + } + } - array->set (i, newValue); - return; + if (DynamicObject* o = arrayVar.getDynamicObject()) + { + if (key.isString()) + { + o->setProperty (Identifier (key), newValue); + return; + } } Expression::assign (s, newValue);