Inspect/reflect emscripten::val from c++

1,328 views
Skip to first unread message

Lee Morgan

unread,
May 13, 2014, 6:37:34 AM5/13/14
to emscripte...@googlegroups.com
Hi,

Can I access the object keys given an emscripten::val.

How do I get type information about a val? or an array length etc

Thanks!

Chad Austin

unread,
May 15, 2014, 2:04:33 AM5/15/14
to emscripte...@googlegroups.com
On Tue, May 13, 2014 at 3:37 AM, Lee Morgan <lee.morg...@gmail.com> wrote:
Hi,

Can I access the object keys given an emscripten::val.

Hi Lee,

That's a good question.  Here's one way:

#include <stdio.h>
#include <emscripten/val.h>

using emscripten::val;

int main() {
    val window = val::global("window");
    val keys = val::global("Object").call<val>("keys", window);
    int length = keys["length"].as<int>();
    for (int i = 0; i < length; ++i) {
        printf("%s\n", keys[i].as<std::string>().c_str());
    }
}

The example program looks up the global 'window' object and then prints out all of the keys.


How do I get type information about a val? or an array length etc

The above example illustrates getting array length, but I don't think there's a typeof operator exposed yet.

In particular, what kind of type information do you need?

Thanks,
Chad

Lee Morgan

unread,
May 15, 2014, 7:41:41 AM5/15/14
to emscripte...@googlegroups.com
Hi Chad,

Thanks for your answer.

Say I want to convert a JS value to my C++ variant type, so if string, cast string if number cast float, if array iterate etc

(I'm trying to store a JS object as c++ map)

Cheers
Lee

Chad Austin

unread,
May 15, 2014, 2:19:31 PM5/15/14
to emscripte...@googlegroups.com
On Thu, May 15, 2014 at 4:41 AM, Lee Morgan <lee.morg...@gmail.com> wrote:
Hi Chad,

Thanks for your answer.

Say I want to convert a JS value to my C++ variant type, so if string, cast string if number cast float, if array iterate etc

(I'm trying to store a JS object as c++ map)

I see, cool.

When I get home to my Ubuntu VM where I do emscripten development, I'll try to expose the 'typeof' operator to emval.

Just so you're aware, here are the semantics of the JS typeof operator: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof


Cheers
Lee

On Thursday, May 15, 2014 7:04:33 AM UTC+1, Chad Austin wrote:
On Tue, May 13, 2014 at 3:37 AM, Lee Morgan <lee.morg...@gmail.com> wrote:
Hi,

Can I access the object keys given an emscripten::val.

Hi Lee,

That's a good question.  Here's one way:

#include <stdio.h>
#include <emscripten/val.h>

using emscripten::val;

int main() {
    val window = val::global("window");
    val keys = val::global("Object").call<val>("keys", window);
    int length = keys["length"].as<int>();
    for (int i = 0; i < length; ++i) {
        printf("%s\n", keys[i].as<std::string>().c_str());
    }
}

The example program looks up the global 'window' object and then prints out all of the keys.


How do I get type information about a val? or an array length etc

The above example illustrates getting array length, but I don't think there's a typeof operator exposed yet.

In particular, what kind of type information do you need?

Thanks,
Chad

--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Chad Austin
Technical Director, IMVU

Chad Austin

unread,
May 17, 2014, 2:47:55 AM5/17/14
to emscripte...@googlegroups.com
When the linked pull request is merged, you will be able to call val::typeof(), which returns another val.  You probably want to write val::typeof().as<std::string>() most of the time.

https://github.com/kripken/emscripten/pull/2360

Cheers,
Chad

Lee Morgan

unread,
May 19, 2014, 5:37:34 AM5/19/14
to emscripte...@googlegroups.com
Fantastic, Thanks Chad!
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-discuss+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Chad Austin
Technical Director, IMVU

Reply all
Reply to author
Forward
0 new messages