Skip to content

Commit 845f5ee

Browse files
committed
initial work
1 parent df27ec9 commit 845f5ee

File tree

11 files changed

+111
-209
lines changed

11 files changed

+111
-209
lines changed

NativeScript/CMakeLists.txt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${COMMON_FLAGS}")
2121
# Arguments
2222

2323
set(TARGET_PLATFORM "macos" CACHE STRING "Target platform for the Objective-C bridge")
24-
set(TARGET_ENGINE "hermes" CACHE STRING "Target JS engine for the NativeScript runtime")
24+
set(TARGET_ENGINE "v8" CACHE STRING "Target JS engine for the NativeScript runtime")
2525
set(METADATA_SIZE 0 CACHE STRING "Size of embedded metadata in bytes")
2626

2727
if(TARGET_PLATFORM STREQUAL "ios")
@@ -159,10 +159,12 @@ if(ENABLE_JS_RUNTIME)
159159
)
160160

161161
elseif(TARGET_ENGINE_HERMES)
162+
message(STATUS "Hermes engine detected")
163+
162164
include_directories(
163165
napi/hermes
164-
napi/hermes/hermes
165-
napi/hermes/jsi
166+
napi/hermes/include/hermes
167+
napi/hermes/include/jsi
166168
)
167169

168170
set(SOURCE_FILES

NativeScript/runtime/Console.cpp

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
#include "Console.h"
2-
#include "js_native_api_types.h"
2+
33
#include <iostream>
44
#include <string>
55

6-
namespace charon {
6+
#include "js_native_api_types.h"
7+
8+
namespace nativescript {
79

810
void Console::init(napi_env env) {
911
napi_value global, Console, console;
@@ -19,7 +21,7 @@ void Console::init(napi_env env) {
1921
.setter = nullptr,
2022
.value = nullptr,
2123
.attributes = napi_default,
22-
.data = (void *)kConsoleStreamLog,
24+
.data = (void*)kConsoleStreamLog,
2325
},
2426
{
2527
.utf8name = "error",
@@ -29,7 +31,7 @@ void Console::init(napi_env env) {
2931
.setter = nullptr,
3032
.value = nullptr,
3133
.attributes = napi_default,
32-
.data = (void *)kConsoleStreamError,
34+
.data = (void*)kConsoleStreamError,
3335
},
3436
{
3537
.utf8name = "warn",
@@ -39,7 +41,7 @@ void Console::init(napi_env env) {
3941
.setter = nullptr,
4042
.value = nullptr,
4143
.attributes = napi_default,
42-
.data = (void *)kConsoleStreamWarn,
44+
.data = (void*)kConsoleStreamWarn,
4345
},
4446
};
4547

@@ -83,7 +85,7 @@ napi_value Console::constructor(napi_env env, napi_callback_info cbinfo) {
8385
napi_value Console::log(napi_env env, napi_callback_info cbinfo) {
8486
size_t argc = 0;
8587
ConsoleStream stream;
86-
void *data = nullptr;
88+
void* data = nullptr;
8789

8890
napi_get_cb_info(env, cbinfo, &argc, nullptr, nullptr, &data);
8991

@@ -123,7 +125,7 @@ napi_value Console::log(napi_env env, napi_callback_info cbinfo) {
123125

124126
size_t length = 0;
125127
napi_get_value_string_utf8(env, argstr, nullptr, 0, &length);
126-
char *strbuf = (char *)malloc(length + 2);
128+
char* strbuf = (char*)malloc(length + 2);
127129
napi_get_value_string_utf8(env, argstr, strbuf, length + 2, &length);
128130
strbuf[length] = i >= (argc - 1) ? '\0' : ' ';
129131
strbuf[length + 1] = '\0';
@@ -135,35 +137,35 @@ napi_value Console::log(napi_env env, napi_callback_info cbinfo) {
135137
std::string log;
136138
log += "[JS]";
137139
switch (stream) {
138-
case kConsoleStreamLog:
139-
// log += "LOG";
140-
break;
141-
case kConsoleStreamError:
142-
log += " ERROR";
143-
break;
144-
case kConsoleStreamWarn:
145-
log += " WARN";
146-
break;
140+
case kConsoleStreamLog:
141+
// log += "LOG";
142+
break;
143+
case kConsoleStreamError:
144+
log += " ERROR";
145+
break;
146+
case kConsoleStreamWarn:
147+
log += " WARN";
148+
break;
147149
}
148150
log += " ";
149151
log += string;
150152
log += "\n";
151153

152154
switch (stream) {
153-
case kConsoleStreamLog:
154-
std::cout << log;
155-
break;
156-
case kConsoleStreamError:
157-
std::cerr << log;
158-
break;
159-
case kConsoleStreamWarn:
160-
std::cerr << log;
161-
break;
155+
case kConsoleStreamLog:
156+
std::cout << log;
157+
break;
158+
case kConsoleStreamError:
159+
std::cerr << log;
160+
break;
161+
case kConsoleStreamWarn:
162+
std::cerr << log;
163+
break;
162164
}
163165

164166
napi_value undefined;
165167
napi_get_undefined(env, &undefined);
166168
return undefined;
167169
}
168170

169-
} // namespace charon
171+
} // namespace nativescript

NativeScript/runtime/Console.h

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

44
#include "js_native_api.h"
55

6-
namespace charon {
6+
namespace nativescript {
77

88
enum ConsoleStream {
99
kConsoleStreamLog,
@@ -12,14 +12,14 @@ enum ConsoleStream {
1212
};
1313

1414
class Console {
15-
public:
15+
public:
1616
static void init(napi_env env);
1717

1818
static napi_value constructor(napi_env env, napi_callback_info cbinfo);
1919

2020
static napi_value log(napi_env env, napi_callback_info cbinfo);
2121
};
2222

23-
} // namespace charon
23+
} // namespace nativescript
2424

25-
#endif // CONSOLE_H
25+
#endif // CONSOLE_H

NativeScript/runtime/NativeScript.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include "Runtime.h"
33
#include "RuntimeConfig.h"
44

5-
using namespace charon;
5+
using namespace nativescript;
66

77
@implementation Config
88

NativeScript/runtime/Performance.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#include "Performance.h"
2+
23
#include "mach/mach_time.h"
34

4-
namespace charon {
5+
namespace nativescript {
56

67
void Performance::init(napi_env env) {
78
napi_value global, Performance, performance;
@@ -63,4 +64,4 @@ napi_value Performance::now(napi_env env, napi_callback_info cbinfo) {
6364
return result;
6465
}
6566

66-
} // namespace charon
67+
} // namespace nativescript

NativeScript/runtime/Performance.h

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

44
#include "js_native_api.h"
55

6-
namespace charon {
6+
namespace nativescript {
77

88
class Performance {
9-
public:
9+
public:
1010
static void init(napi_env env);
1111

1212
static napi_value constructor(napi_env env, napi_callback_info cbinfo);
1313

1414
static napi_value now(napi_env env, napi_callback_info cbinfo);
1515
};
1616

17-
} // namespace charon
17+
} // namespace nativescript
1818

19-
#endif // PERFORMANCE_H
19+
#endif // PERFORMANCE_H

NativeScript/runtime/Runtime.cpp

Lines changed: 12 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -23,31 +23,9 @@
2323

2424
#include "NativeScript.h"
2525

26-
namespace charon {
27-
28-
// class BytecodeBuffer : public facebook::jsi::Buffer {
29-
// public:
30-
// BytecodeBuffer(const uint8_t *data, size_t length)
31-
// : data_(data), length_(length) {}
32-
33-
// size_t size() const override { return length_; }
34-
// const uint8_t *data() const override { return data_; }
35-
36-
// private:
37-
// const uint8_t *data_;
38-
// size_t length_;
39-
// };
40-
41-
Runtime::Runtime(std::string& mainPath) : mainPath(mainPath) {
42-
// hermes::vm::RuntimeConfig config =
43-
// hermes::vm::RuntimeConfig::Builder().withMicrotaskQueue(true).build();
44-
// threadSafeRuntime = facebook::hermes::makeThreadSafeHermesRuntime(config);
45-
// runtime =
46-
// (facebook::hermes::HermesRuntime
47-
// *)&threadSafeRuntime->getUnsafeRuntime();
48-
49-
// runtime->createNapiEnv(&env);
26+
namespace nativescript {
5027

28+
Runtime::Runtime() {
5129
js_set_runtime_flags("");
5230

5331
js_create_runtime(&runtime);
@@ -87,8 +65,7 @@ Runtime::Runtime(std::string& mainPath) : mainPath(mainPath) {
8765
)";
8866

8967
napi_value compatScript, result;
90-
napi_create_string_utf8(env, CompatScript, NAPI_AUTO_LENGTH,
91-
&compatScript);
68+
napi_create_string_utf8(env, CompatScript, NAPI_AUTO_LENGTH, &compatScript);
9269
napi_run_script(env, compatScript, &result);
9370

9471
Console::init(env);
@@ -97,7 +74,7 @@ Runtime::Runtime(std::string& mainPath) : mainPath(mainPath) {
9774
Timers::init(env);
9875
#endif // __APPLE__
9976

100-
require = Require::init(env, mainPath, mainPath);
77+
require = Require::init(env, RuntimeConfig.BaseDir, RuntimeConfig.BaseDir);
10178

10279
const char* metadata_path = std::getenv("NS_METADATA_PATH");
10380
objc_bridge_init(env, metadata_path, RuntimeConfig.MetadataPtr);
@@ -110,96 +87,28 @@ Runtime::Runtime(std::string& mainPath) : mainPath(mainPath) {
11087
napi_close_handle_scope(env, scope);
11188
}
11289

113-
napi_value Runtime::evaluateModule(std::string& spec) {
114-
NapiScope scope(env);
115-
std::string path = require->resolve(spec);
116-
return require->require(env, path);
117-
}
118-
119-
int Runtime::runScriptString(std::string& scriptSrc) {
90+
void Runtime::RunScript(std::string& scriptSrc) {
12091
NapiScope scope(env);
12192

12293
napi_value script, result;
12394
napi_create_string_utf8(env, scriptSrc.c_str(), scriptSrc.length(), &script);
12495
js_execute_script(env, script, "<anonymous>", &result);
125-
126-
return 0;
127-
}
128-
129-
int Runtime::executeJS(const char* sourceFile) {
130-
NapiScope scope(env);
131-
132-
auto f = std::fopen(sourceFile, "r");
133-
if (!f) {
134-
std::cout << "Failed to open file: " << sourceFile << std::endl;
135-
return 1;
136-
}
137-
138-
auto source = std::string{};
139-
auto buf = std::array<char, 1024>{};
140-
while (auto n = std::fread(buf.data(), 1, buf.size(), f)) {
141-
source.append(buf.data(), n);
142-
}
143-
144-
std::fclose(f);
145-
146-
// auto buffer = std::make_shared<facebook::jsi::StringBuffer>(source);
147-
// std::string sourceURL = sourceFile;
148-
149-
// auto result = runtime->evaluateJavaScript(buffer, sourceURL);
150-
151-
napi_value script, result;
152-
napi_create_string_utf8(env, source.c_str(), source.length(), &script);
153-
js_execute_script(env, script, sourceFile, &result);
154-
155-
return 0;
15696
}
15797

158-
int Runtime::executeBytecode(const uint8_t* data, size_t size) {
98+
napi_value Runtime::RunModule(std::string spec) {
15999
NapiScope scope(env);
160-
161-
// auto buffer = std::make_shared<BytecodeBuffer>(data, size);
162-
// std::string sourceURL = "embedded-hbc";
163-
164-
// auto result = runtime->evaluateJavaScript(buffer, sourceURL);
165-
// TODO implement this for v8
166-
167-
return 0;
100+
std::string path = require->resolve(spec);
101+
return require->require(env, path);
168102
}
169103

170-
bool Runtime::eventLoopStep() { return false; }
171-
172-
void Runtime::addEventLoopToRunLoop(bool exitOnEmpty) {
173-
auto handler =
174-
^void(CFRunLoopObserverRef observer, CFRunLoopActivity activity) {
175-
if (activity == kCFRunLoopBeforeWaiting) {
176-
bool moreWork = this->eventLoopStep();
177-
if (moreWork) {
178-
CFRunLoopWakeUp(CFRunLoopGetMain());
179-
} else if (exitOnEmpty) {
180-
CFRunLoopStop(CFRunLoopGetMain());
181-
}
182-
}
183-
};
184-
185-
CFRunLoopObserverRef observer = CFRunLoopObserverCreateWithHandler(
186-
kCFAllocatorDefault, kCFRunLoopAllActivities, true, 0, handler);
187-
CFRunLoopAddObserver(CFRunLoopGetMain(), observer, kCFRunLoopDefaultMode);
104+
void Runtime::RunMainModule() {
105+
napi_value result = RunModule("./");
188106
}
189107

190-
void Runtime::runRunLoop() {
191-
// Why does this not stop?
192-
// while (true) {
193-
// CFRunLoopRunResult result = CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0,
194-
// true); if (result == kCFRunLoopRunFinished || result ==
195-
// kCFRunLoopRunStopped) {
196-
// break;
197-
// }
198-
// }
199-
108+
void Runtime::RunLoop() {
200109
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, true);
201110
}
202111

203-
} // namespace charon
112+
} // namespace nativescript
204113

205114
#endif // ENABLE_JS_RUNTIME

0 commit comments

Comments
 (0)