Skip to content

Commit 5dfcf3e

Browse files
committed
workers support
1 parent 74e70db commit 5dfcf3e

File tree

238 files changed

+51628
-585
lines changed

Some content is hidden

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

238 files changed

+51628
-585
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,6 @@ v8_build
5555
/project-template-ios/.build_env_vars.sh
5656
/project-template-ios/__PROJECT_NAME__.xcodeproj/project.xcworkspace/xcshareddata/
5757
/project-template-vision/.build_env_vars.sh
58-
/project-template-vision/__PROJECT_NAME__.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist
58+
/project-template-vision/__PROJECT_NAME__.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist
59+
60+
.cache/

NativeScript/CMakeLists.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ elseif(TARGET_ENGINE STREQUAL "hermes")
7272
elseif(TARGET_ENGINE STREQUAL "v8")
7373
set(TARGET_ENGINE_V8 TRUE)
7474
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")
75+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-rtti -stdlib=libc++ -std=c++20 -DTARGET_ENGINE_V8")
7676
elseif(TARGET_ENGINE STREQUAL "quickjs")
7777
set(TARGET_ENGINE_QUICKJS TRUE)
7878
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++ -std=c++20 -DTARGET_ENGINE_QUICKJS")
@@ -85,6 +85,7 @@ else()
8585
endif()
8686

8787
if(ENABLE_JS_RUNTIME)
88+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DENABLE_JS_RUNTIME")
8889
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DENABLE_JS_RUNTIME")
8990
elseif(TARGET_PLATFORM_MACOS)
9091
# If building a generic library for macOS, we'll build a dylib instead of a framework
@@ -134,6 +135,12 @@ if(ENABLE_JS_RUNTIME)
134135
${SOURCE_FILES}
135136
runtime/modules/console/Console.cpp
136137
runtime/Runtime.cpp
138+
runtime/modules/worker/Worker.mm
139+
runtime/modules/worker/MessageJSON.cpp
140+
runtime/modules/worker/MessageV8.cpp
141+
runtime/modules/worker/ConcurrentQueue.cpp
142+
runtime/modules/worker/WorkerImpl.mm
143+
runtime/modules/worker/WorkerImpl.mm
137144
runtime/modules/module/ModuleInternal.cpp
138145
runtime/modules/performance/Performance.cpp
139146
runtime/Bundle.mm
@@ -163,6 +170,7 @@ if(ENABLE_JS_RUNTIME)
163170
elseif(TARGET_ENGINE_HERMES)
164171
include_directories(
165172
napi/hermes
173+
napi/hermes/include
166174
napi/hermes/include/hermes
167175
napi/hermes/include/jsi
168176
)

NativeScript/cli/main.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ void bootFromBytecode(std::string baseDir, const void* data, size_t size) {
1717

1818
auto runtime = Runtime();
1919

20+
runtime.Init();
21+
2022
// TODO
2123
// runtime.ExecuteBytecode(data, size);
2224

@@ -28,6 +30,8 @@ void bootFromModuleSpec(std::string baseDir, std::string spec) {
2830

2931
auto runtime = Runtime();
3032

33+
runtime.Init();
34+
3135
try {
3236
runtime.RunModule(spec);
3337
} catch (const nativescript::NativeScriptException& e) {
@@ -75,7 +79,6 @@ int main(int argc, char** argv) {
7579
std::string cwd = std::filesystem::current_path().string();
7680

7781
if (status == segappend_ok) {
78-
auto runtime = Runtime();
7982
size_t bytecode_size = *(size_t*)segmentData;
8083
segmentData += sizeof(size_t);
8184

NativeScript/ffi/NativeScriptException.h

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

44
#include <string>
55

6+
#include "js_native_api.h"
67
#include "js_native_api_types.h"
78

89
namespace nativescript {
@@ -15,17 +16,23 @@ class NativeScriptException {
1516
NativeScriptException(napi_env env, napi_value error,
1617
const std::string& message,
1718
const std::string& name = "NativeScriptException");
18-
~NativeScriptException();
19+
NativeScriptException(napi_env env, const std::string& message,
20+
const std::string& name = "NativeScriptException");
1921

20-
void ReThrowToJS(napi_env env);
22+
void ReThrowToJS(napi_env env, napi_value* errorOut = nullptr);
2123

2224
static void OnUncaughtError(napi_env env, napi_value error);
2325

26+
inline std::string Name() const {
27+
if (name_.empty()) {
28+
return "NativeScriptException";
29+
}
30+
return name_;
31+
}
32+
2433
std::string Description() const;
2534

2635
private:
27-
napi_env env_;
28-
napi_ref javascriptException_;
2936
std::string name_;
3037
std::string message_;
3138
std::string stackTrace_;

NativeScript/ffi/NativeScriptException.mm

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,40 +10,42 @@
1010
namespace nativescript {
1111

1212
NativeScriptException::NativeScriptException(const std::string& message) {
13-
this->javascriptException_ = nullptr;
1413
this->message_ = message;
1514
this->name_ = "NativeScriptException";
1615
}
1716

1817
NativeScriptException::NativeScriptException(const std::string& message,
1918
const std::string& stackTrace) {
20-
this->javascriptException_ = nullptr;
2119
this->message_ = message;
2220
this->stackTrace_ = stackTrace;
2321
this->name_ = "NativeScriptException";
2422
}
2523

24+
NativeScriptException::NativeScriptException(napi_env env, const std::string& message,
25+
const std::string& name) {
26+
this->message_ = message;
27+
this->stackTrace_ = "";
28+
this->fullMessage_ = message;
29+
this->name_ = name;
30+
}
31+
2632
NativeScriptException::NativeScriptException(napi_env env, napi_value error,
2733
const std::string& message, const std::string& name) {
28-
this->env_ = env;
29-
this->javascriptException_ = nullptr;
30-
napi_create_reference(env, error, 1, &this->javascriptException_);
3134
this->message_ = GetErrorMessage(env, error, message);
3235
this->stackTrace_ = GetErrorStackTrace(env, error);
3336
this->fullMessage_ = GetFullMessage(env, error, this->message_);
3437
this->name_ = name;
3538
napi_set_named_property(env, error, "name", napi_util::to_js_string(env, name));
3639
}
3740

38-
NativeScriptException::~NativeScriptException() {
39-
if (this->javascriptException_ != nullptr) {
40-
napi_delete_reference(this->env_, this->javascriptException_);
41-
this->javascriptException_ = nullptr;
41+
std::string NativeScriptException::Description() const {
42+
if (this->fullMessage_.empty()) {
43+
return this->message_;
44+
} else {
45+
return this->fullMessage_;
4246
}
4347
}
4448

45-
std::string NativeScriptException::Description() const { return this->fullMessage_; }
46-
4749
void NativeScriptException::OnUncaughtError(napi_env env, napi_value error) {
4850
NapiScope scope(env);
4951

@@ -105,20 +107,10 @@
105107
// });
106108
}
107109

108-
void NativeScriptException::ReThrowToJS(napi_env env) {
110+
void NativeScriptException::ReThrowToJS(napi_env env, napi_value* errorOut) {
109111
napi_value errObj;
110112

111-
if (javascriptException_ != nullptr) {
112-
napi_get_reference_value(env, javascriptException_, &errObj);
113-
if (napi_util::is_of_type(env, errObj, napi_object)) {
114-
if (!fullMessage_.empty()) {
115-
napi_set_named_property(env, errObj, "fullMessage",
116-
napi_util::to_js_string(env, fullMessage_));
117-
} else if (!message_.empty()) {
118-
napi_set_named_property(env, errObj, "fullMessage", napi_util::to_js_string(env, message_));
119-
}
120-
}
121-
} else if (!fullMessage_.empty()) {
113+
if (!fullMessage_.empty()) {
122114
napi_create_error(env, napi_util::to_js_string(env, name_),
123115
napi_util::to_js_string(env, fullMessage_), &errObj);
124116
} else if (!message_.empty()) {
@@ -130,7 +122,11 @@
130122
&errObj);
131123
}
132124

133-
napi_throw(env, errObj);
125+
if (errorOut != nullptr) {
126+
*errorOut = errObj;
127+
} else {
128+
napi_throw(env, errObj);
129+
}
134130
}
135131

136132
void NativeScriptException::PrintErrorMessage(const std::string& errorMessage) {

NativeScript/napi/common/native_api_util.h

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,60 @@
11
#ifndef NATIVE_API_UTIL_H_
22
#define NATIVE_API_UTIL_H_
33

4+
#include <string>
5+
#include <ios>
6+
#include <cstddef>
7+
#include <cwchar>
8+
9+
#ifdef TARGET_ENGINE_V8
10+
namespace std {
11+
template<>
12+
struct char_traits<unsigned short> {
13+
using char_type = unsigned short;
14+
using int_type = int;
15+
using off_type = streamoff;
16+
using pos_type = streampos;
17+
using state_type = mbstate_t;
18+
19+
static void assign(char_type& c1, const char_type& c2) noexcept { c1 = c2; }
20+
static void assign(char_type* s, size_t n, char_type a) noexcept {
21+
for (size_t i = 0; i < n; ++i) s[i] = a;
22+
}
23+
static bool eq(char_type c1, char_type c2) noexcept { return c1 == c2; }
24+
static bool lt(char_type c1, char_type c2) noexcept { return c1 < c2; }
25+
static int compare(const char_type* s1, const char_type* s2, size_t n) {
26+
for (size_t i = 0; i < n; ++i) {
27+
if (lt(s1[i], s2[i])) return -1;
28+
if (lt(s2[i], s1[i])) return 1;
29+
}
30+
return 0;
31+
}
32+
static size_t length(const char_type* s) {
33+
size_t len = 0;
34+
while (!eq(s[len], char_type(0))) ++len;
35+
return len;
36+
}
37+
static const char_type* find(const char_type* s, size_t n, const char_type& a) {
38+
for (size_t i = 0; i < n; ++i) if (eq(s[i], a)) return s + i;
39+
return nullptr;
40+
}
41+
static char_type* move(char_type* s1, const char_type* s2, size_t n) {
42+
if (n == 0) return s1;
43+
return (char_type*)memmove(s1, s2, n * sizeof(char_type));
44+
}
45+
static char_type* copy(char_type* s1, const char_type* s2, size_t n) {
46+
if (n == 0) return s1;
47+
return (char_type*)memcpy(s1, s2, n * sizeof(char_type));
48+
}
49+
static char_type to_char_type(int_type c) noexcept { return char_type(c); }
50+
static int_type to_int_type(char_type c) noexcept { return int_type(c); }
51+
static bool eq_int_type(int_type c1, int_type c2) noexcept { return c1 == c2; }
52+
static int_type eof() noexcept { return static_cast<int_type>(-1); }
53+
static int_type not_eof(int_type c) noexcept { return eq_int_type(c, eof()) ? 0 : c; }
54+
};
55+
}
56+
#endif // TARGET_ENGINE_V8
57+
458
#include <dlfcn.h>
559

660
#include <sstream>
@@ -90,6 +144,47 @@
90144

91145
namespace napi_util {
92146

147+
class PersistentObject {
148+
public:
149+
PersistentObject(napi_env env, napi_value value)
150+
: env_(env), isOwnedRef_(true) {
151+
napi_create_reference(env_, value, 1, &ref_);
152+
}
153+
154+
PersistentObject(napi_env env, napi_ref ref)
155+
: env_(env), ref_(ref), isOwnedRef_(false) {
156+
uint32_t result;
157+
napi_reference_ref(env, ref, &result);
158+
}
159+
160+
~PersistentObject() {
161+
if (isOwnedRef_)
162+
napi_delete_reference(env_, ref_);
163+
else {
164+
uint32_t result;
165+
napi_reference_unref(env_, ref_, &result);
166+
}
167+
}
168+
169+
napi_value GetValue(napi_env env = nullptr) {
170+
if (env == nullptr) {
171+
env = env_;
172+
}
173+
napi_value value;
174+
napi_get_reference_value(env, ref_, &value);
175+
return value;
176+
}
177+
178+
napi_ref GetRef() const { return ref_; }
179+
180+
napi_env GetEnv() const { return env_; }
181+
182+
private:
183+
bool isOwnedRef_ = false;
184+
napi_env env_;
185+
napi_ref ref_;
186+
};
187+
93188
inline napi_property_descriptor desc(const char* name, napi_callback method,
94189
void* data = nullptr) {
95190
return {
@@ -127,6 +222,17 @@ inline std::string get_cxx_string(napi_env env, napi_value str) {
127222
return result;
128223
}
129224

225+
inline napi_value to_js_number(napi_env env, double value) {
226+
napi_value js_number;
227+
napi_create_double(env, value, &js_number);
228+
return js_number;
229+
}
230+
inline napi_value to_js_number(napi_env env, int32_t value) {
231+
napi_value js_number;
232+
napi_create_int32(env, value, &js_number);
233+
return js_number;
234+
}
235+
130236
inline napi_value to_js_string(napi_env env, const std::string& str) {
131237
napi_value js_string;
132238
napi_create_string_utf8(env, str.c_str(), str.length(), &js_string);

0 commit comments

Comments
 (0)