JavaScript arrays in ConvertFromV8

Developer
Aug 12, 2010 at 8:07 AM

I needed to convert JavaScript arrays to CLR arrays.  I offer this patch:

Index: JavascriptInterop.cpp
===================================================================
--- JavascriptInterop.cpp    (revision 5780)
+++ JavascriptInterop.cpp    (working copy)
@@ -82,7 +82,15 @@
         return gcnew System::Double(iValue->NumberValue());
     else if (iValue->IsString())
         return gcnew System::String(*String::Utf8Value(iValue->ToString()));
-    else if (iValue->IsExternal())
+    else if (iValue->IsArray()) {
+        v8::Local<v8::Object> array_o = iValue->ToObject();
+        v8::Local<v8::Array> a = v8::Local<v8::Array>::Cast(array_o);
+        int l = a->Length();
+        array<System::Object^>^ clr_a = gcnew array<System::Object^>(l);
+        for (int i = 0; i < l; i ++)
+            clr_a[i] = ConvertFromV8(a->Get(i));
+        return clr_a;
+    } else if (iValue->IsExternal())
         return UnwrapObject(iValue);
     else if (iValue->IsObject())
     {

Coordinator
Aug 12, 2010 at 2:09 PM

Thanks.

A new release is about to go out which already supports these conversions. Stay tuned it will be released shortly.

Cheers