|
1 | 1 | #include "Cif.h" |
| 2 | +#include <Foundation/Foundation.h> |
| 3 | +#include <iostream> |
| 4 | +#include <vector> |
| 5 | +#include "Metadata.h" |
2 | 6 | #include "MetadataReader.h" |
3 | 7 | #include "ObjCBridge.h" |
4 | 8 | #include "TypeConv.h" |
5 | 9 | #include "Util.h" |
6 | | -#include <Foundation/Foundation.h> |
7 | | -#include <iostream> |
8 | | -#include <vector> |
9 | 10 |
|
10 | 11 | namespace objc_bridge { |
11 | 12 |
|
12 | 13 | // Essentially, we cache libffi structures per unique method signature, |
13 | 14 | // this helps us avoid the overhead of creating them on the fly for each |
14 | 15 | // invocation. |
15 | | -Cif *ObjCBridgeState::getMethodCif(napi_env env, Method method) { |
| 16 | +Cif* ObjCBridgeState::getMethodCif(napi_env env, Method method) { |
16 | 17 | auto encoding = std::string(method_getTypeEncoding(method)); |
17 | 18 | auto find = this->cifs[encoding]; |
18 | 19 | if (find != nullptr) { |
|
25 | 26 | return cif; |
26 | 27 | } |
27 | 28 |
|
28 | | -Cif *ObjCBridgeState::getMethodCif(napi_env env, MDSectionOffset offset) { |
| 29 | +Cif* ObjCBridgeState::getMethodCif(napi_env env, MDSectionOffset offset) { |
29 | 30 | auto find = this->mdMethodSignatureCache[offset]; |
30 | 31 | if (find != nullptr) { |
31 | 32 | return find; |
|
37 | 38 | return cif; |
38 | 39 | } |
39 | 40 |
|
40 | | -Cif *ObjCBridgeState::getBlockCif(napi_env env, MDSectionOffset offset) { |
| 41 | +Cif* ObjCBridgeState::getBlockCif(napi_env env, MDSectionOffset offset) { |
41 | 42 | auto find = this->mdBlockSignatureCache[offset]; |
42 | 43 | if (find != nullptr) { |
43 | 44 | return find; |
|
49 | 50 | return cif; |
50 | 51 | } |
51 | 52 |
|
52 | | -Cif *ObjCBridgeState::getCFunctionCif(napi_env env, MDSectionOffset offset) { |
| 53 | +Cif* ObjCBridgeState::getCFunctionCif(napi_env env, MDSectionOffset offset) { |
53 | 54 | auto find = this->mdFunctionSignatureCache[offset]; |
54 | 55 | if (find != nullptr) { |
55 | 56 | return find; |
|
65 | 66 | auto signature = [NSMethodSignature signatureWithObjCTypes:encoding.c_str()]; |
66 | 67 | unsigned long numberOfArguments = signature.numberOfArguments; |
67 | 68 | this->argc = (int)numberOfArguments - 2; |
68 | | - this->argv = (napi_value *)malloc(sizeof(napi_value) * this->argc); |
| 69 | + this->argv = (napi_value*)malloc(sizeof(napi_value) * this->argc); |
69 | 70 |
|
70 | 71 | unsigned int argc = (unsigned int)numberOfArguments; |
71 | 72 |
|
72 | | - const char *returnType = signature.methodReturnType; |
| 73 | + const char* returnType = signature.methodReturnType; |
73 | 74 | this->returnType = TypeConv::Make(env, &returnType); |
74 | 75 |
|
75 | | - ffi_type *rtype = this->returnType->type; |
76 | | - ffi_type **atypes = (ffi_type **)malloc(sizeof(ffi_type *) * argc); |
| 76 | + ffi_type* rtype = this->returnType->type; |
| 77 | + ffi_type** atypes = (ffi_type**)malloc(sizeof(ffi_type*) * argc); |
77 | 78 |
|
78 | 79 | unsigned long methodReturnLength = signature.methodReturnLength; |
79 | 80 | unsigned long frameLength = signature.frameLength; |
|
82 | 83 | this->rvalueLength = methodReturnLength; |
83 | 84 | this->frameLength = frameLength; |
84 | 85 |
|
85 | | - this->avalues = (void **)malloc(sizeof(void *) * argc); |
86 | | - this->shouldFree = (bool *)malloc(sizeof(bool) * this->argc); |
| 86 | + this->avalues = (void**)malloc(sizeof(void*) * argc); |
| 87 | + this->shouldFree = (bool*)malloc(sizeof(bool) * this->argc); |
87 | 88 | memset(this->shouldFree, false, sizeof(bool) * this->argc); |
88 | 89 | this->shouldFreeAny = false; |
89 | 90 |
|
90 | 91 | for (int i = 0; i < numberOfArguments; i++) { |
91 | | - const char *argenc = [signature getArgumentTypeAtIndex:i]; |
| 92 | + const char* argenc = [signature getArgumentTypeAtIndex:i]; |
92 | 93 |
|
93 | 94 | auto argTypeInfo = TypeConv::Make(env, &argenc); |
94 | 95 | atypes[i] = argTypeInfo->type; |
|
107 | 108 | } |
108 | 109 |
|
109 | 110 | if (status != FFI_OK) { |
110 | | - std::cout << "Failed to prepare CIF, libffi returned error:" << status |
111 | | - << std::endl; |
| 111 | + std::cout << "Failed to prepare CIF, libffi returned error:" << status << std::endl; |
112 | 112 | return; |
113 | 113 | } |
114 | 114 | } |
115 | 115 |
|
116 | | -Cif::Cif(napi_env env, MDMetadataReader *reader, MDSectionOffset offset, |
117 | | - bool isMethod, bool isBlock) { |
| 116 | +Cif::Cif(napi_env env, MDMetadataReader* reader, MDSectionOffset offset, bool isMethod, |
| 117 | + bool isBlock) { |
118 | 118 | auto returnTypeKind = reader->getTypeKind(offset); |
119 | | - bool next = (returnTypeKind & mdTypeFlagNext) != 0; |
120 | | - isVariadic = (returnTypeKind & mdTypeFlagVariadic) != 0; |
| 119 | + bool next = ((MDTypeFlag)returnTypeKind & mdTypeFlagNext) != 0; |
| 120 | + isVariadic = ((MDTypeFlag)returnTypeKind & mdTypeFlagVariadic) != 0; |
121 | 121 |
|
122 | 122 | returnType = TypeConv::Make(env, reader, &offset); |
123 | 123 |
|
124 | | - ffi_type **atypes = nullptr; |
| 124 | + ffi_type** atypes = nullptr; |
125 | 125 |
|
126 | 126 | auto implicitArgs = isMethod ? 2 : isBlock ? 1 : 0; |
127 | 127 |
|
|
130 | 130 | if (next || isMethod || isBlock) { |
131 | 131 | while (next) { |
132 | 132 | auto argTypeKind = reader->getTypeKind(offset); |
133 | | - next = (argTypeKind & mdTypeFlagNext) != 0; |
| 133 | + next = ((MDTypeFlag)argTypeKind & mdTypeFlagNext) != 0; |
134 | 134 | auto argTypeInfo = TypeConv::Make(env, reader, &offset); |
135 | 135 | std::string enc; |
136 | 136 | argTypeInfo->encode(&enc); |
|
141 | 141 |
|
142 | 142 | auto totalArgc = argc + implicitArgs; |
143 | 143 |
|
144 | | - argv = (napi_value *)malloc(sizeof(napi_value) * argc); |
145 | | - shouldFree = (bool *)malloc(sizeof(bool) * argc); |
| 144 | + argv = (napi_value*)malloc(sizeof(napi_value) * argc); |
| 145 | + shouldFree = (bool*)malloc(sizeof(bool) * argc); |
146 | 146 |
|
147 | | - atypes = (ffi_type **)malloc(sizeof(ffi_type *) * totalArgc); |
148 | | - avalues = (void **)malloc(sizeof(void *) * argc); |
| 147 | + atypes = (ffi_type**)malloc(sizeof(ffi_type*) * totalArgc); |
| 148 | + avalues = (void**)malloc(sizeof(void*) * argc); |
149 | 149 |
|
150 | 150 | if (isMethod) { |
151 | 151 | atypes[0] = &ffi_type_pointer; |
|
167 | 167 | shouldFree = nullptr; |
168 | 168 | } |
169 | 169 |
|
170 | | - ffi_status status = ffi_prep_cif(&cif, FFI_DEFAULT_ABI, argc + implicitArgs, |
171 | | - returnType->type, atypes); |
| 170 | + ffi_status status = |
| 171 | + ffi_prep_cif(&cif, FFI_DEFAULT_ABI, argc + implicitArgs, returnType->type, atypes); |
172 | 172 |
|
173 | 173 | if (status != FFI_OK) { |
174 | | - std::cout << "Failed to prepare CIF, libffi returned error: " << status |
175 | | - << std::endl; |
| 174 | + std::cout << "Failed to prepare CIF, libffi returned error: " << status << std::endl; |
176 | 175 | return; |
177 | 176 | } |
178 | 177 |
|
|
184 | 183 | rvalueLength = cif.rtype->size; |
185 | 184 | } |
186 | 185 |
|
187 | | -} // namespace objc_bridge |
| 186 | +} // namespace objc_bridge |
0 commit comments