Skip to content

Commit 23e23dd

Browse files
committed
clean up and add ci
1 parent 15a5903 commit 23e23dd

15 files changed

Lines changed: 272 additions & 159 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
branches: [main]
8+
9+
jobs:
10+
lint:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Setup repo
14+
uses: actions/checkout@v2
15+
16+
- name: Setup Deno
17+
uses: denoland/setup-deno@main
18+
with:
19+
deno-version: "v1.x"
20+
21+
- name: Check Formatting
22+
run: deno fmt --check
23+
24+
- name: Lint
25+
run: deno lint
26+
27+
test-macos:
28+
runs-on: macos-latest
29+
steps:
30+
- name: Setup repo
31+
uses: actions/checkout@v2
32+
33+
- name: Setup Deno
34+
uses: denoland/setup-deno@main
35+
with:
36+
deno-version: "v1.x"
37+
38+
- name: Build libffi
39+
run: deno task build-libffi
40+
41+
- name: Build (macOS, universal)
42+
run: deno task build macos
43+
44+
- name: Test (macOS, Node.js)
45+
run: node examples/foundation.js
46+
47+
- name: Test (macOS, Deno)
48+
run: deno run -A examples/foundation.js
49+
50+
build-ios:
51+
runs-on: macos-latest
52+
steps:
53+
- name: Setup repo
54+
uses: actions/checkout@v2
55+
56+
- name: Setup Deno
57+
uses: denoland/setup-deno@main
58+
with:
59+
deno-version: "v1.x"
60+
61+
- name: Build libffi
62+
run: deno task build-libffi
63+
64+
- name: Build (iOS, universal)
65+
run: deno task build ios-universal

‎CMakeLists.txt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ set(LIB_SOURCE_FILES
9999
src/struct.mm
100100
src/object_ref.mm
101101
src/js_object.mm
102+
src/enum.mm
103+
src/variable.mm
102104
segappend/src/segappend.cc
103105
)
104106

‎deno.json‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@
2828
]
2929
},
3030

31+
"lint": {
32+
"exclude": [
33+
"build",
34+
"libffi",
35+
"node_modules",
36+
"metadata",
37+
".vscode",
38+
"segappend"
39+
]
40+
},
41+
3142
"imports": {
3243
"objc": "./mod.ts"
3344
}

‎include/class.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
#define CLASS_H
33

44
#include "node_api_util.h"
5-
#include <cstdlib>
65

76
namespace objc_bridge {
87

98
NAPI_FUNCTION(registerClass);
109
NAPI_FUNCTION(getClass);
1110
NAPI_FUNCTION(import);
11+
NAPI_FUNCTION(classGetter);
1212

13-
}
13+
} // namespace objc_bridge
1414

1515
#endif /* CLASS_H */

‎include/enum.h‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef ENUM_H
2+
#define ENUM_H
3+
4+
#include "node_api_util.h"
5+
6+
namespace objc_bridge {
7+
8+
NAPI_FUNCTION(enumGetter);
9+
10+
} // namespace objc_bridge
11+
12+
#endif /* ENUM_H */

‎include/method_cif.h‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010

