Safe calls work for native properties

#KT-2209 fixed
This commit is contained in:
pTalanov
2012-06-15 19:08:46 +04:00
parent 3bb14ecd7f
commit ee689cb1ab
4 changed files with 68 additions and 7 deletions
@@ -71,4 +71,8 @@ public final class NativeInteropTest extends SingleFileTranslationTest {
public void testSimpleUndefined() throws Exception {
fooBoxTest();
}
public void testKt2209() throws Exception {
fooBoxTest();
}
}
@@ -20,7 +20,8 @@ import com.google.dart.compiler.backend.js.ast.JsExpression;
import com.google.dart.compiler.backend.js.ast.JsName;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
import org.jetbrains.jet.lang.descriptors.PropertyDescriptor;
import org.jetbrains.k2js.translate.context.TranslationContext;
import org.jetbrains.k2js.translate.utils.TranslationUtils;
@@ -33,7 +34,6 @@ import static org.jetbrains.k2js.translate.utils.JsDescriptorUtils.getExpectedTh
* <p/>
* For native apis that use .property notation for access.
*/
//TODO: test this class
public final class NativePropertyAccessTranslator extends PropertyAccessTranslator {
@Nullable
@@ -43,8 +43,8 @@ public final class NativePropertyAccessTranslator extends PropertyAccessTranslat
/*package*/
NativePropertyAccessTranslator(@NotNull PropertyDescriptor descriptor,
@Nullable JsExpression receiver,
@NotNull TranslationContext context) {
@Nullable JsExpression receiver,
@NotNull TranslationContext context) {
super(context);
this.receiver = receiver;
this.propertyDescriptor = descriptor.getOriginal();
@@ -59,6 +59,17 @@ public final class NativePropertyAccessTranslator extends PropertyAccessTranslat
@NotNull
@Override
protected JsExpression translateAsGet(@Nullable JsExpression receiver) {
return getCallType().constructCall(receiver, new CallType.CallConstructor() {
@NotNull
@Override
public JsExpression construct(@Nullable JsExpression receiver) {
return doTranslateAsGet(receiver);
}
}, context());
}
@NotNull
private JsExpression doTranslateAsGet(JsExpression receiver) {
JsName nativePropertyName = context().getNameForDescriptor(propertyDescriptor);
if (receiver != null) {
return qualified(nativePropertyName, receiver);
@@ -70,9 +81,15 @@ public final class NativePropertyAccessTranslator extends PropertyAccessTranslat
@Override
@NotNull
protected JsExpression translateAsSet(@Nullable JsExpression receiver, @NotNull JsExpression setTo) {
protected JsExpression translateAsSet(@Nullable JsExpression receiver, @NotNull final JsExpression setTo) {
assert receiver != null;
return assignment(translateAsGet(receiver), setTo);
return getCallType().constructCall(receiver, new CallType.CallConstructor() {
@NotNull
@Override
public JsExpression construct(@Nullable JsExpression receiver) {
return assignment(translateAsGet(receiver), setTo);
}
}, context());
}
@NotNull
@@ -86,7 +103,7 @@ public final class NativePropertyAccessTranslator extends PropertyAccessTranslat
if (receiver != null) {
return receiver;
}
assert !propertyDescriptor.getReceiverParameter().exists() : "Cant have native extension properties.";
assert !propertyDescriptor.getReceiverParameter().exists() : "Can't have native extension properties.";
DeclarationDescriptor expectedThisDescriptor = getExpectedThisDescriptor(propertyDescriptor);
if (expectedThisDescriptor == null) {
return null;
@@ -0,0 +1,24 @@
package foo
native
trait Chrome {
val extension:Extension
}
native
trait Extension {
val lastError:LastError?
}
native
trait LastError {
val message:String
}
native
val chrome:Chrome = noImpl
fun box() : Boolean {
val lastError = chrome.extension.lastError?.message
return lastError == null
}
@@ -0,0 +1,16 @@
/*
* Copyright 2010-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var chrome = {extension: {lastError:null}}