Skip to content

Commit 2653700

Browse files
authored
fix: type error with NSTextView conforming to NSView, and improve macos sdk types performance in IDE by allowing importing individual frameworks (#23)
* improve TS emitter, fix error related to NSTextView * fix: type error with NSTextView conforming to NSView, and improve macos sdk types performance in IDE by allowing importing individual frameworks
1 parent 9e2a90c commit 2653700

File tree

473 files changed

+26543
-19783
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

473 files changed

+26543
-19783
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,6 @@ v8_build
5757
/project-template-vision/.build_env_vars.sh
5858
/project-template-vision/__PROJECT_NAME__.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist
5959

60-
.cache/
60+
.cache/
61+
62+
SwiftBindgen

NativeScript/CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ elseif(TARGET_PLATFORM STREQUAL "ios-sim")
4141
set(TARGET_PLATFORM_IOS TRUE)
4242
set(TARGET_PLATFORM_SIM TRUE)
4343
set(SDK_NAME "iphonesimulator")
44-
set(CMAKE_OSX_ARCHITECTURES "arm64;x86_64")
45-
set(TARGET_PLATFORM_SPEC "ios-arm64_x86_64-simulator")
44+
set(CMAKE_OSX_ARCHITECTURES "arm64")
45+
set(TARGET_PLATFORM_SPEC "ios-arm64-simulator")
4646

4747
elseif(TARGET_PLATFORM STREQUAL "macos")
4848
set(CMAKE_XCODE_ATTRIBUTE_MACOSX_DEPLOYMENT_TARGET "13.3")
@@ -236,6 +236,7 @@ if(BUILD_CLI_BINARY)
236236
set(SOURCE_FILES ${SOURCE_FILES}
237237
cli/main.cpp
238238
cli/segappend.cpp
239+
cli/BundleLoader.mm
239240
)
240241
endif()
241242

NativeScript/NativeScript.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ __attribute__((visibility("default")))
2424
@property BOOL LogToSystemConsole;
2525
@property int ArgumentsCount;
2626
@property(nonatomic) char** Arguments;
27+
@property(nonatomic) void (*CustomLogCallback)(const char* message);
2728

2829
@end
2930

NativeScript/cli/BundleLoader.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef BUNDLE_LOADER_H
2+
#define BUNDLE_LOADER_H
3+
#ifdef __APPLE__
4+
5+
#include <string>
6+
7+
std::string resolveMainPath();
8+
9+
#endif // __APPLE__
10+
#endif // BUNDLE_LOADER_H

NativeScript/cli/BundleLoader.mm

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include "BundleLoader.h"
2+
#include <Foundation/Foundation.h>
3+
4+
// Check if Resources/app/ exists, then load package.json["main"] || app/index.js full file path
5+
6+
std::string resolveMainPath() {
7+
NSFileManager* fileManager = [NSFileManager defaultManager];
8+
NSString* resourcesPath = [[NSBundle mainBundle] resourcePath];
9+
NSString* appPath = [resourcesPath stringByAppendingPathComponent:@"app"];
10+
BOOL isDir;
11+
12+
if ([fileManager fileExistsAtPath:appPath isDirectory:&isDir] && isDir) {
13+
NSString* packageJsonPath = [appPath stringByAppendingPathComponent:@"package.json"];
14+
if ([fileManager fileExistsAtPath:packageJsonPath]) {
15+
NSData* jsonData = [NSData dataWithContentsOfFile:packageJsonPath];
16+
NSError* error;
17+
NSDictionary* packageDict = [NSJSONSerialization JSONObjectWithData:jsonData
18+
options:0
19+
error:&error];
20+
if (error == nil) {
21+
NSString* mainEntry = packageDict[@"main"];
22+
if (mainEntry != nil) {
23+
NSString* mainPath = [appPath stringByAppendingPathComponent:mainEntry];
24+
if ([fileManager fileExistsAtPath:mainPath]) {
25+
return std::string([mainPath UTF8String]);
26+
}
27+
}
28+
}
29+
}
30+
31+
// Fallback to app/index.js
32+
NSString* indexPath = [appPath stringByAppendingPathComponent:@"index.js"];
33+
if ([fileManager fileExistsAtPath:indexPath]) {
34+
return std::string([indexPath UTF8String]);
35+
}
36+
}
37+
38+
return "";
39+
}

NativeScript/cli/main.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
#include <filesystem>
44
#include <fstream>
55
#include <iostream>
6+
#include <string>
67

78
#include "ffi/NativeScriptException.h"
89
#include "runtime/Bundle.h"
910
#include "runtime/Runtime.h"
1011
#include "runtime/RuntimeConfig.h"
1112
#include "segappend.h"
1213
#include "ffi/Tasks.h"
14+
#include "BundleLoader.h"
1315

1416
using namespace nativescript;
1517

@@ -89,6 +91,13 @@ int main(int argc, char** argv) {
8991

9092
bootFromBytecode(cwd, segmentData, bytecode_size);
9193
} else {
94+
std::string mainPath = resolveMainPath();
95+
96+
if (!mainPath.empty()) {
97+
bootFromModuleSpec(cwd, mainPath);
98+
return 0;
99+
}
100+
92101
if (argc < 3) {
93102
std::cout << "Usage: " << argv[0] << " run <js file>" << std::endl;
94103
return 1;

NativeScript/ffi/Class.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ void initFastEnumeratorIteratorFactory(napi_env env,
2222
NAPI_FUNCTION(registerClass);
2323
NAPI_FUNCTION(import);
2424
NAPI_FUNCTION(classGetter);
25+
NAPI_FUNCTION(BridgedConstructor);
2526

2627
class ObjCBridgeState;
2728

NativeScript/ffi/ClassBuilder.mm

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -264,80 +264,76 @@
264264
size_t argc = 2;
265265
napi_value args[2];
266266
napi_value thisArg;
267-
267+
268268
napi_get_cb_info(env, info, &argc, args, &thisArg, nullptr);
269-
269+
270270
if (argc < 1) {
271-
napi_throw_error(env, nullptr, "extend() requires at least one parameter with method definitions");
271+
napi_throw_error(env, nullptr,
272+
"extend() requires at least one parameter with method definitions");
272273
return nullptr;
273274
}
274-
275+
275276
// Validate that the first argument is an object
276277
napi_valuetype argType;
277278
napi_typeof(env, args[0], &argType);
278279
if (argType != napi_object) {
279280
napi_throw_error(env, nullptr, "extend() first parameter must be an object");
280281
return nullptr;
281282
}
282-
283+
283284
// Get the native class from 'this' (the constructor function)
284285
Class baseNativeClass = nullptr;
285286
napi_unwrap(env, thisArg, (void**)&baseNativeClass);
286-
287+
287288
if (baseNativeClass == nullptr) {
288289
napi_throw_error(env, nullptr, "extend() can only be called on native class constructors");
289290
return nullptr;
290291
}
291-
292+
292293
// Create a unique class name
293294
napi_value baseClassName;
294295
napi_get_named_property(env, thisArg, "name", &baseClassName);
295296
static char baseClassNameBuf[512];
296297
napi_get_value_string_utf8(env, baseClassName, baseClassNameBuf, 512, nullptr);
297-
298+
298299
std::string newClassName = baseClassNameBuf;
299300
newClassName += "_Extended_";
300301
newClassName += std::to_string(rand());
301-
302+
302303
// Create the new constructor function that extends the base
303304
napi_value newConstructor;
304-
napi_define_class(env, newClassName.c_str(), newClassName.length(),
305-
[](napi_env env, napi_callback_info info) -> napi_value {
306-
// Constructor implementation - delegate to base class
307-
napi_value thisArg;
308-
napi_get_cb_info(env, info, nullptr, nullptr, &thisArg, nullptr);
309-
return thisArg;
310-
}, nullptr, 0, nullptr, &newConstructor);
311-
305+
napi_define_class(env, newClassName.c_str(), newClassName.length(), JS_BridgedConstructor,
306+
nullptr, 0, nullptr, &newConstructor);
307+
312308
// Set up JavaScript inheritance from the base class
313309
napi_inherits(env, newConstructor, thisArg);
314-
310+
315311
// Get prototype for adding methods
316312
napi_value newPrototype;
317313
napi_get_named_property(env, newConstructor, "prototype", &newPrototype);
318-
314+
319315
// Add methods from the first parameter to the prototype
320316
napi_value methodNames;
321317
napi_get_all_property_names(env, args[0], napi_key_own_only, napi_key_skip_symbols,
322318
napi_key_numbers_to_strings, &methodNames);
323-
319+
324320
uint32_t methodCount = 0;
325321
napi_get_array_length(env, methodNames, &methodCount);
326-
322+
327323
for (uint32_t i = 0; i < methodCount; i++) {
328324
napi_value methodName, methodFunc;
329325
napi_get_element(env, methodNames, i, &methodName);
330-
326+
331327
static char methodNameBuf[512];
332328
napi_get_value_string_utf8(env, methodName, methodNameBuf, 512, nullptr);
333329
std::string name = methodNameBuf;
334-
330+
335331
napi_get_named_property(env, args[0], name.c_str(), &methodFunc);
336-
332+
337333
// Add the method to the prototype
338334
napi_set_named_property(env, newPrototype, name.c_str(), methodFunc);
339335
}
340-
336+
341337
// Handle optional second parameter for protocols
342338
if (argc >= 2) {
343339
napi_valuetype secondArgType;
@@ -346,22 +342,22 @@
346342
napi_value protocols;
347343
bool hasProtocols = false;
348344
napi_has_named_property(env, args[1], "protocols", &hasProtocols);
349-
345+
350346
if (hasProtocols) {
351347
napi_get_named_property(env, args[1], "protocols", &protocols);
352348
napi_set_named_property(env, newConstructor, "ObjCProtocols", protocols);
353349
}
354350
}
355351
}
356-
352+
357353
// Use ClassBuilder to create the native class and bridge the methods
358354
ClassBuilder* builder = new ClassBuilder(env, newConstructor);
359355
builder->build();
360-
356+
361357
// Register the builder in the bridge state
362358
auto bridgeState = ObjCBridgeState::InstanceData(env);
363359
bridgeState->classesByPointer[builder->nativeClass] = builder;
364-
360+
365361
return newConstructor;
366362
}
367363

NativeScript/ffi/ClassMember.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ inline id assertSelf(napi_env env, napi_value jsThis) {
478478
}
479479
}
480480

481-
// NSLog(@"objcNativeCall: %p, %@", self, NSStringFromSelector(method->methodOrGetter.selector));
481+
NSLog(@"objcNativeCall: %p, %@", self, NSStringFromSelector(method->methodOrGetter.selector));
482482

483483
if (!objcNativeCall(env, cif, self, avalues, rvalue)) {
484484
return nullptr;

NativeScript/ffi/Closure.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ void JSBlockCallback(ffi_cif* cif, void* ret, void* args[], void* data) {
260260

261261
Closure::~Closure() {
262262
if (func != nullptr) {
263-
napi_delete_reference(env, func);
263+
napi_reference_unref(env, func, nullptr);
264264
}
265265
#ifndef ENABLE_JS_RUNTIME
266266
if (tsfn != nullptr) {

0 commit comments

Comments
 (0)