Skip to content

Commit

Permalink
Wip fastn_compiler::Compiler::js
Browse files Browse the repository at this point in the history
  • Loading branch information
Arpita-Jaiswal committed Nov 20, 2024
1 parent 87c0ba9 commit 5f1f6ae
Show file tree
Hide file tree
Showing 8 changed files with 515 additions and 35 deletions.
53 changes: 53 additions & 0 deletions fastn-resolved-to-js/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,3 +308,56 @@ impl<T: std::cmp::PartialEq> VecMap<T> {
values
}
}

pub fn default_bag_into_js_ast(doc: &dyn fastn_resolved::tdoc::TDoc) -> Vec<fastn_js::Ast> {
let mut ftd_asts = vec![];

let mut export_asts = vec![];
for thing in fastn_builtins::get_default_bag().values() {
if let fastn_resolved::Definition::Variable(v) = thing {
ftd_asts.push(v.to_ast(doc, None, &mut false));
} else if let fastn_resolved::Definition::Function(f) = thing {
if f.external_implementation {
continue;
}
ftd_asts.push(f.to_ast(doc));
} else if let fastn_resolved::Definition::Export { from, to, .. } = thing {
export_asts.push(fastn_js::Ast::Export {
from: from.to_string(),
to: to.to_string(),
})
}
}

// Global default inherited variable
ftd_asts.push(fastn_js::Ast::StaticVariable(fastn_js::StaticVariable {
name: "inherited".to_string(),
value: fastn_js::SetPropertyValue::Value(fastn_js::Value::Record {
fields: vec![
(
"colors".to_string(),
fastn_js::SetPropertyValue::Reference(
"ftd#default-colors__DOT__getClone()__DOT__setAndReturn\
(\"is_root\"__COMMA__\
true)"
.to_string(),
),
),
(
"types".to_string(),
fastn_js::SetPropertyValue::Reference(
"ftd#default-types__DOT__getClone()__DOT__setAndReturn\
(\"is_root\"__COMMA__\
true)"
.to_string(),
),
),
],
other_references: vec![],
}),
prefix: None,
}));

ftd_asts.extend(export_asts);
ftd_asts
}
23 changes: 1 addition & 22 deletions ftd/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ pub fn document_into_js_ast(document: ftd::interpreter::Document) -> JSAstData {

// Check if document tree has rive. This is used to add rive script.
let mut has_rive_components = false;
let mut document_asts = vec![ftd::js::from_tree(
let mut document_asts = vec![fastn_resolved_to_js::from_tree(
document.tree.as_slice(),
&doc,
&mut has_rive_components,
Expand Down Expand Up @@ -172,24 +172,3 @@ pub fn document_into_js_ast(document: ftd::interpreter::Document) -> JSAstData {
scripts,
}
}

pub fn from_tree(
tree: &[fastn_resolved::ComponentInvocation],
doc: &dyn fastn_resolved::tdoc::TDoc,
has_rive_components: &mut bool,
) -> fastn_js::Ast {
use fastn_resolved_to_js::ComponentExt;

let mut statements = vec![];
for (index, component) in tree.iter().enumerate() {
statements.extend(component.to_component_statements(
fastn_js::COMPONENT_PARENT,
index,
doc,
&fastn_resolved_to_js::ResolverData::none(),
false,
has_rive_components,
))
}
fastn_js::component0(fastn_js::MAIN_FUNCTION, statements)
}
Loading

0 comments on commit 5f1f6ae

Please sign in to comment.