Skip to content

Commit 362403e

Browse files
committed
add back cli build, add URL module, refactor runtime modules
1 parent a5e84a0 commit 362403e

Some content is hidden

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

84 files changed

+6031
-971
lines changed

NativeScript/CMakeLists.txt

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${COMMON_FLAGS}")
2121
set(TARGET_PLATFORM "macos" CACHE STRING "Target platform for the Objective-C bridge")
2222
set(TARGET_ENGINE "v8" CACHE STRING "Target JS engine for the NativeScript runtime")
2323
set(METADATA_SIZE 0 CACHE STRING "Size of embedded metadata in bytes")
24+
set(BUILD_CLI_BINARY OFF CACHE BOOL "Build the NativeScript CLI binary")
2425

2526
if(TARGET_PLATFORM STREQUAL "ios")
2627
set(CMAKE_XCODE_ATTRIBUTE_IPHONEOS_DEPLOYMENT_TARGET "13.0")
@@ -70,8 +71,8 @@ elseif(TARGET_ENGINE STREQUAL "hermes")
7071
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++ -std=c++20 -DTARGET_ENGINE_HERMES")
7172
elseif(TARGET_ENGINE STREQUAL "v8")
7273
set(TARGET_ENGINE_V8 TRUE)
73-
add_link_options("-fuse-ld=/opt/homebrew/opt/llvm/bin/ld64.lld")
74-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-rtti -stdlib=libc++ -std=c++20 -DTARGET_ENGINE_V8")
74+
add_link_options("-fuse-ld=lld")
75+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-rtti -stdlib=libc++ -std=c++20 -DTARGET_ENGINE_V8 -DV8_COMPRESS_POINTERS -DV8_ENABLE_SANDBOX")
7576
elseif(TARGET_ENGINE STREQUAL "quickjs")
7677
set(TARGET_ENGINE_QUICKJS TRUE)
7778
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++ -std=c++20 -DTARGET_ENGINE_QUICKJS")
@@ -100,7 +101,6 @@ message(STATUS "ENABLE_JS_RUNTIME = ${ENABLE_JS_RUNTIME}")
100101
include_directories(
101102
./
102103
../metadata-generator/include
103-
ada
104104
napi/common
105105
libffi/${LIBFFI_BUILD}/include
106106
)
@@ -126,20 +126,24 @@ set(SOURCE_FILES
126126
ffi/Interop.mm
127127
ffi/InlineFunctions.mm
128128
ffi/ClassBuilder.mm
129+
ffi/NativeScriptException.mm
129130
)
130131

131132
if(ENABLE_JS_RUNTIME)
132133
set(SOURCE_FILES
133134
${SOURCE_FILES}
134-
runtime/Console.cpp
135+
runtime/modules/console/Console.cpp
135136
runtime/Runtime.cpp
136-
runtime/Require.cpp
137-
runtime/Performance.cpp
137+
runtime/modules/module/ModuleInternal.cpp
138+
runtime/modules/performance/Performance.cpp
138139
runtime/Bundle.mm
139-
runtime/Timers.mm
140-
runtime/App.mm
140+
runtime/modules/timers/Timers.mm
141+
runtime/modules/app/App.mm
141142
runtime/NativeScript.mm
142143
runtime/RuntimeConfig.cpp
144+
runtime/modules/url/ada/ada.cpp
145+
runtime/modules/url/URL.cpp
146+
runtime/modules/url/URLSearchParams.cpp
143147
)
144148

145149
if(TARGET_ENGINE_V8)
@@ -157,8 +161,6 @@ if(ENABLE_JS_RUNTIME)
157161
)
158162

159163
elseif(TARGET_ENGINE_HERMES)
160-
message(STATUS "Hermes engine detected")
161-
162164
include_directories(
163165
napi/hermes
164166
napi/hermes/include/hermes
@@ -219,6 +221,13 @@ else()
219221
)
220222
endif()
221223

224+
if(BUILD_CLI_BINARY)
225+
set(SOURCE_FILES ${SOURCE_FILES}
226+
cli/main.cpp
227+
cli/segappend.cpp
228+
)
229+
endif()
230+
222231
# Find SDK
223232

