Skip to content

Commit e6f0e07

Browse files
committed
add qjs and jsc napi
1 parent e4d2ce4 commit e6f0e07

File tree

266 files changed

+171638
-63
lines changed

Some content is hidden

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

266 files changed

+171638
-63
lines changed

NativeScript/CMakeLists.txt

Lines changed: 85 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ set(NAME NativeScript)
88
set(VERSION 0.1.0)
99
set(BUNDLE_IDENTIFIER "org.nativescript.runtime")
1010

11-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fobjective-c")
11+
enable_language(OBJCXX)
1212

1313
set(CMAKE_CXX_STANDARD 20)
1414

@@ -30,6 +30,7 @@ if(TARGET_PLATFORM STREQUAL "ios")
3030
set(SDK_NAME "iphoneos")
3131
set(CMAKE_OSX_ARCHITECTURES "arm64")
3232
set(TARGET_PLATFORM_SPEC "ios-arm64")
33+
3334
elseif(TARGET_PLATFORM STREQUAL "ios-sim")
3435
set(CMAKE_XCODE_ATTRIBUTE_IPHONEOS_DEPLOYMENT_TARGET "13.0")
3536
set(CMAKE_XCODE_ATTRIBUTE_TARGETED_DEVICE_FAMILY "1,2")
@@ -41,6 +42,7 @@ elseif(TARGET_PLATFORM STREQUAL "ios-sim")
4142
set(SDK_NAME "iphonesimulator")
4243
set(CMAKE_OSX_ARCHITECTURES "arm64;x86_64")
4344
set(TARGET_PLATFORM_SPEC "ios-arm64_x86_64-simulator")
45+
4446
elseif(TARGET_PLATFORM STREQUAL "macos")
4547
set(CMAKE_XCODE_ATTRIBUTE_MACOSX_DEPLOYMENT_TARGET "13.0")
4648
set(CMAKE_OSX_DEPLOYMENT_TARGET "13.0")
@@ -49,31 +51,38 @@ elseif(TARGET_PLATFORM STREQUAL "macos")
4951
set(SDK_NAME "macosx")
5052
set(CMAKE_OSX_ARCHITECTURES "arm64;x86_64")
5153
set(TARGET_PLATFORM_SPEC "macos-arm64_x86_64")
54+
5255
else()
5356
message(FATAL_ERROR "Unknown target platform: ${TARGET_PLATFORM}")
5457
return()
5558
endif()
5659

5760
set(ENABLE_JS_RUNTIME TRUE)
5861

59-
if (TARGET_ENGINE STREQUAL "none")
62+
if(TARGET_ENGINE STREQUAL "none")
6063
unset(ENABLE_JS_RUNTIME)
6164
set(TARGET_ENGINE_NONE TRUE)
62-
elseif (TARGET_ENGINE STREQUAL "hermes")
65+
elseif(TARGET_ENGINE STREQUAL "hermes")
6366
set(TARGET_ENGINE_HERMES TRUE)
6467
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++ -std=c++20 -DTARGET_ENGINE_HERMES")
65-
elseif (TARGET_ENGINE STREQUAL "v8")
68+
elseif(TARGET_ENGINE STREQUAL "v8")
6669
set(TARGET_ENGINE_V8 TRUE)
6770
add_link_options("-fuse-ld=/opt/homebrew/opt/llvm/bin/ld64.lld")
6871
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-rtti -stdlib=libc++ -std=c++20 -DV8_COMPRESS_POINTERS -DV8_ENABLE_SANDBOX -DTARGET_ENGINE_V8")
72+
elseif(TARGET_ENGINE STREQUAL "quickjs")
73+
set(TARGET_ENGINE_QUICKJS TRUE)
74+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DTARGET_ENGINE_QUICKJS")
75+
elseif(TARGET_ENGINE STREQUAL "jsc")
76+
set(TARGET_ENGINE_JSC TRUE)
77+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DTARGET_ENGINE_JSC")
6978
else()
7079
message(FATAL_ERROR "Unknown target engine: ${TARGET_ENGINE}")
7180
return()
7281
endif()
7382

74-
if (ENABLE_JS_RUNTIME)
83+
if(ENABLE_JS_RUNTIME)
7584
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DENABLE_JS_RUNTIME")
76-
elseif (TARGET_PLATFORM_MACOS)
85+
elseif(TARGET_PLATFORM_MACOS)
7786
# If building a generic library for macOS, we'll build a dylib instead of a framework
7887
unset(BUILD_FRAMEWORK)
7988
set(GENERIC_NAPI TRUE)
@@ -120,24 +129,6 @@ include_directories(
120129
/Library/Developer/CommandLineTools/usr/include
121130
)
122131

