From 1c5338ccf7fca46801cc008ce4e99d92c8a8ba85 Mon Sep 17 00:00:00 2001 From: Zalim Bashorov Date: Thu, 28 Aug 2014 21:44:58 +0400 Subject: [PATCH] JS backend: added the ability to add metadata to js nodes. --- .../compiler/backend/js/ast/AbstractNode.java | 11 ++++- .../dart/compiler/backend/js/ast/JsName.java | 3 +- .../backend/js/ast/metadata/HasMetadata.kt | 42 +++++++++++++++++++ .../js/ast/metadata/MetadataProperty.kt | 28 +++++++++++++ .../js/ast/metadata/metadataProperties.kt | 30 +++++++++++++ 5 files changed, 111 insertions(+), 3 deletions(-) create mode 100644 js/js.dart-ast/src/com/google/dart/compiler/backend/js/ast/metadata/HasMetadata.kt create mode 100644 js/js.dart-ast/src/com/google/dart/compiler/backend/js/ast/metadata/MetadataProperty.kt create mode 100644 js/js.dart-ast/src/com/google/dart/compiler/backend/js/ast/metadata/metadataProperties.kt diff --git a/js/js.dart-ast/src/com/google/dart/compiler/backend/js/ast/AbstractNode.java b/js/js.dart-ast/src/com/google/dart/compiler/backend/js/ast/AbstractNode.java index d2d906be326..80cae8a9a0c 100644 --- a/js/js.dart-ast/src/com/google/dart/compiler/backend/js/ast/AbstractNode.java +++ b/js/js.dart-ast/src/com/google/dart/compiler/backend/js/ast/AbstractNode.java @@ -1,13 +1,20 @@ package com.google.dart.compiler.backend.js.ast; import com.google.dart.compiler.backend.js.JsToStringGenerationVisitor; +import com.google.dart.compiler.backend.js.ast.metadata.HasMetadata; import com.google.dart.compiler.util.TextOutputImpl; -abstract class AbstractNode implements JsNode { +abstract class AbstractNode extends HasMetadata implements JsNode { @Override public String toString() { TextOutputImpl out = new TextOutputImpl(); new JsToStringGenerationVisitor(out).accept(this); return out.toString(); } -} \ No newline at end of file + + protected T withMetadataFrom(T other) { + this.copyMetadataFrom(other); + //noinspection unchecked + return (T) this; + } +} diff --git a/js/js.dart-ast/src/com/google/dart/compiler/backend/js/ast/JsName.java b/js/js.dart-ast/src/com/google/dart/compiler/backend/js/ast/JsName.java index 57672d49927..0fb3a5cd37c 100644 --- a/js/js.dart-ast/src/com/google/dart/compiler/backend/js/ast/JsName.java +++ b/js/js.dart-ast/src/com/google/dart/compiler/backend/js/ast/JsName.java @@ -4,13 +4,14 @@ package com.google.dart.compiler.backend.js.ast; +import com.google.dart.compiler.backend.js.ast.metadata.HasMetadata; import com.google.dart.compiler.common.Symbol; import org.jetbrains.annotations.NotNull; /** * An abstract base class for named JavaScript objects. */ -public class JsName implements Symbol { +public class JsName extends HasMetadata implements Symbol { private final JsScope enclosing; private final String ident; private JsNode staticRef = null; diff --git a/js/js.dart-ast/src/com/google/dart/compiler/backend/js/ast/metadata/HasMetadata.kt b/js/js.dart-ast/src/com/google/dart/compiler/backend/js/ast/metadata/HasMetadata.kt new file mode 100644 index 00000000000..0f956141273 --- /dev/null +++ b/js/js.dart-ast/src/com/google/dart/compiler/backend/js/ast/metadata/HasMetadata.kt @@ -0,0 +1,42 @@ +/* + * Copyright 2010-2014 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. + */ + +package com.google.dart.compiler.backend.js.ast.metadata + +abstract class HasMetadata { + private val metadata: MutableMap = hashMapOf() + + fun getData(key: String): T { + [suppress("UNCHECKED_CAST")] + return metadata[key] as T + } + + fun setData(key: String, value: T) { + metadata[key] = value + } + + fun hasData(key: String): Boolean { + return metadata.containsKey(key) + } + + fun removeData(key: String) { + metadata.remove(key) + } + + fun copyMetadataFrom(other: HasMetadata) { + metadata.putAll(other.metadata) + } +} diff --git a/js/js.dart-ast/src/com/google/dart/compiler/backend/js/ast/metadata/MetadataProperty.kt b/js/js.dart-ast/src/com/google/dart/compiler/backend/js/ast/metadata/MetadataProperty.kt new file mode 100644 index 00000000000..0be6e29f252 --- /dev/null +++ b/js/js.dart-ast/src/com/google/dart/compiler/backend/js/ast/metadata/MetadataProperty.kt @@ -0,0 +1,28 @@ +/* + * Copyright 2010-2014 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. + */ + +package com.google.dart.compiler.backend.js.ast.metadata + +private class MetadataProperty(val default: R) { + fun get(thisRef: T, desc: PropertyMetadata): R { + if (!thisRef.hasData(desc.name)) return default + return thisRef.getData(desc.name) + } + + fun set(thisRef: T, desc: PropertyMetadata, value: R) { + thisRef.setData(desc.name, value) + } +} diff --git a/js/js.dart-ast/src/com/google/dart/compiler/backend/js/ast/metadata/metadataProperties.kt b/js/js.dart-ast/src/com/google/dart/compiler/backend/js/ast/metadata/metadataProperties.kt new file mode 100644 index 00000000000..616f5cab39b --- /dev/null +++ b/js/js.dart-ast/src/com/google/dart/compiler/backend/js/ast/metadata/metadataProperties.kt @@ -0,0 +1,30 @@ +/* + * Copyright 2010-2014 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. + */ + +package com.google.dart.compiler.backend.js.ast.metadata + +import com.google.dart.compiler.backend.js.ast.* +import org.jetbrains.jet.lang.types.lang.InlineStrategy +import kotlin.properties.Delegates + +public var JsName.staticRef: JsNode? by MetadataProperty(default = null) + +public var JsInvocation.inlineStrategy: InlineStrategy? by MetadataProperty(default = null) + +public var JsFunction.isLocal: Boolean by MetadataProperty(default = false) + +public var JsParameter.hasDefaultValue: Boolean by MetadataProperty(default = false) +