Skip to content
Merged
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
3 changes: 2 additions & 1 deletion test-app/app/src/main/assets/app/mainpage.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,5 @@ require("./tests/byte-buffer-test");
require("./tests/dex-interface-implementation");
require("./tests/testInterfaceImplementation");
require("./tests/testRuntimeImplementedAPIs");
require("./tests/testsInstanceOfOperator");
require("./tests/testsInstanceOfOperator");
require("./tests/testReleaseNativeCounterpart");
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
describe("Test native counterpart release", function () {

var myCustomEquality = function(first, second) {
return first == second;
};

beforeEach(function() {
jasmine.addCustomEqualityTester(myCustomEquality);
});

it("Calling a method on a released object should throw exception", function () {

var errorMessage = "";

try{
var object1 = new java.lang.Object();

global.__releaseNativeCounterpart(object1);

object1.toString();
} catch(e){
errorMessage = e.message;
}

expect(errorMessage).toBe("Failed calling toString on a java/lang/Object instance. The JavaScript instance no longer has available Java instance counterpart.");
});

it("Calling release on a non native object should throw exception", function () {

var errorMessage = "";

try{
var object2 = {prop: "test"};
global.__releaseNativeCounterpart(object2);
} catch(e){
errorMessage = e.message;
}

expect(errorMessage).toBe("Trying to release a non native object!");
});


it("Calling release on a non native primitive type should throw exception", function () {

var errorMessage = "";

try{
global.__releaseNativeCounterpart(42);
} catch(e){
errorMessage = e.message;
}

expect(errorMessage).toBe("Argument is not an object!");
});

it("Calling the __releaseNativeCounterpart function with 0 arguments should throw exception", function(){
var errorMessage = "";

try{
global.__releaseNativeCounterpart();
} catch(e){
errorMessage = e.message;
}

expect(errorMessage).toBe("Unexpected arguments count!");
});

it("Calling the __releaseNativeCounterpart function with more than 1 arguments should throw exception", function(){
var errorMessage = "";

try{
global.__releaseNativeCounterpart({},{});
} catch(e){
errorMessage = e.message;
}

expect(errorMessage).toBe("Unexpected arguments count!");
});


});
9 changes: 8 additions & 1 deletion test-app/runtime/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ android {
arguments.push("-DOPTIMIZED_WITH_INSPECTOR_BUILD=true")
}

