[dss-commits] r8758 - in dss/trunk: core tests

dss-commits at forum.digitalstrom.org dss-commits at forum.digitalstrom.org
Fri Sep 11 16:26:27 CEST 2009


Author: pstaehlin
Date: 2009-09-11 16:26:27 +0200 (Fri, 11 Sep 2009)
New Revision: 8758

Added:
   dss/trunk/tests/jshandlertests.cpp
Modified:
   dss/trunk/core/jshandler.cpp
   dss/trunk/core/jshandler.h
   dss/trunk/tests/CMakeLists.txt
Log:
- Modified ScriptObject to allow setting/getting 
  properties
- ScriptContext::getRootObject returns a script
  object. This makes it easier to modify the 
  root object.

References #127


Modified: dss/trunk/core/jshandler.cpp
===================================================================
--- dss/trunk/core/jshandler.cpp	2009-09-10 09:06:01 UTC (rev 8757)
+++ dss/trunk/core/jshandler.cpp	2009-09-11 14:26:27 UTC (rev 8758)
@@ -166,6 +166,7 @@
     if (m_pRootObject == NULL) {
       throw ScriptException("Could not create root-object");
     }
+    m_RootObject.reset(new ScriptObject(m_pRootObject, *this));
 
     JS_DefineFunctions(m_pContext, m_pRootObject, global_methods);
 
@@ -312,8 +313,20 @@
   : m_pObject(_pObject),
     m_Context(_context)
   {
+    assert(_pObject != NULL);
   } // ctor
 
+  ScriptObject::ScriptObject(ScriptContext& _context, ScriptObject* _pParent)
+  : m_pObject(NULL),
+    m_Context(_context)
+  {
+    JSObject* parentObj = NULL;
+    if(_pParent != NULL) {
+      parentObj = _pParent->m_pObject;
+    }
+    m_pObject = JS_NewObject(m_Context.getJSContext(), NULL, NULL, parentObj);
+  } // ctor
+
   template<>
   jsval ScriptObject::getProperty(const std::string& _name) {
     JSBool found;
@@ -335,17 +348,53 @@
   template<>
   std::string ScriptObject::getProperty(const std::string& _name) {
     jsval value = getProperty<jsval>(_name);
-    if(JSVAL_IS_STRING(value)) {
-      return std::string(JS_GetStringBytes(JSVAL_TO_STRING(value)));
-    }
-    throw ScriptException(std::string("Property is not of std::string type: ") + _name);
+    return m_Context.convertTo<std::string>(value);
   } // getProperty<string>
 
+  template<>
+  int ScriptObject::getProperty(const std::string& _name) {
+    jsval value = getProperty<jsval>(_name);
+    return m_Context.convertTo<int>(value);
+  } // getProperty<string>
+
+  template<>
+  void ScriptObject::setProperty(const std::string& _name, jsval _value) {
+    JS_SetProperty(m_Context.getJSContext(), m_pObject, _name.c_str(), &_value);
+  } // setProperty<jsval>
+
+  template<>
+  void ScriptObject::setProperty(const std::string& _name, const std::string& _value) {
+    JSString* str = JS_NewStringCopyN(m_Context.getJSContext(), _value.c_str(), _value.size());
+    setProperty(_name, STRING_TO_JSVAL(str));
+  } // setProperty<std::string>
+
+  template<>
+  void ScriptObject::setProperty(const std::string& _name, const char* _value) {
+    std::string str(_value);
+    setProperty<const std::string&>(_name, str);
+  } // setProperty<const char*>
+
+  template<>
+  void ScriptObject::setProperty(const std::string& _name, int _value) {
+    jsval val;
+    if(!JS_NewNumberValue(m_Context.getJSContext(), _value, &val)) {
+      throw ScriptException("could not allocate number");
+    }
+    setProperty(_name, val);
+  } // setProperty<int>
+
+  template<>
+  void ScriptObject::setProperty(const std::string& _name, ScriptObject* _value) {
+    assert(_value != NULL);
+    setProperty(_name, OBJECT_TO_JSVAL(_value->m_pObject));
+  } // setProperty<ScriptObject>
+
   bool ScriptObject::is(const std::string& _className) {
     return getClassName() == _className;
-  }
+  } // is
 
   const std::string ScriptObject::getClassName() {
     return getProperty<std::string>("className");
   } // getClassName
+
 }

Modified: dss/trunk/core/jshandler.h
===================================================================
--- dss/trunk/core/jshandler.h	2009-09-10 09:06:01 UTC (rev 8757)
+++ dss/trunk/core/jshandler.h	2009-09-11 14:26:27 UTC (rev 8758)
@@ -35,11 +35,13 @@
 #endif
 
 #include <boost/ptr_container/ptr_vector.hpp>
