How to convert a deprecated low Firrtl Transform to the Dependency API - chisel

I am updating a Firrtl transform that looks like this
class RetimeTransform extends Transform {
override def inputForm: CircuitForm = LowForm
override def outputForm: CircuitForm = LowForm
...
to the new Dependency API. Changed the transform to this
class RetimeTransform extends Transform with DependencyAPIMigration {
but now it does not run the transform in the same order as before. Is there a simple way to specify the dependencies so I get the original behavior?

Many thanks to the Chisel team. It seems the answer is to do the conversion like this.
class RetimeTransform extends Transform with DependencyAPIMigration {
override def prerequisites: Seq[TransformDependency] = Forms.LowForm
override def optionalPrerequisites: Seq[TransformDependency] = Forms.LowFormOptimized
override def optionalPrerequisiteOf: Seq[TransformDependency] = Forms.LowEmitters

Related

Grails: JSON views with namespacing convention?

Typical non-json template rendering uses naming convention to locate views, but I cannot get this to work with JSON Views.
Sample class
class StrainsController extends ManagerController {
static namespace = "manager";
def edit() {
def model = []
respond([view: "/manager/strains/edit"], model)
}
}
Folder Structure
grails-app
views
manager
strains
edit.gson
Ideally I shouldn't have to explicitly define where my gson view is located, Grails convention over configuration motto should already handle it. Is there a special configuration option I need to enable for this to work?
Grails 3.2.8
JSON Views 1.2.3
Is there a special configuration option I need to enable for this to
work?
No. I expect support for that just isn't implemented. If you file an issue at https://github.com/grails/grails-views/issues we will take a look.
Thanks for the feedback.
The issue was my fault. When I use respond I need to add static responseFormats = ['json'] to my controller in order to allow for convention mapping.
class StrainsController extends ManagerController {
static namespace = "manager";
static responseFormats = ['json'];
def edit() {
def model = []
respond(model)
}
}

real JS classes in scala.js to develop webcomponents

I would like to develop some of the webcomponents in a Polymer 2.0 project with scala.js. While there is a wonderful demo-project on github demonstrating how it works with Polymer 1.0. I cannot get something similar to work with Polymer 2.0 and the native Element-registration technique.
A simple facade might look like the following
#ScalaJSDefined
class PolymerElement extends PolymerBase {
def is: String = ""
def properties: js.Dynamic = js.Dynamic.literal()
}
#js.native
#JSGlobal("Polymer.Element")
class PolymerBase extends HTMLElement
The actual Element:
#JSExportTopLevel("MyElement")
#ScalaJSDefined
class MyElement extends PolymerElement {
private var label = "init"
override def is = "my-element"
override def properties = js.Dynamic.literal(
"label" -> Map(
"type" -> "String",
"value" -> "init",
"notify" -> true
).toJSDictionary
)
def testMe = {
println(label)
}
}
object MyElement {
#JSExportStatic
val is: String = MyElement.is
#JSExportStatic
val properties: js.Dynamic = MyElement.properties
}
No matter whether I take the old style element registration Polymer(MyElement) or the platform native variant window.customElement.define(MyElement.is, MyElement)
It obviously throws an exception as MyElement isn't instatiable with new MyElement.
It throws the exception:
Uncaught TypeError: Class constructor PolymerElement cannot be invoked without 'new'
Studying the Scala.js facade writing guide, I already tried a lot of facade variants declaring PolymerElement and PolymerBase abstract.
A possible solution that comes to my mind is, writing a native JavaScript Class, that indeed is instantiable and using #js.native facades on them. But I'm looking for a way to achieve it with something Scala.js 0.6.16 provides.
Updated version (2018-04)
Ok, this is possible helps to someone else too and I decided to publish my new version of it.
I'm using this pure ScalaJS solution to integrate with Polymer2/CustomElements.
My environment is:
Scala : 2.12
ScalaJS: 0.6.22
And also : "org.scala-js" %%% "scalajs-dom" % "0.9.2"
ScalaJS options:
"-P:scalajs:sjsDefinedByDefault"
I've created some ScalaJS facades for CustomElements and Polymer 2 and published them here - https://bitbucket.org/latestbit/scalymer/src/tip/src/main/scala/org/latestbit/sjs/polymer2/?at=default
They're not full-featured Polymer facades, just in the very beginning state, but they are working for me.
And you can use them easily without any hacks like:
#JSExportTopLevel(name = "TestElement")
class TestElement() extends Polymer.Element {
override def connectedCallback(): Unit = {
super.connectedCallback()
global.console.log(s"Attribute name ${getAttribute("name")}. Body is ${dom.document.querySelector("body")}")
global.console.log(s"${this.$.selectDynamic("testCl")}")
global.console.log(s"${$$("testCl")}")
}
}
object TestElement {
#JSExportStatic
def is = "test-element"
#JSExportStatic
def properties = js.Dictionary(
"name" -> js.Dictionary(
"type" -> "String"
)
)
}
Then register it also in Scala like:
object TestJsApplication {
def main() : Unit = {
Globals.customElements.define(TestElement.is,
js.constructorOf[TestElement]
)
}
}
The html part is usual:
<dom-module id="test-element">
<template>
<span id="testCl">Not much here yet.</span>
This is <b>[[name]]</b>.
</template>
</dom-module>
You will find the complete example here - https://bitbucket.org/latestbit/scalymer/src
An old try to solve (for historical purposes)
Ok, this is the best 'solution' I've found.
This is not solving it completely, but I hope it'll be a helpful trick while we're expecting sjs improvements in this area.
Get any library to 'mixin' js classes. I've used https://github.com/rse/aggregation.
Create your ScalaJS component but don't try to inherit it from Polymer.Element directly:
#ScalaJSDefined
#JSExportTopLevel("TestPolymerElement")
class TestPolymerElement extends js.Object {
def test = g.console.log("Hello from scala")
}
object TestPolymerElement {
#JSExportStatic
def is = "test-polymer-element"
}
Create a JS pure "companion" class to inherit Polymer.Element and ScalaJS component as a mixin and register it:
class TestPolymerElementJs extends aggregation(Polymer.Element,TestPolymerElement) {
}
customElements.define(TestPolymerElementJs.is, TestPolymerElementJs);
Also, you can define the properties and manage them in ScalaJS like:
#ScalaJSDefined
#JSExportTopLevel("TestPolymerElement")
class TestPolymerElement(val name : String) extends js.Object {
def test = g.console.log(s"Hello from ${name}")
}
object TestPolymerElement {
#JSExportStatic
def is = "test-polymer-element"
#JSExportStatic
def properties = js.Dictionary (
"name" -> js.Dictionary(
"type" -> "String"
)
)
}

Grails 2.5.0 static compilation, controllers and grails features

I am testing out Grails static compilation, specifically GrailsCompileStatic. The documentation is limited in explaining what Grails dynamic features aren't supported. My test Controller is very simple, but I'm running into problems already.
#GrailsCompileStatic
class UserController {
UserService userService
def list() {
def model = [:]
def model = request.JSON
withFormat {
json {
render(model as JSON)
}
}
}
}
When compiling the application I get two compile time errors. The first about a missing property for JSON on the request object, and a second error about a missing method for json in the withFormat closure.
Seems to me I'm either doing something wrong or GrailsCompileStatic doesn't work with these features?
About request.JSON
The request object's getJSON() method is added via the ConvertersPluginSupport class. The exact lines are:
private static void enhanceRequest() {
// Methods for Reading JSON/XML from Requests
def getXMLMethod = { -> XML.parse((HttpServletRequest) delegate) }
def getJSONMethod = { -> JSON.parse((HttpServletRequest) delegate)}
def requestMc = GrailsMetaClassUtils.getExpandoMetaClass(HttpServletRequest)
requestMc.getXML = getXMLMethod
requestMc.getJSON = getJSONMethod
}
As you can see it uses the dynamic dispatch mechanism, but fortunately it's not such a big deal. You can simply replicate it by executing JSON.parse(request) anywhere in your controller.
Pay attention though! JSON.parse(HttpServletRequest) returns an Object, which is either a JSONObject or a JSONArray, so if you plan on using them explicitly, and you are compiling statically, you will have to cast it.
You might create a common base class for your controllers:
import org.codehaus.groovy.grails.web.json.JSONArray
import org.codehaus.groovy.grails.web.json.JSONObject
import grails.converters.JSON
#GrailsCompileStatic
class BaseController {
protected JSONObject getJSONObject() {
(JSONObject) JSON.parse(request)
}
protected JSONArray getJSONArray() {
(JSONArray) JSON.parse(request)
}
}
Then in your controller you can simpy invoke getJSONObject() or getJSONArray. It's a bit of a workaround, but results in a staticly compileable code.
About withFormat
This is a bit more complicated. The withFormat construct is really a method, which has a Closure as it's first parameter. The internal implementation then figures out based on the current request or response content type which part of the argument closure is to be used.
If you want to figure out how to do this statically, take a look at the source code.
You could extend this class, then use it's protected methods, but I don't know if it's worth all the hussle, you would loose much of Grails' conciseness. But if you really want to do it, you can. Don't you just love open source projects ? :)

Swing wrapper seems to lack certain peer functions?

any thoughts why the swing components in scala do not seem to be fully wrapped? For example, the "paintImmediately" function. Or "Update" (which I can't seem to override due to this).
An examination of the Scala source code compared to the scala API seems a little confusing.. (scala 2.9 source exert below). The API seems to suggest these other functions exist:
http://www.scala-lang.org/api/current/scala/swing/Component$SuperMixin.html
From what I can tell the SuperMixin trait is what opens up override calls to the peer component. Just a few seems defined. How might I override Update(Graphics g)? Is there a more fluent way of calling Panel.peer.paintImmediately(rect)?
I'm considering modding the Scala source to build a new Panel class to correct these issues for drawing.
abstract class Component extends UIElement {
override lazy val peer: javax.swing.JComponent = new javax.swing.JComponent with SuperMixin {}
var initP: JComponent = null
/**
* This trait is used to redirect certain calls from the peer to the wrapper
* and back. Useful to expose methods that can be customized by overriding.
*/
protected trait SuperMixin extends JComponent {
override def paintComponent(g: Graphics) {
Component.this.paintComponent(g.asInstanceOf[Graphics2D])
}
def __super__paintComponent(g: Graphics) {
super.paintComponent(g)
}
override def paintBorder(g: Graphics) {
Component.this.paintBorder(g.asInstanceOf[Graphics2D])
}
def __super__paintBorder(g: Graphics) {
super.paintBorder(g)
}
override def paintChildren(g: Graphics) {
Component.this.paintChildren(g.asInstanceOf[Graphics2D])
}
def __super__paintChildren(g: Graphics) {
super.paintChildren(g)
}
override def paint(g: Graphics) {
Component.this.paint(g.asInstanceOf[Graphics2D])
}
def __super__paint(g: Graphics) {
super.paint(g)
}
}
Most scala swing wrappers delegate to the corresponding wrapped peer component in the java library, and the operations are made available through implicit conversions.
I'll add some detail when I have some minutes to spare.

Scala function call

I have found following function calls in several frameworks which appear to me as if the framework extends some base classes. Some examples:
within(500 millis)
or
"Testcase description" in
{ .... }
First example returns a duration object with the duration of 500 milliseconds from akka and second is the definition of a testcase from scalatest.
I would like to know how this behavior is achieved and how it is called.
This is done with the "Pimp my library" technique.
To add non existing methods to a class, you define an implicit method that converts objects of that class to objects of a class that has the method:
class Units(i: Int) {
def millis = i
}
implicit def toUnits(i: Int) = new Units(i)
class Specs(s: String) {
def in(thunk: => Unit) = thunk
}
implicit def toSpecs(s: String) = new Specs(s)
See also "Where does Scala looks for Implicits?"
If I'm not mistaken, those pieces of code can be desugared as
within(500.millis)
and
"Testcase description".in({ ... })
This should make it easier to see what's going on.