Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,6 @@ v8_build
/project-template-vision/.build_env_vars.sh
/project-template-vision/__PROJECT_NAME__.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist

.cache/
.cache/

SwiftBindgen
5 changes: 3 additions & 2 deletions NativeScript/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ elseif(TARGET_PLATFORM STREQUAL "ios-sim")
set(TARGET_PLATFORM_IOS TRUE)
set(TARGET_PLATFORM_SIM TRUE)
set(SDK_NAME "iphonesimulator")
set(CMAKE_OSX_ARCHITECTURES "arm64;x86_64")
set(TARGET_PLATFORM_SPEC "ios-arm64_x86_64-simulator")
set(CMAKE_OSX_ARCHITECTURES "arm64")
set(TARGET_PLATFORM_SPEC "ios-arm64-simulator")

elseif(TARGET_PLATFORM STREQUAL "macos")
set(CMAKE_XCODE_ATTRIBUTE_MACOSX_DEPLOYMENT_TARGET "13.3")
Expand Down Expand Up @@ -236,6 +236,7 @@ if(BUILD_CLI_BINARY)
set(SOURCE_FILES ${SOURCE_FILES}
cli/main.cpp
cli/segappend.cpp
cli/BundleLoader.mm
)
endif()

Expand Down
1 change: 1 addition & 0 deletions NativeScript/NativeScript.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ __attribute__((visibility("default")))
@property BOOL LogToSystemConsole;
@property int ArgumentsCount;
@property(nonatomic) char** Arguments;
@property(nonatomic) void (*CustomLogCallback)(const char* message);

@end

Expand Down
10 changes: 10 additions & 0 deletions NativeScript/cli/BundleLoader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef BUNDLE_LOADER_H
#define BUNDLE_LOADER_H
#ifdef __APPLE__

#include <string>

std::string resolveMainPath();

#endif // __APPLE__
#endif // BUNDLE_LOADER_H
39 changes: 39 additions & 0 deletions NativeScript/cli/BundleLoader.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include "BundleLoader.h"
#include <Foundation/Foundation.h>

// Check if Resources/app/ exists, then load package.json["main"] || app/index.js full file path

std::string resolveMainPath() {
NSFileManager* fileManager = [NSFileManager defaultManager];
NSString* resourcesPath = [[NSBundle mainBundle] resourcePath];
NSString* appPath = [resourcesPath stringByAppendingPathComponent:@"app"];
BOOL isDir;

if ([fileManager fileExistsAtPath:appPath isDirectory:&isDir] && isDir) {
NSString* packageJsonPath = [appPath stringByAppendingPathComponent:@"package.json"];
if ([fileManager fileExistsAtPath:packageJsonPath]) {
NSData* jsonData = [NSData dataWithContentsOfFile:packageJsonPath];
NSError* error;
NSDictionary* packageDict = [NSJSONSerialization JSONObjectWithData:jsonData
options:0
error:&error];
if (error == nil) {
NSString* mainEntry = packageDict[@"main"];
if (mainEntry != nil) {
NSString* mainPath = [appPath stringByAppendingPathComponent:mainEntry];
if ([fileManager fileExistsAtPath:mainPath]) {
return std::string([mainPath UTF8String]);
}
}
}
}

// Fallback to app/index.js
NSString* indexPath = [appPath stringByAppendingPathComponent:@"index.js"];
if ([fileManager fileExistsAtPath:indexPath]) {
return std::string([indexPath UTF8String]);
}
}

return "";
}
9 changes: 9 additions & 0 deletions NativeScript/cli/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>

#include "ffi/NativeScriptException.h"
#include "runtime/Bundle.h"
#include "runtime/Runtime.h"
#include "runtime/RuntimeConfig.h"
#include "segappend.h"
#include "ffi/Tasks.h"
#include "BundleLoader.h"

using namespace nativescript;

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

bootFromBytecode(cwd, segmentData, bytecode_size);
} else {
std::string mainPath = resolveMainPath();

if (!mainPath.empty()) {
bootFromModuleSpec(cwd, mainPath);
return 0;
}

if (argc < 3) {
std::cout << "Usage: " << argv[0] << " run <js file>" << std::endl;
return 1;
Expand Down
1 change: 1 addition & 0 deletions NativeScript/ffi/Class.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ void initFastEnumeratorIteratorFactory(napi_env env,
NAPI_FUNCTION(registerClass);
NAPI_FUNCTION(import);
NAPI_FUNCTION(classGetter);
NAPI_FUNCTION(BridgedConstructor);

class ObjCBridgeState;

Expand Down
54 changes: 25 additions & 29 deletions NativeScript/ffi/ClassBuilder.mm
Original file line number Diff line number Diff line change
Expand Up @@ -264,80 +264,76 @@
size_t argc = 2;
napi_value args[2];
napi_value thisArg;

napi_get_cb_info(env, info, &argc, args, &thisArg, nullptr);

if (argc < 1) {
napi_throw_error(env, nullptr, "extend() requires at least one parameter with method definitions");
napi_throw_error(env, nullptr,
"extend() requires at least one parameter with method definitions");
return nullptr;
}

// Validate that the first argument is an object
napi_valuetype argType;
napi_typeof(env, args[0], &argType);
if (argType != napi_object) {
napi_throw_error(env, nullptr, "extend() first parameter must be an object");
return nullptr;
}

// Get the native class from 'this' (the constructor function)
Class baseNativeClass = nullptr;
napi_unwrap(env, thisArg, (void**)&baseNativeClass);

if (baseNativeClass == nullptr) {
napi_throw_error(env, nullptr, "extend() can only be called on native class constructors");
return nullptr;
}

// Create a unique class name
napi_value baseClassName;
napi_get_named_property(env, thisArg, "name", &baseClassName);
static char baseClassNameBuf[512];
napi_get_value_string_utf8(env, baseClassName, baseClassNameBuf, 512, nullptr);

std::string newClassName = baseClassNameBuf;
newClassName += "_Extended_";
newClassName += std::to_string(rand());

// Create the new constructor function that extends the base
napi_value newConstructor;
napi_define_class(env, newClassName.c_str(), newClassName.length(),
[](napi_env env, napi_callback_info info) -> napi_value {
// Constructor implementation - delegate to base class
napi_value thisArg;
napi_get_cb_info(env, info, nullptr, nullptr, &thisArg, nullptr);
return thisArg;
}, nullptr, 0, nullptr, &newConstructor);

napi_define_class(env, newClassName.c_str(), newClassName.length(), JS_BridgedConstructor,
nullptr, 0, nullptr, &newConstructor);

// Set up JavaScript inheritance from the base class
napi_inherits(env, newConstructor, thisArg);

// Get prototype for adding methods
napi_value newPrototype;
napi_get_named_property(env, newConstructor, "prototype", &newPrototype);

// Add methods from the first parameter to the prototype
napi_value methodNames;
napi_get_all_property_names(env, args[0], napi_key_own_only, napi_key_skip_symbols,
napi_key_numbers_to_strings, &methodNames);

uint32_t methodCount = 0;
napi_get_array_length(env, methodNames, &methodCount);

for (uint32_t i = 0; i < methodCount; i++) {
napi_value methodName, methodFunc;
napi_get_element(env, methodNames, i, &methodName);

static char methodNameBuf[512];
napi_get_value_string_utf8(env, methodName, methodNameBuf, 512, nullptr);
std::string name = methodNameBuf;

napi_get_named_property(env, args[0], name.c_str(), &methodFunc);

// Add the method to the prototype
napi_set_named_property(env, newPrototype, name.c_str(), methodFunc);
}

// Handle optional second parameter for protocols
if (argc >= 2) {
napi_valuetype secondArgType;
Expand All @@ -346,22 +342,22 @@
napi_value protocols;
bool hasProtocols = false;
napi_has_named_property(env, args[1], "protocols", &hasProtocols);

if (hasProtocols) {
napi_get_named_property(env, args[1], "protocols", &protocols);
napi_set_named_property(env, newConstructor, "ObjCProtocols", protocols);
}
}
}

// Use ClassBuilder to create the native class and bridge the methods
ClassBuilder* builder = new ClassBuilder(env, newConstructor);
builder->build();

// Register the builder in the bridge state
auto bridgeState = ObjCBridgeState::InstanceData(env);
bridgeState->classesByPointer[builder->nativeClass] = builder;

return newConstructor;
}

Expand Down
2 changes: 1 addition & 1 deletion NativeScript/ffi/ClassMember.mm
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ inline id assertSelf(napi_env env, napi_value jsThis) {
}
}

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

