Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions test-app/app/src/main/assets/app/mainpage.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ require("./tests/testGC");
require("./tests/testsMemoryManagement");
require("./tests/testFieldGetSet");
require("./tests/extendedClassesTests");
require("./tests/testDispatchOverrideWarnings");
require("./tests/extendClassNameTests");
require("./tests/testJniReferenceLeak");
require("./tests/testNativeModules");
Expand Down
167 changes: 167 additions & 0 deletions test-app/app/src/main/assets/app/tests/testDispatchOverrideWarnings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
// Java->JS calls look the method up on the instance on every call, so an own
// property of the instance or a method replaced after extend() is what runs. Debug
// builds keep calling it but log a warning once per class and method, since the
// iOS runtime binds the class's methods at extend time instead. Every extend() spec
// uses a class of its own: the warning names the class, and each is logged only once.
// Interfaces are named in full so the static binding generator implements them.
describe("Java->JS calls through a function other than the class's own", function () {
var LogcatUtil = com.tns.tests.LogcatUtil;

function runFromJava(runnable) {
new java.util.concurrent.FutureTask(runnable, null).run();
}

function warnings(className) {
return LogcatUtil.countJsWarnings(className, 'Java called "run" on ');
}

it("calls the class's own method without a warning", function () {
var runs = 0;
var C = java.lang.Object.extend("DispatchWarnOwnMethod", {
interfaces: [java.lang.Runnable],
run: function () { runs++; }
});
var instance = new C();

runFromJava(instance);
runFromJava(new C());

expect(runs).toBe(2);
expect(warnings("DispatchWarnOwnMethod")).toBe(0);
});

it("calls an instance's own property and warns", function () {
var classRuns = 0;
var ownRuns = 0;
var C = java.lang.Object.extend("DispatchWarnInstanceProperty", {
interfaces: [java.lang.Runnable],
run: function () { classRuns++; }
});
var instance = new C();
instance.run = function () { ownRuns++; };

runFromJava(instance);

expect(ownRuns).toBe(1);
expect(classRuns).toBe(0);
expect(warnings("DispatchWarnInstanceProperty")).toBe(1);
});

it("calls a TypeScript class field and warns", function () {
var fieldRuns = 0;
var FieldClass = (function (_super) {
__extends(DispatchWarnClassField, _super);
function DispatchWarnClassField() {
var _this = _super.call(this) || this;
_this.run = function () { fieldRuns++; };
return _this;
}
DispatchWarnClassField = __decorate([
Interfaces([java.lang.Runnable])
], DispatchWarnClassField);
return DispatchWarnClassField;
}(java.lang.Object));
var instance = new FieldClass();

runFromJava(instance);

expect(fieldRuns).toBe(1);
expect(warnings("DispatchWarnClassField")).toBe(1);
});

it("calls a method replaced after extend and warns", function () {
var originalRuns = 0;
var replacedRuns = 0;
var implementation = {
interfaces: [java.lang.Runnable],
run: function () { originalRuns++; }
};
var C = java.lang.Object.extend("DispatchWarnReplacedMethod", implementation);
var instance = new C();
implementation.run = function () { replacedRuns++; };

runFromJava(instance);

expect(replacedRuns).toBe(1);
expect(originalRuns).toBe(0);
expect(warnings("DispatchWarnReplacedMethod")).toBe(1);
});

it("calls a method added to the class's prototype after extend and warns", function () {
var originalRuns = 0;
var addedRuns = 0;
var C = java.lang.Object.extend("DispatchWarnPrototypeMethod", {
interfaces: [java.lang.Runnable],
run: function () { originalRuns++; }
});
C.prototype.run = function () { addedRuns++; };

runFromJava(new C());

expect(addedRuns).toBe(1);
expect(originalRuns).toBe(0);
expect(warnings("DispatchWarnPrototypeMethod")).toBe(1);
});

it("warns once however often and on however many instances the call repeats", function () {
var ownRuns = 0;
var C = java.lang.Object.extend("DispatchWarnRepeated", {
interfaces: [java.lang.Runnable],
run: function () {}
});
for (var i = 0; i < 3; i++) {
var instance = new C();
instance.run = function () { ownRuns++; };
runFromJava(instance);
runFromJava(instance);
}

expect(ownRuns).toBe(6);
expect(warnings("DispatchWarnRepeated")).toBe(1);
});

// Every `new Runnable({...})` shares one Java class, so the spec without an override
// runs first and both count only the warnings their own calls add.
it("calls an interface implementation's method without a warning", function () {
var runs = 0;
var runnable = new java.lang.Runnable({ run: function () { runs++; } });
var className = String(runnable.getClass().getName());
var before = warnings(className);

runFromJava(runnable);

expect(runs).toBe(1);
expect(warnings(className)).toBe(before);
});

it("calls an own property of an interface implementation and warns once", function () {
var implementationRuns = 0;
var ownRuns = 0;
var runnable = new java.lang.Runnable({ run: function () { implementationRuns++; } });
var className = String(runnable.getClass().getName());
var before = warnings(className);
runnable.run = function () { ownRuns++; };

runFromJava(runnable);
runFromJava(runnable);

expect(ownRuns).toBe(2);
expect(implementationRuns).toBe(0);
expect(warnings(className)).toBe(before + 1);
});

it("calls an instance's own property on an instance Java created and warns", function () {
var ownRuns = 0;
var C = java.lang.Object.extend("DispatchWarnJavaCreated", {
interfaces: [java.lang.Runnable],
run: function () {}
});
var instance = com.tns.tests.JavaCreated.newInstance(new C().getClass());
instance.run = function () { ownRuns++; };

runFromJava(instance);

expect(ownRuns).toBe(1);
expect(warnings("DispatchWarnJavaCreated")).toBe(1);
});
});
22 changes: 22 additions & 0 deletions test-app/app/src/main/java/com/tns/tests/LogcatUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,26 @@ public static boolean logcatContainsString(String str) throws Exception {

return output.contains(str);
}

