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
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,87 @@ describe("Test Constructor Resolution When Nulls are passed", function () {

expect(result).toMatch(/\S*.DummyClass and \S*.Object/);
});
});

// Each spec reads .null itself: the suites above read it at require time, and
// these must hold whether or not anything else in this runtime has read it.
describe("Typed null literals leave their class untouched", function () {
var dummyClass = new com.tns.tests.DummyClass();

it("keeps String.valueOf working after String.null is read", function () {
var Described = java.lang.Object.extend({
toString: function () {
return "described in JS";
}
});
void java.lang.String.null;

expect(java.lang.String.valueOf(new Described())).toBe("described in JS");
expect(java.lang.String.valueOf(new java.io.File("/typed-null"))).toBe("/typed-null");
expect(java.lang.String.valueOf(42)).toBe("42");
expect(java.lang.String.valueOf(true)).toBe("true");
});

it("keeps Integer.valueOf and an enum's valueOf working after their .null is read", function () {
void java.lang.Integer.null;
void java.util.concurrent.TimeUnit.null;

expect(java.lang.Integer.valueOf(5).intValue()).toBe(5);
expect(java.util.concurrent.TimeUnit.valueOf("SECONDS").name()).toBe("SECONDS");
});

it("does not accept the class itself as a typed null once its .null is read", function () {
void java.io.File.null;

expect(function () {
java.util.Objects.toString(java.io.File, "fallback");
}).toThrowError(/Cannot convert object/);
});

it("returns a cached object, distinct from the class, whose valueOf is null", function () {
var fileNull = java.io.File.null;

expect(fileNull).not.toBe(java.io.File);
expect(typeof fileNull).toBe("object");
expect(java.io.File.null).toBe(fileNull);
expect(fileNull.valueOf()).toBeNull();
expect(Object.keys(fileNull)).toEqual([]);
});

it("converts a typed null to a Java null", function () {
expect(java.util.Objects.isNull(java.io.File.null)).toBe(true);
expect(java.util.Objects.toString(java.lang.Object.null, "fallback")).toBe("fallback");
expect(java.lang.String.valueOf(java.lang.String.null)).toBe("null");
});

it("types a JS-extended class's .null as its Java base class", function () {
var Extended = com.tns.tests.DummyClass.extend({});
var extendedNull = Extended.null;

expect(extendedNull).not.toBe(com.tns.tests.DummyClass.null);
expect(Extended.null).toBe(extendedNull);
expect(extendedNull.valueOf()).toBeNull();
expect(dummyClass.methodWithOverloadsWithOneArgument(extendedNull)).toContain("DummyClass");
});

it("gives a worker typed nulls of its own that leave its classes untouched", function (done) {
var worker = new Worker("./typedNullWorker.js");
worker.onmessage = function (msg) {
expect(msg.data).toEqual({
valueOf: "7",
nullValueOf: null,
distinct: true,
overload: "java.io.File",
isNull: true
});
worker.terminate();
done();
};
worker.onerror = function (error) {
fail("worker failed: " + error.message);
worker.terminate();
done();
};
worker.postMessage("go");
});
});
10 changes: 10 additions & 0 deletions test-app/app/src/main/assets/app/tests/typedNullWorker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
onmessage = function () {
var stringNull = java.lang.String.null;
postMessage({
valueOf: java.lang.String.valueOf(7),
nullValueOf: stringNull.valueOf(),
distinct: stringNull !== java.lang.String,
overload: new com.tns.tests.DummyClass().methodWithOverloadsWithOneArgument(java.io.File.null),
isNull: java.util.Objects.isNull(stringNull)
});
};
35 changes: 24 additions & 11 deletions test-app/runtime/src/main/cpp/MetadataNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -454,18 +454,31 @@ void MetadataNode::NullObjectAccessorGetterCallback(const FunctionCallbackInfo<V
auto isolate = info.GetIsolate();

auto thiz = ResolveProxyReceiver(isolate, info.This(), "read", "null", false);
Local<Value> hiddenVal;
V8GetPrivateValue(isolate, thiz, V8StringConstants::GetNullNodePrivate(isolate), hiddenVal);
if (hiddenVal.IsEmpty()) {
auto context = isolate->GetCurrentContext();
auto nullObjectKey = V8StringConstants::GetNullObjectPrivate(isolate);

// A separate object, cached on the receiver so X.null is stable. Never tag
// or modify the constructor: anything carrying the null-node private
// converts to a Java null, and a constructor's valueOf may be a static
// Java method. Through the accessor a JS-extended class inherits, This is
// the subclass and Data the Java base class node, so that typed null is
// of the base class.
Local<Value> nullObject;
if (!thiz->GetPrivate(context, nullObjectKey).ToLocal(&nullObject) || !nullObject->IsObject()) {
auto node = reinterpret_cast<MetadataNode*>(info.Data().As<External>()->Value(v8::kExternalPointerTypeTagDefault));
V8SetPrivateValue(isolate, thiz, V8StringConstants::GetNullNodePrivate(isolate), External::New(isolate, node, v8::kExternalPointerTypeTagDefault));
auto funcTemplate = NewNonConstructor(isolate, MetadataNode::NullValueOfCallback);
auto context = isolate->GetCurrentContext();
thiz->Delete(context, V8StringConstants::GetValueOf(isolate));
thiz->Set(context, V8StringConstants::GetValueOf(isolate), funcTemplate->GetFunction(context).ToLocalChecked());
}

info.GetReturnValue().Set(thiz);
auto sentinel = Object::New(isolate);
V8SetPrivateValue(isolate, sentinel, V8StringConstants::GetNullNodePrivate(isolate), External::New(isolate, node, v8::kExternalPointerTypeTagDefault));
// Function::New does not cache its template in the context, so the
// function lives only as long as the sentinel.
auto valueOf = Function::New(context, MetadataNode::NullValueOfCallback, Local<Value>(), 0,
ConstructorBehavior::kThrow).ToLocalChecked();
sentinel->DefineOwnProperty(context, V8StringConstants::GetValueOf(isolate), valueOf,
PropertyAttribute::DontEnum).Check();
thiz->SetPrivate(context, nullObjectKey, sentinel).Check();
nullObject = sentinel;
}

info.GetReturnValue().Set(nullObject);
} catch (NativeScriptException& e) {
e.ReThrowToV8();
} catch (std::exception e) {
Expand Down
5 changes: 5 additions & 0 deletions test-app/runtime/src/main/cpp/V8StringConstants.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ Local<Private> V8StringConstants::GetCastMarkerPrivate(Isolate* isolate) {
return GetConstantsForIsolate(isolate)->CAST_MARKER_PRIVATE.Get(isolate);
}

Local<Private> V8StringConstants::GetNullObjectPrivate(Isolate* isolate) {
return GetConstantsForIsolate(isolate)->NULL_OBJECT_PRIVATE.Get(isolate);
}

Local<String> V8StringConstants::GetIsPrototypeImplementationObject(Isolate* isolate) {
auto consts = GetConstantsForIsolate(isolate);

Expand Down Expand Up @@ -149,6 +153,7 @@ const string V8StringConstants::EXTEND = "extend";
const string V8StringConstants::NULL_OBJECT = "null";
const string V8StringConstants::NULL_NODE_NAME = "nullNode";
const string V8StringConstants::CAST_MARKER = "t::cast";
const string V8StringConstants::NULL_OBJECT_KEY = "t::nullObject";
const string V8StringConstants::IS_PROTOTYPE_IMPLEMENTATION_OBJECT = "__isPrototypeImplementationObject";
const string V8StringConstants::NATIVE_EXCEPTION = "nativeException";
const string V8StringConstants::STACK_TRACE = "stackTrace";
Expand Down
6 changes: 6 additions & 0 deletions test-app/runtime/src/main/cpp/V8StringConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ class V8StringConstants {

static v8::Local<v8::Private> GetCastMarkerPrivate(v8::Isolate* isolate);

static v8::Local<v8::Private> GetNullObjectPrivate(v8::Isolate* isolate);

static const std::string CLASS_IMPLEMENTATION_OBJECT;
static const std::string DEBUG_NAME;
static const std::string EXTEND;
Expand All @@ -75,6 +77,7 @@ class V8StringConstants {
static const std::string DISCARDED_ERROR;
static const std::string IMPLEMENTATION_OBJECT;
static const std::string CAST_MARKER;
static const std::string NULL_OBJECT_KEY;

/*
* The structure contains Persistent handles to the most commonly used v8 String values;
Expand Down Expand Up @@ -163,6 +166,8 @@ class V8StringConstants {
NULL_NODE_PRIVATE.Reset(isolate, Private::ForApi(isolate, Local<String>::New(isolate, *NULL_NODE_NAME_PERSISTENT)));
str = String::NewFromUtf8(isolate, CAST_MARKER.c_str(), NewStringType::kInternalized).ToLocalChecked();
CAST_MARKER_PRIVATE.Reset(isolate, Private::ForApi(isolate, str));
str = String::NewFromUtf8(isolate, NULL_OBJECT_KEY.c_str(), NewStringType::kInternalized).ToLocalChecked();
NULL_OBJECT_PRIVATE.Reset(isolate, Private::ForApi(isolate, str));
};

~PerIsolateV8Constants() {
Expand Down Expand Up @@ -226,6 +231,7 @@ class V8StringConstants {
v8::Persistent<v8::String>* IMPLEMENTATION_OBJECT_PERSISTENT = nullptr;
v8::Global<v8::Private> NULL_NODE_PRIVATE;
v8::Global<v8::Private> CAST_MARKER_PRIVATE;
v8::Global<v8::Private> NULL_OBJECT_PRIVATE;
};

private:
Expand Down
Loading