if (!objcNativeCall(env, cif, self, avalues, rvalue)) {
return nullptr;
Expand Down
2 changes: 1 addition & 1 deletion NativeScript/ffi/Closure.mm
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ void JSBlockCallback(ffi_cif* cif, void* ret, void* args[], void* data) {

Closure::~Closure() {
if (func != nullptr) {
napi_delete_reference(env, func);
napi_reference_unref(env, func, nullptr);
}
#ifndef ENABLE_JS_RUNTIME
if (tsfn != nullptr) {
Expand Down
19 changes: 10 additions & 9 deletions NativeScript/ffi/Interop.mm
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ void registerInterop(napi_env env, napi_value global) {

napi_value jsHelpers;
napi_create_string_utf8(env, jsHelpersSource, NAPI_AUTO_LENGTH, &jsHelpers);
napi_run_script(env, jsHelpers, nullptr);
napi_value result;
napi_run_script(env, jsHelpers, &result);

napi_value interop;
napi_create_object(env, &interop);
Expand Down Expand Up @@ -351,15 +352,15 @@ napi_value interop_stringFromCString(napi_env env, napi_callback_info info) {
return nullptr;
}

void *data = nullptr;
void* data = nullptr;

if (Pointer::isInstance(env, arg)) {
Pointer *ptr = Pointer::unwrap(env, arg);
Pointer* ptr = Pointer::unwrap(env, arg);
data = ptr->data;
}

if (Reference::isInstance(env, arg)) {
Reference *ref = Reference::unwrap(env, arg);
Reference* ref = Reference::unwrap(env, arg);
data = ref->data;
}

Expand All @@ -368,9 +369,9 @@ napi_value interop_stringFromCString(napi_env env, napi_callback_info info) {
if (data == nullptr) {
napi_get_null(env, &result);
} else {
napi_create_string_utf8(env, (const char *)data, NAPI_AUTO_LENGTH, &result);
napi_create_string_utf8(env, (const char*)data, NAPI_AUTO_LENGTH, &result);
}

return result;
}

Expand Down Expand Up @@ -600,11 +601,11 @@ napi_value interop_bufferFromData(napi_env env, napi_callback_info info) {
napi_value jsPointer = get_ref_value(env, bridgeState->pointerClass);
napi_value result;
napi_status status = napi_new_instance(env, jsPointer, 0, nullptr, &result);

if (status != napi_ok) {
return nullptr;
}

Pointer* ptr = Pointer::unwrap(env, result);
if (ptr == nullptr) {
return nullptr;
Expand Down Expand Up @@ -934,7 +935,7 @@ napi_value interop_bufferFromData(napi_env env, napi_callback_info info) {
return jsThis;
}

FunctionReference::~FunctionReference() { napi_delete_reference(env, ref); }
FunctionReference::~FunctionReference() {}

void* FunctionReference::getFunctionPointer(MDSectionOffset offset) {
if (closure == nullptr) {
Expand Down
16 changes: 13 additions & 3 deletions NativeScript/ffi/ObjCBridge.mm
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,21 @@ void finalize_bridge_data(napi_env env, void* data, void* hint) {

self_dl = dlopen(nullptr, RTLD_NOW);

if (metadata_ptr) {
if (metadata_ptr && *((const char*)metadata_ptr) != '\0') {
#ifdef EMBED_METADATA_SIZE
NSLog(@"Ignoring metadata pointer due to embedded metadata");
metadata = new MDMetadataReader((void*)embedded_metadata);
#else
NSLog(@"Using metadata from pointer: %p", metadata_ptr);
metadata = new MDMetadataReader((void*)metadata_ptr);
#endif
} else {
#ifdef EMBED_METADATA_SIZE
if (metadata_path != nullptr) {
NSLog(@"Loading metadata from file: %s", metadata_path);
metadata = loadMetadataFromFile(metadata_path);
} else {
NSLog(@"Using embedded metadata");
metadata = new MDMetadataReader((void*)embedded_metadata);
}
#else
Expand All @@ -85,7 +93,7 @@ void finalize_bridge_data(napi_env env, void* data, void* hint) {
#endif
}

objc_autoreleasePool = objc_autoreleasePoolPush();
// objc_autoreleasePool = objc_autoreleasePoolPush();
}

ObjCBridgeState::~ObjCBridgeState() {
Expand Down Expand Up @@ -134,7 +142,9 @@ void finalize_bridge_data(napi_env env, void* data, void* hint) {
}
cFunctionCache.clear();

objc_autoreleasePoolPop(objc_autoreleasePool);
// if (objc_autoreleasePool != nullptr)
// objc_autoreleasePoolPop(objc_autoreleasePool);

delete metadata;
dlclose(self_dl);
}
Expand Down
Loading
Loading