Skip to content

Commit

Permalink
update examples and make flush autocomplete
Browse files Browse the repository at this point in the history
  • Loading branch information
ealmloff committed Dec 3, 2022
1 parent b0692af commit 964ddd1
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 98 deletions.
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ ux = "0.1.5"

[dev-dependencies]
sledgehammer_utils = "0.1.0"
wasm-bindgen = "0.2"
wasm-bindgen = "0.2"

[dev-dependencies.web-sys]
version = "0.3"
features = ["Node", "console"]
107 changes: 61 additions & 46 deletions examples/test.rs
Original file line number Diff line number Diff line change
@@ -1,67 +1,82 @@
use sledgehammer_bindgen::bindgen;
use sledgehammer_utils::ux::u24;
use web_sys::{console, Node};

fn main() {
#[bindgen]
extern "C" {
fn initialize() {
"let nodes = [document.getElementById(\"main\")];"
}
fn set_attribute(id: u24, name: &str<u8, name_cache>, value: &str<u16>) -> i32 {
"nodes[$id$].setAttribute($name$, $value$);"
}
fn set_attribute_ns(
id: u24,
name: &str<u8, name_cache>,
namespace: &str<u8, ns_cache>,
value: &str<u16>,
) {
"nodes[$id$].setAttributeNS($namespace$, $name$, $value$);"
mod js {
const JS: &str = r#"const nodes = [document.getElementById("main")];
export function get_node(id){
return nodes[id];
}"#;

extern "C" {
#[wasm_bindgen]
fn get_node(id: u16) -> Node;
}
fn create_element(id: u24, name: &str<u8, name_cache>) {

fn create_element(id: u16, name: &'static str<u8, name_cache>) {
"nodes[$id$]=document.createElement($name$);"
}
fn create_element_ns(id: u24, name: &str<u8, name_cache>, namespace: &str<u8, ns_cache>) {
"nodes[$id$]=document.createElementNS($namespace$, $name$);"
}
fn create_text_node(id: u24, text: &str<u16>) {
"nodes[$id$]=document.createTextNode($text$);"

fn create_element_ns(
id: u16,
name: &'static str<u8, name_cache>,
ns: &'static str<u8, ns_cache>,
) {
"nodes[$id$]=document.createElementNS($ns$,$name$);"
}
fn append_child(parent: u24, child: u24) {
"nodes[$parent$].appendChild(nodes[$child$]);"

fn set_attribute(id: u16, name: &'static str<u8, name_cache>, val: impl Writable<u8>) {
"nodes[$id$].setAttribute($name$,$val$);"
}
fn remove_child(parent: u24, child: u24) {
"nodes[$parent$].removeChild(nodes[$child$]);"

fn remove_attribute(id: u16, name: &'static str<u8, name_cache>) {
"nodes[$id$].removeAttribute($name$);"
}
fn insert_before(parent: u24, child: u24) {
"nodes[$parent$].before(nodes[$child$]);"

fn append_child(id: u16, id2: u16) {
"nodes[$id$].appendChild(nodes[$id2$]);"
}
fn replace_child(parent: u24, child: u24) {
"nodes[$parent$].after(nodes[$child$]);"

fn insert_before(parent: u16, id: u16, id2: u16) {
"nodes[$parent$].insertBefore(nodes[$id$],nodes[$id2$]);"
}
fn set_text_content(id: u24, text: &str<u16>) {
"nodes[$id$].textContent = $text$;"

fn set_text(id: u16, text: impl Writable<u8>) {
"nodes[$id$].textContent=$text$;"
}
fn remove(id: u24) {

fn remove(id: u16) {
"nodes[$id$].remove();"
}
fn replace(id: u24, new_id: u24) {
"nodes[$id$].replaceWith(nodes[$new_id$]);"

fn replace(id: u16, id2: u16) {
"nodes[$id$].replaceWith(nodes[$id2$]);"
}

fn clone(id: u16, id2: u16) {
"nodes[$id2$]=nodes[$id$].cloneNode(true);"
}
fn clone(id: u24, into: u24) {
"nodes[$into$]= nodes[$id$].cloneNode();"

fn first_child(id: u16) {
"node[id]=node[id].firstChild;"
}
fn test(path: &[u8<u8>], id: u24) {
"console.log($path$, $id$);"

fn next_sibling(id: u16) {
"node[id]=node[id].nextSibling;"
}
}

let mut channel1 = Channel::default();
channel1.create_element(1u8.into(), "div");
channel1.create_element_ns(2u8.into(), "svg", "http://www.w3.org/2000/svg");
let mut channel2 = Channel::default();
channel2.append(channel1);
channel2.append_child(0u8.into(), 1u8.into());
channel2.append_child(1u8.into(), 2u8.into());
channel2.test(&[1, 2, 3], 4u8.into());
channel2.flush();
let main = 0;
let node1 = 1;
let node2 = 2;
channel1.create_element(node1, "div");
channel1.create_element(node2, "span");
channel1.append_child(node1, node2);
channel1.set_text(node2, "Hello World!");
channel1.append_child(main, node1);
channel1.flush();

console::log_1(&get_node(0).into());
}
104 changes: 53 additions & 51 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ use syn::{ForeignItemFn, ItemFn, TypeParamBound};
/// // initialize is a special function that is called when the js is initialized. It can be used to set up the js environment.
/// // this is the js code that is executed when the js is loaded.
/// const JS: &str = r#"
/// const nodes = ["hello"];
/// const text = ["hello"];
///
/// export function get(id) {
/// console.log("got", nodes[id]);
/// return nodes[id];
/// console.log("got", text[id]);
/// return text[id];
/// }
/// "#;
///
Expand Down Expand Up @@ -436,66 +436,68 @@ impl Bindings {
}
}

#[cfg(target_family = "wasm")]
pub fn flush(&mut self){
self.encode_op(#end_msg);
let msg_ptr = self.msg.as_ptr();
let str_ptr = self.str_buffer.as_ptr();
// the pointer will only be updated when the message vec is resized, so we have a flag to check if the pointer has changed to avoid unnecessary decoding
if unsafe { *METADATA } == 255 {
// this is the first message, so we need to encode all the metadata
unsafe {
create(DATA.as_mut_ptr() as usize);
*DATA_PTR = msg_ptr;
*STR_PTR = str_ptr;
*METADATA = 7;
}
} else {
if unsafe { *DATA_PTR } != msg_ptr {
#[cfg(target_family = "wasm")]
{
self.encode_op(#end_msg);
let msg_ptr = self.msg.as_ptr();
let str_ptr = self.str_buffer.as_ptr();
// the pointer will only be updated when the message vec is resized, so we have a flag to check if the pointer has changed to avoid unnecessary decoding
if unsafe { *METADATA } == 255 {
// this is the first message, so we need to encode all the metadata
unsafe {
create(DATA.as_mut_ptr() as usize);
*DATA_PTR = msg_ptr;
// the first bit encodes if the msg pointer has changed
*METADATA = 1;
*STR_PTR = str_ptr;
*METADATA = 7;
}
} else {
unsafe {
// the first bit encodes if the msg pointer has changed
*METADATA = 0;
if unsafe { *DATA_PTR } != msg_ptr {
unsafe {
*DATA_PTR = msg_ptr;
// the first bit encodes if the msg pointer has changed
*METADATA = 1;
}
} else {
unsafe {
// the first bit encodes if the msg pointer has changed
*METADATA = 0;
}
}
}
if unsafe { *STR_PTR } != str_ptr {
unsafe {
*STR_PTR = str_ptr;
// the second bit encodes if the str pointer has changed
*METADATA |= 2;
if unsafe { *STR_PTR } != str_ptr {
unsafe {
*STR_PTR = str_ptr;
// the second bit encodes if the str pointer has changed
*METADATA |= 2;
}
}
}
}
unsafe {
if !self.str_buffer.is_empty() {
// the third bit encodes if there is any strings
*METADATA |= 4;
*STR_LEN_PTR = self.str_buffer.len() as u32;
if *STR_LEN_PTR < 100 {
// the fourth bit encodes if the strings are entirely ascii and small
*METADATA |= (self.str_buffer.is_ascii() as u8) << 3;
unsafe {
if !self.str_buffer.is_empty() {
// the third bit encodes if there is any strings
*METADATA |= 4;
*STR_LEN_PTR = self.str_buffer.len() as u32;
if *STR_LEN_PTR < 100 {
// the fourth bit encodes if the strings are entirely ascii and small
*METADATA |= (self.str_buffer.is_ascii() as u8) << 3;
}
}
}
}
let new_mem_size = core::arch::wasm32::memory_size(0);
unsafe{
// we need to update the memory if the memory has grown
if new_mem_size != LAST_MEM_SIZE {
LAST_MEM_SIZE = new_mem_size;
update_memory(wasm_bindgen::memory());
let new_mem_size = core::arch::wasm32::memory_size(0);
unsafe{
// we need to update the memory if the memory has grown
if new_mem_size != LAST_MEM_SIZE {
LAST_MEM_SIZE = new_mem_size;
update_memory(wasm_bindgen::memory());
}
}
}

run();
self.current_op_batch_idx = 0;
self.current_op_byte_idx = #reads_per_u32;
self.str_buffer.clear();
self.msg.clear();
run();
self.current_op_batch_idx = 0;
self.current_op_byte_idx = #reads_per_u32;
self.str_buffer.clear();
self.msg.clear();
}
}

#(#methods)*
Expand Down

0 comments on commit 964ddd1

Please sign in to comment.