23
23
24
24
#include " NativeScript.h"
25
25
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 {
50
27
28
+ Runtime::Runtime () {
51
29
js_set_runtime_flags (" " );
52
30
53
31
js_create_runtime (&runtime);
@@ -87,8 +65,7 @@ Runtime::Runtime(std::string& mainPath) : mainPath(mainPath) {
87
65
)" ;
88
66
89
67
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);
92
69
napi_run_script (env, compatScript, &result);
93
70
94
71
Console::init (env);
@@ -97,7 +74,7 @@ Runtime::Runtime(std::string& mainPath) : mainPath(mainPath) {
97
74
Timers::init (env);
98
75
#endif // __APPLE__
99
76
100
- require = Require::init (env, mainPath, mainPath );
77
+ require = Require::init (env, RuntimeConfig. BaseDir , RuntimeConfig. BaseDir );
101
78
102
79
const char * metadata_path = std::getenv (" NS_METADATA_PATH" );
103
80
objc_bridge_init (env, metadata_path, RuntimeConfig.MetadataPtr );
@@ -110,96 +87,28 @@ Runtime::Runtime(std::string& mainPath) : mainPath(mainPath) {
110
87
napi_close_handle_scope (env, scope);
111
88
}
112
89
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) {
120
91
NapiScope scope (env);
121
92
122
93
napi_value script, result;
123
94
napi_create_string_utf8 (env, scriptSrc.c_str (), scriptSrc.length (), &script);
124
95
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 ;
156
96
}
157
97
158
- int Runtime::executeBytecode ( const uint8_t * data, size_t size ) {
98
+ napi_value Runtime::RunModule (std::string spec ) {
159
99
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);
168
102
}
169
103
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 (" ./" );
188
106
}
189
107
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 () {
200
109
CFRunLoopRunInMode (kCFRunLoopDefaultMode , 0 , true );
201
110
}
202
111
203
- } // namespace charon
112
+ } // namespace nativescript
204
113
205
114
#endif // ENABLE_JS_RUNTIME
0 commit comments