make interpreter extend the interpreter core class

This commit is contained in:
Evan Almloff committed 2024-02-28 11:22:15 -06:00
1 parent f196514b4f
commit 8a969a26dc
4 files changed
+211 -219

No files matched your search

Generated
+152 -154
View File
File diff suppressed because it is too large. Load diff
+1 -1
View File
@@ -17,7 +17,7 @@ web-sys = { version = "0.3.56", optional = true, features = [
"Element", "Element",
"Node", "Node",
] } ] }
sledgehammer_bindgen = { version = "0.4.0", default-features = false, optional = true } sledgehammer_bindgen = { git = "https://github.com/ealmloff/sledgehammer_bindgen", default-features = false, optional = true }
sledgehammer_utils = { version = "0.2", optional = true } sledgehammer_utils = { version = "0.2", optional = true }
serde = { version = "1.0", features = ["derive"], optional = true } serde = { version = "1.0", features = ["derive"], optional = true }
+58 -63
View File
@@ -4,42 +4,37 @@ use js_sys::Function;
#[cfg(feature = "webonly")] #[cfg(feature = "webonly")]
use web_sys::Node; use web_sys::Node;
#[cfg(feature = "webonly")]
use wasm_bindgen::prelude::wasm_bindgen;
use sledgehammer_bindgen::bindgen; use sledgehammer_bindgen::bindgen;
pub const SLEDGEHAMMER_JS: &str = GENERATED_JS; pub const SLEDGEHAMMER_JS: &str = GENERATED_JS;
#[cfg(feature = "webonly")] #[cfg(feature = "webonly")]
#[wasm_bindgen::prelude::wasm_bindgen(inline_js = r#" #[wasm_bindgen(module = "src/js/web.js")]
export function save_template(channel, nodes, tmpl_id) {
channel.save_template(nodes, tmpl_id);
}
export function hydrate(channel, ids) {
channel.hydrate(ids);
}
export function get_node(channel, id) {
return channel.get_node(id);
}
export function initialize(channel, root, handler) {
channel.initialize(root, handler);
}
"#)]
extern "C" { extern "C" {
pub fn save_template(channel: &JSChannel, nodes: Vec<Node>, tmpl_id: u16); #[wasm_bindgen]
pub type Interpreter;
pub fn hydrate(channel: &JSChannel, ids: Vec<u32>); #[wasm_bindgen(method)]
pub fn save_template(this: &Interpreter, nodes: Vec<Node>, tmpl_id: u16);
pub fn get_node(channel: &JSChannel, id: u32) -> Node; #[wasm_bindgen(method)]
pub fn hydrate(this: &Interpreter, ids: Vec<u32>);
pub fn initialize(channel: &JSChannel, root: Node, handler: &Function); #[wasm_bindgen(method)]
pub fn get_node(this: &Interpreter, id: u32) -> Node;
#[wasm_bindgen(method)]
pub fn initialize(this: &Interpreter, root: Node, handler: &Function);
} }
#[bindgen(module)] #[bindgen(module)]
mod js { mod js {
// Load in the JavaScript file with all the imports /// The interpreter extends the core interpreter which contains the state for the interpreter along with some functions that all platforms use like `AppendChildren`.
const JS_FILE: &str = "./src/gen/interpreter.js"; #[extends(Interpreter)]
pub struct InterpreterInterface;
// Boot the interpreter and attach us some bindings!
const JS: &str = r#""#;
fn mount_to_root() { fn mount_to_root() {
"{this.AppendChildren(this.root, this.stack.length-1);}" "{this.AppendChildren(this.root, this.stack.length-1);}"
@@ -147,12 +142,12 @@ mod js {
*/ */
fn append_children_to_top(many: u16) { fn append_children_to_top(many: u16) {
"{ "{
let root = this.stack[this.stack.length-many-1]; let root = this.stack[this.stack.length-many-1];
this.els = this.stack.splice(this.stack.length-many); this.els = this.stack.splice(this.stack.length-many);
for (let k = 0; k < many; k++) { for (let k = 0; k < many; k++) {
root.appendChild(this.els[k]); root.appendChild(this.els[k]);
} }
}" }"
} }
fn set_top_attribute(field: &str<u8, attr>, value: &str, ns: &str<u8, ns_cache>) { fn set_top_attribute(field: &str<u8, attr>, value: &str, ns: &str<u8, ns_cache>) {
"{this.setAttributeInner(this.stack[this.stack.length-1], $field$, $value$, $ns$);}" "{this.setAttributeInner(this.stack[this.stack.length-1], $field$, $value$, $ns$);}"
@@ -171,47 +166,47 @@ mod js {
} }
fn foreign_event_listener(event: &str<u8, evt>, id: u32, bubbles: u8) { fn foreign_event_listener(event: &str<u8, evt>, id: u32, bubbles: u8) {
r#" r#"
bubbles = bubbles == 1; bubbles = bubbles == 1;
let node = this.nodes[id]; let node = this.nodes[id];
if(node.listening){ if(node.listening){
node.listening += 1; node.listening += 1;
} else { } else {
node.listening = 1; node.listening = 1;
} }
node.setAttribute('data-dioxus-id', `\${id}`); node.setAttribute('data-dioxus-id', `\${id}`);
const event_name = $event$; const event_name = $event$;
// if this is a mounted listener, we send the event immediately // if this is a mounted listener, we send the event immediately
if (event_name === "mounted") { if (event_name === "mounted") {
window.ipc.postMessage( window.ipc.postMessage(
this.serializeIpcMessage("user_event", { this.serializeIpcMessage("user_event", {
name: event_name, name: event_name,
element: id, element: id,
data: null, data: null,
bubbles, bubbles,
}) })
); );
} else { } else {
this.listeners.create(event_name, node, bubbles, (event) => { this.listeners.create(event_name, node, bubbles, (event) => {
this.handler(event, event_name, bubbles); this.handler(event, event_name, bubbles);
}); });
}"# }"#
} }
fn assign_id_ref(array: &[u8], id: u32) { fn assign_id_ref(array: &[u8], id: u32) {
"{this.nodes[$id$] = this.LoadChild($array$);}" "{this.nodes[$id$] = this.LoadChild($array$);}"
} }
fn hydrate_text_ref(array: &[u8], value: &str, id: u32) { fn hydrate_text_ref(array: &[u8], value: &str, id: u32) {
r#"{ r#"{
let node = this.LoadChild($array$); let node = this.LoadChild($array$);
if (node.nodeType == node.TEXT_NODE) { if (node.nodeType == node.TEXT_NODE) {
node.textContent = value; node.textContent = value;
} else { } else {
let text = document.createTextNode(value); let text = document.createTextNode(value);
node.replaceWith(text); node.replaceWith(text);
node = text; node = text;
} }
this.nodes[$id$] = node; this.nodes[$id$] = node;
}"# }"#
} }
fn replace_placeholder_ref(array: &[u8], n: u16) { fn replace_placeholder_ref(array: &[u8], n: u16) {
"{this.els = this.stack.splice(this.stack.length - $n$); let node = this.LoadChild($array$); node.replaceWith(...this.els);}" "{this.els = this.stack.splice(this.stack.length - $n$); let node = this.LoadChild($array$); node.replaceWith(...this.els);}"
-1
View File
@@ -43,7 +43,6 @@ mod hot_reload;
#[cfg(feature = "hydrate")] #[cfg(feature = "hydrate")]
mod rehydrate; mod rehydrate;
/// Runs the app as a future that can be scheduled around the main thread. /// Runs the app as a future that can be scheduled around the main thread.
/// ///
/// Polls futures internal to the VirtualDOM, hence the async nature of this function. /// Polls futures internal to the VirtualDOM, hence the async nature of this function.