From aecb5c72c503a4c7283a67a73e466f9d9d51a7be Mon Sep 17 00:00:00 2001 From: James Strachan Date: Fri, 25 May 2012 21:43:04 +0100 Subject: [PATCH] code generate the JS dom.kt file so its got full W3C DOM API compatibility based on the API that ships with the JDK --- js/js.libraries/src/core/dom.kt | 273 ++++++++++++++++-- .../kotlin/tools/GenerateJavaScriptStubs.kt | 80 +++++ .../kotlin/tools/GenerateStandardLib.kt | 13 +- 3 files changed, 339 insertions(+), 27 deletions(-) create mode 100644 libraries/stdlib/test/org/jetbrains/kotlin/tools/GenerateJavaScriptStubs.kt diff --git a/js/js.libraries/src/core/dom.kt b/js/js.libraries/src/core/dom.kt index 8131d461d72..ede8fd82827 100644 --- a/js/js.libraries/src/core/dom.kt +++ b/js/js.libraries/src/core/dom.kt @@ -1,34 +1,255 @@ + package org.w3c.dom +// +// NOTE THIS FILE IS AUTO-GENERATED by the GeneratedJavaScriptStubs.kt +// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib +// + import js.noImpl -/** - * A stub class definition so we can work with the DOM APIs as native objects - */ -native public trait Document : Node { - fun getElementById(id : String) : Element = js.noImpl - fun createTextNode(text : String) : Node = js.noImpl +// Contains stub APIs for the W3C DOM API so we can delegate to the platform DOM instead + + +native public trait Attr: Node { + fun getName(): String = js.noImpl + fun getValue(): String = js.noImpl + fun setValue(arg1: String): Unit = js.noImpl + fun getSpecified(): Boolean = js.noImpl + fun getOwnerElement(): Element = js.noImpl + fun getSchemaTypeInfo(): TypeInfo = js.noImpl + fun isId(): Boolean = js.noImpl } -native public trait Attr : Node {} -native public trait CDATASection : Text {} -native public trait CharacterData : Node {} -native public trait Comment : CharacterData {} -native public trait DOMImplementation {} -native public trait DocumentType : Node {} -native public trait Element : Node { - fun appendChild(child : Node) : Unit = js.noImpl - fun getTextContent() : String = js.noImpl +native public trait CDATASection: Text { +} + +native public trait CharacterData: Node { + fun getLength(): Int = js.noImpl + fun getData(): String = js.noImpl + fun replaceData(arg1: Int, arg2: Int, arg3: String): Unit = js.noImpl + fun setData(arg1: String): Unit = js.noImpl + fun substringData(arg1: Int, arg2: Int): String = js.noImpl + fun appendData(arg1: String): Unit = js.noImpl + fun insertData(arg1: Int, arg2: String): Unit = js.noImpl + fun deleteData(arg1: Int, arg2: Int): Unit = js.noImpl +} + +native public trait Comment: CharacterData { +} + +native public trait Document: Node { + fun getImplementation(): DOMImplementation = js.noImpl + fun getInputEncoding(): String = js.noImpl + fun getXmlEncoding(): String = js.noImpl + fun createElement(arg1: String): Element = js.noImpl + fun createComment(arg1: String): Comment = js.noImpl + fun getDoctype(): DocumentType = js.noImpl + fun getDocumentElement(): Element = js.noImpl + fun createDocumentFragment(): DocumentFragment = js.noImpl + fun createTextNode(arg1: String): Text = js.noImpl + fun createCDATASection(arg1: String): CDATASection = js.noImpl + fun createProcessingInstruction(arg1: String, arg2: String): ProcessingInstruction = js.noImpl + fun createAttribute(arg1: String): Attr = js.noImpl + fun createEntityReference(arg1: String): EntityReference = js.noImpl + fun getElementsByTagName(arg1: String): NodeList = js.noImpl + fun importNode(arg1: Node, arg2: Boolean): Node = js.noImpl + fun createElementNS(arg1: String, arg2: String): Element = js.noImpl + fun createAttributeNS(arg1: String, arg2: String): Attr = js.noImpl + fun getElementsByTagNameNS(arg1: String, arg2: String): NodeList = js.noImpl + fun getElementById(arg1: String): Element = js.noImpl + fun getXmlStandalone(): Boolean = js.noImpl + fun setXmlStandalone(arg1: Boolean): Unit = js.noImpl + fun getXmlVersion(): String = js.noImpl + fun setXmlVersion(arg1: String): Unit = js.noImpl + fun getStrictErrorChecking(): Boolean = js.noImpl + fun setStrictErrorChecking(arg1: Boolean): Unit = js.noImpl + fun getDocumentURI(): String = js.noImpl + fun setDocumentURI(arg1: String): Unit = js.noImpl + fun adoptNode(arg1: Node): Node = js.noImpl + fun getDomConfig(): DOMConfiguration = js.noImpl + fun normalizeDocument(): Unit = js.noImpl + fun renameNode(arg1: Node, arg2: String, arg3: String): Node = js.noImpl +} + +native public trait DocumentFragment: Node { +} + +native public trait DocumentType: Node { + fun getName(): String = js.noImpl + fun getEntities(): NamedNodeMap = js.noImpl + fun getNotations(): NamedNodeMap = js.noImpl + fun getPublicId(): String = js.noImpl + fun getSystemId(): String = js.noImpl + fun getInternalSubset(): String = js.noImpl +} + +native public trait DOMConfiguration { + fun setParameter(arg1: String, arg2: Any): Unit = js.noImpl + fun getParameter(arg1: String): Any = js.noImpl + fun canSetParameter(arg1: String, arg2: Any): Boolean = js.noImpl + fun getParameterNames(): DOMStringList = js.noImpl +} + +native public trait DOMError { + fun getLocation(): DOMLocator = js.noImpl + fun getMessage(): String = js.noImpl + fun getType(): String = js.noImpl + fun getSeverity(): Short = js.noImpl + fun getRelatedException(): Any = js.noImpl + fun getRelatedData(): Any = js.noImpl +} + +native public trait DOMErrorHandler { + fun handleError(arg1: DOMError): Boolean = js.noImpl +} + +native public trait DOMImplementation { + fun getFeature(arg1: String, arg2: String): Any = js.noImpl + fun hasFeature(arg1: String, arg2: String): Boolean = js.noImpl + fun createDocumentType(arg1: String, arg2: String, arg3: String): DocumentType = js.noImpl + fun createDocument(arg1: String, arg2: String, arg3: DocumentType): Document = js.noImpl +} + +native public trait DOMLocator { + fun getLineNumber(): Int = js.noImpl + fun getColumnNumber(): Int = js.noImpl + fun getByteOffset(): Int = js.noImpl + fun getUtf16Offset(): Int = js.noImpl + fun getRelatedNode(): Node = js.noImpl + fun getUri(): String = js.noImpl +} + +native public trait DOMStringList { + fun getLength(): Int = js.noImpl + fun contains(arg1: String): Boolean = js.noImpl + fun item(arg1: Int): String = js.noImpl +} + +native public trait Element: Node { + fun getAttribute(arg1: String): String = js.noImpl + fun setAttribute(arg1: String, arg2: String): Unit = js.noImpl + fun getSchemaTypeInfo(): TypeInfo = js.noImpl + fun getElementsByTagName(arg1: String): NodeList = js.noImpl + fun getElementsByTagNameNS(arg1: String, arg2: String): NodeList = js.noImpl + fun getTagName(): String = js.noImpl + fun removeAttribute(arg1: String): Unit = js.noImpl + fun getAttributeNode(arg1: String): Attr = js.noImpl + fun setAttributeNode(arg1: Attr): Attr = js.noImpl + fun removeAttributeNode(arg1: Attr): Attr = js.noImpl + fun getAttributeNS(arg1: String, arg2: String): String = js.noImpl + fun setAttributeNS(arg1: String, arg2: String, arg3: String): Unit = js.noImpl + fun removeAttributeNS(arg1: String, arg2: String): Unit = js.noImpl + fun getAttributeNodeNS(arg1: String, arg2: String): Attr = js.noImpl + fun setAttributeNodeNS(arg1: Attr): Attr = js.noImpl + fun hasAttribute(arg1: String): Boolean = js.noImpl + fun hasAttributeNS(arg1: String, arg2: String): Boolean = js.noImpl + fun setIdAttribute(arg1: String, arg2: Boolean): Unit = js.noImpl + fun setIdAttributeNS(arg1: String, arg2: String, arg3: Boolean): Unit = js.noImpl + fun setIdAttributeNode(arg1: Attr, arg2: Boolean): Unit = js.noImpl +} + +native public trait Entity: Node { + fun getInputEncoding(): String = js.noImpl + fun getXmlEncoding(): String = js.noImpl + fun getXmlVersion(): String = js.noImpl + fun getPublicId(): String = js.noImpl + fun getSystemId(): String = js.noImpl + fun getNotationName(): String = js.noImpl +} + +native public trait EntityReference: Node { +} + +native public trait NameList { + fun getLength(): Int = js.noImpl + fun getName(arg1: Int): String = js.noImpl + fun contains(arg1: String): Boolean = js.noImpl + fun getNamespaceURI(arg1: Int): String = js.noImpl + fun containsNS(arg1: String, arg2: String): Boolean = js.noImpl +} + +native public trait NamedNodeMap { + fun getLength(): Int = js.noImpl + fun item(arg1: Int): Node = js.noImpl + fun getNamedItem(arg1: String): Node = js.noImpl + fun setNamedItem(arg1: Node): Node = js.noImpl + fun removeNamedItem(arg1: String): Node = js.noImpl + fun getNamedItemNS(arg1: String, arg2: String): Node = js.noImpl + fun setNamedItemNS(arg1: Node): Node = js.noImpl + fun removeNamedItemNS(arg1: String, arg2: String): Node = js.noImpl +} + +native public trait Node { + fun normalize(): Unit = js.noImpl + fun isSupported(arg1: String, arg2: String): Boolean = js.noImpl + fun getAttributes(): NamedNodeMap = js.noImpl + fun getUserData(arg1: String): Any = js.noImpl + fun setUserData(arg1: String, arg2: Any, arg3: UserDataHandler): Any = js.noImpl + fun getPrefix(): String = js.noImpl + fun getFeature(arg1: String, arg2: String): Any = js.noImpl + fun hasAttributes(): Boolean = js.noImpl + fun setPrefix(arg1: String): Unit = js.noImpl + fun removeChild(arg1: Node): Node = js.noImpl + fun replaceChild(arg1: Node, arg2: Node): Node = js.noImpl + fun getFirstChild(): Node = js.noImpl + fun getLastChild(): Node = js.noImpl + fun getNextSibling(): Node = js.noImpl + fun insertBefore(arg1: Node, arg2: Node): Node = js.noImpl + fun getLocalName(): String = js.noImpl + fun getNodeName(): String = js.noImpl + fun getNodeValue(): String = js.noImpl + fun setNodeValue(arg1: String): Unit = js.noImpl + fun getNodeType(): Short = js.noImpl + fun getParentNode(): Node = js.noImpl + fun getChildNodes(): NodeList = js.noImpl + fun getPreviousSibling(): Node = js.noImpl + fun getOwnerDocument(): Document = js.noImpl + fun appendChild(arg1: Node): Node = js.noImpl + fun hasChildNodes(): Boolean = js.noImpl + fun cloneNode(arg1: Boolean): Node = js.noImpl + fun getNamespaceURI(): String = js.noImpl + fun getBaseURI(): String = js.noImpl + fun compareDocumentPosition(arg1: Node): Short = js.noImpl + fun getTextContent(): String = js.noImpl + fun setTextContent(arg1: String): Unit = js.noImpl + fun isSameNode(arg1: Node): Boolean = js.noImpl + fun lookupPrefix(arg1: String): String = js.noImpl + fun isDefaultNamespace(arg1: String): Boolean = js.noImpl + fun lookupNamespaceURI(arg1: String): String = js.noImpl + fun isEqualNode(arg1: Node): Boolean = js.noImpl +} + +native public trait NodeList { + fun getLength(): Int = js.noImpl + fun item(arg1: Int): Node = js.noImpl +} + +native public trait Notation: Node { + fun getPublicId(): String = js.noImpl + fun getSystemId(): String = js.noImpl +} + +native public trait ProcessingInstruction: Node { + fun getData(): String = js.noImpl + fun getTarget(): String = js.noImpl + fun setData(arg1: String): Unit = js.noImpl +} + +native public trait Text: CharacterData { + fun splitText(arg1: Int): Text = js.noImpl + fun isElementContentWhitespace(): Boolean = js.noImpl + fun getWholeText(): String = js.noImpl + fun replaceWholeText(arg1: String): Text = js.noImpl +} + +native public trait TypeInfo { + fun getTypeName(): String = js.noImpl + fun getTypeNamespace(): String = js.noImpl + fun isDerivedFrom(arg1: String, arg2: String, arg3: Int): Boolean = js.noImpl +} + +native public trait UserDataHandler { + fun handle(arg1: Short, arg2: String, arg3: Any, arg4: Node, arg5: Node): Unit = js.noImpl } -native public trait Entity : Node {} -native public trait EntityReference : Node {} -native public trait NameList {} -native public trait NamedNodeMap {} -native public trait Node {} -native public trait NodeList {} -native public trait Notation : Node {} -native public trait ProcessingInstruction : Node {} -native public trait Text : CharacterData {} -native public trait TypeInfo {} -native public trait UserDataHandler {} diff --git a/libraries/stdlib/test/org/jetbrains/kotlin/tools/GenerateJavaScriptStubs.kt b/libraries/stdlib/test/org/jetbrains/kotlin/tools/GenerateJavaScriptStubs.kt new file mode 100644 index 00000000000..ce512009d96 --- /dev/null +++ b/libraries/stdlib/test/org/jetbrains/kotlin/tools/GenerateJavaScriptStubs.kt @@ -0,0 +1,80 @@ +package org.jetbrains.kotlin.tools + +import java.io.File +import java.io.FileWriter +import java.io.PrintWriter +import org.w3c.dom.* + +/** +* This tool generates JavaScript stubs for classes available in the JDK which are already available in the browser environment +* such as the W3C DOM +*/ +fun generateDomAPI(file: File): Unit { + write(file) { + + println(""" +package org.w3c.dom + +// +// NOTE THIS FILE IS AUTO-GENERATED by the GeneratedJavaScriptStubs.kt +// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib +// + +import js.noImpl + +// Contains stub APIs for the W3C DOM API so we can delegate to the platform DOM instead + +""") + + val classes = arrayList(javaClass(), javaClass(), + javaClass(), javaClass(), + javaClass(),javaClass(),javaClass(), + javaClass(), + javaClass(), javaClass(), + javaClass(), + javaClass(), + javaClass(), + javaClass(), + javaClass(), javaClass(), + javaClass(), javaClass(), javaClass(), javaClass(), + javaClass(), javaClass(), + javaClass(), javaClass(), + javaClass()) + + fun simpleTypeName(klass: Class?): String { + val answer = klass?.getSimpleName()?.capitalize() ?: "Unit" + return if (answer == "Void") "Unit" else if (answer == "Object") "Any" else answer + } + + for (klass in classes) { + val interfaces = klass.getInterfaces() + val extends = if (interfaces != null && interfaces.size == 1) ": ${interfaces[0]?.getSimpleName()}" else "" + + println("native public trait ${klass.getSimpleName()}$extends {") + + // lets iterate through each method + val methods = klass.getDeclaredMethods() + if (methods != null) { + for (method in methods) { + if (method != null) { + val parameterTypes = method.getParameterTypes()!! + + // TODO in java 7 its not easy with reflection to get the parameter argument name... + var counter = 0 + val parameters = parameterTypes.map?, String>{ "arg${++counter}: ${simpleTypeName(it)}" }.makeString(", ") + val returnType = simpleTypeName(method.getReturnType()) + println(" fun ${method.getName()}($parameters): $returnType = js.noImpl") + } + } + } + println("}") + println("") + } + } +} + +fun write(file: File, block: PrintWriter.() -> Unit): Unit { + println("Generating file: ${file.getCanonicalPath()}") + val writer = PrintWriter(FileWriter(file)) + writer.use { writer.block() } +} diff --git a/libraries/stdlib/test/org/jetbrains/kotlin/tools/GenerateStandardLib.kt b/libraries/stdlib/test/org/jetbrains/kotlin/tools/GenerateStandardLib.kt index edd79dba1ea..6a21cfd9b82 100644 --- a/libraries/stdlib/test/org/jetbrains/kotlin/tools/GenerateStandardLib.kt +++ b/libraries/stdlib/test/org/jetbrains/kotlin/tools/GenerateStandardLib.kt @@ -8,7 +8,14 @@ fun generateFile(outFile: File, header: String, inputFile: File, f: (String)-> S outFile.getParentFile()?.mkdirs() val writer = PrintWriter(FileWriter(outFile)) try { - writer.println("// NOTE this file is auto-generated from $inputFile") + writer.println(""" +// +// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt +// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib +// +// Generated from input file: $inputFile +// +""") writer.println(header) val reader = FileReader(inputFile).buffered() @@ -48,6 +55,10 @@ fun main(args: Array) { } val outDir = File(srcDir, "../generated") + val jsCoreDir = File(srcDir, "../../../../js/js.libraries/src/core") + require(jsCoreDir.exists()) + generateDomAPI(File(jsCoreDir, "dom.kt")) + val otherArrayNames = arrayList("Boolean", "Byte", "Char", "Short", "Int", "Long", "Float", "Double") // JLangIterables - Generic iterable stuff