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

wrapping objective c objects from javascript #83

Open
astroblastro opened this issue Apr 27, 2016 · 6 comments
Open

wrapping objective c objects from javascript #83

astroblastro opened this issue Apr 27, 2016 · 6 comments

Comments

@astroblastro
Copy link

astroblastro commented Apr 27, 2016

I'm working on an experiment where I'm trying to nest a framework instances in nodobjc in a NSView returned from electron. Is there a way to cast these pointers so that I can use the functions in this object?

var myView = mainWindow.getNativeWindowHandle() //returns pointer to NSView
console.log(myView);

output: Buffer@0x7ff8a2ff6300 c0 2d 06 a5 f8 7f 00 00

@TooTallNate
Copy link
Owner

Well, it's not great, but you can do:

require('nodobjc/lib/core').wrapValue(buffer, '@');

Pull requests appreciated to add perhaps a Class#cast(pointer) function or something more user-friendly for this use-case.

@astroblastro
Copy link
Author

First off, thank you so much for the prompt response... and I guess I should apologize because I'm new to objective c (my experience lies mostly in c#, c++ and starting on javascript).

this would return an "@"? I'm not exactly sure what that means. Would this take the buffer and cast it as an id? Any thoughts on how i'd use NSView based on that?

@TooTallNate
Copy link
Owner

See https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtTypeEncodings.html.

@ means an generic "id", which you can send arbitrary messages to. Essentially, the result of the wrapValue() call is the NSView instance. Invoke it as a function to send NSView instance method messages to it.

var nsview = .wrapValue();
nsview('setFrameSize', { width: 100, height: 100 });

@astroblastro
Copy link
Author

i didn't get terribly far. I just crashes out right when I try to call any functions from nsview, it doesn't even catch the exception.

var $ = require ('nodobjc');
var $C = require ('nodobjc/lib/core');
.........
var myView = mainWindow.getNativeWindowHandle();
console.log(JSON.stringify(myView));
console.log(typeof myView);
var nsview = $C.wrapValue(myView, '@');
try {
nsview('setFrameSize', { width: 100, height: 100 });
} catch(err){
console.log(err);
}

/////
// output:
// {"type":"Buffer","data":[48,4,19,156,229,127,0,0]}
// object

@astroblastro
Copy link
Author

fixed by making this change...
var nsview = $C.wrapValue(myView, '@');

to...

var nsview = $C.wrapValue(myView.readPointer(0), '@');

@bkorobeinikov
Copy link

bkorobeinikov commented Jun 13, 2016

Could you share your working code here? i'm trying to do the same thing but chrome window just crash when calling 'setFrameSize' or any other method from nsview object.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants