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
2 changes: 2 additions & 0 deletions NativeScript/ffi/Cif.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class Cif {
Cif(napi_env env, std::string typeEncoding);
Cif(napi_env env, MDMetadataReader* reader, MDSectionOffset offset,
bool isMethod = false, bool isBlock = false);

~Cif();
};

} // namespace nativescript
Expand Down
15 changes: 15 additions & 0 deletions NativeScript/ffi/Cif.mm
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,19 @@
rvalueLength = cif.rtype->size;
}

Cif::~Cif() {
if (rvalue != nullptr) {
free(rvalue);
}
if (argv != nullptr) {
free(argv);
}
if (avalues != nullptr) {
free(avalues);
}
if (shouldFree != nullptr) {
free(shouldFree);
}
}

} // namespace nativescript
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
1 change: 1 addition & 0 deletions NativeScript/ffi/Closure.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Closure {
ffi_cif cif;
ffi_closure* closure;
void* fnptr;
ffi_type** atypes = nullptr; // Track malloc'd atypes array

std::shared_ptr<TypeConv> returnType;
std::vector<std::shared_ptr<TypeConv>> argTypes;
Expand Down
23 changes: 13 additions & 10 deletions NativeScript/ffi/Closure.mm
Original file line number Diff line number Diff line change
Expand Up @@ -177,20 +177,21 @@ void JSBlockCallback(ffi_cif* cif, void* ret, void* args[], void* data) {
int skipArgs = isBlock ? 1 : 0;

ffi_type* rtype = this->returnType->type;
ffi_type** atypes = (ffi_type**)malloc(sizeof(ffi_type*) * (argc + skipArgs));
this->atypes = (ffi_type**)malloc(sizeof(ffi_type*) * (argc + skipArgs));

if (isBlock) {
atypes[0] = &ffi_type_pointer;
this->atypes[0] = &ffi_type_pointer;
}

for (int i = 0; i < argc; i++) {
const char* argenc = [signature getArgumentTypeAtIndex:i];
auto argTypeInfo = TypeConv::Make(env, &argenc);
atypes[i + skipArgs] = argTypeInfo->type;
this->atypes[i + skipArgs] = argTypeInfo->type;
this->argTypes.push_back(argTypeInfo);
}

ffi_status status = ffi_prep_cif(&cif, FFI_DEFAULT_ABI, (int)argc + skipArgs, rtype, atypes);
ffi_status status =
ffi_prep_cif(&cif, FFI_DEFAULT_ABI, (int)argc + skipArgs, rtype, this->atypes);

if (status != FFI_OK) {
std::cout << "ffi_prep_cif failed" << std::endl;
Expand All @@ -215,7 +216,6 @@ void JSBlockCallback(ffi_cif* cif, void* ret, void* args[], void* data) {
if (encoding != nullptr) returnType->encode(encoding);

ffi_type* rtype = returnType->type;
ffi_type** atypes = nullptr;

if (isMethod && encoding != nullptr) {
const char* argenc = "@";
Expand All @@ -237,17 +237,17 @@ void JSBlockCallback(ffi_cif* cif, void* ret, void* args[], void* data) {
auto skipArgs = isBlock ? 1 : 0;

if (!argTypes.empty() || isBlock) {
atypes = (ffi_type**)malloc(sizeof(ffi_type*) * (argTypes.size() + skipArgs));
this->atypes = (ffi_type**)malloc(sizeof(ffi_type*) * (argTypes.size() + skipArgs));
if (isBlock) {
atypes[0] = &ffi_type_pointer;
this->atypes[0] = &ffi_type_pointer;
}
for (int i = 0; i < argTypes.size(); i++) {
atypes[i + skipArgs] = argTypes[i]->type;
this->atypes[i + skipArgs] = argTypes[i]->type;
}
}

ffi_status status =
ffi_prep_cif(&cif, FFI_DEFAULT_ABI, argTypes.size() + skipArgs, rtype, atypes);
ffi_prep_cif(&cif, FFI_DEFAULT_ABI, argTypes.size() + skipArgs, rtype, this->atypes);

if (status != FFI_OK) {
std::cout << "Failed to prepare CIF, libffi returned error:" << status << std::endl;
Expand All @@ -260,13 +260,16 @@ 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) {
napi_release_threadsafe_function(tsfn, napi_tsfn_abort);
}
#endif // ENABLE_JS_RUNTIME
if (atypes != nullptr) {
free(atypes);
}
ffi_closure_free(closure);
}

Expand Down
Loading
Loading