-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNativeScript.mm
More file actions
179 lines (149 loc) · 4.9 KB
/
NativeScript.mm
File metadata and controls
179 lines (149 loc) · 4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include "NativeScript.h"
#include "Runtime.h"
#include "RuntimeConfig.h"
#include "ffi/NativeScriptException.h"
#include "ffi/Tasks.h"
#include "js_native_api.h"
#include "jsr.h"
using namespace nativescript;
@implementation Config
@synthesize BaseDir;
@synthesize ApplicationPath;
@synthesize MetadataPtr;
@synthesize IsDebug;
@end
@implementation NativeScript
extern char defaultStartOfMetadataSection __asm("section$start$__DATA$__TNSMetadata");
std::unique_ptr<Runtime> runtime_;
- (void)runScriptString:(NSString*)script runLoop:(BOOL)runLoop {
std::string cppScript = [script UTF8String];
runtime_->RunScript(cppScript);
if (runLoop) {
runtime_->RunLoop();
}
Tasks::Drain();
}
- (void)runMainApplication {
std::string spec = "./app";
try {
runtime_->RunModule(spec);
} catch (const NativeScriptException& e) {
std::string description = e.Description();
NSLog(@"NativeScript runMainApplication failed: %s", description.c_str());
@throw [NSException exceptionWithName:@"NativeScriptException"
reason:@(description.c_str())
userInfo:nil];
} catch (const std::exception& e) {
NSLog(@"NativeScript runMainApplication failed: %s", e.what());
@throw [NSException exceptionWithName:@"NativeScriptException" reason:@(e.what()) userInfo:nil];
}
runtime_->RunLoop();
Tasks::Drain();
}
- (bool)liveSync {
if (!runtime_) {
return false;
}
napi_env env = runtime_->GetEnv();
if (env == nullptr) {
return false;
}
bool didInvokeCallback = false;
NapiScope scope(env);
napi_value global;
if (napi_get_global(env, &global) != napi_ok) {
return false;
}
bool hasLiveSyncCallback = false;
if (napi_has_named_property(env, global, "__onLiveSync", &hasLiveSyncCallback) != napi_ok ||
!hasLiveSyncCallback) {
return false;
}
napi_value callback;
if (napi_get_named_property(env, global, "__onLiveSync", &callback) != napi_ok) {
return false;
}
napi_valuetype callbackType;
if (napi_typeof(env, callback, &callbackType) != napi_ok || callbackType != napi_function) {
return false;
}
napi_value context;
if (napi_create_object(env, &context) != napi_ok) {
return false;
}
napi_value result;
napi_status status = napi_call_function(env, global, callback, 1, &context, &result);
if (status != napi_ok) {
bool hasPendingException = false;
if (napi_is_exception_pending(env, &hasPendingException) == napi_ok && hasPendingException) {
napi_value error;
napi_get_and_clear_last_exception(env, &error);
}
return false;
}
didInvokeCallback = true;
if (result != nullptr) {
napi_valuetype returnType;
if (napi_typeof(env, result, &returnType) == napi_ok && returnType == napi_boolean) {
bool callbackResult = false;
if (napi_get_value_bool(env, result, &callbackResult) == napi_ok) {
didInvokeCallback = callbackResult;
}
}
}
Tasks::Drain();
return didInvokeCallback;
}
- (void)shutdownRuntime {
runtime_ = nullptr;
}
- (instancetype)initWithConfig:(Config*)config {
if (self = [super init]) {
RuntimeConfig.BaseDir = [config.BaseDir UTF8String];
if (config.ApplicationPath != nil) {
RuntimeConfig.ApplicationPath =
[[config.BaseDir stringByAppendingPathComponent:config.ApplicationPath] UTF8String];
} else {
RuntimeConfig.ApplicationPath =
[[config.BaseDir stringByAppendingPathComponent:@"app"] UTF8String];
}
RuntimeConfig.Arguments.clear();
if (config.Arguments != nullptr && config.ArgumentsCount > 0) {
RuntimeConfig.Arguments.reserve((size_t)config.ArgumentsCount);
for (int i = 0; i < config.ArgumentsCount; i++) {
const char* arg = config.Arguments[i];
RuntimeConfig.Arguments.emplace_back(arg != nullptr ? arg : "");
}
} else {
NSArray<NSString*>* processArgs = [[NSProcessInfo processInfo] arguments];
RuntimeConfig.Arguments.reserve((size_t)[processArgs count]);
for (NSString* arg in processArgs) {
RuntimeConfig.Arguments.emplace_back([arg UTF8String]);
}
}
if (config.MetadataPtr != nil) {
RuntimeConfig.MetadataPtr = [config MetadataPtr];
} else {
RuntimeConfig.MetadataPtr = &defaultStartOfMetadataSection;
}
RuntimeConfig.IsDebug = [config IsDebug];
RuntimeConfig.LogToSystemConsole = [config LogToSystemConsole];
RuntimeConfig.CustomLogCallback = [config CustomLogCallback];
runtime_ = std::make_unique<Runtime>();
// TODO: separate runtime init and measure the time
runtime_->Init();
if (RuntimeConfig.IsDebug) {
// TODO: Inspector for debugging
// runtime_->enableInspector();
}
}
return self;
}
- (instancetype)initializeWithConfig:(Config*)config {
return [self initWithConfig:config];
}
- (void)restartWithConfig:(Config*)config {
[self shutdownRuntime];
[self initWithConfig:config];
}
@end