+#include <boost/scoped_ptr.hpp>
 
 namespace dss {
 
   class ScriptContext;
   class ScriptExtension;
+  class ScriptObject;
 
   /** Wrapper for a script runtime environment. The ScriptEnvironment
     * is also responsible for creating Contexts and enhancing them with
@@ -76,6 +78,7 @@
     JSScript* m_pScriptToExecute;
     std::string m_FileName;
     JSObject* m_pRootObject;
+    boost::scoped_ptr<ScriptObject> m_RootObject;
     JSObject* m_pSourceObject;
     ScriptEnvironment& m_Environment;
     JSContext* m_pContext;
@@ -107,6 +110,7 @@
     /** Returns a const reference to the ScriptEnvironment */
     const ScriptEnvironment& getEnvironment() const { return m_Environment; }
     ScriptEnvironment& getEnvironment() { return m_Environment; }
+    ScriptObject& getRootObject() { return *m_RootObject; }
   public:
 
     /** Helper function to convert a jsval to a t. */
@@ -169,6 +173,7 @@
     ScriptContext& m_Context;
   public:
     ScriptObject(JSObject* _pObject, ScriptContext& _context);
+    ScriptObject(ScriptContext& _context, ScriptObject* _pParent);
 
     /** Returns the objects "classname" property. This property must be
       * present for this call to succeed.
@@ -182,7 +187,12 @@
     /** Returns the property named \a _name as type \a t */
     template<class t>
     t getProperty(const std::string& _name);
+
+    /** Sets the property named \a _name to \a _value */
+    template<class t>
+    void setProperty(const std::string& _name, t _value);
   }; // ScriptObject
-}
 
+} // namespace dss
+
 #endif

Modified: dss/trunk/tests/CMakeLists.txt
===================================================================
--- dss/trunk/tests/CMakeLists.txt	2009-09-10 09:06:01 UTC (rev 8757)
+++ dss/trunk/tests/CMakeLists.txt	2009-09-11 14:26:27 UTC (rev 8758)
@@ -4,9 +4,9 @@
 
 add_executable( dsstests EXCLUDE_FROM_ALL testrunner.cpp ../namespaces.cpp
 	basetests.cpp  datetoolstests.cpp  ds485tests.cpp
-	        eventtests.cpp  modeljstests.cpp  modeltests.cpp
-		        propertysystemtests.cpp  scriptstest.cpp
-			seriestests.cpp)
+        eventtests.cpp  modeljstests.cpp  modeltests.cpp
+        propertysystemtests.cpp  scriptstest.cpp
+	seriestests.cpp jshandlertests.cpp)
 
 target_link_libraries(dsstests ${BOOST_TEST_LIB} core unix webservices 
 	pthread mongoose slaves ${REQUIRED_LIBS})

Added: dss/trunk/tests/jshandlertests.cpp
===================================================================
--- dss/trunk/tests/jshandlertests.cpp	                        (rev 0)
+++ dss/trunk/tests/jshandlertests.cpp	2009-09-11 14:26:27 UTC (rev 8758)
@@ -0,0 +1,55 @@
+/*
+    Copyright (c) 2009 digitalSTROM.org, Zurich, Switzerland
+    Copyright (c) 2009 futureLAB AG, Winterthur, Switzerland
+
+    This file is part of digitalSTROM Server.
+
+    digitalSTROM Server is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    digitalSTROM Server is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with digitalSTROM Server. If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#define BOOST_TEST_NO_MAIN
+#define BOOST_TEST_DYN_LINK
+#include <boost/test/unit_test.hpp>
+#include <boost/shared_ptr.hpp>
+
+#include <string>
+
+#include "../core/jshandler.h"
+
+using namespace dss;
+
+BOOST_AUTO_TEST_SUITE(JSHandler)
+
+BOOST_AUTO_TEST_CASE(testSimpleObject) {
+  boost::scoped_ptr<ScriptEnvironment> env(new ScriptEnvironment());
+  env->initialize();
+
+  boost::scoped_ptr<ScriptContext> ctx(env->getContext());
+  ScriptObject obj(*ctx, NULL);
+  obj.setProperty("testing", 1);
+  BOOST_CHECK_EQUAL(obj.getProperty<int>("testing"), 1);
+  ctx->getRootObject().setProperty("obj", &obj);
+
+  ctx->loadFromMemory("obj.testing");
+  BOOST_CHECK_EQUAL(ctx->evaluate<int>(), 1);
+  obj.setProperty("testing", 0);
+  BOOST_CHECK_EQUAL(ctx->evaluate<int>(), 0);
+
+  obj.setProperty("testing", "test");
+  BOOST_CHECK_EQUAL(obj.getProperty<std::string>("testing"), "test");
+  BOOST_CHECK_EQUAL(ctx->evaluate<std::string>(), "test");
+} // testSimpleObject
+
+BOOST_AUTO_TEST_SUITE_END()



More information about the dss-commits mailing list