Skip to content

Commit 90e8154

Browse files
committed
refactor: runtime code
1 parent 845f5ee commit 90e8154

16 files changed

+111
-129
lines changed

NativeScript/CMakeLists.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@ cmake_minimum_required(VERSION 3.15)
22

33
# Metadata
44

5-
project(NativeScript)
5+
project(NativeScript CXX OBJCXX)
66

77
set(NAME NativeScript)
88
set(VERSION 0.1.0)
99
set(BUNDLE_IDENTIFIER "org.nativescript.runtime")
1010

11-
enable_language(OBJCXX)
12-
1311
set(CMAKE_CXX_STANDARD 20)
1412

1513
set(BUILD_FRAMEWORK TRUE)

NativeScript/runtime/App.h

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
#ifndef APP_H
22
#define APP_H
33

4-
// #include <hermes/hermes.h>
5-
// #include <hermes/hermes_api.h>
6-
#include "js_native_api.h"
4+
#include "js_native_api_types.h"
75

86
class App {
9-
public:
10-
// facebook::hermes::HermesRuntime *runtime;
11-
12-
static App *init(napi_env env);
13-
static napi_value run(napi_env env, napi_callback_info cbinfo);
7+
public:
8+
static App* Init(napi_env env);
9+
static napi_value Run(napi_env env, napi_callback_info cbinfo);
1410
};
1511

16-
#endif // APP_H
12+
#endif // APP_H

NativeScript/runtime/App.mm

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
#ifdef __APPLE__
22

33
#include "App.h"
4+
#include "js_native_api.h"
45

56
#ifdef TARGET_OS_MAC
67

78
#import <AppKit/AppKit.h>
89

910
#endif
1011

