Skip to content

Commit 537a6f6

Browse files
committed
part 37
1 parent d2bfb90 commit 537a6f6

File tree

39 files changed

+145
-167
lines changed

39 files changed

+145
-167
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,7 @@
1414
.cxx
1515
local.properties
1616
dist
17+
dist_v8
18+
dist_quickjs
19+
dist_hermes
20+
dist_jsc

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Android Runtime for NativeScript
1+
# Node-API Android Runtime for NativeScript
22
[![Build Status](https://travis-ci.org/NativeScript/android-runtime.svg?branch=master)](https://travis-ci.org/NativeScript/android-runtime)
33

44
Contains the source code for the NativeScript's Android Runtime. [NativeScript](https://www.nativescript.org/) is a framework which enables developers to write truly native mobile applications for Android and iOS using JavaScript and CSS. Each mobile platform has its own ecosystem and offers completely different development tools and language(s) - Java for Android and Objective C (Swift) for iOS. In order to translate JavaScript code to the corresponding native APIs some kind of proxy mechanism is needed. This is exactly what the "Runtime" parts of NativeScript are responsible for. The Android Runtime may be thought of as "The Bridge" between the JavaScript and Android worlds. A NativeScript application for Android is a standard native package (apk) which besides the JavaScript files embed the runtime as well.
@@ -77,14 +77,15 @@ Note: You might need to run the Android Studio from the command line in order to
7777
Windows:
7878

7979
```Shell
80-
gradlew
80+
gradlew -Pengine=V8
8181
```
8282

8383
Mac/Linux:
8484

8585
```Shell
86-
./gradlew
86+
./gradlew -Pengine=V8
8787
```
88+
You can pass in `QUICKJS`, `HERMES`, `JSC` or `V8` to compile the runtime with the respective JS engine.
8889

8990
* The build process includes building of the runtime package (both optimized and with unstripped v8 symbol table), as well as all supplementary tools used for the android builds: metadata-generator, binding-generator, metadata-generator, static-binding-generator
9091
* The result of the build will be in the dist folder.

build.gradle

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,16 @@ if (hasNdkVersion) {
2222
println "Using NDK version " + ndkVersion
2323
}
2424

25+
def hasEngine = project.hasProperty("engine");
26+
2527
def isWinOs = System.properties['os.name'].toLowerCase().contains('windows')
2628
def pVersion = "no package version was provided by build.gradle build"
2729
def arVersion = "no commit sha was provided by build.gradle build"
2830
def generateRegularRuntimePackage = !project.hasProperty("skipUnoptimized")
2931

30-
def DIST_PATH = "$rootDir/dist"
32+
def jsEngine = engine ?: 'v8'
33+
34+
def DIST_PATH = "$rootDir/dist_${jsEngine.toLowerCase()}"
3135
def TEST_APP_PATH = "$rootDir/test-app"
3236
def BUILD_TOOLS_PATH = "$TEST_APP_PATH/build-tools"
3337
def DIST_FRAMEWORK_PATH = "$DIST_PATH/framework"
@@ -198,7 +202,13 @@ def getAssembleReleaseBuildArguments = { ->
198202
if (hasNdkVersion) {
199203
arguments.add("-PndkVersion="+ndkVersion)
200204
}
205+
206+
if (hasEngine) {
207+
arguments.add("-Pengine=${engine}")
208+
}
209+
201210
arguments += ["--warning-mode", "all"]
211+
202212
return arguments
203213
}
204214

test-app/app/src/main/assets/app/Infrastructure/Jasmine/jasmine-2.0.1/boot.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,18 @@ var TerminalReporter = require('../jasmine-reporters/terminal_reporter').Termina
4040
},
4141

4242
it: function(desc, func) {
43-
if (func.length > 0) {
44-
return env.it(desc, func);
45-
} else {
46-
return env.it(desc, function(done) {
47-
func();
48-
__ns__setTimeout(() => {
49-
done();
50-
}, 1)
51-
52-
})
53-
}
43+
return env.it(desc, func);
44+
// if (func.length > 0) {
45+
// return env.it(desc, func);
46+
// } else {
47+
// return env.it(desc, function(done) {
48+
// func();
49+
// __ns__setTimeout(() => {
50+
// done();
51+
// }, 1)
52+
//
53+
// })
54+
// }
5455
},
5556

5657
xit: function(desc, func) {

test-app/app/src/main/assets/app/MyActivity.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@ var MyActivity = (function (_super) {
2727
_super.apply(this, arguments);
2828
}
2929
MyActivity.prototype.onCreate = function (bundle) {
30-
console.log("BUNDLE", typeof bundle);
3130
_super.prototype.onCreate.call(this, bundle);
32-
// require('./tests/testsWithContext').run(this);
31+
// require('./tests/testsWithContext').run(this);
3332
//run jasmine
3433

3534
// execute();
@@ -59,9 +58,7 @@ var MyActivity = (function (_super) {
5958
button.setOnClickListener(
6059
new android.view.View.OnClickListener("AppClickListener", {
6160
onClick: function () {
62-
const now = performance.now();
6361
button.setBackgroundColor(colors[taps % colors.length]);
64-
console.log(performance.now() - now);
6562
taps++;
6663
},
6764
})

test-app/app/src/main/assets/app/mainpage.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ __approot = __dirname.substr(0, __dirname.length - 4);
1717

1818

1919
// PASSING TESTS
20-
//require("./tests/testMetadata")
20+
//require("./tests/testMetadata");
2121
//require("./tests/testWeakRef");
2222
//require("./tests/testMethodResolution");
2323
//require("./tests/testMethodResolutionWithNulls");
@@ -53,16 +53,25 @@ __approot = __dirname.substr(0, __dirname.length - 4);
5353
//require("./tests/discardedExceptionsTest");
5454
//require("./tests/requireExceptionTests");
5555
//require("./tests/exceptionHandlingTests");
56+
5657
//require("./tests/tests");
58+
5759
//require("./tests/testsForTypescript");
60+
5861
//require("./tests/testGC");
62+
5963
//require("./tests/testReleaseNativeCounterpart");
64+
6065
//require("./tests/testArrays");
66+
6167
//require("./tests/testJniReferenceLeak");
68+
6269
//var shared = require("./shared");
6370
//shared.runRequireTests();
6471
//shared.runRuntimeTests();
72+
6573
//shared.runWorkerTests();
74+
6675
//require("./tests/dispatchCallbacksOnUiThreadTests");
6776
//require("./tests/testsMemoryManagement");
6877

@@ -74,5 +83,5 @@ __approot = __dirname.substr(0, __dirname.length - 4);
7483
//require('./tests/testURLImpl.js');
7584
//require('./tests/testURLSearchParamsImpl.js');
7685

77-
//require("./tests/testMultithreadedJavascript"); //FAILS
86+
require("./tests/testMultithreadedJavascript"); //FAILS
7887

test-app/app/src/main/assets/app/tests/testMultithreadedJavascript.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@ describe("Test multithreaded javascript ", () => {
22
it("Should execute callbacks on specified native thread", done => {
33

44
const currentThreadId = java.lang.Thread.currentThread().getId();
5-
console.log("TEST BEGIN!!",currentThreadId);
65
new java.lang.Thread(new java.lang.Runnable({
76
run() {
87
const threadId = java.lang.Thread.currentThread().getId();
98
expect(threadId).not.toEqual(currentThreadId);
109

1110
const mainHandler = new android.os.Handler(android.os.Looper.getMainLooper());
12-
// done() must called on the main thread to ensure that jasmine tests
13-
// continue executing there
11+
// // done() must called on the main thread to ensure that jasmine tests
12+
// // continue executing there
1413
mainHandler.post(new java.lang.Runnable({
1514
run: done
1615
}));

test-app/app/src/main/java/com/tns/LogcatLogger.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
public final class LogcatLogger implements Logger {
77
private final static String DEFAULT_LOG_TAG = "TNS.Java";
88

9-
private boolean enabled = true;
9+
private boolean enabled = false;
1010

1111
public LogcatLogger(Context context) {
1212
this.initLogging(context);
@@ -33,11 +33,9 @@ public final void write(String tag, String msg) {
3333
}
3434

3535
private void initLogging(Context context) {
36-
// setEnabled(true);
3736
boolean isDebuggableApp = Util.isDebuggableApp(context);
3837
if (isDebuggableApp) {
3938
String verboseLoggingProp = Util.readSystemProperty("nativescript.verbose.logging");
40-
4139
if (Util.isPositive(verboseLoggingProp)) {
4240
setEnabled(true);
4341
}

test-app/app/src/main/java/com/tns/RuntimeHelper.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,6 @@ public static Runtime initRuntime(Context context) {
7474

7575
Logger logger = new LogcatLogger(context);
7676

77-
logger.setEnabled(true);
78-
7977
Runtime.nativeLibraryLoaded = true;
8078
Runtime runtime = null;
8179
boolean showErrorIntent = hasErrorIntent(context);

test-app/runtime/build.gradle

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
apply plugin: 'com.android.library'
22

33
// can be: "V8", "JSC", "HERMES", "QUICKJS"
4-
def jsEngine = "V8"
5-
if (project.hasProperty("engine")) {
6-
jsEngine = engine;
4+
def jsEngine = "HERMES"
5+
def hasEngine = project.hasProperty("engine")
6+
if (hasEngine) {
7+
jsEngine = engine
78
}
89

10+
printf("Compiling NativeScript with %s.\n", jsEngine)
11+
912
def optimized = project.hasProperty("optimized")
1013
if (optimized) {
1114
println "Optimized build triggered."
@@ -171,24 +174,21 @@ android {
171174
packagingOptions {
172175
exclude "**/libjsc.so"
173176
exclude "**/libv8.so"
174-
exclude "**/libjsi.so"
175177
exclude '**/libc++_shared.so'
176-
exclude "**/libfbjni.so"
178+
exclude '**/libfbjni.so'
177179
}
178180
} else if (jsEngine == "JSC") {
179181
packagingOptions {
180182
exclude "**/libv8android.so"
181183
exclude "**/libhermes.so"
182184
exclude "**/libjsi.so"
183-
exclude "**/libfbjni.so"
184185
}
185186
} else if (jsEngine == "QUICKJS") {
186187
packagingOptions {
187188
exclude "**/libv8.so"
188189
exclude "**/libhermes.so"
189190
exclude "**/libjsc.so"
190191
exclude "**/libjsi.so"
191-
exclude "**/libfbjni.so"
192192
}
193193
}
194194
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//
2+
// Created by Ammar Ahmed on 17/01/2025.
3+
//
4+
5+
#ifndef TEST_APP_JSR_COMMON_H
6+
#define TEST_APP_JSR_COMMON_H
7+
8+
#include "js_native_api.h"
9+
10+
napi_status js_create_runtime(napi_runtime* runtime);
11+
napi_status js_create_napi_env(napi_env* env, napi_runtime runtime);
12+
napi_status js_set_runtime_flags(const char* flags);
13+
napi_status js_lock_env(napi_env env);
14+
napi_status js_unlock_env(napi_env env);
15+
napi_status js_free_napi_env(napi_env env);
16+
napi_status js_free_runtime(napi_runtime runtime);
17+
napi_status js_execute_script(napi_env env,
18+
napi_value script,
19+
const char *file,
20+
napi_value *result);
21+
22+
napi_status js_execute_pending_jobs(napi_env env);
23+
24+
napi_status js_get_engine_ptr(napi_env env, int64_t *engine_ptr);
25+
napi_status js_adjust_external_memory(napi_env env, int64_t changeInBytes, int64_t* externalMemory);
26+
napi_status js_cache_script(napi_env env, const char *source, const char *file);
27+
napi_status js_run_cached_script(napi_env env, const char * file, napi_value script, void* cache, napi_value *result);
28+
29+
#endif //TEST_APP_JSR_COMMON_H

test-app/runtime/src/main/cpp/napi/hermes/include/node-api/js_native_api_types.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ typedef uint16_t char16_t;
2222
// JSVM API types are all opaque pointers for ABI stability
2323
// typedef undefined structs instead of void* for compile time type safety
2424
typedef struct napi_env__* napi_env;
25+
typedef struct NapiRuntime *napi_runtime;
2526
typedef struct napi_value__* napi_value;
2627
typedef struct napi_ref__* napi_ref;
2728
typedef struct napi_handle_scope__* napi_handle_scope;

test-app/runtime/src/main/cpp/napi/hermes/jsr.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ napi_status js_lock_env(napi_env env) {
2828
if (itFound == JSR::env_to_jsr_cache.end()) {
2929
return napi_invalid_arg;
3030
}
31-
3231
itFound->second->lock();
3332

3433
return napi_ok;
@@ -39,7 +38,6 @@ napi_status js_unlock_env(napi_env env) {
3938
if (itFound == JSR::env_to_jsr_cache.end()) {
4039
return napi_invalid_arg;
4140
}
42-
4341
itFound->second->unlock();
4442

4543
return napi_ok;
@@ -51,6 +49,10 @@ napi_status js_create_napi_env(napi_env* env, napi_runtime runtime) {
5149
return runtime->hermes->rt->createNapiEnv( env);
5250
}
5351

52+
napi_status js_set_runtime_flags(const char* flags) {
53+
return napi_ok;
54+
}
55+
5456
napi_status js_free_napi_env(napi_env env) {
5557
return napi_ok;
5658
}

test-app/runtime/src/main/cpp/napi/hermes/jsr.h

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
#include "hermes/hermes.h"
99
#include "hermes/hermes_api.h"
1010
#include "jsi/threadsafe.h"
11-
12-
typedef struct NapiRuntime *napi_runtime;
11+
#include "jsr_common.h"
1312

1413
class JSR {
1514
public:
@@ -27,23 +26,6 @@ class JSR {
2726
static std::unordered_map<napi_env, JSR *> env_to_jsr_cache;
2827
};
2928

30-
napi_status js_create_runtime(napi_runtime* runtime);
31-
napi_status js_lock_env(napi_env env);
32-
napi_status js_unlock_env(napi_env env);
33-
napi_status js_create_napi_env(napi_env* env, napi_runtime runtime);
34-
napi_status js_free_napi_env(napi_env env);
35-
napi_status js_free_runtime(napi_runtime runtime);
36-
napi_status js_execute_script(napi_env env,
37-
napi_value script,
38-
const char *file,
39-
napi_value *result);
40-
41-
napi_status js_execute_pending_jobs(napi_env env);
42-
napi_status js_get_engine_ptr(napi_env env, int64_t *engine_ptr);
43-
napi_status js_adjust_external_memory(napi_env env, int64_t changeInBytes, int64_t* externalMemory);
44-
napi_status js_cache_script(napi_env env, const char *source, const char *file);
45-
napi_status js_run_cached_script(napi_env env, const char * file, napi_value script, void* cache, napi_value *result);
46-
4729
class NapiScope {
4830
public:
4931
explicit NapiScope(napi_env env)
@@ -64,7 +46,5 @@ class NapiScope {
6446
};
6547

6648
#define JSEnterScope
67-
#define JSEnter
68-
#define JSLeave
6949

7050
#endif //TEST_APP_JSR_H

test-app/runtime/src/main/cpp/napi/jsc/jsr.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ napi_status js_create_napi_env(napi_env* env, napi_runtime runtime) {
2828

2929
}
3030

31+
napi_status js_set_runtime_flags(const char* flags) {
32+
return napi_ok;
33+
}
34+
3135
napi_status js_lock_env(napi_env env) {
3236
return napi_ok;
3337
}

test-app/runtime/src/main/cpp/napi/jsc/jsr.h

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,11 @@
55
#ifndef TEST_APP_JSR_H
66
#define TEST_APP_JSR_H
77

8+
#include "jsr_common.h"
89
#include "jsc-api.h"
910

1011
typedef struct NapiRuntime *napi_runtime;
1112

12-
napi_status js_create_runtime(napi_runtime* runtime);
13-
napi_status js_create_napi_env(napi_env* env, napi_runtime runtime);
14-
napi_status js_lock_env(napi_env env);
15-
napi_status js_unlock_env(napi_env env);
16-
napi_status js_free_napi_env(napi_env env);
17-
napi_status js_free_runtime(napi_runtime runtime);
18-
napi_status js_execute_script(napi_env env,
19-
napi_value script,
20-
const char *file,
21-
napi_value *result);
22-
23-
napi_status js_execute_pending_jobs(napi_env env);
24-
napi_status js_get_engine_ptr(napi_env env, int64_t *engine_ptr);
25-
napi_status js_adjust_external_memory(napi_env env, int64_t changeInBytes, int64_t* externalMemory);
26-
napi_status js_cache_script(napi_env env, const char *source, const char *file);
27-
napi_status js_run_cached_script(napi_env env, const char * file, napi_value script, void* cache, napi_value *result);
28-
2913
class NapiScope {
3014
public:
3115
explicit NapiScope(napi_env env)

0 commit comments

Comments
 (0)