// Lines this process logged under the JS tag at warning level or above that contain
// both strings. Leaves the log in place, so earlier lines of this process count too.
public static int countJsWarnings(String needle, String otherNeedle) throws Exception {
// -v brief prefixes each line with "W/JS ( 1234): "; --pid needs API 24.
String pidTag = "(" + android.os.Process.myPid() + "):";
Process process = Runtime.getRuntime().exec(new String[]{"logcat", "-d", "-v", "brief", "-s", "JS:W"});
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
int count = 0;
String line;
while ((line = bufferedReader.readLine()) != null) {
int prefixEnd = line.indexOf("):");
if (prefixEnd < 0 || !line.substring(0, prefixEnd + 2).replace(" ", "").endsWith(pidTag)) {
continue;
}
if (line.contains(needle) && line.contains(otherNeedle)) {
count++;
}
}
process.waitFor();
return count;
}
}
1 change: 1 addition & 0 deletions test-app/runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ add_library(
src/main/cpp/IsolateTracked.cpp
src/main/cpp/JEnv.cpp
src/main/cpp/DesugaredInterfaceCompanionClassNameResolver.cpp
src/main/cpp/DispatchOverrides.cpp
src/main/cpp/JType.cpp
src/main/cpp/JniSignatureParser.cpp
src/main/cpp/JsArgConverter.cpp
Expand Down
15 changes: 12 additions & 3 deletions test-app/runtime/src/main/cpp/CallbackHandlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,8 @@ bool CallbackHandlers::RegisterInstance(Isolate *isolate, const Local<Object> &j
bool isInterface,
const std::string &baseClassName,
uint64_t defaultOverrides,
const vector<string> *contentMemberNames) {
const vector<string> *contentMemberNames,
DispatchOverrides *dispatchOverrides) {
bool success;

DEBUG_WRITE("RegisterInstance called for '%s'", fullClassName.c_str());
Expand All @@ -236,7 +237,7 @@ bool CallbackHandlers::RegisterInstance(Isolate *isolate, const Local<Object> &j

int javaObjectID = objectManager->GenerateNewObjectID();

objectManager->Link(jsObject, javaObjectID, nullptr);
objectManager->Link(jsObject, javaObjectID, nullptr, dispatchOverrides);

// resolve constructor
const auto &mi = MethodCache::ResolveConstructorSignature(argWrapper, fullClassName,
Expand Down Expand Up @@ -1228,7 +1229,8 @@ vector<string> CallbackHandlers::GetTypeMetadata(const string &name, int index)

Local<Value> CallbackHandlers::CallJSMethod(Isolate *isolate, JNIEnv *_env,
const Local<Object> &jsObject, const Local<String> &methodName,
const JavaCallArgs &args) {
const JavaCallArgs &args, DispatchOverrides *dispatchOverrides,
int methodNameId) {
SET_PROFILER_FRAME();

JEnv env(_env);
Expand All @@ -1249,6 +1251,13 @@ Local<Value> CallbackHandlers::CallJSMethod(Isolate *isolate, JNIEnv *_env,
EscapableHandleScope handleScope(isolate);

auto jsMethod = method.As<Function>();
#ifdef APPLICATION_IN_DEBUG
if (dispatchOverrides != nullptr) {
dispatchOverrides->Check(isolate, context, jsObject, methodName, methodNameId, jsMethod);
} else {
DispatchOverrides::CheckInstanceProperty(isolate, context, jsObject, methodName, methodNameId, jsMethod);
}
#endif
int argc = args.count;

constexpr int kInlineArgs = 16;
Expand Down
6 changes: 4 additions & 2 deletions test-app/runtime/src/main/cpp/CallbackHandlers.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ namespace tns {
bool isInterface,
const std::string &baseClassName = std::string(),
uint64_t defaultOverrides = 0,
const std::vector<std::string> *contentMemberNames = nullptr);
const std::vector<std::string> *contentMemberNames = nullptr,
DispatchOverrides *dispatchOverrides = nullptr);

// Whether GetImplementedInterfaces would list any interface, without calling Java.
static bool HasImplementedInterfaces(v8::Isolate *isolate, const v8::Local<v8::Object> &implementationObject);
Expand Down Expand Up @@ -97,7 +98,8 @@ namespace tns {

static v8::Local<v8::Value>
CallJSMethod(v8::Isolate *isolate, JNIEnv *_env, const v8::Local<v8::Object> &jsObject,
const v8::Local<v8::String> &methodName, const JavaCallArgs &args);
const v8::Local<v8::String> &methodName, const JavaCallArgs &args,
DispatchOverrides *dispatchOverrides = nullptr, int methodNameId = -1);

static v8::Local<v8::Value>
GetJavaField(v8::Isolate *isolate, const v8::Local<v8::Object> &caller,
Expand Down
Loading