Skip to content

Implementation of References #41

Closed

Description

@DavePearce

(@altmattr --- this may interest you)

An interesting question is how best to implement references. In particular, we want references to correspond (wherever possible) to native javascript objects. Doing this would allow, for example, us to represent the DOM natively in Whiley. For example, consider this code:

public type Element is { js_string innerText,  ... }

public export method getText(&Element p) -> js_string:
    return p->innerText

This is a very basic implementation of the DOM Element interface. However, this currently compiles to the following JavaScript:

function main$getText(p) {
   return p.$ref.innerText;
}

This arises because all references are currently implemented using a record with the single field $ref. For some references (e.g. &int, &(null|bool)) this is actually the right thing to do. But, for other reference types (e.g. &(int[]) and &{int f} we can actually implement them in a different fashion.

Activity

  1. DavePearce commented on Aug 22, 2019

    @DavePearce
    MemberAuthor

    QUESTION: Can we use isCopyable() to determine when a reference requires a wrapper and when it doesn't? At least, what types already exhibit reference semantics:

    • (primitives) The primitives null, bool, byte and int are immutable when translated into their JavaScript equivalents (i.e. Boolean and Number)
    • (lambdas) Lambda types of the form function(T1)->(T2) are immutable when translated into JavaScript lambdas.
    • (arrays) Array types of the form int[] are mutable when translated directly into JavaScript arrays.
    • (records) Record types of the form { T f1, ... } are mutable when translated directly into JavaScript objects.
    • (Unions) The mutability of a union currently depends on the mutability of the element types. However, in the future with tagged unions, this will no longer be the case as they will be translated into JavaScript objects.
  2. DavePearce commented on Aug 22, 2019

    @DavePearce
    MemberAuthor

    Ok, the answer seems to be YES. Done and dusted.

  3. altmattr commented on Aug 22, 2019

    @altmattr

    very nice. And just FYI, here is the DOM node type I was playing with

    type node is {
        string name,
        attribute[] attrs, 
        node[] children
    } | string
    

    I haven't made it as far as being able to use it - that's another issue - but I quite like the idea that a node is either a "real" node or just a text node.

  4. DavePearce commented on Aug 22, 2019

    @DavePearce
    MemberAuthor

    Ok, I have just updated HTML.wy with a very initial implementation of dom::Node. Needs a lot of work. However, using this, I have written and tested the following method:

    import std::math
    import w3c::dom
    
    // A simple method which traverses a given DOM tree counting the
    // maximum depth.
    public export method max_depth(dom::Node node) -> (int r):
        int max = 0
        int i = 0
        //
        while i < |node->childNodes|:
            // Extract child node
            dom::Node child = node->childNodes[i]
            // Determine depth from child node
            int d = max_depth(child)
            // Update current maximum
            max = math::max(max,d+1)
            // Continue
            i = i + 1
        //
        return max
    

    This does in fact traverse the real DOM and count the depth of nodes. For example, this is the HTML file I used to test the above:

    <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
    <html> <head>
        <title></title>
        <script type="text/javascript" src="wy.js"></script>
        <script type="text/javascript" src="../bin/js/std.js"></script>
        <script type="text/javascript" src="../bin/js/dom.js"></script>        
        <script type="text/javascript" src="../bin/js/main.js"></script>
    </head>
    <body bgcolor="#ffffff">
      <h1>HTML Test</h1>
      <div id="mydiv">
        <b>MY DIV</b>
        <div><i>IN</i>NER</div>
      </div>
    <script>
      window.onload = function() {
      var d = document.getElementById("mydiv");  
      alert(main$max_depth(d));
    }
    </script>
    </body>
    </html>
    

    There are a few interesting issues which I need to think about but, in principle, this makes a lot of sense to me!!

  5. DavePearce commented on Aug 22, 2019

    @DavePearce
    MemberAuthor

    Have reopened this issue as, thinking about it, have realised a number reasons where it breaks badly. These definitely need thought!! An example test which now fails would be:

    public export method test():
        &{int x} c = new {x: 5}
        &{int x} d = c
        *c = {x: 4}
        assume (*c) == {x:4}
        assume (*d) == {x:4}
    

    The problem is that this is now translated as follows:

    function Reference_Valid_5$test() {
       var c = new Wy.Record({x: 5});
       var d = c;
       c = new Wy.Record({x: 4});
       Wy.assert(Wy.equals(c, new Wy.Record({x: 4})));
       Wy.assert(Wy.equals(d, new Wy.Record({x: 4})));
    }
    

    Observe how we're no longer handling the indirect assignment correctly, leading to a failed test.

  6. DavePearce commented on Aug 23, 2019

    @DavePearce
    MemberAuthor

    I think there are at least two possible solutions here:

    1. Support some notion of a native type which is treated differently.
    2. Stick with the current untagged union approach and, instead, add runtime type information to those types that need it (e.g. lambdas, and possibly unions).
  7. added a commit that references this issue on Aug 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions