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