How to call javascript anonymous function in C++

155 views
Skip to first unread message

Charles Han

unread,
Jul 12, 2012, 1:11:36 AM7/12/12
to v8-u...@googlegroups.com
Hi,

I am trying to use a third party javascript lib in my C++ app. The javascript lib named HPACK.json has anonymous function like this:

(function(cache){
 (this.JSON || (JSON = {})).hpack = function(collection, compression){ ...};
})([]);

I have tried with this code using V8 but no luck:
//Load the javascript into the V8 handle already
Handle<String> foo_name = String::New("json.hpack");
Handle<Value> foo_val = context->Global()->Get(foo_name);

How can I get access to this function in my C++ code?

Thanks

Matthias Ernst

unread,
Jul 12, 2012, 2:02:05 AM7/12/12
to v8-u...@googlegroups.com
You have to look up the nested path individually, i.e. first JSON,
then hpack, i.e.

Handle<Function> hpack =
context->Global()->Get(String::New("JSON")).As<Object>()->Get(String::New("hpack")).As<Function>();

See also the thread starting here:
https://groups.google.com/forum/#!msg/v8-users/X1Dtpkgm6eA/sqwmpFDx8s8J

Matthias
> --
> v8-users mailing list
> v8-u...@googlegroups.com
> http://groups.google.com/group/v8-users

Charles Han

unread,
Jul 13, 2012, 12:43:25 AM7/13/12
to v8-u...@googlegroups.com
Thanks Matthias, this works indeed.
Reply all
Reply to author
Forward
0 new messages