Skip to content
This repository was archived by the owner on Feb 6, 2025. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/runtime.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
// You can store this to call your function. this must be bound to the current instance.
export function SuperCall(selector, argTypes, returnType) {
const func = CFunc("objc_msgSendSuper", [{type: '^' + objc_super_typeEncoding}, {type: ":"}, ...argTypes], returnType);
return function(...args) {
return function() {
const struct = make_objc_super(this, this.superclass());
const structPtr = MOPointer.alloc().initWithValue_(struct);
return func(structPtr, selector, ...args);
const params = ['structPtr', 'selector'].concat(
[].slice.apply(arguments).map((val, i) => `arguments[${i}]`)
)
return eval(`func(${params.join(', ')})`)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so this is supposed to yield a string like func(structPtr, selector, arguments[0], arguments[1], arguments[2]), right?

Copy link
Owner

@darknoon darknoon Sep 15, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would something like this work?

return func.apply(undefined, [structPtr, selector, ...arguments])

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay. That'd work in a normal JavaScript environment -- in fact that's pretty much how babel transforms the spread operator iirc. But it won't work in CocoaScript as the MOBridgeSupportFunction returned by CFunc doesn't support .apply() (apply is undefined).

The eval() I'm using here is really just a poor-mans .apply() to pass all the supplied arguments on to the MOBridgeSupportFunction invocation. There may be a better way to do it though?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kannonboy would it work to grab apply from somewhere else and then call it with a MOBridgeSupportFunction?. eval is a pretty bad hack from a perf and cleanliness perspective. Overall this library has some nasty hacks on the objc side, so I guess we can use it if all else fails.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@darknoon I agree using eval() it's pretty far from ideal, but I think it might be the best option available.

I tried to see if I could repurpose Function.prototype.apply() by messing around in Sketch's console, but it doesn't quite work as I'd hoped:

Function.prototype.apply.apply(
  function(arg) {
    log('called with ' + arg)
  },
  ['cat']
)

Result is:

called with undefined

And the implementation of .apply() is native of course (you can test with log(Function.prototype.apply.toString()))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Performance wise is slice bad for arguments and eval bad for variable lookup etc. See #8 for more + tested alternative solution :)

};
}

Expand Down