Skip to content

Commit

Permalink
fixed stackoverflow issue caused by default_bag
Browse files Browse the repository at this point in the history
  • Loading branch information
harshdoesdev committed Oct 25, 2023
1 parent 2eef0ed commit 4efeba0
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 97 deletions.
2 changes: 1 addition & 1 deletion ftd/src/executor/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ fn p(s: &str, t: &str, fix: bool, file_location: &std::path::PathBuf) {
let doc = interpret_helper("foo", s).unwrap_or_else(|e| panic!("{:?}", e));
let mut executor =
ftd::executor::ExecuteDoc::from_interpreter(doc).unwrap_or_else(|e| panic!("{:?}", e));
for thing in ftd::interpreter::default::default_bag().keys() {
for thing in ftd::interpreter::default::get_default_bag().keys() {
executor.bag.remove(thing);
}
let expected_json = serde_json::to_string_pretty(&executor).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion ftd/src/interpreter/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl InterpreterState {
fn new(id: String) -> InterpreterState {
InterpreterState {
id,
bag: ftd::interpreter::default::default_bag(),
bag: ftd::interpreter::default::get_default_bag().clone(),
..Default::default()
}
}
Expand Down
2 changes: 1 addition & 1 deletion ftd/src/interpreter/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub fn interpret_helper(
#[track_caller]
fn p(s: &str, t: &str, fix: bool, file_location: &std::path::PathBuf) {
let mut i = interpret_helper("foo", s).unwrap_or_else(|e| panic!("{:?}", e));
for thing in ftd::interpreter::default::default_bag().keys() {
for thing in ftd::interpreter::default::get_default_bag().keys() {
i.data.remove(thing);
}
let expected_json = serde_json::to_string_pretty(&i).unwrap();
Expand Down
151 changes: 79 additions & 72 deletions ftd/src/interpreter/things/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7597,7 +7597,7 @@ pub fn default_bag() -> indexmap::IndexMap<String, ftd::interpreter::Thing> {
value: ftd::interpreter::Value::Record {
name: ftd::interpreter::FTD_COLOR.to_string(),
fields: std::iter::IntoIterator::into_iter([(
"light".to_string(),
"light".to_string().to_string(),
ftd::interpreter::PropertyValue::Value {
value:
ftd::interpreter::Value::String {
Expand Down Expand Up @@ -7879,7 +7879,7 @@ pub fn default_bag() -> indexmap::IndexMap<String, ftd::interpreter::Thing> {
line_number: 0,
},
), (
"dark".to_string(),
"dark".to_string().to_string(),
ftd::interpreter::PropertyValue::Value {
value: ftd::interpreter::Value::String {
text: "#65b693".to_string()
Expand Down Expand Up @@ -9784,75 +9784,7 @@ pub fn default_bag() -> indexmap::IndexMap<String, ftd::interpreter::Thing> {
line_number: 0,
}),
),
// mask-position
(
ftd::interpreter::FTD_MASK_MULTI_DATA.to_string(),
ftd::interpreter::Thing::Record(ftd::interpreter::Record {
name: ftd::interpreter::FTD_MASK_MULTI_DATA.to_string(),
fields: std::iter::IntoIterator::into_iter([
ftd::interpreter::Field {
name: "image".to_string(),
kind: ftd::interpreter::Kind::record(ftd::interpreter::FTD_MASK_IMAGE_DATA)
.into_kind_data(),
mutable: false,
value: None,
access_modifier: Default::default(),
line_number: 0,
},
ftd::interpreter::Field {
name: "size".to_string(),
kind: ftd::interpreter::Kind::or_type(ftd::interpreter::FTD_MASK_SIZE)
.into_kind_data()
.into_optional(),
mutable: false,
value: None,
access_modifier: Default::default(),
line_number: 0,
},
ftd::interpreter::Field {
name: "size-x".to_string(),
kind: ftd::interpreter::Kind::or_type(ftd::interpreter::FTD_MASK_SIZE)
.into_kind_data()
.into_optional(),
mutable: false,
value: None,
access_modifier: Default::default(),
line_number: 0,
},
ftd::interpreter::Field {
name: "size-y".to_string(),
kind: ftd::interpreter::Kind::or_type(ftd::interpreter::FTD_MASK_SIZE)
.into_kind_data()
.into_optional(),
mutable: false,
value: None,
access_modifier: Default::default(),
line_number: 0,
},
ftd::interpreter::Field {
name: "repeat".to_string(),
kind: ftd::interpreter::Kind::or_type(ftd::interpreter::FTD_MASK_REPEAT)
.into_kind_data()
.into_optional(),
mutable: false,
value: None,
access_modifier: Default::default(),
line_number: 0,
},
ftd::interpreter::Field {
name: "position".to_string(),
kind: ftd::interpreter::Kind::or_type(ftd::interpreter::FTD_MASK_POSITION)
.into_kind_data()
.into_optional(),
mutable: false,
value: None,
access_modifier: Default::default(),
line_number: 0,
},
]).collect(),
line_number: 0,
}),
),

(
ftd::interpreter::FTD_MASK_REPEAT.to_string(),
ftd::interpreter::Thing::OrType(ftd::interpreter::OrType {
Expand Down Expand Up @@ -10075,6 +10007,74 @@ pub fn default_bag() -> indexmap::IndexMap<String, ftd::interpreter::Thing> {
line_number: 0,
}),
),
(
ftd::interpreter::FTD_MASK_MULTI_DATA.to_string(),
ftd::interpreter::Thing::Record(ftd::interpreter::Record {
name: ftd::interpreter::FTD_MASK_MULTI_DATA.to_string(),
fields: std::iter::IntoIterator::into_iter([
ftd::interpreter::Field {
name: "image".to_string(),
kind: ftd::interpreter::Kind::record(ftd::interpreter::FTD_MASK_IMAGE_DATA)
.into_kind_data(),
mutable: false,
value: None,
access_modifier: Default::default(),
line_number: 0,
},
ftd::interpreter::Field {
name: "size".to_string(),
kind: ftd::interpreter::Kind::or_type(ftd::interpreter::FTD_MASK_SIZE)
.into_kind_data()
.into_optional(),
mutable: false,
value: None,
access_modifier: Default::default(),
line_number: 0,
},
ftd::interpreter::Field {
name: "size-x".to_string(),
kind: ftd::interpreter::Kind::or_type(ftd::interpreter::FTD_MASK_SIZE)
.into_kind_data()
.into_optional(),
mutable: false,
value: None,
access_modifier: Default::default(),
line_number: 0,
},
ftd::interpreter::Field {
name: "size-y".to_string(),
kind: ftd::interpreter::Kind::or_type(ftd::interpreter::FTD_MASK_SIZE)
.into_kind_data()
.into_optional(),
mutable: false,
value: None,
access_modifier: Default::default(),
line_number: 0,
},
ftd::interpreter::Field {
name: "repeat".to_string(),
kind: ftd::interpreter::Kind::or_type(ftd::interpreter::FTD_MASK_REPEAT)
.into_kind_data()
.into_optional(),
mutable: false,
value: None,
access_modifier: Default::default(),
line_number: 0,
},
ftd::interpreter::Field {
name: "position".to_string(),
kind: ftd::interpreter::Kind::or_type(ftd::interpreter::FTD_MASK_POSITION)
.into_kind_data()
.into_optional(),
mutable: false,
value: None,
access_modifier: Default::default(),
line_number: 0,
},
]).collect(),
line_number: 0,
}),
),
(
ftd::interpreter::FTD_MASK.to_string(),
ftd::interpreter::Thing::OrType(ftd::interpreter::OrType {
Expand All @@ -10096,7 +10096,6 @@ pub fn default_bag() -> indexmap::IndexMap<String, ftd::interpreter::Thing> {
None,
0,
)),
// todo: mode, position, composite, etc.
],
line_number: 0,
}),
Expand All @@ -10105,6 +10104,14 @@ pub fn default_bag() -> indexmap::IndexMap<String, ftd::interpreter::Thing> {
.collect()
}

pub static DEFAULT_BAG: once_cell::sync::OnceCell<
indexmap::IndexMap<String, ftd::interpreter::things::Thing>,
> = once_cell::sync::OnceCell::new();

pub fn get_default_bag() -> &'static indexmap::IndexMap<String, ftd::interpreter::things::Thing> {
DEFAULT_BAG.get_or_init(ftd::interpreter::things::default::default_bag)
}

pub fn image_function() -> ftd::interpreter::ComponentDefinition {
ftd::interpreter::ComponentDefinition {
name: "ftd#image".to_string(),
Expand Down
32 changes: 16 additions & 16 deletions ftd/src/js/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ pub struct CheckBox {

impl CheckBox {
pub fn from(component: &ftd::interpreter::Component) -> CheckBox {
let component_definition = ftd::interpreter::default::default_bag()
let component_definition = ftd::interpreter::default::get_default_bag()
.get("ftd#checkbox")
.unwrap()
.clone()
Expand Down Expand Up @@ -233,7 +233,7 @@ pub struct TextInput {

impl TextInput {
pub fn from(component: &ftd::interpreter::Component) -> TextInput {
let component_definition = ftd::interpreter::default::default_bag()
let component_definition = ftd::interpreter::default::get_default_bag()
.get("ftd#text-input")
.unwrap()
.clone()
Expand Down Expand Up @@ -377,7 +377,7 @@ pub struct Iframe {

impl Iframe {
pub fn from(component: &ftd::interpreter::Component) -> Iframe {
let component_definition = ftd::interpreter::default::default_bag()
let component_definition = ftd::interpreter::default::get_default_bag()
.get("ftd#iframe")
.unwrap()
.clone()
Expand Down Expand Up @@ -495,7 +495,7 @@ pub struct Code {

impl Code {
pub fn from(component: &ftd::interpreter::Component, _doc: &ftd::interpreter::TDoc) -> Code {
let component_definition = ftd::interpreter::default::default_bag()
let component_definition = ftd::interpreter::default::get_default_bag()
.get("ftd#code")
.unwrap()
.clone()
Expand Down Expand Up @@ -619,7 +619,7 @@ pub struct Image {

impl Image {
pub fn from(component: &ftd::interpreter::Component) -> Image {
let component_definition = ftd::interpreter::default::default_bag()
let component_definition = ftd::interpreter::default::get_default_bag()
.get("ftd#image")
.unwrap()
.clone()
Expand Down Expand Up @@ -718,7 +718,7 @@ pub struct Video {

impl Video {
pub fn from(component: &ftd::interpreter::Component) -> Video {
let component_definition = ftd::interpreter::default::default_bag()
let component_definition = ftd::interpreter::default::get_default_bag()
.get("ftd#video")
.unwrap()
.clone()
Expand Down Expand Up @@ -1124,7 +1124,7 @@ impl InheritedProperties {

impl Text {
pub fn from(component: &ftd::interpreter::Component) -> Text {
let component_definition = ftd::interpreter::default::default_bag()
let component_definition = ftd::interpreter::default::get_default_bag()
.get("ftd#text")
.unwrap()
.clone()
Expand Down Expand Up @@ -1190,7 +1190,7 @@ impl Text {

impl Integer {
pub fn from(component: &ftd::interpreter::Component) -> Integer {
let component_definition = ftd::interpreter::default::default_bag()
let component_definition = ftd::interpreter::default::get_default_bag()
.get("ftd#integer")
.unwrap()
.clone()
Expand Down Expand Up @@ -1255,7 +1255,7 @@ impl Integer {

impl Decimal {
pub fn from(component: &ftd::interpreter::Component) -> Decimal {
let component_definition = ftd::interpreter::default::default_bag()
let component_definition = ftd::interpreter::default::get_default_bag()
.get("ftd#decimal")
.unwrap()
.clone()
Expand Down Expand Up @@ -1320,7 +1320,7 @@ impl Decimal {

impl Boolean {
pub fn from(component: &ftd::interpreter::Component) -> Boolean {
let component_definition = ftd::interpreter::default::default_bag()
let component_definition = ftd::interpreter::default::get_default_bag()
.get("ftd#boolean")
.unwrap()
.clone()
Expand Down Expand Up @@ -1385,7 +1385,7 @@ impl Boolean {

impl Document {
pub fn from(component: &ftd::interpreter::Component) -> Document {
let component_definition = ftd::interpreter::default::default_bag()
let component_definition = ftd::interpreter::default::get_default_bag()
.get("ftd#document")
.unwrap()
.clone()
Expand Down Expand Up @@ -1636,7 +1636,7 @@ impl DocumentMeta {

impl Column {
pub fn from(component: &ftd::interpreter::Component) -> Column {
let component_definition = ftd::interpreter::default::default_bag()
let component_definition = ftd::interpreter::default::get_default_bag()
.get("ftd#column")
.unwrap()
.clone()
Expand Down Expand Up @@ -1702,7 +1702,7 @@ impl Column {

impl Row {
pub fn from(component: &ftd::interpreter::Component) -> Row {
let component_definition = ftd::interpreter::default::default_bag()
let component_definition = ftd::interpreter::default::get_default_bag()
.get("ftd#row")
.unwrap()
.clone()
Expand Down Expand Up @@ -1768,7 +1768,7 @@ impl Row {

impl ContainerElement {
pub fn from(component: &ftd::interpreter::Component) -> ContainerElement {
let component_definition = ftd::interpreter::default::default_bag()
let component_definition = ftd::interpreter::default::get_default_bag()
.get("ftd#container")
.unwrap()
.clone()
Expand Down Expand Up @@ -1836,7 +1836,7 @@ pub struct Device {

impl Device {
pub fn from(component: &ftd::interpreter::Component, device: &str) -> Device {
let component_definition = ftd::interpreter::default::default_bag()
let component_definition = ftd::interpreter::default::get_default_bag()
.get(device)
.unwrap()
.clone()
Expand Down Expand Up @@ -2021,7 +2021,7 @@ pub struct Rive {

impl Rive {
pub fn from(component: &ftd::interpreter::Component) -> Rive {
let component_definition = ftd::interpreter::default::default_bag()
let component_definition = ftd::interpreter::default::get_default_bag()
.get("ftd#rive")
.unwrap()
.clone()
Expand Down
10 changes: 5 additions & 5 deletions ftd/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ pub fn all_js_without_test(package_name: &str) -> String {
/// This returns asts of things present in `ftd` module or `default_bag`
pub fn default_bag_into_js_ast() -> Vec<fastn_js::Ast> {
let mut ftd_asts = vec![];
let bag = ftd::interpreter::default::default_bag();
let bag = ftd::interpreter::default::get_default_bag();
let doc = ftd::interpreter::TDoc {
name: "",
aliases: &ftd::interpreter::default::default_aliases(),
bag: ftd::interpreter::BagOrState::Bag(&bag),
bag: ftd::interpreter::BagOrState::Bag(bag),
};
let mut export_asts = vec![];
for thing in ftd::interpreter::default::default_bag().values() {
for thing in ftd::interpreter::default::get_default_bag().values() {
if let ftd::interpreter::Thing::Variable(v) = thing {
ftd_asts.push(v.to_ast(&doc, None, &mut false));
} else if let ftd::interpreter::Thing::Function(f) = thing {
Expand Down Expand Up @@ -98,15 +98,15 @@ pub fn document_into_js_ast(document: ftd::interpreter::Document) -> JSAstData {
&doc,
&mut has_rive_components,
)];
let default_thing_name = ftd::interpreter::default::default_bag()
let default_thing_name = ftd::interpreter::default::get_default_bag()
.into_iter()
.map(|v| v.0)
.collect_vec();

let mut export_asts = vec![];

for (key, thing) in document.data.iter() {
if default_thing_name.contains(key) {
if default_thing_name.contains(&key) {
continue;
}
if let ftd::interpreter::Thing::Component(c) = thing {
Expand Down
Loading

0 comments on commit 4efeba0

Please sign in to comment.