ccgus / CocoaScript

JavaScript + the Cocoa frameworks, and then ObjC brackets show up to party as well.
Other
618 stars 58 forks source link

fix Mocha_getProperty for macOS 10.13.2 #51

Closed mathieudutour closed 6 years ago

mathieudutour commented 7 years ago

macOS 10.13.2 has a new class called Function so we need to explicitly check for it, same way as we were checking for Object

ccgus commented 6 years ago

Somehow I completely missed this one.

Do you have any sample script which uses this? I'd like to test it, because I'm curious about the logic of the if statement. Shouldn't it be along the lines of: if (objCClass && !([propertyName isEqualToString:@"Object"] || [propertyName isEqualToString:@"Function"])) { ?

mathieudutour commented 6 years ago

!a && !b === !(a || b) so we are both right ;)

lodash uses Function in one of its methods:

function isNative(fn) {
  // Based on isNative() from Lodash
  var funcToString = Function.prototype.toString;
  var hasOwnProperty = Object.prototype.hasOwnProperty;
  var reIsNative = RegExp('^' + funcToString
  // Take an example native function source for comparison
  .call(hasOwnProperty
  // Strip regex characters so we can use it for regex
  ).replace(/[\\^$.*+?()[\]{}|]/g, '\\$&'
  // Remove hasOwnProperty from the template to make it generic
  ).replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g, '$1.*?') + '$');
  try {
    var source = funcToString.call(fn);
    return reIsNative.test(source);
  } catch (err) {
    return false;
  }
}
ccgus commented 6 years ago

Yea, you're right now that I stare at the code a bit more. For some reason it tripped something up in my brain from long ago. I'll get this merged.