You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
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.
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:
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.
I think there are at least two possible solutions here:
Support some notion of a native type which is treated differently.
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).
Activity
DavePearce commentedon Aug 22, 2019
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:null,bool,byteandintare immutable when translated into their JavaScript equivalents (i.e.BooleanandNumber)function(T1)->(T2)are immutable when translated into JavaScript lambdas.int[]are mutable when translated directly into JavaScript arrays.{ T f1, ... }are mutable when translated directly into JavaScript objects.Support native references where possible #41
DavePearce commentedon Aug 22, 2019
Ok, the answer seems to be YES. Done and dusted.
altmattr commentedon Aug 22, 2019
very nice. And just FYI, here is the DOM node type I was playing with
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.
DavePearce commentedon Aug 22, 2019
Ok, I have just updated
HTML.wywith a very initial implementation ofdom::Node. Needs a lot of work. However, using this, I have written and tested the following method: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:
There are a few interesting issues which I need to think about but, in principle, this makes a lot of sense to me!!
DavePearce commentedon Aug 22, 2019
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:
The problem is that this is now translated as follows:
Observe how we're no longer handling the indirect assignment correctly, leading to a failed test.
DavePearce commentedon Aug 23, 2019
I think there are at least two possible solutions here:
nativetype which is treated differently.Add (and update) test cases #41