11-
App *App::init(napi_env env) {
12+
App *App::Init(napi_env env) {
1213
App *appInst = new App();
1314

1415
napi_value global, App, app;
@@ -18,13 +19,13 @@
1819
napi_create_object(env, &App);
1920
napi_set_named_property(env, global, "App", App);
2021

21-
napi_create_function(env, "run", NAPI_AUTO_LENGTH, run, appInst, &app);
22+
napi_create_function(env, "run", NAPI_AUTO_LENGTH, Run, appInst, &app);
2223
napi_set_named_property(env, App, "run", app);
2324

2425
return appInst;
2526
}
2627

27-
napi_value App::run(napi_env env, napi_callback_info cbinfo) {
28+
napi_value App::Run(napi_env env, napi_callback_info cbinfo) {
2829
App *appInst = nullptr;
2930
napi_get_cb_info(env, cbinfo, nullptr, nullptr, nullptr, (void **)&appInst);
3031

NativeScript/runtime/Console.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
#include <iostream>
44
#include <string>
55

6-
#include "js_native_api_types.h"
6+
#include "js_native_api.h"
77

88
namespace nativescript {
99

10-
void Console::init(napi_env env) {
10+
void Console::Init(napi_env env) {
1111
napi_value global, Console, console;
1212

1313
napi_get_global(env, &global);
@@ -16,7 +16,7 @@ void Console::init(napi_env env) {
1616
{
1717
.utf8name = "log",
1818
.name = nullptr,
19-
.method = log,
19+
.method = Log,
2020
.getter = nullptr,
2121
.setter = nullptr,
2222
.value = nullptr,
@@ -26,7 +26,7 @@ void Console::init(napi_env env) {
2626
{
2727
.utf8name = "error",
2828
.name = nullptr,
29-
.method = log,
29+
.method = Log,
3030
.getter = nullptr,
3131
.setter = nullptr,
3232
.value = nullptr,
@@ -36,7 +36,7 @@ void Console::init(napi_env env) {
3636
{
3737
.utf8name = "warn",
3838
.name = nullptr,
39-
.method = log,
39+
.method = Log,
4040
.getter = nullptr,
4141
.setter = nullptr,
4242
.value = nullptr,
@@ -45,7 +45,7 @@ void Console::init(napi_env env) {
4545
},
4646
};
4747

48-
napi_define_class(env, "Console", NAPI_AUTO_LENGTH, Console::constructor,
48+
napi_define_class(env, "Console", NAPI_AUTO_LENGTH, Console::Constructor,
4949
nullptr, 3, properties, &Console);
5050

5151
napi_new_instance(env, Console, 0, nullptr, &console);
@@ -76,13 +76,13 @@ void Console::init(napi_env env) {
7676
napi_define_properties(env, global, 2, globalProperties);
7777
}
7878

79-
napi_value Console::constructor(napi_env env, napi_callback_info cbinfo) {
79+
napi_value Console::Constructor(napi_env env, napi_callback_info cbinfo) {
8080
napi_value thisArg;
8181
napi_get_cb_info(env, cbinfo, nullptr, nullptr, &thisArg, nullptr);
8282
return thisArg;
8383
}
8484

85-
napi_value Console::log(napi_env env, napi_callback_info cbinfo) {
85+
napi_value Console::Log(napi_env env, napi_callback_info cbinfo) {
8686
size_t argc = 0;
8787
ConsoleStream stream;
8888
void* data = nullptr;

NativeScript/runtime/Console.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef CONSOLE_H
22
#define CONSOLE_H
33

4-
#include "js_native_api.h"
4+
#include "js_native_api_types.h"
55

66
namespace nativescript {
77

@@ -13,11 +13,11 @@ enum ConsoleStream {
1313

1414
class Console {
1515
public:
16-
static void init(napi_env env);
16+
static void Init(napi_env env);
1717

18-
static napi_value constructor(napi_env env, napi_callback_info cbinfo);
18+
static napi_value Constructor(napi_env env, napi_callback_info cbinfo);
1919

20-
static napi_value log(napi_env env, napi_callback_info cbinfo);
20+
static napi_value Log(napi_env env, napi_callback_info cbinfo);
2121
};
2222

2323
} // namespace nativescript

NativeScript/runtime/NativeScript.mm

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ @implementation NativeScript
2121

2222
- (void)runScriptString:(NSString*)script runLoop:(BOOL)runLoop {
2323
std::string cppScript = [script UTF8String];
24-
runtime_->runScriptString(cppScript);
24+
runtime_->RunScript(cppScript);
2525
if (runLoop) {
26-
runtime_->runRunLoop();
26+
runtime_->RunLoop();
2727
}
2828
}
2929

3030
- (void)runMainApplication {
3131
std::string spec = "./app/bundle.js";
32-
runtime_->evaluateModule(spec);
33-
runtime_->runRunLoop();
32+
runtime_->RunModule(spec);
33+
runtime_->RunLoop();
3434
}
3535

3636
- (bool)liveSync {
@@ -59,7 +59,7 @@ - (instancetype)initWithConfig:(Config*)config {
5959
RuntimeConfig.IsDebug = [config IsDebug];
6060
RuntimeConfig.LogToSystemConsole = [config LogToSystemConsole];
6161

62-
runtime_ = std::make_unique<Runtime>(RuntimeConfig.BaseDir);
62+
runtime_ = std::make_unique<Runtime>();
6363

6464
// TODO: separate runtime init and measure the time
6565

NativeScript/runtime/Performance.cpp

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

3+
#include "js_native_api.h"
34
#include "mach/mach_time.h"
45

56
namespace nativescript {
67

7-
void Performance::init(napi_env env) {
8+
void Performance::Init(napi_env env) {
89
napi_value global, Performance, performance;
910

1011
napi_get_global(env, &global);
@@ -13,7 +14,7 @@ void Performance::init(napi_env env) {
1314
{
1415
.utf8name = "now",
1516
.name = nullptr,
16-
.method = now,
17+
.method = Now,
1718
.getter = nullptr,
1819
.setter = nullptr,
1920
.value = nullptr,
@@ -23,7 +24,7 @@ void Performance::init(napi_env env) {
2324
};
2425

2526
napi_define_class(env, "Performance", NAPI_AUTO_LENGTH,
26-
Performance::constructor, nullptr, 1, properties,
27+
Performance::Constructor, nullptr, 1, properties,
2728
&Performance);
2829

2930
napi_new_instance(env, Performance, 0, nullptr, &performance);
@@ -44,14 +45,14 @@ void Performance::init(napi_env env) {
4445
napi_define_properties(env, global, 1, globalProperties);
4546
}
4647

47-
napi_value Performance::constructor(napi_env env, napi_callback_info cbinfo) {
48+
napi_value Performance::Constructor(napi_env env, napi_callback_info cbinfo) {
4849
napi_value thisArg;
4950
napi_get_cb_info(env, cbinfo, nullptr, nullptr, &thisArg, nullptr);
5051

5152
return thisArg;
5253
}
5354

54-
napi_value Performance::now(napi_env env, napi_callback_info cbinfo) {
55+
napi_value Performance::Now(napi_env env, napi_callback_info cbinfo) {
5556
uint64_t time = mach_absolute_time();
5657
mach_timebase_info_data_t timebase;
5758
mach_timebase_info(&timebase);

NativeScript/runtime/Performance.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
#ifndef PERFORMANCE_H
22
#define PERFORMANCE_H
33

4-
#include "js_native_api.h"
4+
#include "js_native_api_types.h"
55

66
namespace nativescript {
77

88
class Performance {
99
public:
10-
static void init(napi_env env);
10+
static void Init(napi_env env);
1111

12-
static napi_value constructor(napi_env env, napi_callback_info cbinfo);
12+
static napi_value Constructor(napi_env env, napi_callback_info cbinfo);
1313

14-
static napi_value now(napi_env env, napi_callback_info cbinfo);
14+
static napi_value Now(napi_env env, napi_callback_info cbinfo);
1515
};
1616

1717
} // namespace nativescript

NativeScript/runtime/Require.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,39 +9,39 @@
99
#include "NapiUtil.h"
1010
#include "js_native_api.h"
1111

12-
napi_value Require::createRequire(napi_env env, std::string& path,
12+
napi_value Require::CreateRequire(napi_env env, std::string& path,
1313
std::string& tilde, Require** pRequire) {
1414
Require* require = new Require(path, tilde);
1515
if (pRequire) {
1616
*pRequire = require;
1717
}
1818
napi_value result;
1919
napi_create_function(env, "require", NAPI_AUTO_LENGTH,
20-
Require::requireCallback, require, &result);
20+
Require::RequireCallback, require, &result);
2121
napi_ref ref;
2222
// napi_wrap(env, result, require, Require::finalize, nullptr, &ref);
23-
napi_add_finalizer(env, result, require, Require::finalize, nullptr, &ref);
23+
napi_add_finalizer(env, result, require, Require::Finalize, nullptr, &ref);
2424
return result;
2525
}
2626

27-
Require* Require::init(napi_env env, std::string& path, std::string& tilde) {
27+
Require* Require::Init(napi_env env, std::string& path, std::string& tilde) {
2828
napi_value global;
2929
napi_get_global(env, &global);
3030

3131
Require* out = nullptr;
32-
napi_value require = createRequire(env, path, tilde, &out);
32+
napi_value require = CreateRequire(env, path, tilde, &out);
3333

3434
napi_set_named_property(env, global, "require", require);
3535

3636
return out;
3737
}
3838

39-
void Require::finalize(napi_env env, void* data, void* hint) {
39+
void Require::Finalize(napi_env env, void* data, void* hint) {
4040
Require* require = (Require*)data;
4141
delete require;
4242
}
4343

44-
std::string Require::resolve(std::string& spec) {
44+
std::string Require::Resolve(std::string& spec) {
4545
if (spec.find("/") == 0) {
4646
return spec;
4747
}
@@ -85,8 +85,8 @@ void finalize_dlobject(napi_env env, void* finalize_data, void* finalize_hint) {
8585

8686
typedef napi_value napi_module_register_fn(napi_env env, napi_value exports);
8787

88-
napi_value Require::require(napi_env env, std::string& spec) {
89-
std::string path = resolve(spec);
88+
napi_value Require::RequireModule(napi_env env, std::string spec) {
89+
std::string path = Resolve(spec);
9090

9191
if (path.ends_with(".node") || path.ends_with(".dylib") ||
9292
path.ends_with(".so")) {
@@ -181,7 +181,7 @@ napi_value Require::require(napi_env env, std::string& spec) {
181181

182182
std::string dirname = path.substr(0, path.rfind("/"));
183183

184-
require = Require::createRequire(env, dirname, tilde);
184+
require = Require::CreateRequire(env, dirname, tilde);
185185

186186
napi_create_string_utf8(env, dirname.c_str(), NAPI_AUTO_LENGTH, &__dirname);
187187

@@ -210,12 +210,12 @@ napi_value Require::require(napi_env env, std::string& spec) {
210210
return exports;
211211
}
212212

213-
napi_value Require::requireCallback(napi_env env, napi_callback_info cbinfo) {
213+
napi_value Require::RequireCallback(napi_env env, napi_callback_info cbinfo) {
214214
napi_value arg;
215215
Require* require;
216216
size_t argc = 1;
217217
napi_get_cb_info(env, cbinfo, &argc, &arg, nullptr, (void**)&require);
218218
std::string spec = getStringValue(env, arg);
219-
napi_value res = require->require(env, spec);
219+
napi_value res = require->RequireModule(env, spec);
220220
return res;
221221
}

NativeScript/runtime/Require.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@ class Require {
88
public:
99
Require(std::string path, std::string tilde) : path(path), tilde(tilde) {}
1010

11-
static Require *init(napi_env env, std::string &path, std::string &tilde);
11+
static Require *Init(napi_env env, std::string &path, std::string &tilde);
1212

13-
static napi_value createRequire(napi_env env, std::string &path,
13+
static napi_value CreateRequire(napi_env env, std::string &path,
1414
std::string &tilde,
1515
Require **pRequire = nullptr);
1616

17-
static void finalize(napi_env env, void *data, void *hint);
17+
static void Finalize(napi_env env, void *data, void *hint);
1818

19-
std::string resolve(std::string &spec);
20-
napi_value require(napi_env env, std::string &spec);
19+
std::string Resolve(std::string &spec);
20+
napi_value RequireModule(napi_env env, std::string spec);
2121

22-
static napi_value requireCallback(napi_env env, napi_callback_info cbinfo);
22+
static napi_value RequireCallback(napi_env env, napi_callback_info cbinfo);
2323

2424
std::string path;
2525
std::string tilde;

0 commit comments

Comments
 (0)