224233
find_program(XCODEBUILD_EXECUTABLE xcodebuild)
@@ -247,11 +256,15 @@ message(STATUS "SDK = ${CMAKE_OSX_SYSROOT}")
247256

248257
# Build targets
249258

250-
add_library(
251-
${NAME}
252-
SHARED
253-
${SOURCE_FILES}
254-
)
259+
if(BUILD_CLI_BINARY)
260+
add_executable(${NAME} ${SOURCE_FILES})
261+
else()
262+
add_library(
263+
${NAME}
264+
SHARED
265+
${SOURCE_FILES}
266+
)
267+
endif()
255268

256269
target_sources(
257270
${NAME}

NativeScript/NativeScript.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ extern "C"
88
#endif // __cplusplus
99

1010
void
11-
objc_bridge_init(void* env, const char* metadata_path, const void* metadata_ptr);
11+
nativescript_init(void* env, const char* metadata_path, const void* metadata_ptr);
1212

1313
#ifdef __OBJC__
1414

NativeScript/runtime/main.cpp renamed to NativeScript/cli/main.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1+
#include "ffi/NativeScriptException.h"
12
#ifdef ENABLE_JS_RUNTIME
23

34
#include <filesystem>
45
#include <fstream>
56
#include <iostream>
67

7-
#include "Bundle.h"
8-
#include "Runtime.h"
9-
#include "RuntimeConfig.h"
8+
#include "runtime/Bundle.h"
9+
#include "runtime/Runtime.h"
10+
#include "runtime/RuntimeConfig.h"
1011
#include "segappend.h"
1112

1213
using namespace nativescript;
@@ -27,12 +28,19 @@ void bootFromModuleSpec(std::string baseDir, std::string spec) {
2728

2829
auto runtime = Runtime();
2930

30-
runtime.RunModule(spec);
31+
try {
32+
runtime.RunModule(spec);
33+
} catch (const nativescript::NativeScriptException& e) {
34+
std::cerr << "Error running module: " << e.Description() << std::endl;
35+
return;
36+
}
3137

3238
runtime.RunLoop();
3339
}
3440

3541
int main(int argc, char** argv) {
42+
RuntimeConfig.LogToSystemConsole = true;
43+
3644
#ifdef __APPLE__
3745
std::string bytecodePath = getBytecodePathFromBundle();
3846
if (!bytecodePath.empty()) {
File renamed without changes.
File renamed without changes.

NativeScript/ffi/AutoreleasePool.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
#include "node_api_util.h"
55

66
extern "C" {
7-
void *objc_autoreleasePoolPush(void);
8-
void objc_autoreleasePoolPop(void *pool);
7+
void* objc_autoreleasePoolPush(void);
8+
void objc_autoreleasePoolPop(void* pool);
99
}
1010

11-
namespace objc_bridge {
11+
namespace nativescript {
1212

1313
NAPI_FUNCTION(autoreleasepool);
1414

15-
} // namespace objc_bridge
15+
} // namespace nativescript
1616

17-
#endif // AUTORELEASEPOOL_H
17+
#endif // AUTORELEASEPOOL_H

NativeScript/ffi/AutoreleasePool.mm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "AutoreleasePool.h"
22

3-
namespace objc_bridge {
3+
namespace nativescript {
44

55
napi_value JS_autoreleasepool(napi_env env, napi_callback_info info) {
66
napi_value callback;
@@ -15,4 +15,4 @@ napi_value JS_autoreleasepool(napi_env env, napi_callback_info info) {
1515
return result;
1616
}
1717

18-
} // namespace objc_bridge
18+
} // namespace nativescript

NativeScript/ffi/Block.h

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,32 @@
11
#ifndef BLOCK_H
22
#define BLOCK_H
33

4+
#include <cstdlib>
5+
46
#include "Cif.h"
57
#include "Closure.h"
68
#include "node_api_util.h"
7-
#include <cstdlib>
89

9-
namespace objc_bridge {
10+
namespace nativescript {
1011

1112
class FunctionPointer {
12-
public:
13-
void *function;
13+
public:
14+
void* function;
1415
metagen::MDSectionOffset offset;
15-
Cif *cif;
16+
Cif* cif;
1617

17-
static napi_value wrap(napi_env env, void *function, metagen::MDSectionOffset offset, bool isBlock);
18-
static void finalize(napi_env env, void *finalize_data, void *finalize_hint);
18+
static napi_value wrap(napi_env env, void* function,
19+
metagen::MDSectionOffset offset, bool isBlock);
20+
static void finalize(napi_env env, void* finalize_data, void* finalize_hint);
1921

2022
static napi_value jsCallAsCFunction(napi_env env, napi_callback_info cbinfo);
2123
static napi_value jsCallAsBlock(napi_env env, napi_callback_info cbinfo);
2224
};
2325

24-
id registerBlock(napi_env env, Closure *closure, napi_value callback);
26+
id registerBlock(napi_env env, Closure* closure, napi_value callback);
2527

2628
NAPI_FUNCTION(registerBlock);
2729

28-
} // namespace objc_bridge
30+
} // namespace nativescript
2931

3032
#endif /* BLOCK_H */

NativeScript/ffi/Block.mm

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#include "Block.h"
22
#import <Foundation/Foundation.h>
3-
#include "js_native_api_types.h"
43
#include "Interop.h"
54
#include "ObjCBridge.h"
65
#include "js_native_api.h"
6+
#include "js_native_api_types.h"
77
#include "node_api_util.h"
88
#include "objc/runtime.h"
99

@@ -24,7 +24,7 @@
2424
void* invoke;
2525
Block_descriptor_1* descriptor;
2626
// imported variables
27-
objc_bridge::Closure* closure;
27+
nativescript::Closure* closure;
2828
};
2929

3030
void block_copy(void* dest, void* src) {}
@@ -36,7 +36,7 @@ void block_finalize(napi_env env, void* data, void* hint) {
3636
delete block;
3737
}
3838

39-
namespace objc_bridge {
39+
namespace nativescript {
4040

4141
void* stackBlockISA = nullptr;
4242

@@ -63,22 +63,25 @@ id registerBlock(napi_env env, Closure* closure, napi_value callback) {
6363
// TODO: fix memory management of objc blocks here
6464
// napi_wrap(env, callback, block, block_finalize, nullptr, &ref);
6565
// if (ref == nullptr) {
66-
// Deno doesn't handle napi_wrap properly.
67-
ref = make_ref(env, callback, 1);
66+
// Deno doesn't handle napi_wrap properly.
67+
ref = make_ref(env, callback, 1);
6868
// } else {
6969
// uint32_t refCount;
7070
// napi_reference_ref(env, ref, &refCount);
7171
// }
7272
closure->func = ref;
7373

7474
auto bridgeState = ObjCBridgeState::InstanceData(env);
75+
76+
#ifndef ENABLE_JS_RUNTIME
7577
if (napiSupportsThreadsafeFunctions(bridgeState->self_dl)) {
7678
napi_value workName;
7779
napi_create_string_utf8(env, "Block", NAPI_AUTO_LENGTH, &workName);
7880
napi_create_threadsafe_function(env, callback, nullptr, workName, 0, 1, nullptr, nullptr,
7981
closure, Closure::callBlockFromMainThread, &closure->tsfn);
8082
if (closure->tsfn) napi_unref_threadsafe_function(env, closure->tsfn);
8183
}
84+
#endif // ENABLE_JS_RUNTIME
8285

8386
return (id)block;
8487
}
@@ -207,4 +210,4 @@ id registerBlock(napi_env env, Closure* closure, napi_value callback) {
207210
return cif->returnType->toJS(env, rvalue);
208211
}
209212

210-
} // namespace objc_bridge
213+
} // namespace nativescript

NativeScript/ffi/CFunction.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33

44
#include "Cif.h"
55

6-
namespace objc_bridge {
6+
namespace nativescript {
77

88
class CFunction {
9-
public:
9+
public:
1010
static napi_value jsCall(napi_env env, napi_callback_info cbinfo);
1111

12-
CFunction(void *fnptr) : fnptr(fnptr) {}
12+
CFunction(void* fnptr) : fnptr(fnptr) {}
1313
~CFunction();
1414

15-
void *fnptr;
16-
Cif *cif = nullptr;
15+
void* fnptr;
16+
Cif* cif = nullptr;
1717
};
1818

19-
} // namespace objc_bridge
19+
} // namespace nativescript
2020

2121
#endif /* C_FUNCTION_H */

0 commit comments

Comments
 (0)