Lumen.Web.Node.insert_before/3 native_implemented_function
-use std::sync::Arc;
+//! Unlike [Node.appendChild](https://developer.mozilla.org/en-US/docs/Web/API/Node/appendChild),
+//! this does not return the appended child as that is prone to errors with chaining.
+//!
+//! ```elixir
+//! case Lumen.Web.Node.insert_before(parent, new_child, reference_child) do
+//! {:ok, inserted_child} -> ...
+//! {:error, :hierarchy_request} -> ...
+//! {:error, :wrong_document} -> ...
+//! {:error, :no_modification_allowed} -> ...
+//! {:error, :not_found} -> ...
+//! {:error, :not_supported} -> ...
+//! end
+//! ```
use wasm_bindgen::JsCast;
use web_sys::DomException;
use liblumen_alloc::erts::exception;
-use liblumen_alloc::erts::exception::system::Alloc;
-use liblumen_alloc::erts::process::code::stack::frame::{Frame, Placement};
-use liblumen_alloc::erts::process::code::{self, result_from_exception};
use liblumen_alloc::erts::process::Process;
-use liblumen_alloc::erts::term::{atom_unchecked, Atom, Term};
-use liblumen_alloc::erts::ModuleFunctionArity;
+use liblumen_alloc::erts::term::{atom_unchecked, Term};
+
+use lumen_runtime_macros::native_implemented_function;
use crate::node::node_from_term;
use crate::{error, ok_tuple};
-/// Unlike [Node.appendChild](https://developer.mozilla.org/en-US/docs/Web/API/Node/appendChild),
-/// this does not return the appended child as that is prone to errors with chaining.
-///
-/// ```elixir
-/// case Lumen.Web.Node.insert_before(parent, new_child, reference_child) do
-/// {:ok, inserted_child} -> ...
-/// {:error, :hierarchy_request} -> ...
-/// {:error, :wrong_document} -> ...
-/// {:error, :no_modification_allowed} -> ...
-/// {:error, :not_found} -> ...
-/// {:error, :not_supported} -> ...
-/// end
-/// ```
-pub fn place_frame_with_arguments(
- process: &Process,
- placement: Placement,
- parent: Term,
- new_child: Term,
- reference_child: Term,
-) -> Result<(), Alloc> {
- process.stack_push(reference_child)?;
- process.stack_push(new_child)?;
- process.stack_push(parent)?;
- process.place_frame(frame(), placement);
-
- Ok(())
-}
-
-// Private
-
-pub fn code(arc_process: &Arc<Process>) -> code::Result {
- arc_process.reduce();
-
- let parent = arc_process.stack_pop().unwrap();
- let new_child = arc_process.stack_pop().unwrap();
- let reference_child = arc_process.stack_pop().unwrap();
-
- match native(arc_process, parent, new_child, reference_child) {
- Ok(ok_or_error_tuple) => {
- arc_process.return_from_call(ok_or_error_tuple)?;
-
- Process::call_code(arc_process)
- }
- Err(exception) => result_from_exception(arc_process, exception),
- }
-}
-
-fn frame() -> Frame {
- Frame::new(module_function_arity(), code)
-}
-
-fn function() -> Atom {
- Atom::try_from_str("insert_before").unwrap()
-}
-
-fn module_function_arity() -> Arc<ModuleFunctionArity> {
- Arc::new(ModuleFunctionArity {
- module: super::module(),
- function: function(),
- arity: 3,
- })
-}
-
+#[native_implemented_function(insert_before/3)]
fn native(
process: &Process,
parent: Term,
