diff --git a/test-app/app/src/main/assets/app/tests/testMethodResolutionWithNulls.js b/test-app/app/src/main/assets/app/tests/testMethodResolutionWithNulls.js index 983bdcd31..43fbb6d1a 100644 --- a/test-app/app/src/main/assets/app/tests/testMethodResolutionWithNulls.js +++ b/test-app/app/src/main/assets/app/tests/testMethodResolutionWithNulls.js @@ -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"); + }); }); \ No newline at end of file diff --git a/test-app/app/src/main/assets/app/tests/typedNullWorker.js b/test-app/app/src/main/assets/app/tests/typedNullWorker.js new file mode 100644 index 000000000..cc5d70362 --- /dev/null +++ b/test-app/app/src/main/assets/app/tests/typedNullWorker.js @@ -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) + }); +}; diff --git a/test-app/runtime/src/main/cpp/MetadataNode.cpp b/test-app/runtime/src/main/cpp/MetadataNode.cpp index 41e0e797e..f5a2be4e0 100644 --- a/test-app/runtime/src/main/cpp/MetadataNode.cpp +++ b/test-app/runtime/src/main/cpp/MetadataNode.cpp @@ -454,18 +454,31 @@ void MetadataNode::NullObjectAccessorGetterCallback(const FunctionCallbackInfo 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 nullObject; + if (!thiz->GetPrivate(context, nullObjectKey).ToLocal(&nullObject) || !nullObject->IsObject()) { auto node = reinterpret_cast(info.Data().As()->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(), 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) { diff --git a/test-app/runtime/src/main/cpp/V8StringConstants.cpp b/test-app/runtime/src/main/cpp/V8StringConstants.cpp index bd1818ea3..eeecac708 100644 --- a/test-app/runtime/src/main/cpp/V8StringConstants.cpp +++ b/test-app/runtime/src/main/cpp/V8StringConstants.cpp @@ -48,6 +48,10 @@ Local V8StringConstants::GetCastMarkerPrivate(Isolate* isolate) { return GetConstantsForIsolate(isolate)->CAST_MARKER_PRIVATE.Get(isolate); } +Local V8StringConstants::GetNullObjectPrivate(Isolate* isolate) { + return GetConstantsForIsolate(isolate)->NULL_OBJECT_PRIVATE.Get(isolate); +} + Local V8StringConstants::GetIsPrototypeImplementationObject(Isolate* isolate) { auto consts = GetConstantsForIsolate(isolate); @@ -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"; diff --git a/test-app/runtime/src/main/cpp/V8StringConstants.h b/test-app/runtime/src/main/cpp/V8StringConstants.h index 6539d5dd8..e6b0d5314 100644 --- a/test-app/runtime/src/main/cpp/V8StringConstants.h +++ b/test-app/runtime/src/main/cpp/V8StringConstants.h @@ -53,6 +53,8 @@ class V8StringConstants { static v8::Local GetCastMarkerPrivate(v8::Isolate* isolate); + static v8::Local GetNullObjectPrivate(v8::Isolate* isolate); + static const std::string CLASS_IMPLEMENTATION_OBJECT; static const std::string DEBUG_NAME; static const std::string EXTEND; @@ -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; @@ -163,6 +166,8 @@ class V8StringConstants { NULL_NODE_PRIVATE.Reset(isolate, Private::ForApi(isolate, Local::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() { @@ -226,6 +231,7 @@ class V8StringConstants { v8::Persistent* IMPLEMENTATION_OBJECT_PERSISTENT = nullptr; v8::Global NULL_NODE_PRIVATE; v8::Global CAST_MARKER_PRIVATE; + v8::Global NULL_OBJECT_PRIVATE; }; private: