lukaskollmer / objc

🔮 NodeJS ↔ Objective-C bridge (experimental)
MIT License
98 stars 20 forks source link

Ref conversions #10

Closed jmbldwn closed 6 years ago

jmbldwn commented 6 years ago

I'm trying to extract some data from a CGSize struct I got from a call to CVImageBufferGetDisplaySize.

I appear to be successfully calling the function that gives me the size for my image, using ffi:

const c = new ffi.Library(null, {
    CMSampleBufferGetImageBuffer: ['pointer', ['pointer']],
    CVImageBufferGetDisplaySize: ['pointer', ['pointer']]
});

The call to CVImageBufferGetDisplaySize returns a pointer to a CGSize struct. I want to convert that struct to a javascript object, but it's not clear how best to do that.

I tried adding the function NSStringFromCGSize to my ffi definition. It's a call that converts CGSize to a string representation, whereupon I could do some js mojo to get the values out of it.

But ffi can't find NSStringFromCGSize.

Not that this is the best way to do the conversion, but it seems like that should work.

So, really two questions:

  1. How can i access global functions like NSStringFromCGSize from frameworks like UIKit in javascript?
  2. Is there a better way to go about converting references to structs to javascript?
lukaskollmer commented 6 years ago
  1. NSStringFromCGSize is defined in UIKit and therefore iOS-only. On macOS, you should have a look at NSStringFromSize instead.
  2. Yes. You can use the ref-struct module to "emulate" structs as javascript objects. Example using CGSize:
    
    const objc = require('objc');
    const ffi = require('ffi');
    const Struct = require('ref-struct');

const CGSize = Struct({ width : 'double', height: 'double' });

const c = new ffi.Library(null, { NSStringFromSize: ['pointer', [CGSize]] });

let size = new CGSize(); size.width = 1.23; size.height = 2.46;

console.log(objc.wrap(c.NSStringFromSize(size))); // -> '[objc.InstanceProxy {1.23, 2.46}]'

jmbldwn commented 6 years ago

Handy. Thanks.