Skip to content
Open
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ FetchContent_Declare(arcana.cpp
GIT_REPOSITORY https://github.com/microsoft/arcana.cpp.git
GIT_TAG 1a8a5d6e95413ed14b38a6ac9419048f9a9c8009)
FetchContent_Declare(AndroidExtensions
GIT_REPOSITORY https://github.com/bghgary/AndroidExtensions.git
GIT_TAG 7d88a601fda9892791e7b4e994e375e049615688)
GIT_REPOSITORY https://github.com/matthargett/AndroidExtensions.git
GIT_TAG 4ffcc4ab149a02b9ee72622eee708b2753fd2093)
FetchContent_Declare(asio
GIT_REPOSITORY https://github.com/chriskohlhoff/asio.git
GIT_TAG f693a3eb7fe72a5f19b975289afc4f437d373d9c)
Expand Down Expand Up @@ -72,12 +72,49 @@ option(JSRUNTIMEHOST_POLYFILL_URL "Include JsRuntimeHost Polyfill URL and URLSea
option(JSRUNTIMEHOST_POLYFILL_ABORT_CONTROLLER "Include JsRuntimeHost Polyfills AbortController and AbortSignal." ON)
option(JSRUNTIMEHOST_POLYFILL_WEBSOCKET "Include JsRuntimeHost Polyfill WebSocket." ON)
option(JSRUNTIMEHOST_POLYFILL_BLOB "Include JsRuntimeHost Polyfill Blob." ON)
option(JSR_ENABLE_ASAN "Enable AddressSanitizer support." OFF)

if(JSR_ENABLE_ASAN)
message(STATUS "JSR_ENABLE_ASAN=ON (appending address sanitizer flags)")
set(JSR_ASAN_COMPILE_FLAGS "-fsanitize=address -fno-omit-frame-pointer")
foreach(var CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
if(NOT "${${var}}" MATCHES "-fsanitize=address")
set(${var} "${${var}} ${JSR_ASAN_COMPILE_FLAGS}")
endif()
set(${var} "${${var}}" CACHE STRING "" FORCE)
endforeach()

set(JSR_ASAN_LINK_FLAGS "-fsanitize=address")
foreach(var CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS)
if(NOT "${${var}}" MATCHES "-fsanitize=address")
set(${var} "${${var}} ${JSR_ASAN_LINK_FLAGS}")
endif()
set(${var} "${${var}}" CACHE STRING "" FORCE)
endforeach()
message(STATUS "ASan compile flags: C=${CMAKE_C_FLAGS} CXX=${CMAKE_CXX_FLAGS}")
message(STATUS "ASan link flags: EXE=${CMAKE_EXE_LINKER_FLAGS} SHARED=${CMAKE_SHARED_LINKER_FLAGS}")
endif()

# --------------------------------------------------

FetchContent_MakeAvailable_With_Message(arcana.cpp)
set_property(TARGET arcana PROPERTY FOLDER Dependencies)

if(ANDROID)
FetchContent_GetProperties(AndroidExtensions)
if(NOT AndroidExtensions_POPULATED)
FetchContent_Populate(AndroidExtensions)
FetchContent_GetProperties(AndroidExtensions)
message(STATUS "Patching AndroidExtensions Globals.cpp in ${androidextensions_SOURCE_DIR}")
file(COPY
${CMAKE_CURRENT_SOURCE_DIR}/patches/AndroidExtensions/Globals.cpp
DESTINATION ${androidextensions_SOURCE_DIR}/Source)
add_subdirectory(${androidextensions_SOURCE_DIR} ${androidextensions_BINARY_DIR})
else()
add_subdirectory(${androidextensions_SOURCE_DIR} ${androidextensions_BINARY_DIR})
endif()
endif()

if(JSRUNTIMEHOST_POLYFILL_XMLHTTPREQUEST)
FetchContent_MakeAvailable_With_Message(UrlLib)
set_property(TARGET UrlLib PROPERTY FOLDER Dependencies)
Expand Down
37 changes: 1 addition & 36 deletions Core/Node-API-JSI/Include/napi/napi.h
Original file line number Diff line number Diff line change
@@ -1,49 +1,14 @@
#pragma once

#include <jsi/jsi.h>
#include <napi/js_native_api_types.h>
#include <functional>
#include <initializer_list>
#include <memory>
#include <string>
#include <vector>
#include <optional>

// Copied from js_native_api_types.h (https://git.io/J8aI5)
typedef enum {
napi_default = 0,
napi_writable = 1 << 0,
napi_enumerable = 1 << 1,
napi_configurable = 1 << 2,
} napi_property_attributes;

typedef enum {
// ES6 types (corresponds to typeof)
napi_undefined,
napi_null,
napi_boolean,
napi_number,
napi_string,
napi_symbol,
napi_object,
napi_function,
napi_external,
} napi_valuetype;

typedef enum {
napi_int8_array,
napi_uint8_array,
napi_uint8_clamped_array,
napi_int16_array,
napi_uint16_array,
napi_int32_array,
napi_uint32_array,
napi_float32_array,
napi_float64_array,
// JSI doesn't support bigint.
// napi_bigint64_array,
// napi_biguint64_array,
} napi_typedarray_type;

struct napi_env__ {
napi_env__(facebook::jsi::Runtime& rt)
: rt{rt}
Expand Down
5 changes: 4 additions & 1 deletion Core/Node-API/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ elseif(ANDROID)
set(NAPI_JAVASCRIPT_ENGINE "V8" CACHE STRING "JavaScript engine for Node-API")
elseif(UNIX)
set(NAPI_JAVASCRIPT_ENGINE "JavaScriptCore" CACHE STRING "JavaScript engine for Node-API")
set(JAVASCRIPTCORE_LIBRARY "/usr/lib/x86_64-linux-gnu/libjavascriptcoregtk-4.1.so" CACHE STRING "Path to the JavaScriptCore shared library")
find_library(JAVASCRIPTCORE_LIBRARY javascriptcoregtk-4.1)
if(NOT JAVASCRIPTCORE_LIBRARY)
message(FATAL_ERROR "JavaScriptCore library not found. Please install libwebkit2gtk-4.1-dev")
endif()
else()
message(FATAL_ERROR "Unable to select Node-API JavaScript engine for platform")
endif()
Expand Down
31 changes: 24 additions & 7 deletions Core/Node-API/Source/js_native_api_javascriptcore.cc
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,9 @@ struct napi_ref__ {
CHECK_NAPI(ReferenceInfo::GetObjectId(env, _value, &_objectId));
if (_objectId == 0) {
CHECK_NAPI(ReferenceInfo::Initialize(env, _value, [value = _value](ReferenceInfo* info) {
if (info->Env()->shutting_down) {
return;
}
auto entry{info->Env()->active_ref_values.find(value)};
// NOTE: The finalizer callback is actually on a "sentinel" JS object that is linked to the
// actual JS object we are trying to track. This means it is possible for the tracked object
Expand Down Expand Up @@ -1563,14 +1566,28 @@ napi_status napi_call_function(napi_env env,
CHECK_ARG(env, argv);
}

JSObjectRef function_object = ToJSObject(env, func);

std::vector<JSValueRef> call_args(argc + 1);
call_args[0] = ToJSValue(recv);
for (size_t i = 0; i < argc; ++i) {
call_args[i + 1] = ToJSValue(argv[i]);
}

JSValueRef exception{};
JSValueRef return_value{JSObjectCallAsFunction(
env->context,
ToJSObject(env, func),
JSValueIsUndefined(env->context, ToJSValue(recv)) ? nullptr : ToJSObject(env, recv),
argc,
ToJSValues(argv),
&exception)};
JSValueRef call_value{JSObjectGetProperty(
env->context, function_object, JSString("call"), &exception)};
CHECK_JSC(env, exception);

JSObjectRef call_object = JSValueToObject(env->context, call_value, &exception);
CHECK_JSC(env, exception);

JSValueRef return_value{JSObjectCallAsFunction(env->context,
call_object,
function_object,
call_args.size(),
call_args.data(),
&exception)};
CHECK_JSC(env, exception);

if (result != nullptr) {
Expand Down
2 changes: 2 additions & 0 deletions Core/Node-API/Source/js_native_api_javascriptcore.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ struct napi_env__ {
napi_extended_error_info last_error{nullptr, nullptr, 0, napi_ok};
std::unordered_map<napi_value, std::uintptr_t> active_ref_values{};
std::list<napi_ref> strong_refs{};
bool shutting_down{false};

JSValueRef constructor_info_symbol{};
JSValueRef function_info_symbol{};
Expand All @@ -32,6 +33,7 @@ struct napi_env__ {
}

~napi_env__() {
shutting_down = true;
deinit_refs();
deinit_symbol(wrapper_info_symbol);
deinit_symbol(reference_info_symbol);
Expand Down
1 change: 1 addition & 0 deletions Tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
add_subdirectory(UnitTests)
add_subdirectory(NodeApi)
npm(install --silent)
111 changes: 111 additions & 0 deletions Tests/NodeApi/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
---
Language: Cpp
# BasedOnStyle: Google
AccessModifierOffset: -1
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Right
AlignOperands: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Inline
AllowShortIfStatementsOnASingleLine: true
AllowShortLoopsOnASingleLine: true
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: true
BinPackArguments: false
BinPackParameters: false
BraceWrapping:
AfterClass: false
AfterControlStatement: false
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Attach
BreakBeforeInheritanceComma: false
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeColon
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit: 80
CommentPragmas: '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerAlignment: false
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: true
ForEachMacros:
- foreach
- Q_FOREACH
- BOOST_FOREACH
IncludeBlocks: Preserve
IncludeCategories:
- Regex: '^<ext/.*\.h>'
Priority: 2
- Regex: '^<.*\.h>'
Priority: 1
- Regex: '^<.*'
Priority: 2
- Regex: '.*'
Priority: 3
IncludeIsMainRegex: '([-_](test|unittest))?$'
IndentCaseLabels: true
IndentPPDirectives: None
IndentWidth: 2
IndentWrappedFunctionNames: false
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: false
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBlockIndentWidth: 2
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: false
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 1
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 200
PointerAlignment: Left
ReflowComments: true
SortIncludes: true
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 2
SpacesInAngles: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Auto
TabWidth: 8
UseTab: Never
Loading
Loading