Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Returning a wrapped class from another class #2273

Closed
zbjornson opened this issue Jul 30, 2015 · 3 comments
Closed

Returning a wrapped class from another class #2273

zbjornson opened this issue Jul 30, 2015 · 3 comments
Labels
c++ Issues and PRs that require attention from people who are familiar with C++. question Issues that look for answers.

Comments

@zbjornson
Copy link
Contributor

I have two C++ classes. A method on one of them is supposed to return a new instance of b. I tried to follow the pattern described here:

// foo.cc
const int argc = 3;
Local<Value> argv[argc] = { NanNew(*arr), NanNew(sw), NanNew(sh) };
Local<Function> cons = Local<Function>::New(Isolate::GetCurrent(), Bar::constructor);
NanReturnValue(cons->NewInstance(argc, argv));

but then:

No instance of overloaded function "v8::Local<T>::New [with T=v8::Function]" matches the argument list. Argument types are: (v8:Isolate *(), v8::Persistent<v8::FunctionTemplate, v8::NonCopyablePersistentTraits<V8::Functiontemplate>>)

I can't find an example of this pattern in the io.js source, and the closest google result is an unanswered SO question. I found one example where a factory method was added to Bar but that seems like it should be unnecessary. Any tips on the pattern to use here? Thanks.

@Fishrock123 Fishrock123 added the c++ Issues and PRs that require attention from people who are familiar with C++. label Jul 30, 2015
@brendanashworth brendanashworth added the question Issues that look for answers. label Jul 30, 2015
@bnoordhuis
Copy link
Member

Bar::constructor is a Persistent<Function>? You need to turn it into a Local first: Local<Function> constructor = Local<Function>::New(isolate, Bar::constructor);.

@zbjornson
Copy link
Contributor Author

I think that's what I did in the 3rd line of my example unless I'm misreading.

But, Bar::constructor is actually a Persistent<FunctionTemplate>, not a Function. That's what the package I'm updating (node-canvas) had originally and, from my understanding, is the correct type to use for a class returned to JS.

class ImageData: public node::ObjectWrap {
    public:
    static Persistent<FunctionTemplate> constructor;
    static void Initialize(Handle<Object> target);
    //...
    ImageData(uint8_t *data, int width, int height) : _width(width), _height(height), _data(data) {}
    private:
    //...
};

Full context:

Thanks.

@zbjornson
Copy link
Contributor Author

Erm, think I had the solution in my temporary ImageData factory method, which was to call GetFunction on the ImageData constructor FunctionTemplate:

const int argc = 3;
Local<Value> argv[argc] = { clampedArray, NanNew(sw), NanNew(sh) };
Local<FunctionTemplate> cons = NanNew(ImageData::constructor);
Local<Object> instance = cons->GetFunction()->NewInstance(argc, argv);
NanReturnValue(instance);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ Issues and PRs that require attention from people who are familiar with C++. question Issues that look for answers.
Projects
None yet
Development

No branches or pull requests

4 participants