TooTallNate / NodObjC

The Node.js ⇆ Objective-C bridge
http://tootallnate.github.io/NodObjC
MIT License
1.41k stars 124 forks source link

wrapping objective c objects from javascript #83

Open astroblastro opened 8 years ago

astroblastro commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

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

to...

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

bkorobeinikov commented 8 years ago

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.