[beast: 27/70] V8BSE: wrap vector derived Sequence types by v8:Array copy



commit cf3c37016f0203afae68dd148cb0280e17177551
Author: Tim Janik <timj gnu org>
Date:   Wed Feb 22 16:53:22 2017 +0100

    V8BSE: wrap vector derived Sequence types by v8:Array copy
    
    Signed-off-by: Tim Janik <timj gnu org>

 ebeast/v8bse/V8Stub.py     |    4 +++-
 ebeast/v8bse/nodemodule.cc |   37 +++++++++++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+), 1 deletions(-)
---
diff --git a/ebeast/v8bse/V8Stub.py b/ebeast/v8bse/V8Stub.py
index d71aa45..6c49d2e 100644
--- a/ebeast/v8bse/V8Stub.py
+++ b/ebeast/v8bse/V8Stub.py
@@ -103,10 +103,12 @@ class Generator:
     s += '\n// v8pp::convert<> specialisations\n'
     s += 'namespace v8pp {\n'
     for tp in v8pp_class_types:
+      cn = colon_typename (tp)
       if tp.storage == Decls.INTERFACE:
-        cn = colon_typename (tp)
         s += 'template<> struct convert%-40s : convert_AidaRemoteHandle<%s>  {};\n' % ('<%s>' % cn, cn)
         s += 'template<> struct convert%-40s : convert_AidaRemoteHandle<%s*> {};\n' % ('<%s*>' % cn, cn)
+      elif tp.storage == Decls.SEQUENCE:
+        s += 'template<> struct convert%-40s : convert_AidaSequence<%s> {};\n' % ('<%s>' % cn, cn)
     s += '} // v8pp\n'
     # V8stub - main binding stub
     s += '\n// Main binding stub\n'
diff --git a/ebeast/v8bse/nodemodule.cc b/ebeast/v8bse/nodemodule.cc
index 2a9bb90..1ebbe09 100644
--- a/ebeast/v8bse/nodemodule.cc
+++ b/ebeast/v8bse/nodemodule.cc
@@ -106,6 +106,43 @@ struct convert_AidaRemoteHandle<DerivedHandle*>
   static J    to_v8    (v8::Isolate *const isolate, const N *n)             { return !n ? J() : 
v8pp::convert<N>::to_v8 (isolate, *n); }
 };
 
+/// Helper to specialize v8pp::convert<> for all Sequence types.
+template<class Class>
+struct convert_AidaSequence
+{
+  using from_type = Class;
+  using value_type = typename Class::value_type;
+  using to_type = v8::Handle<v8::Array>;
+  static bool
+  is_valid (v8::Isolate*, v8::Handle<v8::Value> value)
+  {
+    return !value.IsEmpty() && value->IsArray();
+  }
+  static from_type
+  from_v8 (v8::Isolate *const isolate, v8::Handle<v8::Value> value)
+  {
+    v8::HandleScope scope (isolate);
+    if (!is_valid (isolate, value))
+      throw std::invalid_argument ("expected array object");
+    v8::Local<v8::Array> arr = value.As<v8::Array>();
+    const size_t arrlen = arr->Length();
+    from_type result;
+    result.reserve (arrlen);
+    for (size_t i = 0; i < arrlen; i++)
+      result.push_back (v8pp::from_v8<value_type> (isolate, arr->Get (i)));
+    return result;
+  }
+  static to_type
+  to_v8 (v8::Isolate *const isolate, from_type const &value)
+  {
+    v8::EscapableHandleScope scope (isolate);
+    v8::Local<v8::Array> arr = v8::Array::New (isolate, value.size());
+    for (size_t i = 0; i < value.size(); i++)
+      arr->Set (i, v8pp::to_v8 (isolate, value[i]));
+    return scope.Escape (arr);
+  }
+};
+
 #include "v8bse.cc"
 
 // v8pp binding for Bse


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]