123-
if (TARGET_ENGINE_V8)
124-
include_directories(
125-
napi/v8
126-
napi/v8/include
127-
napi/v8/v8_inspector
128-
)
129-
elseif(TARGET_ENGINE_HERMES)
130-
include_directories(
131-
napi/hermes
132-
napi/hermes/hermes
133-
napi/hermes/jsi
134-
)
135-
elseif(TARGET_ENGINE_NONE)
136-
include_directories(
137-
napi/generic
138-
)
139-
endif()
140-
141132
set(LIB_SOURCE_FILES
142133
ffi/AutoreleasePool.mm
143134
ffi/Protocol.mm
@@ -161,7 +152,7 @@ set(LIB_SOURCE_FILES
161152
ffi/ClassBuilder.mm
162153
)
163154

164-
if (ENABLE_JS_RUNTIME)
155+
if(ENABLE_JS_RUNTIME)
165156
set(LIB_SOURCE_FILES
166157
${LIB_SOURCE_FILES}
167158
runtime/Console.cpp
@@ -175,19 +166,72 @@ if (ENABLE_JS_RUNTIME)
175166
runtime/RuntimeConfig.cpp
176167
)
177168

178-
if (TARGET_ENGINE_V8)
169+
if(TARGET_ENGINE_V8)
170+
include_directories(
171+
napi/v8
172+
napi/v8/include
173+
napi/v8/v8_inspector
174+
)
175+
179176
set(LIB_SOURCE_FILES
180177
${LIB_SOURCE_FILES}
181178
napi/v8/v8-api.cpp
182179
napi/v8/jsr.cpp
183180
napi/v8/SimpleAllocator.cpp
184181
)
182+
185183
elseif(TARGET_ENGINE_HERMES)
184+
include_directories(
185+
napi/hermes
186+
napi/hermes/hermes
187+
napi/hermes/jsi
188+
)
189+
186190
set(LIB_SOURCE_FILES
187191
${LIB_SOURCE_FILES}
188192
napi/hermes/jsr.cpp
189193
)
194+
195+
elseif(TARGET_ENGINE_QUICKJS)
196+
add_subdirectory(${PROJECT_SOURCE_DIR}/napi/quickjs/mimalloc-dev mimalloc)
197+
198+
set(LIB_SOURCE_FILES ${LIB_SOURCE_FILES}
199+
# quickjs
200+
napi/quickjs/source/cutils.c
201+
napi/quickjs/source/libregexp.c
202+
napi/quickjs/source/libbf.c
203+
napi/quickjs/source/libunicode.c
204+
napi/quickjs/source/quickjs.c
205+
# napi
206+
napi/quickjs/quickjs-api.c
207+
napi/quickjs/jsr.cpp
208+
)
209+
210+
include_directories(
211+
napi/quickjs
212+
napi/quickjs/source
213+
napi/common
214+
# mimalloc
215+
napi/quickjs/mimalloc-dev/include
216+
)
217+
218+
elseif(TARGET_ENGINE_JSC)
219+
include_directories(
220+
napi/jsc
221+
napi/jsc/include
222+
napi/common
223+
)
224+
225+
set(LIB_SOURCE_FILES ${LIB_SOURCE_FILES}
226+
napi/jsc/jsc-api.cpp
227+
napi/jsc/jsr.cpp
228+
)
229+
190230
endif()
231+
else()
232+
include_directories(
233+
napi/generic
234+
)
191235
endif()
192236

193237
# Build targets
@@ -214,7 +258,7 @@ set_target_properties(${NAME} PROPERTIES
214258
PUBLIC_HEADER "NativeScript.h"
215259
)
216260

217-
if (BUILD_FRAMEWORK)
261+
if(BUILD_FRAMEWORK)
218262
message(STATUS "FRAMEWORK = TRUE")
219263
set_target_properties(${NAME} PROPERTIES
220264
FRAMEWORK TRUE
@@ -257,7 +301,7 @@ target_link_directories(
257301
${CMAKE_CURRENT_SOURCE_DIR}/libffi/${LIBFFI_BUILD}
258302
)
259303

260-
if (TARGET_ENGINE_HERMES)
304+
if(TARGET_ENGINE_HERMES)
261305
target_link_options(
262306
${NAME}
263307
PRIVATE
@@ -294,12 +338,20 @@ elseif(TARGET_PLATFORM_IOS)
294338
)
295339
endif()
296340

297-
# if (GENERIC_NAPI)
298-
target_link_options(
341+
if(TARGET_ENGINE_QUICKJS)
342+
target_link_libraries(
299343
${NAME}
300344
PRIVATE
301-
"-Wl"
302-
"-undefined"
303-
"dynamic_lookup"
345+
mimalloc-static
304346
)
347+
endif()
348+
349+
# if (GENERIC_NAPI)
350+
target_link_options(
351+
${NAME}
352+
PRIVATE
353+
"-Wl"
354+
"-undefined"
355+
"dynamic_lookup"
356+
)
305357
# endif()
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* Copyright (C) 2013, 2016 Apple Inc. All rights reserved.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions
6+
* are met:
7+
* 1. Redistributions of source code must retain the above copyright
8+
* notice, this list of conditions and the following disclaimer.
9+
* 2. Redistributions in binary form must reproduce the above copyright
10+
* notice, this list of conditions and the following disclaimer in the
11+
* documentation and/or other materials provided with the distribution.
12+
*
13+
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14+
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17+
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21+
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24+
*/
25+
26+
#ifndef APICallbackFunction_h
27+
#define APICallbackFunction_h
28+
29+
#include "APICast.h"
30+
#include "Error.h"
31+
#include "JSCallbackConstructor.h"
32+
#include "JSLock.h"
33+
#include <wtf/Vector.h>
34+
35+
namespace JSC {
36+
37+
struct APICallbackFunction {
38+
39+
template <typename T> static EncodedJSValue JSC_HOST_CALL call(ExecState*);
40+
template <typename T> static EncodedJSValue JSC_HOST_CALL construct(ExecState*);
41+
42+
};
43+
44+
template <typename T>
45+
EncodedJSValue JSC_HOST_CALL APICallbackFunction::call(ExecState* exec)
46+
{
47+
VM& vm = exec->vm();
48+
auto scope = DECLARE_THROW_SCOPE(vm);
49+
JSContextRef execRef = toRef(exec);
50+
JSObjectRef functionRef = toRef(exec->jsCallee());
51+
JSObjectRef thisObjRef = toRef(jsCast<JSObject*>(exec->thisValue().toThis(exec, NotStrictMode)));
52+
53+
int argumentCount = static_cast<int>(exec->argumentCount());
54+
Vector<JSValueRef, 16> arguments;
55+
arguments.reserveInitialCapacity(argumentCount);
56+
for (int i = 0; i < argumentCount; i++)
57+
arguments.uncheckedAppend(toRef(exec, exec->uncheckedArgument(i)));
58+
59+
JSValueRef exception = 0;
60+
JSValueRef result;
61+
{
62+
JSLock::DropAllLocks dropAllLocks(exec);
63+
result = jsCast<T*>(toJS(functionRef))->functionCallback()(execRef, functionRef, thisObjRef, argumentCount, arguments.data(), &exception);
64+
}
65+
if (exception)
66+
throwException(exec, scope, toJS(exec, exception));
67+
68+
// result must be a valid JSValue.
69+
if (!result)
70+
return JSValue::encode(jsUndefined());
71+
72+
return JSValue::encode(toJS(exec, result));
73+
}
74+
75+
template <typename T>
76+
EncodedJSValue JSC_HOST_CALL APICallbackFunction::construct(ExecState* exec)
77+
{
78+
VM& vm = exec->vm();
79+
auto scope = DECLARE_THROW_SCOPE(vm);
80+
JSObject* constructor = exec->jsCallee();
81+
JSContextRef ctx = toRef(exec);
82+
JSObjectRef constructorRef = toRef(constructor);
83+
84+
JSObjectCallAsConstructorCallback callback = jsCast<T*>(constructor)->constructCallback();
85+
if (callback) {
86+
size_t argumentCount = exec->argumentCount();
87+
Vector<JSValueRef, 16> arguments;
88+
arguments.reserveInitialCapacity(argumentCount);
89+
for (size_t i = 0; i < argumentCount; ++i)
90+
arguments.uncheckedAppend(toRef(exec, exec->uncheckedArgument(i)));
91+
92+
JSValueRef exception = 0;
93+
JSObjectRef result;
94+
{
95+
JSLock::DropAllLocks dropAllLocks(exec);
96+
result = callback(ctx, constructorRef, argumentCount, arguments.data(), &exception);
97+
}
98+
if (exception) {
99+
throwException(exec, scope, toJS(exec, exception));
100+
return JSValue::encode(toJS(exec, exception));
101+
}
102+
// result must be a valid JSValue.
103+
if (!result)
104+
return throwVMTypeError(exec, scope);
105+
return JSValue::encode(toJS(result));
106+
}
107+
108+
return JSValue::encode(toJS(JSObjectMake(ctx, jsCast<JSCallbackConstructor*>(constructor)->classRef(), 0)));
109+
}
110+
111+
} // namespace JSC
112+
113+
#endif // APICallbackFunction_h

0 commit comments

Comments
 (0)