on master help sign in
Merge pull request #5 from fitzgen/initial-sanity-tests

Initial sanity tests

commit8f8add60674c4e5379c59d1de928f69b534170ca authorNick Fitzgerald <fitzgen@gmail.com> dateTue 15 Jan 2019, 16:27 (8 years ago) parentcb66a9c2baf10c
10 files changed+377−212
Cargo.lock+50−64 Cargo.toml+18 README.md+3−3 js/change-list.js+35−33 src/change_list.rs+16−3 src/lib.rs+2−5 src/macros.rs−7 src/node.rs+81−64 src/vdom.rs+37−30 tests/web.rs+135−3
Cargo.lock+50−64
@@ -40,7 +40,6 @@ dependencies = [
4040[[package]]
4141name = "bumpalo"
4242version = "1.1.0"
43−source = "registry+https://github.com/rust-lang/crates.io-index"
4443
4544[[package]]
4645name = "cc"
@@ -55,57 +54,67 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
5554[[package]]
5655name = "console_error_panic_hook"
5756version = "0.1.5"
58−source = "registry+https://github.com/rust-lang/crates.io-index"
5957dependencies = [
6058 "cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
61− "wasm-bindgen 0.2.31 (registry+https://github.com/rust-lang/crates.io-index)",
59+ "wasm-bindgen 0.2.31",
60+]
61+
62+[[package]]
63+name = "console_log"
64+version = "0.1.2"
65+source = "registry+https://github.com/rust-lang/crates.io-index"
66+dependencies = [
67+ "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
68+ "web-sys 0.3.8",
6269]
6370
6471[[package]]
6572name = "dodrio"
6673version = "0.1.0"
6774dependencies = [
68− "bumpalo 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
69− "js-sys 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
70− "wasm-bindgen 0.2.31 (registry+https://github.com/rust-lang/crates.io-index)",
71− "wasm-bindgen-test 0.2.31 (registry+https://github.com/rust-lang/crates.io-index)",
72− "web-sys 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
75+ "bumpalo 1.1.0",
76+ "console_log 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
77+ "js-sys 0.3.8",
78+ "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
79+ "wasm-bindgen 0.2.31",
80+ "wasm-bindgen-test 0.2.31",
81+ "web-sys 0.3.8",
7382]
7483
7584[[package]]
7685name = "dodrio-counter"
7786version = "0.1.0"
7887dependencies = [
79− "bumpalo 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
80− "console_error_panic_hook 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
88+ "bumpalo 1.1.0",
89+ "console_error_panic_hook 0.1.5",
8190 "dodrio 0.1.0",
82− "wasm-bindgen 0.2.31 (registry+https://github.com/rust-lang/crates.io-index)",
83− "wasm-bindgen-test 0.2.31 (registry+https://github.com/rust-lang/crates.io-index)",
84− "web-sys 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
91+ "wasm-bindgen 0.2.31",
92+ "wasm-bindgen-test 0.2.31",
93+ "web-sys 0.3.8",
8594]
8695
8796[[package]]
8897name = "dodrio-hello-world"
8998version = "0.1.0"
9099dependencies = [
91− "bumpalo 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
92− "console_error_panic_hook 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
100+ "bumpalo 1.1.0",
101+ "console_error_panic_hook 0.1.5",
93102 "dodrio 0.1.0",
94− "wasm-bindgen 0.2.31 (registry+https://github.com/rust-lang/crates.io-index)",
95− "wasm-bindgen-test 0.2.31 (registry+https://github.com/rust-lang/crates.io-index)",
96− "web-sys 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
103+ "wasm-bindgen 0.2.31",
104+ "wasm-bindgen-test 0.2.31",
105+ "web-sys 0.3.8",
97106]
98107
99108[[package]]
100109name = "dodrio-input-form"
101110version = "0.1.0"
102111dependencies = [
103− "bumpalo 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
104− "console_error_panic_hook 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
112+ "bumpalo 1.1.0",
113+ "console_error_panic_hook 0.1.5",
105114 "dodrio 0.1.0",
106− "wasm-bindgen 0.2.31 (registry+https://github.com/rust-lang/crates.io-index)",
107− "wasm-bindgen-test 0.2.31 (registry+https://github.com/rust-lang/crates.io-index)",
108− "web-sys 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
115+ "wasm-bindgen 0.2.31",
116+ "wasm-bindgen-test 0.2.31",
117+ "web-sys 0.3.8",
109118]
110119
111120[[package]]
@@ -164,9 +173,8 @@ dependencies = [
164173[[package]]
165174name = "js-sys"
166175version = "0.3.8"
167−source = "registry+https://github.com/rust-lang/crates.io-index"
168176dependencies = [
169− "wasm-bindgen 0.2.31 (registry+https://github.com/rust-lang/crates.io-index)",
177+ "wasm-bindgen 0.2.31",
170178]
171179
172180[[package]]
@@ -349,78 +357,70 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
349357[[package]]
350358name = "wasm-bindgen"
351359version = "0.2.31"
352−source = "registry+https://github.com/rust-lang/crates.io-index"
353360dependencies = [
354− "wasm-bindgen-macro 0.2.31 (registry+https://github.com/rust-lang/crates.io-index)",
361+ "wasm-bindgen-macro 0.2.31",
355362]
356363
357364[[package]]
358365name = "wasm-bindgen-backend"
359366version = "0.2.31"
360−source = "registry+https://github.com/rust-lang/crates.io-index"
361367dependencies = [
362368 "lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
363369 "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
364370 "proc-macro2 0.4.24 (registry+https://github.com/rust-lang/crates.io-index)",
365371 "quote 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)",
366372 "syn 0.15.22 (registry+https://github.com/rust-lang/crates.io-index)",
367− "wasm-bindgen-shared 0.2.31 (registry+https://github.com/rust-lang/crates.io-index)",
373+ "wasm-bindgen-shared 0.2.31",
368374]
369375
370376[[package]]
371377name = "wasm-bindgen-futures"
372378version = "0.3.8"
373−source = "registry+https://github.com/rust-lang/crates.io-index"
374379dependencies = [
375380 "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)",
376− "js-sys 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
377− "wasm-bindgen 0.2.31 (registry+https://github.com/rust-lang/crates.io-index)",
381+ "js-sys 0.3.8",
382+ "wasm-bindgen 0.2.31",
378383]
379384
380385[[package]]
381386name = "wasm-bindgen-macro"
382387version = "0.2.31"
383−source = "registry+https://github.com/rust-lang/crates.io-index"
384388dependencies = [
385389 "quote 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)",
386− "wasm-bindgen-macro-support 0.2.31 (registry+https://github.com/rust-lang/crates.io-index)",
390+ "wasm-bindgen-macro-support 0.2.31",
387391]
388392
389393[[package]]
390394name = "wasm-bindgen-macro-support"
391395version = "0.2.31"
392−source = "registry+https://github.com/rust-lang/crates.io-index"
393396dependencies = [
394397 "proc-macro2 0.4.24 (registry+https://github.com/rust-lang/crates.io-index)",
395398 "quote 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)",
396399 "syn 0.15.22 (registry+https://github.com/rust-lang/crates.io-index)",
397− "wasm-bindgen-backend 0.2.31 (registry+https://github.com/rust-lang/crates.io-index)",
398− "wasm-bindgen-shared 0.2.31 (registry+https://github.com/rust-lang/crates.io-index)",
400+ "wasm-bindgen-backend 0.2.31",
401+ "wasm-bindgen-shared 0.2.31",
399402]
400403
401404[[package]]
402405name = "wasm-bindgen-shared"
403406version = "0.2.31"
404−source = "registry+https://github.com/rust-lang/crates.io-index"
405407
406408[[package]]
407409name = "wasm-bindgen-test"
408410version = "0.2.31"
409−source = "registry+https://github.com/rust-lang/crates.io-index"
410411dependencies = [
411− "console_error_panic_hook 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
412+ "console_error_panic_hook 0.1.5",
412413 "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)",
413− "js-sys 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
414+ "js-sys 0.3.8",
414415 "scoped-tls 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
415− "wasm-bindgen 0.2.31 (registry+https://github.com/rust-lang/crates.io-index)",
416− "wasm-bindgen-futures 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
417− "wasm-bindgen-test-macro 0.2.31 (registry+https://github.com/rust-lang/crates.io-index)",
416+ "wasm-bindgen 0.2.31",
417+ "wasm-bindgen-futures 0.3.8",
418+ "wasm-bindgen-test-macro 0.2.31",
418419]
419420
420421[[package]]
421422name = "wasm-bindgen-test-macro"
422423version = "0.2.31"
423−source = "registry+https://github.com/rust-lang/crates.io-index"
424424dependencies = [
425425 "proc-macro2 0.4.24 (registry+https://github.com/rust-lang/crates.io-index)",
426426 "quote 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -429,7 +429,6 @@ dependencies = [
429429[[package]]
430430name = "wasm-bindgen-webidl"
431431version = "0.2.25"
432−source = "registry+https://github.com/rust-lang/crates.io-index"
433432dependencies = [
434433 "failure 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
435434 "heck 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -437,21 +436,20 @@ dependencies = [
437436 "proc-macro2 0.4.24 (registry+https://github.com/rust-lang/crates.io-index)",
438437 "quote 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)",
439438 "syn 0.15.22 (registry+https://github.com/rust-lang/crates.io-index)",
440− "wasm-bindgen-backend 0.2.31 (registry+https://github.com/rust-lang/crates.io-index)",
439+ "wasm-bindgen-backend 0.2.31",
441440 "weedle 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
442441]
443442
444443[[package]]
445444name = "web-sys"
446445version = "0.3.8"
447−source = "registry+https://github.com/rust-lang/crates.io-index"
448446dependencies = [
449447 "env_logger 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
450448 "failure 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
451− "js-sys 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
449+ "js-sys 0.3.8",
452450 "sourcefile 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
453− "wasm-bindgen 0.2.31 (registry+https://github.com/rust-lang/crates.io-index)",
454− "wasm-bindgen-webidl 0.2.25 (registry+https://github.com/rust-lang/crates.io-index)",
451+ "wasm-bindgen 0.2.31",
452+ "wasm-bindgen-webidl 0.2.25",
455453]
456454
457455[[package]]
@@ -503,17 +501,15 @@ dependencies = [
503501"checksum atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "9a7d5b8723950951411ee34d271d99dddcc2035a16ab25310ea2c8cfd4369652"
504502"checksum backtrace 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "89a47830402e9981c5c41223151efcced65a0510c13097c769cede7efb34782a"
505503"checksum backtrace-sys 0.1.24 (registry+https://github.com/rust-lang/crates.io-index)" = "c66d56ac8dabd07f6aacdaf633f4b8262f5b3601a810a0dcddffd5c22c69daa0"
506−"checksum bumpalo 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc4fcb5e70b6c60b241977e660f32831396c2a8e2208fdb40277ad22f69e9644"
507504"checksum cc 1.0.25 (registry+https://github.com/rust-lang/crates.io-index)" = "f159dfd43363c4d08055a07703eb7a3406b0dac4d0584d96965a3262db3c9d16"
508505"checksum cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "082bb9b28e00d3c9d39cc03e64ce4cea0f1bb9b3fde493f0cbc008472d22bdf4"
509−"checksum console_error_panic_hook 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "6c5dd2c094474ec60a6acaf31780af270275e3153bafff2db5995b715295762e"
506+"checksum console_log 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1e7871d2947441b0fdd8e2bd1ce2a2f75304f896582c0d572162d48290683c48"
510507"checksum env_logger 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "afb070faf94c85d17d50ca44f6ad076bce18ae92f0037d350947240a36e9d42e"
511508"checksum failure 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "6dd377bcc1b1b7ce911967e3ec24fa19c3224394ec05b54aa7b083d498341ac7"
512509"checksum failure_derive 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "64c2d913fe8ed3b6c6518eedf4538255b989945c14c2a7d5cbff62a5e2120596"
513510"checksum futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)" = "49e7653e374fe0d0c12de4250f0bdb60680b8c80eed558c5c7538eec9c89e21b"
514511"checksum heck 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ea04fa3ead4e05e51a7c806fc07271fdbde4e246a6c6d1efd52e72230b771b82"
515512"checksum humantime 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3ca7e5f2e110db35f93b837c81797f3714500b81d517bf20c431b16d3ca4f114"
516−"checksum js-sys 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "27968ddad7df6b0895bf223d475fb71077bbc1de063777ae27cb442163591d36"
517513"checksum lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a374c89b9db55895453a74c1e38861d9deec0b01b405a82516e9d5de4820dea1"
518514"checksum libc 0.2.44 (registry+https://github.com/rust-lang/crates.io-index)" = "10923947f84a519a45c8fefb7dd1b3e8c08747993381adee176d7a82b4195311"
519515"checksum log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c84ec4b527950aa83a329754b01dbe3f58361d1c5efacd1f6d68c494d08a17c6"
@@ -539,16 +535,6 @@ dependencies = [
539535"checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc"
540536"checksum utf8-ranges 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "796f7e48bef87609f7ade7e06495a87d5cd06c7866e6a5cbfceffc558a243737"
541537"checksum version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "914b1a6776c4c929a602fafd8bc742e06365d4bcbe48c30f9cca5824f70dc9dd"
542−"checksum wasm-bindgen 0.2.31 (registry+https://github.com/rust-lang/crates.io-index)" = "f45ea87af0efdd3b142836cf2ea233c938fbd0ead3a9da0fd240abd5303d1f5c"
543−"checksum wasm-bindgen-backend 0.2.31 (registry+https://github.com/rust-lang/crates.io-index)" = "65aafeecd4dcb2385505cbd33479c08c961902eae2d19f8cdc09a27c3cd50e09"
544−"checksum wasm-bindgen-futures 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "5b36999d57b7cc8b5463adcef55e9847efce0cb54dc3239a1f8082aae986d841"
545−"checksum wasm-bindgen-macro 0.2.31 (registry+https://github.com/rust-lang/crates.io-index)" = "e3723ce26569facc66e1b53982a1035224d5c0d89a766eb3a45e56153e59b5e1"
546−"checksum wasm-bindgen-macro-support 0.2.31 (registry+https://github.com/rust-lang/crates.io-index)" = "51ff31a6a49abf9743b2cac233bf83a33f24b154878ffdce5a41c8d11f91b3d9"
547−"checksum wasm-bindgen-shared 0.2.31 (registry+https://github.com/rust-lang/crates.io-index)" = "98c697f76b3ab6a0e8abd4489ba81c1ad2eded87f8a871ea5593bb45323400c8"
548−"checksum wasm-bindgen-test 0.2.31 (registry+https://github.com/rust-lang/crates.io-index)" = "371bcf73e0105dd6475ce979641adce782c45685c66502b69014ea3ed33e1c4c"
549−"checksum wasm-bindgen-test-macro 0.2.31 (registry+https://github.com/rust-lang/crates.io-index)" = "14e80d7f52743df9276c7c8a37b793332d746309b0e63696d33d1de3d464c674"
550−"checksum wasm-bindgen-webidl 0.2.25 (registry+https://github.com/rust-lang/crates.io-index)" = "bb97b10781b6df5a71d9c3aa9c8b65023816fdba1b4b13a00eb5591eeeb7f6ea"
551−"checksum web-sys 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "4e4abda3e6221ccb9b9a1853d75c33a37a385e533e784bd8ea121f6ff279ec1c"
552538"checksum weedle 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "26a4c67f132386d965390b8a734d5d10adbcd30eb5cc74bd9229af8b83f10044"
553539"checksum winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "92c1eb33641e276cfa214a0522acad57be5c56b10cb348b3c5117db75f3ac4b0"
554540"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
Cargo.toml+18
@@ -11,6 +11,7 @@ default = []
1111bumpalo = "1.0.2"
1212wasm-bindgen = "0.2.25"
1313js-sys = "0.3.2"
14+log = "0.4.6"
1415
1516[dependencies.web-sys]
1617version = "0.3.8"
@@ -24,6 +25,15 @@ features = [
2425
2526[dev-dependencies]
2627wasm-bindgen-test = "0.2"
28+console_log = "0.1.2"
29+
30+[dev-dependencies.web-sys]
31+version = "0.3.8"
32+features = [
33+ "Attr",
34+ "NamedNodeMap",
35+ "NodeList",
36+]
2737
2838[profile.release]
2939# Tell `rustc` to optimize for small code size.
@@ -35,3 +45,11 @@ members = [
3545 "./examples/hello-world/crate",
3646 "./examples/input-form/crate",
3747]
48+
49+[patch.crates-io]
50+bumpalo = { path = "../bumpalo" }
51+console_error_panic_hook = { path = "../console_error_panic_hook" }
52+wasm-bindgen = { path = "../wasm-bindgen" }
53+wasm-bindgen-test = { path = "../wasm-bindgen/crates/test" }
54+js-sys = { path = "../wasm-bindgen/crates/js-sys" }
55+web-sys = { path = "../wasm-bindgen/crates/web-sys" }
README.md+3−3
@@ -47,10 +47,10 @@ phases. And virtual DOMs are very phase oriented.
4747Rendering happens as follows:
4848
4949* The application state is rendered into bump allocation arena (1).
50−* (1) diffed with (2) to produce (3).
51−* JavaScript code applies (3) to the physical DOM.
50+* (1) diffed with (2) and the changes are emitted into (3).
51+* JavaScript code applies the change list in (3) to the physical DOM.
5252* (1) and (2) are swapped, double-buffering style, and the new (1) has its bump
53− allocation pointer reset.
53+ allocation pointer reset, as does (3).
5454* Rinse and repeat.
5555
5656### Change List as Stack Machine
js/change-list.js+35−33
@@ -12,21 +12,21 @@ function string(mem, pointer, length) {
1212const OP_TABLE = [
1313 // 0
1414 function setText(stack, mem8, mem32, i) {
15− console.log("setText");
15+ // console.log("setText");
1616 const pointer = mem32[i++];
1717 const length = mem32[i++];
1818 const str = string(mem8, pointer, length);
19− console.log(" str =", str);
20− console.log(" top(stack) =", top(stack));
19+ // console.log(" str =", str);
20+ // console.log(" top(stack) =", top(stack));
2121 top(stack).textContent = str;
2222 return i;
2323 },
2424
2525 // 1
2626 function removeSelfAndNextSiblings(stack, mem8, mem32, i) {
27− console.log("removeSelfAndNextSiblings");
27+ // console.log("removeSelfAndNextSiblings");
2828 const node = stack.pop();
29− console.log(" top(stack) =", node);
29+ // console.log(" top(stack) =", node);
3030 let sibling = node.nextSibling;
3131 while (sibling) {
3232 const temp = sibling.nextSibling;
@@ -39,11 +39,11 @@ const OP_TABLE = [
3939
4040 // 2
4141 function replaceWith(stack, mem8, mem32, i) {
42− console.log("replaceWith");
42+ // console.log("replaceWith");
4343 const newNode = stack.pop();
44− console.log(" newNode =", newNode);
44+ // console.log(" newNode =", newNode);
4545 const oldNode = stack.pop();
46− console.log(" oldNode =", oldNode);
46+ // console.log(" oldNode =", oldNode);
4747 oldNode.replaceWith(newNode);
4848 stack.push(newNode);
4949 return i;
@@ -51,43 +51,43 @@ const OP_TABLE = [
5151
5252 // 3
5353 function setAttribute(stack, mem8, mem32, i) {
54− console.log("setAttribute");
54+ // console.log("setAttribute");
5555 const pointer1 = mem32[i++];
5656 const length1 = mem32[i++];
5757 const name = string(mem8, pointer1, length1);
58− console.log(" name =", name);
58+ // console.log(" name =", name);
5959 const pointer2 = mem32[i++];
6060 const length2 = mem32[i++];
6161 const value = string(mem8, pointer2, length2);
62− console.log(" value =", value);
63− console.log(" top(stack) =", top(stack));
62+ // console.log(" value =", value);
63+ // console.log(" top(stack) =", top(stack));
6464 top(stack).setAttribute(name, value);
6565 return i;
6666 },
6767
6868 // 4
6969 function removeAttribute(stack, mem8, mem32, i) {
70− console.log("removeAttribute");
70+ // console.log("removeAttribute");
7171 const pointer = mem32[i++];
7272 const length = mem32[i++];
7373 const name = string(mem8, pointer, length);
74− console.log(" name =", name);
74+ // console.log(" name =", name);
7575 top(stack).removeAttribute(name);
7676 return i;
7777 },
7878
7979 // 5
8080 function pushFirstChild(stack, mem8, mem32, i) {
81− console.log("pushFirstChild");
82− console.log(" top(stack) =", top(stack));
81+ // console.log("pushFirstChild");
82+ // console.log(" top(stack) =", top(stack));
8383 stack.push(top(stack).firstChild);
8484 return i;
8585 },
8686
8787 // 6
8888 function popPushNextSibling(stack, mem8, mem32, i) {
89− console.log("popPushNextSibling");
90− console.log(" top(stack) =", top(stack));
89+ // console.log("popPushNextSibling");
90+ // console.log(" top(stack) =", top(stack));
9191 const node = stack.pop();
9292 stack.push(node.nextSibling);
9393 return i;
@@ -95,16 +95,16 @@ const OP_TABLE = [
9595
9696 // 7
9797 function pop(stack, mem8, mem32, i) {
98− console.log("pop");
99− console.log(" top(stack) =", top(stack));
98+ // console.log("pop");
99+ // console.log(" top(stack) =", top(stack));
100100 stack.pop();
101101 return i;
102102 },
103103
104104 // 8
105105 function appendChild(stack, mem8, mem32, i) {
106− console.log("appendChild");
107− console.log(" top(stack) =", top(stack));
106+ // console.log("appendChild");
107+ // console.log(" top(stack) =", top(stack));
108108 const child = stack.pop();
109109 top(stack).appendChild(child);
110110 return i;
@@ -112,30 +112,31 @@ const OP_TABLE = [
112112
113113 // 9
114114 function createTextNode(stack, mem8, mem32, i) {
115− console.log("appendChild");
115+ // console.log("appendChild");
116116 const pointer = mem32[i++];
117117 const length = mem32[i++];
118118 const text = string(mem8, pointer, length);
119− console.log(" text =", text);
120− console.log(" top(stack) =", top(stack));
119+ // console.log(" text =", text);
120+ // console.log(" top(stack) =", top(stack));
121121 stack.push(document.createTextNode(text));
122122 return i;
123123 },
124124
125125 // 10
126126 function createElement(stack, mem8, mem32, i) {
127− console.log("createElement");
127+ // console.log("createElement");
128128 const pointer = mem32[i++];
129129 const length = mem32[i++];
130130 const tagName = string(mem8, pointer, length);
131− console.log(" tagName =", tagName);
132− console.log(" top(stack) =", top(stack));
131+ // console.log(" tagName =", tagName);
132+ // console.log(" top(stack) =", top(stack));
133133 stack.push(document.createElement(tagName));
134134 return i;
135135 }
136136];
137137
138−export class ChangeList {
138+// export
139+class ChangeList {
139140 constructor(container) {
140141 this.container = container;
141142 this.ranges = [];
@@ -148,7 +149,7 @@ export class ChangeList {
148149 }
149150
150151 applyChanges(memory) {
151− console.log("======== ChangeList.prototype.applyChanges ==============================");
152+ // console.log("======== ChangeList.prototype.applyChanges ==============================");
152153 if (this.ranges.length == 0) {
153154 return;
154155 }
@@ -171,10 +172,11 @@ export class ChangeList {
171172 const end = (start + len) / 4;
172173 for (let i = start / 4; i < end; ) {
173174 const op = mem32[i++];
174− console.log();
175− console.log(OP_TABLE[op].name);
176− console.log(this.stack);
175+ // console.log();
176+ // console.log(OP_TABLE[op].name);
177+ // console.log(this.stack);
177178 i = OP_TABLE[op](this.stack, mem8, mem32, i);
178179 }
179180 }
180181}
182+window.ChangeList = ChangeList;
src/change_list.rs+16−3
@@ -1,10 +1,12 @@
11use bumpalo::{Bump, BumpAllocSafe};
2+use log::*;
3+use std::sync::Once;
24use wasm_bindgen::prelude::*;
35
46pub mod js {
57 use wasm_bindgen::prelude::*;
68
7− #[wasm_bindgen(module = "dodrio/change-list")]
9+ #[wasm_bindgen]
810 extern "C" {
911 #[derive(Clone, Debug)]
1012 pub type ChangeList;
@@ -12,10 +14,10 @@ pub mod js {
1214 #[wasm_bindgen(constructor)]
1315 pub fn new(container: &web_sys::Node) -> ChangeList;
1416
15− #[wasm_bindgen(method, js_name = addChangeListRange)]
17+ #[wasm_bindgen(structural, method, js_name = addChangeListRange)]
1618 pub fn add_change_list_range(this: &ChangeList, start: usize, len: usize);
1719
18− #[wasm_bindgen(method, js_name = applyChanges)]
20+ #[wasm_bindgen(structural, method, js_name = applyChanges)]
1921 pub fn apply_changes(this: &ChangeList, memory: JsValue);
2022 }
2123}
@@ -27,6 +29,17 @@ pub(crate) struct ChangeList {
2729
2830impl ChangeList {
2931 pub(crate) fn new(container: &web_sys::Node) -> ChangeList {
32+ // XXX: Because wasm-bindgen-test doesn't support third party JS
33+ // dependencies, we can't use `wasm_bindgen(module = "...")` for our
34+ // `ChangeList` JS import. Instead, this *should* be a local JS snippet,
35+ // but that isn't implemented yet:
36+ // https://github.com/rustwasm/rfcs/pull/6
37+ static EVAL: Once = Once::new();
38+ EVAL.call_once(|| {
39+ js_sys::eval(include_str!("../js/change-list.js"))
40+ .expect("should eval change-list.js OK");
41+ });
42+
3043 let bump = Bump::new();
3144 let js = js::ChangeList::new(container);
3245 ChangeList { bump, js }
src/lib.rs+2−5
@@ -1,17 +1,14 @@
11use bumpalo::Bump;
22
3−#[macro_use]
4−mod macros;
5−
63pub mod change_list;
74pub mod node;
85pub mod vdom;
96
10−pub use self::node::{Attribute, Node, NodeRef};
7+pub use self::node::{Attribute, ElementNode, Node, TextNode};
118pub use self::vdom::Vdom;
129
1310pub trait Render {
14− fn render<'a, 'bump>(&'a self, bump: &'bump Bump) -> NodeRef<'bump>
11+ fn render<'a, 'bump>(&'a self, bump: &'bump Bump) -> Node<'bump>
1512 where
1613 'a: 'bump;
1714}
src/macros.rsdeleted−7
@@ -1,7 +0,0 @@
1−macro_rules! debug {
2− ( $fmt:expr $(, $x:expr )* $(,)* ) => {{
3− if cfg!(debug_assertions) {
4− web_sys::console::log_1(&format!($fmt, $($x),*).into());
5− }
6− }}
7−}
src/node.rs+81−64
@@ -1,23 +1,29 @@
1−use bumpalo::BumpAllocSafe;
1+use bumpalo::{Bump, BumpAllocSafe};
22
3−#[derive(Clone, Debug)]
4−pub struct Node<'a, Attributes, Children> {
5− data: NodeData<'a>,
6− attributes: Attributes,
7− children: Children,
3+/// A node is either a text node or an element.
4+#[derive(Debug, Clone)]
5+pub enum Node<'a> {
6+ /// A text node.
7+ Text(TextNode<'a>),
8+
9+ /// An element potentially with attributes and children.
10+ Element(ElementNode<'a>),
811}
912
10−#[derive(Clone, Copy, Debug)]
11−pub enum NodeData<'a> {
12− Element { tag_name: &'a str },
13− Text { text: &'a str },
13+/// Text nodes are just a string of text. They cannot have attributes or
14+/// children.
15+#[derive(Debug, Clone)]
16+pub struct TextNode<'a> {
17+ pub(crate) text: &'a str,
1418}
1519
16−#[derive(Clone, Debug)]
17−pub struct NodeRef<'a> {
18− pub(crate) data: NodeData<'a>,
20+/// Elements have a tag name, zero or more attributes, and zero or more
21+/// children.
22+#[derive(Debug, Clone)]
23+pub struct ElementNode<'a> {
24+ pub(crate) tag_name: &'a str,
1925 pub(crate) attributes: &'a [Attribute<'a>],
20− pub(crate) children: &'a [NodeRef<'a>],
26+ pub(crate) children: &'a [Node<'a>],
2127}
2228
2329#[derive(Clone, Debug)]
@@ -26,69 +32,80 @@ pub struct Attribute<'a> {
2632 pub value: &'a str,
2733}
2834
29−impl<'a, Attributes, Children> BumpAllocSafe for Node<'a, Attributes, Children>
30−where
31− Attributes: BumpAllocSafe,
32− Children: BumpAllocSafe,
33−{
34−}
35+impl<'a> Node<'a> {
36+ /// Is this node a text node?
37+ pub fn is_text(&self) -> bool {
38+ match self {
39+ Node::Text(_) => true,
40+ _ => false,
41+ }
42+ }
3543
36−impl<'a, Attributes, Children> From<&'a Node<'a, Attributes, Children>> for NodeRef<'a>
37−where
38− Attributes: AsRef<[Attribute<'a>]>,
39− Children: AsRef<[NodeRef<'a>]>,
40−{
41− fn from(node: &'a Node<'a, Attributes, Children>) -> NodeRef<'a> {
42− NodeRef {
43− data: node.data,
44− attributes: node.attributes.as_ref(),
45− children: node.children.as_ref(),
44+ /// Is this node an element?
45+ pub fn is_element(&self) -> bool {
46+ match self {
47+ Node::Element { .. } => true,
48+ _ => false,
4649 }
4750 }
4851}
4952
50−impl<'a, Attributes, Children> From<&'a mut Node<'a, Attributes, Children>> for NodeRef<'a>
51−where
52− Attributes: AsRef<[Attribute<'a>]>,
53− Children: AsRef<[NodeRef<'a>]>,
54−{
55− fn from(node: &'a mut Node<'a, Attributes, Children>) -> NodeRef<'a> {
56− NodeRef {
57− data: node.data,
58− attributes: node.attributes.as_ref(),
59− children: node.children.as_ref(),
60− }
53+impl<'a> TextNode<'a> {
54+ /// Get this text node's text content.
55+ pub fn text(&self) -> &'a str {
56+ self.text
57+ }
58+}
59+
60+impl<'a> ElementNode<'a> {
61+ /// Get this element's tag name.
62+ pub fn tag_name(&self) -> &'a str {
63+ self.tag_name
64+ }
65+
66+ /// Get this element's attributes.
67+ pub fn attributes(&self) -> &'a [Attribute<'a>] {
68+ self.attributes
69+ }
70+
71+ /// Get this element's attributes.
72+ pub fn children(&self) -> &'a [Node<'a>] {
73+ self.children
6174 }
6275}
6376
64−impl<'a, Attributes, Children> Node<'a, Attributes, Children>
65−where
66− Attributes: AsRef<[Attribute<'a>]>,
67− Children: AsRef<[NodeRef<'a>]>,
68−{
69− pub fn element(
77+impl<'a> BumpAllocSafe for Node<'a> {}
78+impl<'a> BumpAllocSafe for Attribute<'a> {}
79+
80+impl<'a> Node<'a> {
81+ /// Construct a new text node with the given text.
82+ #[inline]
83+ pub fn text(text: &'a str) -> Node<'a> {
84+ Node::Text(TextNode { text })
85+ }
86+
87+ /// Construct a new element node with the given tag name and children.
88+ #[inline]
89+ pub fn element<Attributes, Children>(
90+ bump: &'a Bump,
7091 tag_name: &'a str,
7192 attributes: Attributes,
7293 children: Children,
73− ) -> Node<'a, Attributes, Children> {
74− let data = NodeData::Element { tag_name };
75− Node {
76− data,
77− attributes,
78− children,
79− }
80− }
81−}
94+ ) -> Node<'a>
95+ where
96+ Attributes: 'a + BumpAllocSafe + AsRef<[Attribute<'a>]>,
97+ Children: 'a + BumpAllocSafe + AsRef<[Node<'a>]>,
98+ {
99+ let children: &'a Children = bump.alloc(children);
100+ let children: &'a [Node<'a>] = children.as_ref();
101+
102+ let attributes: &'a Attributes = bump.alloc(attributes);
103+ let attributes: &'a [Attribute<'a>] = attributes.as_ref();
82104
83−impl<'a> Node<'a, [Attribute<'a>; 0], [NodeRef<'a>; 0]> {
84− pub fn text(text: &'a str) -> Node<'a, [Attribute<'a>; 0], [NodeRef<'a>; 0]> {
85− let data = NodeData::Text { text };
86− let attributes = [];
87− let children = [];
88− Node {
89− data,
105+ Node::Element(ElementNode {
106+ tag_name,
90107 attributes,
91108 children,
92− }
109+ })
93110 }
94111}
src/vdom.rs+37−30
@@ -1,7 +1,8 @@
11use super::change_list::ChangeList;
2−use super::node::{Attribute, Node, NodeData, NodeRef};
2+use super::node::{Attribute, ElementNode, Node, TextNode};
33use super::Render;
44use bumpalo::Bump;
5+use log::*;
56use std::cmp;
67use std::mem;
78
@@ -11,10 +12,10 @@ pub struct Vdom {
1112 container: web_sys::Element,
1213
1314 // Actually a reference into `self.dom_buffers[0]`.
14− current_root: Option<NodeRef<'static>>,
15+ current_root: Option<Node<'static>>,
1516}
1617
17−unsafe fn extend_node_lifetime<'a>(node: NodeRef<'a>) -> NodeRef<'static> {
18+unsafe fn extend_node_lifetime<'a>(node: Node<'a>) -> Node<'static> {
1819 mem::transmute(node)
1920}
2021
@@ -30,7 +31,7 @@ impl Vdom {
3031 container.set_inner_html("");
3132
3233 // Create a dummy `<div/>` in our container.
33− let current_root: NodeRef = dom_buffers[0].alloc(Node::element("div", [], [])).into();
34+ let current_root = Node::element(&dom_buffers[0], "div", [], []);
3435 let current_root = Some(unsafe { extend_node_lifetime(current_root) });
3536 let window = web_sys::window().expect("should have acess to the Window");
3637 let document = window
@@ -85,53 +86,55 @@ impl Vdom {
8586 mem::swap(&mut first[0], &mut second[0]);
8687 }
8788
88− unsafe fn set_current_root(&mut self, current: NodeRef<'static>) {
89+ unsafe fn set_current_root(&mut self, current: Node<'static>) {
8990 debug_assert!(self.current_root.is_none());
9091 self.current_root = Some(current);
9192 }
9293
93− fn diff(&self, old: NodeRef, new: NodeRef) {
94− debug!("---------------------------------------------------------");
95− debug!("dodrio::Vdom::diff");
96− debug!(" old = {:#?}", old);
97− debug!(" new = {:#?}", new);
98− match (new.data, old.data) {
99− (NodeData::Text { text: new_text }, NodeData::Text { text: old_text }) => {
94+ fn diff(&self, old: Node, new: Node) {
95+ // debug!("---------------------------------------------------------");
96+ // debug!("dodrio::Vdom::diff");
97+ // debug!(" old = {:#?}", old);
98+ // debug!(" new = {:#?}", new);
99+ match (&new, old) {
100+ (&Node::Text(TextNode { text: new_text }), Node::Text(TextNode { text: old_text })) => {
100101 debug!(" both are text nodes");
101102 if new_text != old_text {
102103 debug!(" text needs updating");
103104 self.change_list.emit_set_text(new_text);
104105 }
105106 }
106− (NodeData::Text { .. }, NodeData::Element { .. }) => {
107+ (&Node::Text(TextNode { .. }), Node::Element(ElementNode { .. })) => {
107108 debug!(" replacing a text node with an element");
108109 self.create(new);
109110 self.change_list.emit_replace_with();
110111 }
111− (NodeData::Element { .. }, NodeData::Text { .. }) => {
112+ (&Node::Element(ElementNode { .. }), Node::Text(TextNode { .. })) => {
112113 debug!(" replacing an element with a text node");
113114 self.create(new);
114115 self.change_list.emit_replace_with();
115116 }
116117 (
117− NodeData::Element {
118+ &Node::Element(ElementNode {
118119 tag_name: new_tag_name,
119− },
120− NodeData::Element {
120+ attributes: new_attributes,
121+ children: new_children,
122+ }),
123+ Node::Element(ElementNode {
121124 tag_name: old_tag_name,
122− },
125+ attributes: old_attributes,
126+ children: old_children,
127+ }),
123128 ) => {
124129 debug!(" updating an element");
125−
126130 if new_tag_name != old_tag_name {
127131 debug!(" different tag names; creating new element and replacing old element");
128132 self.create(new);
129133 self.change_list.emit_replace_with();
130134 return;
131135 }
132−
133− self.diff_attributes(old.attributes, new.attributes);
134− self.diff_children(old.children, new.children);
136+ self.diff_attributes(old_attributes, new_attributes);
137+ self.diff_children(old_children, new_children);
135138 }
136139 }
137140 }
@@ -165,7 +168,7 @@ impl Vdom {
165168 }
166169 }
167170
168− fn diff_children(&self, old: &[NodeRef], new: &[NodeRef]) {
171+ fn diff_children(&self, old: &[Node], new: &[Node]) {
169172 debug!(" updating children shared by old and new");
170173
171174 let num_children_to_diff = cmp::min(new.len(), old.len());
@@ -217,18 +220,22 @@ impl Vdom {
217220 }
218221 }
219222
220− fn create(&self, node: NodeRef) {
221− debug!("dodrio::Vdom::create({:#?})", node);
222− match node.data {
223− NodeData::Text { text } => {
223+ fn create(&self, node: Node) {
224+ // debug!("dodrio::Vdom::create({:#?})", node);
225+ match node {
226+ Node::Text(TextNode { text }) => {
224227 self.change_list.emit_create_text_node(text);
225228 }
226− NodeData::Element { tag_name } => {
229+ Node::Element(ElementNode {
230+ tag_name,
231+ attributes,
232+ children,
233+ }) => {
227234 self.change_list.emit_create_element(tag_name);
228− for attr in node.attributes {
235+ for attr in attributes {
229236 self.change_list.emit_set_attribute(&attr.name, &attr.value);
230237 }
231− for child in node.children {
238+ for child in children {
232239 self.create(child.clone());
233240 self.change_list.emit_append_child();
234241 }
tests/web.rs+135−3
@@ -2,12 +2,144 @@
22
33#![cfg(target_arch = "wasm32")]
44
5−extern crate wasm_bindgen_test;
5+use bumpalo::Bump;
6+use dodrio::{
7+ node::{Attribute, Node},
8+ vdom::Vdom,
9+ Render,
10+};
11+use log::*;
12+use wasm_bindgen::JsCast;
613use wasm_bindgen_test::*;
714
815wasm_bindgen_test_configure!(run_in_browser);
916
17+fn window() -> web_sys::Window {
18+ web_sys::window().expect("no global `window` exists")
19+}
20+
21+fn document() -> web_sys::Document {
22+ window()
23+ .document()
24+ .expect("should have a document on window")
25+}
26+
27+fn create_element(tag: &str) -> web_sys::Element {
28+ document()
29+ .create_element(tag)
30+ .expect("should create element OK")
31+}
32+
33+fn init_logging() {
34+ use std::sync::Once;
35+ static START: Once = Once::new();
36+ START.call_once(|| {
37+ console_log::init_with_level(Level::Trace).expect("could not initialize console_log");
38+ });
39+}
40+
41+fn assert_rendered<R: Render>(container: &web_sys::Element, r: &R) {
42+ init_logging();
43+ let bump = &Bump::new();
44+ let node = r.render(bump);
45+ let child = container
46+ .first_child()
47+ .expect("container does not have anything rendered into it?");
48+ check_node(&child, &node);
49+
50+ fn stringify_actual_node(n: &web_sys::Node) -> String {
51+ if let Some(el) = n.dyn_ref::<web_sys::Element>() {
52+ el.outer_html()
53+ } else {
54+ format!("#text({:?})", n.text_content())
55+ }
56+ }
57+
58+ fn check_node(actual: &web_sys::Node, expected: &Node) {
59+ info!("check_render:");
60+ info!(" actual = {}", stringify_actual_node(&actual));
61+ info!(" expected = {:?}", expected);
62+ match expected {
63+ Node::Text(text_node) => {
64+ assert_eq!(actual.node_name().to_uppercase(), "#TEXT");
65+ assert_eq!(actual.text_content().unwrap_or_default(), text_node.text());
66+ }
67+ Node::Element(elem) => {
68+ assert_eq!(
69+ actual.node_name().to_uppercase(),
70+ elem.tag_name().to_uppercase()
71+ );
72+ let actual = actual
73+ .dyn_ref::<web_sys::Element>()
74+ .expect("`actual` should be an `Element`");
75+ check_attributes(actual.attributes(), elem.attributes());
76+ check_children(actual.child_nodes(), elem.children());
77+ }
78+ }
79+ }
80+
81+ fn check_attributes(actual: web_sys::NamedNodeMap, expected: &[Attribute]) {
82+ assert_eq!(actual.length(), expected.len() as u32);
83+ for attr in expected {
84+ let actual_attr = actual
85+ .get_named_item(attr.name)
86+ .expect(&format!("should have attribute \"{}\"", attr.name));
87+ assert_eq!(actual_attr.value(), attr.value);
88+ }
89+ }
90+
91+ fn check_children(actual: web_sys::NodeList, expected: &[Node]) {
92+ assert_eq!(actual.length(), expected.len() as u32);
93+ for (i, child) in expected.iter().enumerate() {
94+ let actual_child = actual.item(i as u32).unwrap();
95+ check_node(&actual_child, child);
96+ }
97+ }
98+}
99+
100+#[wasm_bindgen_test]
101+fn render_initial_text() {
102+ struct Hello;
103+
104+ impl Render for Hello {
105+ fn render<'a, 'bump>(&'a self, _bump: &'bump Bump) -> Node<'bump>
106+ where
107+ 'a: 'bump,
108+ {
109+ Node::text("Hello")
110+ }
111+ }
112+
113+ let container = create_element("div");
114+ Vdom::new(&container, &Hello);
115+ assert_rendered(&container, &Hello);
116+}
117+
10118#[wasm_bindgen_test]
11−fn pass() {
12− assert_eq!(1 + 1, 2);
119+fn render_initial_node() {
120+ struct Hello;
121+
122+ impl Render for Hello {
123+ fn render<'a, 'bump>(&'a self, bump: &'bump Bump) -> Node<'bump>
124+ where
125+ 'a: 'bump,
126+ {
127+ Node::element(
128+ bump,
129+ "div",
130+ [Attribute {
131+ name: "id",
132+ value: "hello-world",
133+ }],
134+ [
135+ Node::text("Hello "),
136+ Node::element(bump, "span", [], [Node::text("World!")]),
137+ ],
138+ )
139+ }
140+ }
141+
142+ let container = create_element("div");
143+ Vdom::new(&container, &Hello);
144+ assert_rendered(&container, &Hello);
13145}