JSPatch icon indicating copy to clipboard operation
JSPatch copied to clipboard

Is this implementation wrong with JPMethodSignature convert to ffi?

Open SatanWoo opened this issue 7 years ago • 2 comments

I guess the following code in the function ffiTypeWithEncodingChar of file JPMethodSignature.m seems to be wrong:

case '{':
        {
            NSString *typeStr = [NSString stringWithCString:c encoding:NSASCIIStringEncoding];
            NSUInteger end = [typeStr rangeOfString:@"}"].location;
            if (end != NSNotFound) {
                NSString *structName = [typeStr substringWithRange:NSMakeRange(1, end - 1)];
                ffi_type *type = malloc(sizeof(ffi_type));
                type->alignment = 0;
                type->size = 0;
                type->type = FFI_TYPE_STRUCT;
                NSDictionary *structDefine = [JPExtension registeredStruct][structName];
                NSUInteger subTypeCount = [structDefine[@"keys"] count];
                NSString *subTypes = structDefine[@"types"];
                ffi_type **sub_types = malloc(sizeof(ffi_type *) * (subTypeCount + 1));
                for (NSUInteger i=0; i<subTypeCount; i++) {
                    sub_types[i] = [self ffiTypeWithEncodingChar:[subTypes cStringUsingEncoding:NSASCIIStringEncoding]];
                    type->size += sub_types[i]->size;
                }
                sub_types[subTypeCount] = NULL;
                type->elements = sub_types;
                return type;
            }
        }

For example, If I parse {CGRect} as parameter and then the registeredStruct should return {CGPoint}{CGSize} as subTypes into following for-loop iteration.

However, [self ffiTypeWithEncodingChar:[subTypes cStringUsingEncoding:NSASCIIStringEncoding] seems to always parse {CGPoint} twice rather than {CGPoint} and {CGSize} respectively.

So am i right? Thanks for a lot for any help in advance

SatanWoo avatar Mar 28 '18 17:03 SatanWoo

require('JPEngine').defineStruct({
  "name": "CGRect",
  "types": "FFFF",
  "keys": ["x", "y", "width", "height"]
})

if parse {CGRect}, registeredStruct should return 'FFFF' as subTypes. CGRect type: "{CGRect={CGPoint=dd}{CGSize=dd}}" The struct name should be "CGRect", but now it's "{CGRect={CGPoint=dd", here is bug.

bang590 avatar Mar 29 '18 03:03 bang590

Ok, I checked out the jsCFunctionTest.js in your JSPatchDemo and found the definition is as below:

require('JPEngine').defineStruct({
    "name": "CGSize",
    "types": "FF",
    "keys": ["width", "height"]
});
require('JPEngine').defineStruct({
    "name": "CGPoint",
    "types": "FF",
    "keys": ["x", "y"]
});
require('JPEngine').defineStruct({
    "name": "CGRect",
    "types": "{CGPoint}{CGSize}",
    "keys": ["origin", "size"]
});

Anyway, thanks ~

SatanWoo avatar Mar 29 '18 03:03 SatanWoo