Fix initialization order of globals

This commit is contained in:
Olivier Goffart 2021-05-20 09:54:24 +02:00 committed by Olivier Goffart
parent 6d48468823
commit a071738a4a
3 changed files with 53 additions and 4 deletions

View file

@ -15,7 +15,7 @@ use std::rc::Rc;
use crate::api::{Struct, Value}; use crate::api::{Struct, Value};
use crate::eval; use crate::eval;
use sixtyfps_compilerlib::{langtype::Type, object_tree::Component}; use sixtyfps_compilerlib::{generator, langtype::Type, object_tree::Component};
use sixtyfps_corelib::{rtti, Callback, Property}; use sixtyfps_corelib::{rtti, Callback, Property};
pub trait GlobalComponent { pub trait GlobalComponent {
@ -85,8 +85,8 @@ impl GlobalComponentInstance {
} }
} }
let rc = Rc::pin(instance); let rc = Rc::pin(instance);
for (k, expr) in &component.root_element.borrow().bindings { generator::handle_property_bindings_init(component, |_, k, expr| {
if expr.expression.is_constant() { if expr.analysis.borrow().as_ref().map_or(false, |a| a.is_const) {
rc.properties[k].as_ref().set(eval::eval_expression( rc.properties[k].as_ref().set(eval::eval_expression(
&expr.expression, &expr.expression,
&mut eval::EvalLocalContext::from_global(&(rc.clone() as _)), &mut eval::EvalLocalContext::from_global(&(rc.clone() as _)),
@ -103,7 +103,7 @@ impl GlobalComponentInstance {
) )
}); });
} }
} });
rc rc
} }
} }

View file

@ -18,6 +18,7 @@ TestCase := Rectangle {
callback set_a(int); callback set_a(int);
set_a(a) => { Glob.a = a; } set_a(a) => { Glob.a = a; }
property <int> value1: Glob.b; property <int> value1: Glob.b;
property <bool> test: value1 == 3+3;
} }
/* /*

View file

@ -0,0 +1,48 @@
/* LICENSE BEGIN
This file is part of the SixtyFPS Project -- https://sixtyfps.io
Copyright (c) 2020 Olivier Goffart <olivier.goffart@sixtyfps.io>
Copyright (c) 2020 Simon Hausmann <simon.hausmann@sixtyfps.io>
SPDX-License-Identifier: GPL-3.0-only
This file is also available under commercial licensing terms.
Please contact info@sixtyfps.io for more information.
LICENSE END */
// This tests that constant property from global are properly initialized
global Glob := {
property <int> a: 3;
property <int> b: a + 3;
}
global Glob2 := {
property <int> a: other;
property <int> other: 5;
}
TestCase := Rectangle {
r := Rectangle {
property <int> value1: Glob.b;
property <int> value2: true ? Glob2.a : 88;
}
property <bool> test: r.value1 + r.value2 == 3+3 +5;
}
/*
```rust
let instance = TestCase::new();
assert!(instance.get_test());
```
```cpp
auto handle = TestCase::create();
const TestCase &instance = *handle;
assert(instance.get_test());
```
```js
let instance = new sixtyfps.TestCase({});
assert(instance.test);
```
*/