Good SIMPLE v8 C++ wrapping example?

282 de afișări
Accesați primul mesaj necitit

kent williams

necitită,
14 dec. 2016, 17:53:3114.12.2016
– v8-users
Call me silly but I've spent this afternoon looking at v8/samples and the various web pages/blog entries (and the embedder's guide) and not getting a clear picture of how to create a new object type with methods and a constructor in v8.

Is there a good example out there?

In my particular case, I have this implemented using SpiderMonkey, which seems a lot more straightforward.  It has the JS_InitClass call where you pass in a constructor, a destructor, and an array of method descriptors.

In v8, it seems spread out and confusing.  There's this: https://gist.github.com/mythagel/4527961 but end up sidetracked by decoding all the C++11 junk.

If that's it, I'll just buckle down and figure it out. But if someone can point me at something to start from, I'd be much obliged!

Yang Guo

necitită,
15 dec. 2016, 01:20:1315.12.2016
– v8-users
Yes, the documentation for various APIs is a bit lacking, unless you already know where to look. For what you are describing it sounds like you want to take a look here:


Cheers,

Yang

kent williams

necitită,
16 dec. 2016, 18:08:0216.12.2016
– v8-users
Here's what I was able to scrape together based on the example given.

https://gist.github.com/Chaircrusher/48d53ee78567ca213f7a858e91e4771d

Questions:
1. Where do I add functions to the object template? I am doing it by calling ObjectTemplate::Set on the ObjectTemplate for the class.  Will that work for eg:

var x = new TestClass;
x.Method(something);

2. The constructor function for an object template -- I read conflicting information about how this is supposed to work. Some places say that the args.This() is an allocated instance of your object. The xample Yang Guo gave (which this code follows) suggests you have to allocate an object, set its Prototype to args.Holder()->GetPrototype().  Which is it?

kent williams

necitită,
19 dec. 2016, 13:18:3119.12.2016
– v8-users
That example has some weird:
Line 11341:

This->SetPrototype(context, proto).FromJust();

What is the FromJust() for? Does that method have side effects?
Same deal at 11347:
This->Set(context, v8_str("a"), args[0]).FromJust();

tbl...@icloud.com

necitită,
23 dec. 2016, 20:55:1623.12.2016
– v8-users
SetPrototype and Set can fail, so they return optionals. FromJust asserts that the optional is not empty (and crashes the program if it is empty).
Răspundeți tuturor
Răspundeți autorului
Redirecționați
0 mesaje noi