1111
namespace objc_bridge {
1212

13-
ffi_type *getTypeForEncoding(const char **encoding);
14-
1513
class MethodCif {
1614
public:
1715
ffi_cif cif;
@@ -29,7 +27,6 @@ class MethodCif {
2927
bool shouldFreeAny;
3028
bool *shouldFree;
3129

32-
MethodCif();
3330
MethodCif(napi_env env, std::string typeEncoding);
3431
MethodCif(napi_env env, MDMetadataReader *reader, MDSectionOffset offset);
3532

‎include/struct.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
namespace objc_bridge {
1010

11+
napi_value JS_structGetter(napi_env env, napi_callback_info info);
12+
1113
typedef struct StructFieldInfo {
1214
char *name;
1315
uint16_t offset;

‎include/variable.h‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef VARIABLE_H
2+
#define VARIABLE_H
3+
4+
#include "node_api_util.h"
5+
6+
namespace objc_bridge {
7+
8+
NAPI_FUNCTION(variableGetter);
9+
10+
} // namespace objc_bridge
11+
12+
#endif /* VARIABLE_H */

‎src/bridge.mm‎

Lines changed: 7 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "autoreleasepool.h"
44
#include "block.h"
55
#include "class.h"
6+
#include "enum.h"
67
#include "js_native_api.h"
78
#include "js_native_api_types.h"
89
#include "native_call.h"
@@ -11,172 +12,27 @@
1112
#include "object_ref.h"
1213
#include "struct.h"
1314
#include "type_conv.h"
15+
#include "variable.h"
1416
#import <Foundation/Foundation.h>
1517

1618
using namespace objc_bridge;
1719

1820
void finalize_bridge_data(napi_env env, void *data, void *hint) {
19-
ObjCBridgeData *bridgeData = (ObjCBridgeData *)data;
21+
auto bridgeData = (ObjCBridgeData *)data;
2022
delete bridgeData;
2123
}
2224

23-
NAPI_FUNCTION(variableGetter) {
24-
void *data;
25-
napi_get_cb_info(env, cbinfo, nullptr, nullptr, nullptr, &data);
26-
MDSectionOffset offset = (MDSectionOffset)((size_t)data);
27-
MDSectionOffset originalOffset = offset;
28-
auto bridgeData = ObjCBridgeData::InstanceData(env);
29-
30-
auto cached = bridgeData->mdValueCache[offset];
31-
if (cached != nullptr) {
32-
return get_ref_value(env, cached);
33-
}
34-
35-
// name
36-
offset += sizeof(MDSectionOffset);
37-
// typeKind
38-
offset += sizeof(MDTypeKind);
39-
40-
auto evalKind = bridgeData->metadata->getVariableEvalKind(offset);
41-
offset += sizeof(MDVariableEvalKind);
42-
43-
napi_value result = nullptr;
44-
45-
switch (evalKind) {
46-
case mdEvalDouble: {
47-
auto value = bridgeData->metadata->getDouble(offset);
48-
napi_create_double(env, value, &result);
49-
break;
50-
}
51-
52-
case mdEvalInt64: {
53-
auto value = bridgeData->metadata->getInt64(offset);
54-
napi_create_int64(env, value, &result);
55-
break;
56-
}
57-
58-
case mdEvalString: {
59-
auto value = bridgeData->metadata->getString(offset);
60-
napi_create_string_utf8(env, value, NAPI_AUTO_LENGTH, &result);
61-
break;
62-
}
63-
64-
default:
65-
break;
66-
}
67-
68-
if (result != nullptr) {
69-
bridgeData->mdValueCache[originalOffset] = make_ref(env, result);
70-
}
71-
72-
return result;
73-
}
74-
75-
NAPI_FUNCTION(enumGetter) {
76-
void *data;
77-
napi_get_cb_info(env, cbinfo, nullptr, nullptr, nullptr, &data);
78-
MDSectionOffset offset = (MDSectionOffset)((size_t)data);
79-
MDSectionOffset originalOffset = offset;
80-
auto bridgeData = ObjCBridgeData::InstanceData(env);
81-
82-
auto cached = bridgeData->mdValueCache[offset];
83-
if (cached != nullptr) {
84-
return get_ref_value(env, cached);
85-
}
86-
87-
napi_value result;
88-
napi_create_object(env, &result);
89-
90-
// name
91-
offset += sizeof(MDSectionOffset);
92-
93-
bool next = true;
94-
while (next) {
95-
auto nameOffset = bridgeData->metadata->getOffset(offset);
96-
next = (nameOffset & mdSectionOffsetNext) != 0;
97-
nameOffset &= ~mdSectionOffsetNext;
98-
auto name = bridgeData->metadata->resolveString(nameOffset);
99-
offset += sizeof(MDSectionOffset);
100-
int64_t value = bridgeData->metadata->getEnumValue(offset);
101-
offset += sizeof(int64_t);
102-
103-
napi_value member;
104-
napi_create_int64(env, value, &member);
105-
106-
napi_property_descriptor prop = {
107-
.utf8name = name,
108-
.attributes = napi_enumerable,
109-
.getter = nullptr,
110-
.setter = nullptr,
111-
.value = member,
112-
.data = nullptr,
113-
.method = nullptr,
114-
};
115-
116-
napi_define_properties(env, result, 1, &prop);
117-
}
118-
119-
bridgeData->mdValueCache[originalOffset] = make_ref(env, result);
120-
121-
return result;
122-
}
123-
124-
NAPI_FUNCTION(structGetter) {
125-
NAPI_PREAMBLE
126-
127-
void *data;
128-
napi_get_cb_info(env, cbinfo, nullptr, nullptr, nullptr, &data);
129-
MDSectionOffset offset = (MDSectionOffset)((size_t)data);
130-
131-
auto bridgeData = ObjCBridgeData::InstanceData(env);
132-
auto structInfo = bridgeData->getStructInfo(env, offset);
133-
return StructObject::getJSClass(env, structInfo);
134-
}
135-
136-
NAPI_FUNCTION(classGetter) {
137-
void *data;
138-
napi_get_cb_info(env, cbinfo, nullptr, nullptr, nullptr, &data);
139-
MDSectionOffset offset = (MDSectionOffset)((size_t)data);
140-
auto bridgeData = ObjCBridgeData::InstanceData(env);
141-
142-
auto cached = bridgeData->mdValueCache[offset];
143-
if (cached != nullptr) {
144-
return get_ref_value(env, cached);
145-
}
146-
147-
std::string name = bridgeData->metadata->getString(offset);
148-
auto cls = bridgeData->getBridgedClass(env, name);
149-
150-
if (cls != nullptr) {
151-
bridgeData->mdValueCache[offset] = cls->constructor;
152-
} else {
153-
return nullptr;
154-
}
155-
156-
return get_ref_value(env, cls->constructor);
157-
}
158-
15925
NAPI_EXPORT NAPI_MODULE_REGISTER {
160-
NAPI_PREAMBLE
161-
162-
ObjCBridgeData *bridgeData = new ObjCBridgeData();
163-
164-
NAPI_GUARD(napi_set_instance_data(env, (void *)bridgeData,
165-
finalize_bridge_data, nil)) {
166-
NAPI_THROW_LAST_ERROR
167-
return exports;
168-
}
26+
auto bridgeData = new ObjCBridgeData();
27+
napi_set_instance_data(env, (void *)bridgeData, finalize_bridge_data, nil);
16928

17029
const napi_property_descriptor properties[] = {
17130
NAPI_FUNCTION_DESC(getClass), NAPI_FUNCTION_DESC(registerClass),
17231
NAPI_FUNCTION_DESC(registerBlock), NAPI_FUNCTION_DESC(import),
17332
NAPI_FUNCTION_DESC(autoreleasepool),
17433
};
17534

176-
NAPI_GUARD(napi_define_properties(env, exports, 5, properties)) {
177-
NAPI_THROW_LAST_ERROR
178-
return exports;
179-
}
35+
napi_define_properties(env, exports, 5, properties);
18036

18137
napi_value global;
18238
napi_get_global(env, &global);
@@ -205,7 +61,7 @@ void finalize_bridge_data(napi_env env, void *data, void *hint) {
20561

20662
napi_define_properties(env, global, 1, &objectRefClass);
20763

208-
MDSectionOffset offset = bridgeData->metadata->structsOffset;
64+
auto offset = bridgeData->metadata->structsOffset;
20965
while (offset < bridgeData->metadata->size) {
21066
// Sometimes there is padding after file ends.
21167
if (bridgeData->metadata->getOffset(offset) == 0)

‎src/bridged_class.mm‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113
}
114114

115115
// TODO: Remove this function at some point
116+
// It was used to compare libffi overhead.
116117
NAPI_FUNCTION(lengthCustom) {
117118
napi_value jsThis;
118119
void *data;

0 commit comments

Comments
 (0)