if(useCCache) {
if (useCCache) {
arguments.push("-DUSE_CCACHE=true")
}

Expand Down Expand Up @@ -104,6 +104,8 @@ allprojects {

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
testImplementation 'junit:junit:4.12'
testImplementation 'org.mockito:mockito-core:1.10.19'
}

tasks.whenTaskAdded { task ->
Expand All @@ -112,6 +114,11 @@ tasks.whenTaskAdded { task ->
setRuntimeCommit.dependsOn(setPackageVersion)
task.dependsOn(setRuntimeCommit)
}

if(taskName.contains("bundleReleaseAar")){
task.dependsOn("testDebugUnitTest")
}

if (taskName.contains("Strip")) {
task.finalizedBy(revertVersionFile)
}
Expand Down
45 changes: 42 additions & 3 deletions test-app/runtime/src/main/cpp/CallbackHandlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,9 @@ CallbackHandlers::GetImplementedInterfaces(JEnv &env, const Local<Object> &imple
auto interfacesArr = prop->ToObject(isolate);

auto context = isolate->GetCurrentContext();
int length = interfacesArr->Get(v8::String::NewFromUtf8(isolate, "length"))->ToObject(isolate)->Uint32Value(context).ToChecked();
int length = interfacesArr->Get(
v8::String::NewFromUtf8(isolate, "length"))->ToObject(isolate)->Uint32Value(
context).ToChecked();

if (length > 0) {
for (int i = 0; i < length; i++) {
Expand Down Expand Up @@ -670,6 +672,38 @@ void CallbackHandlers::TimeCallback(const v8::FunctionCallbackInfo<v8::Value> &a
args.GetReturnValue().Set(duration);
}

void CallbackHandlers::ReleaseNativeCounterpartCallback(
const v8::FunctionCallbackInfo<v8::Value> &info) {
try {
SET_PROFILER_FRAME();

validateProvidedArgumentsLength(info, 1);

auto isolate = info.GetIsolate();
Handle<Object> obj = info[0].As<Object>();

auto runtime = Runtime::GetRuntime(isolate);
auto objectManager = runtime->GetObjectManager();
objectManager->ReleaseNativeCounterpart(obj);
} catch (NativeScriptException &e) {
e.ReThrowToV8();
} catch (std::exception e) {
stringstream ss;
ss << "Error: c++ exception: " << e.what() << endl;
NativeScriptException nsEx(ss.str());
nsEx.ReThrowToV8();
} catch (...) {
NativeScriptException nsEx(std::string("Error: c++ exception!"));
nsEx.ReThrowToV8();
}
}

void CallbackHandlers::validateProvidedArgumentsLength(const v8::FunctionCallbackInfo<v8::Value> &args, int expectedSize) {
if(args.Length() != expectedSize){
throw NativeScriptException("Unexpected arguments count!");
}
}

void CallbackHandlers::DumpReferenceTablesMethodCallback(
const v8::FunctionCallbackInfo<v8::Value> &args) {
DumpReferenceTablesMethod();
Expand Down Expand Up @@ -887,8 +921,11 @@ void CallbackHandlers::NewThreadCallback(const v8::FunctionCallbackInfo<v8::Valu
auto thiz = args.This();
auto isolate = thiz->GetIsolate();

auto currentExecutingScriptName = StackTrace::CurrentStackTrace(isolate, 1, StackTrace::kScriptName)->GetFrame(isolate, 0)->GetScriptName();
auto currentExecutingScriptNameStr = ArgConverter::ConvertToString(currentExecutingScriptName);
auto currentExecutingScriptName = StackTrace::CurrentStackTrace(isolate, 1,
StackTrace::kScriptName)->GetFrame(
isolate, 0)->GetScriptName();
auto currentExecutingScriptNameStr = ArgConverter::ConvertToString(
currentExecutingScriptName);
auto lastForwardSlash = currentExecutingScriptNameStr.find_last_of("/");
auto currentDir = currentExecutingScriptNameStr.substr(0, lastForwardSlash + 1);
string fileSchema("file://");
Expand Down Expand Up @@ -1444,6 +1481,8 @@ jmethodID CallbackHandlers::ENABLE_VERBOSE_LOGGING_METHOD_ID = nullptr;
jmethodID CallbackHandlers::DISABLE_VERBOSE_LOGGING_METHOD_ID = nullptr;
jmethodID CallbackHandlers::INIT_WORKER_METHOD_ID = nullptr;



NumericCasts CallbackHandlers::castFunctions;
ArrayElementAccessor CallbackHandlers::arrayElementAccessor;
FieldAccessor CallbackHandlers::fieldAccessor;
6 changes: 6 additions & 0 deletions test-app/runtime/src/main/cpp/CallbackHandlers.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ namespace tns {
static void
DisableVerboseLoggingMethodCallback(const v8::FunctionCallbackInfo<v8::Value> &args);

static void ReleaseNativeCounterpartCallback(const v8::FunctionCallbackInfo<v8::Value> &info);

static v8::Local<v8::Object> FindClass(v8::Isolate *isolate, const std::string &className);

static void NewThreadCallback(const v8::FunctionCallbackInfo<v8::Value> &args);
Expand Down Expand Up @@ -194,6 +196,8 @@ namespace tns {
*/
static jobjectArray GetJavaStringArray(JEnv &env, int length);

static void validateProvidedArgumentsLength(const v8::FunctionCallbackInfo<v8::Value> &args, int expectedSize);

static short MAX_JAVA_STRING_ARRAY_LENGTH;

static jclass RUNTIME_CLASS;
Expand Down Expand Up @@ -235,6 +239,8 @@ namespace tns {
jfieldID _fieldID;
jobject _runtime;
};


};
}

Expand Down
Loading